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 abstract class UPCEAN extends GenericBarcodeImpl 76 implements Configurable { 77 78 private ChecksumMode checksumMode = ChecksumMode.CP_AUTO; 79 80 83 public void configure(Configuration cfg) throws ConfigurationException { 84 Length mw = new Length(cfg.getChild("module-width").getValue("0.33mm"), "mm"); 86 this.moduleWidth = mw.getValueAsMillimeter(); 87 88 super.configure(cfg); 89 90 this.checksumMode = ChecksumMode.byName( 92 cfg.getChild("checksum").getValue(ChecksumMode.CP_AUTO.getName())); 93 } 94 95 99 public void setChecksumMode(ChecksumMode mode) { 100 this.checksumMode = mode; 101 } 102 103 107 public ChecksumMode getChecksumMode() { 108 return this.checksumMode; 109 } 110 111 114 public double getBarWidth(int width) { 115 if ((width >= 1) && (width <= 4)) { 116 return width * moduleWidth; 117 } else { 118 throw new IllegalArgumentException ("Only widths 1 to 4 allowed"); 119 } 120 } 121 122 123 127 public abstract UPCEANLogicImpl createLogicImpl(); 128 129 130 133 public void generateBarcode(CanvasProvider canvas, String msg) { 134 if ((msg == null) || (msg.length() == 0)) { 135 throw new NullPointerException ("Parameter msg must not be empty"); 136 } 137 138 ClassicBarcodeLogicHandler handler = new UPCEANCanvasLogicHandler(this, new Canvas(canvas)); 139 141 UPCEANLogicImpl impl = createLogicImpl(); 142 impl.generateBarcodeLogic(handler, msg); 143 } 144 145 150 protected double supplementalWidth(String msg) { 151 double width = 0; 152 int suppLen = UPCEANLogicImpl.getSupplementalLength(msg); 153 if (suppLen > 0) { 154 width += quietZone; 156 width += 4 * moduleWidth; width += suppLen * 7 * moduleWidth; 158 width += (suppLen - 1) * 2 * moduleWidth; 159 } 160 return width; 161 } 162 163 166 public BarcodeDimension calcDimensions(String msg) { 167 double width = 3 * moduleWidth; width += 6 * 7 * moduleWidth; 169 width += 5 * moduleWidth; width += 6 * 7 * moduleWidth; 171 width += 3 * moduleWidth; width += supplementalWidth(msg); 173 final double qz = (hasQuietZone() ? quietZone : 0); 174 return new BarcodeDimension(width, getHeight(), 175 width + (2 * qz), getHeight(), 176 quietZone, 0.0); 177 } 178 179 180 } | Popular Tags |