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

/**
 * <p>
 * Implements <a href="http://en.wikipedia.org/wiki/POSTNET">POSTNET</a> and
 * <a href="http://en.wikipedia.org/wiki/Postal_Alpha_Numeric_Encoding_Technique">PLANET</a> bar
 * code symbologies.
 *
 * <p>
 * POSTNET and PLANET both use numerical input data and include a modulo-10 check digit.
 *
 * @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
 */
public class Postnet extends Symbol {

    public static enum Mode {
        PLANET, POSTNET
    };

    private static final String[] PN_TABLE = { "LLSSS", "SSSLL", "SSLSL", "SSLLS", "SLSSL", "SLSLS", "SLLSS", "LSSSL", "LSSLS", "LSLSS" };

    private static final String[] PL_TABLE = { "SSLLL", "LLLSS", "LLSLS", "LLSSL", "LSLLS", "LSLSL", "LSSLL", "SLLLS", "SLLSL", "SLSLL" };

    private Mode mode;

    public Postnet() {
        this.mode = Mode.POSTNET;
        this.default_height = 12;
        this.humanReadableLocation = HumanReadableLocation.NONE;
    }

    /**
     * Sets the barcode mode (PLANET or POSTNET). The default mode is POSTNET.
     *
     * @param mode the barcode mode (PLANET or POSTNET)
     */
    public void setMode(final Mode mode) {
        this.mode = mode;
    }

    /**
     * Returns the barcode mode (PLANET or POSTNET). The default mode is POSTNET.
     *
     * @return the barcode mode (PLANET or POSTNET)
     */
    public Mode getMode() {
        return this.mode;
    }

    @Override
    protected void encode() {
        final String[] table = this.mode == Mode.POSTNET ? PN_TABLE : PL_TABLE;
        encode(table);
    }

    private void encode(final String[] table) {
        int i, sum, check_digit;
        String dest;

        if (this.content.length() > 38) {
            throw new OkapiException("Input too long");
        }

        if (!this.content.matches("[0-9]+")) {
            throw new OkapiException("Invalid characters in data");
        }

        sum = 0;
        dest = "L";

        for (i = 0; i < this.content.length(); i++) {
            dest += table[this.content.charAt(i) - '0'];
            sum += this.content.charAt(i) - '0';
        }

        check_digit = (10 - sum % 10) % 10;
        infoLine("Check Digit: " + check_digit);

        dest += table[check_digit];
        dest += "L";

        infoLine("Encoding: " + dest);
        this.readable = this.content;
        this.pattern = new String[] { dest };
        this.row_count = 1;
        this.row_height = new int[] { -1 };
    }

    @Override
    protected void plotSymbol() {
        int xBlock, shortHeight;
        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;
        shortHeight = (int) (0.4 * this.default_height);
        for (xBlock = 0; xBlock < this.pattern[0].length(); xBlock++) {
            if (this.pattern[0].charAt(xBlock) == 'L') {
                y = baseY;
                h = this.default_height;
            } else {
                y = baseY + this.default_height - shortHeight;
                h = shortHeight;
            }
            this.rectangles.add(new Rectangle2D.Double(x, y, w, h));
            x += 2.5 * w;
        }

        this.symbol_width = (int) Math.ceil((this.pattern[0].length() - 1) * 2.5 * 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));
        }
    }
}