001/* 002 * $Id: Barcode39.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; 045 046import java.awt.Canvas; 047import java.awt.Image; 048import java.awt.image.MemoryImageSource; 049import com.itextpdf.text.error_messages.MessageLocalization; 050 051import com.itextpdf.text.Element; 052import com.itextpdf.text.ExceptionConverter; 053import com.itextpdf.text.Rectangle; 054import com.itextpdf.text.BaseColor; 055 056/** Implements the code 39 and code 39 extended. The default parameters are: 057 * <pre> 058 *x = 0.8f; 059 *n = 2; 060 *font = BaseFont.createFont("Helvetica", "winansi", false); 061 *size = 8; 062 *baseline = size; 063 *barHeight = size * 3; 064 *textAlignment = Element.ALIGN_CENTER; 065 *generateChecksum = false; 066 *checksumText = false; 067 *startStopText = true; 068 *extended = false; 069 * </pre> 070 * 071 * @author Paulo Soares 072 */ 073public class Barcode39 extends Barcode{ 074 075 /** The bars to generate the code. 076 */ 077 private static final byte BARS[][] = 078 { 079 {0,0,0,1,1,0,1,0,0}, 080 {1,0,0,1,0,0,0,0,1}, 081 {0,0,1,1,0,0,0,0,1}, 082 {1,0,1,1,0,0,0,0,0}, 083 {0,0,0,1,1,0,0,0,1}, 084 {1,0,0,1,1,0,0,0,0}, 085 {0,0,1,1,1,0,0,0,0}, 086 {0,0,0,1,0,0,1,0,1}, 087 {1,0,0,1,0,0,1,0,0}, 088 {0,0,1,1,0,0,1,0,0}, 089 {1,0,0,0,0,1,0,0,1}, 090 {0,0,1,0,0,1,0,0,1}, 091 {1,0,1,0,0,1,0,0,0}, 092 {0,0,0,0,1,1,0,0,1}, 093 {1,0,0,0,1,1,0,0,0}, 094 {0,0,1,0,1,1,0,0,0}, 095 {0,0,0,0,0,1,1,0,1}, 096 {1,0,0,0,0,1,1,0,0}, 097 {0,0,1,0,0,1,1,0,0}, 098 {0,0,0,0,1,1,1,0,0}, 099 {1,0,0,0,0,0,0,1,1}, 100 {0,0,1,0,0,0,0,1,1}, 101 {1,0,1,0,0,0,0,1,0}, 102 {0,0,0,0,1,0,0,1,1}, 103 {1,0,0,0,1,0,0,1,0}, 104 {0,0,1,0,1,0,0,1,0}, 105 {0,0,0,0,0,0,1,1,1}, 106 {1,0,0,0,0,0,1,1,0}, 107 {0,0,1,0,0,0,1,1,0}, 108 {0,0,0,0,1,0,1,1,0}, 109 {1,1,0,0,0,0,0,0,1}, 110 {0,1,1,0,0,0,0,0,1}, 111 {1,1,1,0,0,0,0,0,0}, 112 {0,1,0,0,1,0,0,0,1}, 113 {1,1,0,0,1,0,0,0,0}, 114 {0,1,1,0,1,0,0,0,0}, 115 {0,1,0,0,0,0,1,0,1}, 116 {1,1,0,0,0,0,1,0,0}, 117 {0,1,1,0,0,0,1,0,0}, 118 {0,1,0,1,0,1,0,0,0}, 119 {0,1,0,1,0,0,0,1,0}, 120 {0,1,0,0,0,1,0,1,0}, 121 {0,0,0,1,0,1,0,1,0}, 122 {0,1,0,0,1,0,1,0,0} 123 }; 124 125 /** The index chars to <CODE>BARS</CODE>. 126 */ 127 private static final String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; 128 129 /** The character combinations to make the code 39 extended. 130 */ 131 private static final String EXTENDED = "%U" + 132 "$A$B$C$D$E$F$G$H$I$J$K$L$M$N$O$P$Q$R$S$T$U$V$W$X$Y$Z" + 133 "%A%B%C%D%E /A/B/C/D/E/F/G/H/I/J/K/L - ./O" + 134 " 0 1 2 3 4 5 6 7 8 9/Z%F%G%H%I%J%V" + 135 " A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" + 136 "%K%L%M%N%O%W" + 137 "+A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z" + 138 "%P%Q%R%S%T"; 139 140 /** Creates a new Barcode39. 141 */ 142 public Barcode39() { 143 try { 144 x = 0.8f; 145 n = 2; 146 font = BaseFont.createFont("Helvetica", "winansi", false); 147 size = 8; 148 baseline = size; 149 barHeight = size * 3; 150 textAlignment = Element.ALIGN_CENTER; 151 generateChecksum = false; 152 checksumText = false; 153 startStopText = true; 154 extended = false; 155 } 156 catch (Exception e) { 157 throw new ExceptionConverter(e); 158 } 159 } 160 161 /** Creates the bars. 162 * @param text the text to create the bars. This text does not include the start and 163 * stop characters 164 * @return the bars 165 */ 166 public static byte[] getBarsCode39(String text) { 167 text = "*" + text + "*"; 168 byte bars[] = new byte[text.length() * 10 - 1]; 169 for (int k = 0; k < text.length(); ++k) { 170 int idx = CHARS.indexOf(text.charAt(k)); 171 if (idx < 0) 172 throw new IllegalArgumentException(MessageLocalization.getComposedMessage("the.character.1.is.illegal.in.code.39", text.charAt(k))); 173 System.arraycopy(BARS[idx], 0, bars, k * 10, 9); 174 } 175 return bars; 176 } 177 178 /** Converts the extended text into a normal, escaped text, 179 * ready to generate bars. 180 * @param text the extended text 181 * @return the escaped text 182 */ 183 public static String getCode39Ex(String text) { 184 StringBuilder out = new StringBuilder(""); 185 for (int k = 0; k < text.length(); ++k) { 186 char c = text.charAt(k); 187 if (c > 127) 188 throw new IllegalArgumentException(MessageLocalization.getComposedMessage("the.character.1.is.illegal.in.code.39.extended", c)); 189 char c1 = EXTENDED.charAt(c * 2); 190 char c2 = EXTENDED.charAt(c * 2 + 1); 191 if (c1 != ' ') 192 out.append(c1); 193 out.append(c2); 194 } 195 return out.toString(); 196 } 197 198 /** Calculates the checksum. 199 * @param text the text 200 * @return the checksum 201 */ 202 static char getChecksum(String text) { 203 int chk = 0; 204 for (int k = 0; k < text.length(); ++k) { 205 int idx = CHARS.indexOf(text.charAt(k)); 206 if (idx < 0) 207 throw new IllegalArgumentException(MessageLocalization.getComposedMessage("the.character.1.is.illegal.in.code.39", text.charAt(k))); 208 chk += idx; 209 } 210 return CHARS.charAt(chk % 43); 211 } 212 213 /** Gets the maximum area that the barcode and the text, if 214 * any, will occupy. The lower left corner is always (0, 0). 215 * @return the size the barcode occupies. 216 */ 217 public Rectangle getBarcodeSize() { 218 float fontX = 0; 219 float fontY = 0; 220 String fCode = code; 221 if (extended) 222 fCode = getCode39Ex(code); 223 if (font != null) { 224 if (baseline > 0) 225 fontY = baseline - font.getFontDescriptor(BaseFont.DESCENT, size); 226 else 227 fontY = -baseline + size; 228 String fullCode = code; 229 if (generateChecksum && checksumText) 230 fullCode += getChecksum(fCode); 231 if (startStopText) 232 fullCode = "*" + fullCode + "*"; 233 fontX = font.getWidthPoint(altText != null ? altText : fullCode, size); 234 } 235 int len = fCode.length() + 2; 236 if (generateChecksum) 237 ++len; 238 float fullWidth = len * (6 * x + 3 * x * n) + (len - 1) * x; 239 fullWidth = Math.max(fullWidth, fontX); 240 float fullHeight = barHeight + fontY; 241 return new Rectangle(fullWidth, fullHeight); 242 } 243 244 /** Places the barcode in a <CODE>PdfContentByte</CODE>. The 245 * barcode is always placed at coordinates (0, 0). Use the 246 * translation matrix to move it elsewhere.<p> 247 * The bars and text are written in the following colors:<p> 248 * <P><TABLE BORDER=1> 249 * <TR> 250 * <TH><P><CODE>barColor</CODE></TH> 251 * <TH><P><CODE>textColor</CODE></TH> 252 * <TH><P>Result</TH> 253 * </TR> 254 * <TR> 255 * <TD><P><CODE>null</CODE></TD> 256 * <TD><P><CODE>null</CODE></TD> 257 * <TD><P>bars and text painted with current fill color</TD> 258 * </TR> 259 * <TR> 260 * <TD><P><CODE>barColor</CODE></TD> 261 * <TD><P><CODE>null</CODE></TD> 262 * <TD><P>bars and text painted with <CODE>barColor</CODE></TD> 263 * </TR> 264 * <TR> 265 * <TD><P><CODE>null</CODE></TD> 266 * <TD><P><CODE>textColor</CODE></TD> 267 * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD> 268 * </TR> 269 * <TR> 270 * <TD><P><CODE>barColor</CODE></TD> 271 * <TD><P><CODE>textColor</CODE></TD> 272 * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD> 273 * </TR> 274 * </TABLE> 275 * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed 276 * @param barColor the color of the bars. It can be <CODE>null</CODE> 277 * @param textColor the color of the text. It can be <CODE>null</CODE> 278 * @return the dimensions the barcode occupies 279 */ 280 public Rectangle placeBarcode(PdfContentByte cb, BaseColor barColor, BaseColor textColor) { 281 String fullCode = code; 282 float fontX = 0; 283 String bCode = code; 284 if (extended) 285 bCode = getCode39Ex(code); 286 if (font != null) { 287 if (generateChecksum && checksumText) 288 fullCode += getChecksum(bCode); 289 if (startStopText) 290 fullCode = "*" + fullCode + "*"; 291 fontX = font.getWidthPoint(fullCode = altText != null ? altText : fullCode, size); 292 } 293 if (generateChecksum) 294 bCode += getChecksum(bCode); 295 int len = bCode.length() + 2; 296 float fullWidth = len * (6 * x + 3 * x * n) + (len - 1) * x; 297 float barStartX = 0; 298 float textStartX = 0; 299 switch (textAlignment) { 300 case Element.ALIGN_LEFT: 301 break; 302 case Element.ALIGN_RIGHT: 303 if (fontX > fullWidth) 304 barStartX = fontX - fullWidth; 305 else 306 textStartX = fullWidth - fontX; 307 break; 308 default: 309 if (fontX > fullWidth) 310 barStartX = (fontX - fullWidth) / 2; 311 else 312 textStartX = (fullWidth - fontX) / 2; 313 break; 314 } 315 float barStartY = 0; 316 float textStartY = 0; 317 if (font != null) { 318 if (baseline <= 0) 319 textStartY = barHeight - baseline; 320 else { 321 textStartY = -font.getFontDescriptor(BaseFont.DESCENT, size); 322 barStartY = textStartY + baseline; 323 } 324 } 325 byte bars[] = getBarsCode39(bCode); 326 boolean print = true; 327 if (barColor != null) 328 cb.setColorFill(barColor); 329 for (int k = 0; k < bars.length; ++k) { 330 float w = (bars[k] == 0 ? x : x * n); 331 if (print) 332 cb.rectangle(barStartX, barStartY, w - inkSpreading, barHeight); 333 print = !print; 334 barStartX += w; 335 } 336 cb.fill(); 337 if (font != null) { 338 if (textColor != null) 339 cb.setColorFill(textColor); 340 cb.beginText(); 341 cb.setFontAndSize(font, size); 342 cb.setTextMatrix(textStartX, textStartY); 343 cb.showText(fullCode); 344 cb.endText(); 345 } 346 return getBarcodeSize(); 347 } 348 349 /** Creates a <CODE>java.awt.Image</CODE>. This image only 350 * contains the bars without any text. 351 * @param foreground the color of the bars 352 * @param background the color of the background 353 * @return the image 354 */ 355 public java.awt.Image createAwtImage(java.awt.Color foreground, java.awt.Color background) { 356 int f = foreground.getRGB(); 357 int g = background.getRGB(); 358 Canvas canvas = new Canvas(); 359 360 String bCode = code; 361 if (extended) 362 bCode = getCode39Ex(code); 363 if (generateChecksum) 364 bCode += getChecksum(bCode); 365 int len = bCode.length() + 2; 366 int nn = (int)n; 367 int fullWidth = len * (6 + 3 * nn) + (len - 1); 368 byte bars[] = getBarsCode39(bCode); 369 boolean print = true; 370 int ptr = 0; 371 int height = (int)barHeight; 372 int pix[] = new int[fullWidth * height]; 373 for (int k = 0; k < bars.length; ++k) { 374 int w = (bars[k] == 0 ? 1 : nn); 375 int c = g; 376 if (print) 377 c = f; 378 print = !print; 379 for (int j = 0; j < w; ++j) 380 pix[ptr++] = c; 381 } 382 for (int k = fullWidth; k < pix.length; k += fullWidth) { 383 System.arraycopy(pix, 0, pix, k, fullWidth); 384 } 385 Image img = canvas.createImage(new MemoryImageSource(fullWidth, height, pix, 0, fullWidth)); 386 387 return img; 388 } 389}