From b48ab942a7581c0f076a715d8317e1e7bfb92660 Mon Sep 17 00:00:00 2001 From: Saverio Bavosio Date: Thu, 2 Dec 2021 18:51:27 +0100 Subject: [PATCH 1/2] Add QR Code print management --- src/Command/QRCode.php | 103 +++++++++++++++++++++++++++++++ src/Label/Element.php | 15 +++++ src/Language/Tspl.php | 2 + src/Language/Tspl/TsplQRCode.php | 41 ++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 src/Command/QRCode.php create mode 100644 src/Language/Tspl/TsplQRCode.php diff --git a/src/Command/QRCode.php b/src/Command/QRCode.php new file mode 100644 index 0000000..8128f4c --- /dev/null +++ b/src/Command/QRCode.php @@ -0,0 +1,103 @@ +x = $x; + $this->y = $y; + $this->data = $data; + $this->cellWidth = $cellWidth; + $this->eccLevel = $eccLevel; + $this->mode = $mode; + } + + public function magnify(int $value) + { + $this->magnification = $value; + + return $this; + } + + public function model(string $value) + { + $this->model = $value; + + return $this; + } + + public function getCellWidth(): ?int + { + return $this->cellWidth; + } + + public function getData(): string + { + return $this->data; + } + + public function getECCLevel(): string + { + return $this->eccLevel; + } + + public function getMagnification(): ?int + { + return $this->magnification; + } + + public function getMode(): ?string + { + return $this->mode; + } + + public function getModel(): ?string + { + return $this->model; + } + +} diff --git a/src/Label/Element.php b/src/Label/Element.php index 963d6a0..7b603a2 100644 --- a/src/Label/Element.php +++ b/src/Label/Element.php @@ -18,6 +18,7 @@ use PhpAidc\LabelPrinter\Command\Clear; use PhpAidc\LabelPrinter\Command\Bitmap; use PhpAidc\LabelPrinter\Command\Barcode; +use PhpAidc\LabelPrinter\Command\QRCode; use PhpAidc\LabelPrinter\Command\TextLine; use PhpAidc\LabelPrinter\Command\TextBlock; use PhpAidc\LabelPrinter\Command\ExternalImage; @@ -97,4 +98,18 @@ public static function textBlock(int $x, int $y, string $text, string $font, flo { return new TextBlock(...\func_get_args()); } + + /** + * Print a qr code in the label + * @param int $x + * @param int $y + * @param string $data + * @param string $eccLevel Error correction recovery level (L: 7% / M: 15% / Q: 25% / H: 30%) + * @param int $cellWidth Width of a single cell (1~N) + * @param string $mode Encode mode (A: auto / M: manual) + */ + public static function qrcode(int $x, int $y, string $data, string $eccLevel, int $cellWidth, string $mode): QRCode + { + return new QRCode(...\func_get_args()); + } } diff --git a/src/Language/Tspl.php b/src/Language/Tspl.php index 45fbb40..0423cde 100644 --- a/src/Language/Tspl.php +++ b/src/Language/Tspl.php @@ -21,6 +21,7 @@ use PhpAidc\LabelPrinter\Command\TextLine; use PhpAidc\LabelPrinter\Command\TextBlock; use PhpAidc\LabelPrinter\Command\InternalImage; +use PhpAidc\LabelPrinter\Command\QRCode; use PhpAidc\LabelPrinter\Contract\Label; use PhpAidc\LabelPrinter\Contract\Media; use PhpAidc\LabelPrinter\Contract\Command; @@ -38,6 +39,7 @@ final class Tspl implements Language Clear::class => Handlers\TsplClear::class, Bitmap::class => Handlers\TsplBitmap::class, Barcode::class => Handlers\TsplBarcode::class, + QRCode::class => Handlers\TsplQRCode::class, TextLine::class => Handlers\TsplTextLine::class, TextBlock::class => Handlers\TsplTextBlock::class, InternalImage::class => Handlers\TsplInternalImage::class, diff --git a/src/Language/Tspl/TsplQRCode.php b/src/Language/Tspl/TsplQRCode.php new file mode 100644 index 0000000..f051ae0 --- /dev/null +++ b/src/Language/Tspl/TsplQRCode.php @@ -0,0 +1,41 @@ +getX(), + $command->getY(), + $command->getECCLevel(), + $command->getCellWidth(), + $command->getMode() + ]); + + // rotation + $instruction .= ','.$command->getRotation()->getDegrees(); + + // model + if ($command->getModel()) { + $instruction .= ','.(string) $command->getModel(); + } + + yield $instruction.\sprintf(',"%s"', $command->getData()); + } +} From 09c9b25e60ac27753847c3bf89a8f54edbde548b Mon Sep 17 00:00:00 2001 From: Saverio Bavosio Date: Tue, 28 Dec 2021 12:03:12 +0100 Subject: [PATCH 2/2] Fixed format string problem on 'BLOCK' element --- src/Language/Tspl/TsplTextBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Language/Tspl/TsplTextBlock.php b/src/Language/Tspl/TsplTextBlock.php index 8506380..0f1f53c 100644 --- a/src/Language/Tspl/TsplTextBlock.php +++ b/src/Language/Tspl/TsplTextBlock.php @@ -56,7 +56,7 @@ public function translate(TextBlock $command): iterable $specifier = \is_float($size) ? '%.2F' : '%d'; - $format = "%d,%d,%d,%d,\"%s\",%d,{$specifier},{$specifier},0,%d,0,\"%s\""; + $format = "%d,%d,%d,%d,\"%s\",%d,{$specifier},{$specifier},%d,%d,\"%s\""; yield \vsprintf('BLOCK '.$format, [ $command->getX(),