Skip to content

Commit

Permalink
Adding Code128C
Browse files Browse the repository at this point in the history
  • Loading branch information
elminson committed Sep 16, 2018
1 parent 09f5050 commit ced5c15
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 37 deletions.
72 changes: 37 additions & 35 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ $barcode->generate();
Code128
![Codabar](https://github.com/elminson/barcode/blob/master/temp/code128.png)

Code128C
![Codabar](https://github.com/elminson/barcode/blob/master/temp/code128c.png)

Code128B
![Codabar](https://github.com/elminson/barcode/blob/master/temp/code128b.png)

Expand Down
35 changes: 35 additions & 0 deletions src/BarCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public function generate()
$this->code_string = $this->barcodabar();
break;
}
case "code128c":
{
$this->code_string = $this->barcodebaseC();
break;
}
default:
$this->code_string = $this->barcodebase();
}
Expand Down Expand Up @@ -230,6 +235,36 @@ private function barcodabar()
return $this->prepareBarCode($code_string);
}

private function barcodebaseC()
{
//Validate code is pair amount of characters
$chksum = $this->config_code->getChecksum($this->getCode_type());

// Must not change order of array elements as the checksum depends on the array's key to validate final code
$code_array = $this->config_code->getCode($this->getCode_type());

$code_keys = array_keys($code_array);
$code_values = array_flip($code_keys);

$code_string = "";

$code = preg_replace('/[^0-9]/', '', $this->text); // Code 128C supports numbers only
$size = strlen($code);
$i = 1;
for ($X = 1; $X <= $size; $X += 2) {
$activeKey = substr($code, ($X - 1), 2);
$code_string .= $code_array[$activeKey];
$chksum = ($chksum + ($code_values[$activeKey] * $i));
$i++;
}

if ($chksum != 0) {
$code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];
}

return $this->prepareBarCode($code_string);
}

private function barcodebase()
{

Expand Down
111 changes: 111 additions & 0 deletions src/ConfigCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ConfigCode
'code128' => ['code' => 'code128', 'chksum' => 104],
'code128b' => ['code' => 'code128b', 'chksum' => 104],
'code128a' => ['code' => 'code128a', 'chksum' => 103],
'code128c' => ['code' => 'code128c', 'chksum' => 105],
'code39' => ['code' => 'code39', 'chksum' => 0],
'code25' => ['code' => 'code25', 'chksum' => 0],
'codabar' => ['code' => 'codabar', 'chksum' => 0],
Expand Down Expand Up @@ -136,6 +137,115 @@ public function __construct()
$this->code_data_array = [
'code128' => $this->code128,
'code128b' => $this->code128,
'code128c' => [
'00' => '212222',
'01' => '222122',
'02' => '222221',
'03' => '121223',
'04' => '121322',
'05' => '131222',
'06' => '122213',
'07' => '122312',
'08' => '132212',
'09' => '221213',
'10' => '221312',
'11' => '231212',
'12' => '112232',
'13' => '122132',
'14' => '122231',
'15' => '113222',
'16' => '123122',
'17' => '123221',
'18' => '223211',
'19' => '221132',
'20' => '221231',
'21' => '213212',
'22' => '223112',
'23' => '312131',
'24' => '311222',
'25' => '321122',
'26' => '321221',
'27' => '312212',
'28' => '322112',
'29' => '322211',
'30' => '212123',
'31' => '212321',
'32' => '232121',
'33' => '111323',
'34' => '131123',
'35' => '131321',
'36' => '112313',
'37' => '132113',
'38' => '132311',
'39' => '211313',
'40' => '231113',
'41' => '231311',
'42' => '112133',
'43' => '112331',
'44' => '132131',
'45' => '113123',
'46' => '113321',
'47' => '133121',
'48' => '313121',
'49' => '211331',
'50' => '231131',
'51' => '213113',
'52' => '213311',
'53' => '213131',
'54' => '311123',
'55' => '311321',
'56' => '331121',
'57' => '312113',
'58' => '312311',
'59' => '332111',
'60' => '314111',
'61' => '221411',
'62' => '431111',
'63' => '111224',
'64' => '111422',
'65' => '121124',
'66' => '121421',
'67' => '141122',
'68' => '141221',
'69' => '112214',
'70' => '112412',
'71' => '122114',
'72' => '122411',
'73' => '142112',
'74' => '142211',
'75' => '241211',
'76' => '221114',
'77' => '413111',
'78' => '241112',
'79' => '134111',
'80' => '111242',
'81' => '121142',
'82' => '121241',
'83' => '114212',
'84' => '124112',
'85' => '124211',
'86' => '411212',
'87' => '421112',
'88' => '421211',
'89' => '212141',
'90' => '214121',
'91' => '412121',
'92' => '111143',
'93' => '111341',
'94' => '131141',
'95' => '114113',
'96' => '114311',
'97' => '411113',
'98' => '411311',
'99' => '113141',
'CODE B' => '114131',
'FNC 4' => '311141',
'FNC 1' => '411131',
'Start A' => '211412',
'Start B' => '211214',
'Start C' => '211232',
'Stop' => '2331112'
],
'code128a' => [
" " => "212222",
"!" => "222122",
Expand Down Expand Up @@ -333,6 +443,7 @@ public function __construct()
'code128' => ['start' => '211214', 'end' => '2331112'],
'code128b' => ['start' => '211214', 'end' => '2331112'],
'code128a' => ['start' => '211412', 'end' => '2331112'],
'code128c' => ['start' => '211232', 'end' => '2331112'],
'code39' => ['start' => '1211212111', 'end' => '121121211'],
'code25' => ['start' => '1111', 'end' => '311'],
'codabar' => ['start' => '11221211', 'end' => '1122121'],
Expand Down
Binary file added temp/code128c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ced5c15

Please sign in to comment.