001/* 002 * $Id: BarcodeEANSUPP.java 4784 2011-03-15 08:33:00Z blowagie $ 003 * 004 * This file is part of the iText (R) project. 005 * Copyright (c) 1998-2011 1T3XT BVBA 006 * Authors: Bruno Lowagie, Paulo Soares, et al. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Affero General Public License version 3 010 * as published by the Free Software Foundation with the addition of the 011 * following permission added to Section 15 as permitted in Section 7(a): 012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT, 013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 014 * 015 * This program is distributed in the hope that it will be useful, but 016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 017 * or FITNESS FOR A PARTICULAR PURPOSE. 018 * See the GNU Affero General Public License for more details. 019 * You should have received a copy of the GNU Affero General Public License 020 * along with this program; if not, see http://www.gnu.org/licenses or write to 021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 022 * Boston, MA, 02110-1301 USA, or download the license from the following URL: 023 * http://itextpdf.com/terms-of-use/ 024 * 025 * The interactive user interfaces in modified source and object code versions 026 * of this program must display Appropriate Legal Notices, as required under 027 * Section 5 of the GNU Affero General Public License. 028 * 029 * In accordance with Section 7(b) of the GNU Affero General Public License, 030 * a covered work must retain the producer line in every PDF that is created 031 * or manipulated using iText. 032 * 033 * You can be released from the requirements of the license by purchasing 034 * a commercial license. Buying such a license is mandatory as soon as you 035 * develop commercial activities involving the iText software without 036 * disclosing the source code of your own applications. 037 * These activities include: offering paid services to customers as an ASP, 038 * serving PDFs on the fly in a web application, shipping iText with a closed 039 * source product. 040 * 041 * For more information, please contact iText Software Corp. at this 042 * address: sales@itextpdf.com 043 */ 044package com.itextpdf.text.pdf; 045import com.itextpdf.text.error_messages.MessageLocalization; 046 047import com.itextpdf.text.Rectangle; 048import com.itextpdf.text.BaseColor; 049 050/** This class takes 2 barcodes, an EAN/UPC and a supplemental 051 * and creates a single barcode with both combined in the 052 * expected layout. The UPC/EAN should have a positive text 053 * baseline and the supplemental a negative one (in the supplemental 054 * the text is on the top of the barcode.<p> 055 * The default parameters are: 056 * <pre> 057 *n = 8; // horizontal distance between the two barcodes 058 * </pre> 059 * 060 * @author Paulo Soares 061 */ 062public class BarcodeEANSUPP extends Barcode{ 063 064 /** The barcode with the EAN/UPC. 065 */ 066 protected Barcode ean; 067 /** The barcode with the supplemental. 068 */ 069 protected Barcode supp; 070 071 /** Creates new combined barcode. 072 * @param ean the EAN/UPC barcode 073 * @param supp the supplemental barcode 074 */ 075 public BarcodeEANSUPP(Barcode ean, Barcode supp) { 076 n = 8; // horizontal distance between the two barcodes 077 this.ean = ean; 078 this.supp = supp; 079 } 080 081 /** Gets the maximum area that the barcode and the text, if 082 * any, will occupy. The lower left corner is always (0, 0). 083 * @return the size the barcode occupies. 084 */ 085 public Rectangle getBarcodeSize() { 086 Rectangle rect = ean.getBarcodeSize(); 087 rect.setRight(rect.getWidth() + supp.getBarcodeSize().getWidth() + n); 088 return rect; 089 } 090 091 /** Places the barcode in a <CODE>PdfContentByte</CODE>. The 092 * barcode is always placed at coordinates (0, 0). Use the 093 * translation matrix to move it elsewhere.<p> 094 * The bars and text are written in the following colors:<p> 095 * <P><TABLE BORDER=1> 096 * <TR> 097 * <TH><P><CODE>barColor</CODE></TH> 098 * <TH><P><CODE>textColor</CODE></TH> 099 * <TH><P>Result</TH> 100 * </TR> 101 * <TR> 102 * <TD><P><CODE>null</CODE></TD> 103 * <TD><P><CODE>null</CODE></TD> 104 * <TD><P>bars and text painted with current fill color</TD> 105 * </TR> 106 * <TR> 107 * <TD><P><CODE>barColor</CODE></TD> 108 * <TD><P><CODE>null</CODE></TD> 109 * <TD><P>bars and text painted with <CODE>barColor</CODE></TD> 110 * </TR> 111 * <TR> 112 * <TD><P><CODE>null</CODE></TD> 113 * <TD><P><CODE>textColor</CODE></TD> 114 * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD> 115 * </TR> 116 * <TR> 117 * <TD><P><CODE>barColor</CODE></TD> 118 * <TD><P><CODE>textColor</CODE></TD> 119 * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD> 120 * </TR> 121 * </TABLE> 122 * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed 123 * @param barColor the color of the bars. It can be <CODE>null</CODE> 124 * @param textColor the color of the text. It can be <CODE>null</CODE> 125 * @return the dimensions the barcode occupies 126 */ 127 public Rectangle placeBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor) { 128 if (supp.getFont() != null) 129 supp.setBarHeight(ean.getBarHeight() + supp.getBaseline() - supp.getFont().getFontDescriptor(BaseFont.CAPHEIGHT, supp.getSize())); 130 else 131 supp.setBarHeight(ean.getBarHeight()); 132 Rectangle eanR = ean.getBarcodeSize(); 133 cb.saveState(); 134 ean.placeBarcode(cb, barColor, textColor); 135 cb.restoreState(); 136 cb.saveState(); 137 cb.concatCTM(1, 0, 0, 1, eanR.getWidth() + n, eanR.getHeight() - ean.getBarHeight()); 138 supp.placeBarcode(cb, barColor, textColor); 139 cb.restoreState(); 140 return getBarcodeSize(); 141 } 142 143 /** Creates a <CODE>java.awt.Image</CODE>. This image only 144 * contains the bars without any text. 145 * @param foreground the color of the bars 146 * @param background the color of the background 147 * @return the image 148 */ 149 public java.awt.Image createAwtImage(java.awt.Color foreground, java.awt.Color background) { 150 throw new UnsupportedOperationException(MessageLocalization.getComposedMessage("the.two.barcodes.must.be.composed.externally")); 151 } 152}