1 58 package org.krysalis.barcode.impl; 59 60 import org.apache.avalon.framework.configuration.Configurable; 61 import org.apache.avalon.framework.configuration.Configuration; 62 import org.apache.avalon.framework.configuration.ConfigurationException; 63 import org.krysalis.barcode.BarcodeDimension; 64 import org.krysalis.barcode.ChecksumMode; 65 import org.krysalis.barcode.ClassicBarcodeLogicHandler; 66 import org.krysalis.barcode.output.Canvas; 67 import org.krysalis.barcode.output.CanvasProvider; 68 import org.krysalis.barcode.tools.Length; 69 70 75 public class Codabar extends GenericBarcodeImpl 76 implements Configurable { 77 78 79 protected static final double DEFAULT_WIDE_FACTOR = 3.0; 80 81 private ChecksumMode checksumMode = ChecksumMode.CP_AUTO; 82 private double wideFactor = DEFAULT_WIDE_FACTOR; 84 85 88 public void configure(Configuration cfg) throws ConfigurationException { 89 Length mw = new Length(cfg.getChild("module-width").getValue("0.21mm"), "mm"); 91 this.moduleWidth = mw.getValueAsMillimeter(); 92 93 super.configure(cfg); 94 95 this.checksumMode = ChecksumMode.byName( 97 cfg.getChild("checksum").getValue(ChecksumMode.CP_AUTO.getName())); 98 99 this.wideFactor = cfg.getChild("wide-factor").getValueAsFloat((float)DEFAULT_WIDE_FACTOR); 101 } 102 103 107 public void setChecksumMode(ChecksumMode mode) { 108 this.checksumMode = mode; 109 } 110 111 115 public ChecksumMode getChecksumMode() { 116 return this.checksumMode; 117 } 118 119 122 public double getBarWidth(int width) { 123 if (width == 1) { 124 return moduleWidth; 125 } else if (width == 2) { 126 return moduleWidth * wideFactor; 127 } else throw new IllegalArgumentException ("Only widths 1 and 2 allowed"); 128 } 129 130 133 public void generateBarcode(CanvasProvider canvas, String msg) { 134 if ((msg == null) 135 || (msg.length() == 0)) { 136 throw new NullPointerException ("Parameter msg must not be empty"); 137 } 138 139 ClassicBarcodeLogicHandler handler = 140 new DefaultCanvasLogicHandler(this, new Canvas(canvas)); 141 142 CodabarLogicImpl impl = new CodabarLogicImpl(getChecksumMode()); 143 impl.generateBarcodeLogic(handler, msg); 144 } 145 146 private double calcCharWidth(char c) { 147 final int idx = CodabarLogicImpl.getCharIndex(c); 148 if (idx >= 0) { 149 int narrow = 0; 150 int wide = 0; 151 for (int i = 0; i < 7; i++) { 152 final byte width = CodabarLogicImpl.CHARSET[idx][i]; 153 if (width == 0) { 154 narrow++; 155 } else { 156 wide++; 157 } 158 } 159 return (narrow * moduleWidth) + (wide * moduleWidth * wideFactor); 160 } else { 161 throw new IllegalArgumentException ("Invalid character: " + c); 162 } 163 } 164 165 168 public BarcodeDimension calcDimensions(String msg) { 169 double width = 0.0; 170 for (int i = 0; i < msg.length(); i++) { 171 if (i > 0) { 172 width += moduleWidth; } 174 width += calcCharWidth(msg.charAt(i)); 175 } 176 final double qz = (hasQuietZone() ? quietZone : 0); 177 return new BarcodeDimension(width, getHeight(), 178 width + (2 * qz), getHeight(), 179 quietZone, 0.0); 180 } 181 182 183 } | Popular Tags |