001: /*
002: * Copyright 2002 by Paulo Soares.
003: *
004: * The contents of this file are subject to the Mozilla Public License Version 1.1
005: * (the "License"); you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the License.
011: *
012: * The Original Code is 'iText, a free JAVA-PDF library'.
013: *
014: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
015: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
016: * All Rights Reserved.
017: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
018: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
019: *
020: * Contributor(s): all the names of the contributors are added in the source code
021: * where applicable.
022: *
023: * Alternatively, the contents of this file may be used under the terms of the
024: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
025: * provisions of LGPL are applicable instead of those above. If you wish to
026: * allow use of your version of this file only under the terms of the LGPL
027: * License and not to allow others to use your version of this file under
028: * the MPL, indicate your decision by deleting the provisions above and
029: * replace them with the notice and other provisions required by the LGPL.
030: * If you do not delete the provisions above, a recipient may use your version
031: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
032: *
033: * This library is free software; you can redistribute it and/or modify it
034: * under the terms of the MPL as stated above or under the terms of the GNU
035: * Library General Public License as published by the Free Software Foundation;
036: * either version 2 of the License, or any later version.
037: *
038: * This library is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
040: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
041: * details.
042: *
043: * If you didn't download this code from the following link, you should check if
044: * you aren't using an obsolete version:
045: * http://www.lowagie.com/iText/
046: */
047: package com.lowagie.text.pdf;
048:
049: import java.awt.Canvas;
050: import java.awt.Color;
051: import java.awt.Image;
052: import java.awt.image.MemoryImageSource;
053:
054: import com.lowagie.text.Rectangle;
055:
056: /** Implements the Postnet and Planet barcodes. The default parameters are:
057: * <pre>
058: *n = 72f / 22f; // distance between bars
059: *x = 0.02f * 72f; // bar width
060: *barHeight = 0.125f * 72f; // height of the tall bars
061: *size = 0.05f * 72f; // height of the short bars
062: *codeType = POSTNET; // type of code
063: * </pre>
064: *
065: * @author Paulo Soares (psoares@consiste.pt)
066: */
067: public class BarcodePostnet extends Barcode {
068:
069: /** The bars for each character.
070: */
071: private static final byte BARS[][] = { { 1, 1, 0, 0, 0 },
072: { 0, 0, 0, 1, 1 }, { 0, 0, 1, 0, 1 }, { 0, 0, 1, 1, 0 },
073: { 0, 1, 0, 0, 1 }, { 0, 1, 0, 1, 0 }, { 0, 1, 1, 0, 0 },
074: { 1, 0, 0, 0, 1 }, { 1, 0, 0, 1, 0 }, { 1, 0, 1, 0, 0 } };
075:
076: /** Creates new BarcodePostnet */
077: public BarcodePostnet() {
078: n = 72f / 22f; // distance between bars
079: x = 0.02f * 72f; // bar width
080: barHeight = 0.125f * 72f; // height of the tall bars
081: size = 0.05f * 72f; // height of the short bars
082: codeType = POSTNET; // type of code
083: }
084:
085: /** Creates the bars for Postnet.
086: * @param text the code to be created without checksum
087: * @return the bars
088: */
089: public static byte[] getBarsPostnet(String text) {
090: int total = 0;
091: for (int k = text.length() - 1; k >= 0; --k) {
092: int n = text.charAt(k) - '0';
093: total += n;
094: }
095: text += (char) (((10 - (total % 10)) % 10) + '0');
096: byte bars[] = new byte[text.length() * 5 + 2];
097: bars[0] = 1;
098: bars[bars.length - 1] = 1;
099: for (int k = 0; k < text.length(); ++k) {
100: int c = text.charAt(k) - '0';
101: System.arraycopy(BARS[c], 0, bars, k * 5 + 1, 5);
102: }
103: return bars;
104: }
105:
106: /** Gets the maximum area that the barcode and the text, if
107: * any, will occupy. The lower left corner is always (0, 0).
108: * @return the size the barcode occupies.
109: */
110: public Rectangle getBarcodeSize() {
111: float width = ((code.length() + 1) * 5 + 1) * n + x;
112: return new Rectangle(width, barHeight);
113: }
114:
115: /** Places the barcode in a <CODE>PdfContentByte</CODE>. The
116: * barcode is always placed at coodinates (0, 0). Use the
117: * translation matrix to move it elsewhere.<p>
118: * The bars and text are written in the following colors:<p>
119: * <P><TABLE BORDER=1>
120: * <TR>
121: * <TH><P><CODE>barColor</CODE></TH>
122: * <TH><P><CODE>textColor</CODE></TH>
123: * <TH><P>Result</TH>
124: * </TR>
125: * <TR>
126: * <TD><P><CODE>null</CODE></TD>
127: * <TD><P><CODE>null</CODE></TD>
128: * <TD><P>bars and text painted with current fill color</TD>
129: * </TR>
130: * <TR>
131: * <TD><P><CODE>barColor</CODE></TD>
132: * <TD><P><CODE>null</CODE></TD>
133: * <TD><P>bars and text painted with <CODE>barColor</CODE></TD>
134: * </TR>
135: * <TR>
136: * <TD><P><CODE>null</CODE></TD>
137: * <TD><P><CODE>textColor</CODE></TD>
138: * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD>
139: * </TR>
140: * <TR>
141: * <TD><P><CODE>barColor</CODE></TD>
142: * <TD><P><CODE>textColor</CODE></TD>
143: * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD>
144: * </TR>
145: * </TABLE>
146: * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed
147: * @param barColor the color of the bars. It can be <CODE>null</CODE>
148: * @param textColor the color of the text. It can be <CODE>null</CODE>
149: * @return the dimensions the barcode occupies
150: */
151: public Rectangle placeBarcode(PdfContentByte cb, Color barColor,
152: Color textColor) {
153: if (barColor != null)
154: cb.setColorFill(barColor);
155: byte bars[] = getBarsPostnet(code);
156: byte flip = 1;
157: if (codeType == PLANET) {
158: flip = 0;
159: bars[0] = 0;
160: bars[bars.length - 1] = 0;
161: }
162: float startX = 0;
163: for (int k = 0; k < bars.length; ++k) {
164: cb.rectangle(startX, 0, x - inkSpreading,
165: bars[k] == flip ? barHeight : size);
166: startX += n;
167: }
168: cb.fill();
169: return getBarcodeSize();
170: }
171:
172: /** Creates a <CODE>java.awt.Image</CODE>. This image only
173: * contains the bars without any text.
174: * @param foreground the color of the bars
175: * @param background the color of the background
176: * @return the image
177: *
178: */
179: public java.awt.Image createAwtImage(Color foreground,
180: Color background) {
181: int f = foreground.getRGB();
182: int g = background.getRGB();
183: Canvas canvas = new Canvas();
184: int barWidth = (int) x;
185: if (barWidth <= 0)
186: barWidth = 1;
187: int barDistance = (int) n;
188: if (barDistance <= barWidth)
189: barDistance = barWidth + 1;
190: int barShort = (int) size;
191: if (barShort <= 0)
192: barShort = 1;
193: int barTall = (int) barHeight;
194: if (barTall <= barShort)
195: barTall = barShort + 1;
196: int width = ((code.length() + 1) * 5 + 1) * barDistance
197: + barWidth;
198: int pix[] = new int[width * barTall];
199: byte bars[] = getBarsPostnet(code);
200: byte flip = 1;
201: if (codeType == PLANET) {
202: flip = 0;
203: bars[0] = 0;
204: bars[bars.length - 1] = 0;
205: }
206: int idx = 0;
207: for (int k = 0; k < bars.length; ++k) {
208: boolean dot = (bars[k] == flip);
209: for (int j = 0; j < barDistance; ++j) {
210: pix[idx + j] = ((dot && j < barWidth) ? f : g);
211: }
212: idx += barDistance;
213: }
214: int limit = width * (barTall - barShort);
215: for (int k = width; k < limit; k += width)
216: System.arraycopy(pix, 0, pix, k, width);
217: idx = limit;
218: for (int k = 0; k < bars.length; ++k) {
219: for (int j = 0; j < barDistance; ++j) {
220: pix[idx + j] = ((j < barWidth) ? f : g);
221: }
222: idx += barDistance;
223: }
224: for (int k = limit + width; k < pix.length; k += width)
225: System.arraycopy(pix, limit, pix, k, width);
226: Image img = canvas.createImage(new MemoryImageSource(width,
227: barTall, pix, 0, width));
228:
229: return img;
230: }
231: }
|