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.BaselineAlignment; 65 import org.krysalis.barcode.ChecksumMode; 66 import org.krysalis.barcode.HumanReadablePlacement; 67 import org.krysalis.barcode.output.Canvas; 68 import org.krysalis.barcode.output.CanvasProvider; 69 import org.krysalis.barcode.tools.Length; 70 71 76 public class POSTNET extends HeightVariableBarcodeImpl 77 implements Configurable { 78 79 private ChecksumMode checksumMode = ChecksumMode.CP_AUTO; 80 81 private double intercharGapWidth = moduleWidth; 82 private BaselineAlignment baselinePosition = BaselineAlignment.ALIGN_BOTTOM; 83 private double shortBarHeight = 1.25f; 84 85 88 public void configure(Configuration cfg) throws ConfigurationException { 89 Length mw = new Length(cfg.getChild("module-width").getValue("0.020in"), "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 Length igw = new Length(cfg.getChild("interchar-gap-width").getValue("0.026in"), "mm"); 101 this.intercharGapWidth = igw.getValueAsMillimeter(); 102 103 Length h = new Length(cfg.getChild("tall-bar-height").getValue("0.125in"), "mm"); 104 this.setBarHeight(h.getValueAsMillimeter()); 105 106 Length hbh = new Length(cfg.getChild("short-bar-height").getValue("0.050in"), "mm"); 107 this.shortBarHeight = hbh.getValueAsMillimeter(); 108 109 this.msgPos = HumanReadablePlacement.byName( 111 cfg.getChild("human-readable").getValue(HumanReadablePlacement.HRP_NONE.getName())); 112 113 this.baselinePosition = BaselineAlignment.byName( 114 cfg.getChild("baseline-alignment").getValue(BaselineAlignment.ALIGN_BOTTOM.getName())); 115 } 116 117 121 public void setChecksumMode(ChecksumMode mode) { 122 this.checksumMode = mode; 123 } 124 125 129 public ChecksumMode getChecksumMode() { 130 return this.checksumMode; 131 } 132 133 136 public double getBarWidth(int width) { 137 if (width == 1) { 138 return moduleWidth; 139 } else if (width == -1) { 140 return this.intercharGapWidth; 141 } else throw new IllegalArgumentException ("Only width 1 allowed"); 142 } 143 144 147 public double getBarHeight(int height) { 148 if (height == 2) { 149 return getBarHeight(); 150 } else if (height == 1) { 151 return shortBarHeight; 152 } else if (height == -1) { 153 return getBarHeight(); } else throw new IllegalArgumentException ("Only height 0 or 1 allowed"); 155 } 156 157 160 public void generateBarcode(CanvasProvider canvas, String msg) { 161 if ((msg == null) 162 || (msg.length() == 0)) { 163 throw new NullPointerException ("Parameter msg must not be empty"); 164 } 165 166 DefaultHeightVariableLogicHandler handler = 167 new DefaultHeightVariableLogicHandler(this, new Canvas(canvas)); 168 169 POSTNETLogicImpl impl = new POSTNETLogicImpl(getChecksumMode()); 170 impl.generateBarcodeLogic(handler, msg); 171 } 172 173 176 public BarcodeDimension calcDimensions(String msg) { 177 String modMsg = POSTNETLogicImpl.removeIgnoredCharacters(msg); 178 final double width = (((modMsg.length() * 5) + 2) * moduleWidth) 179 + (((modMsg.length() * 5) + 1) * intercharGapWidth); 180 final double qz = (hasQuietZone() ? quietZone : 0); 181 double height = getHeight(); 182 if (getMsgPosition() == HumanReadablePlacement.HRP_NONE) { 183 height -= getHumanReadableHeight(); 184 } 185 return new BarcodeDimension(width, height, 186 width + (2 * qz), height, 187 quietZone, 0.0); 188 } 189 190 193 public BaselineAlignment getBaselinePosition() { 194 return baselinePosition; 195 } 196 197 200 public void setBaselinePosition(BaselineAlignment baselinePosition) { 201 this.baselinePosition = baselinePosition; 202 } 203 204 } | Popular Tags |