Mercurial > hg > soundsoftware-site
comparison vendor/plugins/rfpdf/lib/barcode/c128bobject.rb @ 441:cbce1fd3b1b7 redmine-1.2
Update to Redmine 1.2-stable branch (Redmine SVN rev 6000)
author | Chris Cannam |
---|---|
date | Mon, 06 Jun 2011 14:24:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
245:051f544170fe | 441:cbce1fd3b1b7 |
---|---|
1 | |
2 #============================================================+ | |
3 # File name : c128bobject.rb | |
4 # Begin : 2002-07-31 | |
5 # Last Update : 2004-12-29 | |
6 # Author : Karim Mribti [barcode@mribti.com] | |
7 # Version : 0.0.8a 2001-04-01 (original code) | |
8 # License : GNU LGPL (Lesser General Public License) 2.1 | |
9 # http://www.gnu.org/copyleft/lesser.txt | |
10 # Source Code : http://www.mribti.com/barcode/ | |
11 # | |
12 # Description : Code 128-B Barcode Render Class for PHP using | |
13 # the GD graphics library. | |
14 # Code 128-B is a continuous, multilevel and full | |
15 # ASCII code. | |
16 # | |
17 # NOTE: | |
18 # This version contains changes by Nicola Asuni: | |
19 # - porting to Ruby | |
20 # - code style and formatting | |
21 # - automatic php documentation in PhpDocumentor Style | |
22 # (www.phpdoc.org) | |
23 # - minor bug fixing | |
24 #============================================================+ | |
25 | |
26 # | |
27 # Code 128-B Barcode Render Class for PHP using the GD graphics library.<br> | |
28 # Code 128-B is a continuous, multilevel and full ASCII code. | |
29 # @author Karim Mribti, Nicola Asuni | |
30 # @name BarcodeObject | |
31 # @package com.tecnick.tcpdf | |
32 # @@version 0.0.8a 2001-04-01 (original code) | |
33 # @since 2001-03-25 | |
34 # @license http://www.gnu.org/copyleft/lesser.html LGPL | |
35 # | |
36 | |
37 # | |
38 # Code 128-B Barcode Render Class for PHP using the GD graphics library.<br> | |
39 # Code 128-B is a continuous, multilevel and full ASCII code. | |
40 # @author Karim Mribti, Nicola Asuni | |
41 # @name BarcodeObject | |
42 # @package com.tecnick.tcpdf | |
43 # @@version 0.0.8a 2001-04-01 (original code) | |
44 # @since 2001-03-25 | |
45 # @license http://www.gnu.org/copyleft/lesser.html LGPL | |
46 # | |
47 class C128BObject extends BarcodeObject { | |
48 | |
49 # | |
50 # Class Constructor. | |
51 # @param int $Width Image width in pixels. | |
52 # @param int $Height Image height in pixels. | |
53 # @param int $Style Barcode style. | |
54 # @param int $Value value to print on barcode. | |
55 # | |
56 def __construct($Width, $Height, $Style, $Value) | |
57 parent::__construct($Width, $Height, $Style); | |
58 @mValue = $Value; | |
59 @mChars = " !\"#$%&'()*+�-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{ }~"; | |
60 @mCharSet = array ( | |
61 "212222", # 00# | |
62 "222122", # 01# | |
63 "222221", # 02# | |
64 "121223", # 03# | |
65 "121322", # 04# | |
66 "131222", # 05# | |
67 "122213", # 06# | |
68 "122312", # 07# | |
69 "132212", # 08# | |
70 "221213", # 09# | |
71 "221312", # 10# | |
72 "231212", # 11# | |
73 "112232", # 12# | |
74 "122132", # 13# | |
75 "122231", # 14# | |
76 "113222", # 15# | |
77 "123122", # 16# | |
78 "123221", # 17# | |
79 "223211", # 18# | |
80 "221132", # 19# | |
81 "221231", # 20# | |
82 "213212", # 21# | |
83 "223112", # 22# | |
84 "312131", # 23# | |
85 "311222", # 24# | |
86 "321122", # 25# | |
87 "321221", # 26# | |
88 "312212", # 27# | |
89 "322112", # 28# | |
90 "322211", # 29# | |
91 "212123", # 30# | |
92 "212321", # 31# | |
93 "232121", # 32# | |
94 "111323", # 33# | |
95 "131123", # 34# | |
96 "131321", # 35# | |
97 "112313", # 36# | |
98 "132113", # 37# | |
99 "132311", # 38# | |
100 "211313", # 39# | |
101 "231113", # 40# | |
102 "231311", # 41# | |
103 "112133", # 42# | |
104 "112331", # 43# | |
105 "132131", # 44# | |
106 "113123", # 45# | |
107 "113321", # 46# | |
108 "133121", # 47# | |
109 "313121", # 48# | |
110 "211331", # 49# | |
111 "231131", # 50# | |
112 "213113", # 51# | |
113 "213311", # 52# | |
114 "213131", # 53# | |
115 "311123", # 54# | |
116 "311321", # 55# | |
117 "331121", # 56# | |
118 "312113", # 57# | |
119 "312311", # 58# | |
120 "332111", # 59# | |
121 "314111", # 60# | |
122 "221411", # 61# | |
123 "431111", # 62# | |
124 "111224", # 63# | |
125 "111422", # 64# | |
126 "121124", # 65# | |
127 "121421", # 66# | |
128 "141122", # 67# | |
129 "141221", # 68# | |
130 "112214", # 69# | |
131 "112412", # 70# | |
132 "122114", # 71# | |
133 "122411", # 72# | |
134 "142112", # 73# | |
135 "142211", # 74# | |
136 "241211", # 75# | |
137 "221114", # 76# | |
138 "413111", # 77# | |
139 "241112", # 78# | |
140 "134111", # 79# | |
141 "111242", # 80# | |
142 "121142", # 81# | |
143 "121241", # 82# | |
144 "114212", # 83# | |
145 "124112", # 84# | |
146 "124211", # 85# | |
147 "411212", # 86# | |
148 "421112", # 87# | |
149 "421211", # 88# | |
150 "212141", # 89# | |
151 "214121", # 90# | |
152 "412121", # 91# | |
153 "111143", # 92# | |
154 "111341", # 93# | |
155 "131141", # 94# | |
156 "114113", # 95# | |
157 "114311", # 96# | |
158 "411113", # 97# | |
159 "411311", # 98# | |
160 "113141", # 99# | |
161 "114131", # 100# | |
162 "311141", # 101# | |
163 "411131" # 102# | |
164 ); | |
165 end | |
166 | |
167 # | |
168 # Returns the character index. | |
169 # @param char $char character. | |
170 # @return int character index or -1 in case of error. | |
171 # @access private | |
172 # | |
173 def GetCharIndex($char) | |
174 for ($i=0;$i<95;$i++) | |
175 if (@mChars[$i] == $char) | |
176 return $i; | |
177 end | |
178 end | |
179 return -1; | |
180 end | |
181 | |
182 # | |
183 # Returns the bar size. | |
184 # @param int $xres Horizontal resolution. | |
185 # @param char $char Character. | |
186 # @return int barcode size. | |
187 # @access private | |
188 # | |
189 def GetBarSize($xres, $char) | |
190 switch ($char) | |
191 case '1' | |
192 $cVal = BCD_C128_BAR_1; | |
193 | |
194 case '2' | |
195 $cVal = BCD_C128_BAR_2; | |
196 | |
197 case '3' | |
198 $cVal = BCD_C128_BAR_3; | |
199 | |
200 case '4' | |
201 $cVal = BCD_C128_BAR_4; | |
202 | |
203 default | |
204 $cVal = 0; | |
205 end | |
206 end | |
207 return $cVal# $xres; | |
208 end | |
209 | |
210 # | |
211 # Returns barcode size. | |
212 # @param int $xres Horizontal resolution. | |
213 # @return barcode size. | |
214 # @access private | |
215 # | |
216 def GetSize($xres) | |
217 $len = @mValue.length; | |
218 | |
219 if ($len == 0) { | |
220 @mError = "Null value"; | |
221 return false; | |
222 end | |
223 $ret = 0; | |
224 for ($i=0;$i<$len;$i++) | |
225 if (($id = GetCharIndex(@mValue[$i])) == -1) | |
226 @mError = "C128B not include the char '".@mValue[$i]."'"; | |
227 return false; | |
228 else | |
229 $cset = @mCharSet[$id]; | |
230 $ret += GetBarSize($xres, $cset[0]); | |
231 $ret += GetBarSize($xres, $cset[1]); | |
232 $ret += GetBarSize($xres, $cset[2]); | |
233 $ret += GetBarSize($xres, $cset[3]); | |
234 $ret += GetBarSize($xres, $cset[4]); | |
235 $ret += GetBarSize($xres, $cset[5]); | |
236 end | |
237 end | |
238 # length of Check character# | |
239 $cset = GetCheckCharValue(); | |
240 $CheckSize = 0; | |
241 for ($i=0;$i<6;$i++) | |
242 $CheckSize += GetBarSize($cset[$i], $xres); | |
243 end | |
244 | |
245 $StartSize = 2*BCD_C128_BAR_2*$xres + 3*BCD_C128_BAR_1*$xres + BCD_C128_BAR_4*$xres; | |
246 $StopSize = 2*BCD_C128_BAR_2*$xres + 3*BCD_C128_BAR_1*$xres + 2*BCD_C128_BAR_3*$xres; | |
247 | |
248 return $StartSize + $ret + $CheckSize + $StopSize; | |
249 end | |
250 | |
251 # | |
252 # Returns the check-char value. | |
253 # @return string. | |
254 # @access private | |
255 # | |
256 def GetCheckCharValue() | |
257 $len = @mValue.length; | |
258 $sum = 104; # 'B' type; | |
259 for ($i=0;$i<$len;$i++) | |
260 $sum += GetCharIndex(@mValue[$i])# ($i+1); | |
261 end | |
262 $check = $sum % 103; | |
263 return @mCharSet[$check]; | |
264 end | |
265 | |
266 # | |
267 # Draws the start code. | |
268 # @param int $DrawPos Drawing position. | |
269 # @param int $yPos Vertical position. | |
270 # @param int $ySize Vertical size. | |
271 # @param int $xres Horizontal resolution. | |
272 # @return int drawing position. | |
273 # @access private | |
274 # | |
275 def DrawStart($DrawPos, $yPos, $ySize, $xres) | |
276 # Start code is '211214'# | |
277 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize('2', $xres), $ySize); | |
278 $DrawPos += GetBarSize('2', $xres); | |
279 $DrawPos += GetBarSize('1', $xres); | |
280 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize('1', $xres), $ySize); | |
281 $DrawPos += GetBarSize('1', $xres); | |
282 $DrawPos += GetBarSize('2', $xres); | |
283 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize('1', $xres), $ySize); | |
284 $DrawPos += GetBarSize('1', $xres); | |
285 $DrawPos += GetBarSize('4', $xres); | |
286 return $DrawPos; | |
287 end | |
288 | |
289 # | |
290 # Draws the stop code. | |
291 # @param int $DrawPos Drawing position. | |
292 # @param int $yPos Vertical position. | |
293 # @param int $ySize Vertical size. | |
294 # @param int $xres Horizontal resolution. | |
295 # @return int drawing position. | |
296 # @access private | |
297 # | |
298 def DrawStop($DrawPos, $yPos, $ySize, $xres) | |
299 # Stop code is '2331112'# | |
300 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize('2', $xres) , $ySize); | |
301 $DrawPos += GetBarSize('2', $xres); | |
302 $DrawPos += GetBarSize('3', $xres); | |
303 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize('3', $xres) , $ySize); | |
304 $DrawPos += GetBarSize('3', $xres); | |
305 $DrawPos += GetBarSize('1', $xres); | |
306 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize('1', $xres) , $ySize); | |
307 $DrawPos += GetBarSize('1', $xres); | |
308 $DrawPos += GetBarSize('1', $xres); | |
309 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize('2', $xres) , $ySize); | |
310 $DrawPos += GetBarSize('2', $xres); | |
311 return $DrawPos; | |
312 end | |
313 | |
314 # | |
315 # Draws the check-char code. | |
316 # @param int $DrawPos Drawing position. | |
317 # @param int $yPos Vertical position. | |
318 # @param int $ySize Vertical size. | |
319 # @param int $xres Horizontal resolution. | |
320 # @return int drawing position. | |
321 # @access private | |
322 # | |
323 def DrawCheckChar($DrawPos, $yPos, $ySize, $xres) | |
324 $cset = GetCheckCharValue(); | |
325 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize($cset[0], $xres) , $ySize); | |
326 $DrawPos += GetBarSize($cset[0], $xres); | |
327 $DrawPos += GetBarSize($cset[1], $xres); | |
328 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize($cset[2], $xres) , $ySize); | |
329 $DrawPos += GetBarSize($cset[2], $xres); | |
330 $DrawPos += GetBarSize($cset[3], $xres); | |
331 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize($cset[4], $xres) , $ySize); | |
332 $DrawPos += GetBarSize($cset[4], $xres); | |
333 $DrawPos += GetBarSize($cset[5], $xres); | |
334 return $DrawPos; | |
335 end | |
336 | |
337 # | |
338 # Draws the barcode object. | |
339 # @param int $xres Horizontal resolution. | |
340 # @return bool true in case of success. | |
341 # | |
342 def DrawObject($xres) | |
343 $len = @mValue.length; | |
344 if (($size = GetSize($xres))==0) | |
345 return false; | |
346 end | |
347 | |
348 if (@mStyle & BCS_ALIGN_CENTER) $sPos = (integer)((@mWidth - $size ) / 2); | |
349 elsif (@mStyle & BCS_ALIGN_RIGHT) $sPos = @mWidth - $size; | |
350 else $sPos = 0; | |
351 | |
352 # Total height of bar code -Bars only-# | |
353 if (@mStyle & BCS_DRAW_TEXT) $ysize = @mHeight - BCD_DEFAULT_MAR_Y1 - BCD_DEFAULT_MAR_Y2 - GetFontHeight(@mFont); | |
354 else $ysize = @mHeight - BCD_DEFAULT_MAR_Y1 - BCD_DEFAULT_MAR_Y2; | |
355 | |
356 # Draw text# | |
357 if (@mStyle & BCS_DRAW_TEXT) | |
358 if (@mStyle & BCS_STRETCH_TEXT) | |
359 for ($i=0;$i<$len;$i++) | |
360 @DrawChar(@mFont, $sPos+(2*BCD_C128_BAR_2*$xres + 3*BCD_C128_BAR_1*$xres + BCD_C128_BAR_4*$xres)+($size/$len)*$i, | |
361 $ysize + BCD_DEFAULT_MAR_Y1 + BCD_DEFAULT_TEXT_OFFSET, @mValue[$i]); | |
362 else# Center# | |
363 $text_width = GetFontWidth(@mFont)# @mValue.length; | |
364 @DrawText(@mFont, $sPos+(($size-$text_width)/2)+(2*BCD_C128_BAR_2*$xres + 3*BCD_C128_BAR_1*$xres + BCD_C128_BAR_4*$xres), | |
365 $ysize + BCD_DEFAULT_MAR_Y1 + BCD_DEFAULT_TEXT_OFFSET, @mValue); | |
366 end | |
367 end | |
368 | |
369 $cPos = 0; | |
370 $DrawPos = @DrawStart($sPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres); | |
371 do { | |
372 $c = GetCharIndex(@mValue[$cPos]); | |
373 $cset = @mCharSet[$c]; | |
374 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize($cset[0], $xres) , $ysize); | |
375 $DrawPos += GetBarSize($cset[0], $xres); | |
376 $DrawPos += GetBarSize($cset[1], $xres); | |
377 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize($cset[2], $xres) , $ysize); | |
378 $DrawPos += GetBarSize($cset[2], $xres); | |
379 $DrawPos += GetBarSize($cset[3], $xres); | |
380 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, GetBarSize($cset[4], $xres) , $ysize); | |
381 $DrawPos += GetBarSize($cset[4], $xres); | |
382 $DrawPos += GetBarSize($cset[5], $xres); | |
383 $cPos += 1; | |
384 end while ($cPos<$len); | |
385 $DrawPos = @DrawCheckChar($DrawPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres); | |
386 $DrawPos = @DrawStop($DrawPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres); | |
387 return true; | |
388 end | |
389 } | |
390 | |
391 #============================================================+ | |
392 # END OF FILE | |
393 #============================================================+ |