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.BarcodeGenerator; 65 import org.krysalis.barcode.HumanReadablePlacement; 66 import org.krysalis.barcode.output.Canvas; 67 import org.krysalis.barcode.output.CanvasProvider; 68 import org.krysalis.barcode.tools.Length; 69 import org.krysalis.barcode.tools.UnitConv; 70 71 76 public abstract class GenericBarcodeImpl 77 implements BarcodeGenerator, Configurable { 78 79 80 protected double height = 15.0; 82 protected double moduleWidth; 83 84 protected HumanReadablePlacement msgPos = HumanReadablePlacement.HRP_BOTTOM; 85 86 protected double fontSize = 8; 88 protected String fontName = "Helvetica"; 90 protected boolean doQuietZone = true; 91 92 protected double quietZone; 93 94 97 public void configure(Configuration cfg) throws ConfigurationException { 98 Length h = new Length(cfg.getChild("height").getValue("15mm"), "mm"); 100 this.setHeight(h.getValueAsMillimeter()); 101 102 this.doQuietZone = cfg.getChild("quiet-zone").getAttributeAsBoolean("enabled", true); 104 Length qz = new Length(cfg.getChild("quiet-zone").getValue("10mw"), "mw"); 105 if (qz.getUnit().equalsIgnoreCase("mw")) { 106 this.quietZone = qz.getValue() * getModuleWidth(); 107 } else { 108 this.quietZone = qz.getValueAsMillimeter(); 109 } 110 111 this.msgPos = HumanReadablePlacement.byName( 113 cfg.getChild("human-readable").getValue(HumanReadablePlacement.HRP_BOTTOM.getName())); 114 } 115 116 120 public double getHumanReadableHeight() { 121 double textHeight = UnitConv.pt2mm(this.fontSize); 122 return 1.0 * textHeight; 123 } 124 125 129 public double getBarHeight() { 130 return this.height; 131 } 132 133 137 public double getHeight() { 138 return getBarHeight() + getHumanReadableHeight(); 139 } 140 141 145 public void setBarHeight(double height) { 146 this.height = height; 147 } 148 149 153 public void setHeight(double height) { 154 this.height = height - getHumanReadableHeight(); 155 } 156 157 161 public double getModuleWidth() { 162 return this.moduleWidth; 163 } 164 165 169 public void setModuleWidth(double width) { 170 this.moduleWidth = width; 171 } 172 173 178 public abstract double getBarWidth(int width); 179 180 184 public boolean hasQuietZone() { 185 return this.doQuietZone; 186 } 187 188 192 public void doQuietZone(boolean value) { 193 this.doQuietZone = value; 194 } 195 196 200 public double getQuietZone() { 201 return this.quietZone; 202 } 203 204 208 public HumanReadablePlacement getMsgPosition() { 209 return this.msgPos; 210 } 211 212 216 public void setMsgPosition(HumanReadablePlacement placement) { 217 this.msgPos = placement; 218 } 219 220 228 protected void drawCenteredChar(Canvas canvas, char ch, 229 double x1, double x2, double y1) { 230 canvas.drawCenteredChar(ch, x1, x2, y1 - UnitConv.pt2mm(fontSize) * 0.2, 231 fontName, fontSize); 232 } 233 234 242 protected void drawJustifiedText(Canvas canvas, String text, 243 double x1, double x2, double y1) { 244 canvas.drawJustifiedText(text, x1, x2, y1 - UnitConv.pt2mm(fontSize) * 0.2, 245 fontName, fontSize); 246 } 247 248 256 protected void drawCenteredText(Canvas canvas, String text, 257 double x1, double x2, double y1) { 258 canvas.drawCenteredText(text, x1, x2, y1 - UnitConv.pt2mm(fontSize) * 0.2, 259 fontName, fontSize); 260 } 261 262 263 public abstract void generateBarcode(CanvasProvider canvas, String msg); 264 265 266 public BarcodeDimension calcDimensions(String msg) { 267 throw new UnsupportedOperationException ("NYI"); 268 } 269 } | Popular Tags |