1
hao
2025-03-27 e610e1c17f62b423a717fadaaa7b139d02857793
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
"use strict";
 
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.getTotalWidthOfEncodings = exports.calculateEncodingAttributes = exports.getBarcodePadding = exports.getEncodingHeight = exports.getMaximumHeightOfEncodings = undefined;
 
var _merge = require("../help/merge.js");
 
var _merge2 = _interopRequireDefault(_merge);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function getEncodingHeight(encoding, options) {
    return options.height + (options.displayValue && encoding.text.length > 0 ? options.fontSize + options.textMargin : 0) + options.marginTop + options.marginBottom;
}
 
function getBarcodePadding(textWidth, barcodeWidth, options) {
    if (options.displayValue && barcodeWidth < textWidth) {
        if (options.textAlign == "center") {
            return Math.floor((textWidth - barcodeWidth) / 2);
        } else if (options.textAlign == "left") {
            return 0;
        } else if (options.textAlign == "right") {
            return Math.floor(textWidth - barcodeWidth);
        }
    }
    return 0;
}
 
function calculateEncodingAttributes(encodings, barcodeOptions, context) {
    for (var i = 0; i < encodings.length; i++) {
        var encoding = encodings[i];
        var options = (0, _merge2.default)(barcodeOptions, encoding.options);
 
        // Calculate the width of the encoding
        var textWidth;
        if (options.displayValue) {
            textWidth = messureText(encoding.text, options, context);
        } else {
            textWidth = 0;
        }
 
        var barcodeWidth = encoding.data.length * options.width;
        encoding.width = Math.ceil(Math.max(textWidth, barcodeWidth));
 
        encoding.height = getEncodingHeight(encoding, options);
 
        encoding.barcodePadding = getBarcodePadding(textWidth, barcodeWidth, options);
    }
}
 
function getTotalWidthOfEncodings(encodings) {
    var totalWidth = 0;
    for (var i = 0; i < encodings.length; i++) {
        totalWidth += encodings[i].width;
    }
    return totalWidth;
}
 
function getMaximumHeightOfEncodings(encodings) {
    var maxHeight = 0;
    for (var i = 0; i < encodings.length; i++) {
        if (encodings[i].height > maxHeight) {
            maxHeight = encodings[i].height;
        }
    }
    return maxHeight;
}
 
function messureText(string, options, context) {
    var ctx;
 
    if (context) {
        ctx = context;
    } else if (typeof document !== "undefined") {
        ctx = document.createElement("canvas").getContext("2d");
    } else {
        // If the text cannot be messured we will return 0.
        // This will make some barcode with big text render incorrectly
        return 0;
    }
    ctx.font = options.fontOptions + " " + options.fontSize + "px " + options.font;
 
    // Calculate the width of the encoding
    var measureTextResult = ctx.measureText(string);
    if (!measureTextResult) {
        // Some implementations don't implement measureText and return undefined.
        // If the text cannot be measured we will return 0.
        // This will make some barcode with big text render incorrectly
        return 0;
    }
    var size = measureTextResult.width;
    return size;
}
 
exports.getMaximumHeightOfEncodings = getMaximumHeightOfEncodings;
exports.getEncodingHeight = getEncodingHeight;
exports.getBarcodePadding = getBarcodePadding;
exports.calculateEncodingAttributes = calculateEncodingAttributes;
exports.getTotalWidthOfEncodings = getTotalWidthOfEncodings;