Dépôt officiel du code source de l'ERP OpenConcerto
/trunk/Modules/Module Label/.settings/org.eclipse.jdt.core.prefs |
---|
New file |
0,0 → 1,7 |
eclipse.preferences.version=1 |
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled |
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 |
org.eclipse.jdt.core.compiler.compliance=1.8 |
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error |
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error |
org.eclipse.jdt.core.compiler.source=1.8 |
/trunk/Modules/Module Label/.classpath |
---|
1,7 → 1,7 |
<?xml version="1.0" encoding="UTF-8"?> |
<classpath> |
<classpathentry kind="src" path="src"/> |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> |
<classpathentry combineaccessrules="false" kind="src" path="/OpenConcerto"/> |
<classpathentry kind="output" path="bin"/> |
</classpath> |
/trunk/Modules/Module Label/lib/barcode4j-2.1.0.jar |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/trunk/Modules/Module Label/lib/barcode4j-2.1.0.jar |
---|
New file |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/trunk/Modules/Module Label/lib/jbarcode-0.2.8.jar |
---|
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
/trunk/Modules/Module Label/lib/jbarcode-0.2.8.jar |
---|
New file |
Property changes: |
Added: svn:mime-type |
+application/octet-stream |
\ No newline at end of property |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/util/EciMode.java |
---|
New file |
0,0 → 1,67 |
/* |
* Copyright 2018 Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.util; |
import java.nio.charset.Charset; |
import java.nio.charset.UnsupportedCharsetException; |
public class EciMode { |
public static final EciMode NONE = new EciMode(-1, null); |
public final int mode; |
public final Charset charset; |
private EciMode(final int mode, final Charset charset) { |
this.mode = mode; |
this.charset = charset; |
} |
public static EciMode of(final String data, final String charsetName, final int mode) { |
try { |
final Charset charset = Charset.forName(charsetName); |
if (charset.canEncode() && charset.newEncoder().canEncode(data)) { |
return new EciMode(mode, charset); |
} else { |
return NONE; |
} |
} catch (final UnsupportedCharsetException e) { |
return NONE; |
} |
} |
public EciMode or(final String data, final String charsetName, final int mode) { |
if (!equals(NONE)) { |
return this; |
} else { |
return of(data, charsetName, mode); |
} |
} |
@Override |
public boolean equals(final Object other) { |
return other instanceof EciMode && ((EciMode) other).mode == this.mode; |
} |
@Override |
public int hashCode() { |
return Integer.valueOf(this.mode).hashCode(); |
} |
@Override |
public String toString() { |
return "EciMode[mode=" + this.mode + ", charset=" + this.charset + "]"; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/util/Strings.java |
---|
New file |
0,0 → 1,226 |
/* |
* Copyright 2018 Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.util; |
import static java.nio.charset.StandardCharsets.ISO_8859_1; |
import java.nio.charset.StandardCharsets; |
import uk.org.okapibarcode.backend.OkapiException; |
/** |
* String utility class. |
* |
* @author Daniel Gredler |
*/ |
public final class Strings { |
private Strings() { |
// utility class |
} |
/** |
* Replaces raw values with special placeholders, where applicable. |
* |
* @param s the string to add placeholders to |
* @return the specified string, with placeholders added |
* @see <a href="http://www.zint.org.uk/Manual.aspx?type=p&page=4">Zint placeholders</a> |
* @see #unescape(String, boolean) |
*/ |
public static String escape(final String s) { |
final StringBuilder sb = new StringBuilder(s.length() + 10); |
for (int i = 0; i < s.length(); i++) { |
final char c = s.charAt(i); |
switch (c) { |
case '\u0000': |
sb.append("\\0"); // null |
break; |
case '\u0004': |
sb.append("\\E"); // end of transmission |
break; |
case '\u0007': |
sb.append("\\a"); // bell |
break; |
case '\u0008': |
sb.append("\\b"); // backspace |
break; |
case '\u0009': |
sb.append("\\t"); // horizontal tab |
break; |
case '\n': |
sb.append("\\n"); // line feed |
break; |
case '\u000b': |
sb.append("\\v"); // vertical tab |
break; |
case '\u000c': |
sb.append("\\f"); // form feed |
break; |
case '\r': |
sb.append("\\r"); // carriage return |
break; |
case '\u001b': |
sb.append("\\e"); // escape |
break; |
case '\u001d': |
sb.append("\\G"); // group separator |
break; |
case '\u001e': |
sb.append("\\R"); // record separator |
break; |
case '\\': |
sb.append("\\\\"); // escape the escape character |
break; |
default: |
if (c >= 32 && c <= 126) { |
sb.append(c); // printable ASCII |
} else { |
final byte[] bytes = String.valueOf(c).getBytes(ISO_8859_1); |
final String hex = String.format("%02X", bytes[0] & 0xFF); |
sb.append("\\x").append(hex); |
} |
break; |
} |
} |
return sb.toString(); |
} |
/** |
* Replaces any special placeholders with their raw values (not including FNC values). |
* |
* @param s the string to check for placeholders |
* @param lenient whether or not to be lenient with unrecognized escape sequences |
* @return the specified string, with placeholders replaced |
* @see <a href="http://www.zint.org.uk/Manual.aspx?type=p&page=4">Zint placeholders</a> |
* @see #escape(String) |
*/ |
public static String unescape(final String s, final boolean lenient) { |
final StringBuilder sb = new StringBuilder(s.length()); |
for (int i = 0; i < s.length(); i++) { |
final char c = s.charAt(i); |
if (c != '\\') { |
sb.append(c); |
} else { |
if (i + 1 >= s.length()) { |
final String msg = "Error processing escape sequences: expected escape character, found end of string"; |
throw new OkapiException(msg); |
} else { |
final char c2 = s.charAt(i + 1); |
switch (c2) { |
case '0': |
sb.append('\u0000'); // null |
i++; |
break; |
case 'E': |
sb.append('\u0004'); // end of transmission |
i++; |
break; |
case 'a': |
sb.append('\u0007'); // bell |
i++; |
break; |
case 'b': |
sb.append('\u0008'); // backspace |
i++; |
break; |
case 't': |
sb.append('\u0009'); // horizontal tab |
i++; |
break; |
case 'n': |
sb.append('\n'); // line feed |
i++; |
break; |
case 'v': |
sb.append('\u000b'); // vertical tab |
i++; |
break; |
case 'f': |
sb.append('\u000c'); // form feed |
i++; |
break; |
case 'r': |
sb.append('\r'); // carriage return |
i++; |
break; |
case 'e': |
sb.append('\u001b'); // escape |
i++; |
break; |
case 'G': |
sb.append('\u001d'); // group separator |
i++; |
break; |
case 'R': |
sb.append('\u001e'); // record separator |
i++; |
break; |
case '\\': |
sb.append('\\'); // escape the escape character |
i++; |
break; |
case 'x': |
if (i + 3 >= s.length()) { |
final String msg = "Error processing escape sequences: expected hex sequence, found end of string"; |
throw new OkapiException(msg); |
} else { |
final char c3 = s.charAt(i + 2); |
final char c4 = s.charAt(i + 3); |
if (isHex(c3) && isHex(c4)) { |
final byte b = (byte) Integer.parseInt("" + c3 + c4, 16); |
sb.append(new String(new byte[] { b }, StandardCharsets.ISO_8859_1)); |
i += 3; |
} else { |
final String msg = "Error processing escape sequences: expected hex sequence, found '" + c3 + c4 + "'"; |
throw new OkapiException(msg); |
} |
} |
break; |
default: |
if (lenient) { |
sb.append(c); |
} else { |
throw new OkapiException("Error processing escape sequences: expected valid escape character, found '" + c2 + "'"); |
} |
} |
} |
} |
} |
return sb.toString(); |
} |
private static boolean isHex(final char c) { |
return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f'; |
} |
/** |
* Appends the specific integer to the specified string, in binary format, padded to the |
* specified number of digits. |
* |
* @param s the string to append to |
* @param value the value to append, in binary format |
* @param digits the number of digits to pad to |
*/ |
public static void binaryAppend(final StringBuilder s, final int value, final int digits) { |
final int start = 0x01 << digits - 1; |
for (int i = 0; i < digits; i++) { |
if ((value & start >> i) == 0) { |
s.append('0'); |
} else { |
s.append('1'); |
} |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/util/Gs1.java |
---|
New file |
0,0 → 1,596 |
/* |
* Copyright 2018 Robin Stuart, Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.util; |
import uk.org.okapibarcode.backend.OkapiException; |
/** |
* GS1 utility class. |
*/ |
public final class Gs1 { |
private Gs1() { |
// utility class |
} |
/** |
* Verifies that the specified data is in good GS1 format <tt>"[AI]data"</tt> pairs, and returns |
* a reduced version of the input string containing FNC1 escape sequences instead of AI |
* brackets. With a few small exceptions, this code matches the Zint GS1 validation code as |
* closely as possible, in order to make it easier to keep in sync. |
* |
* @param s the data string to verify |
* @param fnc1 the string to use to represent FNC1 in the output |
* @return the input data, verified and with FNC1 strings added at the appropriate positions |
* @see <a href="https://sourceforge.net/p/zint/code/ci/master/tree/backend/gs1.c">Corresponding |
* Zint code</a> |
* @see <a href="http://www.gs1.org/docs/gsmp/barcodes/GS1_General_Specifications.pdf">GS1 |
* specification</a> |
*/ |
public static String verify(final String s, final String fnc1) { |
// Enforce compliance with GS1 General Specification |
// http://www.gs1.org/docs/gsmp/barcodes/GS1_General_Specifications.pdf |
final char[] source = s.toCharArray(); |
final StringBuilder reduced = new StringBuilder(source.length); |
final int[] ai_value = new int[100]; |
final int[] ai_location = new int[100]; |
final int[] data_location = new int[100]; |
final int[] data_length = new int[100]; |
int error_latch; |
/* Detect extended ASCII characters */ |
for (int i = 0; i < source.length; i++) { |
if (source[i] >= 128) { |
throw new OkapiException("Extended ASCII characters are not supported by GS1"); |
} |
if (source[i] < 32) { |
throw new OkapiException("Control characters are not supported by GS1"); |
} |
} |
/* Make sure we start with an AI */ |
if (source[0] != '[') { |
throw new OkapiException("Data does not start with an AI"); |
} |
/* Check the position of the brackets */ |
int bracket_level = 0; |
int max_bracket_level = 0; |
int ai_length = 0; |
int max_ai_length = 0; |
int min_ai_length = 5; |
int j = 0; |
boolean ai_latch = false; |
for (int i = 0; i < source.length; i++) { |
ai_length += j; |
if (j == 1 && source[i] != ']' && (source[i] < '0' || source[i] > '9')) { |
ai_latch = true; |
} |
if (source[i] == '[') { |
bracket_level++; |
j = 1; |
} |
if (source[i] == ']') { |
bracket_level--; |
if (ai_length < min_ai_length) { |
min_ai_length = ai_length; |
} |
j = 0; |
ai_length = 0; |
} |
if (bracket_level > max_bracket_level) { |
max_bracket_level = bracket_level; |
} |
if (ai_length > max_ai_length) { |
max_ai_length = ai_length; |
} |
} |
min_ai_length--; |
if (bracket_level != 0) { |
/* Not all brackets are closed */ |
throw new OkapiException("Malformed AI in input data (brackets don't match)"); |
} |
if (max_bracket_level > 1) { |
/* Nested brackets */ |
throw new OkapiException("Found nested brackets in input data"); |
} |
if (max_ai_length > 4) { |
/* AI is too long */ |
throw new OkapiException("Invalid AI in input data (AI too long)"); |
} |
if (min_ai_length <= 1) { |
/* AI is too short */ |
throw new OkapiException("Invalid AI in input data (AI too short)"); |
} |
if (ai_latch) { |
/* Non-numeric data in AI */ |
throw new OkapiException("Invalid AI in input data (non-numeric characters in AI)"); |
} |
int ai_count = 0; |
for (int i = 1; i < source.length; i++) { |
if (source[i - 1] == '[') { |
ai_location[ai_count] = i; |
ai_value[ai_count] = 0; |
for (j = 0; source[i + j] != ']'; j++) { |
ai_value[ai_count] *= 10; |
ai_value[ai_count] += Character.getNumericValue(source[i + j]); |
} |
ai_count++; |
} |
} |
for (int i = 0; i < ai_count; i++) { |
data_location[i] = ai_location[i] + 3; |
if (ai_value[i] >= 100) { |
data_location[i]++; |
} |
if (ai_value[i] >= 1000) { |
data_location[i]++; |
} |
data_length[i] = source.length - data_location[i]; |
for (j = source.length - 1; j >= data_location[i]; j--) { |
if (source[j] == '[') { |
data_length[i] = j - data_location[i]; |
} |
} |
} |
for (int i = 0; i < ai_count; i++) { |
if (data_length[i] == 0) { |
/* No data for given AI */ |
throw new OkapiException("Empty data field in input data"); |
} |
} |
// Check for valid AI values and data lengths according to GS1 General |
// Specification Release 18, January 2018 |
for (int i = 0; i < ai_count; i++) { |
error_latch = 2; |
switch (ai_value[i]) { |
// Length 2 Fixed |
case 20: // VARIANT |
if (data_length[i] != 2) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 3 Fixed |
case 422: // ORIGIN |
case 424: // COUNTRY PROCESS |
case 426: // COUNTRY FULL PROCESS |
if (data_length[i] != 3) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 4 Fixed |
case 8111: // POINTS |
if (data_length[i] != 4) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 6 Fixed |
case 11: // PROD DATE |
case 12: // DUE DATE |
case 13: // PACK DATE |
case 15: // BEST BY |
case 16: // SELL BY |
case 17: // USE BY |
case 7006: // FIRST FREEZE DATE |
case 8005: // PRICE PER UNIT |
if (data_length[i] != 6) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 10 Fixed |
case 7003: // EXPIRY TIME |
if (data_length[i] != 10) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 13 Fixed |
case 410: // SHIP TO LOC |
case 411: // BILL TO |
case 412: // PURCHASE FROM |
case 413: // SHIP FOR LOC |
case 414: // LOC NO |
case 415: // PAY TO |
case 416: // PROD/SERV LOC |
case 7001: // NSN |
if (data_length[i] != 13) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 14 Fixed |
case 1: // GTIN |
case 2: // CONTENT |
case 8001: // DIMENSIONS |
if (data_length[i] != 14) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 17 Fixed |
case 402: // GSIN |
if (data_length[i] != 17) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 18 Fixed |
case 0: // SSCC |
case 8006: // ITIP |
case 8017: // GSRN PROVIDER |
case 8018: // GSRN RECIPIENT |
if (data_length[i] != 18) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 2 Max |
case 7010: // PROD METHOD |
if (data_length[i] > 2) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 3 Max |
case 427: // ORIGIN SUBDIVISION |
case 7008: // AQUATIC SPECIES |
if (data_length[i] > 3) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 4 Max |
case 7004: // ACTIVE POTENCY |
if (data_length[i] > 4) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 6 Max |
case 242: // MTO VARIANT |
if (data_length[i] > 6) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 8 Max |
case 30: // VAR COUNT |
case 37: // COUNT |
if (data_length[i] > 8) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 10 Max |
case 7009: // FISHING GEAR TYPE |
case 8019: // SRIN |
if (data_length[i] > 10) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 12 Max |
case 7005: // CATCH AREA |
case 8011: // CPID SERIAL |
if (data_length[i] > 12) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 20 Max |
case 10: // BATCH/LOT |
case 21: // SERIAL |
case 22: // CPV |
case 243: // PCN |
case 254: // GLN EXTENSION COMPONENT |
case 420: // SHIP TO POST |
case 7020: // REFURB LOT |
case 7021: // FUNC STAT |
case 7022: // REV STAT |
case 710: // NHRN PZN |
case 711: // NHRN CIP |
case 712: // NHRN CN |
case 713: // NHRN DRN |
case 714: // NHRN AIM |
case 8002: // CMT NO |
case 8012: // VERSION |
if (data_length[i] > 20) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 25 Max |
case 8020: // REF NO |
if (data_length[i] > 25) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 30 Max |
case 240: // ADDITIONAL ID |
case 241: // CUST PART NO |
case 250: // SECONDARY SERIAL |
case 251: // REF TO SOURCE |
case 400: // ORDER NUMBER |
case 401: // GINC |
case 403: // ROUTE |
case 7002: // MEAT CUT |
case 7023: // GIAI ASSEMBLY |
case 8004: // GIAI |
case 8010: // CPID |
case 8013: // BUDI-DI |
case 90: // INTERNAL |
if (data_length[i] > 30) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 34 Max |
case 8007: // IBAN |
if (data_length[i] > 34) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
// Length 70 Max |
case 8110: // Coupon code |
case 8112: // Paperless coupon code |
case 8200: // PRODUCT URL |
if (data_length[i] > 70) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
break; |
} |
if (ai_value[i] == 253) { // GDTI |
if (data_length[i] < 14 || data_length[i] > 30) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] == 255) { // GCN |
if (data_length[i] < 14 || data_length[i] > 25) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 3100 && ai_value[i] <= 3169) { |
if (data_length[i] != 6) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 3200 && ai_value[i] <= 3379) { |
if (data_length[i] != 6) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 3400 && ai_value[i] <= 3579) { |
if (data_length[i] != 6) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 3600 && ai_value[i] <= 3699) { |
if (data_length[i] != 6) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 3900 && ai_value[i] <= 3909) { // AMOUNT |
if (data_length[i] > 15) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 3910 && ai_value[i] <= 3919) { // AMOUNT |
if (data_length[i] < 4 || data_length[i] > 18) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 3920 && ai_value[i] <= 3929) { // PRICE |
if (data_length[i] > 15) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 3930 && ai_value[i] <= 3939) { // PRICE |
if (data_length[i] < 4 || data_length[i] > 18) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 3940 && ai_value[i] <= 3949) { // PRCNT OFF |
if (data_length[i] != 4) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] == 421) { // SHIP TO POST |
if (data_length[i] < 3 || data_length[i] > 12) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] == 423 || ai_value[i] == 425) { |
// COUNTRY INITIAL PROCESS || COUNTRY DISASSEMBLY |
if (data_length[i] < 3 || data_length[i] > 15) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] == 7007) { // HARVEST DATE |
if (data_length[i] < 6 || data_length[i] > 12) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 7030 && ai_value[i] <= 7039) { // PROCESSOR # |
if (data_length[i] < 4 || data_length[i] > 30) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] == 8003) { // GRAI |
if (data_length[i] < 15 || data_length[i] > 30) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] == 8008) { // PROD TIME |
if (data_length[i] < 9 || data_length[i] > 12) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (ai_value[i] >= 91 && ai_value[i] <= 99) { // INTERNAL |
if (data_length[i] > 90) { |
error_latch = 1; |
} else { |
error_latch = 0; |
} |
} |
if (error_latch == 1) { |
throw new OkapiException("Invalid data length for AI"); |
} |
if (error_latch == 2) { |
throw new OkapiException("Invalid AI value"); |
} |
} |
/* Resolve AI data - put resulting string in 'reduced' */ |
int last_ai = 0; |
boolean fixedLengthAI = true; |
for (int i = 0; i < source.length; i++) { |
if (source[i] != '[' && source[i] != ']') { |
reduced.append(source[i]); |
} |
if (source[i] == '[') { |
/* Start of an AI string */ |
if (!fixedLengthAI) { |
reduced.append(fnc1); |
} |
last_ai = 10 * Character.getNumericValue(source[i + 1]) + Character.getNumericValue(source[i + 2]); |
/* |
* The following values from |
* "GS-1 General Specification version 8.0 issue 2, May 2008" figure 5.4.8.2.1 - 1 |
* "Element Strings with Pre-Defined Length Using Application Identifiers" |
*/ |
fixedLengthAI = last_ai >= 0 && last_ai <= 4 || last_ai >= 11 && last_ai <= 20 || last_ai == 23 |
|| /* legacy support - see 5.3.8.2.2 */ |
last_ai >= 31 && last_ai <= 36 || last_ai == 41; |
} |
} |
return reduced.toString(); |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/util/Doubles.java |
---|
New file |
0,0 → 1,39 |
/* |
* Copyright 2015 Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.util; |
/** |
* Double utility class. |
* |
* @author Daniel Gredler |
*/ |
public final class Doubles { |
private Doubles() { |
// utility class |
} |
/** |
* It's usually not a good idea to check floating point numbers for exact equality. This method |
* allows us to check for approximate equality. |
* |
* @param d1 the first double |
* @param d2 the second double |
* @return whether or not the two doubles are approximately equal (to within 0.0001) |
*/ |
public static boolean roughlyEqual(final double d1, final double d2) { |
return Math.abs(d1 - d2) < 0.0001; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/util/Arrays.java |
---|
New file |
0,0 → 1,112 |
/* |
* Copyright 2018 Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.util; |
import uk.org.okapibarcode.backend.OkapiException; |
/** |
* Array utility class. |
* |
* @author Daniel Gredler |
*/ |
public final class Arrays { |
private Arrays() { |
// utility class |
} |
/** |
* Returns the position of the specified value in the specified array. |
* |
* @param value the value to search for |
* @param array the array to search in |
* @return the position of the specified value in the specified array |
*/ |
public static int positionOf(final char value, final char[] array) { |
for (int i = 0; i < array.length; i++) { |
if (value == array[i]) { |
return i; |
} |
} |
throw new OkapiException("Unable to find character '" + value + "' in character array."); |
} |
/** |
* Returns the position of the specified value in the specified array. |
* |
* @param value the value to search for |
* @param array the array to search in |
* @return the position of the specified value in the specified array |
*/ |
public static int positionOf(final int value, final int[] array) { |
for (int i = 0; i < array.length; i++) { |
if (value == array[i]) { |
return i; |
} |
} |
throw new OkapiException("Unable to find integer '" + value + "' in integer array."); |
} |
/** |
* Returns <code>true</code> if the specified array contains the specified value. |
* |
* @param array the array to check in |
* @param value the value to check for |
* @return true if the specified array contains the specified value |
*/ |
public static boolean contains(final int[] array, final int value) { |
for (int i = 0; i < array.length; i++) { |
if (array[i] == value) { |
return true; |
} |
} |
return false; |
} |
/** |
* Returns <code>true</code> if the specified array contains the specified sub-array at the |
* specified index. |
* |
* @param array the array to search in |
* @param searchFor the sub-array to search for |
* @param index the index at which to search |
* @return whether or not the specified array contains the specified sub-array at the specified |
* index |
*/ |
public static boolean containsAt(final byte[] array, final byte[] searchFor, final int index) { |
for (int i = 0; i < searchFor.length; i++) { |
if (index + i >= array.length || array[index + i] != searchFor[i]) { |
return false; |
} |
} |
return true; |
} |
/** |
* Inserts the specified array into the specified original array at the specified index. |
* |
* @param original the original array into which we want to insert another array |
* @param index the index at which we want to insert the array |
* @param inserted the array that we want to insert |
* @return the combined array |
*/ |
public static int[] insertArray(final int[] original, final int index, final int[] inserted) { |
final int[] modified = new int[original.length + inserted.length]; |
System.arraycopy(original, 0, modified, 0, index); |
System.arraycopy(inserted, 0, modified, index, inserted.length); |
System.arraycopy(original, index, modified, index + inserted.length, modified.length - index - inserted.length); |
return modified; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/output/SvgRenderer.java |
---|
New file |
0,0 → 1,225 |
/* |
* Copyright 2014-2015 Robin Stuart, Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.output; |
import static uk.org.okapibarcode.backend.HumanReadableAlignment.CENTER; |
import static uk.org.okapibarcode.backend.HumanReadableAlignment.JUSTIFY; |
import java.awt.Color; |
import java.awt.geom.Ellipse2D; |
import java.awt.geom.Rectangle2D; |
import java.io.IOException; |
import java.io.OutputStream; |
import java.io.StringWriter; |
import javax.xml.parsers.DocumentBuilderFactory; |
import javax.xml.parsers.ParserConfigurationException; |
import javax.xml.transform.OutputKeys; |
import javax.xml.transform.Transformer; |
import javax.xml.transform.TransformerException; |
import javax.xml.transform.TransformerFactory; |
import javax.xml.transform.TransformerFactoryConfigurationError; |
import javax.xml.transform.dom.DOMSource; |
import javax.xml.transform.stream.StreamResult; |
import org.w3c.dom.Document; |
import org.w3c.dom.Text; |
import uk.org.okapibarcode.backend.Hexagon; |
import uk.org.okapibarcode.backend.HumanReadableAlignment; |
import uk.org.okapibarcode.backend.Symbol; |
import uk.org.okapibarcode.backend.TextBox; |
/** |
* Renders symbologies to SVG (Scalable Vector Graphics). |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
* @author Daniel Gredler |
*/ |
public class SvgRenderer implements SymbolRenderer { |
/** The output stream to render to. */ |
private final OutputStream out; |
/** The magnification factor to apply. */ |
private final double magnification; |
/** The paper (background) color. */ |
private final Color paper; |
/** The ink (foreground) color. */ |
private final Color ink; |
/** Whether or not to include the XML prolog in the output. */ |
private final boolean xmlProlog; |
/** |
* Creates a new SVG renderer. |
* |
* @param out the output stream to render to |
* @param magnification the magnification factor to apply |
* @param paper the paper (background) color |
* @param ink the ink (foreground) color |
* @param xmlProlog whether or not to include the XML prolog in the output (usually {@code true} |
* for standalone SVG documents, {@code false} for SVG content embedded directly in HTML |
* documents) |
*/ |
public SvgRenderer(final OutputStream out, final double magnification, final Color paper, final Color ink, final boolean xmlProlog) { |
this.out = out; |
this.magnification = magnification; |
this.paper = paper; |
this.ink = ink; |
this.xmlProlog = xmlProlog; |
} |
/** {@inheritDoc} */ |
@Override |
public void render(final Symbol symbol) throws IOException { |
final String content = symbol.getContent(); |
final int width = (int) (symbol.getWidth() * this.magnification); |
final int height = (int) (symbol.getHeight() * this.magnification); |
final int marginX = (int) (symbol.getQuietZoneHorizontal() * this.magnification); |
final int marginY = (int) (symbol.getQuietZoneVertical() * this.magnification); |
String title; |
if (content == null || content.isEmpty()) { |
title = "OkapiBarcode Generated Symbol"; |
} else { |
title = content; |
} |
final String fgColour = String.format("%02X", this.ink.getRed()) + String.format("%02X", this.ink.getGreen()) + String.format("%02X", this.ink.getBlue()); |
final String bgColour = String.format("%02X", this.paper.getRed()) + String.format("%02X", this.paper.getGreen()) + String.format("%02X", this.paper.getBlue()); |
try (ExtendedOutputStreamWriter writer = new ExtendedOutputStreamWriter(this.out, "%.2f")) { |
// XML Prolog |
if (this.xmlProlog) { |
writer.append("<?xml version=\"1.0\" standalone=\"no\"?>\n"); |
writer.append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n"); |
writer.append(" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"); |
} |
// Header |
writer.append("<svg width=\"").appendInt(width).append("\" height=\"").appendInt(height).append("\" version=\"1.1").append("\" xmlns=\"http://www.w3.org/2000/svg\">\n"); |
writer.append(" <desc>").append(clean(title)).append("</desc>\n"); |
writer.append(" <g id=\"barcode\" fill=\"#").append(fgColour).append("\">\n"); |
writer.append(" <rect x=\"0\" y=\"0\" width=\"").appendInt(width).append("\" height=\"").appendInt(height).append("\" fill=\"#").append(bgColour).append("\" />\n"); |
// Rectangles |
for (int i = 0; i < symbol.getRectangles().size(); i++) { |
final Rectangle2D.Double rect = symbol.getRectangles().get(i); |
writer.append(" <rect x=\"").append(rect.x * this.magnification + marginX).append("\" y=\"").append(rect.y * this.magnification + marginY).append("\" width=\"") |
.append(rect.width * this.magnification).append("\" height=\"").append(rect.height * this.magnification).append("\" />\n"); |
} |
// Text |
for (int i = 0; i < symbol.getTexts().size(); i++) { |
final TextBox text = symbol.getTexts().get(i); |
final HumanReadableAlignment alignment = text.alignment == JUSTIFY && text.text.length() == 1 ? CENTER : text.alignment; |
double x; |
String anchor; |
switch (alignment) { |
case LEFT: |
case JUSTIFY: |
x = this.magnification * text.x + marginX; |
anchor = "start"; |
break; |
case RIGHT: |
x = this.magnification * text.x + this.magnification * text.width + marginX; |
anchor = "end"; |
break; |
case CENTER: |
x = this.magnification * text.x + this.magnification * text.width / 2 + marginX; |
anchor = "middle"; |
break; |
default: |
throw new IllegalStateException("Unknown alignment: " + alignment); |
} |
writer.append(" <text x=\"").append(x).append("\" y=\"").append(text.y * this.magnification + marginY).append("\" text-anchor=\"").append(anchor).append("\"\n"); |
if (alignment == JUSTIFY) { |
writer.append(" textLength=\"").append(text.width * this.magnification).append("\" lengthAdjust=\"spacing\"\n"); |
} |
writer.append(" font-family=\"").append(clean(symbol.getFontName())).append("\" font-size=\"").append(symbol.getFontSize() * this.magnification).append("\" fill=\"#") |
.append(fgColour).append("\">\n"); |
writer.append(" ").append(clean(text.text)).append("\n"); |
writer.append(" </text>\n"); |
} |
// Circles |
for (int i = 0; i < symbol.getTarget().size(); i++) { |
final Ellipse2D.Double ellipse = symbol.getTarget().get(i); |
String color; |
if ((i & 1) == 0) { |
color = fgColour; |
} else { |
color = bgColour; |
} |
writer.append(" <circle cx=\"").append((ellipse.x + ellipse.width / 2) * this.magnification + marginX).append("\" cy=\"") |
.append((ellipse.y + ellipse.width / 2) * this.magnification + marginY).append("\" r=\"").append(ellipse.width / 2 * this.magnification).append("\" fill=\"#").append(color) |
.append("\" />\n"); |
} |
// Hexagons |
for (int i = 0; i < symbol.getHexagons().size(); i++) { |
final Hexagon hexagon = symbol.getHexagons().get(i); |
writer.append(" <path d=\""); |
for (int j = 0; j < 6; j++) { |
if (j == 0) { |
writer.append("M "); |
} else { |
writer.append("L "); |
} |
writer.append(hexagon.pointX[j] * this.magnification + marginX).append(" ").append(hexagon.pointY[j] * this.magnification + marginY).append(" "); |
} |
writer.append("Z\" />\n"); |
} |
// Footer |
writer.append(" </g>\n"); |
writer.append("</svg>\n"); |
} |
} |
/** |
* Cleans / sanitizes the specified string for inclusion in XML. A bit convoluted, but we're |
* trying to do it without adding an external dependency just for this... |
* |
* @param s the string to be cleaned / sanitized |
* @return the cleaned / sanitized string |
*/ |
protected String clean(String s) { |
// remove control characters |
s = s.replaceAll("[\u0000-\u001f]", ""); |
// escape XML characters |
try { |
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); |
final Text text = document.createTextNode(s); |
final Transformer transformer = TransformerFactory.newInstance().newTransformer(); |
final DOMSource source = new DOMSource(text); |
final StringWriter writer = new StringWriter(); |
final StreamResult result = new StreamResult(writer); |
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
transformer.transform(source, result); |
return writer.toString(); |
} catch (ParserConfigurationException | TransformerException | TransformerFactoryConfigurationError e) { |
return s; |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/output/ExtendedOutputStreamWriter.java |
---|
New file |
0,0 → 1,80 |
/* |
* Copyright 2015 Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.output; |
import java.io.IOException; |
import java.io.OutputStream; |
import java.io.OutputStreamWriter; |
import java.nio.charset.StandardCharsets; |
import java.util.Locale; |
/** |
* {@link OutputStreamWriter} extension which provides some convenience methods for writing numbers. |
*/ |
class ExtendedOutputStreamWriter extends OutputStreamWriter { |
/** Format to use when writing doubles to the stream. */ |
private final String doubleFormat; |
/** |
* Creates a new extended output stream writer, using the UTF-8 charset. |
* |
* @param out the stream to write to |
* @param doubleFormat the format to use when writing doubles to the stream |
*/ |
public ExtendedOutputStreamWriter(final OutputStream out, final String doubleFormat) { |
super(out, StandardCharsets.UTF_8); |
this.doubleFormat = doubleFormat; |
} |
/** {@inheritDoc} */ |
@Override |
public ExtendedOutputStreamWriter append(final CharSequence cs) throws IOException { |
super.append(cs); |
return this; |
} |
/** {@inheritDoc} */ |
@Override |
public ExtendedOutputStreamWriter append(final CharSequence cs, final int start, final int end) throws IOException { |
super.append(cs, start, end); |
return this; |
} |
/** |
* Writes the specified double to the stream, formatted according to the format specified in the |
* constructor. |
* |
* @param d the double to write to the stream |
* @return this writer |
* @throws IOException if an I/O error occurs |
*/ |
public ExtendedOutputStreamWriter append(final double d) throws IOException { |
super.append(String.format(Locale.ROOT, this.doubleFormat, d)); |
return this; |
} |
/** |
* Writes the specified integer to the stream. |
* |
* @param i the integer to write to the stream |
* @return this writer |
* @throws IOException if an I/O error occurs |
*/ |
public ExtendedOutputStreamWriter appendInt(final int i) throws IOException { |
super.append(String.valueOf(i)); |
return this; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/output/SymbolRenderer.java |
---|
New file |
0,0 → 1,34 |
/* |
* Copyright 2015 Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.output; |
import java.io.IOException; |
import uk.org.okapibarcode.backend.Symbol; |
/** |
* Renders symbols to some output format. |
*/ |
public interface SymbolRenderer { |
/** |
* Renders the specified symbology. |
* |
* @param symbol the symbology to render |
* @throws IOException if there is an I/O error |
*/ |
void render(Symbol symbol) throws IOException; |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/output/Java2DRenderer.java |
---|
New file |
0,0 → 1,169 |
/* |
* Copyright 2014-2015 Robin Stuart, Robert Elliott, Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.output; |
import static uk.org.okapibarcode.backend.HumanReadableAlignment.CENTER; |
import static uk.org.okapibarcode.backend.HumanReadableAlignment.JUSTIFY; |
import java.awt.Color; |
import java.awt.Font; |
import java.awt.FontMetrics; |
import java.awt.Graphics2D; |
import java.awt.Polygon; |
import java.awt.font.FontRenderContext; |
import java.awt.font.TextAttribute; |
import java.awt.geom.Area; |
import java.awt.geom.Ellipse2D; |
import java.awt.geom.Rectangle2D; |
import java.util.Collections; |
import java.util.List; |
import uk.org.okapibarcode.backend.Hexagon; |
import uk.org.okapibarcode.backend.HumanReadableAlignment; |
import uk.org.okapibarcode.backend.Symbol; |
import uk.org.okapibarcode.backend.TextBox; |
/** |
* Renders symbologies using the Java 2D API. |
*/ |
public class Java2DRenderer implements SymbolRenderer { |
/** The graphics to render to. */ |
private final Graphics2D g2d; |
/** The magnification factor to apply. */ |
private final double magnification; |
/** The paper (background) color. */ |
private final Color paper; |
/** The ink (foreground) color. */ |
private final Color ink; |
/** |
* Creates a new Java 2D renderer. If the specified paper color is <tt>null</tt>, the symbol is |
* drawn without clearing the existing <tt>g2d</tt> background. |
* |
* @param g2d the graphics to render to |
* @param magnification the magnification factor to apply |
* @param paper the paper (background) color (may be <tt>null</tt>) |
* @param ink the ink (foreground) color |
*/ |
public Java2DRenderer(final Graphics2D g2d, final double magnification, final Color paper, final Color ink) { |
this.g2d = g2d; |
this.magnification = magnification; |
this.paper = paper; |
this.ink = ink; |
} |
/** {@inheritDoc} */ |
@Override |
public void render(final Symbol symbol) { |
final int marginX = (int) (symbol.getQuietZoneHorizontal() * this.magnification); |
final int marginY = (int) (symbol.getQuietZoneVertical() * this.magnification); |
Font f = symbol.getFont(); |
if (f != null) { |
f = f.deriveFont((float) (f.getSize2D() * this.magnification)); |
} else { |
f = new Font(symbol.getFontName(), Font.PLAIN, (int) (symbol.getFontSize() * this.magnification)); |
f = f.deriveFont(Collections.singletonMap(TextAttribute.TRACKING, 0)); |
} |
final Font oldFont = this.g2d.getFont(); |
final Color oldColor = this.g2d.getColor(); |
if (this.paper != null) { |
final int w = (int) (symbol.getWidth() * this.magnification); |
final int h = (int) (symbol.getHeight() * this.magnification); |
this.g2d.setColor(this.paper); |
this.g2d.fillRect(0, 0, w, h); |
} |
this.g2d.setColor(this.ink); |
for (final Rectangle2D.Double rect : symbol.getRectangles()) { |
final double x = rect.x * this.magnification + marginX; |
final double y = rect.y * this.magnification + marginY; |
final double w = rect.width * this.magnification; |
final double h = rect.height * this.magnification; |
this.g2d.fillRect((int) x, (int) y, (int) w, (int) h); |
} |
for (final TextBox text : symbol.getTexts()) { |
final HumanReadableAlignment alignment = text.alignment == JUSTIFY && text.text.length() == 1 ? CENTER : text.alignment; |
final Font font = alignment != JUSTIFY ? f : addTracking(f, text.width * this.magnification, text.text, this.g2d); |
this.g2d.setFont(font); |
final FontMetrics fm = this.g2d.getFontMetrics(); |
final Rectangle2D bounds = fm.getStringBounds(text.text, this.g2d); |
final float y = (float) (text.y * this.magnification) + marginY; |
float x; |
switch (alignment) { |
case LEFT: |
case JUSTIFY: |
x = (float) (this.magnification * text.x + marginX); |
break; |
case RIGHT: |
x = (float) (this.magnification * text.x + this.magnification * text.width - bounds.getWidth() + marginX); |
break; |
case CENTER: |
x = (float) (this.magnification * text.x + this.magnification * text.width / 2 - bounds.getWidth() / 2 + marginX); |
break; |
default: |
throw new IllegalStateException("Unknown alignment: " + alignment); |
} |
this.g2d.drawString(text.text, x, y); |
} |
for (final Hexagon hexagon : symbol.getHexagons()) { |
final Polygon polygon = new Polygon(); |
for (int j = 0; j < 6; j++) { |
polygon.addPoint((int) (hexagon.pointX[j] * this.magnification + marginX), (int) (hexagon.pointY[j] * this.magnification + marginY)); |
} |
this.g2d.fill(polygon); |
} |
final List<Ellipse2D.Double> target = symbol.getTarget(); |
for (int i = 0; i + 1 < target.size(); i += 2) { |
final Ellipse2D.Double outer = adjust(target.get(i), this.magnification, marginX, marginY); |
final Ellipse2D.Double inner = adjust(target.get(i + 1), this.magnification, marginX, marginY); |
final Area area = new Area(outer); |
area.subtract(new Area(inner)); |
this.g2d.fill(area); |
} |
this.g2d.setFont(oldFont); |
this.g2d.setColor(oldColor); |
} |
private static Ellipse2D.Double adjust(final Ellipse2D.Double ellipse, final double magnification, final int marginX, final int marginY) { |
final double x = ellipse.x * magnification + marginX; |
final double y = ellipse.y * magnification + marginY; |
final double w = ellipse.width * magnification + marginX; |
final double h = ellipse.height * magnification + marginY; |
return new Ellipse2D.Double(x, y, w, h); |
} |
private static Font addTracking(final Font baseFont, final double maxTextWidth, final String text, final Graphics2D g2d) { |
final FontRenderContext frc = g2d.getFontRenderContext(); |
final double originalWidth = baseFont.getStringBounds(text, frc).getWidth(); |
final double extraSpace = maxTextWidth - originalWidth; |
final double extraSpacePerGap = extraSpace / (text.length() - 1); |
final double scaleX = baseFont.isTransformed() ? baseFont.getTransform().getScaleX() : 1; |
final double tracking = extraSpacePerGap / (baseFont.getSize2D() * scaleX); |
return baseFont.deriveFont(Collections.singletonMap(TextAttribute.TRACKING, tracking)); |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/output/PostScriptRenderer.java |
---|
New file |
0,0 → 1,211 |
/* |
* Copyright 2015 Robin Stuart, Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.output; |
import static uk.org.okapibarcode.backend.HumanReadableAlignment.CENTER; |
import static uk.org.okapibarcode.backend.HumanReadableAlignment.JUSTIFY; |
import static uk.org.okapibarcode.util.Doubles.roughlyEqual; |
import java.awt.Color; |
import java.awt.geom.Ellipse2D; |
import java.awt.geom.Rectangle2D; |
import java.io.IOException; |
import java.io.OutputStream; |
import uk.org.okapibarcode.backend.Hexagon; |
import uk.org.okapibarcode.backend.HumanReadableAlignment; |
import uk.org.okapibarcode.backend.Symbol; |
import uk.org.okapibarcode.backend.TextBox; |
/** |
* Renders symbologies to EPS (Encapsulated PostScript). |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
* @author Daniel Gredler |
*/ |
public class PostScriptRenderer implements SymbolRenderer { |
/** The output stream to render to. */ |
private final OutputStream out; |
/** The magnification factor to apply. */ |
private final double magnification; |
/** The paper (background) color. */ |
private final Color paper; |
/** The ink (foreground) color. */ |
private final Color ink; |
/** |
* Creates a new PostScript renderer. |
* |
* @param out the output stream to render to |
* @param magnification the magnification factor to apply |
* @param paper the paper (background) color |
* @param ink the ink (foreground) color |
*/ |
public PostScriptRenderer(final OutputStream out, final double magnification, final Color paper, final Color ink) { |
this.out = out; |
this.magnification = magnification; |
this.paper = paper; |
this.ink = ink; |
} |
/** {@inheritDoc} */ |
@Override |
public void render(final Symbol symbol) throws IOException { |
// All y dimensions are reversed because EPS origin (0,0) is at the bottom left, not top |
// left |
final String content = symbol.getContent(); |
final int width = (int) (symbol.getWidth() * this.magnification); |
final int height = (int) (symbol.getHeight() * this.magnification); |
final int marginX = (int) (symbol.getQuietZoneHorizontal() * this.magnification); |
final int marginY = (int) (symbol.getQuietZoneVertical() * this.magnification); |
String title; |
if (content == null || content.isEmpty()) { |
title = "OkapiBarcode Generated Symbol"; |
} else { |
title = content; |
} |
try (ExtendedOutputStreamWriter writer = new ExtendedOutputStreamWriter(this.out, "%.2f")) { |
// Header |
writer.append("%!PS-Adobe-3.0 EPSF-3.0\n"); |
writer.append("%%Creator: OkapiBarcode\n"); |
writer.append("%%Title: ").append(title).append('\n'); |
writer.append("%%Pages: 0\n"); |
writer.append("%%BoundingBox: 0 0 ").appendInt(width).append(" ").appendInt(height).append("\n"); |
writer.append("%%EndComments\n"); |
// Definitions |
writer.append("/TL { setlinewidth moveto lineto stroke } bind def\n"); |
writer.append("/TC { moveto 0 360 arc 360 0 arcn fill } bind def\n"); |
writer.append("/TH { 0 setlinewidth moveto lineto lineto lineto lineto lineto closepath fill } bind def\n"); |
writer.append("/TB { 2 copy } bind def\n"); |
writer.append("/TR { newpath 4 1 roll exch moveto 1 index 0 rlineto 0 exch rlineto neg 0 rlineto closepath fill } bind def\n"); |
writer.append("/TE { pop pop } bind def\n"); |
// Background |
writer.append("newpath\n"); |
writer.append(this.ink.getRed() / 255.0).append(" ").append(this.ink.getGreen() / 255.0).append(" ").append(this.ink.getBlue() / 255.0).append(" setrgbcolor\n"); |
writer.append(this.paper.getRed() / 255.0).append(" ").append(this.paper.getGreen() / 255.0).append(" ").append(this.paper.getBlue() / 255.0).append(" setrgbcolor\n"); |
writer.append(height).append(" 0.00 TB 0.00 ").append(width).append(" TR\n"); |
// Rectangles |
for (int i = 0; i < symbol.getRectangles().size(); i++) { |
final Rectangle2D.Double rect = symbol.getRectangles().get(i); |
if (i == 0) { |
writer.append("TE\n"); |
writer.append(this.ink.getRed() / 255.0).append(" ").append(this.ink.getGreen() / 255.0).append(" ").append(this.ink.getBlue() / 255.0).append(" setrgbcolor\n"); |
writer.append(rect.height * this.magnification).append(" ").append(height - (rect.y + rect.height) * this.magnification - marginY).append(" TB ") |
.append(rect.x * this.magnification + marginX).append(" ").append(rect.width * this.magnification).append(" TR\n"); |
} else { |
final Rectangle2D.Double prev = symbol.getRectangles().get(i - 1); |
if (!roughlyEqual(rect.height, prev.height) || !roughlyEqual(rect.y, prev.y)) { |
writer.append("TE\n"); |
writer.append(this.ink.getRed() / 255.0).append(" ").append(this.ink.getGreen() / 255.0).append(" ").append(this.ink.getBlue() / 255.0).append(" setrgbcolor\n"); |
writer.append(rect.height * this.magnification).append(" ").append(height - (rect.y + rect.height) * this.magnification - marginY).append(" "); |
} |
writer.append("TB ").append(rect.x * this.magnification + marginX).append(" ").append(rect.width * this.magnification).append(" TR\n"); |
} |
} |
// Text |
for (int i = 0; i < symbol.getTexts().size(); i++) { |
final TextBox text = symbol.getTexts().get(i); |
final HumanReadableAlignment alignment = text.alignment == JUSTIFY && text.text.length() == 1 ? CENTER : text.alignment; |
if (i == 0) { |
writer.append("TE\n"); |
; |
writer.append(this.ink.getRed() / 255.0).append(" ").append(this.ink.getGreen() / 255.0).append(" ").append(this.ink.getBlue() / 255.0).append(" setrgbcolor\n"); |
} |
writer.append("matrix currentmatrix\n"); |
writer.append("/").append(symbol.getFontName()).append(" findfont\n"); |
writer.append(symbol.getFontSize() * this.magnification).append(" scalefont setfont\n"); |
final double y = height - text.y * this.magnification - marginY; |
switch (alignment) { |
case LEFT: |
final double leftX = this.magnification * text.x + marginX; |
writer.append(" 0 0 moveto ").append(leftX).append(" ").append(y).append(" translate 0.00 rotate 0 0 moveto\n"); |
writer.append(" (").append(text.text).append(") show\n"); |
break; |
case JUSTIFY: |
final double textX = this.magnification * text.x + marginX; |
final double textW = this.magnification * text.width; |
writer.append(" 0 0 moveto ").append(textX).append(" ").append(y).append(" translate 0.00 rotate 0 0 moveto\n"); |
writer.append(" (").append(text.text).append(") dup stringwidth pop ").append(textW).append(" sub neg 1 index length 1 sub div 0").append(" 3 -1 roll ashow\n"); |
break; |
case RIGHT: |
final double rightX = this.magnification * text.x + this.magnification * text.width + marginX; |
writer.append(" 0 0 moveto ").append(rightX).append(" ").append(y).append(" translate 0.00 rotate 0 0 moveto\n"); |
writer.append(" (").append(text.text).append(") stringwidth\n"); |
writer.append("pop\n"); |
writer.append("-1 mul 0 rmoveto\n"); |
writer.append(" (").append(text.text).append(") show\n"); |
break; |
case CENTER: |
final double centerX = this.magnification * text.x + this.magnification * text.width / 2 + marginX; |
writer.append(" 0 0 moveto ").append(centerX).append(" ").append(y).append(" translate 0.00 rotate 0 0 moveto\n"); |
writer.append(" (").append(text.text).append(") stringwidth\n"); |
writer.append("pop\n"); |
writer.append("-2 div 0 rmoveto\n"); |
writer.append(" (").append(text.text).append(") show\n"); |
break; |
default: |
throw new IllegalStateException("Unknown alignment: " + alignment); |
} |
writer.append("setmatrix\n"); |
} |
// Circles |
// Because MaxiCode size is fixed, this ignores magnification |
for (int i = 0; i < symbol.getTarget().size(); i += 2) { |
final Ellipse2D.Double ellipse1 = symbol.getTarget().get(i); |
final Ellipse2D.Double ellipse2 = symbol.getTarget().get(i + 1); |
if (i == 0) { |
writer.append("TE\n"); |
writer.append(this.ink.getRed() / 255.0).append(" ").append(this.ink.getGreen() / 255.0).append(" ").append(this.ink.getBlue() / 255.0).append(" setrgbcolor\n"); |
writer.append(this.ink.getRed() / 255.0).append(" ").append(this.ink.getGreen() / 255.0).append(" ").append(this.ink.getBlue() / 255.0).append(" setrgbcolor\n"); |
} |
final double x1 = ellipse1.x + ellipse1.width / 2; |
final double x2 = ellipse2.x + ellipse2.width / 2; |
final double y1 = height - ellipse1.y - ellipse1.width / 2; |
final double y2 = height - ellipse2.y - ellipse2.width / 2; |
final double r1 = ellipse1.width / 2; |
final double r2 = ellipse2.width / 2; |
writer.append(x1 + marginX).append(" ").append(y1 - marginY).append(" ").append(r1).append(" ").append(x2 + marginX).append(" ").append(y2 - marginY).append(" ").append(r2).append(" ") |
.append(x2 + r2 + marginX).append(" ").append(y2 - marginY).append(" TC\n"); |
} |
// Hexagons |
// Because MaxiCode size is fixed, this ignores magnification |
for (int i = 0; i < symbol.getHexagons().size(); i++) { |
final Hexagon hexagon = symbol.getHexagons().get(i); |
for (int j = 0; j < 6; j++) { |
writer.append(hexagon.pointX[j] + marginX).append(" ").append(height - hexagon.pointY[j] - marginY).append(" "); |
} |
writer.append(" TH\n"); |
} |
// Footer |
writer.append("\nshowpage\n"); |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Upc.java |
---|
New file |
0,0 → 1,411 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.backend.Ean.calcDigit; |
import static uk.org.okapibarcode.backend.Ean.validateAndPad; |
import static uk.org.okapibarcode.backend.HumanReadableLocation.BOTTOM; |
import static uk.org.okapibarcode.backend.HumanReadableLocation.NONE; |
import static uk.org.okapibarcode.backend.HumanReadableLocation.TOP; |
import java.awt.geom.Rectangle2D; |
import java.util.Arrays; |
/** |
* <p> |
* Implements UPC bar code symbology according to BS EN 797:1996. |
* |
* <p> |
* UPC-A requires an 11 digit article number. The check digit is calculated. UPC-E is a |
* zero-compressed version of UPC-A developed for smaller packages. The code requires a 6 digit |
* article number (digits 0-9). The check digit is calculated. Also supports Number System 1 |
* encoding by entering a 7-digit article number stating with the digit 1. |
* |
* <p> |
* EAN-2 and EAN-5 add-on symbols can be added using the '+' character followed by the add-on data. |
* |
* @author <a href="mailto:jakel2006@me.com">Robert Elliott</a> |
*/ |
public class Upc extends Symbol { |
public static enum Mode { |
UPCA, UPCE |
}; |
private static final String[] SET_AC = { "3211", "2221", "2122", "1411", "1132", "1231", "1114", "1312", "1213", "3112" }; |
private static final String[] SET_B = { "1123", "1222", "2212", "1141", "2311", "1321", "4111", "2131", "3121", "2113" }; |
/* Number set for UPC-E symbol (EN Table 4) */ |
private static final String[] UPC_PARITY_0 = { "BBBAAA", "BBABAA", "BBAABA", "BBAAAB", "BABBAA", "BAABBA", "BAAABB", "BABABA", "BABAAB", "BAABAB" }; |
/* Not covered by BS EN 797 */ |
private static final String[] UPC_PARITY_1 = { "AAABBB", "AABABB", "AABBAB", "AABBBA", "ABAABB", "ABBAAB", "ABBBAA", "ABABAB", "ABABBA", "ABBABA" }; |
private Mode mode = Mode.UPCA; |
private boolean showCheckDigit = true; |
private int guardPatternExtraHeight = 5; |
private boolean linkageFlag; |
private EanUpcAddOn addOn; |
/** Creates a new instance. */ |
public Upc() { |
this.humanReadableAlignment = HumanReadableAlignment.JUSTIFY; |
} |
/** |
* Sets the UPC mode (UPC-A or UPC-E). The default is UPC-A. |
* |
* @param mode the UPC mode (UPC-A or UPC-E) |
*/ |
public void setMode(final Mode mode) { |
this.mode = mode; |
} |
/** |
* Returns the UPC mode (UPC-A or UPC-E). |
* |
* @return the UPC mode (UPC-A or UPC-E) |
*/ |
public Mode getMode() { |
return this.mode; |
} |
/** |
* Sets whether or not to show the check digit in the human-readable text. |
* |
* @param showCheckDigit whether or not to show the check digit in the human-readable text |
*/ |
public void setShowCheckDigit(final boolean showCheckDigit) { |
this.showCheckDigit = showCheckDigit; |
} |
/** |
* Returns whether or not to show the check digit in the human-readable text. |
* |
* @return whether or not to show the check digit in the human-readable text |
*/ |
public boolean getShowCheckDigit() { |
return this.showCheckDigit; |
} |
/** |
* Sets the extra height used for the guard patterns. The default value is <code>5</code>. |
* |
* @param guardPatternExtraHeight the extra height used for the guard patterns |
*/ |
public void setGuardPatternExtraHeight(final int guardPatternExtraHeight) { |
this.guardPatternExtraHeight = guardPatternExtraHeight; |
} |
/** |
* Returns the extra height used for the guard patterns. |
* |
* @return the extra height used for the guard patterns |
*/ |
public int getGuardPatternExtraHeight() { |
return this.guardPatternExtraHeight; |
} |
/** |
* Sets the linkage flag. If set to <code>true</code>, this symbol is part of a composite |
* symbol. |
* |
* @param linkageFlag the linkage flag |
*/ |
protected void setLinkageFlag(final boolean linkageFlag) { |
this.linkageFlag = linkageFlag; |
} |
@Override |
protected void encode() { |
separateContent(); |
if (this.content.isEmpty()) { |
throw new OkapiException("Missing UPC data"); |
} |
if (this.mode == Mode.UPCA) { |
upca(); |
} else { |
upce(); |
} |
} |
private void separateContent() { |
final int splitPoint = this.content.indexOf('+'); |
if (splitPoint == -1) { |
// there is no add-on data |
this.addOn = null; |
} else if (splitPoint == this.content.length() - 1) { |
// we found the add-on separator, but no add-on data |
throw new OkapiException("Invalid add-on data"); |
} else { |
// there is a '+' in the input data, use an add-on EAN2 or EAN5 |
this.addOn = new EanUpcAddOn(); |
this.addOn.font = this.font; |
this.addOn.fontName = this.fontName; |
this.addOn.fontSize = this.fontSize; |
this.addOn.humanReadableLocation = this.humanReadableLocation == NONE ? NONE : TOP; |
this.addOn.moduleWidth = this.moduleWidth; |
this.addOn.default_height = this.default_height + this.guardPatternExtraHeight - 8; |
this.addOn.setContent(this.content.substring(splitPoint + 1)); |
this.content = this.content.substring(0, splitPoint); |
} |
} |
private void upca() { |
this.content = validateAndPad(this.content, 11); |
final char check = calcDigit(this.content); |
infoLine("Check Digit: " + check); |
final String hrt = this.content + check; |
final StringBuilder dest = new StringBuilder("111"); |
for (int i = 0; i < 12; i++) { |
if (i == 6) { |
dest.append("11111"); |
} |
dest.append(SET_AC[hrt.charAt(i) - '0']); |
} |
dest.append("111"); |
this.readable = hrt; |
this.pattern = new String[] { dest.toString() }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
private void upce() { |
this.content = validateAndPad(this.content, 7); |
final String expanded = expandToEquivalentUpcA(this.content, true); |
infoLine("UPC-A Equivalent: " + expanded); |
final char check = calcDigit(expanded); |
infoLine("Check Digit: " + check); |
final String hrt = this.content + check; |
final int numberSystem = getNumberSystem(this.content); |
final String[] parityArray = numberSystem == 1 ? UPC_PARITY_1 : UPC_PARITY_0; |
final String parity = parityArray[check - '0']; |
final StringBuilder dest = new StringBuilder("111"); |
for (int i = 0; i < 6; i++) { |
if (parity.charAt(i) == 'A') { |
dest.append(SET_AC[this.content.charAt(i + 1) - '0']); |
} else { // B |
dest.append(SET_B[this.content.charAt(i + 1) - '0']); |
} |
} |
dest.append("111111"); |
this.readable = hrt; |
this.pattern = new String[] { dest.toString() }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
/** |
* Expands the zero-compressed UPC-E code to make a UPC-A equivalent (EN Table 5). |
* |
* @param content the UPC-E code to expand |
* @param validate whether or not to validate the input |
* @return the UPC-A equivalent of the specified UPC-E code |
*/ |
protected String expandToEquivalentUpcA(final String content, final boolean validate) { |
final char[] upce = content.toCharArray(); |
final char[] upca = new char[11]; |
Arrays.fill(upca, '0'); |
upca[0] = upce[0]; |
upca[1] = upce[1]; |
upca[2] = upce[2]; |
final char emode = upce[6]; |
switch (emode) { |
case '0': |
case '1': |
case '2': |
upca[3] = emode; |
upca[8] = upce[3]; |
upca[9] = upce[4]; |
upca[10] = upce[5]; |
break; |
case '3': |
upca[3] = upce[3]; |
upca[9] = upce[4]; |
upca[10] = upce[5]; |
if (validate && (upce[3] == '0' || upce[3] == '1' || upce[3] == '2')) { |
/* Note 1 - "X3 shall not be equal to 0, 1 or 2" */ |
throw new OkapiException("Invalid UPC-E data"); |
} |
break; |
case '4': |
upca[3] = upce[3]; |
upca[4] = upce[4]; |
upca[10] = upce[5]; |
if (validate && upce[4] == '0') { |
/* Note 2 - "X4 shall not be equal to 0" */ |
throw new OkapiException("Invalid UPC-E data"); |
} |
break; |
default: |
upca[3] = upce[3]; |
upca[4] = upce[4]; |
upca[5] = upce[5]; |
upca[10] = emode; |
if (validate && upce[5] == '0') { |
/* Note 3 - "X5 shall not be equal to 0" */ |
throw new OkapiException("Invalid UPC-E data"); |
} |
break; |
} |
return new String(upca); |
} |
/** Two number systems can be used: system 0 and system 1. */ |
private static int getNumberSystem(final String content) { |
switch (content.charAt(0)) { |
case '0': |
return 0; |
case '1': |
return 1; |
default: |
throw new OkapiException("Invalid input data"); |
} |
} |
@Override |
protected void plotSymbol() { |
int xBlock; |
int x, y, w, h; |
boolean black = true; |
final int compositeOffset = this.linkageFlag ? 6 : 0; // space for composite separator above |
final int hrtOffset = this.humanReadableLocation == TOP ? getTheoreticalHumanReadableHeight() : 0; // space |
// for |
// HRT |
// above |
this.rectangles.clear(); |
this.texts.clear(); |
x = 0; |
/* Draw the bars in the symbology */ |
for (xBlock = 0; xBlock < this.pattern[0].length(); xBlock++) { |
w = this.pattern[0].charAt(xBlock) - '0'; |
if (black) { |
y = 0; |
h = this.default_height; |
/* Add extension to guide bars */ |
if (this.mode == Mode.UPCA) { |
if (x < 10 || x > 84 || x > 45 && x < 49) { |
h += this.guardPatternExtraHeight; |
} |
if (this.linkageFlag && (x == 0 || x == 94)) { |
h += 2; |
y -= 2; |
} |
} else { |
if (x < 4 || x > 45) { |
h += this.guardPatternExtraHeight; |
} |
if (this.linkageFlag && (x == 0 || x == 50)) { |
h += 2; |
y -= 2; |
} |
} |
final Rectangle2D.Double rect = new Rectangle2D.Double(scale(x), y + compositeOffset + hrtOffset, scale(w), h); |
this.rectangles.add(rect); |
this.symbol_width = Math.max(this.symbol_width, (int) rect.getMaxX()); |
this.symbol_height = Math.max(this.symbol_height, (int) rect.getHeight()); |
} |
black = !black; |
x += w; |
} |
/* Add separator for composite symbology, if necessary */ |
if (this.linkageFlag) { |
if (this.mode == Mode.UPCA) { |
this.rectangles.add(new Rectangle2D.Double(scale(0), 0, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(94), 0, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(-1), 2, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(95), 2, scale(1), 2)); |
} else { // UPCE |
this.rectangles.add(new Rectangle2D.Double(scale(0), 0, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(50), 0, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(-1), 2, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(51), 2, scale(1), 2)); |
} |
this.symbol_height += 4; |
} |
/* Now add the text */ |
if (this.humanReadableLocation == BOTTOM) { |
this.symbol_height -= this.guardPatternExtraHeight; |
final double baseline = this.symbol_height + this.fontSize; |
if (this.mode == Mode.UPCA) { |
this.texts.add(new TextBox(scale(-9), baseline, scale(4), this.readable.substring(0, 1), HumanReadableAlignment.RIGHT)); |
this.texts.add(new TextBox(scale(12), baseline, scale(32), this.readable.substring(1, 6), this.humanReadableAlignment)); |
this.texts.add(new TextBox(scale(51), baseline, scale(32), this.readable.substring(6, 11), this.humanReadableAlignment)); |
if (this.showCheckDigit) { |
this.texts.add(new TextBox(scale(97), baseline, scale(4), this.readable.substring(11, 12), HumanReadableAlignment.LEFT)); |
} |
} else { // UPCE |
this.texts.add(new TextBox(scale(-9), baseline, scale(4), this.readable.substring(0, 1), HumanReadableAlignment.RIGHT)); |
this.texts.add(new TextBox(scale(5), baseline, scale(39), this.readable.substring(1, 7), this.humanReadableAlignment)); |
if (this.showCheckDigit) { |
this.texts.add(new TextBox(scale(53), baseline, scale(4), this.readable.substring(7, 8), HumanReadableAlignment.LEFT)); |
} |
} |
} else if (this.humanReadableLocation == TOP) { |
final double baseline = this.fontSize; |
final int width = this.mode == Mode.UPCA ? 94 : 50; |
this.texts.add(new TextBox(scale(0), baseline, scale(width), this.readable, this.humanReadableAlignment)); |
} |
/* Now add the add-on symbol, if necessary */ |
if (this.addOn != null) { |
final int gap = 9; |
final int baseX = this.symbol_width + scale(gap); |
final Rectangle2D.Double r1 = this.rectangles.get(0); |
final Rectangle2D.Double ar1 = this.addOn.rectangles.get(0); |
final int baseY = (int) (r1.y + r1.getHeight() - ar1.y - ar1.getHeight()); |
for (final TextBox t : this.addOn.getTexts()) { |
this.texts.add(new TextBox(baseX + t.x, baseY + t.y, t.width, t.text, t.alignment)); |
} |
for (final Rectangle2D.Double r : this.addOn.getRectangles()) { |
this.rectangles.add(new Rectangle2D.Double(baseX + r.x, baseY + r.y, r.width, r.height)); |
} |
this.symbol_width += scale(gap) + this.addOn.symbol_width; |
this.pattern[0] = this.pattern[0] + gap + this.addOn.pattern[0]; |
} |
} |
/** Scales the specified width or x-dimension according to the current module width. */ |
private int scale(final int w) { |
return this.moduleWidth * w; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/UspsOneCode.java |
---|
New file |
0,0 → 1,458 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.backend.HumanReadableLocation.NONE; |
import static uk.org.okapibarcode.backend.HumanReadableLocation.TOP; |
import java.awt.geom.Rectangle2D; |
import java.math.BigInteger; |
/** |
* <p> |
* Implements USPS OneCode (also known as Intelligent Mail Barcode) according to USPS-B-3200F. |
* |
* <p> |
* OneCode is a fixed length (65-bar) symbol which combines routing and customer information in a |
* single symbol. Input data consists of a 20 digit tracking code, followed by a dash (-), followed |
* by a delivery point ZIP code which can be 0, 5, 9 or 11 digits in length. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
* @see <a href= |
* "https://ribbs.usps.gov/intelligentmail_mailpieces/documents/tech_guides/SPUSPSG.pdf">USPS |
* OneCode Specification</a> |
*/ |
public class UspsOneCode extends Symbol { |
/* The following lookup tables were generated using the code in Appendix C */ |
/** Appendix D Table 1 - 5 of 13 characters */ |
private static final int[] APPX_D_I = { 0x001F, 0x1F00, 0x002F, 0x1E80, 0x0037, 0x1D80, 0x003B, 0x1B80, 0x003D, 0x1780, 0x003E, 0x0F80, 0x004F, 0x1E40, 0x0057, 0x1D40, 0x005B, 0x1B40, 0x005D, |
0x1740, 0x005E, 0x0F40, 0x0067, 0x1CC0, 0x006B, 0x1AC0, 0x006D, 0x16C0, 0x006E, 0x0EC0, 0x0073, 0x19C0, 0x0075, 0x15C0, 0x0076, 0x0DC0, 0x0079, 0x13C0, 0x007A, 0x0BC0, 0x007C, 0x07C0, |
0x008F, 0x1E20, 0x0097, 0x1D20, 0x009B, 0x1B20, 0x009D, 0x1720, 0x009E, 0x0F20, 0x00A7, 0x1CA0, 0x00AB, 0x1AA0, 0x00AD, 0x16A0, 0x00AE, 0x0EA0, 0x00B3, 0x19A0, 0x00B5, 0x15A0, 0x00B6, |
0x0DA0, 0x00B9, 0x13A0, 0x00BA, 0x0BA0, 0x00BC, 0x07A0, 0x00C7, 0x1C60, 0x00CB, 0x1A60, 0x00CD, 0x1660, 0x00CE, 0x0E60, 0x00D3, 0x1960, 0x00D5, 0x1560, 0x00D6, 0x0D60, 0x00D9, 0x1360, |
0x00DA, 0x0B60, 0x00DC, 0x0760, 0x00E3, 0x18E0, 0x00E5, 0x14E0, 0x00E6, 0x0CE0, 0x00E9, 0x12E0, 0x00EA, 0x0AE0, 0x00EC, 0x06E0, 0x00F1, 0x11E0, 0x00F2, 0x09E0, 0x00F4, 0x05E0, 0x00F8, |
0x03E0, 0x010F, 0x1E10, 0x0117, 0x1D10, 0x011B, 0x1B10, 0x011D, 0x1710, 0x011E, 0x0F10, 0x0127, 0x1C90, 0x012B, 0x1A90, 0x012D, 0x1690, 0x012E, 0x0E90, 0x0133, 0x1990, 0x0135, 0x1590, |
0x0136, 0x0D90, 0x0139, 0x1390, 0x013A, 0x0B90, 0x013C, 0x0790, 0x0147, 0x1C50, 0x014B, 0x1A50, 0x014D, 0x1650, 0x014E, 0x0E50, 0x0153, 0x1950, 0x0155, 0x1550, 0x0156, 0x0D50, 0x0159, |
0x1350, 0x015A, 0x0B50, 0x015C, 0x0750, 0x0163, 0x18D0, 0x0165, 0x14D0, 0x0166, 0x0CD0, 0x0169, 0x12D0, 0x016A, 0x0AD0, 0x016C, 0x06D0, 0x0171, 0x11D0, 0x0172, 0x09D0, 0x0174, 0x05D0, |
0x0178, 0x03D0, 0x0187, 0x1C30, 0x018B, 0x1A30, 0x018D, 0x1630, 0x018E, 0x0E30, 0x0193, 0x1930, 0x0195, 0x1530, 0x0196, 0x0D30, 0x0199, 0x1330, 0x019A, 0x0B30, 0x019C, 0x0730, 0x01A3, |
0x18B0, 0x01A5, 0x14B0, 0x01A6, 0x0CB0, 0x01A9, 0x12B0, 0x01AA, 0x0AB0, 0x01AC, 0x06B0, 0x01B1, 0x11B0, 0x01B2, 0x09B0, 0x01B4, 0x05B0, 0x01B8, 0x03B0, 0x01C3, 0x1870, 0x01C5, 0x1470, |
0x01C6, 0x0C70, 0x01C9, 0x1270, 0x01CA, 0x0A70, 0x01CC, 0x0670, 0x01D1, 0x1170, 0x01D2, 0x0970, 0x01D4, 0x0570, 0x01D8, 0x0370, 0x01E1, 0x10F0, 0x01E2, 0x08F0, 0x01E4, 0x04F0, 0x01E8, |
0x02F0, 0x020F, 0x1E08, 0x0217, 0x1D08, 0x021B, 0x1B08, 0x021D, 0x1708, 0x021E, 0x0F08, 0x0227, 0x1C88, 0x022B, 0x1A88, 0x022D, 0x1688, 0x022E, 0x0E88, 0x0233, 0x1988, 0x0235, 0x1588, |
0x0236, 0x0D88, 0x0239, 0x1388, 0x023A, 0x0B88, 0x023C, 0x0788, 0x0247, 0x1C48, 0x024B, 0x1A48, 0x024D, 0x1648, 0x024E, 0x0E48, 0x0253, 0x1948, 0x0255, 0x1548, 0x0256, 0x0D48, 0x0259, |
0x1348, 0x025A, 0x0B48, 0x025C, 0x0748, 0x0263, 0x18C8, 0x0265, 0x14C8, 0x0266, 0x0CC8, 0x0269, 0x12C8, 0x026A, 0x0AC8, 0x026C, 0x06C8, 0x0271, 0x11C8, 0x0272, 0x09C8, 0x0274, 0x05C8, |
0x0278, 0x03C8, 0x0287, 0x1C28, 0x028B, 0x1A28, 0x028D, 0x1628, 0x028E, 0x0E28, 0x0293, 0x1928, 0x0295, 0x1528, 0x0296, 0x0D28, 0x0299, 0x1328, 0x029A, 0x0B28, 0x029C, 0x0728, 0x02A3, |
0x18A8, 0x02A5, 0x14A8, 0x02A6, 0x0CA8, 0x02A9, 0x12A8, 0x02AA, 0x0AA8, 0x02AC, 0x06A8, 0x02B1, 0x11A8, 0x02B2, 0x09A8, 0x02B4, 0x05A8, 0x02B8, 0x03A8, 0x02C3, 0x1868, 0x02C5, 0x1468, |
0x02C6, 0x0C68, 0x02C9, 0x1268, 0x02CA, 0x0A68, 0x02CC, 0x0668, 0x02D1, 0x1168, 0x02D2, 0x0968, 0x02D4, 0x0568, 0x02D8, 0x0368, 0x02E1, 0x10E8, 0x02E2, 0x08E8, 0x02E4, 0x04E8, 0x0307, |
0x1C18, 0x030B, 0x1A18, 0x030D, 0x1618, 0x030E, 0x0E18, 0x0313, 0x1918, 0x0315, 0x1518, 0x0316, 0x0D18, 0x0319, 0x1318, 0x031A, 0x0B18, 0x031C, 0x0718, 0x0323, 0x1898, 0x0325, 0x1498, |
0x0326, 0x0C98, 0x0329, 0x1298, 0x032A, 0x0A98, 0x032C, 0x0698, 0x0331, 0x1198, 0x0332, 0x0998, 0x0334, 0x0598, 0x0338, 0x0398, 0x0343, 0x1858, 0x0345, 0x1458, 0x0346, 0x0C58, 0x0349, |
0x1258, 0x034A, 0x0A58, 0x034C, 0x0658, 0x0351, 0x1158, 0x0352, 0x0958, 0x0354, 0x0558, 0x0361, 0x10D8, 0x0362, 0x08D8, 0x0364, 0x04D8, 0x0383, 0x1838, 0x0385, 0x1438, 0x0386, 0x0C38, |
0x0389, 0x1238, 0x038A, 0x0A38, 0x038C, 0x0638, 0x0391, 0x1138, 0x0392, 0x0938, 0x0394, 0x0538, 0x03A1, 0x10B8, 0x03A2, 0x08B8, 0x03A4, 0x04B8, 0x03C1, 0x1078, 0x03C2, 0x0878, 0x03C4, |
0x0478, 0x040F, 0x1E04, 0x0417, 0x1D04, 0x041B, 0x1B04, 0x041D, 0x1704, 0x041E, 0x0F04, 0x0427, 0x1C84, 0x042B, 0x1A84, 0x042D, 0x1684, 0x042E, 0x0E84, 0x0433, 0x1984, 0x0435, 0x1584, |
0x0436, 0x0D84, 0x0439, 0x1384, 0x043A, 0x0B84, 0x043C, 0x0784, 0x0447, 0x1C44, 0x044B, 0x1A44, 0x044D, 0x1644, 0x044E, 0x0E44, 0x0453, 0x1944, 0x0455, 0x1544, 0x0456, 0x0D44, 0x0459, |
0x1344, 0x045A, 0x0B44, 0x045C, 0x0744, 0x0463, 0x18C4, 0x0465, 0x14C4, 0x0466, 0x0CC4, 0x0469, 0x12C4, 0x046A, 0x0AC4, 0x046C, 0x06C4, 0x0471, 0x11C4, 0x0472, 0x09C4, 0x0474, 0x05C4, |
0x0487, 0x1C24, 0x048B, 0x1A24, 0x048D, 0x1624, 0x048E, 0x0E24, 0x0493, 0x1924, 0x0495, 0x1524, 0x0496, 0x0D24, 0x0499, 0x1324, 0x049A, 0x0B24, 0x049C, 0x0724, 0x04A3, 0x18A4, 0x04A5, |
0x14A4, 0x04A6, 0x0CA4, 0x04A9, 0x12A4, 0x04AA, 0x0AA4, 0x04AC, 0x06A4, 0x04B1, 0x11A4, 0x04B2, 0x09A4, 0x04B4, 0x05A4, 0x04C3, 0x1864, 0x04C5, 0x1464, 0x04C6, 0x0C64, 0x04C9, 0x1264, |
0x04CA, 0x0A64, 0x04CC, 0x0664, 0x04D1, 0x1164, 0x04D2, 0x0964, 0x04D4, 0x0564, 0x04E1, 0x10E4, 0x04E2, 0x08E4, 0x0507, 0x1C14, 0x050B, 0x1A14, 0x050D, 0x1614, 0x050E, 0x0E14, 0x0513, |
0x1914, 0x0515, 0x1514, 0x0516, 0x0D14, 0x0519, 0x1314, 0x051A, 0x0B14, 0x051C, 0x0714, 0x0523, 0x1894, 0x0525, 0x1494, 0x0526, 0x0C94, 0x0529, 0x1294, 0x052A, 0x0A94, 0x052C, 0x0694, |
0x0531, 0x1194, 0x0532, 0x0994, 0x0534, 0x0594, 0x0543, 0x1854, 0x0545, 0x1454, 0x0546, 0x0C54, 0x0549, 0x1254, 0x054A, 0x0A54, 0x054C, 0x0654, 0x0551, 0x1154, 0x0552, 0x0954, 0x0561, |
0x10D4, 0x0562, 0x08D4, 0x0583, 0x1834, 0x0585, 0x1434, 0x0586, 0x0C34, 0x0589, 0x1234, 0x058A, 0x0A34, 0x058C, 0x0634, 0x0591, 0x1134, 0x0592, 0x0934, 0x05A1, 0x10B4, 0x05A2, 0x08B4, |
0x05C1, 0x1074, 0x05C2, 0x0874, 0x0607, 0x1C0C, 0x060B, 0x1A0C, 0x060D, 0x160C, 0x060E, 0x0E0C, 0x0613, 0x190C, 0x0615, 0x150C, 0x0616, 0x0D0C, 0x0619, 0x130C, 0x061A, 0x0B0C, 0x061C, |
0x070C, 0x0623, 0x188C, 0x0625, 0x148C, 0x0626, 0x0C8C, 0x0629, 0x128C, 0x062A, 0x0A8C, 0x062C, 0x068C, 0x0631, 0x118C, 0x0632, 0x098C, 0x0643, 0x184C, 0x0645, 0x144C, 0x0646, 0x0C4C, |
0x0649, 0x124C, 0x064A, 0x0A4C, 0x0651, 0x114C, 0x0652, 0x094C, 0x0661, 0x10CC, 0x0662, 0x08CC, 0x0683, 0x182C, 0x0685, 0x142C, 0x0686, 0x0C2C, 0x0689, 0x122C, 0x068A, 0x0A2C, 0x0691, |
0x112C, 0x0692, 0x092C, 0x06A1, 0x10AC, 0x06A2, 0x08AC, 0x06C1, 0x106C, 0x06C2, 0x086C, 0x0703, 0x181C, 0x0705, 0x141C, 0x0706, 0x0C1C, 0x0709, 0x121C, 0x070A, 0x0A1C, 0x0711, 0x111C, |
0x0712, 0x091C, 0x0721, 0x109C, 0x0722, 0x089C, 0x0741, 0x105C, 0x0742, 0x085C, 0x0781, 0x103C, 0x0782, 0x083C, 0x080F, 0x1E02, 0x0817, 0x1D02, 0x081B, 0x1B02, 0x081D, 0x1702, 0x081E, |
0x0F02, 0x0827, 0x1C82, 0x082B, 0x1A82, 0x082D, 0x1682, 0x082E, 0x0E82, 0x0833, 0x1982, 0x0835, 0x1582, 0x0836, 0x0D82, 0x0839, 0x1382, 0x083A, 0x0B82, 0x0847, 0x1C42, 0x084B, 0x1A42, |
0x084D, 0x1642, 0x084E, 0x0E42, 0x0853, 0x1942, 0x0855, 0x1542, 0x0856, 0x0D42, 0x0859, 0x1342, 0x085A, 0x0B42, 0x0863, 0x18C2, 0x0865, 0x14C2, 0x0866, 0x0CC2, 0x0869, 0x12C2, 0x086A, |
0x0AC2, 0x0871, 0x11C2, 0x0872, 0x09C2, 0x0887, 0x1C22, 0x088B, 0x1A22, 0x088D, 0x1622, 0x088E, 0x0E22, 0x0893, 0x1922, 0x0895, 0x1522, 0x0896, 0x0D22, 0x0899, 0x1322, 0x089A, 0x0B22, |
0x08A3, 0x18A2, 0x08A5, 0x14A2, 0x08A6, 0x0CA2, 0x08A9, 0x12A2, 0x08AA, 0x0AA2, 0x08B1, 0x11A2, 0x08B2, 0x09A2, 0x08C3, 0x1862, 0x08C5, 0x1462, 0x08C6, 0x0C62, 0x08C9, 0x1262, 0x08CA, |
0x0A62, 0x08D1, 0x1162, 0x08D2, 0x0962, 0x08E1, 0x10E2, 0x0907, 0x1C12, 0x090B, 0x1A12, 0x090D, 0x1612, 0x090E, 0x0E12, 0x0913, 0x1912, 0x0915, 0x1512, 0x0916, 0x0D12, 0x0919, 0x1312, |
0x091A, 0x0B12, 0x0923, 0x1892, 0x0925, 0x1492, 0x0926, 0x0C92, 0x0929, 0x1292, 0x092A, 0x0A92, 0x0931, 0x1192, 0x0932, 0x0992, 0x0943, 0x1852, 0x0945, 0x1452, 0x0946, 0x0C52, 0x0949, |
0x1252, 0x094A, 0x0A52, 0x0951, 0x1152, 0x0961, 0x10D2, 0x0983, 0x1832, 0x0985, 0x1432, 0x0986, 0x0C32, 0x0989, 0x1232, 0x098A, 0x0A32, 0x0991, 0x1132, 0x09A1, 0x10B2, 0x09C1, 0x1072, |
0x0A07, 0x1C0A, 0x0A0B, 0x1A0A, 0x0A0D, 0x160A, 0x0A0E, 0x0E0A, 0x0A13, 0x190A, 0x0A15, 0x150A, 0x0A16, 0x0D0A, 0x0A19, 0x130A, 0x0A1A, 0x0B0A, 0x0A23, 0x188A, 0x0A25, 0x148A, 0x0A26, |
0x0C8A, 0x0A29, 0x128A, 0x0A2A, 0x0A8A, 0x0A31, 0x118A, 0x0A43, 0x184A, 0x0A45, 0x144A, 0x0A46, 0x0C4A, 0x0A49, 0x124A, 0x0A51, 0x114A, 0x0A61, 0x10CA, 0x0A83, 0x182A, 0x0A85, 0x142A, |
0x0A86, 0x0C2A, 0x0A89, 0x122A, 0x0A91, 0x112A, 0x0AA1, 0x10AA, 0x0AC1, 0x106A, 0x0B03, 0x181A, 0x0B05, 0x141A, 0x0B06, 0x0C1A, 0x0B09, 0x121A, 0x0B11, 0x111A, 0x0B21, 0x109A, 0x0B41, |
0x105A, 0x0B81, 0x103A, 0x0C07, 0x1C06, 0x0C0B, 0x1A06, 0x0C0D, 0x1606, 0x0C0E, 0x0E06, 0x0C13, 0x1906, 0x0C15, 0x1506, 0x0C16, 0x0D06, 0x0C19, 0x1306, 0x0C23, 0x1886, 0x0C25, 0x1486, |
0x0C26, 0x0C86, 0x0C29, 0x1286, 0x0C31, 0x1186, 0x0C43, 0x1846, 0x0C45, 0x1446, 0x0C49, 0x1246, 0x0C51, 0x1146, 0x0C61, 0x10C6, 0x0C83, 0x1826, 0x0C85, 0x1426, 0x0C89, 0x1226, 0x0C91, |
0x1126, 0x0CA1, 0x10A6, 0x0CC1, 0x1066, 0x0D03, 0x1816, 0x0D05, 0x1416, 0x0D09, 0x1216, 0x0D11, 0x1116, 0x0D21, 0x1096, 0x0D41, 0x1056, 0x0D81, 0x1036, 0x0E03, 0x180E, 0x0E05, 0x140E, |
0x0E09, 0x120E, 0x0E11, 0x110E, 0x0E21, 0x108E, 0x0E41, 0x104E, 0x0E81, 0x102E, 0x0F01, 0x101E, 0x100F, 0x1E01, 0x1017, 0x1D01, 0x101B, 0x1B01, 0x101D, 0x1701, 0x1027, 0x1C81, 0x102B, |
0x1A81, 0x102D, 0x1681, 0x1033, 0x1981, 0x1035, 0x1581, 0x1039, 0x1381, 0x1047, 0x1C41, 0x104B, 0x1A41, 0x104D, 0x1641, 0x1053, 0x1941, 0x1055, 0x1541, 0x1059, 0x1341, 0x1063, 0x18C1, |
0x1065, 0x14C1, 0x1069, 0x12C1, 0x1071, 0x11C1, 0x1087, 0x1C21, 0x108B, 0x1A21, 0x108D, 0x1621, 0x1093, 0x1921, 0x1095, 0x1521, 0x1099, 0x1321, 0x10A3, 0x18A1, 0x10A5, 0x14A1, 0x10A9, |
0x12A1, 0x10B1, 0x11A1, 0x10C3, 0x1861, 0x10C5, 0x1461, 0x10C9, 0x1261, 0x10D1, 0x1161, 0x1107, 0x1C11, 0x110B, 0x1A11, 0x110D, 0x1611, 0x1113, 0x1911, 0x1115, 0x1511, 0x1119, 0x1311, |
0x1123, 0x1891, 0x1125, 0x1491, 0x1129, 0x1291, 0x1131, 0x1191, 0x1143, 0x1851, 0x1145, 0x1451, 0x1149, 0x1251, 0x1183, 0x1831, 0x1185, 0x1431, 0x1189, 0x1231, 0x1207, 0x1C09, 0x120B, |
0x1A09, 0x120D, 0x1609, 0x1213, 0x1909, 0x1215, 0x1509, 0x1219, 0x1309, 0x1223, 0x1889, 0x1225, 0x1489, 0x1229, 0x1289, 0x1243, 0x1849, 0x1245, 0x1449, 0x1283, 0x1829, 0x1285, 0x1429, |
0x1303, 0x1819, 0x1305, 0x1419, 0x1407, 0x1C05, 0x140B, 0x1A05, 0x140D, 0x1605, 0x1413, 0x1905, 0x1415, 0x1505, 0x1423, 0x1885, 0x1425, 0x1485, 0x1443, 0x1845, 0x1483, 0x1825, 0x1503, |
0x1815, 0x1603, 0x180D, 0x1807, 0x1C03, 0x180B, 0x1A03, 0x1813, 0x1903, 0x1823, 0x1883, 0x1843, 0x1445, 0x1249, 0x1151, 0x10E1, 0x0C46, 0x0A4A, 0x0952, 0x08E2, 0x064C, 0x0554, 0x04E4, |
0x0358, 0x02E8, 0x01F0 }; |
/** Appendix D Table II - 2 of 13 characters */ |
private static final int[] APPX_D_II = { 0x0003, 0x1800, 0x0005, 0x1400, 0x0006, 0x0C00, 0x0009, 0x1200, 0x000A, 0x0A00, 0x000C, 0x0600, 0x0011, 0x1100, 0x0012, 0x0900, 0x0014, 0x0500, 0x0018, |
0x0300, 0x0021, 0x1080, 0x0022, 0x0880, 0x0024, 0x0480, 0x0028, 0x0280, 0x0030, 0x0180, 0x0041, 0x1040, 0x0042, 0x0840, 0x0044, 0x0440, 0x0048, 0x0240, 0x0050, 0x0140, 0x0060, 0x00C0, |
0x0081, 0x1020, 0x0082, 0x0820, 0x0084, 0x0420, 0x0088, 0x0220, 0x0090, 0x0120, 0x0101, 0x1010, 0x0102, 0x0810, 0x0104, 0x0410, 0x0108, 0x0210, 0x0201, 0x1008, 0x0202, 0x0808, 0x0204, |
0x0408, 0x0401, 0x1004, 0x0402, 0x0804, 0x0801, 0x1002, 0x1001, 0x0802, 0x0404, 0x0208, 0x0110, 0x00A0 }; |
/** Appendix D Table IV - Bar-to-Character Mapping (reverse lookup) */ |
private static final int[] APPX_D_IV = { 67, 6, 78, 16, 86, 95, 34, 40, 45, 113, 117, 121, 62, 87, 18, 104, 41, 76, 57, 119, 115, 72, 97, 2, 127, 26, 105, 35, 122, 52, 114, 7, 24, 82, 68, 63, 94, |
44, 77, 112, 70, 100, 39, 30, 107, 15, 125, 85, 10, 65, 54, 88, 20, 106, 46, 66, 8, 116, 29, 61, 99, 80, 90, 37, 123, 51, 25, 84, 129, 56, 4, 109, 96, 28, 36, 47, 11, 71, 33, 102, 21, 9, |
17, 49, 124, 79, 64, 91, 42, 69, 53, 60, 14, 1, 27, 103, 126, 75, 89, 50, 120, 19, 32, 110, 92, 111, 130, 59, 31, 12, 81, 43, 55, 5, 74, 22, 101, 128, 58, 118, 48, 108, 38, 98, 93, 23, 83, |
13, 73, 3 }; |
public UspsOneCode() { |
this.default_height = 8; |
this.humanReadableLocation = HumanReadableLocation.NONE; |
this.humanReadableAlignment = HumanReadableAlignment.LEFT; // spec section 2.4.2 |
} |
@Override |
protected void encode() { |
String zip = ""; |
String zip_adder; |
String tracker = ""; |
int i, j; |
final int length = this.content.length(); |
BigInteger accum; |
BigInteger x_reg; |
BigInteger mask; |
int usps_crc; |
final int[] codeword = new int[10]; |
final int[] characters = new int[10]; |
final boolean[] bar_map = new boolean[130]; |
char c; |
if (!this.content.matches("[0-9\u002D]+")) { |
throw new OkapiException("Invalid characters in input data"); |
} |
if (length > 32) { |
throw new OkapiException("Input too long"); |
} |
/* separate the tracking code from the routing code */ |
j = 0; |
for (i = 0; i < length; i++) { |
if (this.content.charAt(i) == '-') { |
j = 1; |
} else { |
if (j == 0) { |
/* reading tracker */ |
tracker += this.content.charAt(i); |
} else { |
/* reading zip code */ |
zip += this.content.charAt(i); |
} |
} |
} |
if (tracker.length() != 20) { |
throw new OkapiException("Invalid length tracking code"); |
} |
if (zip.length() > 11) { |
throw new OkapiException("Invalid ZIP code"); |
} |
/* *** Step 1 - Conversion of Data Fields into Binary Data *** */ |
/* Routing code first */ |
if (zip.length() > 0) { |
x_reg = new BigInteger(zip); |
} else { |
x_reg = new BigInteger("0"); |
} |
/* add weight to routing code */ |
if (zip.length() > 9) { |
zip_adder = "1000100001"; |
} else { |
if (zip.length() > 5) { |
zip_adder = "100001"; |
} else { |
if (zip.length() > 0) { |
zip_adder = "1"; |
} else { |
zip_adder = "0"; |
} |
} |
} |
accum = new BigInteger(zip_adder); |
accum = accum.add(x_reg); |
accum = accum.multiply(BigInteger.valueOf(10)); |
accum = accum.add(BigInteger.valueOf(Character.getNumericValue(tracker.charAt(0)))); |
accum = accum.multiply(BigInteger.valueOf(5)); |
accum = accum.add(BigInteger.valueOf(Character.getNumericValue(tracker.charAt(1)))); |
for (i = 2; i < tracker.length(); i++) { |
accum = accum.multiply(BigInteger.valueOf(10)); |
accum = accum.add(BigInteger.valueOf(Character.getNumericValue(tracker.charAt(i)))); |
} |
/* *** Step 2 - Generation of 11-bit CRC on Binary Data *** */ |
final int[] byte_array = new int[13]; |
for (i = 0; i < byte_array.length; i++) { |
mask = accum.shiftRight(96 - 8 * i); |
mask = mask.and(new BigInteger("255")); |
byte_array[i] = mask.intValue(); |
} |
usps_crc = USPS_MSB_Math_CRC11GenerateFrameCheckSequence(byte_array); |
/* *** Step 3 - Conversion from Binary Data to Codewords *** */ |
/* start with codeword J which is base 636 */ |
x_reg = accum.mod(BigInteger.valueOf(636)); |
codeword[9] = x_reg.intValue(); |
accum = accum.subtract(x_reg); |
accum = accum.divide(BigInteger.valueOf(636)); |
for (i = 8; i >= 0; i--) { |
x_reg = accum.mod(BigInteger.valueOf(1365)); |
codeword[i] = x_reg.intValue(); |
accum = accum.subtract(x_reg); |
accum = accum.divide(BigInteger.valueOf(1365)); |
} |
for (i = 0; i < 9; i++) { |
if (codeword[i] == 1365) { |
codeword[i] = 0; |
} |
} |
/* *** Step 4 - Inserting Additional Information into Codewords *** */ |
codeword[9] = codeword[9] * 2; |
if (usps_crc >= 1024) { |
codeword[0] += 659; |
} |
info("Codewords: "); |
for (i = 0; i < 10; i++) { |
infoSpace(codeword[i]); |
} |
infoLine(); |
/* *** Step 5 - Conversion from Codewords to Characters *** */ |
for (i = 0; i < 10; i++) { |
if (codeword[i] < 1287) { |
characters[i] = APPX_D_I[codeword[i]]; |
} else { |
characters[i] = APPX_D_II[codeword[i] - 1287]; |
} |
} |
for (i = 0; i < 10; i++) { |
if ((usps_crc & 1 << i) != 0) { |
characters[i] = 0x1FFF - characters[i]; |
} |
} |
/* *** Step 6 - Conversion from Characters to the Intelligent Mail Barcode *** */ |
for (i = 0; i < 10; i++) { |
for (j = 0; j < 13; j++) { |
if ((characters[i] & 1 << j) == 0) { |
bar_map[APPX_D_IV[13 * i + j] - 1] = false; |
} else { |
bar_map[APPX_D_IV[13 * i + j] - 1] = true; |
} |
} |
} |
this.readable = formatHumanReadableText(this.content); |
this.pattern = new String[1]; |
this.row_count = 1; |
this.row_height = new int[1]; |
this.row_height[0] = -1; |
this.pattern[0] = ""; |
for (i = 0; i < 65; i++) { |
c = 'T'; |
if (bar_map[i]) { |
c = 'D'; |
} |
if (bar_map[i + 65]) { |
c = 'A'; |
} |
if (bar_map[i] && bar_map[i + 65]) { |
c = 'F'; |
} |
this.pattern[0] += c; |
} |
infoLine("Encoding: " + this.pattern[0]); |
} |
private static int USPS_MSB_Math_CRC11GenerateFrameCheckSequence(final int[] bytes) { |
final int generatorPolynomial = 0x0F35; |
int frameCheckSequence = 0x07FF; |
int data; |
int byteIndex, bit; |
int byteArrayPtr = 0; |
/* Do most significant byte skipping the 2 most significant bits */ |
data = bytes[byteArrayPtr] << 5; |
byteArrayPtr++; |
for (bit = 2; bit < 8; bit++) { |
if (((frameCheckSequence ^ data) & 0x400) != 0) { |
frameCheckSequence = frameCheckSequence << 1 ^ generatorPolynomial; |
} else { |
frameCheckSequence = frameCheckSequence << 1; |
} |
frameCheckSequence &= 0x7FF; |
data <<= 1; |
} |
/* Do rest of the bytes */ |
for (byteIndex = 1; byteIndex < 13; byteIndex++) { |
data = bytes[byteArrayPtr] << 3; |
byteArrayPtr++; |
for (bit = 0; bit < 8; bit++) { |
if (((frameCheckSequence ^ data) & 0x0400) != 0) { |
frameCheckSequence = frameCheckSequence << 1 ^ generatorPolynomial; |
} else { |
frameCheckSequence = frameCheckSequence << 1; |
} |
frameCheckSequence &= 0x7FF; |
data <<= 1; |
} |
} |
return frameCheckSequence; |
} |
/** |
* <p> |
* Formats the barcode content into the correct human-readable format, per section 2.4.3 of the |
* spec: |
* |
* <p> |
* The human-readable information, when required, shall consist of the 20-digit tracking code |
* and the 5-, 9-, or 11-digit routing code, if present. The fields of the tracking code, as |
* defined in 2.1.3, shall be separated with a space added between data fields. When the barcode |
* contains a routing code, the 5-digit ZIP Code, the 4-digit add-on, and the remaining 2 digits |
* shall be separated with a space added between data fields. |
* |
* <p> |
* Appendix F contains a good overview of the different IMb constructs / formats. |
* |
* @param content the content to be formatted |
* @return the formatted content |
*/ |
protected static String formatHumanReadableText(final String content) { |
final StringBuilder hrt = new StringBuilder(50); |
boolean mid9 = false; // 9-digit mailer ID instead of 6-digit mailer ID |
boolean tracing = true; // STID indicates Origin IMb Tracing Services (050, 052) |
boolean pimb = true; // barcode identifier (BI) is 94, indicating pIMb |
boolean mpe5 = false; // if MPE = 5, it's a CFS/RFS variant of pIMb |
int i = 0; |
for (final char c : content.toCharArray()) { |
if (c < '0' || c > '9') { |
continue; |
} |
if (i == 5 && c == '9') { |
mid9 = true; |
} |
if (i == 2 && c != '0' || i == 3 && c != '5' || i == 4 && c != '0' && c != '2') { |
tracing = false; |
} |
if (i == 0 && c != '9' || i == 1 && c != '4') { |
pimb = false; |
} |
if (i == 5 && c == '5') { |
mpe5 = true; |
} |
if (i == 2 // BI -> STID |
|| i == 5 // STID -> ... |
|| i == 6 && pimb || i == 10 && pimb || i == 13 && pimb && !mpe5 || i == 15 && pimb && !mpe5 || i == 11 && !mid9 && !tracing && !pimb || i == 14 && mid9 && !tracing && !pimb |
|| i == 20 // ... -> zip-5 |
|| i == 25 // zip-5 -> zip-4 |
|| i == 29) { // zip-4 -> zip-2 |
hrt.append(' '); |
} |
hrt.append(c); |
i++; |
} |
return hrt.toString().trim(); |
} |
@Override |
protected void plotSymbol() { |
int xBlock, shortHeight, longHeight; |
double x, y, w, h; |
this.rectangles.clear(); |
this.texts.clear(); |
int baseY; |
if (this.humanReadableLocation == TOP) { |
baseY = getTheoreticalHumanReadableHeight(); |
} else { |
baseY = 0; |
} |
x = 0; |
w = this.moduleWidth; |
y = 0; |
h = 0; |
shortHeight = (int) (0.25 * this.default_height); |
longHeight = (int) (0.625 * this.default_height); |
for (xBlock = 0; xBlock < this.pattern[0].length(); xBlock++) { |
switch (this.pattern[0].charAt(xBlock)) { |
case 'A': |
y = baseY; |
h = longHeight; |
break; |
case 'D': |
y = baseY + this.default_height - longHeight; |
h = longHeight; |
break; |
case 'F': |
y = baseY; |
h = this.default_height; |
break; |
case 'T': |
y = baseY + this.default_height - longHeight; |
h = shortHeight; |
break; |
} |
final Rectangle2D.Double rect = new Rectangle2D.Double(x, y, w, h); |
this.rectangles.add(rect); |
x += 2.43 * w; |
} |
this.symbol_width = (int) Math.ceil((this.pattern[0].length() - 1) * 2.43 * w + w); // final |
// bar |
// doesn't |
// need |
// extra |
// whitespace |
this.symbol_height = this.default_height; |
if (this.humanReadableLocation != NONE && !this.readable.isEmpty()) { |
double baseline; |
if (this.humanReadableLocation == TOP) { |
baseline = this.fontSize; |
} else { |
baseline = this.symbol_height + this.fontSize; |
} |
this.texts.add(new TextBox(0, baseline, this.symbol_width, this.readable, this.humanReadableAlignment)); |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/CodablockF.java |
---|
New file |
0,0 → 1,869 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import java.awt.geom.Rectangle2D; |
import java.nio.charset.StandardCharsets; |
/** |
* <p> |
* Implements Codablock-F according to AIM Europe "Uniform Symbology Specification - Codablock F", |
* 1995. |
* |
* <p> |
* Codablock-F is a multi-row symbology using Code 128 encoding. It can encode any 8-bit ISO 8859-1 |
* (Latin-1) data up to approximately 1000 alpha-numeric characters or 2000 numeric digits in |
* length. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class CodablockF extends Symbol { |
private enum Mode { |
SHIFTA, LATCHA, SHIFTB, LATCHB, SHIFTC, LATCHC, AORB, ABORC, CANDB, CANDBB |
} |
private enum CfMode { |
MODEA, MODEB, MODEC |
} |
/* Annex A Table A.1 */ |
private static final String[] C_128_TABLE = { "212222", "222122", "222221", "121223", "121322", "131222", "122213", "122312", "132212", "221213", "221312", "231212", "112232", "122132", "122231", |
"113222", "123122", "123221", "223211", "221132", "221231", "213212", "223112", "312131", "311222", "321122", "321221", "312212", "322112", "322211", "212123", "212321", "232121", |
"111323", "131123", "131321", "112313", "132113", "132311", "211313", "231113", "231311", "112133", "112331", "132131", "113123", "113321", "133121", "313121", "211331", "231131", |
"213113", "213311", "213131", "311123", "311321", "331121", "312113", "312311", "332111", "314111", "221411", "431111", "111224", "111422", "121124", "121421", "141122", "141221", |
"112214", "112412", "122114", "122411", "142112", "142211", "241211", "221114", "413111", "241112", "134111", "111242", "121142", "121241", "114212", "124112", "124211", "411212", |
"421112", "421211", "212141", "214121", "412121", "111143", "111341", "131141", "114113", "114311", "411113", "411311", "113141", "114131", "311141", "411131", "211412", "211214", |
"211232", "2331112" }; |
private final int[][] blockmatrix = new int[44][62]; |
private int columns_needed; |
private int rows_needed; |
private CfMode final_mode; |
private final CfMode[] subset_selector = new CfMode[44]; |
/** |
* TODO: It doesn't appear that this symbol should support GS1 (it's not in the GS1 spec and |
* Zint doesn't support GS1 with this type of symbology). However, the code below does contain |
* GS1 checks, so we'll mark it as supported for now. It's very possible that the code below |
* which supports GS1 only does so because it was originally copied from the Code 128 source |
* code (just a suspicion, though). |
*/ |
@Override |
protected boolean gs1Supported() { |
return true; |
} |
@Override |
protected void encode() { |
int input_length, i, j, k; |
int min_module_height; |
Mode last_mode, this_mode; |
double estimate_codelength; |
String row_pattern; |
final int[] row_indicator = new int[44]; |
final int[] row_check = new int[44]; |
int k1_sum, k2_sum; |
int k1_check, k2_check; |
this.final_mode = CfMode.MODEA; |
if (!this.content.matches("[\u0000-\u00FF]+")) { |
throw new OkapiException("Invalid characters in input data"); |
} |
this.inputData = toBytes(this.content, StandardCharsets.ISO_8859_1, 0x00); |
input_length = this.inputData.length - 1; |
if (input_length > 5450) { |
throw new OkapiException("Input data too long"); |
} |
/* Make a guess at how many characters will be needed to encode the data */ |
estimate_codelength = 0.0; |
last_mode = Mode.AORB; /* Codablock always starts with Code A */ |
for (i = 0; i < input_length; i++) { |
this_mode = findSubset(this.inputData[i]); |
if (this_mode != last_mode) { |
estimate_codelength += 1.0; |
} |
if (this_mode != Mode.ABORC) { |
estimate_codelength += 1.0; |
} else { |
estimate_codelength += 0.5; |
} |
if (this.inputData[i] > 127) { |
estimate_codelength += 1.0; |
} |
last_mode = this_mode; |
} |
/* Decide symbol size based on the above guess */ |
this.rows_needed = (int) (0.5 + Math.sqrt((estimate_codelength + 2) / 1.45)); |
if (this.rows_needed < 2) { |
this.rows_needed = 2; |
} |
if (this.rows_needed > 44) { |
this.rows_needed = 44; |
} |
this.columns_needed = (int) (estimate_codelength + 2) / this.rows_needed; |
if (this.columns_needed < 4) { |
this.columns_needed = 4; |
} |
if (this.columns_needed > 62) { |
throw new OkapiException("Input data too long"); |
} |
/* Encode the data */ |
data_encode_blockf(); |
/* Add check digits - Annex F */ |
k1_sum = 0; |
k2_sum = 0; |
for (i = 0; i < input_length; i++) { |
if (this.inputData[i] == FNC1) { |
k1_sum += (i + 1) * 29; /* GS */ |
k2_sum += i * 29; |
} else { |
k1_sum += (i + 1) * this.inputData[i]; |
k2_sum += i * this.inputData[i]; |
} |
} |
k1_check = k1_sum % 86; |
k2_check = k2_sum % 86; |
if (this.final_mode == CfMode.MODEA || this.final_mode == CfMode.MODEB) { |
k1_check = k1_check + 64; |
if (k1_check > 95) { |
k1_check -= 96; |
} |
k2_check = k2_check + 64; |
if (k2_check > 95) { |
k2_check -= 96; |
} |
} |
this.blockmatrix[this.rows_needed - 1][this.columns_needed - 2] = k1_check; |
this.blockmatrix[this.rows_needed - 1][this.columns_needed - 1] = k2_check; |
/* Calculate row height (4.6.1.a) */ |
min_module_height = (int) (0.55 * (this.columns_needed + 3)) + 3; |
if (min_module_height < 8) { |
min_module_height = 8; |
} |
/* Encode the Row Indicator in the First Row of the Symbol - Table D2 */ |
if (this.subset_selector[0] == CfMode.MODEC) { |
/* Code C */ |
row_indicator[0] = this.rows_needed - 2; |
} else { |
/* Code A or B */ |
row_indicator[0] = this.rows_needed + 62; |
if (row_indicator[0] > 95) { |
row_indicator[0] -= 95; |
} |
} |
/* Encode the Row Indicator in the Second and Subsequent Rows of the Symbol - Table D3 */ |
for (i = 1; i < this.rows_needed; i++) { |
/* Note that the second row is row number 1 because counting starts from 0 */ |
if (this.subset_selector[i] == CfMode.MODEC) { |
/* Code C */ |
row_indicator[i] = i + 42; |
} else { |
/* Code A or B */ |
if (i < 6) { |
row_indicator[i] = i + 10; |
} else { |
row_indicator[i] = i + 20; |
} |
} |
} |
/* Calculate row check digits - Annex E */ |
for (i = 0; i < this.rows_needed; i++) { |
k = 103; |
switch (this.subset_selector[i]) { |
case MODEA: |
k += 98; |
break; |
case MODEB: |
k += 100; |
break; |
case MODEC: |
k += 99; |
break; |
} |
k += 2 * row_indicator[i]; |
for (j = 0; j < this.columns_needed; j++) { |
k += (j + 3) * this.blockmatrix[i][j]; |
} |
row_check[i] = k % 103; |
} |
this.readable = ""; |
this.row_count = this.rows_needed; |
this.pattern = new String[this.row_count]; |
this.row_height = new int[this.row_count]; |
infoLine("Grid Size: " + this.columns_needed + " X " + this.rows_needed); |
infoLine("K1 Check Digit: " + k1_check); |
infoLine("K2 Check Digit: " + k2_check); |
/* Resolve the data into patterns and place in symbol structure */ |
info("Encoding: "); |
for (i = 0; i < this.rows_needed; i++) { |
row_pattern = ""; |
/* Start character */ |
row_pattern += C_128_TABLE[103]; /* Always Start A */ |
switch (this.subset_selector[i]) { |
case MODEA: |
row_pattern += C_128_TABLE[98]; |
info("MODEA "); |
break; |
case MODEB: |
row_pattern += C_128_TABLE[100]; |
info("MODEB "); |
break; |
case MODEC: |
row_pattern += C_128_TABLE[99]; |
info("MODEC "); |
break; |
} |
row_pattern += C_128_TABLE[row_indicator[i]]; |
infoSpace(row_indicator[i]); |
for (j = 0; j < this.columns_needed; j++) { |
row_pattern += C_128_TABLE[this.blockmatrix[i][j]]; |
infoSpace(this.blockmatrix[i][j]); |
} |
row_pattern += C_128_TABLE[row_check[i]]; |
info("(" + row_check[i] + ") "); |
/* Stop character */ |
row_pattern += C_128_TABLE[106]; |
/* Write the information into the symbol */ |
this.pattern[i] = row_pattern; |
this.row_height[i] = 15; |
} |
infoLine(); |
this.symbol_height = this.rows_needed * 15; |
} |
private Mode findSubset(final int letter) { |
Mode mode; |
if (letter == FNC1) { |
mode = Mode.AORB; |
} else if (letter <= 31) { |
mode = Mode.SHIFTA; |
} else if (letter >= 48 && letter <= 57) { |
mode = Mode.ABORC; |
} else if (letter <= 95) { |
mode = Mode.AORB; |
} else if (letter <= 127) { |
mode = Mode.SHIFTB; |
} else if (letter <= 159) { |
mode = Mode.SHIFTA; |
} else if (letter <= 223) { |
mode = Mode.AORB; |
} else { |
mode = Mode.SHIFTB; |
} |
return mode; |
} |
private void data_encode_blockf() { |
int i, j, input_position, current_row; |
int column_position, c; |
CfMode current_mode; |
boolean done, exit_status; |
final int input_length = this.inputData.length - 1; |
exit_status = false; |
current_row = 0; |
current_mode = CfMode.MODEA; |
column_position = 0; |
input_position = 0; |
c = 0; |
do { |
done = false; |
/* |
* 'done' ensures that the instructions are followed in the correct order for each input |
* character |
*/ |
if (column_position == 0) { |
/* The Beginning of a row */ |
c = this.columns_needed; |
current_mode = character_subset_select(input_position); |
this.subset_selector[current_row] = current_mode; |
if (current_row == 0 && this.inputDataType == DataType.GS1) { |
/* Section 4.4.7.1 */ |
this.blockmatrix[current_row][column_position] = 102; /* FNC1 */ |
column_position++; |
c--; |
} |
} |
if (this.inputData[input_position] == FNC1) { |
this.blockmatrix[current_row][column_position] = 102; /* FNC1 */ |
column_position++; |
c--; |
input_position++; |
done = true; |
} |
if (!done) { |
if (c <= 2) { |
/* Annex B section 1 rule 1 */ |
/* |
* Ensure that there is sufficient encodation capacity to continue (using the |
* rules of Annex B.2). |
*/ |
switch (current_mode) { |
case MODEA: /* Table B1 applies */ |
if (findSubset(this.inputData[input_position]) == Mode.ABORC) { |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
done = true; |
} |
if (findSubset(this.inputData[input_position]) == Mode.SHIFTB && c == 1) { |
/* Needs two symbols */ |
this.blockmatrix[current_row][column_position] = 100; /* Code B */ |
column_position++; |
c--; |
done = true; |
} |
if (this.inputData[input_position] >= 244 && !done) { |
/* Needs three symbols */ |
this.blockmatrix[current_row][column_position] = 100; /* Code B */ |
column_position++; |
c--; |
if (c == 1) { |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
column_position++; |
c--; |
} |
done = true; |
} |
if (this.inputData[input_position] >= 128 && !done && c == 1) { |
/* Needs two symbols */ |
this.blockmatrix[current_row][column_position] = 100; /* Code B */ |
column_position++; |
c--; |
done = true; |
} |
break; |
case MODEB: /* Table B2 applies */ |
if (findSubset(this.inputData[input_position]) == Mode.ABORC) { |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
done = true; |
} |
if (findSubset(this.inputData[input_position]) == Mode.SHIFTA && c == 1) { |
/* Needs two symbols */ |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
column_position++; |
c--; |
done = true; |
} |
if (this.inputData[input_position] >= 128 && this.inputData[input_position] <= 159 && !done) { |
/* Needs three symbols */ |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
column_position++; |
c--; |
if (c == 1) { |
this.blockmatrix[current_row][column_position] = 100; /* Code B */ |
column_position++; |
c--; |
} |
done = true; |
} |
if (this.inputData[input_position] >= 160 && !done && c == 1) { |
/* Needs two symbols */ |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
column_position++; |
c--; |
done = true; |
} |
break; |
case MODEC: /* Table B3 applies */ |
if (findSubset(this.inputData[input_position]) != Mode.ABORC && c == 1) { |
/* Needs two symbols */ |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
column_position++; |
c--; |
done = true; |
} |
if (findSubset(this.inputData[input_position]) == Mode.ABORC && findSubset(this.inputData[input_position + 1]) != Mode.ABORC && c == 1) { |
/* Needs two symbols */ |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
column_position++; |
c--; |
done = true; |
} |
if (this.inputData[input_position] >= 128) { |
/* Needs three symbols */ |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
column_position++; |
c--; |
if (c == 1) { |
this.blockmatrix[current_row][column_position] = 100; /* Code B */ |
column_position++; |
c--; |
} |
} |
break; |
} |
} |
} |
if (!done) { |
if ((findSubset(this.inputData[input_position]) == Mode.AORB || findSubset(this.inputData[input_position]) == Mode.SHIFTA) && current_mode == CfMode.MODEA) { |
/* Annex B section 1 rule 2 */ |
/* |
* If in Code Subset A and the next data character can be encoded in Subset A |
* encode the next character. |
*/ |
if (this.inputData[input_position] >= 128) { |
/* Extended ASCII character */ |
this.blockmatrix[current_row][column_position] = 101; /* FNC4 */ |
column_position++; |
c--; |
} |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
done = true; |
} |
} |
if (!done) { |
if ((findSubset(this.inputData[input_position]) == Mode.AORB || findSubset(this.inputData[input_position]) == Mode.SHIFTB) && current_mode == CfMode.MODEB) { |
/* Annex B section 1 rule 3 */ |
/* |
* If in Code Subset B and the next data character can be encoded in subset B, |
* encode the next character. |
*/ |
if (this.inputData[input_position] >= 128) { |
/* Extended ASCII character */ |
this.blockmatrix[current_row][column_position] = 100; /* FNC4 */ |
column_position++; |
c--; |
} |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
done = true; |
} |
} |
if (!done) { |
if (findSubset(this.inputData[input_position]) == Mode.ABORC && findSubset(this.inputData[input_position + 1]) == Mode.ABORC && current_mode == CfMode.MODEC) { |
/* Annex B section 1 rule 4 */ |
/* If in Code Subset C and the next data are 2 digits, encode them. */ |
this.blockmatrix[current_row][column_position] = (this.inputData[input_position] - '0') * 10 + this.inputData[input_position + 1] - '0'; |
column_position++; |
c--; |
input_position += 2; |
done = true; |
} |
} |
if (!done) { |
if ((current_mode == CfMode.MODEA || current_mode == CfMode.MODEB) && (findSubset(this.inputData[input_position]) == Mode.ABORC || this.inputData[input_position] == FNC1)) { |
// Count the number of numeric digits |
// If 4 or more numeric data characters occur together when in subsets A or B: |
// a. If there is an even number of numeric data characters, insert a Code C |
// character before the |
// first numeric digit to change to subset C. |
// b. If there is an odd number of numeric data characters, insert a Code Set C |
// character immediately |
// after the first numeric digit to change to subset C. |
i = 0; |
j = 0; |
do { |
i++; |
if (this.inputData[input_position + j] == FNC1) { |
i++; |
} |
j++; |
} while (findSubset(this.inputData[input_position + j]) == Mode.ABORC || this.inputData[input_position + j] == FNC1); |
i--; |
if (i >= 4) { |
/* Annex B section 1 rule 5 */ |
if (i % 2 == 1) { |
/* Annex B section 1 rule 5a */ |
this.blockmatrix[current_row][column_position] = 99; /* Code C */ |
column_position++; |
c--; |
this.blockmatrix[current_row][column_position] = (this.inputData[input_position] - '0') * 10 + this.inputData[input_position + 1] - '0'; |
column_position++; |
c--; |
input_position += 2; |
current_mode = CfMode.MODEC; |
} else { |
/* Annex B section 1 rule 5b */ |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
} |
done = true; |
} else { |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
done = true; |
} |
} |
} |
if (!done) { |
if (current_mode == CfMode.MODEB && findSubset(this.inputData[input_position]) == Mode.SHIFTA) { |
/* Annex B section 1 rule 6 */ |
/* |
* When in subset B and an ASCII control character occurs in the data: a. If |
* there is a lower case character immediately following the control character, |
* insert a Shift character before the control character. b. Otherwise, insert a |
* Code A character before the control character to change to subset A. |
*/ |
if (this.inputData[input_position + 1] >= 96 && this.inputData[input_position + 1] <= 127) { |
/* Annex B section 1 rule 6a */ |
this.blockmatrix[current_row][column_position] = 98; /* Shift */ |
column_position++; |
c--; |
if (this.inputData[input_position] >= 128) { |
/* Extended ASCII character */ |
this.blockmatrix[current_row][column_position] = 100; /* FNC4 */ |
column_position++; |
c--; |
} |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
} else { |
/* Annex B section 1 rule 6b */ |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
column_position++; |
c--; |
if (this.inputData[input_position] >= 128) { |
/* Extended ASCII character */ |
this.blockmatrix[current_row][column_position] = 100; /* FNC4 */ |
column_position++; |
c--; |
} |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
current_mode = CfMode.MODEA; |
} |
done = true; |
} |
} |
if (!done) { |
if (current_mode == CfMode.MODEA && findSubset(this.inputData[input_position]) == Mode.SHIFTB) { |
/* Annex B section 1 rule 7 */ |
/* |
* When in subset A and a lower case character occurs in the data: a. If |
* following that character, a control character occurs in the data before the |
* occurrence of another lower case character, insert a Shift character before |
* the lower case character. b. Otherwise, insert a Code B character before the |
* lower case character to change to subset B. |
*/ |
if (findSubset(this.inputData[input_position + 1]) == Mode.SHIFTA && findSubset(this.inputData[input_position + 2]) == Mode.SHIFTB) { |
/* Annex B section 1 rule 7a */ |
this.blockmatrix[current_row][column_position] = 98; /* Shift */ |
column_position++; |
c--; |
if (this.inputData[input_position] >= 128) { |
/* Extended ASCII character */ |
this.blockmatrix[current_row][column_position] = 101; /* FNC4 */ |
column_position++; |
c--; |
} |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
} else { |
/* Annex B section 1 rule 7b */ |
this.blockmatrix[current_row][column_position] = 100; /* Code B */ |
column_position++; |
c--; |
if (this.inputData[input_position] >= 128) { |
/* Extended ASCII character */ |
this.blockmatrix[current_row][column_position] = 101; /* FNC4 */ |
column_position++; |
c--; |
} |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
current_mode = CfMode.MODEB; |
} |
done = true; |
} |
} |
if (!done) { |
if (current_mode == CfMode.MODEC && (findSubset(this.inputData[input_position]) != Mode.ABORC || findSubset(this.inputData[input_position + 1]) != Mode.ABORC)) { |
/* Annex B section 1 rule 8 */ |
/* |
* When in subset C and a non-numeric character (or a single digit) occurs in |
* the data, insert a Code A or Code B character before that character, |
* following rules 8a and 8b to determine between code subsets A and B. a. If an |
* ASCII control character (eg NUL) occurs in the data before any lower case |
* character, use Code A. b. Otherwise use Code B. |
*/ |
if (findSubset(this.inputData[input_position]) == Mode.SHIFTA) { |
/* Annex B section 1 rule 8a */ |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
column_position++; |
c--; |
if (this.inputData[input_position] >= 128) { |
/* Extended ASCII character */ |
this.blockmatrix[current_row][column_position] = 101; /* FNC4 */ |
column_position++; |
c--; |
} |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
current_mode = CfMode.MODEA; |
} else { |
/* Annex B section 1 rule 8b */ |
this.blockmatrix[current_row][column_position] = 100; /* Code B */ |
column_position++; |
c--; |
if (this.inputData[input_position] >= 128) { |
/* Extended ASCII character */ |
this.blockmatrix[current_row][column_position] = 100; /* FNC4 */ |
column_position++; |
c--; |
} |
this.blockmatrix[current_row][column_position] = a3_convert(this.inputData[input_position]); |
column_position++; |
c--; |
input_position++; |
current_mode = CfMode.MODEB; |
} |
done = true; |
} |
} |
if (input_position == input_length) { |
/* End of data - Annex B rule 5a */ |
if (c == 1) { |
if (current_mode == CfMode.MODEA) { |
this.blockmatrix[current_row][column_position] = 100; /* Code B */ |
current_mode = CfMode.MODEB; |
} else { |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
current_mode = CfMode.MODEA; |
} |
column_position++; |
c--; |
} |
if (c == 0) { |
/* Another row is needed */ |
column_position = 0; |
c = this.columns_needed; |
current_row++; |
this.subset_selector[current_row] = CfMode.MODEA; |
current_mode = CfMode.MODEA; |
} |
if (c > 2) { |
/* Fill up the last row */ |
do { |
if (current_mode == CfMode.MODEA) { |
this.blockmatrix[current_row][column_position] = 100; /* Code B */ |
current_mode = CfMode.MODEB; |
} else { |
this.blockmatrix[current_row][column_position] = 101; /* Code A */ |
current_mode = CfMode.MODEA; |
} |
column_position++; |
c--; |
} while (c > 2); |
} |
/* If (c == 2) { do nothing } */ |
exit_status = true; |
this.final_mode = current_mode; |
} else { |
if (c <= 0) { |
/* Start new row - Annex B rule 5b */ |
column_position = 0; |
current_row++; |
if (current_row > 43) { |
throw new OkapiException("Too many rows."); |
} |
} |
} |
} while (!exit_status); |
if (current_row == 0) { |
/* fill up the first row */ |
for (c = column_position; c <= this.columns_needed; c++) { |
if (current_mode == CfMode.MODEA) { |
this.blockmatrix[current_row][c] = 100; /* Code B */ |
current_mode = CfMode.MODEB; |
} else { |
this.blockmatrix[current_row][c] = 101; /* Code A */ |
current_mode = CfMode.MODEA; |
} |
} |
current_row++; |
/* add a second row */ |
this.subset_selector[current_row] = CfMode.MODEA; |
current_mode = CfMode.MODEA; |
for (c = 0; c <= this.columns_needed - 2; c++) { |
if (current_mode == CfMode.MODEA) { |
this.blockmatrix[current_row][c] = 100; /* Code B */ |
current_mode = CfMode.MODEB; |
} else { |
this.blockmatrix[current_row][c] = 101; /* Code A */ |
current_mode = CfMode.MODEA; |
} |
} |
} |
this.rows_needed = current_row + 1; |
} |
private CfMode character_subset_select(final int input_position) { |
/* Section 4.5.2 - Determining the Character Subset Selector in a Row */ |
if (this.inputData[input_position] >= '0' && this.inputData[input_position] <= '9') { |
/* Rule 1 */ |
return CfMode.MODEC; |
} |
if (this.inputData[input_position] >= 128 && this.inputData[input_position] <= 160) { |
/* Rule 2 (i) */ |
return CfMode.MODEA; |
} |
if (this.inputData[input_position] >= 0 && this.inputData[input_position] <= 31) { |
/* Rule 3 */ |
return CfMode.MODEA; |
} |
/* Rule 4 */ |
return CfMode.MODEB; |
} |
private int a3_convert(final int source) { |
/* Annex A section 3 */ |
if (source < 32) { |
return source + 64; |
} |
if (source >= 32 && source <= 127) { |
return source - 32; |
} |
if (source >= 128 && source <= 159) { |
return source - 128 + 64; |
} |
/* if source >= 160 */ |
return source - 128 - 32; |
} |
@Override |
protected void plotSymbol() { |
int xBlock, yBlock; |
int x, y, w, h; |
boolean black; |
this.rectangles.clear(); |
y = 1; |
h = 1; |
for (yBlock = 0; yBlock < this.row_count; yBlock++) { |
black = true; |
x = 0; |
for (xBlock = 0; xBlock < this.pattern[yBlock].length(); xBlock++) { |
if (black) { |
black = false; |
w = this.pattern[yBlock].charAt(xBlock) - '0'; |
if (this.row_height[yBlock] == -1) { |
h = this.default_height; |
} else { |
h = this.row_height[yBlock]; |
} |
if (w != 0 && h != 0) { |
final Rectangle2D.Double rect = new Rectangle2D.Double(x, y, w, h); |
this.rectangles.add(rect); |
} |
if (x + w > this.symbol_width) { |
this.symbol_width = x + w; |
} |
} else { |
black = true; |
} |
x += this.pattern[yBlock].charAt(xBlock) - '0'; |
} |
y += h; |
if (y > this.symbol_height) { |
this.symbol_height = y; |
} |
/* Add bars between rows */ |
if (yBlock != this.row_count - 1) { |
final Rectangle2D.Double rect = new Rectangle2D.Double(11, y - 1, this.symbol_width - 24, 2); |
this.rectangles.add(rect); |
} |
} |
/* Add top and bottom binding bars */ |
final Rectangle2D.Double top = new Rectangle2D.Double(0, 0, this.symbol_width, 2); |
this.rectangles.add(top); |
final Rectangle2D.Double bottom = new Rectangle2D.Double(0, y - 1, this.symbol_width, 2); |
this.rectangles.add(bottom); |
this.symbol_height += 1; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Hexagon.java |
---|
New file |
0,0 → 1,41 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
/** |
* Calculate a set of points to make a hexagon |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class Hexagon { |
private static final double INK_SPREAD = 1.25; |
private static final double[] OFFSET_X = { 0.0, 0.86, 0.86, 0.0, -0.86, -0.86 }; |
private static final double[] OFFSET_Y = { 1.0, 0.5, -0.5, -1.0, -0.5, 0.5 }; |
public final double centreX; |
public final double centreY; |
public final double[] pointX = new double[6]; |
public final double[] pointY = new double[6]; |
public Hexagon(final double centreX, final double centreY) { |
this.centreX = centreX; |
this.centreY = centreY; |
for (int i = 0; i < 6; i++) { |
this.pointX[i] = centreX + OFFSET_X[i] * INK_SPREAD; |
this.pointY[i] = centreY + OFFSET_Y[i] * INK_SPREAD; |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Code16k.java |
---|
New file |
0,0 → 1,779 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import java.awt.geom.Rectangle2D; |
import java.nio.charset.StandardCharsets; |
/** |
* <p> |
* Implements Code 16K symbology according to BS EN 12323:2005. |
* |
* <p> |
* Encodes using a stacked symbology based on Code 128. Supports encoding of any 8-bit ISO 8859-1 |
* (Latin-1) data with a maximum data capacity of 77 alpha-numeric characters or 154 numerical |
* digits. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class Code16k extends Symbol { |
private enum Mode { |
NULL, SHIFTA, LATCHA, SHIFTB, LATCHB, SHIFTC, LATCHC, AORB, ABORC, CANDB, CANDBB |
} |
/* EN 12323 Table 1 - "Code 16K" character encodations */ |
private static final String[] C16K_TABLE = { "212222", "222122", "222221", "121223", "121322", "131222", "122213", "122312", "132212", "221213", "221312", "231212", "112232", "122132", "122231", |
"113222", "123122", "123221", "223211", "221132", "221231", "213212", "223112", "312131", "311222", "321122", "321221", "312212", "322112", "322211", "212123", "212321", "232121", |
"111323", "131123", "131321", "112313", "132113", "132311", "211313", "231113", "231311", "112133", "112331", "132131", "113123", "113321", "133121", "313121", "211331", "231131", |
"213113", "213311", "213131", "311123", "311321", "331121", "312113", "312311", "332111", "314111", "221411", "431111", "111224", "111422", "121124", "121421", "141122", "141221", |
"112214", "112412", "122114", "122411", "142112", "142211", "241211", "221114", "413111", "241112", "134111", "111242", "121142", "121241", "114212", "124112", "124211", "411212", |
"421112", "421211", "212141", "214121", "412121", "111143", "111341", "131141", "114113", "114311", "411113", "411311", "113141", "114131", "311141", "411131", "211412", "211214", |
"211232", "211133" }; |
/* EN 12323 Table 3 and Table 4 - Start patterns and stop patterns */ |
private static final String[] C16K_START_STOP = { "3211", "2221", "2122", "1411", "1132", "1231", "1114", "3112" }; |
/* EN 12323 Table 5 - Start and stop values defining row numbers */ |
private static final int[] C16K_START_VALUES = { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7 }; |
private static final int[] C16K_STOP_VALUES = { 0, 1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 7, 0, 1, 2, 3 }; |
private final Mode[] block_mode = new Mode[170]; /* RENAME block_mode */ |
private final int[] block_length = new int[170]; /* RENAME block_length */ |
private int block_count; |
@Override |
protected boolean gs1Supported() { |
return true; |
} |
@Override |
protected void encode() { |
// TODO: is it possible to share any of this code with Code128, which is more up to date? |
String width_pattern; |
int current_row, rows_needed, first_check, second_check; |
int indexchaine, pads_needed; |
char[] set, fset; |
Mode mode; |
char last_set, current_set; |
int i, j, k, m, read; |
int[] values; |
int bar_characters; |
double glyph_count; |
int first_sum, second_sum; |
int input_length; |
int c_count; |
boolean f_state; |
if (!this.content.matches("[\u0000-\u00FF]+")) { |
throw new OkapiException("Invalid characters in input data"); |
} |
this.inputData = toBytes(this.content, StandardCharsets.ISO_8859_1); |
input_length = this.inputData.length; |
bar_characters = 0; |
set = new char[160]; |
fset = new char[160]; |
values = new int[160]; |
if (input_length > 157) { |
throw new OkapiException("Input too long"); |
} |
/* Detect extended ASCII characters */ |
for (i = 0; i < input_length; i++) { |
if (this.inputData[i] >= 128) { |
fset[i] = 'f'; |
} else { |
fset[i] = ' '; |
} |
} |
/* Decide when to latch to extended mode */ |
for (i = 0; i < input_length; i++) { |
j = 0; |
if (fset[i] == 'f') { |
do { |
j++; |
} while (fset[i + j] == 'f'); |
if (j >= 5 || j >= 3 && i + j == input_length - 1) { |
for (k = 0; k <= j; k++) { |
fset[i + k] = 'F'; |
} |
} |
} |
} |
/* Decide if it is worth reverting to 646 encodation for a few characters */ |
if (input_length > 1) { |
for (i = 1; i < input_length; i++) { |
if (fset[i - 1] == 'F' && fset[i] == ' ') { |
/* Detected a change from 8859-1 to 646 - count how long for */ |
for (j = 0; fset[i + j] == ' ' && i + j < input_length; j++) { |
; |
} |
if (j < 5 || j < 3 && i + j == input_length - 1) { |
/* Change to shifting back rather than latching back */ |
for (k = 0; k < j; k++) { |
fset[i + k] = 'n'; |
} |
} |
} |
} |
} |
/* Detect mode A, B and C characters */ |
this.block_count = 0; |
indexchaine = 0; |
mode = findSubset(this.inputData[indexchaine]); |
if (this.inputData[indexchaine] == FNC1) { |
mode = Mode.ABORC; |
} /* FNC1 */ |
for (i = 0; i < 160; i++) { |
this.block_length[i] = 0; |
} |
do { |
this.block_mode[this.block_count] = mode; |
while (this.block_mode[this.block_count] == mode && indexchaine < input_length) { |
this.block_length[this.block_count]++; |
indexchaine++; |
if (indexchaine < input_length) { |
mode = findSubset(this.inputData[indexchaine]); |
if (this.inputData[indexchaine] == FNC1) { |
mode = Mode.ABORC; |
} /* FNC1 */ |
} |
} |
this.block_count++; |
} while (indexchaine < input_length); |
reduceSubsetChanges(this.block_count); |
/* Put set data into set[] */ |
read = 0; |
for (i = 0; i < this.block_count; i++) { |
for (j = 0; j < this.block_length[i]; j++) { |
switch (this.block_mode[i]) { |
case SHIFTA: |
set[read] = 'a'; |
break; |
case LATCHA: |
set[read] = 'A'; |
break; |
case SHIFTB: |
set[read] = 'b'; |
break; |
case LATCHB: |
set[read] = 'B'; |
break; |
case LATCHC: |
set[read] = 'C'; |
break; |
} |
read++; |
} |
} |
/* Adjust for strings which start with shift characters - make them latch instead */ |
if (set[0] == 'a') { |
i = 0; |
do { |
set[i] = 'A'; |
i++; |
} while (set[i] == 'a'); |
} |
if (set[0] == 'b') { |
i = 0; |
do { |
set[i] = 'B'; |
i++; |
} while (set[i] == 'b'); |
} |
/* Watch out for odd-length Mode C blocks */ |
c_count = 0; |
for (i = 0; i < read; i++) { |
if (set[i] == 'C') { |
if (this.inputData[i] == FNC1) { |
if ((c_count & 1) != 0) { |
if (i - c_count != 0) { |
set[i - c_count] = 'B'; |
} else { |
set[i - 1] = 'B'; |
} |
} |
c_count = 0; |
} else { |
c_count++; |
} |
} else { |
if ((c_count & 1) != 0) { |
if (i - c_count != 0) { |
set[i - c_count] = 'B'; |
} else { |
set[i - 1] = 'B'; |
} |
} |
c_count = 0; |
} |
} |
if ((c_count & 1) != 0) { |
if (i - c_count != 0) { |
set[i - c_count] = 'B'; |
} else { |
set[i - 1] = 'B'; |
} |
} |
for (i = 1; i < read - 1; i++) { |
if (set[i] == 'C' && set[i - 1] == 'B' && set[i + 1] == 'B') { |
set[i] = 'B'; |
} |
} |
/* Make sure the data will fit in the symbol */ |
last_set = ' '; |
glyph_count = 0.0; |
for (i = 0; i < input_length; i++) { |
if (set[i] == 'a' || set[i] == 'b') { |
glyph_count = glyph_count + 1.0; |
} |
if (fset[i] == 'f' || fset[i] == 'n') { |
glyph_count = glyph_count + 1.0; |
} |
if (set[i] == 'A' || set[i] == 'B' || set[i] == 'C') { |
if (set[i] != last_set) { |
last_set = set[i]; |
glyph_count = glyph_count + 1.0; |
} |
} |
if (i == 0) { |
if (set[i] == 'B' && set[1] == 'C') { |
glyph_count = glyph_count - 1.0; |
} |
if (set[i] == 'B' && set[1] == 'B' && set[2] == 'C') { |
glyph_count = glyph_count - 1.0; |
} |
if (fset[i] == 'F') { |
glyph_count = glyph_count + 2.0; |
} |
} else { |
if (fset[i] == 'F' && fset[i - 1] != 'F') { |
glyph_count = glyph_count + 2.0; |
} |
if (fset[i] != 'F' && fset[i - 1] == 'F') { |
glyph_count = glyph_count + 2.0; |
} |
} |
if (set[i] == 'C' && this.inputData[i] != FNC1) { |
glyph_count = glyph_count + 0.5; |
} else { |
glyph_count = glyph_count + 1.0; |
} |
} |
if (this.inputDataType == DataType.GS1 && set[0] != 'A') { |
/* FNC1 can be integrated with mode character */ |
glyph_count--; |
} |
if (glyph_count > 77.0) { |
throw new OkapiException("Input too long"); |
} |
/* Calculate how tall the symbol will be */ |
glyph_count = glyph_count + 2.0; |
i = (int) glyph_count; |
rows_needed = i / 5; |
if (i % 5 > 0) { |
rows_needed++; |
} |
if (rows_needed == 1) { |
rows_needed = 2; |
} |
/* start with the mode character - Table 2 */ |
m = 0; |
switch (set[0]) { |
case 'A': |
m = 0; |
break; |
case 'B': |
m = 1; |
break; |
case 'C': |
m = 2; |
break; |
} |
if (this.readerInit) { |
if (m == 2) { |
m = 5; |
} |
if (this.inputDataType == DataType.GS1) { |
throw new OkapiException("Cannot use both GS1 mode and Reader Initialisation"); |
} else { |
if (set[0] == 'B' && set[1] == 'C') { |
m = 6; |
} |
} |
values[bar_characters] = 7 * (rows_needed - 2) + m; /* see 4.3.4.2 */ |
values[bar_characters + 1] = 96; /* FNC3 */ |
bar_characters += 2; |
} else { |
if (this.inputDataType == DataType.GS1) { |
/* Integrate FNC1 */ |
switch (set[0]) { |
case 'B': |
m = 3; |
break; |
case 'C': |
m = 4; |
break; |
} |
} else { |
if (set[0] == 'B' && set[1] == 'C') { |
m = 5; |
} |
if (set[0] == 'B' && set[1] == 'B' && set[2] == 'C') { |
m = 6; |
} |
} |
} |
values[bar_characters] = 7 * (rows_needed - 2) + m; /* see 4.3.4.2 */ |
bar_characters++; |
// } |
current_set = set[0]; |
f_state = false; |
/* |
* f_state remembers if we are in Extended ASCII mode (value 1) or in ISO/IEC 646 mode |
* (value 0) |
*/ |
if (fset[0] == 'F') { |
switch (current_set) { |
case 'A': |
values[bar_characters] = 101; |
values[bar_characters + 1] = 101; |
break; |
case 'B': |
values[bar_characters] = 100; |
values[bar_characters + 1] = 100; |
break; |
} |
bar_characters += 2; |
f_state = true; |
} |
read = 0; |
/* Encode the data */ |
do { |
if (read != 0 && set[read] != set[read - 1]) { /* Latch different code set */ |
switch (set[read]) { |
case 'A': |
values[bar_characters] = 101; |
bar_characters++; |
current_set = 'A'; |
break; |
case 'B': |
values[bar_characters] = 100; |
bar_characters++; |
current_set = 'B'; |
break; |
case 'C': |
if (!(read == 1 && set[0] == 'B')) { /* Not Mode C/Shift B */ |
if (!(read == 2 && set[0] == 'B' && set[1] == 'B')) { |
/* Not Mode C/Double Shift B */ |
values[bar_characters] = 99; |
bar_characters++; |
} |
} |
current_set = 'C'; |
break; |
} |
} |
if (read != 0) { |
if (fset[read] == 'F' && !f_state) { |
/* Latch beginning of extended mode */ |
switch (current_set) { |
case 'A': |
values[bar_characters] = 101; |
values[bar_characters + 1] = 101; |
break; |
case 'B': |
values[bar_characters] = 100; |
values[bar_characters + 1] = 100; |
break; |
} |
bar_characters += 2; |
f_state = true; |
} |
if (fset[read] == ' ' && f_state) { |
/* Latch end of extended mode */ |
switch (current_set) { |
case 'A': |
values[bar_characters] = 101; |
values[bar_characters + 1] = 101; |
break; |
case 'B': |
values[bar_characters] = 100; |
values[bar_characters + 1] = 100; |
break; |
} |
bar_characters += 2; |
f_state = false; |
} |
} |
if (fset[i] == 'f' || fset[i] == 'n') { |
/* Shift extended mode */ |
switch (current_set) { |
case 'A': |
values[bar_characters] = 101; /* FNC 4 */ |
break; |
case 'B': |
values[bar_characters] = 100; /* FNC 4 */ |
break; |
} |
bar_characters++; |
} |
if (set[i] == 'a' || set[i] == 'b') { |
/* Insert shift character */ |
values[bar_characters] = 98; |
bar_characters++; |
} |
if (this.inputData[read] != FNC1) { |
switch (set[read]) { /* Encode data characters */ |
case 'A': |
case 'a': |
getValueSubsetA(this.inputData[read], values, bar_characters); |
bar_characters++; |
read++; |
break; |
case 'B': |
case 'b': |
getValueSubsetB(this.inputData[read], values, bar_characters); |
bar_characters++; |
read++; |
break; |
case 'C': |
getValueSubsetC(this.inputData[read], this.inputData[read + 1], values, bar_characters); |
bar_characters++; |
read += 2; |
break; |
} |
} else { |
values[bar_characters] = 102; |
bar_characters++; |
read++; |
} |
} while (read < input_length); |
pads_needed = 5 - (bar_characters + 2) % 5; |
if (pads_needed == 5) { |
pads_needed = 0; |
} |
if (bar_characters + pads_needed < 8) { |
pads_needed += 8 - (bar_characters + pads_needed); |
} |
for (i = 0; i < pads_needed; i++) { |
values[bar_characters] = 106; |
bar_characters++; |
} |
/* Calculate check digits */ |
first_sum = 0; |
second_sum = 0; |
for (i = 0; i < bar_characters; i++) { |
first_sum += (i + 2) * values[i]; |
second_sum += (i + 1) * values[i]; |
} |
first_check = first_sum % 107; |
second_sum += first_check * (bar_characters + 1); |
second_check = second_sum % 107; |
values[bar_characters] = first_check; |
values[bar_characters + 1] = second_check; |
bar_characters += 2; |
this.readable = ""; |
this.pattern = new String[rows_needed]; |
this.row_count = rows_needed; |
this.row_height = new int[rows_needed]; |
infoLine("Symbol Rows: " + rows_needed); |
infoLine("First Check Digit: " + first_check); |
infoLine("Second Check Digit: " + second_check); |
info("Codewords: "); |
for (current_row = 0; current_row < rows_needed; current_row++) { |
width_pattern = ""; |
width_pattern += C16K_START_STOP[C16K_START_VALUES[current_row]]; |
width_pattern += "1"; |
for (i = 0; i < 5; i++) { |
width_pattern += C16K_TABLE[values[current_row * 5 + i]]; |
infoSpace(values[current_row * 5 + i]); |
} |
width_pattern += C16K_START_STOP[C16K_STOP_VALUES[current_row]]; |
this.pattern[current_row] = width_pattern; |
this.row_height[current_row] = 10; |
} |
infoLine(); |
} |
private void getValueSubsetA(final int source, final int[] values, final int bar_chars) { |
if (source > 127) { |
if (source < 160) { |
values[bar_chars] = source + 64 - 128; |
} else { |
values[bar_chars] = source - 32 - 128; |
} |
} else { |
if (source < 32) { |
values[bar_chars] = source + 64; |
} else { |
values[bar_chars] = source - 32; |
} |
} |
} |
private void getValueSubsetB(final int source, final int[] values, final int bar_chars) { |
if (source > 127) { |
values[bar_chars] = source - 32 - 128; |
} else { |
values[bar_chars] = source - 32; |
} |
} |
private void getValueSubsetC(final int source_a, final int source_b, final int[] values, final int bar_chars) { |
int weight; |
weight = 10 * Character.getNumericValue(source_a) + Character.getNumericValue(source_b); |
values[bar_chars] = weight; |
} |
private Mode findSubset(final int letter) { |
Mode mode; |
if (letter <= 31) { |
mode = Mode.SHIFTA; |
} else if (letter >= 48 && letter <= 57) { |
mode = Mode.ABORC; |
} else if (letter <= 95) { |
mode = Mode.AORB; |
} else if (letter <= 127) { |
mode = Mode.SHIFTB; |
} else if (letter <= 159) { |
mode = Mode.SHIFTA; |
} else if (letter <= 223) { |
mode = Mode.AORB; |
} else { |
mode = Mode.SHIFTB; |
} |
return mode; |
} |
private void reduceSubsetChanges( |
final int block_count) { /* Implements rules from ISO 15417 Annex E */ |
int i, length; |
Mode current, last, next; |
for (i = 0; i < block_count; i++) { |
current = this.block_mode[i]; |
length = this.block_length[i]; |
if (i != 0) { |
last = this.block_mode[i - 1]; |
} else { |
last = Mode.NULL; |
} |
if (i != block_count - 1) { |
next = this.block_mode[i + 1]; |
} else { |
next = Mode.NULL; |
} |
if (i == 0) { /* first block */ |
if (block_count == 1 && length == 2 && current == Mode.ABORC) { /* Rule 1a */ |
this.block_mode[i] = Mode.LATCHC; |
} |
if (current == Mode.ABORC) { |
if (length >= 4) { /* Rule 1b */ |
this.block_mode[i] = Mode.LATCHC; |
} else { |
this.block_mode[i] = Mode.AORB; |
current = Mode.AORB; |
} |
} |
if (current == Mode.SHIFTA) { /* Rule 1c */ |
this.block_mode[i] = Mode.LATCHA; |
} |
if (current == Mode.AORB && next == Mode.SHIFTA) { /* Rule 1c */ |
this.block_mode[i] = Mode.LATCHA; |
current = Mode.LATCHA; |
} |
if (current == Mode.AORB) { /* Rule 1d */ |
this.block_mode[i] = Mode.LATCHB; |
} |
} else { |
if (current == Mode.ABORC && length >= 4) { /* Rule 3 */ |
this.block_mode[i] = Mode.LATCHC; |
current = Mode.LATCHC; |
} |
if (current == Mode.ABORC) { |
this.block_mode[i] = Mode.AORB; |
current = Mode.AORB; |
} |
if (current == Mode.AORB && last == Mode.LATCHA) { |
this.block_mode[i] = Mode.LATCHA; |
current = Mode.LATCHA; |
} |
if (current == Mode.AORB && last == Mode.LATCHB) { |
this.block_mode[i] = Mode.LATCHB; |
current = Mode.LATCHB; |
} |
if (current == Mode.AORB && next == Mode.SHIFTA) { |
this.block_mode[i] = Mode.LATCHA; |
current = Mode.LATCHA; |
} |
if (current == Mode.AORB && next == Mode.SHIFTB) { |
this.block_mode[i] = Mode.LATCHB; |
current = Mode.LATCHB; |
} |
if (current == Mode.AORB) { |
this.block_mode[i] = Mode.LATCHB; |
current = Mode.LATCHB; |
} |
if (current == Mode.SHIFTA && length > 1) { /* Rule 4 */ |
this.block_mode[i] = Mode.LATCHA; |
current = Mode.LATCHA; |
} |
if (current == Mode.SHIFTB && length > 1) { /* Rule 5 */ |
this.block_mode[i] = Mode.LATCHB; |
current = Mode.LATCHB; |
} |
if (current == Mode.SHIFTA && last == Mode.LATCHA) { |
this.block_mode[i] = Mode.LATCHA; |
current = Mode.LATCHA; |
} |
if (current == Mode.SHIFTB && last == Mode.LATCHB) { |
this.block_mode[i] = Mode.LATCHB; |
current = Mode.LATCHB; |
} |
if (current == Mode.SHIFTA && last == Mode.LATCHC) { |
this.block_mode[i] = Mode.LATCHA; |
current = Mode.LATCHA; |
} |
if (current == Mode.SHIFTB && last == Mode.LATCHC) { |
this.block_mode[i] = Mode.LATCHB; |
current = Mode.LATCHB; |
} |
} /* Rule 2 is implimented elsewhere, Rule 6 is implied */ |
} |
combineSubsetBlocks(block_count); |
} |
private void combineSubsetBlocks(int block_count) { |
int i, j; |
/* bring together same type blocks */ |
if (block_count > 1) { |
i = 1; |
while (i < block_count) { |
if (this.block_mode[i - 1] == this.block_mode[i]) { |
/* bring together */ |
this.block_length[i - 1] = this.block_length[i - 1] + this.block_length[i]; |
j = i + 1; |
/* decreace the list */ |
while (j < block_count) { |
this.block_length[j - 1] = this.block_length[j]; |
this.block_mode[j - 1] = this.block_mode[j]; |
j++; |
} |
block_count = block_count - 1; |
i--; |
} |
i++; |
} |
} |
} |
@Override |
protected void plotSymbol() { |
int xBlock, yBlock; |
int x, y, w, h; |
boolean black; |
this.rectangles.clear(); |
y = 1; |
h = 1; |
for (yBlock = 0; yBlock < this.row_count; yBlock++) { |
black = true; |
x = 15; |
for (xBlock = 0; xBlock < this.pattern[yBlock].length(); xBlock++) { |
if (black) { |
black = false; |
w = this.pattern[yBlock].charAt(xBlock) - '0'; |
if (this.row_height[yBlock] == -1) { |
h = this.default_height; |
} else { |
h = this.row_height[yBlock]; |
} |
if (w != 0 && h != 0) { |
final Rectangle2D.Double rect = new Rectangle2D.Double(x, y, w, h); |
this.rectangles.add(rect); |
} |
if (x + w > this.symbol_width) { |
this.symbol_width = x + w; |
} |
} else { |
black = true; |
} |
x += this.pattern[yBlock].charAt(xBlock) - '0'; |
} |
y += h; |
if (y > this.symbol_height) { |
this.symbol_height = y; |
} |
/* Add bars between rows */ |
if (yBlock != this.row_count - 1) { |
final Rectangle2D.Double rect = new Rectangle2D.Double(15, y - 1, this.symbol_width - 15, 2); |
this.rectangles.add(rect); |
} |
} |
/* Add top and bottom binding bars */ |
final Rectangle2D.Double top = new Rectangle2D.Double(0, 0, this.symbol_width + 15, 2); |
this.rectangles.add(top); |
final Rectangle2D.Double bottom = new Rectangle2D.Double(0, y - 1, this.symbol_width + 15, 2); |
this.rectangles.add(bottom); |
this.symbol_width += 15; |
this.symbol_height += 1; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Pharmazentralnummer.java |
---|
New file |
0,0 → 1,76 |
/* |
* Copyright 2015 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
/** |
* PZN8 is a Code 39 based symbology used by the pharmaceutical industry in Germany. PZN8 encodes a |
* 7 digit number and includes a modulo-10 check digit. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class Pharmazentralnummer extends Symbol { |
/* |
* Pharmazentral Nummer is a Code 3 of 9 symbol with an extra check digit. Now generates PZN-8. |
*/ |
@Override |
protected void encode() { |
final int l = this.content.length(); |
String localstr; |
int zeroes, count = 0, check_digit; |
final Code3Of9 c = new Code3Of9(); |
if (l > 7) { |
throw new OkapiException("Input data too long"); |
} |
if (!this.content.matches("[0-9]+")) { |
throw new OkapiException("Invalid characters in input"); |
} |
localstr = "-"; |
zeroes = 7 - l + 1; |
for (int i = 1; i < zeroes; i++) { |
localstr += '0'; |
} |
localstr += this.content; |
for (int i = 1; i < 8; i++) { |
count += i * Character.getNumericValue(localstr.charAt(i)); |
} |
check_digit = count % 11; |
if (check_digit == 11) { |
check_digit = 0; |
} |
if (check_digit == 10) { |
throw new OkapiException("Not a valid PZN identifier"); |
} |
infoLine("Check Digit: " + check_digit); |
localstr += (char) (check_digit + '0'); |
c.setContent(localstr); |
this.readable = "PZN" + localstr; |
this.pattern = new String[1]; |
this.pattern[0] = c.pattern[0]; |
this.row_count = 1; |
this.row_height = new int[1]; |
this.row_height[0] = -1; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Code3Of9Extended.java |
---|
New file |
0,0 → 1,78 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
/** |
* <p> |
* Implements Code 3 of 9 Extended, also known as Code 39e and Code39+. |
* |
* <p> |
* Supports encoding of all characters in the 7-bit ASCII table. A modulo-43 check digit can be |
* added if required. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class Code3Of9Extended extends Symbol { |
private static final String[] E_CODE_39 = { "%U", "$A", "$B", "$C", "$D", "$E", "$F", "$G", "$H", "$I", "$J", "$K", "$L", "$M", "$N", "$O", "$P", "$Q", "$R", "$S", "$T", "$U", "$V", "$W", "$X", |
"$Y", "$Z", "%A", "%B", "%C", "%D", "%E", " ", "/A", "/B", "/C", "/D", "/E", "/F", "/G", "/H", "/I", "/J", "/K", "/L", "-", ".", "/O", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", |
"/Z", "%F", "%G", "%H", "%I", "%J", "%V", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "%K", "%L", |
"%M", "%N", "%O", "%W", "+A", "+B", "+C", "+D", "+E", "+F", "+G", "+H", "+I", "+J", "+K", "+L", "+M", "+N", "+O", "+P", "+Q", "+R", "+S", "+T", "+U", "+V", "+W", "+X", "+Y", "+Z", "%P", |
"%Q", "%R", "%S", "%T" }; |
public enum CheckDigit { |
NONE, MOD43 |
} |
private CheckDigit checkOption = CheckDigit.NONE; |
/** |
* Select addition of optional Modulo-43 check digit or encoding without check digit. |
* |
* @param checkMode check digit option |
*/ |
public void setCheckDigit(final CheckDigit checkMode) { |
this.checkOption = checkMode; |
} |
@Override |
protected void encode() { |
String buffer = ""; |
final int l = this.content.length(); |
int asciicode; |
final Code3Of9 c = new Code3Of9(); |
if (this.checkOption == CheckDigit.MOD43) { |
c.setCheckDigit(Code3Of9.CheckDigit.MOD43); |
} |
if (!this.content.matches("[\u0000-\u007F]+")) { |
throw new OkapiException("Invalid characters in input data"); |
} |
for (int i = 0; i < l; i++) { |
asciicode = this.content.charAt(i); |
buffer += E_CODE_39[asciicode]; |
} |
c.setContent(buffer); |
this.readable = this.content; |
this.pattern = new String[1]; |
this.pattern[0] = c.pattern[0]; |
this.row_count = 1; |
this.row_height = new int[1]; |
this.row_height[0] = -1; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/JapanPost.java |
---|
New file |
0,0 → 1,143 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.util.Arrays.positionOf; |
import java.awt.geom.Rectangle2D; |
import java.util.Locale; |
/** |
* <p> |
* Implements the Japanese Postal Code symbology as used to encode address data for mail items in |
* Japan. Valid input characters are digits 0-9, characters A-Z and the dash (-) character. A |
* modulo-19 check digit is added and should not be included in the input data. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class JapanPost extends Symbol { |
private static final String[] JAPAN_TABLE = { "FFT", "FDA", "DFA", "FAD", "FTF", "DAF", "AFD", "ADF", "TFF", "FTT", "TFT", "DAT", "DTA", "ADT", "TDA", "ATD", "TAD", "TTF", "FFF" }; |
private static final char[] KASUT_SET = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; |
private static final char[] CH_KASUT_SET = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; |
@Override |
protected void encode() { |
String dest; |
String inter; |
int i, sum, check; |
char c; |
this.content = this.content.toUpperCase(Locale.ENGLISH); |
if (!this.content.matches("[0-9A-Z\\-]+")) { |
throw new OkapiException("Invalid characters in data"); |
} |
inter = ""; |
for (i = 0; i < this.content.length() && inter.length() < 20; i++) { |
c = this.content.charAt(i); |
if (c >= '0' && c <= '9') { |
inter += c; |
} |
if (c == '-') { |
inter += c; |
} |
if (c >= 'A' && c <= 'J') { |
inter += 'a'; |
inter += CH_KASUT_SET[c - 'A']; |
} |
if (c >= 'K' && c <= 'O') { |
inter += 'b'; |
inter += CH_KASUT_SET[c - 'K']; |
} |
if (c >= 'U' && c <= 'Z') { |
inter += 'c'; |
inter += CH_KASUT_SET[c - 'U']; |
} |
} |
for (i = inter.length(); i < 20; i++) { |
inter += "d"; |
} |
dest = "FD"; |
sum = 0; |
for (i = 0; i < 20; i++) { |
dest += JAPAN_TABLE[positionOf(inter.charAt(i), KASUT_SET)]; |
sum += positionOf(inter.charAt(i), CH_KASUT_SET); |
} |
/* Calculate check digit */ |
check = 19 - sum % 19; |
if (check == 19) { |
check = 0; |
} |
dest += JAPAN_TABLE[positionOf(CH_KASUT_SET[check], KASUT_SET)]; |
dest += "DF"; |
infoLine("Encoding: " + dest); |
infoLine("Check Digit: " + check); |
this.readable = ""; |
this.pattern = new String[] { dest }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
@Override |
protected void plotSymbol() { |
int xBlock; |
int x, y, w, h; |
this.rectangles.clear(); |
x = 0; |
w = 1; |
y = 0; |
h = 0; |
for (xBlock = 0; xBlock < this.pattern[0].length(); xBlock++) { |
switch (this.pattern[0].charAt(xBlock)) { |
case 'A': |
y = 0; |
h = 5; |
break; |
case 'D': |
y = 3; |
h = 5; |
break; |
case 'F': |
y = 0; |
h = 8; |
break; |
case 'T': |
y = 3; |
h = 2; |
break; |
} |
final Rectangle2D.Double rect = new Rectangle2D.Double(x, y, w, h); |
this.rectangles.add(rect); |
x += 2; |
} |
this.symbol_width = this.pattern[0].length() * 3; |
this.symbol_height = 8; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Nve18.java |
---|
New file |
0,0 → 1,88 |
/* |
* Copyright 2015 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
/** |
* <p> |
* Calculate NVE-18 (Nummer der Versandeinheit), also known as SSCC-18 (Serial Shipping Container |
* Code). |
* |
* <p> |
* Encodes a 17-digit number, adding a modulo-10 check digit. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class Nve18 extends Symbol { |
@Override |
protected void encode() { |
String gs1Equivalent = ""; |
int zeroes; |
int count = 0; |
int c, cdigit; |
int p = 0; |
if (this.content.length() > 17) { |
throw new OkapiException("Input data too long"); |
} |
if (!this.content.matches("[0-9]+")) { |
throw new OkapiException("Invalid characters in input"); |
} |
// Add leading zeroes |
zeroes = 17 - this.content.length(); |
for (int i = 0; i < zeroes; i++) { |
gs1Equivalent += "0"; |
} |
gs1Equivalent += this.content; |
// Add Modulus-10 check digit |
for (int i = gs1Equivalent.length() - 1; i >= 0; i--) { |
c = Character.getNumericValue(gs1Equivalent.charAt(i)); |
if (p % 2 == 0) { |
c = c * 3; |
} |
count += c; |
p++; |
} |
cdigit = 10 - count % 10; |
if (cdigit == 10) { |
cdigit = 0; |
} |
infoLine("NVE Check Digit: " + cdigit); |
this.content = "[00]" + gs1Equivalent + cdigit; |
// Defer to Code 128 |
final Code128 code128 = new Code128(); |
code128.setDataType(DataType.GS1); |
code128.setHumanReadableLocation(this.humanReadableLocation); |
code128.setContent(this.content); |
this.readable = code128.readable; |
this.pattern = code128.pattern; |
this.row_count = code128.row_count; |
this.row_height = code128.row_height; |
this.symbol_height = code128.symbol_height; |
this.symbol_width = code128.symbol_width; |
this.rectangles = code128.rectangles; |
this.texts = code128.texts; |
info(code128.encodeInfo); |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/AztecCode.java |
---|
New file |
0,0 → 1,1824 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static java.nio.charset.StandardCharsets.US_ASCII; |
import static uk.org.okapibarcode.util.Arrays.insertArray; |
/** |
* <p> |
* Implements Aztec Code bar code symbology According to ISO/IEC 24778:2008. |
* |
* <p> |
* Aztec Code can encode 8-bit ISO 8859-1 (Latin-1) data (except 0x00 Null characters) up to a |
* maximum length of approximately 3800 numeric characters, 3000 alphabetic characters or 1900 bytes |
* of data in a two-dimensional matrix symbol. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class AztecCode extends Symbol { |
/* 27 x 27 data grid */ |
private static final int[] COMPACT_AZTEC_MAP = { 609, 608, 411, 413, 415, 417, 419, 421, 423, 425, 427, 429, 431, 433, 435, 437, 439, 441, 443, 445, 447, 449, 451, 453, 455, 457, 459, 607, 606, |
410, 412, 414, 416, 418, 420, 422, 424, 426, 428, 430, 432, 434, 436, 438, 440, 442, 444, 446, 448, 450, 452, 454, 456, 458, 605, 604, 409, 408, 243, 245, 247, 249, 251, 253, 255, 257, |
259, 261, 263, 265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 460, 461, 603, 602, 407, 406, 242, 244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 264, 266, 268, 270, 272, 274, 276, |
278, 280, 282, 462, 463, 601, 600, 405, 404, 241, 240, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 284, 285, 464, 465, 599, 598, 403, 402, 239, |
238, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 286, 287, 466, 467, 597, 596, 401, 400, 237, 236, 105, 104, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, |
23, 25, 27, 140, 141, 288, 289, 468, 469, 595, 594, 399, 398, 235, 234, 103, 102, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 142, 143, 290, 291, 470, 471, 593, 592, 397, 396, 233, |
232, 101, 100, 1, 1, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 0, 1, 28, 29, 144, 145, 292, 293, 472, 473, 591, 590, 395, 394, 231, 230, 99, 98, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 30, 31, |
146, 147, 294, 295, 474, 475, 589, 588, 393, 392, 229, 228, 97, 96, 2027, 1, 0, 0, 0, 0, 0, 0, 0, 1, 2007, 32, 33, 148, 149, 296, 297, 476, 477, 587, 586, 391, 390, 227, 226, 95, 94, 2026, |
1, 0, 1, 1, 1, 1, 1, 0, 1, 2008, 34, 35, 150, 151, 298, 299, 478, 479, 585, 584, 389, 388, 225, 224, 93, 92, 2025, 1, 0, 1, 0, 0, 0, 1, 0, 1, 2009, 36, 37, 152, 153, 300, 301, 480, 481, |
583, 582, 387, 386, 223, 222, 91, 90, 2024, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2010, 38, 39, 154, 155, 302, 303, 482, 483, 581, 580, 385, 384, 221, 220, 89, 88, 2023, 1, 0, 1, 0, 0, 0, 1, 0, 1, |
2011, 40, 41, 156, 157, 304, 305, 484, 485, 579, 578, 383, 382, 219, 218, 87, 86, 2022, 1, 0, 1, 1, 1, 1, 1, 0, 1, 2012, 42, 43, 158, 159, 306, 307, 486, 487, 577, 576, 381, 380, 217, 216, |
85, 84, 2021, 1, 0, 0, 0, 0, 0, 0, 0, 1, 2013, 44, 45, 160, 161, 308, 309, 488, 489, 575, 574, 379, 378, 215, 214, 83, 82, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 46, 47, 162, 163, 310, 311, 490, |
491, 573, 572, 377, 376, 213, 212, 81, 80, 0, 0, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 0, 0, 48, 49, 164, 165, 312, 313, 492, 493, 571, 570, 375, 374, 211, 210, 78, 76, 74, 72, 70, 68, |
66, 64, 62, 60, 58, 56, 54, 50, 51, 166, 167, 314, 315, 494, 495, 569, 568, 373, 372, 209, 208, 79, 77, 75, 73, 71, 69, 67, 65, 63, 61, 59, 57, 55, 52, 53, 168, 169, 316, 317, 496, 497, |
567, 566, 371, 370, 206, 204, 202, 200, 198, 196, 194, 192, 190, 188, 186, 184, 182, 180, 178, 176, 174, 170, 171, 318, 319, 498, 499, 565, 564, 369, 368, 207, 205, 203, 201, 199, 197, |
195, 193, 191, 189, 187, 185, 183, 181, 179, 177, 175, 172, 173, 320, 321, 500, 501, 563, 562, 366, 364, 362, 360, 358, 356, 354, 352, 350, 348, 346, 344, 342, 340, 338, 336, 334, 332, |
330, 328, 326, 322, 323, 502, 503, 561, 560, 367, 365, 363, 361, 359, 357, 355, 353, 351, 349, 347, 345, 343, 341, 339, 337, 335, 333, 331, 329, 327, 324, 325, 504, 505, 558, 556, 554, |
552, 550, 548, 546, 544, 542, 540, 538, 536, 534, 532, 530, 528, 526, 524, 522, 520, 518, 516, 514, 512, 510, 506, 507, 559, 557, 555, 553, 551, 549, 547, 545, 543, 541, 539, 537, 535, |
533, 531, 529, 527, 525, 523, 521, 519, 517, 515, 513, 511, 508, 509 }; |
private static final int[][] AZTEC_MAP = new int[151][151]; |
/* |
* From Table 2: |
* |
* 1 = upper 2 = lower 4 = mixed 8 = punctuation 16 = digits 32 = binary |
* |
* Values can be OR'ed, so e.g. 12 = 4 | 8, and 23 = 1 | 2 | 4 | 16 |
*/ |
private static final int[] AZTEC_CODE_SET = { 32, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 12, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 4, 4, 4, 4, 4, 23, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
24, 8, 24, 8, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8, 4, 8, 4, 4, 4, 2, 2, 2, 2, 2, 2, |
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 8, 4, 8, 4, 4 }; |
/* From Table 2 */ |
private static final int[] AZTEC_SYMBOL_CHAR = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 300, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 15, 16, 17, 18, 19, 1, 6, 7, 8, 9, 10, 11, 12, |
13, 14, 15, 16, 301, 18, 302, 20, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 21, 22, 23, 24, 25, 26, 20, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, |
27, 27, 21, 28, 22, 23, 24, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 25, 30, 26, 27 }; |
/* |
* Problem characters are: 300: Carriage Return (ASCII 13) 301: Comma (ASCII 44) 302: Full Stop |
* (ASCII 46) |
*/ |
private static final String[] PENTBIT = { "00000", "00001", "00010", "00011", "00100", "00101", "00110", "00111", "01000", "01001", "01010", "01011", "01100", "01101", "01110", "01111", "10000", |
"10001", "10010", "10011", "10100", "10101", "10110", "10111", "11000", "11001", "11010", "11011", "11100", "11101", "11110", "11111" }; |
private static final String[] QUADBIT = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; |
private static final String[] TRIBIT = { "000", "001", "010", "011", "100", "101", "110", "111" }; |
/* Codewords per symbol */ |
private static final int[] AZTEC_SIZES = { 21, 48, 60, 88, 120, 156, 196, 240, 230, 272, 316, 364, 416, 470, 528, 588, 652, 720, 790, 864, 940, 1020, 920, 992, 1066, 1144, 1224, 1306, 1392, 1480, |
1570, 1664 }; |
private static final int[] AZTEC_COMPACT_SIZES = { 17, 40, 51, 76 }; |
/* Data bits per symbol maximum with 10% error correction */ |
private static final int[] AZTEC_10_DATA_SIZES = { 96, 246, 408, 616, 840, 1104, 1392, 1704, 2040, 2420, 2820, 3250, 3720, 4200, 4730, 5270, 5840, 6450, 7080, 7750, 8430, 9150, 9900, 10680, 11484, |
12324, 13188, 14076, 15000, 15948, 16920, 17940 }; |
/* Data bits per symbol maximum with 23% error correction */ |
private static final int[] AZTEC_23_DATA_SIZES = { 84, 204, 352, 520, 720, 944, 1184, 1456, 1750, 2070, 2410, 2780, 3180, 3590, 4040, 4500, 5000, 5520, 6060, 6630, 7210, 7830, 8472, 9132, 9816, |
10536, 11280, 12036, 12828, 13644, 14472, 15348 }; |
/* Data bits per symbol maximum with 36% error correction */ |
private static final int[] AZTEC_36_DATA_SIZES = { 66, 168, 288, 432, 592, 776, 984, 1208, 1450, 1720, 2000, 2300, 2640, 2980, 3350, 3740, 4150, 4580, 5030, 5500, 5990, 6500, 7032, 7584, 8160, |
8760, 9372, 9996, 10656, 11340, 12024, 12744 }; |
/* Data bits per symbol maximum with 50% error correction */ |
private static final int[] AZTEC_50_DATA_SIZES = { 48, 126, 216, 328, 456, 600, 760, 936, 1120, 1330, 1550, 1790, 2050, 2320, 2610, 2910, 3230, 3570, 3920, 4290, 4670, 5070, 5484, 5916, 6360, |
6828, 7308, 7800, 8316, 8844, 9384, 9948 }; |
private static final int[] AZTEC_COMPACT_10_DATA_SIZES = { 78, 198, 336, 520 }; |
private static final int[] AZTEC_COMPACT_23_DATA_SIZES = { 66, 168, 288, 440 }; |
private static final int[] AZTEC_COMPACT_36_DATA_SIZES = { 48, 138, 232, 360 }; |
private static final int[] AZTEC_COMPACT_50_DATA_SIZES = { 36, 102, 176, 280 }; |
private static final int[] AZTEC_OFFSET = { 66, 64, 62, 60, 57, 55, 53, 51, 49, 47, 45, 42, 40, 38, 36, 34, 32, 30, 28, 25, 23, 21, 19, 17, 15, 13, 10, 8, 6, 4, 2, 0 }; |
private static final int[] AZTEC_COMPACT_OFFSET = { 6, 4, 2, 0 }; |
/* Initialize AZTEC_MAP */ |
static { |
int layer, start, length, n, i; |
int x, y; |
for (x = 0; x < 151; x++) { |
for (y = 0; y < 151; y++) { |
AZTEC_MAP[x][y] = 0; |
} |
} |
for (layer = 1; layer < 33; layer++) { |
start = 112 * (layer - 1) + 16 * (layer - 1) * (layer - 1) + 2; |
length = 28 + (layer - 1) * 4 + layer * 4; |
/* Top */ |
i = 0; |
x = 64 - (layer - 1) * 2; |
y = 63 - (layer - 1) * 2; |
for (n = start; n < start + length; n += 2) { |
AZTEC_MAP[avoidReferenceGrid(x + i)][avoidReferenceGrid(y)] = n; |
AZTEC_MAP[avoidReferenceGrid(x + i)][avoidReferenceGrid(y - 1)] = n + 1; |
i++; |
} |
/* Right */ |
i = 0; |
x = 78 + (layer - 1) * 2; |
y = 64 - (layer - 1) * 2; |
for (n = start + length; n < start + length * 2; n += 2) { |
AZTEC_MAP[avoidReferenceGrid(x)][avoidReferenceGrid(y + i)] = n; |
AZTEC_MAP[avoidReferenceGrid(x + 1)][avoidReferenceGrid(y + i)] = n + 1; |
i++; |
} |
/* Bottom */ |
i = 0; |
x = 77 + (layer - 1) * 2; |
y = 78 + (layer - 1) * 2; |
for (n = start + length * 2; n < start + length * 3; n += 2) { |
AZTEC_MAP[avoidReferenceGrid(x - i)][avoidReferenceGrid(y)] = n; |
AZTEC_MAP[avoidReferenceGrid(x - i)][avoidReferenceGrid(y + 1)] = n + 1; |
i++; |
} |
/* Left */ |
i = 0; |
x = 63 - (layer - 1) * 2; |
y = 77 + (layer - 1) * 2; |
for (n = start + length * 3; n < start + length * 4; n += 2) { |
AZTEC_MAP[avoidReferenceGrid(x)][avoidReferenceGrid(y - i)] = n; |
AZTEC_MAP[avoidReferenceGrid(x - 1)][avoidReferenceGrid(y - i)] = n + 1; |
i++; |
} |
} |
/* Central finder pattern */ |
for (y = 69; y <= 81; y++) { |
for (x = 69; x <= 81; x++) { |
AZTEC_MAP[x][y] = 1; |
} |
} |
for (y = 70; y <= 80; y++) { |
for (x = 70; x <= 80; x++) { |
AZTEC_MAP[x][y] = 0; |
} |
} |
for (y = 71; y <= 79; y++) { |
for (x = 71; x <= 79; x++) { |
AZTEC_MAP[x][y] = 1; |
} |
} |
for (y = 72; y <= 78; y++) { |
for (x = 72; x <= 78; x++) { |
AZTEC_MAP[x][y] = 0; |
} |
} |
for (y = 73; y <= 77; y++) { |
for (x = 73; x <= 77; x++) { |
AZTEC_MAP[x][y] = 1; |
} |
} |
for (y = 74; y <= 76; y++) { |
for (x = 74; x <= 76; x++) { |
AZTEC_MAP[x][y] = 0; |
} |
} |
/* Guide bars */ |
for (y = 11; y < 151; y += 16) { |
for (x = 1; x < 151; x += 2) { |
AZTEC_MAP[x][y] = 1; |
AZTEC_MAP[y][x] = 1; |
} |
} |
/* Descriptor */ |
for (i = 0; i < 10; i++) { /* Top */ |
AZTEC_MAP[avoidReferenceGrid(66 + i)][avoidReferenceGrid(64)] = 20000 + i; |
} |
for (i = 0; i < 10; i++) { /* Right */ |
AZTEC_MAP[avoidReferenceGrid(77)][avoidReferenceGrid(66 + i)] = 20010 + i; |
} |
for (i = 0; i < 10; i++) { /* Bottom */ |
AZTEC_MAP[avoidReferenceGrid(75 - i)][avoidReferenceGrid(77)] = 20020 + i; |
} |
for (i = 0; i < 10; i++) { /* Left */ |
AZTEC_MAP[avoidReferenceGrid(64)][avoidReferenceGrid(75 - i)] = 20030 + i; |
} |
/* Orientation */ |
AZTEC_MAP[avoidReferenceGrid(64)][avoidReferenceGrid(64)] = 1; |
AZTEC_MAP[avoidReferenceGrid(65)][avoidReferenceGrid(64)] = 1; |
AZTEC_MAP[avoidReferenceGrid(64)][avoidReferenceGrid(65)] = 1; |
AZTEC_MAP[avoidReferenceGrid(77)][avoidReferenceGrid(64)] = 1; |
AZTEC_MAP[avoidReferenceGrid(77)][avoidReferenceGrid(65)] = 1; |
AZTEC_MAP[avoidReferenceGrid(77)][avoidReferenceGrid(76)] = 1; |
} |
private static int avoidReferenceGrid(final int input) { |
int output = input; |
if (output > 10) { |
output++; |
} |
if (output > 26) { |
output++; |
} |
if (output > 42) { |
output++; |
} |
if (output > 58) { |
output++; |
} |
if (output > 74) { |
output++; |
} |
if (output > 90) { |
output++; |
} |
if (output > 106) { |
output++; |
} |
if (output > 122) { |
output++; |
} |
if (output > 138) { |
output++; |
} |
return output; |
} |
private int preferredSize = 0; |
private int preferredEccLevel = 2; |
private String structuredAppendMessageId; |
private int structuredAppendPosition = 1; |
private int structuredAppendTotal = 1; |
/** |
* <p> |
* Sets a preferred symbol size. This value may be ignored if data string is too large to fit in |
* the specified symbol size. Values correspond to symbol sizes as shown in the following table: |
* |
* <table summary="Available Aztec Code symbol sizes"> |
* <tbody> |
* <tr> |
* <th>Input</th> |
* <th>Symbol Size</th> |
* <th>Input</th> |
* <th>Symbol Size</th> |
* </tr> |
* <tr> |
* <td>1</td> |
* <td>15 x 15</td> |
* <td>19</td> |
* <td>79 x 79</td> |
* </tr> |
* <tr> |
* <td>2</td> |
* <td>19 x 19</td> |
* <td>20</td> |
* <td>83 x 83</td> |
* </tr> |
* <tr> |
* <td>3</td> |
* <td>23 x 23</td> |
* <td>21</td> |
* <td>87 x 87</td> |
* </tr> |
* <tr> |
* <td>4</td> |
* <td>27 x 27</td> |
* <td>22</td> |
* <td>91 x 91</td> |
* </tr> |
* <tr> |
* <td>5</td> |
* <td>19 x 19</td> |
* <td>23</td> |
* <td>95 x 95</td> |
* </tr> |
* <tr> |
* <td>6</td> |
* <td>23 x 23</td> |
* <td>24</td> |
* <td>101 x 101</td> |
* </tr> |
* <tr> |
* <td>7</td> |
* <td>27 x 27</td> |
* <td>25</td> |
* <td>105 x 105</td> |
* </tr> |
* <tr> |
* <td>8</td> |
* <td>31 x 31</td> |
* <td>26</td> |
* <td>109 x 109</td> |
* </tr> |
* <tr> |
* <td>9</td> |
* <td>37 x 37</td> |
* <td>27</td> |
* <td>113 x 113</td> |
* </tr> |
* <tr> |
* <td>10</td> |
* <td>41 x 41</td> |
* <td>28</td> |
* <td>117 x 117</td> |
* </tr> |
* <tr> |
* <td>11</td> |
* <td>45 x 45</td> |
* <td>29</td> |
* <td>121 x 121</td> |
* </tr> |
* <tr> |
* <td>12</td> |
* <td>49 x 49</td> |
* <td>30</td> |
* <td>125 x 125</td> |
* </tr> |
* <tr> |
* <td>13</td> |
* <td>53 x 53</td> |
* <td>31</td> |
* <td>131 x 131</td> |
* </tr> |
* <tr> |
* <td>14</td> |
* <td>57 x 57</td> |
* <td>32</td> |
* <td>135 x 135</td> |
* </tr> |
* <tr> |
* <td>15</td> |
* <td>61 x 61</td> |
* <td>33</td> |
* <td>139 x 139</td> |
* </tr> |
* <tr> |
* <td>16</td> |
* <td>67 x 67</td> |
* <td>34</td> |
* <td>143 x 143</td> |
* </tr> |
* <tr> |
* <td>17</td> |
* <td>71 x 71</td> |
* <td>35</td> |
* <td>147 x 147</td> |
* </tr> |
* <tr> |
* <td>18</td> |
* <td>75 x 75</td> |
* <td>36</td> |
* <td>151 x 151</td> |
* </tr> |
* </tbody> |
* </table> |
* |
* <p> |
* Note that sizes 1 to 4 are the "compact" Aztec Code symbols; sizes 5 to 36 are the |
* "full-range" Aztec Code symbols. |
* |
* @param size an integer in the range 1 - 36 |
*/ |
public void setPreferredSize(final int size) { |
if (size < 1 || size > 36) { |
throw new IllegalArgumentException("Invalid size: " + size); |
} |
this.preferredSize = size; |
} |
/** |
* Returns the preferred symbol size. |
* |
* @return the preferred symbol size |
*/ |
public int getPreferredSize() { |
return this.preferredSize; |
} |
/** |
* Sets the preferred minimum amount of symbol space dedicated to error correction. This value |
* will be ignored if a symbol size has been set by <code>setPreferredSize</code>. Valid options |
* are: |
* |
* <table summary="Error correction options"> |
* <tbody> |
* <tr> |
* <th>Mode</th> |
* <th>Error Correction Capacity</th> |
* </tr> |
* <tr> |
* <td>1</td> |
* <td>> 10% + 3 codewords</td> |
* </tr> |
* <tr> |
* <td>2</td> |
* <td>> 23% + 3 codewords</td> |
* </tr> |
* <tr> |
* <td>3</td> |
* <td>> 36% + 3 codewords</td> |
* </tr> |
* <tr> |
* <td>4</td> |
* <td>> 50% + 3 codewords</td> |
* </tr> |
* </tbody> |
* </table> |
* |
* @param eccLevel an integer in the range 1 - 4 |
*/ |
public void setPreferredEccLevel(final int eccLevel) { |
if (eccLevel < 1 || eccLevel > 4) { |
throw new IllegalArgumentException("Invalid ECC level: " + eccLevel); |
} |
this.preferredEccLevel = eccLevel; |
} |
/** |
* Returns the preferred error correction level. |
* |
* @return the preferred error correction level |
*/ |
public int getPreferredEccLevel() { |
return this.preferredEccLevel; |
} |
/** |
* If this Aztec Code symbol is part of a series of Aztec Code symbols appended in a structured |
* format, this method sets the position of this symbol in the series. Valid values are 1 |
* through 26 inclusive. |
* |
* @param position the position of this Aztec Code symbol in the structured append series |
*/ |
public void setStructuredAppendPosition(final int position) { |
if (position < 1 || position > 26) { |
throw new IllegalArgumentException("Invalid Aztec Code structured append position: " + position); |
} |
this.structuredAppendPosition = position; |
} |
/** |
* Returns the position of this Aztec Code symbol in a series of symbols using structured |
* append. If this symbol is not part of such a series, this method will return <code>1</code>. |
* |
* @return the position of this Aztec Code symbol in a series of symbols using structured append |
*/ |
public int getStructuredAppendPosition() { |
return this.structuredAppendPosition; |
} |
/** |
* If this Aztec Code symbol is part of a series of Aztec Code symbols appended in a structured |
* format, this method sets the total number of symbols in the series. Valid values are 1 |
* through 26 inclusive. A value of 1 indicates that this symbol is not part of a structured |
* append series. |
* |
* @param total the total number of Aztec Code symbols in the structured append series |
*/ |
public void setStructuredAppendTotal(final int total) { |
if (total < 1 || total > 26) { |
throw new IllegalArgumentException("Invalid Aztec Code structured append total: " + total); |
} |
this.structuredAppendTotal = total; |
} |
/** |
* Returns the size of the series of Aztec Code symbols using structured append that this symbol |
* is part of. If this symbol is not part of a structured append series, this method will return |
* <code>1</code>. |
* |
* @return size of the series that this symbol is part of |
*/ |
public int getStructuredAppendTotal() { |
return this.structuredAppendTotal; |
} |
/** |
* If this Aztec Code symbol is part of a series of Aztec Code symbols appended in a structured |
* format, this method sets the unique message ID for the series. Values may not contain spaces |
* and must contain only printable ASCII characters. Message IDs are optional. |
* |
* @param messageId the unique message ID for the series that this symbol is part of |
*/ |
public void setStructuredAppendMessageId(final String messageId) { |
if (messageId != null && !messageId.matches("^[\\x21-\\x7F]+$")) { |
throw new IllegalArgumentException("Invalid Aztec Code structured append message ID: " + messageId); |
} |
this.structuredAppendMessageId = messageId; |
} |
/** |
* Returns the unique message ID of the series of Aztec Code symbols using structured append |
* that this symbol is part of. If this symbol is not part of a structured append series, this |
* method will return <code>null</code>. |
* |
* @return the unique message ID for the series that this symbol is part of |
*/ |
public String getStructuredAppendMessageId() { |
return this.structuredAppendMessageId; |
} |
@Override |
protected boolean gs1Supported() { |
return true; |
} |
@Override |
protected void encode() { |
int layers; |
boolean compact; |
StringBuilder adjustedString; |
if (this.inputDataType == DataType.GS1 && this.readerInit) { |
throw new OkapiException("Cannot encode in GS1 and Reader Initialisation mode at the same time"); |
} |
eciProcess(); // Get ECI mode |
/* Optional structured append (Section 8 of spec) */ |
/* ML + UL start flag handled later, not part of data */ |
if (this.structuredAppendTotal != 1) { |
final StringBuilder prefix = new StringBuilder(); |
if (this.structuredAppendMessageId != null) { |
prefix.append(' ').append(this.structuredAppendMessageId).append(' '); |
} |
prefix.append((char) (this.structuredAppendPosition + 64)); // 1-26 as A-Z |
prefix.append((char) (this.structuredAppendTotal + 64)); // 1-26 as A-Z |
final int[] prefixArray = toBytes(prefix.toString(), US_ASCII); |
this.inputData = insertArray(this.inputData, 0, prefixArray); |
} |
final String binaryString = generateAztecBinary(); |
int dataLength = binaryString.length(); |
if (this.preferredSize == 0) { |
/* The size of the symbol can be determined by Okapi */ |
int dataMaxSize = 0; |
final int compLoop = this.readerInit ? 1 : 4; |
do { |
/* Decide what size symbol to use - the smallest that fits the data */ |
int[] dataSizes; |
int[] compactDataSizes; |
switch (this.preferredEccLevel) { |
/* |
* For each level of error correction work out the smallest symbol which the data |
* will fit in |
*/ |
case 1: |
dataSizes = AZTEC_10_DATA_SIZES; |
compactDataSizes = AZTEC_COMPACT_10_DATA_SIZES; |
break; |
case 2: |
dataSizes = AZTEC_23_DATA_SIZES; |
compactDataSizes = AZTEC_COMPACT_23_DATA_SIZES; |
break; |
case 3: |
dataSizes = AZTEC_36_DATA_SIZES; |
compactDataSizes = AZTEC_COMPACT_36_DATA_SIZES; |
break; |
case 4: |
dataSizes = AZTEC_50_DATA_SIZES; |
compactDataSizes = AZTEC_COMPACT_50_DATA_SIZES; |
break; |
default: |
throw new OkapiException("Unrecognized ECC level: " + this.preferredEccLevel); |
} |
layers = 0; |
compact = false; |
for (int i = 32; i > 0; i--) { |
if (dataLength < dataSizes[i - 1]) { |
layers = i; |
compact = false; |
dataMaxSize = dataSizes[i - 1]; |
} |
} |
for (int i = compLoop; i > 0; i--) { |
if (dataLength < compactDataSizes[i - 1]) { |
layers = i; |
compact = true; |
dataMaxSize = compactDataSizes[i - 1]; |
} |
} |
if (layers == 0) { |
/* Couldn't find a symbol which fits the data */ |
throw new OkapiException("Input too long (too many bits for selected ECC)"); |
} |
adjustedString = adjustBinaryString(binaryString, compact, layers); |
dataLength = adjustedString.length(); |
} while (dataLength > dataMaxSize); |
/* |
* This loop will only repeat on the rare occasions when the rule about not having all |
* 1s or all 0s means that the binary string has had to be lengthened beyond the maximum |
* number of bits that can be encoded in a symbol of the selected size |
*/ |
} else { |
/* The size of the symbol has been specified by the user */ |
if (this.preferredSize >= 1 && this.preferredSize <= 4) { |
compact = true; |
layers = this.preferredSize; |
} else { |
compact = false; |
layers = this.preferredSize - 4; |
} |
adjustedString = adjustBinaryString(binaryString, compact, layers); |
/* Check if the data actually fits into the selected symbol size */ |
final int codewordSize = getCodewordSize(layers); |
final int[] sizes = compact ? AZTEC_COMPACT_SIZES : AZTEC_SIZES; |
final int dataMaxSize = codewordSize * (sizes[layers - 1] - 3); |
if (adjustedString.length() > dataMaxSize) { |
throw new OkapiException("Data too long for specified Aztec Code symbol size"); |
} |
} |
if (this.readerInit && compact && layers > 1) { |
throw new OkapiException("Symbol is too large for reader initialization"); |
} |
if (this.readerInit && layers > 22) { |
throw new OkapiException("Symbol is too large for reader initialization"); |
} |
final int codewordSize = getCodewordSize(layers); |
final int dataBlocks = adjustedString.length() / codewordSize; |
int eccBlocks; |
if (compact) { |
eccBlocks = AZTEC_COMPACT_SIZES[layers - 1] - dataBlocks; |
} else { |
eccBlocks = AZTEC_SIZES[layers - 1] - dataBlocks; |
} |
infoLine("Compact Mode: " + compact); |
infoLine("Layers: " + layers); |
infoLine("Codeword Length: " + codewordSize + " bits"); |
infoLine("Data Codewords: " + dataBlocks); |
infoLine("ECC Codewords: " + eccBlocks); |
/* Add ECC data to the adjusted string */ |
addErrorCorrection(adjustedString, codewordSize, dataBlocks, eccBlocks); |
/* Invert the data so that actual data is on the outside and reed-solomon on the inside */ |
for (int i = 0; i < adjustedString.length() / 2; i++) { |
final int mirror = adjustedString.length() - i - 1; |
final char c = adjustedString.charAt(i); |
adjustedString.setCharAt(i, adjustedString.charAt(mirror)); |
adjustedString.setCharAt(mirror, c); |
} |
/* Create the descriptor / mode message */ |
final String descriptor = createDescriptor(compact, layers, dataBlocks); |
/* Plot all of the data into the symbol in pre-defined spiral pattern */ |
if (compact) { |
this.readable = ""; |
this.row_count = 27 - 2 * AZTEC_COMPACT_OFFSET[layers - 1]; |
this.row_height = new int[this.row_count]; |
this.row_height[0] = -1; |
this.pattern = new String[this.row_count]; |
for (int y = AZTEC_COMPACT_OFFSET[layers - 1]; y < 27 - AZTEC_COMPACT_OFFSET[layers - 1]; y++) { |
final StringBuilder bin = new StringBuilder(27); |
for (int x = AZTEC_COMPACT_OFFSET[layers - 1]; x < 27 - AZTEC_COMPACT_OFFSET[layers - 1]; x++) { |
final int j = COMPACT_AZTEC_MAP[y * 27 + x]; |
if (j == 0) { |
bin.append('0'); |
} |
if (j == 1) { |
bin.append('1'); |
} |
if (j >= 2) { |
if (j - 2 < adjustedString.length()) { |
bin.append(adjustedString.charAt(j - 2)); |
} else { |
if (j >= 2000) { |
bin.append(descriptor.charAt(j - 2000)); |
} else { |
bin.append('0'); |
} |
} |
} |
} |
this.row_height[y - AZTEC_COMPACT_OFFSET[layers - 1]] = 1; |
this.pattern[y - AZTEC_COMPACT_OFFSET[layers - 1]] = bin2pat(bin); |
} |
} else { |
this.readable = ""; |
this.row_count = 151 - 2 * AZTEC_OFFSET[layers - 1]; |
this.row_height = new int[this.row_count]; |
this.row_height[0] = -1; |
this.pattern = new String[this.row_count]; |
for (int y = AZTEC_OFFSET[layers - 1]; y < 151 - AZTEC_OFFSET[layers - 1]; y++) { |
final StringBuilder bin = new StringBuilder(151); |
for (int x = AZTEC_OFFSET[layers - 1]; x < 151 - AZTEC_OFFSET[layers - 1]; x++) { |
final int j = AZTEC_MAP[x][y]; |
if (j == 1) { |
bin.append('1'); |
} |
if (j == 0) { |
bin.append('0'); |
} |
if (j >= 2) { |
if (j - 2 < adjustedString.length()) { |
bin.append(adjustedString.charAt(j - 2)); |
} else { |
if (j >= 20000) { |
bin.append(descriptor.charAt(j - 20000)); |
} else { |
bin.append('0'); |
} |
} |
} |
} |
this.row_height[y - AZTEC_OFFSET[layers - 1]] = 1; |
this.pattern[y - AZTEC_OFFSET[layers - 1]] = bin2pat(bin); |
} |
} |
} |
private String generateAztecBinary() { |
/* Encode input data into a binary string */ |
int i, j, k, bytes; |
int curtable, newtable, lasttable, chartype, maplength, blocks; |
final int[] charmap = new int[2 * this.inputData.length]; |
final int[] typemap = new int[2 * this.inputData.length]; |
final int[] blockType = new int[this.inputData.length + 1]; |
final int[] blockLength = new int[this.inputData.length + 1]; |
/* Lookup input string in encoding table */ |
maplength = 0; |
/* Add FNC1 to beginning of GS1 messages */ |
if (this.inputDataType == DataType.GS1) { |
charmap[maplength] = 0; // FLG |
typemap[maplength++] = 8; // PUNC |
charmap[maplength] = 400; // (0) |
typemap[maplength++] = 8; // PUNC |
} |
if (this.eciMode != 3) { |
int flagNumber; |
charmap[maplength] = 0; // FLG |
typemap[maplength++] = 8; // PUNC |
flagNumber = 6; |
if (this.eciMode < 100000) { |
flagNumber = 5; |
} |
if (this.eciMode < 10000) { |
flagNumber = 4; |
} |
if (this.eciMode < 1000) { |
flagNumber = 3; |
} |
if (this.eciMode < 100) { |
flagNumber = 2; |
} |
if (this.eciMode < 10) { |
flagNumber = 1; |
} |
charmap[maplength] = 400 + flagNumber; |
typemap[maplength++] = 8; // PUNC |
} |
for (i = 0; i < this.inputData.length; i++) { |
if (this.inputData[i] == FNC1) { |
/* FNC1 represented by FLG(0) */ |
charmap[maplength] = 0; // FLG |
typemap[maplength++] = 8; // PUNC |
charmap[maplength] = 400; // (0) |
typemap[maplength++] = 8; // PUNC |
} else { |
if (this.inputData[i] > 0x7F || this.inputData[i] == 0x00) { |
charmap[maplength] = this.inputData[i]; |
typemap[maplength++] = 32; // BINARY |
} else { |
charmap[maplength] = AZTEC_SYMBOL_CHAR[this.inputData[i]]; |
typemap[maplength++] = AZTEC_CODE_SET[this.inputData[i]]; |
} |
} |
} |
/* Look for double character encoding possibilities */ |
for (i = 0; i < maplength - 1; i++) { |
if (charmap[i] == 300 && charmap[i + 1] == 11 && typemap[i] == 12 && typemap[i + 1] == 4) { |
/* CR LF combination */ |
charmap[i] = 2; |
typemap[i] = 8; // PUNC |
if (i + 1 != maplength) { |
for (j = i + 1; j < maplength; j++) { |
charmap[j] = charmap[j + 1]; |
typemap[j] = typemap[j + 1]; |
} |
} |
maplength--; |
} |
if (charmap[i] == 302 && charmap[i + 1] == 1 && typemap[i] == 24 && typemap[i + 1] == 23) { |
/* . SP combination */ |
charmap[i] = 3; |
typemap[i] = 8; // PUNC; |
if (i + 1 != maplength) { |
for (j = i + 1; j < maplength; j++) { |
charmap[j] = charmap[j + 1]; |
typemap[j] = typemap[j + 1]; |
} |
} |
maplength--; |
} |
if (charmap[i] == 301 && charmap[i + 1] == 1 && typemap[i] == 24 && typemap[i + 1] == 23) { |
/* , SP combination */ |
charmap[i] = 4; |
typemap[i] = 8; // PUNC; |
if (i + 1 != maplength) { |
for (j = i + 1; j < maplength; j++) { |
charmap[j] = charmap[j + 1]; |
typemap[j] = typemap[j + 1]; |
} |
} |
maplength--; |
} |
if (charmap[i] == 21 && charmap[i + 1] == 1 && typemap[i] == 8 && typemap[i + 1] == 23) { |
/* : SP combination */ |
charmap[i] = 5; |
typemap[i] = 8; // PUNC; |
if (i + 1 != maplength) { |
for (j = i + 1; j < maplength; j++) { |
charmap[j] = charmap[j + 1]; |
typemap[j] = typemap[j + 1]; |
} |
} |
maplength--; |
} |
} |
/* look for blocks of characters which use the same table */ |
blocks = 0; |
for (i = 0; i < maplength; i++) { |
if (i > 0 && typemap[i] == typemap[i - 1]) { |
blockLength[blocks - 1]++; |
} else { |
blocks++; |
blockType[blocks - 1] = typemap[i]; |
blockLength[blocks - 1] = 1; |
} |
} |
if ((blockType[0] & 1) != 0) { |
blockType[0] = 1; |
} |
if ((blockType[0] & 2) != 0) { |
blockType[0] = 2; |
} |
if ((blockType[0] & 4) != 0) { |
blockType[0] = 4; |
} |
if ((blockType[0] & 8) != 0) { |
blockType[0] = 8; |
} |
if (blocks > 1) { |
/* look for adjacent blocks which can use the same table (left to right search) */ |
for (i = 1; i < blocks; i++) { |
if ((blockType[i] & blockType[i - 1]) != 0) { |
blockType[i] = blockType[i] & blockType[i - 1]; |
} |
} |
if ((blockType[blocks - 1] & 1) != 0) { |
blockType[blocks - 1] = 1; |
} |
if ((blockType[blocks - 1] & 2) != 0) { |
blockType[blocks - 1] = 2; |
} |
if ((blockType[blocks - 1] & 4) != 0) { |
blockType[blocks - 1] = 4; |
} |
if ((blockType[blocks - 1] & 8) != 0) { |
blockType[blocks - 1] = 8; |
} |
/* look for adjacent blocks which can use the same table (right to left search) */ |
for (i = blocks - 2; i > 0; i--) { |
if ((blockType[i] & blockType[i + 1]) != 0) { |
blockType[i] = blockType[i] & blockType[i + 1]; |
} |
} |
/* determine the encoding table for characters which do not fit with adjacent blocks */ |
for (i = 1; i < blocks; i++) { |
if ((blockType[i] & 8) != 0) { |
blockType[i] = 8; |
} |
if ((blockType[i] & 4) != 0) { |
blockType[i] = 4; |
} |
if ((blockType[i] & 2) != 0) { |
blockType[i] = 2; |
} |
if ((blockType[i] & 1) != 0) { |
blockType[i] = 1; |
} |
} |
/* |
* if less than 4 characters are preceded and followed by binary blocks then it is more |
* efficient to also encode these in binary |
*/ |
// for (i = 1; i < blocks - 1; i++) { |
// if ((blockType[i - 1] == 32) && (blockLength[i] < 4)) { |
// int nonBinaryLength = blockLength[i]; |
// for (int l = i; ((l < blocks) && (blockType[l] != 32)); l++) { |
// nonBinaryLength += blockLength[l]; |
// } |
// if (nonBinaryLength < 4) { |
// blockType[i] = 32; |
// } |
// } |
// } |
/* Combine blocks of the same type */ |
i = 0; |
do { |
if (blockType[i] == blockType[i + 1]) { |
blockLength[i] += blockLength[i + 1]; |
for (j = i + 1; j < blocks - 1; j++) { |
blockType[j] = blockType[j + 1]; |
blockLength[j] = blockLength[j + 1]; |
} |
blocks--; |
} else { |
i++; |
} |
} while (i < blocks - 1); |
} |
/* Put the adjusted block data back into typemap */ |
j = 0; |
for (i = 0; i < blocks; i++) { |
if (blockLength[i] < 3 && blockType[i] != 32) { /* Shift character(s) needed */ |
for (k = 0; k < blockLength[i]; k++) { |
typemap[j + k] = blockType[i] + 64; |
} |
} else { /* Latch character (or byte mode) needed */ |
for (k = 0; k < blockLength[i]; k++) { |
typemap[j + k] = blockType[i]; |
} |
} |
j += blockLength[i]; |
} |
/* Don't shift an initial capital letter */ |
if (maplength > 0 && typemap[0] == 65) { |
typemap[0] = 1; |
} |
/* |
* Problem characters (those that appear in different tables with different values) can now |
* be resolved into their tables |
*/ |
for (i = 0; i < maplength; i++) { |
if (charmap[i] >= 300 && charmap[i] < 400) { |
curtable = typemap[i]; |
if (curtable > 64) { |
curtable -= 64; |
} |
switch (charmap[i]) { |
case 300: |
/* Carriage Return */ |
switch (curtable) { |
case 8: |
charmap[i] = 1; |
break; // PUNC |
case 4: |
charmap[i] = 14; |
break; // PUNC |
} |
break; |
case 301: |
/* Comma */ |
switch (curtable) { |
case 8: |
charmap[i] = 17; |
break; // PUNC |
case 16: |
charmap[i] = 12; |
break; // DIGIT |
} |
break; |
case 302: |
/* Full Stop */ |
switch (curtable) { |
case 8: |
charmap[i] = 19; |
break; // PUNC |
case 16: |
charmap[i] = 13; |
break; // DIGIT |
} |
break; |
} |
} |
} |
final StringBuilder binaryString = new StringBuilder(); |
info("Encoding: "); |
curtable = 1; /* start with 1 table */ |
lasttable = 1; |
/* Optional structured append start flag (Section 8 of spec) */ |
if (this.structuredAppendTotal != 1) { |
binaryString.append(PENTBIT[29]); |
info("ML "); |
binaryString.append(PENTBIT[29]); |
info("UL "); |
} |
for (i = 0; i < maplength; i++) { |
newtable = curtable; |
if (typemap[i] != curtable && charmap[i] < 400) { |
/* Change table */ |
if (curtable == 32) { |
/* |
* If ending binary mode the current table is the same as when entering binary |
* mode |
*/ |
curtable = lasttable; |
newtable = lasttable; |
} |
if (typemap[i] > 64) { |
/* Shift character */ |
switch (typemap[i]) { |
case 64 + 1: |
/* To UPPER */ |
switch (curtable) { |
case 2: |
/* US */ |
binaryString.append(PENTBIT[28]); |
info("US "); |
break; |
case 4: |
/* UL */ |
binaryString.append(PENTBIT[29]); |
info("UL "); |
newtable = 1; |
break; |
case 8: |
/* UL */ |
binaryString.append(PENTBIT[31]); |
info("UL "); |
newtable = 1; |
break; |
case 16: |
/* US */ |
binaryString.append(QUADBIT[15]); |
info("US "); |
break; |
} |
break; |
case 64 + 2: |
/* To LOWER */ |
switch (curtable) { |
case 1: |
/* LL */ |
binaryString.append(PENTBIT[28]); |
info("LL "); |
newtable = 2; |
break; |
case 4: |
/* LL */ |
binaryString.append(PENTBIT[28]); |
info("LL "); |
newtable = 2; |
break; |
case 8: |
/* UL LL */ |
binaryString.append(PENTBIT[31]); |
info("UL "); |
binaryString.append(PENTBIT[28]); |
info("LL "); |
newtable = 2; |
break; |
case 16: |
/* UL LL */ |
binaryString.append(QUADBIT[14]); |
info("UL "); |
binaryString.append(PENTBIT[28]); |
info("LL "); |
newtable = 2; |
break; |
} |
break; |
case 64 + 4: |
/* To MIXED */ |
switch (curtable) { |
case 1: |
/* ML */ |
binaryString.append(PENTBIT[29]); |
info("ML "); |
newtable = 4; |
break; |
case 2: |
/* ML */ |
binaryString.append(PENTBIT[29]); |
info("ML "); |
newtable = 4; |
break; |
case 8: |
/* UL ML */ |
binaryString.append(PENTBIT[31]); |
info("UL "); |
binaryString.append(PENTBIT[29]); |
info("ML "); |
newtable = 4; |
break; |
case 16: |
/* UL ML */ |
binaryString.append(QUADBIT[14]); |
info("UL "); |
binaryString.append(PENTBIT[29]); |
info("ML "); |
newtable = 4; |
break; |
} |
break; |
case 64 + 8: |
/* To PUNC */ |
switch (curtable) { |
case 1: |
/* PS */ |
binaryString.append(PENTBIT[0]); |
info("PS "); |
break; |
case 2: |
/* PS */ |
binaryString.append(PENTBIT[0]); |
info("PS "); |
break; |
case 4: |
/* PS */ |
binaryString.append(PENTBIT[0]); |
info("PS "); |
break; |
case 16: |
/* PS */ |
binaryString.append(QUADBIT[0]); |
info("PS "); |
break; |
} |
break; |
case 64 + 16: |
/* To DIGIT */ |
switch (curtable) { |
case 1: |
/* DL */ |
binaryString.append(PENTBIT[30]); |
info("DL "); |
newtable = 16; |
break; |
case 2: |
/* DL */ |
binaryString.append(PENTBIT[30]); |
info("DL "); |
newtable = 16; |
break; |
case 4: |
/* UL DL */ |
binaryString.append(PENTBIT[29]); |
info("UL "); |
binaryString.append(PENTBIT[30]); |
info("DL "); |
newtable = 16; |
break; |
case 8: |
/* UL DL */ |
binaryString.append(PENTBIT[31]); |
info("UL "); |
binaryString.append(PENTBIT[30]); |
info("DL "); |
newtable = 16; |
break; |
} |
break; |
} |
} else { |
/* Latch character */ |
switch (typemap[i]) { |
case 1: |
/* To UPPER */ |
switch (curtable) { |
case 2: |
/* ML UL */ |
binaryString.append(PENTBIT[29]); |
info("ML "); |
binaryString.append(PENTBIT[29]); |
info("UL "); |
newtable = 1; |
break; |
case 4: |
/* UL */ |
binaryString.append(PENTBIT[29]); |
info("UL "); |
newtable = 1; |
break; |
case 8: |
/* UL */ |
binaryString.append(PENTBIT[31]); |
info("UL "); |
newtable = 1; |
break; |
case 16: |
/* UL */ |
binaryString.append(QUADBIT[14]); |
info("UL "); |
newtable = 1; |
break; |
} |
break; |
case 2: |
/* To LOWER */ |
switch (curtable) { |
case 1: |
/* LL */ |
binaryString.append(PENTBIT[28]); |
info("LL "); |
newtable = 2; |
break; |
case 4: |
/* LL */ |
binaryString.append(PENTBIT[28]); |
info("LL "); |
newtable = 2; |
break; |
case 8: |
/* UL LL */ |
binaryString.append(PENTBIT[31]); |
info("UL "); |
binaryString.append(PENTBIT[28]); |
info("LL "); |
newtable = 2; |
break; |
case 16: |
/* UL LL */ |
binaryString.append(QUADBIT[14]); |
info("UL "); |
binaryString.append(PENTBIT[28]); |
info("LL "); |
newtable = 2; |
break; |
} |
break; |
case 4: |
/* To MIXED */ |
switch (curtable) { |
case 1: |
/* ML */ |
binaryString.append(PENTBIT[29]); |
info("ML "); |
newtable = 4; |
break; |
case 2: |
/* ML */ |
binaryString.append(PENTBIT[29]); |
info("ML "); |
newtable = 4; |
break; |
case 8: |
/* UL ML */ |
binaryString.append(PENTBIT[31]); |
info("UL "); |
binaryString.append(PENTBIT[29]); |
info("ML "); |
newtable = 4; |
break; |
case 16: |
/* UL ML */ |
binaryString.append(QUADBIT[14]); |
info("UL "); |
binaryString.append(PENTBIT[29]); |
info("ML "); |
newtable = 4; |
break; |
} |
break; |
case 8: |
/* To PUNC */ |
switch (curtable) { |
case 1: |
/* ML PL */ |
binaryString.append(PENTBIT[29]); |
info("ML "); |
binaryString.append(PENTBIT[30]); |
info("PL "); |
newtable = 8; |
break; |
case 2: |
/* ML PL */ |
binaryString.append(PENTBIT[29]); |
info("ML "); |
binaryString.append(PENTBIT[30]); |
info("PL "); |
newtable = 8; |
break; |
case 4: |
/* PL */ |
binaryString.append(PENTBIT[30]); |
info("PL "); |
newtable = 8; |
break; |
case 16: |
/* UL ML PL */ |
binaryString.append(QUADBIT[14]); |
info("UL "); |
binaryString.append(PENTBIT[29]); |
info("ML "); |
binaryString.append(PENTBIT[30]); |
info("PL "); |
newtable = 8; |
break; |
} |
break; |
case 16: |
/* To DIGIT */ |
switch (curtable) { |
case 1: |
/* DL */ |
binaryString.append(PENTBIT[30]); |
info("DL "); |
newtable = 16; |
break; |
case 2: |
/* DL */ |
binaryString.append(PENTBIT[30]); |
info("DL "); |
newtable = 16; |
break; |
case 4: |
/* UL DL */ |
binaryString.append(PENTBIT[29]); |
info("UL "); |
binaryString.append(PENTBIT[30]); |
info("DL "); |
newtable = 16; |
break; |
case 8: |
/* UL DL */ |
binaryString.append(PENTBIT[31]); |
info("UL "); |
binaryString.append(PENTBIT[30]); |
info("DL "); |
newtable = 16; |
break; |
} |
break; |
case 32: |
/* To BINARY */ |
lasttable = curtable; |
switch (curtable) { |
case 1: |
/* BS */ |
binaryString.append(PENTBIT[31]); |
info("BS "); |
newtable = 32; |
break; |
case 2: |
/* BS */ |
binaryString.append(PENTBIT[31]); |
info("BS "); |
newtable = 32; |
break; |
case 4: |
/* BS */ |
binaryString.append(PENTBIT[31]); |
info("BS "); |
newtable = 32; |
break; |
case 8: |
/* UL BS */ |
binaryString.append(PENTBIT[31]); |
info("UL "); |
binaryString.append(PENTBIT[31]); |
info("BS "); |
lasttable = 1; |
newtable = 32; |
break; |
case 16: |
/* UL BS */ |
binaryString.append(QUADBIT[14]); |
info("UL "); |
binaryString.append(PENTBIT[31]); |
info("BS "); |
lasttable = 1; |
newtable = 32; |
break; |
} |
bytes = 0; |
do { |
bytes++; |
} while (typemap[i + bytes - 1] == 32); |
bytes--; |
if (bytes > 2079) { |
throw new OkapiException("Input too long"); |
} |
if (bytes > 31) { |
/* Put 00000 followed by 11-bit number of bytes less 31 */ |
binaryString.append("00000"); |
for (int weight = 0x400; weight > 0; weight = weight >> 1) { |
if ((bytes - 31 & weight) != 0) { |
binaryString.append('1'); |
} else { |
binaryString.append('0'); |
} |
} |
} else { |
/* Put 5-bit number of bytes */ |
for (int weight = 0x10; weight > 0; weight = weight >> 1) { |
if ((bytes & weight) != 0) { |
binaryString.append('1'); |
} else { |
binaryString.append('0'); |
} |
} |
} |
break; |
} |
} |
} |
/* Add data to the binary string */ |
curtable = newtable; |
chartype = typemap[i]; |
if (chartype > 64) { |
chartype -= 64; |
} |
switch (chartype) { |
case 1: |
case 2: |
case 4: |
case 8: |
if (charmap[i] >= 400) { |
info("FLG(" + (charmap[i] - 400) + ") "); |
binaryString.append(TRIBIT[charmap[i] - 400]); |
if (charmap[i] != 400) { |
/* ECI */ |
binaryString.append(eciToBinary()); |
} |
} else { |
binaryString.append(PENTBIT[charmap[i]]); |
infoSpace(charmap[i]); |
} |
break; |
case 16: |
binaryString.append(QUADBIT[charmap[i]]); |
infoSpace(charmap[i]); |
break; |
case 32: |
for (int weight = 0x80; weight > 0; weight = weight >> 1) { |
if ((charmap[i] & weight) != 0) { |
binaryString.append('1'); |
} else { |
binaryString.append('0'); |
} |
} |
infoSpace(charmap[i]); |
break; |
} |
} |
infoLine(); |
return binaryString.toString(); |
} |
/** Adjusts bit stream so that no codewords are all 0s or all 1s, per Section 7.3.1.2 */ |
private StringBuilder adjustBinaryString(final String binaryString, final boolean compact, final int layers) { |
final StringBuilder adjustedString = new StringBuilder(); |
final int codewordSize = getCodewordSize(layers); |
int ones = 0; |
/* Insert dummy digits needed to prevent codewords of all 0s or all 1s */ |
for (int i = 0; i < binaryString.length(); i++) { |
if ((adjustedString.length() + 1) % codewordSize == 0) { |
if (ones == codewordSize - 1) { |
// codeword of B-1 1s, add dummy 0 |
adjustedString.append('0'); |
i--; |
} else if (ones == 0) { |
// codeword of B-1 0s, add dummy 1 |
adjustedString.append('1'); |
i--; |
} else { |
// no dummy value needed |
adjustedString.append(binaryString.charAt(i)); |
} |
ones = 0; |
} else { |
adjustedString.append(binaryString.charAt(i)); |
if (binaryString.charAt(i) == '1') { |
ones++; |
} |
} |
} |
/* Add padding */ |
int adjustedLength = adjustedString.length(); |
final int remainder = adjustedLength % codewordSize; |
int padBits = codewordSize - remainder; |
if (padBits == codewordSize) { |
padBits = 0; |
} |
for (int i = 0; i < padBits; i++) { |
adjustedString.append('1'); |
} |
adjustedLength = adjustedString.length(); |
/* Make sure padding didn't create an invalid (all 1s) codeword */ |
ones = 0; |
for (int i = adjustedLength - codewordSize; i < adjustedLength && i >= 0; i++) { |
if (adjustedString.charAt(i) == '1') { |
ones++; |
} |
} |
if (ones == codewordSize) { |
adjustedString.setCharAt(adjustedLength - 1, '0'); |
} |
/* Log the codewords */ |
info("Codewords: "); |
for (int i = 0; i < adjustedLength / codewordSize; i++) { |
int l = 0, m = 1 << codewordSize - 1; |
for (int j = 0; j < codewordSize; j++) { |
if (adjustedString.charAt(i * codewordSize + j) == '1') { |
l += m; |
} |
m = m >> 1; |
} |
infoSpace(l); |
} |
infoLine(); |
/* Return the adjusted bit string */ |
return adjustedString; |
} |
private String eciToBinary() { |
final String eciNumber = Integer.toString(this.eciMode); |
final StringBuilder binary = new StringBuilder(4 * eciNumber.length()); |
for (int i = 0; i < eciNumber.length(); i++) { |
binary.append(QUADBIT[eciNumber.charAt(i) - '0' + 2]); |
infoSpace(eciNumber.charAt(i)); |
} |
return binary.toString(); |
} |
/** Creates the descriptor / mode message, per Section 7.2 */ |
private String createDescriptor(final boolean compact, final int layers, final int dataBlocks) { |
final StringBuilder descriptor = new StringBuilder(); |
int descDataSize; |
if (compact) { |
/* The first 2 bits represent the number of layers minus 1 */ |
if ((layers - 1 & 0x02) != 0) { |
descriptor.append('1'); |
} else { |
descriptor.append('0'); |
} |
if ((layers - 1 & 0x01) != 0) { |
descriptor.append('1'); |
} else { |
descriptor.append('0'); |
} |
/* The next 6 bits represent the number of data blocks minus 1 */ |
if (this.readerInit) { |
descriptor.append('1'); |
} else { |
if ((dataBlocks - 1 & 0x20) != 0) { |
descriptor.append('1'); |
} else { |
descriptor.append('0'); |
} |
} |
for (int i = 0x10; i > 0; i = i >> 1) { |
if ((dataBlocks - 1 & i) != 0) { |
descriptor.append('1'); |
} else { |
descriptor.append('0'); |
} |
} |
descDataSize = 2; |
} else { |
/* The first 5 bits represent the number of layers minus 1 */ |
for (int i = 0x10; i > 0; i = i >> 1) { |
if ((layers - 1 & i) != 0) { |
descriptor.append('1'); |
} else { |
descriptor.append('0'); |
} |
} |
/* The next 11 bits represent the number of data blocks minus 1 */ |
if (this.readerInit) { |
descriptor.append('1'); |
} else { |
if ((dataBlocks - 1 & 0x400) != 0) { |
descriptor.append('1'); |
} else { |
descriptor.append('0'); |
} |
} |
for (int i = 0x200; i > 0; i = i >> 1) { |
if ((dataBlocks - 1 & i) != 0) { |
descriptor.append('1'); |
} else { |
descriptor.append('0'); |
} |
} |
descDataSize = 4; |
} |
infoLine("Mode Message: " + descriptor); |
/* Split into 4-bit codewords */ |
final int[] desc_data = new int[descDataSize]; |
for (int i = 0; i < descDataSize; i++) { |
for (int weight = 0; weight < 4; weight++) { |
if (descriptor.charAt(i * 4 + weight) == '1') { |
desc_data[i] += 8 >> weight; |
} |
} |
} |
/* |
* Add Reed-Solomon error correction with Galois Field GF(16) and prime modulus x^4 + x + 1 |
* (Section 7.2.3) |
*/ |
final ReedSolomon rs = new ReedSolomon(); |
rs.init_gf(0x13); |
if (compact) { |
rs.init_code(5, 1); |
rs.encode(2, desc_data); |
final int[] desc_ecc = new int[6]; |
for (int i = 0; i < 5; i++) { |
desc_ecc[i] = rs.getResult(i); |
} |
for (int i = 0; i < 5; i++) { |
for (int weight = 0x08; weight > 0; weight = weight >> 1) { |
if ((desc_ecc[4 - i] & weight) != 0) { |
descriptor.append('1'); |
} else { |
descriptor.append('0'); |
} |
} |
} |
} else { |
rs.init_code(6, 1); |
rs.encode(4, desc_data); |
final int[] desc_ecc = new int[6]; |
for (int i = 0; i < 6; i++) { |
desc_ecc[i] = rs.getResult(i); |
} |
for (int i = 0; i < 6; i++) { |
for (int weight = 0x08; weight > 0; weight = weight >> 1) { |
if ((desc_ecc[5 - i] & weight) != 0) { |
descriptor.append('1'); |
} else { |
descriptor.append('0'); |
} |
} |
} |
} |
return descriptor.toString(); |
} |
/** |
* Adds error correction data to the specified binary string, which already contains the primary |
* data |
*/ |
private void addErrorCorrection(final StringBuilder adjustedString, final int codewordSize, final int dataBlocks, final int eccBlocks) { |
int x, poly, startWeight; |
/* Split into codewords and calculate Reed-Solomon error correction codes */ |
switch (codewordSize) { |
case 6: |
x = 32; |
poly = 0x43; |
startWeight = 0x20; |
break; |
case 8: |
x = 128; |
poly = 0x12d; |
startWeight = 0x80; |
break; |
case 10: |
x = 512; |
poly = 0x409; |
startWeight = 0x200; |
break; |
case 12: |
x = 2048; |
poly = 0x1069; |
startWeight = 0x800; |
break; |
default: |
throw new OkapiException("Unrecognized codeword size: " + codewordSize); |
} |
final ReedSolomon rs = new ReedSolomon(); |
final int[] data = new int[dataBlocks + 3]; |
final int[] ecc = new int[eccBlocks + 3]; |
for (int i = 0; i < dataBlocks; i++) { |
for (int weight = 0; weight < codewordSize; weight++) { |
if (adjustedString.charAt(i * codewordSize + weight) == '1') { |
data[i] += x >> weight; |
} |
} |
} |
rs.init_gf(poly); |
rs.init_code(eccBlocks, 1); |
rs.encode(dataBlocks, data); |
for (int i = 0; i < eccBlocks; i++) { |
ecc[i] = rs.getResult(i); |
} |
for (int i = eccBlocks - 1; i >= 0; i--) { |
for (int weight = startWeight; weight > 0; weight = weight >> 1) { |
if ((ecc[i] & weight) != 0) { |
adjustedString.append('1'); |
} else { |
adjustedString.append('0'); |
} |
} |
} |
} |
/** Determines codeword bit length - Table 3 */ |
private static int getCodewordSize(final int layers) { |
if (layers >= 23) { |
return 12; |
} else if (layers >= 9 && layers <= 22) { |
return 10; |
} else if (layers >= 3 && layers <= 8) { |
return 8; |
} else { |
assert layers <= 2; |
return 6; |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/ChannelCode.java |
---|
New file |
0,0 → 1,167 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
/** |
* <p> |
* Implements Channel Code according to ANSI/AIM BC12-1998. |
* |
* <p> |
* Channel Code encodes whole integer values between 0 and 7,742,862. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class ChannelCode extends Symbol { |
private int preferredNumberOfChannels; |
private final int[] space = new int[11]; |
private final int[] bar = new int[11]; |
private double currentValue; |
private double targetValue; |
/** |
* Sets the preferred number of channels used to encode data. This setting will be ignored if |
* the value to be encoded requires more channels. |
* |
* @param channels the preferred number of channels (3 to 8, inclusive) |
*/ |
public void setPreferredNumberOfChannels(final int channels) { |
if (channels < 3 || channels > 8) { |
throw new IllegalArgumentException("Invalid Channel Code number of channels: " + channels); |
} |
this.preferredNumberOfChannels = channels; |
} |
/** |
* Returns the preferred number of channels used to encode data. |
* |
* @return the preferred number of channels used to encode data |
*/ |
public int getPreferredNumberOfChannels() { |
return this.preferredNumberOfChannels; |
} |
@Override |
protected void encode() { |
int channels; |
int i; |
int leadingZeroCount; |
if (this.content.length() > 7) { |
throw new OkapiException("Input too long"); |
} |
if (!this.content.matches("[0-9]+")) { |
throw new OkapiException("Invalid characters in input"); |
} |
if (this.preferredNumberOfChannels <= 2 || this.preferredNumberOfChannels > 8) { |
channels = 3; |
} else { |
channels = this.preferredNumberOfChannels; |
} |
this.targetValue = Integer.parseInt(this.content); |
switch (channels) { |
case 3: |
if (this.targetValue > 26) { |
channels++; |
} |
case 4: |
if (this.targetValue > 292) { |
channels++; |
} |
case 5: |
if (this.targetValue > 3493) { |
channels++; |
} |
case 6: |
if (this.targetValue > 44072) { |
channels++; |
} |
case 7: |
if (this.targetValue > 576688) { |
channels++; |
} |
case 8: |
if (this.targetValue > 7742862) { |
channels++; |
} |
} |
if (channels == 9) { |
throw new OkapiException("Value out of range"); |
} |
infoLine("Channels Used: " + channels); |
for (i = 0; i < 11; i++) { |
this.bar[i] = 0; |
this.space[i] = 0; |
} |
this.bar[0] = this.space[1] = this.bar[1] = this.space[2] = this.bar[2] = 1; |
this.currentValue = 0; |
this.pattern = new String[1]; |
nextSpace(channels, 3, channels, channels); |
leadingZeroCount = channels - 1 - this.content.length(); |
this.readable = ""; |
for (i = 0; i < leadingZeroCount; i++) { |
this.readable += "0"; |
} |
this.readable += this.content; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
private void nextSpace(final int channels, final int i, final int maxSpace, final int maxBar) { |
for (int s = i < channels + 2 ? 1 : maxSpace; s <= maxSpace; s++) { |
this.space[i] = s; |
nextBar(channels, i, maxBar, maxSpace + 1 - s); |
} |
} |
private void nextBar(final int channels, final int i, final int maxBar, final int maxSpace) { |
int b = this.space[i] + this.bar[i - 1] + this.space[i - 1] + this.bar[i - 2] > 4 ? 1 : 2; |
if (i < channels + 2) { |
for (; b <= maxBar; b++) { |
this.bar[i] = b; |
nextSpace(channels, i + 1, maxSpace, maxBar + 1 - b); |
} |
} else if (b <= maxBar) { |
this.bar[i] = maxBar; |
checkIfDone(); |
this.currentValue++; |
} |
} |
private void checkIfDone() { |
if (this.currentValue == this.targetValue) { |
/* Target reached - save the generated pattern */ |
final StringBuilder sb = new StringBuilder(); |
sb.append("11110"); |
for (int i = 0; i < 11; i++) { |
sb.append((char) (this.space[i] + '0')); |
sb.append((char) (this.bar[i] + '0')); |
} |
this.pattern[0] = sb.toString(); |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/DataBar14.java |
---|
New file |
0,0 → 1,616 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.backend.DataBarLimited.getWidths; |
import java.math.BigInteger; |
/** |
* <p> |
* Implements GS1 DataBar Omnidirectional and GS1 DataBar Truncated according to ISO/IEC 24724:2011. |
* |
* <p> |
* Input data should be a 13-digit Global Trade Identification Number (GTIN) without check digit or |
* Application Identifier [01]. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class DataBar14 extends Symbol { |
public enum Mode { |
/** DataBar-14 */ |
LINEAR, |
/** DataBar-14 Omnidirectional */ |
OMNI, |
/** DataBar-14 Omnidirectional Stacked */ |
STACKED |
} |
private static final int[] G_SUM_TABLE = { 0, 161, 961, 2015, 2715, 0, 336, 1036, 1516 }; |
private static final int[] T_TABLE = { 1, 10, 34, 70, 126, 4, 20, 48, 81 }; |
private static final int[] MODULES_ODD = { 12, 10, 8, 6, 4, 5, 7, 9, 11 }; |
private static final int[] MODULES_EVEN = { 4, 6, 8, 10, 12, 10, 8, 6, 4 }; |
private static final int[] WIDEST_ODD = { 8, 6, 4, 3, 1, 2, 4, 6, 8 }; |
private static final int[] WIDEST_EVEN = { 1, 3, 5, 6, 8, 7, 5, 3, 1 }; |
private static final int[] CHECKSUM_WEIGHT = { /* Table 5 */ |
1, 3, 9, 27, 2, 6, 18, 54, 4, 12, 36, 29, 8, 24, 72, 58, 16, 48, 65, 37, 32, 17, 51, 74, 64, 34, 23, 69, 49, 68, 46, 59 }; |
private static final int[] FINDER_PATTERN = { 3, 8, 2, 1, 1, 3, 5, 5, 1, 1, 3, 3, 7, 1, 1, 3, 1, 9, 1, 1, 2, 7, 4, 1, 1, 2, 5, 6, 1, 1, 2, 3, 8, 1, 1, 1, 5, 7, 1, 1, 1, 3, 9, 1, 1 }; |
private boolean linkageFlag; |
private Mode mode = Mode.LINEAR; |
@Override |
public void setDataType(final DataType dummy) { |
// Do nothing! |
} |
/** |
* Although this is a GS1 symbology, input data is expected to omit the [01] Application |
* Identifier, as well as the check digit. Thus, the input data is not considered GS1-format |
* data. |
*/ |
@Override |
protected boolean gs1Supported() { |
return false; |
} |
protected void setLinkageFlag(final boolean linkageFlag) { |
this.linkageFlag = linkageFlag; |
} |
protected boolean getLinkageFlag() { |
return this.linkageFlag; |
} |
/** |
* Sets the symbol mode. The default is {@link Mode#LINEAR}. |
* |
* @param mode the symbol mode |
*/ |
public void setMode(final Mode mode) { |
this.mode = mode; |
} |
/** |
* Returns the symbol mode. |
* |
* @return the symbol mode |
*/ |
public Mode getMode() { |
return this.mode; |
} |
@Override |
protected void encode() { |
final boolean[][] grid = new boolean[5][100]; |
BigInteger accum; |
BigInteger left_reg; |
BigInteger right_reg; |
final int[] data_character = new int[4]; |
final int[] data_group = new int[4]; |
final int[] v_odd = new int[4]; |
final int[] v_even = new int[4]; |
int i; |
final int[][] data_widths = new int[8][4]; |
int checksum; |
int c_left; |
int c_right; |
final int[] total_widths = new int[46]; |
int writer; |
char latch; |
int j; |
int count; |
int check_digit; |
final StringBuilder bin = new StringBuilder(); |
int compositeOffset = 0; |
if (this.content.length() > 13) { |
throw new OkapiException("Input too long"); |
} |
if (!this.content.matches("[0-9]+?")) { |
throw new OkapiException("Invalid characters in input"); |
} |
accum = new BigInteger(this.content); |
if (this.linkageFlag) { |
accum = accum.add(new BigInteger("10000000000000")); |
compositeOffset = 1; |
} |
/* Calculate left and right pair values */ |
left_reg = accum.divide(new BigInteger("4537077")); |
right_reg = accum.mod(new BigInteger("4537077")); |
/* Calculate four data characters */ |
accum = left_reg.divide(new BigInteger("1597")); |
data_character[0] = accum.intValue(); |
accum = left_reg.mod(new BigInteger("1597")); |
data_character[1] = accum.intValue(); |
accum = right_reg.divide(new BigInteger("1597")); |
data_character[2] = accum.intValue(); |
accum = right_reg.mod(new BigInteger("1597")); |
data_character[3] = accum.intValue(); |
info("Data Characters: "); |
for (i = 0; i < 4; i++) { |
infoSpace(data_character[i]); |
} |
infoLine(); |
/* Calculate odd and even subset values */ |
if (data_character[0] >= 0 && data_character[0] <= 160) { |
data_group[0] = 0; |
} |
if (data_character[0] >= 161 && data_character[0] <= 960) { |
data_group[0] = 1; |
} |
if (data_character[0] >= 961 && data_character[0] <= 2014) { |
data_group[0] = 2; |
} |
if (data_character[0] >= 2015 && data_character[0] <= 2714) { |
data_group[0] = 3; |
} |
if (data_character[0] >= 2715 && data_character[0] <= 2840) { |
data_group[0] = 4; |
} |
if (data_character[1] >= 0 && data_character[1] <= 335) { |
data_group[1] = 5; |
} |
if (data_character[1] >= 336 && data_character[1] <= 1035) { |
data_group[1] = 6; |
} |
if (data_character[1] >= 1036 && data_character[1] <= 1515) { |
data_group[1] = 7; |
} |
if (data_character[1] >= 1516 && data_character[1] <= 1596) { |
data_group[1] = 8; |
} |
if (data_character[3] >= 0 && data_character[3] <= 335) { |
data_group[3] = 5; |
} |
if (data_character[3] >= 336 && data_character[3] <= 1035) { |
data_group[3] = 6; |
} |
if (data_character[3] >= 1036 && data_character[3] <= 1515) { |
data_group[3] = 7; |
} |
if (data_character[3] >= 1516 && data_character[3] <= 1596) { |
data_group[3] = 8; |
} |
if (data_character[2] >= 0 && data_character[2] <= 160) { |
data_group[2] = 0; |
} |
if (data_character[2] >= 161 && data_character[2] <= 960) { |
data_group[2] = 1; |
} |
if (data_character[2] >= 961 && data_character[2] <= 2014) { |
data_group[2] = 2; |
} |
if (data_character[2] >= 2015 && data_character[2] <= 2714) { |
data_group[2] = 3; |
} |
if (data_character[2] >= 2715 && data_character[2] <= 2840) { |
data_group[2] = 4; |
} |
v_odd[0] = (data_character[0] - G_SUM_TABLE[data_group[0]]) / T_TABLE[data_group[0]]; |
v_even[0] = (data_character[0] - G_SUM_TABLE[data_group[0]]) % T_TABLE[data_group[0]]; |
v_odd[1] = (data_character[1] - G_SUM_TABLE[data_group[1]]) % T_TABLE[data_group[1]]; |
v_even[1] = (data_character[1] - G_SUM_TABLE[data_group[1]]) / T_TABLE[data_group[1]]; |
v_odd[3] = (data_character[3] - G_SUM_TABLE[data_group[3]]) % T_TABLE[data_group[3]]; |
v_even[3] = (data_character[3] - G_SUM_TABLE[data_group[3]]) / T_TABLE[data_group[3]]; |
v_odd[2] = (data_character[2] - G_SUM_TABLE[data_group[2]]) / T_TABLE[data_group[2]]; |
v_even[2] = (data_character[2] - G_SUM_TABLE[data_group[2]]) % T_TABLE[data_group[2]]; |
/* Use RSS subset width algorithm */ |
for (i = 0; i < 4; i++) { |
if (i == 0 || i == 2) { |
int[] widths = getWidths(v_odd[i], MODULES_ODD[data_group[i]], 4, WIDEST_ODD[data_group[i]], 1); |
data_widths[0][i] = widths[0]; |
data_widths[2][i] = widths[1]; |
data_widths[4][i] = widths[2]; |
data_widths[6][i] = widths[3]; |
widths = getWidths(v_even[i], MODULES_EVEN[data_group[i]], 4, WIDEST_EVEN[data_group[i]], 0); |
data_widths[1][i] = widths[0]; |
data_widths[3][i] = widths[1]; |
data_widths[5][i] = widths[2]; |
data_widths[7][i] = widths[3]; |
} else { |
int[] widths = getWidths(v_odd[i], MODULES_ODD[data_group[i]], 4, WIDEST_ODD[data_group[i]], 0); |
data_widths[0][i] = widths[0]; |
data_widths[2][i] = widths[1]; |
data_widths[4][i] = widths[2]; |
data_widths[6][i] = widths[3]; |
widths = getWidths(v_even[i], MODULES_EVEN[data_group[i]], 4, WIDEST_EVEN[data_group[i]], 1); |
data_widths[1][i] = widths[0]; |
data_widths[3][i] = widths[1]; |
data_widths[5][i] = widths[2]; |
data_widths[7][i] = widths[3]; |
} |
} |
/* Calculate the checksum */ |
checksum = 0; |
for (i = 0; i < 8; i++) { |
checksum += CHECKSUM_WEIGHT[i] * data_widths[i][0]; |
checksum += CHECKSUM_WEIGHT[i + 8] * data_widths[i][1]; |
checksum += CHECKSUM_WEIGHT[i + 16] * data_widths[i][2]; |
checksum += CHECKSUM_WEIGHT[i + 24] * data_widths[i][3]; |
} |
checksum %= 79; |
/* Calculate the two check characters */ |
if (checksum >= 8) { |
checksum++; |
} |
if (checksum >= 72) { |
checksum++; |
} |
c_left = checksum / 9; |
c_right = checksum % 9; |
infoLine("Checksum: " + checksum); |
/* Put element widths together */ |
total_widths[0] = 1; |
total_widths[1] = 1; |
total_widths[44] = 1; |
total_widths[45] = 1; |
for (i = 0; i < 8; i++) { |
total_widths[i + 2] = data_widths[i][0]; |
total_widths[i + 15] = data_widths[7 - i][1]; |
total_widths[i + 23] = data_widths[i][3]; |
total_widths[i + 36] = data_widths[7 - i][2]; |
} |
for (i = 0; i < 5; i++) { |
total_widths[i + 10] = FINDER_PATTERN[i + 5 * c_left]; |
total_widths[i + 31] = FINDER_PATTERN[4 - i + 5 * c_right]; |
} |
this.row_count = 0; |
final boolean[] separator = new boolean[100]; |
for (i = 0; i < separator.length; i++) { |
separator[i] = false; |
} |
/* Put this data into the symbol */ |
if (this.mode == Mode.LINEAR) { |
writer = 0; |
latch = '0'; |
for (i = 0; i < 46; i++) { |
for (j = 0; j < total_widths[i]; j++) { |
if (latch == '1') { |
grid[this.row_count][writer] = true; |
} |
writer++; |
} |
if (latch == '1') { |
latch = '0'; |
} else { |
latch = '1'; |
} |
} |
if (this.symbol_width < writer) { |
this.symbol_width = writer; |
} |
if (this.linkageFlag) { |
/* separator pattern for composite symbol */ |
for (i = 4; i < 92; i++) { |
separator[i] = !grid[0][i]; |
} |
latch = '1'; |
for (i = 16; i < 32; i++) { |
if (!grid[0][i]) { |
if (latch == '1') { |
separator[i] = true; |
latch = '0'; |
} else { |
separator[i] = false; |
latch = '1'; |
} |
} else { |
separator[i] = false; |
latch = '1'; |
} |
} |
latch = '1'; |
for (i = 63; i < 78; i++) { |
if (!grid[0][i]) { |
if (latch == '1') { |
separator[i] = true; |
latch = '0'; |
} else { |
separator[i] = false; |
latch = '1'; |
} |
} else { |
separator[i] = false; |
latch = '1'; |
} |
} |
} |
this.row_count = this.row_count + 1; |
count = 0; |
check_digit = 0; |
/* Calculate check digit from Annex A and place human readable text */ |
final StringBuilder hrt = new StringBuilder(14); |
for (i = this.content.length(); i < 13; i++) { |
hrt.append('0'); |
} |
hrt.append(this.content); |
for (i = 0; i < 13; i++) { |
count += hrt.charAt(i) - '0'; |
if ((i & 1) == 0) { |
count += 2 * (hrt.charAt(i) - '0'); |
} |
} |
check_digit = 10 - count % 10; |
if (check_digit == 10) { |
check_digit = 0; |
} |
infoLine("Check Digit: " + check_digit); |
hrt.append((char) (check_digit + '0')); |
this.readable = "(01)" + hrt; |
} |
if (this.mode == Mode.STACKED) { |
/* top row */ |
writer = 0; |
latch = '0'; |
for (i = 0; i < 23; i++) { |
for (j = 0; j < total_widths[i]; j++) { |
grid[this.row_count][writer] = latch == '1'; |
writer++; |
} |
if (latch == '1') { |
latch = '0'; |
} else { |
latch = '1'; |
} |
} |
grid[this.row_count][writer] = true; |
grid[this.row_count][writer + 1] = false; |
/* bottom row */ |
this.row_count = this.row_count + 2; |
grid[this.row_count][0] = true; |
grid[this.row_count][1] = false; |
writer = 0; |
latch = '1'; |
for (i = 23; i < 46; i++) { |
for (j = 0; j < total_widths[i]; j++) { |
grid[this.row_count][writer + 2] = latch == '1'; |
writer++; |
} |
if (latch == '1') { |
latch = '0'; |
} else { |
latch = '1'; |
} |
} |
/* separator pattern */ |
for (i = 1; i < 46; i++) { |
if (grid[this.row_count - 2][i] == grid[this.row_count][i]) { |
if (!grid[this.row_count - 2][i]) { |
grid[this.row_count - 1][i] = true; |
} |
} else { |
if (!grid[this.row_count - 1][i - 1]) { |
grid[this.row_count - 1][i] = true; |
} |
} |
} |
for (i = 0; i < 4; i++) { |
grid[this.row_count - 1][i] = false; |
} |
if (this.linkageFlag) { |
/* separator pattern for composite symbol */ |
for (i = 4; i < 46; i++) { |
separator[i] = !grid[0][i]; |
} |
latch = '1'; |
for (i = 16; i < 32; i++) { |
if (!grid[0][i]) { |
if (latch == '1') { |
separator[i] = true; |
latch = '0'; |
} else { |
separator[i] = false; |
latch = '1'; |
} |
} else { |
separator[i] = false; |
latch = '1'; |
} |
} |
} |
this.row_count = this.row_count + 1; |
if (this.symbol_width < 50) { |
this.symbol_width = 50; |
} |
} |
if (this.mode == Mode.OMNI) { |
/* top row */ |
writer = 0; |
latch = '0'; |
for (i = 0; i < 23; i++) { |
for (j = 0; j < total_widths[i]; j++) { |
grid[this.row_count][writer] = latch == '1'; |
writer++; |
} |
latch = latch == '1' ? '0' : '1'; |
} |
grid[this.row_count][writer] = true; |
grid[this.row_count][writer + 1] = false; |
/* bottom row */ |
this.row_count = this.row_count + 4; |
grid[this.row_count][0] = true; |
grid[this.row_count][1] = false; |
writer = 0; |
latch = '1'; |
for (i = 23; i < 46; i++) { |
for (j = 0; j < total_widths[i]; j++) { |
grid[this.row_count][writer + 2] = latch == '1'; |
writer++; |
} |
if (latch == '1') { |
latch = '0'; |
} else { |
latch = '1'; |
} |
} |
/* middle separator */ |
for (i = 5; i < 46; i += 2) { |
grid[this.row_count - 2][i] = true; |
} |
/* top separator */ |
for (i = 4; i < 46; i++) { |
if (!grid[this.row_count - 4][i]) { |
grid[this.row_count - 3][i] = true; |
} |
} |
latch = '1'; |
for (i = 17; i < 33; i++) { |
if (!grid[this.row_count - 4][i]) { |
if (latch == '1') { |
grid[this.row_count - 3][i] = true; |
latch = '0'; |
} else { |
grid[this.row_count - 3][i] = false; |
latch = '1'; |
} |
} else { |
grid[this.row_count - 3][i] = false; |
latch = '1'; |
} |
} |
/* bottom separator */ |
for (i = 4; i < 46; i++) { |
if (!grid[this.row_count][i]) { |
grid[this.row_count - 1][i] = true; |
} |
} |
latch = '1'; |
for (i = 16; i < 32; i++) { |
if (!grid[this.row_count][i]) { |
if (latch == '1') { |
grid[this.row_count - 1][i] = true; |
latch = '0'; |
} else { |
grid[this.row_count - 1][i] = false; |
latch = '1'; |
} |
} else { |
grid[this.row_count - 1][i] = false; |
latch = '1'; |
} |
} |
if (this.symbol_width < 50) { |
this.symbol_width = 50; |
} |
if (this.linkageFlag) { |
/* separator pattern for composite symbol */ |
for (i = 4; i < 46; i++) { |
separator[i] = !grid[0][i]; |
} |
latch = '1'; |
for (i = 16; i < 32; i++) { |
if (!grid[0][i]) { |
if (latch == '1') { |
separator[i] = true; |
latch = '0'; |
} else { |
separator[i] = false; |
latch = '1'; |
} |
} else { |
separator[i] = false; |
latch = '1'; |
} |
} |
} |
this.row_count = this.row_count + 1; |
} |
this.pattern = new String[this.row_count + compositeOffset]; |
this.row_height = new int[this.row_count + compositeOffset]; |
if (this.linkageFlag) { |
bin.setLength(0); |
for (j = 0; j < this.symbol_width; j++) { |
if (separator[j]) { |
bin.append('1'); |
} else { |
bin.append('0'); |
} |
} |
this.pattern[0] = bin2pat(bin); |
this.row_height[0] = 1; |
} |
for (i = 0; i < this.row_count; i++) { |
bin.setLength(0); |
for (j = 0; j < this.symbol_width; j++) { |
if (grid[i][j]) { |
bin.append('1'); |
} else { |
bin.append('0'); |
} |
} |
this.pattern[i + compositeOffset] = bin2pat(bin); |
} |
if (this.mode == Mode.LINEAR) { |
this.row_height[0 + compositeOffset] = -1; |
} |
if (this.mode == Mode.STACKED) { |
this.row_height[0 + compositeOffset] = 5; |
this.row_height[1 + compositeOffset] = 1; |
this.row_height[2 + compositeOffset] = 7; |
} |
if (this.mode == Mode.OMNI) { |
this.row_height[0 + compositeOffset] = -1; |
this.row_height[1 + compositeOffset] = 1; |
this.row_height[2 + compositeOffset] = 1; |
this.row_height[3 + compositeOffset] = 1; |
this.row_height[4 + compositeOffset] = -1; |
} |
if (this.linkageFlag) { |
this.row_count++; |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Ean.java |
---|
New file |
0,0 → 1,352 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.backend.HumanReadableLocation.BOTTOM; |
import static uk.org.okapibarcode.backend.HumanReadableLocation.NONE; |
import static uk.org.okapibarcode.backend.HumanReadableLocation.TOP; |
import java.awt.geom.Rectangle2D; |
/** |
* <p> |
* Implements EAN bar code symbology according to BS EN 797:1996. |
* |
* <p> |
* European Article Number data can be encoded in EAN-8 or EAN-13 format requiring a 7-digit or |
* 12-digit input respectively. EAN-13 numbers map to Global Trade Identification Numbers (GTIN) |
* whereas EAN-8 symbols are generally for internal use only. Check digit is calculated and should |
* not be in input data. Leading zeroes are added as required. |
* |
* <p> |
* Add-on content can be appended to the main symbol content by adding a <tt>'+'</tt> character, |
* followed by the add-on content (up to 5 digits). |
* |
* @author <a href="mailto:jakel2006@me.com">Robert Elliott</a> |
*/ |
public class Ean extends Symbol { |
public enum Mode { |
EAN8, EAN13 |
}; |
private static final String[] EAN13_PARITY = { "AAAAAA", "AABABB", "AABBAB", "AABBBA", "ABAABB", "ABBAAB", "ABBBAA", "ABABAB", "ABABBA", "ABBABA" }; |
private static final String[] EAN_SET_A = { "3211", "2221", "2122", "1411", "1132", "1231", "1114", "1312", "1213", "3112" }; |
private static final String[] EAN_SET_B = { "1123", "1222", "2212", "1141", "2311", "1321", "4111", "2131", "3121", "2113" }; |
private Mode mode = Mode.EAN13; |
private int guardPatternExtraHeight = 5; |
private boolean linkageFlag; |
private EanUpcAddOn addOn; |
/** Creates a new instance. */ |
public Ean() { |
this.humanReadableAlignment = HumanReadableAlignment.JUSTIFY; |
} |
/** |
* Sets the EAN mode (EAN-8 or EAN-13). The default is EAN-13. |
* |
* @param mode the EAN mode (EAN-8 or EAN-13) |
*/ |
public void setMode(final Mode mode) { |
this.mode = mode; |
} |
/** |
* Returns the EAN mode (EAN-8 or EAN-13). |
* |
* @return the EAN mode (EAN-8 or EAN-13) |
*/ |
public Mode getMode() { |
return this.mode; |
} |
/** |
* Sets the extra height used for the guard patterns. The default value is <code>5</code>. |
* |
* @param guardPatternExtraHeight the extra height used for the guard patterns |
*/ |
public void setGuardPatternExtraHeight(final int guardPatternExtraHeight) { |
this.guardPatternExtraHeight = guardPatternExtraHeight; |
} |
/** |
* Returns the extra height used for the guard patterns. |
* |
* @return the extra height used for the guard patterns |
*/ |
public int getGuardPatternExtraHeight() { |
return this.guardPatternExtraHeight; |
} |
/** |
* Sets the linkage flag. If set to <code>true</code>, this symbol is part of a composite |
* symbol. |
* |
* @param linkageFlag the linkage flag |
*/ |
protected void setLinkageFlag(final boolean linkageFlag) { |
this.linkageFlag = linkageFlag; |
} |
@Override |
protected void encode() { |
separateContent(); |
if (this.content.isEmpty()) { |
throw new OkapiException("Missing EAN data"); |
} |
if (this.mode == Mode.EAN8) { |
ean8(); |
} else { |
ean13(); |
} |
} |
private void separateContent() { |
final int splitPoint = this.content.indexOf('+'); |
if (splitPoint == -1) { |
// there is no add-on data |
this.addOn = null; |
} else if (splitPoint == this.content.length() - 1) { |
// we found the add-on separator, but no add-on data |
throw new OkapiException("Invalid add-on data"); |
} else { |
// there is a '+' in the input data, use an add-on EAN2 or EAN5 |
this.addOn = new EanUpcAddOn(); |
this.addOn.font = this.font; |
this.addOn.fontName = this.fontName; |
this.addOn.fontSize = this.fontSize; |
this.addOn.humanReadableLocation = this.humanReadableLocation == NONE ? NONE : TOP; |
this.addOn.moduleWidth = this.moduleWidth; |
this.addOn.default_height = this.default_height + this.guardPatternExtraHeight - 8; |
this.addOn.setContent(this.content.substring(splitPoint + 1)); |
this.content = this.content.substring(0, splitPoint); |
} |
} |
private void ean13() { |
this.content = validateAndPad(this.content, 12); |
final char check = calcDigit(this.content); |
infoLine("Check Digit: " + check); |
final String hrt = this.content + check; |
final char parityChar = hrt.charAt(0); |
final String parity = EAN13_PARITY[parityChar - '0']; |
infoLine("Parity Digit: " + parityChar); |
final StringBuilder dest = new StringBuilder("111"); |
for (int i = 1; i < 13; i++) { |
if (i == 7) { |
dest.append("11111"); |
} |
if (i <= 6) { |
if (parity.charAt(i - 1) == 'B') { |
dest.append(EAN_SET_B[hrt.charAt(i) - '0']); |
} else { |
dest.append(EAN_SET_A[hrt.charAt(i) - '0']); |
} |
} else { |
dest.append(EAN_SET_A[hrt.charAt(i) - '0']); |
} |
} |
dest.append("111"); |
this.readable = hrt; |
this.pattern = new String[] { dest.toString() }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
private void ean8() { |
this.content = validateAndPad(this.content, 7); |
final char check = calcDigit(this.content); |
infoLine("Check Digit: " + check); |
final String hrt = this.content + check; |
final StringBuilder dest = new StringBuilder("111"); |
for (int i = 0; i < 8; i++) { |
if (i == 4) { |
dest.append("11111"); |
} |
dest.append(EAN_SET_A[hrt.charAt(i) - '0']); |
} |
dest.append("111"); |
this.readable = hrt; |
this.pattern = new String[] { dest.toString() }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
protected static String validateAndPad(String s, final int targetLength) { |
if (!s.matches("[0-9]+")) { |
throw new OkapiException("Invalid characters in input"); |
} |
if (s.length() > targetLength) { |
throw new OkapiException("Input data too long"); |
} |
if (s.length() < targetLength) { |
for (int i = s.length(); i < targetLength; i++) { |
s = '0' + s; |
} |
} |
return s; |
} |
public static char calcDigit(final String s) { |
int count = 0; |
int p = 0; |
for (int i = s.length() - 1; i >= 0; i--) { |
int c = Character.getNumericValue(s.charAt(i)); |
if (p % 2 == 0) { |
c = c * 3; |
} |
count += c; |
p++; |
} |
int cdigit = 10 - count % 10; |
if (cdigit == 10) { |
cdigit = 0; |
} |
return (char) (cdigit + '0'); |
} |
@Override |
protected void plotSymbol() { |
int xBlock; |
int x, y, w, h; |
boolean black = true; |
final int compositeOffset = this.linkageFlag ? 6 : 0; // space for composite separator above |
final int hrtOffset = this.humanReadableLocation == TOP ? getTheoreticalHumanReadableHeight() : 0; // space |
// for |
// HRT |
// above |
this.rectangles.clear(); |
this.texts.clear(); |
x = 0; |
/* Draw the bars in the symbology */ |
for (xBlock = 0; xBlock < this.pattern[0].length(); xBlock++) { |
w = this.pattern[0].charAt(xBlock) - '0'; |
if (black) { |
y = 0; |
h = this.default_height; |
/* Add extension to guide bars */ |
if (this.mode == Mode.EAN13) { |
if (x < 3 || x > 91 || x > 45 && x < 49) { |
h += this.guardPatternExtraHeight; |
} |
if (this.linkageFlag && (x == 0 || x == 94)) { |
h += 2; |
y -= 2; |
} |
} else { |
if (x < 3 || x > 62 || x > 30 && x < 35) { |
h += this.guardPatternExtraHeight; |
} |
if (this.linkageFlag && (x == 0 || x == 66)) { |
h += 2; |
y -= 2; |
} |
} |
final Rectangle2D.Double rect = new Rectangle2D.Double(scale(x), y + compositeOffset + hrtOffset, scale(w), h); |
this.rectangles.add(rect); |
this.symbol_width = Math.max(this.symbol_width, (int) rect.getMaxX()); |
this.symbol_height = Math.max(this.symbol_height, (int) rect.getHeight()); |
} |
black = !black; |
x += w; |
} |
/* Add separator for composite symbology, if necessary */ |
if (this.linkageFlag) { |
if (this.mode == Mode.EAN13) { |
this.rectangles.add(new Rectangle2D.Double(scale(0), 0, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(94), 0, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(-1), 2, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(95), 2, scale(1), 2)); |
} else { // EAN8 |
this.rectangles.add(new Rectangle2D.Double(scale(0), 0, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(66), 0, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(-1), 2, scale(1), 2)); |
this.rectangles.add(new Rectangle2D.Double(scale(67), 2, scale(1), 2)); |
} |
this.symbol_height += 4; |
} |
/* Now add the text */ |
if (this.humanReadableLocation == BOTTOM) { |
this.symbol_height -= this.guardPatternExtraHeight; |
final double baseline = this.symbol_height + this.fontSize; |
if (this.mode == Mode.EAN13) { |
this.texts.add(new TextBox(scale(-9), baseline, scale(4), this.readable.substring(0, 1), HumanReadableAlignment.RIGHT)); |
this.texts.add(new TextBox(scale(5), baseline, scale(39), this.readable.substring(1, 7), this.humanReadableAlignment)); |
this.texts.add(new TextBox(scale(51), baseline, scale(39), this.readable.substring(7, 13), this.humanReadableAlignment)); |
} else { // EAN8 |
this.texts.add(new TextBox(scale(5), baseline, scale(25), this.readable.substring(0, 4), this.humanReadableAlignment)); |
this.texts.add(new TextBox(scale(37), baseline, scale(25), this.readable.substring(4, 8), this.humanReadableAlignment)); |
} |
} else if (this.humanReadableLocation == TOP) { |
final double baseline = this.fontSize; |
final int width = this.mode == Mode.EAN13 ? 94 : 66; |
this.texts.add(new TextBox(scale(0), baseline, scale(width), this.readable, this.humanReadableAlignment)); |
} |
/* Now add the add-on symbol, if necessary */ |
if (this.addOn != null) { |
final int gap = 9; |
final int baseX = this.symbol_width + scale(gap); |
final Rectangle2D.Double r1 = this.rectangles.get(0); |
final Rectangle2D.Double ar1 = this.addOn.rectangles.get(0); |
final int baseY = (int) (r1.y + r1.getHeight() - ar1.y - ar1.getHeight()); |
for (final TextBox t : this.addOn.getTexts()) { |
this.texts.add(new TextBox(baseX + t.x, baseY + t.y, t.width, t.text, t.alignment)); |
} |
for (final Rectangle2D.Double r : this.addOn.getRectangles()) { |
this.rectangles.add(new Rectangle2D.Double(baseX + r.x, baseY + r.y, r.width, r.height)); |
} |
this.symbol_width += scale(gap) + this.addOn.symbol_width; |
this.pattern[0] = this.pattern[0] + gap + this.addOn.pattern[0]; |
} |
} |
/** Scales the specified width or x-dimension according to the current module width. */ |
private int scale(final int w) { |
return this.moduleWidth * w; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Logmars.java |
---|
New file |
0,0 → 1,98 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.util.Arrays.positionOf; |
/** |
* Implements the LOGMARS (Logistics Applications of Automated Marking and Reading Symbols) standard |
* used by the US Department of Defense. Input data can be of any length and supports the characters |
* 0-9, A-Z, dash (-), full stop (.), space, dollar ($), slash (/), plus (+) and percent (%). A |
* Modulo-43 check digit is calculated and added, and should not form part of the input data. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class Logmars extends Symbol { |
private static final String[] CODE39LM = { "1113313111", "3113111131", "1133111131", "3133111111", "1113311131", "3113311111", "1133311111", "1113113131", "3113113111", "1133113111", "3111131131", |
"1131131131", "3131131111", "1111331131", "3111331111", "1131331111", "1111133131", "3111133111", "1131133111", "1111333111", "3111111331", "1131111331", "3131111311", "1111311331", |
"3111311311", "1131311311", "1111113331", "3111113311", "1131113311", "1111313311", "3311111131", "1331111131", "3331111111", "1311311131", "3311311111", "1331311111", "1311113131", |
"3311113111", "1331113111", "1313131111", "1313111311", "1311131311", "1113131311" }; |
private static final char[] LOOKUP = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', |
'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%' }; |
/** Ratio of wide bar width to narrow bar width. */ |
private double moduleWidthRatio = 3; |
/** |
* Sets the ratio of wide bar width to narrow bar width. Valid values are usually between |
* {@code 2} and {@code 3}. The default value is {@code 3}. |
* |
* @param moduleWidthRatio the ratio of wide bar width to narrow bar width |
*/ |
public void setModuleWidthRatio(final double moduleWidthRatio) { |
this.moduleWidthRatio = moduleWidthRatio; |
} |
/** |
* Returns the ratio of wide bar width to narrow bar width. |
* |
* @return the ratio of wide bar width to narrow bar width |
*/ |
public double getModuleWidthRatio() { |
return this.moduleWidthRatio; |
} |
/** {@inheritDoc} */ |
@Override |
protected double getModuleWidth(final int originalWidth) { |
if (originalWidth == 1) { |
return 1; |
} else { |
return this.moduleWidthRatio; |
} |
} |
/** {@inheritDoc} */ |
@Override |
protected void encode() { |
if (!this.content.matches("[0-9A-Z\\. \\-$/+%]*")) { |
throw new OkapiException("Invalid characters in input"); |
} |
String p = ""; |
final int l = this.content.length(); |
int charval, counter = 0; |
char thischar; |
char checkDigit; |
for (int i = 0; i < l; i++) { |
thischar = this.content.charAt(i); |
charval = positionOf(thischar, LOOKUP); |
counter += charval; |
p += CODE39LM[charval]; |
} |
counter = counter % 43; |
checkDigit = LOOKUP[counter]; |
infoLine("Check Digit: " + checkDigit); |
p += CODE39LM[counter]; |
this.readable = this.content + checkDigit; |
this.pattern = new String[] { "1311313111" + p + "131131311" }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/KoreaPost.java |
---|
New file |
0,0 → 1,64 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
/** |
* <p> |
* Implements Korea Post Barcode. Input should consist of of a six-digit number. A Modulo-10 check |
* digit is calculated and added, and should not form part of the input data. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class KoreaPost extends Symbol { |
private static final String[] KOREA_TABLE = { "1313150613", "0713131313", "0417131313", "1506131313", "0413171313", "17171313", "1315061313", "0413131713", "17131713", "13171713" }; |
@Override |
protected void encode() { |
if (!this.content.matches("[0-9]+")) { |
throw new OkapiException("Invalid characters in input"); |
} |
if (this.content.length() > 6) { |
throw new OkapiException("Input data too long"); |
} |
String padded = ""; |
for (int i = 0; i < 6 - this.content.length(); i++) { |
padded += "0"; |
} |
padded += this.content; |
int total = 0; |
String accumulator = ""; |
for (int i = 0; i < padded.length(); i++) { |
final int j = Character.getNumericValue(padded.charAt(i)); |
accumulator += KOREA_TABLE[j]; |
total += j; |
} |
int checkd = 10 - total % 10; |
if (checkd == 10) { |
checkd = 0; |
} |
infoLine("Check Digit: " + checkd); |
accumulator += KOREA_TABLE[checkd]; |
this.readable = padded + checkd; |
this.pattern = new String[] { accumulator }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Pharmacode2Track.java |
---|
New file |
0,0 → 1,117 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import java.awt.geom.Rectangle2D; |
/** |
* Implements the Two-Track Pharmacode bar code symbology. <br> |
* Pharmacode Two-Track is an alternative system to Pharmacode One-Track used for the identification |
* of pharmaceuticals. The symbology is able to encode whole numbers between 4 and 64570080. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class Pharmacode2Track extends Symbol { |
@Override |
protected void encode() { |
int i, tester = 0; |
String inter, dest; |
if (this.content.length() > 8) { |
throw new OkapiException("Input too long"); |
} |
if (!this.content.matches("[0-9]+")) { |
throw new OkapiException("Invalid characters in data"); |
} |
for (i = 0; i < this.content.length(); i++) { |
tester *= 10; |
tester += Character.getNumericValue(this.content.charAt(i)); |
} |
if (tester < 4 || tester > 64570080) { |
throw new OkapiException("Data out of range"); |
} |
inter = ""; |
do { |
switch (tester % 3) { |
case 0: |
inter += "F"; |
tester = (tester - 3) / 3; |
break; |
case 1: |
inter += "D"; |
tester = (tester - 1) / 3; |
break; |
case 2: |
inter += "A"; |
tester = (tester - 2) / 3; |
break; |
} |
} while (tester != 0); |
dest = ""; |
for (i = inter.length() - 1; i >= 0; i--) { |
dest += inter.charAt(i); |
} |
infoLine("Encoding: " + dest); |
this.readable = ""; |
this.pattern = new String[1]; |
this.pattern[0] = dest; |
this.row_count = 1; |
this.row_height = new int[1]; |
this.row_height[0] = -1; |
} |
@Override |
protected void plotSymbol() { |
int xBlock; |
int x, y, w, h; |
this.rectangles.clear(); |
x = 0; |
w = 1; |
y = 0; |
h = 0; |
for (xBlock = 0; xBlock < this.pattern[0].length(); xBlock++) { |
switch (this.pattern[0].charAt(xBlock)) { |
case 'A': |
y = 0; |
h = this.default_height / 2; |
break; |
case 'D': |
y = this.default_height / 2; |
h = this.default_height / 2; |
break; |
case 'F': |
y = 0; |
h = this.default_height; |
break; |
} |
final Rectangle2D.Double rect = new Rectangle2D.Double(x, y, w, h); |
this.rectangles.add(rect); |
x += 2; |
} |
this.symbol_width = this.pattern[0].length() * 2; |
this.symbol_height = this.default_height; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/CodeOne.java |
---|
New file |
0,0 → 1,1869 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import java.math.BigInteger; |
import java.nio.charset.StandardCharsets; |
/** |
* <p> |
* Implements Code One. |
* |
* <p> |
* Code One is able to encode the ISO 8859-1 (Latin-1) character set or GS1 data. There are two |
* types of Code One symbol: variable height symbols which are roughly square (versions A thought to |
* H) and fixed-height versions (version S and T). Version S symbols can only encode numeric data. |
* The width of version S and version T symbols is determined by the length of the input data. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class CodeOne extends Symbol { |
public enum Version { |
NONE, A, B, C, D, E, F, G, H, S, T |
} |
private static final int[] C40_SHIFT = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }; |
private static final int[] C40_VALUE = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
11, 12, 13, 14, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 22, |
23, 24, 25, 26, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }; |
private static final int[] TEXT_SHIFT = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3 }; |
private static final int[] TEXT_VALUE = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
11, 12, 13, 14, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 22, 23, 24, 25, |
26, 0, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 27, 28, 29, 30, 31 }; |
private static final int[] C1_HEIGHT = { 16, 22, 28, 40, 52, 70, 104, 148 }; |
private static final int[] C1_WIDTH = { 18, 22, 32, 42, 54, 76, 98, 134 }; |
private static final int[] C1_DATA_LENGTH = { 10, 19, 44, 91, 182, 370, 732, 1480 }; |
private static final int[] C1_ECC_LENGTH = { 10, 16, 26, 44, 70, 140, 280, 560 }; |
private static final int[] C1_BLOCKS = { 1, 1, 1, 1, 1, 2, 4, 8 }; |
private static final int[] C1_DATA_BLOCKS = { 10, 19, 44, 91, 182, 185, 183, 185 }; |
private static final int[] C1_ECC_BLOCKS = { 10, 16, 26, 44, 70, 70, 70, 70 }; |
private static final int[] C1_GRID_WIDTH = { 4, 5, 7, 9, 12, 17, 22, 30 }; |
private static final int[] C1_GRID_HEIGHT = { 5, 7, 10, 15, 21, 30, 46, 68 }; |
private enum Mode { |
C1_ASCII, C1_C40, C1_DECIMAL, C1_TEXT, C1_EDI, C1_BYTE |
} |
private Version preferredVersion = Version.NONE; |
private final int[] data = new int[1500]; |
private final int[][] datagrid = new int[136][120]; |
private final boolean[][] outputGrid = new boolean[148][134]; |
/** |
* Sets the preferred symbol size / version. Versions A to H are square symbols. Version S and T |
* are fixed height symbols. This value may be ignored if the input data does not fit in the |
* specified version. |
* |
* @param version the preferred symbol version |
*/ |
public void setPreferredVersion(final Version version) { |
this.preferredVersion = version; |
} |
/** |
* Returns the preferred symbol version. |
* |
* @return the preferred symbol version |
*/ |
public Version getPreferredVersion() { |
return this.preferredVersion; |
} |
@Override |
protected boolean gs1Supported() { |
return true; |
} |
@Override |
protected void encode() { |
int size = 1, i, j, data_blocks; |
int row, col; |
int sub_version = 0; |
int codewords; |
final int[] ecc = new int[600]; |
final int[] stream = new int[2100]; |
int block_width; |
final int length = this.content.length(); |
final ReedSolomon rs = new ReedSolomon(); |
int data_length; |
int data_cw, ecc_cw; |
final int[] sub_data = new int[190]; |
final StringBuilder bin = new StringBuilder(); |
if (!this.content.matches("[\u0000-\u00FF]+")) { |
throw new OkapiException("Invalid characters in input data"); |
} |
if (this.preferredVersion == Version.S) { |
/* Version S */ |
infoLine("Version: S"); |
if (length > 18) { |
throw new OkapiException("Input data too long"); |
} |
if (!this.content.matches("[0-9]+?")) { |
throw new OkapiException("Invalid characters in input"); |
} |
sub_version = 3; |
codewords = 12; |
block_width = 6; /* Version S-30 */ |
if (length <= 12) { |
sub_version = 2; |
codewords = 8; |
block_width = 4; |
} /* Version S-20 */ |
if (length <= 6) { |
sub_version = 1; |
codewords = 4; |
block_width = 2; |
} /* Version S-10 */ |
final BigInteger elreg = new BigInteger(this.content); |
for (i = 0; i < codewords; i++) { |
BigInteger codewordValue = elreg.shiftRight(5 * i); |
codewordValue = codewordValue.and(BigInteger.valueOf(0b11111)); |
this.data[codewords - i - 1] = codewordValue.intValue(); |
} |
logCodewords(codewords); |
rs.init_gf(0x25); |
rs.init_code(codewords, 1); |
rs.encode(codewords, this.data); |
infoLine("ECC Codeword Count: " + codewords); |
for (i = 0; i < codewords; i++) { |
stream[i] = this.data[i]; |
stream[i + codewords] = rs.getResult(codewords - i - 1); |
} |
for (i = 0; i < 136; i++) { |
for (j = 0; j < 120; j++) { |
this.datagrid[i][j] = '0'; |
} |
} |
i = 0; |
for (row = 0; row < 2; row++) { |
for (col = 0; col < block_width; col++) { |
if ((stream[i] & 0x10) != 0) { |
this.datagrid[row * 2][col * 5] = '1'; |
} |
if ((stream[i] & 0x08) != 0) { |
this.datagrid[row * 2][col * 5 + 1] = '1'; |
} |
if ((stream[i] & 0x04) != 0) { |
this.datagrid[row * 2][col * 5 + 2] = '1'; |
} |
if ((stream[i] & 0x02) != 0) { |
this.datagrid[row * 2 + 1][col * 5] = '1'; |
} |
if ((stream[i] & 0x01) != 0) { |
this.datagrid[row * 2 + 1][col * 5 + 1] = '1'; |
} |
if ((stream[i + 1] & 0x10) != 0) { |
this.datagrid[row * 2][col * 5 + 3] = '1'; |
} |
if ((stream[i + 1] & 0x08) != 0) { |
this.datagrid[row * 2][col * 5 + 4] = '1'; |
} |
if ((stream[i + 1] & 0x04) != 0) { |
this.datagrid[row * 2 + 1][col * 5 + 2] = '1'; |
} |
if ((stream[i + 1] & 0x02) != 0) { |
this.datagrid[row * 2 + 1][col * 5 + 3] = '1'; |
} |
if ((stream[i + 1] & 0x01) != 0) { |
this.datagrid[row * 2 + 1][col * 5 + 4] = '1'; |
} |
i += 2; |
} |
} |
infoLine("Grid Size: " + block_width + " X " + 2); |
size = 9; |
this.row_count = 8; |
this.symbol_width = 10 * sub_version + 1; |
} |
if (this.preferredVersion == Version.T) { |
/* Version T */ |
infoLine("Version: T"); |
for (i = 0; i < 40; i++) { |
this.data[i] = 0; |
} |
data_length = encodeAsCode1Data(); |
if (data_length > 38) { |
throw new OkapiException("Input data too long"); |
} |
size = 10; |
sub_version = 3; |
data_cw = 38; |
ecc_cw = 22; |
block_width = 12; |
if (data_length <= 24) { |
sub_version = 2; |
data_cw = 24; |
ecc_cw = 16; |
block_width = 8; |
} |
if (data_length <= 10) { |
sub_version = 1; |
data_cw = 10; |
ecc_cw = 10; |
block_width = 4; |
} |
logCodewords(data_length); |
for (i = data_length; i < data_cw; i++) { |
this.data[i] = 129; /* Pad */ |
} |
/* Calculate error correction data */ |
rs.init_gf(0x12d); |
rs.init_code(ecc_cw, 1); |
rs.encode(data_cw, this.data); |
infoLine("ECC Codeword Count: " + ecc_cw); |
/* "Stream" combines data and error correction data */ |
for (i = 0; i < data_cw; i++) { |
stream[i] = this.data[i]; |
} |
for (i = 0; i < ecc_cw; i++) { |
stream[data_cw + i] = rs.getResult(ecc_cw - i - 1); |
} |
for (i = 0; i < 136; i++) { |
for (j = 0; j < 120; j++) { |
this.datagrid[i][j] = '0'; |
} |
} |
i = 0; |
for (row = 0; row < 5; row++) { |
for (col = 0; col < block_width; col++) { |
if ((stream[i] & 0x80) != 0) { |
this.datagrid[row * 2][col * 4] = '1'; |
} |
if ((stream[i] & 0x40) != 0) { |
this.datagrid[row * 2][col * 4 + 1] = '1'; |
} |
if ((stream[i] & 0x20) != 0) { |
this.datagrid[row * 2][col * 4 + 2] = '1'; |
} |
if ((stream[i] & 0x10) != 0) { |
this.datagrid[row * 2][col * 4 + 3] = '1'; |
} |
if ((stream[i] & 0x08) != 0) { |
this.datagrid[row * 2 + 1][col * 4] = '1'; |
} |
if ((stream[i] & 0x04) != 0) { |
this.datagrid[row * 2 + 1][col * 4 + 1] = '1'; |
} |
if ((stream[i] & 0x02) != 0) { |
this.datagrid[row * 2 + 1][col * 4 + 2] = '1'; |
} |
if ((stream[i] & 0x01) != 0) { |
this.datagrid[row * 2 + 1][col * 4 + 3] = '1'; |
} |
i++; |
} |
} |
infoLine("Grid Size: " + block_width + " X " + 5); |
this.row_count = 16; |
this.symbol_width = sub_version * 16 + 1; |
} |
if (this.preferredVersion != Version.S && this.preferredVersion != Version.T) { |
/* Version A to H */ |
for (i = 0; i < 1500; i++) { |
this.data[i] = 0; |
} |
data_length = encodeAsCode1Data(); |
for (i = 7; i >= 0; i--) { |
if (C1_DATA_LENGTH[i] >= data_length) { |
size = i + 1; |
} |
} |
if (getSize(this.preferredVersion) > size) { |
size = getSize(this.preferredVersion); |
} |
final char version = (char) (size - 1 + 'A'); |
infoLine("Version: " + version); |
logCodewords(data_length); |
for (i = data_length; i < C1_DATA_LENGTH[size - 1]; i++) { |
this.data[i] = 129; /* Pad */ |
} |
/* Calculate error correction data */ |
data_length = C1_DATA_LENGTH[size - 1]; |
data_blocks = C1_BLOCKS[size - 1]; |
rs.init_gf(0x12d); |
rs.init_code(C1_ECC_BLOCKS[size - 1], 0); |
for (i = 0; i < data_blocks; i++) { |
for (j = 0; j < C1_DATA_BLOCKS[size - 1]; j++) { |
sub_data[j] = this.data[j * data_blocks + i]; |
} |
rs.encode(C1_DATA_BLOCKS[size - 1], sub_data); |
for (j = 0; j < C1_ECC_BLOCKS[size - 1]; j++) { |
ecc[C1_ECC_LENGTH[size - 1] - (j * data_blocks + i) - 1] = rs.getResult(j); |
} |
} |
infoLine("ECC Codeword Count: " + C1_ECC_LENGTH[size - 1]); |
/* "Stream" combines data and error correction data */ |
for (i = 0; i < data_length; i++) { |
stream[i] = this.data[i]; |
} |
for (i = 0; i < C1_ECC_LENGTH[size - 1]; i++) { |
stream[data_length + i] = ecc[i]; |
} |
for (i = 0; i < 136; i++) { |
for (j = 0; j < 120; j++) { |
this.datagrid[i][j] = '0'; |
} |
} |
i = 0; |
for (row = 0; row < C1_GRID_HEIGHT[size - 1]; row++) { |
for (col = 0; col < C1_GRID_WIDTH[size - 1]; col++) { |
if ((stream[i] & 0x80) != 0) { |
this.datagrid[row * 2][col * 4] = '1'; |
} |
if ((stream[i] & 0x40) != 0) { |
this.datagrid[row * 2][col * 4 + 1] = '1'; |
} |
if ((stream[i] & 0x20) != 0) { |
this.datagrid[row * 2][col * 4 + 2] = '1'; |
} |
if ((stream[i] & 0x10) != 0) { |
this.datagrid[row * 2][col * 4 + 3] = '1'; |
} |
if ((stream[i] & 0x08) != 0) { |
this.datagrid[row * 2 + 1][col * 4] = '1'; |
} |
if ((stream[i] & 0x04) != 0) { |
this.datagrid[row * 2 + 1][col * 4 + 1] = '1'; |
} |
if ((stream[i] & 0x02) != 0) { |
this.datagrid[row * 2 + 1][col * 4 + 2] = '1'; |
} |
if ((stream[i] & 0x01) != 0) { |
this.datagrid[row * 2 + 1][col * 4 + 3] = '1'; |
} |
i++; |
} |
} |
infoLine("Grid Size: " + C1_GRID_WIDTH[size - 1] + " X " + C1_GRID_HEIGHT[size - 1]); |
this.row_count = C1_HEIGHT[size - 1]; |
this.symbol_width = C1_WIDTH[size - 1]; |
} |
for (i = 0; i < 148; i++) { |
for (j = 0; j < 134; j++) { |
this.outputGrid[i][j] = false; |
} |
} |
switch (size) { |
case 1: |
/* Version A */ |
plotCentralFinder(6, 3, 1); |
plotVerticalBar(4, 6, 1); |
plotVerticalBar(12, 5, 0); |
setGridModule(5, 12); |
plotSpigot(0); |
plotSpigot(15); |
plotDataBlock(0, 0, 5, 4, 0, 0); |
plotDataBlock(0, 4, 5, 12, 0, 2); |
plotDataBlock(5, 0, 5, 12, 6, 0); |
plotDataBlock(5, 12, 5, 4, 6, 2); |
break; |
case 2: |
/* Version B */ |
plotCentralFinder(8, 4, 1); |
plotVerticalBar(4, 8, 1); |
plotVerticalBar(16, 7, 0); |
setGridModule(7, 16); |
plotSpigot(0); |
plotSpigot(21); |
plotDataBlock(0, 0, 7, 4, 0, 0); |
plotDataBlock(0, 4, 7, 16, 0, 2); |
plotDataBlock(7, 0, 7, 16, 8, 0); |
plotDataBlock(7, 16, 7, 4, 8, 2); |
break; |
case 3: |
/* Version C */ |
plotCentralFinder(11, 4, 2); |
plotVerticalBar(4, 11, 1); |
plotVerticalBar(26, 13, 1); |
plotVerticalBar(4, 10, 0); |
plotVerticalBar(26, 10, 0); |
plotSpigot(0); |
plotSpigot(27); |
plotDataBlock(0, 0, 10, 4, 0, 0); |
plotDataBlock(0, 4, 10, 20, 0, 2); |
plotDataBlock(0, 24, 10, 4, 0, 4); |
plotDataBlock(10, 0, 10, 4, 8, 0); |
plotDataBlock(10, 4, 10, 20, 8, 2); |
plotDataBlock(10, 24, 10, 4, 8, 4); |
break; |
case 4: |
/* Version D */ |
plotCentralFinder(16, 5, 1); |
plotVerticalBar(4, 16, 1); |
plotVerticalBar(20, 16, 1); |
plotVerticalBar(36, 16, 1); |
plotVerticalBar(4, 15, 0); |
plotVerticalBar(20, 15, 0); |
plotVerticalBar(36, 15, 0); |
plotSpigot(0); |
plotSpigot(12); |
plotSpigot(27); |
plotSpigot(39); |
plotDataBlock(0, 0, 15, 4, 0, 0); |
plotDataBlock(0, 4, 15, 14, 0, 2); |
plotDataBlock(0, 18, 15, 14, 0, 4); |
plotDataBlock(0, 32, 15, 4, 0, 6); |
plotDataBlock(15, 0, 15, 4, 10, 0); |
plotDataBlock(15, 4, 15, 14, 10, 2); |
plotDataBlock(15, 18, 15, 14, 10, 4); |
plotDataBlock(15, 32, 15, 4, 10, 6); |
break; |
case 5: |
/* Version E */ |
plotCentralFinder(22, 5, 2); |
plotVerticalBar(4, 22, 1); |
plotVerticalBar(26, 24, 1); |
plotVerticalBar(48, 22, 1); |
plotVerticalBar(4, 21, 0); |
plotVerticalBar(26, 21, 0); |
plotVerticalBar(48, 21, 0); |
plotSpigot(0); |
plotSpigot(12); |
plotSpigot(39); |
plotSpigot(51); |
plotDataBlock(0, 0, 21, 4, 0, 0); |
plotDataBlock(0, 4, 21, 20, 0, 2); |
plotDataBlock(0, 24, 21, 20, 0, 4); |
plotDataBlock(0, 44, 21, 4, 0, 6); |
plotDataBlock(21, 0, 21, 4, 10, 0); |
plotDataBlock(21, 4, 21, 20, 10, 2); |
plotDataBlock(21, 24, 21, 20, 10, 4); |
plotDataBlock(21, 44, 21, 4, 10, 6); |
break; |
case 6: |
/* Version F */ |
plotCentralFinder(31, 5, 3); |
plotVerticalBar(4, 31, 1); |
plotVerticalBar(26, 35, 1); |
plotVerticalBar(48, 31, 1); |
plotVerticalBar(70, 35, 1); |
plotVerticalBar(4, 30, 0); |
plotVerticalBar(26, 30, 0); |
plotVerticalBar(48, 30, 0); |
plotVerticalBar(70, 30, 0); |
plotSpigot(0); |
plotSpigot(12); |
plotSpigot(24); |
plotSpigot(45); |
plotSpigot(57); |
plotSpigot(69); |
plotDataBlock(0, 0, 30, 4, 0, 0); |
plotDataBlock(0, 4, 30, 20, 0, 2); |
plotDataBlock(0, 24, 30, 20, 0, 4); |
plotDataBlock(0, 44, 30, 20, 0, 6); |
plotDataBlock(0, 64, 30, 4, 0, 8); |
plotDataBlock(30, 0, 30, 4, 10, 0); |
plotDataBlock(30, 4, 30, 20, 10, 2); |
plotDataBlock(30, 24, 30, 20, 10, 4); |
plotDataBlock(30, 44, 30, 20, 10, 6); |
plotDataBlock(30, 64, 30, 4, 10, 8); |
break; |
case 7: |
/* Version G */ |
plotCentralFinder(47, 6, 2); |
plotVerticalBar(6, 47, 1); |
plotVerticalBar(27, 49, 1); |
plotVerticalBar(48, 47, 1); |
plotVerticalBar(69, 49, 1); |
plotVerticalBar(90, 47, 1); |
plotVerticalBar(6, 46, 0); |
plotVerticalBar(27, 46, 0); |
plotVerticalBar(48, 46, 0); |
plotVerticalBar(69, 46, 0); |
plotVerticalBar(90, 46, 0); |
plotSpigot(0); |
plotSpigot(12); |
plotSpigot(24); |
plotSpigot(36); |
plotSpigot(67); |
plotSpigot(79); |
plotSpigot(91); |
plotSpigot(103); |
plotDataBlock(0, 0, 46, 6, 0, 0); |
plotDataBlock(0, 6, 46, 19, 0, 2); |
plotDataBlock(0, 25, 46, 19, 0, 4); |
plotDataBlock(0, 44, 46, 19, 0, 6); |
plotDataBlock(0, 63, 46, 19, 0, 8); |
plotDataBlock(0, 82, 46, 6, 0, 10); |
plotDataBlock(46, 0, 46, 6, 12, 0); |
plotDataBlock(46, 6, 46, 19, 12, 2); |
plotDataBlock(46, 25, 46, 19, 12, 4); |
plotDataBlock(46, 44, 46, 19, 12, 6); |
plotDataBlock(46, 63, 46, 19, 12, 8); |
plotDataBlock(46, 82, 46, 6, 12, 10); |
break; |
case 8: |
/* Version H */ |
plotCentralFinder(69, 6, 3); |
plotVerticalBar(6, 69, 1); |
plotVerticalBar(26, 73, 1); |
plotVerticalBar(46, 69, 1); |
plotVerticalBar(66, 73, 1); |
plotVerticalBar(86, 69, 1); |
plotVerticalBar(106, 73, 1); |
plotVerticalBar(126, 69, 1); |
plotVerticalBar(6, 68, 0); |
plotVerticalBar(26, 68, 0); |
plotVerticalBar(46, 68, 0); |
plotVerticalBar(66, 68, 0); |
plotVerticalBar(86, 68, 0); |
plotVerticalBar(106, 68, 0); |
plotVerticalBar(126, 68, 0); |
plotSpigot(0); |
plotSpigot(12); |
plotSpigot(24); |
plotSpigot(36); |
plotSpigot(48); |
plotSpigot(60); |
plotSpigot(87); |
plotSpigot(99); |
plotSpigot(111); |
plotSpigot(123); |
plotSpigot(135); |
plotSpigot(147); |
plotDataBlock(0, 0, 68, 6, 0, 0); |
plotDataBlock(0, 6, 68, 18, 0, 2); |
plotDataBlock(0, 24, 68, 18, 0, 4); |
plotDataBlock(0, 42, 68, 18, 0, 6); |
plotDataBlock(0, 60, 68, 18, 0, 8); |
plotDataBlock(0, 78, 68, 18, 0, 10); |
plotDataBlock(0, 96, 68, 18, 0, 12); |
plotDataBlock(0, 114, 68, 6, 0, 14); |
plotDataBlock(68, 0, 68, 6, 12, 0); |
plotDataBlock(68, 6, 68, 18, 12, 2); |
plotDataBlock(68, 24, 68, 18, 12, 4); |
plotDataBlock(68, 42, 68, 18, 12, 6); |
plotDataBlock(68, 60, 68, 18, 12, 8); |
plotDataBlock(68, 78, 68, 18, 12, 10); |
plotDataBlock(68, 96, 68, 18, 12, 12); |
plotDataBlock(68, 114, 68, 6, 12, 14); |
break; |
case 9: |
/* Version S */ |
plotHorizontalBar(5, 1); |
plotHorizontalBar(7, 1); |
setGridModule(6, 0); |
setGridModule(6, this.symbol_width - 1); |
resetGridModule(7, 1); |
resetGridModule(7, this.symbol_width - 2); |
switch (sub_version) { |
case 1: |
/* Version S-10 */ |
setGridModule(0, 5); |
plotDataBlock(0, 0, 4, 5, 0, 0); |
plotDataBlock(0, 5, 4, 5, 0, 1); |
break; |
case 2: |
/* Version S-20 */ |
setGridModule(0, 10); |
setGridModule(4, 10); |
plotDataBlock(0, 0, 4, 10, 0, 0); |
plotDataBlock(0, 10, 4, 10, 0, 1); |
break; |
case 3: |
/* Version S-30 */ |
setGridModule(0, 15); |
setGridModule(4, 15); |
setGridModule(6, 15); |
plotDataBlock(0, 0, 4, 15, 0, 0); |
plotDataBlock(0, 15, 4, 15, 0, 1); |
break; |
} |
break; |
case 10: |
/* Version T */ |
plotHorizontalBar(11, 1); |
plotHorizontalBar(13, 1); |
plotHorizontalBar(15, 1); |
setGridModule(12, 0); |
setGridModule(12, this.symbol_width - 1); |
setGridModule(14, 0); |
setGridModule(14, this.symbol_width - 1); |
resetGridModule(13, 1); |
resetGridModule(13, this.symbol_width - 2); |
resetGridModule(15, 1); |
resetGridModule(15, this.symbol_width - 2); |
switch (sub_version) { |
case 1: |
/* Version T-16 */ |
setGridModule(0, 8); |
setGridModule(10, 8); |
plotDataBlock(0, 0, 10, 8, 0, 0); |
plotDataBlock(0, 8, 10, 8, 0, 1); |
break; |
case 2: |
/* Version T-32 */ |
setGridModule(0, 16); |
setGridModule(10, 16); |
setGridModule(12, 16); |
plotDataBlock(0, 0, 10, 16, 0, 0); |
plotDataBlock(0, 16, 10, 16, 0, 1); |
break; |
case 3: |
/* Verion T-48 */ |
setGridModule(0, 24); |
setGridModule(10, 24); |
setGridModule(12, 24); |
setGridModule(14, 24); |
plotDataBlock(0, 0, 10, 24, 0, 0); |
plotDataBlock(0, 24, 10, 24, 0, 1); |
break; |
} |
break; |
} |
this.readable = ""; |
this.pattern = new String[this.row_count]; |
this.row_height = new int[this.row_count]; |
for (i = 0; i < this.row_count; i++) { |
bin.setLength(0); |
for (j = 0; j < this.symbol_width; j++) { |
if (this.outputGrid[i][j]) { |
bin.append('1'); |
} else { |
bin.append('0'); |
} |
} |
this.pattern[i] = bin2pat(bin); |
this.row_height[i] = 1; |
} |
} |
private void logCodewords(final int count) { |
info("Codewords: "); |
for (int i = 0; i < count; i++) { |
infoSpace(this.data[i]); |
} |
infoLine(); |
} |
private int encodeAsCode1Data() { |
Mode current_mode, next_mode; |
boolean latch; |
boolean done; |
int sourcePoint, targetPoint, i, j; |
int c40_p; |
int text_p; |
int edi_p; |
int byte_start = 0; |
final int[] c40_buffer = new int[6]; |
final int[] text_buffer = new int[6]; |
final int[] edi_buffer = new int[6]; |
String decimal_binary = ""; |
int length; |
int shift_set, value; |
int data_left, decimal_count; |
int sub_value; |
int bits_left_in_byte, target_count; |
boolean isTwoDigits; |
this.inputData = toBytes(this.content, StandardCharsets.ISO_8859_1); |
length = this.inputData.length; |
sourcePoint = 0; |
targetPoint = 0; |
c40_p = 0; |
text_p = 0; |
edi_p = 0; |
if (this.inputDataType == DataType.GS1) { |
this.data[targetPoint] = 232; |
targetPoint++; |
} /* FNC1 */ |
/* Step A */ |
current_mode = Mode.C1_ASCII; |
next_mode = Mode.C1_ASCII; |
do { |
if (current_mode != next_mode) { |
/* Change mode */ |
switch (next_mode) { |
case C1_C40: |
this.data[targetPoint] = 230; |
targetPoint++; |
break; |
case C1_TEXT: |
this.data[targetPoint] = 239; |
targetPoint++; |
break; |
case C1_EDI: |
this.data[targetPoint] = 238; |
targetPoint++; |
break; |
case C1_BYTE: |
this.data[targetPoint] = 231; |
targetPoint++; |
break; |
} |
} |
if (current_mode != Mode.C1_BYTE && next_mode == Mode.C1_BYTE) { |
byte_start = targetPoint; |
} |
current_mode = next_mode; |
if (current_mode == Mode.C1_ASCII) { /* Step B - ASCII encodation */ |
next_mode = Mode.C1_ASCII; |
if (length - sourcePoint >= 21) { /* Step B1 */ |
j = 0; |
for (i = 0; i < 21; i++) { |
if (this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9') { |
j++; |
} |
} |
if (j == 21) { |
next_mode = Mode.C1_DECIMAL; |
decimal_binary += "1111"; |
} |
} |
if (next_mode == Mode.C1_ASCII && length - sourcePoint >= 13) { /* Step B2 */ |
j = 0; |
for (i = 0; i < 13; i++) { |
if (this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9') { |
j++; |
} |
} |
if (j == 13) { |
latch = false; |
for (i = sourcePoint + 13; i < length; i++) { |
if (!(this.inputData[i] >= '0' && this.inputData[i] <= '9')) { |
latch = true; |
} |
} |
if (!latch) { |
next_mode = Mode.C1_DECIMAL; |
decimal_binary += "1111"; |
} |
} |
} |
if (next_mode == Mode.C1_ASCII) { /* Step B3 */ |
isTwoDigits = false; |
if (sourcePoint + 1 != length) { |
if (this.inputData[sourcePoint] >= '0' && this.inputData[sourcePoint] <= '9') { |
if (this.inputData[sourcePoint + 1] >= '0' && this.inputData[sourcePoint + 1] <= '9') { |
// remaining data consists of two numeric digits |
this.data[targetPoint] = 10 * (this.inputData[sourcePoint] - '0') + this.inputData[sourcePoint + 1] - '0' + 130; |
targetPoint++; |
sourcePoint += 2; |
isTwoDigits = true; |
} |
} |
} |
if (!isTwoDigits) { |
if (this.inputData[sourcePoint] == FNC1) { |
if (length - sourcePoint >= 15) { /* Step B4 */ |
j = 0; |
for (i = 0; i < 15; i++) { |
if (this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9') { |
j++; |
} |
} |
if (j == 15) { |
this.data[targetPoint] = 236; /* FNC1 and change to Decimal */ |
targetPoint++; |
sourcePoint++; |
next_mode = Mode.C1_DECIMAL; |
} |
} |
if (length - sourcePoint >= 7) { /* Step B5 */ |
j = 0; |
for (i = 0; i < 7; i++) { |
if (this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9') { |
j++; |
} |
} |
if (j == 7) { |
latch = false; |
for (i = sourcePoint + 7; i < length; i++) { |
if (!(this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9')) { |
latch = true; |
} |
} |
if (!latch) { |
this.data[targetPoint] = 236; /* |
* FNC1 and change to Decimal |
*/ |
targetPoint++; |
sourcePoint++; |
next_mode = Mode.C1_DECIMAL; |
} |
} |
} |
} |
if (next_mode == Mode.C1_ASCII) { |
/* Step B6 */ |
next_mode = lookAheadTest(length, sourcePoint, current_mode); |
if (next_mode == Mode.C1_ASCII) { |
if (this.inputData[sourcePoint] > 127) { |
/* Step B7 */ |
this.data[targetPoint] = 235; |
targetPoint++; /* FNC4 */ |
this.data[targetPoint] = this.inputData[sourcePoint] - 128 + 1; |
targetPoint++; |
sourcePoint++; |
} else { |
/* Step B8 */ |
if (this.inputData[sourcePoint] == FNC1) { |
this.data[targetPoint] = 232; |
targetPoint++; |
sourcePoint++; /* FNC1 */ |
} else { |
this.data[targetPoint] = this.inputData[sourcePoint] + 1; |
targetPoint++; |
sourcePoint++; |
} |
} |
} |
} |
} |
} |
} |
if (current_mode == Mode.C1_C40) { /* Step C - C40 encodation */ |
done = false; |
next_mode = Mode.C1_C40; |
if (c40_p == 0) { |
if (length - sourcePoint >= 12) { |
j = 0; |
for (i = 0; i < 12; i++) { |
if (this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9') { |
j++; |
} |
} |
if (j == 12) { |
next_mode = Mode.C1_ASCII; |
done = true; |
} |
} |
if (length - sourcePoint >= 8) { |
j = 0; |
for (i = 0; i < 8; i++) { |
if (this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9') { |
j++; |
} |
} |
if (length - sourcePoint == 8) { |
latch = true; |
} else { |
latch = true; |
for (j = sourcePoint + 8; j < length; j++) { |
if (this.inputData[j] <= '0' || this.inputData[j] >= '9') { |
latch = false; |
} |
} |
} |
if (j == 8 && latch) { |
next_mode = Mode.C1_ASCII; |
done = true; |
} |
} |
if (!done) { |
next_mode = lookAheadTest(length, sourcePoint, current_mode); |
} |
} |
if (next_mode != Mode.C1_C40) { |
this.data[targetPoint] = 255; |
targetPoint++; /* Unlatch */ |
} else { |
if (this.inputData[sourcePoint] > 127) { |
c40_buffer[c40_p] = 1; |
c40_p++; |
c40_buffer[c40_p] = 30; |
c40_p++; /* Upper Shift */ |
shift_set = C40_SHIFT[this.inputData[sourcePoint] - 128]; |
value = C40_VALUE[this.inputData[sourcePoint] - 128]; |
} else { |
shift_set = C40_SHIFT[this.inputData[sourcePoint]]; |
value = C40_VALUE[this.inputData[sourcePoint]]; |
} |
if (this.inputData[sourcePoint] == FNC1) { |
shift_set = 2; |
value = 27; /* FNC1 */ |
} |
if (shift_set != 0) { |
c40_buffer[c40_p] = shift_set - 1; |
c40_p++; |
} |
c40_buffer[c40_p] = value; |
c40_p++; |
if (c40_p >= 3) { |
int iv; |
iv = 1600 * c40_buffer[0] + 40 * c40_buffer[1] + c40_buffer[2] + 1; |
this.data[targetPoint] = iv / 256; |
targetPoint++; |
this.data[targetPoint] = iv % 256; |
targetPoint++; |
c40_buffer[0] = c40_buffer[3]; |
c40_buffer[1] = c40_buffer[4]; |
c40_buffer[2] = c40_buffer[5]; |
c40_buffer[3] = 0; |
c40_buffer[4] = 0; |
c40_buffer[5] = 0; |
c40_p -= 3; |
} |
sourcePoint++; |
} |
} |
if (current_mode == Mode.C1_TEXT) { /* Step D - Text encodation */ |
done = false; |
next_mode = Mode.C1_TEXT; |
if (text_p == 0) { |
if (length - sourcePoint >= 12) { |
j = 0; |
for (i = 0; i < 12; i++) { |
if (this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9') { |
j++; |
} |
} |
if (j == 12) { |
next_mode = Mode.C1_ASCII; |
done = true; |
} |
} |
if (length - sourcePoint >= 8) { |
j = 0; |
for (i = 0; i < 8; i++) { |
if (this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9') { |
j++; |
} |
} |
if (length - sourcePoint == 8) { |
latch = true; |
} else { |
latch = true; |
for (j = sourcePoint + 8; j < length; j++) { |
if (this.inputData[j] <= '0' || this.inputData[j] >= '9') { |
latch = false; |
} |
} |
} |
if (j == 8 && latch) { |
next_mode = Mode.C1_ASCII; |
done = true; |
} |
} |
if (!done) { |
next_mode = lookAheadTest(length, sourcePoint, current_mode); |
} |
} |
if (next_mode != Mode.C1_TEXT) { |
this.data[targetPoint] = 255; |
targetPoint++; /* Unlatch */ |
} else { |
if (this.inputData[sourcePoint] > 127) { |
text_buffer[text_p] = 1; |
text_p++; |
text_buffer[text_p] = 30; |
text_p++; /* Upper Shift */ |
shift_set = TEXT_SHIFT[this.inputData[sourcePoint] - 128]; |
value = TEXT_VALUE[this.inputData[sourcePoint] - 128]; |
} else { |
shift_set = TEXT_SHIFT[this.inputData[sourcePoint]]; |
value = TEXT_VALUE[this.inputData[sourcePoint]]; |
} |
if (this.inputData[sourcePoint] == FNC1) { |
shift_set = 2; |
value = 27; /* FNC1 */ |
} |
if (shift_set != 0) { |
text_buffer[text_p] = shift_set - 1; |
text_p++; |
} |
text_buffer[text_p] = value; |
text_p++; |
if (text_p >= 3) { |
int iv; |
iv = 1600 * text_buffer[0] + 40 * text_buffer[1] + text_buffer[2] + 1; |
this.data[targetPoint] = iv / 256; |
targetPoint++; |
this.data[targetPoint] = iv % 256; |
targetPoint++; |
text_buffer[0] = text_buffer[3]; |
text_buffer[1] = text_buffer[4]; |
text_buffer[2] = text_buffer[5]; |
text_buffer[3] = 0; |
text_buffer[4] = 0; |
text_buffer[5] = 0; |
text_p -= 3; |
} |
sourcePoint++; |
} |
} |
if (current_mode == Mode.C1_EDI) { /* Step E - EDI Encodation */ |
value = 0; |
next_mode = Mode.C1_EDI; |
if (edi_p == 0) { |
if (length - sourcePoint >= 12) { |
j = 0; |
for (i = 0; i < 12; i++) { |
if (this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9') { |
j++; |
} |
} |
if (j == 12) { |
next_mode = Mode.C1_ASCII; |
} |
} |
if (length - sourcePoint >= 8) { |
j = 0; |
for (i = 0; i < 8; i++) { |
if (this.inputData[sourcePoint + i] >= '0' && this.inputData[sourcePoint + i] <= '9') { |
j++; |
} |
} |
if (length - sourcePoint == 8) { |
latch = true; |
} else { |
latch = true; |
for (j = sourcePoint + 8; j < length; j++) { |
if (this.inputData[j] <= '0' || this.inputData[j] >= '9') { |
latch = false; |
} |
} |
} |
if (j == 8 && latch) { |
next_mode = Mode.C1_ASCII; |
} |
} |
if (!(isEdiEncodable(this.inputData[sourcePoint]) && isEdiEncodable(this.inputData[sourcePoint + 1]) && isEdiEncodable(this.inputData[sourcePoint + 2]))) { |
next_mode = Mode.C1_ASCII; |
} |
} |
if (next_mode != Mode.C1_EDI) { |
this.data[targetPoint] = 255; |
targetPoint++; /* Unlatch */ |
} else { |
if (this.inputData[sourcePoint] == 13) { |
value = 0; |
} |
if (this.inputData[sourcePoint] == '*') { |
value = 1; |
} |
if (this.inputData[sourcePoint] == '>') { |
value = 2; |
} |
if (this.inputData[sourcePoint] == ' ') { |
value = 3; |
} |
if (this.inputData[sourcePoint] >= '0' && this.inputData[sourcePoint] <= '9') { |
value = this.inputData[sourcePoint] - '0' + 4; |
} |
if (this.inputData[sourcePoint] >= 'A' && this.inputData[sourcePoint] <= 'Z') { |
value = this.inputData[sourcePoint] - 'A' + 14; |
} |
edi_buffer[edi_p] = value; |
edi_p++; |
if (edi_p >= 3) { |
int iv; |
iv = 1600 * edi_buffer[0] + 40 * edi_buffer[1] + edi_buffer[2] + 1; |
this.data[targetPoint] = iv / 256; |
targetPoint++; |
this.data[targetPoint] = iv % 256; |
targetPoint++; |
edi_buffer[0] = edi_buffer[3]; |
edi_buffer[1] = edi_buffer[4]; |
edi_buffer[2] = edi_buffer[5]; |
edi_buffer[3] = 0; |
edi_buffer[4] = 0; |
edi_buffer[5] = 0; |
edi_p -= 3; |
} |
sourcePoint++; |
} |
} |
if (current_mode == Mode.C1_DECIMAL) { /* Step F - Decimal encodation */ |
next_mode = Mode.C1_DECIMAL; |
data_left = length - sourcePoint; |
decimal_count = 0; |
if (data_left >= 1) { |
if (this.inputData[sourcePoint] >= '0' && this.inputData[sourcePoint] <= '9') { |
decimal_count = 1; |
} |
} |
if (data_left >= 2) { |
if (decimal_count == 1 && this.inputData[sourcePoint + 1] >= '0' && this.inputData[sourcePoint + 1] <= '9') { |
decimal_count = 2; |
} |
} |
if (data_left >= 3) { |
if (decimal_count == 2 && this.inputData[sourcePoint + 2] >= '0' && this.inputData[sourcePoint + 2] <= '9') { |
decimal_count = 3; |
} |
} |
if (decimal_count != 3) { |
/* Finish Decimal mode and go back to ASCII */ |
decimal_binary += "111111"; /* Unlatch */ |
target_count = 3; |
if (decimal_binary.length() <= 16) { |
target_count = 2; |
} |
if (decimal_binary.length() <= 8) { |
target_count = 1; |
} |
bits_left_in_byte = 8 * target_count - decimal_binary.length(); |
if (bits_left_in_byte == 8) { |
bits_left_in_byte = 0; |
} |
if (bits_left_in_byte == 2) { |
decimal_binary += "01"; |
} |
if (bits_left_in_byte == 4 || bits_left_in_byte == 6) { |
if (decimal_count >= 1) { |
sub_value = this.inputData[sourcePoint] - '0' + 1; |
for (i = 0x08; i > 0; i = i >> 1) { |
if ((sub_value & i) != 0) { |
decimal_binary += "1"; |
} else { |
decimal_binary += "0"; |
} |
} |
sourcePoint++; |
} else { |
decimal_binary += "1111"; |
} |
} |
if (bits_left_in_byte == 6) { |
decimal_binary += "01"; |
} |
/* Binary buffer is full - transfer to data */ |
if (target_count >= 1) { |
for (i = 0; i < 8; i++) { |
if (decimal_binary.charAt(i) == '1') { |
this.data[targetPoint] += 128 >> i; |
} |
} |
targetPoint++; |
} |
if (target_count >= 2) { |
for (i = 0; i < 8; i++) { |
if (decimal_binary.charAt(8 + i) == '1') { |
this.data[targetPoint] += 128 >> i; |
} |
} |
targetPoint++; |
} |
if (target_count == 3) { |
for (i = 0; i < 8; i++) { |
if (decimal_binary.charAt(16 + i) == '1') { |
this.data[targetPoint] += 128 >> i; |
} |
} |
targetPoint++; |
} |
next_mode = Mode.C1_ASCII; |
} else { |
/* There are three digits - convert the value to binary */ |
value = 100 * (this.inputData[sourcePoint] - '0') + 10 * (this.inputData[sourcePoint + 1] - '0') + this.inputData[sourcePoint + 2] - '0' + 1; |
for (i = 0x200; i > 0; i = i >> 1) { |
if ((value & i) != 0) { |
decimal_binary += "1"; |
} else { |
decimal_binary += "0"; |
} |
} |
sourcePoint += 3; |
} |
if (decimal_binary.length() >= 24) { |
/* Binary buffer is full - transfer to data */ |
for (i = 0; i < 8; i++) { |
if (decimal_binary.charAt(i) == '1') { |
this.data[targetPoint] += 128 >> i; |
} |
if (decimal_binary.charAt(8 + i) == '1') { |
this.data[targetPoint + 1] += 128 >> i; |
} |
if (decimal_binary.charAt(16 + i) == '1') { |
this.data[targetPoint + 2] += 128 >> i; |
} |
} |
targetPoint += 3; |
if (decimal_binary.length() > 24) { |
decimal_binary = decimal_binary.substring(24); |
} |
} |
} |
if (current_mode == Mode.C1_BYTE) { |
next_mode = Mode.C1_BYTE; |
if (this.inputData[sourcePoint] == FNC1) { |
next_mode = Mode.C1_ASCII; |
} else { |
if (this.inputData[sourcePoint] <= 127) { |
next_mode = lookAheadTest(length, sourcePoint, current_mode); |
} |
} |
if (next_mode != Mode.C1_BYTE) { |
/* Insert byte field length */ |
if (targetPoint - byte_start <= 249) { |
for (i = targetPoint; i >= byte_start; i--) { |
this.data[i + 1] = this.data[i]; |
} |
this.data[byte_start] = targetPoint - byte_start; |
targetPoint++; |
} else { |
for (i = targetPoint; i >= byte_start; i--) { |
this.data[i + 2] = this.data[i]; |
} |
this.data[byte_start] = 249 + (targetPoint - byte_start) / 250; |
this.data[byte_start + 1] = (targetPoint - byte_start) % 250; |
targetPoint += 2; |
} |
} else { |
this.data[targetPoint] = this.inputData[sourcePoint]; |
targetPoint++; |
sourcePoint++; |
} |
} |
if (targetPoint > 1480) { |
/* Data is too large for symbol */ |
throw new OkapiException("Input data too long"); |
} |
} while (sourcePoint < length); |
/* Empty buffers */ |
if (c40_p == 2) { |
int iv; |
c40_buffer[2] = 1; |
iv = 1600 * c40_buffer[0] + 40 * c40_buffer[1] + c40_buffer[2] + 1; |
this.data[targetPoint] = iv / 256; |
targetPoint++; |
this.data[targetPoint] = iv % 256; |
targetPoint++; |
this.data[targetPoint] = 255; |
targetPoint++; /* Unlatch */ |
} |
if (c40_p == 1) { |
int iv; |
c40_buffer[1] = 1; |
c40_buffer[2] = 31; /* Pad */ |
iv = 1600 * c40_buffer[0] + 40 * c40_buffer[1] + c40_buffer[2] + 1; |
this.data[targetPoint] = iv / 256; |
targetPoint++; |
this.data[targetPoint] = iv % 256; |
targetPoint++; |
this.data[targetPoint] = 255; |
targetPoint++; /* Unlatch */ |
} |
if (text_p == 2) { |
int iv; |
text_buffer[2] = 1; |
iv = 1600 * text_buffer[0] + 40 * text_buffer[1] + text_buffer[2] + 1; |
this.data[targetPoint] = iv / 256; |
targetPoint++; |
this.data[targetPoint] = iv % 256; |
targetPoint++; |
this.data[targetPoint] = 255; |
targetPoint++; /* Unlatch */ |
} |
if (text_p == 1) { |
int iv; |
text_buffer[1] = 1; |
text_buffer[2] = 31; /* Pad */ |
iv = 1600 * text_buffer[0] + 40 * text_buffer[1] + text_buffer[2] + 1; |
this.data[targetPoint] = iv / 256; |
targetPoint++; |
this.data[targetPoint] = iv % 256; |
targetPoint++; |
this.data[targetPoint] = 255; |
targetPoint++; /* Unlatch */ |
} |
if (current_mode == Mode.C1_DECIMAL) { |
/* Finish Decimal mode and go back to ASCII */ |
decimal_binary += "111111"; /* Unlatch */ |
target_count = 3; |
if (decimal_binary.length() <= 16) { |
target_count = 2; |
} |
if (decimal_binary.length() <= 8) { |
target_count = 1; |
} |
bits_left_in_byte = 8 * target_count - decimal_binary.length(); |
if (bits_left_in_byte == 8) { |
bits_left_in_byte = 0; |
} |
if (bits_left_in_byte == 2) { |
decimal_binary += "01"; |
} |
if (bits_left_in_byte == 4 || bits_left_in_byte == 6) { |
decimal_binary += "1111"; |
} |
if (bits_left_in_byte == 6) { |
decimal_binary += "01"; |
} |
/* Binary buffer is full - transfer to data */ |
if (target_count >= 1) { |
for (i = 0; i < 8; i++) { |
if (decimal_binary.charAt(i) == '1') { |
this.data[targetPoint] += 128 >> i; |
} |
} |
targetPoint++; |
} |
if (target_count >= 2) { |
for (i = 0; i < 8; i++) { |
if (decimal_binary.charAt(8 + i) == '1') { |
this.data[targetPoint] += 128 >> i; |
} |
} |
targetPoint++; |
} |
if (target_count == 3) { |
for (i = 0; i < 8; i++) { |
if (decimal_binary.charAt(16 + i) == '1') { |
this.data[targetPoint] += 128 >> i; |
} |
} |
targetPoint++; |
} |
} |
if (current_mode == Mode.C1_BYTE) { |
/* Insert byte field length */ |
if (targetPoint - byte_start <= 249) { |
for (i = targetPoint; i >= byte_start; i--) { |
this.data[i + 1] = this.data[i]; |
} |
this.data[byte_start] = targetPoint - byte_start; |
targetPoint++; |
} else { |
for (i = targetPoint; i >= byte_start; i--) { |
this.data[i + 2] = this.data[i]; |
} |
this.data[byte_start] = 249 + (targetPoint - byte_start) / 250; |
this.data[byte_start + 1] = (targetPoint - byte_start) % 250; |
targetPoint += 2; |
} |
} |
/* Re-check length of data */ |
if (targetPoint > 1480) { |
/* Data is too large for symbol */ |
throw new OkapiException("Input data too long"); |
} |
return targetPoint; |
} |
private Mode lookAheadTest(final int sourcelen, final int position, final Mode current_mode) { |
double ascii_count, c40_count, text_count, edi_count, byte_count; |
int reduced_char; |
int done, best_count, sp; |
Mode best_scheme; |
/* Step J */ |
if (current_mode == Mode.C1_ASCII) { |
ascii_count = 0.0; |
c40_count = 1.0; |
text_count = 1.0; |
edi_count = 1.0; |
byte_count = 2.0; |
} else { |
ascii_count = 1.0; |
c40_count = 2.0; |
text_count = 2.0; |
edi_count = 2.0; |
byte_count = 3.0; |
} |
switch (current_mode) { |
case C1_C40: |
c40_count = 0.0; |
break; |
case C1_TEXT: |
text_count = 0.0; |
break; |
case C1_BYTE: |
byte_count = 0.0; |
break; |
case C1_EDI: |
edi_count = 0.0; |
break; |
} |
for (sp = position; sp < sourcelen && sp <= position + 8; sp++) { |
if (this.inputData[sp] <= 127) { |
reduced_char = this.inputData[sp]; |
} else { |
reduced_char = this.inputData[sp] - 127; |
} |
/* Step L */ |
if (this.inputData[sp] >= '0' && this.inputData[sp] <= '9') { |
ascii_count += 0.5; |
} else { |
ascii_count = roundUpToNextInteger(ascii_count); |
if (this.inputData[sp] > 127) { |
ascii_count += 2.0; |
} else { |
ascii_count += 1.0; |
} |
} |
/* Step M */ |
done = 0; |
if (reduced_char == ' ') { |
c40_count += 2.0 / 3.0; |
done = 1; |
} |
if (reduced_char >= '0' && reduced_char <= '9') { |
c40_count += 2.0 / 3.0; |
done = 1; |
} |
if (reduced_char >= 'A' && reduced_char <= 'Z') { |
c40_count += 2.0 / 3.0; |
done = 1; |
} |
if (this.inputData[sp] > 127) { |
c40_count += 4.0 / 3.0; |
} |
if (done == 0) { |
c40_count += 4.0 / 3.0; |
} |
/* Step N */ |
done = 0; |
if (reduced_char == ' ') { |
text_count += 2.0 / 3.0; |
done = 1; |
} |
if (reduced_char >= '0' && reduced_char <= '9') { |
text_count += 2.0 / 3.0; |
done = 1; |
} |
if (reduced_char >= 'a' && reduced_char <= 'z') { |
text_count += 2.0 / 3.0; |
done = 1; |
} |
if (this.inputData[sp] > 127) { |
text_count += 4.0 / 3.0; |
} |
if (done == 0) { |
text_count += 4.0 / 3.0; |
} |
/* Step O */ |
done = 0; |
if (this.inputData[sp] == 13) { |
edi_count += 2.0 / 3.0; |
done = 1; |
} |
if (this.inputData[sp] == '*') { |
edi_count += 2.0 / 3.0; |
done = 1; |
} |
if (this.inputData[sp] == '>') { |
edi_count += 2.0 / 3.0; |
done = 1; |
} |
if (this.inputData[sp] == ' ') { |
edi_count += 2.0 / 3.0; |
done = 1; |
} |
if (this.inputData[sp] >= '0' && this.inputData[sp] <= '9') { |
edi_count += 2.0 / 3.0; |
done = 1; |
} |
if (this.inputData[sp] >= 'A' && this.inputData[sp] <= 'Z') { |
edi_count += 2.0 / 3.0; |
done = 1; |
} |
if (this.inputData[sp] > 127) { |
edi_count += 13.0 / 3.0; |
} else { |
if (done == 0) { |
edi_count += 10.0 / 3.0; |
} |
} |
/* Step P */ |
if (this.inputData[sp] == FNC1) { |
byte_count += 3.0; |
} else { |
byte_count += 1.0; |
} |
} |
ascii_count = roundUpToNextInteger(ascii_count); |
c40_count = roundUpToNextInteger(c40_count); |
text_count = roundUpToNextInteger(text_count); |
edi_count = roundUpToNextInteger(edi_count); |
byte_count = roundUpToNextInteger(byte_count); |
best_scheme = Mode.C1_ASCII; |
if (sp == sourcelen) { |
/* Step K */ |
best_count = (int) edi_count; |
if (text_count <= best_count) { |
best_count = (int) text_count; |
best_scheme = Mode.C1_TEXT; |
} |
if (c40_count <= best_count) { |
best_count = (int) c40_count; |
best_scheme = Mode.C1_C40; |
} |
if (ascii_count <= best_count) { |
best_count = (int) ascii_count; |
best_scheme = Mode.C1_ASCII; |
} |
if (byte_count <= best_count) { |
best_scheme = Mode.C1_BYTE; |
} |
} else { |
/* Step Q */ |
if (edi_count + 1.0 <= ascii_count && edi_count + 1.0 <= c40_count && edi_count + 1.0 <= byte_count && edi_count + 1.0 <= text_count) { |
best_scheme = Mode.C1_EDI; |
} |
if (c40_count + 1.0 <= ascii_count && c40_count + 1.0 <= text_count) { |
if (c40_count < edi_count) { |
best_scheme = Mode.C1_C40; |
} else { |
if (c40_count == edi_count) { |
if (preferEdi(sourcelen, position)) { |
best_scheme = Mode.C1_EDI; |
} else { |
best_scheme = Mode.C1_C40; |
} |
} |
} |
} |
if (text_count + 1.0 <= ascii_count && text_count + 1.0 <= c40_count && text_count + 1.0 <= byte_count && text_count + 1.0 <= edi_count) { |
best_scheme = Mode.C1_TEXT; |
} |
if (ascii_count + 1.0 <= byte_count && ascii_count + 1.0 <= c40_count && ascii_count + 1.0 <= text_count && ascii_count + 1.0 <= edi_count) { |
best_scheme = Mode.C1_ASCII; |
} |
if (byte_count + 1.0 <= ascii_count && byte_count + 1.0 <= c40_count && byte_count + 1.0 <= text_count && byte_count + 1.0 <= edi_count) { |
best_scheme = Mode.C1_BYTE; |
} |
} |
return best_scheme; |
} |
private double roundUpToNextInteger(final double input) { |
double fraction, output; |
fraction = input - (int) input; |
if (fraction > 0.01) { |
output = input - fraction + 1.0; |
} else { |
output = input; |
} |
return output; |
} |
private boolean preferEdi(final int sourcelen, final int position) { |
int i; |
for (i = position; isEdiEncodable(this.inputData[position + i]) && position + i < sourcelen; i++) { |
; |
} |
if (position + i == sourcelen) { |
/* Reached end of input */ |
return false; |
} |
if (this.inputData[position + i - 1] == 13) { |
return true; |
} |
if (this.inputData[position + i - 1] == '*') { |
return true; |
} |
if (this.inputData[position + i - 1] == '>') { |
return true; |
} |
return false; |
} |
private boolean isEdiEncodable(final int input) { |
boolean result = false; |
if (input == 13) { |
result = true; |
} |
if (input == '*') { |
result = true; |
} |
if (input == '>') { |
result = true; |
} |
if (input == ' ') { |
result = true; |
} |
if (input >= '0' && input <= '9') { |
result = true; |
} |
if (input >= 'A' && input <= 'Z') { |
result = true; |
} |
return result; |
} |
private void plotCentralFinder(final int start_row, final int row_count, final int full_rows) { |
for (int i = 0; i < row_count; i++) { |
if (i < full_rows) { |
plotHorizontalBar(start_row + i * 2, 1); |
} else { |
plotHorizontalBar(start_row + i * 2, 0); |
if (i != row_count - 1) { |
setGridModule(start_row + i * 2 + 1, 1); |
setGridModule(start_row + i * 2 + 1, this.symbol_width - 2); |
} |
} |
} |
} |
private void plotHorizontalBar(final int row_no, final int full) { |
if (full != 0) { |
for (int i = 0; i < this.symbol_width; i++) { |
setGridModule(row_no, i); |
} |
} else { |
for (int i = 1; i < this.symbol_width - 1; i++) { |
setGridModule(row_no, i); |
} |
} |
} |
private void plotVerticalBar(final int column, final int height, final int top) { |
if (top != 0) { |
for (int i = 0; i < height; i++) { |
setGridModule(i, column); |
} |
} else { |
for (int i = 0; i < height; i++) { |
setGridModule(this.row_count - i - 1, column); |
} |
} |
} |
private void plotSpigot(final int row_no) { |
for (int i = this.symbol_width - 1; i > 0; i--) { |
if (this.outputGrid[row_no][i - 1]) { |
setGridModule(row_no, i); |
} |
} |
} |
private void plotDataBlock(final int start_row, final int start_col, final int height, final int width, final int row_offset, final int col_offset) { |
for (int i = start_row; i < start_row + height; i++) { |
for (int j = start_col; j < start_col + width; j++) { |
if (this.datagrid[i][j] == '1') { |
setGridModule(i + row_offset, j + col_offset); |
} |
} |
} |
} |
private void setGridModule(final int row, final int column) { |
this.outputGrid[row][column] = true; |
} |
private void resetGridModule(final int row, final int column) { |
this.outputGrid[row][column] = false; |
} |
private static int getSize(final Version version) { |
switch (version) { |
case A: |
return 1; |
case B: |
return 2; |
case C: |
return 3; |
case D: |
return 4; |
case E: |
return 5; |
case F: |
return 6; |
case G: |
return 7; |
case H: |
return 8; |
default: |
return 0; |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Code11.java |
---|
New file |
0,0 → 1,215 |
/* |
* Copyright 2014 Robin Stuart, Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.util.Arrays.positionOf; |
/** |
* <p> |
* Implements Code 11 bar code symbology. |
* |
* <p> |
* Code 11 can encode any length string consisting of the digits 0-9 and the dash character (-). One |
* or two modulo-11 check digits are calculated. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
* @author Daniel Gredler |
*/ |
public class Code11 extends Symbol { |
private static final String[] CODE_11_TABLE = { "111121", "211121", "121121", "221111", "112121", "212111", "122111", "111221", "211211", "211111", "112111" }; |
private static final char[] CHARACTER_SET = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-' }; |
/** Ratio of wide bar width to narrow bar width. */ |
private double moduleWidthRatio = 2; |
/** The number of check digits to calculate ({@code 1} or {@code 2}). */ |
private int checkDigitCount = 2; |
/** Optional start delimiter to be shown in the human-readable text. */ |
private Character startDelimiter; |
/** Optional stop delimiter to be shown in the human-readable text. */ |
private Character stopDelimiter; |
/** |
* Sets the ratio of wide bar width to narrow bar width. Valid values are usually between |
* {@code 2} and {@code 3}. The default value is {@code 2}. |
* |
* @param moduleWidthRatio the ratio of wide bar width to narrow bar width |
*/ |
public void setModuleWidthRatio(final double moduleWidthRatio) { |
this.moduleWidthRatio = moduleWidthRatio; |
} |
/** |
* Returns the ratio of wide bar width to narrow bar width. |
* |
* @return the ratio of wide bar width to narrow bar width |
*/ |
public double getModuleWidthRatio() { |
return this.moduleWidthRatio; |
} |
/** |
* Sets the number of check digits to calculate ({@code 1} or {@code 2}). The default value is |
* {@code 2}. |
* |
* @param checkDigitCount the number of check digits to calculate |
*/ |
public void setCheckDigitCount(final int checkDigitCount) { |
if (checkDigitCount < 1 || checkDigitCount > 2) { |
throw new IllegalArgumentException("Check digit count must be 1 or 2."); |
} |
this.checkDigitCount = checkDigitCount; |
} |
/** |
* Returns the number of check digits to calculate (1 or 2). |
* |
* @return the number of check digits to calculate |
*/ |
public int getCheckDigitCount() { |
return this.checkDigitCount; |
} |
/** |
* Sets an optional start delimiter to be shown in the human-readable text (defaults to |
* <code>null</code>). |
* |
* @param startDelimiter an optional start delimiter to be shown in the human-readable text |
*/ |
public void setStartDelimiter(final Character startDelimiter) { |
this.startDelimiter = startDelimiter; |
} |
/** |
* Returns the optional start delimiter to be shown in the human-readable text. |
* |
* @return the optional start delimiter to be shown in the human-readable text |
*/ |
public Character getStartDelimiter() { |
return this.startDelimiter; |
} |
/** |
* Sets an optional stop delimiter to be shown in the human-readable text (defaults to |
* <code>null</code>). |
* |
* @param stopDelimiter an optional stop delimiter to be shown in the human-readable text |
*/ |
public void setStopDelimiter(final Character stopDelimiter) { |
this.stopDelimiter = stopDelimiter; |
} |
/** |
* Returns the optional stop delimiter to be shown in the human-readable text. |
* |
* @return the optional stop delimiter to be shown in the human-readable text |
*/ |
public Character getStopDelimiter() { |
return this.stopDelimiter; |
} |
/** {@inheritDoc} */ |
@Override |
protected void encode() { |
if (!this.content.matches("[0-9-]+")) { |
throw new OkapiException("Invalid characters in input"); |
} |
String horizontalSpacing = "112211"; |
String humanReadable = this.content; |
final int length = this.content.length(); |
final int[] weight = new int[length + 1]; |
for (int i = 0; i < length; i++) { |
final char c = this.content.charAt(i); |
weight[i] = positionOf(c, CHARACTER_SET); |
horizontalSpacing += CODE_11_TABLE[weight[i]]; |
} |
final int checkDigitC = getCheckDigitC(weight, length); |
horizontalSpacing += CODE_11_TABLE[checkDigitC]; |
humanReadable += CHARACTER_SET[checkDigitC]; |
infoLine("Check Digit C: " + checkDigitC); |
if (this.checkDigitCount == 2) { |
weight[length] = checkDigitC; |
final int checkDigitK = getCheckDigitK(weight, length + 1); |
horizontalSpacing += CODE_11_TABLE[checkDigitK]; |
humanReadable += CHARACTER_SET[checkDigitK]; |
infoLine("Check Digit K: " + checkDigitK); |
} |
horizontalSpacing += "112211"; |
this.readable = humanReadable; |
if (this.startDelimiter != null) { |
this.readable = this.startDelimiter + this.readable; |
} |
if (this.stopDelimiter != null) { |
this.readable = this.readable + this.stopDelimiter; |
} |
this.pattern = new String[] { horizontalSpacing }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
private static int getCheckDigitC(final int[] weight, final int length) { |
int countC = 0; |
int weightC = 1; |
for (int i = length - 1; i >= 0; i--) { |
countC += weightC * weight[i]; |
weightC++; |
if (weightC > 10) { |
weightC = 1; |
} |
} |
return countC % 11; |
} |
private static int getCheckDigitK(final int[] weight, final int length) { |
int countK = 0; |
int weightK = 1; |
for (int i = length - 1; i >= 0; i--) { |
countK += weightK * weight[i]; |
weightK++; |
if (weightK > 9) { |
weightK = 1; |
} |
} |
return countK % 11; |
} |
/** {@inheritDoc} */ |
@Override |
protected double getModuleWidth(final int originalWidth) { |
if (originalWidth == 1) { |
return 1; |
} else { |
return this.moduleWidthRatio; |
} |
} |
/** {@inheritDoc} */ |
@Override |
protected int[] getCodewords() { |
return getPatternAsCodewords(6); |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/QrCode.java |
---|
New file |
0,0 → 1,1692 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.util.Arrays.positionOf; |
import java.nio.CharBuffer; |
import java.nio.charset.Charset; |
/** |
* <p> |
* Implements QR Code bar code symbology According to ISO/IEC 18004:2015. |
* |
* <p> |
* The maximum capacity of a (version 40) QR Code symbol is 7089 numeric digits, 4296 alphanumeric |
* characters or 2953 bytes of data. QR Code symbols can also be used to encode GS1 data. QR Code |
* symbols can encode characters in the Latin-1 set and Kanji characters which are members of the |
* Shift-JIS encoding scheme. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class QrCode extends Symbol { |
public enum EccLevel { |
L, M, Q, H |
} |
private enum QrMode { |
NULL, KANJI, BINARY, ALPHANUM, NUMERIC |
} |
/* Table 5 - Encoding/Decoding table for Alphanumeric mode */ |
private static final char[] RHODIUM = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', |
'V', 'W', 'X', 'Y', 'Z', ' ', '$', '%', '*', '+', '-', '.', '/', ':' }; |
private static final int[] QR_DATA_CODEWORDS_L = { 19, 34, 55, 80, 108, 136, 156, 194, 232, 274, 324, 370, 428, 461, 523, 589, 647, 721, 795, 861, 932, 1006, 1094, 1174, 1276, 1370, 1468, 1531, |
1631, 1735, 1843, 1955, 2071, 2191, 2306, 2434, 2566, 2702, 2812, 2956 }; |
private static final int[] QR_DATA_CODEWORDS_M = { 16, 28, 44, 64, 86, 108, 124, 154, 182, 216, 254, 290, 334, 365, 415, 453, 507, 563, 627, 669, 714, 782, 860, 914, 1000, 1062, 1128, 1193, 1267, |
1373, 1455, 1541, 1631, 1725, 1812, 1914, 1992, 2102, 2216, 2334 }; |
private static final int[] QR_DATA_CODEWORDS_Q = { 13, 22, 34, 48, 62, 76, 88, 110, 132, 154, 180, 206, 244, 261, 295, 325, 367, 397, 445, 485, 512, 568, 614, 664, 718, 754, 808, 871, 911, 985, |
1033, 1115, 1171, 1231, 1286, 1354, 1426, 1502, 1582, 1666 }; |
private static final int[] QR_DATA_CODEWORDS_H = { 9, 16, 26, 36, 46, 60, 66, 86, 100, 122, 140, 158, 180, 197, 223, 253, 283, 313, 341, 385, 406, 442, 464, 514, 538, 596, 628, 661, 701, 745, 793, |
845, 901, 961, 986, 1054, 1096, 1142, 1222, 1276 }; |
private static final int[] QR_BLOCKS_L = { 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25 }; |
private static final int[] QR_BLOCKS_M = { 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49 }; |
private static final int[] QR_BLOCKS_Q = { 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68 }; |
private static final int[] QR_BLOCKS_H = { 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81 }; |
private static final int[] QR_TOTAL_CODEWORDS = { 26, 44, 70, 100, 134, 172, 196, 242, 292, 346, 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085, 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, |
2051, 2185, 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706 }; |
private static final int[] QR_SIZES = { 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77, 81, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, 129, 133, 137, 141, 145, 149, 153, 157, |
161, 165, 169, 173, 177 }; |
private static final int[] QR_ALIGN_LOOPSIZE = { 0, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7 }; |
private static final int[] QR_TABLE_E1 = { 6, 18, 0, 0, 0, 0, 0, 6, 22, 0, 0, 0, 0, 0, 6, 26, 0, 0, 0, 0, 0, 6, 30, 0, 0, 0, 0, 0, 6, 34, 0, 0, 0, 0, 0, 6, 22, 38, 0, 0, 0, 0, 6, 24, 42, 0, 0, 0, |
0, 6, 26, 46, 0, 0, 0, 0, 6, 28, 50, 0, 0, 0, 0, 6, 30, 54, 0, 0, 0, 0, 6, 32, 58, 0, 0, 0, 0, 6, 34, 62, 0, 0, 0, 0, 6, 26, 46, 66, 0, 0, 0, 6, 26, 48, 70, 0, 0, 0, 6, 26, 50, 74, 0, 0, |
0, 6, 30, 54, 78, 0, 0, 0, 6, 30, 56, 82, 0, 0, 0, 6, 30, 58, 86, 0, 0, 0, 6, 34, 62, 90, 0, 0, 0, 6, 28, 50, 72, 94, 0, 0, 6, 26, 50, 74, 98, 0, 0, 6, 30, 54, 78, 102, 0, 0, 6, 28, 54, |
80, 106, 0, 0, 6, 32, 58, 84, 110, 0, 0, 6, 30, 58, 86, 114, 0, 0, 6, 34, 62, 90, 118, 0, 0, 6, 26, 50, 74, 98, 122, 0, 6, 30, 54, 78, 102, 126, 0, 6, 26, 52, 78, 104, 130, 0, 6, 30, 56, |
82, 108, 134, 0, 6, 34, 60, 86, 112, 138, 0, 6, 30, 58, 86, 114, 142, 0, 6, 34, 62, 90, 118, 146, 0, 6, 30, 54, 78, 102, 126, 150, 6, 24, 50, 76, 102, 128, 154, 6, 28, 54, 80, 106, 132, |
158, 6, 32, 58, 84, 110, 136, 162, 6, 26, 54, 82, 110, 138, 166, 6, 30, 58, 86, 114, 142, 170 }; |
private static final int[] QR_ANNEX_C = { |
/* Format information bit sequences */ |
0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0, 0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976, 0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, |
0x083b, 0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed }; |
private static final int[] QR_ANNEX_D = { |
/* Version information bit sequences */ |
0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d, 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9, 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, |
0x1cc1a, 0x1d33f, 0x1ed75, 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64, 0x27541, 0x28c69 }; |
private int preferredVersion; |
private EccLevel preferredEccLevel = EccLevel.L; |
/** |
* Sets the preferred symbol size / version. This value may be ignored if the data string is too |
* large to fit into the specified symbol. Input values correspond to symbol sizes as shown in |
* the following table: |
* |
* <table summary="Available QR Code sizes"> |
* <tbody> |
* <tr> |
* <th>Input</th> |
* <th>Symbol Size</th> |
* <th>Input</th> |
* <th>Symbol Size</th> |
* </tr> |
* <tr> |
* <td>1</td> |
* <td>21 x 21</td> |
* <td>21</td> |
* <td>101 x 101</td> |
* </tr> |
* <tr> |
* <td>2</td> |
* <td>25 x 25</td> |
* <td>22</td> |
* <td>105 x 105</td> |
* </tr> |
* <tr> |
* <td>3</td> |
* <td>29 x 29</td> |
* <td>23</td> |
* <td>109 x 109</td> |
* </tr> |
* <tr> |
* <td>4</td> |
* <td>33 x 33</td> |
* <td>24</td> |
* <td>113 x 113</td> |
* </tr> |
* <tr> |
* <td>5</td> |
* <td>37 x 37</td> |
* <td>25</td> |
* <td>117 x 117</td> |
* </tr> |
* <tr> |
* <td>6</td> |
* <td>41 x 41</td> |
* <td>26</td> |
* <td>121 x 121</td> |
* </tr> |
* <tr> |
* <td>7</td> |
* <td>45 x 45</td> |
* <td>27</td> |
* <td>125 x 125</td> |
* </tr> |
* <tr> |
* <td>8</td> |
* <td>49 x 49</td> |
* <td>28</td> |
* <td>129 x 129</td> |
* </tr> |
* <tr> |
* <td>9</td> |
* <td>53 x 53</td> |
* <td>29</td> |
* <td>133 x 133</td> |
* </tr> |
* <tr> |
* <td>10</td> |
* <td>57 x 57</td> |
* <td>30</td> |
* <td>137 x 137</td> |
* </tr> |
* <tr> |
* <td>11</td> |
* <td>61 x 61</td> |
* <td>31</td> |
* <td>141 x 141</td> |
* </tr> |
* <tr> |
* <td>12</td> |
* <td>65 x 65</td> |
* <td>32</td> |
* <td>145 x 145</td> |
* </tr> |
* <tr> |
* <td>13</td> |
* <td>69 x 69</td> |
* <td>33</td> |
* <td>149 x 149</td> |
* </tr> |
* <tr> |
* <td>14</td> |
* <td>73 x 73</td> |
* <td>34</td> |
* <td>153 x 153</td> |
* </tr> |
* <tr> |
* <td>15</td> |
* <td>77 x 77</td> |
* <td>35</td> |
* <td>157 x 157</td> |
* </tr> |
* <tr> |
* <td>16</td> |
* <td>81 x 81</td> |
* <td>36</td> |
* <td>161 x 161</td> |
* </tr> |
* <tr> |
* <td>17</td> |
* <td>85 x 85</td> |
* <td>37</td> |
* <td>165 x 165</td> |
* </tr> |
* <tr> |
* <td>18</td> |
* <td>89 x 89</td> |
* <td>38</td> |
* <td>169 x 169</td> |
* </tr> |
* <tr> |
* <td>19</td> |
* <td>93 x 93</td> |
* <td>39</td> |
* <td>173 x 173</td> |
* </tr> |
* <tr> |
* <td>20</td> |
* <td>97 x 97</td> |
* <td>40</td> |
* <td>177 x 177</td> |
* </tr> |
* </tbody> |
* </table> |
* |
* @param version the preferred symbol version |
*/ |
public void setPreferredVersion(final int version) { |
this.preferredVersion = version; |
} |
/** |
* Returns the preferred symbol version. |
* |
* @return the preferred symbol version |
*/ |
public int getPreferredVersion() { |
return this.preferredVersion; |
} |
/** |
* Sets the preferred amount of symbol space allocated to error correction. This value may be |
* ignored if there is room for a higher error correction level. Levels are predefined according |
* to the following table: |
* |
* <table summary="QR Code error correction levels"> |
* <tbody> |
* <tr> |
* <th>ECC Level</th> |
* <th>Error Correction Capacity</th> |
* <th>Recovery Capacity</th> |
* </tr> |
* <tr> |
* <td>L (default)</td> |
* <td>Approx 20% of symbol</td> |
* <td>Approx 7%</td> |
* </tr> |
* <tr> |
* <td>M</td> |
* <td>Approx 37% of symbol</td> |
* <td>Approx 15%</td> |
* </tr> |
* <tr> |
* <td>Q</td> |
* <td>Approx 55% of symbol</td> |
* <td>Approx 25%</td> |
* </tr> |
* <tr> |
* <td>H</td> |
* <td>Approx 65% of symbol</td> |
* <td>Approx 30%</td> |
* </tr> |
* </tbody> |
* </table> |
* |
* @param preferredEccLevel the preferred error correction level |
*/ |
public void setPreferredEccLevel(final EccLevel preferredEccLevel) { |
this.preferredEccLevel = preferredEccLevel; |
} |
/** |
* Returns the preferred amount of symbol space allocated to error correction. |
* |
* @return the preferred amount of symbol space allocated to error correction |
*/ |
public EccLevel getPreferredEccLevel() { |
return this.preferredEccLevel; |
} |
@Override |
protected boolean gs1Supported() { |
return true; |
} |
@Override |
protected void encode() { |
int i, j; |
int est_binlen; |
EccLevel ecc_level; |
int max_cw; |
int targetCwCount, version, blocks; |
int size; |
int bitmask; |
final boolean gs1 = this.inputDataType == DataType.GS1; |
eciProcess(); // Get ECI mode |
if (this.eciMode == 20) { |
/* Shift-JIS encoding, use Kanji mode */ |
final Charset c = Charset.forName("Shift_JIS"); |
this.inputData = new int[this.content.length()]; |
for (i = 0; i < this.inputData.length; i++) { |
final CharBuffer buffer = CharBuffer.wrap(this.content, i, i + 1); |
final byte[] bytes = c.encode(buffer).array(); |
final int value = bytes.length == 2 ? (bytes[0] & 0xff) << 8 | bytes[1] & 0xff : bytes[0]; |
this.inputData[i] = value; |
} |
} else { |
/* inputData already initialized in eciProcess() */ |
} |
QrMode[] inputMode = new QrMode[this.inputData.length]; |
defineMode(inputMode, this.inputData); |
est_binlen = getBinaryLength(40, inputMode, this.inputData, gs1, this.eciMode); |
ecc_level = this.preferredEccLevel; |
switch (this.preferredEccLevel) { |
case L: |
default: |
max_cw = 2956; |
break; |
case M: |
max_cw = 2334; |
break; |
case Q: |
max_cw = 1666; |
break; |
case H: |
max_cw = 1276; |
break; |
} |
if (est_binlen > 8 * max_cw) { |
throw new OkapiException("Input too long for selected error correction level"); |
} |
// ZINT NOTE: this block is different from the corresponding block of code in Zint; |
// it is simplified, but the simplification required that the applyOptimisation method |
// be changed to be free of side effects (by putting the optimized mode array into a |
// new array instead of modifying the existing array) |
version = 40; |
for (i = 39; i >= 0; i--) { |
int[] dataCodewords; |
switch (ecc_level) { |
case L: |
default: |
dataCodewords = QR_DATA_CODEWORDS_L; |
break; |
case M: |
dataCodewords = QR_DATA_CODEWORDS_M; |
break; |
case Q: |
dataCodewords = QR_DATA_CODEWORDS_Q; |
break; |
case H: |
dataCodewords = QR_DATA_CODEWORDS_H; |
break; |
} |
final int proposedVersion = i + 1; |
final int proposedBinLen = getBinaryLength(proposedVersion, inputMode, this.inputData, gs1, this.eciMode); |
if (8 * dataCodewords[i] >= proposedBinLen) { |
version = proposedVersion; |
est_binlen = proposedBinLen; |
} |
} |
inputMode = applyOptimisation(version, inputMode); |
// ZINT NOTE: end of block of code that is different |
// TODO: delete this |
// |
// autosize = 40; |
// for (i = 39; i >= 0; i--) { |
// switch (ecc_level) { |
// case L: |
// if ((8 * QR_DATA_CODEWORDS_L[i]) >= est_binlen) { |
// autosize = i + 1; |
// } |
// break; |
// case M: |
// if ((8 * QR_DATA_CODEWORDS_M[i]) >= est_binlen) { |
// autosize = i + 1; |
// } |
// break; |
// case Q: |
// if ((8 * QR_DATA_CODEWORDS_Q[i]) >= est_binlen) { |
// autosize = i + 1; |
// } |
// break; |
// case H: |
// if ((8 * QR_DATA_CODEWORDS_H[i]) >= est_binlen) { |
// autosize = i + 1; |
// } |
// break; |
// } |
// } |
// |
// // Now see if the optimized binary will fit in a smaller symbol. |
// canShrink = true; |
// |
// do { |
// if (autosize == 1) { |
// est_binlen = getBinaryLength(autosize, inputMode, inputData, gs1, eciMode); // TODO: |
// added |
// canShrink = false; |
// } else { |
// est_binlen = getBinaryLength(autosize - 1, inputMode, inputData, gs1, eciMode); |
// |
// switch (ecc_level) { |
// case L: |
// if ((8 * QR_DATA_CODEWORDS_L[autosize - 2]) < est_binlen) { |
// canShrink = false; |
// } |
// break; |
// case M: |
// if ((8 * QR_DATA_CODEWORDS_M[autosize - 2]) < est_binlen) { |
// canShrink = false; |
// } |
// break; |
// case Q: |
// if ((8 * QR_DATA_CODEWORDS_Q[autosize - 2]) < est_binlen) { |
// canShrink = false; |
// } |
// break; |
// case H: |
// if ((8 * QR_DATA_CODEWORDS_H[autosize - 2]) < est_binlen) { |
// canShrink = false; |
// } |
// break; |
// } |
// |
// if (canShrink) { |
// // Optimization worked - data will fit in a smaller symbol |
// autosize--; |
// } else { |
// // Data did not fit in the smaller symbol, revert to original size |
// est_binlen = getBinaryLength(autosize, inputMode, inputData, gs1, eciMode); |
// } |
// } |
// } while (canShrink); |
// |
// version = autosize; |
if (this.preferredVersion >= 1 && this.preferredVersion <= 40) { |
/* |
* If the user has selected a larger symbol than the smallest available, then use the |
* size the user has selected, and re-optimize for this symbol size. |
*/ |
if (this.preferredVersion > version) { |
version = this.preferredVersion; |
est_binlen = getBinaryLength(this.preferredVersion, inputMode, this.inputData, gs1, this.eciMode); |
inputMode = applyOptimisation(version, inputMode); |
} |
if (this.preferredVersion < version) { |
throw new OkapiException("Input too long for selected symbol size"); |
} |
} |
/* Ensure maximum error correction capacity */ |
if (est_binlen <= QR_DATA_CODEWORDS_M[version - 1] * 8) { |
ecc_level = EccLevel.M; |
} |
if (est_binlen <= QR_DATA_CODEWORDS_Q[version - 1] * 8) { |
ecc_level = EccLevel.Q; |
} |
if (est_binlen <= QR_DATA_CODEWORDS_H[version - 1] * 8) { |
ecc_level = EccLevel.H; |
} |
targetCwCount = QR_DATA_CODEWORDS_L[version - 1]; |
blocks = QR_BLOCKS_L[version - 1]; |
switch (ecc_level) { |
case M: |
targetCwCount = QR_DATA_CODEWORDS_M[version - 1]; |
blocks = QR_BLOCKS_M[version - 1]; |
break; |
case Q: |
targetCwCount = QR_DATA_CODEWORDS_Q[version - 1]; |
blocks = QR_BLOCKS_Q[version - 1]; |
break; |
case H: |
targetCwCount = QR_DATA_CODEWORDS_H[version - 1]; |
blocks = QR_BLOCKS_H[version - 1]; |
break; |
} |
final int[] datastream = new int[targetCwCount + 1]; |
final int[] fullstream = new int[QR_TOTAL_CODEWORDS[version - 1] + 1]; |
qrBinary(datastream, version, targetCwCount, inputMode, this.inputData, gs1, this.eciMode, est_binlen); |
addEcc(fullstream, datastream, version, targetCwCount, blocks); |
size = QR_SIZES[version - 1]; |
final int[] grid = new int[size * size]; |
infoLine("Version: " + version); |
infoLine("ECC Level: " + ecc_level.name()); |
setupGrid(grid, size, version); |
populateGrid(grid, size, fullstream, QR_TOTAL_CODEWORDS[version - 1]); |
if (version >= 7) { |
addVersionInfo(grid, size, version); |
} |
bitmask = applyBitmask(grid, size, ecc_level); |
infoLine("Mask Pattern: " + Integer.toBinaryString(bitmask)); |
addFormatInfo(grid, size, ecc_level, bitmask); |
this.readable = ""; |
this.pattern = new String[size]; |
this.row_count = size; |
this.row_height = new int[size]; |
for (i = 0; i < size; i++) { |
final StringBuilder bin = new StringBuilder(size); |
for (j = 0; j < size; j++) { |
if ((grid[i * size + j] & 0x01) != 0) { |
bin.append('1'); |
} else { |
bin.append('0'); |
} |
} |
this.pattern[i] = bin2pat(bin); |
this.row_height[i] = 1; |
} |
} |
/** Place Kanji / Binary / Alphanumeric / Numeric values in inputMode. */ |
private static void defineMode(final QrMode[] inputMode, final int[] inputData) { |
for (int i = 0; i < inputData.length; i++) { |
if (inputData[i] > 0xff) { |
inputMode[i] = QrMode.KANJI; |
} else { |
inputMode[i] = QrMode.BINARY; |
if (isAlpha(inputData[i])) { |
inputMode[i] = QrMode.ALPHANUM; |
} |
if (inputData[i] == FNC1) { |
inputMode[i] = QrMode.ALPHANUM; |
} |
if (isNumeric(inputData[i])) { |
inputMode[i] = QrMode.NUMERIC; |
} |
} |
} |
// TODO: uncomment |
// /* If less than 6 numeric digits together then don't use numeric mode */ |
// for (int i = 0; i < inputMode.length; i++) { |
// if (inputMode[i] == QrMode.NUMERIC) { |
// if (((i != 0) && (inputMode[i - 1] != QrMode.NUMERIC)) || (i == 0)) { |
// mlen = 0; |
// while (((mlen + i) < inputMode.length) && (inputMode[mlen + i] == QrMode.NUMERIC)) { |
// mlen++; |
// }; |
// if (mlen < 6) { |
// for (int j = 0; j < mlen; j++) { |
// inputMode[i + j] = QrMode.ALPHANUM; |
// } |
// } |
// } |
// } |
// } |
// |
// /* If less than 4 alphanumeric characters together then don't use alphanumeric mode */ |
// for (int i = 0; i < inputMode.length; i++) { |
// if (inputMode[i] == QrMode.ALPHANUM) { |
// if (((i != 0) && (inputMode[i - 1] != QrMode.ALPHANUM)) || (i == 0)) { |
// mlen = 0; |
// while (((mlen + i) < inputMode.length) && (inputMode[mlen + i] == QrMode.ALPHANUM)) { |
// mlen++; |
// }; |
// if (mlen < 4) { |
// for (int j = 0; j < mlen; j++) { |
// inputMode[i + j] = QrMode.BINARY; |
// } |
// } |
// } |
// } |
// } |
} |
/** Calculate the actual bit length of the proposed binary string. */ |
private static int getBinaryLength(final int version, final QrMode[] inputModeUnoptimized, final int[] inputData, final boolean gs1, final int eciMode) { |
int i, j; |
QrMode currentMode; |
final int inputLength = inputModeUnoptimized.length; |
int count = 0; |
int alphaLength; |
int percent = 0; |
// ZINT NOTE: in Zint, this call modifies the input mode array directly; here, we leave |
// the original array alone so that subsequent binary length checks don't irrevocably |
// optimize the mode array for the wrong QR Code version |
final QrMode[] inputMode = applyOptimisation(version, inputModeUnoptimized); |
currentMode = QrMode.NULL; |
if (gs1) { |
count += 4; |
} |
if (eciMode != 3) { |
count += 12; |
} |
for (i = 0; i < inputLength; i++) { |
if (inputMode[i] != currentMode) { |
count += 4; |
switch (inputMode[i]) { |
case KANJI: |
count += tribus(version, 8, 10, 12); |
count += blockLength(i, inputMode) * 13; |
break; |
case BINARY: |
count += tribus(version, 8, 16, 16); |
for (j = i; j < i + blockLength(i, inputMode); j++) { |
if (inputData[j] > 0xff) { |
count += 16; |
} else { |
count += 8; |
} |
} |
break; |
case ALPHANUM: |
count += tribus(version, 9, 11, 13); |
alphaLength = blockLength(i, inputMode); |
// In alphanumeric mode % becomes %% |
if (gs1) { |
for (j = i; j < i + alphaLength; j++) { // TODO: need to do this only if |
// in GS1 mode? or is the other |
// code wrong? |
// https://sourceforge.net/p/zint/tickets/104/#227b |
if (inputData[j] == '%') { |
percent++; |
} |
} |
} |
alphaLength += percent; |
switch (alphaLength % 2) { |
case 0: |
count += alphaLength / 2 * 11; |
break; |
case 1: |
count += (alphaLength - 1) / 2 * 11; |
count += 6; |
break; |
} |
break; |
case NUMERIC: |
count += tribus(version, 10, 12, 14); |
switch (blockLength(i, inputMode) % 3) { |
case 0: |
count += blockLength(i, inputMode) / 3 * 10; |
break; |
case 1: |
count += (blockLength(i, inputMode) - 1) / 3 * 10; |
count += 4; |
break; |
case 2: |
count += (blockLength(i, inputMode) - 2) / 3 * 10; |
count += 7; |
break; |
} |
break; |
} |
currentMode = inputMode[i]; |
} |
} |
return count; |
} |
/** |
* Implements a custom optimization algorithm, because implementation of the algorithm shown in |
* Annex J.2 created LONGER binary sequences. |
*/ |
private static QrMode[] applyOptimisation(final int version, final QrMode[] inputMode) { |
final int inputLength = inputMode.length; |
int blockCount = 0; |
int i, j; |
QrMode currentMode = QrMode.NULL; |
for (i = 0; i < inputLength; i++) { |
if (inputMode[i] != currentMode) { |
currentMode = inputMode[i]; |
blockCount++; |
} |
} |
final int[] blockLength = new int[blockCount]; |
final QrMode[] blockMode = new QrMode[blockCount]; |
j = -1; |
currentMode = QrMode.NULL; |
for (i = 0; i < inputLength; i++) { |
if (inputMode[i] != currentMode) { |
j++; |
blockLength[j] = 1; |
blockMode[j] = inputMode[i]; |
currentMode = inputMode[i]; |
} else { |
blockLength[j]++; |
} |
} |
if (blockCount > 1) { |
// Search forward |
for (i = 0; i <= blockCount - 2; i++) { |
if (blockMode[i] == QrMode.BINARY) { |
switch (blockMode[i + 1]) { |
case KANJI: |
if (blockLength[i + 1] < tribus(version, 4, 5, 6)) { |
blockMode[i + 1] = QrMode.BINARY; |
} |
break; |
case ALPHANUM: |
if (blockLength[i + 1] < tribus(version, 7, 8, 9)) { |
blockMode[i + 1] = QrMode.BINARY; |
} |
break; |
case NUMERIC: |
if (blockLength[i + 1] < tribus(version, 3, 4, 5)) { |
blockMode[i + 1] = QrMode.BINARY; |
} |
break; |
} |
} |
if (blockMode[i] == QrMode.ALPHANUM && blockMode[i + 1] == QrMode.NUMERIC) { |
if (blockLength[i + 1] < tribus(version, 6, 8, 10)) { |
blockMode[i + 1] = QrMode.ALPHANUM; |
} |
} |
} |
// Search backward |
for (i = blockCount - 1; i > 0; i--) { |
if (blockMode[i] == QrMode.BINARY) { |
switch (blockMode[i - 1]) { |
case KANJI: |
if (blockLength[i - 1] < tribus(version, 4, 5, 6)) { |
blockMode[i - 1] = QrMode.BINARY; |
} |
break; |
case ALPHANUM: |
if (blockLength[i - 1] < tribus(version, 7, 8, 9)) { |
blockMode[i - 1] = QrMode.BINARY; |
} |
break; |
case NUMERIC: |
if (blockLength[i - 1] < tribus(version, 3, 4, 5)) { |
blockMode[i - 1] = QrMode.BINARY; |
} |
break; |
} |
} |
if (blockMode[i] == QrMode.ALPHANUM && blockMode[i - 1] == QrMode.NUMERIC) { |
if (blockLength[i - 1] < tribus(version, 6, 8, 10)) { |
blockMode[i - 1] = QrMode.ALPHANUM; |
} |
} |
} |
} |
// ZINT NOTE: this method is different from the original Zint code in that it creates a |
// new array to hold the optimized values and returns it, rather than modifying the |
// original array; this allows this method to be called as many times as we want without |
// worrying about side effects |
final QrMode[] optimized = new QrMode[inputMode.length]; |
j = 0; |
for (int block = 0; block < blockCount; block++) { |
currentMode = blockMode[block]; |
for (i = 0; i < blockLength[block]; i++) { |
optimized[j] = currentMode; |
j++; |
} |
} |
return optimized; |
} |
/** Find the length of the block starting from 'start'. */ |
private static int blockLength(final int start, final QrMode[] inputMode) { |
final QrMode mode = inputMode[start]; |
int count = 0; |
final int i = start; |
do { |
count++; |
} while (i + count < inputMode.length && inputMode[i + count] == mode); |
return count; |
} |
/** Choose from three numbers based on version. */ |
private static int tribus(final int version, final int a, final int b, final int c) { |
if (version < 10) { |
return a; |
} else if (version >= 10 && version <= 26) { |
return b; |
} else { |
return c; |
} |
} |
/** Returns true if input is in the Alphanumeric set (see Table J.1) */ |
private static boolean isAlpha(final int c) { |
return c >= '0' && c <= '9' || c >= 'A' && c <= 'Z' || c == ' ' || c == '$' || c == '%' || c == '*' || c == '+' || c == '-' || c == '.' || c == '/' || c == ':'; |
} |
/** Returns true if input is in the Numeric set (see Table J.1) */ |
private static boolean isNumeric(final int c) { |
return c >= '0' && c <= '9'; |
} |
/** Converts input data to a binary stream and adds padding. */ |
private void qrBinary(final int[] datastream, final int version, final int target_binlen, final QrMode[] inputMode, final int[] inputData, final boolean gs1, final int eciMode, |
final int est_binlen) { |
// TODO: make encodeInfo a StringBuilder, make this method static? |
int position = 0; |
int short_data_block_length, i; |
int padbits; |
int current_binlen, current_bytes; |
int toggle; |
QrMode data_block; |
final StringBuilder binary = new StringBuilder(est_binlen + 12); |
if (gs1) { |
binary.append("0101"); /* FNC1 */ |
} |
if (eciMode != 3) { |
binary.append("0111"); /* ECI (Table 4) */ |
if (eciMode <= 127) { |
binaryAppend(eciMode, 8, binary); /* 000000 to 000127 */ |
} else if (eciMode <= 16383) { |
binaryAppend(0x8000 + eciMode, 16, binary); /* 000000 to 016383 */ |
} else { |
binaryAppend(0xC00000 + eciMode, 24, binary); /* 000000 to 999999 */ |
} |
} |
info("Encoding: "); |
do { |
data_block = inputMode[position]; |
short_data_block_length = 0; |
do { |
short_data_block_length++; |
} while (short_data_block_length + position < inputMode.length && inputMode[position + short_data_block_length] == data_block); |
switch (data_block) { |
case KANJI: |
/* Kanji mode */ |
/* Mode indicator */ |
binary.append("1000"); |
/* Character count indicator */ |
binaryAppend(short_data_block_length, tribus(version, 8, 10, 12), binary); |
info("KNJI "); |
/* Character representation */ |
for (i = 0; i < short_data_block_length; i++) { |
int jis = inputData[position + i]; |
if (jis >= 0x8140 && jis <= 0x9ffc) { |
jis -= 0x8140; |
} else if (jis >= 0xe040 && jis <= 0xebbf) { |
jis -= 0xc140; |
} |
final int prod = (jis >> 8) * 0xc0 + (jis & 0xff); |
binaryAppend(prod, 13, binary); |
infoSpace(prod); |
} |
break; |
case BINARY: |
/* Byte mode */ |
/* Mode indicator */ |
binary.append("0100"); |
/* Character count indicator */ |
binaryAppend(short_data_block_length, tribus(version, 8, 16, 16), binary); |
info("BYTE "); |
/* Character representation */ |
for (i = 0; i < short_data_block_length; i++) { |
int b = inputData[position + i]; |
if (b == FNC1) { |
b = 0x1d; /* FNC1 */ |
} |
binaryAppend(b, 8, binary); |
infoSpace(b); |
} |
break; |
case ALPHANUM: |
/* Alphanumeric mode */ |
/* Mode indicator */ |
binary.append("0010"); |
/* If in GS1 mode, expand FNC1 -> '%' and expand '%' -> '%%' in a new array */ |
int percentCount = 0; |
if (gs1) { |
for (i = 0; i < short_data_block_length; i++) { |
if (inputData[position + i] == '%') { |
percentCount++; |
} |
} |
} |
final int[] inputExpanded = new int[short_data_block_length + percentCount]; |
percentCount = 0; |
for (i = 0; i < short_data_block_length; i++) { |
final int c = inputData[position + i]; |
if (c == FNC1) { |
inputExpanded[i + percentCount] = '%'; /* FNC1 */ |
} else { |
inputExpanded[i + percentCount] = c; |
if (gs1 && c == '%') { |
percentCount++; |
inputExpanded[i + percentCount] = c; |
} |
} |
} |
/* Character count indicator */ |
binaryAppend(inputExpanded.length, tribus(version, 9, 11, 13), binary); |
info("ALPH "); |
/* Character representation */ |
for (i = 0; i + 1 < inputExpanded.length; i += 2) { |
final int first = positionOf((char) inputExpanded[i], RHODIUM); |
final int second = positionOf((char) inputExpanded[i + 1], RHODIUM); |
final int prod = first * 45 + second; |
final int count = 2; |
binaryAppend(prod, 1 + 5 * count, binary); |
infoSpace(prod); |
} |
if (inputExpanded.length % 2 != 0) { |
final int first = positionOf((char) inputExpanded[inputExpanded.length - 1], RHODIUM); |
final int prod = first; |
final int count = 1; |
binaryAppend(prod, 1 + 5 * count, binary); |
infoSpace(prod); |
} |
break; |
case NUMERIC: |
/* Numeric mode */ |
/* Mode indicator */ |
binary.append("0001"); |
/* Character count indicator */ |
binaryAppend(short_data_block_length, tribus(version, 10, 12, 14), binary); |
info("NUMB "); |
/* Character representation */ |
i = 0; |
while (i < short_data_block_length) { |
final int first = Character.getNumericValue(inputData[position + i]); |
int count = 1; |
int prod = first; |
if (i + 1 < short_data_block_length) { |
final int second = Character.getNumericValue(inputData[position + i + 1]); |
count = 2; |
prod = prod * 10 + second; |
if (i + 2 < short_data_block_length) { |
final int third = Character.getNumericValue(inputData[position + i + 2]); |
count = 3; |
prod = prod * 10 + third; |
} |
} |
binaryAppend(prod, 1 + 3 * count, binary); |
infoSpace(prod); |
i += count; |
} |
break; |
} |
position += short_data_block_length; |
} while (position < inputMode.length); |
infoLine(); |
/* Terminator */ |
binary.append("0000"); |
current_binlen = binary.length(); |
padbits = 8 - current_binlen % 8; |
if (padbits == 8) { |
padbits = 0; |
} |
current_bytes = (current_binlen + padbits) / 8; |
/* Padding bits */ |
for (i = 0; i < padbits; i++) { |
binary.append('0'); |
} |
/* Put data into 8-bit codewords */ |
for (i = 0; i < current_bytes; i++) { |
datastream[i] = 0x00; |
for (int p = 0; p < 8; p++) { |
if (binary.charAt(i * 8 + p) == '1') { |
datastream[i] += 0x80 >> p; |
} |
} |
} |
/* Add pad codewords */ |
toggle = 0; |
for (i = current_bytes; i < target_binlen; i++) { |
if (toggle == 0) { |
datastream[i] = 0xec; |
toggle = 1; |
} else { |
datastream[i] = 0x11; |
toggle = 0; |
} |
} |
info("Codewords: "); |
for (i = 0; i < target_binlen; i++) { |
infoSpace(datastream[i]); |
} |
infoLine(); |
} |
private static void binaryAppend(final int value, final int length, final StringBuilder binary) { |
final int start = 0x01 << length - 1; |
for (int i = 0; i < length; i++) { |
if ((value & start >> i) != 0) { |
binary.append('1'); |
} else { |
binary.append('0'); |
} |
} |
} |
/** |
* Splits data into blocks, adds error correction and then interleaves the blocks and error |
* correction data. |
*/ |
private static void addEcc(final int[] fullstream, final int[] datastream, final int version, final int data_cw, final int blocks) { |
final int ecc_cw = QR_TOTAL_CODEWORDS[version - 1] - data_cw; |
final int short_data_block_length = data_cw / blocks; |
final int qty_long_blocks = data_cw % blocks; |
final int qty_short_blocks = blocks - qty_long_blocks; |
final int ecc_block_length = ecc_cw / blocks; |
int i, j, length_this_block, posn; |
final int[] data_block = new int[short_data_block_length + 2]; |
final int[] ecc_block = new int[ecc_block_length + 2]; |
final int[] interleaved_data = new int[data_cw + 2]; |
final int[] interleaved_ecc = new int[ecc_cw + 2]; |
posn = 0; |
for (i = 0; i < blocks; i++) { |
if (i < qty_short_blocks) { |
length_this_block = short_data_block_length; |
} else { |
length_this_block = short_data_block_length + 1; |
} |
for (j = 0; j < ecc_block_length; j++) { |
ecc_block[j] = 0; |
} |
for (j = 0; j < length_this_block; j++) { |
data_block[j] = datastream[posn + j]; |
} |
final ReedSolomon rs = new ReedSolomon(); |
rs.init_gf(0x11d); |
rs.init_code(ecc_block_length, 0); |
rs.encode(length_this_block, data_block); |
for (j = 0; j < ecc_block_length; j++) { |
ecc_block[j] = rs.getResult(j); |
} |
for (j = 0; j < short_data_block_length; j++) { |
interleaved_data[j * blocks + i] = data_block[j]; |
} |
if (i >= qty_short_blocks) { |
interleaved_data[short_data_block_length * blocks + i - qty_short_blocks] = data_block[short_data_block_length]; |
} |
for (j = 0; j < ecc_block_length; j++) { |
interleaved_ecc[j * blocks + i] = ecc_block[ecc_block_length - j - 1]; |
} |
posn += length_this_block; |
} |
for (j = 0; j < data_cw; j++) { |
fullstream[j] = interleaved_data[j]; |
} |
for (j = 0; j < ecc_cw; j++) { |
fullstream[j + data_cw] = interleaved_ecc[j]; |
} |
} |
private static void setupGrid(final int[] grid, final int size, final int version) { |
int i; |
boolean toggle = true; |
/* Add timing patterns */ |
for (i = 0; i < size; i++) { |
if (toggle) { |
grid[6 * size + i] = 0x21; |
grid[i * size + 6] = 0x21; |
toggle = false; |
} else { |
grid[6 * size + i] = 0x20; |
grid[i * size + 6] = 0x20; |
toggle = true; |
} |
} |
/* Add finder patterns */ |
placeFinder(grid, size, 0, 0); |
placeFinder(grid, size, 0, size - 7); |
placeFinder(grid, size, size - 7, 0); |
/* Add separators */ |
for (i = 0; i < 7; i++) { |
grid[7 * size + i] = 0x10; |
grid[i * size + 7] = 0x10; |
grid[7 * size + size - 1 - i] = 0x10; |
grid[i * size + size - 8] = 0x10; |
grid[(size - 8) * size + i] = 0x10; |
grid[(size - 1 - i) * size + 7] = 0x10; |
} |
grid[7 * size + 7] = 0x10; |
grid[7 * size + size - 8] = 0x10; |
grid[(size - 8) * size + 7] = 0x10; |
/* Add alignment patterns */ |
if (version != 1) { |
/* Version 1 does not have alignment patterns */ |
final int loopsize = QR_ALIGN_LOOPSIZE[version - 1]; |
for (int x = 0; x < loopsize; x++) { |
for (int y = 0; y < loopsize; y++) { |
final int xcoord = QR_TABLE_E1[(version - 2) * 7 + x]; |
final int ycoord = QR_TABLE_E1[(version - 2) * 7 + y]; |
if ((grid[ycoord * size + xcoord] & 0x10) == 0) { |
placeAlign(grid, size, xcoord, ycoord); |
} |
} |
} |
} |
/* Reserve space for format information */ |
for (i = 0; i < 8; i++) { |
grid[8 * size + i] += 0x20; |
grid[i * size + 8] += 0x20; |
grid[8 * size + size - 1 - i] = 0x20; |
grid[(size - 1 - i) * size + 8] = 0x20; |
} |
grid[8 * size + 8] += 0x20; |
grid[(size - 1 - 7) * size + 8] = 0x21; /* Dark Module from Figure 25 */ |
/* Reserve space for version information */ |
if (version >= 7) { |
for (i = 0; i < 6; i++) { |
grid[(size - 9) * size + i] = 0x20; |
grid[(size - 10) * size + i] = 0x20; |
grid[(size - 11) * size + i] = 0x20; |
grid[i * size + size - 9] = 0x20; |
grid[i * size + size - 10] = 0x20; |
grid[i * size + size - 11] = 0x20; |
} |
} |
} |
private static void placeFinder(final int[] grid, final int size, final int x, final int y) { |
final int[] finder = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }; |
for (int xp = 0; xp < 7; xp++) { |
for (int yp = 0; yp < 7; yp++) { |
if (finder[xp + 7 * yp] == 1) { |
grid[(yp + y) * size + xp + x] = 0x11; |
} else { |
grid[(yp + y) * size + xp + x] = 0x10; |
} |
} |
} |
} |
private static void placeAlign(final int[] grid, final int size, int x, int y) { |
final int[] alignment = { 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1 }; |
x -= 2; |
y -= 2; /* Input values represent centre of pattern */ |
for (int xp = 0; xp < 5; xp++) { |
for (int yp = 0; yp < 5; yp++) { |
if (alignment[xp + 5 * yp] == 1) { |
grid[(yp + y) * size + xp + x] = 0x11; |
} else { |
grid[(yp + y) * size + xp + x] = 0x10; |
} |
} |
} |
} |
private static void populateGrid(final int[] grid, final int size, final int[] fullstream, final int cw) { |
boolean goingUp = true; |
int row = 0; /* right hand side */ |
int i, n, y; |
n = cw * 8; |
y = size - 1; |
i = 0; |
do { |
int x = size - 2 - row * 2; |
if (x < 6) { |
x--; /* skip over vertical timing pattern */ |
} |
if ((grid[y * size + x + 1] & 0xf0) == 0) { |
if (cwbit(fullstream, i)) { |
grid[y * size + x + 1] = 0x01; |
} else { |
grid[y * size + x + 1] = 0x00; |
} |
i++; |
} |
if (i < n) { |
if ((grid[y * size + x] & 0xf0) == 0) { |
if (cwbit(fullstream, i)) { |
grid[y * size + x] = 0x01; |
} else { |
grid[y * size + x] = 0x00; |
} |
i++; |
} |
} |
if (goingUp) { |
y--; |
} else { |
y++; |
} |
if (y == -1) { |
/* reached the top */ |
row++; |
y = 0; |
goingUp = false; |
} |
if (y == size) { |
/* reached the bottom */ |
row++; |
y = size - 1; |
goingUp = true; |
} |
} while (i < n); |
} |
private static boolean cwbit(final int[] fullstream, final int i) { |
return (fullstream[i / 8] & 0x80 >> i % 8) != 0; |
} |
private static int applyBitmask(final int[] grid, final int size, final EccLevel ecc_level) { |
int x, y; |
char p; |
int pattern; |
int best_val, best_pattern; |
final int[] penalty = new int[8]; |
final byte[] mask = new byte[size * size]; |
final byte[] eval = new byte[size * size]; |
/* Perform data masking */ |
for (x = 0; x < size; x++) { |
for (y = 0; y < size; y++) { |
mask[y * size + x] = 0x00; |
// all eight bit mask variants are encoded in the 8 bits of the bytes that make up |
// the mask array |
if ((grid[y * size + x] & 0xf0) == 0) { // exclude areas not to be masked |
if ((y + x & 1) == 0) { |
mask[y * size + x] += (byte) 0x01; |
} |
if ((y & 1) == 0) { |
mask[y * size + x] += (byte) 0x02; |
} |
if (x % 3 == 0) { |
mask[y * size + x] += (byte) 0x04; |
} |
if ((y + x) % 3 == 0) { |
mask[y * size + x] += (byte) 0x08; |
} |
if ((y / 2 + x / 3 & 1) == 0) { |
mask[y * size + x] += (byte) 0x10; |
} |
if ((y * x & 1) + y * x % 3 == 0) { |
mask[y * size + x] += (byte) 0x20; |
} |
if (((y * x & 1) + y * x % 3 & 1) == 0) { |
mask[y * size + x] += (byte) 0x40; |
} |
if (((y + x & 1) + y * x % 3 & 1) == 0) { |
mask[y * size + x] += (byte) 0x80; |
} |
} |
} |
} |
/* Apply data masks to grid, result in eval */ |
for (x = 0; x < size; x++) { |
for (y = 0; y < size; y++) { |
if ((grid[y * size + x] & 0x01) != 0) { |
p = 0xff; |
} else { |
p = 0x00; |
} |
eval[y * size + x] = (byte) (mask[y * size + x] ^ p); |
} |
} |
/* Evaluate result */ |
for (pattern = 0; pattern < 8; pattern++) { |
addFormatInfoEval(eval, size, ecc_level, pattern); |
penalty[pattern] = evaluate(eval, size, pattern); |
} |
best_pattern = 0; |
best_val = penalty[0]; |
for (pattern = 1; pattern < 8; pattern++) { |
if (penalty[pattern] < best_val) { |
best_pattern = pattern; |
best_val = penalty[pattern]; |
} |
} |
/* Apply mask */ |
for (x = 0; x < size; x++) { |
for (y = 0; y < size; y++) { |
if ((mask[y * size + x] & 0x01 << best_pattern) != 0) { |
if ((grid[y * size + x] & 0x01) != 0) { |
grid[y * size + x] = 0x00; |
} else { |
grid[y * size + x] = 0x01; |
} |
} |
} |
} |
return best_pattern; |
} |
/** Adds format information to eval. */ |
private static void addFormatInfoEval(final byte[] eval, final int size, final EccLevel ecc_level, final int pattern) { |
int format = pattern; |
int seq; |
int i; |
switch (ecc_level) { |
case L: |
format += 0x08; |
break; |
case Q: |
format += 0x18; |
break; |
case H: |
format += 0x10; |
break; |
} |
seq = QR_ANNEX_C[format]; |
for (i = 0; i < 6; i++) { |
eval[i * size + 8] = (byte) ((seq >> i & 0x01) != 0 ? 0x01 >> pattern : 0x00); |
} |
for (i = 0; i < 8; i++) { |
eval[8 * size + size - i - 1] = (byte) ((seq >> i & 0x01) != 0 ? 0x01 >> pattern : 0x00); |
} |
for (i = 0; i < 6; i++) { |
eval[8 * size + 5 - i] = (byte) ((seq >> i + 9 & 0x01) != 0 ? 0x01 >> pattern : 0x00); |
} |
for (i = 0; i < 7; i++) { |
eval[(size - 7 + i) * size + 8] = (byte) ((seq >> i + 8 & 0x01) != 0 ? 0x01 >> pattern : 0x00); |
} |
eval[7 * size + 8] = (byte) ((seq >> 6 & 0x01) != 0 ? 0x01 >> pattern : 0x00); |
eval[8 * size + 8] = (byte) ((seq >> 7 & 0x01) != 0 ? 0x01 >> pattern : 0x00); |
eval[8 * size + 7] = (byte) ((seq >> 8 & 0x01) != 0 ? 0x01 >> pattern : 0x00); |
} |
private static int evaluate(final byte[] eval, final int size, final int pattern) { |
int x, y, block, weight; |
int result = 0; |
int state; |
int p; |
int dark_mods; |
int percentage, k; |
int a, b, afterCount, beforeCount; |
final byte[] local = new byte[size * size]; |
// all eight bit mask variants have been encoded in the 8 bits of the bytes |
// that make up the grid array; select them for evaluation according to the |
// desired pattern |
for (x = 0; x < size; x++) { |
for (y = 0; y < size; y++) { |
if ((eval[y * size + x] & 0x01 << pattern) != 0) { |
local[y * size + x] = '1'; |
} else { |
local[y * size + x] = '0'; |
} |
} |
} |
/* Test 1: Adjacent modules in row/column in same colour */ |
/* Vertical */ |
for (x = 0; x < size; x++) { |
state = local[x]; |
block = 0; |
for (y = 0; y < size; y++) { |
if (local[y * size + x] == state) { |
block++; |
} else { |
if (block > 5) { |
result += 3 + block - 5; |
} |
block = 0; |
state = local[y * size + x]; |
} |
} |
if (block > 5) { |
result += 3 + block - 5; |
} |
} |
/* Horizontal */ |
for (y = 0; y < size; y++) { |
state = local[y * size]; |
block = 0; |
for (x = 0; x < size; x++) { |
if (local[y * size + x] == state) { |
block++; |
} else { |
if (block > 5) { |
result += 3 + block - 5; |
} |
block = 0; |
state = local[y * size + x]; |
} |
} |
if (block > 5) { |
result += 3 + block - 5; |
} |
} |
/* Test 2: Block of modules in same color */ |
for (x = 0; x < size - 1; x++) { |
for (y = 0; y < size - 1; y++) { |
if (local[y * size + x] == local[(y + 1) * size + x] && local[y * size + x] == local[y * size + x + 1] && local[y * size + x] == local[(y + 1) * size + x + 1]) { |
result += 3; |
} |
} |
} |
/* Test 3: 1:1:3:1:1 ratio pattern in row/column */ |
/* Vertical */ |
for (x = 0; x < size; x++) { |
for (y = 0; y < size - 7; y++) { |
p = 0; |
for (weight = 0; weight < 7; weight++) { |
if (local[(y + weight) * size + x] == '1') { |
p += 0x40 >> weight; |
} |
} |
if (p == 0x5d) { |
/* Pattern found, check before and after */ |
beforeCount = 0; |
for (b = y - 4; b < y; b++) { |
if (b < 0) { |
beforeCount++; |
} else { |
if (local[b * size + x] == '0') { |
beforeCount++; |
} else { |
beforeCount = 0; |
} |
} |
} |
afterCount = 0; |
for (a = y + 7; a <= y + 10; a++) { |
if (a >= size) { |
afterCount++; |
} else { |
if (local[a * size + x] == '0') { |
afterCount++; |
} else { |
afterCount = 0; |
} |
} |
} |
if (beforeCount == 4 || afterCount == 4) { |
// Pattern is preceded or followed by light area 4 modules wide |
result += 40; |
} |
} |
} |
} |
/* Horizontal */ |
for (y = 0; y < size; y++) { |
for (x = 0; x < size - 7; x++) { |
p = 0; |
for (weight = 0; weight < 7; weight++) { |
if (local[y * size + x + weight] == '1') { |
p += 0x40 >> weight; |
} |
} |
if (p == 0x5d) { |
/* Pattern found, check before and after */ |
beforeCount = 0; |
for (b = x - 4; b < x; b++) { |
if (b < 0) { |
beforeCount++; |
} else { |
if (local[y * size + b] == '0') { |
beforeCount++; |
} else { |
beforeCount = 0; |
} |
} |
} |
afterCount = 0; |
for (a = x + 7; a <= x + 10; a++) { |
if (a >= size) { |
afterCount++; |
} else { |
if (local[y * size + a] == '0') { |
afterCount++; |
} else { |
afterCount = 0; |
} |
} |
} |
if (beforeCount == 4 || afterCount == 4) { |
// Pattern is preceded or followed by light area 4 modules wide |
result += 40; |
} |
} |
} |
} |
/* Test 4: Proportion of dark modules in entire symbol */ |
dark_mods = 0; |
for (x = 0; x < size; x++) { |
for (y = 0; y < size; y++) { |
if (local[y * size + x] == '1') { |
dark_mods++; |
} |
} |
} |
percentage = 100 * (dark_mods / (size * size)); |
if (percentage <= 50) { |
k = (100 - percentage - 50) / 5; |
} else { |
k = (percentage - 50) / 5; |
} |
result += 10 * k; |
return result; |
} |
/* Adds format information to grid. */ |
private static void addFormatInfo(final int[] grid, final int size, final EccLevel ecc_level, final int pattern) { |
int format = pattern; |
int seq; |
int i; |
switch (ecc_level) { |
case L: |
format += 0x08; |
break; |
case Q: |
format += 0x18; |
break; |
case H: |
format += 0x10; |
break; |
} |
seq = QR_ANNEX_C[format]; |
for (i = 0; i < 6; i++) { |
grid[i * size + 8] += seq >> i & 0x01; |
} |
for (i = 0; i < 8; i++) { |
grid[8 * size + size - i - 1] += seq >> i & 0x01; |
} |
for (i = 0; i < 6; i++) { |
grid[8 * size + 5 - i] += seq >> i + 9 & 0x01; |
} |
for (i = 0; i < 7; i++) { |
grid[(size - 7 + i) * size + 8] += seq >> i + 8 & 0x01; |
} |
grid[7 * size + 8] += seq >> 6 & 0x01; |
grid[8 * size + 8] += seq >> 7 & 0x01; |
grid[8 * size + 7] += seq >> 8 & 0x01; |
} |
/** Adds version information. */ |
private static void addVersionInfo(final int[] grid, final int size, final int version) { |
// TODO: Zint masks with 0x41 instead of 0x01; which is correct? |
// https://sourceforge.net/p/zint/tickets/110/ |
final int version_data = QR_ANNEX_D[version - 7]; |
for (int i = 0; i < 6; i++) { |
grid[(size - 11) * size + i] += version_data >> i * 3 & 0x01; |
grid[(size - 10) * size + i] += version_data >> i * 3 + 1 & 0x01; |
grid[(size - 9) * size + i] += version_data >> i * 3 + 2 & 0x01; |
grid[i * size + size - 11] += version_data >> i * 3 & 0x01; |
grid[i * size + size - 10] += version_data >> i * 3 + 1 & 0x01; |
grid[i * size + size - 9] += version_data >> i * 3 + 2 & 0x01; |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/HumanReadableLocation.java |
---|
New file |
0,0 → 1,30 |
/* |
* Copyright 2015 Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
/** |
* The location of a bar code's human-readable text. |
*/ |
public enum HumanReadableLocation { |
/** Display the human-readable text below the bar code. */ |
BOTTOM, |
/** Display the human-readable text above the bar code. */ |
TOP, |
/** Do not display the human-readable text. */ |
NONE |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/EanUpcAddOn.java |
---|
New file |
0,0 → 1,112 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
/** |
* <p> |
* Implements EAN/UPC add-on bar code symbology according to BS EN 797:1996. |
* |
* @see Ean |
* @see Upc |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class EanUpcAddOn extends Symbol { |
private static final String[] EAN_SET_A = { "3211", "2221", "2122", "1411", "1132", "1231", "1114", "1312", "1213", "3112" }; |
private static final String[] EAN_SET_B = { "1123", "1222", "2212", "1141", "2311", "1321", "4111", "2131", "3121", "2113" }; |
private static final String[] EAN2_PARITY = { "AA", "AB", "BA", "BB" }; |
private static final String[] EAN5_PARITY = { "BBAAA", "BABAA", "BAABA", "BAAAB", "ABBAA", "AABBA", "AAABB", "ABABA", "ABAAB", "AABAB" }; |
@Override |
protected void encode() { |
if (!this.content.matches("[0-9]+")) { |
throw new OkapiException("Invalid characters in input"); |
} |
if (this.content.length() > 5) { |
throw new OkapiException("Input data too long"); |
} |
final int targetLength = this.content.length() > 2 ? 5 : 2; |
if (this.content.length() < targetLength) { |
for (int i = this.content.length(); i < targetLength; i++) { |
this.content = '0' + this.content; |
} |
} |
final String bars = targetLength == 2 ? ean2(this.content) : ean5(this.content); |
this.readable = this.content; |
this.pattern = new String[] { bars }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
private static String ean2(final String content) { |
final int sum = (content.charAt(0) - '0') * 10 + content.charAt(1) - '0'; |
final String parity = EAN2_PARITY[sum % 4]; |
final StringBuilder sb = new StringBuilder(); |
sb.append("112"); /* Start */ |
for (int i = 0; i < 2; i++) { |
final int val = content.charAt(i) - '0'; |
if (parity.charAt(i) == 'B') { |
sb.append(EAN_SET_B[val]); |
} else { |
sb.append(EAN_SET_A[val]); |
} |
if (i != 1) { /* Glyph separator */ |
sb.append("11"); |
} |
} |
return sb.toString(); |
} |
private static String ean5(final String content) { |
int sum = 0; |
for (int i = 0; i < 5; i++) { |
if (i % 2 == 0) { |
sum += 3 * (content.charAt(i) - '0'); |
} else { |
sum += 9 * (content.charAt(i) - '0'); |
} |
} |
final String parity = EAN5_PARITY[sum % 10]; |
final StringBuilder sb = new StringBuilder(); |
sb.append("112"); /* Start */ |
for (int i = 0; i < 5; i++) { |
final int val = content.charAt(i) - '0'; |
if (parity.charAt(i) == 'B') { |
sb.append(EAN_SET_B[val]); |
} else { |
sb.append(EAN_SET_A[val]); |
} |
if (i != 4) { /* Glyph separator */ |
sb.append("11"); |
} |
} |
return sb.toString(); |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Code93.java |
---|
New file |
0,0 → 1,189 |
/* |
* Copyright 2014-2015 Robin Stuart, Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.util.Arrays.positionOf; |
/** |
* <p> |
* Implements <a href="http://en.wikipedia.org/wiki/Code_93">Code 93</a>. |
* |
* <p> |
* Supports encoding of 7-bit ASCII text. Two check digits are added. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
* @author Daniel Gredler |
*/ |
public class Code93 extends Symbol { |
/** |
* Code 93 control characters, indexed by ASCII codes (NOTE: a = Ctrl $, b = Ctrl %, c = Ctrl /, |
* d = Ctrl + for sequences of two characters). |
*/ |
private static final String[] CODE_93_CTRL = { "bU", "aA", "aB", "aC", "aD", "aE", "aF", "aG", "aH", "aI", "aJ", "aK", "aL", "aM", "aN", "aO", "aP", "aQ", "aR", "aS", "aT", "aU", "aV", "aW", "aX", |
"aY", "aZ", "bA", "bB", "bC", "bD", "bE", " ", "cA", "cB", "cC", "$", "%", "cF", "cG", "cH", "cI", "cJ", "+", "cL", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "cZ", |
"bF", "bG", "bH", "bI", "bJ", "bV", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bK", "bL", "bM", |
"bN", "bO", "bW", "dA", "dB", "dC", "dD", "dE", "dF", "dG", "dH", "dI", "dJ", "dK", "dL", "dM", "dN", "dO", "dP", "dQ", "dR", "dS", "dT", "dU", "dV", "dW", "dX", "dY", "dZ", "bP", "bQ", |
"bR", "bS", "bT" }; |
/** |
* Mapping of control characters to pattern table index (NOTE: a = Ctrl $, b = Ctrl %, c = Ctrl |
* /, d = Ctrl + for sequences of two characters). |
*/ |
private static final char[] CODE_93_LOOKUP = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', 'a', 'b', 'c', 'd' }; |
/** Code 93 pattern table. */ |
private static final String[] CODE_93_TABLE = { "131112", "111213", "111312", "111411", "121113", "121212", "121311", "111114", "131211", "141111", "211113", "211212", "211311", "221112", |
"221211", "231111", "112113", "112212", "112311", "122112", "132111", "111123", "111222", "111321", "121122", "131121", "212112", "212211", "211122", "211221", "221121", "222111", |
"112122", "112221", "122121", "123111", "121131", "311112", "311211", "321111", "112131", "113121", "211131", "121221", "312111", "311121", "122211" }; |
/** Whether or not to show check digits in the human-readable text. */ |
private boolean showCheckDigits = true; |
/** Optional start/stop delimiter to be shown in the human-readable text. */ |
private Character startStopDelimiter; |
/** |
* Sets whether or not to show check digits in the human-readable text (defaults to |
* <code>true</code>). |
* |
* @param showCheckDigits whether or not to show check digits in the human-readable text |
*/ |
public void setShowCheckDigits(final boolean showCheckDigits) { |
this.showCheckDigits = showCheckDigits; |
} |
/** |
* Returns whether or not this symbol shows check digits in the human-readable text. |
* |
* @return whether or not this symbol shows check digits in the human-readable text |
*/ |
public boolean getShowCheckDigits() { |
return this.showCheckDigits; |
} |
/** |
* Sets an optional start/stop delimiter to be shown in the human-readable text (defaults to |
* <code>null</code>). |
* |
* @param startStopDelimiter an optional start/stop delimiter to be shown in the human-readable |
* text |
*/ |
public void setStartStopDelimiter(final Character startStopDelimiter) { |
this.startStopDelimiter = startStopDelimiter; |
} |
/** |
* Returns the optional start/stop delimiter to be shown in the human-readable text. |
* |
* @return the optional start/stop delimiter to be shown in the human-readable text |
*/ |
public Character getStartStopDelimiter() { |
return this.startStopDelimiter; |
} |
/** {@inheritDoc} */ |
@Override |
protected void encode() { |
final char[] controlChars = toControlChars(this.content); |
int l = controlChars.length; |
if (!this.content.matches("[\u0000-\u007F]+")) { |
throw new OkapiException("Invalid characters in input data"); |
} |
final int[] values = new int[controlChars.length + 2]; |
for (int i = 0; i < l; i++) { |
values[i] = positionOf(controlChars[i], CODE_93_LOOKUP); |
} |
final int c = calculateCheckDigitC(values, l); |
values[l] = c; |
l++; |
final int k = calculateCheckDigitK(values, l); |
values[l] = k; |
l++; |
this.readable = this.content; |
if (this.showCheckDigits) { |
this.readable = this.readable + CODE_93_LOOKUP[c] + CODE_93_LOOKUP[k]; |
} |
if (this.startStopDelimiter != null) { |
this.readable = this.startStopDelimiter + this.readable + this.startStopDelimiter; |
} |
infoLine("Check Digit C: " + c); |
infoLine("Check Digit K: " + k); |
this.pattern = new String[] { toPattern(values) }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
private static char[] toControlChars(final String s) { |
final StringBuilder buffer = new StringBuilder(); |
final char[] chars = s.toCharArray(); |
for (int i = 0; i < chars.length; i++) { |
final int asciiCode = chars[i]; |
buffer.append(CODE_93_CTRL[asciiCode]); |
} |
return buffer.toString().toCharArray(); |
} |
private static int calculateCheckDigitC(final int[] values, final int length) { |
int c = 0; |
int weight = 1; |
for (int i = length - 1; i >= 0; i--) { |
c += values[i] * weight; |
weight++; |
if (weight == 21) { |
weight = 1; |
} |
} |
c = c % 47; |
return c; |
} |
private static int calculateCheckDigitK(final int[] values, final int length) { |
int k = 0; |
int weight = 1; |
for (int i = length - 1; i >= 0; i--) { |
k += values[i] * weight; |
weight++; |
if (weight == 16) { |
weight = 1; |
} |
} |
k = k % 47; |
return k; |
} |
private static String toPattern(final int[] values) { |
final StringBuilder buffer = new StringBuilder("111141"); |
for (int i = 0; i < values.length; i++) { |
buffer.append(CODE_93_TABLE[values[i]]); |
} |
buffer.append("1111411"); |
return buffer.toString(); |
} |
/** {@inheritDoc} */ |
@Override |
protected int[] getCodewords() { |
return getPatternAsCodewords(6); |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/KixCode.java |
---|
New file |
0,0 → 1,107 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.util.Arrays.positionOf; |
import java.awt.geom.Rectangle2D; |
import java.util.Locale; |
/** |
* <p> |
* Implements Dutch Post KIX Code as used by Royal Dutch TPG Post (Netherlands). |
* |
* <p> |
* The input data can consist of digits 0-9 and characters A-Z, and should be 11 characters in |
* length. No check digit is added. |
* |
* <p> |
* KIX Code is the same as RM4SCC, but without the check digit. |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
* @see <a href="http://www.tntpost.nl/zakelijk/klantenservice/downloads/kIX_code/download.aspx">KIX |
* Code Specification</a> |
*/ |
public class KixCode extends Symbol { |
private static final String[] ROYAL_TABLE = { "TTFF", "TDAF", "TDFA", "DTAF", "DTFA", "DDAA", "TADF", "TFTF", "TFDA", "DATF", "DADA", "DFTA", "TAFD", "TFAD", "TFFT", "DAAD", "DAFT", "DFAT", |
"ATDF", "ADTF", "ADDA", "FTTF", "FTDA", "FDTA", "ATFD", "ADAD", "ADFT", "FTAD", "FTFT", "FDAT", "AADD", "AFTD", "AFDT", "FATD", "FADT", "FFTT" }; |
private static final char[] KR_SET = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', |
'V', 'W', 'X', 'Y', 'Z' }; |
@Override |
protected void encode() { |
this.content = this.content.toUpperCase(Locale.ENGLISH); |
if (!this.content.matches("[0-9A-Z]+")) { |
throw new OkapiException("Invalid characters in data"); |
} |
final StringBuilder sb = new StringBuilder(this.content.length()); |
for (int i = 0; i < this.content.length(); i++) { |
final int j = positionOf(this.content.charAt(i), KR_SET); |
sb.append(ROYAL_TABLE[j]); |
} |
final String dest = sb.toString(); |
infoLine("Encoding: " + dest); |
this.readable = ""; |
this.pattern = new String[] { dest }; |
this.row_count = 1; |
this.row_height = new int[] { -1 }; |
} |
@Override |
protected void plotSymbol() { |
int xBlock; |
int x, y, w, h; |
this.rectangles.clear(); |
x = 0; |
w = 1; |
y = 0; |
h = 0; |
for (xBlock = 0; xBlock < this.pattern[0].length(); xBlock++) { |
final char c = this.pattern[0].charAt(xBlock); |
switch (c) { |
case 'A': |
y = 0; |
h = 5; |
break; |
case 'D': |
y = 3; |
h = 5; |
break; |
case 'F': |
y = 0; |
h = 8; |
break; |
case 'T': |
y = 3; |
h = 2; |
break; |
default: |
throw new IllegalStateException("Unknown pattern character: " + c); |
} |
this.rectangles.add(new Rectangle2D.Double(x, y, w, h)); |
x += 2; |
} |
this.symbol_width = (this.pattern[0].length() - 1) * 2 + 1; // final bar doesn't need extra |
// whitespace |
this.symbol_height = 8; |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/ReedSolomon.java |
---|
New file |
0,0 → 1,103 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
/** |
* |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a> |
*/ |
public class ReedSolomon { |
private int logmod; |
private int rlen; |
private int[] logt; |
private int[] alog; |
private int[] rspoly; |
public int[] res; |
public int getResult(final int count) { |
return this.res[count]; |
} |
public void init_gf(final int poly) { |
int m, b, p, v; |
// Find the top bit, and hence the symbol size |
for (b = 1, m = 0; b <= poly; b <<= 1) { |
m++; |
} |
b >>= 1; |
m--; |
// Calculate the log/alog tables |
this.logmod = (1 << m) - 1; |
this.logt = new int[this.logmod + 1]; |
this.alog = new int[this.logmod]; |
for (p = 1, v = 0; v < this.logmod; v++) { |
this.alog[v] = p; |
this.logt[p] = v; |
p <<= 1; |
if ((p & b) != 0) { |
p ^= poly; |
} |
} |
} |
public void init_code(final int nsym, int index) { |
int i, k; |
this.rspoly = new int[nsym + 1]; |
this.rlen = nsym; |
this.rspoly[0] = 1; |
for (i = 1; i <= nsym; i++) { |
this.rspoly[i] = 1; |
for (k = i - 1; k > 0; k--) { |
if (this.rspoly[k] != 0) { |
this.rspoly[k] = this.alog[(this.logt[this.rspoly[k]] + index) % this.logmod]; |
} |
this.rspoly[k] ^= this.rspoly[k - 1]; |
} |
this.rspoly[0] = this.alog[(this.logt[this.rspoly[0]] + index) % this.logmod]; |
index++; |
} |
} |
public void encode(final int len, final int[] data) { |
int i, k, m; |
this.res = new int[this.rlen]; |
for (i = 0; i < this.rlen; i++) { |
this.res[i] = 0; |
} |
for (i = 0; i < len; i++) { |
m = this.res[this.rlen - 1] ^ data[i]; |
for (k = this.rlen - 1; k > 0; k--) { |
if (m != 0 && this.rspoly[k] != 0) { |
this.res[k] = this.res[k - 1] ^ this.alog[(this.logt[m] + this.logt[this.rspoly[k]]) % this.logmod]; |
} else { |
this.res[k] = this.res[k - 1]; |
} |
} |
if (m != 0 && this.rspoly[0] != 0) { |
this.res[0] = this.alog[(this.logt[m] + this.logt[this.rspoly[0]]) % this.logmod]; |
} else { |
this.res[0] = 0; |
} |
} |
} |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/HumanReadableAlignment.java |
---|
New file |
0,0 → 1,34 |
/* |
* Copyright 2018 Daniel Gredler |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
/** |
* The text alignment of a bar code's human-readable text. |
*/ |
public enum HumanReadableAlignment { |
/** Left-align the human-readable text. */ |
LEFT, |
/** Right-align the human-readable text. */ |
RIGHT, |
/** Center the human-readable text. */ |
CENTER, |
/** Justify the human-readable text by adjusting the spaces between the characters. */ |
JUSTIFY |
} |
/trunk/Modules/Module Label/src/uk/org/okapibarcode/backend/Code49.java |
---|
New file |
0,0 → 1,850 |
/* |
* Copyright 2014 Robin Stuart |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
* in compliance with the License. You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
* or implied. See the License for the specific language governing permissions and limitations under |
* the License. |
*/ |
package uk.org.okapibarcode.backend; |
import static uk.org.okapibarcode.util.Arrays.positionOf; |
import java.awt.geom.Rectangle2D; |
import java.nio.charset.StandardCharsets; |
/** |
* <p> |
* Implements Code 49 according to ANSI/AIM-BC6-2000. |
* |
* <p> |
* Supports full 7-bit ASCII input up to a maximum of 49 characters or 81 numeric digits. GS1 data |