1
|
|
|
<?php |
2
|
|
|
//============================================================+ |
3
|
|
|
// File name : c39object.php |
4
|
|
|
// Begin : 2002-07-31 |
5
|
|
|
// Last Update : 2004-12-29 |
6
|
|
|
// Author : Karim Mribti [[email protected]] |
7
|
|
|
// : Nicola Asuni [[email protected]] |
8
|
|
|
// Version : 0.0.8a 2001-04-01 (original code) |
9
|
|
|
// License : GNU LGPL (Lesser General Public License) 2.1 |
10
|
|
|
// http://www.gnu.org/copyleft/lesser.txt |
11
|
|
|
// Source Code : http://www.mribti.com/barcode/ |
12
|
|
|
// |
13
|
|
|
// Description : Code 39 Barcode Render Class for PHP using |
14
|
|
|
// the GD graphics library. |
15
|
|
|
// Code 39 is an alphanumeric bar code that can |
16
|
|
|
// encode decimal number, case alphabet and some |
17
|
|
|
// special symbols. |
18
|
|
|
// |
19
|
|
|
// NOTE: |
20
|
|
|
// This version contains changes by Nicola Asuni: |
21
|
|
|
// - porting to PHP4 |
22
|
|
|
// - code style and formatting |
23
|
|
|
// - automatic php documentation in PhpDocumentor Style |
24
|
|
|
// (www.phpdoc.org) |
25
|
|
|
// - minor bug fixing |
26
|
|
|
//============================================================+ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Code 39 Barcode Render Class.<br> |
30
|
|
|
* Code 39 is an alphanumeric bar code that can encode decimal number, case alphabet and some special symbols. |
31
|
|
|
* @author Karim Mribti, Nicola Asuni |
32
|
|
|
* @name BarcodeObject |
33
|
|
|
* @package com.tecnick.tcpdf |
34
|
|
|
* @version 0.0.8a 2001-04-01 (original code) |
35
|
|
|
* @since 2001-03-25 |
36
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Code 39 Barcode Render Class.<br> |
41
|
|
|
* Code 39 is an alphanumeric bar code that can encode decimal number, case alphabet and some special symbols. |
42
|
|
|
* @author Karim Mribti, Nicola Asuni |
43
|
|
|
* @name BarcodeObject |
44
|
|
|
* @package com.tecnick.tcpdf |
45
|
|
|
* @version 0.0.8a 2001-04-01 (original code) |
46
|
|
|
* @since 2001-03-25 |
47
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL |
48
|
|
|
*/ |
49
|
|
|
class C39Object extends BarcodeObject { |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Class Constructor. |
53
|
|
|
* @param int $Width Image width in pixels. |
54
|
|
|
* @param int $Height Image height in pixels. |
55
|
|
|
* @param int $Style Barcode style. |
56
|
|
|
* @param int $Value value to print on barcode. |
57
|
|
|
*/ |
58
|
|
|
function C39Object($Width, $Height, $Style, $Value) { |
|
|
|
|
59
|
|
|
$this->BarcodeObject($Width, $Height, $Style); |
60
|
|
|
$this->mValue = $Value; |
|
|
|
|
61
|
|
|
$this->mChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%"; |
|
|
|
|
62
|
|
|
$this->mCharSet = array ( |
|
|
|
|
63
|
|
|
/* 0 */ "000110100", |
64
|
|
|
/* 1 */ "100100001", |
65
|
|
|
/* 2 */ "001100001", |
66
|
|
|
/* 3 */ "101100000", |
67
|
|
|
/* 4 */ "000110001", |
68
|
|
|
/* 5 */ "100110000", |
69
|
|
|
/* 6 */ "001110000", |
70
|
|
|
/* 7 */ "000100101", |
71
|
|
|
/* 8 */ "100100100", |
72
|
|
|
/* 9 */ "001100100", |
73
|
|
|
/* A */ "100001001", |
74
|
|
|
/* B */ "001001001", |
75
|
|
|
/* C */ "101001000", |
76
|
|
|
/* D */ "000011001", |
77
|
|
|
/* E */ "100011000", |
78
|
|
|
/* F */ "001011000", |
79
|
|
|
/* G */ "000001101", |
80
|
|
|
/* H */ "100001100", |
81
|
|
|
/* I */ "001001100", |
82
|
|
|
/* J */ "000011100", |
83
|
|
|
/* K */ "100000011", |
84
|
|
|
/* L */ "001000011", |
85
|
|
|
/* M */ "101000010", |
86
|
|
|
/* N */ "000010011", |
87
|
|
|
/* O */ "100010010", |
88
|
|
|
/* P */ "001010010", |
89
|
|
|
/* Q */ "000000111", |
90
|
|
|
/* R */ "100000110", |
91
|
|
|
/* S */ "001000110", |
92
|
|
|
/* T */ "000010110", |
93
|
|
|
/* U */ "110000001", |
94
|
|
|
/* V */ "011000001", |
95
|
|
|
/* W */ "111000000", |
96
|
|
|
/* X */ "010010001", |
97
|
|
|
/* Y */ "110010000", |
98
|
|
|
/* Z */ "011010000", |
99
|
|
|
/* - */ "010000101", |
100
|
|
|
/* . */ "110000100", |
101
|
|
|
/* SP */ "011000100", |
102
|
|
|
/* * */ "010010100", |
103
|
|
|
/* $ */ "010101000", |
104
|
|
|
/* / */ "010100010", |
105
|
|
|
/* + */ "010001010", |
106
|
|
|
/* % */ "000101010" |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the character index. |
112
|
|
|
* @param char $char character. |
113
|
|
|
* @return int character index or -1 in case of error. |
114
|
|
|
* @access private |
115
|
|
|
*/ |
116
|
|
|
function GetCharIndex($char) { |
|
|
|
|
117
|
|
|
for ($i=0;$i<44;$i++) { |
118
|
|
|
if ($this->mChars[$i] == $char) { |
119
|
|
|
return $i; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
return -1; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns barcode size. |
127
|
|
|
* @param int $xres Horizontal resolution. |
128
|
|
|
* @return barcode size. |
|
|
|
|
129
|
|
|
* @access private |
130
|
|
|
*/ |
131
|
|
|
function GetSize($xres) { |
|
|
|
|
132
|
|
|
$len = strlen($this->mValue); |
133
|
|
|
|
134
|
|
|
if ($len == 0) { |
135
|
|
|
$this->mError = "Null value"; |
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
for ($i=0;$i<$len;$i++) { |
140
|
|
|
if ($this->GetCharIndex($this->mValue[$i]) == -1 || $this->mValue[$i] == '*') { |
141
|
|
|
/* The asterisk is only used as a start and stop code */ |
142
|
|
|
$this->mError = "C39 not include the char '".$this->mValue[$i]."'"; |
|
|
|
|
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/* Start, Stop is 010010100 == '*' */ |
148
|
|
|
$StartSize = BCD_C39_NARROW_BAR * $xres * 6 + BCD_C39_WIDE_BAR * $xres * 3; |
149
|
|
|
$StopSize = BCD_C39_NARROW_BAR * $xres * 6 + BCD_C39_WIDE_BAR * $xres * 3; |
150
|
|
|
$CharSize = BCD_C39_NARROW_BAR * $xres * 6 + BCD_C39_WIDE_BAR * $xres * 3; /* Same for all chars */ |
151
|
|
|
|
152
|
|
|
return $CharSize * $len + $StartSize + $StopSize + /* Space between chars */ BCD_C39_NARROW_BAR * $xres * ($len-1); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Draws the start code. |
157
|
|
|
* @param int $DrawPos Drawing position. |
158
|
|
|
* @param int $yPos Vertical position. |
159
|
|
|
* @param int $ySize Vertical size. |
160
|
|
|
* @param int $xres Horizontal resolution. |
161
|
|
|
* @return int drawing position. |
162
|
|
|
* @access private |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
function DrawStart($DrawPos, $yPos, $ySize, $xres) { |
|
|
|
|
165
|
|
|
/* Start code is '*' */ |
166
|
|
|
$narrow = BCD_C39_NARROW_BAR * $xres; |
167
|
|
|
$wide = BCD_C39_WIDE_BAR * $xres; |
168
|
|
|
$this->DrawSingleBar($DrawPos, $yPos, $narrow , $ySize); |
169
|
|
|
$DrawPos += $narrow; |
170
|
|
|
$DrawPos += $wide; |
171
|
|
|
$this->DrawSingleBar($DrawPos, $yPos, $narrow , $ySize); |
172
|
|
|
$DrawPos += $narrow; |
173
|
|
|
$DrawPos += $narrow; |
174
|
|
|
$this->DrawSingleBar($DrawPos, $yPos, $wide , $ySize); |
175
|
|
|
$DrawPos += $wide; |
176
|
|
|
$DrawPos += $narrow; |
177
|
|
|
$this->DrawSingleBar($DrawPos, $yPos, $wide , $ySize); |
178
|
|
|
$DrawPos += $wide; |
179
|
|
|
$DrawPos += $narrow; |
180
|
|
|
$this->DrawSingleBar($DrawPos, $yPos, $narrow, $ySize); |
181
|
|
|
$DrawPos += $narrow; |
182
|
|
|
$DrawPos += $narrow; /* Space between chars */ |
183
|
|
|
return $DrawPos; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Draws the stop code. |
188
|
|
|
* @param int $DrawPos Drawing position. |
189
|
|
|
* @param int $yPos Vertical position. |
190
|
|
|
* @param int $ySize Vertical size. |
191
|
|
|
* @param int $xres Horizontal resolution. |
192
|
|
|
* @return int drawing position. |
193
|
|
|
* @access private |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
function DrawStop($DrawPos, $yPos, $ySize, $xres) { |
|
|
|
|
196
|
|
|
/* Stop code is '*' */ |
197
|
|
|
$narrow = BCD_C39_NARROW_BAR * $xres; |
198
|
|
|
$wide = BCD_C39_WIDE_BAR * $xres; |
199
|
|
|
$this->DrawSingleBar($DrawPos, $yPos, $narrow , $ySize); |
200
|
|
|
$DrawPos += $narrow; |
201
|
|
|
$DrawPos += $wide; |
202
|
|
|
$this->DrawSingleBar($DrawPos, $yPos, $narrow , $ySize); |
203
|
|
|
$DrawPos += $narrow; |
204
|
|
|
$DrawPos += $narrow; |
205
|
|
|
$this->DrawSingleBar($DrawPos, $yPos, $wide , $ySize); |
206
|
|
|
$DrawPos += $wide; |
207
|
|
|
$DrawPos += $narrow; |
208
|
|
|
$this->DrawSingleBar($DrawPos, $yPos, $wide , $ySize); |
209
|
|
|
$DrawPos += $wide; |
210
|
|
|
$DrawPos += $narrow; |
211
|
|
|
$this->DrawSingleBar($DrawPos, $yPos, $narrow, $ySize); |
212
|
|
|
$DrawPos += $narrow; |
213
|
|
|
return $DrawPos; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Draws the barcode object. |
218
|
|
|
* @param int $xres Horizontal resolution. |
219
|
|
|
* @return bool true in case of success. |
220
|
|
|
*/ |
221
|
|
|
function DrawObject($xres) { |
|
|
|
|
222
|
|
|
$len = strlen($this->mValue); |
223
|
|
|
|
224
|
|
|
$narrow = BCD_C39_NARROW_BAR * $xres; |
225
|
|
|
$wide = BCD_C39_WIDE_BAR * $xres; |
226
|
|
|
|
227
|
|
|
if (($size = $this->GetSize($xres))==0) { |
228
|
|
|
return false; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$cPos = 0; |
232
|
|
|
if ($this->mStyle & BCS_ALIGN_CENTER) $sPos = (integer)(($this->mWidth - $size ) / 2); |
233
|
|
|
else if ($this->mStyle & BCS_ALIGN_RIGHT) $sPos = $this->mWidth - $size; |
234
|
|
|
else $sPos = 0; |
235
|
|
|
|
236
|
|
|
/* Total height of bar code -Bars only- */ |
237
|
|
|
if ($this->mStyle & BCS_DRAW_TEXT) $ysize = $this->mHeight - BCD_DEFAULT_MAR_Y1 - BCD_DEFAULT_MAR_Y2 - $this->GetFontHeight($this->mFont); |
238
|
|
|
else $ysize = $this->mHeight - BCD_DEFAULT_MAR_Y1 - BCD_DEFAULT_MAR_Y2; |
239
|
|
|
|
240
|
|
|
/* Draw text */ |
241
|
|
|
if ($this->mStyle & BCS_DRAW_TEXT) { |
242
|
|
|
if ($this->mStyle & BCS_STRETCH_TEXT) { |
243
|
|
View Code Duplication |
for ($i=0;$i<$len;$i++) { |
|
|
|
|
244
|
|
|
$this->DrawChar($this->mFont, $sPos+($narrow*6+$wide*3)+($size/$len)*$i, |
245
|
|
|
$ysize + BCD_DEFAULT_MAR_Y1 + BCD_DEFAULT_TEXT_OFFSET, $this->mValue[$i]); |
246
|
|
|
} |
247
|
|
View Code Duplication |
} else {/* Center */ |
|
|
|
|
248
|
|
|
$text_width = $this->GetFontWidth($this->mFont) * strlen($this->mValue); |
249
|
|
|
$this->DrawText($this->mFont, $sPos+(($size-$text_width)/2)+($narrow*6+$wide*3), |
250
|
|
|
$ysize + BCD_DEFAULT_MAR_Y1 + BCD_DEFAULT_TEXT_OFFSET, $this->mValue); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$DrawPos = $this->DrawStart($sPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres); |
255
|
|
|
do { |
256
|
|
|
$c = $this->GetCharIndex($this->mValue[$cPos]); |
257
|
|
|
$cset = $this->mCharSet[$c]; |
258
|
|
|
$this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[0] == '0') ? $narrow : $wide , $ysize); |
259
|
|
|
$DrawPos += ($cset[0] == '0') ? $narrow : $wide; |
260
|
|
|
$DrawPos += ($cset[1] == '0') ? $narrow : $wide; |
261
|
|
|
$this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[2] == '0') ? $narrow : $wide , $ysize); |
262
|
|
|
$DrawPos += ($cset[2] == '0') ? $narrow : $wide; |
263
|
|
|
$DrawPos += ($cset[3] == '0') ? $narrow : $wide; |
264
|
|
|
$this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[4] == '0') ? $narrow : $wide , $ysize); |
265
|
|
|
$DrawPos += ($cset[4] == '0') ? $narrow : $wide; |
266
|
|
|
$DrawPos += ($cset[5] == '0') ? $narrow : $wide; |
267
|
|
|
$this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[6] == '0') ? $narrow : $wide , $ysize); |
268
|
|
|
$DrawPos += ($cset[6] == '0') ? $narrow : $wide; |
269
|
|
|
$DrawPos += ($cset[7] == '0') ? $narrow : $wide; |
270
|
|
|
$this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[8] == '0') ? $narrow : $wide , $ysize); |
271
|
|
|
$DrawPos += ($cset[8] == '0') ? $narrow : $wide; |
272
|
|
|
$DrawPos += $narrow; /* Space between chars */ |
273
|
|
|
$cPos++; |
274
|
|
|
} while ($cPos<$len); |
275
|
|
|
$DrawPos = $this->DrawStop($DrawPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres); |
|
|
|
|
276
|
|
|
return true; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
//============================================================+ |
281
|
|
|
// END OF FILE |
282
|
|
|
//============================================================+ |
283
|
|
|
?> |
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.