1 58 package org.krysalis.barcode.impl; 59 60 import org.krysalis.barcode.BarGroup; 61 import org.krysalis.barcode.ChecksumMode; 62 import org.krysalis.barcode.ClassicBarcodeLogicHandler; 63 64 70 public class CodabarLogicImpl { 71 72 private static final char[] CHARACTERS = 73 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 74 'a', 'b', 'c', 'd', 'e', 'n', 't', 75 '-', '$', ':', '/', '.', '+', '*'}; 76 77 78 protected static final byte[][] CHARSET = 79 {{0, 0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 1, 1, 0}, {0, 0, 0, 1, 0, 0, 1}, {1, 1, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 1, 0}, {1, 0, 0, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 0, 1}, {0, 1, 0, 0, 1, 0, 0}, {0, 1, 1, 0, 0, 0, 0}, {1, 0, 0, 1, 0, 0, 0}, {0, 0, 1, 1, 0, 1, 0}, {0, 1, 0, 1, 0, 0, 1}, {0, 0, 0, 1, 0, 1, 1}, {0, 0, 0, 1, 1, 1, 0}, {0, 0, 0, 1, 1, 1, 0}, {0, 1, 0, 1, 0, 0, 1}, {0, 0, 1, 1, 0, 1, 0}, {0, 0, 0, 1, 1, 0, 0}, {0, 0, 1, 1, 0, 0, 0}, {1, 0, 0, 0, 1, 0, 1}, {1, 0, 1, 0, 0, 0, 1}, {1, 0, 1, 0, 1, 0, 0}, {0, 0, 1, 0, 1, 0, 1}, {0, 0, 0, 1, 0, 1, 1}}; 104 private ChecksumMode checksumMode = ChecksumMode.CP_AUTO; 105 106 107 111 public CodabarLogicImpl(ChecksumMode mode) { 112 this.checksumMode = mode; 113 } 114 115 119 public ChecksumMode getChecksumMode() { 120 return this.checksumMode; 121 } 122 123 128 protected static int getCharIndex(char ch) { 129 for (int i = 0; i < CHARACTERS.length; i++) { 130 if (ch == CHARACTERS[i]) { 131 return i; 132 } 133 } 134 return -1; 135 } 136 137 142 protected static boolean isValidChar(char ch) { 143 return (getCharIndex(ch) >= 0); 144 } 145 146 151 protected static boolean isStartStopChar(char ch) { 152 return ((ch == 'a') || (ch == 'b') 153 || (ch == 'c') || (ch == 'd') 154 || (ch == 'e') || (ch == '*') 155 || (ch == 'n') || (ch == 't')); 156 } 157 158 private int widthAt(char ch, int index) throws IllegalArgumentException { 159 int chidx = getCharIndex(ch); 160 if (chidx >= 0) { 161 int binary = CHARSET[chidx][index]; 162 return binary + 1; 163 } else { 164 throw new IllegalArgumentException ("Invalid character: " + ch); 165 } 166 } 167 168 173 protected void encodeChar(ClassicBarcodeLogicHandler logic, char c) { 174 logic.startBarGroup(BarGroup.MSG_CHARACTER, new Character (c).toString()); 175 for (byte i = 0; i < 7; i++) { 176 final int width = widthAt(c, i); 177 final boolean black = ((i % 2) == 0); 178 logic.addBar(black, width); 179 } 180 logic.endBarGroup(); 181 } 182 183 private void handleChecksum(StringBuffer sb) { 184 if ((getChecksumMode() == ChecksumMode.CP_ADD) 185 || (getChecksumMode() == ChecksumMode.CP_CHECK)) { 186 throw new UnsupportedOperationException ( 187 "No checksums are currently supported for Codabar symbols"); 188 } else if (getChecksumMode() == ChecksumMode.CP_IGNORE) { 189 return; 190 } else if (getChecksumMode() == ChecksumMode.CP_AUTO) { 191 return; } 193 } 194 195 200 public void generateBarcodeLogic(ClassicBarcodeLogicHandler logic, String msg) { 201 StringBuffer sb = new StringBuffer (msg); 202 203 handleChecksum(sb); 204 205 logic.startBarcode(sb.toString()); 206 207 for (int i = 0; i < sb.length(); i++) { 208 if (i > 0) { 209 logic.addBar(false, 1); 211 } 212 final char ch = sb.charAt(i); 213 if (!isValidChar(ch)) throw new IllegalArgumentException ("Invalid character: " + ch); 214 encodeChar(logic, ch); 215 } 216 217 logic.endBarcode(); 218 } 219 220 } 221 | Popular Tags |