001: /*
002: * $Id: Barcode.java 2505 2006-12-20 10:12:33Z blowagie $
003: *
004: * Copyright 2002-2006 by Paulo Soares.
005: *
006: * The contents of this file are subject to the Mozilla Public License Version 1.1
007: * (the "License"); you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the License.
013: *
014: * The Original Code is 'iText, a free JAVA-PDF library'.
015: *
016: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
017: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
018: * All Rights Reserved.
019: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
020: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
021: *
022: * Contributor(s): all the names of the contributors are added in the source code
023: * where applicable.
024: *
025: * Alternatively, the contents of this file may be used under the terms of the
026: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
027: * provisions of LGPL are applicable instead of those above. If you wish to
028: * allow use of your version of this file only under the terms of the LGPL
029: * License and not to allow others to use your version of this file under
030: * the MPL, indicate your decision by deleting the provisions above and
031: * replace them with the notice and other provisions required by the LGPL.
032: * If you do not delete the provisions above, a recipient may use your version
033: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
034: *
035: * This library is free software; you can redistribute it and/or modify it
036: * under the terms of the MPL as stated above or under the terms of the GNU
037: * Library General Public License as published by the Free Software Foundation;
038: * either version 2 of the License, or any later version.
039: *
040: * This library is distributed in the hope that it will be useful, but WITHOUT
041: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
042: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
043: * details.
044: *
045: * If you didn't download this code from the following link, you should check if
046: * you aren't using an obsolete version:
047: * http://www.lowagie.com/iText/
048: */
049: package com.lowagie.text.pdf;
050:
051: import java.awt.Color;
052:
053: import com.lowagie.text.ExceptionConverter;
054: import com.lowagie.text.Image;
055: import com.lowagie.text.Rectangle;
056:
057: /** Base class containing properties and methods commom to all
058: * barcode types.
059: *
060: * @author Paulo Soares (psoares@consiste.pt)
061: */
062: public abstract class Barcode {
063: /** A type of barcode */
064: public static final int EAN13 = 1;
065: /** A type of barcode */
066: public static final int EAN8 = 2;
067: /** A type of barcode */
068: public static final int UPCA = 3;
069: /** A type of barcode */
070: public static final int UPCE = 4;
071: /** A type of barcode */
072: public static final int SUPP2 = 5;
073: /** A type of barcode */
074: public static final int SUPP5 = 6;
075: /** A type of barcode */
076: public static final int POSTNET = 7;
077: /** A type of barcode */
078: public static final int PLANET = 8;
079: /** A type of barcode */
080: public static final int CODE128 = 9;
081: /** A type of barcode */
082: public static final int CODE128_UCC = 10;
083: /** A type of barcode */
084: public static final int CODE128_RAW = 11;
085: /** A type of barcode */
086: public static final int CODABAR = 12;
087:
088: /** The minimum bar width.
089: */
090: protected float x;
091:
092: /** The bar multiplier for wide bars or the distance between
093: * bars for Postnet and Planet.
094: */
095: protected float n;
096:
097: /** The text font. <CODE>null</CODE> if no text.
098: */
099: protected BaseFont font;
100:
101: /** The size of the text or the height of the shorter bar
102: * in Postnet.
103: */
104: protected float size;
105:
106: /** If positive, the text distance under the bars. If zero or negative,
107: * the text distance above the bars.
108: */
109: protected float baseline;
110:
111: /** The height of the bars.
112: */
113: protected float barHeight;
114:
115: /** The text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>,
116: * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>.
117: */
118: protected int textAlignment;
119:
120: /** The optional checksum generation.
121: */
122: protected boolean generateChecksum;
123:
124: /** Shows the generated checksum in the the text.
125: */
126: protected boolean checksumText;
127:
128: /** Show the start and stop character '*' in the text for
129: * the barcode 39 or 'ABCD' for codabar.
130: */
131: protected boolean startStopText;
132:
133: /** Generates extended barcode 39.
134: */
135: protected boolean extended;
136:
137: /** The code to generate.
138: */
139: protected String code = "";
140:
141: /** Show the guard bars for barcode EAN.
142: */
143: protected boolean guardBars;
144:
145: /** The code type.
146: */
147: protected int codeType;
148:
149: /** The ink spreading. */
150: protected float inkSpreading = 0;
151:
152: /** Gets the minimum bar width.
153: * @return the minimum bar width
154: */
155: public float getX() {
156: return x;
157: }
158:
159: /** Sets the minimum bar width.
160: * @param x the minimum bar width
161: */
162: public void setX(float x) {
163: this .x = x;
164: }
165:
166: /** Gets the bar multiplier for wide bars.
167: * @return the bar multiplier for wide bars
168: */
169: public float getN() {
170: return n;
171: }
172:
173: /** Sets the bar multiplier for wide bars.
174: * @param n the bar multiplier for wide bars
175: */
176: public void setN(float n) {
177: this .n = n;
178: }
179:
180: /** Gets the text font. <CODE>null</CODE> if no text.
181: * @return the text font. <CODE>null</CODE> if no text
182: */
183: public BaseFont getFont() {
184: return font;
185: }
186:
187: /** Sets the text font.
188: * @param font the text font. Set to <CODE>null</CODE> to suppress any text
189: */
190: public void setFont(BaseFont font) {
191: this .font = font;
192: }
193:
194: /** Gets the size of the text.
195: * @return the size of the text
196: */
197: public float getSize() {
198: return size;
199: }
200:
201: /** Sets the size of the text.
202: * @param size the size of the text
203: */
204: public void setSize(float size) {
205: this .size = size;
206: }
207:
208: /** Gets the text baseline.
209: * If positive, the text distance under the bars. If zero or negative,
210: * the text distance above the bars.
211: * @return the baseline.
212: */
213: public float getBaseline() {
214: return baseline;
215: }
216:
217: /** Sets the text baseline.
218: * If positive, the text distance under the bars. If zero or negative,
219: * the text distance above the bars.
220: * @param baseline the baseline.
221: */
222: public void setBaseline(float baseline) {
223: this .baseline = baseline;
224: }
225:
226: /** Gets the height of the bars.
227: * @return the height of the bars
228: */
229: public float getBarHeight() {
230: return barHeight;
231: }
232:
233: /** Sets the height of the bars.
234: * @param barHeight the height of the bars
235: */
236: public void setBarHeight(float barHeight) {
237: this .barHeight = barHeight;
238: }
239:
240: /** Gets the text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>,
241: * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>.
242: * @return the text alignment
243: */
244: public int getTextAlignment() {
245: return textAlignment;
246: }
247:
248: /** Sets the text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>,
249: * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>.
250: * @param textAlignment the text alignment
251: */
252: public void setTextAlignment(int textAlignment) {
253: this .textAlignment = textAlignment;
254: }
255:
256: /** Gets the optional checksum generation.
257: * @return the optional checksum generation
258: */
259: public boolean isGenerateChecksum() {
260: return generateChecksum;
261: }
262:
263: /** Setter for property generateChecksum.
264: * @param generateChecksum New value of property generateChecksum.
265: */
266: public void setGenerateChecksum(boolean generateChecksum) {
267: this .generateChecksum = generateChecksum;
268: }
269:
270: /** Gets the property to show the generated checksum in the the text.
271: * @return value of property checksumText
272: */
273: public boolean isChecksumText() {
274: return checksumText;
275: }
276:
277: /** Sets the property to show the generated checksum in the the text.
278: * @param checksumText new value of property checksumText
279: */
280: public void setChecksumText(boolean checksumText) {
281: this .checksumText = checksumText;
282: }
283:
284: /** Sets the property to show the start and stop character '*' in the text for
285: * the barcode 39.
286: * @return value of property startStopText
287: */
288: public boolean isStartStopText() {
289: return startStopText;
290: }
291:
292: /** Gets the property to show the start and stop character '*' in the text for
293: * the barcode 39.
294: * @param startStopText new value of property startStopText
295: */
296: public void setStartStopText(boolean startStopText) {
297: this .startStopText = startStopText;
298: }
299:
300: /** Gets the property to generate extended barcode 39.
301: * @return value of property extended.
302: */
303: public boolean isExtended() {
304: return extended;
305: }
306:
307: /** Sets the property to generate extended barcode 39.
308: * @param extended new value of property extended
309: */
310: public void setExtended(boolean extended) {
311: this .extended = extended;
312: }
313:
314: /** Gets the code to generate.
315: * @return the code to generate
316: */
317: public String getCode() {
318: return code;
319: }
320:
321: /** Sets the code to generate.
322: * @param code the code to generate
323: */
324: public void setCode(String code) {
325: this .code = code;
326: }
327:
328: /** Gets the property to show the guard bars for barcode EAN.
329: * @return value of property guardBars
330: */
331: public boolean isGuardBars() {
332: return guardBars;
333: }
334:
335: /** Sets the property to show the guard bars for barcode EAN.
336: * @param guardBars new value of property guardBars
337: */
338: public void setGuardBars(boolean guardBars) {
339: this .guardBars = guardBars;
340: }
341:
342: /** Gets the code type.
343: * @return the code type
344: */
345: public int getCodeType() {
346: return codeType;
347: }
348:
349: /** Sets the code type.
350: * @param codeType the code type
351: */
352: public void setCodeType(int codeType) {
353: this .codeType = codeType;
354: }
355:
356: /** Gets the maximum area that the barcode and the text, if
357: * any, will occupy. The lower left corner is always (0, 0).
358: * @return the size the barcode occupies.
359: */
360: public abstract Rectangle getBarcodeSize();
361:
362: /** Places the barcode in a <CODE>PdfContentByte</CODE>. The
363: * barcode is always placed at coodinates (0, 0). Use the
364: * translation matrix to move it elsewhere.<p>
365: * The bars and text are written in the following colors:<p>
366: * <P><TABLE BORDER=1>
367: * <TR>
368: * <TH><P><CODE>barColor</CODE></TH>
369: * <TH><P><CODE>textColor</CODE></TH>
370: * <TH><P>Result</TH>
371: * </TR>
372: * <TR>
373: * <TD><P><CODE>null</CODE></TD>
374: * <TD><P><CODE>null</CODE></TD>
375: * <TD><P>bars and text painted with current fill color</TD>
376: * </TR>
377: * <TR>
378: * <TD><P><CODE>barColor</CODE></TD>
379: * <TD><P><CODE>null</CODE></TD>
380: * <TD><P>bars and text painted with <CODE>barColor</CODE></TD>
381: * </TR>
382: * <TR>
383: * <TD><P><CODE>null</CODE></TD>
384: * <TD><P><CODE>textColor</CODE></TD>
385: * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD>
386: * </TR>
387: * <TR>
388: * <TD><P><CODE>barColor</CODE></TD>
389: * <TD><P><CODE>textColor</CODE></TD>
390: * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD>
391: * </TR>
392: * </TABLE>
393: * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed
394: * @param barColor the color of the bars. It can be <CODE>null</CODE>
395: * @param textColor the color of the text. It can be <CODE>null</CODE>
396: * @return the dimensions the barcode occupies
397: */
398: public abstract Rectangle placeBarcode(PdfContentByte cb,
399: Color barColor, Color textColor);
400:
401: /** Creates a template with the barcode.
402: * @param cb the <CODE>PdfContentByte</CODE> to create the template. It
403: * serves no other use
404: * @param barColor the color of the bars. It can be <CODE>null</CODE>
405: * @param textColor the color of the text. It can be <CODE>null</CODE>
406: * @return the template
407: * @see #placeBarcode(PdfContentByte cb, Color barColor, Color textColor)
408: */
409: public PdfTemplate createTemplateWithBarcode(PdfContentByte cb,
410: Color barColor, Color textColor) {
411: PdfTemplate tp = cb.createTemplate(0, 0);
412: Rectangle rect = placeBarcode(tp, barColor, textColor);
413: tp.setBoundingBox(rect);
414: return tp;
415: }
416:
417: /** Creates an <CODE>Image</CODE> with the barcode.
418: * @param cb the <CODE>PdfContentByte</CODE> to create the <CODE>Image</CODE>. It
419: * serves no other use
420: * @param barColor the color of the bars. It can be <CODE>null</CODE>
421: * @param textColor the color of the text. It can be <CODE>null</CODE>
422: * @return the <CODE>Image</CODE>
423: * @see #placeBarcode(PdfContentByte cb, Color barColor, Color textColor)
424: */
425: public Image createImageWithBarcode(PdfContentByte cb,
426: Color barColor, Color textColor) {
427: try {
428: return Image.getInstance(createTemplateWithBarcode(cb,
429: barColor, textColor));
430: } catch (Exception e) {
431: throw new ExceptionConverter(e);
432: }
433: }
434:
435: /** Creates a <CODE>java.awt.Image</CODE>. This image only
436: * contains the bars without any text.
437: * @param foreground the color of the bars
438: * @param background the color of the background
439: * @return the image
440: */
441: public abstract java.awt.Image createAwtImage(Color foreground,
442: Color background);
443:
444: /** Gets the amount of ink spreading.
445: * @return the ink spreading
446: *
447: */
448: public float getInkSpreading() {
449: return this .inkSpreading;
450: }
451:
452: /** Sets the amount of ink spreading. This value will be subtracted
453: * to the width of each bar. The actual value will depend on the ink
454: * and the printing medium.
455: * @param inkSpreading the ink spreading
456: *
457: */
458: public void setInkSpreading(float inkSpreading) {
459: this .inkSpreading = inkSpreading;
460: }
461:
462: /**
463: * The alternate text to be used, if present.
464: */
465: protected String altText;
466:
467: /**
468: * Gets the alternate text.
469: * @return the alternate text
470: */
471: public String getAltText() {
472: return this .altText;
473: }
474:
475: /**
476: * Sets the alternate text. If present, this text will be used instead of the
477: * text derived from the supplied code.
478: * @param altText the alternate text
479: */
480: public void setAltText(String altText) {
481: this.altText = altText;
482: }
483:
484: }
|