KickJava   Java API By Example, From Geeks To Geeks.

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


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

47 package com.lowagie.text.pdf;
48 import java.awt.Color JavaDoc;
49
50 import com.lowagie.text.Rectangle;
51
52 /** This class takes 2 barcodes, an EAN/UPC and a supplemental
53  * and creates a single barcode with both combined in the
54  * expected layout. The UPC/EAN should have a positive text
55   * baseline and the supplemental a negative one (in the supplemental
56  * the text is on the top of the barcode.<p>
57  * The default parameters are:
58  * <pre>
59  *n = 8; // horizontal distance between the two barcodes
60  * </pre>
61  *
62  * @author Paulo Soares (psoares@consiste.pt)
63  */

64 public class BarcodeEANSUPP extends Barcode{
65     
66     /** The barcode with the EAN/UPC.
67      */

68     protected Barcode ean;
69     /** The barcode with the supplemental.
70      */

71     protected Barcode supp;
72     
73     /** Creates new combined barcode.
74      * @param ean the EAN/UPC barcode
75      * @param supp the supplemental barcode
76      */

77     public BarcodeEANSUPP(Barcode ean, Barcode supp) {
78         n = 8; // horizontal distance between the two barcodes
79
this.ean = ean;
80         this.supp = supp;
81     }
82     
83     /** Gets the maximum area that the barcode and the text, if
84      * any, will occupy. The lower left corner is always (0, 0).
85      * @return the size the barcode occupies.
86      */

87     public Rectangle getBarcodeSize() {
88         Rectangle rect = ean.getBarcodeSize();
89         rect.setRight(rect.getWidth() + supp.getBarcodeSize().getWidth() + n);
90         return rect;
91     }
92     
93     /** Places the barcode in a <CODE>PdfContentByte</CODE>. The
94      * barcode is always placed at coodinates (0, 0). Use the
95      * translation matrix to move it elsewhere.<p>
96      * The bars and text are written in the following colors:<p>
97      * <P><TABLE BORDER=1>
98      * <TR>
99      * <TH><P><CODE>barColor</CODE></TH>
100      * <TH><P><CODE>textColor</CODE></TH>
101      * <TH><P>Result</TH>
102      * </TR>
103      * <TR>
104      * <TD><P><CODE>null</CODE></TD>
105      * <TD><P><CODE>null</CODE></TD>
106      * <TD><P>bars and text painted with current fill color</TD>
107      * </TR>
108      * <TR>
109      * <TD><P><CODE>barColor</CODE></TD>
110      * <TD><P><CODE>null</CODE></TD>
111      * <TD><P>bars and text painted with <CODE>barColor</CODE></TD>
112      * </TR>
113      * <TR>
114      * <TD><P><CODE>null</CODE></TD>
115      * <TD><P><CODE>textColor</CODE></TD>
116      * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD>
117      * </TR>
118      * <TR>
119      * <TD><P><CODE>barColor</CODE></TD>
120      * <TD><P><CODE>textColor</CODE></TD>
121      * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD>
122      * </TR>
123      * </TABLE>
124      * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed
125      * @param barColor the color of the bars. It can be <CODE>null</CODE>
126      * @param textColor the color of the text. It can be <CODE>null</CODE>
127      * @return the dimensions the barcode occupies
128      */

129     public Rectangle placeBarcode(PdfContentByte cb, Color JavaDoc barColor, Color JavaDoc textColor) {
130         if (supp.getFont() != null)
131             supp.setBarHeight(ean.getBarHeight() + supp.getBaseline() - supp.getFont().getFontDescriptor(BaseFont.CAPHEIGHT, supp.getSize()));
132         else
133             supp.setBarHeight(ean.getBarHeight());
134         Rectangle eanR = ean.getBarcodeSize();
135         cb.saveState();
136         ean.placeBarcode(cb, barColor, textColor);
137         cb.restoreState();
138         cb.saveState();
139         cb.concatCTM(1, 0, 0, 1, eanR.getWidth() + n, eanR.getHeight() - ean.getBarHeight());
140         supp.placeBarcode(cb, barColor, textColor);
141         cb.restoreState();
142         return getBarcodeSize();
143     }
144     
145     /** Creates a <CODE>java.awt.Image</CODE>. This image only
146      * contains the bars without any text.
147      * @param foreground the color of the bars
148      * @param background the color of the background
149      * @return the image
150      */

151     public java.awt.Image JavaDoc createAwtImage(Color JavaDoc foreground, Color JavaDoc background) {
152         throw new UnsupportedOperationException JavaDoc("The two barcodes must be composed externally.");
153     }
154 }
155
Popular Tags