181 |
ilm |
1 |
/*
|
|
|
2 |
* Copyright 2014 Robin Stuart
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
|
5 |
* in compliance with the License. You may obtain a copy of the License at
|
|
|
6 |
*
|
|
|
7 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
*
|
|
|
9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
10 |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
11 |
* or implied. See the License for the specific language governing permissions and limitations under
|
|
|
12 |
* the License.
|
|
|
13 |
*/
|
|
|
14 |
|
|
|
15 |
package uk.org.okapibarcode.backend;
|
|
|
16 |
|
|
|
17 |
import static uk.org.okapibarcode.backend.HumanReadableLocation.NONE;
|
|
|
18 |
import static uk.org.okapibarcode.backend.HumanReadableLocation.TOP;
|
|
|
19 |
|
|
|
20 |
import java.awt.geom.Rectangle2D;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* <p>
|
|
|
24 |
* Implements <a href="http://en.wikipedia.org/wiki/POSTNET">POSTNET</a> and
|
|
|
25 |
* <a href="http://en.wikipedia.org/wiki/Postal_Alpha_Numeric_Encoding_Technique">PLANET</a> bar
|
|
|
26 |
* code symbologies.
|
|
|
27 |
*
|
|
|
28 |
* <p>
|
|
|
29 |
* POSTNET and PLANET both use numerical input data and include a modulo-10 check digit.
|
|
|
30 |
*
|
|
|
31 |
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
|
|
|
32 |
*/
|
|
|
33 |
public class Postnet extends Symbol {
|
|
|
34 |
|
|
|
35 |
public static enum Mode {
|
|
|
36 |
PLANET, POSTNET
|
|
|
37 |
};
|
|
|
38 |
|
|
|
39 |
private static final String[] PN_TABLE = { "LLSSS", "SSSLL", "SSLSL", "SSLLS", "SLSSL", "SLSLS", "SLLSS", "LSSSL", "LSSLS", "LSLSS" };
|
|
|
40 |
|
|
|
41 |
private static final String[] PL_TABLE = { "SSLLL", "LLLSS", "LLSLS", "LLSSL", "LSLLS", "LSLSL", "LSSLL", "SLLLS", "SLLSL", "SLSLL" };
|
|
|
42 |
|
|
|
43 |
private Mode mode;
|
|
|
44 |
|
|
|
45 |
public Postnet() {
|
|
|
46 |
this.mode = Mode.POSTNET;
|
|
|
47 |
this.default_height = 12;
|
|
|
48 |
this.humanReadableLocation = HumanReadableLocation.NONE;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Sets the barcode mode (PLANET or POSTNET). The default mode is POSTNET.
|
|
|
53 |
*
|
|
|
54 |
* @param mode the barcode mode (PLANET or POSTNET)
|
|
|
55 |
*/
|
|
|
56 |
public void setMode(final Mode mode) {
|
|
|
57 |
this.mode = mode;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Returns the barcode mode (PLANET or POSTNET). The default mode is POSTNET.
|
|
|
62 |
*
|
|
|
63 |
* @return the barcode mode (PLANET or POSTNET)
|
|
|
64 |
*/
|
|
|
65 |
public Mode getMode() {
|
|
|
66 |
return this.mode;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
@Override
|
|
|
70 |
protected void encode() {
|
|
|
71 |
final String[] table = this.mode == Mode.POSTNET ? PN_TABLE : PL_TABLE;
|
|
|
72 |
encode(table);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
private void encode(final String[] table) {
|
|
|
76 |
int i, sum, check_digit;
|
|
|
77 |
String dest;
|
|
|
78 |
|
|
|
79 |
if (this.content.length() > 38) {
|
|
|
80 |
throw new OkapiException("Input too long");
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
if (!this.content.matches("[0-9]+")) {
|
|
|
84 |
throw new OkapiException("Invalid characters in data");
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
sum = 0;
|
|
|
88 |
dest = "L";
|
|
|
89 |
|
|
|
90 |
for (i = 0; i < this.content.length(); i++) {
|
|
|
91 |
dest += table[this.content.charAt(i) - '0'];
|
|
|
92 |
sum += this.content.charAt(i) - '0';
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
check_digit = (10 - sum % 10) % 10;
|
|
|
96 |
infoLine("Check Digit: " + check_digit);
|
|
|
97 |
|
|
|
98 |
dest += table[check_digit];
|
|
|
99 |
dest += "L";
|
|
|
100 |
|
|
|
101 |
infoLine("Encoding: " + dest);
|
|
|
102 |
this.readable = this.content;
|
|
|
103 |
this.pattern = new String[] { dest };
|
|
|
104 |
this.row_count = 1;
|
|
|
105 |
this.row_height = new int[] { -1 };
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
@Override
|
|
|
109 |
protected void plotSymbol() {
|
|
|
110 |
int xBlock, shortHeight;
|
|
|
111 |
double x, y, w, h;
|
|
|
112 |
|
|
|
113 |
this.rectangles.clear();
|
|
|
114 |
this.texts.clear();
|
|
|
115 |
|
|
|
116 |
int baseY;
|
|
|
117 |
if (this.humanReadableLocation == TOP) {
|
|
|
118 |
baseY = getTheoreticalHumanReadableHeight();
|
|
|
119 |
} else {
|
|
|
120 |
baseY = 0;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
x = 0;
|
|
|
124 |
w = this.moduleWidth;
|
|
|
125 |
shortHeight = (int) (0.4 * this.default_height);
|
|
|
126 |
for (xBlock = 0; xBlock < this.pattern[0].length(); xBlock++) {
|
|
|
127 |
if (this.pattern[0].charAt(xBlock) == 'L') {
|
|
|
128 |
y = baseY;
|
|
|
129 |
h = this.default_height;
|
|
|
130 |
} else {
|
|
|
131 |
y = baseY + this.default_height - shortHeight;
|
|
|
132 |
h = shortHeight;
|
|
|
133 |
}
|
|
|
134 |
this.rectangles.add(new Rectangle2D.Double(x, y, w, h));
|
|
|
135 |
x += 2.5 * w;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
this.symbol_width = (int) Math.ceil((this.pattern[0].length() - 1) * 2.5 * w + w); // final
|
|
|
139 |
// bar
|
|
|
140 |
// doesn't
|
|
|
141 |
// need
|
|
|
142 |
// extra
|
|
|
143 |
// whitespace
|
|
|
144 |
this.symbol_height = this.default_height;
|
|
|
145 |
|
|
|
146 |
if (this.humanReadableLocation != NONE && !this.readable.isEmpty()) {
|
|
|
147 |
double baseline;
|
|
|
148 |
if (this.humanReadableLocation == TOP) {
|
|
|
149 |
baseline = this.fontSize;
|
|
|
150 |
} else {
|
|
|
151 |
baseline = this.symbol_height + this.fontSize;
|
|
|
152 |
}
|
|
|
153 |
this.texts.add(new TextBox(0, baseline, this.symbol_width, this.readable, this.humanReadableAlignment));
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
}
|