1 58 package org.krysalis.barcode.impl; 59 60 import java.util.Stack ; 61 62 import org.krysalis.barcode.BarGroup; 63 import org.krysalis.barcode.BarcodeDimension; 64 import org.krysalis.barcode.ClassicBarcodeLogicHandler; 65 import org.krysalis.barcode.HumanReadablePlacement; 66 import org.krysalis.barcode.output.Canvas; 67 68 74 public class UPCEANCanvasLogicHandler implements ClassicBarcodeLogicHandler { 75 76 private UPCEAN bcImpl; 77 private Canvas canvas; 78 private double x = 0.0; 79 private BarcodeDimension dim; 80 private String msg; 81 private String lastgroup; 82 private double groupx; 83 private boolean inMsgGroup; 84 private boolean inSupplemental; 85 private Stack groupStack = new Stack (); 86 87 92 public UPCEANCanvasLogicHandler(GenericBarcodeImpl bcImpl, Canvas canvas) { 93 if (!(bcImpl instanceof UPCEAN)) { 94 throw new IllegalArgumentException ("This LogicHandler can only be " 95 + "used with UPC and EAN barcode implementations"); 96 } 97 this.bcImpl = (UPCEAN)bcImpl; 98 this.canvas = canvas; 99 } 100 101 private double getStartX() { 102 if (bcImpl.hasQuietZone()) { 103 return bcImpl.getQuietZone(); 104 } else { 105 return 0.0; 106 } 107 } 108 109 110 public void startBarcode(String msg) { 111 this.msg = msg; 112 this.dim = bcImpl.calcDimensions(msg); 114 115 canvas.establishDimensions(dim); 116 x = getStartX(); 117 inMsgGroup = false; 118 inSupplemental = false; 119 120 } 121 122 123 public void startBarGroup(BarGroup type, String submsg) { 124 if (type == BarGroup.UPC_EAN_GUARD) { 125 } else if (type == BarGroup.UPC_EAN_GROUP) { 127 inMsgGroup = true; 128 groupx = x; 129 lastgroup = submsg; 130 } else if (type == BarGroup.UPC_EAN_LEAD) { 131 lastgroup = submsg; 132 } else if (type == BarGroup.UPC_EAN_CHECK) { 133 if (!inMsgGroup) { 134 lastgroup = submsg; 135 } 136 } else if (type == BarGroup.UPC_EAN_SUPP) { 137 inSupplemental = true; 138 x += bcImpl.getQuietZone(); 139 groupx = x; 140 } 141 groupStack.push(type); 142 } 143 144 145 public void addBar(boolean black, int width) { 146 final double w = bcImpl.getBarWidth(width); 147 if (black) { 148 final double h; 149 final double y; 150 if (!inSupplemental) { 151 if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_NONE) { 152 canvas.drawRectWH(x, 0, w, bcImpl.getHeight()); 153 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) { 154 if (inMsgGroup) { 155 h = bcImpl.getBarHeight(); 156 y = bcImpl.getHumanReadableHeight(); 157 } else { 158 h = bcImpl.getBarHeight() + (bcImpl.getHumanReadableHeight() / 2); 159 y = bcImpl.getHumanReadableHeight() / 2; 160 } 161 canvas.drawRectWH(x, y, w, h); 162 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) { 163 if (inMsgGroup) { 164 h = bcImpl.getBarHeight(); 165 } else { 166 h = bcImpl.getBarHeight() + (bcImpl.getHumanReadableHeight() / 2); 167 } 168 canvas.drawRectWH(x, 0.0, w, h); 169 } 170 } else { 171 if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_NONE) { 173 h = bcImpl.getBarHeight(); 174 y = bcImpl.getHumanReadableHeight(); 175 canvas.drawRectWH(x, y, w, h); 176 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) { 177 h = bcImpl.getBarHeight() 178 + (bcImpl.getHumanReadableHeight() / 2) 179 - bcImpl.getHumanReadableHeight(); 180 y = bcImpl.getHumanReadableHeight() / 2; 181 canvas.drawRectWH(x, y, w, h); 182 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) { 183 h = bcImpl.getBarHeight() 184 + (bcImpl.getHumanReadableHeight() / 2) 185 - bcImpl.getHumanReadableHeight(); 186 y = bcImpl.getHumanReadableHeight(); 187 canvas.drawRectWH(x, y, w, h); 188 } 189 } 190 } 191 x += w; 192 } 193 194 private boolean isEAN() { 195 return (bcImpl instanceof EAN13) || (bcImpl instanceof EAN8); 196 } 197 198 199 public void endBarGroup() { 200 BarGroup group = (BarGroup)groupStack.pop(); 201 202 if (group == BarGroup.UPC_EAN_GROUP) { 203 inMsgGroup = false; 204 if (lastgroup == null) { 205 return; 207 } 208 int colonPos = lastgroup.indexOf(":"); 209 String grouptext = lastgroup; 210 if (colonPos >= 0) { 211 String lead = new Character (grouptext.charAt(0)).toString(); 212 drawLeadChar(lead); 213 grouptext = grouptext.substring(colonPos + 1); 214 } 215 216 drawGroupText(grouptext); 218 } else if (group == BarGroup.UPC_EAN_LEAD) { 219 if (!isEAN()) { 220 drawLeadChar(lastgroup); 221 } 222 } else if (group == BarGroup.UPC_EAN_CHECK) { 223 if (!isEAN()) { 224 drawTrailingChar(lastgroup); 225 } 226 } else if (group == BarGroup.UPC_EAN_SUPP) { 227 drawSupplementalText(UPCEANLogicImpl.retrieveSupplemental(this.msg)); 228 inSupplemental = false; 229 } 230 } 231 232 private void drawLeadChar(String lead) { 233 final double leadw = 7 * bcImpl.getBarWidth(1); 234 final double leadx = getStartX() 235 - 3 * bcImpl.getBarWidth(1) 236 - leadw; 237 238 if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_NONE) { 239 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) { 241 bcImpl.drawCenteredText(canvas, lead, leadx, leadx + leadw, 242 bcImpl.getHumanReadableHeight()); 243 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) { 244 bcImpl.drawCenteredText(canvas, lead, leadx, leadx + leadw, 245 bcImpl.getHeight()); 246 } 247 } 248 249 private void drawTrailingChar(String trailer) { 250 final double trailerw = 7 * bcImpl.getBarWidth(1); 251 final double trailerx = getStartX() 252 + this.dim.getWidth() 253 - bcImpl.supplementalWidth(this.msg) 254 + 3 * bcImpl.getBarWidth(1); 255 256 if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_NONE) { 257 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) { 259 bcImpl.drawCenteredText(canvas, 260 trailer, trailerx, trailerx + trailerw, 261 bcImpl.getHumanReadableHeight()); 262 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) { 263 bcImpl.drawCenteredText(canvas, 264 trailer, trailerx, trailerx + trailerw, 265 bcImpl.getHeight()); 266 } 267 } 268 269 private void drawGroupText(String text) { 270 if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_NONE) { 271 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) { 273 bcImpl.drawJustifiedText(canvas, text, 274 groupx + bcImpl.getBarWidth(1), 275 x - bcImpl.getBarWidth(1), 276 bcImpl.getHumanReadableHeight()); 277 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) { 278 bcImpl.drawJustifiedText(canvas, text, 279 groupx + bcImpl.getBarWidth(1), 280 x - bcImpl.getBarWidth(1), 281 bcImpl.getHeight()); 282 } 283 } 284 285 private void drawSupplementalText(String supp) { 286 if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) { 287 bcImpl.drawCenteredText(canvas, supp, 288 groupx, 289 x, 290 bcImpl.getHeight()); 291 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) { 292 bcImpl.drawCenteredText(canvas, supp, 293 groupx, 294 x, 295 bcImpl.getHumanReadableHeight()); 296 } 297 } 298 299 300 public void endBarcode() { 301 } 302 303 } 304 305 | Popular Tags |