1
hao
2025-05-19 8ef9c48b03ec22db9a56746f898993cb41eb4753
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
import merge from "../help/merge.js";
import {calculateEncodingAttributes, getTotalWidthOfEncodings, getMaximumHeightOfEncodings} from "./shared.js";
 
var svgns = "http://www.w3.org/2000/svg";
 
class SVGRenderer{
    constructor(svg, encodings, options){
        this.svg = svg;
        this.encodings = encodings;
        this.options = options;
        this.document = options.xmlDocument || document;
    }
 
    render(){
        var currentX = this.options.marginLeft;
 
        this.prepareSVG();
        for(let i = 0; i < this.encodings.length; i++){
            var encoding = this.encodings[i];
            var encodingOptions = merge(this.options, encoding.options);
 
            var group = this.createGroup(currentX, encodingOptions.marginTop, this.svg);
 
            this.setGroupOptions(group, encodingOptions);
 
            this.drawSvgBarcode(group, encodingOptions, encoding);
            this.drawSVGText(group, encodingOptions, encoding);
 
            currentX += encoding.width;
        }
    }
 
    prepareSVG(){
        // Clear the SVG
        while (this.svg.firstChild) {
            this.svg.removeChild(this.svg.firstChild);
        }
 
        calculateEncodingAttributes(this.encodings, this.options);
        var totalWidth = getTotalWidthOfEncodings(this.encodings);
        var maxHeight = getMaximumHeightOfEncodings(this.encodings);
 
        var width = totalWidth + this.options.marginLeft + this.options.marginRight;
        this.setSvgAttributes(width, maxHeight);
 
        if(this.options.background){
            this.drawRect(0, 0, width, maxHeight, this.svg).setAttribute(
                "style", "fill:" + this.options.background + ";"
            );
        }
    }
 
    drawSvgBarcode(parent, options, encoding){
        var binary = encoding.data;
 
        // Creates the barcode out of the encoded binary
        var yFrom;
        if(options.textPosition == "top"){
            yFrom = options.fontSize + options.textMargin;
        }
        else{
            yFrom = 0;
        }
 
        var barWidth = 0;
        var x = 0;
        for(var b = 0; b < binary.length; b++){
            x = b * options.width + encoding.barcodePadding;
 
            if(binary[b] === "1"){
                barWidth++;
            }
            else if(barWidth > 0){
                this.drawRect(x - options.width * barWidth, yFrom, options.width * barWidth, options.height, parent);
                barWidth = 0;
            }
        }
 
        // Last draw is needed since the barcode ends with 1
        if(barWidth > 0){
            this.drawRect(x - options.width * (barWidth - 1), yFrom, options.width * barWidth, options.height, parent);
        }
    }
 
    drawSVGText(parent, options, encoding){
        var textElem = this.document.createElementNS(svgns, 'text');
 
        // Draw the text if displayValue is set
        if(options.displayValue){
            var x, y;
 
            textElem.setAttribute("style",
                "font:" + options.fontOptions + " " + options.fontSize + "px " + options.font
            );
 
            if(options.textPosition == "top"){
                y = options.fontSize - options.textMargin;
            }
            else{
                y = options.height + options.textMargin + options.fontSize;
            }
 
            // Draw the text in the correct X depending on the textAlign option
            if(options.textAlign == "left" || encoding.barcodePadding > 0){
                x = 0;
                textElem.setAttribute("text-anchor", "start");
            }
            else if(options.textAlign == "right"){
                x = encoding.width - 1;
                textElem.setAttribute("text-anchor", "end");
            }
            // In all other cases, center the text
            else{
                x = encoding.width / 2;
                textElem.setAttribute("text-anchor", "middle");
            }
 
            textElem.setAttribute("x", x);
            textElem.setAttribute("y", y);
 
            textElem.appendChild(this.document.createTextNode(encoding.text));
 
            parent.appendChild(textElem);
        }
    }
 
 
    setSvgAttributes(width, height){
        var svg = this.svg;
        svg.setAttribute("width", width + "px");
        svg.setAttribute("height", height + "px");
        svg.setAttribute("x", "0px");
        svg.setAttribute("y", "0px");
        svg.setAttribute("viewBox", "0 0 " + width + " " + height);
 
        svg.setAttribute("xmlns", svgns);
        svg.setAttribute("version", "1.1");
 
        svg.setAttribute("style", "transform: translate(0,0)");
    }
 
    createGroup(x, y, parent){
        var group = this.document.createElementNS(svgns, 'g');
        group.setAttribute("transform", "translate(" + x + ", " + y + ")");
 
        parent.appendChild(group);
 
        return group;
    }
 
    setGroupOptions(group, options){
        group.setAttribute("style",
            "fill:" + options.lineColor + ";"
        );
    }
 
    drawRect(x, y, width, height, parent){
        var rect = this.document.createElementNS(svgns, 'rect');
 
        rect.setAttribute("x", x);
        rect.setAttribute("y", y);
        rect.setAttribute("width", width);
        rect.setAttribute("height", height);
 
        parent.appendChild(rect);
 
        return rect;
    }
}
 
export default SVGRenderer;