Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QR Code print management #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/Command/QRCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* This file is part of PhpAidc LabelPrinter package.
*
* © Appwilio (https://appwilio.com)
* © JhaoDa (https://github.com/jhaoda)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpAidc\LabelPrinter\Command;

use PhpAidc\LabelPrinter\Contract\Command;
use PhpAidc\LabelPrinter\Command\Concerns\Alignable;
use PhpAidc\LabelPrinter\Command\Concerns\Rotatable;
use PhpAidc\LabelPrinter\Command\Concerns\PositionAware;

final class QRCode implements Command
{
use Alignable;
use Rotatable;
use PositionAware;

/** @var int|null */
private $cellWidth;

/** @var string */
private $data;

/** @var string|null */
private $eccLevel;

/** @var int|null */
private $height;

/** @var int|null */
private $magnification;

/** @var string|null */
private $mode;

/** @var string|null */
private $model;

public function __construct(int $x, int $y, string $data, string $eccLevel, int $cellWidth, string $mode)
{
$this->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;
}

}
15 changes: 15 additions & 0 deletions src/Label/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
2 changes: 2 additions & 0 deletions src/Language/Tspl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions src/Language/Tspl/TsplQRCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* This file is part of PhpAidc LabelPrinter package.
*
* © Appwilio (https://appwilio.com)
* © JhaoDa (https://github.com/jhaoda)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpAidc\LabelPrinter\Language\Tspl;

use PhpAidc\LabelPrinter\Command\QRCode;

final class TsplQRCode
{
public function translate(QRCode $command): iterable
{
$instruction = \vsprintf('QRCODE %d,%d,%s,%d,%s', [
$command->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());
}
}
2 changes: 1 addition & 1 deletion src/Language/Tspl/TsplTextBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down