From 86fc8cf9d5ad864a3328cabe7831b5d55c3df94f Mon Sep 17 00:00:00 2001 From: mtg Date: Thu, 26 Nov 2015 11:52:54 +0100 Subject: [PATCH 1/2] Support simple latex output via \rule (inspired by qrcode package by Anders Hendrickson --- qrencode.php | 29 +++++++++++++++++++++++++++++ qrvect.php | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/qrencode.php b/qrencode.php index 249235c..9d35852 100644 --- a/qrencode.php +++ b/qrencode.php @@ -299,6 +299,13 @@ public static function eps($text, $outfile = false, $level = QR_ECLEVEL_L, $size $enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color, $cmyk); return $enc->encodeEPS($text, $outfile, $saveandprint=false); } + + //---------------------------------------------------------------------- + public static function latex_rules($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $writeheader=true, $back_color = 0xFFFFFF, $fore_color = 0x000000, $cmyk = false) + { + $enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color, $cmyk); + return $enc->encodeLatex_Rules($text, $outfile, $size . "mm", $writeheader); + } //---------------------------------------------------------------------- public static function svg($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false, $back_color = 0xFFFFFF, $fore_color = 0x000000) @@ -543,6 +550,28 @@ public function encodeEPS($intext, $outfile = false,$saveandprint=false) } } + //---------------------------------------------------------------------- + public function encodeLatex_Rules($intext, $outfile = false, $blocksize = "2mm", $writeheader = true) + { + try { + + ob_start(); + $tab = $this->encode($intext); + $err = ob_get_contents(); + ob_end_clean(); + + if ($err != '') + QRtools::log($outfile, $err); + + return QRvect::latex_rules($tab, $outfile, $blocksize, $writeheader); + + } catch (Exception $e) { + + QRtools::log($outfile, $e->getMessage()); + + } + } + //---------------------------------------------------------------------- public function encodeSVG($intext, $outfile = false,$saveandprint=false) { diff --git a/qrvect.php b/qrvect.php index 113fd12..352932c 100644 --- a/qrvect.php +++ b/qrvect.php @@ -26,7 +26,6 @@ class QRvect { - //---------------------------------------------------------------------- public static function eps($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE, $back_color = 0xFFFFFF, $fore_color = 0x000000, $cmyk = false) { $vect = self::vectEPS($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color, $cmyk); @@ -46,7 +45,7 @@ public static function eps($frame, $filename = false, $pixelPerPoint = 4, $outer } } } - + //---------------------------------------------------------------------- private static function vectEPS($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000, $cmyk = false) @@ -135,6 +134,54 @@ private static function vectEPS($frame, $pixelPerPoint = 4, $outerFrame = 4, $ba return $output; } + + //---------------------------------------------------------------------- + public static function latex_rules($frame, $filename = false, $blocksize = "2mm", $writeheader = true) + { + $output = ""; + if($writeheader) + { + $output .= "\\documentclass{article}\n"; + $output .= "\\begin{document}\n"; + $output .= "\\newlength\\qrmodulesize\n"; + $output .= "\\newlength\qrminipagewidth\n"; + } + + $h = count($frame); + $w = strlen($frame[0]); + + $output .= "\\setlength{\\qrmodulesize}{" . $blocksize . "}\n"; + $output .= "\\setlength{\\qrminipagewidth}{\\qrmodulesize}\n"; + $output .= "\\multiply\\qrminipagewidth by $w\n"; + + $output .= "\\begin{minipage}{\\qrminipagewidth}\n"; + $output .= "\\baselineskip=\\qrmodulesize\n"; + $output .= "\\parindent=0mm\n"; + $output .= "\\def\\qrb{\\rule{\\qrmodulesize}{\\qrmodulesize}}\n"; + $output .= "\\def\\qrw{\\rule{\\qrmodulesize}{0pt}}\n"; + + for($i=0; $i<$h; $i++) { + $output .= "\\par"; + for($j=0; $j<$w; $j++) { + if( $frame[$i][$j] == '1') + $output .= "\\qrb"; + else + $output .= "\\qrw"; + } + } + + $output .= "\\end{minipage}\n"; + + if($writeheader) + $output .= "\\end{document}\n"; + + if($filename) + QRtools::save($output, $filename); + else + return($output); + } + + //---------------------------------------------------------------------- public static function svg($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE, $back_color, $fore_color) From 876b7c9d908c28485430a03d38ff1ecff1c370bf Mon Sep 17 00:00:00 2001 From: mtg Date: Thu, 26 Nov 2015 12:17:24 +0100 Subject: [PATCH 2/2] Latex output: size defines total code width in mm --- qrencode.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qrencode.php b/qrencode.php index 9d35852..ec702ad 100644 --- a/qrencode.php +++ b/qrencode.php @@ -304,7 +304,7 @@ public static function eps($text, $outfile = false, $level = QR_ECLEVEL_L, $size public static function latex_rules($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $writeheader=true, $back_color = 0xFFFFFF, $fore_color = 0x000000, $cmyk = false) { $enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color, $cmyk); - return $enc->encodeLatex_Rules($text, $outfile, $size . "mm", $writeheader); + return $enc->encodeLatex_Rules($text, $outfile, $size, $writeheader); } //---------------------------------------------------------------------- @@ -551,7 +551,7 @@ public function encodeEPS($intext, $outfile = false,$saveandprint=false) } //---------------------------------------------------------------------- - public function encodeLatex_Rules($intext, $outfile = false, $blocksize = "2mm", $writeheader = true) + public function encodeLatex_Rules($intext, $outfile = false, $totalsize = "16", $writeheader = true) { try { @@ -563,7 +563,7 @@ public function encodeLatex_Rules($intext, $outfile = false, $blocksize = "2mm", if ($err != '') QRtools::log($outfile, $err); - return QRvect::latex_rules($tab, $outfile, $blocksize, $writeheader); + return QRvect::latex_rules($tab, $outfile, sprintf("%.3fmm", $totalsize / strlen($tab[0])), $writeheader); } catch (Exception $e) {