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 76 public class BarcodeCodabar extends Barcode{ 77 78 80 private static final byte BARS[][] = 81 { 82 {0,0,0,0,0,1,1}, {0,0,0,0,1,1,0}, {0,0,0,1,0,0,1}, {1,1,0,0,0,0,0}, {0,0,1,0,0,1,0}, {1,0,0,0,0,1,0}, {0,1,0,0,0,0,1}, {0,1,0,0,1,0,0}, {0,1,1,0,0,0,0}, {1,0,0,1,0,0,0}, {0,0,0,1,1,0,0}, {0,0,1,1,0,0,0}, {1,0,0,0,1,0,1}, {1,0,1,0,0,0,1}, {1,0,1,0,1,0,0}, {0,0,1,0,1,0,1}, {0,0,1,1,0,1,0}, {0,1,0,1,0,0,1}, {0,0,0,1,0,1,1}, {0,0,0,1,1,1,0} }; 103 104 106 private static final String CHARS = "0123456789-$:/.+ABCD"; 107 108 private static final int START_STOP_IDX = 16; 109 111 public BarcodeCodabar() { 112 try { 113 x = 0.8f; 114 n = 2; 115 font = BaseFont.createFont("Helvetica", "winansi", false); 116 size = 8; 117 baseline = size; 118 barHeight = size * 3; 119 textAlignment = Element.ALIGN_CENTER; 120 generateChecksum = false; 121 checksumText = false; 122 startStopText = false; 123 codeType = CODABAR; 124 } 125 catch (Exception e) { 126 throw new ExceptionConverter(e); 127 } 128 } 129 130 134 public static byte[] getBarsCodabar(String text) { 135 text = text.toUpperCase(); 136 int len = text.length(); 137 if (len < 2) 138 throw new IllegalArgumentException ("Codabar must have at least a start and stop character."); 139 if (CHARS.indexOf(text.charAt(0)) < START_STOP_IDX || CHARS.indexOf(text.charAt(len - 1)) < START_STOP_IDX) 140 throw new IllegalArgumentException ("Codabar must have one of 'ABCD' as start/stop character."); 141 byte bars[] = new byte[text.length() * 8 - 1]; 142 for (int k = 0; k < len; ++k) { 143 int idx = CHARS.indexOf(text.charAt(k)); 144 if (idx >= START_STOP_IDX && k > 0 && k < len - 1) 145 throw new IllegalArgumentException ("In codabar, start/stop characters are only allowed at the extremes."); 146 if (idx < 0) 147 throw new IllegalArgumentException ("The character '" + text.charAt(k) + "' is illegal in codabar."); 148 System.arraycopy(BARS[idx], 0, bars, k * 8, 7); 149 } 150 return bars; 151 } 152 153 public static String calculateChecksum(String code) { 154 if (code.length() < 2) 155 return code; 156 String text = code.toUpperCase(); 157 int sum = 0; 158 int len = text.length(); 159 for (int k = 0; k < len; ++k) 160 sum += CHARS.indexOf(text.charAt(k)); 161 sum = (sum + 15) / 16 * 16 - sum; 162 return code.substring(0, len - 1) + CHARS.charAt(sum) + code.substring(len - 1); 163 } 164 165 169 public Rectangle getBarcodeSize() { 170 float fontX = 0; 171 float fontY = 0; 172 String text = code; 173 if (generateChecksum && checksumText) 174 text = calculateChecksum(code); 175 if (!startStopText) 176 text = text.substring(1, text.length() - 1); 177 if (font != null) { 178 if (baseline > 0) 179 fontY = baseline - font.getFontDescriptor(BaseFont.DESCENT, size); 180 else 181 fontY = -baseline + size; 182 fontX = font.getWidthPoint(altText != null ? altText : text, size); 183 } 184 text = code; 185 if (generateChecksum) 186 text = calculateChecksum(code); 187 byte bars[] = getBarsCodabar(text); 188 int wide = 0; 189 for (int k = 0; k < bars.length; ++k) { 190 wide += (int)bars[k]; 191 } 192 int narrow = bars.length - wide; 193 float fullWidth = x * (narrow + wide * n); 194 fullWidth = Math.max(fullWidth, fontX); 195 float fullHeight = barHeight + fontY; 196 return new Rectangle(fullWidth, fullHeight); 197 } 198 199 235 public Rectangle placeBarcode(PdfContentByte cb, Color barColor, Color textColor) { 236 String fullCode = code; 237 if (generateChecksum && checksumText) 238 fullCode = calculateChecksum(code); 239 if (!startStopText) 240 fullCode = fullCode.substring(1, fullCode.length() - 1); 241 float fontX = 0; 242 if (font != null) { 243 fontX = font.getWidthPoint(fullCode = altText != null ? altText : fullCode, size); 244 } 245 byte bars[] = getBarsCodabar(generateChecksum ? calculateChecksum(code) : code); 246 int wide = 0; 247 for (int k = 0; k < bars.length; ++k) { 248 wide += (int)bars[k]; 249 } 250 int narrow = bars.length - wide; 251 float fullWidth = x * (narrow + wide * n); 252 float barStartX = 0; 253 float textStartX = 0; 254 switch (textAlignment) { 255 case Element.ALIGN_LEFT: 256 break; 257 case Element.ALIGN_RIGHT: 258 if (fontX > fullWidth) 259 barStartX = fontX - fullWidth; 260 else 261 textStartX = fullWidth - fontX; 262 break; 263 default: 264 if (fontX > fullWidth) 265 barStartX = (fontX - fullWidth) / 2; 266 else 267 textStartX = (fullWidth - fontX) / 2; 268 break; 269 } 270 float barStartY = 0; 271 float textStartY = 0; 272 if (font != null) { 273 if (baseline <= 0) 274 textStartY = barHeight - baseline; 275 else { 276 textStartY = -font.getFontDescriptor(BaseFont.DESCENT, size); 277 barStartY = textStartY + baseline; 278 } 279 } 280 boolean print = true; 281 if (barColor != null) 282 cb.setColorFill(barColor); 283 for (int k = 0; k < bars.length; ++k) { 284 float w = (bars[k] == 0 ? x : x * n); 285 if (print) 286 cb.rectangle(barStartX, barStartY, w - inkSpreading, barHeight); 287 print = !print; 288 barStartX += w; 289 } 290 cb.fill(); 291 if (font != null) { 292 if (textColor != null) 293 cb.setColorFill(textColor); 294 cb.beginText(); 295 cb.setFontAndSize(font, size); 296 cb.setTextMatrix(textStartX, textStartY); 297 cb.showText(fullCode); 298 cb.endText(); 299 } 300 return getBarcodeSize(); 301 } 302 303 309 public java.awt.Image createAwtImage(Color foreground, Color background) { 310 int f = foreground.getRGB(); 311 int g = background.getRGB(); 312 Canvas canvas = new Canvas (); 313 314 String fullCode = code; 315 if (generateChecksum && checksumText) 316 fullCode = calculateChecksum(code); 317 if (!startStopText) 318 fullCode = fullCode.substring(1, fullCode.length() - 1); 319 byte bars[] = getBarsCodabar(generateChecksum ? calculateChecksum(code) : code); 320 int wide = 0; 321 for (int k = 0; k < bars.length; ++k) { 322 wide += (int)bars[k]; 323 } 324 int narrow = bars.length - wide; 325 int fullWidth = narrow + wide * (int)n; 326 boolean print = true; 327 int ptr = 0; 328 int height = (int)barHeight; 329 int pix[] = new int[fullWidth * height]; 330 for (int k = 0; k < bars.length; ++k) { 331 int w = (bars[k] == 0 ? 1 : (int)n); 332 int c = g; 333 if (print) 334 c = f; 335 print = !print; 336 for (int j = 0; j < w; ++j) 337 pix[ptr++] = c; 338 } 339 for (int k = fullWidth; k < pix.length; k += fullWidth) { 340 System.arraycopy(pix, 0, pix, k, fullWidth); 341 } 342 Image img = canvas.createImage(new MemoryImageSource (fullWidth, height, pix, 0, fullWidth)); 343 344 return img; 345 } 346 } 347 | Popular Tags |