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
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
"use strict";
 
Object.defineProperty(exports, "__esModule", {
    value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _merge = require("../help/merge.js");
 
var _merge2 = _interopRequireDefault(_merge);
 
var _shared = require("./shared.js");
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
var CanvasRenderer = function () {
    function CanvasRenderer(canvas, encodings, options) {
        _classCallCheck(this, CanvasRenderer);
 
        this.canvas = canvas;
        this.encodings = encodings;
        this.options = options;
    }
 
    _createClass(CanvasRenderer, [{
        key: "render",
        value: function render() {
            // Abort if the browser does not support HTML5 canvas
            if (!this.canvas.getContext) {
                throw new Error('The browser does not support canvas.');
            }
 
            this.prepareCanvas();
            for (var i = 0; i < this.encodings.length; i++) {
                var encodingOptions = (0, _merge2.default)(this.options, this.encodings[i].options);
 
                this.drawCanvasBarcode(encodingOptions, this.encodings[i]);
                this.drawCanvasText(encodingOptions, this.encodings[i]);
 
                this.moveCanvasDrawing(this.encodings[i]);
            }
 
            this.restoreCanvas();
        }
    }, {
        key: "prepareCanvas",
        value: function prepareCanvas() {
            // Get the canvas context
            var ctx = this.canvas.getContext("2d");
 
            ctx.save();
 
            (0, _shared.calculateEncodingAttributes)(this.encodings, this.options, ctx);
            var totalWidth = (0, _shared.getTotalWidthOfEncodings)(this.encodings);
            var maxHeight = (0, _shared.getMaximumHeightOfEncodings)(this.encodings);
 
            this.canvas.width = totalWidth + this.options.marginLeft + this.options.marginRight;
 
            this.canvas.height = maxHeight;
 
            // Paint the canvas
            ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
            if (this.options.background) {
                ctx.fillStyle = this.options.background;
                ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
            }
 
            ctx.translate(this.options.marginLeft, 0);
        }
    }, {
        key: "drawCanvasBarcode",
        value: function drawCanvasBarcode(options, encoding) {
            // Get the canvas context
            var ctx = this.canvas.getContext("2d");
 
            var binary = encoding.data;
 
            // Creates the barcode out of the encoded binary
            var yFrom;
            if (options.textPosition == "top") {
                yFrom = options.marginTop + options.fontSize + options.textMargin;
            } else {
                yFrom = options.marginTop;
            }
 
            ctx.fillStyle = options.lineColor;
 
            for (var b = 0; b < binary.length; b++) {
                var x = b * options.width + encoding.barcodePadding;
 
                if (binary[b] === "1") {
                    ctx.fillRect(x, yFrom, options.width, options.height);
                } else if (binary[b]) {
                    ctx.fillRect(x, yFrom, options.width, options.height * binary[b]);
                }
            }
        }
    }, {
        key: "drawCanvasText",
        value: function drawCanvasText(options, encoding) {
            // Get the canvas context
            var ctx = this.canvas.getContext("2d");
 
            var font = options.fontOptions + " " + options.fontSize + "px " + options.font;
 
            // Draw the text if displayValue is set
            if (options.displayValue) {
                var x, y;
 
                if (options.textPosition == "top") {
                    y = options.marginTop + options.fontSize - options.textMargin;
                } else {
                    y = options.height + options.textMargin + options.marginTop + options.fontSize;
                }
 
                ctx.font = font;
 
                // Draw the text in the correct X depending on the textAlign option
                if (options.textAlign == "left" || encoding.barcodePadding > 0) {
                    x = 0;
                    ctx.textAlign = 'left';
                } else if (options.textAlign == "right") {
                    x = encoding.width - 1;
                    ctx.textAlign = 'right';
                }
                // In all other cases, center the text
                else {
                        x = encoding.width / 2;
                        ctx.textAlign = 'center';
                    }
 
                ctx.fillText(encoding.text, x, y);
            }
        }
    }, {
        key: "moveCanvasDrawing",
        value: function moveCanvasDrawing(encoding) {
            var ctx = this.canvas.getContext("2d");
 
            ctx.translate(encoding.width, 0);
        }
    }, {
        key: "restoreCanvas",
        value: function restoreCanvas() {
            // Get the canvas context
            var ctx = this.canvas.getContext("2d");
 
            ctx.restore();
        }
    }]);
 
    return CanvasRenderer;
}();
 
exports.default = CanvasRenderer;