all files / barcode/one-dimension/ gs1-databar.js

97.16% Statements 171/176
91.45% Branches 107/117
100% Functions 20/20
97.09% Lines 167/172
11 statements, 6 functions, 9 branches Ignored     
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256  1× 1× 1× 1× 1× 1×   1× 1× 1× 1×     1×   1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 63×   1× 63× 63× 32×   63× 9×   54× 1×   53× 1×   61× 1×   60× 60× 2×   58× 58×   1× 60× 60× 1×   59× 59× 59× 59×       59×           59× 59× 59× 59× 59× 1×   58×   1× 57× 57×   1× 57× 13× 13×   44× 44× 44× 18×   26× 12×   14× 14× 14× 14× 14× 8×           6× 6×               1× 71× 71× 71× 1353× 1353×   1353×   71×   1× 2706× 2706× 5242×     1× 8× 8× 8× 392× 392×       8× 32×   8× 32×   8×   1× 6× 6× 6× 126×   6× 6× 6× 6× 46× 6× 6×     6×         6× 6× 126×   6× 6×   1× 57× 57×     57× 57× 57× 97× 57×           57× 57× 57× 57× 57× 57×     57× 57×     57× 57× 57×   57× 57× 57× 57× 97× 97× 97× 97× 97× 6639×   6639× 1836×   4803× 1836×       1836×     97×   57× 57× 1836×   57× 31× 31×   31× 1×   31×     1× 5×   1×   1×    
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "../one-dimension", "../rendering/renderer", "../utility/dom-util", "../../gs1/gs1-databar-encoder"], function (require, exports, one_dimension_1, renderer_1, dom_util_1, gs1_databar_encoder_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var OMNIDIRECTIONAL_HEIGHT = 33;
    var TRUNCATED_HEIGHT = 13;
    var STACKED_HEIGHTS = [5, 1, 7];
    var STACKED_OMNIDIRECTIONAL_HEIGHTS = [33, 1, 1, 1, 33];
    var STACKED_QUIET_ZONE = 10;
    var LIMITED_HEIGHT = 10;
    var FINDER_PATTERN_3 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1];
    var FINDER_SEPARATOR = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0];
    var GS1DataBar = (function (_super) {
        __extends(GS1DataBar, _super);
        function GS1DataBar() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        GS1DataBar.prototype.validateInput = function (char) {
            var gtin = char;
            if (char.length === 18 && char.substring(0, 4) === '(01)') {
                gtin = char.substring(4);
            }
            if (gtin.search(/^[0-9]{13}$/) !== -1) {
                gtin = gtin + gs1_databar_encoder_1.calculateCheckDigit(gtin);
            }
            else if (gtin.search(/^[0-9]{14}$/) === -1) {
                return this.message('DataBarInvalidGtin');
            }
            else if (Number(gtin.charAt(13)) !== gs1_databar_encoder_1.calculateCheckDigit(gtin.substring(0, 13))) {
                return this.message('GS1InvalidCheckDigit');
            }
            if (this.databarVariant === 'Limited' && gtin.charAt(0) !== '0' && gtin.charAt(0) !== '1') {
                return this.message('DataBarLimitedInvalidGtin');
            }
            var sizeError = this.validateSize(gtin);
            if (sizeError) {
                return sizeError;
            }
            this.gtin = gtin;
            return undefined;
        };
        GS1DataBar.prototype.validateSize = function (gtin) {
            var barcodeSize = this.getDrawableSize(this.margin, this.width, this.height);
            if (barcodeSize.width <= 0 || barcodeSize.height <= 0) {
                return this.message('InsufficientSize');
            }
            var stacked = this.databarVariant === 'Stacked' || this.databarVariant === 'StackedOmnidirectional';
            var quietZoneModules = stacked ? STACKED_QUIET_ZONE : 0;
            var moduleCount = this.databarVariant === 'Limited' ? 80 : (stacked ? 50 : 96);
            var totalModules = stacked ?
                (this.databarVariant === 'Stacked' ? 13 : 69) :
                (this.databarVariant === 'Limited' ? 10 :
                    (this.databarVariant === 'Truncated' ? 13 : 33));
            var textOptions = {
                width: undefined, height: undefined, x: 0, y: 0, color: this.foreColor,
                string: this.displayText.text ? this.displayText.text : '(01)' + gtin,
                stringSize: this.displayText.size, visibility: this.displayText.visibility,
                fontStyle: this.displayText.font
            };
            dom_util_1.createMeasureElements();
            var textSize = dom_util_1.measureText(textOptions);
            var scaleValue = this.isSvgMode ? 1 : 1.5;
            var barHeight = barcodeSize.height - (textSize.height / 2 + 2) * scaleValue;
            if (barcodeSize.width / (moduleCount + quietZoneModules * 2) < 1 || barHeight / totalModules < 1) {
                return this.message('InsufficientSize');
            }
            return undefined;
        };
        GS1DataBar.prototype.draw = function (canvas) {
            var rows = this.buildRows(this.gtin);
            this.drawRows(canvas, rows);
        };
        GS1DataBar.prototype.buildRows = function (gtin) {
            if (this.databarVariant === 'Limited') {
                var encoded_1 = gs1_databar_encoder_1.encodeGtinDataBarLimited(gtin);
                return [{ pixels: this.expandWidths(encoded_1.widths, false), height: LIMITED_HEIGHT }];
            }
            var encoded = gs1_databar_encoder_1.encodeGtinDataBar(gtin);
            var widths = encoded.widths;
            if (this.databarVariant === 'Omnidirectional') {
                return [{ pixels: this.expandWidths(widths, false), height: OMNIDIRECTIONAL_HEIGHT }];
            }
            if (this.databarVariant === 'Truncated') {
                return [{ pixels: this.expandWidths(widths, false), height: TRUNCATED_HEIGHT }];
            }
            var topWidths = [1, 1].concat(widths.slice(2, 23), [1, 1, 0]);
            var bottomWidths = [1, 1].concat(widths.slice(23, 44), [1, 1, 0]);
            var top = this.expandWidths(topWidths, false);
            var bottom = this.expandWidths(bottomWidths, true);
            if (this.databarVariant === 'Stacked') {
                return [
                    { pixels: top, height: STACKED_HEIGHTS[0] },
                    { pixels: this.stackedSeparator(top, bottom), height: STACKED_HEIGHTS[1] },
                    { pixels: bottom, height: STACKED_HEIGHTS[2] }
                ];
            }
            var separators = this.stackedOmnidirectionalSeparators(bottom);
            return [
                { pixels: top, height: STACKED_OMNIDIRECTIONAL_HEIGHTS[0] },
                { pixels: separators[0], height: STACKED_OMNIDIRECTIONAL_HEIGHTS[1] },
                { pixels: separators[1], height: STACKED_OMNIDIRECTIONAL_HEIGHTS[2] },
                { pixels: separators[2], height: STACKED_OMNIDIRECTIONAL_HEIGHTS[3] },
                { pixels: bottom, height: STACKED_OMNIDIRECTIONAL_HEIGHTS[4] }
            ];
        };
        GS1DataBar.prototype.expandWidths = function (widths, barFirst) {
            var pixels = [];
            var index;
            for (index = 0; index < widths.length; index += 2) {
                this.appendRun(pixels, widths[parseInt(index.toString(), 10)], barFirst ? 1 : 0);
                var spaceWidth = index + 1 < widths.length ?
                    widths[parseInt((index + 1).toString(), 10)] : 0;
                this.appendRun(pixels, spaceWidth, barFirst ? 0 : 1);
            }
            return pixels;
        };
        GS1DataBar.prototype.appendRun = function (pixels, count, value) {
            var index;
            for (index = 0; index < count; index++) {
                pixels.push(value);
            }
        };
        GS1DataBar.prototype.stackedSeparator = function (top, bottom) {
            var separator = [0];
            var index;
            for (index = 1; index < 50; index++) {
                var current = index;
                separator[parseInt(current.toString(), 10)] =
                    top[parseInt(current.toString(), 10)] === bottom[parseInt(current.toString(), 10)] ?
                        1 - top[parseInt(current.toString(), 10)] : 1 - separator[parseInt((current - 1).toString(), 10)];
            }
            for (index = 0; index < 4; index++) {
                separator[parseInt(index.toString(), 10)] = 0;
            }
            for (index = 46; index < 50; index++) {
                separator[parseInt(index.toString(), 10)] = 0;
            }
            return separator;
        };
        GS1DataBar.prototype.stackedOmnidirectionalSeparators = function (bottom) {
            var first = [0, 0, 0, 0];
            var index;
            for (index = 0; index < 21; index++) {
                first.push(1, 0);
            }
            first.push(0, 0, 0, 0);
            var third = first.slice();
            var isFinderThree = true;
            for (index = 0; index <= 12; index++) {
                if (bottom[parseInt((19 + index).toString(), 10)] !== FINDER_PATTERN_3[parseInt(index.toString(), 10)]) {
                    isFinderThree = false;
                    break;
                }
            }
            Iif (isFinderThree) {
                for (index = 0; index <= 12; index++) {
                    third[parseInt((19 + index).toString(), 10)] = FINDER_SEPARATOR[parseInt(index.toString(), 10)];
                }
            }
            var second = [0, 0, 0, 0];
            for (index = 0; index < 21; index++) {
                second.push(0, 1);
            }
            second.push(0, 0, 0, 0);
            return [first, second, third];
        };
        GS1DataBar.prototype.drawRows = function (canvas, rows) {
            var barcodeSize = this.getDrawableSize(this.margin, this.width, this.height);
            Iif (barcodeSize.height <= 0 || barcodeSize.width <= 0) {
                return;
            }
            var stacked = this.databarVariant === 'Stacked' || this.databarVariant === 'StackedOmnidirectional';
            var quietZoneModules = stacked ? STACKED_QUIET_ZONE : 0;
            var moduleCount = rows[0].pixels.length + quietZoneModules * 2;
            var totalHeight = rows.reduce(function (sum, row) { return sum + row.height; }, 0);
            var textOptions = {
                width: undefined, height: undefined, x: 0, y: 0, color: this.foreColor,
                string: this.displayText.text ? this.displayText.text : '(01)' + this.gtin,
                stringSize: this.displayText.size, visibility: this.displayText.visibility,
                fontStyle: this.displayText.font
            };
            dom_util_1.createMeasureElements();
            var textSize = dom_util_1.measureText(textOptions);
            var textHeight = (textSize.height / 2) + 2;
            var scaleValue = this.isSvgMode ? 1 : 1.5;
            var barHeight = barcodeSize.height - textHeight * scaleValue;
            Iif (barHeight <= 0) {
                return;
            }
            var moduleWidth = barcodeSize.width / moduleCount;
            Iif (moduleWidth < 1) {
                return;
            }
            var rowHeightScale = barHeight / totalHeight;
            var position = this.displayText.position;
            var barTop = position === 'Top' ?
                barcodeSize.y + textHeight + this.displayText.margin.top : barcodeSize.y;
            var options = [];
            var offsetY = barTop;
            var index;
            for (index = 0; index < rows.length; index++) {
                var row = rows[parseInt(index.toString(), 10)];
                var height = row.height * rowHeightScale;
                var moduleIndex = void 0;
                var runStart = -1;
                for (moduleIndex = 0; moduleIndex <= row.pixels.length; moduleIndex++) {
                    var isBar = moduleIndex < row.pixels.length &&
                        row.pixels[parseInt(moduleIndex.toString(), 10)] === 1;
                    if (isBar && runStart === -1) {
                        runStart = moduleIndex;
                    }
                    else if (!isBar && runStart !== -1) {
                        options.push({
                            x: barcodeSize.x + (quietZoneModules + runStart) * moduleWidth, y: offsetY,
                            width: (moduleIndex - runStart) * moduleWidth, height: height, color: this.foreColor
                        });
                        runStart = -1;
                    }
                }
                offsetY += height;
            }
            var renderer = new renderer_1.BarcodeRenderer(canvas.id, this.isSvgMode);
            for (index = 0; index < options.length; index++) {
                renderer.renderRectElement(canvas, options[parseInt(index.toString(), 10)]);
            }
            if (this.displayText.visibility) {
                textOptions.x = barcodeSize.x + (barcodeSize.width - textSize.width) / 2;
                textOptions.y = position === 'Bottom' ?
                    (barcodeSize.y + barcodeSize.height) + 2 : (barcodeSize.y + textHeight) - 2;
                if (!this.isSvgMode) {
                    textOptions.y /= 1.5;
                }
                renderer.renderTextElement(canvas, textOptions);
            }
        };
        GS1DataBar.prototype.message = function (key) {
            return this.locale ? (this.locale.getConstant(key) || key) : key;
        };
        return GS1DataBar;
    }(one_dimension_1.OneDimension));
    exports.GS1DataBar = GS1DataBar;
});