BCGcode128.barcode.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - Code 128, A, B, C
  6. *
  7. * # Code C Working properly only on PHP4 or PHP5.0.3+ due to bug :
  8. * http://bugs.php.net/bug.php?id=28862
  9. *
  10. * !! Warning !!
  11. * If you display the checksum on the label, you may obtain
  12. * some garbage since some characters are not displayable.
  13. *
  14. *--------------------------------------------------------------------
  15. * Copyright (C) Jean-Sebastien Goupil
  16. * http://www.barcodephp.com
  17. */
  18. include_once('BCGParseException.php');
  19. include_once('BCGBarcode1D.php');
  20. define('CODE128_A', 1); // Table A
  21. define('CODE128_B', 2); // Table B
  22. define('CODE128_C', 3); // Table C
  23. class BCGcode128 extends BCGBarcode1D {
  24. const KEYA_FNC3 = 96;
  25. const KEYA_FNC2 = 97;
  26. const KEYA_SHIFT = 98;
  27. const KEYA_CODEC = 99;
  28. const KEYA_CODEB = 100;
  29. const KEYA_FNC4 = 101;
  30. const KEYA_FNC1 = 102;
  31. const KEYB_FNC3 = 96;
  32. const KEYB_FNC2 = 97;
  33. const KEYB_SHIFT = 98;
  34. const KEYB_CODEC = 99;
  35. const KEYB_FNC4 = 100;
  36. const KEYB_CODEA = 101;
  37. const KEYB_FNC1 = 102;
  38. const KEYC_CODEB = 100;
  39. const KEYC_CODEA = 101;
  40. const KEYC_FNC1 = 102;
  41. const KEY_STARTA = 103;
  42. const KEY_STARTB = 104;
  43. const KEY_STARTC = 105;
  44. const KEY_STOP = 106;
  45. protected $keysA, $keysB, $keysC;
  46. private $starting_text;
  47. private $indcheck, $data, $lastTable;
  48. private $tilde;
  49. private $shift;
  50. private $latch;
  51. private $fnc;
  52. private $METHOD = null; // Array of method available to create Code128 (CODE128_A, CODE128_B, CODE128_C)
  53. /**
  54. * Constructor.
  55. *
  56. * @param char $start
  57. */
  58. public function __construct($start = null) {
  59. parent::__construct();
  60. /* CODE 128 A */
  61. $this->keysA = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_';
  62. for ($i = 0; $i < 32; $i++) {
  63. $this->keysA .= chr($i);
  64. }
  65. /* CODE 128 B */
  66. $this->keysB = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' . chr(127);
  67. /* CODE 128 C */
  68. $this->keysC = '0123456789';
  69. $this->code = array(
  70. '101111', /* 00 */
  71. '111011', /* 01 */
  72. '111110', /* 02 */
  73. '010112', /* 03 */
  74. '010211', /* 04 */
  75. '020111', /* 05 */
  76. '011102', /* 06 */
  77. '011201', /* 07 */
  78. '021101', /* 08 */
  79. '110102', /* 09 */
  80. '110201', /* 10 */
  81. '120101', /* 11 */
  82. '001121', /* 12 */
  83. '011021', /* 13 */
  84. '011120', /* 14 */
  85. '002111', /* 15 */
  86. '012011', /* 16 */
  87. '012110', /* 17 */
  88. '112100', /* 18 */
  89. '110021', /* 19 */
  90. '110120', /* 20 */
  91. '102101', /* 21 */
  92. '112001', /* 22 */
  93. '201020', /* 23 */
  94. '200111', /* 24 */
  95. '210011', /* 25 */
  96. '210110', /* 26 */
  97. '201101', /* 27 */
  98. '211001', /* 28 */
  99. '211100', /* 29 */
  100. '101012', /* 30 */
  101. '101210', /* 31 */
  102. '121010', /* 32 */
  103. '000212', /* 33 */
  104. '020012', /* 34 */
  105. '020210', /* 35 */
  106. '001202', /* 36 */
  107. '021002', /* 37 */
  108. '021200', /* 38 */
  109. '100202', /* 39 */
  110. '120002', /* 40 */
  111. '120200', /* 41 */
  112. '001022', /* 42 */
  113. '001220', /* 43 */
  114. '021020', /* 44 */
  115. '002012', /* 45 */
  116. '002210', /* 46 */
  117. '022010', /* 47 */
  118. '202010', /* 48 */
  119. '100220', /* 49 */
  120. '120020', /* 50 */
  121. '102002', /* 51 */
  122. '102200', /* 52 */
  123. '102020', /* 53 */
  124. '200012', /* 54 */
  125. '200210', /* 55 */
  126. '220010', /* 56 */
  127. '201002', /* 57 */
  128. '201200', /* 58 */
  129. '221000', /* 59 */
  130. '203000', /* 60 */
  131. '110300', /* 61 */
  132. '320000', /* 62 */
  133. '000113', /* 63 */
  134. '000311', /* 64 */
  135. '010013', /* 65 */
  136. '010310', /* 66 */
  137. '030011', /* 67 */
  138. '030110', /* 68 */
  139. '001103', /* 69 */
  140. '001301', /* 70 */
  141. '011003', /* 71 */
  142. '011300', /* 72 */
  143. '031001', /* 73 */
  144. '031100', /* 74 */
  145. '130100', /* 75 */
  146. '110003', /* 76 */
  147. '302000', /* 77 */
  148. '130001', /* 78 */
  149. '023000', /* 79 */
  150. '000131', /* 80 */
  151. '010031', /* 81 */
  152. '010130', /* 82 */
  153. '003101', /* 83 */
  154. '013001', /* 84 */
  155. '013100', /* 85 */
  156. '300101', /* 86 */
  157. '310001', /* 87 */
  158. '310100', /* 88 */
  159. '101030', /* 89 */
  160. '103010', /* 90 */
  161. '301010', /* 91 */
  162. '000032', /* 92 */
  163. '000230', /* 93 */
  164. '020030', /* 94 */
  165. '003002', /* 95 */
  166. '003200', /* 96 */
  167. '300002', /* 97 */
  168. '300200', /* 98 */
  169. '002030', /* 99 */
  170. '003020', /* 100*/
  171. '200030', /* 101*/
  172. '300020', /* 102*/
  173. '100301', /* 103*/
  174. '100103', /* 104*/
  175. '100121', /* 105*/
  176. '122000' /*STOP*/
  177. );
  178. $this->setStart($start);
  179. $this->setTilde(true);
  180. // Latches and Shifts
  181. $this->latch = array(
  182. array(null, self::KEYA_CODEB, self::KEYA_CODEC),
  183. array(self::KEYB_CODEA, null, self::KEYB_CODEC),
  184. array(self::KEYC_CODEA, self::KEYC_CODEB, null)
  185. );
  186. $this->shift = array(
  187. array(null, self::KEYA_SHIFT),
  188. array(self::KEYB_SHIFT, null)
  189. );
  190. $this->fnc = array(
  191. array(self::KEYA_FNC1, self::KEYA_FNC2, self::KEYA_FNC3, self::KEYA_FNC4),
  192. array(self::KEYB_FNC1, self::KEYB_FNC2, self::KEYB_FNC3, self::KEYB_FNC4),
  193. array(self::KEYC_FNC1, null, null, null)
  194. );
  195. // Method available
  196. $this->METHOD = array(CODE128_A => 'A', CODE128_B => 'B', CODE128_C => 'C');
  197. }
  198. /**
  199. * Specifies the start code. Can be 'A', 'B', 'C', or null
  200. * - Table A: Capitals + ASCII 0-31 + punct
  201. * - Table B: Capitals + LowerCase + punct
  202. * - Table C: Numbers
  203. *
  204. * If null is specified, the table selection is automatically made.
  205. * The default is null.
  206. *
  207. * @param string $table
  208. */
  209. public function setStart($table) {
  210. if ($table !== 'A' && $table !== 'B' && $table !== 'C' && $table !== null) {
  211. throw new BCGArgumentException('The starting table must be A, B, C or null.', 'table');
  212. }
  213. $this->starting_text = $table;
  214. }
  215. /**
  216. * Gets the tilde.
  217. *
  218. * @return bool
  219. */
  220. public function getTilde() {
  221. return $this->tilde;
  222. }
  223. /**
  224. * Accepts tilde to be process as a special character.
  225. * If true, you can do this:
  226. * - ~~ : to make ONE tilde
  227. * - ~Fx : to insert FCNx. x is equal from 1 to 4.
  228. *
  229. * @param boolean $accept
  230. */
  231. public function setTilde($accept) {
  232. $this->tilde = (bool)$accept;
  233. }
  234. /**
  235. * Parses the text before displaying it.
  236. *
  237. * @param mixed $text
  238. */
  239. public function parse($text) {
  240. $this->setStartFromText($text);
  241. $this->text = '';
  242. $seq = '';
  243. $currentMode = $this->starting_text;
  244. // Here, we format correctly what the user gives.
  245. if (!is_array($text)) {
  246. $seq = $this->getSequence($text, $currentMode);
  247. $this->text = $text;
  248. } else {
  249. // This loop checks for UnknownText AND raises an exception if a character is not allowed in a table
  250. reset($text);
  251. while (list($key1, $val1) = each($text)) { // We take each value
  252. if (!is_array($val1)) { // This is not a table
  253. if (is_string($val1)) { // If it's a string, parse as unknown
  254. $seq .= $this->getSequence($val1, $currentMode);
  255. $this->text .= $val1;
  256. } else {
  257. // it's the case of "array(ENCODING, 'text')"
  258. // We got ENCODING in $val1, calling 'each' again will get 'text' in $val2
  259. list($key2, $val2) = each($text);
  260. $seq .= $this->{'setParse' . $this->METHOD[$val1]}($val2, $currentMode);
  261. $this->text .= $val2;
  262. }
  263. } else { // The method is specified
  264. // $val1[0] = ENCODING
  265. // $val1[1] = 'text'
  266. $value = isset($val1[1]) ? $val1[1] : ''; // If data available
  267. $seq .= $this->{'setParse' . $this->METHOD[$val1[0]]}($value, $currentMode);
  268. $this->text .= $value;
  269. }
  270. }
  271. }
  272. if ($seq !== '') {
  273. $bitstream = $this->createBinaryStream($this->text, $seq);
  274. $this->setData($bitstream);
  275. }
  276. $this->addDefaultLabel();
  277. }
  278. /**
  279. * Draws the barcode.
  280. *
  281. * @param resource $im
  282. */
  283. public function draw($im) {
  284. $c = count($this->data);
  285. for ($i = 0; $i < $c; $i++) {
  286. $this->drawChar($im, $this->data[$i], true);
  287. }
  288. $this->drawChar($im, '1', true);
  289. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  290. }
  291. /**
  292. * Returns the maximal size of a barcode.
  293. *
  294. * @param int $w
  295. * @param int $h
  296. * @return int[]
  297. */
  298. public function getDimension($w, $h) {
  299. // Contains start + text + checksum + stop
  300. $textlength = count($this->data) * 11;
  301. $endlength = 2; // + final bar
  302. $w += $textlength + $endlength;
  303. $h += $this->thickness;
  304. return parent::getDimension($w, $h);
  305. }
  306. /**
  307. * Validates the input.
  308. */
  309. protected function validate() {
  310. $c = count($this->data);
  311. if ($c === 0) {
  312. throw new BCGParseException('code128', 'No data has been entered.');
  313. }
  314. parent::validate();
  315. }
  316. /**
  317. * Overloaded method to calculate checksum.
  318. */
  319. protected function calculateChecksum() {
  320. // Checksum
  321. // First Char (START)
  322. // + Starting with the first data character following the start character,
  323. // take the value of the character (between 0 and 102, inclusive) multiply
  324. // it by its character position (1) and add that to the running checksum.
  325. // Modulated 103
  326. $this->checksumValue = $this->indcheck[0];
  327. $c = count($this->indcheck);
  328. for ($i = 1; $i < $c; $i++) {
  329. $this->checksumValue += $this->indcheck[$i] * $i;
  330. }
  331. $this->checksumValue = $this->checksumValue % 103;
  332. }
  333. /**
  334. * Overloaded method to display the checksum.
  335. */
  336. protected function processChecksum() {
  337. if ($this->checksumValue === false) { // Calculate the checksum only once
  338. $this->calculateChecksum();
  339. }
  340. if ($this->checksumValue !== false) {
  341. if ($this->lastTable === 'C') {
  342. return (string)$this->checksumValue;
  343. }
  344. return $this->{'keys' . $this->lastTable}[$this->checksumValue];
  345. }
  346. return false;
  347. }
  348. /**
  349. * Specifies the starting_text table if none has been specified earlier.
  350. *
  351. * @param string $text
  352. */
  353. private function setStartFromText($text) {
  354. if ($this->starting_text === null) {
  355. // If we have a forced table at the start, we get that one...
  356. if (is_array($text)) {
  357. if (is_array($text[0])) {
  358. // Code like array(array(ENCODING, ''))
  359. $this->starting_text = $this->METHOD[$text[0][0]];
  360. return;
  361. } else {
  362. if (is_string($text[0])) {
  363. // Code like array('test') (Automatic text)
  364. $text = $text[0];
  365. } else {
  366. // Code like array(ENCODING, '')
  367. $this->starting_text = $this->METHOD[$text[0]];
  368. return;
  369. }
  370. }
  371. }
  372. // At this point, we had an "automatic" table selection...
  373. // If we can get at least 4 numbers, go in C; otherwise go in B.
  374. $tmp = preg_quote($this->keysC, '/');
  375. $length = strlen($text);
  376. if ($length >= 4 && preg_match('/[' . $tmp . ']/', substr($text, 0, 4))) {
  377. $this->starting_text = 'C';
  378. } else {
  379. if ($length > 0 && strpos($this->keysB, $text[0]) !== false) {
  380. $this->starting_text = 'B';
  381. } else {
  382. $this->starting_text = 'A';
  383. }
  384. }
  385. }
  386. }
  387. /**
  388. * Extracts the ~ value from the $text at the $pos.
  389. * If the tilde is not ~~, ~F1, ~F2, ~F3, ~F4; an error is raised.
  390. *
  391. * @param string $text
  392. * @param int $pos
  393. * @return string
  394. */
  395. private static function extractTilde($text, $pos) {
  396. if ($text[$pos] === '~') {
  397. if (isset($text[$pos + 1])) {
  398. // Do we have a tilde?
  399. if ($text[$pos + 1] === '~') {
  400. return '~~';
  401. } elseif ($text[$pos + 1] === 'F') {
  402. // Do we have a number after?
  403. if (isset($text[$pos + 2])) {
  404. $v = intval($text[$pos + 2]);
  405. if ($v >= 1 && $v <= 4) {
  406. return '~F' . $v;
  407. } else {
  408. throw new BCGParseException('code128', 'Bad ~F. You must provide a number from 1 to 4.');
  409. }
  410. } else {
  411. throw new BCGParseException('code128', 'Bad ~F. You must provide a number from 1 to 4.');
  412. }
  413. } else {
  414. throw new BCGParseException('code128', 'Wrong code after the ~.');
  415. }
  416. } else {
  417. throw new BCGParseException('code128', 'Wrong code after the ~.');
  418. }
  419. } else {
  420. throw new BCGParseException('code128', 'There is no ~ at this location.');
  421. }
  422. }
  423. /**
  424. * Gets the "dotted" sequence for the $text based on the $currentMode.
  425. * There is also a check if we use the special tilde ~
  426. *
  427. * @param string $text
  428. * @param string $currentMode
  429. * @return string
  430. */
  431. private function getSequenceParsed($text, $currentMode) {
  432. if ($this->tilde) {
  433. $sequence = '';
  434. $previousPos = 0;
  435. while (($pos = strpos($text, '~', $previousPos)) !== false) {
  436. $tildeData = self::extractTilde($text, $pos);
  437. $simpleTilde = ($tildeData === '~~');
  438. if ($simpleTilde && $currentMode !== 'B') {
  439. throw new BCGParseException('code128', 'The Table ' . $currentMode . ' doesn\'t contain the character ~.');
  440. }
  441. // At this point, we know we have ~Fx
  442. if ($tildeData !== '~F1' && $currentMode === 'C') {
  443. // The mode C doesn't support ~F2, ~F3, ~F4
  444. throw new BCGParseException('code128', 'The Table C doesn\'t contain the function ' . $tildeData . '.');
  445. }
  446. $length = $pos - $previousPos;
  447. if ($currentMode === 'C') {
  448. if ($length % 2 === 1) {
  449. throw new BCGParseException('code128', 'The text "' . $text . '" must have an even number of character to be encoded in Table C.');
  450. }
  451. }
  452. $sequence .= str_repeat('.', $length);
  453. $sequence .= '.';
  454. $sequence .= (!$simpleTilde) ? 'F' : '';
  455. $previousPos = $pos + strlen($tildeData);
  456. }
  457. // Flushing
  458. $length = strlen($text) - $previousPos;
  459. if ($currentMode === 'C') {
  460. if ($length % 2 === 1) {
  461. throw new BCGParseException('code128', 'The text "' . $text . '" must have an even number of character to be encoded in Table C.');
  462. }
  463. }
  464. $sequence .= str_repeat('.', $length);
  465. return $sequence;
  466. } else {
  467. return str_repeat('.', strlen($text));
  468. }
  469. }
  470. /**
  471. * Parses the text and returns the appropriate sequence for the Table A.
  472. *
  473. * @param string $text
  474. * @param string $currentMode
  475. * @return string
  476. */
  477. private function setParseA($text, &$currentMode) {
  478. $tmp = preg_quote($this->keysA, '/');
  479. // If we accept the ~ for special character, we must allow it.
  480. if ($this->tilde) {
  481. $tmp .= '~';
  482. }
  483. $match = array();
  484. if (preg_match('/[^' . $tmp . ']/', $text, $match) === 1) {
  485. // We found something not allowed
  486. throw new BCGParseException('code128', 'The text "' . $text . '" can\'t be parsed with the Table A. The character "' . $match[0] . '" is not allowed.');
  487. } else {
  488. $latch = ($currentMode === 'A') ? '' : '0';
  489. $currentMode = 'A';
  490. return $latch . $this->getSequenceParsed($text, $currentMode);
  491. }
  492. }
  493. /**
  494. * Parses the text and returns the appropriate sequence for the Table B.
  495. *
  496. * @param string $text
  497. * @param string $currentMode
  498. * @return string
  499. */
  500. private function setParseB($text, &$currentMode) {
  501. $tmp = preg_quote($this->keysB, '/');
  502. $match = array();
  503. if (preg_match('/[^' . $tmp . ']/', $text, $match) === 1) {
  504. // We found something not allowed
  505. throw new BCGParseException('code128', 'The text "' . $text . '" can\'t be parsed with the Table B. The character "' . $match[0] . '" is not allowed.');
  506. } else {
  507. $latch = ($currentMode === 'B') ? '' : '1';
  508. $currentMode = 'B';
  509. return $latch . $this->getSequenceParsed($text, $currentMode);
  510. }
  511. }
  512. /**
  513. * Parses the text and returns the appropriate sequence for the Table C.
  514. *
  515. * @param string $text
  516. * @param string $currentMode
  517. * @return string
  518. */
  519. private function setParseC($text, &$currentMode) {
  520. $tmp = preg_quote($this->keysC, '/');
  521. // If we accept the ~ for special character, we must allow it.
  522. if ($this->tilde) {
  523. $tmp .= '~F';
  524. }
  525. $match = array();
  526. if (preg_match('/[^' . $tmp . ']/', $text, $match) === 1) {
  527. // We found something not allowed
  528. throw new BCGParseException('code128', 'The text "' . $text . '" can\'t be parsed with the Table C. The character "' . $match[0] . '" is not allowed.');
  529. } else {
  530. $latch = ($currentMode === 'C') ? '' : '2';
  531. $currentMode = 'C';
  532. return $latch . $this->getSequenceParsed($text, $currentMode);
  533. }
  534. }
  535. /**
  536. * Depending on the $text, it will return the correct
  537. * sequence to encode the text.
  538. *
  539. * @param string $text
  540. * @param string $starting_text
  541. * @return string
  542. */
  543. private function getSequence($text, &$starting_text) {
  544. $e = 10000;
  545. $latLen = array(
  546. array(0, 1, 1),
  547. array(1, 0, 1),
  548. array(1, 1, 0)
  549. );
  550. $shftLen = array(
  551. array($e, 1, $e),
  552. array(1, $e, $e),
  553. array($e, $e, $e)
  554. );
  555. $charSiz = array(2, 2, 1);
  556. $startA = $e;
  557. $startB = $e;
  558. $startC = $e;
  559. if ($starting_text === 'A') { $startA = 0; }
  560. if ($starting_text === 'B') { $startB = 0; }
  561. if ($starting_text === 'C') { $startC = 0; }
  562. $curLen = array($startA, $startB, $startC);
  563. $curSeq = array(null, null, null);
  564. $nextNumber = false;
  565. $x = 0;
  566. $xLen = strlen($text);
  567. for ($x = 0; $x < $xLen; $x++) {
  568. $input = $text[$x];
  569. // 1.
  570. for ($i = 0; $i < 3; $i++) {
  571. for ($j = 0; $j < 3; $j++) {
  572. if (($curLen[$i] + $latLen[$i][$j]) < $curLen[$j]) {
  573. $curLen[$j] = $curLen[$i] + $latLen[$i][$j];
  574. $curSeq[$j] = $curSeq[$i] . $j;
  575. }
  576. }
  577. }
  578. // 2.
  579. $nxtLen = array($e, $e, $e);
  580. $nxtSeq = array();
  581. // 3.
  582. $flag = false;
  583. $posArray = array();
  584. // Special case, we do have a tilde and we process them
  585. if ($this->tilde && $input === '~') {
  586. $tildeData = self::extractTilde($text, $x);
  587. if ($tildeData === '~~') {
  588. // We simply skip a tilde
  589. $posArray[] = 1;
  590. $x++;
  591. } elseif (substr($tildeData, 0, 2) === '~F') {
  592. $v = intval($tildeData[2]);
  593. $posArray[] = 0;
  594. $posArray[] = 1;
  595. if ($v === 1) {
  596. $posArray[] = 2;
  597. }
  598. $x += 2;
  599. $flag = true;
  600. }
  601. } else {
  602. $pos = strpos($this->keysA, $input);
  603. if ($pos !== false) {
  604. $posArray[] = 0;
  605. }
  606. $pos = strpos($this->keysB, $input);
  607. if ($pos !== false) {
  608. $posArray[] = 1;
  609. }
  610. // Do we have the next char a number?? OR a ~F1
  611. $pos = strpos($this->keysC, $input);
  612. if ($nextNumber || ($pos !== false && isset($text[$x + 1]) && strpos($this->keysC, $text[$x + 1]) !== false)) {
  613. $nextNumber = !$nextNumber;
  614. $posArray[] = 2;
  615. }
  616. }
  617. $c = count($posArray);
  618. for ($i = 0; $i < $c; $i++) {
  619. if (($curLen[$posArray[$i]] + $charSiz[$posArray[$i]]) < $nxtLen[$posArray[$i]]) {
  620. $nxtLen[$posArray[$i]] = $curLen[$posArray[$i]] + $charSiz[$posArray[$i]];
  621. $nxtSeq[$posArray[$i]] = $curSeq[$posArray[$i]] . '.';
  622. }
  623. for ($j = 0; $j < 2; $j++) {
  624. if ($j === $posArray[$i]) { continue; }
  625. if (($curLen[$j] + $shftLen[$j][$posArray[$i]] + $charSiz[$posArray[$i]]) < $nxtLen[$j]) {
  626. $nxtLen[$j] = $curLen[$j] + $shftLen[$j][$posArray[$i]] + $charSiz[$posArray[$i]];
  627. $nxtSeq[$j] = $curSeq[$j] . chr($posArray[$i] + 65) . '.';
  628. }
  629. }
  630. }
  631. if ($c === 0) {
  632. // We found an unsuported character
  633. throw new BCGParseException('code128', 'Character ' . $input . ' not supported.');
  634. }
  635. if ($flag) {
  636. for ($i = 0; $i < 5; $i++) {
  637. if (isset($nxtSeq[$i])) {
  638. $nxtSeq[$i] .= 'F';
  639. }
  640. }
  641. }
  642. // 4.
  643. for ($i = 0; $i < 3; $i++) {
  644. $curLen[$i] = $nxtLen[$i];
  645. if (isset($nxtSeq[$i])) {
  646. $curSeq[$i] = $nxtSeq[$i];
  647. }
  648. }
  649. }
  650. // Every curLen under $e is possible but we take the smallest
  651. $m = $e;
  652. $k = -1;
  653. for ($i = 0; $i < 3; $i++) {
  654. if ($curLen[$i] < $m) {
  655. $k = $i;
  656. $m = $curLen[$i];
  657. }
  658. }
  659. if ($k === -1) {
  660. return '';
  661. }
  662. return $curSeq[$k];
  663. }
  664. /**
  665. * Depending on the sequence $seq given (returned from getSequence()),
  666. * this method will return the code stream in an array. Each char will be a
  667. * string of bit based on the Code 128.
  668. *
  669. * Each letter from the sequence represents bits.
  670. *
  671. * 0 to 2 are latches
  672. * A to B are Shift + Letter
  673. * . is a char in the current encoding
  674. *
  675. * @param string $text
  676. * @param string $seq
  677. * @return string[][]
  678. */
  679. private function createBinaryStream($text, $seq) {
  680. $c = strlen($seq);
  681. $data = array(); // code stream
  682. $indcheck = array(); // index for checksum
  683. $currentEncoding = 0;
  684. if ($this->starting_text === 'A') {
  685. $currentEncoding = 0;
  686. $indcheck[] = self::KEY_STARTA;
  687. $this->lastTable = 'A';
  688. } elseif ($this->starting_text === 'B') {
  689. $currentEncoding = 1;
  690. $indcheck[] = self::KEY_STARTB;
  691. $this->lastTable = 'B';
  692. } elseif ($this->starting_text === 'C') {
  693. $currentEncoding = 2;
  694. $indcheck[] = self::KEY_STARTC;
  695. $this->lastTable = 'C';
  696. }
  697. $data[] = $this->code[103 + $currentEncoding];
  698. $temporaryEncoding = -1;
  699. for ($i = 0, $counter = 0; $i < $c; $i++) {
  700. $input = $seq[$i];
  701. $inputI = intval($input);
  702. if ($input === '.') {
  703. $this->encodeChar($data, $currentEncoding, $seq, $text, $i, $counter, $indcheck);
  704. if ($temporaryEncoding !== -1) {
  705. $currentEncoding = $temporaryEncoding;
  706. $temporaryEncoding = -1;
  707. }
  708. } elseif ($input >= 'A' && $input <= 'B') {
  709. // We shift
  710. $encoding = ord($input) - 65;
  711. $shift = $this->shift[$currentEncoding][$encoding];
  712. $indcheck[] = $shift;
  713. $data[] = $this->code[$shift];
  714. if ($temporaryEncoding === -1) {
  715. $temporaryEncoding = $currentEncoding;
  716. }
  717. $currentEncoding = $encoding;
  718. } elseif ($inputI >= 0 && $inputI < 3) {
  719. $temporaryEncoding = -1;
  720. // We latch
  721. $latch = $this->latch[$currentEncoding][$inputI];
  722. if ($latch !== null) {
  723. $indcheck[] = $latch;
  724. $this->lastTable = chr(65 + $inputI);
  725. $data[] = $this->code[$latch];
  726. $currentEncoding = $inputI;
  727. }
  728. }
  729. }
  730. return array($indcheck, $data);
  731. }
  732. /**
  733. * Encodes characters, base on its encoding and sequence
  734. *
  735. * @param int[] $data
  736. * @param int $encoding
  737. * @param string $seq
  738. * @param string $text
  739. * @param int $i
  740. * @param int $counter
  741. * @param int[] $indcheck
  742. */
  743. private function encodeChar(&$data, $encoding, $seq, $text, &$i, &$counter, &$indcheck) {
  744. if (isset($seq[$i + 1]) && $seq[$i + 1] === 'F') {
  745. // We have a flag !!
  746. if ($text[$counter + 1] === 'F') {
  747. $number = $text[$counter + 2];
  748. $fnc = $this->fnc[$encoding][$number - 1];
  749. $indcheck[] = $fnc;
  750. $data[] = $this->code[$fnc];
  751. // Skip F + number
  752. $counter += 2;
  753. } else {
  754. // Not supposed
  755. }
  756. $i++;
  757. } else {
  758. if ($encoding === 2) {
  759. // We take 2 numbers in the same time
  760. $code = (int)substr($text, $counter, 2);
  761. $indcheck[] = $code;
  762. $data[] = $this->code[$code];
  763. $counter++;
  764. $i++;
  765. } else {
  766. $keys = ($encoding === 0) ? $this->keysA : $this->keysB;
  767. $pos = strpos($keys, $text[$counter]);
  768. $indcheck[] = $pos;
  769. $data[] = $this->code[$pos];
  770. }
  771. }
  772. $counter++;
  773. }
  774. /**
  775. * Saves data into the classes.
  776. *
  777. * This method will save data, calculate real column number
  778. * (if -1 was selected), the real error level (if -1 was
  779. * selected)... It will add Padding to the end and generate
  780. * the error codes.
  781. *
  782. * @param array $data
  783. */
  784. private function setData($data) {
  785. $this->indcheck = $data[0];
  786. $this->data = $data[1];
  787. $this->calculateChecksum();
  788. $this->data[] = $this->code[$this->checksumValue];
  789. $this->data[] = $this->code[self::KEY_STOP];
  790. }
  791. }
  792. ?>