Skip to content

Commit

Permalink
Encode pairs of numbers in type C from the end to the start
Browse files Browse the repository at this point in the history
Leaving odd characters in the previous encoding. See
https://en.wikipedia.org/wiki/Code_128#Barcode_length_optimization
  • Loading branch information
Caffe1neAdd1ct committed May 29, 2019
1 parent c0ced18 commit 9ed85d3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/Type/Linear/CodeOneTwoEight/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,25 @@ protected function getNumericSequence($code)
if (!empty($numseq[1])) {
$end_offset = 0;
foreach ($numseq[1] as $val) {
// offset to the start of numeric substr
$offset = $val[1];

// numeric sequence
$slen = strlen($val[0]);
if (($slen % 2) != 0) {
// the length must be even
--$slen;
// add 1 to start of offset so numbers are c type encoded "from the end"
++$offset;
}

if ($offset > $end_offset) {
// non numeric sequence
$sequence = array_merge(
$sequence,
$this->get128ABsequence(substr($code, $end_offset, ($offset - $end_offset)))
);
}
// numeric sequence
$slen = strlen($val[0]);
if (($slen % 2) != 0) {
// the length must be even
--$slen;
}
$sequence[] = array('C', substr($code, $offset, $slen), $slen);
$end_offset = $offset + $slen;
}
Expand Down
12 changes: 6 additions & 6 deletions test/Linear/CodeOneTwoEightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public function testGetGrid()

$bobj = $this->obj->getBarcodeObj('C128', '12345678901');
$grid = $bobj->getGrid();
$expected = "110100111001011001110010001011000111000101101100001010011011110110101111011101001110011010111100"
."1001100011101011\n";
$expected = "110100100001001110011010111011110111011011101011101100010000101100110110111101100110110011001100"
. "1101100011101011\n";
$this->assertEquals($expected, $grid);

$bobj = $this->obj->getBarcodeObj('C128', '1234');
Expand All @@ -93,8 +93,8 @@ public function testGetGrid()

$bobj = $this->obj->getBarcodeObj('C128', 'HI34567A');
$grid = $bobj->getGrid();
$expected = "1101001000011000101000110001000101011101111010001011000111000101101011110111011101101110101000"
."11000100110001001100011101011\n";
$expected = "1101001000011000101000110001000101100101110010111011110101110110001000010110010111101110101000"
."11000100100011001100011101011\n";
$this->assertEquals($expected, $grid);

$bobj = $this->obj->getBarcodeObj('C128', 'Barcode 1');
Expand All @@ -117,8 +117,8 @@ public function testGetGrid()

$bobj = $this->obj->getBarcodeObj('C128', chr(241).'0000801234999999999');
$grid = $bobj->getGrid();
$expected = "1101001110011110101110111101011101101100110011011001100101001111001011001110010001011000101110"
."111101011101111010111011110101110111101011110111011100101100110011101001100011101011\n";
$expected = "1101001000011110101110100111011001011101111011011001100100011001001100110110011101101"
."1101101000111010111011110101110111101011101111010111011110110100010001100011101011\n";
$this->assertEquals($expected, $grid);

$bobj = $this->obj->getBarcodeObj('C128', chr(241)."000000\tABCDEF");
Expand Down

0 comments on commit 9ed85d3

Please sign in to comment.