1 49 package com.lowagie.text.pdf; 50 51 import java.awt.Canvas ; 52 import java.awt.Color ; 53 import java.awt.Image ; 54 import java.awt.image.MemoryImageSource ; 55 56 import com.lowagie.text.Element; 57 import com.lowagie.text.ExceptionConverter; 58 import com.lowagie.text.Rectangle; 59 60 77 public class Barcode39 extends Barcode{ 78 79 81 private static final byte BARS[][] = 82 { 83 {0,0,0,1,1,0,1,0,0}, 84 {1,0,0,1,0,0,0,0,1}, 85 {0,0,1,1,0,0,0,0,1}, 86 {1,0,1,1,0,0,0,0,0}, 87 {0,0,0,1,1,0,0,0,1}, 88 {1,0,0,1,1,0,0,0,0}, 89 {0,0,1,1,1,0,0,0,0}, 90 {0,0,0,1,0,0,1,0,1}, 91 {1,0,0,1,0,0,1,0,0}, 92 {0,0,1,1,0,0,1,0,0}, 93 {1,0,0,0,0,1,0,0,1}, 94 {0,0,1,0,0,1,0,0,1}, 95 {1,0,1,0,0,1,0,0,0}, 96 {0,0,0,0,1,1,0,0,1}, 97 {1,0,0,0,1,1,0,0,0}, 98 {0,0,1,0,1,1,0,0,0}, 99 {0,0,0,0,0,1,1,0,1}, 100 {1,0,0,0,0,1,1,0,0}, 101 {0,0,1,0,0,1,1,0,0}, 102 {0,0,0,0,1,1,1,0,0}, 103 {1,0,0,0,0,0,0,1,1}, 104 {0,0,1,0,0,0,0,1,1}, 105 {1,0,1,0,0,0,0,1,0}, 106 {0,0,0,0,1,0,0,1,1}, 107 {1,0,0,0,1,0,0,1,0}, 108 {0,0,1,0,1,0,0,1,0}, 109 {0,0,0,0,0,0,1,1,1}, 110 {1,0,0,0,0,0,1,1,0}, 111 {0,0,1,0,0,0,1,1,0}, 112 {0,0,0,0,1,0,1,1,0}, 113 {1,1,0,0,0,0,0,0,1}, 114 {0,1,1,0,0,0,0,0,1}, 115 {1,1,1,0,0,0,0,0,0}, 116 {0,1,0,0,1,0,0,0,1}, 117 {1,1,0,0,1,0,0,0,0}, 118 {0,1,1,0,1,0,0,0,0}, 119 {0,1,0,0,0,0,1,0,1}, 120 {1,1,0,0,0,0,1,0,0}, 121 {0,1,1,0,0,0,1,0,0}, 122 {0,1,0,1,0,1,0,0,0}, 123 {0,1,0,1,0,0,0,1,0}, 124 {0,1,0,0,0,1,0,1,0}, 125 {0,0,0,1,0,1,0,1,0}, 126 {0,1,0,0,1,0,1,0,0} 127 }; 128 129 131 private static final String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; 132 133 135 private static final String EXTENDED = "%U" + 136 "$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" + 137 "%A%B%C%D%E /A/B/C/D/E/F/G/H/I/J/K/L - ./O" + 138 " 0 1 2 3 4 5 6 7 8 9/Z%F%G%H%I%J%V" + 139 " 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" + 140 "%K%L%M%N%O%W" + 141 "+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" + 142 "%P%Q%R%S%T"; 143 144 146 public Barcode39() { 147 try { 148 x = 0.8f; 149 n = 2; 150 font = BaseFont.createFont("Helvetica", "winansi", false); 151 size = 8; 152 baseline = size; 153 barHeight = size * 3; 154 textAlignment = Element.ALIGN_CENTER; 155 generateChecksum = false; 156 checksumText = false; 157 startStopText = true; 158 extended = false; 159 } 160 catch (Exception e) { 161 throw new ExceptionConverter(e); 162 } 163 } 164 165 170 public static byte[] getBarsCode39(String text) { 171 text = "*" + text + "*"; 172 byte bars[] = new byte[text.length() * 10 - 1]; 173 for (int k = 0; k < text.length(); ++k) { 174 int idx = CHARS.indexOf(text.charAt(k)); 175 if (idx < 0) 176 throw new IllegalArgumentException ("The character '" + text.charAt(k) + "' is illegal in code 39."); 177 System.arraycopy(BARS[idx], 0, bars, k * 10, 9); 178 } 179 return bars; 180 } 181 182 187 public static String getCode39Ex(String text) { 188 String out = ""; 189 for (int k = 0; k < text.length(); ++k) { 190 char c = text.charAt(k); 191 if (c > 127) 192 throw new IllegalArgumentException ("The character '" + c + "' is illegal in code 39 extended."); 193 char c1 = EXTENDED.charAt(c * 2); 194 char c2 = EXTENDED.charAt(c * 2 + 1); 195 if (c1 != ' ') 196 out += c1; 197 out += c2; 198 } 199 return out; 200 } 201 202 206 static char getChecksum(String text) { 207 int chk = 0; 208 for (int k = 0; k < text.length(); ++k) { 209 int idx = CHARS.indexOf(text.charAt(k)); 210 if (idx < 0) 211 throw new IllegalArgumentException ("The character '" + text.charAt(k) + "' is illegal in code 39."); 212 chk += idx; 213 } 214 return CHARS.charAt(chk % 43); 215 } 216 217 221 public Rectangle getBarcodeSize() { 222 float fontX = 0; 223 float fontY = 0; 224 String fCode = code; 225 if (extended) 226 fCode = getCode39Ex(code); 227 if (font != null) { 228 if (baseline > 0) 229 fontY = baseline - font.getFontDescriptor(BaseFont.DESCENT, size); 230 else 231 fontY = -baseline + size; 232 String fullCode = code; 233 if (generateChecksum && checksumText) 234 fullCode += getChecksum(fCode); 235 if (startStopText) 236 fullCode = "*" + fullCode + "*"; 237 fontX = font.getWidthPoint(altText != null ? altText : fullCode, size); 238 } 239 int len = fCode.length() + 2; 240 if (generateChecksum) 241 ++len; 242 float fullWidth = len * (6 * x + 3 * x * n) + (len - 1) * x; 243 fullWidth = Math.max(fullWidth, fontX); 244 float fullHeight = barHeight + fontY; 245 return new Rectangle(fullWidth, fullHeight); 246 } 247 248 284 public Rectangle placeBarcode(PdfContentByte cb, Color barColor, Color textColor) { 285 String fullCode = code; 286 float fontX = 0; 287 String bCode = code; 288 if (extended) 289 bCode = getCode39Ex(code); 290 if (font != null) { 291 if (generateChecksum && checksumText) 292 fullCode += getChecksum(bCode); 293 if (startStopText) 294 fullCode = "*" + fullCode + "*"; 295 fontX = font.getWidthPoint(fullCode = altText != null ? altText : fullCode, size); 296 } 297 if (generateChecksum) 298 bCode += getChecksum(bCode); 299 int len = bCode.length() + 2; 300 float fullWidth = len * (6 * x + 3 * x * n) + (len - 1) * x; 301 float barStartX = 0; 302 float textStartX = 0; 303 switch (textAlignment) { 304 case Element.ALIGN_LEFT: 305 break; 306 case Element.ALIGN_RIGHT: 307 if (fontX > fullWidth) 308 barStartX = fontX - fullWidth; 309 else 310 textStartX = fullWidth - fontX; 311 break; 312 default: 313 if (fontX > fullWidth) 314 barStartX = (fontX - fullWidth) / 2; 315 else 316 textStartX = (fullWidth - fontX) / 2; 317 break; 318 } 319 float barStartY = 0; 320 float textStartY = 0; 321 if (font != null) { 322 if (baseline <= 0) 323 textStartY = barHeight - baseline; 324 else { 325 textStartY = -font.getFontDescriptor(BaseFont.DESCENT, size); 326 barStartY = textStartY + baseline; 327 } 328 } 329 byte bars[] = getBarsCode39(bCode); 330 boolean print = true; 331 if (barColor != null) 332 cb.setColorFill(barColor); 333 for (int k = 0; k < bars.length; ++k) { 334 float w = (bars[k] == 0 ? x : x * n); 335 if (print) 336 cb.rectangle(barStartX, barStartY, w - inkSpreading, barHeight); 337 print = !print; 338 barStartX += w; 339 } 340 cb.fill(); 341 if (font != null) { 342 if (textColor != null) 343 cb.setColorFill(textColor); 344 cb.beginText(); 345 cb.setFontAndSize(font, size); 346 cb.setTextMatrix(textStartX, textStartY); 347 cb.showText(fullCode); 348 cb.endText(); 349 } 350 return getBarcodeSize(); 351 } 352 353 359 public java.awt.Image createAwtImage(Color foreground, Color background) { 360 int f = foreground.getRGB(); 361 int g = background.getRGB(); 362 Canvas canvas = new Canvas (); 363 364 String bCode = code; 365 if (extended) 366 bCode = getCode39Ex(code); 367 if (generateChecksum) 368 bCode += getChecksum(bCode); 369 int len = bCode.length() + 2; 370 int nn = (int)n; 371 int fullWidth = len * (6 + 3 * nn) + (len - 1); 372 byte bars[] = getBarsCode39(bCode); 373 boolean print = true; 374 int ptr = 0; 375 int height = (int)barHeight; 376 int pix[] = new int[fullWidth * height]; 377 for (int k = 0; k < bars.length; ++k) { 378 int w = (bars[k] == 0 ? 1 : nn); 379 int c = g; 380 if (print) 381 c = f; 382 print = !print; 383 for (int j = 0; j < w; ++j) 384 pix[ptr++] = c; 385 } 386 for (int k = fullWidth; k < pix.length; k += fullWidth) { 387 System.arraycopy(pix, 0, pix, k, fullWidth); 388 } 389 Image img = canvas.createImage(new MemoryImageSource (fullWidth, height, pix, 0, fullWidth)); 390 391 return img; 392 } 393 } 394 | Popular Tags |