OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * 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;

import java.awt.geom.Rectangle2D;

/**
 * <p>
 * Implements USPS Intelligent Mail Package Barcode (IMpb), a linear barcode based on GS1-128.
 * Includes additional data checks.
 *
 * @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
 * @see <a href=
 *      "https://ribbs.usps.gov/intelligentmail_package/documents/tech_guides/BarcodePackageIMSpec.pdf">IMpb
 *      Specification</a>
 */
public class UspsPackage extends Symbol {

    @Override
    protected void encode() {

        if (!this.content.matches("[0-9\\[\\]]+")) {
            /* Input must be numeric only */
            throw new OkapiException("Invalid IMpb data");
        }

        if (this.content.length() % 2 != 0) {
            /* Input must be even length */
            throw new OkapiException("Invalid IMpb data");
        }

        final Code128 code128 = new Code128();
        code128.unsetCc();
        code128.setDataType(DataType.GS1);
        code128.setContent(this.content);

        final boolean fourTwenty = this.content.length() > 4 && this.content.charAt(1) == '4' && this.content.charAt(2) == '2' && this.content.charAt(3) == '0';

        String hrt = "";
        int bracketCount = 0;
        for (int i = 0; i < this.content.length(); i++) {
            if (this.content.charAt(i) == '[') {
                bracketCount++;
            }
            if (!(fourTwenty && bracketCount < 2)) {
                if (this.content.charAt(i) >= '0' && this.content.charAt(i) <= '9') {
                    hrt += this.content.charAt(i);
                }
            }
        }

        String spacedHrt = "";
        for (int i = 0; i < hrt.length(); i++) {
            spacedHrt += hrt.charAt(i);
            if (i % 4 == 3) {
                spacedHrt += " ";
            }
        }

        this.encodeInfo = code128.encodeInfo;
        this.readable = spacedHrt;
        this.pattern = new String[1];
        this.pattern[0] = code128.pattern[0];
        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;
        boolean black;
        final int offset = 20;
        final int yoffset = 15;
        final String banner = "USPS TRACKING #";

        this.rectangles.clear();
        this.texts.clear();
        y = yoffset;
        h = 0;
        black = true;
        x = 0;
        for (xBlock = 0; xBlock < this.pattern[0].length(); xBlock++) {
            w = this.pattern[0].charAt(xBlock) - '0';
            if (black) {
                if (this.row_height[0] == -1) {
                    h = this.default_height;
                } else {
                    h = this.row_height[0];
                }
                if (w != 0 && h != 0) {
                    final Rectangle2D.Double rect = new Rectangle2D.Double(x + offset, y, w, h);
                    this.rectangles.add(rect);
                }
                this.symbol_width = x + w + 2 * offset;
            }
            black = !black;
            x += w;
        }
        this.symbol_height = h + 2 * yoffset;

        // Add boundary bars
        final Rectangle2D.Double topBar = new Rectangle2D.Double(0, 0, this.symbol_width, 2);
        final Rectangle2D.Double bottomBar = new Rectangle2D.Double(0, this.symbol_height - 2, this.symbol_width, 2);
        this.rectangles.add(topBar);
        this.rectangles.add(bottomBar);

        this.texts.add(new TextBox(0, this.symbol_height - 6.0, this.symbol_width, this.readable, this.humanReadableAlignment));
        this.texts.add(new TextBox(0, 12.0, this.symbol_width, banner, this.humanReadableAlignment));
    }

    /** {@inheritDoc} */
    @Override
    protected int[] getCodewords() {
        return getPatternAsCodewords(6);
    }
}