Skip to content

Commit

Permalink
Add support for multiple FNC1 codes in C128C (GS1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Oct 13, 2016
1 parent e6fe260 commit 88da0ec
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.15.1
1.15.2
2 changes: 1 addition & 1 deletion src/Type/Linear/CodeOneTwoEight.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class CodeOneTwoEight extends \Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight\P
* @var array
*/
protected $fnc_a = array(241 => 102, 242 => 97, 243 => 96, 244 => 101);

/**
* Map special FNC codes for Code Set B (FNC 1-4)
*
Expand Down
9 changes: 3 additions & 6 deletions src/Type/Linear/CodeOneTwoEight/C.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class C extends \Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight
* @var string
*/
protected $format = 'C128C';

/**
* Get the code point array
*
Expand All @@ -50,16 +50,13 @@ protected function getCodeData()
$code = $this->code;
// array of symbols
$code_data = array();
// length of the code
$len = strlen($code);


$startid = 105;
if (ord($code[0]) == 241) {
$code_data[] = 102;
$code = substr($code, 1);
--$len;
}
$this->getCodeDataC($code_data, $code, $len);
$this->getCodeDataC($code_data, $code);
return $this->finalizeCodeData($code_data, $startid);
}
}
38 changes: 25 additions & 13 deletions src/Type/Linear/CodeOneTwoEight/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,37 @@ protected function getCodeDataB(&$code_data, $code, $len)
*
* @param array $code_data Array of codepoints to alter
* @param string $code Code to process
* @param int $len Number of characters to process
*
* @retun array
*
* @throws BarcodeException in case of error
*/
protected function getCodeDataC(&$code_data, $code, $len)
protected function getCodeDataC(&$code_data, $code)
{
if (($len % 2) != 0) {
throw new BarcodeException('The length must be even');
}
for ($pos = 0; $pos < $len; $pos += 2) {
$chrnum = $code[$pos].$code[($pos + 1)];
if (preg_match('/([0-9]{2})/', $chrnum) > 0) {
$code_data[] = intval($chrnum);
} else {
throw new BarcodeException('Invalid character sequence');
// code blocks separated by FNC1 (chr 241)
$blocks = explode(chr(241), $code);

foreach ($blocks as $blk) {
$len = strlen($blk);

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 = $blk[$pos].$blk[($pos + 1)];
if (preg_match('/([0-9]{2})/', $chrnum) > 0) {
$code_data[] = intval($chrnum);
} else {
throw new BarcodeException('Invalid character sequence');
}
}

$code_data[] = 102;
}

// remove last 102 code
array_pop($code_data);
}

/**
Expand All @@ -218,13 +230,13 @@ protected function finalizeCodeData($code_data, $startid)
}
// 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;
}
}
13 changes: 13 additions & 0 deletions test/Linear/CodeOneTwoEight/CTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public function testGetGrid()
$expected = "11010011100111101011101100110110011101101110101110110001000010110011011011110111101101101100"
."011101011\n";
$this->assertEquals($expected, $grid);

$bobj = $this->obj->getBarcodeObj('C128C', chr(241).'00123456780000000001');
$grid = $bobj->getGrid();
$expected = "11010011100111101011101101100110010110011100100010110001110001011011000010100110110011001101"
."1001100110110011001101100110011001101100100010011001100011101011\n";
$this->assertEquals($expected, $grid);

$bobj = $this->obj->getBarcodeObj('C128C', chr(241).'42029651'.chr(241).'9405510200864168997758');
$grid = $bobj->getGrid();
$expected = "11010011100111101011101011011100011001100110101111000101101110100011110101110100010111101000"
."100110011011101000110011001101101100110011110100100110001000101000010011010111011110111101110101110"
."1100010111100101001100011101011\n";
$this->assertEquals($expected, $grid);
}

public function testInvalidLength()
Expand Down
7 changes: 0 additions & 7 deletions test/Linear/CodeOneTwoEightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,6 @@ public function testGetGrid()
."0110011011001100110110011001101100110011001101100100101111001100011101011\n";
$this->assertEquals($expected, $grid);

$bobj = $this->obj->getBarcodeObj('C128', chr(241).'2112345'.chr(241).'1109010117100101');
$grid = $bobj->getGrid();
$expected = "1101001110011110101110111101011101101110010010110011100100010110001011110111011011100100111101"
."0111010111011110110001001001100100100011001101100110011011001001110011011001000100110011011001"
."1001101100101000111101100011101011\n";
$this->assertEquals($expected, $grid);

$bobj = $this->obj->getBarcodeObj('C128', chr(241).'42029651'.chr(241).'9405510200864168997758');
$grid = $bobj->getGrid();
$expected = "11010011100111101011101111010111010110111000110011001101011110001011011101000101111011101111010"
Expand Down

0 comments on commit 88da0ec

Please sign in to comment.