composer package updates
[openemr.git] / vendor / mpdf / mpdf / src / Barcode / Code128.php
blob4e1e2122cd8b6570a9784904a8ee078d394cde00
1 <?php
3 namespace Mpdf\Barcode;
5 use Mpdf\Utils\UtfString;
7 /**
8 * C128 barcodes.
9 * Very capable code, excellent density, high reliability; in very wide use world-wide
11 class Code128 extends \Mpdf\Barcode\AbstractBarcode implements \Mpdf\Barcode\BarcodeInterface
14 /**
15 * @param string $code
16 * @param string $type
17 * @param bool $ean
19 public function __construct($code, $type = 'B', $ean = false)
21 $this->init($code, $type, $ean);
23 $this->data['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
24 $this->data['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
25 $this->data['lightmL'] = 10; // LEFT light margin = x X-dim (spec.)
26 $this->data['lightmR'] = 10; // RIGHT light margin = x X-dim (spec.)
27 $this->data['lightTB'] = 0; // TOP/BOTTOM light margin = x X-dim (non-spec.)
30 /**
31 * @param string $code
32 * @param string $type
33 * @param bool $ean
35 protected function init($code, $type, $ean)
37 $code = UtfString::strcode2utf($code); // mPDF 5.7.1 Allows e.g. <barcode code="5432&#013;1068" type="C128A" />
39 $chr = [
40 '212222', /* 00 */
41 '222122', /* 01 */
42 '222221', /* 02 */
43 '121223', /* 03 */
44 '121322', /* 04 */
45 '131222', /* 05 */
46 '122213', /* 06 */
47 '122312', /* 07 */
48 '132212', /* 08 */
49 '221213', /* 09 */
50 '221312', /* 10 */
51 '231212', /* 11 */
52 '112232', /* 12 */
53 '122132', /* 13 */
54 '122231', /* 14 */
55 '113222', /* 15 */
56 '123122', /* 16 */
57 '123221', /* 17 */
58 '223211', /* 18 */
59 '221132', /* 19 */
60 '221231', /* 20 */
61 '213212', /* 21 */
62 '223112', /* 22 */
63 '312131', /* 23 */
64 '311222', /* 24 */
65 '321122', /* 25 */
66 '321221', /* 26 */
67 '312212', /* 27 */
68 '322112', /* 28 */
69 '322211', /* 29 */
70 '212123', /* 30 */
71 '212321', /* 31 */
72 '232121', /* 32 */
73 '111323', /* 33 */
74 '131123', /* 34 */
75 '131321', /* 35 */
76 '112313', /* 36 */
77 '132113', /* 37 */
78 '132311', /* 38 */
79 '211313', /* 39 */
80 '231113', /* 40 */
81 '231311', /* 41 */
82 '112133', /* 42 */
83 '112331', /* 43 */
84 '132131', /* 44 */
85 '113123', /* 45 */
86 '113321', /* 46 */
87 '133121', /* 47 */
88 '313121', /* 48 */
89 '211331', /* 49 */
90 '231131', /* 50 */
91 '213113', /* 51 */
92 '213311', /* 52 */
93 '213131', /* 53 */
94 '311123', /* 54 */
95 '311321', /* 55 */
96 '331121', /* 56 */
97 '312113', /* 57 */
98 '312311', /* 58 */
99 '332111', /* 59 */
100 '314111', /* 60 */
101 '221411', /* 61 */
102 '431111', /* 62 */
103 '111224', /* 63 */
104 '111422', /* 64 */
105 '121124', /* 65 */
106 '121421', /* 66 */
107 '141122', /* 67 */
108 '141221', /* 68 */
109 '112214', /* 69 */
110 '112412', /* 70 */
111 '122114', /* 71 */
112 '122411', /* 72 */
113 '142112', /* 73 */
114 '142211', /* 74 */
115 '241211', /* 75 */
116 '221114', /* 76 */
117 '413111', /* 77 */
118 '241112', /* 78 */
119 '134111', /* 79 */
120 '111242', /* 80 */
121 '121142', /* 81 */
122 '121241', /* 82 */
123 '114212', /* 83 */
124 '124112', /* 84 */
125 '124211', /* 85 */
126 '411212', /* 86 */
127 '421112', /* 87 */
128 '421211', /* 88 */
129 '212141', /* 89 */
130 '214121', /* 90 */
131 '412121', /* 91 */
132 '111143', /* 92 */
133 '111341', /* 93 */
134 '131141', /* 94 */
135 '114113', /* 95 */
136 '114311', /* 96 */
137 '411113', /* 97 */
138 '411311', /* 98 */
139 '113141', /* 99 */
140 '114131', /* 100 */
141 '311141', /* 101 */
142 '411131', /* 102 */
143 '211412', /* 103 START A */
144 '211214', /* 104 START B */
145 '211232', /* 105 START C */
146 '233111', /* STOP */
147 '200000' /* END */
150 switch (strtoupper($type)) {
151 case 'A':
152 $startid = 103;
153 $keys = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_';
154 for ($i = 0; $i < 32; ++$i) {
155 $keys .= chr($i);
157 break;
158 case 'B':
159 $startid = 104;
160 $keys = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' . chr(127);
161 break;
162 case 'C':
163 $startid = 105;
164 $keys = '';
165 if ((strlen($code) % 2) != 0) {
166 // The length of barcode value must be even ($code). You must pad the number with zeros
167 throw new \Mpdf\Barcode\BarcodeException('Invalid CODE128C barcode value');
169 for ($i = 0; $i <= 99; ++$i) {
170 $keys .= chr($i);
172 $newCode = '';
173 $hclen = (strlen($code) / 2);
174 for ($i = 0; $i < $hclen; ++$i) {
175 $newCode .= chr((int) ($code{(2 * $i)} . $code{(2 * $i + 1)}));
177 $code = $newCode;
178 break;
179 default:
180 throw new \Mpdf\Barcode\BarcodeException('Invalid CODE128 barcode type');
183 // calculate check character
184 $sum = $startid;
186 // Add FNC 1 - which identifies it as EAN-128
187 if ($ean) {
188 $code = chr(102) . $code;
190 $clen = strlen($code);
191 for ($i = 0; $i < $clen; ++$i) {
192 if ($ean && $i == 0) {
193 $sum += 102;
194 } else {
195 $sum += (strpos($keys, $code[$i]) * ($i + 1));
198 $check = ($sum % 103);
199 $checkdigit = $check;
201 // add start, check and stop codes
202 $code = chr($startid) . $code . chr($check) . chr(106) . chr(107);
203 $bararray = ['code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => []];
204 $k = 0;
205 $len = strlen($code);
207 for ($i = 0; $i < $len; ++$i) {
209 $ck = strpos($keys, $code[$i]);
210 if (($i == 0) || ($ean && $i == 1) | ($i > ($len - 4))) {
211 $char_num = ord($code[$i]);
212 $seq = $chr[$char_num];
213 } elseif (($ck >= 0) && isset($chr[$ck])) {
214 $seq = $chr[$ck];
215 } else {
216 // invalid character
217 throw new \Mpdf\Barcode\BarcodeException(sprintf('Invalid character "%s" in CODE128C barcode value', $code[$i]));
219 for ($j = 0; $j < 6; ++$j) {
220 if (($j % 2) == 0) {
221 $t = true; // bar
222 } else {
223 $t = false; // space
225 $w = $seq[$j];
226 $bararray['bcode'][$k] = ['t' => $t, 'w' => $w, 'h' => 1, 'p' => 0];
227 $bararray['maxw'] += $w;
228 ++$k;
232 $bararray['checkdigit'] = $checkdigit;
233 $this->data = $bararray;
236 public function getType()
238 return 'CODE128';