comparison vendor/plugins/rfpdf/lib/barcode/c39object.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 : c39object.rb
4 # Begin : 2002-07-31
5 # Last Update : 2004-12-29
6 # Author : Karim Mribti [barcode@mribti.com]
7 # : Nicola Asuni [info@tecnick.com]
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 Ruby
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 def __construct($Width, $Height, $Style, $Value)
59 parent::__construct($Width, $Height, $Style);
60 @mValue = $Value;
61 @mChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-.#$/+%";
62 @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 end
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 def GetCharIndex($char)
117 for ($i=0;$i<44;$i++)
118 if (@mChars[$i] == $char)
119 return $i;
120 end
121 end
122 return -1;
123 end
124
125 #
126 # Returns barcode size.
127 # @param int $xres Horizontal resolution.
128 # @return barcode size.
129 # @access private
130 #
131 def GetSize($xres)
132 $len = @mValue.length;
133
134 if ($len == 0) {
135 @mError = "Null value";
136 return false;
137 end
138
139 for ($i=0;$i<$len;$i++)
140 if (GetCharIndex(@mValue[$i]) == -1 || @mValue[$i] == '*')
141 # The asterisk is only used as a start and stop code#
142 @mError = "C39 not include the char '".@mValue[$i]."'";
143 return false;
144 end
145 end
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 end
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 def DrawStart($DrawPos, $yPos, $ySize, $xres)
165 # Start code is '*'#
166 $narrow = BCD_C39_NARROW_BAR# $xres;
167 $wide = BCD_C39_WIDE_BAR# $xres;
168 @DrawSingleBar($DrawPos, $yPos, $narrow , $ySize);
169 $DrawPos += $narrow;
170 $DrawPos += $wide;
171 @DrawSingleBar($DrawPos, $yPos, $narrow , $ySize);
172 $DrawPos += $narrow;
173 $DrawPos += $narrow;
174 @DrawSingleBar($DrawPos, $yPos, $wide , $ySize);
175 $DrawPos += $wide;
176 $DrawPos += $narrow;
177 @DrawSingleBar($DrawPos, $yPos, $wide , $ySize);
178 $DrawPos += $wide;
179 $DrawPos += $narrow;
180 @DrawSingleBar($DrawPos, $yPos, $narrow, $ySize);
181 $DrawPos += $narrow;
182 $DrawPos += $narrow; # Space between chars#
183 return $DrawPos;
184 end
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 def DrawStop($DrawPos, $yPos, $ySize, $xres)
196 # Stop code is '*'#
197 $narrow = BCD_C39_NARROW_BAR# $xres;
198 $wide = BCD_C39_WIDE_BAR# $xres;
199 @DrawSingleBar($DrawPos, $yPos, $narrow , $ySize);
200 $DrawPos += $narrow;
201 $DrawPos += $wide;
202 @DrawSingleBar($DrawPos, $yPos, $narrow , $ySize);
203 $DrawPos += $narrow;
204 $DrawPos += $narrow;
205 @DrawSingleBar($DrawPos, $yPos, $wide , $ySize);
206 $DrawPos += $wide;
207 $DrawPos += $narrow;
208 @DrawSingleBar($DrawPos, $yPos, $wide , $ySize);
209 $DrawPos += $wide;
210 $DrawPos += $narrow;
211 @DrawSingleBar($DrawPos, $yPos, $narrow, $ySize);
212 $DrawPos += $narrow;
213 return $DrawPos;
214 end
215
216 #
217 # Draws the barcode object.
218 # @param int $xres Horizontal resolution.
219 # @return bool true in case of success.
220 #
221 def DrawObject($xres)
222 $len = @mValue.length;
223
224 $narrow = BCD_C39_NARROW_BAR# $xres;
225 $wide = BCD_C39_WIDE_BAR# $xres;
226
227 if (($size = GetSize($xres))==0)
228 return false;
229 end
230
231 $cPos = 0;
232 if (@mStyle & BCS_ALIGN_CENTER) $sPos = (integer)((@mWidth - $size ) / 2);
233 elsif (@mStyle & BCS_ALIGN_RIGHT) $sPos = @mWidth - $size;
234 else $sPos = 0;
235
236 # Total height of bar code -Bars only-#
237 if (@mStyle & BCS_DRAW_TEXT) $ysize = @mHeight - BCD_DEFAULT_MAR_Y1 - BCD_DEFAULT_MAR_Y2 - GetFontHeight(@mFont);
238 else $ysize = @mHeight - BCD_DEFAULT_MAR_Y1 - BCD_DEFAULT_MAR_Y2;
239
240 # Draw text#
241 if (@mStyle & BCS_DRAW_TEXT)
242 if (@mStyle & BCS_STRETCH_TEXT)
243 for ($i=0;$i<$len;$i++)
244 @DrawChar(@mFont, $sPos+($narrow*6+$wide*3)+($size/$len)*$i,
245 $ysize + BCD_DEFAULT_MAR_Y1 + BCD_DEFAULT_TEXT_OFFSET, @mValue[$i]);
246 else# Center#
247 $text_width = GetFontWidth(@mFont)# @mValue.length;
248 @DrawText(@mFont, $sPos+(($size-$text_width)/2)+($narrow*6+$wide*3),
249 $ysize + BCD_DEFAULT_MAR_Y1 + BCD_DEFAULT_TEXT_OFFSET, @mValue);
250 end
251 end
252
253 $DrawPos = @DrawStart($sPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres);
254 do {
255 $c = GetCharIndex(@mValue[$cPos]);
256 $cset = @mCharSet[$c];
257 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[0] == '0') ? $narrow : $wide , $ysize);
258 $DrawPos += ($cset[0] == '0') ? $narrow : $wide;
259 $DrawPos += ($cset[1] == '0') ? $narrow : $wide;
260 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[2] == '0') ? $narrow : $wide , $ysize);
261 $DrawPos += ($cset[2] == '0') ? $narrow : $wide;
262 $DrawPos += ($cset[3] == '0') ? $narrow : $wide;
263 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[4] == '0') ? $narrow : $wide , $ysize);
264 $DrawPos += ($cset[4] == '0') ? $narrow : $wide;
265 $DrawPos += ($cset[5] == '0') ? $narrow : $wide;
266 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[6] == '0') ? $narrow : $wide , $ysize);
267 $DrawPos += ($cset[6] == '0') ? $narrow : $wide;
268 $DrawPos += ($cset[7] == '0') ? $narrow : $wide;
269 @DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[8] == '0') ? $narrow : $wide , $ysize);
270 $DrawPos += ($cset[8] == '0') ? $narrow : $wide;
271 $DrawPos += $narrow; # Space between chars#
272 $cPos += 1;
273 end while ($cPos<$len);
274 $DrawPos = @DrawStop($DrawPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres);
275 return true;
276 end
277 }
278
279 #============================================================+
280 # END OF FILE
281 #============================================================+