OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
181 ilm 1
/*
2
 * Copyright 2015 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
package uk.org.okapibarcode.backend;
15
 
16
import java.awt.geom.Rectangle2D;
17
 
18
/**
19
 * <p>
20
 * Implements USPS Intelligent Mail Package Barcode (IMpb), a linear barcode based on GS1-128.
21
 * Includes additional data checks.
22
 *
23
 * @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
24
 * @see <a href=
25
 *      "https://ribbs.usps.gov/intelligentmail_package/documents/tech_guides/BarcodePackageIMSpec.pdf">IMpb
26
 *      Specification</a>
27
 */
28
public class UspsPackage extends Symbol {
29
 
30
    @Override
31
    protected void encode() {
32
 
33
        if (!this.content.matches("[0-9\\[\\]]+")) {
34
            /* Input must be numeric only */
35
            throw new OkapiException("Invalid IMpb data");
36
        }
37
 
38
        if (this.content.length() % 2 != 0) {
39
            /* Input must be even length */
40
            throw new OkapiException("Invalid IMpb data");
41
        }
42
 
43
        final Code128 code128 = new Code128();
44
        code128.unsetCc();
45
        code128.setDataType(DataType.GS1);
46
        code128.setContent(this.content);
47
 
48
        final boolean fourTwenty = this.content.length() > 4 && this.content.charAt(1) == '4' && this.content.charAt(2) == '2' && this.content.charAt(3) == '0';
49
 
50
        String hrt = "";
51
        int bracketCount = 0;
52
        for (int i = 0; i < this.content.length(); i++) {
53
            if (this.content.charAt(i) == '[') {
54
                bracketCount++;
55
            }
56
            if (!(fourTwenty && bracketCount < 2)) {
57
                if (this.content.charAt(i) >= '0' && this.content.charAt(i) <= '9') {
58
                    hrt += this.content.charAt(i);
59
                }
60
            }
61
        }
62
 
63
        String spacedHrt = "";
64
        for (int i = 0; i < hrt.length(); i++) {
65
            spacedHrt += hrt.charAt(i);
66
            if (i % 4 == 3) {
67
                spacedHrt += " ";
68
            }
69
        }
70
 
71
        this.encodeInfo = code128.encodeInfo;
72
        this.readable = spacedHrt;
73
        this.pattern = new String[1];
74
        this.pattern[0] = code128.pattern[0];
75
        this.row_count = 1;
76
        this.row_height = new int[1];
77
        this.row_height[0] = -1;
78
    }
79
 
80
    @Override
81
    protected void plotSymbol() {
82
        int xBlock;
83
        int x, y, w, h;
84
        boolean black;
85
        final int offset = 20;
86
        final int yoffset = 15;
87
        final String banner = "USPS TRACKING #";
88
 
89
        this.rectangles.clear();
90
        this.texts.clear();
91
        y = yoffset;
92
        h = 0;
93
        black = true;
94
        x = 0;
95
        for (xBlock = 0; xBlock < this.pattern[0].length(); xBlock++) {
96
            w = this.pattern[0].charAt(xBlock) - '0';
97
            if (black) {
98
                if (this.row_height[0] == -1) {
99
                    h = this.default_height;
100
                } else {
101
                    h = this.row_height[0];
102
                }
103
                if (w != 0 && h != 0) {
104
                    final Rectangle2D.Double rect = new Rectangle2D.Double(x + offset, y, w, h);
105
                    this.rectangles.add(rect);
106
                }
107
                this.symbol_width = x + w + 2 * offset;
108
            }
109
            black = !black;
110
            x += w;
111
        }
112
        this.symbol_height = h + 2 * yoffset;
113
 
114
        // Add boundary bars
115
        final Rectangle2D.Double topBar = new Rectangle2D.Double(0, 0, this.symbol_width, 2);
116
        final Rectangle2D.Double bottomBar = new Rectangle2D.Double(0, this.symbol_height - 2, this.symbol_width, 2);
117
        this.rectangles.add(topBar);
118
        this.rectangles.add(bottomBar);
119
 
120
        this.texts.add(new TextBox(0, this.symbol_height - 6.0, this.symbol_width, this.readable, this.humanReadableAlignment));
121
        this.texts.add(new TextBox(0, 12.0, this.symbol_width, banner, this.humanReadableAlignment));
122
    }
123
 
124
    /** {@inheritDoc} */
125
    @Override
126
    protected int[] getCodewords() {
127
        return getPatternAsCodewords(6);
128
    }
129
}