KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > BarcodeInter25


1 /*
2  * $Id: BarcodeInter25.java 2374 2006-09-15 05:11:54Z xlv $
3  *
4  * Copyright 2002-2006 by Paulo Soares.
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49 package com.lowagie.text.pdf;
50
51 import java.awt.Canvas JavaDoc;
52 import java.awt.Color JavaDoc;
53 import java.awt.Image JavaDoc;
54 import java.awt.image.MemoryImageSource JavaDoc;
55
56 import com.lowagie.text.Element;
57 import com.lowagie.text.ExceptionConverter;
58 import com.lowagie.text.Rectangle;
59
60 /** Implements the code interleaved 2 of 5. The text can include
61  * non numeric characters that are printed but do not generate bars.
62  * The default parameters are:
63  * <pre>
64  *x = 0.8f;
65  *n = 2;
66  *font = BaseFont.createFont("Helvetica", "winansi", false);
67  *size = 8;
68  *baseline = size;
69  *barHeight = size * 3;
70  *textAlignment = Element.ALIGN_CENTER;
71  *generateChecksum = false;
72  *checksumText = false;
73  * </pre>
74  *
75  * @author Paulo Soares (psoares@consiste.pt)
76  */

77 public class BarcodeInter25 extends Barcode{
78
79     /** The bars to generate the code.
80      */

81     private static final byte BARS[][] =
82     {
83         {0,0,1,1,0},
84         {1,0,0,0,1},
85         {0,1,0,0,1},
86         {1,1,0,0,0},
87         {0,0,1,0,1},
88         {1,0,1,0,0},
89         {0,1,1,0,0},
90         {0,0,0,1,1},
91         {1,0,0,1,0},
92         {0,1,0,1,0}
93     };
94
95     /** Creates new BarcodeInter25 */
96     public BarcodeInter25() {
97         try {
98             x = 0.8f;
99             n = 2;
100             font = BaseFont.createFont("Helvetica", "winansi", false);
101             size = 8;
102             baseline = size;
103             barHeight = size * 3;
104             textAlignment = Element.ALIGN_CENTER;
105             generateChecksum = false;
106             checksumText = false;
107         }
108         catch (Exception JavaDoc e) {
109             throw new ExceptionConverter(e);
110         }
111     }
112     
113     /** Deletes all the non numeric characters from <CODE>text</CODE>.
114      * @param text the text
115      * @return a <CODE>String</CODE> with only numeric characters
116      */

117     public static String JavaDoc keepNumbers(String JavaDoc text) {
118         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
119         for (int k = 0; k < text.length(); ++k) {
120             char c = text.charAt(k);
121             if (c >= '0' && c <= '9')
122                 sb.append(c);
123         }
124         return sb.toString();
125     }
126     
127     /** Calculates the checksum.
128      * @param text the numeric text
129      * @return the checksum
130      */

131     public static char getChecksum(String JavaDoc text) {
132         int mul = 3;
133         int total = 0;
134         for (int k = text.length() - 1; k >= 0; --k) {
135             int n = text.charAt(k) - '0';
136             total += mul * n;
137             mul ^= 2;
138         }
139         return (char)(((10 - (total % 10)) % 10) + '0');
140     }
141
142     /** Creates the bars for the barcode.
143      * @param text the text. It can contain non numeric characters
144      * @return the barcode
145      */

146     public static byte[] getBarsInter25(String JavaDoc text) {
147         text = keepNumbers(text);
148         if ((text.length() & 1) != 0)
149             throw new IllegalArgumentException JavaDoc("The text length must be even.");
150         byte bars[] = new byte[text.length() * 5 + 7];
151         int pb = 0;
152         bars[pb++] = 0;
153         bars[pb++] = 0;
154         bars[pb++] = 0;
155         bars[pb++] = 0;
156         int len = text.length() / 2;
157         for (int k = 0; k < len; ++k) {
158             int c1 = text.charAt(k * 2) - '0';
159             int c2 = text.charAt(k * 2 + 1) - '0';
160             byte b1[] = BARS[c1];
161             byte b2[] = BARS[c2];
162             for (int j = 0; j < 5; ++j) {
163                 bars[pb++] = b1[j];
164                 bars[pb++] = b2[j];
165             }
166         }
167         bars[pb++] = 1;
168         bars[pb++] = 0;
169         bars[pb++] = 0;
170         return bars;
171     }
172
173     /** Gets the maximum area that the barcode and the text, if
174      * any, will occupy. The lower left corner is always (0, 0).
175      * @return the size the barcode occupies.
176      */

177     public Rectangle getBarcodeSize() {
178         float fontX = 0;
179         float fontY = 0;
180         if (font != null) {
181             if (baseline > 0)
182                 fontY = baseline - font.getFontDescriptor(BaseFont.DESCENT, size);
183             else
184                 fontY = -baseline + size;
185             String JavaDoc fullCode = code;
186             if (generateChecksum && checksumText)
187                 fullCode += getChecksum(fullCode);
188             fontX = font.getWidthPoint(altText != null ? altText : fullCode, size);
189         }
190         String JavaDoc fullCode = keepNumbers(code);
191         int len = fullCode.length();
192         if (generateChecksum)
193             ++len;
194         float fullWidth = len * (3 * x + 2 * x * n) + (6 + n ) * x;
195         fullWidth = Math.max(fullWidth, fontX);
196         float fullHeight = barHeight + fontY;
197         return new Rectangle(fullWidth, fullHeight);
198     }
199     
200     /** Places the barcode in a <CODE>PdfContentByte</CODE>. The
201      * barcode is always placed at coodinates (0, 0). Use the
202      * translation matrix to move it elsewhere.<p>
203      * The bars and text are written in the following colors:<p>
204      * <P><TABLE BORDER=1>
205      * <TR>
206      * <TH><P><CODE>barColor</CODE></TH>
207      * <TH><P><CODE>textColor</CODE></TH>
208      * <TH><P>Result</TH>
209      * </TR>
210      * <TR>
211      * <TD><P><CODE>null</CODE></TD>
212      * <TD><P><CODE>null</CODE></TD>
213      * <TD><P>bars and text painted with current fill color</TD>
214      * </TR>
215      * <TR>
216      * <TD><P><CODE>barColor</CODE></TD>
217      * <TD><P><CODE>null</CODE></TD>
218      * <TD><P>bars and text painted with <CODE>barColor</CODE></TD>
219      * </TR>
220      * <TR>
221      * <TD><P><CODE>null</CODE></TD>
222      * <TD><P><CODE>textColor</CODE></TD>
223      * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD>
224      * </TR>
225      * <TR>
226      * <TD><P><CODE>barColor</CODE></TD>
227      * <TD><P><CODE>textColor</CODE></TD>
228      * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD>
229      * </TR>
230      * </TABLE>
231      * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed
232      * @param barColor the color of the bars. It can be <CODE>null</CODE>
233      * @param textColor the color of the text. It can be <CODE>null</CODE>
234      * @return the dimensions the barcode occupies
235      */

236     public Rectangle placeBarcode(PdfContentByte cb, Color JavaDoc barColor, Color JavaDoc textColor) {
237         String JavaDoc fullCode = code;
238         float fontX = 0;
239         if (font != null) {
240             if (generateChecksum && checksumText)
241                 fullCode += getChecksum(fullCode);
242             fontX = font.getWidthPoint(fullCode = altText != null ? altText : fullCode, size);
243         }
244         String JavaDoc bCode = keepNumbers(code);
245         if (generateChecksum)
246             bCode += getChecksum(bCode);
247         int len = bCode.length();
248         float fullWidth = len * (3 * x + 2 * x * n) + (6 + n ) * x;
249         float barStartX = 0;
250         float textStartX = 0;
251         switch (textAlignment) {
252             case Element.ALIGN_LEFT:
253                 break;
254             case Element.ALIGN_RIGHT:
255                 if (fontX > fullWidth)
256                     barStartX = fontX - fullWidth;
257                 else
258                     textStartX = fullWidth - fontX;
259                 break;
260             default:
261                 if (fontX > fullWidth)
262                     barStartX = (fontX - fullWidth) / 2;
263                 else
264                     textStartX = (fullWidth - fontX) / 2;
265                 break;
266         }
267         float barStartY = 0;
268         float textStartY = 0;
269         if (font != null) {
270             if (baseline <= 0)
271                 textStartY = barHeight - baseline;
272             else {
273                 textStartY = -font.getFontDescriptor(BaseFont.DESCENT, size);
274                 barStartY = textStartY + baseline;
275             }
276         }
277         byte bars[] = getBarsInter25(bCode);
278         boolean print = true;
279         if (barColor != null)
280             cb.setColorFill(barColor);
281         for (int k = 0; k < bars.length; ++k) {
282             float w = (bars[k] == 0 ? x : x * n);
283             if (print)
284                 cb.rectangle(barStartX, barStartY, w - inkSpreading, barHeight);
285             print = !print;
286             barStartX += w;
287         }
288         cb.fill();
289         if (font != null) {
290             if (textColor != null)
291                 cb.setColorFill(textColor);
292             cb.beginText();
293             cb.setFontAndSize(font, size);
294             cb.setTextMatrix(textStartX, textStartY);
295             cb.showText(fullCode);
296             cb.endText();
297         }
298         return getBarcodeSize();
299     }
300     
301     /** Creates a <CODE>java.awt.Image</CODE>. This image only
302      * contains the bars without any text.
303      * @param foreground the color of the bars
304      * @param background the color of the background
305      * @return the image
306      */

307     public java.awt.Image JavaDoc createAwtImage(Color JavaDoc foreground, Color JavaDoc background) {
308         int f = foreground.getRGB();
309         int g = background.getRGB();
310         Canvas JavaDoc canvas = new Canvas JavaDoc();
311
312         String JavaDoc bCode = keepNumbers(code);
313         if (generateChecksum)
314             bCode += getChecksum(bCode);
315         int len = bCode.length();
316         int nn = (int)n;
317         int fullWidth = len * (3 + 2 * nn) + (6 + nn );
318         byte bars[] = getBarsInter25(bCode);
319         boolean print = true;
320         int ptr = 0;
321         int height = (int)barHeight;
322         int pix[] = new int[fullWidth * height];
323         for (int k = 0; k < bars.length; ++k) {
324             int w = (bars[k] == 0 ? 1 : nn);
325             int c = g;
326             if (print)
327                 c = f;
328             print = !print;
329             for (int j = 0; j < w; ++j)
330                 pix[ptr++] = c;
331         }
332         for (int k = fullWidth; k < pix.length; k += fullWidth) {
333             System.arraycopy(pix, 0, pix, k, fullWidth);
334         }
335         Image JavaDoc img = canvas.createImage(new MemoryImageSource JavaDoc(fullWidth, height, pix, 0, fullWidth));
336         
337         return img;
338     }
339 }
Popular Tags