all files / pdf417/ pdf417-util.js

99.17% Statements 119/120
88.1% Branches 37/42
100% Functions 11/11
99.17% Lines 119/120
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 1721×   1× 1× 1× 80×   1×   361×         1× 80× 80× 80× 80× 80× 80× 80× 80× 80× 80× 80× 80× 76× 76× 76× 76× 76× 2×     80× 80× 80× 80× 80×                       80× 69× 69× 13×     80×   1× 69× 69× 69× 69× 69×   1× 56× 56×     56× 56× 56× 56× 56× 56× 56× 56×   56×   56× 56× 458× 458× 458× 35721× 23039× 23039×   12682× 12682× 29371×   12682×     56× 53×   56×   1× 76× 76× 76× 76× 76× 76× 76× 76× 76× 76× 76×   1× 53× 53× 53× 53× 53× 1×   52× 1×     51×   53× 53× 53× 2× 2×     51×   53×   1× 53× 50×   3× 3× 3× 3× 3× 3× 23× 23× 23× 9×     14×     3× 3× 3×   1× 56×   1×   1×    
define(["require", "exports", "../barcode/rendering/renderer", "../barcode/utility/barcode-util", "../barcode/utility/dom-util", "./pdf417-encoder"], function (require, exports, renderer_1, barcode_util_1, dom_util_1, pdf417_encoder_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var PDF417 = (function () {
        function PDF417() {
            this.textHeight = 0;
        }
        Object.defineProperty(PDF417.prototype, "scale", {
            get: function () {
                return this.isSvgMode ? 1 : 1.5;
            },
            enumerable: true,
            configurable: true
        });
        PDF417.prototype.build = function () {
            var scale = this.scale;
            var left = this.margin.left * scale;
            var right = this.margin.right * scale;
            var top = this.margin.top * scale;
            var bottom = this.margin.bottom * scale;
            var x = left;
            var y = top;
            var width = Math.max(0, this.width - left - right);
            var height = Math.max(0, this.height - top - bottom);
            this.textHeight = 0;
            this.textOptions = undefined;
            if (this.displayText.visibility) {
                this.textOptions = this.measureDisplayText();
                var textMargin = this.displayText.margin;
                this.textHeight = this.textOptions.height + (textMargin.top + textMargin.bottom) * scale;
                height = Math.max(0, height - this.textHeight);
                if (this.displayText.position === 'Top') {
                    y += this.textHeight;
                }
            }
            this.symbolX = x;
            this.symbolY = y;
            this.symbolWidth = width;
            this.symbolHeight = height;
            this.result = pdf417_encoder_1.encodePDF417({
                value: this.value,
                compactionMode: this.compactionMode,
                errorCorrectionLevel: this.errorCorrectionLevel,
                rows: this.rows,
                columns: this.columns,
                isCompact: this.isCompact,
                rowHeightRatio: this.rowHeightRatio,
                availableWidth: width,
                availableHeight: height,
                previousLayout: this.previousLayout
            });
            if (this.result.success) {
                this.calculateModuleSize();
                if (!(this.moduleWidth >= 1 && this.rowHeight >= 1)) {
                    this.result = { success: false, error: 'InsufficientSize' };
                }
            }
            return this.result;
        };
        PDF417.prototype.calculateModuleSize = function () {
            var result = this.result;
            var quiet = pdf417_encoder_1.PDF417_QUIET_ZONE;
            var ratio = this.rowHeightRatio || pdf417_encoder_1.PDF417_MIN_ROW_HEIGHT;
            this.moduleWidth = Math.min(this.symbolWidth / (result.moduleCount + 2 * quiet), this.symbolHeight / (result.rows * ratio + 2 * quiet));
            this.rowHeight = this.moduleWidth * ratio;
        };
        PDF417.prototype.draw = function (canvas) {
            var result = this.result;
            Iif (!result || !result.success) {
                return;
            }
            var rows = result.rows;
            var moduleWidth = this.moduleWidth;
            var rowHeight = this.rowHeight;
            var ratio = this.rowHeightRatio || pdf417_encoder_1.PDF417_MIN_ROW_HEIGHT;
            var quiet = pdf417_encoder_1.PDF417_QUIET_ZONE;
            var drawnWidth = result.moduleCount * moduleWidth;
            var drawnHeight = rows * rowHeight;
            var offsetX = this.symbolX + quiet * moduleWidth +
                (this.symbolWidth - (result.moduleCount + 2 * quiet) * moduleWidth) / 2;
            var offsetY = this.symbolY + quiet * rowHeight +
                (this.symbolHeight - (rows * ratio + 2 * quiet) * moduleWidth) / 2;
            var renderer = this.getInstance(canvas.id);
            for (var r = 0; r < rows; r++) {
                var row = result.matrix[parseInt(r.toString(), 10)];
                var c = 0;
                while (c < row.length) {
                    if (row[parseInt(c.toString(), 10)] !== 1) {
                        c++;
                        continue;
                    }
                    var start = c;
                    while (c < row.length && row[parseInt(c.toString(), 10)] === 1) {
                        c++;
                    }
                    renderer.renderRectElement(canvas, barcode_util_1.getBaseAttributes((c - start) * moduleWidth, rowHeight, offsetX + start * moduleWidth, offsetY + r * rowHeight, this.foreColor));
                }
            }
            if (this.textOptions) {
                this.drawDisplayText(canvas, renderer, offsetX, drawnWidth, offsetY, drawnHeight);
            }
            this.result.matrix = undefined;
        };
        PDF417.prototype.measureDisplayText = function () {
            dom_util_1.createMeasureElements();
            var textOptions = barcode_util_1.getBaseAttributes(0, 0, 0, 0, this.foreColor);
            textOptions.string = this.displayText.text ? this.displayText.text : this.value;
            textOptions.fontStyle = this.displayText.font;
            textOptions.stringSize = this.displayText.size;
            textOptions.visibility = this.displayText.visibility;
            var size = dom_util_1.measureText(textOptions);
            textOptions.width = size.width * this.scale;
            textOptions.height = size.height * this.scale;
            textOptions.stringSize = this.displayText.size * this.scale;
            return textOptions;
        };
        PDF417.prototype.drawDisplayText = function (canvas, renderer, symbolLeft, symbolWidth, symbolTop, symbolHeight) {
            var scale = this.scale;
            var options = this.textOptions;
            var textMargin = this.displayText.margin;
            this.fitDisplayText(options, this.symbolWidth - (textMargin.left + textMargin.right) * scale);
            if (this.displayText.alignment === 'Left') {
                options.x = symbolLeft + textMargin.left * scale;
            }
            else if (this.displayText.alignment === 'Right') {
                options.x = symbolLeft + symbolWidth - options.width - textMargin.right * scale;
            }
            else {
                options.x = symbolLeft + (symbolWidth - options.width) / 2;
            }
            options.x = Math.max(this.margin.left * scale, options.x);
            var baselineOffset = options.height * 0.8;
            if (this.displayText.position === 'Top') {
                options.y = symbolTop - (textMargin.bottom * scale) - options.height + baselineOffset;
                options.y = Math.max(options.y, this.margin.top * scale + textMargin.top * scale + baselineOffset);
            }
            else {
                options.y = symbolTop + symbolHeight + textMargin.top * scale + baselineOffset;
            }
            renderer.renderTextElement(canvas, options);
        };
        PDF417.prototype.fitDisplayText = function (options, maxWidth) {
            if (options.width <= maxWidth) {
                return;
            }
            var ellipsis = '\u2026';
            var full = options.string;
            var low = 0;
            var high = full.length;
            var width = 0;
            while (low < high) {
                var mid = Math.ceil((low + high) / 2);
                options.string = full.substring(0, mid) + ellipsis;
                if (dom_util_1.measureText(options).width <= maxWidth) {
                    low = mid;
                }
                else {
                    high = mid - 1;
                }
            }
            options.string = low > 0 ? full.substring(0, low).replace(/\s+$/, '') + ellipsis : '';
            width = options.string ? dom_util_1.measureText(options).width : 0;
            options.width = width;
        };
        PDF417.prototype.getInstance = function (id) {
            return new renderer_1.BarcodeRenderer(id, this.isSvgMode);
        };
        return PDF417;
    }());
    exports.PDF417 = PDF417;
});