added composer files and installed vendor packages. Also modified xsls to display base project
This commit is contained in:
+325
@@ -0,0 +1,325 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Barcode.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Barcode
|
||||
*
|
||||
* Barcode Barcode class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Barcode
|
||||
{
|
||||
/**
|
||||
* Maximum accepted length of the barcode payload.
|
||||
*
|
||||
* No single barcode symbology can encode anywhere near this much data
|
||||
* (the densest, QR version 40-L, tops out at ~7089 numeric digits), so
|
||||
* this is a defensive upper bound to reject abusive inputs early, before
|
||||
* any encoder spends time and memory on data it can never represent.
|
||||
*/
|
||||
public const MAX_CODE_LENGTH = 30_000;
|
||||
|
||||
/**
|
||||
* List of supported Barcode Types with description.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
public const BARCODETYPES = [
|
||||
'C128' => 'CODE 128',
|
||||
'C128A' => 'CODE 128 A',
|
||||
'C128B' => 'CODE 128 B',
|
||||
'C128C' => 'CODE 128 C',
|
||||
'C39' => 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.',
|
||||
'C39+' => 'CODE 39 + CHECKSUM',
|
||||
'C39E' => 'CODE 39 EXTENDED',
|
||||
'C39E+' => 'CODE 39 EXTENDED + CHECKSUM',
|
||||
'C93' => 'CODE 93 - USS-93',
|
||||
'CODABAR' => 'CODABAR',
|
||||
'CODE11' => 'CODE 11',
|
||||
'EAN13' => 'EAN 13',
|
||||
'EAN2' => 'EAN 2-Digits UPC-Based Extension',
|
||||
'EAN5' => 'EAN 5-Digits UPC-Based Extension',
|
||||
'EAN8' => 'EAN 8',
|
||||
'I25' => 'Interleaved 2 of 5',
|
||||
'I25+' => 'Interleaved 2 of 5 + CHECKSUM',
|
||||
'IMB' => 'IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200',
|
||||
'IMBPRE' => 'IMB - Intelligent Mail Barcode pre-processed',
|
||||
'KIX' => 'KIX (Klant index - Customer index)',
|
||||
'LRAW' => '1D RAW MODE (comma-separated rows of 01 strings)',
|
||||
'MSI' => 'MSI (Variation of Plessey code)',
|
||||
'MSI+' => 'MSI + CHECKSUM (modulo 11)',
|
||||
'PHARMA' => 'PHARMACODE',
|
||||
'PHARMA2T' => 'PHARMACODE TWO-TRACKS',
|
||||
'PLANET' => 'PLANET',
|
||||
'POSTNET' => 'POSTNET',
|
||||
'RMS4CC' => 'RMS4CC (Royal Mail 4-state Customer Bar Code)',
|
||||
'S25' => 'Standard 2 of 5',
|
||||
'S25+' => 'Standard 2 of 5 + CHECKSUM',
|
||||
'UPCA' => 'UPC-A',
|
||||
'UPCE' => 'UPC-E',
|
||||
'AZTEC' => 'AZTEC Code (ISO/IEC 24778:2008)',
|
||||
'DATAMATRIX' => 'DATAMATRIX (ISO/IEC 16022)',
|
||||
'PDF417' => 'PDF417 (ISO/IEC 15438:2006)',
|
||||
'QRCODE' => 'QR-CODE',
|
||||
'SRAW' => '2D RAW MODE (comma-separated rows of 01 strings)',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the barcode object
|
||||
*
|
||||
* @param string|BarcodeType $type Barcode type (leading token, optionally followed by
|
||||
* comma-separated extra parameters), or a BarcodeType enum case
|
||||
* @param string $code Barcode content
|
||||
* @param int $width Barcode width in user units (excluding padding).
|
||||
* A negative value indicates the multiplication
|
||||
* factor for each column.
|
||||
* @param int $height Barcode height in user units (excluding padding).
|
||||
* A negative value indicates the multiplication
|
||||
* factor for each row.
|
||||
* @param string $color Foreground color in Web notation
|
||||
* (color name, or hexadecimal code, or CSS syntax)
|
||||
* @param array{int, int, int, int} $padding Additional padding to add around the barcode
|
||||
* (top, right, bottom, left) in user units. A
|
||||
* negative value indicates the multiplication
|
||||
* factor for each row or column.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
* @throws \Com\Tecnick\Color\Exception in case of color parsing errors
|
||||
*/
|
||||
public function getBarcodeObj(
|
||||
string|BarcodeType $type,
|
||||
string $code,
|
||||
int $width = -1,
|
||||
int $height = -1,
|
||||
string $color = 'black',
|
||||
array $padding = [0, 0, 0, 0],
|
||||
): Model {
|
||||
if ($type instanceof BarcodeType) {
|
||||
$type = $type->value;
|
||||
}
|
||||
|
||||
if (\strlen($code) > self::MAX_CODE_LENGTH) {
|
||||
throw new BarcodeException(
|
||||
'The barcode payload is too long: ' . \strlen($code) . ' bytes (maximum ' . self::MAX_CODE_LENGTH . ')',
|
||||
);
|
||||
}
|
||||
|
||||
// extract extra parameters (if any)
|
||||
$params = \explode(',', $type);
|
||||
$type = \array_shift($params);
|
||||
|
||||
return match ($type) {
|
||||
'C128' => new \Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'C128A' => new \Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight\CodeOneTwoEightA(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'C128B' => new \Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight\CodeOneTwoEightB(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'C128C' => new \Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight\CodeOneTwoEightC(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'C39' => new \Com\Tecnick\Barcode\Type\Linear\CodeThreeNine(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'C39+' => new \Com\Tecnick\Barcode\Type\Linear\CodeThreeNineCheck(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'C39E' => new \Com\Tecnick\Barcode\Type\Linear\CodeThreeNineExt(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'C39E+' => new \Com\Tecnick\Barcode\Type\Linear\CodeThreeNineExtCheck(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'C93' => new \Com\Tecnick\Barcode\Type\Linear\CodeNineThree(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'CODABAR' => new \Com\Tecnick\Barcode\Type\Linear\Codabar(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'CODE11' => new \Com\Tecnick\Barcode\Type\Linear\CodeOneOne(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'EAN13' => new \Com\Tecnick\Barcode\Type\Linear\EanOneThree(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'EAN2' => new \Com\Tecnick\Barcode\Type\Linear\EanTwo($code, $width, $height, $color, $params, $padding),
|
||||
'EAN5' => new \Com\Tecnick\Barcode\Type\Linear\EanFive($code, $width, $height, $color, $params, $padding),
|
||||
'EAN8' => new \Com\Tecnick\Barcode\Type\Linear\EanEight($code, $width, $height, $color, $params, $padding),
|
||||
'I25' => new \Com\Tecnick\Barcode\Type\Linear\InterleavedTwoOfFive(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'I25+' => new \Com\Tecnick\Barcode\Type\Linear\InterleavedTwoOfFiveCheck(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'IMB' => new \Com\Tecnick\Barcode\Type\Linear\Imb($code, $width, $height, $color, $params, $padding),
|
||||
'IMBPRE' => new \Com\Tecnick\Barcode\Type\Linear\ImbPre($code, $width, $height, $color, $params, $padding),
|
||||
'KIX' => new \Com\Tecnick\Barcode\Type\Linear\KlantIndex($code, $width, $height, $color, $params, $padding),
|
||||
'LRAW' => new \Com\Tecnick\Barcode\Type\Linear\Raw($code, $width, $height, $color, $params, $padding),
|
||||
'MSI' => new \Com\Tecnick\Barcode\Type\Linear\Msi($code, $width, $height, $color, $params, $padding),
|
||||
'MSI+' => new \Com\Tecnick\Barcode\Type\Linear\MsiCheck($code, $width, $height, $color, $params, $padding),
|
||||
'PHARMA' => new \Com\Tecnick\Barcode\Type\Linear\Pharma($code, $width, $height, $color, $params, $padding),
|
||||
'PHARMA2T' => new \Com\Tecnick\Barcode\Type\Linear\PharmaTwoTracks(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'PLANET' => new \Com\Tecnick\Barcode\Type\Linear\Planet($code, $width, $height, $color, $params, $padding),
|
||||
'POSTNET' => new \Com\Tecnick\Barcode\Type\Linear\Postnet(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'RMS4CC' => new \Com\Tecnick\Barcode\Type\Linear\RoyalMailFourCc(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'S25' => new \Com\Tecnick\Barcode\Type\Linear\StandardTwoOfFive(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'S25+' => new \Com\Tecnick\Barcode\Type\Linear\StandardTwoOfFiveCheck(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'UPCA' => new \Com\Tecnick\Barcode\Type\Linear\UpcA($code, $width, $height, $color, $params, $padding),
|
||||
'UPCE' => new \Com\Tecnick\Barcode\Type\Linear\UpcE($code, $width, $height, $color, $params, $padding),
|
||||
'AZTEC' => new \Com\Tecnick\Barcode\Type\Square\Aztec($code, $width, $height, $color, $params, $padding),
|
||||
'DATAMATRIX' => new \Com\Tecnick\Barcode\Type\Square\Datamatrix(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'PDF417' => new \Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven(
|
||||
$code,
|
||||
$width,
|
||||
$height,
|
||||
$color,
|
||||
$params,
|
||||
$padding,
|
||||
),
|
||||
'QRCODE' => new \Com\Tecnick\Barcode\Type\Square\QrCode($code, $width, $height, $color, $params, $padding),
|
||||
'SRAW' => new \Com\Tecnick\Barcode\Type\Square\Raw($code, $width, $height, $color, $params, $padding),
|
||||
default => throw new BarcodeException('Unsupported barcode type: ' . $type),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* BarcodeType.php
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\BarcodeType
|
||||
*
|
||||
* Backed enum for the supported barcode symbologies. The backing value of each
|
||||
* case is the leading type token accepted by Barcode::getBarcodeObj() (before
|
||||
* any comma-separated extra parameters).
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
enum BarcodeType: string
|
||||
{
|
||||
case C128 = 'C128';
|
||||
|
||||
case C128A = 'C128A';
|
||||
|
||||
case C128B = 'C128B';
|
||||
|
||||
case C128C = 'C128C';
|
||||
|
||||
case C39 = 'C39';
|
||||
|
||||
case C39Plus = 'C39+';
|
||||
|
||||
case C39E = 'C39E';
|
||||
|
||||
case C39EPlus = 'C39E+';
|
||||
|
||||
case C93 = 'C93';
|
||||
|
||||
case CODABAR = 'CODABAR';
|
||||
|
||||
case CODE11 = 'CODE11';
|
||||
|
||||
case EAN13 = 'EAN13';
|
||||
|
||||
case EAN2 = 'EAN2';
|
||||
|
||||
case EAN5 = 'EAN5';
|
||||
|
||||
case EAN8 = 'EAN8';
|
||||
|
||||
case I25 = 'I25';
|
||||
|
||||
case I25Plus = 'I25+';
|
||||
|
||||
case IMB = 'IMB';
|
||||
|
||||
case IMBPRE = 'IMBPRE';
|
||||
|
||||
case KIX = 'KIX';
|
||||
|
||||
case LRAW = 'LRAW';
|
||||
|
||||
case MSI = 'MSI';
|
||||
|
||||
case MSIPlus = 'MSI+';
|
||||
|
||||
case PHARMA = 'PHARMA';
|
||||
|
||||
case PHARMA2T = 'PHARMA2T';
|
||||
|
||||
case PLANET = 'PLANET';
|
||||
|
||||
case POSTNET = 'POSTNET';
|
||||
|
||||
case RMS4CC = 'RMS4CC';
|
||||
|
||||
case S25 = 'S25';
|
||||
|
||||
case S25Plus = 'S25+';
|
||||
|
||||
case UPCA = 'UPCA';
|
||||
|
||||
case UPCE = 'UPCE';
|
||||
|
||||
case AZTEC = 'AZTEC';
|
||||
|
||||
case DATAMATRIX = 'DATAMATRIX';
|
||||
|
||||
case PDF417 = 'PDF417';
|
||||
|
||||
case QRCODE = 'QRCODE';
|
||||
|
||||
case SRAW = 'SRAW';
|
||||
|
||||
/**
|
||||
* Resolve a loose barcode type token to the matching enum case.
|
||||
*
|
||||
* Accepts the exact leading type token (as validated by getBarcodeObj) or an
|
||||
* enum instance (returned unchanged). Unknown values throw.
|
||||
*
|
||||
* @param string|self $value Barcode type token or enum case.
|
||||
*
|
||||
* @throws BarcodeException if the value does not match a known barcode type.
|
||||
*/
|
||||
public static function fromLoose(string|self $value): self
|
||||
{
|
||||
if ($value instanceof self) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return self::tryFrom($value) ?? throw new BarcodeException('Unsupported barcode type: ' . $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Exception.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Exception
|
||||
*
|
||||
* Custom Exception class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Exception extends \Exception {}
|
||||
+375
@@ -0,0 +1,375 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Math.php
|
||||
*
|
||||
* @since 2026-08-06
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Math
|
||||
*
|
||||
* Arbitrary precision arithmetic on non-negative decimal integer strings.
|
||||
* The bcmath extension is used when available, otherwise the equivalent
|
||||
* pure-PHP implementation is used, so bcmath is an optional dependency.
|
||||
*
|
||||
* @since 2026-08-06
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
final class Math
|
||||
{
|
||||
/**
|
||||
* Maximum number of digits of a divisor that can be processed as an integer
|
||||
* without overflowing the running remainder, including on 32 bit platforms.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private const MAX_INT_DIGITS = 8;
|
||||
|
||||
/**
|
||||
* Cached availability of the bcmath functions.
|
||||
*/
|
||||
private static ?bool $bcmath = null;
|
||||
|
||||
/**
|
||||
* Returns true if the bcmath functions are available.
|
||||
*/
|
||||
public static function hasBcmath(): bool
|
||||
{
|
||||
return self::$bcmath ??=
|
||||
\function_exists('bcadd')
|
||||
&& \function_exists('bcmul')
|
||||
&& \function_exists('bcdiv')
|
||||
&& \function_exists('bcmod');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two non-negative decimal integers.
|
||||
*
|
||||
* @param string $left First operand
|
||||
* @param string $right Second operand
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
public static function add(string $left, string $right): string
|
||||
{
|
||||
if (!self::hasBcmath()) {
|
||||
return self::fallbackAdd($left, $right);
|
||||
}
|
||||
|
||||
return \bcadd(self::normalize($left), self::normalize($right), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two non-negative decimal integers.
|
||||
*
|
||||
* @param string $left First operand
|
||||
* @param string $right Second operand
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
public static function mul(string $left, string $right): string
|
||||
{
|
||||
if (!self::hasBcmath()) {
|
||||
return self::fallbackMul($left, $right);
|
||||
}
|
||||
|
||||
return \bcmul(self::normalize($left), self::normalize($right), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Integer division of two non-negative decimal integers.
|
||||
*
|
||||
* @param string $left Dividend
|
||||
* @param string $right Divisor
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
public static function div(string $left, string $right): string
|
||||
{
|
||||
if (!self::hasBcmath()) {
|
||||
return self::fallbackDiv($left, $right);
|
||||
}
|
||||
|
||||
return \bcdiv(self::normalize($left), self::normalize($right), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remainder of the integer division of two non-negative decimal integers.
|
||||
*
|
||||
* @param string $left Dividend
|
||||
* @param string $right Divisor
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
public static function mod(string $left, string $right): string
|
||||
{
|
||||
if (!self::hasBcmath()) {
|
||||
return self::fallbackMod($left, $right);
|
||||
}
|
||||
|
||||
return \bcmod(self::normalize($left), self::normalize($right), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two non-negative decimal integers without bcmath.
|
||||
*
|
||||
* @param string $left First operand
|
||||
* @param string $right Second operand
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
public static function fallbackAdd(string $left, string $right): string
|
||||
{
|
||||
$lft = self::normalize($left);
|
||||
$rgt = self::normalize($right);
|
||||
$pl = \strlen($lft) - 1;
|
||||
$pr = \strlen($rgt) - 1;
|
||||
$carry = 0;
|
||||
$sum = '';
|
||||
while ($pl >= 0 || $pr >= 0 || $carry > 0) {
|
||||
$digit = $carry;
|
||||
if ($pl >= 0) {
|
||||
$digit += (int) $lft[$pl];
|
||||
--$pl;
|
||||
}
|
||||
|
||||
if ($pr >= 0) {
|
||||
$digit += (int) $rgt[$pr];
|
||||
--$pr;
|
||||
}
|
||||
|
||||
$sum = (string) ($digit % 10) . $sum;
|
||||
$carry = \intdiv($digit, 10);
|
||||
}
|
||||
|
||||
return self::normalize($sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two non-negative decimal integers without bcmath.
|
||||
*
|
||||
* @param string $left First operand
|
||||
* @param string $right Second operand
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
public static function fallbackMul(string $left, string $right): string
|
||||
{
|
||||
$lft = self::normalize($left);
|
||||
$rgt = self::normalize($right);
|
||||
if ($lft === '0' || $rgt === '0') {
|
||||
return '0';
|
||||
}
|
||||
|
||||
$llen = \strlen($lft);
|
||||
$rlen = \strlen($rgt);
|
||||
// partial products, least significant digit first
|
||||
$digits = \array_fill(0, $llen + $rlen, 0);
|
||||
for ($pl = $llen - 1; $pl >= 0; --$pl) {
|
||||
$mul = (int) $lft[$pl];
|
||||
$carry = 0;
|
||||
$pos = $llen - 1 - $pl;
|
||||
for ($pr = $rlen - 1; $pr >= 0; --$pr) {
|
||||
$cur = ($digits[$pos] ?? 0) + ($mul * (int) $rgt[$pr]) + $carry;
|
||||
$digits[$pos] = $cur % 10;
|
||||
$carry = \intdiv($cur, 10);
|
||||
++$pos;
|
||||
}
|
||||
|
||||
while ($carry > 0) {
|
||||
$cur = ($digits[$pos] ?? 0) + $carry;
|
||||
$digits[$pos] = $cur % 10;
|
||||
$carry = \intdiv($cur, 10);
|
||||
++$pos;
|
||||
}
|
||||
}
|
||||
|
||||
$product = '';
|
||||
foreach ($digits as $digit) {
|
||||
$product = (string) $digit . $product;
|
||||
}
|
||||
|
||||
return self::normalize($product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Integer division of two non-negative decimal integers without bcmath.
|
||||
*
|
||||
* @param string $left Dividend
|
||||
* @param string $right Divisor
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
public static function fallbackDiv(string $left, string $right): string
|
||||
{
|
||||
return self::fallbackDivMod($left, $right)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remainder of the integer division of two non-negative decimal integers without bcmath.
|
||||
*
|
||||
* @param string $left Dividend
|
||||
* @param string $right Divisor
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
public static function fallbackMod(string $left, string $right): string
|
||||
{
|
||||
return self::fallbackDivMod($left, $right)[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Long division of two non-negative decimal integers.
|
||||
*
|
||||
* @param string $left Dividend
|
||||
* @param string $right Divisor
|
||||
*
|
||||
* @return array{numeric-string, numeric-string} Quotient and remainder
|
||||
*/
|
||||
private static function fallbackDivMod(string $left, string $right): array
|
||||
{
|
||||
$lft = self::normalize($left);
|
||||
$rgt = self::normalize($right);
|
||||
if ($rgt === '0') {
|
||||
throw new \DivisionByZeroError('Division by zero');
|
||||
}
|
||||
|
||||
// divisors small enough to keep the running remainder inside an integer
|
||||
if (\strlen($rgt) <= self::MAX_INT_DIGITS) {
|
||||
return self::fallbackDivModInt($lft, (int) $rgt);
|
||||
}
|
||||
|
||||
$quotient = '';
|
||||
$remainder = '0';
|
||||
$len = \strlen($lft);
|
||||
for ($pos = 0; $pos < $len; ++$pos) {
|
||||
// shift the next dividend digit into the remainder
|
||||
$remainder = self::normalize($remainder . $lft[$pos]);
|
||||
$digit = 0;
|
||||
while (self::compare($remainder, $rgt) >= 0) {
|
||||
$remainder = self::subtract($remainder, $rgt);
|
||||
++$digit;
|
||||
}
|
||||
|
||||
$quotient .= (string) $digit;
|
||||
}
|
||||
|
||||
return [self::normalize($quotient), $remainder];
|
||||
}
|
||||
|
||||
/**
|
||||
* Long division of a non-negative decimal integer by an integer divisor.
|
||||
*
|
||||
* @param numeric-string $left Normalized dividend
|
||||
* @param int $right Divisor greater than zero
|
||||
*
|
||||
* @return array{numeric-string, numeric-string} Quotient and remainder
|
||||
*/
|
||||
private static function fallbackDivModInt(string $left, int $right): array
|
||||
{
|
||||
$quotient = '';
|
||||
$remainder = 0;
|
||||
$len = \strlen($left);
|
||||
for ($pos = 0; $pos < $len; ++$pos) {
|
||||
$remainder = ($remainder * 10) + (int) $left[$pos];
|
||||
$quotient .= (string) \intdiv($remainder, $right);
|
||||
$remainder %= $right;
|
||||
}
|
||||
|
||||
return [self::normalize($quotient), (string) $remainder];
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two normalized non-negative decimal integers.
|
||||
*
|
||||
* @param string $left First operand
|
||||
* @param string $right Second operand
|
||||
*
|
||||
* @return int Negative if left is lower, zero if equal, positive if left is greater
|
||||
*/
|
||||
private static function compare(string $left, string $right): int
|
||||
{
|
||||
$llen = \strlen($left);
|
||||
$rlen = \strlen($right);
|
||||
if ($llen !== $rlen) {
|
||||
return $llen <=> $rlen;
|
||||
}
|
||||
|
||||
return \strcmp($left, $right);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two normalized non-negative decimal integers, where the first one is the greater.
|
||||
*
|
||||
* @param string $left Minuend
|
||||
* @param string $right Subtrahend
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
private static function subtract(string $left, string $right): string
|
||||
{
|
||||
$pl = \strlen($left) - 1;
|
||||
$pr = \strlen($right) - 1;
|
||||
$borrow = 0;
|
||||
$diff = '';
|
||||
while ($pl >= 0) {
|
||||
$digit = (int) $left[$pl] - $borrow;
|
||||
if ($pr >= 0) {
|
||||
$digit -= (int) $right[$pr];
|
||||
--$pr;
|
||||
}
|
||||
|
||||
if ($digit < 0) {
|
||||
$digit += 10;
|
||||
$borrow = 1;
|
||||
} else {
|
||||
$borrow = 0;
|
||||
}
|
||||
|
||||
$diff = (string) $digit . $diff;
|
||||
--$pl;
|
||||
}
|
||||
|
||||
return self::normalize($diff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip the leading zeros from a non-negative decimal integer string.
|
||||
*
|
||||
* @param string $number Number to normalize
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
private static function normalize(string $number): string
|
||||
{
|
||||
if ($number === '' || !\ctype_digit($number)) {
|
||||
throw new \ValueError('Expecting a non-negative decimal integer string');
|
||||
}
|
||||
|
||||
$number = \ltrim($number, '0');
|
||||
if ($number === '') {
|
||||
return '0';
|
||||
}
|
||||
|
||||
/** @var numeric-string */
|
||||
return $number;
|
||||
}
|
||||
}
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Model.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
use Com\Tecnick\Color\Exception as ColorException;
|
||||
use Com\Tecnick\Color\Model\Rgb;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Model
|
||||
*
|
||||
* Barcode Model interface
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
interface Model
|
||||
{
|
||||
/**
|
||||
* Set the size of the barcode to be exported
|
||||
*
|
||||
* @param int $width Barcode width in user units (excluding padding).
|
||||
* A negative value indicates the multiplication
|
||||
* factor for each column.
|
||||
* @param int $height Barcode height in user units (excluding padding).
|
||||
* A negative value indicates the multiplication
|
||||
* factor for each row.
|
||||
* @param array{int, int, int, int} $padding Additional padding to add around the barcode
|
||||
* (top, right, bottom, left) in user units. A
|
||||
* negative value indicates the number of rows
|
||||
* or columns.
|
||||
*/
|
||||
public function setSize(int $width, int $height, array $padding = [0, 0, 0, 0]): static;
|
||||
|
||||
/**
|
||||
* Set the color of the bars.
|
||||
* An empty or transparent foreground color is rejected with a BarcodeException.
|
||||
*
|
||||
* @param string $color Foreground color in Web notation (color name, or hexadecimal code, or CSS syntax)
|
||||
*
|
||||
* @throws ColorException in case of color error
|
||||
* @throws BarcodeException in case of empty or transparent color
|
||||
*/
|
||||
public function setColor(string $color): static;
|
||||
|
||||
/**
|
||||
* Set the background color
|
||||
*
|
||||
* @param string $color Background color in Web notation (color name, or hexadecimal code, or CSS syntax)
|
||||
*
|
||||
* @throws ColorException in case of color error
|
||||
*/
|
||||
public function setBackgroundColor(string $color): static;
|
||||
|
||||
/**
|
||||
* Get the barcode raw array
|
||||
*
|
||||
* @return array{
|
||||
* 'type': string,
|
||||
* 'format': string,
|
||||
* 'params': array<int|float|string>,
|
||||
* 'code': string,
|
||||
* 'extcode': string,
|
||||
* 'ncols': int,
|
||||
* 'nrows': int,
|
||||
* 'width': int,
|
||||
* 'height': int,
|
||||
* 'width_ratio': float,
|
||||
* 'height_ratio': float,
|
||||
* 'padding': array{'T': int, 'R': int, 'B': int, 'L': int},
|
||||
* 'full_width': int,
|
||||
* 'full_height': int,
|
||||
* 'color_obj': Rgb,
|
||||
* 'bg_color_obj': ?Rgb,
|
||||
* 'bars': array<array{int, int, int, int}>,
|
||||
* }
|
||||
*/
|
||||
public function getArray(): array;
|
||||
|
||||
/**
|
||||
* Get the extended code (code + checksum)
|
||||
*/
|
||||
public function getExtendedCode(): string;
|
||||
|
||||
/**
|
||||
* Get the barcode as SVG image object
|
||||
*
|
||||
* @param string|null $filename The file name without extension (optional).
|
||||
* Only allows alphanumeric characters, underscores and hyphens.
|
||||
* Defaults to a md5 hash of the data.
|
||||
* The file extension is always '.svg'.
|
||||
*/
|
||||
public function getSvg(?string $filename = null): void;
|
||||
|
||||
/**
|
||||
* Get the barcode as inline SVG code.
|
||||
*
|
||||
* @return string Inline SVG code.
|
||||
*/
|
||||
public function getInlineSvgCode(): string;
|
||||
|
||||
/**
|
||||
* Get the barcode as SVG code, including the XML declaration.
|
||||
*
|
||||
* @return string SVG code
|
||||
*/
|
||||
public function getSvgCode(): string;
|
||||
|
||||
/**
|
||||
* Get an HTML representation of the barcode.
|
||||
*
|
||||
* @return string HTML code (DIV block)
|
||||
*/
|
||||
public function getHtmlDiv(): string;
|
||||
|
||||
/**
|
||||
* Get Barcode as PNG Image (requires GD or Imagick library)
|
||||
*
|
||||
* @param string|null $filename The file name without extension (optional).
|
||||
* Only allows alphanumeric characters, underscores and hyphens.
|
||||
* Defaults to a md5 hash of the data.
|
||||
* The file extension is always '.png'.
|
||||
*/
|
||||
public function getPng(?string $filename = null): void;
|
||||
|
||||
/**
|
||||
* Get the barcode as PNG image (requires GD or Imagick library)
|
||||
*
|
||||
* @param bool $imagick If true try to use the Imagick extension
|
||||
*
|
||||
* @return string PNG image data
|
||||
*/
|
||||
public function getPngData(bool $imagick = true): string;
|
||||
|
||||
/**
|
||||
* Get the barcode as PNG image (requires Imagick library)
|
||||
*
|
||||
* @throws BarcodeException if the Imagick library is not installed
|
||||
*/
|
||||
public function getPngDataImagick(): string;
|
||||
|
||||
/**
|
||||
* Get the barcode as GD image object (requires GD library)
|
||||
*
|
||||
* @throws BarcodeException if the GD library is not installed
|
||||
*/
|
||||
public function getGd(): \GdImage;
|
||||
|
||||
/**
|
||||
* Get a raw barcode string representation using characters
|
||||
*
|
||||
* @param string $space_char Character or string to use for filling empty spaces
|
||||
* @param string $bar_char Character or string to use for filling bars
|
||||
*/
|
||||
public function getGrid(string $space_char = '0', string $bar_char = '1'): string;
|
||||
|
||||
/**
|
||||
* Get a raw barcode grid array
|
||||
*
|
||||
* @param string $space_char Character or string to use for filling empty spaces
|
||||
* @param string $bar_char Character or string to use for filling bars
|
||||
*
|
||||
* @return array<int, array<int, string>>
|
||||
*/
|
||||
public function getGridArray(string $space_char = '0', string $bar_char = '1'): array;
|
||||
|
||||
/**
|
||||
* Get the array containing all the formatted bars coordinates
|
||||
*
|
||||
* @return array<int, array{float, float, float, float}>
|
||||
*/
|
||||
public function getBarsArrayXYXY(): array;
|
||||
|
||||
/**
|
||||
* Get the array containing all the formatted bars coordinates
|
||||
*
|
||||
* @return array<int, array{float, float, float, float}>
|
||||
*/
|
||||
public function getBarsArrayXYWH(): array;
|
||||
}
|
||||
+737
@@ -0,0 +1,737 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Type.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
use Com\Tecnick\Color\Exception as ColorException;
|
||||
use Com\Tecnick\Color\Model\Rgb;
|
||||
use Com\Tecnick\Color\Pdf;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type
|
||||
*
|
||||
* Barcode Type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.ExcessiveClassComplexity")
|
||||
*/
|
||||
abstract class Type extends \Com\Tecnick\Barcode\Type\Convert implements Model
|
||||
{
|
||||
/**
|
||||
* Initialize a new barcode object
|
||||
*
|
||||
* @param string $code Barcode content
|
||||
* @param int $width Barcode width in user units (excluding padding).
|
||||
* A negative value indicates the multiplication
|
||||
* factor for each column.
|
||||
* @param int $height Barcode height in user units (excluding padding).
|
||||
* A negative value indicates the multiplication
|
||||
* factor for each row.
|
||||
* @param string $color Foreground color in Web notation
|
||||
* (color name, or hexadecimal code, or CSS syntax)
|
||||
* @param array<int|float|string> $params Array containing extra parameters for the specified barcode type
|
||||
* @param array{int, int, int, int} $padding Additional padding to add around the barcode
|
||||
* (top, right, bottom, left) in user units. A
|
||||
* negative value indicates the number of rows
|
||||
* or columns.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
* @throws ColorException in case of color error
|
||||
*/
|
||||
public function __construct(
|
||||
string $code,
|
||||
int $width = -1,
|
||||
int $height = -1,
|
||||
string $color = 'black',
|
||||
array $params = [],
|
||||
array $padding = [0, 0, 0, 0],
|
||||
) {
|
||||
$this->code = $code;
|
||||
$this->extcode = $code;
|
||||
$this->params = $params;
|
||||
$this->setParameters();
|
||||
$this->setBars();
|
||||
$this->setSize($width, $height, $padding);
|
||||
$this->setColor($color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set extra (optional) parameters
|
||||
*/
|
||||
protected function setParameters(): void {}
|
||||
|
||||
/**
|
||||
* Set the bars array
|
||||
*/
|
||||
protected function setBars(): void {}
|
||||
|
||||
/**
|
||||
* Set the size of the barcode to be exported
|
||||
*
|
||||
* @param int $width Barcode width in user units (excluding padding).
|
||||
* A negative value indicates the multiplication
|
||||
* factor for each column.
|
||||
* @param int $height Barcode height in user units (excluding padding).
|
||||
* A negative value indicates the multiplication
|
||||
* factor for each row.
|
||||
* @param array{int, int, int, int} $padding Additional padding to add around the barcode
|
||||
* (top, right, bottom, left) in user units. A
|
||||
* negative value indicates the number of rows
|
||||
* or columns.
|
||||
*
|
||||
* @throws BarcodeException in case of an empty barcode or invalid padding
|
||||
*/
|
||||
public function setSize(int $width, int $height, array $padding = [0, 0, 0, 0]): static
|
||||
{
|
||||
if ($this->ncols <= 0 || $this->nrows <= 0) {
|
||||
throw new BarcodeException('Empty barcode: the number of rows and columns must be greater than zero');
|
||||
}
|
||||
|
||||
$this->width = $width;
|
||||
if ($this->width <= 0) {
|
||||
$this->width = \abs(\min(-1, $this->width)) * $this->ncols;
|
||||
}
|
||||
|
||||
$this->height = $height;
|
||||
if ($this->height <= 0) {
|
||||
$this->height = \abs(\min(-1, $this->height)) * $this->nrows;
|
||||
}
|
||||
|
||||
$this->width_ratio = $this->width / $this->ncols;
|
||||
$this->height_ratio = $this->height / $this->nrows;
|
||||
|
||||
$this->setPadding($padding);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the barcode padding
|
||||
*
|
||||
* @param array{int, int, int, int} $padding Additional padding to add around the barcode
|
||||
* (top, right, bottom, left) in user units.
|
||||
* A negative value indicates the number of rows or columns.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setPadding(array $padding): static
|
||||
{
|
||||
if (\count($padding) !== 4) {
|
||||
throw new BarcodeException('Invalid padding, expecting an array of 4 numbers (top, right, bottom, left)');
|
||||
}
|
||||
|
||||
foreach ($padding as $key => $val) {
|
||||
$side = match ($key) {
|
||||
0 => 'T',
|
||||
1 => 'R',
|
||||
2 => 'B',
|
||||
3 => 'L',
|
||||
};
|
||||
$ratio = match ($key) {
|
||||
0, 2 => $this->height_ratio,
|
||||
1, 3 => $this->width_ratio,
|
||||
};
|
||||
if ($val < 0) {
|
||||
$val = \abs(\min(-1, $val)) * $ratio;
|
||||
}
|
||||
|
||||
$this->padding[$side] = (int) $val;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, float> $rgbcolor
|
||||
*/
|
||||
protected function getRgbComponent(array $rgbcolor, string $channel): float
|
||||
{
|
||||
return $rgbcolor[$channel] ?? 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the color of the bars.
|
||||
* An empty or transparent foreground color is rejected with a BarcodeException.
|
||||
*
|
||||
* @param string $color Foreground color in Web notation (color name, or hexadecimal code, or CSS syntax)
|
||||
*
|
||||
* @throws ColorException in case of color error
|
||||
* @throws BarcodeException in case of empty or transparent color
|
||||
*/
|
||||
public function setColor(string $color): static
|
||||
{
|
||||
$colobj = $this->getRgbColorObject($color);
|
||||
if (!$colobj instanceof \Com\Tecnick\Color\Model\Rgb) {
|
||||
throw new BarcodeException('The foreground color cannot be empty or transparent');
|
||||
}
|
||||
|
||||
$this->color_obj = $colobj;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the background color
|
||||
*
|
||||
* @param string $color Background color in Web notation (color name, or hexadecimal code, or CSS syntax)
|
||||
*
|
||||
* @throws ColorException in case of color error
|
||||
*/
|
||||
public function setBackgroundColor(string $color): static
|
||||
{
|
||||
$this->bg_color_obj = $this->getRgbColorObject($color);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the RGB Color object for the given color representation
|
||||
*
|
||||
* @param string $color Color in Web notation (color name, or hexadecimal code, or CSS syntax)
|
||||
*
|
||||
* @throws ColorException in case of color error
|
||||
*/
|
||||
protected function getRgbColorObject(string $color): ?Rgb
|
||||
{
|
||||
$pdf = new Pdf();
|
||||
$cobj = $pdf->getColorObject($color);
|
||||
if ($cobj instanceof \Com\Tecnick\Color\Model) {
|
||||
return new Rgb($cobj->toRgbArray());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the barcode raw array
|
||||
*
|
||||
* @return array{
|
||||
* 'type': string,
|
||||
* 'format': string,
|
||||
* 'params': array<int|float|string>,
|
||||
* 'code': string,
|
||||
* 'extcode': string,
|
||||
* 'ncols': int,
|
||||
* 'nrows': int,
|
||||
* 'width': int,
|
||||
* 'height': int,
|
||||
* 'width_ratio': float,
|
||||
* 'height_ratio': float,
|
||||
* 'padding': array{'T': int, 'R': int, 'B': int, 'L': int},
|
||||
* 'full_width': int,
|
||||
* 'full_height': int,
|
||||
* 'color_obj': Rgb,
|
||||
* 'bg_color_obj': ?Rgb,
|
||||
* 'bars': array<array{int, int, int, int}>,
|
||||
* }
|
||||
*/
|
||||
public function getArray(): array
|
||||
{
|
||||
return [
|
||||
'type' => $this::TYPE,
|
||||
'format' => $this::FORMAT,
|
||||
'params' => $this->params,
|
||||
'code' => $this->code,
|
||||
'extcode' => $this->extcode,
|
||||
'ncols' => $this->ncols,
|
||||
'nrows' => $this->nrows,
|
||||
'width' => $this->width,
|
||||
'height' => $this->height,
|
||||
'width_ratio' => $this->width_ratio,
|
||||
'height_ratio' => $this->height_ratio,
|
||||
'padding' => $this->padding,
|
||||
'full_width' => $this->width + $this->padding['L'] + $this->padding['R'],
|
||||
'full_height' => $this->height + $this->padding['T'] + $this->padding['B'],
|
||||
'color_obj' => $this->color_obj,
|
||||
'bg_color_obj' => $this->bg_color_obj,
|
||||
'bars' => $this->bars,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the extended code (code + checksum)
|
||||
*/
|
||||
public function getExtendedCode(): string
|
||||
{
|
||||
return $this->extcode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the data as file to the browser.
|
||||
*
|
||||
* @param string $data The file data.
|
||||
* @param string $mime The file MIME type (i.e. 'application/svg+xml' or 'image/png').
|
||||
* @param string $fileext The file extension (i.e. 'svg' or 'png').
|
||||
* @param string|null $filename The file name without extension (optional).
|
||||
* Only allows alphanumeric characters, underscores and hyphens.
|
||||
* Defaults to a md5 hash of the data.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function getHTTPFile(string $data, string $mime, string $fileext, ?string $filename = null): void
|
||||
{
|
||||
if (\is_null($filename) || \preg_match('/^[a-zA-Z0-9_\-]{1,250}$/', $filename) !== 1) {
|
||||
$filename = \md5($data);
|
||||
}
|
||||
|
||||
\header('Content-Type: ' . $mime);
|
||||
\header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
|
||||
\header('Pragma: public');
|
||||
\header('Expires: Thu, 04 jan 1973 00:00:00 GMT'); // Date in the past
|
||||
\header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
\header('Content-Disposition: inline; filename="' . $filename . '.' . $fileext . '";');
|
||||
if (($_SERVER['HTTP_ACCEPT_ENCODING'] ?? null) === null) {
|
||||
// the content length may vary if the server is using compression
|
||||
\header('Content-Length: ' . \strlen($data));
|
||||
}
|
||||
|
||||
echo $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the barcode as SVG image object.
|
||||
*
|
||||
* @param string|null $filename The file name without extension (optional).
|
||||
* Only allows alphanumeric characters, underscores and hyphens.
|
||||
* Defaults to a md5 hash of the data.
|
||||
* The file extension is always '.svg'.
|
||||
*/
|
||||
public function getSvg(?string $filename = null): void
|
||||
{
|
||||
$this->getHTTPFile($this->getSvgCode(), 'application/svg+xml', 'svg', $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the barcode as inline SVG code.
|
||||
*
|
||||
* @return string Inline SVG code.
|
||||
*/
|
||||
public function getInlineSvgCode(): string
|
||||
{
|
||||
// flags for htmlspecialchars
|
||||
$hflag = ENT_NOQUOTES;
|
||||
if (\defined('ENT_XML1') && \defined('ENT_DISALLOWED')) {
|
||||
$hflag = ENT_XML1 | ENT_DISALLOWED;
|
||||
}
|
||||
|
||||
$width = \sprintf('%F', $this->width + $this->padding['L'] + $this->padding['R']);
|
||||
$height = \sprintf('%F', $this->height + $this->padding['T'] + $this->padding['B']);
|
||||
|
||||
$svg =
|
||||
'<svg'
|
||||
. ' version="1.2"'
|
||||
. ' baseProfile="full"'
|
||||
. ' xmlns="http://www.w3.org/2000/svg"'
|
||||
. ' xmlns:xlink="http://www.w3.org/1999/xlink"'
|
||||
. ' xmlns:ev="http://www.w3.org/2001/xml-events"'
|
||||
. ' width="'
|
||||
. $width
|
||||
. '"'
|
||||
. ' height="'
|
||||
. $height
|
||||
. '"'
|
||||
. ' viewBox="0 0 '
|
||||
. $width
|
||||
. ' '
|
||||
. $height
|
||||
. '"'
|
||||
. '>'
|
||||
. "\n"
|
||||
. "\t"
|
||||
. '<desc>'
|
||||
. \htmlspecialchars($this->code, $hflag, 'UTF-8')
|
||||
. '</desc>'
|
||||
. "\n";
|
||||
if ($this->bg_color_obj instanceof \Com\Tecnick\Color\Model\Rgb) {
|
||||
$svg .=
|
||||
' <rect x="0" y="0" width="'
|
||||
. $width
|
||||
. '"'
|
||||
. ' height="'
|
||||
. $height
|
||||
. '"'
|
||||
. ' fill="'
|
||||
. $this->bg_color_obj->getRgbHexColor()
|
||||
. '"'
|
||||
. ' stroke="none"'
|
||||
. ' stroke-width="0"'
|
||||
. ' stroke-linecap="square"'
|
||||
. ' />'
|
||||
. "\n";
|
||||
}
|
||||
|
||||
$svg .=
|
||||
' <g id="bars" fill="'
|
||||
. $this->color_obj->getRgbHexColor()
|
||||
. '"'
|
||||
. ' stroke="none"'
|
||||
. ' stroke-width="0"'
|
||||
. ' stroke-linecap="square"'
|
||||
. '>'
|
||||
. "\n";
|
||||
$bars = $this->getBarsArrayXYWH();
|
||||
foreach ($bars as $bar) {
|
||||
$svg .= \sprintf(
|
||||
' <rect x="%F" y="%F" width="%F" height="%F" />' . "\n",
|
||||
$bar[0],
|
||||
$bar[1],
|
||||
$bar[2],
|
||||
$bar[3],
|
||||
);
|
||||
}
|
||||
|
||||
return $svg . (' </g>' . "\n" . '</svg>' . "\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the barcode as SVG code, including the XML declaration.
|
||||
*
|
||||
* @return string SVG code
|
||||
*/
|
||||
public function getSvgCode(): string
|
||||
{
|
||||
return '<?xml version="1.0" standalone="no" ?>' . "\n" . $this->getInlineSvgCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an HTML representation of the barcode.
|
||||
*
|
||||
* @return string HTML code (DIV block)
|
||||
*/
|
||||
public function getHtmlDiv(): string
|
||||
{
|
||||
$html = \sprintf(
|
||||
'<div style="width:%Fpx;height:%Fpx;position:relative;font-size:0;border:none;padding:0;margin:0;',
|
||||
$this->width + $this->padding['L'] + $this->padding['R'],
|
||||
$this->height + $this->padding['T'] + $this->padding['B'],
|
||||
);
|
||||
if ($this->bg_color_obj instanceof \Com\Tecnick\Color\Model\Rgb) {
|
||||
$html .= 'background-color:' . $this->bg_color_obj->getCssColor() . ';';
|
||||
}
|
||||
|
||||
$html .= '">' . "\n";
|
||||
$bars = $this->getBarsArrayXYWH();
|
||||
foreach ($bars as $bar) {
|
||||
$html .= \sprintf(
|
||||
' <div style="background-color:%s;left:%Fpx;top:%Fpx;width:%Fpx;height:%Fpx;position:absolute;border:none;padding:0;margin:0;"> </div>'
|
||||
. "\n",
|
||||
$this->color_obj->getCssColor(),
|
||||
$bar[0],
|
||||
$bar[1],
|
||||
$bar[2],
|
||||
$bar[3],
|
||||
);
|
||||
}
|
||||
|
||||
return $html . ('</div>' . "\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Barcode as PNG Image (requires GD or Imagick library)
|
||||
*
|
||||
* @param string|null $filename The file name without extension (optional).
|
||||
* Only allows alphanumeric characters, underscores and hyphens.
|
||||
* Defaults to a md5 hash of the data.
|
||||
* The file extension is always '.png'.
|
||||
*
|
||||
* @throws BarcodeException in case image generation fails
|
||||
*/
|
||||
public function getPng(?string $filename = null): void
|
||||
{
|
||||
$this->getHTTPFile($this->getPngData(), 'image/png', 'png', $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the barcode as PNG image (requires GD or Imagick library)
|
||||
*
|
||||
* @param bool $imagick If true try to use the Imagick extension
|
||||
*
|
||||
* @return string PNG image data
|
||||
*
|
||||
* @throws BarcodeException in case image generation fails
|
||||
*/
|
||||
public function getPngData(bool $imagick = true): string
|
||||
{
|
||||
if ($imagick && \extension_loaded('imagick')) {
|
||||
return $this->getPngDataImagick();
|
||||
}
|
||||
|
||||
$gdImage = $this->getGd();
|
||||
\ob_start();
|
||||
\imagepng($gdImage);
|
||||
$data = \ob_get_clean();
|
||||
if ($data === false) {
|
||||
throw new BarcodeException('Unable to get PNG data');
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximum width or height, in pixels, of a rendered barcode image.
|
||||
* Guards against pathological size multipliers triggering huge allocations.
|
||||
*/
|
||||
protected const MAX_IMAGE_SIDE = 30_000;
|
||||
|
||||
/**
|
||||
* Compute and validate the rendered image dimensions, in pixels.
|
||||
*
|
||||
* @return array{int, int} [width, height], each at least 1 pixel
|
||||
*
|
||||
* @throws BarcodeException if the requested image size is too large
|
||||
*/
|
||||
protected function getImageSize(): array
|
||||
{
|
||||
$width = \max(1, (int) \ceil($this->width + $this->padding['L'] + $this->padding['R']));
|
||||
$height = \max(1, (int) \ceil($this->height + $this->padding['T'] + $this->padding['B']));
|
||||
if ($width > self::MAX_IMAGE_SIDE || $height > self::MAX_IMAGE_SIDE) {
|
||||
throw new BarcodeException(
|
||||
'The requested image size ('
|
||||
. $width
|
||||
. 'x'
|
||||
. $height
|
||||
. ' px) exceeds the maximum of '
|
||||
. self::MAX_IMAGE_SIDE
|
||||
. ' px per side',
|
||||
);
|
||||
}
|
||||
|
||||
return [$width, $height];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the barcode as PNG image (requires Imagick library)
|
||||
*
|
||||
* @throws BarcodeException if the Imagick library is not installed or the image is too large
|
||||
*/
|
||||
public function getPngDataImagick(): string
|
||||
{
|
||||
$imagick = new \Imagick();
|
||||
[$width, $height] = $this->getImageSize();
|
||||
$imagick->newImage($width, $height, 'none', 'png');
|
||||
$imagickdraw = new \ImagickDraw();
|
||||
if ($this->bg_color_obj instanceof \Com\Tecnick\Color\Model\Rgb) {
|
||||
$rgbcolor = $this->bg_color_obj->getNormalizedArray(255);
|
||||
$imagickdraw->setfillcolor(
|
||||
'rgb('
|
||||
. (string) $this->getRgbComponent($rgbcolor, 'R')
|
||||
. ','
|
||||
. (string) $this->getRgbComponent($rgbcolor, 'G')
|
||||
. ','
|
||||
. (string) $this->getRgbComponent($rgbcolor, 'B')
|
||||
. ')',
|
||||
);
|
||||
$imagickdraw->rectangle(0, 0, $width, $height);
|
||||
}
|
||||
|
||||
$rgbcolor = $this->color_obj->getNormalizedArray(255);
|
||||
$imagickdraw->setfillcolor(
|
||||
'rgb('
|
||||
. (string) $this->getRgbComponent($rgbcolor, 'R')
|
||||
. ','
|
||||
. (string) $this->getRgbComponent($rgbcolor, 'G')
|
||||
. ','
|
||||
. (string) $this->getRgbComponent($rgbcolor, 'B')
|
||||
. ')',
|
||||
);
|
||||
$bars = $this->getBarsArrayXYXY();
|
||||
foreach ($bars as $bar) {
|
||||
$imagickdraw->rectangle($bar[0], $bar[1], $bar[2], $bar[3]);
|
||||
}
|
||||
|
||||
$imagick->drawimage($imagickdraw);
|
||||
return $imagick->getImageBlob();
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply GD background color/alpha strategy.
|
||||
*
|
||||
* @throws BarcodeException if background allocation fails
|
||||
*/
|
||||
protected function applyGdBackground(\GdImage $img, int $width, int $height): void
|
||||
{
|
||||
$bgColorObj = $this->bg_color_obj;
|
||||
if ($bgColorObj instanceof \Com\Tecnick\Color\Model\Rgb) {
|
||||
$rgbcolor = $bgColorObj->getNormalizedArray(255);
|
||||
$bg_color = \imagecolorallocate(
|
||||
$img,
|
||||
(int) \round($this->getRgbComponent($rgbcolor, 'R')),
|
||||
(int) \round($this->getRgbComponent($rgbcolor, 'G')),
|
||||
(int) \round($this->getRgbComponent($rgbcolor, 'B')),
|
||||
);
|
||||
if ($bg_color === false) {
|
||||
throw new BarcodeException('Unable to allocate GD background color');
|
||||
}
|
||||
\imagefilledrectangle($img, 0, 0, $width, $height, $bg_color);
|
||||
return;
|
||||
}
|
||||
|
||||
$bgobj = clone $this->color_obj;
|
||||
$rgbcolor = $bgobj->invertColor()->getNormalizedArray(255);
|
||||
$background_color = \imagecolorallocate(
|
||||
$img,
|
||||
(int) \round($this->getRgbComponent($rgbcolor, 'R')),
|
||||
(int) \round($this->getRgbComponent($rgbcolor, 'G')),
|
||||
(int) \round($this->getRgbComponent($rgbcolor, 'B')),
|
||||
);
|
||||
if ($background_color === false) {
|
||||
throw new BarcodeException('Unable to allocate default GD background color');
|
||||
}
|
||||
\imagecolortransparent($img, $background_color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the barcode as GD image object (requires GD library)
|
||||
*
|
||||
* @throws BarcodeException if the GD library is not installed or the image is too large
|
||||
*/
|
||||
public function getGd(): \GdImage
|
||||
{
|
||||
[$width, $height] = $this->getImageSize();
|
||||
$img = \imagecreate($width, $height);
|
||||
if ($img === false) {
|
||||
throw new BarcodeException('Unable to create GD image');
|
||||
}
|
||||
|
||||
$this->applyGdBackground($img, $width, $height);
|
||||
|
||||
$rgbcolor = $this->color_obj->getNormalizedArray(255);
|
||||
$bar_color = \imagecolorallocate(
|
||||
$img,
|
||||
(int) \round($this->getRgbComponent($rgbcolor, 'R')),
|
||||
(int) \round($this->getRgbComponent($rgbcolor, 'G')),
|
||||
(int) \round($this->getRgbComponent($rgbcolor, 'B')),
|
||||
);
|
||||
if ($bar_color === false) {
|
||||
throw new BarcodeException('Unable to allocate GD foreground color');
|
||||
}
|
||||
$bars = $this->getBarsArrayXYXY();
|
||||
foreach ($bars as $bar) {
|
||||
\imagefilledrectangle(
|
||||
$img,
|
||||
(int) \floor($bar[0]),
|
||||
(int) \floor($bar[1]),
|
||||
(int) \floor($bar[2]),
|
||||
(int) \floor($bar[3]),
|
||||
$bar_color,
|
||||
);
|
||||
}
|
||||
|
||||
return $img;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a raw barcode string representation using characters
|
||||
*
|
||||
* @param string $space_char Character or string to use for filling empty spaces
|
||||
* @param string $bar_char Character or string to use for filling bars
|
||||
*/
|
||||
public function getGrid(string $space_char = '0', string $bar_char = '1'): string
|
||||
{
|
||||
$raw = $this->getGridArray($space_char, $bar_char);
|
||||
$grid = '';
|
||||
foreach ($raw as $row) {
|
||||
$grid .= \implode('', $row) . "\n";
|
||||
}
|
||||
|
||||
return $grid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array containing all the formatted bars coordinates
|
||||
*
|
||||
* @return array<int, array{float, float, float, float}>
|
||||
*/
|
||||
public function getBarsArrayXYXY(): array
|
||||
{
|
||||
$rect = [];
|
||||
foreach ($this->bars as $bar) {
|
||||
if ($bar[2] <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($bar[3] <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rect[] = $this->getBarRectXYXY($bar);
|
||||
}
|
||||
|
||||
if ($this->nrows > 1) {
|
||||
// reprint rotated to cancel row gaps
|
||||
$rot = $this->getRotatedBarArray();
|
||||
foreach ($rot as $bar) {
|
||||
if ($bar[2] <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($bar[3] <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rect[] = $this->getBarRectXYXY($bar);
|
||||
}
|
||||
}
|
||||
|
||||
return $rect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array containing all the formatted bars coordinates
|
||||
*
|
||||
* @return array<int, array{float, float, float, float}>
|
||||
*/
|
||||
public function getBarsArrayXYWH(): array
|
||||
{
|
||||
$rect = [];
|
||||
foreach ($this->bars as $bar) {
|
||||
if ($bar[2] <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($bar[3] <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rect[] = $this->getBarRectXYWH($bar);
|
||||
}
|
||||
|
||||
if ($this->nrows > 1) {
|
||||
// reprint rotated to cancel row gaps
|
||||
$rot = $this->getRotatedBarArray();
|
||||
foreach ($rot as $bar) {
|
||||
if ($bar[2] <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($bar[3] <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rect[] = $this->getBarRectXYWH($bar);
|
||||
}
|
||||
}
|
||||
|
||||
return $rect;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Convert.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
use Com\Tecnick\Barcode\Math;
|
||||
use Com\Tecnick\Color\Model\Rgb as Color;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Convert
|
||||
*
|
||||
* Barcode Convert class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Convert
|
||||
{
|
||||
/**
|
||||
* Barcode type (linear or square)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const TYPE = '';
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = '';
|
||||
|
||||
/**
|
||||
* Array containing extra parameters for the specified barcode type
|
||||
*
|
||||
* @var array<int|float|string>
|
||||
*/
|
||||
protected array $params = [];
|
||||
|
||||
/**
|
||||
* Code to convert (barcode content)
|
||||
*/
|
||||
protected string $code = '';
|
||||
|
||||
/**
|
||||
* Resulting code after applying checksum etc.
|
||||
*/
|
||||
protected string $extcode = '';
|
||||
|
||||
/**
|
||||
* Total number of columns
|
||||
*/
|
||||
protected int $ncols = 0;
|
||||
|
||||
/**
|
||||
* Total number of rows
|
||||
*/
|
||||
protected int $nrows = 1;
|
||||
|
||||
/**
|
||||
* Array containing the position and dimensions of each barcode bar
|
||||
* (x, y, width, height)
|
||||
*
|
||||
* @var array<array{int, int, int, int}>
|
||||
*/
|
||||
protected array $bars = [];
|
||||
|
||||
/**
|
||||
* Barcode width
|
||||
*/
|
||||
protected int $width = 0;
|
||||
|
||||
/**
|
||||
* Barcode height
|
||||
*/
|
||||
protected int $height = 0;
|
||||
|
||||
/**
|
||||
* Additional padding to add around the barcode (top, right, bottom, left) in user units.
|
||||
* A negative value indicates the multiplication factor for each row or column.
|
||||
*
|
||||
* @var array{'T': int, 'R': int, 'B': int, 'L': int}
|
||||
*/
|
||||
protected array $padding = [
|
||||
'T' => 0,
|
||||
'R' => 0,
|
||||
'B' => 0,
|
||||
'L' => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
* Ratio between the barcode width and the number of columns
|
||||
*/
|
||||
protected float $width_ratio = 0;
|
||||
|
||||
/**
|
||||
* Ratio between the barcode height and the number of rows
|
||||
*/
|
||||
protected float $height_ratio = 0;
|
||||
|
||||
/**
|
||||
* Foreground Color object
|
||||
*/
|
||||
protected Color $color_obj;
|
||||
|
||||
/**
|
||||
* Background Color object
|
||||
*/
|
||||
protected ?Color $bg_color_obj = null;
|
||||
|
||||
/**
|
||||
* Process binary sequence rows.
|
||||
*
|
||||
* @param array<int, string|array<int>> $rows Binary sequence data to process
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function processBinarySequence(array $rows): void
|
||||
{
|
||||
if ($rows === []) {
|
||||
throw new BarcodeException('Empty input string');
|
||||
}
|
||||
|
||||
$this->nrows = \count($rows);
|
||||
$firstRow = $rows[0] ?? '';
|
||||
$this->ncols = \is_array($firstRow) ? \count($firstRow) : \strlen($firstRow);
|
||||
|
||||
if ($this->ncols === 0) {
|
||||
throw new BarcodeException('Empty columns');
|
||||
}
|
||||
|
||||
$this->bars = [];
|
||||
foreach ($rows as $posy => $row) {
|
||||
if (!\is_array($row)) {
|
||||
$row = \str_split($row, 1);
|
||||
}
|
||||
|
||||
$prevcol = '';
|
||||
$bar_width = 0;
|
||||
$row[] = '0';
|
||||
for ($posx = 0; $posx <= $this->ncols; ++$posx) {
|
||||
if (($row[$posx] ?? '0') !== $prevcol) {
|
||||
if ($prevcol === '1') {
|
||||
$this->bars[] = [$posx - $bar_width, $posy, $bar_width, 1];
|
||||
}
|
||||
|
||||
$bar_width = 0;
|
||||
}
|
||||
|
||||
++$bar_width;
|
||||
$prevcol = (string) ($row[$posx] ?? '0');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract rows from a binary sequence of comma-separated 01 strings.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*
|
||||
* @throws BarcodeException in case of invalid input pattern
|
||||
*/
|
||||
protected function getRawCodeRows(string $data): array
|
||||
{
|
||||
$search = [
|
||||
'/[\s]*/s', // remove spaces and newlines
|
||||
'/^[\[,]+/', // remove leading brackets or commas
|
||||
'/[\],]+$/', // remove trailing brackets or commas
|
||||
'/[\]][\[]/', // convert bracket-separated rows to comma-separated
|
||||
];
|
||||
|
||||
$replace = ['', '', '', ','];
|
||||
|
||||
$code = \preg_replace($search, $replace, $data);
|
||||
if ($code === null) {
|
||||
throw new BarcodeException('Invalid input string');
|
||||
}
|
||||
|
||||
return \explode(',', $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two non-negative decimal integers.
|
||||
*
|
||||
* @param numeric-string $left First operand
|
||||
* @param numeric-string $right Second operand
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
protected function addNumeric(string $left, string $right): string
|
||||
{
|
||||
return Math::add($left, $right);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two non-negative decimal integers.
|
||||
*
|
||||
* @param numeric-string $left First operand
|
||||
* @param numeric-string $right Second operand
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
protected function mulNumeric(string $left, string $right): string
|
||||
{
|
||||
return Math::mul($left, $right);
|
||||
}
|
||||
|
||||
/**
|
||||
* Integer division of two non-negative decimal integers.
|
||||
*
|
||||
* @param numeric-string $left Dividend
|
||||
* @param numeric-string $right Divisor
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
protected function divNumeric(string $left, string $right): string
|
||||
{
|
||||
return Math::div($left, $right);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remainder of the integer division of two non-negative decimal integers.
|
||||
*
|
||||
* @param numeric-string $left Dividend
|
||||
* @param numeric-string $right Divisor
|
||||
*/
|
||||
protected function modNumeric(string $left, string $right): int
|
||||
{
|
||||
return (int) Math::mod($left, $right);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert large integer number to hexadecimal representation.
|
||||
*
|
||||
* @param string $number Number to convert (as string)
|
||||
*
|
||||
* @return string hexadecimal representation
|
||||
*/
|
||||
protected function convertDecToHex(string $number): string
|
||||
{
|
||||
if (!\preg_match('/^[0-9]+$/', $number)) {
|
||||
return '00';
|
||||
}
|
||||
|
||||
$number = \ltrim($number, '0');
|
||||
if ($number === '') {
|
||||
return '00';
|
||||
}
|
||||
|
||||
/** @var numeric-string $number */
|
||||
$hex = [];
|
||||
while ($number !== '0') {
|
||||
$hex[] = \strtoupper(\dechex($this->modNumeric($number, '16')));
|
||||
$number = $this->divNumeric($number, '16');
|
||||
}
|
||||
|
||||
$hex = \array_reverse($hex);
|
||||
return \implode('', $hex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert large hexadecimal number to decimal representation (string).
|
||||
*
|
||||
* @param string $hex Hexadecimal number to convert (as string)
|
||||
*
|
||||
* @return numeric-string decimal representation
|
||||
*/
|
||||
protected function convertHexToDec(string $hex): string
|
||||
{
|
||||
$dec = '0';
|
||||
$bitval = '1';
|
||||
$len = \strlen($hex);
|
||||
for ($pos = $len - 1; $pos >= 0; --$pos) {
|
||||
$dec = $this->addNumeric($dec, $this->mulNumeric((string) \hexdec($hex[$pos]), $bitval));
|
||||
$bitval = $this->mulNumeric($bitval, '16');
|
||||
}
|
||||
|
||||
return $dec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a raw barcode grid array
|
||||
*
|
||||
* @param string $space_char Character or string to use for filling empty spaces
|
||||
* @param string $bar_char Character or string to use for filling bars
|
||||
*
|
||||
* @return array<int, array<int, string>>
|
||||
*/
|
||||
public function getGridArray(string $space_char = '0', string $bar_char = '1'): array
|
||||
{
|
||||
$raw = \array_fill(0, \max(0, $this->nrows), \array_fill(0, \max(0, $this->ncols), $space_char));
|
||||
foreach ($this->bars as $bar) {
|
||||
if ($bar[2] <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($bar[3] <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for ($vert = 0; $vert < $bar[3]; ++$vert) {
|
||||
for ($horiz = 0; $horiz < $bar[2]; ++$horiz) {
|
||||
$raw[$bar[1] + $vert][$bar[0] + $horiz] = $bar_char;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bars array ordered by columns
|
||||
*
|
||||
* @return array<int, array{int, int, int, int}>
|
||||
*/
|
||||
protected function getRotatedBarArray(): array
|
||||
{
|
||||
$grid = $this->getGridArray();
|
||||
if ($grid === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$cols = \array_map(null, ...$grid);
|
||||
$bars = [];
|
||||
foreach ($cols as $posx => $col) {
|
||||
$prevrow = '';
|
||||
$bar_height = 0;
|
||||
$col[] = '0';
|
||||
for ($posy = 0; $posy <= $this->nrows; ++$posy) {
|
||||
if (($col[$posy] ?? '0') !== $prevrow) {
|
||||
if ($prevrow === '1') {
|
||||
$bars[] = [$posx, $posy - $bar_height, 1, $bar_height];
|
||||
}
|
||||
|
||||
$bar_height = 0;
|
||||
}
|
||||
|
||||
++$bar_height;
|
||||
$prevrow = $col[$posy] ?? '0';
|
||||
}
|
||||
}
|
||||
|
||||
return $bars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the adjusted rectangular coordinates (x1,y1,x2,y2) for the specified bar
|
||||
*
|
||||
* @param array{int, int, int, int} $bar Raw bar coordinates
|
||||
*
|
||||
* @return array{float, float, float, float} Bar coordinates
|
||||
*/
|
||||
protected function getBarRectXYXY(array $bar): array
|
||||
{
|
||||
return [
|
||||
$this->padding['L'] + ($bar[0] * $this->width_ratio),
|
||||
$this->padding['T'] + ($bar[1] * $this->height_ratio),
|
||||
$this->padding['L'] + (($bar[0] + $bar[2]) * $this->width_ratio) - 1,
|
||||
$this->padding['T'] + (($bar[1] + $bar[3]) * $this->height_ratio) - 1,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the adjusted rectangular coordinates (x,y,w,h) for the specified bar
|
||||
*
|
||||
* @param array{int, int, int, int} $bar Raw bar coordinates
|
||||
*
|
||||
* @return array{float, float, float, float} Bar coordinates
|
||||
*/
|
||||
protected function getBarRectXYWH(array $bar): array
|
||||
{
|
||||
return [
|
||||
$this->padding['L'] + ($bar[0] * $this->width_ratio),
|
||||
$this->padding['T'] + ($bar[1] * $this->height_ratio),
|
||||
$bar[2] * $this->width_ratio,
|
||||
$bar[3] * $this->height_ratio,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Linear.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear
|
||||
*
|
||||
* Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Linear extends \Com\Tecnick\Barcode\Type
|
||||
{
|
||||
/**
|
||||
* Barcode type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const TYPE = 'linear';
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Codabar.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\Codabar;
|
||||
*
|
||||
* Codabar Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Codabar extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
protected function getBarWidth(string $char, int $pos): int
|
||||
{
|
||||
$pattern = $this::CHBAR[$char] ?? '11111111';
|
||||
return (int) ($pattern[$pos] ?? '1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'CODABAR';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'0' => '11111221',
|
||||
'1' => '11112211',
|
||||
'2' => '11121121',
|
||||
'3' => '22111111',
|
||||
'4' => '11211211',
|
||||
'5' => '21111211',
|
||||
'6' => '12111121',
|
||||
'7' => '12112111',
|
||||
'8' => '12211111',
|
||||
'9' => '21121111',
|
||||
'-' => '11122111',
|
||||
'$' => '11221111',
|
||||
':' => '21112121',
|
||||
'/' => '21211121',
|
||||
'.' => '21212111',
|
||||
'+' => '11222221',
|
||||
'A' => '11221211',
|
||||
'B' => '12121121',
|
||||
'C' => '11121221',
|
||||
'D' => '11122211',
|
||||
];
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = 'A' . \strtoupper($this->code) . 'A';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 1;
|
||||
$this->bars = [];
|
||||
$this->formatCode();
|
||||
$clen = \strlen($this->extcode);
|
||||
for ($chr = 0; $chr < $clen; ++$chr) {
|
||||
$char = $this->extcode[$chr];
|
||||
if (!\array_key_exists($char, $this::CHBAR)) {
|
||||
throw new BarcodeException('Invalid character: ' . (\ord($char) & 0xFF));
|
||||
}
|
||||
|
||||
for ($pos = 0; $pos < 8; ++$pos) {
|
||||
$bar_width = $this->getBarWidth($char, $pos);
|
||||
if (($pos % 2) === 0) {
|
||||
$this->bars[] = [$this->ncols, 0, $bar_width, 1];
|
||||
}
|
||||
|
||||
$this->ncols += $bar_width;
|
||||
}
|
||||
}
|
||||
|
||||
--$this->ncols;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CodeNineThree.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeNineThree;
|
||||
*
|
||||
* CodeNineThree Barcode type class
|
||||
* CODE 93 - USS-93
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class CodeNineThree extends \Com\Tecnick\Barcode\Type\Linear\CodeThreeNineExtCheck
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'C93';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
32 => '311211', // space
|
||||
36 => '321111', // $
|
||||
37 => '211131', // %
|
||||
42 => '111141', // start-stop
|
||||
43 => '113121', // +
|
||||
45 => '121131', // -
|
||||
46 => '311112', // .
|
||||
47 => '112131', // /
|
||||
48 => '131112', // 0
|
||||
49 => '111213', // 1
|
||||
50 => '111312', // 2
|
||||
51 => '111411', // 3
|
||||
52 => '121113', // 4
|
||||
53 => '121212', // 5
|
||||
54 => '121311', // 6
|
||||
55 => '111114', // 7
|
||||
56 => '131211', // 8
|
||||
57 => '141111', // 9
|
||||
65 => '211113', // A
|
||||
66 => '211212', // B
|
||||
67 => '211311', // C
|
||||
68 => '221112', // D
|
||||
69 => '221211', // E
|
||||
70 => '231111', // F
|
||||
71 => '112113', // G
|
||||
72 => '112212', // H
|
||||
73 => '112311', // I
|
||||
74 => '122112', // J
|
||||
75 => '132111', // K
|
||||
76 => '111123', // L
|
||||
77 => '111222', // M
|
||||
78 => '111321', // N
|
||||
79 => '121122', // O
|
||||
80 => '131121', // P
|
||||
81 => '212112', // Q
|
||||
82 => '212211', // R
|
||||
83 => '211122', // S
|
||||
84 => '211221', // T
|
||||
85 => '221121', // U
|
||||
86 => '222111', // V
|
||||
87 => '112122', // W
|
||||
88 => '112221', // X
|
||||
89 => '122121', // Y
|
||||
90 => '123111', // Z
|
||||
128 => '121221', // ($)
|
||||
129 => '311121', // (/)
|
||||
130 => '122211', // (+)
|
||||
131 => '312111', // (%)
|
||||
];
|
||||
|
||||
/**
|
||||
* Map for extended characters
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected const EXTCODES = [
|
||||
"\x83U",
|
||||
"\x80A",
|
||||
"\x80B",
|
||||
"\x80C",
|
||||
"\x80D",
|
||||
"\x80E",
|
||||
"\x80F",
|
||||
"\x80G",
|
||||
"\x80H",
|
||||
"\x80I",
|
||||
"\x80J",
|
||||
"\x80K",
|
||||
"\x80L",
|
||||
"\x80M",
|
||||
"\x80N",
|
||||
"\x80O",
|
||||
"\x80P",
|
||||
"\x80Q",
|
||||
"\x80R",
|
||||
"\x80S",
|
||||
"\x80T",
|
||||
"\x80U",
|
||||
"\x80V",
|
||||
"\x80W",
|
||||
"\x80X",
|
||||
"\x80Y",
|
||||
"\x80Z",
|
||||
"\x83A",
|
||||
"\x83B",
|
||||
"\x83C",
|
||||
"\x83D",
|
||||
"\x83E",
|
||||
' ',
|
||||
"\x81A",
|
||||
"\x81B",
|
||||
"\x81C",
|
||||
"\x81D",
|
||||
"\x81E",
|
||||
|
||||
"\x81F",
|
||||
"\x81G",
|
||||
"\x81H",
|
||||
"\x81I",
|
||||
"\x81J",
|
||||
"\x81K",
|
||||
"\x81L",
|
||||
'-',
|
||||
'.',
|
||||
"\x81O",
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
"\x81Z",
|
||||
"\x83F",
|
||||
"\x83G",
|
||||
"\x83H",
|
||||
"\x83I",
|
||||
"\x83J",
|
||||
"\x83V",
|
||||
'A',
|
||||
'B',
|
||||
'C',
|
||||
'D',
|
||||
'E',
|
||||
'F',
|
||||
'G',
|
||||
'H',
|
||||
'I',
|
||||
'J',
|
||||
'K',
|
||||
'L',
|
||||
'M',
|
||||
'N',
|
||||
'O',
|
||||
'P',
|
||||
'Q',
|
||||
'R',
|
||||
'S',
|
||||
'T',
|
||||
'U',
|
||||
'V',
|
||||
'W',
|
||||
'X',
|
||||
'Y',
|
||||
'Z',
|
||||
"\x83K",
|
||||
"\x83L",
|
||||
"\x83M",
|
||||
"\x83N",
|
||||
"\x83O",
|
||||
"\x83W",
|
||||
"\x82A",
|
||||
"\x82B",
|
||||
"\x82C",
|
||||
"\x82D",
|
||||
"\x82E",
|
||||
"\x82F",
|
||||
"\x82G",
|
||||
"\x82H",
|
||||
"\x82I",
|
||||
"\x82J",
|
||||
"\x82K",
|
||||
"\x82L",
|
||||
"\x82M",
|
||||
"\x82N",
|
||||
"\x82O",
|
||||
"\x82P",
|
||||
"\x82Q",
|
||||
"\x82R",
|
||||
"\x82S",
|
||||
"\x82T",
|
||||
"\x82U",
|
||||
"\x82V",
|
||||
"\x82W",
|
||||
"\x82X",
|
||||
"\x82Y",
|
||||
"\x82Z",
|
||||
"\x83P",
|
||||
"\x83Q",
|
||||
"\x83R",
|
||||
"\x83S",
|
||||
"\x83T",
|
||||
];
|
||||
|
||||
/**
|
||||
* Characters used for checksum
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected const CHKSUM = [
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
'A',
|
||||
'B',
|
||||
'C',
|
||||
'D',
|
||||
'E',
|
||||
'F',
|
||||
'G',
|
||||
'H',
|
||||
'I',
|
||||
'J',
|
||||
'K',
|
||||
'L',
|
||||
'M',
|
||||
'N',
|
||||
'O',
|
||||
'P',
|
||||
'Q',
|
||||
'R',
|
||||
'S',
|
||||
'T',
|
||||
'U',
|
||||
'V',
|
||||
'W',
|
||||
'X',
|
||||
'Y',
|
||||
'Z',
|
||||
'-',
|
||||
'.',
|
||||
' ',
|
||||
'$',
|
||||
'/',
|
||||
'+',
|
||||
'%',
|
||||
'<',
|
||||
'=',
|
||||
'>',
|
||||
'?',
|
||||
];
|
||||
|
||||
protected function getBarPattern(int $char): string
|
||||
{
|
||||
return $this::CHBAR[$char] ?? '000000';
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate CODE 93 checksum (modulo 47).
|
||||
*
|
||||
* @param string $code Code to represent.
|
||||
*
|
||||
* @return string char checksum.
|
||||
*/
|
||||
protected function getChecksum(string $code): string
|
||||
{
|
||||
// translate special characters
|
||||
$code = \strtr($code, \chr(128) . \chr(131) . \chr(129) . \chr(130), '<=>?');
|
||||
$clen = \strlen($code);
|
||||
// calculate check digit C
|
||||
$pck = 1;
|
||||
$check = 0;
|
||||
for ($idx = $clen - 1; $idx >= 0; --$idx) {
|
||||
$check += $this->getChecksumIndex($code[$idx]) * $pck;
|
||||
++$pck;
|
||||
if ($pck > 20) {
|
||||
$pck = 1;
|
||||
}
|
||||
}
|
||||
|
||||
$check %= 47;
|
||||
$chk = $this->getChecksumChar($check);
|
||||
$code .= $chk;
|
||||
// calculate check digit K
|
||||
$pck = 1;
|
||||
$check = 0;
|
||||
for ($idx = $clen; $idx >= 0; --$idx) {
|
||||
$check += $this->getChecksumIndex($code[$idx]) * $pck;
|
||||
++$pck;
|
||||
if ($pck > 15) {
|
||||
$pck = 1;
|
||||
}
|
||||
}
|
||||
|
||||
$check %= 47;
|
||||
$key = $this->getChecksumChar($check);
|
||||
$checksum = $chk . $key;
|
||||
// restore special characters
|
||||
return \strtr($checksum, '<=>?', \chr(128) . \chr(131) . \chr(129) . \chr(130));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.ExcessiveMethodLength")
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 1;
|
||||
$this->bars = [];
|
||||
$this->formatCode();
|
||||
$clen = \strlen($this->extcode);
|
||||
for ($chr = 0; $chr < $clen; ++$chr) {
|
||||
$char = \ord($this->extcode[$chr]);
|
||||
$pattern = $this->getBarPattern($char);
|
||||
for ($pos = 0; $pos < 6; ++$pos) {
|
||||
$bar_width = (int) ($pattern[$pos] ?? '0');
|
||||
if (($pos % 2) === 0) {
|
||||
$this->bars[] = [$this->ncols, 0, $bar_width, 1];
|
||||
}
|
||||
|
||||
$this->ncols += $bar_width;
|
||||
}
|
||||
}
|
||||
|
||||
$this->bars[] = [$this->ncols, 0, 1, 1];
|
||||
++$this->ncols;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CodeOneOne.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeOneOne;
|
||||
*
|
||||
* CodeOneOne Barcode type class
|
||||
* CODE 11
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class CodeOneOne extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
protected function getBarWidth(string $char, int $pos): int
|
||||
{
|
||||
$pattern = $this::CHBAR[$char] ?? '000000';
|
||||
return (int) ($pattern[$pos] ?? '0');
|
||||
}
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'CODE11';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'0' => '111121',
|
||||
'1' => '211121',
|
||||
'2' => '121121',
|
||||
'3' => '221111',
|
||||
'4' => '112121',
|
||||
'5' => '212111',
|
||||
'6' => '122111',
|
||||
'7' => '111221',
|
||||
'8' => '211211',
|
||||
'9' => '211111',
|
||||
'-' => '112111',
|
||||
'S' => '112211',
|
||||
];
|
||||
|
||||
/**
|
||||
* Calculate the checksum.
|
||||
*
|
||||
* @param string $code Code to represent.
|
||||
*
|
||||
* @return string char checksum.
|
||||
*/
|
||||
protected function getChecksum(string $code): string
|
||||
{
|
||||
$len = \strlen($code);
|
||||
// calculate check digit C
|
||||
$ptr = 1;
|
||||
$cval = 0;
|
||||
for ($pos = $len - 1; $pos >= 0; --$pos) {
|
||||
$digit = $code[$pos];
|
||||
$dval = $digit === '-' ? 10 : (int) $digit;
|
||||
|
||||
$cval += $dval * $ptr;
|
||||
++$ptr;
|
||||
if ($ptr > 10) {
|
||||
$ptr = 1;
|
||||
}
|
||||
}
|
||||
|
||||
$cval %= 11;
|
||||
$ccheck = $cval === 10 ? '-' : (string) $cval;
|
||||
|
||||
if ($len <= 10) {
|
||||
return $ccheck;
|
||||
}
|
||||
|
||||
// calculate check digit K (computed over the code with the C check digit appended)
|
||||
$code .= $ccheck;
|
||||
$klen = \strlen($code);
|
||||
$ptr = 1;
|
||||
$kval = 0;
|
||||
for ($pos = $klen - 1; $pos >= 0; --$pos) {
|
||||
$digit = $code[$pos];
|
||||
$dval = $digit === '-' ? 10 : (int) $digit;
|
||||
|
||||
$kval += $dval * $ptr;
|
||||
++$ptr;
|
||||
if ($ptr > 9) {
|
||||
$ptr = 1;
|
||||
}
|
||||
}
|
||||
|
||||
$kval %= 11;
|
||||
$kcheck = $kval === 10 ? '-' : (string) $kval;
|
||||
|
||||
return $ccheck . $kcheck;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = 'S' . $this->code . $this->getChecksum($this->code) . 'S';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 1;
|
||||
$this->bars = [];
|
||||
$this->formatCode();
|
||||
$clen = \strlen($this->extcode);
|
||||
for ($chr = 0; $chr < $clen; ++$chr) {
|
||||
$char = $this->extcode[$chr];
|
||||
if (!\array_key_exists($char, $this::CHBAR)) {
|
||||
throw new BarcodeException('Invalid character: ' . (\ord($char) & 0xFF));
|
||||
}
|
||||
|
||||
for ($pos = 0; $pos < 6; ++$pos) {
|
||||
$bar_width = $this->getBarWidth($char, $pos);
|
||||
if (($pos % 2) === 0 && $bar_width > 0) {
|
||||
$this->bars[] = [$this->ncols, 0, $bar_width, 1];
|
||||
}
|
||||
|
||||
$this->ncols += $bar_width;
|
||||
}
|
||||
}
|
||||
|
||||
--$this->ncols;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CodeOneTwoEight.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight;
|
||||
*
|
||||
* CodeOneTwoEight Barcode type class
|
||||
* CODE 128
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class CodeOneTwoEight extends \Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight\Process
|
||||
{
|
||||
/**
|
||||
* Get the code point array
|
||||
*
|
||||
* @return array<int, int>
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getCodeData(): array
|
||||
{
|
||||
$code = $this->code;
|
||||
// array of symbols
|
||||
$code_data = [];
|
||||
// split code into sequences
|
||||
$sequence = $this->getNumericSequence($code);
|
||||
// process the sequence
|
||||
$startid = 0;
|
||||
foreach ($sequence as $key => $seq) {
|
||||
switch ($seq[0]) {
|
||||
case 'A':
|
||||
$this->processSequenceA($sequence, $code_data, $startid, $key, $seq);
|
||||
break;
|
||||
case 'B':
|
||||
$this->processSequenceB($sequence, $code_data, $startid, $key, $seq);
|
||||
break;
|
||||
case 'C':
|
||||
$this->processSequenceC($sequence, $code_data, $startid, $key, $seq);
|
||||
break;
|
||||
default:
|
||||
throw new BarcodeException('Invalid sequence mode');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->finalizeCodeData($code_data, $startid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{0: string, 1: string, 2: int, 3?: string}> $sequence
|
||||
*/
|
||||
protected function getSequenceMode(array $sequence, int $key): string
|
||||
{
|
||||
return $sequence[$key][0] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{0: string, 1: string, 2: int, 3?: string}> $sequence
|
||||
*/
|
||||
protected function hasSequenceShift(array $sequence, int $key): bool
|
||||
{
|
||||
return ($sequence[$key][3] ?? null) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the A sequence
|
||||
*
|
||||
* @param array<int, array{0: string, 1: string, 2: int, 3?: string}> $sequence Sequence to process
|
||||
* @param array<int, int> $code_data Array of codepoints to alter
|
||||
* @param int $startid Start ID
|
||||
* @param int $key Sequence current key
|
||||
* @param array{string, string, int} $seq Sequence current value
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function processSequenceA(array &$sequence, array &$code_data, int &$startid, int $key, array $seq): void
|
||||
{
|
||||
$prev_mode = $this->getSequenceMode($sequence, $key - 1);
|
||||
if ($key === 0) {
|
||||
$startid = 103;
|
||||
}
|
||||
|
||||
if ($key !== 0 && $prev_mode !== 'A') {
|
||||
$hasPrevShift = $this->hasSequenceShift($sequence, $key - 1);
|
||||
$singleShift = $seq[2] === 1 && $prev_mode === 'B' && !$hasPrevShift;
|
||||
$codeSwitch = match (true) {
|
||||
$singleShift => 98,
|
||||
!$hasPrevShift => 101,
|
||||
default => null,
|
||||
};
|
||||
|
||||
if ($codeSwitch !== null) {
|
||||
$code_data[] = $codeSwitch;
|
||||
if ($codeSwitch === 98) {
|
||||
// mark single shift
|
||||
$sequence[$key][3] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->getCodeDataA($code_data, $seq[1], (int) $seq[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the B sequence
|
||||
*
|
||||
* @param array<int, array{0: string, 1: string, 2: int, 3?: string}> $sequence Sequence to process
|
||||
* @param array<int, int> $code_data Array of codepoints to alter
|
||||
* @param int $startid Start ID
|
||||
* @param int $key Sequence current key
|
||||
* @param array{string, string, int} $seq Sequence current value
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function processSequenceB(array &$sequence, array &$code_data, int &$startid, int $key, array $seq): void
|
||||
{
|
||||
$prev_mode = $this->getSequenceMode($sequence, $key - 1);
|
||||
if ($key === 0) {
|
||||
$this->processSequenceBA($sequence, $code_data, $startid, $key, $seq);
|
||||
}
|
||||
|
||||
if ($key !== 0 && $prev_mode !== 'B') {
|
||||
$this->processSequenceBB($sequence, $code_data, $key, $seq);
|
||||
}
|
||||
|
||||
$this->getCodeDataB($code_data, $seq[1], (int) $seq[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the B-A sequence
|
||||
*
|
||||
* @param array<int, array{0: string, 1: string, 2: int, 3?: string}> $sequence Sequence to process
|
||||
* @param array<int, int> $code_data Array of codepoints to alter
|
||||
* @param int $startid Start ID
|
||||
* @param int $key Sequence current key
|
||||
* @param array{string, string, int} $seq Sequence current value
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function processSequenceBA(array &$sequence, array &$code_data, int &$startid, int $key, array $seq): void
|
||||
{
|
||||
$tmpchr = \ord($seq[1][0]);
|
||||
$next_mode = $this->getSequenceMode($sequence, $key + 1);
|
||||
$startid = 104;
|
||||
if ($seq[2] === 1 && $tmpchr >= 241 && $tmpchr <= 244 && $next_mode !== '' && $next_mode !== 'B') {
|
||||
switch ($next_mode) {
|
||||
case 'A':
|
||||
$startid = 103;
|
||||
$sequence[$key][0] = 'A';
|
||||
$code_data[] = $this->getFncAValue($tmpchr);
|
||||
break;
|
||||
case 'C':
|
||||
$startid = 105;
|
||||
$sequence[$key][0] = 'C';
|
||||
$code_data[] = $this->getFncAValue($tmpchr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the B-B sequence
|
||||
*
|
||||
* @param array<int, array{0: string, 1: string, 2: int, 3?: string}> $sequence Sequence to process
|
||||
* @param array<int, int> $code_data Array of codepoints to alter
|
||||
* @param int $key Sequence current key
|
||||
* @param array{string, string, int} $seq Sequence current value
|
||||
*/
|
||||
protected function processSequenceBB(array &$sequence, array &$code_data, int $key, array $seq): void
|
||||
{
|
||||
$prev_mode = $this->getSequenceMode($sequence, $key - 1);
|
||||
$hasPrevShift = $this->hasSequenceShift($sequence, $key - 1);
|
||||
$singleShift = $seq[2] === 1 && $prev_mode === 'A' && !$hasPrevShift;
|
||||
$codeSwitch = match (true) {
|
||||
$singleShift => 98,
|
||||
!$hasPrevShift => 100,
|
||||
default => null,
|
||||
};
|
||||
|
||||
if ($codeSwitch !== null) {
|
||||
$code_data[] = $codeSwitch;
|
||||
if ($codeSwitch === 98) {
|
||||
// mark single shift
|
||||
$sequence[$key][3] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the C sequence
|
||||
*
|
||||
* @param array<int, array{0: string, 1: string, 2: int, 3?: string}> $sequence Sequence to process
|
||||
* @param array<int, int> $code_data Array of codepoints to alter
|
||||
* @param int $startid Start ID
|
||||
* @param int $key Sequence current key
|
||||
* @param array{string, string, int} $seq Sequence current value
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function processSequenceC(array &$sequence, array &$code_data, int &$startid, int $key, array $seq): void
|
||||
{
|
||||
$prev_mode = $this->getSequenceMode($sequence, $key - 1);
|
||||
if ($key === 0) {
|
||||
$startid = 105;
|
||||
}
|
||||
|
||||
if ($key !== 0 && $prev_mode !== 'C') {
|
||||
$code_data[] = 99;
|
||||
}
|
||||
|
||||
$this->getCodeDataC($code_data, $seq[1]);
|
||||
}
|
||||
|
||||
protected function getBarPattern(int $value): string
|
||||
{
|
||||
return $this::CHBAR[$value] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$code_data = $this->getCodeData();
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 1;
|
||||
$this->bars = [];
|
||||
foreach ($code_data as $val) {
|
||||
$seq = $this->getBarPattern($val);
|
||||
for ($pos = 0; $pos < 6; ++$pos) {
|
||||
$bar_width = (int) ($seq[$pos] ?? '0');
|
||||
if (($pos % 2) === 0 && $bar_width > 0) {
|
||||
$this->bars[] = [$this->ncols, 0, $bar_width, 1];
|
||||
}
|
||||
|
||||
$this->ncols += $bar_width;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CodeOneTwoEightA.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight\CodeOneTwoEightA;
|
||||
*
|
||||
* CodeOneTwoEightA Barcode type class
|
||||
* CODE 128 A
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class CodeOneTwoEightA extends \Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'C128A';
|
||||
|
||||
/**
|
||||
* Get the code point array
|
||||
*
|
||||
* @return array<int, int>
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getCodeData(): array
|
||||
{
|
||||
$code = $this->code;
|
||||
$len = \strlen($code);
|
||||
$code_data = [];
|
||||
$this->getCodeDataA($code_data, $code, $len);
|
||||
return $this->finalizeCodeData($code_data, 103);
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CodeOneTwoEightB.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight\CodeOneTwoEightB;
|
||||
*
|
||||
* CodeOneTwoEightB Barcode type class
|
||||
* CODE 128 B
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class CodeOneTwoEightB extends \Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'C128B';
|
||||
|
||||
/**
|
||||
* Get the code point array
|
||||
*
|
||||
* @return array<int, int>
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getCodeData(): array
|
||||
{
|
||||
$code = $this->code;
|
||||
$len = \strlen($code);
|
||||
$code_data = [];
|
||||
$this->getCodeDataB($code_data, $code, $len);
|
||||
return $this->finalizeCodeData($code_data, 104);
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CodeOneTwoEightC.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight\CodeOneTwoEightC;
|
||||
*
|
||||
* CodeOneTwoEightC Barcode type class
|
||||
* CODE 128 C
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class CodeOneTwoEightC extends \Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'C128C';
|
||||
|
||||
/**
|
||||
* Get the code point array
|
||||
*
|
||||
* @return array<int, int>
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getCodeData(): array
|
||||
{
|
||||
$code = $this->code;
|
||||
$code_data = [];
|
||||
if ($code !== '' && \ord($code[0]) === 241) {
|
||||
$code_data[] = 102;
|
||||
$code = \substr($code, 1);
|
||||
}
|
||||
|
||||
$this->getCodeDataC($code_data, $code);
|
||||
return $this->finalizeCodeData($code_data, 105);
|
||||
}
|
||||
}
|
||||
+470
@@ -0,0 +1,470 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Process.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight\Process;
|
||||
*
|
||||
* Process methods for CodeOneTwoEight Barcode type class
|
||||
* CODE 128
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Process extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'C128';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'212222', // 00
|
||||
'222122', // 01
|
||||
'222221', // 02
|
||||
'121223', // 03
|
||||
'121322', // 04
|
||||
'131222', // 05
|
||||
'122213', // 06
|
||||
'122312', // 07
|
||||
'132212', // 08
|
||||
'221213', // 09
|
||||
'221312', // 10
|
||||
'231212', // 11
|
||||
'112232', // 12
|
||||
'122132', // 13
|
||||
'122231', // 14
|
||||
'113222', // 15
|
||||
'123122', // 16
|
||||
'123221', // 17
|
||||
'223211', // 18
|
||||
'221132', // 19
|
||||
'221231', // 20
|
||||
'213212', // 21
|
||||
'223112', // 22
|
||||
'312131', // 23
|
||||
'311222', // 24
|
||||
'321122', // 25
|
||||
'321221', // 26
|
||||
'312212', // 27
|
||||
'322112', // 28
|
||||
'322211', // 29
|
||||
'212123', // 30
|
||||
'212321', // 31
|
||||
'232121', // 32
|
||||
'111323', // 33
|
||||
'131123', // 34
|
||||
'131321', // 35
|
||||
'112313', // 36
|
||||
'132113', // 37
|
||||
'132311', // 38
|
||||
'211313', // 39
|
||||
'231113', // 40
|
||||
'231311', // 41
|
||||
'112133', // 42
|
||||
'112331', // 43
|
||||
'132131', // 44
|
||||
'113123', // 45
|
||||
'113321', // 46
|
||||
'133121', // 47
|
||||
'313121', // 48
|
||||
'211331', // 49
|
||||
'231131', // 50
|
||||
'213113', // 51
|
||||
'213311', // 52
|
||||
'213131', // 53
|
||||
'311123', // 54
|
||||
'311321', // 55
|
||||
'331121', // 56
|
||||
'312113', // 57
|
||||
'312311', // 58
|
||||
'332111', // 59
|
||||
'314111', // 60
|
||||
'221411', // 61
|
||||
'431111', // 62
|
||||
'111224', // 63
|
||||
'111422', // 64
|
||||
'121124', // 65
|
||||
'121421', // 66
|
||||
'141122', // 67
|
||||
'141221', // 68
|
||||
'112214', // 69
|
||||
'112412', // 70
|
||||
'122114', // 71
|
||||
'122411', // 72
|
||||
'142112', // 73
|
||||
'142211', // 74
|
||||
'241211', // 75
|
||||
'221114', // 76
|
||||
'413111', // 77
|
||||
'241112', // 78
|
||||
'134111', // 79
|
||||
'111242', // 80
|
||||
'121142', // 81
|
||||
'121241', // 82
|
||||
'114212', // 83
|
||||
'124112', // 84
|
||||
'124211', // 85
|
||||
'411212', // 86
|
||||
'421112', // 87
|
||||
'421211', // 88
|
||||
'212141', // 89
|
||||
'214121', // 90
|
||||
'412121', // 91
|
||||
'111143', // 92
|
||||
'111341', // 93
|
||||
'131141', // 94
|
||||
'114113', // 95
|
||||
'114311', // 96
|
||||
'411113', // 97
|
||||
'411311', // 98
|
||||
'113141', // 99
|
||||
'114131', // 100
|
||||
'311141', // 101
|
||||
'411131', // 102
|
||||
'211412', // 103 START A
|
||||
'211214', // 104 START B
|
||||
'211232', // 105 START C
|
||||
'233111', // STOP
|
||||
'200000', // END
|
||||
];
|
||||
|
||||
/**
|
||||
* Map ASCII characters for code A (ASCII 00 - 95)
|
||||
* // 128A (Code Set A) - ASCII characters 00 to 95 (0-9, A-Z and control codes), special characters
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const KEYS_A =
|
||||
' !"#$%&\'()*+,-./'
|
||||
. '0123456789'
|
||||
. ':;<=>?@'
|
||||
. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
. '[\\]^_'
|
||||
. "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
|
||||
. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
|
||||
|
||||
/**
|
||||
* Map ASCII characters for code B (ASCII 32 - 127)
|
||||
* // 128B (Code Set B) - ASCII characters 32 to 127 (0-9, A-Z, a-z), special characters
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const KEYS_B =
|
||||
' !"#$%&\'()*+,-./'
|
||||
. '0123456789'
|
||||
. ':;<=>?@'
|
||||
. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
. '[\\]^_`'
|
||||
. 'abcdefghijklmnopqrstuvwxyz'
|
||||
. '{|}~'
|
||||
. "\x7F";
|
||||
|
||||
protected function getFncAValue(int $char_id): int
|
||||
{
|
||||
return match ($char_id) {
|
||||
241 => 102,
|
||||
242 => 97,
|
||||
243 => 96,
|
||||
244 => 101,
|
||||
default => 0,
|
||||
};
|
||||
}
|
||||
|
||||
protected function getFncBValue(int $char_id): int
|
||||
{
|
||||
return match ($char_id) {
|
||||
241 => 102,
|
||||
242 => 97,
|
||||
243 => 96,
|
||||
244 => 100,
|
||||
default => 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the numeric sequence (if any)
|
||||
*
|
||||
* @param string $code Code to parse
|
||||
*
|
||||
* @return array<int, array{string, string, int}>
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getNumericSequence(string $code): array
|
||||
{
|
||||
$sequence = [];
|
||||
$len = \strlen($code);
|
||||
$end_offset = 0;
|
||||
$offset = 0;
|
||||
|
||||
while ($offset < $len) {
|
||||
$chr = $code[$offset];
|
||||
$ord = \ord($chr);
|
||||
|
||||
if ($ord < 48 || $ord > 57) {
|
||||
++$offset;
|
||||
continue;
|
||||
}
|
||||
|
||||
$digit_start = $offset;
|
||||
while ($offset < $len) {
|
||||
$digit_chr = $code[$offset];
|
||||
$digit_ord = \ord($digit_chr);
|
||||
if ($digit_ord < 48 || $digit_ord > 57) {
|
||||
break;
|
||||
}
|
||||
|
||||
++$offset;
|
||||
}
|
||||
|
||||
$digit_len = $offset - $digit_start;
|
||||
if ($digit_len < 4) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$num_offset = $digit_start;
|
||||
$num_len = $digit_len;
|
||||
if (($num_len % 2) !== 0) {
|
||||
--$num_len;
|
||||
++$num_offset;
|
||||
}
|
||||
|
||||
if ($num_len <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($num_offset > $end_offset) {
|
||||
$sequence = \array_merge(
|
||||
$sequence,
|
||||
$this->get128ABsequence(\substr($code, $end_offset, $num_offset - $end_offset)),
|
||||
);
|
||||
}
|
||||
|
||||
$sequence[] = ['C', \substr($code, $num_offset, $num_len), $num_len];
|
||||
$end_offset = $num_offset + $num_len;
|
||||
}
|
||||
|
||||
if ($end_offset < $len) {
|
||||
$sequence = \array_merge($sequence, $this->get128ABsequence(\substr($code, $end_offset)));
|
||||
}
|
||||
|
||||
if ($sequence === []) {
|
||||
$sequence[] = ['B', $code, $len];
|
||||
}
|
||||
|
||||
return $sequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split text code in A/B sequence for 128 code
|
||||
*
|
||||
* @param string $code Code to split
|
||||
*
|
||||
* @return array<int, array{string, string, int}>
|
||||
*/
|
||||
protected function get128ABsequence(string $code): array
|
||||
{
|
||||
$len = \strlen($code);
|
||||
$sequence = [];
|
||||
|
||||
$has_a_only = false;
|
||||
for ($pos = 0; $pos < $len; ++$pos) {
|
||||
if (\ord($code[$pos]) >= 32) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$has_a_only = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$has_a_only) {
|
||||
$sequence[] = ['B', $code, $len];
|
||||
|
||||
return $sequence;
|
||||
}
|
||||
|
||||
$end_offset = 0;
|
||||
$pos = 0;
|
||||
while ($pos < $len) {
|
||||
if (\ord($code[$pos]) > 95) {
|
||||
++$pos;
|
||||
continue;
|
||||
}
|
||||
|
||||
$start = $pos;
|
||||
while ($pos < $len && \ord($code[$pos]) <= 95) {
|
||||
++$pos;
|
||||
}
|
||||
|
||||
if ($start > $end_offset) {
|
||||
$slen = $start - $end_offset;
|
||||
$sequence[] = ['B', \substr($code, $end_offset, $slen), $slen];
|
||||
}
|
||||
|
||||
$slen = $pos - $start;
|
||||
$sequence[] = ['A', \substr($code, $start, $slen), $slen];
|
||||
$end_offset = $pos;
|
||||
}
|
||||
|
||||
if ($end_offset < $len) {
|
||||
$slen = $len - $end_offset;
|
||||
$sequence[] = ['B', \substr($code, $end_offset, $slen), $slen];
|
||||
}
|
||||
|
||||
return $sequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the A code point array
|
||||
*
|
||||
* @param array<int, int> $code_data Array of codepoints to alter
|
||||
* @param string $code Code to process
|
||||
* @param int $len Number of characters to process
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getCodeDataA(array &$code_data, string $code, int $len): void
|
||||
{
|
||||
for ($pos = 0; $pos < $len; ++$pos) {
|
||||
$char = $code[$pos];
|
||||
$char_id = \ord($char);
|
||||
if ($char_id >= 241 && $char_id <= 244) {
|
||||
$code_data[] = $this->getFncAValue($char_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($char_id <= 95) {
|
||||
$cdpos = \strpos($this::KEYS_A, $char);
|
||||
$code_data[] = \is_int($cdpos) ? $cdpos : 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new BarcodeException('Invalid character sequence');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the B code point array
|
||||
*
|
||||
* @param array<int, int> $code_data Array of codepoints to alter
|
||||
* @param string $code Code to process
|
||||
* @param int $len Number of characters to process
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getCodeDataB(array &$code_data, string $code, int $len): void
|
||||
{
|
||||
for ($pos = 0; $pos < $len; ++$pos) {
|
||||
$char = $code[$pos];
|
||||
$char_id = \ord($char);
|
||||
if ($char_id >= 241 && $char_id <= 244) {
|
||||
$code_data[] = $this->getFncBValue($char_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($char_id >= 32 && $char_id <= 127) {
|
||||
$cdpos = \strpos($this::KEYS_B, $char);
|
||||
$code_data[] = \is_int($cdpos) ? $cdpos : 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new BarcodeException('Invalid character sequence: ' . $char_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the C code point array
|
||||
*
|
||||
* @param array<int, int> $code_data Array of codepoints to alter
|
||||
* @param string $code Code to process
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getCodeDataC(array &$code_data, string $code): void
|
||||
{
|
||||
// code blocks separated by FNC1 (chr 241)
|
||||
$blocks = \explode(\chr(241), $code);
|
||||
|
||||
foreach ($blocks as $block) {
|
||||
$len = \strlen($block);
|
||||
|
||||
if (($len % 2) !== 0) {
|
||||
throw new BarcodeException('The length of each FNC1-separated code block must be even');
|
||||
}
|
||||
|
||||
for ($pos = 0; $pos < $len; $pos += 2) {
|
||||
$chrnum = $block[$pos] . $block[$pos + 1];
|
||||
if (\preg_match('/(\d{2})/', $chrnum) === 1) {
|
||||
$code_data[] = (int) $chrnum;
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new BarcodeException('Invalid character sequence');
|
||||
}
|
||||
|
||||
$code_data[] = 102;
|
||||
}
|
||||
|
||||
// remove last 102 code
|
||||
\array_pop($code_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finalize code data
|
||||
*
|
||||
* @param array<int, int> $code_data Array of codepoints to alter
|
||||
* @param int $startid Start ID code
|
||||
*
|
||||
* @return array<int, int> Array of codepoints
|
||||
*/
|
||||
protected function finalizeCodeData(array $code_data, int $startid): array
|
||||
{
|
||||
// calculate check character
|
||||
$sum = $startid;
|
||||
foreach ($code_data as $key => $val) {
|
||||
$sum += $val * ($key + 1);
|
||||
}
|
||||
|
||||
// add check character
|
||||
$code_data[] = $sum % 103;
|
||||
|
||||
// add stop sequence
|
||||
$code_data[] = 106;
|
||||
$code_data[] = 107;
|
||||
// add start code at the beginning
|
||||
\array_unshift($code_data, $startid);
|
||||
|
||||
return $code_data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CodeThreeNine.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeThreeNine
|
||||
*
|
||||
* CodeThreeNine Barcode type class
|
||||
* CODE 39
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class CodeThreeNine extends \Com\Tecnick\Barcode\Type\Linear\CodeThreeNineExtCheck
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'C39';
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = '*' . \strtoupper($this->code) . '*';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CodeThreeNineCheck.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeThreeNineCheck
|
||||
*
|
||||
* CodeThreeNineCheck Barcode type class
|
||||
* CODE 39 + CHECKSUM
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class CodeThreeNineCheck extends \Com\Tecnick\Barcode\Type\Linear\CodeThreeNineExtCheck
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'C39+';
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$code = \strtoupper($this->code);
|
||||
$this->extcode = '*' . $code . $this->getChecksum($code) . '*';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CodeThreeNineExt.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeThreeNineExt
|
||||
*
|
||||
* CodeThreeNineExt Barcode type class
|
||||
* CODE 39 EXTENDED
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class CodeThreeNineExt extends \Com\Tecnick\Barcode\Type\Linear\CodeThreeNineExtCheck
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'C39E';
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = '*' . $this->getExtendCode(\strtoupper($this->code)) . '*';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,392 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CodeThreeNineExtCheck.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\CodeThreeNineExtCheck
|
||||
*
|
||||
* CodeThreeNineExtCheck Barcode type class
|
||||
* CODE 39 EXTENDED + CHECKSUM
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class CodeThreeNineExtCheck extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'C39E+';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'0' => '111331311',
|
||||
'1' => '311311113',
|
||||
'2' => '113311113',
|
||||
'3' => '313311111',
|
||||
'4' => '111331113',
|
||||
'5' => '311331111',
|
||||
'6' => '113331111',
|
||||
'7' => '111311313',
|
||||
'8' => '311311311',
|
||||
'9' => '113311311',
|
||||
'A' => '311113113',
|
||||
'B' => '113113113',
|
||||
'C' => '313113111',
|
||||
'D' => '111133113',
|
||||
'E' => '311133111',
|
||||
'F' => '113133111',
|
||||
'G' => '111113313',
|
||||
'H' => '311113311',
|
||||
'I' => '113113311',
|
||||
'J' => '111133311',
|
||||
'K' => '311111133',
|
||||
'L' => '113111133',
|
||||
'M' => '313111131',
|
||||
'N' => '111131133',
|
||||
'O' => '311131131',
|
||||
'P' => '113131131',
|
||||
'Q' => '111111333',
|
||||
'R' => '311111331',
|
||||
'S' => '113111331',
|
||||
'T' => '111131331',
|
||||
'U' => '331111113',
|
||||
'V' => '133111113',
|
||||
'W' => '333111111',
|
||||
'X' => '131131113',
|
||||
'Y' => '331131111',
|
||||
'Z' => '133131111',
|
||||
'-' => '131111313',
|
||||
'.' => '331111311',
|
||||
' ' => '133111311',
|
||||
'$' => '131313111',
|
||||
'/' => '131311131',
|
||||
'+' => '131113131',
|
||||
'%' => '111313131',
|
||||
'*' => '131131311',
|
||||
];
|
||||
|
||||
/**
|
||||
* Map for extended characters
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected const EXTCODES = [
|
||||
'%U',
|
||||
'$A',
|
||||
'$B',
|
||||
'$C',
|
||||
'$D',
|
||||
'$E',
|
||||
'$F',
|
||||
'$G',
|
||||
'$H',
|
||||
'$I',
|
||||
'$J',
|
||||
'$K',
|
||||
'$L',
|
||||
'$M',
|
||||
'$N',
|
||||
'$O',
|
||||
'$P',
|
||||
'$Q',
|
||||
'$R',
|
||||
'$S',
|
||||
'$T',
|
||||
'$U',
|
||||
'$V',
|
||||
'$W',
|
||||
'$X',
|
||||
'$Y',
|
||||
'$Z',
|
||||
'%A',
|
||||
'%B',
|
||||
'%C',
|
||||
'%D',
|
||||
'%E',
|
||||
' ',
|
||||
'/A',
|
||||
'/B',
|
||||
'/C',
|
||||
'/D',
|
||||
'/E',
|
||||
'/F',
|
||||
'/G',
|
||||
'/H',
|
||||
'/I',
|
||||
'/J',
|
||||
'/K',
|
||||
'/L',
|
||||
'-',
|
||||
'.',
|
||||
'/O',
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
'/Z',
|
||||
'%F',
|
||||
'%G',
|
||||
'%H',
|
||||
'%I',
|
||||
'%J',
|
||||
'%V',
|
||||
'A',
|
||||
'B',
|
||||
'C',
|
||||
'D',
|
||||
'E',
|
||||
'F',
|
||||
'G',
|
||||
'H',
|
||||
'I',
|
||||
'J',
|
||||
'K',
|
||||
'L',
|
||||
'M',
|
||||
'N',
|
||||
'O',
|
||||
'P',
|
||||
'Q',
|
||||
'R',
|
||||
'S',
|
||||
'T',
|
||||
'U',
|
||||
'V',
|
||||
'W',
|
||||
'X',
|
||||
'Y',
|
||||
'Z',
|
||||
'%K',
|
||||
'%L',
|
||||
'%M',
|
||||
'%N',
|
||||
'%O',
|
||||
'%W',
|
||||
'+A',
|
||||
'+B',
|
||||
'+C',
|
||||
'+D',
|
||||
'+E',
|
||||
'+F',
|
||||
'+G',
|
||||
'+H',
|
||||
'+I',
|
||||
'+J',
|
||||
'+K',
|
||||
'+L',
|
||||
'+M',
|
||||
'+N',
|
||||
'+O',
|
||||
'+P',
|
||||
'+Q',
|
||||
'+R',
|
||||
'+S',
|
||||
'+T',
|
||||
'+U',
|
||||
'+V',
|
||||
'+W',
|
||||
'+X',
|
||||
'+Y',
|
||||
'+Z',
|
||||
'%P',
|
||||
'%Q',
|
||||
'%R',
|
||||
'%S',
|
||||
'%T',
|
||||
];
|
||||
|
||||
/**
|
||||
* Characters used for checksum
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected const CHKSUM = [
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
'A',
|
||||
'B',
|
||||
'C',
|
||||
'D',
|
||||
'E',
|
||||
'F',
|
||||
'G',
|
||||
'H',
|
||||
'I',
|
||||
'J',
|
||||
'K',
|
||||
'L',
|
||||
'M',
|
||||
'N',
|
||||
'O',
|
||||
'P',
|
||||
'Q',
|
||||
'R',
|
||||
'S',
|
||||
'T',
|
||||
'U',
|
||||
'V',
|
||||
'W',
|
||||
'X',
|
||||
'Y',
|
||||
'Z',
|
||||
'-',
|
||||
'.',
|
||||
' ',
|
||||
'$',
|
||||
'/',
|
||||
'+',
|
||||
'%',
|
||||
];
|
||||
|
||||
protected function getExtendedCodeValue(int $item): string
|
||||
{
|
||||
return $this::EXTCODES[$item] ?? '';
|
||||
}
|
||||
|
||||
protected function getChecksumIndex(string $char): int
|
||||
{
|
||||
$index = \array_search($char, $this::CHKSUM, true);
|
||||
if (\is_int($index)) {
|
||||
return $index;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function getChecksumChar(int $index): string
|
||||
{
|
||||
return $this::CHKSUM[$index] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a string to be used for CODE 39 Extended mode.
|
||||
*
|
||||
* @param string $code Code to extend
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getExtendCode(string $code): string
|
||||
{
|
||||
$ext = '';
|
||||
$clen = \strlen($code);
|
||||
for ($chr = 0; $chr < $clen; ++$chr) {
|
||||
$item = \ord($code[$chr]);
|
||||
if ($item > 127) {
|
||||
throw new BarcodeException('Invalid character: ' . ($item & 0xFF));
|
||||
}
|
||||
|
||||
$ext .= $this->getExtendedCodeValue($item);
|
||||
}
|
||||
|
||||
return $ext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate CODE 39 checksum (modulo 43).
|
||||
*
|
||||
* @param string $code Code to represent.
|
||||
*
|
||||
* @return string char checksum.
|
||||
*/
|
||||
protected function getChecksum(string $code): string
|
||||
{
|
||||
$sum = 0;
|
||||
$clen = \strlen($code);
|
||||
for ($chr = 0; $chr < $clen; ++$chr) {
|
||||
$sum += $this->getChecksumIndex($code[$chr]);
|
||||
}
|
||||
|
||||
$idx = $sum % 43;
|
||||
return $this->getChecksumChar($idx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$code = $this->getExtendCode(\strtoupper($this->code));
|
||||
$this->extcode = '*' . $code . $this->getChecksum($code) . '*';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 1;
|
||||
$this->bars = [];
|
||||
$this->formatCode();
|
||||
$clen = \strlen($this->extcode);
|
||||
for ($chr = 0; $chr < $clen; ++$chr) {
|
||||
$char = $this->extcode[$chr];
|
||||
$pattern = $this::CHBAR[$char] ?? null;
|
||||
if ($pattern === null) {
|
||||
throw new BarcodeException('Invalid character: ' . (\ord($char) & 0xFF));
|
||||
}
|
||||
|
||||
for ($pos = 0; $pos < 9; ++$pos) {
|
||||
$bar_width = (int) ($pattern[$pos] ?? '0');
|
||||
if (($pos % 2) === 0 && $bar_width > 0) {
|
||||
$this->bars[] = [$this->ncols, 0, $bar_width, 1];
|
||||
}
|
||||
|
||||
$this->ncols += $bar_width;
|
||||
}
|
||||
|
||||
// intercharacter gap
|
||||
++$this->ncols;
|
||||
}
|
||||
|
||||
--$this->ncols;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* EanEight.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\EanEight;
|
||||
*
|
||||
* EanEight Barcode type class
|
||||
* EAN 8
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class EanEight extends \Com\Tecnick\Barcode\Type\Linear\EanOneThree
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'EAN8';
|
||||
|
||||
/**
|
||||
* Fixed code length
|
||||
*/
|
||||
protected int $code_length = 8;
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
if (!\is_numeric($this->code)) {
|
||||
throw new BarcodeException('Input code must be a number');
|
||||
}
|
||||
|
||||
$this->formatCode();
|
||||
$seq = '101'; // left guard bar
|
||||
$half_len = (int) \ceil($this->code_length / 2);
|
||||
for ($pos = 0; $pos < $half_len; ++$pos) {
|
||||
$seq .= $this->getBarPattern('A', $this->getCharAt($this->extcode, $pos));
|
||||
}
|
||||
|
||||
$seq .= '01010'; // center guard bar
|
||||
for ($pos = $half_len; $pos < $this->code_length; ++$pos) {
|
||||
$seq .= $this->getBarPattern('C', $this->getCharAt($this->extcode, $pos));
|
||||
}
|
||||
|
||||
$seq .= '101'; // right guard bar
|
||||
$this->processBinarySequence($this->getRawCodeRows($seq));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* EanFive.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\EanFive;
|
||||
*
|
||||
* EanFive Barcode type class
|
||||
* EAN 5-Digits UPC-Based Extension
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class EanFive extends \Com\Tecnick\Barcode\Type\Linear\EanTwo
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'EAN5';
|
||||
|
||||
/**
|
||||
* Fixed code length
|
||||
*/
|
||||
protected int $code_length = 5;
|
||||
|
||||
/**
|
||||
* Map parities
|
||||
*
|
||||
* @var array<int|string, array<string>>
|
||||
*/
|
||||
protected const PARITIES = [
|
||||
'0' => ['B', 'B', 'A', 'A', 'A'],
|
||||
'1' => ['B', 'A', 'B', 'A', 'A'],
|
||||
'2' => ['B', 'A', 'A', 'B', 'A'],
|
||||
'3' => ['B', 'A', 'A', 'A', 'B'],
|
||||
'4' => ['A', 'B', 'B', 'A', 'A'],
|
||||
'5' => ['A', 'A', 'B', 'B', 'A'],
|
||||
'6' => ['A', 'A', 'A', 'B', 'B'],
|
||||
'7' => ['A', 'B', 'A', 'B', 'A'],
|
||||
'8' => ['A', 'B', 'A', 'A', 'B'],
|
||||
'9' => ['A', 'A', 'B', 'A', 'B'],
|
||||
];
|
||||
|
||||
/**
|
||||
* Calculate checksum
|
||||
*
|
||||
* @param string $code Code to represent.
|
||||
*
|
||||
* @return int char checksum.
|
||||
*/
|
||||
protected function getChecksum(string $code): int
|
||||
{
|
||||
return (
|
||||
((3 * ((int) $code[0] + (int) $code[2] + (int) $code[4])) + (9 * ((int) $code[1] + (int) $code[3]))) % 10
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* EanOneThree.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\EanOneThree;
|
||||
*
|
||||
* EanOneThree Barcode type class
|
||||
* EAN 13
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class EanOneThree extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
protected function getCharAt(string $value, int $index): string
|
||||
{
|
||||
return $value[$index] ?? '0';
|
||||
}
|
||||
|
||||
protected function getParityPattern(string $digit): string
|
||||
{
|
||||
return $this::PARITIES[$digit] ?? 'AAAAAA';
|
||||
}
|
||||
|
||||
protected function getBarPattern(string $parity, string $digit): string
|
||||
{
|
||||
return $this::CHBAR[$parity][$digit] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'EAN13';
|
||||
|
||||
/**
|
||||
* Fixed code length
|
||||
*/
|
||||
protected int $code_length = 13;
|
||||
|
||||
/**
|
||||
* Check digit
|
||||
*/
|
||||
protected int $check = 0;
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, array<int|string, string>>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'A' => [
|
||||
// left odd parity
|
||||
'0' => '0001101',
|
||||
'1' => '0011001',
|
||||
'2' => '0010011',
|
||||
'3' => '0111101',
|
||||
'4' => '0100011',
|
||||
'5' => '0110001',
|
||||
'6' => '0101111',
|
||||
'7' => '0111011',
|
||||
'8' => '0110111',
|
||||
'9' => '0001011',
|
||||
],
|
||||
'B' => [
|
||||
// left even parity
|
||||
'0' => '0100111',
|
||||
'1' => '0110011',
|
||||
'2' => '0011011',
|
||||
'3' => '0100001',
|
||||
'4' => '0011101',
|
||||
'5' => '0111001',
|
||||
'6' => '0000101',
|
||||
'7' => '0010001',
|
||||
'8' => '0001001',
|
||||
'9' => '0010111',
|
||||
],
|
||||
'C' => [
|
||||
// right
|
||||
'0' => '1110010',
|
||||
'1' => '1100110',
|
||||
'2' => '1101100',
|
||||
'3' => '1000010',
|
||||
'4' => '1011100',
|
||||
'5' => '1001110',
|
||||
'6' => '1010000',
|
||||
'7' => '1000100',
|
||||
'8' => '1001000',
|
||||
'9' => '1110100',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Map parities
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const PARITIES = [
|
||||
'0' => 'AAAAAA',
|
||||
'1' => 'AABABB',
|
||||
'2' => 'AABBAB',
|
||||
'3' => 'AABBBA',
|
||||
'4' => 'ABAABB',
|
||||
'5' => 'ABBAAB',
|
||||
'6' => 'ABBBAA',
|
||||
'7' => 'ABABAB',
|
||||
'8' => 'ABABBA',
|
||||
'9' => 'ABBABA',
|
||||
];
|
||||
|
||||
/**
|
||||
* Calculate checksum
|
||||
*
|
||||
* @param string $code Code to represent.
|
||||
*
|
||||
* @return int char checksum.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getChecksum(string $code): int
|
||||
{
|
||||
$data_len = $this->code_length - 1;
|
||||
$code_len = \strlen($code);
|
||||
$sum_a = 0;
|
||||
for ($pos = 1; $pos < $data_len; $pos += 2) {
|
||||
$sum_a += (int) $code[$pos];
|
||||
}
|
||||
|
||||
if ($this->code_length > 12) {
|
||||
$sum_a *= 3;
|
||||
}
|
||||
|
||||
$sum_b = 0;
|
||||
for ($pos = 0; $pos < $data_len; $pos += 2) {
|
||||
$sum_b += (int) $code[$pos];
|
||||
}
|
||||
|
||||
if ($this->code_length < 13) {
|
||||
$sum_b *= 3;
|
||||
}
|
||||
|
||||
$this->check = ($sum_a + $sum_b) % 10;
|
||||
if ($this->check > 0) {
|
||||
$this->check = 10 - $this->check;
|
||||
}
|
||||
|
||||
if ($code_len === $data_len) {
|
||||
// add check digit
|
||||
return $this->check;
|
||||
}
|
||||
|
||||
if ($this->check !== (int) $code[$data_len]) {
|
||||
// wrong check digit
|
||||
throw new BarcodeException('Invalid check digit: ' . $this->check);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$code = \str_pad($this->code, $this->code_length - 1, '0', STR_PAD_LEFT);
|
||||
// getChecksum() returns the missing check digit, or validates (and returns 0)
|
||||
// when the input already carries it; in the latter case keep the code unchanged
|
||||
// to avoid appending a spurious extra digit to the extended code.
|
||||
$check = $this->getChecksum($code);
|
||||
$this->extcode = \strlen($code) >= $this->code_length ? $code : $code . $check;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
if (!\is_numeric($this->code)) {
|
||||
throw new BarcodeException('Input code must be a number');
|
||||
}
|
||||
|
||||
$this->formatCode();
|
||||
$seq = '101'; // left guard bar
|
||||
$half_len = (int) \ceil($this->code_length / 2);
|
||||
$parity = $this->getParityPattern($this->getCharAt($this->extcode, 0));
|
||||
for ($pos = 1; $pos < $half_len; ++$pos) {
|
||||
$seq .= $this->getBarPattern($this->getCharAt($parity, $pos - 1), $this->getCharAt($this->extcode, $pos));
|
||||
}
|
||||
|
||||
$seq .= '01010'; // center guard bar
|
||||
for ($pos = $half_len; $pos < $this->code_length; ++$pos) {
|
||||
$seq .= $this->getBarPattern('C', $this->getCharAt($this->extcode, $pos));
|
||||
}
|
||||
|
||||
$seq .= '101'; // right guard bar
|
||||
$this->processBinarySequence($this->getRawCodeRows($seq));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* EanTwo.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\EanTwo;
|
||||
*
|
||||
* EanTwo Barcode type class
|
||||
* EAN 2-Digits UPC-Based Extension
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class EanTwo extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
protected function getCharAt(string $value, int $index): string
|
||||
{
|
||||
return $value[$index] ?? '0';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected function getParityPattern(int $check): array
|
||||
{
|
||||
$pattern = $this::PARITIES[$check] ?? ['A', 'A'];
|
||||
return \array_values($pattern);
|
||||
}
|
||||
|
||||
protected function getBarPattern(string $parity, string $digit): string
|
||||
{
|
||||
return $this::CHBAR[$parity][$digit] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'EAN2';
|
||||
|
||||
/**
|
||||
* Fixed code length
|
||||
*/
|
||||
protected int $code_length = 2;
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<string, array<int|string, string>>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'A' => [
|
||||
// left odd parity
|
||||
'0' => '0001101',
|
||||
'1' => '0011001',
|
||||
'2' => '0010011',
|
||||
'3' => '0111101',
|
||||
'4' => '0100011',
|
||||
'5' => '0110001',
|
||||
'6' => '0101111',
|
||||
'7' => '0111011',
|
||||
'8' => '0110111',
|
||||
'9' => '0001011',
|
||||
],
|
||||
'B' => [
|
||||
// left even parity
|
||||
'0' => '0100111',
|
||||
'1' => '0110011',
|
||||
'2' => '0011011',
|
||||
'3' => '0100001',
|
||||
'4' => '0011101',
|
||||
'5' => '0111001',
|
||||
'6' => '0000101',
|
||||
'7' => '0010001',
|
||||
'8' => '0001001',
|
||||
'9' => '0010111',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Map parities
|
||||
*
|
||||
* @var array<int|string, array<string>>
|
||||
*/
|
||||
protected const PARITIES = [
|
||||
'0' => ['A', 'A'],
|
||||
'1' => ['A', 'B'],
|
||||
'2' => ['B', 'A'],
|
||||
'3' => ['B', 'B'],
|
||||
];
|
||||
|
||||
/**
|
||||
* Calculate checksum
|
||||
*
|
||||
* @param string $code Code to represent.
|
||||
*
|
||||
* @return int char checksum.
|
||||
*/
|
||||
protected function getChecksum(string $code): int
|
||||
{
|
||||
return (int) $code % 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = \str_pad($this->code, $this->code_length, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->formatCode();
|
||||
$chk = $this->getChecksum($this->extcode);
|
||||
$parity = $this->getParityPattern($chk);
|
||||
$seq = '1011'; // left guard bar
|
||||
$seq .= $this->getBarPattern($this->getCharAt($parity[0] ?? 'A', 0), $this->getCharAt($this->extcode, 0));
|
||||
$len = \strlen($this->extcode);
|
||||
for ($pos = 1; $pos < $len; ++$pos) {
|
||||
$seq .= '01'; // separator
|
||||
$seq .= $this->getBarPattern(
|
||||
$this->getCharAt($parity[$pos] ?? 'A', 0),
|
||||
$this->getCharAt($this->extcode, $pos),
|
||||
);
|
||||
}
|
||||
|
||||
$this->processBinarySequence($this->getRawCodeRows($seq));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,661 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Imb.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\Imb;
|
||||
*
|
||||
* Imb Barcode type class
|
||||
* IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
|
||||
*
|
||||
* Intelligent Mail barcode is a 65-bar code for use on mail in the United States.
|
||||
* The fields are described as follows:
|
||||
* * The Barcode Identifier shall be assigned by USPS to encode the presort identification that is currently
|
||||
* printed in human readable form on the optional endorsement line (OEL) as well as for future USPS use.
|
||||
* This shall be two digits, with the second digit in the range of 0–4. The allowable encoding ranges shall be
|
||||
* 00–04, 10–14, 20–24, 30–34, 40–44, 50–54, 60–64, 70–74, 80–84, and 90–94.
|
||||
* * The Service Type Identifier shall be assigned by USPS for any combination of services requested on the mailpiece.
|
||||
* The allowable encoding range shall be 000–999.
|
||||
* Each 3-digit value shall correspond to a particular mail class with a particular combination of service(s).
|
||||
* Each service program, such as OneCode Confirm and OneCode ACS, shall provide the list of Service Type Identifier
|
||||
* values.
|
||||
* * The Mailer or Customer Identifier shall be assigned by USPS as a unique, 6 or 9 digit number that identifies
|
||||
* a business entity. The allowable encoding range for the 6 digit Mailer ID shall be 000000- 899999, while the
|
||||
* allowable encoding range for the 9 digit Mailer ID shall be 900000000-999999999. The Serial or
|
||||
* Sequence Number shall be assigned by the mailer for uniquely identifying and tracking mailpieces.
|
||||
* The allowable encoding range shall be 000000000–999999999 when used with a 6 digit Mailer ID and 000000-999999
|
||||
* when used with a 9 digit Mailer ID. e. The Delivery Point ZIP Code shall be assigned by the mailer for routing
|
||||
* the mailpiece. This shall replace POSTNET for routing the mailpiece to its final delivery point.
|
||||
* The length may be 0, 5, 9, or 11 digits. The allowable encoding ranges shall be no ZIP Code, 00000–99999,
|
||||
* 000000000–999999999, and 00000000000–99999999999. A hyphen '-' is required before the zip/delivery point.
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Imb extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'IMB';
|
||||
|
||||
/**
|
||||
* ASC characters
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
protected const ASC_CHR = [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
6,
|
||||
3,
|
||||
5,
|
||||
1,
|
||||
9,
|
||||
8,
|
||||
7,
|
||||
1,
|
||||
2,
|
||||
0,
|
||||
6,
|
||||
4,
|
||||
8,
|
||||
2,
|
||||
9,
|
||||
5,
|
||||
3,
|
||||
0,
|
||||
1,
|
||||
3,
|
||||
7,
|
||||
4,
|
||||
6,
|
||||
8,
|
||||
9,
|
||||
2,
|
||||
0,
|
||||
5,
|
||||
1,
|
||||
9,
|
||||
4,
|
||||
3,
|
||||
8,
|
||||
6,
|
||||
7,
|
||||
1,
|
||||
2,
|
||||
4,
|
||||
3,
|
||||
9,
|
||||
5,
|
||||
7,
|
||||
8,
|
||||
3,
|
||||
0,
|
||||
2,
|
||||
1,
|
||||
4,
|
||||
0,
|
||||
9,
|
||||
1,
|
||||
7,
|
||||
0,
|
||||
2,
|
||||
4,
|
||||
6,
|
||||
3,
|
||||
7,
|
||||
1,
|
||||
9,
|
||||
5,
|
||||
8,
|
||||
];
|
||||
|
||||
/**
|
||||
* DSC characters
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
protected const DSC_CHR = [
|
||||
7,
|
||||
1,
|
||||
9,
|
||||
5,
|
||||
8,
|
||||
0,
|
||||
2,
|
||||
4,
|
||||
6,
|
||||
3,
|
||||
5,
|
||||
8,
|
||||
9,
|
||||
7,
|
||||
3,
|
||||
0,
|
||||
6,
|
||||
1,
|
||||
7,
|
||||
4,
|
||||
6,
|
||||
8,
|
||||
9,
|
||||
2,
|
||||
5,
|
||||
1,
|
||||
7,
|
||||
5,
|
||||
4,
|
||||
3,
|
||||
8,
|
||||
7,
|
||||
6,
|
||||
0,
|
||||
2,
|
||||
5,
|
||||
4,
|
||||
9,
|
||||
3,
|
||||
0,
|
||||
1,
|
||||
6,
|
||||
8,
|
||||
2,
|
||||
0,
|
||||
4,
|
||||
5,
|
||||
9,
|
||||
6,
|
||||
7,
|
||||
5,
|
||||
2,
|
||||
6,
|
||||
3,
|
||||
8,
|
||||
5,
|
||||
1,
|
||||
9,
|
||||
8,
|
||||
7,
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
6,
|
||||
3,
|
||||
];
|
||||
|
||||
/**
|
||||
* ASC positions
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
protected const ASC_POS = [
|
||||
3,
|
||||
0,
|
||||
8,
|
||||
11,
|
||||
1,
|
||||
12,
|
||||
8,
|
||||
11,
|
||||
10,
|
||||
6,
|
||||
4,
|
||||
12,
|
||||
2,
|
||||
7,
|
||||
9,
|
||||
6,
|
||||
7,
|
||||
9,
|
||||
2,
|
||||
8,
|
||||
4,
|
||||
0,
|
||||
12,
|
||||
7,
|
||||
10,
|
||||
9,
|
||||
0,
|
||||
7,
|
||||
10,
|
||||
5,
|
||||
7,
|
||||
9,
|
||||
6,
|
||||
8,
|
||||
2,
|
||||
12,
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
0,
|
||||
1,
|
||||
5,
|
||||
4,
|
||||
6,
|
||||
12,
|
||||
1,
|
||||
0,
|
||||
9,
|
||||
4,
|
||||
7,
|
||||
5,
|
||||
10,
|
||||
2,
|
||||
6,
|
||||
9,
|
||||
11,
|
||||
2,
|
||||
12,
|
||||
6,
|
||||
7,
|
||||
5,
|
||||
11,
|
||||
0,
|
||||
3,
|
||||
2,
|
||||
];
|
||||
|
||||
/**
|
||||
* DSC positions
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
protected const DSC_POS = [
|
||||
2,
|
||||
10,
|
||||
12,
|
||||
5,
|
||||
9,
|
||||
1,
|
||||
5,
|
||||
4,
|
||||
3,
|
||||
9,
|
||||
11,
|
||||
5,
|
||||
10,
|
||||
1,
|
||||
6,
|
||||
3,
|
||||
4,
|
||||
1,
|
||||
10,
|
||||
0,
|
||||
2,
|
||||
11,
|
||||
8,
|
||||
6,
|
||||
1,
|
||||
12,
|
||||
3,
|
||||
8,
|
||||
6,
|
||||
4,
|
||||
4,
|
||||
11,
|
||||
0,
|
||||
6,
|
||||
1,
|
||||
9,
|
||||
11,
|
||||
5,
|
||||
3,
|
||||
7,
|
||||
3,
|
||||
10,
|
||||
7,
|
||||
11,
|
||||
8,
|
||||
2,
|
||||
10,
|
||||
3,
|
||||
5,
|
||||
8,
|
||||
0,
|
||||
3,
|
||||
12,
|
||||
11,
|
||||
8,
|
||||
4,
|
||||
5,
|
||||
1,
|
||||
3,
|
||||
0,
|
||||
7,
|
||||
12,
|
||||
9,
|
||||
8,
|
||||
10,
|
||||
];
|
||||
|
||||
/**
|
||||
* Reverse unsigned short value
|
||||
*
|
||||
* @param int $num Value to reverse
|
||||
*
|
||||
* @return int reversed value
|
||||
*/
|
||||
protected function getReversedUnsignedShort(int $num): int
|
||||
{
|
||||
$rev = 0;
|
||||
for ($pos = 0; $pos < 16; ++$pos) {
|
||||
$rev <<= 1;
|
||||
$rev |= $num & 1;
|
||||
$num >>= 1;
|
||||
}
|
||||
|
||||
return $rev;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $code_arr
|
||||
*/
|
||||
protected function getCodeByte(array $code_arr, int $index): string
|
||||
{
|
||||
return $code_arr[$index] ?? '00';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return numeric-string
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function toNumericString(string $value): string
|
||||
{
|
||||
if ($value === '' || !\ctype_digit($value)) {
|
||||
throw new BarcodeException('Invalid numeric string');
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return numeric-string
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getTrackingDigit(string $tracking_number, int $index): string
|
||||
{
|
||||
$digit = $tracking_number[$index] ?? '';
|
||||
if (!\ctype_digit($digit)) {
|
||||
throw new BarcodeException('Invalid tracking number');
|
||||
}
|
||||
|
||||
return $digit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $table
|
||||
*/
|
||||
protected function getTableCode(array $table, int $index): int
|
||||
{
|
||||
return $table[$index] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $chars
|
||||
*/
|
||||
protected function getCharValue(array $chars, int $index): int
|
||||
{
|
||||
return $chars[$index] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Frame Check Sequence
|
||||
*
|
||||
* @param array<int, string> $code_arr Array of hexadecimal values (13 bytes holding 102 bits right justified).
|
||||
*
|
||||
* @return int 11 bit Frame Check Sequence as integer (decimal base)
|
||||
*/
|
||||
protected function getFrameCheckSequence(array $code_arr): int
|
||||
{
|
||||
$genpoly = 0x0F35; // generator polynomial
|
||||
$fcs = 0x07FF; // Frame Check Sequence
|
||||
// do most significant byte skipping the 2 most significant bits
|
||||
$data = \hexdec($this->getCodeByte($code_arr, 0)) << 5;
|
||||
for ($bit = 2; $bit < 8; ++$bit) {
|
||||
$fcs = (($fcs ^ $data) & 0x400) !== 0 ? ($fcs << 1) ^ $genpoly : $fcs << 1;
|
||||
|
||||
$fcs &= 0x7FF;
|
||||
$data <<= 1;
|
||||
}
|
||||
|
||||
// do rest of bytes
|
||||
for ($byte = 1; $byte < 13; ++$byte) {
|
||||
$data = \hexdec($this->getCodeByte($code_arr, $byte)) << 3;
|
||||
for ($bit = 0; $bit < 8; ++$bit) {
|
||||
$fcs = (($fcs ^ $data) & 0x400) !== 0 ? ($fcs << 1) ^ $genpoly : $fcs << 1;
|
||||
|
||||
$fcs &= 0x7FF;
|
||||
$data <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $fcs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Nof13 tables
|
||||
*
|
||||
* @param int $type Table type: 2 for 2of13 table, 5 for 5of13table
|
||||
* @param int $size Table size (78 for n=2 and 1287 for n=5)
|
||||
*
|
||||
* @return array<int, int> requested table
|
||||
*/
|
||||
protected function getTables(int $type, int $size): array
|
||||
{
|
||||
$table = [];
|
||||
$lli = 0; // LUT lower index
|
||||
$lui = $size - 1; // LUT upper index
|
||||
for ($count = 0; $count < 8192; ++$count) {
|
||||
$bit_count = 0;
|
||||
for ($bit_index = 0; $bit_index < 13; ++$bit_index) {
|
||||
$bit_count += (int) (($count & (1 << $bit_index)) !== 0);
|
||||
}
|
||||
|
||||
// if we don't have the right number of bits on, go on to the next value
|
||||
if ($bit_count === $type) {
|
||||
$reverse = $this->getReversedUnsignedShort($count) >> 3;
|
||||
// if the reverse is less than count, we have already visited this pair before
|
||||
if ($reverse >= $count) {
|
||||
// If count is symmetric, place it at the first free slot from the end of the list.
|
||||
// Otherwise, place it at the first free slot from the beginning of the list AND place
|
||||
// $reverse ath the next free slot from the beginning of the list
|
||||
if ($reverse === $count) {
|
||||
$table[$lui] = $count;
|
||||
--$lui;
|
||||
continue;
|
||||
}
|
||||
|
||||
$table[$lli] = $count;
|
||||
++$lli;
|
||||
$table[$lli] = $reverse;
|
||||
++$lli;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the routing code binary block
|
||||
*
|
||||
* @param string $routing_code the routing code
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getRoutingCode(string $routing_code): string
|
||||
{
|
||||
if ($routing_code !== '' && !\ctype_digit($routing_code)) {
|
||||
throw new BarcodeException('Invalid routing code');
|
||||
}
|
||||
|
||||
if ($routing_code === '') {
|
||||
return '0';
|
||||
}
|
||||
|
||||
$routing_code = $this->toNumericString($routing_code);
|
||||
|
||||
// Conversion of Routing Code
|
||||
return match (\strlen($routing_code)) {
|
||||
5 => $this->addNumeric($routing_code, '1'),
|
||||
9 => $this->addNumeric($routing_code, '100001'),
|
||||
11 => $this->addNumeric($routing_code, '1000100001'),
|
||||
default => throw new BarcodeException('Invalid routing code'),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the processed array of characters
|
||||
*
|
||||
* @return array<int, int>
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getCharsArray(): array
|
||||
{
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 3;
|
||||
$this->bars = [];
|
||||
$code_arr = \explode('-', $this->code);
|
||||
$tracking_number = $code_arr[0];
|
||||
if (!\preg_match('/^\d{2,20}$/', $tracking_number)) {
|
||||
throw new BarcodeException('Invalid tracking number');
|
||||
}
|
||||
|
||||
$binary_code = '0';
|
||||
if (($code_arr[1] ?? null) !== null) {
|
||||
$binary_code = $this->getRoutingCode($code_arr[1]);
|
||||
}
|
||||
|
||||
$binary_code = $this->toNumericString($binary_code);
|
||||
|
||||
$binary_code = $this->mulNumeric($binary_code, '10');
|
||||
$binary_code = $this->addNumeric($binary_code, $this->getTrackingDigit($tracking_number, 0));
|
||||
$binary_code = $this->mulNumeric($binary_code, '5');
|
||||
$binary_code = $this->addNumeric($binary_code, $this->getTrackingDigit($tracking_number, 1));
|
||||
$binary_code = $this->toNumericString($binary_code . \substr($tracking_number, 2, 18));
|
||||
// convert to hexadecimal
|
||||
$binary_code = $this->convertDecToHex($binary_code);
|
||||
// pad to get 13 bytes
|
||||
$binary_code = \str_pad($binary_code, 26, '0', STR_PAD_LEFT);
|
||||
// convert string to array of bytes
|
||||
$binary_code_arr = \chunk_split($binary_code, 2, "\r");
|
||||
$binary_code_arr = \substr($binary_code_arr, 0, -1);
|
||||
$binary_code_arr = \explode("\r", $binary_code_arr);
|
||||
// calculate frame check sequence
|
||||
$fcs = $this->getFrameCheckSequence($binary_code_arr);
|
||||
// exclude first 2 bits from first byte
|
||||
$first_byte = \sprintf('%2s', \dechex((int) (\hexdec($binary_code_arr[0]) << 2) >> 2));
|
||||
$binary_code_102bit = $first_byte . \substr($binary_code, 2);
|
||||
// convert binary data to codewords
|
||||
$codewords = [];
|
||||
$data = $this->toNumericString($this->convertHexToDec($binary_code_102bit));
|
||||
$codewords[0] = $this->modNumeric($data, '636') * 2;
|
||||
$data = $this->divNumeric($data, '636');
|
||||
for ($pos = 1; $pos < 9; ++$pos) {
|
||||
$codewords[$pos] = $this->modNumeric($data, '1365');
|
||||
$data = $this->divNumeric($data, '1365');
|
||||
}
|
||||
|
||||
$codewords[9] = (int) $data;
|
||||
if (($fcs >> 10) === 1) {
|
||||
$codewords[9] += 659;
|
||||
}
|
||||
|
||||
// generate lookup tables
|
||||
$table2of13 = $this->getTables(2, 78);
|
||||
$table5of13 = $this->getTables(5, 1287);
|
||||
// convert codewords to characters
|
||||
$characters = [];
|
||||
$bitmask = 512;
|
||||
foreach ($codewords as $codeword) {
|
||||
$chrcode = $codeword <= 1286
|
||||
? $this->getTableCode($table5of13, $codeword)
|
||||
: $this->getTableCode($table2of13, $codeword - 1287);
|
||||
|
||||
if (($fcs & $bitmask) > 0) {
|
||||
// bitwise invert
|
||||
$chrcode = ~(int) $chrcode & 8191;
|
||||
}
|
||||
|
||||
$characters[] = $chrcode;
|
||||
$bitmask /= 2;
|
||||
}
|
||||
|
||||
return \array_reverse($characters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$chars = $this->getCharsArray();
|
||||
for ($pos = 0; $pos < 65; ++$pos) {
|
||||
$asc_chr = self::ASC_CHR[$pos] ?? 0;
|
||||
$asc_pos = self::ASC_POS[$pos] ?? 0;
|
||||
$dsc_chr = self::DSC_CHR[$pos] ?? 0;
|
||||
$dsc_pos = self::DSC_POS[$pos] ?? 0;
|
||||
$asc = ($this->getCharValue($chars, $asc_chr) & (2 ** $asc_pos)) > 0;
|
||||
$dsc = ($this->getCharValue($chars, $dsc_chr) & (2 ** $dsc_pos)) > 0;
|
||||
if ($asc && $dsc) {
|
||||
// full bar (F)
|
||||
$this->bars[] = [$this->ncols, 0, 1, 3];
|
||||
$this->ncols += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($asc) {
|
||||
// ascender (A)
|
||||
$this->bars[] = [$this->ncols, 0, 1, 2];
|
||||
$this->ncols += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($dsc) {
|
||||
// descender (D)
|
||||
$this->bars[] = [$this->ncols, 1, 1, 2];
|
||||
$this->ncols += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
// tracker (T)
|
||||
$this->bars[] = [$this->ncols, 1, 1, 1];
|
||||
$this->ncols += 2;
|
||||
}
|
||||
|
||||
--$this->ncols;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* ImbPre.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\ImbPre;
|
||||
*
|
||||
* ImbPre Barcode type class
|
||||
* IMB - Intelligent Mail Barcode pre-processed (USPS-B-3200)
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class ImbPre extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'IMBPRE';
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$code = \strtolower($this->code);
|
||||
if (\preg_match('/^[fadt]{65}$/', $code) !== 1) {
|
||||
throw new BarcodeException('Invalid character sequence');
|
||||
}
|
||||
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 3;
|
||||
$this->bars = [];
|
||||
for ($pos = 0; $pos < 65; ++$pos) {
|
||||
switch ($code[$pos]) {
|
||||
case 'f':
|
||||
// full bar
|
||||
$this->bars[] = [$this->ncols, 0, 1, 3];
|
||||
break;
|
||||
case 'a':
|
||||
// ascender
|
||||
$this->bars[] = [$this->ncols, 0, 1, 2];
|
||||
break;
|
||||
case 'd':
|
||||
// descender
|
||||
$this->bars[] = [$this->ncols, 1, 1, 2];
|
||||
break;
|
||||
case 't':
|
||||
// tracker (short)
|
||||
$this->bars[] = [$this->ncols, 1, 1, 1];
|
||||
break;
|
||||
}
|
||||
|
||||
$this->ncols += 2;
|
||||
}
|
||||
|
||||
--$this->ncols;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* InterleavedTwoOfFive.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\InterleavedTwoOfFive;
|
||||
*
|
||||
* InterleavedTwoOfFive Barcode type class
|
||||
* Interleaved 2 of 5
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class InterleavedTwoOfFive extends \Com\Tecnick\Barcode\Type\Linear\InterleavedTwoOfFiveCheck
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'I25';
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = $this->code;
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* InterleavedTwoOfFiveCheck.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\InterleavedTwoOfFiveCheck;
|
||||
*
|
||||
* InterleavedTwoOfFiveCheck Barcode type class
|
||||
* Interleaved 2 of 5 + CHECKSUM
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class InterleavedTwoOfFiveCheck extends \Com\Tecnick\Barcode\Type\Linear\StandardTwoOfFiveCheck
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'I25+';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'0' => '11221',
|
||||
'1' => '21112',
|
||||
'2' => '12112',
|
||||
'3' => '22111',
|
||||
'4' => '11212',
|
||||
'5' => '21211',
|
||||
'6' => '12211',
|
||||
'7' => '11122',
|
||||
'8' => '21121',
|
||||
'9' => '12121',
|
||||
'A' => '11',
|
||||
'Z' => '21',
|
||||
];
|
||||
|
||||
protected function getPattern(string $digit): string
|
||||
{
|
||||
return $this::CHBAR[$digit] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = $this->code . $this->getChecksum($this->code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->formatCode();
|
||||
if ((\strlen($this->extcode) % 2) !== 0) {
|
||||
// add leading zero if code-length is odd
|
||||
$this->extcode = '0' . $this->extcode;
|
||||
}
|
||||
|
||||
// add start and stop codes
|
||||
$this->extcode = 'AA' . \strtolower($this->extcode) . 'ZA';
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 1;
|
||||
$this->bars = [];
|
||||
$clen = \strlen($this->extcode);
|
||||
for ($idx = 0; $idx < $clen; $idx += 2) {
|
||||
$char_bar = $this->extcode[$idx];
|
||||
$char_space = $this->extcode[$idx + 1];
|
||||
if (!\array_key_exists($char_bar, $this::CHBAR) || !\array_key_exists($char_space, $this::CHBAR)) {
|
||||
throw new BarcodeException('Invalid character sequence: ' . $char_bar . $char_space);
|
||||
}
|
||||
|
||||
// create a bar-space sequence
|
||||
$seq = '';
|
||||
$bar_pattern = $this->getPattern($char_bar);
|
||||
$space_pattern = $this->getPattern($char_space);
|
||||
$chrlen = \strlen($bar_pattern);
|
||||
for ($pos = 0; $pos < $chrlen; ++$pos) {
|
||||
$seq .= ($bar_pattern[$pos] ?? '0') . ($space_pattern[$pos] ?? '0');
|
||||
}
|
||||
|
||||
$seqlen = \strlen($seq);
|
||||
for ($pos = 0; $pos < $seqlen; ++$pos) {
|
||||
$bar_width = (int) $seq[$pos];
|
||||
if (($pos % 2) === 0 && $bar_width > 0) {
|
||||
$this->bars[] = [$this->ncols, 0, $bar_width, 1];
|
||||
}
|
||||
|
||||
$this->ncols += $bar_width;
|
||||
}
|
||||
}
|
||||
|
||||
--$this->ncols;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* KlantIndex.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\KlantIndex;
|
||||
*
|
||||
* KlantIndex Barcode type class
|
||||
* KIX (Klant index - Customer index)
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class KlantIndex extends \Com\Tecnick\Barcode\Type\Linear\RoyalMailFourCc
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'KIX';
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$code = \strtoupper($this->code);
|
||||
$len = \strlen($code);
|
||||
for ($pos = 0; $pos < $len; ++$pos) {
|
||||
if (!\array_key_exists($code[$pos], $this::CHBAR)) {
|
||||
throw new BarcodeException('Invalid character: ' . (\ord($code[$pos]) & 0xFF));
|
||||
}
|
||||
}
|
||||
|
||||
$this->extcode = $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 3;
|
||||
$this->bars = [];
|
||||
$this->getCoreBars();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Msi.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\Msi;
|
||||
*
|
||||
* Msi Barcode type class
|
||||
* MSI (Variation of Plessey code)
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Msi extends \Com\Tecnick\Barcode\Type\Linear\MsiCheck
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'MSI';
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = $this->code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MsiCheck.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\MsiCheck;
|
||||
*
|
||||
* MsiCheck Barcode type class
|
||||
* MSI + CHECKSUM (modulo 11)
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class MsiCheck extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
protected function getPattern(string $digit): string
|
||||
{
|
||||
return $this::CHBAR[$digit] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'MSI+';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'0' => '100100100100',
|
||||
'1' => '100100100110',
|
||||
'2' => '100100110100',
|
||||
'3' => '100100110110',
|
||||
'4' => '100110100100',
|
||||
'5' => '100110100110',
|
||||
'6' => '100110110100',
|
||||
'7' => '100110110110',
|
||||
'8' => '110100100100',
|
||||
'9' => '110100100110',
|
||||
'A' => '110100110100',
|
||||
'B' => '110100110110',
|
||||
'C' => '110110100100',
|
||||
'D' => '110110100110',
|
||||
'E' => '110110110100',
|
||||
'F' => '110110110110',
|
||||
];
|
||||
|
||||
/**
|
||||
* Calculate the checksum
|
||||
*
|
||||
* @param string $code Code to represent.
|
||||
*
|
||||
* @return int char checksum.
|
||||
*/
|
||||
protected function getChecksum(string $code): int
|
||||
{
|
||||
$clen = \strlen($code);
|
||||
$pix = 2;
|
||||
$check = 0;
|
||||
for ($pos = $clen - 1; $pos >= 0; --$pos) {
|
||||
$hex = $code[$pos];
|
||||
if (!\ctype_xdigit($hex)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$check += \hexdec($hex) * $pix;
|
||||
++$pix;
|
||||
if ($pix > 7) {
|
||||
$pix = 2;
|
||||
}
|
||||
}
|
||||
|
||||
$check %= 11;
|
||||
if ($check > 0) {
|
||||
return 11 - $check;
|
||||
}
|
||||
|
||||
return $check;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = $this->code . (string) $this->getChecksum($this->code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->formatCode();
|
||||
$seq = '110'; // left guard
|
||||
$clen = \strlen($this->extcode);
|
||||
for ($pos = 0; $pos < $clen; ++$pos) {
|
||||
$digit = $this->extcode[$pos];
|
||||
if (!\array_key_exists($digit, $this::CHBAR)) {
|
||||
throw new BarcodeException('Invalid character: ' . (\ord($digit) & 0xFF));
|
||||
}
|
||||
|
||||
$seq .= $this->getPattern($digit);
|
||||
}
|
||||
|
||||
$seq .= '1001'; // right guard
|
||||
$this->processBinarySequence($this->getRawCodeRows($seq));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Pharma.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\Pharma;
|
||||
*
|
||||
* Pharma Barcode type class
|
||||
* PHARMACODE
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Pharma extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'PHARMA';
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$seq = '';
|
||||
$code = (int) $this->code;
|
||||
while ($code > 0) {
|
||||
if (($code % 2) === 0) {
|
||||
$seq .= '11100';
|
||||
$code -= 2;
|
||||
$code /= 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
$seq .= '100';
|
||||
--$code;
|
||||
$code /= 2;
|
||||
}
|
||||
|
||||
$seq = \substr($seq, 0, -2);
|
||||
$seq = \strrev($seq);
|
||||
$this->processBinarySequence($this->getRawCodeRows($seq));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* PharmaTwoTracks.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\PharmaTwoTracks;
|
||||
*
|
||||
* PharmaTwoTracks Barcode type class
|
||||
* PHARMACODE TWO-TRACKS
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class PharmaTwoTracks extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'PHARMA2T';
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
if (!\ctype_digit($this->code) || (int) $this->code < 1) {
|
||||
throw new BarcodeException('Invalid barcode value: the code must be a positive integer');
|
||||
}
|
||||
|
||||
$seq = '';
|
||||
$code = (int) $this->code;
|
||||
|
||||
do {
|
||||
switch ($code % 3) {
|
||||
case 0:
|
||||
$seq .= '3';
|
||||
$code = ($code - 3) / 3;
|
||||
break;
|
||||
case 1:
|
||||
$seq .= '1';
|
||||
$code = ($code - 1) / 3;
|
||||
break;
|
||||
case 2:
|
||||
$seq .= '2';
|
||||
$code = ($code - 2) / 3;
|
||||
}
|
||||
} while ($code !== 0);
|
||||
|
||||
$seq = \strrev($seq);
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 2;
|
||||
$this->bars = [];
|
||||
$len = \strlen($seq);
|
||||
for ($pos = 0; $pos < $len; ++$pos) {
|
||||
switch ($seq[$pos]) {
|
||||
case '1':
|
||||
$this->bars[] = [$this->ncols, 1, 1, 1];
|
||||
break;
|
||||
case '2':
|
||||
$this->bars[] = [$this->ncols, 0, 1, 1];
|
||||
break;
|
||||
case '3':
|
||||
$this->bars[] = [$this->ncols, 0, 1, 2];
|
||||
break;
|
||||
}
|
||||
|
||||
$this->ncols += 2;
|
||||
}
|
||||
|
||||
--$this->ncols;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Planet.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\Planet;
|
||||
*
|
||||
* Planet Barcode type class
|
||||
* PLANET
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Planet extends \Com\Tecnick\Barcode\Type\Linear\Postnet
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'PLANET';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'0' => '11222',
|
||||
'1' => '22211',
|
||||
'2' => '22121',
|
||||
'3' => '22112',
|
||||
'4' => '21221',
|
||||
'5' => '21212',
|
||||
'6' => '21122',
|
||||
'7' => '12221',
|
||||
'8' => '12212',
|
||||
'9' => '12122',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Postnet.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\Postnet;
|
||||
*
|
||||
* Postnet Barcode type class
|
||||
* POSTNET
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Postnet extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
protected function getBarHeight(string $char, int $pos): int
|
||||
{
|
||||
$pattern = $this::CHBAR[$char] ?? '11111';
|
||||
return (int) ($pattern[$pos] ?? '1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'POSTNET';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'0' => '22111',
|
||||
'1' => '11122',
|
||||
'2' => '11212',
|
||||
'3' => '11221',
|
||||
'4' => '12112',
|
||||
'5' => '12121',
|
||||
'6' => '12211',
|
||||
'7' => '21112',
|
||||
'8' => '21121',
|
||||
'9' => '21211',
|
||||
];
|
||||
|
||||
/**
|
||||
* Calculate the checksum.
|
||||
*
|
||||
* @param string $code Code to represent.
|
||||
*
|
||||
* @return int char checksum.
|
||||
*/
|
||||
protected function getChecksum(string $code): int
|
||||
{
|
||||
$sum = 0;
|
||||
$len = \strlen($code);
|
||||
for ($pos = 0; $pos < $len; ++$pos) {
|
||||
$sum += (int) $code[$pos];
|
||||
}
|
||||
|
||||
$check = $sum % 10;
|
||||
if ($check > 0) {
|
||||
return 10 - $check;
|
||||
}
|
||||
|
||||
return $check;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$code = \preg_replace('/[-\s]+/', '', $this->code);
|
||||
if ($code === null) {
|
||||
throw new BarcodeException('Code not valid');
|
||||
}
|
||||
$this->extcode = $code . $this->getChecksum($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 2;
|
||||
$this->bars = [];
|
||||
$this->formatCode();
|
||||
$clen = \strlen($this->extcode);
|
||||
// start bar
|
||||
$this->bars[] = [$this->ncols, 0, 1, 2];
|
||||
$this->ncols += 2;
|
||||
for ($chr = 0; $chr < $clen; ++$chr) {
|
||||
$char = $this->extcode[$chr];
|
||||
if (!\array_key_exists($char, $this::CHBAR)) {
|
||||
throw new BarcodeException('Invalid character: ' . (\ord($char) & 0xFF));
|
||||
}
|
||||
|
||||
for ($pos = 0; $pos < 5; ++$pos) {
|
||||
$bar_height = $this->getBarHeight($char, $pos);
|
||||
$this->bars[] = [$this->ncols, (int) \floor(1 / $bar_height), 1, $bar_height];
|
||||
$this->ncols += 2;
|
||||
}
|
||||
}
|
||||
|
||||
// end bar
|
||||
$this->bars[] = [$this->ncols, 0, 1, 2];
|
||||
++$this->ncols;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Raw.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\Raw
|
||||
*
|
||||
* Raw Barcode type class
|
||||
* RAW MODE (comma-separated rows)
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Raw extends \Com\Tecnick\Barcode\Type\Raw
|
||||
{
|
||||
/**
|
||||
* Barcode type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const TYPE = 'linear';
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'LRAW';
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* RoyalMailFourCc.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\RoyalMailFourCc;
|
||||
*
|
||||
* RoyalMailFourCc Barcode type class
|
||||
* RMS4CC (Royal Mail 4-state Customer Bar Code)
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class RoyalMailFourCc extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
protected function getChecksumDigit(string $char, int $idx): int
|
||||
{
|
||||
$pair = $this::CHKSUM[$char] ?? '00';
|
||||
return (int) ($pair[$idx] ?? '0');
|
||||
}
|
||||
|
||||
protected function getBarPattern(string $char, int $pos): string
|
||||
{
|
||||
$pattern = $this::CHBAR[$char] ?? '0000';
|
||||
return $pattern[$pos] ?? '0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'RMS4CC';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'0' => '3322',
|
||||
'1' => '3412',
|
||||
'2' => '3421',
|
||||
'3' => '4312',
|
||||
'4' => '4321',
|
||||
'5' => '4411',
|
||||
'6' => '3142',
|
||||
'7' => '3232',
|
||||
'8' => '3241',
|
||||
'9' => '4132',
|
||||
'A' => '4141',
|
||||
'B' => '4231',
|
||||
'C' => '3124',
|
||||
'D' => '3214',
|
||||
'E' => '3223',
|
||||
'F' => '4114',
|
||||
'G' => '4123',
|
||||
'H' => '4213',
|
||||
'I' => '1342',
|
||||
'J' => '1432',
|
||||
'K' => '1441',
|
||||
'L' => '2332',
|
||||
'M' => '2341',
|
||||
'N' => '2431',
|
||||
'O' => '1324',
|
||||
'P' => '1414',
|
||||
'Q' => '1423',
|
||||
'R' => '2314',
|
||||
'S' => '2323',
|
||||
'T' => '2413',
|
||||
'U' => '1144',
|
||||
'V' => '1234',
|
||||
'W' => '1243',
|
||||
'X' => '2134',
|
||||
'Y' => '2143',
|
||||
'Z' => '2233',
|
||||
];
|
||||
|
||||
/**
|
||||
* Characters used for checksum
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHKSUM = [
|
||||
'0' => '11',
|
||||
'1' => '12',
|
||||
'2' => '13',
|
||||
'3' => '14',
|
||||
'4' => '15',
|
||||
'5' => '10',
|
||||
'6' => '21',
|
||||
'7' => '22',
|
||||
'8' => '23',
|
||||
'9' => '24',
|
||||
'A' => '25',
|
||||
'B' => '20',
|
||||
'C' => '31',
|
||||
'D' => '32',
|
||||
'E' => '33',
|
||||
'F' => '34',
|
||||
'G' => '35',
|
||||
'H' => '30',
|
||||
'I' => '41',
|
||||
'J' => '42',
|
||||
'K' => '43',
|
||||
'L' => '44',
|
||||
'M' => '45',
|
||||
'N' => '40',
|
||||
'O' => '51',
|
||||
'P' => '52',
|
||||
'Q' => '53',
|
||||
'R' => '54',
|
||||
'S' => '55',
|
||||
'T' => '50',
|
||||
'U' => '01',
|
||||
'V' => '02',
|
||||
'W' => '03',
|
||||
'X' => '04',
|
||||
'Y' => '05',
|
||||
'Z' => '00',
|
||||
];
|
||||
|
||||
/**
|
||||
* Calculate the checksum.
|
||||
*
|
||||
* @param string $code code to represent.
|
||||
*
|
||||
* @return string char checksum.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getChecksum(string $code): string
|
||||
{
|
||||
$row = 0;
|
||||
$col = 0;
|
||||
$len = \strlen($code);
|
||||
for ($pos = 0; $pos < $len; ++$pos) {
|
||||
$char = $code[$pos];
|
||||
if (!\array_key_exists($char, $this::CHKSUM)) {
|
||||
throw new BarcodeException('Invalid character: ' . (\ord($char) & 0xFF));
|
||||
}
|
||||
|
||||
$row += $this->getChecksumDigit($char, 0);
|
||||
$col += $this->getChecksumDigit($char, 1);
|
||||
}
|
||||
|
||||
$row %= 6;
|
||||
$col %= 6;
|
||||
$check = \array_keys($this::CHKSUM, $row . $col, true);
|
||||
return (string) ($check[0] ?? '0');
|
||||
}
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$code = \strtoupper($this->code);
|
||||
$this->extcode = $code . $this->getChecksum($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the central bars
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getCoreBars(): void
|
||||
{
|
||||
$this->formatCode();
|
||||
$clen = \strlen($this->extcode);
|
||||
for ($chr = 0; $chr < $clen; ++$chr) {
|
||||
$char = $this->extcode[$chr];
|
||||
for ($pos = 0; $pos < 4; ++$pos) {
|
||||
switch ($this->getBarPattern($char, $pos)) {
|
||||
case '1':
|
||||
$this->bars[] = [$this->ncols, 0, 1, 2];
|
||||
break;
|
||||
case '2':
|
||||
$this->bars[] = [$this->ncols, 0, 1, 3];
|
||||
break;
|
||||
case '3':
|
||||
$this->bars[] = [$this->ncols, 1, 1, 1];
|
||||
break;
|
||||
case '4':
|
||||
$this->bars[] = [$this->ncols, 1, 1, 2];
|
||||
break;
|
||||
}
|
||||
|
||||
$this->ncols += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->ncols = 0;
|
||||
$this->nrows = 3;
|
||||
$this->bars = [];
|
||||
|
||||
// start bar
|
||||
$this->bars[] = [$this->ncols, 0, 1, 2];
|
||||
$this->ncols += 2;
|
||||
|
||||
$this->getCoreBars();
|
||||
|
||||
// stop bar
|
||||
$this->bars[] = [$this->ncols, 0, 1, 3];
|
||||
++$this->ncols;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* StandardTwoOfFive.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\StandardTwoOfFive;
|
||||
*
|
||||
* StandardTwoOfFive Barcode type class
|
||||
* Standard 2 of 5
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class StandardTwoOfFive extends \Com\Tecnick\Barcode\Type\Linear\StandardTwoOfFiveCheck
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'S25';
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = $this->code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* StandardTwoOfFiveCheck.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\StandardTwoOfFiveCheck;
|
||||
*
|
||||
* StandardTwoOfFiveCheck Barcode type class
|
||||
* Standard 2 of 5 + CHECKSUM
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class StandardTwoOfFiveCheck extends \Com\Tecnick\Barcode\Type\Linear
|
||||
{
|
||||
protected function getPattern(string $digit): string
|
||||
{
|
||||
return $this::CHBAR[$digit] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'S25+';
|
||||
|
||||
/**
|
||||
* Map characters to barcodes
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected const CHBAR = [
|
||||
'0' => '10101110111010',
|
||||
'1' => '11101010101110',
|
||||
'2' => '10111010101110',
|
||||
'3' => '11101110101010',
|
||||
'4' => '10101110101110',
|
||||
'5' => '11101011101010',
|
||||
'6' => '10111011101010',
|
||||
'7' => '10101011101110',
|
||||
'8' => '11101010111010',
|
||||
'9' => '10111010111010',
|
||||
];
|
||||
|
||||
/**
|
||||
* Calculate the checksum
|
||||
*
|
||||
* @param string $code Code to represent.
|
||||
*
|
||||
* @return int char checksum.
|
||||
*/
|
||||
protected function getChecksum(string $code): int
|
||||
{
|
||||
$clen = \strlen($code);
|
||||
$sum = 0;
|
||||
for ($idx = 0; $idx < $clen; $idx += 2) {
|
||||
$sum += (int) $code[$idx];
|
||||
}
|
||||
|
||||
$sum *= 3;
|
||||
for ($idx = 1; $idx < $clen; $idx += 2) {
|
||||
$sum += (int) $code[$idx];
|
||||
}
|
||||
|
||||
$check = $sum % 10;
|
||||
if ($check > 0) {
|
||||
return 10 - $check;
|
||||
}
|
||||
|
||||
return $check;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format code
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$this->extcode = $this->code . (string) $this->getChecksum($this->code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->formatCode();
|
||||
if ((\strlen($this->extcode) % 2) !== 0) {
|
||||
// add leading zero if code-length is odd
|
||||
$this->extcode = '0' . $this->extcode;
|
||||
}
|
||||
|
||||
$seq = '1110111010';
|
||||
$clen = \strlen($this->extcode);
|
||||
for ($idx = 0; $idx < $clen; ++$idx) {
|
||||
$digit = $this->extcode[$idx];
|
||||
if (!\array_key_exists($digit, $this::CHBAR)) {
|
||||
throw new BarcodeException('Invalid character: ' . (\ord($digit) & 0xFF));
|
||||
}
|
||||
|
||||
$seq .= $this->getPattern($digit);
|
||||
}
|
||||
|
||||
$seq .= '111010111';
|
||||
$this->processBinarySequence($this->getRawCodeRows($seq));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* UpcA.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\UpcA;
|
||||
*
|
||||
* UpcA Barcode type class
|
||||
* UPC-A
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class UpcA extends \Com\Tecnick\Barcode\Type\Linear\EanOneThree
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'UPCA';
|
||||
|
||||
/**
|
||||
* Fixed code length
|
||||
*/
|
||||
protected int $code_length = 12;
|
||||
|
||||
/**
|
||||
* Format the code
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$code = \str_pad($this->code, $this->code_length - 1, '0', STR_PAD_LEFT);
|
||||
$code .= $this->getChecksum($code);
|
||||
++$this->code_length;
|
||||
$this->extcode = '0' . $code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* UpcE.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Linear;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Linear\UpcE;
|
||||
*
|
||||
* UpcE Barcode type class
|
||||
* UPC-E
|
||||
*
|
||||
* UPC-E is a variation of UPC-A which allows for a more compact barcode by eliminating "extra" zeros.
|
||||
* Since the resulting UPC-E barcode is about half the size as an UPC-A barcode, UPC-E is generally used on products
|
||||
* with very small packaging where a full UPC-A barcode couldn't reasonably fit.
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class UpcE extends \Com\Tecnick\Barcode\Type\Linear\UpcA
|
||||
{
|
||||
protected function getCharAt(string $value, int $index): string
|
||||
{
|
||||
return $value[$index] ?? '0';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected function getUpceParityPattern(string $digit, int $check): array
|
||||
{
|
||||
$pattern = $this::PARITIES_UPCE[$digit][$check] ?? ['A', 'A', 'A', 'A', 'A', 'A'];
|
||||
return \array_values($pattern);
|
||||
}
|
||||
|
||||
protected function getBarPattern(string $parity, string $digit): string
|
||||
{
|
||||
return $this::CHBAR[$parity][$digit] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'UPCE';
|
||||
|
||||
/**
|
||||
* Fixed code length
|
||||
*/
|
||||
protected int $code_length = 12;
|
||||
|
||||
/**
|
||||
* Map parities
|
||||
*
|
||||
* @var array<int|string, array<int|string, array<string>>>
|
||||
*/
|
||||
protected const PARITIES_UPCE = [
|
||||
0 => [
|
||||
'0' => ['B', 'B', 'B', 'A', 'A', 'A'],
|
||||
'1' => ['B', 'B', 'A', 'B', 'A', 'A'],
|
||||
'2' => ['B', 'B', 'A', 'A', 'B', 'A'],
|
||||
'3' => ['B', 'B', 'A', 'A', 'A', 'B'],
|
||||
'4' => ['B', 'A', 'B', 'B', 'A', 'A'],
|
||||
'5' => ['B', 'A', 'A', 'B', 'B', 'A'],
|
||||
'6' => ['B', 'A', 'A', 'A', 'B', 'B'],
|
||||
'7' => ['B', 'A', 'B', 'A', 'B', 'A'],
|
||||
'8' => ['B', 'A', 'B', 'A', 'A', 'B'],
|
||||
'9' => ['B', 'A', 'A', 'B', 'A', 'B'],
|
||||
],
|
||||
1 => [
|
||||
'0' => ['A', 'A', 'A', 'B', 'B', 'B'],
|
||||
'1' => ['A', 'A', 'B', 'A', 'B', 'B'],
|
||||
'2' => ['A', 'A', 'B', 'B', 'A', 'B'],
|
||||
'3' => ['A', 'A', 'B', 'B', 'B', 'A'],
|
||||
'4' => ['A', 'B', 'A', 'A', 'B', 'B'],
|
||||
'5' => ['A', 'B', 'B', 'A', 'A', 'B'],
|
||||
'6' => ['A', 'B', 'B', 'B', 'A', 'A'],
|
||||
'7' => ['A', 'B', 'A', 'B', 'A', 'B'],
|
||||
'8' => ['A', 'B', 'A', 'B', 'B', 'A'],
|
||||
'9' => ['A', 'B', 'B', 'A', 'B', 'A'],
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Convert UPC-E code to UPC-A
|
||||
*
|
||||
* @param string $code Code to convert.
|
||||
*/
|
||||
protected function convertUpceToUpca(string $code): string
|
||||
{
|
||||
if ($code[5] < '3') {
|
||||
return '0' . $code[0] . $code[1] . $code[5] . '0000' . $code[2] . $code[3] . $code[4];
|
||||
}
|
||||
|
||||
if ($code[5] === '3') {
|
||||
return '0' . $code[0] . $code[1] . $code[2] . '00000' . $code[3] . $code[4];
|
||||
}
|
||||
|
||||
if ($code[5] === '4') {
|
||||
return '0' . $code[0] . $code[1] . $code[2] . $code[3] . '00000' . $code[4];
|
||||
}
|
||||
|
||||
return '0' . $code[0] . $code[1] . $code[2] . $code[3] . $code[4] . '0000' . $code[5];
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert UPC-A code to UPC-E
|
||||
*
|
||||
* @param string $code Code to convert.
|
||||
*/
|
||||
protected function convertUpcaToUpce(string $code): string
|
||||
{
|
||||
$tmp = \substr($code, 4, 3);
|
||||
if ($tmp === '000' || $tmp === '100' || $tmp === '200') {
|
||||
// manufacturer code ends in 000, 100, or 200
|
||||
return \substr($code, 2, 2) . \substr($code, 9, 3) . \substr($code, 4, 1);
|
||||
}
|
||||
|
||||
$tmp = \substr($code, 5, 2);
|
||||
if ($tmp === '00') {
|
||||
// manufacturer code ends in 00
|
||||
return \substr($code, 2, 3) . \substr($code, 10, 2) . '3';
|
||||
}
|
||||
|
||||
$tmp = \substr($code, 6, 1);
|
||||
if ($tmp === '0') {
|
||||
// manufacturer code ends in 0
|
||||
return \substr($code, 2, 4) . \substr($code, 11, 1) . '4';
|
||||
}
|
||||
|
||||
// manufacturer code does not end in zero
|
||||
return \substr($code, 2, 5) . \substr($code, 11, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the code
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function formatCode(): void
|
||||
{
|
||||
$code = $this->code;
|
||||
if (\strlen($code) === 6) {
|
||||
$code = $this->convertUpceToUpca($code);
|
||||
}
|
||||
|
||||
$code = \str_pad($code, $this->code_length - 1, '0', STR_PAD_LEFT);
|
||||
// append the computed check digit only when the input does not already carry it,
|
||||
// otherwise getChecksum() just validates it and returns 0 (avoid a spurious digit)
|
||||
$check = $this->getChecksum($code);
|
||||
if (\strlen($code) < $this->code_length) {
|
||||
$code .= $check;
|
||||
}
|
||||
|
||||
++$this->code_length;
|
||||
$this->extcode = '0' . $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bars array.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
if (!\is_numeric($this->code)) {
|
||||
throw new BarcodeException('Input code must be a number');
|
||||
}
|
||||
|
||||
$this->formatCode();
|
||||
$upce_code = $this->convertUpcaToUpce($this->extcode);
|
||||
$seq = '101'; // left guard bar
|
||||
$parity = $this->getUpceParityPattern($this->getCharAt($this->extcode, 1), $this->check);
|
||||
for ($pos = 0; $pos < 6; ++$pos) {
|
||||
$seq .= $this->getBarPattern($this->getCharAt($parity[$pos] ?? 'A', 0), $this->getCharAt($upce_code, $pos));
|
||||
}
|
||||
|
||||
$seq .= '010101'; // right guard bar
|
||||
$this->processBinarySequence($this->getRawCodeRows($seq));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Raw.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Raw
|
||||
*
|
||||
* Raw Barcode type class
|
||||
* RAW MODE (comma-separated rows)
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Raw extends \Com\Tecnick\Barcode\Type
|
||||
{
|
||||
/**
|
||||
* Generate the bars array
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->processBinarySequence($this->getRawCodeRows($this->code));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Square.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square
|
||||
*
|
||||
* Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Square extends \Com\Tecnick\Barcode\Type
|
||||
{
|
||||
/**
|
||||
* Barcode type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const TYPE = 'square';
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Aztec.php
|
||||
*
|
||||
* @since 2023-10-12
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
use Com\Tecnick\Barcode\Type\Square\Aztec\AztecHint;
|
||||
use Com\Tecnick\Barcode\Type\Square\Aztec\AztecRange;
|
||||
use Com\Tecnick\Barcode\Type\Square\Aztec\Data;
|
||||
use Com\Tecnick\Barcode\Type\Square\Aztec\Encode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Aztec
|
||||
*
|
||||
* Aztec Barcode type class
|
||||
*
|
||||
* @since 2023-10-12
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Aztec extends \Com\Tecnick\Barcode\Type\Square
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'AZTEC';
|
||||
|
||||
/**
|
||||
* Error correction code percentage of error check words.
|
||||
* A minimum of 23% + 3 words is recommended by ISO/IEC 24778:2008a.
|
||||
*/
|
||||
protected int $ecc = 33;
|
||||
|
||||
/**
|
||||
* Encoding mode
|
||||
*/
|
||||
protected string $hint = 'A';
|
||||
|
||||
/**
|
||||
* Mode:
|
||||
* - A = Automatic selection between Compact (priority) and Full Range.
|
||||
* - F = Force Full Range mode.
|
||||
*/
|
||||
protected string $mode = 'A';
|
||||
|
||||
/**
|
||||
* Extended Channel Interpretation (ECI) code to be added at the beginning of the stream.
|
||||
* See Data:ECI for the list of supported codes.
|
||||
* NOTE: Even if special FNC1 or ECI flag characters could be inserted
|
||||
* at any points in the stream, this will only be added at the beginning of the stream.
|
||||
*/
|
||||
protected int $eci = -1;
|
||||
|
||||
/**
|
||||
* Set extra (optional) parameters:
|
||||
* 1: ECC : Error correction code percentage of error check words.
|
||||
* A minimum of 23% + 3 words is recommended by ISO/IEC 24778:2008a.
|
||||
* 2: HINT : Encoding mode: A=Automatic, B=Binary.
|
||||
* 3: LAYERS : Custom number of layers (0 = auto).
|
||||
* 4: ECI : Extended Channel Interpretation (ECI) code. Use -1 for FNC1. See $this->eci.
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
|
||||
* @SuppressWarnings("PHPMD.NPathComplexity")
|
||||
*/
|
||||
protected function setParameters(): void
|
||||
{
|
||||
parent::setParameters();
|
||||
|
||||
// ecc percentage
|
||||
if (
|
||||
($this->params[0] ?? null) === null
|
||||
|| !\is_numeric($this->params[0])
|
||||
|| (int) $this->params[0] < 1
|
||||
|| (int) $this->params[0] > 100
|
||||
) {
|
||||
$this->params[0] = 33;
|
||||
}
|
||||
|
||||
$this->ecc = (int) $this->params[0];
|
||||
|
||||
// hint
|
||||
$aztecHint = AztecHint::fromLoose(\is_string($this->params[1] ?? null) ? $this->params[1] : '');
|
||||
$this->params[1] = $aztecHint->value;
|
||||
$this->hint = $aztecHint->value;
|
||||
|
||||
// mode
|
||||
$aztecRange = AztecRange::fromLoose(\is_string($this->params[2] ?? null) ? $this->params[2] : '');
|
||||
$this->params[2] = $aztecRange->value;
|
||||
$this->mode = $aztecRange->value;
|
||||
|
||||
// eci code. Used to set the charset encoding. See $this->eci.
|
||||
if (
|
||||
($this->params[3] ?? null) === null
|
||||
|| !\is_numeric($this->params[3])
|
||||
|| !\array_key_exists((int) $this->params[3], Data::ECI)
|
||||
) {
|
||||
$this->params[3] = -1;
|
||||
}
|
||||
|
||||
$this->eci = (int) $this->params[3];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bars array
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
if (\strlen($this->code) === 0) {
|
||||
throw new BarcodeException('Empty input');
|
||||
}
|
||||
|
||||
try {
|
||||
$encode = new Encode($this->code, $this->ecc, $this->eci, $this->hint, $this->mode);
|
||||
$grid = $encode->getGrid();
|
||||
$this->processBinarySequence($grid);
|
||||
} catch (BarcodeException $barcodeException) {
|
||||
throw new BarcodeException('AZTEC: ' . $barcodeException->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* AztecHint.php
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Aztec;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Aztec\AztecHint
|
||||
*
|
||||
* Backed enum for the Aztec Code encoding hint: A (automatic, default) or B
|
||||
* (binary).
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
enum AztecHint: string
|
||||
{
|
||||
/** Automatic encoding (default). */
|
||||
case Automatic = 'A';
|
||||
|
||||
/** Binary encoding. */
|
||||
case Binary = 'B';
|
||||
|
||||
/**
|
||||
* Resolve a loose Aztec hint value to the matching enum case.
|
||||
*
|
||||
* Accepts the canonical letter or an enum instance (returned unchanged).
|
||||
* Unknown values fall back to Automatic, matching the lenient behavior of
|
||||
* Aztec.
|
||||
*
|
||||
* @param string|self $value Hint letter or enum case.
|
||||
*/
|
||||
public static function fromLoose(string|self $value): self
|
||||
{
|
||||
if ($value instanceof self) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return self::tryFrom($value) ?? self::Automatic;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* AztecRange.php
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Aztec;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Aztec\AztecRange
|
||||
*
|
||||
* Backed enum for the Aztec Code symbol range mode: A (automatic selection
|
||||
* between Compact and Full Range, default) or F (force Full Range).
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
enum AztecRange: string
|
||||
{
|
||||
/** Automatic selection between Compact (priority) and Full Range (default). */
|
||||
case Automatic = 'A';
|
||||
|
||||
/** Force Full Range mode. */
|
||||
case FullRange = 'F';
|
||||
|
||||
/**
|
||||
* Resolve a loose Aztec range mode value to the matching enum case.
|
||||
*
|
||||
* Accepts the canonical letter or an enum instance (returned unchanged).
|
||||
* Unknown values fall back to Automatic, matching the lenient behavior of
|
||||
* Aztec.
|
||||
*
|
||||
* @param string|self $value Range mode letter or enum case.
|
||||
*/
|
||||
public static function fromLoose(string|self $value): self
|
||||
{
|
||||
if ($value instanceof self) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return self::tryFrom($value) ?? self::Automatic;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Bitstream.php
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Aztec;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Aztec\Bitstream
|
||||
*
|
||||
* Bitstream for Aztec Barcode type class
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.ExcessiveClassComplexity")
|
||||
*/
|
||||
abstract class Bitstream extends \Com\Tecnick\Barcode\Type\Square\Aztec\Layers
|
||||
{
|
||||
/**
|
||||
* Performs the high-level encoding for the given code and ECI mode.
|
||||
*
|
||||
* @param string $code The code to encode.
|
||||
* @param int $eci The ECI mode to use.
|
||||
* @param string $hint The mode to use.
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function highLevelEncoding(string $code, int $eci = 0, string $hint = 'A'): void
|
||||
{
|
||||
$this->addFLG($eci);
|
||||
$chrarr = \unpack('C*', $code);
|
||||
if ($chrarr === false) {
|
||||
throw new BarcodeException('Unable to unpack the code');
|
||||
}
|
||||
|
||||
$chars = \array_values($chrarr);
|
||||
$chrlen = \count($chars);
|
||||
if ($hint === 'B') {
|
||||
$this->binaryEncode($chars, $chrlen); // @phpstan-ignore argument.type
|
||||
return;
|
||||
}
|
||||
|
||||
$this->autoEncode($chars, $chrlen); // @phpstan-ignore argument.type
|
||||
}
|
||||
|
||||
/**
|
||||
* Forced binary encoding for the given characters.
|
||||
*
|
||||
* @param list<int> $chars Integer ASCII values of the characters to encode.
|
||||
* @param int $chrlen Length of the $chars array.
|
||||
*/
|
||||
protected function binaryEncode(array $chars, int $chrlen): void
|
||||
{
|
||||
$bits = Data::MODE_BITS[Data::MODE_BINARY] ?? 8;
|
||||
$this->addShift(Data::MODE_BINARY);
|
||||
if ($chrlen > 62) {
|
||||
$this->addRawCwd(5, 0);
|
||||
$this->addRawCwd(11, $chrlen - 31);
|
||||
for ($idx = 0; $idx < $chrlen; ++$idx) {
|
||||
$this->addRawCwd($bits, $chars[$idx] ?? 0);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($chrlen > 31) {
|
||||
$this->addRawCwd(5, 31);
|
||||
for ($idx = 0; $idx < 31; ++$idx) {
|
||||
$this->addRawCwd($bits, $chars[$idx] ?? 0);
|
||||
}
|
||||
|
||||
$this->addShift(Data::MODE_BINARY);
|
||||
$this->addRawCwd(5, $chrlen - 31);
|
||||
for ($idx = 31; $idx < $chrlen; ++$idx) {
|
||||
$this->addRawCwd($bits, $chars[$idx] ?? 0);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->addRawCwd(5, $chrlen);
|
||||
for ($idx = 0; $idx < $chrlen; ++$idx) {
|
||||
$this->addRawCwd($bits, $chars[$idx] ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatic encoding for the given characters.
|
||||
*
|
||||
* @param list<int> $chars Integer ASCII values of the characters to encode.
|
||||
* @param int $chrlen Length of the $chars array.
|
||||
*/
|
||||
protected function autoEncode(array $chars, int $chrlen): void
|
||||
{
|
||||
$idx = 0;
|
||||
while ($idx < $chrlen) {
|
||||
if ($this->processBinaryChars($chars, $idx, $chrlen)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->processPunctPairs($chars, $idx, $chrlen)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->processModeChars($chars, $idx, $chrlen);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process mode characters.
|
||||
*
|
||||
* @param list<int> $chars The array of characters.
|
||||
* @param int $idx The current character index.
|
||||
* @param int $chrlen The total number of characters to process.
|
||||
*/
|
||||
protected function processModeChars(array &$chars, int &$idx, int $chrlen): void
|
||||
{
|
||||
$ord = $chars[$idx] ?? 0;
|
||||
$mode = $this->isSameMode($this->encmode, $ord) ? $this->encmode : $this->charMode($ord);
|
||||
|
||||
$nchr = $this->countModeChars($chars, $idx, $chrlen, $mode);
|
||||
if ($this->encmode !== $mode) {
|
||||
$shiftMap = Data::SHIFT_MAP[$this->encmode] ?? [];
|
||||
$canShift = $nchr === 1 && (\array_key_exists($mode, $shiftMap) && $shiftMap[$mode] !== []);
|
||||
if ($canShift) {
|
||||
$this->addShift($mode);
|
||||
$this->mergeTmpCwd();
|
||||
$idx += $nchr;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->addLatch($mode);
|
||||
}
|
||||
|
||||
$this->mergeTmpCwd();
|
||||
$idx += $nchr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count consecutive characters in the same mode.
|
||||
*
|
||||
* @param list<int> $chars The array of characters.
|
||||
* @param int $idx The current character index.
|
||||
* @param int $chrlen The total number of characters to process.
|
||||
* @param int $mode The current mode.
|
||||
*/
|
||||
protected function countModeChars(array &$chars, int $idx, int $chrlen, int $mode): int
|
||||
{
|
||||
$this->tmpCdws = [];
|
||||
$nbits = Data::MODE_BITS[$mode] ?? 0;
|
||||
$count = 0;
|
||||
do {
|
||||
$ord = $chars[$idx] ?? 0;
|
||||
if (
|
||||
!$this->isSameMode($mode, $ord)
|
||||
|| $idx < ($chrlen - 1) && $this->punctPairMode($ord, $chars[$idx + 1] ?? 0) > 0
|
||||
) {
|
||||
return $count;
|
||||
}
|
||||
|
||||
$this->tmpCdws[] = [$nbits, $this->charEnc($mode, $ord)];
|
||||
++$count;
|
||||
++$idx;
|
||||
} while ($idx < $chrlen);
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process consecutive binary characters.
|
||||
*
|
||||
* @param list<int> $chars The array of characters.
|
||||
* @param int $idx The current character index.
|
||||
* @param int $chrlen The total number of characters to process.
|
||||
*
|
||||
* @return bool True if binary characters have been found and processed.
|
||||
*/
|
||||
protected function processBinaryChars(array &$chars, int &$idx, int $chrlen): bool
|
||||
{
|
||||
$binchrs = $this->countBinaryChars($chars, $idx, $chrlen);
|
||||
if ($binchrs === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$encmode = $this->encmode;
|
||||
$this->addShift(Data::MODE_BINARY);
|
||||
if ($binchrs > 62) {
|
||||
$this->addRawCwd(5, 0);
|
||||
$this->addRawCwd(11, $binchrs - 31);
|
||||
$this->mergeTmpCwdRaw();
|
||||
$idx += $binchrs;
|
||||
$this->encmode = $encmode;
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($binchrs > 31) {
|
||||
$nbits = Data::MODE_BITS[Data::MODE_BINARY] ?? 8;
|
||||
$this->addRawCwd(5, 31);
|
||||
for ($bcw = 0; $bcw < 31; ++$bcw) {
|
||||
$tmpCdw = $this->tmpCdws[$bcw] ?? [0, 0];
|
||||
$this->addRawCwd($nbits, $tmpCdw[1]);
|
||||
}
|
||||
|
||||
$this->addShift(Data::MODE_BINARY);
|
||||
$this->addRawCwd(5, $binchrs - 31);
|
||||
for ($bcw = 31; $bcw < $binchrs; ++$bcw) {
|
||||
$tmpCdw = $this->tmpCdws[$bcw] ?? [0, 0];
|
||||
$this->addRawCwd($nbits, $tmpCdw[1]);
|
||||
}
|
||||
|
||||
$idx += $binchrs;
|
||||
$this->encmode = $encmode;
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->addRawCwd(5, $binchrs);
|
||||
$this->mergeTmpCwdRaw();
|
||||
$idx += $binchrs;
|
||||
$this->encmode = $encmode;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count consecutive binary characters.
|
||||
*
|
||||
* @param list<int> $chars The array of characters.
|
||||
* @param int $idx The current character index.
|
||||
* @param int $chrlen The total number of characters to process.
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
|
||||
*/
|
||||
protected function countBinaryChars(array &$chars, int $idx, int $chrlen): int
|
||||
{
|
||||
$this->tmpCdws = [];
|
||||
$count = 0;
|
||||
$nbits = Data::MODE_BITS[Data::MODE_BINARY] ?? 8;
|
||||
while ($idx < $chrlen && $count < 2048) {
|
||||
$ord = $chars[$idx] ?? 0;
|
||||
if ($this->charMode($ord) !== Data::MODE_BINARY) {
|
||||
return $count;
|
||||
}
|
||||
|
||||
$this->tmpCdws[] = [$nbits, $ord];
|
||||
++$count;
|
||||
++$idx;
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process consecutive special Punctuation Pairs.
|
||||
*
|
||||
* @param list<int> $chars The array of characters.
|
||||
* @param int $idx The current character index.
|
||||
* @param int $chrlen The total number of characters to process.
|
||||
*
|
||||
* @return bool True if pair characters have been found and processed.
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
|
||||
*/
|
||||
protected function processPunctPairs(array &$chars, int &$idx, int $chrlen): bool
|
||||
{
|
||||
$ppairs = $this->countPunctPairs($chars, $idx, $chrlen);
|
||||
if ($ppairs === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($this->encmode) {
|
||||
case Data::MODE_PUNCT:
|
||||
break;
|
||||
case Data::MODE_MIXED:
|
||||
$this->addLatch(Data::MODE_PUNCT);
|
||||
break;
|
||||
case Data::MODE_UPPER:
|
||||
case Data::MODE_LOWER:
|
||||
if ($ppairs > 1) {
|
||||
$this->addLatch(Data::MODE_PUNCT);
|
||||
}
|
||||
|
||||
break;
|
||||
case Data::MODE_DIGIT:
|
||||
$common = $this->countPunctAndDigitChars($chars, $idx, $chrlen);
|
||||
$clen = \count($common);
|
||||
if ($clen > 0 && $clen < 6) {
|
||||
$this->tmpCdws = $common;
|
||||
$this->mergeTmpCwdRaw();
|
||||
$idx += $clen;
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($ppairs > 2) {
|
||||
$this->addLatch(Data::MODE_PUNCT);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$this->mergeTmpCwd(Data::MODE_PUNCT);
|
||||
$idx += $ppairs * 2;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count consecutive special Punctuation Pairs.
|
||||
*
|
||||
* @param list<int> $chars The array of characters.
|
||||
* @param int $idx The current character index.
|
||||
* @param int $chrlen The total number of characters to process.
|
||||
*/
|
||||
protected function countPunctPairs(array &$chars, int $idx, int $chrlen): int
|
||||
{
|
||||
$this->tmpCdws = [];
|
||||
$pairs = 0;
|
||||
$maxidx = $chrlen - 1;
|
||||
while ($idx < $maxidx) {
|
||||
$pmode = $this->punctPairMode($chars[$idx] ?? 0, $chars[$idx + 1] ?? 0);
|
||||
if ($pmode === 0) {
|
||||
return $pairs;
|
||||
}
|
||||
|
||||
$this->tmpCdws[] = [5, $pmode];
|
||||
++$pairs;
|
||||
$idx += 2;
|
||||
}
|
||||
|
||||
return $pairs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of consecutive charcters that are in both PUNCT or DIGIT modes.
|
||||
* Returns the array with the codewords.
|
||||
*
|
||||
* @param list<int> $chars The string to count the characters in.
|
||||
* @param int $idx The starting index to count from.
|
||||
* @param int $chrlen The length of the string to count.
|
||||
*
|
||||
* @return array<int, array{int, int}> The array of codewords.
|
||||
*/
|
||||
protected function countPunctAndDigitChars(array $chars, int $idx, int $chrlen): array
|
||||
{
|
||||
$words = [];
|
||||
while ($idx < $chrlen) {
|
||||
$ord = $chars[$idx] ?? 0;
|
||||
if (!$this->isPunctAndDigitChar($ord)) {
|
||||
return $words;
|
||||
}
|
||||
|
||||
$words[] = [4, $this->charEnc(Data::MODE_DIGIT, $ord)];
|
||||
++$idx;
|
||||
}
|
||||
|
||||
return $words;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,327 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Codeword.php
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Aztec;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Aztec\Codeword
|
||||
*
|
||||
* Codeword utility methods for Aztec Barcode type class
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Codeword
|
||||
{
|
||||
/**
|
||||
* @param array<int> $bitstream
|
||||
*/
|
||||
protected function getBitstreamBit(array $bitstream, int $index): int
|
||||
{
|
||||
if (!\array_key_exists($index, $bitstream)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return $bitstream[$index] === 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{int, int}>
|
||||
*/
|
||||
protected function getLatchMap(int $fromMode, int $toMode): array
|
||||
{
|
||||
$map = Data::LATCH_MAP[$fromMode][$toMode] ?? [];
|
||||
return \array_values($map);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{int, int}>
|
||||
*/
|
||||
protected function getShiftMap(int $fromMode, int $toMode): array
|
||||
{
|
||||
$map = Data::SHIFT_MAP[$fromMode][$toMode] ?? [];
|
||||
return \array_values($map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Current character encoding mode.
|
||||
*/
|
||||
protected int $encmode = Data::MODE_UPPER;
|
||||
|
||||
/**
|
||||
* Array containing the high-level encoding bitstream.
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
protected array $bitstream = [];
|
||||
|
||||
/**
|
||||
* Temporary array of codewords.
|
||||
*
|
||||
* @var array<int, array{int, int}>
|
||||
*/
|
||||
protected array $tmpCdws = [];
|
||||
|
||||
/**
|
||||
* Count the total number of bits in the bitstream.
|
||||
*/
|
||||
protected int $totbits = 0;
|
||||
|
||||
/**
|
||||
* Encodes a character using the specified mode and ordinal value.
|
||||
*
|
||||
* @param int $mode The encoding mode.
|
||||
* @param int $ord The ordinal value of the character to encode.
|
||||
*
|
||||
* @return int The encoded character.
|
||||
*/
|
||||
protected function charEnc(int $mode, int $ord): int
|
||||
{
|
||||
return Data::CHAR_ENC[$mode][$ord] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the character mode for a given ASCII code.
|
||||
*
|
||||
* @param int $ord The ASCII code of the character.
|
||||
*
|
||||
* @return int The character mode.
|
||||
*/
|
||||
protected function charMode(int $ord): int
|
||||
{
|
||||
return Data::CHAR_MODES[$ord] ?? Data::MODE_BINARY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if current character is supported by the current code.
|
||||
*
|
||||
* @param int $mode The mode to check.
|
||||
* @param int $ord The character ASCII value to compare against.
|
||||
*
|
||||
* @return bool Returns true if the mode is the same as the ordinal value, false otherwise.
|
||||
*/
|
||||
protected function isSameMode(int $mode, int $ord): bool
|
||||
{
|
||||
return (
|
||||
$mode === $this->charMode($ord)
|
||||
|| $ord === 32
|
||||
&& $mode !== Data::MODE_PUNCT
|
||||
|| $mode === Data::MODE_PUNCT
|
||||
&& ($ord === 13 || $ord === 44 || $ord === 46)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the character is in common between the PUNCT and DIGIT modes.
|
||||
* Characters ' ' (32), '.' (46) and ',' (44) are in common between the PUNCT and DIGIT modes.
|
||||
*
|
||||
* @param int $ord Integer ASCII code of the character to check.
|
||||
*/
|
||||
protected function isPunctAndDigitChar(int $ord): bool
|
||||
{
|
||||
return $ord === 32 || $ord === 44 || $ord === 46;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the PUNCT two-bytes code if the given two characters are a punctuation pair.
|
||||
* Punct codes 2–5 encode two bytes each.
|
||||
*
|
||||
* @param int $ord The current character code.
|
||||
* @param int $next The next character code.
|
||||
*/
|
||||
protected function punctPairMode(int $ord, int $next): int
|
||||
{
|
||||
$key = ($ord << 8) + $next;
|
||||
return match ($key) {
|
||||
(13 << 8) + 10 => 2,
|
||||
(46 << 8) + 32 => 3,
|
||||
(44 << 8) + 32 => 4,
|
||||
(58 << 8) + 32 => 5,
|
||||
default => 0,
|
||||
}; // no punct pair
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a new Codeword as a big-endian bit sequence.
|
||||
*
|
||||
* @param array<int> $bitstream Array of bits to append to.
|
||||
* @param int $totbits Number of bits in the bitstream.
|
||||
* @param int $wsize The number of bits in the codeword.
|
||||
* @param int $value The value of the codeword.
|
||||
*/
|
||||
protected function appendWordToBitstream(array &$bitstream, int &$totbits, int $wsize, int $value): void
|
||||
{
|
||||
for ($idx = $wsize - 1; $idx >= 0; --$idx) {
|
||||
$bitstream[] = ($value >> $idx) & 1;
|
||||
}
|
||||
|
||||
$totbits += $wsize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the bitstream to words.
|
||||
*
|
||||
* @param array<int> $bitstream Array of bits to convert.
|
||||
* @param int $totbits Number of bits in the bitstream.
|
||||
* @param int $wsize The word size.
|
||||
*
|
||||
* @return array<int> Array of words.
|
||||
*/
|
||||
protected function bitstreamToWords(array $bitstream, int $totbits, int $wsize): array
|
||||
{
|
||||
$words = [];
|
||||
$numwords = (int) \ceil($totbits / $wsize);
|
||||
for ($idx = 0; $idx < $numwords; ++$idx) {
|
||||
$wrd = 0;
|
||||
for ($bit = 0; $bit < $wsize; ++$bit) {
|
||||
$pos = ($idx * $wsize) + $bit;
|
||||
if ($this->getBitstreamBit($bitstream, $pos) === 1) {
|
||||
$wrd |= 1 << ($wsize - $bit - 1); // reverse order
|
||||
}
|
||||
}
|
||||
|
||||
$words[] = $wrd;
|
||||
}
|
||||
|
||||
return $words;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new Codeword as a big-endian bit sequence.
|
||||
*
|
||||
* @param int $bits The number of bits in the codeword.
|
||||
* @param int $value The value of the codeword.
|
||||
*/
|
||||
protected function addRawCwd(int $bits, int $value): void
|
||||
{
|
||||
$this->appendWordToBitstream($this->bitstream, $this->totbits, $bits, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a Codeword.
|
||||
*
|
||||
* @param int $mode The encoding mode.
|
||||
* @param int $value The value to encode.
|
||||
*/
|
||||
protected function addCdw(int $mode, int $value): void
|
||||
{
|
||||
$this->addRawCwd(Data::MODE_BITS[$mode] ?? 0, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Latch to another mode.
|
||||
*
|
||||
* @param int $mode The new encoding mode.
|
||||
*/
|
||||
protected function addLatch(int $mode): void
|
||||
{
|
||||
$latch = $this->getLatchMap($this->encmode, $mode);
|
||||
foreach ($latch as $cdw) {
|
||||
$this->addRawCwd($cdw[0], $cdw[1]);
|
||||
}
|
||||
|
||||
$this->encmode = $mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shift to another mode.
|
||||
*/
|
||||
protected function addShift(int $mode): void
|
||||
{
|
||||
$shift = $this->getShiftMap($this->encmode, $mode);
|
||||
foreach ($shift as $cdw) {
|
||||
$this->addRawCwd($cdw[0], $cdw[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the temporary codewords array with the current codewords array.
|
||||
* Shift to the specified mode.
|
||||
*
|
||||
* @param int $mode The encoding mode for the codewords.
|
||||
*/
|
||||
protected function mergeTmpCwdWithShift(int $mode): void
|
||||
{
|
||||
foreach ($this->tmpCdws as $tmpCdw) {
|
||||
$this->addShift($mode);
|
||||
$this->addRawCwd($tmpCdw[0], $tmpCdw[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the temporary codewords array with the current codewords array.
|
||||
* No shift is performed.
|
||||
*/
|
||||
protected function mergeTmpCwdRaw(): void
|
||||
{
|
||||
foreach ($this->tmpCdws as $tmpCdw) {
|
||||
$this->addRawCwd($tmpCdw[0], $tmpCdw[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge temporary codewords with current codewords based on the encoding mode.
|
||||
*
|
||||
* @param int $mode The encoding mode to use for merging codewords.
|
||||
* If negative, the current encoding mode will be used.
|
||||
*/
|
||||
protected function mergeTmpCwd(int $mode = -1): void
|
||||
{
|
||||
if ($mode < 0 || $this->encmode === $mode) {
|
||||
$this->mergeTmpCwdRaw();
|
||||
$this->tmpCdws = [];
|
||||
return;
|
||||
}
|
||||
|
||||
$this->mergeTmpCwdWithShift($mode);
|
||||
$this->tmpCdws = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the FLG (Function Length Group) codeword to the data codewords.
|
||||
*
|
||||
* @param int $eci Extended Channel Interpretation value. If negative, the function does nothing.
|
||||
*/
|
||||
protected function addFLG(int $eci): void
|
||||
{
|
||||
if ($eci < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->encmode !== Data::MODE_PUNCT) {
|
||||
$this->addShift(Data::MODE_PUNCT);
|
||||
}
|
||||
|
||||
if ($eci === 0) {
|
||||
$this->addRawCwd(3, 0); // FNC1
|
||||
return;
|
||||
}
|
||||
|
||||
$seci = (string) $eci;
|
||||
$digits = \strlen($seci);
|
||||
$this->addRawCwd(3, $digits); // 1–6 digits
|
||||
for ($idx = 0; $idx < $digits; ++$idx) {
|
||||
$this->addCdw(Data::MODE_DIGIT, $this->charEnc(Data::MODE_DIGIT, \ord($seci[$idx])));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,559 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Data.php
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Aztec;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Aztec\Data
|
||||
*
|
||||
* Data for Aztec Barcode type class
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Data
|
||||
{
|
||||
/**
|
||||
* Code character encoding mode for uppercase letters.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_UPPER = 0;
|
||||
|
||||
/**
|
||||
* Code character encoding mode for lowercase letters.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_LOWER = 1;
|
||||
|
||||
/**
|
||||
* Code character encoding mode for digits.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_DIGIT = 2;
|
||||
|
||||
/**
|
||||
* Code character encoding mode for mixed cases.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_MIXED = 3;
|
||||
|
||||
/**
|
||||
* Code character encoding mode for punctuation.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_PUNCT = 4;
|
||||
|
||||
/**
|
||||
* Code character encoding mode for binary.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_BINARY = 5;
|
||||
|
||||
/**
|
||||
* Number of bits for each character encoding mode.
|
||||
*
|
||||
* @var array{int, int, int, int, int, int}
|
||||
*/
|
||||
public const MODE_BITS = [
|
||||
5, // 0 = MODE_UPPER
|
||||
5, // 1 = MODE_LOWER
|
||||
4, // 2 = MODE_DIGIT
|
||||
5, // 3 = MODE_MIXED
|
||||
5, // 4 = MODE_PUNCT
|
||||
8, // 5 = MODE_BINARY
|
||||
];
|
||||
|
||||
/**
|
||||
* Code character encoding for each mode.
|
||||
*
|
||||
* @var array<int, array<int>>
|
||||
*/
|
||||
public const CHAR_ENC = [
|
||||
// MODE_UPPER (initial mode)
|
||||
0 => [
|
||||
32 => 1, // ' ' (SP)
|
||||
65 => 2, // 'A'
|
||||
66 => 3, // 'B'
|
||||
67 => 4, // 'C'
|
||||
68 => 5, // 'D'
|
||||
69 => 6, // 'E'
|
||||
70 => 7, // 'F'
|
||||
71 => 8, // 'G'
|
||||
72 => 9, // 'H'
|
||||
73 => 10, // 'I'
|
||||
74 => 11, // 'J'
|
||||
75 => 12, // 'K'
|
||||
76 => 13, // 'L'
|
||||
77 => 14, // 'M'
|
||||
78 => 15, // 'N'
|
||||
79 => 16, // 'O'
|
||||
80 => 17, // 'P'
|
||||
81 => 18, // 'Q'
|
||||
82 => 19, // 'R'
|
||||
83 => 20, // 'S'
|
||||
84 => 21, // 'T'
|
||||
85 => 22, // 'U'
|
||||
86 => 23, // 'V'
|
||||
87 => 24, // 'W'
|
||||
88 => 25, // 'X'
|
||||
89 => 26, // 'Y'
|
||||
90 => 27, // 'Z'
|
||||
],
|
||||
// MODE_LOWER
|
||||
1 => [
|
||||
32 => 1, // ' ' (SP)
|
||||
97 => 2, // 'a'
|
||||
98 => 3, // 'b'
|
||||
99 => 4, // 'c'
|
||||
100 => 5, // 'd'
|
||||
101 => 6, // 'e'
|
||||
102 => 7, // 'f'
|
||||
103 => 8, // 'g'
|
||||
104 => 9, // 'h'
|
||||
105 => 10, // 'i'
|
||||
106 => 11, // 'j'
|
||||
107 => 12, // 'k'
|
||||
108 => 13, // 'l'
|
||||
109 => 14, // 'm'
|
||||
110 => 15, // 'n'
|
||||
111 => 16, // 'o'
|
||||
112 => 17, // 'p'
|
||||
113 => 18, // 'q'
|
||||
114 => 19, // 'r'
|
||||
115 => 20, // 's'
|
||||
116 => 21, // 't'
|
||||
117 => 22, // 'u'
|
||||
118 => 23, // 'v'
|
||||
119 => 24, // 'w'
|
||||
120 => 25, // 'x'
|
||||
121 => 26, // 'y'
|
||||
122 => 27, // 'z'
|
||||
],
|
||||
// MODE_DIGIT
|
||||
2 => [
|
||||
32 => 1, // ' ' (SP)
|
||||
44 => 12, // ','
|
||||
46 => 13, // '.'
|
||||
48 => 2, // '0'
|
||||
49 => 3, // '1'
|
||||
50 => 4, // '2'
|
||||
51 => 5, // '3'
|
||||
52 => 6, // '4'
|
||||
53 => 7, // '5'
|
||||
54 => 8, // '6'
|
||||
55 => 9, // '7'
|
||||
56 => 10, // '8'
|
||||
57 => 11, // '9'
|
||||
],
|
||||
// MODE_MIXED
|
||||
3 => [
|
||||
32 => 1, // ' ' (SP)
|
||||
1 => 2, // '^A' (SOH)
|
||||
2 => 3, // '^B' (STX)
|
||||
3 => 4, // '^C' (ETX)
|
||||
4 => 5, // '^D' (EOT)
|
||||
5 => 6, // '^E' (ENQ)
|
||||
6 => 7, // '^F' (ACK)
|
||||
7 => 8, // '^G' (BEL)
|
||||
8 => 9, // '^H' (BS)
|
||||
9 => 10, // '^I' (HT)
|
||||
10 => 11, // '^J' (LF)
|
||||
11 => 12, // '^K' (VT)
|
||||
12 => 13, // '^L' (FF)
|
||||
13 => 14, // '^M' (CR)
|
||||
27 => 15, // '^[' (ESC)
|
||||
28 => 16, // '^\' (FS)
|
||||
29 => 17, // '^]' (GS)
|
||||
30 => 18, // '^^' (RS)
|
||||
31 => 19, // '^_' (US)
|
||||
64 => 20, // '@'
|
||||
92 => 21, // '\'
|
||||
94 => 22, // '^'
|
||||
95 => 23, // '_'
|
||||
96 => 24, // '`'
|
||||
124 => 25, // '|'
|
||||
126 => 26, // '~'
|
||||
127 => 27, // '^?' (DEL)
|
||||
],
|
||||
// MODE_PUNCT
|
||||
4 => [
|
||||
13 => 1, // '\r' (CR)
|
||||
33 => 6, // '!'
|
||||
34 => 7, // '"'
|
||||
35 => 8, // '#'
|
||||
36 => 9, // '$'
|
||||
37 => 10, // '%'
|
||||
38 => 11, // '&'
|
||||
39 => 12, // '''
|
||||
40 => 13, // '('
|
||||
41 => 14, // ')'
|
||||
42 => 15, // '*'
|
||||
43 => 16, // '+'
|
||||
44 => 17, // ','
|
||||
45 => 18, // '-'
|
||||
46 => 19, // '.'
|
||||
47 => 20, // '/'
|
||||
58 => 21, // ':'
|
||||
59 => 22, // ';'
|
||||
60 => 23, // '<'
|
||||
61 => 24, // '='
|
||||
62 => 25, // '>'
|
||||
63 => 26, // '?'
|
||||
91 => 27, // '['
|
||||
93 => 28, // ']'
|
||||
123 => 29, // '{'
|
||||
125 => 30, // '}'
|
||||
],
|
||||
// MODE_BINARY (all 8-bit values are valid)
|
||||
5 => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* Map character ASCII codes to their non-binary mode.
|
||||
* Exceptions are:
|
||||
* - the space ' ' character (32) that maps for modes 0,1,2.
|
||||
* - the carriage return '\r' character (13) that maps for modes 3,4.
|
||||
* - the comma ',' and dot '.' characters (44,46) that map for modes 2,4.
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
public const CHAR_MODES = [
|
||||
1 => 3, // '^A' (SOH)
|
||||
2 => 3, // '^B' (STX)
|
||||
3 => 3, // '^C' (ETX)
|
||||
4 => 3, // '^D' (EOT)
|
||||
5 => 3, // '^E' (ENQ)
|
||||
6 => 3, // '^F' (ACK)
|
||||
7 => 3, // '^G' (BEL)
|
||||
8 => 3, // '^H' (BS)
|
||||
9 => 3, // '^I' (HT)
|
||||
10 => 3, // '^J' (LF)
|
||||
11 => 3, // '^K' (VT)
|
||||
12 => 3, // '^L' (FF)
|
||||
13 => 3, // '^M' (CR) [3,4]
|
||||
27 => 3, // '^[' (ESC)
|
||||
28 => 3, // '^\' (FS)
|
||||
29 => 3, // '^]' (GS)
|
||||
30 => 3, // '^^' (RS)
|
||||
31 => 3, // '^_' (US)
|
||||
32 => 0, // ' ' [0,1,2]
|
||||
33 => 4, // '!'
|
||||
34 => 4, // '"'
|
||||
35 => 4, // '#'
|
||||
36 => 4, // '$'
|
||||
37 => 4, // '%'
|
||||
38 => 4, // '&'
|
||||
39 => 4, // '''
|
||||
40 => 4, // '('
|
||||
41 => 4, // ')'
|
||||
42 => 4, // '*'
|
||||
43 => 4, // '+'f
|
||||
44 => 2, // ',' [2,4]
|
||||
45 => 4, // '-'
|
||||
46 => 2, // '.' [2,4]
|
||||
47 => 4, // '/'
|
||||
48 => 2, // '0'
|
||||
49 => 2, // '1'
|
||||
50 => 2, // '2'
|
||||
51 => 2, // '3'
|
||||
52 => 2, // '4'
|
||||
53 => 2, // '5'
|
||||
54 => 2, // '6'
|
||||
55 => 2, // '7'
|
||||
56 => 2, // '8'
|
||||
57 => 2, // '9'
|
||||
58 => 4, // ':'
|
||||
59 => 4, // ';'
|
||||
60 => 4, // '<'
|
||||
61 => 4, // '='
|
||||
62 => 4, // '>'
|
||||
63 => 4, // '?'
|
||||
64 => 3, // '@'
|
||||
65 => 0, // 'A'
|
||||
66 => 0, // 'B'
|
||||
67 => 0, // 'C'
|
||||
68 => 0, // 'D'
|
||||
69 => 0, // 'E'
|
||||
70 => 0, // 'F'
|
||||
71 => 0, // 'G'
|
||||
72 => 0, // 'H'
|
||||
73 => 0, // 'I'
|
||||
74 => 0, // 'J'
|
||||
75 => 0, // 'K'
|
||||
76 => 0, // 'L'
|
||||
77 => 0, // 'M'
|
||||
78 => 0, // 'N'
|
||||
79 => 0, // 'O'
|
||||
80 => 0, // 'P'
|
||||
81 => 0, // 'Q'
|
||||
82 => 0, // 'R'
|
||||
83 => 0, // 'S'
|
||||
84 => 0, // 'T'
|
||||
85 => 0, // 'U'
|
||||
86 => 0, // 'V'
|
||||
87 => 0, // 'W'
|
||||
88 => 0, // 'X'
|
||||
89 => 0, // 'Y'
|
||||
90 => 0, // 'Z'
|
||||
91 => 4, // '['
|
||||
92 => 3, // '\'
|
||||
93 => 4, // ']'
|
||||
94 => 3, // '^'
|
||||
95 => 3, // '_'
|
||||
96 => 3, // '`'
|
||||
97 => 1, // 'a'
|
||||
98 => 1, // 'b'
|
||||
99 => 1, // 'c'
|
||||
100 => 1, // 'd'
|
||||
101 => 1, // 'e'
|
||||
102 => 1, // 'f'
|
||||
103 => 1, // 'g'
|
||||
104 => 1, // 'h'
|
||||
105 => 1, // 'i'
|
||||
106 => 1, // 'j'
|
||||
107 => 1, // 'k'
|
||||
108 => 1, // 'l'
|
||||
109 => 1, // 'm'
|
||||
110 => 1, // 'n'
|
||||
111 => 1, // 'o'
|
||||
112 => 1, // 'p'
|
||||
113 => 1, // 'q'
|
||||
114 => 1, // 'r'
|
||||
115 => 1, // 's'
|
||||
116 => 1, // 't'
|
||||
117 => 1, // 'u'
|
||||
118 => 1, // 'v'
|
||||
119 => 1, // 'w'
|
||||
120 => 1, // 'x'
|
||||
121 => 1, // 'y'
|
||||
122 => 1, // 'z'
|
||||
123 => 4, // '{'
|
||||
124 => 3, // '|'
|
||||
125 => 4, // '}'
|
||||
126 => 3, // '~'
|
||||
127 => 3, // '^?' (DEL)
|
||||
];
|
||||
|
||||
/**
|
||||
* Latch map for changing character encoding mode.
|
||||
* Numbers represent: [number of bits to change, latch code value].
|
||||
*
|
||||
* @var array<int, array<int, array<array{int, int}>>>
|
||||
*/
|
||||
public const LATCH_MAP = [
|
||||
// MODE_UPPER
|
||||
0 => [
|
||||
1 => [[5, 28]], // -> LOWER
|
||||
2 => [[5, 30]], // -> DIGIT
|
||||
3 => [[5, 29]], // -> MIXED
|
||||
4 => [[5, 29], [5, 30]], // -> MIXED -> PUNCT
|
||||
],
|
||||
// MODE_LOWER
|
||||
1 => [
|
||||
0 => [[5, 30], [4, 14]], // -> DIGIT -> UPPER
|
||||
2 => [[5, 30]], // -> DIGIT
|
||||
3 => [[5, 29]], // -> MIXED
|
||||
4 => [[5, 29], [5, 30]], // -> MIXED -> PUNCT
|
||||
],
|
||||
// MODE_DIGIT
|
||||
2 => [
|
||||
0 => [[4, 14]], // -> UPPER
|
||||
1 => [[4, 14], [5, 28]], // -> UPPER -> LOWER
|
||||
3 => [[4, 14], [5, 29]], // -> UPPER -> MIXED
|
||||
4 => [[4, 14], [5, 29], [5, 30]], // -> UPPER -> MIXED -> PUNCT
|
||||
],
|
||||
// MODE_MIXED
|
||||
3 => [
|
||||
0 => [[5, 29]], // -> UPPER
|
||||
1 => [[5, 28]], // -> LOWER
|
||||
2 => [[5, 29], [5, 30]], // -> UPPER -> DIGIT
|
||||
4 => [[5, 30]], // -> PUNCT
|
||||
],
|
||||
// MODE_PUNCT
|
||||
4 => [
|
||||
0 => [[5, 31]], // -> UPPER
|
||||
1 => [[5, 31], [5, 28]], // -> UPPER -> LOWER
|
||||
2 => [[5, 31], [5, 30]], // -> UPPER -> DIGIT
|
||||
3 => [[5, 31], [5, 29]], // -> UPPER -> MIXED
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Shift map for changing character encoding mode.
|
||||
* Numbers represent: [number of bits to change, shift code value].
|
||||
*
|
||||
* @var array<int, array<int, array<array{int, int}>>>
|
||||
*/
|
||||
public const SHIFT_MAP = [
|
||||
// MODE_UPPER
|
||||
0 => [
|
||||
1 => [],
|
||||
2 => [],
|
||||
3 => [],
|
||||
4 => [[5, 0]], // -> PUNCT
|
||||
5 => [[5, 31]], // -> BINARY
|
||||
],
|
||||
// MODE_LOWER
|
||||
1 => [
|
||||
0 => [[5, 28]], // -> UPPER
|
||||
2 => [],
|
||||
3 => [],
|
||||
4 => [[5, 0]], // -> PUNCT
|
||||
5 => [[5, 31]], // -> BINARY
|
||||
],
|
||||
// MODE_DIGIT
|
||||
2 => [
|
||||
0 => [[4, 15]], // -> UPPER
|
||||
1 => [],
|
||||
3 => [],
|
||||
4 => [[4, 0]], // -> PUNCT
|
||||
5 => [[4, 14], [5, 31]], // -> LATCH UPPER -> BINARY
|
||||
],
|
||||
// MODE_MIXED
|
||||
3 => [
|
||||
0 => [],
|
||||
1 => [],
|
||||
2 => [],
|
||||
4 => [[5, 0]], // -> PUNCT
|
||||
5 => [[5, 31]], // -> BINARY
|
||||
],
|
||||
// MODE_PUNCT
|
||||
4 => [
|
||||
0 => [],
|
||||
1 => [],
|
||||
2 => [],
|
||||
3 => [],
|
||||
5 => [[5, 31], [5, 31]], // -> LATCH UPPER -> BINARY
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Extended Channel Interpretation (ECI) codes.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
public const ECI = [
|
||||
0 => 'FNC1', // Function 1 character
|
||||
2 => 'Cp437', // Code page 437
|
||||
3 => 'ISO-8859-1', // ISO/IEC 8859-1 - Latin-1 (Default encoding)
|
||||
4 => 'ISO-8859-2', // ISO/IEC 8859-2 - Latin-2
|
||||
5 => 'ISO-8859-3', // ISO/IEC 8859-3 - Latin-3
|
||||
6 => 'ISO-8859-4', // ISO/IEC 8859-4 - Latin-4
|
||||
7 => 'ISO-8859-5', // ISO/IEC 8859-5 - Latin/Cyrillic
|
||||
8 => 'ISO-8859-6', // ISO/IEC 8859-6 - Latin/Arabic
|
||||
9 => 'ISO-8859-7', // ISO/IEC 8859-7 - Latin/Greek
|
||||
10 => 'ISO-8859-8', // ISO/IEC 8859-8 - Latin/Hebrew
|
||||
11 => 'ISO-8859-9', // ISO/IEC 8859-9 - Latin-5
|
||||
12 => 'ISO-8859-10', // ISO/IEC 8859-10 - Latin-6
|
||||
13 => 'ISO-8859-11', // ISO/IEC 8859-11 - Latin/Thai
|
||||
15 => 'ISO-8859-13', // ISO/IEC 8859-13 - Latin-7
|
||||
16 => 'ISO-8859-14', // ISO/IEC 8859-14 - Latin-8 (Celtic)
|
||||
17 => 'ISO-8859-15', // ISO/IEC 8859-15 - Latin-9
|
||||
18 => 'ISO-8859-16', // ISO/IEC 8859-16 - Latin-10
|
||||
20 => 'Shift JIS',
|
||||
21 => 'Cp1250', // Windows-1250 - Superset of Latin-2
|
||||
22 => 'Cp1251', // Windows-1251 - Latin/Cyrillic
|
||||
23 => 'Cp1252', // Windows-1252 - Superset of Latin-1
|
||||
24 => 'Cp1256', // Windows-1256 - Arabic
|
||||
25 => 'UTF-16BE', // UnicodeBig, UnicodeBigUnmarked
|
||||
26 => 'UTF-8',
|
||||
27 => 'US-ASCII',
|
||||
28 => 'Big5',
|
||||
29 => 'GB18030', // GB2312, EUC_CN, GBK
|
||||
30 => 'EUC-KR',
|
||||
];
|
||||
|
||||
/**
|
||||
* Size and capacities of Aztec Compact Code symbols by number of layers.
|
||||
* The array entries are:
|
||||
* - 0: symbol x size;
|
||||
* - 1: codeword count;
|
||||
* - 2: codeword size;
|
||||
* - 3: symbol bit capacity;
|
||||
* - 4: symbol data digits capacity;
|
||||
* - 5: symbol data text capacity;
|
||||
* - 6: symbol data bytes capacity.
|
||||
*
|
||||
* @var array<int, array{int, int, int, int, int, int, int}>
|
||||
*/
|
||||
public const SIZE_COMPACT = [
|
||||
1 => [15, 17, 6, 102, 13, 12, 6],
|
||||
2 => [19, 40, 6, 240, 40, 33, 19],
|
||||
3 => [23, 51, 8, 408, 70, 57, 33],
|
||||
4 => [27, 76, 8, 608, 110, 89, 53],
|
||||
];
|
||||
|
||||
/**
|
||||
* Size and capacities of Aztec Full-range Code symbols by number of layers.
|
||||
* The array entries are:
|
||||
* - 0: symbol x size;
|
||||
* - 1: codeword count;
|
||||
* - 2: codeword size;
|
||||
* - 3: symbol bit capacity;
|
||||
* - 4: symbol data digits capacity;
|
||||
* - 5: symbol data text capacity;
|
||||
* - 6: symbol data bytes capacity.
|
||||
*
|
||||
* @var array<int, array{int, int, int, int, int, int, int}>
|
||||
*/
|
||||
public const SIZE_FULL = [
|
||||
1 => [19, 21, 6, 126, 18, 15, 8],
|
||||
2 => [23, 48, 6, 288, 49, 40, 24],
|
||||
3 => [27, 60, 8, 480, 84, 68, 40],
|
||||
4 => [31, 88, 8, 704, 128, 104, 62],
|
||||
5 => [37, 120, 8, 960, 178, 144, 87],
|
||||
6 => [41, 156, 8, 1248, 232, 187, 114],
|
||||
7 => [45, 196, 8, 1568, 294, 236, 145],
|
||||
8 => [49, 240, 8, 1920, 362, 291, 179],
|
||||
9 => [53, 230, 10, 2300, 433, 348, 214],
|
||||
10 => [57, 272, 10, 2720, 516, 414, 256],
|
||||
11 => [61, 316, 10, 3160, 601, 482, 298],
|
||||
12 => [67, 364, 10, 3640, 691, 554, 343],
|
||||
13 => [71, 416, 10, 4160, 793, 636, 394],
|
||||
14 => [75, 470, 10, 4700, 896, 718, 446],
|
||||
15 => [79, 528, 10, 5280, 1008, 808, 502],
|
||||
16 => [83, 588, 10, 5880, 1123, 900, 559],
|
||||
17 => [87, 652, 10, 6520, 1246, 998, 621],
|
||||
18 => [91, 720, 10, 7200, 1378, 1104, 687],
|
||||
19 => [95, 790, 10, 7900, 1511, 1210, 753],
|
||||
20 => [101, 864, 10, 8640, 1653, 1324, 824],
|
||||
21 => [105, 940, 10, 9400, 1801, 1442, 898],
|
||||
22 => [109, 1020, 10, 10_200, 1956, 1566, 976],
|
||||
23 => [113, 920, 12, 11_040, 2116, 1694, 1056],
|
||||
24 => [117, 992, 12, 11_904, 2281, 1826, 1138],
|
||||
25 => [121, 1066, 12, 12_792, 2452, 1963, 1224],
|
||||
26 => [125, 1144, 12, 13_728, 2632, 2107, 1314],
|
||||
27 => [131, 1224, 12, 14_688, 2818, 2256, 1407],
|
||||
28 => [135, 1306, 12, 15_672, 3007, 2407, 1501],
|
||||
29 => [139, 1392, 12, 16_704, 3205, 2565, 1600],
|
||||
30 => [143, 1480, 12, 17_760, 3409, 2728, 1702],
|
||||
31 => [147, 1570, 12, 18_840, 3616, 2894, 1806],
|
||||
32 => [151, 1664, 12, 19_968, 3832, 3067, 1914],
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,360 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Encode.php
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Aztec;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Aztec\Encode
|
||||
*
|
||||
* Encode for Aztec Barcode type class
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Encode extends \Com\Tecnick\Barcode\Type\Square\Aztec\Bitstream
|
||||
{
|
||||
/**
|
||||
* Bidimensional grid containing the encoded data.
|
||||
*
|
||||
* @var array<int, array<int>>
|
||||
*/
|
||||
protected array $grid = [];
|
||||
|
||||
/**
|
||||
* Coordinate of the grid center.
|
||||
*/
|
||||
protected int $gridcenter = 0;
|
||||
|
||||
/**
|
||||
* Aztec main encoder.
|
||||
*
|
||||
* @param string $code The code to encode.
|
||||
* @param int $ecc The error correction code percentage of error check words.
|
||||
* @param int $eci The ECI mode to use.
|
||||
* @param string $hint The mode to use.
|
||||
* @param string $mode The mode to use (A = Automatic; F = Full Range mode).
|
||||
*
|
||||
* @throws BarcodeException
|
||||
*/
|
||||
public function __construct(string $code, int $ecc = 33, int $eci = 0, string $hint = 'A', string $mode = 'A')
|
||||
{
|
||||
$this->highLevelEncoding($code, $eci, $hint);
|
||||
if (!$this->sizeAndBitStuffing($ecc, $mode)) {
|
||||
throw new BarcodeException('Data too long');
|
||||
}
|
||||
|
||||
$wsize = $this->layer[2];
|
||||
$nbits = $this->layer[3];
|
||||
$numcdw = $this->addCheckWords($this->bitstream, $this->totbits, $nbits, $wsize);
|
||||
$this->setGrid();
|
||||
$this->drawMode($numcdw);
|
||||
$this->drawData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bidimensional grid containing the encoded data.
|
||||
*
|
||||
* @return array<int, array<int>>
|
||||
*/
|
||||
public function getGrid(): array
|
||||
{
|
||||
return $this->grid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Check Codewords array for the given data words.
|
||||
*
|
||||
* @param array<int> $bitstream Array of bits.
|
||||
* @param int $totbits Number of bits in the bitstream.
|
||||
* @param int $nbits Number of bits per layer.
|
||||
* @param int $wsize Word size.
|
||||
*
|
||||
* @return int The number of data codewords.
|
||||
*/
|
||||
protected function addCheckWords(array &$bitstream, int &$totbits, int $nbits, int $wsize): int
|
||||
{
|
||||
$cdw = $this->bitstreamToWords($bitstream, $totbits, $wsize);
|
||||
$numcdw = \count($cdw);
|
||||
$totwords = (int) ($nbits / $wsize);
|
||||
$eccwords = $totwords - $numcdw;
|
||||
$errorCorrection = new ErrorCorrection($wsize);
|
||||
$checkwords = $errorCorrection->checkwords($cdw, $eccwords);
|
||||
// append check codewords
|
||||
foreach ($checkwords as $checkword) {
|
||||
$this->appendWordToBitstream($bitstream, $totbits, $wsize, $checkword);
|
||||
}
|
||||
|
||||
return $numcdw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the grid with all patterns.
|
||||
*/
|
||||
protected function setGrid(): void
|
||||
{
|
||||
// initialize grid
|
||||
$size = $this->layer[0];
|
||||
$size = \max(0, $size);
|
||||
$row = \array_fill(0, $size, 0);
|
||||
$this->grid = \array_fill(0, $size, $row);
|
||||
// draw center
|
||||
$center = (int) (($size - 1) / 2);
|
||||
$this->gridcenter = $center;
|
||||
$this->grid[$center][$center] = 1;
|
||||
// draw finder pattern (bulls-eye)
|
||||
$bewidth = $this->compact ? 11 : 15;
|
||||
$bemid = (int) (($bewidth - 1) / 2);
|
||||
for ($rng = 2; $rng < $bemid; $rng += 2) {
|
||||
// center cross points
|
||||
$this->grid[$center + $rng][$center] = 1;
|
||||
$this->grid[$center - $rng][$center] = 1;
|
||||
$this->grid[$center][$center + $rng] = 1;
|
||||
$this->grid[$center][$center - $rng] = 1;
|
||||
// corner points
|
||||
$this->grid[$center + $rng][$center + $rng] = 1;
|
||||
$this->grid[$center + $rng][$center - $rng] = 1;
|
||||
$this->grid[$center - $rng][$center + $rng] = 1;
|
||||
$this->grid[$center - $rng][$center - $rng] = 1;
|
||||
for ($pos = 1; $pos < $rng; ++$pos) {
|
||||
// horizontal points
|
||||
$this->grid[$center + $rng][$center + $pos] = 1;
|
||||
$this->grid[$center + $rng][$center - $pos] = 1;
|
||||
$this->grid[$center - $rng][$center + $pos] = 1;
|
||||
$this->grid[$center - $rng][$center - $pos] = 1;
|
||||
// vertical points
|
||||
$this->grid[$center + $pos][$center + $rng] = 1;
|
||||
$this->grid[$center + $pos][$center - $rng] = 1;
|
||||
$this->grid[$center - $pos][$center + $rng] = 1;
|
||||
$this->grid[$center - $pos][$center - $rng] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// draw orientation patterns
|
||||
$this->grid[$center - $bemid][$center - $bemid] = 1; // TL
|
||||
$this->grid[$center - $bemid][$center - $bemid + 1] = 1; // TL-R
|
||||
$this->grid[$center - $bemid + 1][$center - $bemid] = 1; // TL-B
|
||||
$this->grid[$center - $bemid][$center + $bemid] = 1; // TR-T
|
||||
$this->grid[$center - $bemid + 1][$center + $bemid] = 1; // TR-B
|
||||
$this->grid[$center + $bemid - 1][$center + $bemid] = 1; // BR
|
||||
if ($this->compact) {
|
||||
return;
|
||||
}
|
||||
|
||||
// draw reference grid for full mode
|
||||
$halfsize = (int) (($size - 1) / 2);
|
||||
// central cross
|
||||
for ($pos = 8; $pos <= $halfsize; $pos += 2) {
|
||||
// horizontal
|
||||
$this->grid[$center][$center - $pos] = 1;
|
||||
$this->grid[$center][$center + $pos] = 1;
|
||||
// vertical
|
||||
$this->grid[$center - $pos][$center] = 1;
|
||||
$this->grid[$center + $pos][$center] = 1;
|
||||
}
|
||||
|
||||
// grid lines
|
||||
for ($pos = 2; $pos <= $halfsize; $pos += 2) {
|
||||
for ($ref = 16; $ref <= $halfsize; $ref += 16) {
|
||||
// horizontal
|
||||
$this->grid[$center - $ref][$center - $pos] = 1;
|
||||
$this->grid[$center - $ref][$center + $pos] = 1;
|
||||
$this->grid[$center + $ref][$center - $pos] = 1;
|
||||
$this->grid[$center + $ref][$center + $pos] = 1;
|
||||
// vertical
|
||||
$this->grid[$center - $pos][$center - $ref] = 1;
|
||||
$this->grid[$center - $pos][$center + $ref] = 1;
|
||||
$this->grid[$center + $pos][$center - $ref] = 1;
|
||||
$this->grid[$center + $pos][$center + $ref] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the mode message to the grid.
|
||||
*
|
||||
* @param int $numcdw Number of data codewords.
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
|
||||
* @SuppressWarnings("PHPMD.NPathComplexity")
|
||||
*/
|
||||
protected function drawMode(int $numcdw): void
|
||||
{
|
||||
$modebs = [];
|
||||
$nbits = 0;
|
||||
$center = $this->gridcenter;
|
||||
$modebits = 40;
|
||||
$layersbits = 5;
|
||||
$codewordsbits = 11;
|
||||
$sidelen = 10;
|
||||
$srow = -7;
|
||||
$scol = -5;
|
||||
if ($this->compact) {
|
||||
$modebits = 28;
|
||||
$layersbits = 2;
|
||||
$codewordsbits = 6;
|
||||
$sidelen = 7;
|
||||
$srow = -5;
|
||||
$scol = -3;
|
||||
}
|
||||
|
||||
$this->appendWordToBitstream($modebs, $nbits, $layersbits, $this->numlayers - 1);
|
||||
$this->appendWordToBitstream($modebs, $nbits, $codewordsbits, $numcdw - 1);
|
||||
$this->addCheckWords($modebs, $nbits, $modebits, 4);
|
||||
// draw the mode message in the grid clockwise starting from the top left corner
|
||||
$bit = 0;
|
||||
// top
|
||||
$ypos = $center + $srow;
|
||||
$xpos = $center + $scol;
|
||||
for ($pos = 0; $pos < $sidelen; ++$pos) {
|
||||
$xpos += $this->skipModeRefGrid($pos);
|
||||
$this->grid[$ypos][$xpos] = ($modebs[$bit++] ?? 0) === 0 ? 0 : 1;
|
||||
++$xpos;
|
||||
}
|
||||
|
||||
// right
|
||||
$ypos += 2;
|
||||
++$xpos;
|
||||
for ($pos = 0; $pos < $sidelen; ++$pos) {
|
||||
$ypos += $this->skipModeRefGrid($pos);
|
||||
$this->grid[$ypos][$xpos] = ($modebs[$bit++] ?? 0) === 0 ? 0 : 1;
|
||||
++$ypos;
|
||||
}
|
||||
|
||||
// bottom
|
||||
++$ypos;
|
||||
$xpos -= 2;
|
||||
for ($pos = 0; $pos < $sidelen; ++$pos) {
|
||||
$xpos -= $this->skipModeRefGrid($pos);
|
||||
$this->grid[$ypos][$xpos] = ($modebs[$bit++] ?? 0) === 0 ? 0 : 1;
|
||||
--$xpos;
|
||||
}
|
||||
|
||||
// left
|
||||
$ypos -= 2;
|
||||
--$xpos;
|
||||
for ($pos = 0; $pos < $sidelen; ++$pos) {
|
||||
$ypos -= $this->skipModeRefGrid($pos);
|
||||
$this->grid[$ypos][$xpos] = ($modebs[$bit++] ?? 0) === 0 ? 0 : 1;
|
||||
--$ypos;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a bit from the end of the bitstream and update the index.
|
||||
*
|
||||
* @param int $bit Index of the bit to pop.
|
||||
*/
|
||||
protected function popBit(int &$bit): int
|
||||
{
|
||||
return ($this->bitstream[$bit--] ?? 0) === 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1 if the current position must be skipped in Full mode.
|
||||
*
|
||||
* @param int $pos Position in the grid.
|
||||
*/
|
||||
protected function skipModeRefGrid(int $pos): int
|
||||
{
|
||||
return (int) (!$this->compact && $pos === 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the offset for the specified position to skip the reference grid.
|
||||
*
|
||||
* @param int $pos Position in the grid.
|
||||
*/
|
||||
protected function skipRefGrid(int $pos): int
|
||||
{
|
||||
return (int) (!$this->compact && ($pos % 16) === 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the data bitstream in the grid in Full mode.
|
||||
*/
|
||||
protected function drawData(): void
|
||||
{
|
||||
$center = $this->gridcenter;
|
||||
$llen = 16; // width of the first layer side
|
||||
$srow = -8; // start top row offset from the center (LSB)
|
||||
$scol = -7; // start top column offset from the center (LSB)
|
||||
if ($this->compact) {
|
||||
$llen = 13;
|
||||
$srow = -6;
|
||||
$scol = -5;
|
||||
}
|
||||
|
||||
$skip = 0; // skip reference grid while drawing dominoes
|
||||
$bit = $this->totbits - 1; // index of last bitstream bit (first to draw)
|
||||
for ($layer = 0; $layer < $this->numlayers; ++$layer) {
|
||||
// top
|
||||
$ypos = $center + $srow;
|
||||
$xpos = $center + $scol;
|
||||
for ($pos = 0; $pos < $llen; ++$pos) {
|
||||
$xpos += $this->skipRefGrid($xpos - $center); // skip reference grid
|
||||
$this->grid[$ypos][$xpos] = $this->popBit($bit);
|
||||
$this->grid[$ypos - 1 - $skip][$xpos] = $this->popBit($bit);
|
||||
++$xpos;
|
||||
}
|
||||
|
||||
// right
|
||||
++$ypos;
|
||||
$xpos -= 2 + $skip;
|
||||
for ($pos = 0; $pos < $llen; ++$pos) {
|
||||
$ypos += $this->skipRefGrid($ypos - $center); // skip reference grid
|
||||
$this->grid[$ypos][$xpos] = $this->popBit($bit);
|
||||
$this->grid[$ypos][$xpos + 1 + $skip] = $this->popBit($bit);
|
||||
++$ypos;
|
||||
}
|
||||
|
||||
// bottom
|
||||
$ypos -= 2 + $skip;
|
||||
--$xpos;
|
||||
for ($pos = 0; $pos < $llen; ++$pos) {
|
||||
$xpos -= $this->skipRefGrid($xpos - $center); // skip reference grid
|
||||
$this->grid[$ypos][$xpos] = $this->popBit($bit);
|
||||
$this->grid[$ypos + 1 + $skip][$xpos] = $this->popBit($bit);
|
||||
--$xpos;
|
||||
}
|
||||
|
||||
// left
|
||||
--$ypos;
|
||||
$xpos += 2 + $skip;
|
||||
for ($pos = 0; $pos < $llen; ++$pos) {
|
||||
$ypos -= $this->skipRefGrid($ypos - $center); // skip reference grid
|
||||
$this->grid[$ypos][$xpos] = $this->popBit($bit);
|
||||
$this->grid[$ypos][$xpos - 1 - $skip] = $this->popBit($bit);
|
||||
--$ypos;
|
||||
}
|
||||
|
||||
$llen += 4;
|
||||
$srow = $ypos - $center;
|
||||
$srow -= $this->skipRefGrid($srow);
|
||||
$scol = $xpos - 1 - $center;
|
||||
$scol -= $this->skipRefGrid($scol);
|
||||
$skip = $this->skipRefGrid($srow - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* ErrorCorrection.php
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Aztec;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Aztec\ErrorCorrection
|
||||
*
|
||||
* ErrorCorrection for Aztec Barcode type class
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class ErrorCorrection
|
||||
{
|
||||
/**
|
||||
* Galois Field primitive by word size.
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
protected const GF = [
|
||||
4 => 19, // 10011 GF(16) (x^4 + x + 1) Mode message
|
||||
6 => 67, // 1000011 GF(64) (x^6 + x + 1) 01–02 layers
|
||||
8 => 301, // 100101101 GF(256) (x^8 + x^5 + x^3 + x^2 + 1) 03–08 layers
|
||||
10 => 1033, // 10000001001 GF(1024) (x^10 + x^3 + 1) 09–22 layers
|
||||
12 => 4201, // 1000001101001 GF(4096) (x^12 + x^6 + x^5 + x^3 + 1) 23–32 layers
|
||||
];
|
||||
|
||||
/**
|
||||
* Map the log and exp (inverse log) tables by word size.
|
||||
* NOTE: It is equal to 2^word_size.
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
protected const TSIZE = [
|
||||
4 => 16,
|
||||
6 => 64,
|
||||
8 => 256,
|
||||
10 => 1024,
|
||||
12 => 4096,
|
||||
];
|
||||
|
||||
/**
|
||||
* Log table.
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
protected array $tlog = [];
|
||||
|
||||
/**
|
||||
* Exponential (inverse log) table.
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
protected array $texp = [];
|
||||
|
||||
/**
|
||||
* Size of the log and exp tables.
|
||||
*/
|
||||
protected int $tsize = 0;
|
||||
|
||||
/**
|
||||
* Initialize the Reed-Solomon Error Correction.
|
||||
*
|
||||
* @param int $wsize Size of a word in bits.
|
||||
*/
|
||||
public function __construct(int $wsize)
|
||||
{
|
||||
$this->genTables($wsize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Reed-Solomon Error Correction Codewords added to the input data.
|
||||
*
|
||||
* @param array<int> $data Array of data codewords to process.
|
||||
* @param int $necc Number of error correction bytes.
|
||||
*
|
||||
* @return array<int>
|
||||
*/
|
||||
public function checkwords(array $data, int $necc): array
|
||||
{
|
||||
$coeff = $this->getCoefficients($data, $necc);
|
||||
return \array_pad($coeff, -$necc, 0); // @phpstan-ignore return.type
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates log and exp (inverse log) tables.
|
||||
*
|
||||
* @param int $wsize Size of the word in bits.
|
||||
*/
|
||||
protected function genTables(int $wsize): void
|
||||
{
|
||||
$this->tsize = self::TSIZE[$wsize] ?? 0;
|
||||
$this->tlog = \array_fill(0, \max(0, $this->tsize), 0);
|
||||
$this->texp = $this->tlog;
|
||||
$primitive = self::GF[$wsize] ?? 0;
|
||||
$val = 1;
|
||||
$sizeminusone = $this->tsize - 1;
|
||||
for ($idx = 0; $idx < $this->tsize; ++$idx) {
|
||||
$this->texp[$idx] = $val;
|
||||
$val <<= 1; // multiply by 2
|
||||
if ($val >= $this->tsize) {
|
||||
$val ^= $primitive;
|
||||
$val &= $sizeminusone;
|
||||
}
|
||||
}
|
||||
|
||||
for ($idx = 0; $idx < ($this->tsize - 1); ++$idx) {
|
||||
$exp = $this->texp[$idx] ?? 0;
|
||||
$this->tlog[$exp] = $idx;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the coefficients of the error correction polynomial.
|
||||
*
|
||||
* @param array<int> $data Array of data codewords to process.
|
||||
* @param int $necc Number of error correction bytes.
|
||||
*
|
||||
* @return array<int> Array of coefficients.
|
||||
*/
|
||||
protected function getCoefficients(array $data, int $necc): array
|
||||
{
|
||||
$gen = [1];
|
||||
for ($idx = 1; $idx <= $necc; ++$idx) {
|
||||
$gen = $this->multiplyCoeff([1, $this->texp[$idx] ?? 0], $gen);
|
||||
}
|
||||
|
||||
$deg = $necc + 1;
|
||||
$coeff = $this->multiplyByMonomial($data, 1, $necc);
|
||||
$len = \count($coeff);
|
||||
while ($len >= $deg && ($coeff[0] ?? 0) !== 0) {
|
||||
$scale = $this->multiply($coeff[0] ?? 0, 1);
|
||||
$largercoeffs = $this->multiplyByMonomial($gen, $scale, $len - $deg);
|
||||
$coeff = $this->addOrSubtract($coeff, $largercoeffs);
|
||||
$len = \count($coeff);
|
||||
}
|
||||
|
||||
return $coeff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the product of two coefficient arrays.
|
||||
*
|
||||
* @param array<int> $acf First array of coefficients.
|
||||
* @param array<int> $bcf Second array of coefficients.
|
||||
*
|
||||
* @return array<int> Array of coefficients.
|
||||
*/
|
||||
protected function multiplyCoeff(array $acf, array $bcf): array
|
||||
{
|
||||
$alen = \count($acf);
|
||||
$blen = \count($bcf);
|
||||
$coeff = \array_fill(0, \max(0, $alen + $blen - 1), 0);
|
||||
for ($aid = 0; $aid < $alen; ++$aid) {
|
||||
for ($bid = 0; $bid < $blen; ++$bid) {
|
||||
$coeff[$aid + $bid] = ($coeff[$aid + $bid] ?? 0) ^ $this->multiply($acf[$aid] ?? 0, $bcf[$bid] ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->trimCoefficients($coeff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the product of $aval and $bval in GF(size).
|
||||
*
|
||||
* @param int $aval First value.
|
||||
* @param int $bval Second value.
|
||||
*/
|
||||
protected function multiply(int $aval, int $bval): int
|
||||
{
|
||||
if ($aval === 0 || $bval === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$sizeMinusOne = $this->tsize - 1;
|
||||
if ($sizeMinusOne <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$index = (($this->tlog[$aval] ?? 0) + ($this->tlog[$bval] ?? 0)) % $sizeMinusOne;
|
||||
|
||||
return $this->texp[$index] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Left-trim coefficients array.
|
||||
*
|
||||
* @param array<int> $coeff Array of coefficients.
|
||||
*
|
||||
* @return array<int> Array of coefficients.
|
||||
*/
|
||||
protected function trimCoefficients(array $coeff): array
|
||||
{
|
||||
while ($coeff !== [] && ($coeff[0] ?? 0) === 0) {
|
||||
\array_shift($coeff);
|
||||
}
|
||||
|
||||
return $coeff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the product of a polynomial by a monomial.
|
||||
*
|
||||
* @param array<int> $coeff Array of polynomial coefficients.
|
||||
* @param int $mon Monomial.
|
||||
* @param int $deg Degree of the monomial.
|
||||
*
|
||||
* @return array<int> Array of coefficients.
|
||||
*/
|
||||
protected function multiplyByMonomial(array $coeff, int $mon, int $deg): array
|
||||
{
|
||||
// if ($mon == 0) {
|
||||
// return array(0);
|
||||
// }
|
||||
$ncf = \count($coeff);
|
||||
$prod = \array_fill(0, \max(0, $ncf + $deg), 0);
|
||||
for ($idx = 0; $idx < $ncf; ++$idx) {
|
||||
$prod[$idx] = $this->multiply($coeff[$idx] ?? 0, $mon);
|
||||
}
|
||||
|
||||
return $this->trimCoefficients($prod);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds or subtracts two coefficient arrays.
|
||||
*
|
||||
* @param array<int> $smaller The smaller array of coefficients.
|
||||
* @param array<int> $larger The larger array of coefficients.
|
||||
*
|
||||
* @return array<int> Array of coefficients.
|
||||
*/
|
||||
protected function addOrSubtract(array $smaller, array $larger): array
|
||||
{
|
||||
// if ($smaller[0] == 0) {
|
||||
// return $larger;
|
||||
// }
|
||||
// if ($larger[0] == 0) {
|
||||
// return $smaller;
|
||||
// }
|
||||
$slen = \count($smaller);
|
||||
$llen = \count($larger);
|
||||
// if ($slen > $llen) {
|
||||
// // swap arrays
|
||||
// list($smaller, $larger) = array($larger, $smaller);
|
||||
// list($slen, $llen) = array($llen, $slen);
|
||||
// }
|
||||
$lendiff = $llen - $slen;
|
||||
$coeff = \array_slice($larger, 0, $lendiff);
|
||||
for ($idx = $lendiff; $idx < $llen; ++$idx) {
|
||||
$coeff[$idx] = ($smaller[$idx - $lendiff] ?? 0) ^ ($larger[$idx] ?? 0);
|
||||
}
|
||||
|
||||
return $this->trimCoefficients($coeff);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Layers.php
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Aztec;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Aztec\Layers
|
||||
*
|
||||
* Layers for Aztec Barcode type class
|
||||
*
|
||||
* @since 2023-10-13
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2023-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Layers extends \Com\Tecnick\Barcode\Type\Square\Aztec\Codeword
|
||||
{
|
||||
/**
|
||||
* @param array<int, array{0: int, 1: int, 2: int, 3: int, 4: int, 5: int, 6: int}> $data
|
||||
*/
|
||||
protected function getLayerMaxBits(array $data): int
|
||||
{
|
||||
if ($data === []) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$last = \array_values($data)[\count($data) - 1] ?? [0, 0, 0, 0, 0, 0, 0];
|
||||
return $last[3] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* True for compact mode (up to 4 layers), false for full-range mode (up to 32 layers).
|
||||
*/
|
||||
protected bool $compact = true;
|
||||
|
||||
/**
|
||||
* Number of data layers.
|
||||
*/
|
||||
protected int $numlayers = 0;
|
||||
|
||||
/**
|
||||
* Size data for the selected layer.
|
||||
*
|
||||
* @var array{int, int, int, int, int, int, int}
|
||||
*/
|
||||
protected array $layer = [0, 0, 0, 0, 0, 0, 0];
|
||||
|
||||
/**
|
||||
* Returns the minimum number of layers required.
|
||||
*
|
||||
* @param array<int, array{int, int, int, int, int, int, int}> $data
|
||||
* Either the Data::SIZE_COMPACT or Data::SIZE_FULL array.
|
||||
* @param int $numbits The number of bits to encode.
|
||||
*/
|
||||
protected function getMinLayers(array $data, int $numbits): int
|
||||
{
|
||||
if ($numbits <= $this->getLayerMaxBits($data)) {
|
||||
foreach ($data as $numlayers => $size) {
|
||||
if ($numbits <= $size[3]) {
|
||||
return $numlayers;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select the layer by the number of bits to encode.
|
||||
*
|
||||
* @param int $numbits The number of bits to encode.
|
||||
* @param string $mode The mode to use (A = Automatic; F = Full Range mode).
|
||||
*
|
||||
* @return bool Returns true if the size computation was successful, false otherwise.
|
||||
*/
|
||||
protected function setLayerByBits(int $numbits, string $mode = 'A'): bool
|
||||
{
|
||||
$this->numlayers = 0;
|
||||
if ($mode === 'A') {
|
||||
$this->compact = true;
|
||||
$this->numlayers = $this->getMinLayers(Data::SIZE_COMPACT, $numbits);
|
||||
}
|
||||
|
||||
if ($this->numlayers === 0) {
|
||||
$this->compact = false;
|
||||
$this->numlayers = $this->getMinLayers(Data::SIZE_FULL, $numbits);
|
||||
}
|
||||
|
||||
if ($this->numlayers === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->compact) {
|
||||
$compactLayer = Data::SIZE_COMPACT[$this->numlayers] ?? null;
|
||||
if ($compactLayer === null) {
|
||||
return false;
|
||||
}
|
||||
$this->layer = $compactLayer;
|
||||
return true;
|
||||
}
|
||||
|
||||
$fullLayer = Data::SIZE_FULL[$this->numlayers] ?? null;
|
||||
if ($fullLayer === null) {
|
||||
return false;
|
||||
}
|
||||
$this->layer = $fullLayer;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the type and number of required layers and performs bit stuffing
|
||||
*
|
||||
* @param int $ecc The error correction level.
|
||||
* @param string $mode The mode to use (A = Automatic; F = Full Range mode).
|
||||
*
|
||||
* @return bool Returns true if the size computation was successful, false otherwise.
|
||||
*/
|
||||
protected function sizeAndBitStuffing(int $ecc, string $mode = 'A'): bool
|
||||
{
|
||||
$nsbits = 0;
|
||||
$eccbits = 11 + (int) (($this->totbits * $ecc) / 100);
|
||||
do {
|
||||
if (!$this->setLayerByBits($this->totbits + $nsbits + $eccbits, $mode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$nsbits = $this->bitStuffing();
|
||||
} while (($nsbits + $eccbits) > $this->layer[3]);
|
||||
|
||||
$this->bitstream = [];
|
||||
$this->totbits = 0;
|
||||
$this->mergeTmpCwdRaw();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bit-stuffing the bitstream into Reed–Solomon codewords.
|
||||
* The resulting codewords are stored in the temporary tmpCdws array.
|
||||
*
|
||||
* @return int The number of bits in the bitstream after bit stuffing.
|
||||
*/
|
||||
protected function bitStuffing(): int
|
||||
{
|
||||
$nsbits = 0;
|
||||
$wsize = $this->layer[2];
|
||||
$mask = (1 << $wsize) - 2; // b-1 bits at 1 and last bit at 0
|
||||
$this->tmpCdws = [];
|
||||
for ($wid = 0; $wid < $this->totbits; $wid += $wsize) {
|
||||
$word = 0;
|
||||
for ($idx = 0; $idx < $wsize; ++$idx) {
|
||||
$bid = $wid + $idx;
|
||||
if ($this->getBitstreamBit($this->bitstream, $bid) === 1) {
|
||||
$word |= 1 << ($wsize - 1 - $idx); // the first bit is MSB
|
||||
}
|
||||
}
|
||||
|
||||
// If the first b−1 bits of a code word have the same value,
|
||||
// an extra bit with the complementary value is inserted into the data stream.
|
||||
$maskedWord = $word & $mask;
|
||||
[$word, $wid] = match (true) {
|
||||
$maskedWord === $mask => [$word & $mask, $wid - 1],
|
||||
$maskedWord === 0 => [$word | 1, $wid - 1],
|
||||
default => [$word, $wid],
|
||||
};
|
||||
|
||||
$this->tmpCdws[] = [$wsize, $word];
|
||||
$nsbits += $wsize;
|
||||
}
|
||||
|
||||
return $nsbits;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Datamatrix.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
use Com\Tecnick\Barcode\Type\Square\Datamatrix\Data;
|
||||
use Com\Tecnick\Barcode\Type\Square\Datamatrix\DatamatrixEncoding;
|
||||
use Com\Tecnick\Barcode\Type\Square\Datamatrix\DatamatrixShape;
|
||||
use Com\Tecnick\Barcode\Type\Square\Datamatrix\Encode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Datamatrix
|
||||
*
|
||||
* Datamatrix Barcode type class
|
||||
* DATAMATRIX (ISO/IEC 16022)
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Datamatrix extends \Com\Tecnick\Barcode\Type\Square
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'DATAMATRIX';
|
||||
|
||||
/**
|
||||
* Array of codewords.
|
||||
*
|
||||
* @var array<int, int>
|
||||
*/
|
||||
protected array $cdw = [];
|
||||
|
||||
/**
|
||||
* Binary grid
|
||||
*
|
||||
* @var array<int, array<int, int>>
|
||||
*/
|
||||
protected array $grid = [];
|
||||
|
||||
/**
|
||||
* Datamatrix Encoding object
|
||||
*/
|
||||
protected Encode $dmx;
|
||||
|
||||
/**
|
||||
* Datamatrix shape key (S=square, R=rectangular)
|
||||
*/
|
||||
protected string $shape = 'S';
|
||||
|
||||
/**
|
||||
* Datamatrix variant (N=default, GS1=FNC1 codeword in first place)
|
||||
*/
|
||||
protected bool $gsonemode = false;
|
||||
|
||||
/**
|
||||
* Datamatrix default encoding.
|
||||
* See Data::SWITCHCDW for valid values.
|
||||
*/
|
||||
protected int $defenc = Data::ENC_ASCII;
|
||||
|
||||
/**
|
||||
* Set extra (optional) parameters:
|
||||
* 1: SHAPE: S=square (default), R=rectangular.
|
||||
* 2: MODE: N=default, GS1 = the FNC1 codeword is added in the first position of Data Matrix ECC 200 version.
|
||||
* 3: ENCODING: ASCII (default), C40, TXT, X12, EDIFACT, BASE256.
|
||||
*/
|
||||
protected function setParameters(): void
|
||||
{
|
||||
parent::setParameters();
|
||||
|
||||
// shape
|
||||
$this->shape = DatamatrixShape::fromLoose(\strval($this->params[0] ?? ''))->value;
|
||||
|
||||
// mode
|
||||
$this->gsonemode = ($this->params[1] ?? null) === 'GS1';
|
||||
|
||||
// encoding
|
||||
if (($this->params[2] ?? null) !== null) {
|
||||
$this->defenc =
|
||||
Data::ENCOPTS[DatamatrixEncoding::fromLoose(\strval($this->params[2]))->value] ?? Data::ENC_ASCII;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add padding codewords
|
||||
*
|
||||
* @param int $size Max barcode size in codewords
|
||||
* @param int $ncw Number of codewords
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function addPadding(int $size, int $ncw): void
|
||||
{
|
||||
if ($size <= $ncw) {
|
||||
return;
|
||||
}
|
||||
|
||||
$lastEnc = (int) $this->dmx->last_enc;
|
||||
if ($lastEnc !== Data::ENC_ASCII && $lastEnc !== Data::ENC_BASE256) {
|
||||
// return to ASCII encodation before padding
|
||||
$this->cdw[] = $lastEnc === Data::ENC_EDF ? 124 : 254;
|
||||
|
||||
++$ncw;
|
||||
}
|
||||
|
||||
if ($size > $ncw) {
|
||||
// add first pad
|
||||
$this->cdw[] = 129;
|
||||
++$ncw;
|
||||
// add remaining pads
|
||||
for ($i = $ncw; $i < $size; ++$i) {
|
||||
$this->cdw[] = $this->dmx->get253StateCodeword(129, $i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the codewords
|
||||
*
|
||||
* @return array{int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int} params
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getCodewords(): array
|
||||
{
|
||||
if (\strlen($this->code) === 0) {
|
||||
throw new BarcodeException('Empty input');
|
||||
}
|
||||
|
||||
// get data codewords
|
||||
$this->cdw = $this->getHighLevelEncoding($this->code);
|
||||
|
||||
// number of data codewords
|
||||
$ncw = \count($this->cdw);
|
||||
|
||||
// check size
|
||||
if ($ncw > 1560) {
|
||||
throw new BarcodeException('the input is too large to fit the barcode');
|
||||
}
|
||||
|
||||
// get minimum required matrix size.
|
||||
$params = Data::getPaddingSize($this->shape, $ncw);
|
||||
$this->addPadding($params[11], $ncw);
|
||||
|
||||
$errorCorrection = new \Com\Tecnick\Barcode\Type\Square\Datamatrix\ErrorCorrection();
|
||||
$this->cdw = $errorCorrection->getErrorCorrection($this->cdw, $params[13], $params[14], $params[15]);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the grid
|
||||
*
|
||||
* @param int $idx Index
|
||||
* @param array<int, int> $places Places
|
||||
* @param int $row Row
|
||||
* @param int $col Column
|
||||
* @param int $rdx Region data row index
|
||||
* @param int $cdx Region data column index
|
||||
* @param int $rdri Region data row max index
|
||||
* @param int $rdci Region data column max index
|
||||
*/
|
||||
protected function setGrid(
|
||||
int &$idx,
|
||||
array &$places,
|
||||
int &$row,
|
||||
int &$col,
|
||||
int &$rdx,
|
||||
int &$cdx,
|
||||
int &$rdri,
|
||||
int &$rdci,
|
||||
): void {
|
||||
// draw bits by case
|
||||
if ($rdx === 0) {
|
||||
// top finder pattern
|
||||
$this->grid[$row][$col] = (int) (($cdx % 2) === 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($rdx === $rdri) {
|
||||
// bottom finder pattern
|
||||
$this->grid[$row][$col] = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if ($cdx === 0) {
|
||||
// left finder pattern
|
||||
$this->grid[$row][$col] = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if ($cdx === $rdci) {
|
||||
// right finder pattern
|
||||
$this->grid[$row][$col] = (int) (($rdx % 2) > 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// data bit
|
||||
$place = $places[$idx] ?? 0;
|
||||
if ($place < 2) {
|
||||
$this->grid[$row][$col] = $place;
|
||||
++$idx;
|
||||
return;
|
||||
}
|
||||
|
||||
// codeword ID
|
||||
$cdw_id = \floor($place / 10) - 1;
|
||||
// codeword BIT mask
|
||||
$cdw_bit = 2 ** (8 - ($place % 10));
|
||||
$cdw_val = $this->cdw[\intval($cdw_id)] ?? 0;
|
||||
$this->grid[$row][$col] = ($cdw_val & $cdw_bit) === 0 ? 0 : 1;
|
||||
|
||||
++$idx;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get high level encoding using the minimum symbol data characters for ECC 200
|
||||
*
|
||||
* @param string $data data to encode
|
||||
*
|
||||
* @return array<int, int> Codewords
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
|
||||
*/
|
||||
protected function getHighLevelEncoding(string $data): array
|
||||
{
|
||||
// STEP A. Start in predefined encodation.
|
||||
$enc = $this->defenc; // current encoding mode
|
||||
$this->dmx->last_enc = $enc; // last used encoding
|
||||
$pos = 0; // current position
|
||||
$cdw = []; // array of codewords to be returned
|
||||
$cdw_num = 0; // number of data codewords
|
||||
$data_length = \strlen($data); // number of chars
|
||||
$field_length = 0; // number of chars in current field
|
||||
|
||||
// Switch to predefined encoding (no action needed if ASCII because it's the default encoding)
|
||||
if ($this->defenc !== Data::ENC_ASCII) {
|
||||
$cdw[] = $this->dmx->getSwitchEncodingCodeword($this->defenc);
|
||||
++$cdw_num;
|
||||
}
|
||||
|
||||
while ($pos < $data_length) {
|
||||
if ($this->gsonemode) {
|
||||
// check for control characters
|
||||
$cco = \ord($data[$pos]);
|
||||
if (
|
||||
$cco === 232 // FNC1 (ASCII 232 - HEX \xE8)
|
||||
|| $cco === 29 // <GS> (ASCII 29 - HEX \x1D)
|
||||
) {
|
||||
$cdw[] = 232; // FNC1
|
||||
++$pos;
|
||||
++$cdw_num;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
switch ($enc) {
|
||||
case Data::ENC_ASCII:
|
||||
// STEP B. While in ASCII encodation
|
||||
$this->dmx->encodeASCII($cdw, $cdw_num, $pos, $data_length, $data, $enc);
|
||||
break;
|
||||
case Data::ENC_C40:
|
||||
// Upper-case alphanumeric
|
||||
case Data::ENC_TXT:
|
||||
// Lower-case alphanumeric
|
||||
case Data::ENC_X12:
|
||||
// ANSI X12
|
||||
$this->dmx->encodeTXT($cdw, $cdw_num, $pos, $data_length, $data, $enc);
|
||||
break;
|
||||
case Data::ENC_EDF:
|
||||
// F. While in EDIFACT (EDF) encodation
|
||||
$this->dmx->encodeEDF($cdw, $cdw_num, $pos, $data_length, $field_length, $data, $enc);
|
||||
break;
|
||||
case Data::ENC_BASE256:
|
||||
// G. While in Base 256 (B256) encodation
|
||||
$this->dmx->encodeBase256($cdw, $cdw_num, $pos, $data_length, $field_length, $data, $enc);
|
||||
break;
|
||||
}
|
||||
|
||||
$this->dmx->last_enc = $enc;
|
||||
}
|
||||
|
||||
return $cdw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bars array
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
$this->dmx = new Encode($this->shape);
|
||||
$params = $this->getCodewords();
|
||||
// get placement map
|
||||
$places = $this->dmx->getPlacementMap($params[2], $params[3]);
|
||||
// fill the grid with data
|
||||
$this->grid = [];
|
||||
$idx = 0;
|
||||
// region data row max index
|
||||
$rdri = $params[4] - 1;
|
||||
// region data column max index
|
||||
$rdci = $params[5] - 1;
|
||||
// for each horizontal region
|
||||
for ($hr = 0; $hr < $params[8]; ++$hr) {
|
||||
// for each row on region
|
||||
for ($rdx = 0; $rdx < $params[4]; ++$rdx) {
|
||||
$row = ($hr * $params[4]) + $rdx;
|
||||
// for each vertical region
|
||||
for ($vr = 0; $vr < $params[9]; ++$vr) {
|
||||
// for each column on region
|
||||
for ($cdx = 0; $cdx < $params[5]; ++$cdx) {
|
||||
$col = ($vr * $params[5]) + $cdx;
|
||||
$this->setGrid($idx, $places, $row, $col, $rdx, $cdx, $rdri, $rdci);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->processBinarySequence($this->grid);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* DatamatrixEncoding.php
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Datamatrix;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Datamatrix\DatamatrixEncoding
|
||||
*
|
||||
* Backed enum for the Data Matrix default encoding scheme. The backing value of
|
||||
* each case is a public key of Data::ENCOPTS (the internal encoding
|
||||
* states are intentionally not exposed here).
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
enum DatamatrixEncoding: string
|
||||
{
|
||||
case ASCII = 'ASCII';
|
||||
|
||||
case C40 = 'C40';
|
||||
|
||||
case TXT = 'TXT';
|
||||
|
||||
case X12 = 'X12';
|
||||
|
||||
case EDF = 'EDF';
|
||||
|
||||
case BASE256 = 'BASE256';
|
||||
|
||||
/**
|
||||
* Resolve a loose Data Matrix encoding value to the matching enum case.
|
||||
*
|
||||
* Accepts the canonical scheme name or an enum instance (returned
|
||||
* unchanged). Unknown values fall back to ASCII, matching the lenient
|
||||
* behavior of Datamatrix.
|
||||
*
|
||||
* @param string|self $value Encoding scheme name or enum case.
|
||||
*/
|
||||
public static function fromLoose(string|self $value): self
|
||||
{
|
||||
if ($value instanceof self) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return self::tryFrom($value) ?? self::ASCII;
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* DatamatrixShape.php
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Datamatrix;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Datamatrix\DatamatrixShape
|
||||
*
|
||||
* Backed enum for the Data Matrix symbol shape: S (square, default) or R
|
||||
* (rectangular).
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
enum DatamatrixShape: string
|
||||
{
|
||||
/** Square (default). */
|
||||
case Square = 'S';
|
||||
|
||||
/** Rectangular. */
|
||||
case Rectangular = 'R';
|
||||
|
||||
/**
|
||||
* Resolve a loose Data Matrix shape value to the matching enum case.
|
||||
*
|
||||
* Accepts the canonical letter or an enum instance (returned unchanged).
|
||||
* Unknown values fall back to Square, matching the lenient behavior of
|
||||
* Datamatrix.
|
||||
*
|
||||
* @param string|self $value Shape letter or enum case.
|
||||
*/
|
||||
public static function fromLoose(string|self $value): self
|
||||
{
|
||||
if ($value instanceof self) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return self::tryFrom($value) ?? self::Square;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Encode.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Datamatrix;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Datamatrix\Encode
|
||||
*
|
||||
* Encode methods for Datamatrix Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Encode extends \Com\Tecnick\Barcode\Type\Square\Datamatrix\EncodeTxt
|
||||
{
|
||||
/**
|
||||
* @param array<int, int> $temp_cw
|
||||
*/
|
||||
protected function getTempCodeword(array $temp_cw, int $index): int
|
||||
{
|
||||
return $temp_cw[$index] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a new encode object
|
||||
*
|
||||
* @param string $shape Datamatrix shape key (S=square, R=rectangular)
|
||||
*/
|
||||
public function __construct(string $shape = 'S')
|
||||
{
|
||||
$this->shape = $shape;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode ASCII
|
||||
*
|
||||
* @param array<int, int> $cdw Codewords array
|
||||
* @param int $cdw_num Codewords number
|
||||
* @param int $pos Current position
|
||||
* @param int $data_length Data length
|
||||
* @param string $data Data string
|
||||
* @param int $enc Current encoding
|
||||
*/
|
||||
public function encodeASCII(
|
||||
array &$cdw,
|
||||
int &$cdw_num,
|
||||
int &$pos,
|
||||
int &$data_length,
|
||||
string &$data,
|
||||
int &$enc,
|
||||
): void {
|
||||
if (
|
||||
$data_length > 1
|
||||
&& $pos < ($data_length - 1)
|
||||
&& (
|
||||
$this->isCharMode(\ord($data[$pos]), Data::ENC_ASCII_NUM)
|
||||
&& $this->isCharMode(\ord($data[$pos + 1]), Data::ENC_ASCII_NUM)
|
||||
)
|
||||
) {
|
||||
// 1. If the next data sequence is at least 2 consecutive digits,
|
||||
// encode the next two digits as a double digit in ASCII mode.
|
||||
$cdw[] = (int) \substr($data, $pos, 2) + 130;
|
||||
++$cdw_num;
|
||||
$pos += 2;
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
|
||||
$newenc = $this->lookAheadTest($data, $pos, $enc);
|
||||
if ($newenc !== $enc) {
|
||||
// switch to new encoding
|
||||
$enc = $newenc;
|
||||
$cdw[] = $this->getSwitchEncodingCodeword($enc);
|
||||
++$cdw_num;
|
||||
return;
|
||||
}
|
||||
|
||||
// get new byte
|
||||
$chr = \ord($data[$pos]);
|
||||
++$pos;
|
||||
if ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
|
||||
// 3. If the next data character is extended ASCII (greater than 127)
|
||||
// encode it in ASCII mode first using the Upper Shift (value 235) character.
|
||||
$cdw[] = 235;
|
||||
$cdw[] = $chr - 127;
|
||||
$cdw_num += 2;
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. Otherwise process the next data character in ASCII encodation.
|
||||
$cdw[] = $chr + 1;
|
||||
++$cdw_num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode EDF4
|
||||
*
|
||||
* @param int $epos Current position
|
||||
* @param array<int, int> $cdw Codewords array
|
||||
* @param int $cdw_num Codewords number
|
||||
* @param int $pos Current position
|
||||
* @param int $data_length Data length
|
||||
* @param int $field_length Field length
|
||||
* @param int $enc Current encoding
|
||||
* @param array<int, int> $temp_cw Temporary codewords array
|
||||
*
|
||||
* @return bool true to break the loop
|
||||
*
|
||||
* @throws \Com\Tecnick\Barcode\Exception in case of error
|
||||
*/
|
||||
public function encodeEDFfour(
|
||||
int $epos,
|
||||
array &$cdw,
|
||||
int &$cdw_num,
|
||||
int &$pos,
|
||||
int &$data_length,
|
||||
int &$field_length,
|
||||
int &$enc,
|
||||
array &$temp_cw,
|
||||
): bool {
|
||||
if ($epos === $data_length) {
|
||||
$enc = Data::ENC_ASCII;
|
||||
$params = Data::getPaddingSize($this->shape, $cdw_num + $field_length);
|
||||
if (($params[11] - $cdw_num) > 2) {
|
||||
$cdw[] = $this->getSwitchEncodingCodeword($enc);
|
||||
++$cdw_num;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($field_length < 4) {
|
||||
$enc = Data::ENC_ASCII;
|
||||
$this->last_enc = $enc;
|
||||
$params = Data::getPaddingSize($this->shape, $cdw_num + $field_length + ($data_length - $epos));
|
||||
if (($params[11] - $cdw_num) <= 2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// set unlatch character
|
||||
$temp_cw[] = 0x1f;
|
||||
++$field_length;
|
||||
// fill empty characters
|
||||
for ($i = $field_length; $i < 4; ++$i) {
|
||||
$temp_cw[] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// encodes four data characters in three codewords
|
||||
$cw0 = $this->getTempCodeword($temp_cw, 0);
|
||||
$cw1 = $this->getTempCodeword($temp_cw, 1);
|
||||
$cw2 = $this->getTempCodeword($temp_cw, 2);
|
||||
$cw3 = $this->getTempCodeword($temp_cw, 3);
|
||||
|
||||
$cdw[] = (($cw0 & 0x3F) << 2) + (($cw1 & 0x30) >> 4);
|
||||
++$cdw_num;
|
||||
if ($field_length > 1) {
|
||||
$cdw[] = (($cw1 & 0x0F) << 4) + (($cw2 & 0x3C) >> 2);
|
||||
++$cdw_num;
|
||||
}
|
||||
|
||||
if ($field_length > 2) {
|
||||
$cdw[] = (($cw2 & 0x03) << 6) + ($cw3 & 0x3F);
|
||||
++$cdw_num;
|
||||
}
|
||||
|
||||
$temp_cw = [];
|
||||
$pos = $epos;
|
||||
$field_length = 0;
|
||||
if ($enc === Data::ENC_ASCII) {
|
||||
return true; // exit from EDIFACT mode
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode EDF
|
||||
*
|
||||
* @param array<int, int> $cdw Codewords array
|
||||
* @param int $cdw_num Codewords number
|
||||
* @param int $pos Current position
|
||||
* @param int $data_length Data length
|
||||
* @param int $field_length Field length
|
||||
* @param string $data Data string
|
||||
* @param int $enc Current encoding
|
||||
*/
|
||||
public function encodeEDF(
|
||||
array &$cdw,
|
||||
int &$cdw_num,
|
||||
int &$pos,
|
||||
int &$data_length,
|
||||
int &$field_length,
|
||||
string &$data,
|
||||
int &$enc,
|
||||
): void {
|
||||
// initialize temporary array with 0 length
|
||||
$temp_cw = [];
|
||||
$epos = $pos;
|
||||
$field_length = 0;
|
||||
do {
|
||||
// 2. process the next character in EDIFACT encodation.
|
||||
$chr = \ord($data[$epos]);
|
||||
if ($this->isCharMode($chr, Data::ENC_EDF)) {
|
||||
++$epos;
|
||||
$temp_cw[] = $chr;
|
||||
++$field_length;
|
||||
}
|
||||
|
||||
if (
|
||||
($field_length === 4 || $epos === $data_length || !$this->isCharMode($chr, Data::ENC_EDF))
|
||||
&& $this->encodeEDFfour($epos, $cdw, $cdw_num, $pos, $data_length, $field_length, $enc, $temp_cw)
|
||||
) {
|
||||
break;
|
||||
}
|
||||
} while ($epos < $data_length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode Base256
|
||||
*
|
||||
* @param array<int, int> $cdw Codewords array
|
||||
* @param int $cdw_num Codewords number
|
||||
* @param int $pos Current position
|
||||
* @param int $data_length Data length
|
||||
* @param int $field_length Field length
|
||||
* @param string $data Data string
|
||||
* @param int $enc Current encoding
|
||||
*/
|
||||
public function encodeBase256(
|
||||
array &$cdw,
|
||||
int &$cdw_num,
|
||||
int &$pos,
|
||||
int &$data_length,
|
||||
int &$field_length,
|
||||
string &$data,
|
||||
int &$enc,
|
||||
): void {
|
||||
// initialize temporary array with 0 length
|
||||
$temp_cw = [];
|
||||
$field_length = 0;
|
||||
while ($pos < $data_length && $field_length <= 1555) {
|
||||
$newenc = $this->lookAheadTest($data, $pos, $enc);
|
||||
if ($newenc !== $enc) {
|
||||
// 1. If the look-ahead test (starting at step J)
|
||||
// indicates another mode, switch to that mode.
|
||||
$enc = $newenc;
|
||||
break; // exit from B256 mode
|
||||
}
|
||||
|
||||
// 2. Otherwise, process the next character in Base 256 encodation.
|
||||
$chr = \ord($data[$pos]);
|
||||
++$pos;
|
||||
$temp_cw[] = $chr;
|
||||
++$field_length;
|
||||
}
|
||||
|
||||
// set field length
|
||||
if ($field_length <= 249) {
|
||||
$cdw[] = $this->get255StateCodeword($field_length, $cdw_num + 1);
|
||||
++$cdw_num;
|
||||
}
|
||||
|
||||
if ($field_length > 249) {
|
||||
$cdw[] = $this->get255StateCodeword((int) \floor($field_length / 250) + 249, $cdw_num + 1);
|
||||
$cdw[] = $this->get255StateCodeword($field_length % 250, $cdw_num + 2);
|
||||
$cdw_num += 2;
|
||||
}
|
||||
|
||||
// add B256 field
|
||||
foreach ($temp_cw as $cht) {
|
||||
$cdw[] = $this->get255StateCodeword($cht, $cdw_num + 1);
|
||||
++$cdw_num;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* EncodeTxt.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Datamatrix;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Datamatrix\EncodeTxt
|
||||
*
|
||||
* Encode TXT/C40 methods for Datamatrix Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class EncodeTxt extends \Com\Tecnick\Barcode\Type\Square\Datamatrix\Steps
|
||||
{
|
||||
/**
|
||||
* @return array<int|string, int>
|
||||
*/
|
||||
protected function getCharset(string $key): array
|
||||
{
|
||||
return Data::CHSET[$key] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int|string, int> $charset
|
||||
*/
|
||||
protected function getCharsetValue(array $charset, int $chr): int
|
||||
{
|
||||
return $charset[$chr] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $temp_cw
|
||||
*/
|
||||
protected function shiftTempCw(array &$temp_cw): int
|
||||
{
|
||||
if (!\array_key_exists(0, $temp_cw)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$val = $temp_cw[0];
|
||||
\array_splice($temp_cw, 0, 1);
|
||||
return $val;
|
||||
}
|
||||
|
||||
protected function getCharsetId(int $enc): string
|
||||
{
|
||||
return Data::CHSET_ID[$enc] ?? 'BAS';
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode TXTC40 shift
|
||||
*
|
||||
* @param int $chr Character code
|
||||
* @param int $enc Current encoding
|
||||
* @param array<int, int> $temp_cw Temporary codewords array
|
||||
* @param int $ptr Pointer
|
||||
*
|
||||
* @throws BarcodeException in case of shift encoding errors
|
||||
*/
|
||||
public function encodeTXTC40shift(int &$chr, int &$enc, array &$temp_cw, int &$ptr): void
|
||||
{
|
||||
$shiftset = $this->getCharset('SH1');
|
||||
if (\array_key_exists($chr, $shiftset)) {
|
||||
$temp_cw[] = 0; // shift 1
|
||||
$temp_cw[] = $this->getCharsetValue($shiftset, $chr);
|
||||
$ptr += 2;
|
||||
return;
|
||||
}
|
||||
|
||||
$shiftset = $this->getCharset('SH2');
|
||||
if (\array_key_exists($chr, $shiftset)) {
|
||||
$temp_cw[] = 1; // shift 2
|
||||
$temp_cw[] = $this->getCharsetValue($shiftset, $chr);
|
||||
$ptr += 2;
|
||||
return;
|
||||
}
|
||||
|
||||
$shiftset = $this->getCharset('S3C');
|
||||
if ($enc === Data::ENC_C40 && \array_key_exists($chr, $shiftset)) {
|
||||
$temp_cw[] = 2; // shift 3
|
||||
$temp_cw[] = $this->getCharsetValue($shiftset, $chr);
|
||||
$ptr += 2;
|
||||
return;
|
||||
}
|
||||
|
||||
$shiftset = $this->getCharset('S3T');
|
||||
if ($enc === Data::ENC_TXT && \array_key_exists($chr, $shiftset)) {
|
||||
$temp_cw[] = 2; // shift 3
|
||||
$temp_cw[] = $this->getCharsetValue($shiftset, $chr);
|
||||
$ptr += 2;
|
||||
return;
|
||||
}
|
||||
|
||||
throw new BarcodeException('Error');
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode TXTC40
|
||||
*
|
||||
* @param string $data Data string
|
||||
* @param int $enc Current encoding
|
||||
* @param array<int, int> $temp_cw Temporary codewords array
|
||||
* @param int $ptr Pointer
|
||||
* @param int $epos End position
|
||||
* @param array<int|string, int> $charset Charset array
|
||||
*
|
||||
* @return int Curent character code
|
||||
*
|
||||
* @throws BarcodeException in case of TXT/C40 encoding errors
|
||||
*/
|
||||
public function encodeTXTC40(string &$data, int &$enc, array &$temp_cw, int &$ptr, int &$epos, array &$charset): int
|
||||
{
|
||||
// 2. process the next character in C40 encodation.
|
||||
$chr = \ord($data[$epos]);
|
||||
++$epos;
|
||||
// check for extended character
|
||||
if (($chr & 0x80) !== 0) {
|
||||
if ($enc === Data::ENC_X12) {
|
||||
throw new BarcodeException('TXTC40 Error');
|
||||
}
|
||||
|
||||
$chr &= 0x7f;
|
||||
$temp_cw[] = 1; // shift 2
|
||||
$temp_cw[] = 30; // upper shift
|
||||
$ptr += 2;
|
||||
}
|
||||
|
||||
if (\array_key_exists($chr, $charset)) {
|
||||
$temp_cw[] = $this->getCharsetValue($charset, $chr);
|
||||
++$ptr;
|
||||
return $chr;
|
||||
}
|
||||
|
||||
$this->encodeTXTC40shift($chr, $enc, $temp_cw, $ptr);
|
||||
|
||||
return $chr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode TXTC40 last
|
||||
* The following rules apply when only one or two symbol characters remain in the symbol
|
||||
* before the start of the error correction codewords.
|
||||
*
|
||||
* @param int $chr Character code
|
||||
* @param array<int, int> $cdw Codewords array
|
||||
* @param int $cdw_num Codewords number
|
||||
* @param int $enc Current encoding
|
||||
* @param array<int, int> $temp_cw Temporary codewords array
|
||||
* @param int $ptr Pointer
|
||||
* @param int $epos End position
|
||||
*/
|
||||
public function encodeTXTC40last(
|
||||
int $chr,
|
||||
array &$cdw,
|
||||
int &$cdw_num,
|
||||
int &$enc,
|
||||
array &$temp_cw,
|
||||
int &$ptr,
|
||||
int &$epos,
|
||||
): void {
|
||||
// get remaining number of data symbols
|
||||
$cdwr = $this->getMaxDataCodewords($cdw_num + $ptr) - $cdw_num;
|
||||
if ($cdwr === 1 && $ptr === 1) {
|
||||
// d. If one symbol character remains and one
|
||||
// C40 value (data character) remains to be encoded
|
||||
$cdw[] = $chr + 1;
|
||||
++$cdw_num;
|
||||
$enc = Data::ENC_ASCII;
|
||||
$this->last_enc = $enc;
|
||||
return;
|
||||
}
|
||||
|
||||
if ($cdwr === 2 && $ptr === 1) {
|
||||
// c. If two symbol characters remain and only one
|
||||
// C40 value (data character) remains to be encoded
|
||||
$cdw[] = 254;
|
||||
$cdw[] = $chr + 1;
|
||||
$cdw_num += 2;
|
||||
$enc = Data::ENC_ASCII;
|
||||
$this->last_enc = $enc;
|
||||
return;
|
||||
}
|
||||
|
||||
if ($cdwr === 2 && $ptr === 2) {
|
||||
// b. If two symbol characters remain and two C40 values remain to be encoded
|
||||
$ch1 = $this->shiftTempCw($temp_cw);
|
||||
$ch2 = $this->shiftTempCw($temp_cw);
|
||||
$ptr -= 2;
|
||||
$tmp = (1600 * $ch1) + (40 * $ch2) + 1;
|
||||
$cdw[] = $tmp >> 8;
|
||||
$cdw[] = $tmp % 256;
|
||||
$cdw_num += 2;
|
||||
$enc = Data::ENC_ASCII;
|
||||
$this->last_enc = $enc;
|
||||
return;
|
||||
}
|
||||
|
||||
if ($enc !== Data::ENC_ASCII) {
|
||||
// switch to ASCII encoding
|
||||
$enc = Data::ENC_ASCII;
|
||||
$this->last_enc = $enc;
|
||||
$cdw[] = $this->getSwitchEncodingCodeword($enc);
|
||||
++$cdw_num;
|
||||
$epos -= $ptr;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode TXT
|
||||
*
|
||||
* @param array<int, int> $cdw Codewords array
|
||||
* @param int $cdw_num Codewords number
|
||||
* @param int $pos Current position
|
||||
* @param int $data_length Data length
|
||||
* @param string $data Data string
|
||||
* @param int $enc Current encoding
|
||||
*
|
||||
* @throws BarcodeException in case of TXT/C40 encoding errors
|
||||
*/
|
||||
public function encodeTXT(array &$cdw, int &$cdw_num, int &$pos, int &$data_length, string &$data, int &$enc): void
|
||||
{
|
||||
/** @var array<int, int> $temp_cw */
|
||||
$temp_cw = [];
|
||||
$ptr = 0;
|
||||
$epos = $pos;
|
||||
// get charset ID
|
||||
$set_id = $this->getCharsetId($enc);
|
||||
// get basic charset for current encoding
|
||||
$charset = $this->getCharset($set_id);
|
||||
do {
|
||||
$chr = $this->encodeTXTC40($data, $enc, $temp_cw, $ptr, $epos, $charset);
|
||||
if ($ptr >= 3) {
|
||||
$ch1 = $this->shiftTempCw($temp_cw);
|
||||
$ch2 = $this->shiftTempCw($temp_cw);
|
||||
$ch3 = $this->shiftTempCw($temp_cw);
|
||||
$ptr -= 3;
|
||||
$tmp = (1600 * $ch1) + (40 * $ch2) + $ch3 + 1;
|
||||
$cdw[] = $tmp >> 8;
|
||||
$cdw[] = $tmp % 256;
|
||||
$cdw_num += 2;
|
||||
$pos = $epos;
|
||||
// 1. If the C40 encoding is at the point of starting a new double symbol character and
|
||||
// if the look-ahead test (starting at step J) indicates another mode, switch to that mode.
|
||||
$newenc = $this->lookAheadTest($data, $pos, $enc);
|
||||
if ($newenc !== $enc) {
|
||||
// switch to new encoding
|
||||
$enc = $newenc;
|
||||
if ($enc !== Data::ENC_ASCII) {
|
||||
// set unlatch character
|
||||
$cdw[] = $this->getSwitchEncodingCodeword(Data::ENC_ASCII);
|
||||
++$cdw_num;
|
||||
}
|
||||
|
||||
$cdw[] = $this->getSwitchEncodingCodeword($enc);
|
||||
++$cdw_num;
|
||||
$pos -= $ptr;
|
||||
$ptr = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while ($ptr > 0 && $epos < $data_length);
|
||||
|
||||
// process last data (if any)
|
||||
if ($ptr > 0) {
|
||||
$this->encodeTXTC40last($chr, $cdw, $cdw_num, $enc, $temp_cw, $ptr, $epos);
|
||||
$pos = $epos;
|
||||
}
|
||||
}
|
||||
}
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* ErrorCorrection.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Datamatrix;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Datamatrix\ErrorCorrection
|
||||
*
|
||||
* Error correction methods and other utilities for Datamatrix Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class ErrorCorrection
|
||||
{
|
||||
/**
|
||||
* @param array<int, int> $values
|
||||
*/
|
||||
protected function getArrayInt(array $values, int $idx): int
|
||||
{
|
||||
return $values[$idx] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Product of two numbers in a Power-of-Two Galois Field
|
||||
*
|
||||
* @param int $numa First number to multiply.
|
||||
* @param int $numb Second number to multiply.
|
||||
* @param array<int, int> $log Log table.
|
||||
* @param array<int, int> $alog Anti-Log table.
|
||||
* @param int $ngf Number of Factors of the Reed-Solomon polynomial.
|
||||
*
|
||||
* @return int product
|
||||
*/
|
||||
protected function getGFProduct(int $numa, int $numb, array $log, array $alog, int $ngf): int
|
||||
{
|
||||
if ($numa === 0 || $numb === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$a = $this->getArrayInt($log, $numa);
|
||||
$b = $this->getArrayInt($log, $numb);
|
||||
$idx = ($a + $b) % ($ngf - 1);
|
||||
|
||||
return $this->getArrayInt($alog, $idx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add error correction codewords to data codewords array (ANNEX E).
|
||||
*
|
||||
* @param array<int, int> $wdc Array of datacodewords.
|
||||
* @param int $nbk Number of blocks.
|
||||
* @param int $ncw Number of data codewords per block.
|
||||
* @param int $ncc Number of correction codewords per block.
|
||||
* @param int $ngf Number of fields on log/antilog table (power of 2).
|
||||
* @param int $vpp The value of its prime modulus polynomial (301 for ECC200).
|
||||
*
|
||||
* @return array<int, int> data codewords + error codewords
|
||||
*/
|
||||
public function getErrorCorrection(array $wdc, int $nbk, int $ncw, int $ncc, int $ngf = 256, int $vpp = 301): array
|
||||
{
|
||||
// generate the log ($log) and antilog ($alog) tables
|
||||
$log = [0];
|
||||
$alog = [1];
|
||||
$this->genLogs($log, $alog, $ngf, $vpp);
|
||||
|
||||
// generate the polynomial coefficients (c)
|
||||
$plc = \array_fill(0, \max(0, $ncc + 1), 0);
|
||||
$plc[0] = 1;
|
||||
for ($i = 1; $i <= $ncc; ++$i) {
|
||||
$plc[$i] = $this->getArrayInt($plc, $i - 1);
|
||||
for ($j = $i - 1; $j >= 1; --$j) {
|
||||
$plc[$j] =
|
||||
$this->getArrayInt($plc, $j - 1)
|
||||
^ $this->getGFProduct(
|
||||
$this->getArrayInt($plc, $j),
|
||||
$this->getArrayInt($alog, $i),
|
||||
$log,
|
||||
$alog,
|
||||
$ngf,
|
||||
);
|
||||
}
|
||||
|
||||
$plc[0] = $this->getGFProduct(
|
||||
$this->getArrayInt($plc, 0),
|
||||
$this->getArrayInt($alog, $i),
|
||||
$log,
|
||||
$alog,
|
||||
$ngf,
|
||||
);
|
||||
}
|
||||
|
||||
\ksort($plc);
|
||||
|
||||
// total number of data codewords
|
||||
$num_wd = $nbk * $ncw;
|
||||
// total number of error codewords
|
||||
$num_we = $nbk * $ncc;
|
||||
// for each block
|
||||
for ($b = 0; $b < $nbk; ++$b) {
|
||||
// create interleaved data block
|
||||
$block = [];
|
||||
for ($n = $b; $n < $num_wd; $n += $nbk) {
|
||||
$block[] = $this->getArrayInt($wdc, $n);
|
||||
}
|
||||
|
||||
// initialize error codewords
|
||||
$wec = \array_fill(0, \max(0, $ncc + 1), 0);
|
||||
// calculate error correction codewords for this block
|
||||
for ($i = 0; $i < $ncw; ++$i) {
|
||||
$ker = $this->getArrayInt($wec, 0) ^ $this->getArrayInt($block, $i);
|
||||
for ($j = 0; $j < $ncc; ++$j) {
|
||||
$wec[$j] =
|
||||
$this->getArrayInt($wec, $j + 1)
|
||||
^ $this->getGFProduct($ker, $this->getArrayInt($plc, $ncc - $j - 1), $log, $alog, $ngf);
|
||||
}
|
||||
}
|
||||
|
||||
// add error codewords at the end of data codewords
|
||||
$j = 0;
|
||||
for ($i = $b; $i < $num_we; $i += $nbk) {
|
||||
$wdc[$num_wd + $i] = $this->getArrayInt($wec, $j);
|
||||
++$j;
|
||||
}
|
||||
}
|
||||
|
||||
// reorder codewords
|
||||
\ksort($wdc);
|
||||
return $wdc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the log ($log) and antilog ($alog) tables
|
||||
*
|
||||
* @param array<int, int> $log Log table
|
||||
* @param array<int, int> $alog Anti-Log table
|
||||
* @param int $ngf Number of fields on log/antilog table (power of 2).
|
||||
* @param int $vpp The value of its prime modulus polynomial (301 for ECC200).
|
||||
*/
|
||||
protected function genLogs(array &$log, array &$alog, int $ngf, int $vpp): void
|
||||
{
|
||||
for ($i = 1; $i < $ngf; ++$i) {
|
||||
$alog[$i] = $this->getArrayInt($alog, $i - 1) * 2;
|
||||
if ($alog[$i] >= $ngf) {
|
||||
$alog[$i] ^= $vpp;
|
||||
}
|
||||
|
||||
$log[$alog[$i]] = $i;
|
||||
}
|
||||
|
||||
\ksort($log);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Modes.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Datamatrix;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Datamatrix\Modes
|
||||
*
|
||||
* Modes for Datamatrix Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Modes extends \Com\Tecnick\Barcode\Type\Square\Datamatrix\Placement
|
||||
{
|
||||
/**
|
||||
* @return array<int, array{0: int, 1: int, 2: int, 3: int, 4: int, 5: int, 6: int, 7: int, 8: int, 9: int, 10: int, 11: int, 12: int, 13: int, 14: int, 15: int}>
|
||||
*/
|
||||
protected function getShapeMatrices(): array
|
||||
{
|
||||
$result = [];
|
||||
foreach (Data::SYMBATTR[$this->shape] ?? [] as $matrix) {
|
||||
$result[] = $matrix;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function getSwitchCodeword(int $mode): int
|
||||
{
|
||||
return Data::SWITCHCDW[$mode] ?? 254;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store last used encoding for data codewords.
|
||||
*/
|
||||
public int $last_enc = 0; // Data::ENC_ASCII
|
||||
|
||||
/**
|
||||
* Datamatrix shape key (S=square, R=rectangular)
|
||||
*/
|
||||
public string $shape;
|
||||
|
||||
/**
|
||||
* Return the 253-state codeword
|
||||
*
|
||||
* @param int $cdwpad Pad codeword.
|
||||
* @param int $cdwpos Number of data codewords from the beginning of encoded data.
|
||||
*/
|
||||
public function get253StateCodeword(int $cdwpad, int $cdwpos): int
|
||||
{
|
||||
$pad = $cdwpad + (((149 * $cdwpos) % 253) + 1);
|
||||
if ($pad > 254) {
|
||||
$pad -= 254;
|
||||
}
|
||||
|
||||
return $pad;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the 255-state codeword
|
||||
*
|
||||
* @param int $cdwpad Pad codeword.
|
||||
* @param int $cdwpos Number of data codewords from the beginning of encoded data.
|
||||
*
|
||||
* @return int pad codeword
|
||||
*/
|
||||
protected function get255StateCodeword(int $cdwpad, int $cdwpos): int
|
||||
{
|
||||
$pad = $cdwpad + (((149 * $cdwpos) % 255) + 1);
|
||||
if ($pad > 255) {
|
||||
$pad -= 256;
|
||||
}
|
||||
|
||||
return $pad;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the char belongs to the selected mode
|
||||
*
|
||||
* @param int $chr Character (byte) to check.
|
||||
* @param int $mode Current encoding mode.
|
||||
*
|
||||
* @return bool true if the char is of the selected mode.
|
||||
*/
|
||||
protected function isCharMode(int $chr, int $mode): bool
|
||||
{
|
||||
return match ($mode) {
|
||||
//Data::ENC_ASCII => 'isASCIIMode',
|
||||
Data::ENC_C40 => $this->isC40Mode($chr),
|
||||
Data::ENC_TXT => $this->isTXTMode($chr),
|
||||
Data::ENC_X12 => $this->isX12Mode($chr),
|
||||
Data::ENC_EDF => $this->isEDFMode($chr),
|
||||
Data::ENC_BASE256 => $this->isBASE256Mode($chr),
|
||||
Data::ENC_ASCII_EXT => $this->isASCIIEXTMode($chr),
|
||||
Data::ENC_ASCII_NUM => $this->isASCIINUMMode($chr),
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
///**
|
||||
// * Tell if char is ASCII character 0 to 127
|
||||
// *
|
||||
// * @param int $chr Character (byte) to check.
|
||||
// *
|
||||
// * @return bool
|
||||
// */
|
||||
//protected function isASCIIMode(int $chr): bool
|
||||
//{
|
||||
// return (($chr >= 0) && ($chr <= 127));
|
||||
//}
|
||||
/**
|
||||
* Tell if char is Upper-case alphanumeric
|
||||
*
|
||||
* @param int $chr Character (byte) to check.
|
||||
*/
|
||||
protected function isC40Mode(int $chr): bool
|
||||
{
|
||||
return $chr === 32 || $chr >= 48 && $chr <= 57 || $chr >= 65 && $chr <= 90;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if char is Lower-case alphanumeric
|
||||
*
|
||||
* @param int $chr Character (byte) to check.
|
||||
*/
|
||||
protected function isTXTMode(int $chr): bool
|
||||
{
|
||||
return $chr === 32 || $chr >= 48 && $chr <= 57 || $chr >= 97 && $chr <= 122;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if char is ANSI X12
|
||||
*
|
||||
* @param int $chr Character (byte) to check.
|
||||
*/
|
||||
protected function isX12Mode(int $chr): bool
|
||||
{
|
||||
return $chr === 13 || $chr === 42 || $chr === 62;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if char is ASCII character 32 to 94
|
||||
*
|
||||
* @param int $chr Character (byte) to check.
|
||||
*/
|
||||
protected function isEDFMode(int $chr): bool
|
||||
{
|
||||
return $chr >= 32 && $chr <= 94;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if char is Function character (FNC1, Structured Append, Reader Program, or Code Page)
|
||||
*
|
||||
* @param int $chr Character (byte) to check.
|
||||
*/
|
||||
protected function isBASE256Mode(int $chr): bool
|
||||
{
|
||||
return $chr === 232 || $chr === 233 || $chr === 234 || $chr === 241;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if char is ASCII character 128 to 255
|
||||
*
|
||||
* @param int $chr Character (byte) to check.
|
||||
*/
|
||||
protected function isASCIIEXTMode(int $chr): bool
|
||||
{
|
||||
return $chr >= 128 && $chr <= 255;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if char is ASCII digits
|
||||
*
|
||||
* @param int $chr Character (byte) to check.
|
||||
*/
|
||||
protected function isASCIINUMMode(int $chr): bool
|
||||
{
|
||||
return $chr >= 48 && $chr <= 57;
|
||||
}
|
||||
|
||||
/**
|
||||
* Choose the minimum matrix size and return the max number of data codewords.
|
||||
*
|
||||
* @param int $numcw Number of current codewords.
|
||||
*
|
||||
* @return int number of data codewords in matrix
|
||||
*/
|
||||
protected function getMaxDataCodewords(int $numcw): int
|
||||
{
|
||||
$mdc = 0;
|
||||
foreach ($this->getShapeMatrices() as $matrix) {
|
||||
if ($matrix[11] < $numcw) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$mdc = $matrix[11];
|
||||
break;
|
||||
}
|
||||
|
||||
return $mdc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the switching codeword to a new encoding mode (latch codeword)
|
||||
*
|
||||
* @param int $mode New encoding mode.
|
||||
*
|
||||
* @return int Switch codeword.
|
||||
*/
|
||||
public function getSwitchEncodingCodeword(int $mode): int
|
||||
{
|
||||
$cdw = $this->getSwitchCodeword($mode);
|
||||
if ($cdw !== 254) {
|
||||
return $cdw;
|
||||
}
|
||||
|
||||
if ($this->last_enc !== Data::ENC_EDF) {
|
||||
return $cdw;
|
||||
}
|
||||
|
||||
return 124;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Placement.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Datamatrix;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Datamatrix\Placement
|
||||
*
|
||||
* Placement methods for Datamatrix Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Placement
|
||||
{
|
||||
/**
|
||||
* Places "chr+bit" with appropriate wrapping within array[].
|
||||
* (Annex F - ECC 200 symbol character placement)
|
||||
*
|
||||
* @param array<int, int> $marr Array of symbols.
|
||||
* @param int $nrow Number of rows.
|
||||
* @param int $ncol Number of columns.
|
||||
* @param int $row Row number.
|
||||
* @param int $col Column number.
|
||||
* @param int $chr Char byte.
|
||||
* @param int $bit Bit.
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function placeModule(array $marr, int $nrow, int $ncol, int $row, int $col, int $chr, int $bit): array
|
||||
{
|
||||
if ($row < 0) {
|
||||
$row += $nrow;
|
||||
$col += 4 - (($nrow + 4) % 8);
|
||||
}
|
||||
|
||||
if ($col < 0) {
|
||||
$col += $ncol;
|
||||
$row += 4 - (($ncol + 4) % 8);
|
||||
}
|
||||
|
||||
$marr[($row * $ncol) + $col] = (10 * $chr) + $bit;
|
||||
return $marr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Places the 8 bits of a utah-shaped symbol character.
|
||||
* (Annex F - ECC 200 symbol character placement)
|
||||
*
|
||||
* @param array<int, int> $marr Array of symbols.
|
||||
* @param int $nrow Number of rows.
|
||||
* @param int $ncol Number of columns.
|
||||
* @param int $row Row number.
|
||||
* @param int $col Column number.
|
||||
* @param int $chr Char byte.
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function placeUtah(array $marr, int $nrow, int $ncol, int $row, int $col, int $chr): array
|
||||
{
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $row - 2, $col - 2, $chr, 1);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $row - 2, $col - 1, $chr, 2);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $row - 1, $col - 2, $chr, 3);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $row - 1, $col - 1, $chr, 4);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $row - 1, $col, $chr, 5);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $row, $col - 2, $chr, 6);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $row, $col - 1, $chr, 7);
|
||||
return $this->placeModule($marr, $nrow, $ncol, $row, $col, $chr, 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Places the 8 bits of the first special corner case.
|
||||
* (Annex F - ECC 200 symbol character placement)
|
||||
*
|
||||
* @param array<int, int> $marr Array of symbols
|
||||
* @param int $nrow Number of rows
|
||||
* @param int $ncol Number of columns
|
||||
* @param int $chr Char byte
|
||||
* @param int $row Current row
|
||||
* @param int $col Current column
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function placeCornerA(array $marr, int $nrow, int $ncol, int &$chr, int $row, int $col): array
|
||||
{
|
||||
if ($row !== $nrow || $col !== 0) {
|
||||
return $marr;
|
||||
}
|
||||
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 0, $chr, 1);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 1, $chr, 2);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 2, $chr, 3);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 2, $chr, 4);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 1, $chr, 5);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 1, $chr, 6);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol - 1, $chr, 7);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol - 1, $chr, 8);
|
||||
++$chr;
|
||||
return $marr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Places the 8 bits of the second special corner case.
|
||||
* (Annex F - ECC 200 symbol character placement)
|
||||
*
|
||||
* @param array<int, int> $marr Array of symbols
|
||||
* @param int $nrow Number of rows
|
||||
* @param int $ncol Number of columns
|
||||
* @param int $chr Char byte
|
||||
* @param int $row Current row
|
||||
* @param int $col Current column
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function placeCornerB(array $marr, int $nrow, int $ncol, int &$chr, int $row, int $col): array
|
||||
{
|
||||
if ($row !== ($nrow - 2) || $col !== 0 || ($ncol % 4) === 0) {
|
||||
return $marr;
|
||||
}
|
||||
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 3, 0, $chr, 1);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 2, 0, $chr, 2);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 0, $chr, 3);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 4, $chr, 4);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 3, $chr, 5);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 2, $chr, 6);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 1, $chr, 7);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 1, $chr, 8);
|
||||
++$chr;
|
||||
return $marr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Places the 8 bits of the third special corner case.
|
||||
* (Annex F - ECC 200 symbol character placement)
|
||||
*
|
||||
* @param array<int, int> $marr Array of symbols
|
||||
* @param int $nrow Number of rows
|
||||
* @param int $ncol Number of columns
|
||||
* @param int $chr Char byte
|
||||
* @param int $row Current row
|
||||
* @param int $col Current column
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function placeCornerC(array $marr, int $nrow, int $ncol, int &$chr, int $row, int $col): array
|
||||
{
|
||||
if ($row !== ($nrow - 2) || $col !== 0 || ($ncol % 8) !== 4) {
|
||||
return $marr;
|
||||
}
|
||||
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 3, 0, $chr, 1);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 2, 0, $chr, 2);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 0, $chr, 3);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 2, $chr, 4);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 1, $chr, 5);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 1, $chr, 6);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 2, $ncol - 1, $chr, 7);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 3, $ncol - 1, $chr, 8);
|
||||
++$chr;
|
||||
return $marr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Places the 8 bits of the fourth special corner case.
|
||||
* (Annex F - ECC 200 symbol character placement)
|
||||
*
|
||||
* @param array<int, int> $marr Array of symbols
|
||||
* @param int $nrow Number of rows
|
||||
* @param int $ncol Number of columns
|
||||
* @param int $chr Char byte
|
||||
* @param int $row Current row
|
||||
* @param int $col Current column
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function placeCornerD(array $marr, int $nrow, int $ncol, int &$chr, int $row, int $col): array
|
||||
{
|
||||
if ($row !== ($nrow + 4) || $col !== 2 || ($ncol % 8)) {
|
||||
return $marr;
|
||||
}
|
||||
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, 0, $chr, 1);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, $nrow - 1, $ncol - 1, $chr, 2);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 3, $chr, 3);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 2, $chr, 4);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 0, $ncol - 1, $chr, 5);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 3, $chr, 6);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 2, $chr, 7);
|
||||
$marr = $this->placeModule($marr, $nrow, $ncol, 1, $ncol - 1, $chr, 8);
|
||||
++$chr;
|
||||
return $marr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sweep upward diagonally, inserting successive characters,
|
||||
* (Annex F - ECC 200 symbol character placement)
|
||||
*
|
||||
* @param array<int, int> $marr Array of symbols
|
||||
* @param int $nrow Number of rows
|
||||
* @param int $ncol Number of columns
|
||||
* @param int $chr Char byte
|
||||
* @param int $row Current row
|
||||
* @param int $col Current column
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function placeSweepUpward(array $marr, int $nrow, int $ncol, int &$chr, int &$row, int &$col): array
|
||||
{
|
||||
do {
|
||||
if ($row < $nrow && $col >= 0 && !($marr[($row * $ncol) + $col] ?? 0)) {
|
||||
$marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
|
||||
++$chr;
|
||||
}
|
||||
|
||||
$row -= 2;
|
||||
$col += 2;
|
||||
} while ($row >= 0 && $col < $ncol);
|
||||
|
||||
++$row;
|
||||
$col += 3;
|
||||
return $marr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sweep downward diagonally, inserting successive characters,
|
||||
* (Annex F - ECC 200 symbol character placement)
|
||||
*
|
||||
* @param array<int, int> $marr Array of symbols
|
||||
* @param int $nrow Number of rows
|
||||
* @param int $ncol Number of columns
|
||||
* @param int $chr Char byte
|
||||
* @param int $row Current row
|
||||
* @param int $col Current column
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function placeSweepDownward(array $marr, int $nrow, int $ncol, int &$chr, int &$row, int &$col): array
|
||||
{
|
||||
do {
|
||||
if ($row >= 0 && $col < $ncol && !($marr[($row * $ncol) + $col] ?? 0)) {
|
||||
$marr = $this->placeUtah($marr, $nrow, $ncol, $row, $col, $chr);
|
||||
++$chr;
|
||||
}
|
||||
|
||||
$row += 2;
|
||||
$col -= 2;
|
||||
} while ($row < $nrow && $col >= 0);
|
||||
|
||||
$row += 3;
|
||||
++$col;
|
||||
return $marr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a placement map.
|
||||
* (Annex F - ECC 200 symbol character placement)
|
||||
*
|
||||
* @param int $nrow Number of rows.
|
||||
* @param int $ncol Number of columns.
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
public function getPlacementMap(int $nrow, int $ncol): array
|
||||
{
|
||||
// initialize array with zeros
|
||||
$marr = \array_fill(0, \max(0, $nrow * $ncol), 0);
|
||||
// set starting values
|
||||
$chr = 1;
|
||||
$row = 4;
|
||||
$col = 0;
|
||||
do {
|
||||
// repeatedly first check for one of the special corner cases, then
|
||||
$marr = $this->placeCornerA($marr, $nrow, $ncol, $chr, $row, $col);
|
||||
$marr = $this->placeCornerB($marr, $nrow, $ncol, $chr, $row, $col);
|
||||
$marr = $this->placeCornerC($marr, $nrow, $ncol, $chr, $row, $col);
|
||||
$marr = $this->placeCornerD($marr, $nrow, $ncol, $chr, $row, $col);
|
||||
// sweep upward diagonally, inserting successive characters,
|
||||
$marr = $this->placeSweepUpward($marr, $nrow, $ncol, $chr, $row, $col);
|
||||
// & then sweep downward diagonally, inserting successive characters,...
|
||||
$marr = $this->placeSweepDownward($marr, $nrow, $ncol, $chr, $row, $col);
|
||||
|
||||
// ... until the entire array is scanned
|
||||
} while ($row < $nrow || $col < $ncol);
|
||||
|
||||
// lastly, if the lower righthand corner is untouched, fill in fixed pattern
|
||||
if (!($marr[($nrow * $ncol) - 1] ?? 0)) {
|
||||
$marr[($nrow * $ncol) - 1] = 1;
|
||||
$marr[($nrow * $ncol) - $ncol - 2] = 1;
|
||||
}
|
||||
|
||||
return $marr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,458 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Steps.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\Datamatrix;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Datamatrix\Steps
|
||||
*
|
||||
* Steps methods for Datamatrix Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Steps extends \Com\Tecnick\Barcode\Type\Square\Datamatrix\Modes
|
||||
{
|
||||
/**
|
||||
* The look-ahead test scans the data to be encoded to find the best mode (Annex P - steps from J to S).
|
||||
*
|
||||
* @param string $data Data to encode
|
||||
* @param int $pos Current position
|
||||
* @param int $mode Current encoding mode
|
||||
*
|
||||
* @return int encoding mode
|
||||
*/
|
||||
public function lookAheadTest(string $data, int $pos, int $mode): int
|
||||
{
|
||||
$data_length = \strlen($data);
|
||||
if ($pos >= $data_length) {
|
||||
return $mode;
|
||||
}
|
||||
|
||||
$charscount = 0; // count processed chars
|
||||
// STEP J
|
||||
$numch = match ($mode) {
|
||||
Data::ENC_C40 => [1.0, 0.0, 2.0, 2.0, 2.0, 2.25],
|
||||
Data::ENC_TXT => [1.0, 2.0, 0.0, 2.0, 2.0, 2.25],
|
||||
Data::ENC_X12 => [1.0, 2.0, 2.0, 0.0, 2.0, 2.25],
|
||||
Data::ENC_EDF => [1.0, 2.0, 2.0, 2.0, 0.0, 2.25],
|
||||
Data::ENC_BASE256 => [1.0, 2.0, 2.0, 2.0, 2.0, 0.0],
|
||||
default => [0.0, 1.0, 1.0, 1.0, 1.0, 1.25],
|
||||
};
|
||||
|
||||
while (true) {
|
||||
if (($pos + $charscount) === $data_length) {
|
||||
return $this->stepK($numch);
|
||||
}
|
||||
|
||||
$chr = \ord($data[$pos + $charscount]);
|
||||
++$charscount;
|
||||
$this->stepL($chr, $numch);
|
||||
$this->stepM($chr, $numch);
|
||||
$this->stepN($chr, $numch);
|
||||
$this->stepO($chr, $numch);
|
||||
$this->stepP($chr, $numch);
|
||||
$this->stepQ($chr, $numch);
|
||||
if ($charscount >= 4) {
|
||||
$ret = $this->stepR($numch, $pos, $data_length, $charscount, $data);
|
||||
if ($ret >= 0) {
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Step K
|
||||
*
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch Number of characters
|
||||
*
|
||||
* @return int encoding mode
|
||||
*/
|
||||
protected function stepK(array $numch): int
|
||||
{
|
||||
if (
|
||||
($numch[Data::ENC_ASCII] ?? 0.0) <= \ceil(\min(
|
||||
$numch[Data::ENC_C40] ?? 0.0,
|
||||
$numch[Data::ENC_TXT] ?? 0.0,
|
||||
$numch[Data::ENC_X12] ?? 0.0,
|
||||
$numch[Data::ENC_EDF] ?? 0.0,
|
||||
$numch[Data::ENC_BASE256] ?? 0.0,
|
||||
))
|
||||
) {
|
||||
return Data::ENC_ASCII;
|
||||
}
|
||||
|
||||
if (
|
||||
($numch[Data::ENC_BASE256] ?? 0.0) < \ceil(\min(
|
||||
$numch[Data::ENC_ASCII] ?? 0.0,
|
||||
$numch[Data::ENC_C40] ?? 0.0,
|
||||
$numch[Data::ENC_TXT] ?? 0.0,
|
||||
$numch[Data::ENC_X12] ?? 0.0,
|
||||
$numch[Data::ENC_EDF] ?? 0.0,
|
||||
))
|
||||
) {
|
||||
return Data::ENC_BASE256;
|
||||
}
|
||||
|
||||
if (
|
||||
($numch[Data::ENC_EDF] ?? 0.0) < \ceil(\min(
|
||||
$numch[Data::ENC_ASCII] ?? 0.0,
|
||||
$numch[Data::ENC_C40] ?? 0.0,
|
||||
$numch[Data::ENC_TXT] ?? 0.0,
|
||||
$numch[Data::ENC_X12] ?? 0.0,
|
||||
$numch[Data::ENC_BASE256] ?? 0.0,
|
||||
))
|
||||
) {
|
||||
return Data::ENC_EDF;
|
||||
}
|
||||
|
||||
if (
|
||||
($numch[Data::ENC_TXT] ?? 0.0) < \ceil(\min(
|
||||
$numch[Data::ENC_ASCII] ?? 0.0,
|
||||
$numch[Data::ENC_C40] ?? 0.0,
|
||||
$numch[Data::ENC_X12] ?? 0.0,
|
||||
$numch[Data::ENC_EDF] ?? 0.0,
|
||||
$numch[Data::ENC_BASE256] ?? 0.0,
|
||||
))
|
||||
) {
|
||||
return Data::ENC_TXT;
|
||||
}
|
||||
|
||||
if (
|
||||
($numch[Data::ENC_X12] ?? 0.0) < \ceil(\min(
|
||||
$numch[Data::ENC_ASCII] ?? 0.0,
|
||||
$numch[Data::ENC_C40] ?? 0.0,
|
||||
$numch[Data::ENC_TXT] ?? 0.0,
|
||||
$numch[Data::ENC_EDF] ?? 0.0,
|
||||
$numch[Data::ENC_BASE256] ?? 0.0,
|
||||
))
|
||||
) {
|
||||
return Data::ENC_X12;
|
||||
}
|
||||
|
||||
return Data::ENC_C40;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch
|
||||
*/
|
||||
protected function getNumch(array $numch, int $mode): float
|
||||
{
|
||||
return match ($mode) {
|
||||
Data::ENC_ASCII => $numch[0],
|
||||
Data::ENC_C40 => $numch[1],
|
||||
Data::ENC_TXT => $numch[2],
|
||||
Data::ENC_X12 => $numch[3],
|
||||
Data::ENC_EDF => $numch[4],
|
||||
Data::ENC_BASE256 => $numch[5],
|
||||
default => 0.0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch
|
||||
*/
|
||||
protected function setNumch(array &$numch, int $mode, float $value): void
|
||||
{
|
||||
switch ($mode) {
|
||||
case Data::ENC_ASCII:
|
||||
$numch[0] = $value;
|
||||
return;
|
||||
|
||||
case Data::ENC_C40:
|
||||
$numch[1] = $value;
|
||||
return;
|
||||
|
||||
case Data::ENC_TXT:
|
||||
$numch[2] = $value;
|
||||
return;
|
||||
|
||||
case Data::ENC_X12:
|
||||
$numch[3] = $value;
|
||||
return;
|
||||
|
||||
case Data::ENC_EDF:
|
||||
$numch[4] = $value;
|
||||
return;
|
||||
|
||||
case Data::ENC_BASE256:
|
||||
$numch[5] = $value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch
|
||||
*/
|
||||
protected function addNumch(array &$numch, int $mode, float $value): void
|
||||
{
|
||||
$this->setNumch($numch, $mode, $this->getNumch($numch, $mode) + $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Step L
|
||||
*
|
||||
* @param int $chr Character code
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch Number of characters
|
||||
*/
|
||||
protected function stepL(int $chr, array &$numch): void
|
||||
{
|
||||
if ($this->isCharMode($chr, Data::ENC_ASCII_NUM)) {
|
||||
$this->addNumch($numch, Data::ENC_ASCII, 0.5);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
|
||||
$this->setNumch($numch, Data::ENC_ASCII, \ceil($this->getNumch($numch, Data::ENC_ASCII)));
|
||||
$this->addNumch($numch, Data::ENC_ASCII, 2.0);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->setNumch($numch, Data::ENC_ASCII, \ceil($this->getNumch($numch, Data::ENC_ASCII)));
|
||||
$this->addNumch($numch, Data::ENC_ASCII, 1.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Step M
|
||||
*
|
||||
* @param int $chr Character code
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch Number of characters
|
||||
*/
|
||||
protected function stepM(int $chr, array &$numch): void
|
||||
{
|
||||
if ($this->isCharMode($chr, Data::ENC_C40)) {
|
||||
$this->addNumch($numch, Data::ENC_C40, 2.0 / 3.0);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
|
||||
$this->addNumch($numch, Data::ENC_C40, 8.0 / 3.0);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->addNumch($numch, Data::ENC_C40, 4.0 / 3.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Step N
|
||||
*
|
||||
* @param int $chr Character code
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch Number of characters
|
||||
*/
|
||||
protected function stepN(int $chr, array &$numch): void
|
||||
{
|
||||
if ($this->isCharMode($chr, Data::ENC_TXT)) {
|
||||
$this->addNumch($numch, Data::ENC_TXT, 2.0 / 3.0);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
|
||||
$this->addNumch($numch, Data::ENC_TXT, 8.0 / 3.0);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->addNumch($numch, Data::ENC_TXT, 4.0 / 3.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Step O
|
||||
*
|
||||
* @param int $chr Character code
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch Number of characters
|
||||
*/
|
||||
protected function stepO(int $chr, array &$numch): void
|
||||
{
|
||||
if ($this->isCharMode($chr, Data::ENC_X12) || $this->isCharMode($chr, Data::ENC_C40)) {
|
||||
$this->addNumch($numch, Data::ENC_X12, 2.0 / 3.0);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
|
||||
$this->addNumch($numch, Data::ENC_X12, 13.0 / 3.0);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->addNumch($numch, Data::ENC_X12, 10.0 / 3.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Step P
|
||||
*
|
||||
* @param int $chr Character code
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch Number of characters
|
||||
*/
|
||||
protected function stepP(int $chr, array &$numch): void
|
||||
{
|
||||
if ($this->isCharMode($chr, Data::ENC_EDF)) {
|
||||
$this->addNumch($numch, Data::ENC_EDF, 3.0 / 4.0);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
|
||||
$this->addNumch($numch, Data::ENC_EDF, 17.0 / 4.0);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->addNumch($numch, Data::ENC_EDF, 13.0 / 4.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Step Q
|
||||
*
|
||||
* @param int $chr Character code
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch Number of characters
|
||||
*/
|
||||
protected function stepQ(int $chr, array &$numch): void
|
||||
{
|
||||
if ($this->isCharMode($chr, Data::ENC_BASE256)) {
|
||||
$this->addNumch($numch, Data::ENC_BASE256, 4.0);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->addNumch($numch, Data::ENC_BASE256, 1.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Step R-f
|
||||
*
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch Number of characters
|
||||
* @param int $pos Current position
|
||||
* @param int $data_length Data length
|
||||
* @param int $charscount Number of processed characters
|
||||
* @param string $data Data to encode
|
||||
*
|
||||
* @return int Encoding mode
|
||||
*/
|
||||
protected function stepRf(array $numch, int $pos, int $data_length, int $charscount, string $data): int
|
||||
{
|
||||
if (
|
||||
(($numch[Data::ENC_C40] ?? 0.0) + 1) < \min(
|
||||
$numch[Data::ENC_ASCII] ?? 0.0,
|
||||
$numch[Data::ENC_TXT] ?? 0.0,
|
||||
$numch[Data::ENC_EDF] ?? 0.0,
|
||||
$numch[Data::ENC_BASE256] ?? 0.0,
|
||||
)
|
||||
) {
|
||||
if (($numch[Data::ENC_C40] ?? 0.0) < ($numch[Data::ENC_X12] ?? 0.0)) {
|
||||
return Data::ENC_C40;
|
||||
}
|
||||
|
||||
if (($numch[Data::ENC_C40] ?? 0.0) === ($numch[Data::ENC_X12] ?? 0.0)) {
|
||||
$ker = $pos + $charscount + 1;
|
||||
while ($ker < $data_length) {
|
||||
$tmpchr = \ord($data[$ker]);
|
||||
if ($this->isCharMode($tmpchr, Data::ENC_X12)) {
|
||||
return Data::ENC_X12;
|
||||
}
|
||||
|
||||
if ($this->isCharMode($tmpchr, Data::ENC_C40)) {
|
||||
break;
|
||||
}
|
||||
|
||||
++$ker;
|
||||
}
|
||||
|
||||
return Data::ENC_C40;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Step R
|
||||
*
|
||||
* @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $numch Number of characters
|
||||
* @param int $pos Current position
|
||||
* @param int $data_length Data length
|
||||
* @param int $charscount Number of processed characters
|
||||
* @param string $data Data to encode
|
||||
*
|
||||
* @return int Encoding mode
|
||||
*/
|
||||
protected function stepR(array $numch, int $pos, int $data_length, int $charscount, string $data): int
|
||||
{
|
||||
if (
|
||||
(($numch[Data::ENC_ASCII] ?? 0.0) + 1) <= \min(
|
||||
$numch[Data::ENC_C40] ?? 0.0,
|
||||
$numch[Data::ENC_TXT] ?? 0.0,
|
||||
$numch[Data::ENC_X12] ?? 0.0,
|
||||
$numch[Data::ENC_EDF] ?? 0.0,
|
||||
$numch[Data::ENC_BASE256] ?? 0.0,
|
||||
)
|
||||
) {
|
||||
return Data::ENC_ASCII;
|
||||
}
|
||||
|
||||
if (
|
||||
(($numch[Data::ENC_BASE256] ?? 0.0) + 1) <= ($numch[Data::ENC_ASCII] ?? 0.0)
|
||||
|| (($numch[Data::ENC_BASE256] ?? 0.0) + 1) < \min(
|
||||
$numch[Data::ENC_C40] ?? 0.0,
|
||||
$numch[Data::ENC_TXT] ?? 0.0,
|
||||
$numch[Data::ENC_X12] ?? 0.0,
|
||||
$numch[Data::ENC_EDF] ?? 0.0,
|
||||
)
|
||||
) {
|
||||
return Data::ENC_BASE256;
|
||||
}
|
||||
|
||||
if (
|
||||
(($numch[Data::ENC_EDF] ?? 0.0) + 1) < \min(
|
||||
$numch[Data::ENC_ASCII] ?? 0.0,
|
||||
$numch[Data::ENC_C40] ?? 0.0,
|
||||
$numch[Data::ENC_TXT] ?? 0.0,
|
||||
$numch[Data::ENC_X12] ?? 0.0,
|
||||
$numch[Data::ENC_BASE256] ?? 0.0,
|
||||
)
|
||||
) {
|
||||
return Data::ENC_EDF;
|
||||
}
|
||||
|
||||
if (
|
||||
(($numch[Data::ENC_TXT] ?? 0.0) + 1) < \min(
|
||||
$numch[Data::ENC_ASCII] ?? 0.0,
|
||||
$numch[Data::ENC_C40] ?? 0.0,
|
||||
$numch[Data::ENC_X12] ?? 0.0,
|
||||
$numch[Data::ENC_EDF] ?? 0.0,
|
||||
$numch[Data::ENC_BASE256] ?? 0.0,
|
||||
)
|
||||
) {
|
||||
return Data::ENC_TXT;
|
||||
}
|
||||
|
||||
if (
|
||||
(($numch[Data::ENC_X12] ?? 0.0) + 1) < \min(
|
||||
$numch[Data::ENC_ASCII] ?? 0.0,
|
||||
$numch[Data::ENC_C40] ?? 0.0,
|
||||
$numch[Data::ENC_TXT] ?? 0.0,
|
||||
$numch[Data::ENC_EDF] ?? 0.0,
|
||||
$numch[Data::ENC_BASE256] ?? 0.0,
|
||||
)
|
||||
) {
|
||||
return Data::ENC_X12;
|
||||
}
|
||||
|
||||
return $this->stepRf($numch, $pos, $data_length, $charscount, $data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,380 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* PdfFourOneSeven.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
use Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven\Data;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven
|
||||
*
|
||||
* PdfFourOneSeven Barcode type class
|
||||
* PDF417 (ISO/IEC 15438:2006)
|
||||
*
|
||||
* PDF417 (ISO/IEC 15438:2006) is a 2-dimensional stacked bar code created by Symbol Technologies in 1991.
|
||||
* It is one of the most popular 2D codes because of its ability to be read with slightly modified handheld
|
||||
* laser or linear CCD scanners.
|
||||
* TECHNICAL DATA / FEATURES OF PDF417:
|
||||
* Encodable Character Set: All 128 ASCII Characters (including extended)
|
||||
* Code Type: Continuous, Multi-Row
|
||||
* Symbol Height: 3 - 90 Rows
|
||||
* Symbol Width: 90X - 583X
|
||||
* Bidirectional Decoding: Yes
|
||||
* Error Correction Characters: 2 - 512
|
||||
* Maximum Data Characters: 1850 text, 2710 digits, 1108 bytes
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class PdfFourOneSeven extends \Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven\Compaction
|
||||
{
|
||||
/**
|
||||
* @param array<int, int> $codewords
|
||||
*/
|
||||
protected function getCodewordValue(array $codewords, int $index): int
|
||||
{
|
||||
return $codewords[$index] ?? 0;
|
||||
}
|
||||
|
||||
protected function getClusterCodewordValue(int $clusterId, int $codeword): int
|
||||
{
|
||||
return Data::CLUSTERS[$clusterId][$codeword] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'PDF417';
|
||||
|
||||
/**
|
||||
* Row height respect X dimension of single module
|
||||
*/
|
||||
protected int $row_height = 2;
|
||||
|
||||
/**
|
||||
* Vertical quiet zone in modules
|
||||
*/
|
||||
protected int $quiet_vertical = 2;
|
||||
|
||||
/**
|
||||
* Horizontal quiet zone in modules
|
||||
*/
|
||||
protected int $quiet_horizontal = 2;
|
||||
|
||||
/**
|
||||
* Aspect ratio (width / height)
|
||||
*/
|
||||
protected float $aspectratio = 2;
|
||||
|
||||
/**
|
||||
* Error correction level (0-8);
|
||||
* Default -1 = automatic correction level
|
||||
*/
|
||||
protected int $ecl = -1;
|
||||
|
||||
/**
|
||||
* Information for macro block
|
||||
*
|
||||
* @var array<string, int|string>
|
||||
*/
|
||||
protected array $macro = [];
|
||||
|
||||
/**
|
||||
* Set extra (optional) parameters
|
||||
*/
|
||||
protected function setParameters(): void
|
||||
{
|
||||
parent::setParameters();
|
||||
|
||||
// aspect ratio
|
||||
if (
|
||||
($this->params[0] ?? null) !== null
|
||||
&& $this->params[0] !== ''
|
||||
&& ($aspectratio = (float) $this->params[0]) >= 1
|
||||
) {
|
||||
$this->aspectratio = $aspectratio;
|
||||
}
|
||||
|
||||
// error correction level (auto)
|
||||
if (($this->params[1] ?? null) !== null && ($ecl = (int) $this->params[1]) >= 0 && $ecl <= 8) {
|
||||
$this->ecl = $ecl;
|
||||
}
|
||||
|
||||
// macro block
|
||||
$this->setMacroBlockParam();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set macro block parameter
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
|
||||
*/
|
||||
protected function setMacroBlockParam(): void
|
||||
{
|
||||
if (
|
||||
($this->params[4] ?? null) !== null
|
||||
&& \is_string($this->params[4])
|
||||
&& ($this->params[2] ?? '') !== ''
|
||||
&& ($this->params[3] ?? '') !== ''
|
||||
&& $this->params[4] !== ''
|
||||
) {
|
||||
$this->macro['segment_total'] = (int) $this->params[2];
|
||||
$this->macro['segment_index'] = (int) $this->params[3];
|
||||
$this->macro['file_id'] = \strtr($this->params[4], "\xff", ',');
|
||||
for ($idx = 0; $idx < 7; ++$idx) {
|
||||
$opt = $idx + 5;
|
||||
if (
|
||||
($this->params[$opt] ?? null) !== null
|
||||
&& \is_string($this->params[$opt])
|
||||
&& $this->params[$opt] !== ''
|
||||
) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
$this->macro['option_' . $idx] = \strtr($this->params[$opt], "\xff", ',');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bars array
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
if (\strlen($this->code) === 0) {
|
||||
throw new BarcodeException('Empty input');
|
||||
}
|
||||
|
||||
$seq = $this->getBinSequence();
|
||||
$this->processBinarySequence($this->getRawCodeRows($seq));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get macro control block codewords
|
||||
*
|
||||
* @param int $numcw Number of codewords
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function getMacroBlock(int &$numcw): array
|
||||
{
|
||||
if ($this->macro === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$macrocw = [];
|
||||
$segmentIndex = (int) ($this->macro['segment_index'] ?? 0);
|
||||
$segmentTotal = (int) ($this->macro['segment_total'] ?? 0);
|
||||
$fileId = (string) ($this->macro['file_id'] ?? '');
|
||||
// beginning of macro control block
|
||||
$macrocw[] = 928;
|
||||
// segment index
|
||||
$cdw = $this->getCompaction(902, \sprintf('%05d', $segmentIndex), false);
|
||||
$macrocw = \array_merge($macrocw, $cdw);
|
||||
// file ID
|
||||
$cdw = $this->getCompaction(900, $fileId, false);
|
||||
$macrocw = \array_merge($macrocw, $cdw);
|
||||
// optional fields
|
||||
$optmodes = [900, 902, 902, 900, 900, 902, 902];
|
||||
$optsize = [-1, 2, 4, -1, -1, -1, 2];
|
||||
foreach ($optmodes as $key => $omode) {
|
||||
$optionKey = 'option_' . $key;
|
||||
if (($this->macro[$optionKey] ?? null) !== null) {
|
||||
$option = (string) $this->macro[$optionKey];
|
||||
$macrocw[] = 923;
|
||||
$macrocw[] = $key;
|
||||
$option = match ($optsize[$key] ?? -1) {
|
||||
2 => \sprintf('%05d', $option),
|
||||
4 => \sprintf('%010d', $option),
|
||||
default => $option,
|
||||
};
|
||||
|
||||
$cdw = $this->getCompaction($omode, $option, false);
|
||||
$macrocw = \array_merge($macrocw, $cdw);
|
||||
}
|
||||
}
|
||||
|
||||
if ($segmentIndex === ($segmentTotal - 1)) {
|
||||
// end of control block
|
||||
$macrocw[] = 922;
|
||||
}
|
||||
|
||||
// update total codewords
|
||||
$numcw += \count($macrocw);
|
||||
return $macrocw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get codewords
|
||||
*
|
||||
* @param int $rows number of rows
|
||||
* @param int $cols number of columns
|
||||
* @param int $ecl error correction level
|
||||
*
|
||||
* @return array<int, int>
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
public function getCodewords(int &$rows, int &$cols, int &$ecl): array
|
||||
{
|
||||
$codewords = []; // array of code-words
|
||||
// get the input sequence array
|
||||
$sequence = $this->getInputSequences($this->code);
|
||||
foreach ($sequence as $seq) {
|
||||
$cws = $this->getCompaction($seq[0], $seq[1], true);
|
||||
$codewords = \array_merge($codewords, $cws);
|
||||
}
|
||||
|
||||
if (($codewords[0] ?? 0) === 900) {
|
||||
// Text Alpha is the default mode, so remove the first code
|
||||
\array_shift($codewords);
|
||||
}
|
||||
|
||||
// count number of codewords
|
||||
$numcw = \count($codewords);
|
||||
if ($numcw > 925) {
|
||||
throw new BarcodeException('The maximum codeword capacity has been reached: ' . $numcw . ' > 925');
|
||||
}
|
||||
|
||||
$macrocw = $this->getMacroBlock($numcw);
|
||||
// set error correction level
|
||||
$ecl = $this->getErrorCorrectionLevel($this->ecl, $numcw);
|
||||
// number of codewords for error correction
|
||||
$errsize = 2 << $ecl;
|
||||
// calculate number of columns (number of codewords per row) and rows
|
||||
$nce = $numcw + $errsize + 1;
|
||||
$cols = (int) \min(30, \max(
|
||||
1,
|
||||
\round((\sqrt(4761 + (68 * $this->aspectratio * $this->row_height * $nce)) - 69) / 34),
|
||||
));
|
||||
$rows = (int) \min(90, \max(3, \ceil($nce / $cols)));
|
||||
$size = $cols * $rows;
|
||||
if ($size > 928) {
|
||||
// set dimensions to get maximum capacity
|
||||
$cols = 16;
|
||||
$rows = 58;
|
||||
if (\abs($this->aspectratio - ((17 * 29) / 32)) < \abs($this->aspectratio - ((17 * 16) / 58))) {
|
||||
$cols = 29;
|
||||
$rows = 32;
|
||||
}
|
||||
|
||||
$size = 928;
|
||||
}
|
||||
|
||||
// calculate padding
|
||||
$pad = (int) ($size - $nce);
|
||||
if ($pad > 0) {
|
||||
// add padding
|
||||
$codewords = \array_merge($codewords, \array_fill(0, $pad, 900));
|
||||
}
|
||||
|
||||
if ($macrocw !== []) {
|
||||
// add macro section
|
||||
$codewords = \array_merge($codewords, $macrocw);
|
||||
}
|
||||
|
||||
// Symbol Length Descriptor (number of data codewords including Symbol Length Descriptor and pad codewords)
|
||||
$sld = (int) ($size - $errsize);
|
||||
// add symbol length description
|
||||
\array_unshift($codewords, $sld);
|
||||
// calculate error correction
|
||||
$ecw = $this->getErrorCorrection($codewords, $ecl);
|
||||
// add error correction codewords
|
||||
return \array_merge($codewords, $ecw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a PDF417 object as binary string
|
||||
*
|
||||
* @return string barcode as binary string
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
public function getBinSequence(): string
|
||||
{
|
||||
$rows = 0;
|
||||
$cols = 0;
|
||||
$ecl = 0;
|
||||
$codewords = $this->getCodewords($rows, $cols, $ecl);
|
||||
$barcode = '';
|
||||
// add horizontal quiet zones to start and stop patterns
|
||||
$pstart = \str_repeat('0', \max(0, $this->quiet_horizontal)) . Data::START_PATTERN;
|
||||
$this->nrows = ($rows * $this->row_height) + (2 * $this->quiet_vertical);
|
||||
$this->ncols = (($cols + 2) * 17) + 35 + (2 * $this->quiet_horizontal);
|
||||
// build rows for vertical quiet zone
|
||||
$empty_row = ',' . \str_repeat('0', \max(0, $this->ncols));
|
||||
$empty_rows = \str_repeat($empty_row, \max(0, $this->quiet_vertical));
|
||||
$barcode .= $empty_rows;
|
||||
$kcw = 0; // codeword index
|
||||
$cid = 0; // initial cluster
|
||||
// for each row
|
||||
for ($rix = 0; $rix < $rows; ++$rix) {
|
||||
// row start code
|
||||
$row = $pstart;
|
||||
$rval = 0;
|
||||
$cval = 0;
|
||||
switch ($cid) {
|
||||
case 0:
|
||||
$rval = (30 * (int) ($rix / 3)) + (int) (($rows - 1) / 3);
|
||||
$cval = (30 * (int) ($rix / 3)) + ($cols - 1);
|
||||
break;
|
||||
case 1:
|
||||
$rval = (30 * (int) ($rix / 3)) + ($ecl * 3) + (($rows - 1) % 3);
|
||||
$cval = (30 * (int) ($rix / 3)) + (int) (($rows - 1) / 3);
|
||||
break;
|
||||
case 2:
|
||||
$rval = (30 * (int) ($rix / 3)) + ($cols - 1);
|
||||
$cval = (30 * (int) ($rix / 3)) + ($ecl * 3) + (($rows - 1) % 3);
|
||||
break;
|
||||
}
|
||||
|
||||
// left row indicator
|
||||
$row .= \sprintf('%17b', $this->getClusterCodewordValue($cid, $rval));
|
||||
// for each column
|
||||
for ($cix = 0; $cix < $cols; ++$cix) {
|
||||
$row .= \sprintf('%17b', $this->getClusterCodewordValue($cid, $this->getCodewordValue(
|
||||
$codewords,
|
||||
$kcw,
|
||||
)));
|
||||
++$kcw;
|
||||
}
|
||||
|
||||
// right row indicator
|
||||
$row .= \sprintf('%17b', $this->getClusterCodewordValue($cid, $cval));
|
||||
// row stop code
|
||||
$row .= Data::STOP_PATTERN . \str_repeat('0', \max(0, $this->quiet_horizontal));
|
||||
$brow = ',' . \str_repeat($row, \max(0, $this->row_height));
|
||||
$barcode .= $brow;
|
||||
++$cid;
|
||||
if ($cid > 2) {
|
||||
$cid = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return $barcode . $empty_rows;
|
||||
}
|
||||
}
|
||||
+314
@@ -0,0 +1,314 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Compaction.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven\Compaction
|
||||
*
|
||||
* Compaction methods for PdfFourOneSeven Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Compaction extends \Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven\Sequence
|
||||
{
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function getTextSubModeValues(int $submode): array
|
||||
{
|
||||
$result = [];
|
||||
foreach (Data::TEXT_SUB_MODES[$submode] ?? [] as $value) {
|
||||
$result[] = $value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function getTextLatchValues(int $submode, int $sub): array
|
||||
{
|
||||
$result = [];
|
||||
foreach (Data::TEXT_LATCH['' . $submode . $sub] ?? [] as $value) {
|
||||
$result[] = $value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function findTextSubModeKey(int $submode, int $chval): ?int
|
||||
{
|
||||
$key = \array_search($chval, $this->getTextSubModeValues($submode), true);
|
||||
if (\is_int($key)) {
|
||||
return $key;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getCodeOrd(string $code, int $idx): int
|
||||
{
|
||||
return \ord($code[$idx] ?? "\x00");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return numeric-string
|
||||
*/
|
||||
protected function getByteNumericString(string $code, int $idx): string
|
||||
{
|
||||
return (string) $this->getCodeOrd($code, $idx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $txtarr
|
||||
*/
|
||||
protected function getTxtArrayValue(array $txtarr, int $idx): int
|
||||
{
|
||||
return $txtarr[$idx] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*
|
||||
* @return numeric-string
|
||||
*/
|
||||
protected function normalizeNumericString(string $value): string
|
||||
{
|
||||
if (\ctype_digit($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return '0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Process Sub Text Compaction
|
||||
*
|
||||
* @param array<int, int> $txtarr Array of characters and sub-mode switching characters
|
||||
* @param int $submode Current submode
|
||||
* @param int $sub New submode
|
||||
* @param string $code Data to compact
|
||||
* @param int $key Character code
|
||||
* @param int $idx Current index
|
||||
* @param int $codelen Code length
|
||||
*/
|
||||
protected function processTextCompactionSub(
|
||||
array &$txtarr,
|
||||
int &$submode,
|
||||
int $sub,
|
||||
string $code,
|
||||
int $key,
|
||||
int $idx,
|
||||
int $codelen,
|
||||
): void {
|
||||
// $sub is the new submode
|
||||
$useShift =
|
||||
(
|
||||
($idx + 1) === $codelen
|
||||
|| ($idx + 1) < $codelen
|
||||
&& \in_array($this->getCodeOrd($code, $idx + 1), $this->getTextSubModeValues($submode), true)
|
||||
)
|
||||
&& ($sub === 3 || $sub === 0 && $submode === 1);
|
||||
|
||||
if ($useShift) {
|
||||
// shift (temporary change only for this char)
|
||||
$txtarr[] = $sub === 3 ? 29 : 27;
|
||||
// add character code to array
|
||||
$txtarr[] = $key;
|
||||
return;
|
||||
}
|
||||
|
||||
// latch
|
||||
foreach ($this->getTextLatchValues($submode, $sub) as $latch) {
|
||||
$txtarr[] = $latch;
|
||||
}
|
||||
|
||||
// set new submode
|
||||
$submode = $sub;
|
||||
|
||||
// add character code to array
|
||||
$txtarr[] = $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process Text Compaction
|
||||
*
|
||||
* @param string $code Data to compact
|
||||
* @param array<int, int> $codewords Codewords
|
||||
*/
|
||||
protected function processTextCompaction(string $code, array &$codewords): void
|
||||
{
|
||||
$submode = 0; // default Alpha sub-mode
|
||||
/** @var array<int, int> $txtarr */
|
||||
$txtarr = []; // array of characters and sub-mode switching characters
|
||||
$codelen = \strlen($code);
|
||||
for ($idx = 0; $idx < $codelen; ++$idx) {
|
||||
$chval = $this->getCodeOrd($code, $idx);
|
||||
$current_key = $this->findTextSubModeKey($submode, $chval);
|
||||
if ($current_key !== null) {
|
||||
// we are on the same sub-mode
|
||||
$txtarr[] = $current_key;
|
||||
continue;
|
||||
}
|
||||
|
||||
// the sub-mode is changed
|
||||
for ($sub = 0; $sub < 4; ++$sub) {
|
||||
// search new sub-mode
|
||||
$sub_key = $this->findTextSubModeKey($sub, $chval);
|
||||
if ($sub !== $submode && $sub_key !== null) {
|
||||
$this->processTextCompactionSub($txtarr, $submode, $sub, $code, $sub_key, $idx, $codelen);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$txtarrlen = \count($txtarr);
|
||||
if (($txtarrlen % 2) !== 0) {
|
||||
// add padding
|
||||
$txtarr[] = 29;
|
||||
++$txtarrlen;
|
||||
}
|
||||
|
||||
// calculate codewords
|
||||
for ($idx = 0; $idx < $txtarrlen; $idx += 2) {
|
||||
$codewords[] = (30 * $this->getTxtArrayValue($txtarr, $idx)) + $this->getTxtArrayValue($txtarr, $idx + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process Byte Compaction
|
||||
*
|
||||
* @param string $code Data to compact
|
||||
* @param array<int, int> $codewords Codewords
|
||||
*/
|
||||
protected function processByteCompaction(string $code, array &$codewords): void
|
||||
{
|
||||
while (($codelen = \strlen($code)) > 0) {
|
||||
$rest = '';
|
||||
$sublen = \strlen($code);
|
||||
if ($codelen > 6) {
|
||||
$rest = \substr($code, 6);
|
||||
$code = \substr($code, 0, 6);
|
||||
$sublen = 6;
|
||||
}
|
||||
|
||||
if ($sublen === 6) {
|
||||
$tdg = $this->mulNumeric($this->getByteNumericString($code, 0), '1099511627776');
|
||||
$tdg = $this->addNumeric($tdg, $this->mulNumeric($this->getByteNumericString($code, 1), '4294967296'));
|
||||
$tdg = $this->addNumeric($tdg, $this->mulNumeric($this->getByteNumericString($code, 2), '16777216'));
|
||||
$tdg = $this->addNumeric($tdg, $this->mulNumeric($this->getByteNumericString($code, 3), '65536'));
|
||||
$tdg = $this->addNumeric($tdg, $this->mulNumeric($this->getByteNumericString($code, 4), '256'));
|
||||
$tdg = $this->addNumeric($tdg, $this->getByteNumericString($code, 5));
|
||||
// tmp array for the 6 bytes block
|
||||
/** @var array<int, int> $cw6 */
|
||||
$cw6 = [];
|
||||
for ($idx = 0; $idx < 5; ++$idx) {
|
||||
$ddg = $this->modNumeric($tdg, '900');
|
||||
$tdg = $this->divNumeric($tdg, '900');
|
||||
// prepend the value to the beginning of the array
|
||||
\array_unshift($cw6, $ddg);
|
||||
}
|
||||
|
||||
// append the result array at the end
|
||||
foreach ($cw6 as $cw) {
|
||||
$codewords[] = $cw;
|
||||
}
|
||||
}
|
||||
|
||||
if ($sublen !== 6) {
|
||||
for ($idx = 0; $idx < $sublen; ++$idx) {
|
||||
$codewords[] = $this->getCodeOrd($code, $idx);
|
||||
}
|
||||
}
|
||||
|
||||
$code = $rest;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process Numeric Compaction
|
||||
*
|
||||
* @param string $code Data to compact
|
||||
* @param array<int, int> $codewords Codewords
|
||||
*/
|
||||
protected function processNumericCompaction(string $code, array &$codewords): void
|
||||
{
|
||||
$len = \strlen($code);
|
||||
// numbers are encoded in groups of up to 44 digits, emitted in left-to-right order
|
||||
for ($start = 0; $start < $len; $start += 44) {
|
||||
$tdg = $this->normalizeNumericString('1' . \substr($code, $start, 44));
|
||||
$group = [];
|
||||
do {
|
||||
// remainders come out least-significant first
|
||||
$group[] = $this->modNumeric($tdg, '900');
|
||||
$tdg = $this->divNumeric($tdg, '900');
|
||||
} while ($tdg !== '0');
|
||||
|
||||
// reverse to big-endian, then append this group after the previous ones
|
||||
$codewords = \array_merge($codewords, \array_reverse($group));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compact data by mode
|
||||
*
|
||||
* @param int $mode Compaction mode number
|
||||
* @param string $code Data to compact
|
||||
* @param bool $addmode If true add the mode codeword in the first position
|
||||
*
|
||||
* @return array<int, int> of codewords
|
||||
*/
|
||||
protected function getCompaction(int $mode, string $code, bool $addmode = true): array
|
||||
{
|
||||
$codewords = []; // array of codewords to return
|
||||
switch ($mode) {
|
||||
case 900:
|
||||
// Text Compaction mode latch
|
||||
$this->processTextCompaction($code, $codewords);
|
||||
break;
|
||||
case 901:
|
||||
case 924:
|
||||
// Byte Compaction mode latch
|
||||
$this->processByteCompaction($code, $codewords);
|
||||
break;
|
||||
case 902:
|
||||
// Numeric Compaction mode latch
|
||||
$this->processNumericCompaction($code, $codewords);
|
||||
break;
|
||||
case 913:
|
||||
// Byte Compaction mode shift
|
||||
$codewords[] = \ord($code);
|
||||
break;
|
||||
}
|
||||
|
||||
if ($addmode) {
|
||||
// add the compaction mode codeword at the beginning
|
||||
\array_unshift($codewords, $mode);
|
||||
}
|
||||
|
||||
return $codewords;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
+215
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Sequence.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven\Sequence
|
||||
*
|
||||
* Sequence methods for PdfFourOneSeven Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Sequence extends \Com\Tecnick\Barcode\Type\Square
|
||||
{
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected function getRsFactors(int $ecl): array
|
||||
{
|
||||
$values = Data::RS_FACTORS[$ecl] ?? [];
|
||||
return \array_values($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $data
|
||||
*/
|
||||
protected function getArrayInt(array $data, int $index): int
|
||||
{
|
||||
return $data[$index] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{int, string}> $sequence_array
|
||||
*/
|
||||
protected function getLastSequenceMode(array $sequence_array): int
|
||||
{
|
||||
if ($sequence_array === []) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return $sequence_array[\count($sequence_array) - 1][0] ?? -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error correction level (0-8) to be used
|
||||
*
|
||||
* @param int $ecl Error correction level
|
||||
* @param int $numcw Number of data codewords
|
||||
*
|
||||
* @return int error correction level
|
||||
*/
|
||||
protected function getErrorCorrectionLevel(int $ecl, int $numcw): int
|
||||
{
|
||||
$maxecl = 8; // maximum error level
|
||||
$maxerrsize = 928 - $numcw; // available codewords for error
|
||||
while ($maxecl > 0 && $maxerrsize < (2 << $maxecl)) {
|
||||
--$maxecl;
|
||||
}
|
||||
|
||||
if ($ecl < 0 || $ecl > 8) {
|
||||
$ecl = $maxecl;
|
||||
$ecl = match (true) {
|
||||
$numcw < 41 => 2,
|
||||
$numcw < 161 => 3,
|
||||
$numcw < 321 => 4,
|
||||
$numcw < 864 => 5,
|
||||
default => $ecl,
|
||||
};
|
||||
}
|
||||
|
||||
return (int) \min($maxecl, $ecl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error correction codewords
|
||||
*
|
||||
* @param array<int, int> $codewords Array of codewords including Symbol Length Descriptor and pad
|
||||
* @param int $ecl Error correction level 0-8
|
||||
*
|
||||
* @return array<int, int> of error correction codewords
|
||||
*/
|
||||
protected function getErrorCorrection(array $codewords, int $ecl): array
|
||||
{
|
||||
// get error correction coefficients
|
||||
$ecc = $this->getRsFactors($ecl);
|
||||
// number of error correction factors
|
||||
$eclsize = \max(0, 2 << $ecl);
|
||||
// maximum index for RS_FACTORS[$ecl]
|
||||
$eclmaxid = $eclsize - 1;
|
||||
// initialize array of error correction codewords
|
||||
$ecw = \array_fill(0, $eclsize, 0);
|
||||
// for each data codeword
|
||||
foreach ($codewords as $codeword) {
|
||||
$tk1 = ($codeword + $this->getArrayInt($ecw, $eclmaxid)) % 929;
|
||||
for ($idx = $eclmaxid; $idx > 0; --$idx) {
|
||||
$tk2 = ($tk1 * $this->getArrayInt($ecc, $idx)) % 929;
|
||||
$tk3 = 929 - $tk2;
|
||||
$ecw[$idx] = (int) (($this->getArrayInt($ecw, $idx - 1) + $tk3) % 929);
|
||||
}
|
||||
|
||||
$tk2 = ($tk1 * $this->getArrayInt($ecc, 0)) % 929;
|
||||
$tk3 = 929 - $tk2;
|
||||
$ecw[0] = (int) ($tk3 % 929);
|
||||
}
|
||||
|
||||
foreach ($ecw as $idx => $err) {
|
||||
if ($err === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ecw[$idx] = (int) (929 - $err);
|
||||
}
|
||||
|
||||
return \array_reverse($ecw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a single sequence
|
||||
*
|
||||
* @param array<int, array{int, string}> $sequence_array Sequence to process
|
||||
* @param string $code Data to process
|
||||
* @param int $seq Current sequence
|
||||
* @param int $offset Current code offset
|
||||
*/
|
||||
protected function processSequence(array &$sequence_array, string $code, int $seq, int $offset): void
|
||||
{
|
||||
// extract text sequence before the number sequence
|
||||
$prevseq = \substr($code, $offset, $seq - $offset);
|
||||
$textseq = [];
|
||||
// get text sequences
|
||||
\preg_match_all('/([\x09\x0a\x0d\x20-\x7e]{5,})/', $prevseq, $textseq, PREG_OFFSET_CAPTURE);
|
||||
$textseq[1][] = ['', \strlen($prevseq)];
|
||||
$txtoffset = 0;
|
||||
foreach ($textseq[1] as $txtseq) {
|
||||
$txtSeqOffset = (int) $txtseq[1];
|
||||
$txtseqlen = \strlen($txtseq[0]);
|
||||
if ($txtSeqOffset > 0) {
|
||||
// extract byte sequence before the text sequence
|
||||
$prevtxtseq = \substr($prevseq, $txtoffset, $txtSeqOffset - $txtoffset);
|
||||
if (\strlen($prevtxtseq) > 0) {
|
||||
// add BYTE sequence
|
||||
$mode = 901;
|
||||
$mode = match (true) {
|
||||
\strlen($prevtxtseq) === 1 && $this->getLastSequenceMode($sequence_array) === 900 => 913,
|
||||
(\strlen($prevtxtseq) % 6) === 0 => 924,
|
||||
default => $mode,
|
||||
};
|
||||
|
||||
$sequence_array[] = [$mode, $prevtxtseq];
|
||||
}
|
||||
}
|
||||
|
||||
if ($txtseqlen > 0) {
|
||||
// add numeric sequence
|
||||
$sequence_array[] = [900, $txtseq[0]];
|
||||
}
|
||||
|
||||
$txtoffset = $txtSeqOffset + $txtseqlen;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of sequences from input
|
||||
*
|
||||
* @param string $code Data to process
|
||||
*
|
||||
* @return array<int, array{int, string}>
|
||||
*/
|
||||
protected function getInputSequences(string $code): array
|
||||
{
|
||||
$sequence_array = []; // array to be returned
|
||||
$numseq = [];
|
||||
// get numeric sequences
|
||||
\preg_match_all('/(\d{13,})/', $code, $numseq, PREG_OFFSET_CAPTURE);
|
||||
$numseq[1][] = ['', \strlen($code)];
|
||||
$offset = 0;
|
||||
foreach ($numseq[1] as $seq) {
|
||||
$seqlen = \strlen($seq[0]);
|
||||
$seqOffset = (int) $seq[1];
|
||||
if ($seqOffset > 0) {
|
||||
$this->processSequence($sequence_array, $code, $seqOffset, $offset);
|
||||
}
|
||||
|
||||
if ($seqlen > 0) {
|
||||
// add numeric sequence
|
||||
$sequence_array[] = [902, $seq[0]];
|
||||
}
|
||||
|
||||
$offset = $seqOffset + $seqlen;
|
||||
}
|
||||
|
||||
return $sequence_array;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* QrCode.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
use Com\Tecnick\Barcode\Type\Square\QrCode\ByteStream;
|
||||
use Com\Tecnick\Barcode\Type\Square\QrCode\Data;
|
||||
use Com\Tecnick\Barcode\Type\Square\QrCode\Encoder;
|
||||
use Com\Tecnick\Barcode\Type\Square\QrCode\QrEccLevel;
|
||||
use Com\Tecnick\Barcode\Type\Square\QrCode\QrEncodingMode;
|
||||
use Com\Tecnick\Barcode\Type\Square\QrCode\Split;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode
|
||||
*
|
||||
* QrCode Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class QrCode extends \Com\Tecnick\Barcode\Type\Square
|
||||
{
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'QRCODE';
|
||||
|
||||
/**
|
||||
* QR code version.
|
||||
* The Size of QRcode is defined as version. Version is an integer value from 1 to 40.
|
||||
* Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
|
||||
* So version 40 is 177*177 matrix.
|
||||
*/
|
||||
protected int $version = 0;
|
||||
|
||||
/**
|
||||
* Error correction level
|
||||
*/
|
||||
protected int $level = 0;
|
||||
|
||||
/**
|
||||
* Encoding mode
|
||||
*/
|
||||
protected int $hint = 2;
|
||||
|
||||
/**
|
||||
* Boolean flag, if false the input string will be converted to uppercase.
|
||||
*/
|
||||
protected bool $case_sensitive = true;
|
||||
|
||||
/**
|
||||
* If negative, checks all masks available,
|
||||
* otherwise the value indicates the number of masks to be checked,
|
||||
* mask ids are random.
|
||||
*/
|
||||
protected int $random_mask = -1;
|
||||
|
||||
/**
|
||||
* If true, estimates the best mask (spec default, but slower);
|
||||
* set to false for a significant performance boost but (probably) lower quality code.
|
||||
*/
|
||||
protected bool $best_mask = true;
|
||||
|
||||
/**
|
||||
* Default mask used when $this->best_mask === false
|
||||
*/
|
||||
protected int $default_mask = 2;
|
||||
|
||||
/**
|
||||
* ByteStream class object
|
||||
*/
|
||||
protected ByteStream $bsObj;
|
||||
|
||||
protected function getEccLevel(string $level): int
|
||||
{
|
||||
return match ($level) {
|
||||
'L' => 0,
|
||||
'M' => 1,
|
||||
'Q' => 2,
|
||||
'H' => 3,
|
||||
default => 0,
|
||||
};
|
||||
}
|
||||
|
||||
protected function getHintMode(string $mode): int
|
||||
{
|
||||
return match ($mode) {
|
||||
'NL' => -1,
|
||||
'NM' => 0,
|
||||
'AN' => 1,
|
||||
'8B' => 2,
|
||||
'KJ' => 3,
|
||||
'ST' => 4,
|
||||
default => 2,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Set extra (optional) parameters:
|
||||
* 1: LEVEL - error correction level: L, M, Q, H
|
||||
* 2: HINT - encoding mode: NL=variable, NM=numeric, AN=alphanumeric, 8B=8bit, KJ=KANJI, ST=STRUCTURED
|
||||
* 3: VERSION - integer value from 1 to 40
|
||||
* 4: CASE SENSITIVE - if 0 the input string will be converted to uppercase
|
||||
* 5: RANDOM MASK - false or number of masks to be checked
|
||||
* 6: BEST MASK - true to find the best mask (slow)
|
||||
* 7: DEFAULT MASK - mask to use when the best mask option is false
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
|
||||
* @SuppressWarnings("PHPMD.NPathComplexity")
|
||||
*/
|
||||
protected function setParameters(): void
|
||||
{
|
||||
parent::setParameters();
|
||||
|
||||
// level
|
||||
$eccLevel = QrEccLevel::fromLoose(\strval($this->params[0] ?? ''));
|
||||
$this->params[0] = $eccLevel->value;
|
||||
$this->level = $this->getEccLevel($eccLevel->value);
|
||||
|
||||
// hint
|
||||
$encMode = QrEncodingMode::fromLoose(\strval($this->params[1] ?? ''));
|
||||
$this->params[1] = $encMode->value;
|
||||
$this->hint = $this->getHintMode($encMode->value);
|
||||
|
||||
// version
|
||||
if (
|
||||
($this->params[2] ?? null) === null
|
||||
|| $this->params[2] < 0
|
||||
|| $this->params[2] > Data::QRSPEC_VERSION_MAX
|
||||
) {
|
||||
$this->params[2] = 0;
|
||||
}
|
||||
|
||||
$this->version = (int) $this->params[2];
|
||||
|
||||
// case sensitive
|
||||
if (($this->params[3] ?? null) === null) {
|
||||
$this->params[3] = 1;
|
||||
}
|
||||
|
||||
$this->case_sensitive = (bool) $this->params[3];
|
||||
|
||||
// random mask mode - number of masks to be checked
|
||||
if (($this->params[4] ?? null) !== null && $this->params[4] !== '' && (int) $this->params[4] !== 0) {
|
||||
$this->random_mask = (int) $this->params[4];
|
||||
}
|
||||
|
||||
// find best mask
|
||||
if (($this->params[5] ?? null) === null) {
|
||||
$this->params[5] = 1;
|
||||
}
|
||||
|
||||
$this->best_mask = (bool) $this->params[5];
|
||||
|
||||
// default mask
|
||||
if (($this->params[6] ?? null) === null) {
|
||||
$this->params[6] = 2;
|
||||
}
|
||||
|
||||
$this->default_mask = (int) $this->params[6];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bars array
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
* @throws \Random\RandomException in case of random generation error
|
||||
*/
|
||||
protected function setBars(): void
|
||||
{
|
||||
if (\strlen($this->code) === 0) {
|
||||
throw new BarcodeException('Empty input');
|
||||
}
|
||||
|
||||
$this->bsObj = new ByteStream($this->hint, $this->version, $this->level);
|
||||
// generate the qrcode
|
||||
$this->processBinarySequence($this->binarize($this->encodeString($this->code)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the frame in binary form
|
||||
*
|
||||
* @param array<int, string> $frame Array to binarize
|
||||
*
|
||||
* @return array<int, string> frame in binary form
|
||||
*/
|
||||
protected function binarize(array $frame): array
|
||||
{
|
||||
$len = \count($frame);
|
||||
// the frame is square (width = height)
|
||||
foreach ($frame as &$frameLine) {
|
||||
for ($idx = 0; $idx < $len; ++$idx) {
|
||||
$frameLine[$idx] = (\ord($frameLine[$idx]) & 1) !== 0 ? '1' : '0';
|
||||
}
|
||||
}
|
||||
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the input string
|
||||
*
|
||||
* @param string $data input string to encode
|
||||
*
|
||||
* @return array<int, string> Encoded data
|
||||
*
|
||||
* @throws BarcodeException in case of split/encoding errors
|
||||
* @throws \Random\RandomException in case of random generation error
|
||||
*/
|
||||
protected function encodeString(string $data): array
|
||||
{
|
||||
if (!$this->case_sensitive) {
|
||||
$data = $this->toUpper($data);
|
||||
}
|
||||
|
||||
$split = new Split($this->bsObj, $this->hint, $this->version);
|
||||
$datacode = $this->bsObj->getByteStream($split->getSplittedString($data));
|
||||
$this->version = $this->bsObj->version;
|
||||
$encoder = new Encoder($this->version, $this->level, $this->random_mask, $this->best_mask, $this->default_mask);
|
||||
return $encoder->encodeMask(-1, $datacode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert input string into upper case mode
|
||||
*
|
||||
* @param string $data Data
|
||||
*/
|
||||
protected function toUpper(string $data): string
|
||||
{
|
||||
$len = \strlen($data);
|
||||
$pos = 0;
|
||||
|
||||
while ($pos < $len) {
|
||||
$mode = $this->bsObj->getEncodingMode($data, $pos);
|
||||
if ($mode === $this->getHintMode('KJ')) {
|
||||
$pos += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (\ord($data[$pos]) >= \ord('a') && \ord($data[$pos]) <= \ord('z')) {
|
||||
$data[$pos] = \chr((\ord($data[$pos]) - 32) & 0xFF);
|
||||
}
|
||||
|
||||
++$pos;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* ByteStream.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\ByteStream
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* @phpstan-import-type Item from \Com\Tecnick\Barcode\Type\Square\QrCode\Estimate
|
||||
*/
|
||||
class ByteStream extends \Com\Tecnick\Barcode\Type\Square\QrCode\Encode
|
||||
{
|
||||
/**
|
||||
* @param array<int, int> $bstream
|
||||
*/
|
||||
protected function getBitValue(array $bstream, int $pos): int
|
||||
{
|
||||
return $bstream[$pos] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize
|
||||
*
|
||||
* @param int $hint Encoding mode
|
||||
* @param int $version Code version
|
||||
* @param int $level Error Correction Level
|
||||
*/
|
||||
public function __construct(int $hint, int $version, int $level)
|
||||
{
|
||||
$this->hint = $hint;
|
||||
$this->version = $version;
|
||||
$this->level = $level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack all bit streams padding bits into a byte array
|
||||
*
|
||||
* @param array<int, Item> $items Items
|
||||
*
|
||||
* @return array<int, int> padded merged byte stream
|
||||
*
|
||||
* @throws BarcodeException
|
||||
*/
|
||||
public function getByteStream(array $items): array
|
||||
{
|
||||
return $this->bitstreamToByte($this->appendPaddingBit($this->mergeBitStream($items)));
|
||||
}
|
||||
|
||||
/**
|
||||
* merge the bit stream
|
||||
*
|
||||
* @param array<int, Item> $items Items
|
||||
*
|
||||
* @return array<int, int> bitstream
|
||||
*
|
||||
* @throws BarcodeException
|
||||
*/
|
||||
protected function mergeBitStream(array $items): array
|
||||
{
|
||||
$items = $this->convertData($items);
|
||||
$bstream = [];
|
||||
foreach ($items as $item) {
|
||||
$bstream = $this->appendBitstream($bstream, $item['bstream']);
|
||||
}
|
||||
|
||||
return $bstream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append Padding Bit to bitstream
|
||||
*
|
||||
* @param array<int, int> $bstream Bit stream
|
||||
*
|
||||
* @return array<int, int> bitstream
|
||||
*/
|
||||
protected function appendPaddingBit(array $bstream): array
|
||||
{
|
||||
if ($bstream === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$bits = \count($bstream);
|
||||
$spec = new Spec();
|
||||
$maxwords = $spec->getDataLength($this->version, $this->level);
|
||||
$maxbits = $maxwords * 8;
|
||||
if ($maxbits === $bits) {
|
||||
return $bstream;
|
||||
}
|
||||
|
||||
if (($maxbits - $bits) < 5) {
|
||||
return $this->appendNum($bstream, $maxbits - $bits, 0);
|
||||
}
|
||||
|
||||
$bits += 4;
|
||||
$words = (int) (($bits + 7) / 8);
|
||||
$padding = [];
|
||||
$padding = $this->appendNum($padding, ($words * 8) - $bits + 4, 0);
|
||||
|
||||
$padlen = $maxwords - $words;
|
||||
if ($padlen > 0) {
|
||||
$padbuf = [];
|
||||
for ($idx = 0; $idx < $padlen; ++$idx) {
|
||||
$padbuf[$idx] = ($idx & 1) !== 0 ? 0x11 : 0xec;
|
||||
}
|
||||
|
||||
$padding = $this->appendBytes($padding, $padlen, $padbuf);
|
||||
}
|
||||
|
||||
return $this->appendBitstream($bstream, $padding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert bitstream to bytes
|
||||
*
|
||||
* @param array<int, int> $bstream Original bitstream
|
||||
*
|
||||
* @return array<int, int> of bytes
|
||||
*/
|
||||
protected function bitstreamToByte(array $bstream): array
|
||||
{
|
||||
$size = \count($bstream);
|
||||
if ($size === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$data = \array_fill(0, \max(0, (int) (($size + 7) / 8)), 0);
|
||||
$bytes = (int) ($size / 8);
|
||||
$pos = 0;
|
||||
for ($idx = 0; $idx < $bytes; ++$idx) {
|
||||
$val = 0;
|
||||
for ($jdx = 0; $jdx < 8; ++$jdx) {
|
||||
$val <<= 1;
|
||||
$val |= $this->getBitValue($bstream, $pos);
|
||||
++$pos;
|
||||
}
|
||||
|
||||
$data[$idx] = $val;
|
||||
}
|
||||
|
||||
if (($size & 7) !== 0) {
|
||||
$val = 0;
|
||||
for ($jdx = 0; $jdx < ($size & 7); ++$jdx) {
|
||||
$val <<= 1;
|
||||
$val |= $this->getBitValue($bstream, $pos);
|
||||
++$pos;
|
||||
}
|
||||
|
||||
$data[$bytes] = $val;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* convertData
|
||||
*
|
||||
* @param array<int, Item> $items Items
|
||||
*
|
||||
* @return array<int, Item>
|
||||
*
|
||||
* @throws BarcodeException
|
||||
*/
|
||||
protected function convertData(array $items): array
|
||||
{
|
||||
$ver = $this->estimateVersion($items, $this->level);
|
||||
if ($ver > $this->version) {
|
||||
$this->version = $ver;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
$cbs = $this->createBitStream($items);
|
||||
$items = $cbs[0];
|
||||
$bits = $cbs[1];
|
||||
if ($bits < 0) {
|
||||
throw new BarcodeException('Negative Bits value');
|
||||
}
|
||||
|
||||
$ver = $this->getMinimumVersion((int) (($bits + 7) / 8), $this->level);
|
||||
if ($ver > $this->version) {
|
||||
$this->version = $ver;
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create BitStream
|
||||
*
|
||||
* @param array<int, Item> $items Items
|
||||
*
|
||||
* @return array{
|
||||
* 0: array<int, Item>,
|
||||
* 1: int,
|
||||
* }
|
||||
*
|
||||
* @throws BarcodeException
|
||||
*/
|
||||
protected function createBitStream(array $items): array
|
||||
{
|
||||
$total = 0;
|
||||
foreach ($items as $key => $item) {
|
||||
$items[$key] = $this->encodeBitStream($item, $this->version);
|
||||
$bits = \count($items[$key]['bstream']);
|
||||
$total += $bits;
|
||||
}
|
||||
|
||||
return [$items, $total];
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode BitStream
|
||||
*
|
||||
* @param Item $inputitem Input item
|
||||
* @param int $version Code version
|
||||
*
|
||||
* @return Item
|
||||
*
|
||||
* @throws BarcodeException
|
||||
*/
|
||||
public function encodeBitStream(array $inputitem, int $version): array
|
||||
{
|
||||
$inputitem['bstream'] = [];
|
||||
$spec = new Spec();
|
||||
$words = $spec->maximumWords($inputitem['mode'], $version);
|
||||
|
||||
if ($inputitem['size'] <= $words) {
|
||||
return match ($inputitem['mode']) {
|
||||
$this->getEncModeValue('NM') => $this->encodeModeNum($inputitem, $version),
|
||||
$this->getEncModeValue('AN') => $this->encodeModeAn($inputitem, $version),
|
||||
$this->getEncModeValue('8B') => $this->encodeMode8($inputitem, $version),
|
||||
$this->getEncModeValue('KJ') => $this->encodeModeKanji($inputitem, $version),
|
||||
$this->getEncModeValue('ST') => $this->encodeModeStructure($inputitem),
|
||||
default => throw new BarcodeException('Invalid mode'),
|
||||
};
|
||||
}
|
||||
|
||||
$st1 = $this->newInputItem($inputitem['mode'], $words, $inputitem['data']);
|
||||
$st2 = $this->newInputItem(
|
||||
$inputitem['mode'],
|
||||
$inputitem['size'] - $words,
|
||||
\array_slice($inputitem['data'], $words),
|
||||
);
|
||||
$st1 = $this->encodeBitStream($st1, $version);
|
||||
$st2 = $this->encodeBitStream($st2, $version);
|
||||
$inputitem['bstream'] = [];
|
||||
$inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st1['bstream']);
|
||||
$inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st2['bstream']);
|
||||
|
||||
return $inputitem;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,576 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Data.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\Data
|
||||
*
|
||||
* Data for QrCode Barcode type class
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Data
|
||||
{
|
||||
/**
|
||||
* Maximum QR Code version.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const QRSPEC_VERSION_MAX = 40;
|
||||
|
||||
/**
|
||||
* Maximum matrix size for maximum version (version 40 is 177*177 matrix).
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const QRSPEC_WIDTH_MAX = 177;
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
/**
|
||||
* Encoding mode: variable.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_NL = -1;
|
||||
|
||||
/**
|
||||
* Encoding mode: numeric.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_NM = 0;
|
||||
|
||||
/**
|
||||
* Encoding mode: alphanumeric.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_AN = 1;
|
||||
|
||||
/**
|
||||
* Encoding mode: 8-bit byte.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_8B = 2;
|
||||
|
||||
/**
|
||||
* Encoding mode: kanji.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_KJ = 3;
|
||||
|
||||
/**
|
||||
* Encoding mode: structured append.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MODE_ST = 4;
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
/**
|
||||
* Matrix index to get width from CAPACITY array.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const QRCAP_WIDTH = 0;
|
||||
|
||||
/**
|
||||
* Matrix index to get number of words from CAPACITY array.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const QRCAP_WORDS = 1;
|
||||
|
||||
/**
|
||||
* Matrix index to get remainder from CAPACITY array.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const QRCAP_REMINDER = 2;
|
||||
|
||||
/**
|
||||
* Matrix index to get error correction level from CAPACITY array.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const QRCAP_EC = 3;
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Structure (currently unsupported)
|
||||
|
||||
/**
|
||||
* Number of header bits for structured mode
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const STRUCTURE_HEADER_BITS = 20;
|
||||
|
||||
/**
|
||||
* Max number of symbols for structured mode
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const MAX_STRUCTURED_SYMBOLS = 16;
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Masks
|
||||
|
||||
/**
|
||||
* Down point base value for case 1 mask pattern (concatenation of same color in a line or a column)
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const N1 = 3;
|
||||
|
||||
/**
|
||||
* Down point base value for case 2 mask pattern (module block of same color)
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const N2 = 3;
|
||||
|
||||
/**
|
||||
* Down point base value for case 3 mask pattern
|
||||
* (1:1:3:1:1 (dark:bright:dark:bright:dark) pattern in a line or a column)
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const N3 = 40;
|
||||
|
||||
/**
|
||||
* Down point base value for case 4 mask pattern (ration of dark modules in whole)
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const N4 = 10;
|
||||
|
||||
/**
|
||||
* Encoding modes (characters which can be encoded in QRcode)
|
||||
*
|
||||
* NL : variable
|
||||
* NM : Encoding mode numeric (0-9). 3 characters are encoded to 10bit length.
|
||||
* AN : Encoding mode alphanumeric (0-9A-Z $%*+-./:) 45characters. 2 characters are encoded to 11bit length.
|
||||
* 8B : Encoding mode 8bit byte data. In theory, 2953 characters or less can be stored in a QRcode.
|
||||
* KJ : Encoding mode KANJI. A KANJI character (multibyte character) is encoded to 13bit length.
|
||||
* ST : Encoding mode STRUCTURED
|
||||
*
|
||||
* @var array<string, int>
|
||||
*/
|
||||
public const ENC_MODES = [
|
||||
'NL' => self::MODE_NL,
|
||||
'NM' => self::MODE_NM,
|
||||
'AN' => self::MODE_AN,
|
||||
'8B' => self::MODE_8B,
|
||||
'KJ' => self::MODE_KJ,
|
||||
'ST' => self::MODE_ST,
|
||||
];
|
||||
|
||||
/**
|
||||
* Array of valid error correction levels
|
||||
* QRcode has a function of an error correcting for miss reading that white is black.
|
||||
* Error correcting is defined in 4 level as below.
|
||||
* L : About 7% or less errors can be corrected.
|
||||
* M : About 15% or less errors can be corrected.
|
||||
* Q : About 25% or less errors can be corrected.
|
||||
* H : About 30% or less errors can be corrected.
|
||||
*
|
||||
* @var array<string, int>
|
||||
*/
|
||||
public const ECC_LEVELS = [
|
||||
'L' => 0,
|
||||
'M' => 1,
|
||||
'Q' => 2,
|
||||
'H' => 3,
|
||||
];
|
||||
|
||||
/**
|
||||
* Alphabet-numeric conversion table.
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
public const AN_TABLE = [
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
|
||||
36,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
37,
|
||||
38,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
39,
|
||||
40,
|
||||
-1,
|
||||
41,
|
||||
42,
|
||||
43,
|
||||
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
44,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
|
||||
-1,
|
||||
10,
|
||||
11,
|
||||
12,
|
||||
13,
|
||||
14,
|
||||
15,
|
||||
16,
|
||||
17,
|
||||
18,
|
||||
19,
|
||||
20,
|
||||
21,
|
||||
22,
|
||||
23,
|
||||
24,
|
||||
|
||||
25,
|
||||
26,
|
||||
27,
|
||||
28,
|
||||
29,
|
||||
30,
|
||||
31,
|
||||
32,
|
||||
33,
|
||||
34,
|
||||
35,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
];
|
||||
|
||||
/**
|
||||
* Array Table of the capacity of symbols.
|
||||
* See Table 1 (pp.13) and Table 12-16 (pp.30-36), JIS X0510:2004.
|
||||
*
|
||||
* @var array<array{int, int, int, array{int, int, int, int}}>
|
||||
*/
|
||||
public const CAPACITY = [
|
||||
[0, 0, 0, [0, 0, 0, 0]],
|
||||
[21, 26, 0, [7, 10, 13, 17]],
|
||||
[25, 44, 7, [10, 16, 22, 28]],
|
||||
[29, 70, 7, [15, 26, 36, 44]],
|
||||
[33, 100, 7, [20, 36, 52, 64]],
|
||||
[37, 134, 7, [26, 48, 72, 88]],
|
||||
[41, 172, 7, [36, 64, 96, 112]],
|
||||
[45, 196, 0, [40, 72, 108, 130]],
|
||||
[49, 242, 0, [48, 88, 132, 156]],
|
||||
[53, 292, 0, [60, 110, 160, 192]],
|
||||
[57, 346, 0, [72, 130, 192, 224]],
|
||||
[61, 404, 0, [80, 150, 224, 264]],
|
||||
[65, 466, 0, [96, 176, 260, 308]],
|
||||
[69, 532, 0, [104, 198, 288, 352]],
|
||||
[73, 581, 3, [120, 216, 320, 384]],
|
||||
[77, 655, 3, [132, 240, 360, 432]],
|
||||
[81, 733, 3, [144, 280, 408, 480]],
|
||||
[85, 815, 3, [168, 308, 448, 532]],
|
||||
[89, 901, 3, [180, 338, 504, 588]],
|
||||
[93, 991, 3, [196, 364, 546, 650]],
|
||||
[97, 1085, 3, [224, 416, 600, 700]],
|
||||
[101, 1156, 4, [224, 442, 644, 750]],
|
||||
[105, 1258, 4, [252, 476, 690, 816]],
|
||||
[109, 1364, 4, [270, 504, 750, 900]],
|
||||
[113, 1474, 4, [300, 560, 810, 960]],
|
||||
[117, 1588, 4, [312, 588, 870, 1050]],
|
||||
[121, 1706, 4, [336, 644, 952, 1110]],
|
||||
[125, 1828, 4, [360, 700, 1020, 1200]],
|
||||
[129, 1921, 3, [390, 728, 1050, 1260]],
|
||||
[133, 2051, 3, [420, 784, 1140, 1350]],
|
||||
[137, 2185, 3, [450, 812, 1200, 1440]],
|
||||
[141, 2323, 3, [480, 868, 1290, 1530]],
|
||||
[145, 2465, 3, [510, 924, 1350, 1620]],
|
||||
[149, 2611, 3, [540, 980, 1440, 1710]],
|
||||
[153, 2761, 3, [570, 1036, 1530, 1800]],
|
||||
[157, 2876, 0, [570, 1064, 1590, 1890]],
|
||||
[161, 3034, 0, [600, 1120, 1680, 1980]],
|
||||
[165, 3196, 0, [630, 1204, 1770, 2100]],
|
||||
[169, 3362, 0, [660, 1260, 1860, 2220]],
|
||||
[173, 3532, 0, [720, 1316, 1950, 2310]],
|
||||
[177, 3706, 0, [750, 1372, 2040, 2430]],
|
||||
];
|
||||
|
||||
/**
|
||||
* Array Length indicator.
|
||||
*
|
||||
* @var array<array{int, int, int}>
|
||||
*/
|
||||
public const LEN_TABLE_BITS = [
|
||||
[10, 12, 14],
|
||||
[9, 11, 13],
|
||||
[8, 16, 16],
|
||||
[8, 10, 12],
|
||||
];
|
||||
|
||||
/**
|
||||
* Array Table of the error correction code (Reed-Solomon block).
|
||||
* See Table 12-16 (pp.30-36), JIS X0510:2004.
|
||||
*
|
||||
* @var array<array{array{int, int}, array{int, int}, array{int, int}, array{int, int}}>
|
||||
*/
|
||||
public const ECC_TABLE = [
|
||||
[[0, 0], [0, 0], [0, 0], [0, 0]],
|
||||
[[1, 0], [1, 0], [1, 0], [1, 0]],
|
||||
[[1, 0], [1, 0], [1, 0], [1, 0]],
|
||||
[[1, 0], [1, 0], [2, 0], [2, 0]],
|
||||
[[1, 0], [2, 0], [2, 0], [4, 0]],
|
||||
[[1, 0], [2, 0], [2, 2], [2, 2]],
|
||||
[[2, 0], [4, 0], [4, 0], [4, 0]],
|
||||
[[2, 0], [4, 0], [2, 4], [4, 1]],
|
||||
[[2, 0], [2, 2], [4, 2], [4, 2]],
|
||||
[[2, 0], [3, 2], [4, 4], [4, 4]],
|
||||
[[2, 2], [4, 1], [6, 2], [6, 2]],
|
||||
[[4, 0], [1, 4], [4, 4], [3, 8]],
|
||||
[[2, 2], [6, 2], [4, 6], [7, 4]],
|
||||
[[4, 0], [8, 1], [8, 4], [12, 4]],
|
||||
[[3, 1], [4, 5], [11, 5], [11, 5]],
|
||||
[[5, 1], [5, 5], [5, 7], [11, 7]],
|
||||
[[5, 1], [7, 3], [15, 2], [3, 13]],
|
||||
[[1, 5], [10, 1], [1, 15], [2, 17]],
|
||||
[[5, 1], [9, 4], [17, 1], [2, 19]],
|
||||
[[3, 4], [3, 11], [17, 4], [9, 16]],
|
||||
[[3, 5], [3, 13], [15, 5], [15, 10]],
|
||||
[[4, 4], [17, 0], [17, 6], [19, 6]],
|
||||
[[2, 7], [17, 0], [7, 16], [34, 0]],
|
||||
[[4, 5], [4, 14], [11, 14], [16, 14]],
|
||||
[[6, 4], [6, 14], [11, 16], [30, 2]],
|
||||
[[8, 4], [8, 13], [7, 22], [22, 13]],
|
||||
[[10, 2], [19, 4], [28, 6], [33, 4]],
|
||||
[[8, 4], [22, 3], [8, 26], [12, 28]],
|
||||
[[3, 10], [3, 23], [4, 31], [11, 31]],
|
||||
[[7, 7], [21, 7], [1, 37], [19, 26]],
|
||||
[[5, 10], [19, 10], [15, 25], [23, 25]],
|
||||
[[13, 3], [2, 29], [42, 1], [23, 28]],
|
||||
[[17, 0], [10, 23], [10, 35], [19, 35]],
|
||||
[[17, 1], [14, 21], [29, 19], [11, 46]],
|
||||
[[13, 6], [14, 23], [44, 7], [59, 1]],
|
||||
[[12, 7], [12, 26], [39, 14], [22, 41]],
|
||||
[[6, 14], [6, 34], [46, 10], [2, 64]],
|
||||
[[17, 4], [29, 14], [49, 10], [24, 46]],
|
||||
[[4, 18], [13, 32], [48, 14], [42, 32]],
|
||||
[[20, 4], [40, 7], [43, 22], [10, 67]],
|
||||
[[19, 6], [18, 31], [34, 34], [20, 61]],
|
||||
];
|
||||
|
||||
/**
|
||||
* Array Positions of alignment patterns.
|
||||
* This array includes only the second and the third position of the alignment patterns.
|
||||
* Rest of them can be calculated from the distance between them.
|
||||
* See Table 1 in Appendix E (pp.71) of JIS X0510:2004.
|
||||
*
|
||||
* @var array<array{int, int}>
|
||||
*/
|
||||
public const ALIGN_PATTERN = [
|
||||
[0, 0],
|
||||
[0, 0],
|
||||
[18, 0],
|
||||
[22, 0],
|
||||
[26, 0],
|
||||
[30, 0],
|
||||
[34, 0],
|
||||
[22, 38],
|
||||
[24, 42],
|
||||
[26, 46],
|
||||
[28, 50],
|
||||
[30, 54],
|
||||
[32, 58],
|
||||
[34, 62],
|
||||
[26, 46],
|
||||
[26, 48],
|
||||
[26, 50],
|
||||
[30, 54],
|
||||
[30, 56],
|
||||
[30, 58],
|
||||
[34, 62],
|
||||
[28, 50],
|
||||
[26, 50],
|
||||
[30, 54],
|
||||
[28, 54],
|
||||
[32, 58],
|
||||
[30, 58],
|
||||
[34, 62],
|
||||
[26, 50],
|
||||
[30, 54],
|
||||
[26, 52],
|
||||
[30, 56],
|
||||
[34, 60],
|
||||
[30, 58],
|
||||
[34, 62],
|
||||
[30, 54],
|
||||
[24, 50],
|
||||
[28, 54],
|
||||
[32, 58],
|
||||
[26, 54],
|
||||
[30, 58],
|
||||
];
|
||||
|
||||
/**
|
||||
* Array Version information pattern (BCH coded).
|
||||
* See Table 1 in Appendix D (pp.68) of JIS X0510:2004.
|
||||
* size: [QRSPEC_VERSION_MAX - 6]
|
||||
*
|
||||
* @var array<int>
|
||||
*/
|
||||
public const VERSION_PATTERN = [
|
||||
0x0_7c94,
|
||||
0x0_85bc,
|
||||
0x0_9a99,
|
||||
0x0_a4d3,
|
||||
0x0_bbf6,
|
||||
0x0_c762,
|
||||
0x0_d847,
|
||||
0x0_e60d,
|
||||
0x0_f928,
|
||||
0x1_0b78,
|
||||
0x1_145d,
|
||||
0x1_2a17,
|
||||
0x1_3532,
|
||||
0x1_49a6,
|
||||
0x1_5683,
|
||||
0x1_68c9,
|
||||
0x1_77ec,
|
||||
0x1_8ec4,
|
||||
0x1_91e1,
|
||||
0x1_afab,
|
||||
0x1_b08e,
|
||||
0x1_cc1a,
|
||||
0x1_d33f,
|
||||
0x1_ed75,
|
||||
0x1_f250,
|
||||
0x2_09d5,
|
||||
0x2_16f0,
|
||||
0x2_28ba,
|
||||
0x2_379f,
|
||||
0x2_4b0b,
|
||||
0x2_542e,
|
||||
0x2_6a64,
|
||||
0x2_7541,
|
||||
0x2_8c69,
|
||||
];
|
||||
|
||||
/**
|
||||
* Array Format information
|
||||
*
|
||||
* @var array<array{int, int, int, int, int, int, int, int}>
|
||||
*/
|
||||
public const FORMAT_INFO = [
|
||||
[0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976],
|
||||
[0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0],
|
||||
[0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed],
|
||||
[0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b],
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Encode.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\Encode
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Encode extends \Com\Tecnick\Barcode\Type\Square\QrCode\EncodingMode
|
||||
{
|
||||
protected function getEncModeValue(string $mode): int
|
||||
{
|
||||
return Data::ENC_MODES[$mode] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } $inputitem
|
||||
*/
|
||||
protected function getDataOrd(array $inputitem, int $idx): int
|
||||
{
|
||||
return \ord($inputitem['data'][$idx] ?? "\x00");
|
||||
}
|
||||
|
||||
/**
|
||||
* encode Mode Num
|
||||
*
|
||||
* @param array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } $inputitem input item
|
||||
* @param int $version Code version
|
||||
*
|
||||
* @return array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } input item
|
||||
*/
|
||||
protected function encodeModeNum(array $inputitem, int $version): array
|
||||
{
|
||||
$words = (int) ($inputitem['size'] / 3);
|
||||
$inputitem['bstream'] = [];
|
||||
$val = 0x1;
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
|
||||
$inputitem['bstream'] = $this->appendNum(
|
||||
$inputitem['bstream'],
|
||||
$this->getLengthIndicator($this->getEncModeValue('NM'), $version),
|
||||
$inputitem['size'],
|
||||
);
|
||||
for ($i = 0; $i < $words; ++$i) {
|
||||
$val = ($this->getDataOrd($inputitem, $i * 3) - \ord('0')) * 100;
|
||||
$val += ($this->getDataOrd($inputitem, ($i * 3) + 1) - \ord('0')) * 10;
|
||||
$val += $this->getDataOrd($inputitem, ($i * 3) + 2) - \ord('0');
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 10, $val);
|
||||
}
|
||||
|
||||
$remaining = $inputitem['size'] - ($words * 3);
|
||||
if ($remaining === 1) {
|
||||
$val = $this->getDataOrd($inputitem, $words * 3) - \ord('0');
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
|
||||
return $inputitem;
|
||||
}
|
||||
|
||||
if ($remaining === 2) {
|
||||
$val = ($this->getDataOrd($inputitem, $words * 3) - \ord('0')) * 10;
|
||||
$val += $this->getDataOrd($inputitem, ($words * 3) + 1) - \ord('0');
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 7, $val);
|
||||
}
|
||||
|
||||
return $inputitem;
|
||||
}
|
||||
|
||||
/**
|
||||
* encode Mode An
|
||||
*
|
||||
* @param array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } $inputitem input item
|
||||
* @param int $version Code version
|
||||
*
|
||||
* @return array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } input item
|
||||
*/
|
||||
protected function encodeModeAn(array $inputitem, int $version): array
|
||||
{
|
||||
$words = (int) ($inputitem['size'] / 2);
|
||||
$inputitem['bstream'] = [];
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x02);
|
||||
$inputitem['bstream'] = $this->appendNum(
|
||||
$inputitem['bstream'],
|
||||
$this->getLengthIndicator($this->getEncModeValue('AN'), $version),
|
||||
$inputitem['size'],
|
||||
);
|
||||
for ($idx = 0; $idx < $words; ++$idx) {
|
||||
$val = $this->lookAnTable($this->getDataOrd($inputitem, $idx * 2)) * 45;
|
||||
$val += $this->lookAnTable($this->getDataOrd($inputitem, ($idx * 2) + 1));
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 11, $val);
|
||||
}
|
||||
|
||||
if (($inputitem['size'] & 1) !== 0) {
|
||||
$val = $this->lookAnTable($this->getDataOrd($inputitem, $words * 2));
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 6, $val);
|
||||
}
|
||||
|
||||
return $inputitem;
|
||||
}
|
||||
|
||||
/**
|
||||
* encode Mode 8
|
||||
*
|
||||
* @param array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } $inputitem input item
|
||||
* @param int $version Code version
|
||||
*
|
||||
* @return array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } input item
|
||||
*/
|
||||
protected function encodeMode8(array $inputitem, int $version): array
|
||||
{
|
||||
$inputitem['bstream'] = [];
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x4);
|
||||
$inputitem['bstream'] = $this->appendNum(
|
||||
$inputitem['bstream'],
|
||||
$this->getLengthIndicator($this->getEncModeValue('8B'), $version),
|
||||
$inputitem['size'],
|
||||
);
|
||||
for ($idx = 0; $idx < $inputitem['size']; ++$idx) {
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, $this->getDataOrd($inputitem, $idx));
|
||||
}
|
||||
|
||||
return $inputitem;
|
||||
}
|
||||
|
||||
/**
|
||||
* encode Mode Kanji
|
||||
*
|
||||
* @param array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } $inputitem input item
|
||||
* @param int $version Code version
|
||||
*
|
||||
* @return array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } input item
|
||||
*/
|
||||
protected function encodeModeKanji(array $inputitem, int $version): array
|
||||
{
|
||||
$inputitem['bstream'] = [];
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x8);
|
||||
$inputitem['bstream'] = $this->appendNum(
|
||||
$inputitem['bstream'],
|
||||
$this->getLengthIndicator($this->getEncModeValue('KJ'), $version),
|
||||
(int) ($inputitem['size'] / 2),
|
||||
);
|
||||
for ($idx = 0; $idx < $inputitem['size']; $idx += 2) {
|
||||
$val = ($this->getDataOrd($inputitem, $idx) << 8) | $this->getDataOrd($inputitem, $idx + 1);
|
||||
$valOffset = 0xc140;
|
||||
if ($val <= 0x9ffc) {
|
||||
$valOffset = 0x8140;
|
||||
}
|
||||
|
||||
$val -= $valOffset;
|
||||
|
||||
$val = ($val & 0xff) + (($val >> 8) * 0xc0);
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 13, $val);
|
||||
}
|
||||
|
||||
return $inputitem;
|
||||
}
|
||||
|
||||
/**
|
||||
* encode Mode Structure
|
||||
*
|
||||
* @param array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } $inputitem input item
|
||||
*
|
||||
* @return array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* } input item
|
||||
*/
|
||||
protected function encodeModeStructure(array $inputitem): array
|
||||
{
|
||||
$inputitem['bstream'] = [];
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x03);
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $this->getDataOrd($inputitem, 1) - 1);
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $this->getDataOrd($inputitem, 0) - 1);
|
||||
$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, $this->getDataOrd($inputitem, 2));
|
||||
return $inputitem;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Encoder.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\Encoder
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Encoder extends \Com\Tecnick\Barcode\Type\Square\QrCode\Init
|
||||
{
|
||||
protected function getRsBlockDataLength(int $row): int
|
||||
{
|
||||
return $this->rsblocks[$row]['dataLength'] ?? 0;
|
||||
}
|
||||
|
||||
protected function getRsBlockCode(int $row, string $type, int $col): int
|
||||
{
|
||||
return $this->rsblocks[$row][$type][$col] ?? 0;
|
||||
}
|
||||
|
||||
protected function getFrameOrd(int $xpos, int $ypos): int
|
||||
{
|
||||
return \ord($this->frame[$ypos][$xpos] ?? "\x00");
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode mask
|
||||
*
|
||||
* @param int $maskNo Mask number (masking mode)
|
||||
* @param array<int, int> $datacode Data code to encode
|
||||
*
|
||||
* @return array<int, string> Encoded Mask
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
* @throws \Random\RandomException in case of random generation error
|
||||
*/
|
||||
public function encodeMask(int $maskNo, array $datacode): array
|
||||
{
|
||||
// initialize values
|
||||
$this->datacode = $datacode;
|
||||
$spec = $this->spc->getEccSpec($this->version, $this->level, [0, 0, 0, 0, 0]);
|
||||
$this->bv1 = $this->spc->rsBlockNum1($spec);
|
||||
$this->dataLength = $this->spc->rsDataLength($spec);
|
||||
$this->eccLength = \max(0, $this->spc->rsEccLength($spec));
|
||||
$this->ecccode = \array_fill(0, $this->eccLength, 0);
|
||||
$this->blocks = $this->spc->rsBlockNum($spec);
|
||||
$this->init($spec);
|
||||
$this->count = 0;
|
||||
$this->width = $this->spc->getWidth($this->version);
|
||||
$this->frame = $this->spc->createFrame($this->version);
|
||||
$this->xpos = $this->width - 1;
|
||||
$this->ypos = $this->width - 1;
|
||||
$this->dir = -1;
|
||||
$this->bit = -1;
|
||||
|
||||
// interleaved data and ecc codes
|
||||
for ($idx = 0; $idx < ($this->dataLength + $this->eccLength); ++$idx) {
|
||||
$code = $this->getCode();
|
||||
$bit = 0x80;
|
||||
for ($jdx = 0; $jdx < 8; ++$jdx) {
|
||||
$addr = $this->getNextPosition();
|
||||
$this->setFrameAt($addr, 0x02 | ($bit & $code) !== 0);
|
||||
$bit >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
// remainder bits
|
||||
$rbits = $this->spc->getRemainder($this->version);
|
||||
for ($idx = 0; $idx < $rbits; ++$idx) {
|
||||
$addr = $this->getNextPosition();
|
||||
$this->setFrameAt($addr, 0x02);
|
||||
}
|
||||
|
||||
// masking
|
||||
$this->runLength = \array_fill(0, \max(0, Data::QRSPEC_WIDTH_MAX + 1), 0);
|
||||
if ($maskNo >= 0) {
|
||||
return $this->makeMask($this->width, $this->frame, $maskNo, $this->level);
|
||||
}
|
||||
|
||||
if ($this->qr_find_best_mask) {
|
||||
return $this->mask($this->width, $this->frame, $this->level);
|
||||
}
|
||||
|
||||
return $this->makeMask($this->width, $this->frame, $this->qr_default_mask % 8, $this->level);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return Reed-Solomon block code
|
||||
*
|
||||
* @return int rsblocks
|
||||
*/
|
||||
protected function getCode(): int
|
||||
{
|
||||
if ($this->count < $this->dataLength) {
|
||||
$row = $this->count % $this->blocks;
|
||||
$col = (int) \floor($this->count / $this->blocks);
|
||||
if ($col >= $this->getRsBlockDataLength(0)) {
|
||||
$row += $this->bv1;
|
||||
}
|
||||
|
||||
$ret = $this->getRsBlockCode($row, 'data', $col);
|
||||
++$this->count;
|
||||
return $ret;
|
||||
}
|
||||
|
||||
if ($this->count < ($this->dataLength + $this->eccLength)) {
|
||||
$row = ($this->count - $this->dataLength) % $this->blocks;
|
||||
$col = (int) \floor(($this->count - $this->dataLength) / $this->blocks);
|
||||
$ret = $this->getRsBlockCode($row, 'ecc', $col);
|
||||
++$this->count;
|
||||
return $ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set frame value at specified position
|
||||
*
|
||||
* @param array{'x': int, 'y': int} $pos X,Y position
|
||||
* @param int $val Value of the character to set
|
||||
*/
|
||||
protected function setFrameAt(array $pos, int $val): void
|
||||
{
|
||||
$this->frame[$pos['y']][$pos['x']] = \chr($val & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the next frame position
|
||||
*
|
||||
* @return array{'x': int, 'y': int} of x,y coordinates
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function getNextPosition(): array
|
||||
{
|
||||
do {
|
||||
if ($this->bit === -1) {
|
||||
$this->bit = 0;
|
||||
return [
|
||||
'x' => $this->xpos,
|
||||
'y' => $this->ypos,
|
||||
];
|
||||
}
|
||||
|
||||
$xpos = $this->xpos;
|
||||
$ypos = $this->ypos;
|
||||
$wdt = $this->width;
|
||||
$this->getNextPositionB($xpos, $ypos, $wdt);
|
||||
if ($xpos < 0 || $ypos < 0) {
|
||||
throw new BarcodeException('Error getting next position');
|
||||
}
|
||||
|
||||
$this->xpos = $xpos;
|
||||
$this->ypos = $ypos;
|
||||
} while (($this->getFrameOrd($xpos, $ypos) & 0x80) !== 0);
|
||||
|
||||
return [
|
||||
'x' => $xpos,
|
||||
'y' => $ypos,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal cycle for getNextPosition
|
||||
*
|
||||
* @param int $xpos X position
|
||||
* @param int $ypos Y position
|
||||
* @param int $wdt Width
|
||||
*/
|
||||
protected function getNextPositionB(int &$xpos, int &$ypos, int $wdt): void
|
||||
{
|
||||
$wasBitZero = $this->bit === 0;
|
||||
if ($wasBitZero) {
|
||||
--$xpos;
|
||||
++$this->bit;
|
||||
}
|
||||
|
||||
if (!$wasBitZero) {
|
||||
++$xpos;
|
||||
$ypos += $this->dir;
|
||||
--$this->bit;
|
||||
}
|
||||
|
||||
if ($this->dir < 0) {
|
||||
if ($ypos < 0) {
|
||||
$ypos = 0;
|
||||
$xpos -= 2;
|
||||
$this->dir = 1;
|
||||
if ($xpos === 6) {
|
||||
--$xpos;
|
||||
$ypos = 9;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ($ypos === $wdt) {
|
||||
$ypos = $wdt - 1;
|
||||
$xpos -= 2;
|
||||
$this->dir = -1;
|
||||
if ($xpos === 6) {
|
||||
--$xpos;
|
||||
$ypos -= 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* EncodingMode.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\EncodingMode
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class EncodingMode extends \Com\Tecnick\Barcode\Type\Square\QrCode\InputItem
|
||||
{
|
||||
protected function getEncModeValue(string $mode): int
|
||||
{
|
||||
return Data::ENC_MODES[$mode] ?? 0;
|
||||
}
|
||||
|
||||
protected function getCharOrd(string $data, int $pos): int
|
||||
{
|
||||
return \ord($data[$pos] ?? "\x00");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $data
|
||||
*/
|
||||
protected function getByteValue(array $data, int $idx): int
|
||||
{
|
||||
return $data[$idx] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the encoding mode to use
|
||||
*
|
||||
* @param string $data Data
|
||||
* @param int $pos Position
|
||||
*
|
||||
* @return int mode
|
||||
*/
|
||||
public function getEncodingMode(string $data, int $pos): int
|
||||
{
|
||||
$dlen = \strlen($data);
|
||||
if ($pos < 0 || $pos >= $dlen) {
|
||||
return $this->getEncModeValue('NL');
|
||||
}
|
||||
|
||||
if ($this->isDigitAt($data, $pos)) {
|
||||
return $this->getEncModeValue('NM');
|
||||
}
|
||||
|
||||
if ($this->isAlphanumericAt($data, $pos)) {
|
||||
return $this->getEncModeValue('AN');
|
||||
}
|
||||
|
||||
return $this->getEncodingModeKj($data, $pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the encoding mode for KJ or 8B
|
||||
*
|
||||
* @param string $data Data
|
||||
* @param int $pos Position
|
||||
*
|
||||
* @return int mode
|
||||
*/
|
||||
protected function getEncodingModeKj(string $data, int $pos): int
|
||||
{
|
||||
if ($this->hint === $this->getEncModeValue('KJ') && ($pos + 1) < \strlen($data)) {
|
||||
$word = ($this->getCharOrd($data, $pos) << 8) | $this->getCharOrd($data, $pos + 1);
|
||||
if ($word >= 0x8140 && $word <= 0x9ffc || $word >= 0xe040 && $word <= 0xebbf) {
|
||||
return $this->getEncModeValue('KJ');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->getEncModeValue('8B');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the character at specified position is a number
|
||||
*
|
||||
* @param string $str Data
|
||||
* @param int $pos Character position
|
||||
*/
|
||||
public function isDigitAt(string $str, int $pos): bool
|
||||
{
|
||||
$slen = \strlen($str);
|
||||
if ($pos < 0 || $pos >= $slen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$ord = $this->getCharOrd($str, $pos);
|
||||
return $ord >= \ord('0') && $ord <= \ord('9');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the character at specified position is an alphanumeric character
|
||||
*
|
||||
* @param string $str Data
|
||||
* @param int $pos Character position
|
||||
*/
|
||||
public function isAlphanumericAt(string $str, int $pos): bool
|
||||
{
|
||||
$slen = \strlen($str);
|
||||
if ($pos < 0 || $pos >= $slen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->lookAnTable($this->getCharOrd($str, $pos)) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append one bitstream to another
|
||||
*
|
||||
* @param array<int, int> $bitstream Original bitstream
|
||||
* @param array<int, int> $append Bitstream to append
|
||||
*
|
||||
* @return array<int, int> bitstream
|
||||
*/
|
||||
protected function appendBitstream(array $bitstream, array $append): array
|
||||
{
|
||||
if (\count($append) === 0) {
|
||||
return $bitstream;
|
||||
}
|
||||
|
||||
if (\count($bitstream) === 0) {
|
||||
return $append;
|
||||
}
|
||||
|
||||
return \array_values(\array_merge($bitstream, $append));
|
||||
}
|
||||
|
||||
/**
|
||||
* Append one bitstream created from number to another
|
||||
*
|
||||
* @param array<int, int> $bitstream Original bitstream
|
||||
* @param int $bits Number of bits
|
||||
* @param int $num Number
|
||||
*
|
||||
* @return array<int, int> bitstream
|
||||
*/
|
||||
protected function appendNum(array $bitstream, int $bits, int $num): array
|
||||
{
|
||||
if ($bits === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->appendBitstream($bitstream, $this->newFromNum($bits, $num));
|
||||
}
|
||||
|
||||
/**
|
||||
* Append one bitstream created from bytes to another
|
||||
*
|
||||
* @param array<int, int> $bitstream Original bitstream
|
||||
* @param int $size Size
|
||||
* @param array<int, int> $data Bytes
|
||||
*
|
||||
* @return array<int, int> bitstream
|
||||
*/
|
||||
protected function appendBytes(array $bitstream, int $size, array $data): array
|
||||
{
|
||||
if ($size === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->appendBitstream($bitstream, $this->newFromBytes($size, $data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return new bitstream from number
|
||||
*
|
||||
* @param int $bits Number of bits
|
||||
* @param int $num Number
|
||||
*
|
||||
* @return array<int, int> bitstream
|
||||
*/
|
||||
protected function newFromNum(int $bits, int $num): array
|
||||
{
|
||||
$bstream = $this->allocate($bits);
|
||||
$mask = 1 << ($bits - 1);
|
||||
for ($idx = 0; $idx < $bits; ++$idx) {
|
||||
$bstream[$idx] = ($num & $mask) !== 0 ? 1 : 0;
|
||||
|
||||
$mask >>= 1;
|
||||
}
|
||||
|
||||
return $bstream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return new bitstream from bytes
|
||||
*
|
||||
* @param int $size Size
|
||||
* @param array<int, int> $data Bytes
|
||||
*
|
||||
* @return array<int, int> bitstream
|
||||
*/
|
||||
protected function newFromBytes(int $size, array $data): array
|
||||
{
|
||||
$bstream = $this->allocate($size * 8);
|
||||
$pval = 0;
|
||||
for ($idx = 0; $idx < $size; ++$idx) {
|
||||
$mask = 0x80;
|
||||
for ($jdx = 0; $jdx < 8; ++$jdx) {
|
||||
$bstream[$pval] = ($this->getByteValue($data, $idx) & $mask) !== 0 ? 1 : 0;
|
||||
|
||||
++$pval;
|
||||
$mask >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $bstream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array with zeros
|
||||
*
|
||||
* @param int $setLength Array size
|
||||
*
|
||||
* @return array<int, int> array
|
||||
*/
|
||||
protected function allocate(int $setLength): array
|
||||
{
|
||||
return \array_fill(0, \max(0, $setLength), 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Estimate.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\Estimate
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* @phpstan-type Item array{
|
||||
* 'mode': int,
|
||||
* 'size': int,
|
||||
* 'data': array<int, string>,
|
||||
* 'bstream': array<int, int>,
|
||||
* }
|
||||
*/
|
||||
abstract class Estimate
|
||||
{
|
||||
protected function getEncModeValue(string $mode): int
|
||||
{
|
||||
return match ($mode) {
|
||||
'NL' => Data::MODE_NL,
|
||||
'NM' => Data::MODE_NM,
|
||||
'AN' => Data::MODE_AN,
|
||||
'8B' => Data::MODE_8B,
|
||||
'KJ' => Data::MODE_KJ,
|
||||
'ST' => Data::MODE_ST,
|
||||
default => 0,
|
||||
};
|
||||
}
|
||||
|
||||
protected function getLenTableBitsValue(int $mode, int $len): int
|
||||
{
|
||||
return Data::LEN_TABLE_BITS[$mode][$len] ?? 0;
|
||||
}
|
||||
|
||||
protected function getCapacityWordsValue(int $version): int
|
||||
{
|
||||
$capacity = Data::CAPACITY[$version] ?? null;
|
||||
if (!\is_array($capacity)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$words = $capacity[Data::QRCAP_WORDS] ?? 0;
|
||||
return \is_int($words) ? $words : 0;
|
||||
}
|
||||
|
||||
protected function getCapacityEcValue(int $version, int $level): int
|
||||
{
|
||||
$capacity = Data::CAPACITY[$version] ?? null;
|
||||
if (!\is_array($capacity)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$ecLevel = $capacity[Data::QRCAP_EC] ?? [];
|
||||
if (!\is_array($ecLevel)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $ecLevel[$level] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encoding mode
|
||||
*/
|
||||
protected int $hint = 2;
|
||||
|
||||
/**
|
||||
* QR code version.
|
||||
* The Size of QRcode is defined as version. Version is an integer value from 1 to 40.
|
||||
* Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
|
||||
* So version 40 is 177*177 matrix.
|
||||
*/
|
||||
public int $version = 0;
|
||||
|
||||
/**
|
||||
* Error correction level
|
||||
*/
|
||||
protected int $level = 0;
|
||||
|
||||
/**
|
||||
* Return the size of length indicator for the mode and version
|
||||
*
|
||||
* @param int $mode Encoding mode
|
||||
* @param int $version Version
|
||||
*
|
||||
* @return int the size of the appropriate length indicator (bits).
|
||||
*/
|
||||
public function getLengthIndicator(int $mode, int $version): int
|
||||
{
|
||||
$modeSt = $this->getEncModeValue('ST');
|
||||
$modeNl = $this->getEncModeValue('NL');
|
||||
if ($mode === $modeSt || $mode < $modeNl || $mode > $modeSt) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$len = match (true) {
|
||||
$version <= 9 => 0,
|
||||
$version <= 26 => 1,
|
||||
default => 2,
|
||||
};
|
||||
|
||||
return $this->getLenTableBitsValue($mode, $len);
|
||||
}
|
||||
|
||||
/**
|
||||
* estimateBitsModeNum
|
||||
*
|
||||
* @return int number of bits
|
||||
*/
|
||||
public function estimateBitsModeNum(int $size): int
|
||||
{
|
||||
$wdt = (int) ($size / 3);
|
||||
$bits = $wdt * 10;
|
||||
match ($size - ($wdt * 3)) {
|
||||
1 => $bits += 4,
|
||||
2 => $bits += 7,
|
||||
default => $bits,
|
||||
};
|
||||
return $bits;
|
||||
}
|
||||
|
||||
/**
|
||||
* estimateBitsModeAn
|
||||
*
|
||||
* @return int number of bits
|
||||
*/
|
||||
public function estimateBitsModeAn(int $size): int
|
||||
{
|
||||
$bits = (int) ($size * 5.5); // (size / 2 ) * 11
|
||||
if (($size & 1) !== 0) {
|
||||
$bits += 6;
|
||||
}
|
||||
|
||||
return $bits;
|
||||
}
|
||||
|
||||
/**
|
||||
* estimateBitsMode8
|
||||
*
|
||||
* @return int number of bits
|
||||
*/
|
||||
public function estimateBitsMode8(int $size): int
|
||||
{
|
||||
return $size * 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* estimateBitsModeKanji
|
||||
*
|
||||
* @return int number of bits
|
||||
*/
|
||||
public function estimateBitsModeKanji(int $size): int
|
||||
{
|
||||
return (int) ($size * 6.5); // (size / 2 ) * 13
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate version
|
||||
*
|
||||
* @param array<int, Item> $items Items
|
||||
* @param int $level Error correction level
|
||||
*
|
||||
* @return int version
|
||||
*
|
||||
* @throws BarcodeException
|
||||
*/
|
||||
public function estimateVersion(array $items, int $level): int
|
||||
{
|
||||
$version = 0;
|
||||
$prev = 0;
|
||||
do {
|
||||
$prev = $version;
|
||||
$bits = $this->estimateBitStreamSize($items, $prev);
|
||||
$version = $this->getMinimumVersion((int) (($bits + 7) / 8), $level);
|
||||
if ($version < 0) {
|
||||
return -1;
|
||||
}
|
||||
} while ($version > $prev);
|
||||
|
||||
return $version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a version number that satisfies the input code length.
|
||||
*
|
||||
* @param int $size Input code length (bytes)
|
||||
* @param int $level Error correction level
|
||||
*
|
||||
* @return int version number
|
||||
*
|
||||
* @throws BarcodeException
|
||||
*/
|
||||
protected function getMinimumVersion(int $size, int $level): int
|
||||
{
|
||||
for ($idx = 1; $idx <= Data::QRSPEC_VERSION_MAX; ++$idx) {
|
||||
$words = $this->getCapacityWordsValue($idx) - $this->getCapacityEcValue($idx, $level);
|
||||
if ($words >= $size) {
|
||||
return $idx;
|
||||
}
|
||||
}
|
||||
|
||||
throw new BarcodeException(
|
||||
'The size of input data is greater than Data::QR capacity, try to lower the error correction mode',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* estimateBitStreamSize
|
||||
*
|
||||
* @param array<int, Item> $items Items
|
||||
* @param int $version Code version
|
||||
*
|
||||
* @return int bits
|
||||
*/
|
||||
protected function estimateBitStreamSize(array $items, int $version): int
|
||||
{
|
||||
$bits = 0;
|
||||
if ($version === 0) {
|
||||
$version = 1;
|
||||
}
|
||||
|
||||
foreach ($items as $item) {
|
||||
switch ($item['mode']) {
|
||||
case $this->getEncModeValue('NM'):
|
||||
$bits = $this->estimateBitsModeNum($item['size']);
|
||||
break;
|
||||
case $this->getEncModeValue('AN'):
|
||||
$bits = $this->estimateBitsModeAn($item['size']);
|
||||
break;
|
||||
case $this->getEncModeValue('8B'):
|
||||
$bits = $this->estimateBitsMode8($item['size']);
|
||||
break;
|
||||
case $this->getEncModeValue('KJ'):
|
||||
$bits = $this->estimateBitsModeKanji($item['size']);
|
||||
break;
|
||||
case $this->getEncModeValue('ST'):
|
||||
return Data::STRUCTURE_HEADER_BITS;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
$len = $this->getLengthIndicator($item['mode'], $version);
|
||||
$mod = 1 << $len;
|
||||
$num = (int) (($item['size'] + $mod - 1) / $mod);
|
||||
$bits += $num * (4 + $len);
|
||||
}
|
||||
|
||||
return $bits;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,480 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Init.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\Init
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* @phpstan-type RSItem array{
|
||||
* 'alpha_to': array<int, int>,
|
||||
* 'fcr': int,
|
||||
* 'genpoly': array<int, int>,
|
||||
* 'gfpoly': int,
|
||||
* 'index_of': array<int, int>,
|
||||
* 'iprim': int,
|
||||
* 'mm': int,
|
||||
* 'nn': int,
|
||||
* 'nroots': int,
|
||||
* 'pad': int,
|
||||
* 'prim': int,
|
||||
* }
|
||||
*
|
||||
* @phpstan-type RSblock array{
|
||||
* 'data': array<int, int>,
|
||||
* 'dataLength': int,
|
||||
* 'ecc': array<int, int>,
|
||||
* 'eccLength': int,
|
||||
* }
|
||||
*/
|
||||
abstract class Init extends \Com\Tecnick\Barcode\Type\Square\QrCode\Mask
|
||||
{
|
||||
/**
|
||||
* Data code
|
||||
*
|
||||
* @var array<int, int>
|
||||
*/
|
||||
protected array $datacode = [];
|
||||
|
||||
/**
|
||||
* Error correction code
|
||||
*
|
||||
* @var array<int, int>
|
||||
*/
|
||||
protected array $ecccode = [];
|
||||
|
||||
/**
|
||||
* Blocks
|
||||
*/
|
||||
protected int $blocks;
|
||||
|
||||
/**
|
||||
* Reed-Solomon blocks
|
||||
*
|
||||
* @var array<int, RSblock>
|
||||
*/
|
||||
protected array $rsblocks = []; //of RSblock
|
||||
|
||||
/**
|
||||
* Counter
|
||||
*/
|
||||
protected int $count;
|
||||
|
||||
/**
|
||||
* Data length
|
||||
*/
|
||||
protected int $dataLength;
|
||||
|
||||
/**
|
||||
* Error correction length
|
||||
*/
|
||||
protected int $eccLength;
|
||||
|
||||
/**
|
||||
* Value bv1
|
||||
*/
|
||||
protected int $bv1;
|
||||
|
||||
/**
|
||||
* Width.
|
||||
*/
|
||||
protected int $width;
|
||||
|
||||
/**
|
||||
* Frame
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected array $frame = [];
|
||||
|
||||
/**
|
||||
* Horizontal bit position
|
||||
*/
|
||||
protected int $xpos;
|
||||
|
||||
/**
|
||||
* Vertical bit position
|
||||
*/
|
||||
protected int $ypos;
|
||||
|
||||
/**
|
||||
* Direction
|
||||
*/
|
||||
protected int $dir;
|
||||
|
||||
/**
|
||||
* Single bit value
|
||||
*/
|
||||
protected int $bit;
|
||||
|
||||
/**
|
||||
* Reed-Solomon items
|
||||
*
|
||||
* @var array<int, RSItem>
|
||||
*/
|
||||
protected array $rsitems = [];
|
||||
|
||||
/**
|
||||
* Initialize code
|
||||
*
|
||||
* @param array<int, int> $spec Array of ECC specification
|
||||
*
|
||||
* @throws BarcodeException in case RS initialization fails
|
||||
*/
|
||||
protected function init(array $spec): void
|
||||
{
|
||||
$dlv = $this->spc->rsDataCodes1($spec);
|
||||
$elv = $this->spc->rsEccCodes1($spec);
|
||||
$rsv = $this->initRs(8, 0x11d, 0, 1, $elv, 255 - $dlv - $elv);
|
||||
$blockNo = 0;
|
||||
$dataPos = 0;
|
||||
$eccPos = 0;
|
||||
$ecc = [];
|
||||
$endfor = $this->spc->rsBlockNum1($spec);
|
||||
$this->initLoop($endfor, $dlv, $elv, $rsv, $eccPos, $blockNo, $dataPos, $ecc);
|
||||
if ($this->spc->rsBlockNum2($spec) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$dlv = $this->spc->rsDataCodes2($spec);
|
||||
$elv = $this->spc->rsEccCodes2($spec);
|
||||
$rsv = $this->initRs(8, 0x11d, 0, 1, $elv, 255 - $dlv - $elv);
|
||||
$endfor = $this->spc->rsBlockNum2($spec);
|
||||
$this->initLoop($endfor, $dlv, $elv, $rsv, $eccPos, $blockNo, $dataPos, $ecc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal loop for init
|
||||
*
|
||||
* @param int $endfor End for
|
||||
* @param int $dlv Data length value
|
||||
* @param int $elv Error correction length value
|
||||
* @param RSItem $rsv Reed-Solomon values
|
||||
* @param int $eccPos Error correction code position
|
||||
* @param int $blockNo Block number
|
||||
* @param int $dataPos Data position
|
||||
* @param array<int, int> $ecc Error correction code
|
||||
*/
|
||||
protected function initLoop(
|
||||
int $endfor,
|
||||
int $dlv,
|
||||
int $elv,
|
||||
array $rsv,
|
||||
int &$eccPos,
|
||||
int &$blockNo,
|
||||
int &$dataPos,
|
||||
array &$ecc,
|
||||
): void {
|
||||
for ($idx = 0; $idx < $endfor; ++$idx) {
|
||||
$data = \array_slice($this->datacode, $dataPos);
|
||||
$ecc = \array_slice($this->ecccode, $eccPos);
|
||||
$ecc = $this->encodeRsChar($rsv, $data, $ecc);
|
||||
$this->rsblocks[$blockNo] = [
|
||||
'data' => $data,
|
||||
'dataLength' => $dlv,
|
||||
'ecc' => $ecc,
|
||||
'eccLength' => $elv,
|
||||
];
|
||||
$this->ecccode = \array_merge(\array_slice($this->ecccode, 0, $eccPos), $ecc);
|
||||
$dataPos += $dlv;
|
||||
$eccPos += $elv;
|
||||
++$blockNo;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a Reed-Solomon codec and add it to existing rsitems
|
||||
*
|
||||
* @param int $symsize Symbol size, bits
|
||||
* @param int $gfpoly Field generator polynomial coefficients
|
||||
* @param int $fcr First root of RS code generator polynomial, index form
|
||||
* @param int $prim Primitive element to generate polynomial roots
|
||||
* @param int $nroots RS code generator polynomial degree (number of roots)
|
||||
* @param int $pad Padding bytes at front of shortened block
|
||||
*
|
||||
* @return RSItem Array of RS values:
|
||||
* mm = Bits per symbol;
|
||||
* nn = Symbols per block;
|
||||
* alpha_to = log lookup table array;
|
||||
* index_of = Antilog lookup table array;
|
||||
* genpoly = Generator polynomial array;
|
||||
* nroots = Number of generator;
|
||||
* roots = number of parity symbols;
|
||||
* fcr = First consecutive root, index form;
|
||||
* prim = Primitive element, index form;
|
||||
* iprim = prim-th root of 1, index form;
|
||||
* pad = Padding bytes in shortened block;
|
||||
* gfpoly.
|
||||
*
|
||||
* @throws BarcodeException in case RS initialization fails
|
||||
*/
|
||||
protected function initRs(int $symsize, int $gfpoly, int $fcr, int $prim, int $nroots, int $pad): array
|
||||
{
|
||||
foreach ($this->rsitems as $rsv) {
|
||||
if ($rsv['pad'] !== $pad) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($rsv['nroots'] !== $nroots) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($rsv['mm'] !== $symsize) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($rsv['gfpoly'] !== $gfpoly) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($rsv['fcr'] !== $fcr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($rsv['prim'] !== $prim) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $rsv;
|
||||
}
|
||||
|
||||
$rsv = $this->initRsChar($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
|
||||
\array_unshift($this->rsitems, $rsv);
|
||||
return $rsv;
|
||||
}
|
||||
|
||||
/**
|
||||
* modnn
|
||||
*
|
||||
* @param RSItem $rsv RS values
|
||||
* @param int $xpos X position
|
||||
*
|
||||
* @return int X position
|
||||
*/
|
||||
protected function modnn(array $rsv, int $xpos): int
|
||||
{
|
||||
while ($xpos >= $rsv['nn']) {
|
||||
$xpos -= $rsv['nn'];
|
||||
$xpos = ($xpos >> $rsv['mm']) + ($xpos & $rsv['nn']);
|
||||
}
|
||||
|
||||
return $xpos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the params for the initRsChar and throws an exception in case of error.
|
||||
*
|
||||
* @param int $symsize Symbol size, bits
|
||||
* @param int $fcr First root of RS code generator polynomial, index form
|
||||
* @param int $prim Primitive element to generate polynomial roots
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function checkRsCharParamsA(int $symsize, int $fcr, int $prim): void
|
||||
{
|
||||
$shfsymsize = 1 << $symsize;
|
||||
if ($symsize < 0 || $symsize > 8 || $fcr < 0 || $fcr >= $shfsymsize || $prim <= 0 || $prim >= $shfsymsize) {
|
||||
throw new BarcodeException('Invalid parameters');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the params for the initRsChar and throws an exception in case of error.
|
||||
*
|
||||
* @param int $symsize Symbol size, bits
|
||||
* @param int $nroots RS code generator polynomial degree (number of roots)
|
||||
* @param int $pad Padding bytes at front of shortened block
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function checkRsCharParamsB(int $symsize, int $nroots, int $pad): void
|
||||
{
|
||||
$shfsymsize = 1 << $symsize;
|
||||
if ($nroots < 0 || $nroots >= $shfsymsize || $pad < 0 || $pad >= ($shfsymsize - 1 - $nroots)) {
|
||||
throw new BarcodeException('Invalid parameters');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a Reed-Solomon codec and returns an array of values.
|
||||
*
|
||||
* @param int $symsize Symbol size, bits
|
||||
* @param int $gfpoly Field generator polynomial coefficients
|
||||
* @param int $fcr First root of RS code generator polynomial, index form
|
||||
* @param int $prim Primitive element to generate polynomial roots
|
||||
* @param int $nroots RS code generator polynomial degree (number of roots)
|
||||
* @param int $pad Padding bytes at front of shortened block
|
||||
*
|
||||
* @return RSItem Array of RS values:
|
||||
* mm = Bits per symbol;
|
||||
* nn = Symbols per block;
|
||||
* alpha_to = log lookup table array;
|
||||
* index_of = Antilog lookup table array;
|
||||
* genpoly = Generator polynomial array;
|
||||
* nroots = Number of generator;
|
||||
* roots = number of parity symbols;
|
||||
* fcr = First consecutive root, index form;
|
||||
* prim = Primitive element, index form;
|
||||
* iprim = prim-th root of 1, index form;
|
||||
* pad = Padding bytes in shortened block;
|
||||
* gfpoly.
|
||||
*
|
||||
* @throws BarcodeException in case the field generator polynomial is invalid
|
||||
*/
|
||||
protected function initRsChar(int $symsize, int $gfpoly, int $fcr, int $prim, int $nroots, int $pad): array
|
||||
{
|
||||
$this->checkRsCharParamsA($symsize, $fcr, $prim);
|
||||
$this->checkRsCharParamsB($symsize, $nroots, $pad);
|
||||
$nn = (1 << $symsize) - 1;
|
||||
$alphaTo = \array_fill(0, \max(0, $nn + 1), 0);
|
||||
$indexOf = \array_fill(0, \max(0, $nn + 1), 0);
|
||||
// Generate Galois field lookup tables
|
||||
$indexOf[0] = $nn; // \log(zero) = -inf
|
||||
$alphaTo[$nn] = 0; // alpha**-inf = 0
|
||||
$srv = 1;
|
||||
for ($idx = 0; $idx < $nn; ++$idx) {
|
||||
$indexOf[$srv] = $idx;
|
||||
$alphaTo[$idx] = $srv;
|
||||
$srv <<= 1;
|
||||
if (($srv & (1 << $symsize)) !== 0) {
|
||||
$srv ^= $gfpoly;
|
||||
}
|
||||
|
||||
$srv &= $nn;
|
||||
}
|
||||
|
||||
if ($srv !== 1) {
|
||||
throw new BarcodeException('field generator polynomial is not primitive!');
|
||||
}
|
||||
|
||||
// form RS code generator polynomial from its roots
|
||||
$genpoly = \array_fill(0, \max(0, $nroots + 1), 0);
|
||||
// find prim-th root of 1, used in decoding
|
||||
|
||||
$iprim = 1;
|
||||
while (($iprim % $prim) !== 0) {
|
||||
$iprim += $nn;
|
||||
}
|
||||
|
||||
$iprim = (int) ($iprim / $prim);
|
||||
$genpoly[0] = 1;
|
||||
for ($idx = 0, $root = $fcr * $prim; $idx < $nroots; ++$idx, $root += $prim) {
|
||||
$genpoly[$idx + 1] = 1;
|
||||
// multiply rs->genpoly[] by @**(root + x)
|
||||
for ($jdx = $idx; $jdx > 0; --$jdx) {
|
||||
$genpolyVal = $genpoly[$jdx] ?? 0;
|
||||
if ($genpolyVal !== 0) {
|
||||
$prev = $genpoly[$jdx - 1] ?? 0;
|
||||
$polyIndex = 0;
|
||||
if ($genpolyVal >= 0) {
|
||||
$polyIndex = $indexOf[$genpolyVal] ?? 0;
|
||||
}
|
||||
$alphaIndex = $this->modnnRaw($nn, $symsize, $polyIndex + $root);
|
||||
$genpoly[$jdx] = $prev ^ ($alphaTo[$alphaIndex] ?? 0);
|
||||
continue;
|
||||
}
|
||||
|
||||
$genpoly[$jdx] = $genpoly[$jdx - 1] ?? 0;
|
||||
}
|
||||
|
||||
// rs->genpoly[0] can never be zero
|
||||
$alphaIndex = $this->modnnRaw($nn, $symsize, ($indexOf[$genpoly[0] ?? 0] ?? 0) + $root);
|
||||
$genpoly[0] = $alphaTo[$alphaIndex] ?? 0;
|
||||
}
|
||||
|
||||
// convert rs->genpoly[] to index form for quicker encoding
|
||||
for ($idx = 0; $idx <= $nroots; ++$idx) {
|
||||
$genpoly[$idx] = $indexOf[$genpoly[$idx] ?? 0] ?? 0;
|
||||
}
|
||||
|
||||
return [
|
||||
'alpha_to' => $alphaTo,
|
||||
'fcr' => $fcr,
|
||||
'genpoly' => $genpoly,
|
||||
'gfpoly' => $gfpoly,
|
||||
'index_of' => $indexOf,
|
||||
'iprim' => $iprim,
|
||||
'mm' => $symsize,
|
||||
'nn' => $nn,
|
||||
'nroots' => $nroots,
|
||||
'pad' => $pad,
|
||||
'prim' => $prim,
|
||||
];
|
||||
}
|
||||
|
||||
protected function modnnRaw(int $nn, int $mm, int $xpos): int
|
||||
{
|
||||
while ($xpos >= $nn) {
|
||||
$xpos -= $nn;
|
||||
$xpos = ($xpos >> $mm) + ($xpos & $nn);
|
||||
}
|
||||
|
||||
return $xpos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a Reed-Solomon codec and returns the parity array
|
||||
*
|
||||
* @param RSItem $rsv RS values
|
||||
* @param array<int, int> $data Data
|
||||
* @param array<int, int> $parity Parity
|
||||
*
|
||||
* @return array<int, int> Parity array
|
||||
*/
|
||||
protected function encodeRsChar(array $rsv, array $data, array $parity): array
|
||||
{
|
||||
$nn = $rsv['nn'];
|
||||
$alphaTo = $rsv['alpha_to'];
|
||||
$indexOf = $rsv['index_of'];
|
||||
$genpoly = $rsv['genpoly'];
|
||||
$nroots = $rsv['nroots'];
|
||||
$pad = $rsv['pad'];
|
||||
$parity = \array_values($parity);
|
||||
$parity = \array_fill(0, \max(0, $nroots), 0);
|
||||
for ($idx = 0; $idx < ($nn - $nroots - $pad); ++$idx) {
|
||||
$feedback = $indexOf[($data[$idx] ?? 0) ^ ($parity[0] ?? 0)] ?? 0;
|
||||
if ($feedback !== $nn) {
|
||||
// feedback term is non-zero
|
||||
// This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
|
||||
// always be for the polynomials constructed by initRs()
|
||||
$feedback = $this->modnn($rsv, $nn - ($genpoly[$nroots] ?? 0) + $feedback);
|
||||
for ($jdx = 1; $jdx < $nroots; ++$jdx) {
|
||||
$parity[$jdx] =
|
||||
($parity[$jdx] ?? 0)
|
||||
^ ($alphaTo[$this->modnn($rsv, $feedback + ($genpoly[$nroots - $jdx] ?? 0))] ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Shift
|
||||
\array_shift($parity);
|
||||
$parity[] = $feedback !== $nn ? $alphaTo[$this->modnn($rsv, $feedback + ($genpoly[0] ?? 0))] ?? 0 : 0;
|
||||
}
|
||||
|
||||
return \array_values($parity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* InputItem.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\InputItem
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* @phpstan-import-type Item from \Com\Tecnick\Barcode\Type\Square\QrCode\Estimate
|
||||
*/
|
||||
abstract class InputItem extends \Com\Tecnick\Barcode\Type\Square\QrCode\Estimate
|
||||
{
|
||||
protected function getEncModeValue(string $mode): int
|
||||
{
|
||||
return Data::ENC_MODES[$mode] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $data
|
||||
*/
|
||||
protected function getItemDataOrd(array $data, int $idx): int
|
||||
{
|
||||
return \ord($data[$idx] ?? "\x00");
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up the alphabet-numeric conversion table (see JIS X0510:2004, pp.19)
|
||||
*
|
||||
* @param int $chr Character value
|
||||
*/
|
||||
public function lookAnTable(int $chr): int
|
||||
{
|
||||
if ($chr < 0 || $chr > 127) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return Data::AN_TABLE[$chr] ?? -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append data to an input object.
|
||||
* The data is copied and appended to the input object.
|
||||
*
|
||||
* @param array<int, Item> $items Input items
|
||||
* @param int $mode Encoding mode.
|
||||
* @param int $size Size of data (byte).
|
||||
* @param array<int, string> $data Array of input data.
|
||||
*
|
||||
* @return array<int, Item> items
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
public function appendNewInputItem(array $items, int $mode, int $size, array $data): array
|
||||
{
|
||||
$newitem = $this->newInputItem($mode, $size, $data);
|
||||
if ($newitem !== []) {
|
||||
$items[] = $newitem;
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* newInputItem
|
||||
*
|
||||
* @param int $mode Encoding mode.
|
||||
* @param int $size Size of data (byte).
|
||||
* @param array<int, string> $data Array of input data.
|
||||
* @param array<int, int> $bstream Binary stream
|
||||
*
|
||||
* @return Item input item
|
||||
*
|
||||
* @throws BarcodeException in case of error
|
||||
*/
|
||||
protected function newInputItem(int $mode, int $size, array $data, array $bstream = []): array
|
||||
{
|
||||
$setData = \array_slice($data, 0, $size);
|
||||
if (\count($setData) < $size) {
|
||||
$setData = \array_merge($setData, \array_fill(0, \max(0, $size - \count($setData)), '0'));
|
||||
}
|
||||
|
||||
if (!$this->check($mode, $size, $setData)) {
|
||||
throw new BarcodeException('Invalid input item');
|
||||
}
|
||||
|
||||
return [
|
||||
'mode' => $mode,
|
||||
'size' => $size,
|
||||
'data' => $setData,
|
||||
'bstream' => $bstream,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the input data.
|
||||
*
|
||||
* @param int $mode Encoding mode.
|
||||
* @param int $size Size of data (byte).
|
||||
* @param array<int, string> $data Data to validate
|
||||
*
|
||||
* @return bool true in case of valid data, false otherwise
|
||||
*/
|
||||
protected function check(int $mode, int $size, array $data): bool
|
||||
{
|
||||
if ($size <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return match ($mode) {
|
||||
$this->getEncModeValue('NM') => $this->checkModeNum($size, $data),
|
||||
$this->getEncModeValue('AN') => $this->checkModeAn($size, $data),
|
||||
$this->getEncModeValue('KJ') => $this->checkModeKanji($size, $data),
|
||||
$this->getEncModeValue('8B'), $this->getEncModeValue('ST') => true,
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* checkModeNum
|
||||
*
|
||||
* @param int $size Size of data (byte).
|
||||
* @param array<int, string> $data Data to validate
|
||||
*
|
||||
* @return bool true or false
|
||||
*/
|
||||
protected function checkModeNum(int $size, array $data): bool
|
||||
{
|
||||
for ($idx = 0; $idx < $size; ++$idx) {
|
||||
$ord = $this->getItemDataOrd($data, $idx);
|
||||
if ($ord < \ord('0') || $ord > \ord('9')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* checkModeAn
|
||||
*
|
||||
* @param int $size Size of data (byte).
|
||||
* @param array<int, string> $data Data to validate
|
||||
*
|
||||
* @return bool true or false
|
||||
*/
|
||||
protected function checkModeAn(int $size, array $data): bool
|
||||
{
|
||||
for ($idx = 0; $idx < $size; ++$idx) {
|
||||
if ($this->lookAnTable($this->getItemDataOrd($data, $idx)) === -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* checkModeKanji
|
||||
*
|
||||
* @param int $size Size of data (byte).
|
||||
* @param array<int, string> $data Data to validate
|
||||
*
|
||||
* @return bool true or false
|
||||
*/
|
||||
protected function checkModeKanji(int $size, array $data): bool
|
||||
{
|
||||
if (($size & 1) !== 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ($idx = 0; $idx < $size; $idx += 2) {
|
||||
$val = ($this->getItemDataOrd($data, $idx) << 8) | $this->getItemDataOrd($data, $idx + 1);
|
||||
if ($val < 0x8140 || $val > 0x9ffc && $val < 0xe040 || $val > 0xebbf) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,391 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Mask.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\Mask
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class Mask extends \Com\Tecnick\Barcode\Type\Square\QrCode\MaskNum
|
||||
{
|
||||
/**
|
||||
* Run length
|
||||
*
|
||||
* @var array<int, int>
|
||||
*/
|
||||
protected array $runLength = [];
|
||||
|
||||
/**
|
||||
* Spec class object
|
||||
*/
|
||||
protected Spec $spc;
|
||||
|
||||
/**
|
||||
* @param array<int, string> $frame
|
||||
*/
|
||||
protected function getFrameRow(array $frame, int $index): string
|
||||
{
|
||||
return $frame[$index] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $frame
|
||||
*/
|
||||
protected function replaceFrameRow(
|
||||
array &$frame,
|
||||
int $index,
|
||||
string $replacement,
|
||||
int $offset,
|
||||
?int $length = null,
|
||||
): void {
|
||||
$replaceLength = $length === null ? null : \max(0, $length);
|
||||
$frame[$index] = \substr_replace($this->getFrameRow($frame, $index), $replacement, $offset, $replaceLength);
|
||||
}
|
||||
|
||||
protected function getRowChar(string $row, int $index): string
|
||||
{
|
||||
return $row[$index] ?? "\0";
|
||||
}
|
||||
|
||||
protected function getRunLengthValue(int $index): int
|
||||
{
|
||||
return $this->runLength[$index] ?? 0;
|
||||
}
|
||||
|
||||
protected function incrementRunLength(int $index): void
|
||||
{
|
||||
$this->runLength[$index] = $this->getRunLengthValue($index) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize
|
||||
*
|
||||
* @param int $version Code version
|
||||
* @param int $level Error Correction Level
|
||||
* @param int $qr_find_from_random If negative, checks all masks available,
|
||||
* otherwise the value indicates the number of masks to be checked,
|
||||
* mask ids are random
|
||||
* @param bool $qr_find_best_mask If true, estimates best mask (slow)
|
||||
* @param int $qr_default_mask Default mask used when $qr_find_best_mask is false
|
||||
*/
|
||||
public function __construct(
|
||||
/**
|
||||
* QR code version.
|
||||
* The Size of QRcode is defined as version. Version is an integer value from 1 to 40.
|
||||
* Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
|
||||
* So version 40 is 177*177 matrix.
|
||||
*/
|
||||
public int $version,
|
||||
/**
|
||||
* Error correction level
|
||||
*/
|
||||
protected int $level,
|
||||
protected int $qr_find_from_random = -1,
|
||||
protected bool $qr_find_best_mask = true,
|
||||
protected int $qr_default_mask = 2,
|
||||
) {
|
||||
$this->spc = new Spec();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the best mask
|
||||
*
|
||||
* @param int $width Width
|
||||
* @param array<int, string> $frame Frame
|
||||
* @param int $level Error Correction lLevel
|
||||
*
|
||||
* @return array<int, string> best mask
|
||||
*
|
||||
* @throws \Random\RandomException in case random mask selection fails
|
||||
*/
|
||||
protected function mask(int $width, array $frame, int $level): array
|
||||
{
|
||||
$minDemerit = PHP_INT_MAX;
|
||||
$bestMask = [];
|
||||
$checked_masks = [0, 1, 2, 3, 4, 5, 6, 7];
|
||||
if ($this->qr_find_from_random >= 0) {
|
||||
// keep at least one candidate mask so a mask and its format info are always applied
|
||||
$howManuOut = \min(7, 8 - ($this->qr_find_from_random % 9));
|
||||
for ($idx = 0; $idx < $howManuOut; ++$idx) {
|
||||
$maxpos = \count($checked_masks) - 1;
|
||||
$remPos = $maxpos > 0 ? \random_int(0, $maxpos) : 0;
|
||||
unset($checked_masks[$remPos]);
|
||||
$checked_masks = \array_values($checked_masks);
|
||||
}
|
||||
}
|
||||
|
||||
$bestMask = $frame;
|
||||
foreach ($checked_masks as $checked_mask) {
|
||||
$mask = \array_fill(0, \max(0, $width), \str_repeat("\0", \max(0, $width)));
|
||||
$demerit = 0;
|
||||
$blacks = $this->makeMaskNo($checked_mask, $width, $frame, $mask);
|
||||
$blacks += $this->writeFormatInformation($width, $mask, $checked_mask, $level);
|
||||
$blacks = (int) ((100 * $blacks) / ($width * $width));
|
||||
$demerit = (int) (\abs($blacks - 50) / 5) * Data::N4;
|
||||
$demerit += $this->evaluateSymbol($width, $mask);
|
||||
if ($demerit < $minDemerit) {
|
||||
$minDemerit = $demerit;
|
||||
$bestMask = $mask;
|
||||
}
|
||||
}
|
||||
|
||||
return $bestMask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a mask
|
||||
*
|
||||
* @param int $width Mask width
|
||||
* @param array<int, string> $frame Frame
|
||||
* @param int $maskNo Mask number
|
||||
* @param int $level Error Correction level
|
||||
*
|
||||
* @return array<int, string> mask
|
||||
*/
|
||||
protected function makeMask(int $width, array $frame, int $maskNo, int $level): array
|
||||
{
|
||||
$mask = [];
|
||||
$this->makeMaskNo($maskNo, $width, $frame, $mask);
|
||||
$this->writeFormatInformation($width, $mask, $maskNo, $level);
|
||||
return $mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write Format Information on the frame and returns the number of black bits
|
||||
*
|
||||
* @param int $width Mask width
|
||||
* @param array<int, string> $frame Frame
|
||||
* @param int $maskNo Mask number
|
||||
* @param int $level Error Correction level
|
||||
*
|
||||
* @return int blacks
|
||||
*/
|
||||
protected function writeFormatInformation(int $width, array &$frame, int $maskNo, int $level): int
|
||||
{
|
||||
$blacks = 0;
|
||||
$spec = new Spec();
|
||||
$format = $spec->getFormatInfo($maskNo, $level);
|
||||
for ($idx = 0; $idx < 8; ++$idx) {
|
||||
$val = 0x84;
|
||||
if (($format & 1) !== 0) {
|
||||
$blacks += 2;
|
||||
$val = 0x85;
|
||||
}
|
||||
|
||||
$this->replaceFrameRow($frame, 8, \chr($val & 0xFF), $width - 1 - $idx, 1);
|
||||
if ($idx < 6) {
|
||||
$this->replaceFrameRow($frame, $idx, \chr($val & 0xFF), 8, 1);
|
||||
$format >>= 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->replaceFrameRow($frame, $idx + 1, \chr($val & 0xFF), 8, 1);
|
||||
|
||||
$format >>= 1;
|
||||
}
|
||||
|
||||
for ($idx = 0; $idx < 7; ++$idx) {
|
||||
$val = 0x84;
|
||||
if (($format & 1) !== 0) {
|
||||
$blacks += 2;
|
||||
$val = 0x85;
|
||||
}
|
||||
|
||||
$this->replaceFrameRow($frame, $width - 7 + $idx, \chr($val & 0xFF), 8, 1);
|
||||
if ($idx === 0) {
|
||||
$this->replaceFrameRow($frame, 8, \chr($val & 0xFF), 7, 1);
|
||||
$format >>= 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->replaceFrameRow($frame, 8, \chr($val & 0xFF), 6 - $idx, 1);
|
||||
|
||||
$format >>= 1;
|
||||
}
|
||||
|
||||
return $blacks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate Symbol and returns demerit value.
|
||||
*
|
||||
* @param int $width Width
|
||||
* @param array<int, string> $frame Frame
|
||||
*/
|
||||
protected function evaluateSymbol(int $width, array $frame): int
|
||||
{
|
||||
// horizontal direction: accumulate the per-row demerit over every row
|
||||
$demerit = 0;
|
||||
for ($ypos = 0; $ypos < $width; ++$ypos) {
|
||||
$frameY = $this->getFrameRow($frame, $ypos);
|
||||
$frameYM = $ypos > 0 ? $this->getFrameRow($frame, $ypos - 1) : $frameY;
|
||||
$demerit += $this->evaluateSymbolB($ypos, $width, $frameY, $frameYM);
|
||||
}
|
||||
|
||||
// vertical direction: accumulate the per-column demerit over every column
|
||||
for ($xpos = 0; $xpos < $width; ++$xpos) {
|
||||
$head = 0;
|
||||
$this->runLength[0] = 1;
|
||||
for ($ypos = 0; $ypos < $width; ++$ypos) {
|
||||
if ($ypos === 0 && \ord($this->getRowChar($this->getFrameRow($frame, $ypos), $xpos)) & 1) {
|
||||
$this->runLength[0] = -1;
|
||||
$head = 1;
|
||||
$this->runLength[$head] = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($ypos > 0) {
|
||||
if (
|
||||
(
|
||||
(
|
||||
\ord($this->getRowChar($this->getFrameRow($frame, $ypos), $xpos))
|
||||
^ \ord($this->getRowChar($this->getFrameRow($frame, $ypos - 1), $xpos))
|
||||
)
|
||||
& 1
|
||||
)
|
||||
!== 0
|
||||
) {
|
||||
++$head;
|
||||
$this->runLength[$head] = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->incrementRunLength($head);
|
||||
}
|
||||
}
|
||||
|
||||
$demerit += $this->calcN1N3($head + 1);
|
||||
}
|
||||
|
||||
return $demerit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate Symbol
|
||||
*
|
||||
* @param int $ypos Y position
|
||||
* @param int $width Width
|
||||
*
|
||||
* @return int demerit
|
||||
*/
|
||||
protected function evaluateSymbolB(int $ypos, int $width, string $frameY, string $frameYM): int
|
||||
{
|
||||
$head = 0;
|
||||
$demerit = 0;
|
||||
$this->runLength[0] = 1;
|
||||
for ($xpos = 0; $xpos < $width; ++$xpos) {
|
||||
if ($xpos > 0 && $ypos > 0) {
|
||||
$b22 =
|
||||
\ord($this->getRowChar($frameY, $xpos))
|
||||
& \ord($this->getRowChar($frameY, $xpos - 1))
|
||||
& \ord($this->getRowChar($frameYM, $xpos))
|
||||
& \ord($this->getRowChar($frameYM, $xpos - 1));
|
||||
$w22 =
|
||||
\ord($this->getRowChar($frameY, $xpos))
|
||||
| \ord($this->getRowChar($frameY, $xpos - 1))
|
||||
| \ord($this->getRowChar($frameYM, $xpos))
|
||||
| \ord($this->getRowChar($frameYM, $xpos - 1));
|
||||
if ((($b22 | ($w22 ^ 1)) & 1) !== 0) {
|
||||
$demerit += Data::N2;
|
||||
}
|
||||
}
|
||||
|
||||
if ($xpos === 0 && \ord($this->getRowChar($frameY, $xpos)) & 1) {
|
||||
$this->runLength[0] = -1;
|
||||
$head = 1;
|
||||
$this->runLength[$head] = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($xpos > 0) {
|
||||
if (
|
||||
((\ord($this->getRowChar($frameY, $xpos)) ^ \ord($this->getRowChar($frameY, $xpos - 1))) & 1) !== 0
|
||||
) {
|
||||
++$head;
|
||||
$this->runLength[$head] = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->incrementRunLength($head);
|
||||
}
|
||||
}
|
||||
|
||||
return $demerit + $this->calcN1N3($head + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calc N1 N3
|
||||
*
|
||||
* @param int $length Length
|
||||
*
|
||||
* @return int demerit
|
||||
*/
|
||||
protected function calcN1N3(int $length): int
|
||||
{
|
||||
$demerit = 0;
|
||||
for ($idx = 0; $idx < $length; ++$idx) {
|
||||
$runLen = $this->getRunLengthValue($idx);
|
||||
if ($runLen >= 5) {
|
||||
$demerit += Data::N1 + ($runLen - 5);
|
||||
}
|
||||
|
||||
if ($idx & 1 && $idx >= 3 && $idx < ($length - 2) && ($runLen % 3) === 0) {
|
||||
$demerit += $this->calcN1N3delta($length, $idx);
|
||||
}
|
||||
}
|
||||
|
||||
return $demerit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calc N1 N3 delta
|
||||
*
|
||||
* @param int $length Length
|
||||
* @param int $idx Index
|
||||
*
|
||||
* @return int demerit delta
|
||||
*/
|
||||
protected function calcN1N3delta(int $length, int $idx): int
|
||||
{
|
||||
$fact = (int) ($this->getRunLengthValue($idx) / 3);
|
||||
if (
|
||||
$this->getRunLengthValue($idx - 2) === $fact
|
||||
&& $this->getRunLengthValue($idx - 1) === $fact
|
||||
&& $this->getRunLengthValue($idx + 1) === $fact
|
||||
&& $this->getRunLengthValue($idx + 2) === $fact
|
||||
) {
|
||||
if ($this->getRunLengthValue($idx - 3) < 0 || $this->getRunLengthValue($idx - 3) >= (4 * $fact)) {
|
||||
return Data::N3;
|
||||
}
|
||||
|
||||
if (($idx + 3) >= $length || $this->getRunLengthValue($idx + 3) >= (4 * $fact)) {
|
||||
return Data::N3;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MaskNum.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\MaskNum
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
abstract class MaskNum
|
||||
{
|
||||
/**
|
||||
* @param array<int, string> $rows
|
||||
*/
|
||||
protected function getRow(array $rows, int $ypos): string
|
||||
{
|
||||
return $rows[$ypos] ?? '';
|
||||
}
|
||||
|
||||
protected function getRowCharOrd(string $row, int $xpos): int
|
||||
{
|
||||
return \ord($row[$xpos] ?? "\x00");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<int, int>> $bitMask
|
||||
*/
|
||||
protected function getMaskBit(array $bitMask, int $ypos, int $xpos): int
|
||||
{
|
||||
return $bitMask[$ypos][$xpos] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make Mask Number
|
||||
*
|
||||
* @param int $maskNo Mask number
|
||||
* @param int $width Width
|
||||
* @param array<int, string> $frame Frame
|
||||
* @param array<int, string> $mask Mask
|
||||
*
|
||||
* @return int mask number
|
||||
*/
|
||||
protected function makeMaskNo(int $maskNo, int $width, array $frame, array &$mask): int
|
||||
{
|
||||
$bnum = 0;
|
||||
$bitMask = $this->generateMaskNo($maskNo, $width, $frame);
|
||||
$mask = $frame;
|
||||
for ($ypos = 0; $ypos < $width; ++$ypos) {
|
||||
for ($xpos = 0; $xpos < $width; ++$xpos) {
|
||||
$mask_bit = $this->getMaskBit($bitMask, $ypos, $xpos);
|
||||
if ($mask_bit === 1) {
|
||||
$frame_row = $this->getRow($frame, $ypos);
|
||||
$mask_row = $this->getRow($mask, $ypos);
|
||||
$mask[$ypos] = \substr_replace(
|
||||
$mask_row,
|
||||
\chr(($this->getRowCharOrd($frame_row, $xpos) ^ $mask_bit) & 0xFF),
|
||||
$xpos,
|
||||
1,
|
||||
);
|
||||
}
|
||||
|
||||
$bnum += $this->getRowCharOrd($this->getRow($mask, $ypos), $xpos) & 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $bnum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return bit mask
|
||||
*
|
||||
* @param int $maskNo Mask number
|
||||
* @param int $width Width
|
||||
* @param array<int, string> $frame Frame
|
||||
*
|
||||
* @return array<int, array<int, int>> bit mask
|
||||
*/
|
||||
protected function generateMaskNo(int $maskNo, int $width, array $frame): array
|
||||
{
|
||||
$mask_width = \max(0, $width);
|
||||
$bitMask = \array_fill(0, $mask_width, \array_fill(0, $mask_width, 0));
|
||||
for ($ypos = 0; $ypos < $width; ++$ypos) {
|
||||
$frame_row = $this->getRow($frame, $ypos);
|
||||
for ($xpos = 0; $xpos < $width; ++$xpos) {
|
||||
if (($this->getRowCharOrd($frame_row, $xpos) & 0x80) !== 0) {
|
||||
$bitMask[$ypos][$xpos] = 0;
|
||||
continue;
|
||||
}
|
||||
$maskFunc = match ($maskNo) {
|
||||
0 => ($xpos + $ypos) & 1,
|
||||
1 => $ypos & 1,
|
||||
2 => $xpos % 3,
|
||||
3 => ($xpos + $ypos) % 3,
|
||||
4 => ((int) ($ypos / 2) + (int) ($xpos / 3)) & 1,
|
||||
5 => (($xpos * $ypos) & 1) + (($xpos * $ypos) % 3),
|
||||
6 => ((($xpos * $ypos) & 1) + (($xpos * $ypos) % 3)) & 1,
|
||||
7 => ((($xpos * $ypos) % 3) + (($xpos + $ypos) & 1)) & 1,
|
||||
default => 1,
|
||||
};
|
||||
$bitMask[$ypos][$xpos] = $maskFunc === 0 ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
return $bitMask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* QrEccLevel.php
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\QrEccLevel
|
||||
*
|
||||
* Backed enum for the QR Code error correction level. The backing value of each
|
||||
* case is the letter used as a key of Data::ECC_LEVELS.
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
enum QrEccLevel: string
|
||||
{
|
||||
/** Low (7%). */
|
||||
case L = 'L';
|
||||
|
||||
/** Medium (15%). */
|
||||
case M = 'M';
|
||||
|
||||
/** Quartile (25%). */
|
||||
case Q = 'Q';
|
||||
|
||||
/** High (30%). */
|
||||
case H = 'H';
|
||||
|
||||
/**
|
||||
* Resolve a loose ECC level value to the matching enum case.
|
||||
*
|
||||
* Accepts the canonical letter or an enum instance (returned unchanged).
|
||||
* Unknown values fall back to L, matching the lenient behavior of QrCode.
|
||||
*
|
||||
* @param string|self $value ECC level letter or enum case.
|
||||
*/
|
||||
public static function fromLoose(string|self $value): self
|
||||
{
|
||||
if ($value instanceof self) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return self::tryFrom($value) ?? self::L;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* QrEncodingMode.php
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\QrEncodingMode
|
||||
*
|
||||
* Backed enum for the QR Code data encoding mode hint. The backing value of each
|
||||
* case is the token used as a key of Data::ENC_MODES.
|
||||
*
|
||||
* @since 2026-07-17
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
enum QrEncodingMode: string
|
||||
{
|
||||
/** Terminator / no data. */
|
||||
case NL = 'NL';
|
||||
|
||||
/** Numeric. */
|
||||
case NM = 'NM';
|
||||
|
||||
/** Alphanumeric. */
|
||||
case AN = 'AN';
|
||||
|
||||
/** 8-bit byte. */
|
||||
case Byte = '8B';
|
||||
|
||||
/** Kanji. */
|
||||
case KJ = 'KJ';
|
||||
|
||||
/** Structured append. */
|
||||
case ST = 'ST';
|
||||
|
||||
/**
|
||||
* Resolve a loose encoding mode value to the matching enum case.
|
||||
*
|
||||
* Accepts the canonical token or an enum instance (returned unchanged).
|
||||
* Unknown values fall back to Byte (8B), matching the lenient behavior of
|
||||
* QrCode.
|
||||
*
|
||||
* @param string|self $value Encoding mode token or enum case.
|
||||
*/
|
||||
public static function fromLoose(string|self $value): self
|
||||
{
|
||||
if ($value instanceof self) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return self::tryFrom($value) ?? self::Byte;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Spec.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\Spec
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* @phpstan-type EccSpec array{
|
||||
* 0: int,
|
||||
* 1: int,
|
||||
* 2: int,
|
||||
* 3: int,
|
||||
* 4: int,
|
||||
* }
|
||||
*/
|
||||
class Spec extends \Com\Tecnick\Barcode\Type\Square\QrCode\SpecRs
|
||||
{
|
||||
/**
|
||||
* @return array{0: int, 1: int, 2: int, 3: array{0: int, 1: int, 2: int, 3: int}}
|
||||
*/
|
||||
protected function getCapacityRow(int $version): array
|
||||
{
|
||||
return Data::CAPACITY[$version] ?? [0, 0, 0, [0, 0, 0, 0]];
|
||||
}
|
||||
|
||||
protected function getCapacityWords(int $version): int
|
||||
{
|
||||
return $this->getCapacityRow($version)[1];
|
||||
}
|
||||
|
||||
protected function getCapacityEcc(int $version, int $level): int
|
||||
{
|
||||
return $this->getCapacityRow($version)[3][$level] ?? 0;
|
||||
}
|
||||
|
||||
protected function getCapacityWidthValue(int $version): int
|
||||
{
|
||||
return $this->getCapacityRow($version)[0];
|
||||
}
|
||||
|
||||
protected function getCapacityRemainderValue(int $version): int
|
||||
{
|
||||
return $this->getCapacityRow($version)[2];
|
||||
}
|
||||
|
||||
protected function getLenTableBitsValue(int $mode, int $index): int
|
||||
{
|
||||
$modeTable = Data::LEN_TABLE_BITS[$mode] ?? [0, 0, 0];
|
||||
|
||||
return $modeTable[$index] ?? 0;
|
||||
}
|
||||
|
||||
protected function getEccTableValue(int $version, int $level, int $index): int
|
||||
{
|
||||
$versionTable = Data::ECC_TABLE[$version] ?? [[0, 0], [0, 0], [0, 0], [0, 0]];
|
||||
$levelTable = $versionTable[$level] ?? [0, 0];
|
||||
|
||||
return $levelTable[$index] ?? 0;
|
||||
}
|
||||
|
||||
protected function getFormatInfoValue(int $level, int $maskNo): int
|
||||
{
|
||||
$levelTable = Data::FORMAT_INFO[$level] ?? [0, 0, 0, 0, 0, 0, 0, 0];
|
||||
|
||||
return $levelTable[$maskNo] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return maximum data code length (bytes) for the version.
|
||||
*
|
||||
* @param int $version Version
|
||||
* @param int $level Error correction level
|
||||
*
|
||||
* @return int maximum size (bytes)
|
||||
*/
|
||||
public function getDataLength(int $version, int $level): int
|
||||
{
|
||||
return $this->getCapacityWords($version) - $this->getCapacityEcc($version, $level);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return maximum error correction code length (bytes) for the version.
|
||||
*
|
||||
* @param int $version Version
|
||||
* @param int $level Error correction level
|
||||
*
|
||||
* @return int ECC size (bytes)
|
||||
*/
|
||||
public function getECCLength(int $version, int $level): int
|
||||
{
|
||||
return $this->getCapacityEcc($version, $level);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the width of the symbol for the version.
|
||||
*
|
||||
* @param int $version Version
|
||||
*
|
||||
* @return int width
|
||||
*/
|
||||
public function getWidth(int $version): int
|
||||
{
|
||||
return $this->getCapacityWidthValue($version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of remainder bits.
|
||||
*
|
||||
* @param int $version Version
|
||||
*
|
||||
* @return int number of remainder bits
|
||||
*/
|
||||
public function getRemainder(int $version): int
|
||||
{
|
||||
return $this->getCapacityRemainderValue($version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum length for the mode and version.
|
||||
*
|
||||
* @param int $mode Encoding mode
|
||||
* @param int $version Version
|
||||
*
|
||||
* @return int the maximum length (bytes)
|
||||
*/
|
||||
public function maximumWords(int $mode, int $version): int
|
||||
{
|
||||
if ($mode === Data::MODE_ST || $mode < Data::MODE_NL || $mode > Data::MODE_ST) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
$lval = match (true) {
|
||||
$version <= 9 => 0,
|
||||
$version <= 26 => 1,
|
||||
default => 2,
|
||||
};
|
||||
|
||||
$bits = $this->getLenTableBitsValue($mode, $lval);
|
||||
$words = (1 << $bits) - 1;
|
||||
if ($mode === Data::MODE_KJ) {
|
||||
$words *= 2; // the number of bytes is required
|
||||
}
|
||||
|
||||
return $words;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of ECC specification.
|
||||
*
|
||||
* @param int $version Version
|
||||
* @param int $level Error correction level
|
||||
* @param EccSpec $spec Array of ECC specification
|
||||
*
|
||||
* @return EccSpec spec:
|
||||
* 0 = # of type1 blocks
|
||||
* 1 = # of data code
|
||||
* 2 = # of ecc code
|
||||
* 3 = # of type2 blocks
|
||||
* 4 = # of data code
|
||||
*/
|
||||
public function getEccSpec(int $version, int $level, array $spec): array
|
||||
{
|
||||
if (\count($spec) < 5) {
|
||||
$spec = [0, 0, 0, 0, 0];
|
||||
}
|
||||
|
||||
$bv1 = $this->getEccTableValue($version, $level, 0);
|
||||
$bv2 = $this->getEccTableValue($version, $level, 1);
|
||||
$data = $this->getDataLength($version, $level);
|
||||
$ecc = $this->getECCLength($version, $level);
|
||||
if ($bv2 === 0) {
|
||||
$spec[0] = $bv1;
|
||||
$spec[1] = (int) ($data / $bv1); /* @phpstan-ignore-line */
|
||||
$spec[2] = (int) ($ecc / $bv1); /* @phpstan-ignore-line */
|
||||
$spec[3] = 0;
|
||||
$spec[4] = 0;
|
||||
return $spec;
|
||||
}
|
||||
|
||||
$spec[0] = $bv1;
|
||||
$spec[1] = (int) ($data / ($bv1 + $bv2));
|
||||
$spec[2] = (int) ($ecc / ($bv1 + $bv2));
|
||||
$spec[3] = $bv2;
|
||||
$spec[4] = $spec[1] + 1;
|
||||
|
||||
return $spec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return BCH encoded format information pattern.
|
||||
*
|
||||
* @param int $maskNo Mask number
|
||||
* @param int $level Error correction level
|
||||
*/
|
||||
public function getFormatInfo(int $maskNo, int $level): int
|
||||
{
|
||||
if ($maskNo < 0 || $maskNo > 7 || $level < 0 || $level > 3) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->getFormatInfoValue($level, $maskNo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,429 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SpecRs.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\SpecRs
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.TooManyPublicMethods")
|
||||
*/
|
||||
abstract class SpecRs
|
||||
{
|
||||
/**
|
||||
* @param array<int, int> $spec
|
||||
*/
|
||||
protected function getSpecValue(array $spec, int $index): int
|
||||
{
|
||||
return match ($index) {
|
||||
0 => $spec[0] ?? 0,
|
||||
1 => $spec[1] ?? 0,
|
||||
2 => $spec[2] ?? 0,
|
||||
3 => $spec[3] ?? 0,
|
||||
4 => $spec[4] ?? 0,
|
||||
default => 0,
|
||||
};
|
||||
}
|
||||
|
||||
protected function getCapacityWidth(int $version): int
|
||||
{
|
||||
$capacity = Data::CAPACITY[$version] ?? [0, 0, 0, [0, 0, 0, 0]];
|
||||
|
||||
return $capacity[0] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $frame
|
||||
*/
|
||||
protected function getFrameRow(array $frame, int $index): string
|
||||
{
|
||||
return $frame[$index] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $frame
|
||||
*/
|
||||
protected function replaceFrameRow(
|
||||
array &$frame,
|
||||
int $index,
|
||||
string $replacement,
|
||||
int $offset,
|
||||
?int $length = null,
|
||||
): void {
|
||||
$replaceLength = $length === null ? null : \max(0, $length);
|
||||
$frame[$index] = \substr_replace($this->getFrameRow($frame, $index), $replacement, $offset, $replaceLength);
|
||||
}
|
||||
|
||||
protected function getVersionPatternValue(int $version): int
|
||||
{
|
||||
return Data::VERSION_PATTERN[$version - 7] ?? 0;
|
||||
}
|
||||
|
||||
protected function getAlignmentStart(int $version): int
|
||||
{
|
||||
$pattern = Data::ALIGN_PATTERN[$version] ?? [0, 0];
|
||||
|
||||
return $pattern[0] ?? 0;
|
||||
}
|
||||
|
||||
protected function getAlignmentEnd(int $version): int
|
||||
{
|
||||
$pattern = Data::ALIGN_PATTERN[$version] ?? [0, 0];
|
||||
|
||||
return $pattern[1] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return block number 0
|
||||
*
|
||||
* @param array<int, int> $spec Spec
|
||||
*
|
||||
* @return int value
|
||||
*/
|
||||
public function rsBlockNum(array $spec): int
|
||||
{
|
||||
return $this->getSpecValue($spec, 0) + $this->getSpecValue($spec, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return block number 1
|
||||
*
|
||||
* @param array<int, int> $spec Spec
|
||||
*
|
||||
* @return int value
|
||||
*/
|
||||
public function rsBlockNum1(array $spec): int
|
||||
{
|
||||
return $this->getSpecValue($spec, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return data codes 1
|
||||
*
|
||||
* @param array<int, int> $spec Spec
|
||||
*
|
||||
* @return int value
|
||||
*/
|
||||
public function rsDataCodes1(array $spec): int
|
||||
{
|
||||
return $this->getSpecValue($spec, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return ecc codes 1
|
||||
*
|
||||
* @param array<int, int> $spec Spec
|
||||
*
|
||||
* @return int value
|
||||
*/
|
||||
public function rsEccCodes1(array $spec): int
|
||||
{
|
||||
return $this->getSpecValue($spec, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return block number 2
|
||||
*
|
||||
* @param array<int, int> $spec Spec
|
||||
*
|
||||
* @return int value
|
||||
*/
|
||||
public function rsBlockNum2(array $spec): int
|
||||
{
|
||||
return $this->getSpecValue($spec, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return data codes 2
|
||||
*
|
||||
* @param array<int, int> $spec Spec
|
||||
*
|
||||
* @return int value
|
||||
*/
|
||||
public function rsDataCodes2(array $spec): int
|
||||
{
|
||||
return $this->getSpecValue($spec, 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return ecc codes 2
|
||||
*
|
||||
* @param array<int, int> $spec Spec
|
||||
*
|
||||
* @return int value
|
||||
*/
|
||||
public function rsEccCodes2(array $spec): int
|
||||
{
|
||||
return $this->getSpecValue($spec, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return data length
|
||||
*
|
||||
* @param array<int, int> $spec Spec
|
||||
*
|
||||
* @return int value
|
||||
*/
|
||||
public function rsDataLength(array $spec): int
|
||||
{
|
||||
return (
|
||||
($this->getSpecValue($spec, 0) * $this->getSpecValue($spec, 1))
|
||||
+ ($this->getSpecValue($spec, 3) * $this->getSpecValue($spec, 4))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return ecc length
|
||||
*
|
||||
* @param array<int, int> $spec Spec
|
||||
*
|
||||
* @return int value
|
||||
*/
|
||||
public function rsEccLength(array $spec): int
|
||||
{
|
||||
return ($this->getSpecValue($spec, 0) + $this->getSpecValue($spec, 3)) * $this->getSpecValue($spec, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a copy of initialized frame.
|
||||
*
|
||||
* @param int $version Version
|
||||
*
|
||||
* @return array<int, string> of unsigned char.
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.ExcessiveMethodLength")
|
||||
*/
|
||||
public function createFrame(int $version): array
|
||||
{
|
||||
$width = \max(0, $this->getCapacityWidth($version));
|
||||
$frameLine = \str_repeat("\0", $width);
|
||||
$frame = \array_fill(0, $width, $frameLine);
|
||||
// Finder pattern
|
||||
$frame = $this->putFinderPattern($frame, 0, 0);
|
||||
$frame = $this->putFinderPattern($frame, $width - 7, 0);
|
||||
$frame = $this->putFinderPattern($frame, 0, $width - 7);
|
||||
// Separator
|
||||
$yOffset = $width - 7;
|
||||
for ($ypos = 0; $ypos < 7; ++$ypos) {
|
||||
$this->replaceFrameRow($frame, $ypos, "\xc0", 7, 1);
|
||||
$this->replaceFrameRow($frame, $ypos, "\xc0", $width - 8, 1);
|
||||
$this->replaceFrameRow($frame, $yOffset, "\xc0", 7, 1);
|
||||
++$yOffset;
|
||||
}
|
||||
|
||||
$setPattern = \str_repeat("\xc0", 8);
|
||||
$frame = $this->qrstrset($frame, 0, 7, $setPattern);
|
||||
$frame = $this->qrstrset($frame, $width - 8, 7, $setPattern);
|
||||
$frame = $this->qrstrset($frame, 0, $width - 8, $setPattern);
|
||||
// Format info
|
||||
$setPattern = \str_repeat("\x84", 9);
|
||||
$frame = $this->qrstrset($frame, 0, 8, $setPattern);
|
||||
$frame = $this->qrstrset($frame, $width - 8, 8, $setPattern, 8);
|
||||
|
||||
$yOffset = $width - 8;
|
||||
for ($ypos = 0; $ypos < 8; ++$ypos, ++$yOffset) {
|
||||
$this->replaceFrameRow($frame, $ypos, "\x84", 8, 1);
|
||||
$this->replaceFrameRow($frame, $yOffset, "\x84", 8, 1);
|
||||
}
|
||||
|
||||
// Timing pattern
|
||||
$wdo = $width - 15;
|
||||
for ($idx = 1; $idx < $wdo; ++$idx) {
|
||||
$this->replaceFrameRow($frame, 6, \chr((0x90 | ($idx & 1)) & 0xFF), 7 + $idx, 1);
|
||||
$this->replaceFrameRow($frame, 7 + $idx, \chr((0x90 | ($idx & 1)) & 0xFF), 6, 1);
|
||||
}
|
||||
|
||||
// Alignment pattern
|
||||
$frame = $this->putAlignmentPattern($version, $frame, $width);
|
||||
// Version information
|
||||
if ($version >= 7) {
|
||||
$vinf = $this->getVersionPattern($version);
|
||||
$val = $vinf;
|
||||
for ($xpos = 0; $xpos < 6; ++$xpos) {
|
||||
for ($ypos = 0; $ypos < 3; ++$ypos) {
|
||||
$this->replaceFrameRow($frame, $width - 11 + $ypos, \chr((0x88 | ($val & 1)) & 0xFF), $xpos, 1);
|
||||
$val >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
$val = $vinf;
|
||||
for ($ypos = 0; $ypos < 6; ++$ypos) {
|
||||
for ($xpos = 0; $xpos < 3; ++$xpos) {
|
||||
$this->replaceFrameRow($frame, $ypos, \chr(0x88 | ($val & 1 & 0xFF)), $xpos + ($width - 11), 1);
|
||||
$val >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// and a little bit...
|
||||
$this->replaceFrameRow($frame, $width - 8, "\x81", 8, 1);
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a value on the array at the specified position
|
||||
*
|
||||
* @param array<int, string> $srctab Source table
|
||||
* @param int $xpos X position
|
||||
* @param int $ypos Y position
|
||||
* @param string $repl Value to replace
|
||||
* @param int|null $replLen Length of the repl string
|
||||
*
|
||||
* @return array<int, string> srctab
|
||||
*/
|
||||
public function qrstrset(array $srctab, int $xpos, int $ypos, string $repl, ?int $replLen = null): array
|
||||
{
|
||||
$replaceLength = \max(0, $replLen ?? \strlen($repl));
|
||||
$srctab[$ypos] = \substr_replace(
|
||||
$this->getFrameRow($srctab, $ypos),
|
||||
$replLen !== null ? \substr($repl, 0, $replLen) : $repl,
|
||||
$xpos,
|
||||
$replaceLength,
|
||||
);
|
||||
|
||||
return $srctab;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put an alignment marker.
|
||||
*
|
||||
* @param array<int, string> $frame Frame
|
||||
* @param int $pox X center coordinate of the pattern
|
||||
* @param int $poy Y center coordinate of the pattern
|
||||
*
|
||||
* @return array<int, string> frame
|
||||
*/
|
||||
public function putAlignmentMarker(array $frame, int $pox, int $poy): array
|
||||
{
|
||||
$finder = [
|
||||
"\xa1\xa1\xa1\xa1\xa1",
|
||||
"\xa1\xa0\xa0\xa0\xa1",
|
||||
"\xa1\xa0\xa1\xa0\xa1",
|
||||
"\xa1\xa0\xa0\xa0\xa1",
|
||||
"\xa1\xa1\xa1\xa1\xa1",
|
||||
];
|
||||
$yStart = $poy - 2;
|
||||
$xStart = $pox - 2;
|
||||
for ($ydx = 0; $ydx < 5; ++$ydx) {
|
||||
$frame = $this->qrstrset($frame, $xStart, $yStart + $ydx, $finder[$ydx] ?? '');
|
||||
}
|
||||
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put a finder pattern.
|
||||
*
|
||||
* @param array<int, string> $frame Frame
|
||||
* @param int $pox X center coordinate of the pattern
|
||||
* @param int $poy Y center coordinate of the pattern
|
||||
*
|
||||
* @return array<int, string> frame
|
||||
*/
|
||||
public function putFinderPattern(array $frame, int $pox, int $poy): array
|
||||
{
|
||||
$finder = [
|
||||
"\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
|
||||
"\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
|
||||
"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
|
||||
"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
|
||||
"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
|
||||
"\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
|
||||
"\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
|
||||
];
|
||||
for ($ypos = 0; $ypos < 7; ++$ypos) {
|
||||
$frame = $this->qrstrset($frame, $pox, $poy + $ypos, $finder[$ypos] ?? '');
|
||||
}
|
||||
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return BCH encoded version information pattern that is used for the symbol of version 7 or greater.
|
||||
* Use lower 18 bits.
|
||||
*
|
||||
* @param int $version Version
|
||||
*/
|
||||
public function getVersionPattern(int $version): int
|
||||
{
|
||||
if ($version < 7 || $version > Data::QRSPEC_VERSION_MAX) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->getVersionPatternValue($version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put an alignment pattern.
|
||||
*
|
||||
* @param int $version Version
|
||||
* @param array<int, string> $frame Frame
|
||||
* @param int $width Width
|
||||
*
|
||||
* @return array<int, string> frame
|
||||
*/
|
||||
public function putAlignmentPattern(int $version, array $frame, int $width): array
|
||||
{
|
||||
if ($version < 2) {
|
||||
return $frame;
|
||||
}
|
||||
|
||||
$alignStart = $this->getAlignmentStart($version);
|
||||
$alignEnd = $this->getAlignmentEnd($version);
|
||||
$dval = $alignEnd - $alignStart;
|
||||
$wdt = 2;
|
||||
if ($dval >= 0) {
|
||||
$wdt = (int) ((($width - $alignStart) / $dval) + 2);
|
||||
}
|
||||
|
||||
if ((($wdt * $wdt) - 3) === 1) {
|
||||
$psx = $alignStart;
|
||||
$psy = $alignStart;
|
||||
return $this->putAlignmentMarker($frame, $psx, $psy);
|
||||
}
|
||||
|
||||
$cpx = $alignStart;
|
||||
$wdo = $wdt - 1;
|
||||
for ($xpos = 1; $xpos < $wdo; ++$xpos) {
|
||||
$frame = $this->putAlignmentMarker($frame, 6, $cpx);
|
||||
$frame = $this->putAlignmentMarker($frame, $cpx, 6);
|
||||
$cpx += $dval;
|
||||
}
|
||||
|
||||
$cpy = $alignStart;
|
||||
for ($y = 0; $y < $wdo; ++$y) {
|
||||
$cpx = $alignStart;
|
||||
for ($xpos = 0; $xpos < $wdo; ++$xpos) {
|
||||
$frame = $this->putAlignmentMarker($frame, $cpx, $cpy);
|
||||
$cpx += $dval;
|
||||
}
|
||||
|
||||
$cpy += $dval;
|
||||
}
|
||||
|
||||
return $frame;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,317 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Split.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square\QrCode;
|
||||
|
||||
use Com\Tecnick\Barcode\Exception as BarcodeException;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\QrCode\Split
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* @phpstan-import-type Item from \Com\Tecnick\Barcode\Type\Square\QrCode\Estimate
|
||||
*/
|
||||
class Split
|
||||
{
|
||||
protected function getEncMode(string $key): int
|
||||
{
|
||||
return Data::ENC_MODES[$key] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Input items
|
||||
*
|
||||
* @var array<int, Item>
|
||||
*/
|
||||
protected array $items = [];
|
||||
|
||||
/**
|
||||
* Initialize
|
||||
*
|
||||
* @param EncodingMode $encodingMode EncodingMode class object
|
||||
* @param int $hint Encoding mode
|
||||
* @param int $version Code version
|
||||
*/
|
||||
public function __construct(
|
||||
/**
|
||||
* EncodingMode class object
|
||||
*/
|
||||
protected EncodingMode $encodingMode,
|
||||
/**
|
||||
* Encoding mode
|
||||
*/
|
||||
protected int $hint,
|
||||
/**
|
||||
* QR code version.
|
||||
* The Size of QRcode is defined as version. Version is an integer value from 1 to 40.
|
||||
* Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
|
||||
* So version 40 is 177*177 matrix.
|
||||
*/
|
||||
protected int $version,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Split the input string
|
||||
*
|
||||
* @param string $data Data
|
||||
*
|
||||
* @return array<int, Item> items
|
||||
*
|
||||
* @throws BarcodeException in case input data cannot be split
|
||||
*/
|
||||
public function getSplittedString(string $data): array
|
||||
{
|
||||
$modeNm = $this->getEncMode('NM');
|
||||
$modeAn = $this->getEncMode('AN');
|
||||
$modeKj = $this->getEncMode('KJ');
|
||||
while (\strlen($data) > 0) {
|
||||
$mode = $this->encodingMode->getEncodingMode($data, 0);
|
||||
switch ($mode) {
|
||||
case $modeNm:
|
||||
$length = $this->eatNum($data);
|
||||
break;
|
||||
case $modeAn:
|
||||
$length = $this->eatAn($data);
|
||||
break;
|
||||
case $modeKj:
|
||||
if ($this->hint === $modeKj) {
|
||||
$length = $this->eatKanji($data);
|
||||
break;
|
||||
}
|
||||
|
||||
$length = $this->eat8($data);
|
||||
break;
|
||||
default:
|
||||
$length = $this->eat8($data);
|
||||
break;
|
||||
}
|
||||
|
||||
if ($length === 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ($length < 0) {
|
||||
throw new BarcodeException('Error while splitting the input data');
|
||||
}
|
||||
|
||||
$data = \substr($data, $length);
|
||||
}
|
||||
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* eatNum
|
||||
*
|
||||
* @param string $data Data
|
||||
*
|
||||
* @return int run
|
||||
*
|
||||
* @throws BarcodeException in case of invalid split input item
|
||||
*/
|
||||
protected function eatNum(string $data): int
|
||||
{
|
||||
$modeNm = $this->getEncMode('NM');
|
||||
$modeAn = $this->getEncMode('AN');
|
||||
$mode8b = $this->getEncMode('8B');
|
||||
$lng = $this->encodingMode->getLengthIndicator($modeNm, $this->version);
|
||||
$pos = 0;
|
||||
while ($this->encodingMode->isDigitAt($data, $pos)) {
|
||||
++$pos;
|
||||
}
|
||||
|
||||
$mode = $this->encodingMode->getEncodingMode($data, $pos);
|
||||
if ($mode === $mode8b) {
|
||||
$dif =
|
||||
$this->encodingMode->estimateBitsModeNum($pos) + 4 + $lng + $this->encodingMode->estimateBitsMode8(1) // + 4 + l8
|
||||
- $this->encodingMode->estimateBitsMode8($pos + 1); // - 4 - l8
|
||||
if ($dif > 0) {
|
||||
return $this->eat8($data);
|
||||
}
|
||||
}
|
||||
|
||||
if ($mode === $modeAn) {
|
||||
$dif =
|
||||
$this->encodingMode->estimateBitsModeNum($pos) + 4 + $lng + $this->encodingMode->estimateBitsModeAn(1) // + 4 + la
|
||||
- $this->encodingMode->estimateBitsModeAn($pos + 1); // - 4 - la
|
||||
if ($dif > 0) {
|
||||
return $this->eatAn($data);
|
||||
}
|
||||
}
|
||||
|
||||
$this->items = $this->encodingMode->appendNewInputItem($this->items, $modeNm, $pos, \str_split($data));
|
||||
return $pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* eatAn
|
||||
*
|
||||
* @param string $data Data
|
||||
*
|
||||
* @return int run
|
||||
*
|
||||
* @throws BarcodeException in case of invalid split input item
|
||||
*/
|
||||
protected function eatAn(string $data): int
|
||||
{
|
||||
$modeAn = $this->getEncMode('AN');
|
||||
$modeNm = $this->getEncMode('NM');
|
||||
$lag = $this->encodingMode->getLengthIndicator($modeAn, $this->version);
|
||||
$lng = $this->encodingMode->getLengthIndicator($modeNm, $this->version);
|
||||
$pos = 1;
|
||||
while ($this->encodingMode->isAlphanumericAt($data, $pos)) {
|
||||
if ($this->encodingMode->isDigitAt($data, $pos)) {
|
||||
$qix = $pos;
|
||||
while ($this->encodingMode->isDigitAt($data, $qix)) {
|
||||
++$qix;
|
||||
}
|
||||
|
||||
$dif =
|
||||
$this->encodingMode->estimateBitsModeAn($pos) // + 4 + lag
|
||||
+ $this->encodingMode->estimateBitsModeNum($qix - $pos)
|
||||
+ 4
|
||||
+ $lng
|
||||
- $this->encodingMode->estimateBitsModeAn($qix); // - 4 - la
|
||||
if ($dif < 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$pos = $qix;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$this->encodingMode->isDigitAt($data, $pos)) {
|
||||
++$pos;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->encodingMode->isAlphanumericAt($data, $pos)) {
|
||||
$dif =
|
||||
$this->encodingMode->estimateBitsModeAn($pos) + 4 + $lag + $this->encodingMode->estimateBitsMode8(1) // + 4 + l8
|
||||
- $this->encodingMode->estimateBitsMode8($pos + 1); // - 4 - l8
|
||||
if ($dif > 0) {
|
||||
return $this->eat8($data);
|
||||
}
|
||||
}
|
||||
|
||||
$this->items = $this->encodingMode->appendNewInputItem($this->items, $modeAn, $pos, \str_split($data));
|
||||
return $pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* eatKanji
|
||||
*
|
||||
* @param string $data Data
|
||||
*
|
||||
* @return int run
|
||||
*
|
||||
* @throws BarcodeException in case of invalid split input item
|
||||
*/
|
||||
protected function eatKanji(string $data): int
|
||||
{
|
||||
$modeKj = $this->getEncMode('KJ');
|
||||
$pos = 0;
|
||||
while ($this->encodingMode->getEncodingMode($data, $pos) === $modeKj) {
|
||||
$pos += 2;
|
||||
}
|
||||
|
||||
$this->items = $this->encodingMode->appendNewInputItem($this->items, $modeKj, $pos, \str_split($data));
|
||||
return $pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* eat8
|
||||
*
|
||||
* @param string $data Data
|
||||
*
|
||||
* @return int run
|
||||
*
|
||||
* @throws BarcodeException in case of invalid split input item
|
||||
*/
|
||||
protected function eat8(string $data): int
|
||||
{
|
||||
$modeAn = $this->getEncMode('AN');
|
||||
$modeNm = $this->getEncMode('NM');
|
||||
$modeKj = $this->getEncMode('KJ');
|
||||
$mode8b = $this->getEncMode('8B');
|
||||
$lag = $this->encodingMode->getLengthIndicator($modeAn, $this->version);
|
||||
$lng = $this->encodingMode->getLengthIndicator($modeNm, $this->version);
|
||||
$pos = 1;
|
||||
$dataStrLen = \strlen($data);
|
||||
while ($pos < $dataStrLen) {
|
||||
$mode = $this->encodingMode->getEncodingMode($data, $pos);
|
||||
if ($mode === $modeKj) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ($mode === $modeNm) {
|
||||
$qix = $pos;
|
||||
while ($this->encodingMode->isDigitAt($data, $qix)) {
|
||||
++$qix;
|
||||
}
|
||||
|
||||
$dif =
|
||||
$this->encodingMode->estimateBitsMode8($pos) // + 4 + l8
|
||||
+ $this->encodingMode->estimateBitsModeNum($qix - $pos)
|
||||
+ 4
|
||||
+ $lng
|
||||
- $this->encodingMode->estimateBitsMode8($qix); // - 4 - l8
|
||||
if ($dif < 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$pos = $qix;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($mode === $modeAn) {
|
||||
$qix = $pos;
|
||||
while ($this->encodingMode->isAlphanumericAt($data, $qix)) {
|
||||
++$qix;
|
||||
}
|
||||
|
||||
$dif =
|
||||
$this->encodingMode->estimateBitsMode8($pos) // + 4 + l8
|
||||
+ $this->encodingMode->estimateBitsModeAn($qix - $pos)
|
||||
+ 4
|
||||
+ $lag
|
||||
- $this->encodingMode->estimateBitsMode8($qix); // - 4 - l8
|
||||
if ($dif < 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$pos = $qix;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($mode !== $modeNm && $mode !== $modeAn) {
|
||||
++$pos;
|
||||
}
|
||||
}
|
||||
|
||||
$this->items = $this->encodingMode->appendNewInputItem($this->items, $mode8b, $pos, \str_split($data));
|
||||
return $pos;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Raw.php
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*
|
||||
* This file is part of tc-lib-barcode software library.
|
||||
*/
|
||||
|
||||
namespace Com\Tecnick\Barcode\Type\Square;
|
||||
|
||||
/**
|
||||
* Com\Tecnick\Barcode\Type\Square\Raw
|
||||
*
|
||||
* Raw Barcode type class
|
||||
* RAW MODE (comma-separated rows)
|
||||
*
|
||||
* @since 2015-02-21
|
||||
* @category Library
|
||||
* @package Barcode
|
||||
* @author Nicola Asuni <info@tecnick.com>
|
||||
* @copyright 2010-2026 Nicola Asuni - Tecnick.com LTD
|
||||
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
|
||||
* @link https://github.com/tecnickcom/tc-lib-barcode
|
||||
*/
|
||||
class Raw extends \Com\Tecnick\Barcode\Type\Raw
|
||||
{
|
||||
/**
|
||||
* Barcode type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const TYPE = 'square';
|
||||
|
||||
/**
|
||||
* Barcode format
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const FORMAT = 'SRAW';
|
||||
}
|
||||
Reference in New Issue
Block a user