PageRenderTime 51ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/monica/vendor/zendframework/zendframework/library/Zend/Barcode/Object/Code128.php

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 313 lines | 210 code | 28 blank | 75 comment | 38 complexity | 50108de4eb6128e0a0acd54b3112a3e4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Barcode\Object;
  10. /**
  11. * Class for generate Code128 barcode
  12. */
  13. class Code128 extends AbstractObject
  14. {
  15. /**
  16. * Drawing of checksum
  17. * (even if it's sometime optional, most of time it's required)
  18. * @var bool
  19. */
  20. protected $withChecksum = true;
  21. /**
  22. * @var array
  23. */
  24. protected $convertedText = array();
  25. protected $codingMap = array(
  26. 0 => "11011001100", 1 => "11001101100", 2 => "11001100110",
  27. 3 => "10010011000", 4 => "10010001100", 5 => "10001001100",
  28. 6 => "10011001000", 7 => "10011000100", 8 => "10001100100",
  29. 9 => "11001001000", 10 => "11001000100", 11 => "11000100100",
  30. 12 => "10110011100", 13 => "10011011100", 14 => "10011001110",
  31. 15 => "10111001100", 16 => "10011101100", 17 => "10011100110",
  32. 18 => "11001110010", 19 => "11001011100", 20 => "11001001110",
  33. 21 => "11011100100", 22 => "11001110100", 23 => "11101101110",
  34. 24 => "11101001100", 25 => "11100101100", 26 => "11100100110",
  35. 27 => "11101100100", 28 => "11100110100", 29 => "11100110010",
  36. 30 => "11011011000", 31 => "11011000110", 32 => "11000110110",
  37. 33 => "10100011000", 34 => "10001011000", 35 => "10001000110",
  38. 36 => "10110001000", 37 => "10001101000", 38 => "10001100010",
  39. 39 => "11010001000", 40 => "11000101000", 41 => "11000100010",
  40. 42 => "10110111000", 43 => "10110001110", 44 => "10001101110",
  41. 45 => "10111011000", 46 => "10111000110", 47 => "10001110110",
  42. 48 => "11101110110", 49 => "11010001110", 50 => "11000101110",
  43. 51 => "11011101000", 52 => "11011100010", 53 => "11011101110",
  44. 54 => "11101011000", 55 => "11101000110", 56 => "11100010110",
  45. 57 => "11101101000", 58 => "11101100010", 59 => "11100011010",
  46. 60 => "11101111010", 61 => "11001000010", 62 => "11110001010",
  47. 63 => "10100110000", 64 => "10100001100", 65 => "10010110000",
  48. 66 => "10010000110", 67 => "10000101100", 68 => "10000100110",
  49. 69 => "10110010000", 70 => "10110000100", 71 => "10011010000",
  50. 72 => "10011000010", 73 => "10000110100", 74 => "10000110010",
  51. 75 => "11000010010", 76 => "11001010000", 77 => "11110111010",
  52. 78 => "11000010100", 79 => "10001111010", 80 => "10100111100",
  53. 81 => "10010111100", 82 => "10010011110", 83 => "10111100100",
  54. 84 => "10011110100", 85 => "10011110010", 86 => "11110100100",
  55. 87 => "11110010100", 88 => "11110010010", 89 => "11011011110",
  56. 90 => "11011110110", 91 => "11110110110", 92 => "10101111000",
  57. 93 => "10100011110", 94 => "10001011110", 95 => "10111101000",
  58. 96 => "10111100010", 97 => "11110101000", 98 => "11110100010",
  59. 99 => "10111011110", 100 => "10111101110", 101 => "11101011110",
  60. 102 => "11110101110",
  61. 103 => "11010000100", 104 => "11010010000", 105 => "11010011100",
  62. 106 => "1100011101011");
  63. /**
  64. * Character sets ABC
  65. * @var array
  66. */
  67. protected $charSets = array(
  68. 'A' => array(
  69. ' ', '!', '"', '#', '$', '%', '&', "'",
  70. '(', ')', '*', '+', ',', '-', '.', '/',
  71. '0', '1', '2', '3', '4', '5', '6', '7',
  72. '8', '9', ':', ';', '<', '=', '>', '?',
  73. '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
  74. 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  75. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
  76. 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
  77. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  78. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  79. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  80. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  81. 'FNC3', 'FNC2', 'SHIFT', 'Code C', 'Code B', 'FNC4', 'FNC1',
  82. 'START A', 'START B', 'START C', 'STOP'),
  83. 'B' => array(
  84. ' ', '!', '"', '#', '$', '%', '&', "'",
  85. '(', ')', '*', '+', ',', '-', '.', '/',
  86. '0', '1', '2', '3', '4', '5', '6', '7',
  87. '8', '9', ':', ';', '<', '=', '>', '?',
  88. '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
  89. 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  90. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
  91. 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
  92. '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  93. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  94. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  95. 'x', 'y', 'z', '{', '|', '}', '~', 0x7F,
  96. 'FNC3', 'FNC2', 'SHIFT', 'Code C', 'FNC4', 'Code A', 'FNC1',
  97. 'START A', 'START B', 'START C', 'STOP',),
  98. 'C' => array(
  99. '00', '01', '02', '03', '04', '05', '06', '07', '08', '09',
  100. '10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
  101. '20', '21', '22', '23', '24', '25', '26', '27', '28', '29',
  102. '30', '31', '32', '33', '34', '35', '36', '37', '38', '39',
  103. '40', '41', '42', '43', '44', '45', '46', '47', '48', '49',
  104. '50', '51', '52', '53', '54', '55', '56', '57', '58', '59',
  105. '60', '61', '62', '63', '64', '65', '66', '67', '68', '69',
  106. '70', '71', '72', '73', '74', '75', '76', '77', '78', '79',
  107. '80', '81', '82', '83', '84', '85', '86', '87', '88', '89',
  108. '90', '91', '92', '93', '94', '95', '96', '97', '98', '99',
  109. 'Code B', 'Code A', 'FNC1', 'START A', 'START B', 'START C', 'STOP'));
  110. /**
  111. * Width of the barcode (in pixels)
  112. * @return integer
  113. */
  114. protected function calculateBarcodeWidth()
  115. {
  116. $quietZone = $this->getQuietZone();
  117. // Each characters contain 11 bars...
  118. $characterLength = 11 * $this->barThinWidth * $this->factor;
  119. $convertedChars = count($this->convertToBarcodeChars($this->getText()));
  120. if ($this->withChecksum) {
  121. $convertedChars++;
  122. }
  123. $encodedData = $convertedChars * $characterLength;
  124. // ...except the STOP character (13)
  125. $encodedData += $characterLength + 2 * $this->barThinWidth * $this->factor;
  126. $width = $quietZone + $encodedData + $quietZone;
  127. return $width;
  128. }
  129. /**
  130. * Partial check of code128 barcode
  131. * @return void
  132. */
  133. protected function checkSpecificParams()
  134. {
  135. }
  136. /**
  137. * Prepare array to draw barcode
  138. * @return array
  139. */
  140. protected function prepareBarcode()
  141. {
  142. $barcodeTable = array();
  143. $convertedChars = $this->convertToBarcodeChars($this->getText());
  144. if ($this->withChecksum) {
  145. $convertedChars[] = $this->getChecksum($this->getText());
  146. }
  147. // STOP CHARACTER
  148. $convertedChars[] = 106;
  149. foreach ($convertedChars as $barcodeChar) {
  150. $barcodePattern = $this->codingMap[$barcodeChar];
  151. foreach (str_split($barcodePattern) as $c) {
  152. $barcodeTable[] = array($c, $this->barThinWidth, 0, 1);
  153. }
  154. }
  155. return $barcodeTable;
  156. }
  157. /**
  158. * Checks if the next $length chars of $string starting at $pos are numeric.
  159. * Returns false if the end of the string is reached.
  160. * @param string $string String to search
  161. * @param int $pos Starting position
  162. * @param int $length Length to search
  163. * @return bool
  164. */
  165. protected static function _isDigit($string, $pos, $length = 2)
  166. {
  167. if ($pos + $length > strlen($string)) {
  168. return false;
  169. }
  170. for ($i = $pos; $i < $pos + $length; $i++) {
  171. if (!is_numeric($string[$i])) {
  172. return false;
  173. }
  174. }
  175. return true;
  176. }
  177. /**
  178. * Convert string to barcode string
  179. * @param string $string
  180. * @return array
  181. */
  182. protected function convertToBarcodeChars($string)
  183. {
  184. $string = (string) $string;
  185. if (!strlen($string)) {
  186. return array();
  187. }
  188. if (isset($this->convertedText[md5($string)])) {
  189. return $this->convertedText[md5($string)];
  190. }
  191. $currentCharset = null;
  192. $sum = 0;
  193. $fak = 0;
  194. $result = array();
  195. for ($pos = 0; $pos < strlen($string); $pos++) {
  196. $char = $string[$pos];
  197. $code = null;
  198. if (static::_isDigit($string, $pos, 4) && $currentCharset != 'C'
  199. || static::_isDigit($string, $pos, 2) && $currentCharset == 'C') {
  200. /**
  201. * Switch to C if the next 4 chars are numeric or stay C if the next 2
  202. * chars are numeric
  203. */
  204. if ($currentCharset != 'C') {
  205. if ($pos == 0) {
  206. $code = array_search("START C", $this->charSets['C']);
  207. } else {
  208. $code = array_search("Code C", $this->charSets[$currentCharset]);
  209. }
  210. $result[] = $code;
  211. $currentCharset = 'C';
  212. }
  213. } elseif (in_array($char, $this->charSets['B']) && $currentCharset != 'B'
  214. && !(in_array($char, $this->charSets['A']) && $currentCharset == 'A')) {
  215. /**
  216. * Switch to B as B contains the char and B is not the current charset.
  217. */
  218. if ($pos == 0) {
  219. $code = array_search("START B", $this->charSets['B']);
  220. } else {
  221. $code = array_search("Code B", $this->charSets[$currentCharset]);
  222. }
  223. $result[] = $code;
  224. $currentCharset = 'B';
  225. } elseif (array_key_exists($char, $this->charSets['A']) && $currentCharset != 'A'
  226. && !(array_key_exists($char, $this->charSets['B']) && $currentCharset == 'B')) {
  227. /**
  228. * Switch to C as C contains the char and C is not the current charset.
  229. */
  230. if ($pos == 0) {
  231. $code = array_search("START A", $this->charSets['A']);
  232. } else {
  233. $code =array_search("Code A", $this->charSets[$currentCharset]);
  234. }
  235. $result[] = $code;
  236. $currentCharset = 'A';
  237. }
  238. if ($currentCharset == 'C') {
  239. $code = array_search(substr($string, $pos, 2), $this->charSets['C']);
  240. $pos++; //Two chars from input
  241. } else {
  242. $code = array_search($string[$pos], $this->charSets[$currentCharset]);
  243. }
  244. $result[] = $code;
  245. }
  246. $this->convertedText[md5($string)] = $result;
  247. return $result;
  248. }
  249. /**
  250. * Set text to encode
  251. * @param string $value
  252. * @return Code128
  253. */
  254. public function setText($value)
  255. {
  256. $this->text = $value;
  257. return $this;
  258. }
  259. /**
  260. * Retrieve text to encode
  261. * @return string
  262. */
  263. public function getText()
  264. {
  265. return $this->text;
  266. }
  267. /**
  268. * Get barcode checksum
  269. *
  270. * @param string $text
  271. * @return int
  272. */
  273. public function getChecksum($text)
  274. {
  275. $tableOfChars = $this->convertToBarcodeChars($text);
  276. $sum = $tableOfChars[0];
  277. unset($tableOfChars[0]);
  278. $k = 1;
  279. foreach ($tableOfChars as $char) {
  280. $sum += ($k++) * $char;
  281. }
  282. $checksum = $sum % 103;
  283. return $checksum;
  284. }
  285. }