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 BarcodeInter25 extends Barcode{ 78 79 81 private static final byte BARS[][] = 82 { 83 {0,0,1,1,0}, 84 {1,0,0,0,1}, 85 {0,1,0,0,1}, 86 {1,1,0,0,0}, 87 {0,0,1,0,1}, 88 {1,0,1,0,0}, 89 {0,1,1,0,0}, 90 {0,0,0,1,1}, 91 {1,0,0,1,0}, 92 {0,1,0,1,0} 93 }; 94 95 96 public BarcodeInter25() { 97 try { 98 x = 0.8f; 99 n = 2; 100 font = BaseFont.createFont("Helvetica", "winansi", false); 101 size = 8; 102 baseline = size; 103 barHeight = size * 3; 104 textAlignment = Element.ALIGN_CENTER; 105 generateChecksum = false; 106 checksumText = false; 107 } 108 catch (Exception e) { 109 throw new ExceptionConverter(e); 110 } 111 } 112 113 117 public static String keepNumbers(String text) { 118 StringBuffer sb = new StringBuffer (); 119 for (int k = 0; k < text.length(); ++k) { 120 char c = text.charAt(k); 121 if (c >= '0' && c <= '9') 122 sb.append(c); 123 } 124 return sb.toString(); 125 } 126 127 131 public static char getChecksum(String text) { 132 int mul = 3; 133 int total = 0; 134 for (int k = text.length() - 1; k >= 0; --k) { 135 int n = text.charAt(k) - '0'; 136 total += mul * n; 137 mul ^= 2; 138 } 139 return (char)(((10 - (total % 10)) % 10) + '0'); 140 } 141 142 146 public static byte[] getBarsInter25(String text) { 147 text = keepNumbers(text); 148 if ((text.length() & 1) != 0) 149 throw new IllegalArgumentException ("The text length must be even."); 150 byte bars[] = new byte[text.length() * 5 + 7]; 151 int pb = 0; 152 bars[pb++] = 0; 153 bars[pb++] = 0; 154 bars[pb++] = 0; 155 bars[pb++] = 0; 156 int len = text.length() / 2; 157 for (int k = 0; k < len; ++k) { 158 int c1 = text.charAt(k * 2) - '0'; 159 int c2 = text.charAt(k * 2 + 1) - '0'; 160 byte b1[] = BARS[c1]; 161 byte b2[] = BARS[c2]; 162 for (int j = 0; j < 5; ++j) { 163 bars[pb++] = b1[j]; 164 bars[pb++] = b2[j]; 165 } 166 } 167 bars[pb++] = 1; 168 bars[pb++] = 0; 169 bars[pb++] = 0; 170 return bars; 171 } 172 173 177 public Rectangle getBarcodeSize() { 178 float fontX = 0; 179 float fontY = 0; 180 if (font != null) { 181 if (baseline > 0) 182 fontY = baseline - font.getFontDescriptor(BaseFont.DESCENT, size); 183 else 184 fontY = -baseline + size; 185 String fullCode = code; 186 if (generateChecksum && checksumText) 187 fullCode += getChecksum(fullCode); 188 fontX = font.getWidthPoint(altText != null ? altText : fullCode, size); 189 } 190 String fullCode = keepNumbers(code); 191 int len = fullCode.length(); 192 if (generateChecksum) 193 ++len; 194 float fullWidth = len * (3 * x + 2 * x * n) + (6 + n ) * x; 195 fullWidth = Math.max(fullWidth, fontX); 196 float fullHeight = barHeight + fontY; 197 return new Rectangle(fullWidth, fullHeight); 198 } 199 200 236 public Rectangle placeBarcode(PdfContentByte cb, Color barColor, Color textColor) { 237 String fullCode = code; 238 float fontX = 0; 239 if (font != null) { 240 if (generateChecksum && checksumText) 241 fullCode += getChecksum(fullCode); 242 fontX = font.getWidthPoint(fullCode = altText != null ? altText : fullCode, size); 243 } 244 String bCode = keepNumbers(code); 245 if (generateChecksum) 246 bCode += getChecksum(bCode); 247 int len = bCode.length(); 248 float fullWidth = len * (3 * x + 2 * x * n) + (6 + n ) * x; 249 float barStartX = 0; 250 float textStartX = 0; 251 switch (textAlignment) { 252 case Element.ALIGN_LEFT: 253 break; 254 case Element.ALIGN_RIGHT: 255 if (fontX > fullWidth) 256 barStartX = fontX - fullWidth; 257 else 258 textStartX = fullWidth - fontX; 259 break; 260 default: 261 if (fontX > fullWidth) 262 barStartX = (fontX - fullWidth) / 2; 263 else 264 textStartX = (fullWidth - fontX) / 2; 265 break; 266 } 267 float barStartY = 0; 268 float textStartY = 0; 269 if (font != null) { 270 if (baseline <= 0) 271 textStartY = barHeight - baseline; 272 else { 273 textStartY = -font.getFontDescriptor(BaseFont.DESCENT, size); 274 barStartY = textStartY + baseline; 275 } 276 } 277 byte bars[] = getBarsInter25(bCode); 278 boolean print = true; 279 if (barColor != null) 280 cb.setColorFill(barColor); 281 for (int k = 0; k < bars.length; ++k) { 282 float w = (bars[k] == 0 ? x : x * n); 283 if (print) 284 cb.rectangle(barStartX, barStartY, w - inkSpreading, barHeight); 285 print = !print; 286 barStartX += w; 287 } 288 cb.fill(); 289 if (font != null) { 290 if (textColor != null) 291 cb.setColorFill(textColor); 292 cb.beginText(); 293 cb.setFontAndSize(font, size); 294 cb.setTextMatrix(textStartX, textStartY); 295 cb.showText(fullCode); 296 cb.endText(); 297 } 298 return getBarcodeSize(); 299 } 300 301 307 public java.awt.Image createAwtImage(Color foreground, Color background) { 308 int f = foreground.getRGB(); 309 int g = background.getRGB(); 310 Canvas canvas = new Canvas (); 311 312 String bCode = keepNumbers(code); 313 if (generateChecksum) 314 bCode += getChecksum(bCode); 315 int len = bCode.length(); 316 int nn = (int)n; 317 int fullWidth = len * (3 + 2 * nn) + (6 + nn ); 318 byte bars[] = getBarsInter25(bCode); 319 boolean print = true; 320 int ptr = 0; 321 int height = (int)barHeight; 322 int pix[] = new int[fullWidth * height]; 323 for (int k = 0; k < bars.length; ++k) { 324 int w = (bars[k] == 0 ? 1 : nn); 325 int c = g; 326 if (print) 327 c = f; 328 print = !print; 329 for (int j = 0; j < w; ++j) 330 pix[ptr++] = c; 331 } 332 for (int k = fullWidth; k < pix.length; k += fullWidth) { 333 System.arraycopy(pix, 0, pix, k, fullWidth); 334 } 335 Image img = canvas.createImage(new MemoryImageSource (fullWidth, height, pix, 0, fullWidth)); 336 337 return img; 338 } 339 } | Popular Tags |