Skip to content

Commit

Permalink
AZTEC Code encoder (#79)
Browse files Browse the repository at this point in the history
* Raise minimum PH version to 5.6 and use constant arrays.
* Add AZTEC Encoder
  • Loading branch information
nicolaasuni committed Oct 20, 2023
1 parent b74f197 commit 8181d55
Show file tree
Hide file tree
Showing 39 changed files with 2,421 additions and 214 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/check.yml
Expand Up @@ -28,9 +28,6 @@ jobs:
os: [ubuntu-latest]
coverage-extension: [pcov]
include:
#- { php-version: '5.3', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' }
#- { php-version: '5.4', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' }
- { php-version: '5.5', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' }
- { php-version: '5.6', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' }
- { php-version: '7.1', experimental: false, os: ubuntu-latest, coverage-extension: 'xdebug' }
steps:
Expand Down
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -53,6 +53,7 @@ This library includes utility PHP classes to generate linear and bidimensional b
* CODE11 : CODE 11
* PHARMA : PHARMACODE
* PHARMA2T : PHARMACODE TWO-TRACKS
* AZTEC : AZTEC Code (ISO/IEC 24778:2008)
* DATAMATRIX : DATAMATRIX (ISO/IEC 16022)
* PDF417 : PDF417 (ISO/IEC 15438:2006)
* QRCODE : QR-CODE
Expand Down Expand Up @@ -146,15 +147,15 @@ Create a composer.json in your projects root-directory:
```json
{
"require": {
"tecnickcom/tc-lib-barcode": "^1.17"
"tecnickcom/tc-lib-barcode": "^1.18"
}
}
```

Or add to an existing project with:

```bash
composer require tecnickcom/tc-lib-barcode ^1.17
composer require tecnickcom/tc-lib-barcode ^1.18
```

## Packaging
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
1.17.38
1.18.0
58 changes: 31 additions & 27 deletions composer.json
Expand Up @@ -5,41 +5,45 @@
"homepage": "http://www.tecnick.com",
"license": "LGPL-3.0-or-later",
"keywords": [
"tc-lib-barcode",
"3 of 9",
"ANSI MH10.8M-1983",
"AZTEC",
"barcode",
"CBC",
"CODABAR",
"CODE 11",
"CODE 128 A B C",
"CODE 39",
"ANSI MH10.8M-1983",
"USD-3",
"3 of 9",
"CODE 93",
"USS-93",
"Standard 2 of 5",
"Interleaved 2 of 5",
"CODE 128 A B C",
"UPC",
"EAN 8",
"Datamatrix",
"EAN 13",
"UPC-A",
"UPC-E",
"MSI",
"POSTNET",
"PLANET",
"RMS4CC",
"Royal Mail",
"CBC",
"EAN 8",
"ECC200",
"Intelligent Mail Barcode",
"Interleaved 2 of 5",
"ISO/IEC 15438:2006",
"ISO/IEC 16022",
"ISO/IEC 24778:2008",
"KIX",
"Klant",
"Intelligent Mail Barcode",
"MSI",
"Onecode",
"USPS-B-3200",
"CODABAR",
"CODE 11",
"PHARMACODE",
"PDF417",
"PHARMACODE TWO-TRACKS",
"Datamatrix",
"ECC200",
"PHARMACODE",
"PLANET",
"POSTNET",
"QR-Code",
"PDF417"
"RMS4CC",
"Royal Mail",
"Standard 2 of 5",
"tc-lib-barcode",
"UPC-A",
"UPC-E",
"UPC",
"USD-3",
"USPS-B-3200",
"USS-93"
],
"authors": [
{
Expand All @@ -49,7 +53,7 @@
}
],
"require": {
"php": ">=5.4",
"php": ">=5.6",
"ext-bcmath": "*",
"ext-date": "*",
"ext-gd": "*",
Expand Down
2 changes: 2 additions & 0 deletions example/index.php
Expand Up @@ -57,6 +57,8 @@
$square = array(
'LRAW' => array('0101010101', '1D RAW MODE (comma-separated rows of 01 strings)'),
'SRAW' => array('0101,1010', '2D RAW MODE (comma-separated rows of 01 strings)'),
'AZTEC' => array('ABCDabcd01234', 'AZTEC (ISO/IEC 24778:2008)'),
'AZTEC,50,A,A' => array('ABCDabcd01234', 'AZTEC (ISO/IEC 24778:2008)'),
'PDF417' => array('0123456789', 'PDF417 (ISO/IEC 15438:2006)'),
'QRCODE' => array('0123456789', 'QR-CODE'),
'QRCODE,H,ST,0,0' => array('abcdefghijklmnopqrstuvwxy0123456789', 'QR-CODE WITH PARAMETERS'),
Expand Down
29 changes: 14 additions & 15 deletions src/Barcode.php
Expand Up @@ -35,45 +35,44 @@ class Barcode
{
/**
* Array containing the map between the barcode type and correspondent class
*
* @var array
*/
protected static $typeclass = array(
const TYPECLASS = array(
'C128' => 'Linear\\CodeOneTwoEight', // CODE 128
'C128A' => 'Linear\\CodeOneTwoEight\\CodeOneTwoEightA', // CODE 128 A
'C128B' => 'Linear\\CodeOneTwoEight\\CodeOneTwoEightB', // CODE 128 B
'C128C' => 'Linear\\CodeOneTwoEight\\CodeOneTwoEightC', // CODE 128 C
'C128' => 'Linear\\CodeOneTwoEight', // CODE 128
'C39E+' => 'Linear\\CodeThreeNineExtCheck', // CODE 39 EXTENDED + CHECKSUM
'C39E' => 'Linear\\CodeThreeNineExt', // CODE 39 EXTENDED
'C39+' => 'Linear\\CodeThreeNineCheck', // CODE 39 + CHECKSUM
'C39' => 'Linear\\CodeThreeNine', // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
'C39+' => 'Linear\\CodeThreeNineCheck', // CODE 39 + CHECKSUM
'C39E' => 'Linear\\CodeThreeNineExt', // CODE 39 EXTENDED
'C39E+' => 'Linear\\CodeThreeNineExtCheck', // CODE 39 EXTENDED + CHECKSUM
'C93' => 'Linear\\CodeNineThree', // CODE 93 - USS-93
'CODABAR' => 'Linear\\Codabar', // CODABAR
'CODE11' => 'Linear\\CodeOneOne', // CODE 11
'EAN13' => 'Linear\\EanOneThree', // EAN 13
'EAN2' => 'Linear\\EanTwo', // EAN 2-Digits UPC-Based Extension
'EAN5' => 'Linear\\EanFive', // EAN 5-Digits UPC-Based Extension
'EAN8' => 'Linear\\EanEight', // EAN 8
'I25+' => 'Linear\\InterleavedTwoOfFiveCheck', // Interleaved 2 of 5 + CHECKSUM
'I25' => 'Linear\\InterleavedTwoOfFive', // Interleaved 2 of 5
'I25+' => 'Linear\\InterleavedTwoOfFiveCheck', // Interleaved 2 of 5 + CHECKSUM
'IMB' => 'Linear\\Imb', // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
'IMBPRE' => 'Linear\\ImbPre', // IMB - Intelligent Mail Barcode pre-processed
'KIX' => 'Linear\\KlantIndex', // KIX (Klant index - Customer index)
'MSI+' => 'Linear\\MsiCheck', // MSI + CHECKSUM (modulo 11)
'LRAW' => 'Linear\\Raw', // 1D RAW MODE (comma-separated rows of 01 strings)
'MSI' => 'Linear\\Msi', // MSI (Variation of Plessey code)
'PHARMA2T' => 'Linear\\PharmaTwoTracks', // PHARMACODE TWO-TRACKS
'MSI+' => 'Linear\\MsiCheck', // MSI + CHECKSUM (modulo 11)
'PHARMA' => 'Linear\\Pharma', // PHARMACODE
'PHARMA2T' => 'Linear\\PharmaTwoTracks', // PHARMACODE TWO-TRACKS
'PLANET' => 'Linear\\Planet', // PLANET
'POSTNET' => 'Linear\\Postnet', // POSTNET
'RMS4CC' => 'Linear\\RoyalMailFourCc', // RMS4CC (Royal Mail 4-state Customer Bar Code)
'S25+' => 'Linear\\StandardTwoOfFiveCheck', // Standard 2 of 5 + CHECKSUM
'S25' => 'Linear\\StandardTwoOfFive', // Standard 2 of 5
'S25+' => 'Linear\\StandardTwoOfFiveCheck', // Standard 2 of 5 + CHECKSUM
'UPCA' => 'Linear\\UpcA', // UPC-A
'UPCE' => 'Linear\\UpcE', // UPC-E
'AZTEC' => 'Square\\Aztec', // AZTEC Code (ISO/IEC 24778:2008)
'DATAMATRIX' => 'Square\\Datamatrix', // DATAMATRIX (ISO/IEC 16022)
'PDF417' => 'Square\\PdfFourOneSeven', // PDF417 (ISO/IEC 15438:2006)
'QRCODE' => 'Square\\QrCode', // QR-CODE
'LRAW' => 'Linear\\Raw', // 1D RAW MODE (comma-separated rows of 01 strings)
'SRAW' => 'Square\\Raw', // 2D RAW MODE (comma-separated rows of 01 strings)
);

Expand All @@ -84,7 +83,7 @@ class Barcode
*/
public function getTypes()
{
return array_keys(self::$typeclass);
return array_keys(self::TYPECLASS);
}

/**
Expand Down Expand Up @@ -116,10 +115,10 @@ public function getBarcodeObj(
$params = explode(',', $type);
$type = array_shift($params);

if (empty(self::$typeclass[$type])) {
if (!array_key_exists($type, self::TYPECLASS)) {
throw new BarcodeException('Unsupported barcode type: ' . $type);
}
$bclass = '\\Com\\Tecnick\\Barcode\\Type\\' . self::$typeclass[$type];
$bclass = '\\Com\\Tecnick\\Barcode\\Type\\' . self::TYPECLASS[$type];
return new $bclass($code, $width, $height, $color, $params, $padding);
}
}
12 changes: 6 additions & 6 deletions src/Type/Linear/Imb.php
Expand Up @@ -67,7 +67,7 @@ class Imb extends \Com\Tecnick\Barcode\Type\Linear
*
* @var array
*/
protected static $asc_chr = array(
const ASC_CHR = array(
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,
Expand All @@ -82,7 +82,7 @@ class Imb extends \Com\Tecnick\Barcode\Type\Linear
*
* @var array
*/
protected static $dsc_chr = array(
const DSC_CHR = array(
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,
Expand All @@ -96,7 +96,7 @@ class Imb extends \Com\Tecnick\Barcode\Type\Linear
*
* @var array
*/
protected static $asc_pos = array(
const ASC_POS = array(
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);

Expand All @@ -105,7 +105,7 @@ class Imb extends \Com\Tecnick\Barcode\Type\Linear
*
* @var array
*/
protected static $dsc_pos = array(
const DSC_POS = array(
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);

Expand Down Expand Up @@ -312,8 +312,8 @@ protected function setBars()
{
$chars = $this->getCharsArray();
for ($pos = 0; $pos < 65; ++$pos) {
$asc = (($chars[self::$asc_chr[$pos]] & pow(2, self::$asc_pos[$pos])) > 0);
$dsc = (($chars[self::$dsc_chr[$pos]] & pow(2, self::$dsc_pos[$pos])) > 0);
$asc = (($chars[self::ASC_CHR[$pos]] & pow(2, self::ASC_POS[$pos])) > 0);
$dsc = (($chars[self::DSC_CHR[$pos]] & pow(2, self::DSC_POS[$pos])) > 0);
if ($asc and $dsc) {
// full bar (F)
$this->bars[] = array($this->ncols, 0, 1, 3);
Expand Down
134 changes: 134 additions & 0 deletions src/Type/Square/Aztec.php
@@ -0,0 +1,134 @@
<?php

/**
* Aztec.php
*
* @since 2023-10-12
* @category Library
* @package Barcode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2023-2023 Nicola Asuni - Tecnick.com LTD
* @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @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\Type\Square\Aztec\Data;
use Com\Tecnick\Barcode\Type\Square\Aztec\Encode;
use Com\Tecnick\Barcode\Exception as BarcodeException;

/**
* 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-2023 Nicola Asuni - Tecnick.com LTD
* @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-barcode
*/
class Aztec extends \Com\Tecnick\Barcode\Type\Square
{
/**
* Barcode format
*
* @var string
*/
protected $format = 'AZTEC';

/**
* Error correction code percentage of error check words.
* A minimum of 23% + 3 words is recommended by ISO/IEC 24778:2008a.
*
* @var int
*/
protected $ecc = 33;

/**
* Encoding mode
*
* @var string
*/
protected $hint = 'A';

/**
* Mode:
* - A = Automatic selection between Compact (priority) and Full Range.
* - F = Force Full Range mode.
*
* @var string
*/
protected $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.
*
* @var int
*/
protected $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.
*/
protected function setParameters()
{
parent::setParameters();

// ecc percentage
if (!isset($this->params[0]) || !in_array($this->params[0], range(1, 100))) {
$this->params[0] = 33;
}
$this->ecc = intval($this->params[0]);

// hint
if (!isset($this->params[1]) || !in_array($this->params[1], ['A', 'B'])) {
$this->params[1] = 'A';
}
$this->hint = $this->params[1];

// mode
if (!isset($this->params[2]) || !in_array($this->params[2], ['A', 'F'])) {
$this->params[2] = 'A';
}
$this->mode = $this->params[2];

// eci code. Used to set the charset encoding. See $this->eci.
if (!isset($this->params[3]) || !array_key_exists($this->params[3], Data::ECI)) {
$this->params[3] = -1;
}
$this->eci = intval($this->params[3]);
}

/**
* Get the bars array
*
* @throws BarcodeException in case of error
*/
protected function setBars()
{
if (strlen((string)$this->code) == 0) {
throw new BarcodeException('Empty input');
}
try {
$enc = new Encode($this->code, $this->ecc, $this->eci, $this->hint, $this->mode);
$grid = $enc->getGrid();
$this->processBinarySequence($grid);
} catch (BarcodeException $e) {
throw new BarcodeException('AZTEC: ' . $e->getMessage());
}
}
}

0 comments on commit 8181d55

Please sign in to comment.