1
|
|
|
<?php |
2
|
|
|
// Copyright (C) 2002-2015 Nicola Asuni - Tecnick.com LTD |
3
|
|
|
// |
4
|
|
|
// This file is part of TCPDF software library. |
5
|
|
|
// |
6
|
|
|
// TCPDF is free software: you can redistribute it and/or modify it |
7
|
|
|
// under the terms of the GNU Lesser General Public License as |
8
|
|
|
// published by the Free Software Foundation, either version 3 of the |
9
|
|
|
// License, or (at your option) any later version. |
10
|
|
|
// |
11
|
|
|
// TCPDF is distributed in the hope that it will be useful, but |
12
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
14
|
|
|
// See the GNU Lesser General Public License for more details. |
15
|
|
|
// |
16
|
|
|
// You should have received a copy of the License |
17
|
|
|
// along with TCPDF. If not, see |
18
|
|
|
// <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>. |
19
|
|
|
// |
20
|
|
|
// See LICENSE.TXT file for more information. |
21
|
|
|
namespace CbCaio\Boletos\Generators\Base; |
22
|
|
|
|
23
|
|
|
abstract class BarcodeGenerator |
24
|
|
|
{ |
25
|
|
|
const TYPE_INTERLEAVED_2_5 = 'I25'; |
26
|
|
|
const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the barcode data |
30
|
|
|
* |
31
|
|
|
* @param string $code code to print |
32
|
|
|
* @param string $type type of barcode |
33
|
|
|
* @return array barcode array |
34
|
|
|
* @public |
35
|
|
|
*/ |
36
|
|
|
protected function getBarcodeData($code, $type) |
37
|
|
|
{ |
38
|
|
|
switch (strtoupper($type)) |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
case self::TYPE_INTERLEAVED_2_5: |
|
|
|
|
42
|
|
|
{ // Interleaved 2 of 5 |
43
|
|
|
$arrcode = $this->barcode_i25($code, FALSE); |
44
|
|
|
break; |
45
|
|
|
} |
46
|
|
|
case self::TYPE_INTERLEAVED_2_5_CHECKSUM: |
|
|
|
|
47
|
|
|
{ // Interleaved 2 of 5 + CHECKSUM |
48
|
|
|
$arrcode = $this->barcode_i25($code, TRUE); |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
default: |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$arrcode = FALSE; |
54
|
|
|
break; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$arrcode = $this->convertBarcodeArrayToNewStyle($arrcode); |
59
|
|
|
|
60
|
|
|
return $arrcode; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Interleaved 2 of 5 barcodes. |
65
|
|
|
* Compact numeric code, widely used in industry, air cargo |
66
|
|
|
* Contains digits (0 to 9) and encodes the data in the width of both bars and spaces. |
67
|
|
|
* |
68
|
|
|
* @param string $code (string) code to represent. |
69
|
|
|
* @param $checksum (boolean) if true add a checksum to the code |
70
|
|
|
* @return array barcode representation. |
71
|
|
|
* @protected |
72
|
|
|
*/ |
73
|
|
|
protected function barcode_i25($code, $checksum = FALSE) |
74
|
|
|
{ |
75
|
|
|
$chr['0'] = '11221'; |
|
|
|
|
76
|
|
|
$chr['1'] = '21112'; |
77
|
|
|
$chr['2'] = '12112'; |
78
|
|
|
$chr['3'] = '22111'; |
79
|
|
|
$chr['4'] = '11212'; |
80
|
|
|
$chr['5'] = '21211'; |
81
|
|
|
$chr['6'] = '12211'; |
82
|
|
|
$chr['7'] = '11122'; |
83
|
|
|
$chr['8'] = '21121'; |
84
|
|
|
$chr['9'] = '12121'; |
85
|
|
|
$chr['A'] = '11'; |
86
|
|
|
$chr['Z'] = '21'; |
87
|
|
|
if ($checksum) |
88
|
|
|
{ |
89
|
|
|
// add checksum |
90
|
|
|
$code .= $this->checksum_s25($code); |
91
|
|
|
} |
92
|
|
|
if ((strlen($code) % 2) != 0) |
93
|
|
|
{ |
94
|
|
|
// add leading zero if code-length is odd |
95
|
|
|
$code = '0' . $code; |
96
|
|
|
} |
97
|
|
|
// add start and stop codes |
98
|
|
|
$code = 'AA' . strtolower($code) . 'ZA'; |
99
|
|
|
|
100
|
|
|
$bararray = ['code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => []]; |
101
|
|
|
$k = 0; |
102
|
|
|
$clen = strlen($code); |
103
|
|
|
for ($i = 0; $i < $clen; $i = ($i + 2)) |
104
|
|
|
{ |
105
|
|
|
$char_bar = $code{$i}; |
106
|
|
|
$char_space = $code{$i + 1}; |
107
|
|
|
if ((!isset($chr[ $char_bar ])) OR (!isset($chr[ $char_space ]))) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
// invalid character |
110
|
|
|
return FALSE; |
111
|
|
|
} |
112
|
|
|
// create a bar-space sequence |
113
|
|
|
$seq = ''; |
114
|
|
|
$chrlen = strlen($chr[ $char_bar ]); |
115
|
|
|
for ($s = 0; $s < $chrlen; $s++) |
116
|
|
|
{ |
117
|
|
|
$seq .= $chr[ $char_bar ]{$s} . $chr[ $char_space ]{$s}; |
118
|
|
|
} |
119
|
|
|
$seqlen = strlen($seq); |
120
|
|
|
for ($j = 0; $j < $seqlen; ++$j) |
121
|
|
|
{ |
122
|
|
|
if (($j % 2) == 0) |
123
|
|
|
{ |
124
|
|
|
$t = TRUE; // bar |
125
|
|
|
} else |
126
|
|
|
{ |
127
|
|
|
$t = FALSE; // space |
128
|
|
|
} |
129
|
|
|
$w = $seq{$j}; |
130
|
|
|
$bararray['bcode'][ $k ] = ['t' => $t, 'w' => $w, 'h' => 1, 'p' => 0]; |
131
|
|
|
$bararray['maxw'] += $w; |
132
|
|
|
++$k; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $bararray; |
137
|
|
|
} |
138
|
|
|
/** |
139
|
|
|
* Checksum for standard 2 of 5 barcodes. |
140
|
|
|
* |
141
|
|
|
* @param string $code (string) code to process. |
142
|
|
|
* @return int checksum. |
143
|
|
|
* @protected |
144
|
|
|
*/ |
145
|
|
|
protected function checksum_s25($code) |
146
|
|
|
{ |
147
|
|
|
$len = strlen($code); |
148
|
|
|
$sum = 0; |
149
|
|
|
for ($i = 0; $i < $len; $i += 2) |
150
|
|
|
{ |
151
|
|
|
$sum += $code{$i}; |
152
|
|
|
} |
153
|
|
|
$sum *= 3; |
154
|
|
|
for ($i = 1; $i < $len; $i += 2) |
155
|
|
|
{ |
156
|
|
|
$sum += ($code{$i}); |
157
|
|
|
} |
158
|
|
|
$r = $sum % 10; |
159
|
|
|
if ($r > 0) |
160
|
|
|
{ |
161
|
|
|
$r = (10 - $r); |
162
|
|
|
} |
163
|
|
|
return $r; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
protected function convertBarcodeArrayToNewStyle($oldBarcodeArray) |
167
|
|
|
{ |
168
|
|
|
$newBarcodeArray = []; |
169
|
|
|
$newBarcodeArray['code'] = $oldBarcodeArray['code']; |
170
|
|
|
$newBarcodeArray['maxWidth'] = $oldBarcodeArray['maxw']; |
171
|
|
|
$newBarcodeArray['maxHeight'] = $oldBarcodeArray['maxh']; |
172
|
|
|
$newBarcodeArray['bars'] = []; |
173
|
|
|
foreach ($oldBarcodeArray['bcode'] as $oldbar) |
174
|
|
|
{ |
175
|
|
|
$newBar = []; |
176
|
|
|
$newBar['width'] = $oldbar['w']; |
177
|
|
|
$newBar['height'] = $oldbar['h']; |
178
|
|
|
$newBar['positionVertical'] = $oldbar['p']; |
179
|
|
|
$newBar['drawBar'] = $oldbar['t']; |
180
|
|
|
$newBar['drawSpacing'] = !$oldbar['t']; |
181
|
|
|
|
182
|
|
|
$newBarcodeArray['bars'][] = $newBar; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $newBarcodeArray; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.