| 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
175
176
177
178
179
180
181
182
183
184
185
186 | 1×
1×
1×
1×
1×
1×
63×
1×
250×
1×
54×
54×
54×
54×
54×
54×
54×
54×
54×
54×
54×
54×
53×
53×
53×
53×
53×
1×
54×
54×
54×
54×
54×
54×
48×
48×
11×
54×
1×
48×
48×
1×
37×
37×
37×
37×
37×
37×
37×
33×
4×
37×
37×
37×
1×
33×
33×
33×
33×
601×
601×
21974×
15723×
6251×
6251×
6251×
33×
33×
33×
33×
1×
4×
4×
4×
4×
4×
4×
72×
72×
1896×
1350×
546×
546×
546×
1×
53×
53×
53×
53×
53×
53×
53×
53×
53×
53×
53×
1×
37×
37×
37×
37×
37×
37×
1×
36×
36×
37×
37×
37×
1×
1×
36×
37×
1×
37×
30×
7×
7×
7×
7×
7×
7×
39×
39×
39×
26×
13×
7×
7×
7×
1×
37×
1×
1×
| define(["require", "exports", "../barcode/rendering/renderer", "../barcode/utility/barcode-util", "../barcode/utility/dom-util", "./dotcode-encoder"], function (require, exports, renderer_1, barcode_util_1, dom_util_1, dotcode_encoder_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var DOT_DIAMETER_RATIO = 0.8;
var MIN_DOT_SPACING = 1 / DOT_DIAMETER_RATIO;
var DotCode = (function () {
function DotCode() {
this.textHeight = 0;
}
Object.defineProperty(DotCode.prototype, "scale", {
get: function () {
return this.isSvgMode ? 1 : 1.5;
},
enumerable: true,
configurable: true
});
DotCode.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 = dotcode_encoder_1.encodeDotCode({ value: this.value, columns: this.columns, isGs1: this.isGs1 });
if (this.result.success) {
this.calculateDotSpacing();
if (!(this.dotSpacing >= MIN_DOT_SPACING)) {
this.result = { success: false, error: 'InsufficientSize' };
}
}
return this.result;
};
DotCode.prototype.calculateDotSpacing = function () {
var result = this.result;
this.dotSpacing = Math.min(this.symbolWidth / (result.columns + 2 * dotcode_encoder_1.DOTCODE_QUIET_ZONE), this.symbolHeight / (result.rows + 2 * dotcode_encoder_1.DOTCODE_QUIET_ZONE));
};
DotCode.prototype.draw = function (canvas) {
var result = this.result;
Iif (!result || !result.success) {
return;
}
var quiet = dotcode_encoder_1.DOTCODE_QUIET_ZONE;
var spacing = this.dotSpacing;
var offsetX = this.symbolX + quiet * spacing +
(this.symbolWidth - (result.columns + 2 * quiet) * spacing) / 2;
var offsetY = this.symbolY + quiet * spacing +
(this.symbolHeight - (result.rows + 2 * quiet) * spacing) / 2;
if (this.isSvgMode) {
this.drawDotsSvg(canvas, offsetX, offsetY, spacing);
}
else {
this.drawDotsCanvas(canvas, offsetX, offsetY, spacing);
}
Eif (this.textOptions) {
this.drawDisplayText(canvas, offsetX, result.columns * spacing, offsetY, result.rows * spacing);
}
this.result.matrix = undefined;
};
DotCode.prototype.drawDotsSvg = function (svg, offsetX, offsetY, spacing) {
var result = this.result;
var radius = spacing * DOT_DIAMETER_RATIO / 2;
var pathData = '';
for (var row = 0; row < result.rows; row++) {
var rowDots = result.matrix[parseInt(row.toString(), 10)];
for (var column = 0; column < result.columns; column++) {
if (rowDots[parseInt(column.toString(), 10)] !== 1) {
continue;
}
var cx = offsetX + column * spacing + spacing / 2;
var cy = offsetY + row * spacing + spacing / 2;
pathData += 'M' + (cx - radius) + ',' + cy +
'a' + radius + ',' + radius + ' 0 1,0 ' + (2 * radius) + ',0' +
'a' + radius + ',' + radius + ' 0 1,0 ' + (-2 * radius) + ',0z';
}
}
var path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
path.setAttribute('d', pathData);
path.setAttribute('fill', this.foreColor);
svg.appendChild(path);
};
DotCode.prototype.drawDotsCanvas = function (canvasElement, offsetX, offsetY, spacing) {
var result = this.result;
var canvas = canvasElement;
var context = canvas.getContext('2d');
var radius = spacing * DOT_DIAMETER_RATIO / 2;
context.fillStyle = this.foreColor;
for (var row = 0; row < result.rows; row++) {
var rowDots = result.matrix[parseInt(row.toString(), 10)];
for (var column = 0; column < result.columns; column++) {
if (rowDots[parseInt(column.toString(), 10)] !== 1) {
continue;
}
context.beginPath();
context.arc(offsetX + column * spacing + spacing / 2, offsetY + row * spacing + spacing / 2, radius, 0, 2 * Math.PI);
context.fill();
}
}
};
DotCode.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;
};
DotCode.prototype.drawDisplayText = function (canvas, symbolLeft, symbolWidth, symbolTop, symbolHeight) {
var renderer = this.getInstance(canvas.id);
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 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);
};
DotCode.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;
};
DotCode.prototype.getInstance = function (id) {
return new renderer_1.BarcodeRenderer(id, this.isSvgMode);
};
return DotCode;
}());
exports.DotCode = DotCode;
});
|