all files / pdf417/ pdf417-util.js

97.58% Statements 121/124
86.36% Branches 38/44
100% Functions 11/11
97.58% Lines 121/124
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 1751×   1× 1× 1× 63×   1×   288×         1× 63× 63× 63× 63× 63× 63× 63× 63× 63× 63× 63× 63× 59× 59× 59× 59× 59× 1×     63× 63× 63× 63× 63×                     63× 53× 53× 2×     63×   1× 53× 53× 53× 53× 53× 51×   53× 53× 51×   53× 53×   1× 51× 51×     51× 51× 51× 51× 51× 51× 51× 51× 51× 403× 403× 403× 32042× 20679× 20679×   11363× 11363× 26304×   11363×     51× 48×   51×   1× 59× 59× 59× 59× 59× 59× 59× 59× 59× 59× 59×   1× 48× 48× 48× 48× 48×     48×       48×   48× 48× 48× 1× 1×     47×   48×   1× 48× 46×   2× 2× 2× 2× 2× 2× 17× 17× 17× 7×     10×     2× 2× 2×   1× 51×   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
            });
            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;
            var moduleWidth = Math.min(this.symbolWidth / (result.moduleCount + 2 * quiet), this.symbolHeight / (result.rows * ratio + 2 * quiet));
            if (moduleWidth >= 1) {
                moduleWidth = Math.floor(moduleWidth);
            }
            var rowHeight = moduleWidth * ratio;
            if (rowHeight >= 1) {
                rowHeight = Math.floor(rowHeight);
            }
            this.moduleWidth = moduleWidth;
            this.rowHeight = rowHeight;
        };
        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 drawnWidth = result.moduleCount * moduleWidth;
            var drawnHeight = rows * rowHeight;
            var offsetX = this.symbolX + (this.symbolWidth - drawnWidth) / 2;
            var offsetY = this.symbolY + (this.symbolHeight - drawnHeight) / 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);
            Iif (this.displayText.alignment === 'Left') {
                options.x = symbolLeft + textMargin.left * scale;
            }
            else Iif (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;
});