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
|
|
|
case self::TYPE_INTERLEAVED_2_5: |
41
|
|
|
// Interleaved 2 of 5 |
42
|
|
|
$arrcode = $this->barcode_i25($code, FALSE); |
43
|
|
|
break; |
44
|
|
|
case self::TYPE_INTERLEAVED_2_5_CHECKSUM: |
45
|
|
|
// Interleaved 2 of 5 + CHECKSUM |
46
|
|
|
$arrcode = $this->barcode_i25($code, TRUE); |
47
|
|
|
break; |
48
|
|
|
default: |
49
|
|
|
$arrcode = FALSE; |
50
|
|
|
break; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$arrcode = $this->convertBarcodeArrayToNewStyle($arrcode); |
54
|
|
|
|
55
|
|
|
return $arrcode; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Interleaved 2 of 5 barcodes. |
60
|
|
|
* Compact numeric code, widely used in industry, air cargo |
61
|
|
|
* Contains digits (0 to 9) and encodes the data in the width of both bars and spaces. |
62
|
|
|
* |
63
|
|
|
* @param string $code (string) code to represent. |
64
|
|
|
* @param $checksum (boolean) if true add a checksum to the code |
65
|
|
|
* @return array barcode representation. |
66
|
|
|
* @protected |
67
|
|
|
*/ |
68
|
|
|
protected function barcode_i25($code, $checksum = FALSE) |
69
|
|
|
{ |
70
|
|
|
$chr = array(); |
71
|
|
|
$chr['0'] = '11221'; |
72
|
|
|
$chr['1'] = '21112'; |
73
|
|
|
$chr['2'] = '12112'; |
74
|
|
|
$chr['3'] = '22111'; |
75
|
|
|
$chr['4'] = '11212'; |
76
|
|
|
$chr['5'] = '21211'; |
77
|
|
|
$chr['6'] = '12211'; |
78
|
|
|
$chr['7'] = '11122'; |
79
|
|
|
$chr['8'] = '21121'; |
80
|
|
|
$chr['9'] = '12121'; |
81
|
|
|
$chr['A'] = '11'; |
82
|
|
|
$chr['Z'] = '21'; |
83
|
|
|
if ($checksum) |
84
|
|
|
{ |
85
|
|
|
// add checksum |
86
|
|
|
$code .= $this->checksum_s25($code); |
87
|
|
|
} |
88
|
|
|
if ((strlen($code) % 2) != 0) |
89
|
|
|
{ |
90
|
|
|
// add leading zero if code-length is odd |
91
|
|
|
$code = '0' . $code; |
92
|
|
|
} |
93
|
|
|
// add start and stop codes |
94
|
|
|
$code = 'AA' . strtolower($code) . 'ZA'; |
95
|
|
|
|
96
|
|
|
$bararray = ['code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => []]; |
97
|
|
|
$k = 0; |
98
|
|
|
$clen = strlen($code); |
99
|
|
|
for ($i = 0; $i < $clen; $i = ($i + 2)) |
100
|
|
|
{ |
101
|
|
|
$char_bar = $code{$i}; |
102
|
|
|
$char_space = $code{$i + 1}; |
103
|
|
|
if ((!isset($chr[ $char_bar ])) || (!isset($chr[ $char_space ]))) |
104
|
|
|
{ |
105
|
|
|
// invalid character |
106
|
|
|
return FALSE; |
107
|
|
|
} |
108
|
|
|
// create a bar-space sequence |
109
|
|
|
$seq = ''; |
110
|
|
|
$chrlen = strlen($chr[ $char_bar ]); |
111
|
|
|
for ($s = 0; $s < $chrlen; $s++) |
112
|
|
|
{ |
113
|
|
|
$seq .= $chr[ $char_bar ]{$s} . $chr[ $char_space ]{$s}; |
114
|
|
|
} |
115
|
|
|
$seqlen = strlen($seq); |
116
|
|
|
for ($j = 0; $j < $seqlen; ++$j) |
117
|
|
|
{ |
118
|
|
|
if (($j % 2) == 0) |
119
|
|
|
{ |
120
|
|
|
$t = TRUE; // bar |
121
|
|
|
} else |
122
|
|
|
{ |
123
|
|
|
$t = FALSE; // space |
124
|
|
|
} |
125
|
|
|
$w = $seq{$j}; |
126
|
|
|
$bararray['bcode'][ $k ] = ['t' => $t, 'w' => $w, 'h' => 1, 'p' => 0]; |
127
|
|
|
$bararray['maxw'] += $w; |
128
|
|
|
++$k; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $bararray; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Checksum for standard 2 of 5 barcodes. |
137
|
|
|
* |
138
|
|
|
* @param string $code (string) code to process. |
139
|
|
|
* @return int checksum. |
140
|
|
|
* @protected |
141
|
|
|
*/ |
142
|
|
|
protected function checksum_s25($code) |
143
|
|
|
{ |
144
|
|
|
$len = strlen($code); |
145
|
|
|
$sum = 0; |
146
|
|
|
for ($i = 0; $i < $len; $i += 2) |
147
|
|
|
{ |
148
|
|
|
$sum += $code{$i}; |
149
|
|
|
} |
150
|
|
|
$sum *= 3; |
151
|
|
|
for ($i = 1; $i < $len; $i += 2) |
152
|
|
|
{ |
153
|
|
|
$sum += ($code{$i}); |
154
|
|
|
} |
155
|
|
|
$r = $sum % 10; |
156
|
|
|
if ($r > 0) |
157
|
|
|
{ |
158
|
|
|
$r = (10 - $r); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $r; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
protected function convertBarcodeArrayToNewStyle($oldBarcodeArray) |
165
|
|
|
{ |
166
|
|
|
$newBarcodeArray = []; |
167
|
|
|
$newBarcodeArray['code'] = $oldBarcodeArray['code']; |
168
|
|
|
$newBarcodeArray['maxWidth'] = $oldBarcodeArray['maxw']; |
169
|
|
|
$newBarcodeArray['maxHeight'] = $oldBarcodeArray['maxh']; |
170
|
|
|
$newBarcodeArray['bars'] = []; |
171
|
|
|
foreach ($oldBarcodeArray['bcode'] as $oldbar) |
172
|
|
|
{ |
173
|
|
|
$newBar = []; |
174
|
|
|
$newBar['width'] = $oldbar['w']; |
175
|
|
|
$newBar['height'] = $oldbar['h']; |
176
|
|
|
$newBar['positionVertical'] = $oldbar['p']; |
177
|
|
|
$newBar['drawBar'] = $oldbar['t']; |
178
|
|
|
$newBar['drawSpacing'] = !$oldbar['t']; |
179
|
|
|
|
180
|
|
|
$newBarcodeArray['bars'][] = $newBar; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $newBarcodeArray; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|