@levischuck/receiptline 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +201 -0
- package/README.md +278 -0
- package/dist/barcode.d.ts +71 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +1003 -0
- package/dist/parse.d.ts +73 -0
- package/dist/targets/base.d.ts +168 -0
- package/dist/targets/index.d.ts +7 -0
- package/dist/targets/svg.d.ts +52 -0
- package/dist/types.d.ts +65 -0
- package/package.json +52 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1003 @@
|
|
|
1
|
+
import { decodeBase64 as O, encodeBase64 as U } from "@levischuck/tiny-encodings";
|
|
2
|
+
import { qrCode as _, EcLevel as H } from "@levischuck/tiny-qr";
|
|
3
|
+
import { toSvgString as R } from "@levischuck/tiny-qr-svg";
|
|
4
|
+
class F {
|
|
5
|
+
/**
|
|
6
|
+
* Measure text width.
|
|
7
|
+
* @param {string} text string to measure
|
|
8
|
+
* @param {string} encoding codepage
|
|
9
|
+
* @returns {number} string width
|
|
10
|
+
*/
|
|
11
|
+
measureText(t, i) {
|
|
12
|
+
let r = 0;
|
|
13
|
+
const h = Array.from(t);
|
|
14
|
+
switch (i) {
|
|
15
|
+
case "cp932":
|
|
16
|
+
case "shiftjis":
|
|
17
|
+
r = h.reduce((e, o) => {
|
|
18
|
+
const s = o.codePointAt(0);
|
|
19
|
+
return s === void 0 ? e : e + (s < 128 || s === 160 || s === 165 || s === 8254 || s > 65376 && s < 65440 ? 1 : 2);
|
|
20
|
+
}, 0);
|
|
21
|
+
break;
|
|
22
|
+
case "cp936":
|
|
23
|
+
case "gb18030":
|
|
24
|
+
case "cp949":
|
|
25
|
+
case "ksc5601":
|
|
26
|
+
case "cp950":
|
|
27
|
+
case "big5":
|
|
28
|
+
r = h.reduce((e, o) => {
|
|
29
|
+
const s = o.codePointAt(0);
|
|
30
|
+
return s === void 0 ? e : e + (s < 128 || s === 160 ? 1 : 2);
|
|
31
|
+
}, 0);
|
|
32
|
+
break;
|
|
33
|
+
case "tis620":
|
|
34
|
+
const n = h.reduce((e, o) => {
|
|
35
|
+
const s = o.codePointAt(0);
|
|
36
|
+
return s === void 0 || (e.consonant ? s === 3633 || s >= 3636 && s <= 3642 || s === 3655 ? e.vowel ? (e.length += 2, e.consonant = e.vowel = e.tone = !1) : e.vowel = !0 : s >= 3656 && s <= 3659 ? e.tone ? (e.length += 2, e.consonant = e.vowel = e.tone = !1) : e.tone = !0 : s === 3635 || s >= 3660 && s <= 3662 ? e.vowel || e.tone ? (e.length += 2, e.consonant = e.vowel = e.tone = !1) : (e.length += s === 3635 ? 2 : 1, e.consonant = !1) : s >= 3585 && s <= 3630 ? (e.length++, e.vowel = e.tone = !1) : (e.length += 2, e.consonant = e.vowel = e.tone = !1) : s >= 3585 && s <= 3630 ? e.consonant = !0 : e.length++), e;
|
|
37
|
+
}, { length: 0, consonant: !1, vowel: !1, tone: !1 });
|
|
38
|
+
n.consonant && (n.length++, n.consonant = n.vowel = n.tone = !1), r = n.length;
|
|
39
|
+
break;
|
|
40
|
+
default:
|
|
41
|
+
r = h.length;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
return r;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create character array from string (supporting Thai combining characters).
|
|
48
|
+
* @param {string} text string
|
|
49
|
+
* @param {string} encoding codepage
|
|
50
|
+
* @returns {string[]} array instance
|
|
51
|
+
*/
|
|
52
|
+
arrayFrom(t, i) {
|
|
53
|
+
const r = Array.from(t);
|
|
54
|
+
switch (i) {
|
|
55
|
+
case "cp932":
|
|
56
|
+
case "shiftjis":
|
|
57
|
+
return r.map((n) => n.replace(/\\/g, "¥").replace(/\u203e/g, "~").replace(/\u301c/g, "~"));
|
|
58
|
+
case "tis620":
|
|
59
|
+
const h = r.reduce((n, e) => {
|
|
60
|
+
const o = e.codePointAt(0);
|
|
61
|
+
return o === void 0 || (n.consonant ? o === 3633 || o >= 3636 && o <= 3642 || o === 3655 ? n.vowel ? (n.result.push(n.consonant + n.vowel + n.tone, e), n.consonant = n.vowel = n.tone = "") : n.vowel = e : o >= 3656 && o <= 3659 ? n.tone ? (n.result.push(n.consonant + n.vowel + n.tone, e), n.consonant = n.vowel = n.tone = "") : n.tone = e : o === 3635 || o >= 3660 && o <= 3662 ? n.vowel || n.tone ? (n.result.push(n.consonant + n.vowel + n.tone, e), n.consonant = n.vowel = n.tone = "") : (n.result.push(n.consonant + e), n.consonant = "") : o >= 3585 && o <= 3630 ? (n.result.push(n.consonant + n.vowel + n.tone), n.consonant = e, n.vowel = n.tone = "") : (n.result.push(n.consonant + n.vowel + n.tone, e), n.consonant = n.vowel = n.tone = "") : o >= 3585 && o <= 3630 ? n.consonant = e : n.result.push(e)), n;
|
|
62
|
+
}, { result: [], consonant: "", vowel: "", tone: "" });
|
|
63
|
+
return h.consonant && (h.result.push(h.consonant + h.vowel + h.tone), h.consonant = h.vowel = h.tone = ""), h.result;
|
|
64
|
+
default:
|
|
65
|
+
return r;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Start printing.
|
|
70
|
+
* @param {object} printer printer configuration
|
|
71
|
+
* @returns {string} commands
|
|
72
|
+
*/
|
|
73
|
+
open(t) {
|
|
74
|
+
return "";
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Finish printing.
|
|
78
|
+
* @returns {string} commands
|
|
79
|
+
*/
|
|
80
|
+
close() {
|
|
81
|
+
return "";
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Set print area.
|
|
85
|
+
* @param {number} left left margin (unit: characters)
|
|
86
|
+
* @param {number} width print area (unit: characters)
|
|
87
|
+
* @param {number} right right margin (unit: characters)
|
|
88
|
+
* @returns {string} commands
|
|
89
|
+
*/
|
|
90
|
+
area(t, i, r) {
|
|
91
|
+
return "";
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Set line alignment.
|
|
95
|
+
* @param {number} align line alignment (0: left, 1: center, 2: right)
|
|
96
|
+
* @returns {string} commands
|
|
97
|
+
*/
|
|
98
|
+
align(t) {
|
|
99
|
+
return "";
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Set absolute print position.
|
|
103
|
+
* @param {number} position absolute position (unit: characters)
|
|
104
|
+
* @returns {string} commands
|
|
105
|
+
*/
|
|
106
|
+
absolute(t) {
|
|
107
|
+
return "";
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Set relative print position.
|
|
111
|
+
* @param {number} position relative position (unit: characters)
|
|
112
|
+
* @returns {string} commands
|
|
113
|
+
*/
|
|
114
|
+
relative(t) {
|
|
115
|
+
return "";
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Print horizontal rule.
|
|
119
|
+
* @param {number} width line width (unit: characters)
|
|
120
|
+
* @returns {string} commands
|
|
121
|
+
*/
|
|
122
|
+
hr(t) {
|
|
123
|
+
return "";
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Print vertical rules.
|
|
127
|
+
* @param {number[]} widths vertical line spacing
|
|
128
|
+
* @param {number} height text height (1-6)
|
|
129
|
+
* @returns {string} commands
|
|
130
|
+
*/
|
|
131
|
+
vr(t, i) {
|
|
132
|
+
return "";
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Start rules.
|
|
136
|
+
* @param {number[]} widths vertical line spacing
|
|
137
|
+
* @returns {string} commands
|
|
138
|
+
*/
|
|
139
|
+
vrstart(t) {
|
|
140
|
+
return "";
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Stop rules.
|
|
144
|
+
* @param {number[]} widths vertical line spacing
|
|
145
|
+
* @returns {string} commands
|
|
146
|
+
*/
|
|
147
|
+
vrstop(t) {
|
|
148
|
+
return "";
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Print vertical and horizontal rules.
|
|
152
|
+
* @param {number[]} widths1 vertical line spacing (stop)
|
|
153
|
+
* @param {number[]} widths2 vertical line spacing (start)
|
|
154
|
+
* @param {number} dl difference in left position
|
|
155
|
+
* @param {number} dr difference in right position
|
|
156
|
+
* @returns {string} commands
|
|
157
|
+
*/
|
|
158
|
+
vrhr(t, i, r, h) {
|
|
159
|
+
return "";
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Set line spacing and feed new line.
|
|
163
|
+
* @param {boolean} vr whether vertical ruled lines are printed
|
|
164
|
+
* @returns {string} commands
|
|
165
|
+
*/
|
|
166
|
+
vrlf(t) {
|
|
167
|
+
return "";
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Cut paper.
|
|
171
|
+
* @returns {string} commands
|
|
172
|
+
*/
|
|
173
|
+
cut() {
|
|
174
|
+
return "";
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Underline text.
|
|
178
|
+
* @returns {string} commands
|
|
179
|
+
*/
|
|
180
|
+
ul() {
|
|
181
|
+
return "";
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Emphasize text.
|
|
185
|
+
* @returns {string} commands
|
|
186
|
+
*/
|
|
187
|
+
em() {
|
|
188
|
+
return "";
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Invert text.
|
|
192
|
+
* @returns {string} commands
|
|
193
|
+
*/
|
|
194
|
+
iv() {
|
|
195
|
+
return "";
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Scale up text.
|
|
199
|
+
* @param {number} wh number of special character '^' (1-7)
|
|
200
|
+
* @returns {string} commands
|
|
201
|
+
*/
|
|
202
|
+
wh(t) {
|
|
203
|
+
return "";
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Cancel text decoration.
|
|
207
|
+
* @returns {string} commands
|
|
208
|
+
*/
|
|
209
|
+
normal() {
|
|
210
|
+
return "";
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Print text.
|
|
214
|
+
* @param {string} text string to print
|
|
215
|
+
* @param {string} encoding codepage
|
|
216
|
+
* @returns {string} commands
|
|
217
|
+
*/
|
|
218
|
+
text(t, i) {
|
|
219
|
+
return "";
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Feed new line.
|
|
223
|
+
* @returns {string} commands
|
|
224
|
+
*/
|
|
225
|
+
lf() {
|
|
226
|
+
return "";
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Insert commands.
|
|
230
|
+
* @param {string} command commands to insert
|
|
231
|
+
* @returns {string} commands
|
|
232
|
+
*/
|
|
233
|
+
command(t) {
|
|
234
|
+
return "";
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Print image.
|
|
238
|
+
* @param {string} image image data (base64 png format)
|
|
239
|
+
* @returns {string} commands
|
|
240
|
+
*/
|
|
241
|
+
image(t) {
|
|
242
|
+
return "";
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Print QR Code.
|
|
246
|
+
* @param {object} symbol QR Code information (data, type, cell, level)
|
|
247
|
+
* @param {string} encoding codepage
|
|
248
|
+
* @returns {string} commands
|
|
249
|
+
*/
|
|
250
|
+
qrcode(t, i) {
|
|
251
|
+
return "";
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Print barcode.
|
|
255
|
+
* @param {object} symbol barcode information (data, type, width, height, hri)
|
|
256
|
+
* @param {string} encoding codepage
|
|
257
|
+
* @returns {string} commands
|
|
258
|
+
*/
|
|
259
|
+
barcode(t, i) {
|
|
260
|
+
return "";
|
|
261
|
+
}
|
|
262
|
+
calculatedWidth() {
|
|
263
|
+
return 0;
|
|
264
|
+
}
|
|
265
|
+
calculatedHeight() {
|
|
266
|
+
return 0;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const f = {
|
|
270
|
+
element: "212222,222122,222221,121223,121322,131222,122213,122312,132212,221213,221312,231212,112232,122132,122231,113222,123122,123221,223211,221132,221231,213212,223112,312131,311222,321122,321221,312212,322112,322211,212123,212321,232121,111323,131123,131321,112313,132113,132311,211313,231113,231311,112133,112331,132131,113123,113321,133121,313121,211331,231131,213113,213311,213131,311123,311321,331121,312113,312311,332111,314111,221411,431111,111224,111422,121124,121421,141122,141221,112214,112412,122114,122411,142112,142211,241211,221114,413111,241112,134111,111242,121142,121241,114212,124112,124211,411212,421112,421211,212141,214121,412121,111143,111341,131141,114113,114311,411113,411311,113141,114131,311141,411131,211412,211214,211232,2331112".split(","),
|
|
271
|
+
starta: 103,
|
|
272
|
+
startb: 104,
|
|
273
|
+
startc: 105,
|
|
274
|
+
atob: 100,
|
|
275
|
+
atoc: 99,
|
|
276
|
+
btoa: 101,
|
|
277
|
+
btoc: 99,
|
|
278
|
+
ctoa: 101,
|
|
279
|
+
ctob: 100,
|
|
280
|
+
shift: 98,
|
|
281
|
+
stop: 106
|
|
282
|
+
};
|
|
283
|
+
function N(a, t, i) {
|
|
284
|
+
a !== f.shift && i.push(a), t = t.replace(/^((?!\d{4,})[\x00-_])+/, (n) => (n.split("").forEach((e) => i.push((e.charCodeAt(0) + 64) % 96)), "")), t = t.replace(/^\d(?=(\d\d){2,}(\D|$))/, (n) => (i.push((n.charCodeAt(0) + 64) % 96), ""));
|
|
285
|
+
const r = t.slice(1), h = r.search(/[^ -_]/);
|
|
286
|
+
/^\d{4,}/.test(t) ? I(f.atoc, t, i) : h >= 0 && r.charCodeAt(h) < 32 ? (i.push(f.shift, t.charCodeAt(0) - 32), N(f.shift, r, i)) : t.length > 0 && S(f.atob, t, i);
|
|
287
|
+
}
|
|
288
|
+
function S(a, t, i) {
|
|
289
|
+
a !== f.shift && i.push(a), t = t.replace(/^((?!\d{4,})[ -\x7f])+/, (n) => (n.split("").forEach((e) => i.push(e.charCodeAt(0) - 32)), "")), t = t.replace(/^\d(?=(\d\d){2,}(\D|$))/, (n) => (i.push(n.charCodeAt(0) - 32), ""));
|
|
290
|
+
const r = t.slice(1), h = r.search(/[^ -_]/);
|
|
291
|
+
/^\d{4,}/.test(t) ? I(f.btoc, t, i) : h >= 0 && r.charCodeAt(h) > 95 ? (i.push(f.shift, t.charCodeAt(0) + 64), S(f.shift, r, i)) : t.length > 0 && N(f.btoa, t, i);
|
|
292
|
+
}
|
|
293
|
+
function I(a, t, i) {
|
|
294
|
+
a !== f.shift && i.push(a), t = t.replace(/^\d{4,}/g, (h) => h.replace(/\d{2}/g, (n) => (i.push(Number(n)), "")));
|
|
295
|
+
const r = t.search(/[^ -_]/);
|
|
296
|
+
r >= 0 && t.charCodeAt(r) < 32 ? N(f.ctoa, t, i) : t.length > 0 && S(f.ctob, t, i);
|
|
297
|
+
}
|
|
298
|
+
function G(a) {
|
|
299
|
+
const t = {};
|
|
300
|
+
let i = a.data.replace(/((?!^[\x00-\x7f]+$).)*/, "");
|
|
301
|
+
if (i.length > 0) {
|
|
302
|
+
t.hri = a.hri, t.text = i.replace(/[\x00- \x7f]/g, " ");
|
|
303
|
+
const r = [], h = i.search(/[^ -_]/);
|
|
304
|
+
/^\d{2}$/.test(i) ? r.push(f.startc, Number(i)) : /^\d{4,}/.test(i) ? I(f.startc, i, r) : h >= 0 && i.charCodeAt(h) < 32 ? N(f.starta, i, r) : i.length > 0 && S(f.startb, i, r), r.push(r.reduce((o, s, c) => o + s * c) % 103, f.stop);
|
|
305
|
+
const n = a.quietZone ? "a" : "0", e = r.reduce((o, s) => o + f.element[s], n) + n;
|
|
306
|
+
t.widths = e.split("").map((o) => parseInt(o, 16) * a.width), t.length = a.width * (r.length * 11 + (a.quietZone ? 22 : 2)), t.height = a.height;
|
|
307
|
+
}
|
|
308
|
+
return t;
|
|
309
|
+
}
|
|
310
|
+
const q = {
|
|
311
|
+
escape: "cU,dA,dB,dC,dD,dE,dF,dG,dH,dI,dJ,dK,dL,dM,dN,dO,dP,dQ,dR,dS,dT,dU,dV,dW,dX,dY,dZ,cA,cB,cC,cD,cE, ,sA,sB,sC,$,%,sF,sG,sH,sI,sJ,+,sL,-,.,/,0,1,2,3,4,5,6,7,8,9,sZ,cF,cG,cH,cI,cJ,cV,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,cK,cL,cM,cN,cO,cW,pA,pB,pC,pD,pE,pF,pG,pH,pI,pJ,pK,pL,pM,pN,pO,pP,pQ,pR,pS,pT,pU,pV,pW,pX,pY,pZ,cP,cQ,cR,cS,cT".split(","),
|
|
312
|
+
code: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%dcsp".split("").reduce((a, t, i) => (a[t] = i, a), {}),
|
|
313
|
+
element: "131112,111213,111312,111411,121113,121212,121311,111114,131211,141111,211113,211212,211311,221112,221211,231111,112113,112212,112311,122112,132111,111123,111222,111321,121122,131121,212112,212211,211122,211221,221121,222111,112122,112221,122121,123111,121131,311112,311211,321111,112131,113121,211131,121221,312111,311121,122211,111141,1111411".split(","),
|
|
314
|
+
start: 47,
|
|
315
|
+
stop: 48
|
|
316
|
+
};
|
|
317
|
+
function K(a) {
|
|
318
|
+
const t = {};
|
|
319
|
+
let i = a.data.replace(/((?!^[\x00-\x7f]+$).)*/, "");
|
|
320
|
+
if (i.length > 0) {
|
|
321
|
+
t.hri = a.hri, t.text = i.replace(/[\x00- \x7f]/g, " ");
|
|
322
|
+
const r = i.split("").reduce((e, o) => e + q.escape[o.charCodeAt(0)], "").split("").map((e) => q.code[e]);
|
|
323
|
+
r.push(r.reduceRight((e, o, s) => e + o * ((r.length - 1 - s) % 20 + 1)) % 47), r.push(r.reduceRight((e, o, s) => e + o * ((r.length - 1 - s) % 15 + 1)) % 47), r.unshift(q.start), r.push(q.stop);
|
|
324
|
+
const h = a.quietZone ? "a" : "0", n = r.reduce((e, o) => e + q.element[o], h) + h;
|
|
325
|
+
t.widths = n.split("").map((e) => parseInt(e, 16) * a.width), t.length = a.width * (r.length * 9 + (a.quietZone ? 21 : 1)), t.height = a.height;
|
|
326
|
+
}
|
|
327
|
+
return t;
|
|
328
|
+
}
|
|
329
|
+
const V = {
|
|
330
|
+
0: "2222255",
|
|
331
|
+
1: "2222552",
|
|
332
|
+
2: "2225225",
|
|
333
|
+
3: "5522222",
|
|
334
|
+
4: "2252252",
|
|
335
|
+
5: "5222252",
|
|
336
|
+
6: "2522225",
|
|
337
|
+
7: "2522522",
|
|
338
|
+
8: "2552222",
|
|
339
|
+
9: "5225222",
|
|
340
|
+
"-": "2225522",
|
|
341
|
+
$: "2255222",
|
|
342
|
+
":": "5222525",
|
|
343
|
+
"/": "5252225",
|
|
344
|
+
".": "5252522",
|
|
345
|
+
"+": "2252525",
|
|
346
|
+
A: "2255252",
|
|
347
|
+
B: "2525225",
|
|
348
|
+
C: "2225255",
|
|
349
|
+
D: "2225552"
|
|
350
|
+
};
|
|
351
|
+
function J(a) {
|
|
352
|
+
const t = {};
|
|
353
|
+
let i = a.data.replace(/((?!^[A-D][0-9\-$:/.+]+[A-D]$).)*/i, "");
|
|
354
|
+
if (i.length > 0) {
|
|
355
|
+
t.hri = a.hri, t.text = i;
|
|
356
|
+
const r = a.quietZone ? "a" : "0", h = i.toUpperCase().split("").reduce((e, o) => e + V[o] + "2", r).slice(0, -1) + r;
|
|
357
|
+
t.widths = h.split("").map((e) => parseInt(e, 16) * a.width + 1 >> 1);
|
|
358
|
+
const n = [25, 39, 50, 3, 5, 6];
|
|
359
|
+
t.length = i.length * n[a.width - 2] - (i.match(/[\d\-$]/g) || []).length * n[a.width + 1] + a.width * (a.quietZone ? 19 : -1), t.height = a.height;
|
|
360
|
+
}
|
|
361
|
+
return t;
|
|
362
|
+
}
|
|
363
|
+
const Z = {
|
|
364
|
+
element: "22552,52225,25225,55222,22525,52522,25522,22255,52252,25252".split(","),
|
|
365
|
+
start: "2222",
|
|
366
|
+
stop: "522"
|
|
367
|
+
};
|
|
368
|
+
function Q(a) {
|
|
369
|
+
const t = {};
|
|
370
|
+
let i = a.data.replace(/((?!^(\d{2})+$).)*/, "");
|
|
371
|
+
if (i.length > 0) {
|
|
372
|
+
t.hri = a.hri, t.text = i;
|
|
373
|
+
const r = i.split("").map((s) => Number(s)), h = a.quietZone ? "a" : "0";
|
|
374
|
+
let n = h + Z.start, e = 0;
|
|
375
|
+
for (; e < r.length; ) {
|
|
376
|
+
const s = Z.element[r[e++]], c = Z.element[r[e++]];
|
|
377
|
+
n += s.split("").reduce((l, b, g) => l + b + c[g], "");
|
|
378
|
+
}
|
|
379
|
+
n += Z.stop + h, t.widths = n.split("").map((s) => parseInt(s, 16) * a.width + 1 >> 1);
|
|
380
|
+
const o = [16, 25, 32, 17, 26, 34];
|
|
381
|
+
t.length = i.length * o[a.width - 2] + o[a.width + 1] + a.width * (a.quietZone ? 20 : 0), t.height = a.height;
|
|
382
|
+
}
|
|
383
|
+
return t;
|
|
384
|
+
}
|
|
385
|
+
const Y = {
|
|
386
|
+
0: "222552522",
|
|
387
|
+
1: "522522225",
|
|
388
|
+
2: "225522225",
|
|
389
|
+
3: "525522222",
|
|
390
|
+
4: "222552225",
|
|
391
|
+
5: "522552222",
|
|
392
|
+
6: "225552222",
|
|
393
|
+
7: "222522525",
|
|
394
|
+
8: "522522522",
|
|
395
|
+
9: "225522522",
|
|
396
|
+
A: "522225225",
|
|
397
|
+
B: "225225225",
|
|
398
|
+
C: "525225222",
|
|
399
|
+
D: "222255225",
|
|
400
|
+
E: "522255222",
|
|
401
|
+
F: "225255222",
|
|
402
|
+
G: "222225525",
|
|
403
|
+
H: "522225522",
|
|
404
|
+
I: "225225522",
|
|
405
|
+
J: "222255522",
|
|
406
|
+
K: "522222255",
|
|
407
|
+
L: "225222255",
|
|
408
|
+
M: "525222252",
|
|
409
|
+
N: "222252255",
|
|
410
|
+
O: "522252252",
|
|
411
|
+
P: "225252252",
|
|
412
|
+
Q: "222222555",
|
|
413
|
+
R: "522222552",
|
|
414
|
+
S: "225222552",
|
|
415
|
+
T: "222252552",
|
|
416
|
+
U: "552222225",
|
|
417
|
+
V: "255222225",
|
|
418
|
+
W: "555222222",
|
|
419
|
+
X: "252252225",
|
|
420
|
+
Y: "552252222",
|
|
421
|
+
Z: "255252222",
|
|
422
|
+
"-": "252222525",
|
|
423
|
+
".": "552222522",
|
|
424
|
+
" ": "255222522",
|
|
425
|
+
$: "252525222",
|
|
426
|
+
"/": "252522252",
|
|
427
|
+
"+": "252225252",
|
|
428
|
+
"%": "222525252",
|
|
429
|
+
"*": "252252522"
|
|
430
|
+
};
|
|
431
|
+
function X(a) {
|
|
432
|
+
const t = {};
|
|
433
|
+
let i = a.data.replace(/((?!^\*?[0-9A-Z\-. $/+%]+\*?$).)*/, "");
|
|
434
|
+
if (i.length > 0) {
|
|
435
|
+
i = i.replace(/^\*?([^*]+)\*?$/, "*$1*"), t.hri = a.hri, t.text = i;
|
|
436
|
+
const r = a.quietZone ? "a" : "0", h = i.split("").reduce((e, o) => e + Y[o] + "2", r).slice(0, -1) + r;
|
|
437
|
+
t.widths = h.split("").map((e) => parseInt(e, 16) * a.width + 1 >> 1);
|
|
438
|
+
const n = [29, 45, 58];
|
|
439
|
+
t.length = i.length * n[a.width - 2] + a.width * (a.quietZone ? 19 : -1), t.height = a.height;
|
|
440
|
+
}
|
|
441
|
+
return t;
|
|
442
|
+
}
|
|
443
|
+
const m = {
|
|
444
|
+
a: "3211,2221,2122,1411,1132,1231,1114,1312,1213,3112".split(","),
|
|
445
|
+
b: "1123,1222,2212,1141,2311,1321,4111,2131,3121,2113".split(","),
|
|
446
|
+
c: "3211,2221,2122,1411,1132,1231,1114,1312,1213,3112".split(","),
|
|
447
|
+
g: "111,11111,111111,11,112".split(","),
|
|
448
|
+
p: "aaaaaa,aababb,aabbab,aabbba,abaabb,abbaab,abbbaa,ababab,ababba,abbaba".split(","),
|
|
449
|
+
e: "bbbaaa,bbabaa,bbaaba,bbaaab,babbaa,baabba,baaabb,bababa,babaab,baabab".split(",")
|
|
450
|
+
};
|
|
451
|
+
function y(a) {
|
|
452
|
+
const t = a.slice(0, 3);
|
|
453
|
+
switch (a[6]) {
|
|
454
|
+
case 0:
|
|
455
|
+
case 1:
|
|
456
|
+
case 2:
|
|
457
|
+
t.push(a[6], 0, 0, 0, 0, a[3], a[4], a[5]);
|
|
458
|
+
break;
|
|
459
|
+
case 3:
|
|
460
|
+
t.push(a[3], 0, 0, 0, 0, 0, a[4], a[5]);
|
|
461
|
+
break;
|
|
462
|
+
case 4:
|
|
463
|
+
t.push(a[3], a[4], 0, 0, 0, 0, 0, a[5]);
|
|
464
|
+
break;
|
|
465
|
+
default:
|
|
466
|
+
t.push(a[3], a[4], a[5], 0, 0, 0, 0, a[6]);
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
return t.push(a[7]), t;
|
|
470
|
+
}
|
|
471
|
+
function D(a) {
|
|
472
|
+
const t = {}, i = a.data.replace(/((?!^\d{12,13}$).)*/, "").split("").map((r) => Number(r));
|
|
473
|
+
if (i.length > 0) {
|
|
474
|
+
i[12] = 0, i[12] = (10 - i.reduce((h, n, e) => h + n * (e % 2 * 2 + 1)) % 10) % 10, t.hri = a.hri, t.text = i.join("");
|
|
475
|
+
let r = (a.quietZone ? "b" : "0") + m.g[0];
|
|
476
|
+
for (let h = 1; h < 7; h++) r += m[m.p[i[0]][h - 1]][i[h]];
|
|
477
|
+
r += m.g[1];
|
|
478
|
+
for (let h = 7; h < 13; h++) r += m.c[i[h]];
|
|
479
|
+
r += m.g[0] + (a.quietZone ? "7" : "0"), t.widths = r.split("").map((h) => parseInt(h, 16) * a.width), t.length = a.width * (a.quietZone ? 113 : 95), t.height = a.height;
|
|
480
|
+
}
|
|
481
|
+
return t;
|
|
482
|
+
}
|
|
483
|
+
function tt(a) {
|
|
484
|
+
const t = {}, i = a.data.replace(/((?!^\d{7,8}$).)*/, "").split("").map((r) => Number(r));
|
|
485
|
+
if (i.length > 0) {
|
|
486
|
+
i[7] = 0, i[7] = (10 - i.reduce((n, e, o) => n + e * (3 - o % 2 * 2), 0) % 10) % 10, t.hri = a.hri, t.text = i.join("");
|
|
487
|
+
const r = a.quietZone ? "7" : "0";
|
|
488
|
+
let h = r + m.g[0];
|
|
489
|
+
for (let n = 0; n < 4; n++) h += m.a[i[n] ?? 0];
|
|
490
|
+
h += m.g[1];
|
|
491
|
+
for (let n = 4; n < 8; n++) h += m.c[i[n] ?? 0];
|
|
492
|
+
h += m.g[0] + r, t.widths = h.split("").map((n) => parseInt(n, 16) * a.width), t.length = a.width * (a.quietZone ? 81 : 67), t.height = a.height;
|
|
493
|
+
}
|
|
494
|
+
return t;
|
|
495
|
+
}
|
|
496
|
+
function et(a) {
|
|
497
|
+
const t = {
|
|
498
|
+
data: "0" + a.data,
|
|
499
|
+
hri: a.hri,
|
|
500
|
+
width: a.width,
|
|
501
|
+
height: a.height,
|
|
502
|
+
quietZone: a.quietZone
|
|
503
|
+
}, i = D(t);
|
|
504
|
+
return i.text && (i.text = i.text.slice(1)), i;
|
|
505
|
+
}
|
|
506
|
+
function it(a) {
|
|
507
|
+
const t = {}, i = a.data.replace(/((?!^0\d{6,7}$).)*/, "").split("").map((r) => Number(r));
|
|
508
|
+
if (i.length > 0) {
|
|
509
|
+
i[7] = 0, i[7] = (10 - y(i).reduce((n, e, o) => n + e * (3 - o % 2 * 2), 0) % 10) % 10, t.hri = a.hri, t.text = i.join("");
|
|
510
|
+
const r = a.quietZone ? "7" : "0";
|
|
511
|
+
let h = r + m.g[0];
|
|
512
|
+
for (let n = 1; n < 7; n++) h += m[m.e[i[7] ?? 0][n - 1]][i[n] ?? 0];
|
|
513
|
+
h += m.g[2] + r, t.widths = h.split("").map((n) => parseInt(n, 16) * a.width), t.length = a.width * (a.quietZone ? 65 : 51), t.height = a.height;
|
|
514
|
+
}
|
|
515
|
+
return t;
|
|
516
|
+
}
|
|
517
|
+
function nt(a) {
|
|
518
|
+
let t = {};
|
|
519
|
+
switch (a.type) {
|
|
520
|
+
case "upc":
|
|
521
|
+
t = a.data.length < 9 ? it(a) : et(a);
|
|
522
|
+
break;
|
|
523
|
+
case "ean":
|
|
524
|
+
case "jan":
|
|
525
|
+
t = a.data.length < 9 ? tt(a) : D(a);
|
|
526
|
+
break;
|
|
527
|
+
case "code39":
|
|
528
|
+
t = X(a);
|
|
529
|
+
break;
|
|
530
|
+
case "itf":
|
|
531
|
+
t = Q(a);
|
|
532
|
+
break;
|
|
533
|
+
case "codabar":
|
|
534
|
+
case "nw7":
|
|
535
|
+
t = J(a);
|
|
536
|
+
break;
|
|
537
|
+
case "code93":
|
|
538
|
+
t = K(a);
|
|
539
|
+
break;
|
|
540
|
+
case "code128":
|
|
541
|
+
t = G(a);
|
|
542
|
+
break;
|
|
543
|
+
}
|
|
544
|
+
return t;
|
|
545
|
+
}
|
|
546
|
+
class E extends F {
|
|
547
|
+
charWidth = 12;
|
|
548
|
+
receiptId = "";
|
|
549
|
+
svgWidth = 576;
|
|
550
|
+
svgHeight = 0;
|
|
551
|
+
svgContent = "";
|
|
552
|
+
lineMargin = 0;
|
|
553
|
+
lineAlign = 0;
|
|
554
|
+
lineWidth = 48;
|
|
555
|
+
lineHeight = 1;
|
|
556
|
+
textElement = "";
|
|
557
|
+
textAttributes = {};
|
|
558
|
+
textPosition = 0;
|
|
559
|
+
textScale = 1;
|
|
560
|
+
textEncoding = "multilingual";
|
|
561
|
+
feedMinimum = 24;
|
|
562
|
+
// printer configuration
|
|
563
|
+
spacing = !1;
|
|
564
|
+
defaultFont = "monospace";
|
|
565
|
+
fontSize = 12;
|
|
566
|
+
// start printing:
|
|
567
|
+
open(t) {
|
|
568
|
+
return this.receiptId = crypto.randomUUID(), this.charWidth = t.charWidth, this.svgWidth = t.cpl * t.charWidth, this.svgHeight = 0, this.svgContent = "", this.lineMargin = 0, this.lineAlign = 0, this.lineWidth = t.cpl, this.lineHeight = 1, this.textElement = "", this.textAttributes = {}, this.textPosition = 0, this.textScale = 1, this.textEncoding = t.encoding, this.feedMinimum = Number(t.charWidth * (t.spacing ? 2.5 : 2)), this.spacing = t.spacing, "";
|
|
569
|
+
}
|
|
570
|
+
setDefaultFont(t) {
|
|
571
|
+
return this.defaultFont = t, "'Courier Prime'";
|
|
572
|
+
}
|
|
573
|
+
// finish printing:
|
|
574
|
+
close() {
|
|
575
|
+
const t = { font: "monospace", size: this.charWidth * 2, style: "", lang: "" };
|
|
576
|
+
switch (this.textEncoding) {
|
|
577
|
+
case "cp932":
|
|
578
|
+
case "shiftjis":
|
|
579
|
+
t.font = "'BIZ UDGothic', monospace", t.lang = "ja";
|
|
580
|
+
break;
|
|
581
|
+
case "cp936":
|
|
582
|
+
case "gb18030":
|
|
583
|
+
t.size -= 2, t.lang = "zh-Hans";
|
|
584
|
+
break;
|
|
585
|
+
case "cp949":
|
|
586
|
+
case "ksc5601":
|
|
587
|
+
t.size -= 2, t.lang = "ko";
|
|
588
|
+
break;
|
|
589
|
+
case "cp950":
|
|
590
|
+
case "big5":
|
|
591
|
+
t.size -= 2, t.lang = "zh-Hant";
|
|
592
|
+
break;
|
|
593
|
+
case "tis620":
|
|
594
|
+
t.font = "'Sarabun', monospace", t.size -= 4, t.lang = "th";
|
|
595
|
+
break;
|
|
596
|
+
default:
|
|
597
|
+
t.font = `${this.defaultFont}, monospace`, t.size -= 2;
|
|
598
|
+
break;
|
|
599
|
+
}
|
|
600
|
+
t.style.length > 0 && (t.style = `<style type="text/css"><![CDATA[${t.style}]]></style>`), t.lang.length > 0 && (t.lang = ` xml:lang="${t.lang}"`), this.fontSize = t.size;
|
|
601
|
+
const i = this.calculatedWidth(), r = this.calculatedHeight();
|
|
602
|
+
return `<svg width="${i}px" height="${r}px" viewBox="0 0 ${i} ${r}" preserveAspectRatio="xMinYMin meet" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">${t.style}<defs><filter id="receipt-${this.receiptId}" x="0" y="0" width="100%" height="100%"><feFlood flood-color="#000"/><feComposite in2="SourceGraphic" operator="out"/></filter></defs><rect width="100%" height="100%" fill="white" /><g font-family="${t.font}" fill="#000" font-size="${t.size}" dominant-baseline="text-after-edge" text-anchor="middle"${t.lang}>${this.svgContent}</g></svg>
|
|
603
|
+
`;
|
|
604
|
+
}
|
|
605
|
+
// set print area:
|
|
606
|
+
area(t, i, r) {
|
|
607
|
+
return this.lineMargin = t, this.lineWidth = i, "";
|
|
608
|
+
}
|
|
609
|
+
// set line alignment:
|
|
610
|
+
align(t) {
|
|
611
|
+
return this.lineAlign = t, "";
|
|
612
|
+
}
|
|
613
|
+
// set absolute print position:
|
|
614
|
+
absolute(t) {
|
|
615
|
+
return this.textPosition = t, "";
|
|
616
|
+
}
|
|
617
|
+
// set relative print position:
|
|
618
|
+
relative(t) {
|
|
619
|
+
return this.textPosition += t, "";
|
|
620
|
+
}
|
|
621
|
+
// print horizontal rule:
|
|
622
|
+
hr(t) {
|
|
623
|
+
const i = this.charWidth, r = `<path d="M0,${i}h${i * t}" fill="none" stroke="#000" stroke-width="2"/>`;
|
|
624
|
+
return this.svgContent += `<g transform="translate(${this.lineMargin * i},${this.svgHeight})">${r}</g>`, "";
|
|
625
|
+
}
|
|
626
|
+
// print vertical rules:
|
|
627
|
+
vr(t, i) {
|
|
628
|
+
const r = this.charWidth, h = r / 2, n = (r + r) * i, e = '<path d="' + t.reduce((o, s) => o + `m${r * s + r},${-n}v${n}`, `M${h},0v${n}`) + '" fill="none" stroke="#000" stroke-width="2"/>';
|
|
629
|
+
return this.svgContent += `<g transform="translate(${this.lineMargin * r},${this.svgHeight})">${e}</g>`, "";
|
|
630
|
+
}
|
|
631
|
+
// start rules:
|
|
632
|
+
vrstart(t) {
|
|
633
|
+
const i = this.charWidth, r = i / 2, h = '<path d="' + t.reduce((n, e) => n + `h${i * e}h${r}v${i}m0,${-i}h${r}`, `M${r},${i + i}v${-r}q0,${-r},${r},${-r}`).replace(/h\d+v\d+m0,-\d+h\d+$/, `q${r},0,${r},${r}v${r}`) + '" fill="none" stroke="#000" stroke-width="2"/>';
|
|
634
|
+
return this.svgContent += `<g transform="translate(${this.lineMargin * i},${this.svgHeight})">${h}</g>`, "";
|
|
635
|
+
}
|
|
636
|
+
// stop rules:
|
|
637
|
+
vrstop(t) {
|
|
638
|
+
const i = this.charWidth, r = i / 2, h = '<path d="' + t.reduce((n, e) => n + `h${i * e}h${r}v${-i}m0,${i}h${r}`, `M${r},0v${r}q0,${r},${r},${r}`).replace(/h\d+v-\d+m0,\d+h\d+$/, `q${r},0,${r},${-r}v${-r}`) + '" fill="none" stroke="#000" stroke-width="2"/>';
|
|
639
|
+
return this.svgContent += `<g transform="translate(${this.lineMargin * i},${this.svgHeight})">${h}</g>`, "";
|
|
640
|
+
}
|
|
641
|
+
// print vertical and horizontal rules:
|
|
642
|
+
vrhr(t, i, r, h) {
|
|
643
|
+
const n = this.charWidth, e = n / 2, o = '<path d="' + t.reduce((c, l) => c + `h${n * l}h${e}v${-n}m0,${n}h${e}`, `M${e},0` + (r > 0 ? `v${e}q0,${e},${e},${e}` : `v${n}h${e}`)).replace(/h\d+v-\d+m0,\d+h\d+$/, h < 0 ? `q${e},0,${e},${-e}v${-e}` : `h${e}v${-n}`) + '" fill="none" stroke="#000" stroke-width="2"/>';
|
|
644
|
+
this.svgContent += `<g transform="translate(${(this.lineMargin + Math.max(-r, 0)) * n},${this.svgHeight})">${o}</g>`;
|
|
645
|
+
const s = '<path d="' + i.reduce((c, l) => c + `h${n * l}h${e}v${n}m0,${-n}h${e}`, `M${e},${n + n}` + (r < 0 ? `v${-e}q0,${-e},${e},${-e}` : `v${-n}h${e}`)).replace(/h\d+v\d+m0,-\d+h\d+$/, h > 0 ? `q${e},0,${e},${e}v${e}` : `h${e}v${n}`) + '" fill="none" stroke="#000" stroke-width="2"/>';
|
|
646
|
+
return this.svgContent += `<g transform="translate(${(this.lineMargin + Math.max(r, 0)) * n},${this.svgHeight})">${s}</g>`, "";
|
|
647
|
+
}
|
|
648
|
+
// set line spacing and feed new line:
|
|
649
|
+
vrlf(t) {
|
|
650
|
+
return this.feedMinimum = Number(this.charWidth * (!t && this.spacing ? 2.5 : 2)), this.lf();
|
|
651
|
+
}
|
|
652
|
+
// cut paper:
|
|
653
|
+
cut() {
|
|
654
|
+
const t = `<path d="M12,12.5l-7.5,-3a2,2,0,1,1,.5,0M12,11.5l-7.5,3a2,2,0,1,0,.5,0" fill="none" stroke="#000" stroke-width="1"/><path d="M12,12l10,-4q-1,-1,-2.5,-1l-10,4v2l10,4q1.5,0,2.5,-1z" fill="#000"/><path d="M24,12h${this.svgWidth - 24}" fill="none" stroke="#000" stroke-width="2" stroke-dasharray="2"/>`;
|
|
655
|
+
return this.svgContent += `<g transform="translate(0,${this.svgHeight})">${t}</g>`, this.lf();
|
|
656
|
+
}
|
|
657
|
+
// underline text:
|
|
658
|
+
ul() {
|
|
659
|
+
return this.textAttributes["text-decoration"] = "underline", "";
|
|
660
|
+
}
|
|
661
|
+
// emphasize text:
|
|
662
|
+
em() {
|
|
663
|
+
return this.textAttributes.stroke = "#000", "";
|
|
664
|
+
}
|
|
665
|
+
// invert text:
|
|
666
|
+
iv() {
|
|
667
|
+
return this.textAttributes.filter = `url(#receipt-${this.receiptId})`, "";
|
|
668
|
+
}
|
|
669
|
+
// scale up text:
|
|
670
|
+
wh(t) {
|
|
671
|
+
const i = t < 2 ? t + 1 : t - 1, r = t < 3 ? t : t - 1;
|
|
672
|
+
return this.textAttributes.transform = `scale(${i},${r})`, this.lineHeight = Math.max(this.lineHeight, r), this.textScale = i, "";
|
|
673
|
+
}
|
|
674
|
+
// cancel text decoration:
|
|
675
|
+
normal() {
|
|
676
|
+
return this.textAttributes = {}, this.textScale = 1, "";
|
|
677
|
+
}
|
|
678
|
+
// print text:
|
|
679
|
+
text(t, i) {
|
|
680
|
+
let r = this.textPosition;
|
|
681
|
+
const h = this.arrayFrom(t, i).reduce((e, o) => {
|
|
682
|
+
const s = this.measureText(o, i) * this.textScale, c = Math.floor((r + s / 2) * this.charWidth / this.textScale);
|
|
683
|
+
return r += s, e + `<tspan x="${c}">${o.replace(/[ &<>]/g, (l) => ({ " ": " ", "&": "&", "<": "<", ">": ">" })[l] || l)}</tspan>`;
|
|
684
|
+
}, ""), n = Object.keys(this.textAttributes).reduce((e, o) => e + ` ${o}="${this.textAttributes[o]}"`, "");
|
|
685
|
+
return this.textElement += `<text${n}>${h}</text>`, this.textPosition += this.measureText(t, i) * this.textScale, "";
|
|
686
|
+
}
|
|
687
|
+
// feed new line:
|
|
688
|
+
lf() {
|
|
689
|
+
const t = this.lineHeight * this.charWidth * 2;
|
|
690
|
+
return this.textElement.length > 0 && (this.svgContent += `<g transform="translate(${this.lineMargin * this.charWidth},${this.svgHeight + t})">${this.textElement}</g>`), this.svgHeight += Math.max(t, this.feedMinimum), this.lineHeight = 1, this.textElement = "", this.textPosition = 0, "";
|
|
691
|
+
}
|
|
692
|
+
// insert commands:
|
|
693
|
+
command(t) {
|
|
694
|
+
return "";
|
|
695
|
+
}
|
|
696
|
+
// print image:
|
|
697
|
+
image(t) {
|
|
698
|
+
const i = O(t), r = new DataView(i.buffer), h = r.getUint32(16, !1), n = r.getUint32(20, !1), e = `<image href="data:image/png;base64,${U(i)}" x="0" y="0" width="${h}" height="${n}"/>`, o = Math.floor(this.lineMargin * this.charWidth + (this.lineWidth * this.charWidth - h) * this.lineAlign / 2);
|
|
699
|
+
return this.svgContent += `<g transform="translate(${o},${this.svgHeight})">${e}</g>`, this.svgHeight += n, "";
|
|
700
|
+
}
|
|
701
|
+
// print QR Code:
|
|
702
|
+
qrcode(t, i) {
|
|
703
|
+
const r = _({
|
|
704
|
+
data: t.data,
|
|
705
|
+
ec: t.level == "l" ? H.L : t.level == "m" ? H.M : t.level == "q" ? H.Q : H.H
|
|
706
|
+
}), h = r.width, n = t.cell, e = R(r, {
|
|
707
|
+
moduleSize: n,
|
|
708
|
+
margin: t.quietZone ? 4 : 0,
|
|
709
|
+
output: "path"
|
|
710
|
+
}), o = Math.floor(this.lineMargin * this.charWidth), s = Math.floor(this.svgHeight);
|
|
711
|
+
return this.svgContent += `<g transform="translate(${o},${s})" shape-rendering="crispEdges">
|
|
712
|
+
<path d="${e}" stroke="transparent" fill="black" shape-rendering="crispEdges" />
|
|
713
|
+
</g>`, this.svgHeight += h * n + (t.quietZone ? 8 : 0), "";
|
|
714
|
+
}
|
|
715
|
+
// print barcode:
|
|
716
|
+
barcode(t, i) {
|
|
717
|
+
const r = nt(t), h = r.height;
|
|
718
|
+
if (h !== void 0 && "length" in r && r.length !== void 0 && r.widths) {
|
|
719
|
+
const n = r.length, e = h + (r.hri ? this.charWidth * 2 + 2 : 0);
|
|
720
|
+
let o = '<path d="';
|
|
721
|
+
if (r.widths.reduce((c, l, b) => (b % 2 === 1 && (o += `M${c},0h${l}v${h}h${-l}z`), c + l), 0), o += '" fill="#000"/>', r.hri && r.text) {
|
|
722
|
+
const c = Math.floor((n - (this.measureText(r.text, i) - 1) * this.charWidth) / 2), l = this.arrayFrom(r.text, i).reduce((b, g, d) => b + `<tspan x="${c + this.charWidth * d}">${g.replace(/[ &<>]/g, (u) => ({ " ": " ", "&": "&", "<": "<", ">": ">" })[u] || u)}</tspan>`, "");
|
|
723
|
+
o += `<text y="${e}">${l}</text>`;
|
|
724
|
+
}
|
|
725
|
+
const s = Math.floor(this.lineMargin * this.charWidth + (this.lineWidth * this.charWidth - n) * this.lineAlign / 2);
|
|
726
|
+
this.svgContent += `<g transform="translate(${s},${this.svgHeight})">${o}</g>`, this.svgHeight += e;
|
|
727
|
+
}
|
|
728
|
+
return "";
|
|
729
|
+
}
|
|
730
|
+
calculatedWidth() {
|
|
731
|
+
return this.svgWidth;
|
|
732
|
+
}
|
|
733
|
+
calculatedHeight() {
|
|
734
|
+
return this.svgHeight + this.fontSize / 2;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
const z = {
|
|
738
|
+
base: new F(),
|
|
739
|
+
svg: new E()
|
|
740
|
+
};
|
|
741
|
+
function rt(a) {
|
|
742
|
+
const t = Object.assign({}, a), i = t.encoding && /^(cp(437|85[28]|86[0356]|1252|93[26]|949|950)|multilingual|shiftjis|gb18030|ksc5601|big5|tis620)$/.test(t.encoding) ? t.encoding : "cp437", r = typeof t.target == "string" ? t.target : void 0, n = (typeof t.target == "object" && t.target !== null ? t.target : void 0) || (r && r in z ? z[r] : z.svg);
|
|
743
|
+
let e;
|
|
744
|
+
return n instanceof E || n instanceof F ? e = n : e = new E(), {
|
|
745
|
+
cpl: t.cpl || 48,
|
|
746
|
+
charWidth: t.charWidth || 12,
|
|
747
|
+
encoding: i,
|
|
748
|
+
spacing: !!t.spacing,
|
|
749
|
+
margin: t.margin || 0,
|
|
750
|
+
marginRight: t.marginRight || 0,
|
|
751
|
+
target: e
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
function at(a, t) {
|
|
755
|
+
const i = a.replace(/^[\t ]+|[\t ]+$/g, "").replace(/\\[\\{|}]/g, (r) => "\\x" + r.charCodeAt(1).toString(16)).replace(/^[^|]*[^\t |]\|/, " $&").replace(/\|[^\t |][^|]*$/, "$& ").replace(/^\|(.*)$/, "$1").replace(/^(.*)\|$/, "$1").split("|").map((r, h, n) => {
|
|
756
|
+
let e = {
|
|
757
|
+
align: 1,
|
|
758
|
+
wrap: t.wrap,
|
|
759
|
+
border: t.border,
|
|
760
|
+
width: 0,
|
|
761
|
+
alignment: t.align
|
|
762
|
+
};
|
|
763
|
+
const o = r.replace(/^[\t ]+|[\t ]+$/g, "");
|
|
764
|
+
if (e.align = 1 + Number(/^[\t ]/.test(r)) - Number(/[\t ]$/.test(r)), /^\{[^{}]*\}$/.test(o)) {
|
|
765
|
+
if (e.property = o.slice(1, -1).replace(/\\;/g, "\\x3b").split(";").reduce((s, c) => {
|
|
766
|
+
const l = { a: "align", b: "border", c: "code", i: "image", o: "option", t: "text", w: "width", x: "command", _: "comment" };
|
|
767
|
+
return /^[\t ]*$/.test(c) || c.replace(
|
|
768
|
+
/^[\t ]*([A-Za-z_]\w*)[\t ]*:[\t ]*([^\t ].*?)[\t ]*$/,
|
|
769
|
+
(g, d, u) => {
|
|
770
|
+
const p = d.replace(/^[abciotwx_]$/, (v) => l[v] || v);
|
|
771
|
+
return s[p] = P(u.replace(/\\n/g, `
|
|
772
|
+
`)), g;
|
|
773
|
+
}
|
|
774
|
+
) === c && (e.error = o), s;
|
|
775
|
+
}, {}), n.length === 1) {
|
|
776
|
+
if ("text" in e.property && e.property.text) {
|
|
777
|
+
const s = e.property.text.toLowerCase();
|
|
778
|
+
t.wrap = !/^nowrap$/.test(s);
|
|
779
|
+
}
|
|
780
|
+
if ("border" in e.property && e.property.border) {
|
|
781
|
+
const s = e.property.border.toLowerCase(), c = { line: -1, space: 1, none: 0 }, l = t.border;
|
|
782
|
+
t.border = /^(line|space|none)$/.test(s) ? c[s] ?? 1 : /^\d+$/.test(s) && Number(s) <= 2 ? Number(s) : 1, l >= 0 && t.border < 0 && (e.vr = "+"), l < 0 && t.border >= 0 && (e.vr = "-");
|
|
783
|
+
}
|
|
784
|
+
if ("width" in e.property && e.property.width) {
|
|
785
|
+
const s = e.property.width.toLowerCase().split(/[\t ]+|,/);
|
|
786
|
+
t.width = s.find((c) => /^auto$/.test(c)) ? [] : s.map((c) => /^\*$/.test(c) ? -1 : /^\d+$/.test(c) ? Number(c) : 0);
|
|
787
|
+
}
|
|
788
|
+
if ("align" in e.property && e.property.align) {
|
|
789
|
+
const s = e.property.align.toLowerCase(), c = { left: 0, center: 1, right: 2 };
|
|
790
|
+
t.align = /^(left|center|right)$/.test(s) ? c[s] ?? 1 : 1;
|
|
791
|
+
}
|
|
792
|
+
if ("option" in e.property && e.property.option) {
|
|
793
|
+
const s = e.property.option.toLowerCase().split(/[\t ]+|,/);
|
|
794
|
+
t.option = {
|
|
795
|
+
type: s.find((c) => /^(upc|ean|jan|code39|itf|codabar|nw7|code93|code128|qrcode)$/.test(c)) || "code128",
|
|
796
|
+
width: Number(s.find((c) => /^\d+$/.test(c) && Number(c) >= 2 && Number(c) <= 4) || "2"),
|
|
797
|
+
height: Number(s.find((c) => /^\d+$/.test(c) && Number(c) >= 24 && Number(c) <= 240) || "72"),
|
|
798
|
+
hri: !!s.find((c) => /^hri$/.test(c)),
|
|
799
|
+
cell: Number(s.find((c) => /^\d+$/.test(c) && Number(c) >= 3 && Number(c) <= 8) || "3"),
|
|
800
|
+
level: s.find((c) => /^[lmqh]$/.test(c)) || "l",
|
|
801
|
+
quietZone: !1
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
if ("code" in e.property && e.property.code && (t.option.type === "qrcode" ? e.code = Object.assign({ data: e.property.code, type: "qrcode" }, t.option) : e.code = Object.assign({ data: e.property.code, type: t.option.type }, t.option)), "image" in e.property && e.property.image) {
|
|
805
|
+
const s = e.property.image.replace(/=.*|[^A-Za-z0-9+/]/g, "");
|
|
806
|
+
switch (s.length % 4) {
|
|
807
|
+
case 1:
|
|
808
|
+
e.image = s.slice(0, -1);
|
|
809
|
+
break;
|
|
810
|
+
case 2:
|
|
811
|
+
e.image = s + "==";
|
|
812
|
+
break;
|
|
813
|
+
case 3:
|
|
814
|
+
e.image = s + "=";
|
|
815
|
+
break;
|
|
816
|
+
default:
|
|
817
|
+
e.image = s;
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
"command" in e.property && e.property.command && (e.command = e.property.command), "comment" in e.property && e.property.comment && (e.comment = e.property.comment);
|
|
822
|
+
}
|
|
823
|
+
} else if (/[{}]/.test(o))
|
|
824
|
+
e.error = o;
|
|
825
|
+
else if (n.length === 1 && /^-+$|^=+$/.test(o)) {
|
|
826
|
+
const s = o.slice(-1);
|
|
827
|
+
e.hr = s === "-" || s === "=" ? s : void 0;
|
|
828
|
+
} else
|
|
829
|
+
e.text = o.replace(/[\x00-\x1f\x7f]|\\x[01][\dA-Fa-f]|\\x7[Ff]/g, "").replace(/\\[-=_"`^~]/g, (s) => "\\x" + s.charCodeAt(1).toString(16)).replace(/\\n/g, `
|
|
830
|
+
`).replace(/~/g, " ").split(/([_"`\n]|\^+)/).map((s) => P(s));
|
|
831
|
+
if (e.wrap = t.wrap, e.border = t.border, t.width.length === 0)
|
|
832
|
+
e.width = -1;
|
|
833
|
+
else if ("text" in e)
|
|
834
|
+
e.width = h < t.width.length ? t.width[h] ?? 0 : 0;
|
|
835
|
+
else if (t.width.find((s) => s < 0))
|
|
836
|
+
e.width = -1;
|
|
837
|
+
else {
|
|
838
|
+
const s = t.width.filter((c) => c > 0);
|
|
839
|
+
e.width = s.length > 0 ? s.reduce((c, l) => c + l, e.border < 0 ? s.length + 1 : (s.length - 1) * e.border) : 0;
|
|
840
|
+
}
|
|
841
|
+
return e.alignment = t.align, e;
|
|
842
|
+
});
|
|
843
|
+
if (i.every((r) => "text" in r) && t.width.length > 0)
|
|
844
|
+
for (; i.length < t.width.length; )
|
|
845
|
+
i.push({ align: 1, text: [""], wrap: t.wrap, border: t.border, width: t.width[i.length] ?? 0, alignment: t.align });
|
|
846
|
+
return i;
|
|
847
|
+
}
|
|
848
|
+
function P(a) {
|
|
849
|
+
return a.replace(/\\$|\\x(.?$|[^\dA-Fa-f].|.[^\dA-Fa-f])/g, "").replace(/\\[^x]/g, "").replace(/\\x([\dA-Fa-f]{2})/g, (t, i) => String.fromCharCode(parseInt(i, 16)));
|
|
850
|
+
}
|
|
851
|
+
function st(a, t) {
|
|
852
|
+
const i = [];
|
|
853
|
+
let r = a.width, h = 1, n = [], e = !1, o = !1, s = !1, c = 0;
|
|
854
|
+
return a.text.forEach((l, b) => {
|
|
855
|
+
if (b % 2 === 0) {
|
|
856
|
+
let g = t.target.arrayFrom(l, t.encoding);
|
|
857
|
+
for (; g.length > 0; ) {
|
|
858
|
+
let d = 0, u = 0;
|
|
859
|
+
for (; u < g.length && (d = t.target.measureText(g[u] ?? "", t.encoding) * (c < 2 ? c + 1 : c - 1), !(d > r)); )
|
|
860
|
+
r -= d, d = 0, u++;
|
|
861
|
+
if (u > 0 && (n.push((e ? "1" : "0") + (o ? "1" : "0") + (s ? "1" : "0") + c), n.push(g.slice(0, u).join("")), h = Math.max(h, c < 3 ? c : c - 1), g = g.slice(u)), d > a.width) {
|
|
862
|
+
g = g.slice(1);
|
|
863
|
+
continue;
|
|
864
|
+
}
|
|
865
|
+
(d > r || r === 0) && (i.push({ data: n, margin: r * a.align / 2, height: h }), r = a.width, n = [], h = 1);
|
|
866
|
+
}
|
|
867
|
+
} else
|
|
868
|
+
switch (l) {
|
|
869
|
+
case `
|
|
870
|
+
`:
|
|
871
|
+
i.push({ data: n, margin: r * a.align / 2, height: h }), r = a.width, n = [], h = 1;
|
|
872
|
+
break;
|
|
873
|
+
case "_":
|
|
874
|
+
e = !e;
|
|
875
|
+
break;
|
|
876
|
+
case '"':
|
|
877
|
+
o = !o;
|
|
878
|
+
break;
|
|
879
|
+
case "`":
|
|
880
|
+
s = !s;
|
|
881
|
+
break;
|
|
882
|
+
default:
|
|
883
|
+
const g = Math.min(l.length, 7);
|
|
884
|
+
c = c === g ? 0 : g;
|
|
885
|
+
break;
|
|
886
|
+
}
|
|
887
|
+
}), n.length > 0 && i.push({ data: n, margin: r * a.align / 2, height: h }), i;
|
|
888
|
+
}
|
|
889
|
+
function ht(a, t, i) {
|
|
890
|
+
const r = [], h = a.every((p) => "text" in p), n = a[0] ?? { align: 1, text: [""], wrap: !0, border: 0, width: 0, alignment: 1 };
|
|
891
|
+
let e = a.filter((p) => p.width !== 0);
|
|
892
|
+
h && (e = e.slice(0, Math.floor(n.border < 0 ? (t.cpl - 1) / 2 : (t.cpl + n.border) / (n.border + 1))));
|
|
893
|
+
const o = e.filter((p) => p.width > 0), s = e.filter((p) => p.width < 0);
|
|
894
|
+
let c = o.reduce((p, v) => p + v.width, 0), l = t.cpl - c;
|
|
895
|
+
h && e.length > 0 && (l -= n.border < 0 ? e.length + 1 : (e.length - 1) * n.border);
|
|
896
|
+
const b = s.length;
|
|
897
|
+
for (; b > l; ) {
|
|
898
|
+
const p = o.reduce((v, C) => v.width > C.width ? v : C);
|
|
899
|
+
p.width--, l++;
|
|
900
|
+
}
|
|
901
|
+
b > 0 && (s.forEach((p, v) => p.width = Math.floor((l + v) / b)), l = 0);
|
|
902
|
+
const g = Math.floor(l * n.alignment / 2), d = t.cpl - l, u = l - g;
|
|
903
|
+
if (h) {
|
|
904
|
+
const p = e.map((w) => st(w, t)), v = e.map((w) => w.width);
|
|
905
|
+
switch (i.line) {
|
|
906
|
+
case "ready":
|
|
907
|
+
r.push(t.target.normal() + t.target.area(g, d, u) + t.target.align(0) + t.target.vrstart(v) + t.target.vrlf(!0)), i.line = "running";
|
|
908
|
+
break;
|
|
909
|
+
case "horizontal":
|
|
910
|
+
const w = g - i.rules.left, $ = d - i.rules.width, A = Math.min(g, i.rules.left), k = Math.min(u, i.rules.right);
|
|
911
|
+
r.push(t.target.normal() + t.target.area(A, t.cpl - A - k, k) + t.target.align(0) + t.target.vrhr(i.rules.widths, v, w, w + $) + t.target.lf()), i.line = "running";
|
|
912
|
+
break;
|
|
913
|
+
}
|
|
914
|
+
i.rules = { left: g, width: d, right: u, widths: v };
|
|
915
|
+
const C = n.wrap ? p.reduce((w, $) => Math.max(w, $.length), 1) : 1;
|
|
916
|
+
for (let w = 0; w < C; w++) {
|
|
917
|
+
let $ = t.target.normal() + t.target.area(g, d, u) + t.target.align(0), A = 0;
|
|
918
|
+
if (i.line === "running") {
|
|
919
|
+
const k = p.reduce((W, x) => w < x.length ? Math.max(W, x[w]?.height ?? 1) : W, 1);
|
|
920
|
+
$ += t.target.normal() + t.target.absolute(A++) + t.target.vr(v, k);
|
|
921
|
+
}
|
|
922
|
+
p.forEach((k, W) => {
|
|
923
|
+
if ($ += t.target.absolute(A), w < k.length) {
|
|
924
|
+
$ += t.target.relative(k[w]?.margin ?? 0);
|
|
925
|
+
const x = k[w]?.data ?? [];
|
|
926
|
+
for (let M = 0; M < x.length; M += 2) {
|
|
927
|
+
const L = Number(String(x[M])[0]), T = Number(String(x[M])[1]), B = Number(String(x[M])[2]), j = Number(String(x[M])[3]);
|
|
928
|
+
$ += t.target.normal(), L && ($ += t.target.ul()), T && ($ += t.target.em()), B && ($ += t.target.iv()), j && ($ += t.target.wh(j)), $ += t.target.text(String(x[M + 1]), t.encoding);
|
|
929
|
+
}
|
|
930
|
+
} else
|
|
931
|
+
$ += t.target.normal() + t.target.text(" ", t.encoding);
|
|
932
|
+
A += (e[W]?.width ?? 0) + Math.abs(n.border);
|
|
933
|
+
}), $ += t.target.lf(), r.push($);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
if ("hr" in n)
|
|
937
|
+
if (n.hr === "=")
|
|
938
|
+
switch (i.line) {
|
|
939
|
+
case "running":
|
|
940
|
+
case "horizontal":
|
|
941
|
+
r.push(t.target.normal() + t.target.area(i.rules.left, i.rules.width, i.rules.right) + t.target.align(0) + t.target.vrstop(i.rules.widths) + t.target.vrlf(!1)), r.push(t.target.cut()), i.line = "ready";
|
|
942
|
+
break;
|
|
943
|
+
default:
|
|
944
|
+
r.push(t.target.cut());
|
|
945
|
+
break;
|
|
946
|
+
}
|
|
947
|
+
else
|
|
948
|
+
switch (i.line) {
|
|
949
|
+
case "waiting":
|
|
950
|
+
r.push(t.target.normal() + t.target.area(g, d, u) + t.target.align(0) + t.target.hr(d) + t.target.lf());
|
|
951
|
+
break;
|
|
952
|
+
case "running":
|
|
953
|
+
i.line = "horizontal";
|
|
954
|
+
break;
|
|
955
|
+
}
|
|
956
|
+
if ("vr" in n)
|
|
957
|
+
if (n.vr === "+")
|
|
958
|
+
i.line = "ready";
|
|
959
|
+
else
|
|
960
|
+
switch (i.line) {
|
|
961
|
+
case "ready":
|
|
962
|
+
i.line = "waiting";
|
|
963
|
+
break;
|
|
964
|
+
case "running":
|
|
965
|
+
case "horizontal":
|
|
966
|
+
r.push(t.target.normal() + t.target.area(i.rules.left, i.rules.width, i.rules.right) + t.target.align(0) + t.target.vrstop(i.rules.widths) + t.target.vrlf(!1)), i.line = "waiting";
|
|
967
|
+
break;
|
|
968
|
+
}
|
|
969
|
+
return "image" in n && n.image && r.push(t.target.normal() + t.target.area(g, d, u) + t.target.align(n.align) + t.target.image(n.image)), "code" in n && n.code && (n.code.type === "qrcode" ? r.push(t.target.normal() + t.target.area(g, d, u) + t.target.align(n.align) + t.target.qrcode(n.code, t.encoding)) : r.push(t.target.normal() + t.target.area(g, d, u) + t.target.align(n.align) + t.target.barcode(n.code, t.encoding))), "command" in n && n.command && r.push(t.target.normal() + t.target.area(g, d, u) + t.target.align(n.align) + t.target.command(n.command)), r.join("");
|
|
970
|
+
}
|
|
971
|
+
function gt(a, t) {
|
|
972
|
+
const i = {
|
|
973
|
+
wrap: !0,
|
|
974
|
+
border: 1,
|
|
975
|
+
width: [],
|
|
976
|
+
align: 1,
|
|
977
|
+
option: { type: "code128", width: 2, height: 72, hri: !1, cell: 3, level: "l", quietZone: !1 },
|
|
978
|
+
line: "waiting",
|
|
979
|
+
rules: { left: 0, width: 0, right: 0, widths: [] }
|
|
980
|
+
}, r = rt(t);
|
|
981
|
+
let h = r.target.open(r);
|
|
982
|
+
a[0] === "\uFEFF" && (a = a.slice(1));
|
|
983
|
+
const n = a.normalize().split(/\n|\r\n|\r/).map((e) => ht(at(e, i), r, i));
|
|
984
|
+
switch (i.line) {
|
|
985
|
+
case "ready":
|
|
986
|
+
i.line = "waiting";
|
|
987
|
+
break;
|
|
988
|
+
case "running":
|
|
989
|
+
case "horizontal":
|
|
990
|
+
n.push(r.target.normal() + r.target.area(i.rules.left, i.rules.width, i.rules.right) + r.target.align(0) + r.target.vrstop(i.rules.widths) + r.target.vrlf(!1)), i.line = "waiting";
|
|
991
|
+
break;
|
|
992
|
+
}
|
|
993
|
+
return h += n.join(""), h += r.target.close(), {
|
|
994
|
+
svg: h,
|
|
995
|
+
width: r.target.calculatedWidth(),
|
|
996
|
+
height: r.target.calculatedHeight()
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
export {
|
|
1000
|
+
F as BaseTarget,
|
|
1001
|
+
E as SvgTarget,
|
|
1002
|
+
gt as transform
|
|
1003
|
+
};
|