PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/xml2pdf/plugins/barcodes/xml2pdf.barcode.code39.php

https://github.com/ericmaicon/jasperToPHP
PHP | 304 lines | 170 code | 28 blank | 106 comment | 11 complexity | bcd978b5c02aa1b2bb2cc09d988cacea MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * code39 barcode tag plugin file.
  4. * @filesource
  5. *
  6. * @author guillaume l. <guillaume@geelweb.org>
  7. * @link http://www.geelweb.org geelweb-dot-org
  8. * @license http://opensource.org/licenses/bsd-license.php BSD License
  9. * @copyright Copyright Š 2006, guillaume luchet
  10. * @version CVS: $Id: xml2pdf.barcode.code39.php,v 1.2 2007/01/05 23:07:31 geelweb Exp $
  11. * @package Xml2Pdf
  12. * @subpackage Barcode
  13. */
  14. // dependances {{{
  15. /**
  16. *
  17. */
  18. require_once(XML2PDF_PLUGINS_TAGS_PATH . '/xml2pdf.tag.barcode.php');
  19. // }}}
  20. // doc {{{
  21. /**
  22. * code39 barcode type plugin.
  23. *
  24. * Thanks to Emmanuel Havet for the script found on
  25. * {@link http://www.fpdf.org fpdf.org}
  26. *
  27. * @author guillaume l. <guillaume@geelweb.org>
  28. * @link http://www.geelweb.org geelweb-dot-org
  29. * @license http://opensource.org/licenses/bsd-license.php BSD License
  30. * @copyright Copyright Š 2006, guillaume luchet
  31. * @version CVS: $Id: xml2pdf.barcode.code39.php,v 1.2 2007/01/05 23:07:31 geelweb Exp $
  32. * @package Xml2Pdf
  33. * @subpackage Barcode
  34. */ // }}}
  35. class xml2pdf_barcode_code39 {
  36. // xml2pdf_barcode_code39::__construct() {{{
  37. /**
  38. * Constructor
  39. */
  40. public function __construct() {}
  41. // }}}
  42. // xml2pdf_barcode_code39::render() {{{
  43. /**
  44. * Render an EAN13 barcode
  45. *
  46. * @param object $barcode an xml2pdf_tag_barcode object
  47. * @return void
  48. */
  49. public static function render($barcode) {
  50. $width = $barcode->width?$barcode->width:0.4;
  51. $height = $barcode->height?$barcode->height:20;
  52. xml2pdf_barcode_code39::Code39($barcode->x, $barcode->y,
  53. $barcode->barcode, false, false, $width, $height, true,
  54. $barcode->pdf);
  55. }
  56. // }}}
  57. // xml2pdf_barcode_code39::Code39() {{{
  58. /**
  59. *
  60. * @param float $x
  61. * @param float $y
  62. * @param string $code
  63. * @param boolean $ext
  64. * @param boolean $cks
  65. * @param float $w
  66. * @param float $h
  67. * @param boolean $wide
  68. * @param object $pdf
  69. * @return void
  70. */
  71. public static function Code39($x, $y, $code, $ext = true, $cks = false,
  72. $w = 0.4, $h = 20, $wide = true, $pdf) {
  73. //suppression des accents
  74. $code = strtr($code, 'ŕâäéčęëěîďňôöůűü', 'aaaeeeeiiiooouuu');
  75. //affichage du code
  76. $pdf->SetFont('Arial', '', 10);
  77. $pdf->Text($x, $y+$h+4, $code);
  78. if($ext) {
  79. //encodage étendu
  80. $code = xml2pdf_barcode_code39::encode_code39_ext($code);
  81. } else {
  82. //passage en majuscules
  83. $code = strtoupper($code);
  84. //contrôle validité
  85. if(!preg_match('|^[0-9A-Z. $/+%-]*$|', $code)) {
  86. $pdf->Error('Invalid barcode value: '.$code);
  87. }
  88. }
  89. //calcul du checksum
  90. if ($cks) {
  91. $code .= xml2pdf_barcode_code39::checksum_code39($code);
  92. }
  93. //ajout des caractčres début / fin
  94. $code = '*'.$code.'*';
  95. //tableaux de correspondance caractčres / barres
  96. $narrow_encoding = array (
  97. '0' => '101001101101',
  98. '1' => '110100101011',
  99. '2' => '101100101011',
  100. '3' => '110110010101',
  101. '4' => '101001101011',
  102. '5' => '110100110101',
  103. '6' => '101100110101',
  104. '7' => '101001011011',
  105. '8' => '110100101101',
  106. '9' => '101100101101',
  107. 'A' => '110101001011',
  108. 'B' => '101101001011',
  109. 'C' => '110110100101',
  110. 'D' => '101011001011',
  111. 'E' => '110101100101',
  112. 'F' => '101101100101',
  113. 'G' => '101010011011',
  114. 'H' => '110101001101',
  115. 'I' => '101101001101',
  116. 'J' => '101011001101',
  117. 'K' => '110101010011',
  118. 'L' => '101101010011',
  119. 'M' => '110110101001',
  120. 'N' => '101011010011',
  121. 'O' => '110101101001',
  122. 'P' => '101101101001',
  123. 'Q' => '101010110011',
  124. 'R' => '110101011001',
  125. 'S' => '101101011001',
  126. 'T' => '101011011001',
  127. 'U' => '110010101011',
  128. 'V' => '100110101011',
  129. 'W' => '110011010101',
  130. 'X' => '100101101011',
  131. 'Y' => '110010110101',
  132. 'Z' => '100110110101',
  133. '-' => '100101011011',
  134. '.' => '110010101101',
  135. ' ' => '100110101101',
  136. '*' => '100101101101',
  137. '$' => '100100100101',
  138. '/' => '100100101001',
  139. '+' => '100101001001',
  140. '%' => '101001001001' );
  141. $wide_encoding = array (
  142. '0' => '101000111011101', '1' => '111010001010111',
  143. '2' => '101110001010111', '3' => '111011100010101',
  144. '4' => '101000111010111', '5' => '111010001110101',
  145. '6' => '101110001110101', '7' => '101000101110111',
  146. '8' => '111010001011101', '9' => '101110001011101',
  147. 'A' => '111010100010111', 'B' => '101110100010111',
  148. 'C' => '111011101000101', 'D' => '101011100010111',
  149. 'E' => '111010111000101', 'F' => '101110111000101',
  150. 'G' => '101010001110111', 'H' => '111010100011101',
  151. 'I' => '101110100011101', 'J' => '101011100011101',
  152. 'K' => '111010101000111', 'L' => '101110101000111',
  153. 'M' => '111011101010001', 'N' => '101011101000111',
  154. 'O' => '111010111010001', 'P' => '101110111010001',
  155. 'Q' => '101010111000111', 'R' => '111010101110001',
  156. 'S' => '101110101110001', 'T' => '101011101110001',
  157. 'U' => '111000101010111', 'V' => '100011101010111',
  158. 'W' => '111000111010101', 'X' => '100010111010111',
  159. 'Y' => '111000101110101', 'Z' => '100011101110101',
  160. '-' => '100010101110111', '.' => '111000101011101',
  161. ' ' => '100011101011101', '*' => '100010111011101',
  162. '$' => '100010001000101', '/' => '100010001010001',
  163. '+' => '100010100010001', '%' => '101000100010001');
  164. //le code barre est déterminé en version large ou étroite (meilleure lisibilité)
  165. //large observe un rapport 3:1 pour le rapport barre large / barre etroite
  166. //etroit 2:1
  167. $encoding = $wide ? $wide_encoding : $narrow_encoding;
  168. //espace inter-caractčre
  169. $gap = ($w > 0.29) ? '00' : '0';
  170. //encodage
  171. $encode = '';
  172. for ($i = 0; $i< strlen($code); $i++) {
  173. $encode .= $encoding[$code{$i}].$gap;
  174. }
  175. //dessin
  176. xml2pdf_barcode_code39::draw_code39($encode, $x, $y, $w, $h, $pdf);
  177. }
  178. // }}}
  179. // xml2pdf_barcode_code39::checksum_code39() {{{
  180. /**
  181. *
  182. * @param string $code
  183. * @return char
  184. */
  185. public static function checksum_code39($code) {
  186. //somme des positions des caractčres en démarrant de zéro
  187. //somme modulo 43
  188. //le caractčre de contrôle est celui ŕ la position du modulo
  189. //exemple : 115 % 43 = 29 -> 'T' est ŕ la place 29 dans le tableau
  190. $chars = array(
  191. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  192. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  193. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  194. 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%');
  195. $sum = 0;
  196. for ($i=0 ; $i<strlen($code); $i++) {
  197. $a = array_keys($chars, $code{$i});
  198. $sum += $a[0];
  199. }
  200. $r = $sum % 43;
  201. return $chars[$r];
  202. }
  203. // }}}
  204. // xml2pdf_barcode_code39::encode_code39_ext() {{{
  205. /**
  206. *
  207. * @param string $code
  208. * @return string
  209. */
  210. public static function encode_code39_ext($code) {
  211. //encodage en code 39 étendu
  212. $encode = array(
  213. chr(0) => '%U', chr(1) => '$A', chr(2) => '$B', chr(3) => '$C',
  214. chr(4) => '$D', chr(5) => '$E', chr(6) => '$F', chr(7) => '$G',
  215. chr(8) => '$H', chr(9) => '$I', chr(10) => '$J', chr(11) => 'ŁK',
  216. chr(12) => '$L', chr(13) => '$M', chr(14) => '$N', chr(15) => '$O',
  217. chr(16) => '$P', chr(17) => '$Q', chr(18) => '$R', chr(19) => '$S',
  218. chr(20) => '$T', chr(21) => '$U', chr(22) => '$V', chr(23) => '$W',
  219. chr(24) => '$X', chr(25) => '$Y', chr(26) => '$Z', chr(27) => '%A',
  220. chr(28) => '%B', chr(29) => '%C', chr(30) => '%D', chr(31) => '%E',
  221. chr(32) => ' ', chr(33) => '/A', chr(34) => '/B', chr(35) => '/C',
  222. chr(36) => '/D', chr(37) => '/E', chr(38) => '/F', chr(39) => '/G',
  223. chr(40) => '/H', chr(41) => '/I', chr(42) => '/J', chr(43) => '/K',
  224. chr(44) => '/L', chr(45) => '-', chr(46) => '.', chr(47) => '/O',
  225. chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
  226. chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
  227. chr(56) => '8', chr(57) => '9', chr(58) => '/Z', chr(59) => '%F',
  228. chr(60) => '%G', chr(61) => '%H', chr(62) => '%I', chr(63) => '%J',
  229. chr(64) => '%V', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C',
  230. chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
  231. chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
  232. chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
  233. chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
  234. chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
  235. chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', chr(91) => '%K',
  236. chr(92) => '%L', chr(93) => '%M', chr(94) => '%N', chr(95) => '%O',
  237. chr(96) => '%W', chr(97) => '+A', chr(98) => '+B', chr(99) => '+C',
  238. chr(100) => '+D', chr(101) => '+E', chr(102) => '+F', chr(103) => '+G',
  239. chr(104) => '+H', chr(105) => '+I', chr(106) => '+J', chr(107) => '+K',
  240. chr(108) => '+L', chr(109) => '+M', chr(110) => '+N', chr(111) => '+O',
  241. chr(112) => '+P', chr(113) => '+Q', chr(114) => '+R', chr(115) => '+S',
  242. chr(116) => '+T', chr(117) => '+U', chr(118) => '+V', chr(119) => '+W',
  243. chr(120) => '+X', chr(121) => '+Y', chr(122) => '+Z', chr(123) => '%P',
  244. chr(124) => '%Q', chr(125) => '%R', chr(126) => '%S', chr(127) => '%T');
  245. $code_ext = '';
  246. for ($i = 0 ; $i<strlen($code); $i++) {
  247. if (ord($code{$i}) > 127) {
  248. $pdf->Error('Invalid character: '.$code{$i});
  249. }
  250. $code_ext .= $encode[$code{$i}];
  251. }
  252. return $code_ext;
  253. }
  254. // }}}
  255. // xml2pdf_barcode_code39::draw_code39() {{{
  256. /**
  257. *
  258. * @param string $code
  259. * @param float $x
  260. * @param float $y
  261. * @param float $w
  262. * @param float $h
  263. * @param object $pdf
  264. * @return void
  265. */
  266. public static function draw_code39($code, $x, $y, $w, $h, $pdf) {
  267. //Dessine les barres
  268. for($i=0; $i<strlen($code); $i++) {
  269. if($code{$i} == '1') {
  270. $pdf->Rect($x+$i*$w, $y, $w, $h, 'F');
  271. }
  272. }
  273. }
  274. // }}}
  275. }
  276. ?>