@andy-defer/react-pdf-builder 1.1.0

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.
@@ -0,0 +1,1629 @@
1
+ import { jsx as v, jsxs as N } from "react/jsx-runtime";
2
+ import P, { createContext as be, useContext as pe, useState as _, forwardRef as ve, useRef as ee, useImperativeHandle as Me, useEffect as Re } from "react";
3
+ import Q from "html2canvas";
4
+ import Z from "jspdf";
5
+ const J = {
6
+ format: "a4",
7
+ orientation: "portrait",
8
+ scale: 2,
9
+ margin: 40
10
+ }, te = be(void 0);
11
+ function He({
12
+ children: l,
13
+ initialConfig: s = J
14
+ }) {
15
+ const [a, c] = _({
16
+ ...J,
17
+ ...s
18
+ }), [u, i] = _(!1), [w, g] = _(null), e = (r) => {
19
+ c((t) => ({ ...t, ...r }));
20
+ };
21
+ return /* @__PURE__ */ v(
22
+ te.Provider,
23
+ {
24
+ value: {
25
+ config: a,
26
+ setConfig: e,
27
+ loading: u,
28
+ setLoading: i,
29
+ error: w,
30
+ setError: g
31
+ },
32
+ children: l
33
+ }
34
+ );
35
+ }
36
+ function re() {
37
+ const l = pe(te);
38
+ if (!l)
39
+ throw new Error("usePDFContext must be used within a PDFProvider");
40
+ return l;
41
+ }
42
+ let H = class {
43
+ async generatePage(s, a = {}) {
44
+ const {
45
+ scale: c = 2,
46
+ backgroundColor: u = "#ffffff",
47
+ margin: i = 10,
48
+ format: w = "a4",
49
+ orientation: g = "portrait"
50
+ } = a, e = document.createElement("div");
51
+ e.style.position = "absolute", e.style.left = "-9999px", e.style.top = "0", e.style.width = "794px", e.style.background = "#ffffff", e.style.padding = "40px", e.appendChild(s.cloneNode(!0)), document.body.appendChild(e);
52
+ try {
53
+ await new Promise((b) => setTimeout(b, 200));
54
+ const r = await Q(e, {
55
+ scale: c,
56
+ backgroundColor: u,
57
+ logging: !1,
58
+ useCORS: !0,
59
+ allowTaint: !0,
60
+ width: 794,
61
+ height: e.scrollHeight,
62
+ windowWidth: 794,
63
+ windowHeight: e.scrollHeight
64
+ });
65
+ document.body.removeChild(e);
66
+ const t = new Z({
67
+ orientation: g,
68
+ unit: "mm",
69
+ format: w
70
+ }), n = t.internal.pageSize.getWidth(), o = t.internal.pageSize.getHeight(), f = n - i * 2, d = o - i * 2, h = r.toDataURL("image/png"), m = f, M = r.height * m / r.width;
71
+ if (M > d) {
72
+ const b = d / M, C = m * b, y = i + (f - C) / 2;
73
+ t.addImage(h, "PNG", y, i, C, d);
74
+ } else {
75
+ const b = i + (d - M) / 2;
76
+ t.addImage(h, "PNG", i, b, m, M);
77
+ }
78
+ return t;
79
+ } catch (r) {
80
+ throw document.body.removeChild(e), new Error(`PDF generation failed: ${r}`);
81
+ }
82
+ }
83
+ async generateMultiplePages(s, a = {}) {
84
+ const {
85
+ filename: c = "document.pdf",
86
+ format: u = "a4",
87
+ orientation: i = "portrait",
88
+ scale: w = 2,
89
+ margin: g = 10,
90
+ backgroundColor: e = "#ffffff"
91
+ } = a, r = new Z({
92
+ orientation: i,
93
+ unit: "mm",
94
+ format: u
95
+ }), t = r.internal.pageSize.getWidth(), n = r.internal.pageSize.getHeight(), o = t - g * 2, f = n - g * 2;
96
+ for (let d = 0; d < s.length; d++) {
97
+ const h = document.createElement("div");
98
+ h.style.position = "absolute", h.style.left = "-9999px", h.style.top = "0", h.style.width = "794px", h.style.background = "#ffffff", h.style.padding = "40px", h.appendChild(s[d].cloneNode(!0)), document.body.appendChild(h);
99
+ try {
100
+ await new Promise((y) => setTimeout(y, 200));
101
+ const m = await Q(h, {
102
+ scale: w,
103
+ backgroundColor: e,
104
+ logging: !1,
105
+ useCORS: !0,
106
+ allowTaint: !0,
107
+ width: 794,
108
+ height: h.scrollHeight,
109
+ windowWidth: 794,
110
+ windowHeight: h.scrollHeight
111
+ });
112
+ document.body.removeChild(h), d > 0 && r.addPage();
113
+ const M = m.toDataURL("image/png"), b = o, C = m.height * b / m.width;
114
+ if (C > f) {
115
+ const y = f / C, p = b * y, x = g + (o - p) / 2;
116
+ r.addImage(M, "PNG", x, g, p, f);
117
+ } else {
118
+ const y = g + (f - C) / 2;
119
+ r.addImage(M, "PNG", g, y, b, C);
120
+ }
121
+ } catch (m) {
122
+ throw document.body.removeChild(h), m;
123
+ }
124
+ }
125
+ return r.save(c), r;
126
+ }
127
+ async toBase64(s, a = {}) {
128
+ const {
129
+ scale: c = 2,
130
+ backgroundColor: u = "#ffffff"
131
+ } = a, i = document.createElement("div");
132
+ i.innerHTML = s, i.style.position = "absolute", i.style.left = "-9999px", i.style.top = "0", i.style.width = "794px", i.style.background = "#ffffff", i.style.padding = "40px", document.body.appendChild(i);
133
+ try {
134
+ await new Promise((g) => setTimeout(g, 300));
135
+ const w = await Q(i, {
136
+ scale: c,
137
+ backgroundColor: u,
138
+ logging: !1,
139
+ useCORS: !0,
140
+ allowTaint: !0,
141
+ width: 794,
142
+ height: i.scrollHeight,
143
+ windowWidth: 794,
144
+ windowHeight: i.scrollHeight
145
+ });
146
+ return document.body.removeChild(i), w.toDataURL("image/png");
147
+ } catch (w) {
148
+ throw document.body.removeChild(i), new Error(`Base64 conversion failed: ${w}`);
149
+ }
150
+ }
151
+ };
152
+ const Pe = ve(
153
+ ({
154
+ children: l,
155
+ format: s = "a4",
156
+ orientation: a = "portrait",
157
+ scale: c = 2,
158
+ margin: u = 40,
159
+ border: i = !0,
160
+ borderColor: w = "#e5e7eb",
161
+ borderWidth: g = 2,
162
+ borderRadius: e = 12,
163
+ className: r = "",
164
+ style: t = {},
165
+ onGenerate: n,
166
+ onDownload: o
167
+ }, f) => {
168
+ const d = ee(null), { setLoading: h, setError: m } = re(), M = async (y = "document.pdf") => {
169
+ try {
170
+ h(!0), m(null);
171
+ const p = d.current;
172
+ if (!p)
173
+ throw new Error("Container not found");
174
+ const x = new H(), S = p.querySelectorAll('[style*="page-break-after: always"]');
175
+ let I = null;
176
+ if (S.length > 0) {
177
+ const D = [];
178
+ S.forEach((k) => {
179
+ D.push(k);
180
+ }), I = await x.generateMultiplePages(D, {
181
+ filename: y,
182
+ format: s,
183
+ orientation: a,
184
+ scale: c,
185
+ margin: 10
186
+ });
187
+ } else
188
+ I = await x.generatePage(p, {
189
+ filename: y,
190
+ format: s,
191
+ orientation: a,
192
+ scale: c,
193
+ margin: 10
194
+ });
195
+ return o && o(I), h(!1), I;
196
+ } catch (p) {
197
+ throw m(p instanceof Error ? p.message : "Unknown error"), h(!1), p;
198
+ }
199
+ }, b = async () => {
200
+ try {
201
+ h(!0), m(null);
202
+ const y = d.current;
203
+ if (!y)
204
+ throw new Error("Container not found");
205
+ const p = new H(), x = y.outerHTML, S = await p.toBase64(x, {
206
+ scale: c
207
+ });
208
+ return n && n(S), h(!1), S;
209
+ } catch (y) {
210
+ throw m(y instanceof Error ? y.message : "Unknown error"), h(!1), y;
211
+ }
212
+ };
213
+ Me(f, () => ({
214
+ generatePDF: M,
215
+ generateBase64: b
216
+ }));
217
+ const C = i ? {
218
+ border: `${g}px solid ${w}`,
219
+ borderRadius: `${e}px`
220
+ } : {};
221
+ return /* @__PURE__ */ v(
222
+ "div",
223
+ {
224
+ ref: d,
225
+ className: `pdf-container ${r}`,
226
+ style: {
227
+ backgroundColor: "#ffffff",
228
+ width: "794px",
229
+ padding: `${u}px`,
230
+ margin: "0 auto",
231
+ ...C,
232
+ ...t
233
+ },
234
+ children: l
235
+ }
236
+ );
237
+ }
238
+ );
239
+ Pe.displayName = "PDFGenerator";
240
+ function T({
241
+ children: l,
242
+ direction: s = "row",
243
+ gap: a = 4,
244
+ align: c = "stretch",
245
+ justify: u = "start",
246
+ wrap: i = !1,
247
+ className: w = "",
248
+ style: g = {}
249
+ }) {
250
+ const e = `gap-${a}`, r = {
251
+ start: "items-start",
252
+ center: "items-center",
253
+ end: "items-end",
254
+ stretch: "items-stretch"
255
+ }[c], t = {
256
+ start: "justify-start",
257
+ center: "justify-center",
258
+ end: "justify-end",
259
+ between: "justify-between",
260
+ around: "justify-around",
261
+ evenly: "justify-evenly"
262
+ }[u];
263
+ return /* @__PURE__ */ v(
264
+ "div",
265
+ {
266
+ className: `flex ${s === "row" ? "flex-row" : "flex-col"} ${e} ${r} ${t} ${i ? "flex-wrap" : ""} ${w}`,
267
+ style: g,
268
+ children: l
269
+ }
270
+ );
271
+ }
272
+ function _e({
273
+ children: l,
274
+ columns: s = 2,
275
+ gap: a = 4,
276
+ className: c = "",
277
+ style: u = {}
278
+ }) {
279
+ const i = `grid-cols-${s}`, w = `gap-${a}`;
280
+ return /* @__PURE__ */ v(
281
+ "div",
282
+ {
283
+ className: `grid ${i} ${w} ${c}`,
284
+ style: u,
285
+ children: l
286
+ }
287
+ );
288
+ }
289
+ function xe({
290
+ children: l,
291
+ padding: s = 4,
292
+ margin: a = 0,
293
+ border: c = !1,
294
+ rounded: u = !1,
295
+ shadow: i = "none",
296
+ className: w = "",
297
+ style: g = {}
298
+ }) {
299
+ const e = `p-${s}`, r = a > 0 ? `m-${a}` : "", t = c ? "border border-gray-200" : "", n = u ? "rounded-lg" : "", o = i !== "none" ? `shadow-${i}` : "";
300
+ return /* @__PURE__ */ v(
301
+ "div",
302
+ {
303
+ className: `${e} ${r} ${t} ${n} ${o} ${w}`,
304
+ style: g,
305
+ children: l
306
+ }
307
+ );
308
+ }
309
+ function $({
310
+ children: l,
311
+ variant: s = "body",
312
+ color: a = "primary",
313
+ align: c = "left",
314
+ bold: u = !1,
315
+ truncate: i = !1,
316
+ className: w = "",
317
+ style: g = {}
318
+ }) {
319
+ const e = {
320
+ h1: "text-4xl font-bold",
321
+ h2: "text-3xl font-bold",
322
+ h3: "text-2xl font-bold",
323
+ h4: "text-xl font-bold",
324
+ h5: "text-lg font-bold",
325
+ body: "text-base",
326
+ small: "text-sm"
327
+ }[s], r = {
328
+ primary: "text-gray-900",
329
+ secondary: "text-gray-700",
330
+ muted: "text-gray-500",
331
+ danger: "text-red-600",
332
+ success: "text-green-600",
333
+ warning: "text-yellow-600"
334
+ }[a], t = {
335
+ left: "text-left",
336
+ center: "text-center",
337
+ right: "text-right",
338
+ justify: "text-justify"
339
+ }[c];
340
+ return /* @__PURE__ */ v(
341
+ "div",
342
+ {
343
+ className: `${e} ${r} ${t} ${u ? "font-bold" : ""} ${i ? "truncate" : ""} ${w}`,
344
+ style: g,
345
+ children: l
346
+ }
347
+ );
348
+ }
349
+ function Qe({
350
+ children: l,
351
+ level: s = 1,
352
+ color: a = "primary",
353
+ className: c = "",
354
+ style: u = {}
355
+ }) {
356
+ const i = {
357
+ 1: "text-4xl font-bold",
358
+ 2: "text-3xl font-bold",
359
+ 3: "text-2xl font-bold",
360
+ 4: "text-xl font-bold",
361
+ 5: "text-lg font-bold",
362
+ 6: "text-base font-bold"
363
+ }[s], w = {
364
+ primary: "text-gray-900",
365
+ secondary: "text-gray-700",
366
+ muted: "text-gray-500"
367
+ }[a], g = `h${s}`;
368
+ return P.createElement(
369
+ g,
370
+ { className: `${i} ${w} ${c}`, style: u },
371
+ l
372
+ );
373
+ }
374
+ function je({
375
+ src: l,
376
+ alt: s = "",
377
+ width: a,
378
+ height: c,
379
+ fit: u = "contain",
380
+ rounded: i = !1,
381
+ fallback: w,
382
+ className: g = "",
383
+ style: e = {}
384
+ }) {
385
+ const r = {
386
+ contain: "object-contain",
387
+ cover: "object-cover",
388
+ fill: "object-fill",
389
+ none: "object-none"
390
+ }[u], t = i === "full" ? "rounded-full" : i ? "rounded-lg" : "", [n, o] = P.useState(!1), f = () => {
391
+ w && !n && o(!0);
392
+ };
393
+ return /* @__PURE__ */ v(
394
+ "img",
395
+ {
396
+ src: n && w ? w : l,
397
+ alt: s,
398
+ width: a,
399
+ height: c,
400
+ className: `${r} ${t} ${g}`,
401
+ style: e,
402
+ onError: f
403
+ }
404
+ );
405
+ }
406
+ function Ge({
407
+ columns: l,
408
+ data: s,
409
+ bordered: a = !1,
410
+ striped: c = !1,
411
+ hoverable: u = !1,
412
+ className: i = "",
413
+ style: w = {}
414
+ }) {
415
+ const g = a ? "border border-gray-200" : "", e = c ? "even:bg-gray-50" : "", r = u ? "hover:bg-gray-50" : "";
416
+ return /* @__PURE__ */ N(
417
+ "table",
418
+ {
419
+ className: `w-full ${g} ${i}`,
420
+ style: w,
421
+ children: [
422
+ /* @__PURE__ */ v("thead", { children: /* @__PURE__ */ v("tr", { className: "bg-gray-100", children: l.map((t) => /* @__PURE__ */ v(
423
+ "th",
424
+ {
425
+ style: { width: t.width },
426
+ className: `px-4 py-2 text-left text-sm font-semibold text-gray-700 ${t.align === "right" ? "text-right" : t.align === "center" ? "text-center" : ""}`,
427
+ children: t.label
428
+ },
429
+ t.key
430
+ )) }) }),
431
+ /* @__PURE__ */ v("tbody", { children: s.map((t, n) => /* @__PURE__ */ v("tr", { className: `${e} ${r}`, children: l.map((o) => /* @__PURE__ */ v(
432
+ "td",
433
+ {
434
+ className: `px-4 py-2 text-sm ${o.align === "right" ? "text-right" : o.align === "center" ? "text-center" : ""}`,
435
+ children: t[o.key]
436
+ },
437
+ o.key
438
+ )) }, n)) })
439
+ ]
440
+ }
441
+ );
442
+ }
443
+ function We({
444
+ children: l,
445
+ className: s = "",
446
+ style: a = {}
447
+ }) {
448
+ return /* @__PURE__ */ v("tr", { className: s, style: a, children: l });
449
+ }
450
+ function Ye({
451
+ children: l,
452
+ align: s = "left",
453
+ className: a = "",
454
+ style: c = {}
455
+ }) {
456
+ const u = {
457
+ left: "text-left",
458
+ center: "text-center",
459
+ right: "text-right"
460
+ }[s];
461
+ return /* @__PURE__ */ v(
462
+ "td",
463
+ {
464
+ className: `px-4 py-2 ${u} ${a}`,
465
+ style: c,
466
+ children: l
467
+ }
468
+ );
469
+ }
470
+ function Ve({
471
+ children: l,
472
+ variant: s = "default",
473
+ size: a = "md",
474
+ rounded: c = !0,
475
+ className: u = "",
476
+ style: i = {}
477
+ }) {
478
+ const w = {
479
+ success: "bg-green-100 text-green-800",
480
+ warning: "bg-yellow-100 text-yellow-800",
481
+ danger: "bg-red-100 text-red-800",
482
+ info: "bg-blue-100 text-blue-800",
483
+ default: "bg-gray-100 text-gray-800"
484
+ }[s], g = {
485
+ sm: "px-2 py-0.5 text-xs",
486
+ md: "px-3 py-1 text-sm",
487
+ lg: "px-4 py-1.5 text-base"
488
+ }[a];
489
+ return /* @__PURE__ */ v(
490
+ "span",
491
+ {
492
+ className: `inline-block ${w} ${g} ${c ? "rounded-full" : "rounded"} ${u}`,
493
+ style: i,
494
+ children: l
495
+ }
496
+ );
497
+ }
498
+ function Xe({
499
+ variant: l = "solid",
500
+ size: s = 2,
501
+ color: a = "gray-300",
502
+ className: c = "",
503
+ style: u = {}
504
+ }) {
505
+ const i = {
506
+ solid: "border-solid",
507
+ dashed: "border-dashed",
508
+ dotted: "border-dotted"
509
+ }[l], w = `border-${a}`;
510
+ return /* @__PURE__ */ v(
511
+ "hr",
512
+ {
513
+ className: `border-${s} ${i} ${w} ${c}`,
514
+ style: u
515
+ }
516
+ );
517
+ }
518
+ function Ke({
519
+ subtotal: l,
520
+ discount: s = 0,
521
+ tax: a = 0,
522
+ shipping: c = 0,
523
+ total: u,
524
+ currency: i = "€",
525
+ className: w = "",
526
+ style: g = {}
527
+ }) {
528
+ const e = (l - s) * (a / 100);
529
+ return /* @__PURE__ */ v(
530
+ xe,
531
+ {
532
+ className: `bg-gray-50 p-4 rounded-lg ${w}`,
533
+ style: g,
534
+ children: /* @__PURE__ */ N(T, { direction: "column", gap: 2, children: [
535
+ /* @__PURE__ */ N(T, { direction: "row", justify: "between", children: [
536
+ /* @__PURE__ */ v($, { variant: "body", color: "muted", children: "Subtotal" }),
537
+ /* @__PURE__ */ N($, { variant: "body", children: [
538
+ l.toFixed(2),
539
+ " ",
540
+ i
541
+ ] })
542
+ ] }),
543
+ s > 0 && /* @__PURE__ */ N(T, { direction: "row", justify: "between", children: [
544
+ /* @__PURE__ */ v($, { variant: "body", color: "muted", children: "Discount" }),
545
+ /* @__PURE__ */ N($, { variant: "body", color: "danger", children: [
546
+ "-",
547
+ s.toFixed(2),
548
+ " ",
549
+ i
550
+ ] })
551
+ ] }),
552
+ a > 0 && /* @__PURE__ */ N(T, { direction: "row", justify: "between", children: [
553
+ /* @__PURE__ */ N($, { variant: "body", color: "muted", children: [
554
+ "Tax (",
555
+ a,
556
+ "%)"
557
+ ] }),
558
+ /* @__PURE__ */ N($, { variant: "body", children: [
559
+ e.toFixed(2),
560
+ " ",
561
+ i
562
+ ] })
563
+ ] }),
564
+ c > 0 && /* @__PURE__ */ N(T, { direction: "row", justify: "between", children: [
565
+ /* @__PURE__ */ v($, { variant: "body", color: "muted", children: "Shipping" }),
566
+ /* @__PURE__ */ N($, { variant: "body", children: [
567
+ c.toFixed(2),
568
+ " ",
569
+ i
570
+ ] })
571
+ ] }),
572
+ /* @__PURE__ */ v("div", { className: "border-t-2 border-gray-300 pt-2 mt-2", children: /* @__PURE__ */ N(T, { direction: "row", justify: "between", children: [
573
+ /* @__PURE__ */ v($, { variant: "h5", bold: !0, children: "Total" }),
574
+ /* @__PURE__ */ N($, { variant: "h5", bold: !0, children: [
575
+ u.toFixed(2),
576
+ " ",
577
+ i
578
+ ] })
579
+ ] }) })
580
+ ] })
581
+ }
582
+ );
583
+ }
584
+ var Ne = Object.defineProperty, U = Object.getOwnPropertySymbols, ne = Object.prototype.hasOwnProperty, oe = Object.prototype.propertyIsEnumerable, q = (l, s, a) => s in l ? Ne(l, s, { enumerable: !0, configurable: !0, writable: !0, value: a }) : l[s] = a, j = (l, s) => {
585
+ for (var a in s || (s = {}))
586
+ ne.call(s, a) && q(l, a, s[a]);
587
+ if (U)
588
+ for (var a of U(s))
589
+ oe.call(s, a) && q(l, a, s[a]);
590
+ return l;
591
+ }, G = (l, s) => {
592
+ var a = {};
593
+ for (var c in l)
594
+ ne.call(l, c) && s.indexOf(c) < 0 && (a[c] = l[c]);
595
+ if (l != null && U)
596
+ for (var c of U(l))
597
+ s.indexOf(c) < 0 && oe.call(l, c) && (a[c] = l[c]);
598
+ return a;
599
+ };
600
+ /**
601
+ * @license QR Code generator library (TypeScript)
602
+ * Copyright (c) Project Nayuki.
603
+ * SPDX-License-Identifier: MIT
604
+ */
605
+ var O;
606
+ ((l) => {
607
+ const s = class E {
608
+ /*-- Constructor (low level) and fields --*/
609
+ // Creates a new QR Code with the given version number,
610
+ // error correction level, data codeword bytes, and mask number.
611
+ // This is a low-level API that most users should not use directly.
612
+ // A mid-level API is the encodeSegments() function.
613
+ constructor(e, r, t, n) {
614
+ if (this.version = e, this.errorCorrectionLevel = r, this.modules = [], this.isFunction = [], e < E.MIN_VERSION || e > E.MAX_VERSION)
615
+ throw new RangeError("Version value out of range");
616
+ if (n < -1 || n > 7)
617
+ throw new RangeError("Mask value out of range");
618
+ this.size = e * 4 + 17;
619
+ let o = [];
620
+ for (let d = 0; d < this.size; d++)
621
+ o.push(!1);
622
+ for (let d = 0; d < this.size; d++)
623
+ this.modules.push(o.slice()), this.isFunction.push(o.slice());
624
+ this.drawFunctionPatterns();
625
+ const f = this.addEccAndInterleave(t);
626
+ if (this.drawCodewords(f), n == -1) {
627
+ let d = 1e9;
628
+ for (let h = 0; h < 8; h++) {
629
+ this.applyMask(h), this.drawFormatBits(h);
630
+ const m = this.getPenaltyScore();
631
+ m < d && (n = h, d = m), this.applyMask(h);
632
+ }
633
+ }
634
+ u(0 <= n && n <= 7), this.mask = n, this.applyMask(n), this.drawFormatBits(n), this.isFunction = [];
635
+ }
636
+ /*-- Static factory functions (high level) --*/
637
+ // Returns a QR Code representing the given Unicode text string at the given error correction level.
638
+ // As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer
639
+ // Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible
640
+ // QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the
641
+ // ecl argument if it can be done without increasing the version.
642
+ static encodeText(e, r) {
643
+ const t = l.QrSegment.makeSegments(e);
644
+ return E.encodeSegments(t, r);
645
+ }
646
+ // Returns a QR Code representing the given binary data at the given error correction level.
647
+ // This function always encodes using the binary segment mode, not any text mode. The maximum number of
648
+ // bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
649
+ // The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
650
+ static encodeBinary(e, r) {
651
+ const t = l.QrSegment.makeBytes(e);
652
+ return E.encodeSegments([t], r);
653
+ }
654
+ /*-- Static factory functions (mid level) --*/
655
+ // Returns a QR Code representing the given segments with the given encoding parameters.
656
+ // The smallest possible QR Code version within the given range is automatically
657
+ // chosen for the output. Iff boostEcl is true, then the ECC level of the result
658
+ // may be higher than the ecl argument if it can be done without increasing the
659
+ // version. The mask number is either between 0 to 7 (inclusive) to force that
660
+ // mask, or -1 to automatically choose an appropriate mask (which may be slow).
661
+ // This function allows the user to create a custom sequence of segments that switches
662
+ // between modes (such as alphanumeric and byte) to encode text in less space.
663
+ // This is a mid-level API; the high-level API is encodeText() and encodeBinary().
664
+ static encodeSegments(e, r, t = 1, n = 40, o = -1, f = !0) {
665
+ if (!(E.MIN_VERSION <= t && t <= n && n <= E.MAX_VERSION) || o < -1 || o > 7)
666
+ throw new RangeError("Invalid value");
667
+ let d, h;
668
+ for (d = t; ; d++) {
669
+ const C = E.getNumDataCodewords(d, r) * 8, y = w.getTotalBits(e, d);
670
+ if (y <= C) {
671
+ h = y;
672
+ break;
673
+ }
674
+ if (d >= n)
675
+ throw new RangeError("Data too long");
676
+ }
677
+ for (const C of [E.Ecc.MEDIUM, E.Ecc.QUARTILE, E.Ecc.HIGH])
678
+ f && h <= E.getNumDataCodewords(d, C) * 8 && (r = C);
679
+ let m = [];
680
+ for (const C of e) {
681
+ a(C.mode.modeBits, 4, m), a(C.numChars, C.mode.numCharCountBits(d), m);
682
+ for (const y of C.getData())
683
+ m.push(y);
684
+ }
685
+ u(m.length == h);
686
+ const M = E.getNumDataCodewords(d, r) * 8;
687
+ u(m.length <= M), a(0, Math.min(4, M - m.length), m), a(0, (8 - m.length % 8) % 8, m), u(m.length % 8 == 0);
688
+ for (let C = 236; m.length < M; C ^= 253)
689
+ a(C, 8, m);
690
+ let b = [];
691
+ for (; b.length * 8 < m.length; )
692
+ b.push(0);
693
+ return m.forEach((C, y) => b[y >>> 3] |= C << 7 - (y & 7)), new E(d, r, b, o);
694
+ }
695
+ /*-- Accessor methods --*/
696
+ // Returns the color of the module (pixel) at the given coordinates, which is false
697
+ // for light or true for dark. The top left corner has the coordinates (x=0, y=0).
698
+ // If the given coordinates are out of bounds, then false (light) is returned.
699
+ getModule(e, r) {
700
+ return 0 <= e && e < this.size && 0 <= r && r < this.size && this.modules[r][e];
701
+ }
702
+ // Modified to expose modules for easy access
703
+ getModules() {
704
+ return this.modules;
705
+ }
706
+ /*-- Private helper methods for constructor: Drawing function modules --*/
707
+ // Reads this object's version field, and draws and marks all function modules.
708
+ drawFunctionPatterns() {
709
+ for (let t = 0; t < this.size; t++)
710
+ this.setFunctionModule(6, t, t % 2 == 0), this.setFunctionModule(t, 6, t % 2 == 0);
711
+ this.drawFinderPattern(3, 3), this.drawFinderPattern(this.size - 4, 3), this.drawFinderPattern(3, this.size - 4);
712
+ const e = this.getAlignmentPatternPositions(), r = e.length;
713
+ for (let t = 0; t < r; t++)
714
+ for (let n = 0; n < r; n++)
715
+ t == 0 && n == 0 || t == 0 && n == r - 1 || t == r - 1 && n == 0 || this.drawAlignmentPattern(e[t], e[n]);
716
+ this.drawFormatBits(0), this.drawVersion();
717
+ }
718
+ // Draws two copies of the format bits (with its own error correction code)
719
+ // based on the given mask and this object's error correction level field.
720
+ drawFormatBits(e) {
721
+ const r = this.errorCorrectionLevel.formatBits << 3 | e;
722
+ let t = r;
723
+ for (let o = 0; o < 10; o++)
724
+ t = t << 1 ^ (t >>> 9) * 1335;
725
+ const n = (r << 10 | t) ^ 21522;
726
+ u(n >>> 15 == 0);
727
+ for (let o = 0; o <= 5; o++)
728
+ this.setFunctionModule(8, o, c(n, o));
729
+ this.setFunctionModule(8, 7, c(n, 6)), this.setFunctionModule(8, 8, c(n, 7)), this.setFunctionModule(7, 8, c(n, 8));
730
+ for (let o = 9; o < 15; o++)
731
+ this.setFunctionModule(14 - o, 8, c(n, o));
732
+ for (let o = 0; o < 8; o++)
733
+ this.setFunctionModule(this.size - 1 - o, 8, c(n, o));
734
+ for (let o = 8; o < 15; o++)
735
+ this.setFunctionModule(8, this.size - 15 + o, c(n, o));
736
+ this.setFunctionModule(8, this.size - 8, !0);
737
+ }
738
+ // Draws two copies of the version bits (with its own error correction code),
739
+ // based on this object's version field, iff 7 <= version <= 40.
740
+ drawVersion() {
741
+ if (this.version < 7)
742
+ return;
743
+ let e = this.version;
744
+ for (let t = 0; t < 12; t++)
745
+ e = e << 1 ^ (e >>> 11) * 7973;
746
+ const r = this.version << 12 | e;
747
+ u(r >>> 18 == 0);
748
+ for (let t = 0; t < 18; t++) {
749
+ const n = c(r, t), o = this.size - 11 + t % 3, f = Math.floor(t / 3);
750
+ this.setFunctionModule(o, f, n), this.setFunctionModule(f, o, n);
751
+ }
752
+ }
753
+ // Draws a 9*9 finder pattern including the border separator,
754
+ // with the center module at (x, y). Modules can be out of bounds.
755
+ drawFinderPattern(e, r) {
756
+ for (let t = -4; t <= 4; t++)
757
+ for (let n = -4; n <= 4; n++) {
758
+ const o = Math.max(Math.abs(n), Math.abs(t)), f = e + n, d = r + t;
759
+ 0 <= f && f < this.size && 0 <= d && d < this.size && this.setFunctionModule(f, d, o != 2 && o != 4);
760
+ }
761
+ }
762
+ // Draws a 5*5 alignment pattern, with the center module
763
+ // at (x, y). All modules must be in bounds.
764
+ drawAlignmentPattern(e, r) {
765
+ for (let t = -2; t <= 2; t++)
766
+ for (let n = -2; n <= 2; n++)
767
+ this.setFunctionModule(e + n, r + t, Math.max(Math.abs(n), Math.abs(t)) != 1);
768
+ }
769
+ // Sets the color of a module and marks it as a function module.
770
+ // Only used by the constructor. Coordinates must be in bounds.
771
+ setFunctionModule(e, r, t) {
772
+ this.modules[r][e] = t, this.isFunction[r][e] = !0;
773
+ }
774
+ /*-- Private helper methods for constructor: Codewords and masking --*/
775
+ // Returns a new byte string representing the given data with the appropriate error correction
776
+ // codewords appended to it, based on this object's version and error correction level.
777
+ addEccAndInterleave(e) {
778
+ const r = this.version, t = this.errorCorrectionLevel;
779
+ if (e.length != E.getNumDataCodewords(r, t))
780
+ throw new RangeError("Invalid argument");
781
+ const n = E.NUM_ERROR_CORRECTION_BLOCKS[t.ordinal][r], o = E.ECC_CODEWORDS_PER_BLOCK[t.ordinal][r], f = Math.floor(E.getNumRawDataModules(r) / 8), d = n - f % n, h = Math.floor(f / n);
782
+ let m = [];
783
+ const M = E.reedSolomonComputeDivisor(o);
784
+ for (let C = 0, y = 0; C < n; C++) {
785
+ let p = e.slice(y, y + h - o + (C < d ? 0 : 1));
786
+ y += p.length;
787
+ const x = E.reedSolomonComputeRemainder(p, M);
788
+ C < d && p.push(0), m.push(p.concat(x));
789
+ }
790
+ let b = [];
791
+ for (let C = 0; C < m[0].length; C++)
792
+ m.forEach((y, p) => {
793
+ (C != h - o || p >= d) && b.push(y[C]);
794
+ });
795
+ return u(b.length == f), b;
796
+ }
797
+ // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
798
+ // data area of this QR Code. Function modules need to be marked off before this is called.
799
+ drawCodewords(e) {
800
+ if (e.length != Math.floor(E.getNumRawDataModules(this.version) / 8))
801
+ throw new RangeError("Invalid argument");
802
+ let r = 0;
803
+ for (let t = this.size - 1; t >= 1; t -= 2) {
804
+ t == 6 && (t = 5);
805
+ for (let n = 0; n < this.size; n++)
806
+ for (let o = 0; o < 2; o++) {
807
+ const f = t - o, h = (t + 1 & 2) == 0 ? this.size - 1 - n : n;
808
+ !this.isFunction[h][f] && r < e.length * 8 && (this.modules[h][f] = c(e[r >>> 3], 7 - (r & 7)), r++);
809
+ }
810
+ }
811
+ u(r == e.length * 8);
812
+ }
813
+ // XORs the codeword modules in this QR Code with the given mask pattern.
814
+ // The function modules must be marked and the codeword bits must be drawn
815
+ // before masking. Due to the arithmetic of XOR, calling applyMask() with
816
+ // the same mask value a second time will undo the mask. A final well-formed
817
+ // QR Code needs exactly one (not zero, two, etc.) mask applied.
818
+ applyMask(e) {
819
+ if (e < 0 || e > 7)
820
+ throw new RangeError("Mask value out of range");
821
+ for (let r = 0; r < this.size; r++)
822
+ for (let t = 0; t < this.size; t++) {
823
+ let n;
824
+ switch (e) {
825
+ case 0:
826
+ n = (t + r) % 2 == 0;
827
+ break;
828
+ case 1:
829
+ n = r % 2 == 0;
830
+ break;
831
+ case 2:
832
+ n = t % 3 == 0;
833
+ break;
834
+ case 3:
835
+ n = (t + r) % 3 == 0;
836
+ break;
837
+ case 4:
838
+ n = (Math.floor(t / 3) + Math.floor(r / 2)) % 2 == 0;
839
+ break;
840
+ case 5:
841
+ n = t * r % 2 + t * r % 3 == 0;
842
+ break;
843
+ case 6:
844
+ n = (t * r % 2 + t * r % 3) % 2 == 0;
845
+ break;
846
+ case 7:
847
+ n = ((t + r) % 2 + t * r % 3) % 2 == 0;
848
+ break;
849
+ default:
850
+ throw new Error("Unreachable");
851
+ }
852
+ !this.isFunction[r][t] && n && (this.modules[r][t] = !this.modules[r][t]);
853
+ }
854
+ }
855
+ // Calculates and returns the penalty score based on state of this QR Code's current modules.
856
+ // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
857
+ getPenaltyScore() {
858
+ let e = 0;
859
+ for (let o = 0; o < this.size; o++) {
860
+ let f = !1, d = 0, h = [0, 0, 0, 0, 0, 0, 0];
861
+ for (let m = 0; m < this.size; m++)
862
+ this.modules[o][m] == f ? (d++, d == 5 ? e += E.PENALTY_N1 : d > 5 && e++) : (this.finderPenaltyAddHistory(d, h), f || (e += this.finderPenaltyCountPatterns(h) * E.PENALTY_N3), f = this.modules[o][m], d = 1);
863
+ e += this.finderPenaltyTerminateAndCount(f, d, h) * E.PENALTY_N3;
864
+ }
865
+ for (let o = 0; o < this.size; o++) {
866
+ let f = !1, d = 0, h = [0, 0, 0, 0, 0, 0, 0];
867
+ for (let m = 0; m < this.size; m++)
868
+ this.modules[m][o] == f ? (d++, d == 5 ? e += E.PENALTY_N1 : d > 5 && e++) : (this.finderPenaltyAddHistory(d, h), f || (e += this.finderPenaltyCountPatterns(h) * E.PENALTY_N3), f = this.modules[m][o], d = 1);
869
+ e += this.finderPenaltyTerminateAndCount(f, d, h) * E.PENALTY_N3;
870
+ }
871
+ for (let o = 0; o < this.size - 1; o++)
872
+ for (let f = 0; f < this.size - 1; f++) {
873
+ const d = this.modules[o][f];
874
+ d == this.modules[o][f + 1] && d == this.modules[o + 1][f] && d == this.modules[o + 1][f + 1] && (e += E.PENALTY_N2);
875
+ }
876
+ let r = 0;
877
+ for (const o of this.modules)
878
+ r = o.reduce((f, d) => f + (d ? 1 : 0), r);
879
+ const t = this.size * this.size, n = Math.ceil(Math.abs(r * 20 - t * 10) / t) - 1;
880
+ return u(0 <= n && n <= 9), e += n * E.PENALTY_N4, u(0 <= e && e <= 2568888), e;
881
+ }
882
+ /*-- Private helper functions --*/
883
+ // Returns an ascending list of positions of alignment patterns for this version number.
884
+ // Each position is in the range [0,177), and are used on both the x and y axes.
885
+ // This could be implemented as lookup table of 40 variable-length lists of integers.
886
+ getAlignmentPatternPositions() {
887
+ if (this.version == 1)
888
+ return [];
889
+ {
890
+ const e = Math.floor(this.version / 7) + 2, r = this.version == 32 ? 26 : Math.ceil((this.version * 4 + 4) / (e * 2 - 2)) * 2;
891
+ let t = [6];
892
+ for (let n = this.size - 7; t.length < e; n -= r)
893
+ t.splice(1, 0, n);
894
+ return t;
895
+ }
896
+ }
897
+ // Returns the number of data bits that can be stored in a QR Code of the given version number, after
898
+ // all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
899
+ // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
900
+ static getNumRawDataModules(e) {
901
+ if (e < E.MIN_VERSION || e > E.MAX_VERSION)
902
+ throw new RangeError("Version number out of range");
903
+ let r = (16 * e + 128) * e + 64;
904
+ if (e >= 2) {
905
+ const t = Math.floor(e / 7) + 2;
906
+ r -= (25 * t - 10) * t - 55, e >= 7 && (r -= 36);
907
+ }
908
+ return u(208 <= r && r <= 29648), r;
909
+ }
910
+ // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
911
+ // QR Code of the given version number and error correction level, with remainder bits discarded.
912
+ // This stateless pure function could be implemented as a (40*4)-cell lookup table.
913
+ static getNumDataCodewords(e, r) {
914
+ return Math.floor(E.getNumRawDataModules(e) / 8) - E.ECC_CODEWORDS_PER_BLOCK[r.ordinal][e] * E.NUM_ERROR_CORRECTION_BLOCKS[r.ordinal][e];
915
+ }
916
+ // Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
917
+ // implemented as a lookup table over all possible parameter values, instead of as an algorithm.
918
+ static reedSolomonComputeDivisor(e) {
919
+ if (e < 1 || e > 255)
920
+ throw new RangeError("Degree out of range");
921
+ let r = [];
922
+ for (let n = 0; n < e - 1; n++)
923
+ r.push(0);
924
+ r.push(1);
925
+ let t = 1;
926
+ for (let n = 0; n < e; n++) {
927
+ for (let o = 0; o < r.length; o++)
928
+ r[o] = E.reedSolomonMultiply(r[o], t), o + 1 < r.length && (r[o] ^= r[o + 1]);
929
+ t = E.reedSolomonMultiply(t, 2);
930
+ }
931
+ return r;
932
+ }
933
+ // Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
934
+ static reedSolomonComputeRemainder(e, r) {
935
+ let t = r.map((n) => 0);
936
+ for (const n of e) {
937
+ const o = n ^ t.shift();
938
+ t.push(0), r.forEach((f, d) => t[d] ^= E.reedSolomonMultiply(f, o));
939
+ }
940
+ return t;
941
+ }
942
+ // Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
943
+ // are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
944
+ static reedSolomonMultiply(e, r) {
945
+ if (e >>> 8 || r >>> 8)
946
+ throw new RangeError("Byte out of range");
947
+ let t = 0;
948
+ for (let n = 7; n >= 0; n--)
949
+ t = t << 1 ^ (t >>> 7) * 285, t ^= (r >>> n & 1) * e;
950
+ return u(t >>> 8 == 0), t;
951
+ }
952
+ // Can only be called immediately after a light run is added, and
953
+ // returns either 0, 1, or 2. A helper function for getPenaltyScore().
954
+ finderPenaltyCountPatterns(e) {
955
+ const r = e[1];
956
+ u(r <= this.size * 3);
957
+ const t = r > 0 && e[2] == r && e[3] == r * 3 && e[4] == r && e[5] == r;
958
+ return (t && e[0] >= r * 4 && e[6] >= r ? 1 : 0) + (t && e[6] >= r * 4 && e[0] >= r ? 1 : 0);
959
+ }
960
+ // Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
961
+ finderPenaltyTerminateAndCount(e, r, t) {
962
+ return e && (this.finderPenaltyAddHistory(r, t), r = 0), r += this.size, this.finderPenaltyAddHistory(r, t), this.finderPenaltyCountPatterns(t);
963
+ }
964
+ // Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
965
+ finderPenaltyAddHistory(e, r) {
966
+ r[0] == 0 && (e += this.size), r.pop(), r.unshift(e);
967
+ }
968
+ };
969
+ s.MIN_VERSION = 1, s.MAX_VERSION = 40, s.PENALTY_N1 = 3, s.PENALTY_N2 = 3, s.PENALTY_N3 = 40, s.PENALTY_N4 = 10, s.ECC_CODEWORDS_PER_BLOCK = [
970
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
971
+ //0, 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 Error correction level
972
+ [-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
973
+ // Low
974
+ [-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],
975
+ // Medium
976
+ [-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
977
+ // Quartile
978
+ [-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
979
+ // High
980
+ ], s.NUM_ERROR_CORRECTION_BLOCKS = [
981
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
982
+ //0, 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 Error correction level
983
+ [-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],
984
+ // Low
985
+ [-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],
986
+ // Medium
987
+ [-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],
988
+ // Quartile
989
+ [-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81]
990
+ // High
991
+ ], l.QrCode = s;
992
+ function a(g, e, r) {
993
+ if (e < 0 || e > 31 || g >>> e)
994
+ throw new RangeError("Value out of range");
995
+ for (let t = e - 1; t >= 0; t--)
996
+ r.push(g >>> t & 1);
997
+ }
998
+ function c(g, e) {
999
+ return (g >>> e & 1) != 0;
1000
+ }
1001
+ function u(g) {
1002
+ if (!g)
1003
+ throw new Error("Assertion error");
1004
+ }
1005
+ const i = class R {
1006
+ /*-- Constructor (low level) and fields --*/
1007
+ // Creates a new QR Code segment with the given attributes and data.
1008
+ // The character count (numChars) must agree with the mode and the bit buffer length,
1009
+ // but the constraint isn't checked. The given bit buffer is cloned and stored.
1010
+ constructor(e, r, t) {
1011
+ if (this.mode = e, this.numChars = r, this.bitData = t, r < 0)
1012
+ throw new RangeError("Invalid argument");
1013
+ this.bitData = t.slice();
1014
+ }
1015
+ /*-- Static factory functions (mid level) --*/
1016
+ // Returns a segment representing the given binary data encoded in
1017
+ // byte mode. All input byte arrays are acceptable. Any text string
1018
+ // can be converted to UTF-8 bytes and encoded as a byte mode segment.
1019
+ static makeBytes(e) {
1020
+ let r = [];
1021
+ for (const t of e)
1022
+ a(t, 8, r);
1023
+ return new R(R.Mode.BYTE, e.length, r);
1024
+ }
1025
+ // Returns a segment representing the given string of decimal digits encoded in numeric mode.
1026
+ static makeNumeric(e) {
1027
+ if (!R.isNumeric(e))
1028
+ throw new RangeError("String contains non-numeric characters");
1029
+ let r = [];
1030
+ for (let t = 0; t < e.length; ) {
1031
+ const n = Math.min(e.length - t, 3);
1032
+ a(parseInt(e.substring(t, t + n), 10), n * 3 + 1, r), t += n;
1033
+ }
1034
+ return new R(R.Mode.NUMERIC, e.length, r);
1035
+ }
1036
+ // Returns a segment representing the given text string encoded in alphanumeric mode.
1037
+ // The characters allowed are: 0 to 9, A to Z (uppercase only), space,
1038
+ // dollar, percent, asterisk, plus, hyphen, period, slash, colon.
1039
+ static makeAlphanumeric(e) {
1040
+ if (!R.isAlphanumeric(e))
1041
+ throw new RangeError("String contains unencodable characters in alphanumeric mode");
1042
+ let r = [], t;
1043
+ for (t = 0; t + 2 <= e.length; t += 2) {
1044
+ let n = R.ALPHANUMERIC_CHARSET.indexOf(e.charAt(t)) * 45;
1045
+ n += R.ALPHANUMERIC_CHARSET.indexOf(e.charAt(t + 1)), a(n, 11, r);
1046
+ }
1047
+ return t < e.length && a(R.ALPHANUMERIC_CHARSET.indexOf(e.charAt(t)), 6, r), new R(R.Mode.ALPHANUMERIC, e.length, r);
1048
+ }
1049
+ // Returns a new mutable list of zero or more segments to represent the given Unicode text string.
1050
+ // The result may use various segment modes and switch modes to optimize the length of the bit stream.
1051
+ static makeSegments(e) {
1052
+ return e == "" ? [] : R.isNumeric(e) ? [R.makeNumeric(e)] : R.isAlphanumeric(e) ? [R.makeAlphanumeric(e)] : [R.makeBytes(R.toUtf8ByteArray(e))];
1053
+ }
1054
+ // Returns a segment representing an Extended Channel Interpretation
1055
+ // (ECI) designator with the given assignment value.
1056
+ static makeEci(e) {
1057
+ let r = [];
1058
+ if (e < 0)
1059
+ throw new RangeError("ECI assignment value out of range");
1060
+ if (e < 128)
1061
+ a(e, 8, r);
1062
+ else if (e < 16384)
1063
+ a(2, 2, r), a(e, 14, r);
1064
+ else if (e < 1e6)
1065
+ a(6, 3, r), a(e, 21, r);
1066
+ else
1067
+ throw new RangeError("ECI assignment value out of range");
1068
+ return new R(R.Mode.ECI, 0, r);
1069
+ }
1070
+ // Tests whether the given string can be encoded as a segment in numeric mode.
1071
+ // A string is encodable iff each character is in the range 0 to 9.
1072
+ static isNumeric(e) {
1073
+ return R.NUMERIC_REGEX.test(e);
1074
+ }
1075
+ // Tests whether the given string can be encoded as a segment in alphanumeric mode.
1076
+ // A string is encodable iff each character is in the following set: 0 to 9, A to Z
1077
+ // (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
1078
+ static isAlphanumeric(e) {
1079
+ return R.ALPHANUMERIC_REGEX.test(e);
1080
+ }
1081
+ /*-- Methods --*/
1082
+ // Returns a new copy of the data bits of this segment.
1083
+ getData() {
1084
+ return this.bitData.slice();
1085
+ }
1086
+ // (Package-private) Calculates and returns the number of bits needed to encode the given segments at
1087
+ // the given version. The result is infinity if a segment has too many characters to fit its length field.
1088
+ static getTotalBits(e, r) {
1089
+ let t = 0;
1090
+ for (const n of e) {
1091
+ const o = n.mode.numCharCountBits(r);
1092
+ if (n.numChars >= 1 << o)
1093
+ return 1 / 0;
1094
+ t += 4 + o + n.bitData.length;
1095
+ }
1096
+ return t;
1097
+ }
1098
+ // Returns a new array of bytes representing the given string encoded in UTF-8.
1099
+ static toUtf8ByteArray(e) {
1100
+ e = encodeURI(e);
1101
+ let r = [];
1102
+ for (let t = 0; t < e.length; t++)
1103
+ e.charAt(t) != "%" ? r.push(e.charCodeAt(t)) : (r.push(parseInt(e.substring(t + 1, t + 3), 16)), t += 2);
1104
+ return r;
1105
+ }
1106
+ };
1107
+ i.NUMERIC_REGEX = /^[0-9]*$/, i.ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+.\/:-]*$/, i.ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
1108
+ let w = i;
1109
+ l.QrSegment = i;
1110
+ })(O || (O = {}));
1111
+ ((l) => {
1112
+ ((s) => {
1113
+ const a = class {
1114
+ // The QR Code can tolerate about 30% erroneous codewords
1115
+ /*-- Constructor and fields --*/
1116
+ constructor(u, i) {
1117
+ this.ordinal = u, this.formatBits = i;
1118
+ }
1119
+ };
1120
+ a.LOW = new a(0, 1), a.MEDIUM = new a(1, 0), a.QUARTILE = new a(2, 3), a.HIGH = new a(3, 2), s.Ecc = a;
1121
+ })(l.QrCode || (l.QrCode = {}));
1122
+ })(O || (O = {}));
1123
+ ((l) => {
1124
+ ((s) => {
1125
+ const a = class {
1126
+ /*-- Constructor and fields --*/
1127
+ constructor(u, i) {
1128
+ this.modeBits = u, this.numBitsCharCount = i;
1129
+ }
1130
+ /*-- Method --*/
1131
+ // (Package-private) Returns the bit width of the character count field for a segment in
1132
+ // this mode in a QR Code at the given version number. The result is in the range [0, 16].
1133
+ numCharCountBits(u) {
1134
+ return this.numBitsCharCount[Math.floor((u + 7) / 17)];
1135
+ }
1136
+ };
1137
+ a.NUMERIC = new a(1, [10, 12, 14]), a.ALPHANUMERIC = new a(2, [9, 11, 13]), a.BYTE = new a(4, [8, 16, 16]), a.KANJI = new a(8, [8, 10, 12]), a.ECI = new a(7, [0, 0, 0]), s.Mode = a;
1138
+ })(l.QrSegment || (l.QrSegment = {}));
1139
+ })(O || (O = {}));
1140
+ var B = O;
1141
+ /**
1142
+ * @license qrcode.react
1143
+ * Copyright (c) Paul O'Shannessy
1144
+ * SPDX-License-Identifier: ISC
1145
+ */
1146
+ var Ae = {
1147
+ L: B.QrCode.Ecc.LOW,
1148
+ M: B.QrCode.Ecc.MEDIUM,
1149
+ Q: B.QrCode.Ecc.QUARTILE,
1150
+ H: B.QrCode.Ecc.HIGH
1151
+ }, ae = 128, se = "L", ie = "#FFFFFF", le = "#000000", ce = !1, de = 1, Se = 4, Ie = 0, $e = 0.1;
1152
+ function fe(l, s = 0) {
1153
+ const a = [];
1154
+ return l.forEach(function(c, u) {
1155
+ let i = null;
1156
+ c.forEach(function(w, g) {
1157
+ if (!w && i !== null) {
1158
+ a.push(
1159
+ `M${i + s} ${u + s}h${g - i}v1H${i + s}z`
1160
+ ), i = null;
1161
+ return;
1162
+ }
1163
+ if (g === c.length - 1) {
1164
+ if (!w)
1165
+ return;
1166
+ i === null ? a.push(`M${g + s},${u + s} h1v1H${g + s}z`) : a.push(
1167
+ `M${i + s},${u + s} h${g + 1 - i}v1H${i + s}z`
1168
+ );
1169
+ return;
1170
+ }
1171
+ w && i === null && (i = g);
1172
+ });
1173
+ }), a.join("");
1174
+ }
1175
+ function ue(l, s) {
1176
+ return l.slice().map((a, c) => c < s.y || c >= s.y + s.h ? a : a.map((u, i) => i < s.x || i >= s.x + s.w ? u : !1));
1177
+ }
1178
+ function Le(l, s, a, c) {
1179
+ if (c == null)
1180
+ return null;
1181
+ const u = l.length + a * 2, i = Math.floor(s * $e), w = u / s, g = (c.width || i) * w, e = (c.height || i) * w, r = c.x == null ? l.length / 2 - g / 2 : c.x * w, t = c.y == null ? l.length / 2 - e / 2 : c.y * w, n = c.opacity == null ? 1 : c.opacity;
1182
+ let o = null;
1183
+ if (c.excavate) {
1184
+ let d = Math.floor(r), h = Math.floor(t), m = Math.ceil(g + r - d), M = Math.ceil(e + t - h);
1185
+ o = { x: d, y: h, w: m, h: M };
1186
+ }
1187
+ const f = c.crossOrigin;
1188
+ return { x: r, y: t, h: e, w: g, excavation: o, opacity: n, crossOrigin: f };
1189
+ }
1190
+ function Fe(l, s) {
1191
+ return s != null ? Math.max(Math.floor(s), 0) : l ? Se : Ie;
1192
+ }
1193
+ function he({
1194
+ value: l,
1195
+ level: s,
1196
+ minVersion: a,
1197
+ includeMargin: c,
1198
+ marginSize: u,
1199
+ imageSettings: i,
1200
+ size: w,
1201
+ boostLevel: g
1202
+ }) {
1203
+ let e = P.useMemo(() => {
1204
+ const d = (Array.isArray(l) ? l : [l]).reduce((h, m) => (h.push(...B.QrSegment.makeSegments(m)), h), []);
1205
+ return B.QrCode.encodeSegments(
1206
+ d,
1207
+ Ae[s],
1208
+ a,
1209
+ void 0,
1210
+ void 0,
1211
+ g
1212
+ );
1213
+ }, [l, s, a, g]);
1214
+ const { cells: r, margin: t, numCells: n, calculatedImageSettings: o } = P.useMemo(() => {
1215
+ let f = e.getModules();
1216
+ const d = Fe(c, u), h = f.length + d * 2, m = Le(
1217
+ f,
1218
+ w,
1219
+ d,
1220
+ i
1221
+ );
1222
+ return {
1223
+ cells: f,
1224
+ margin: d,
1225
+ numCells: h,
1226
+ calculatedImageSettings: m
1227
+ };
1228
+ }, [e, w, i, c, u]);
1229
+ return {
1230
+ qrcode: e,
1231
+ margin: t,
1232
+ cells: r,
1233
+ numCells: n,
1234
+ calculatedImageSettings: o
1235
+ };
1236
+ }
1237
+ var Oe = function() {
1238
+ try {
1239
+ new Path2D().addPath(new Path2D());
1240
+ } catch {
1241
+ return !1;
1242
+ }
1243
+ return !0;
1244
+ }(), De = P.forwardRef(
1245
+ function(s, a) {
1246
+ const c = s, {
1247
+ value: u,
1248
+ size: i = ae,
1249
+ level: w = se,
1250
+ bgColor: g = ie,
1251
+ fgColor: e = le,
1252
+ includeMargin: r = ce,
1253
+ minVersion: t = de,
1254
+ boostLevel: n,
1255
+ marginSize: o,
1256
+ imageSettings: f
1257
+ } = c, h = G(c, [
1258
+ "value",
1259
+ "size",
1260
+ "level",
1261
+ "bgColor",
1262
+ "fgColor",
1263
+ "includeMargin",
1264
+ "minVersion",
1265
+ "boostLevel",
1266
+ "marginSize",
1267
+ "imageSettings"
1268
+ ]), { style: m } = h, M = G(h, ["style"]), b = f == null ? void 0 : f.src, C = P.useRef(null), y = P.useRef(null), p = P.useCallback(
1269
+ (F) => {
1270
+ C.current = F, typeof a == "function" ? a(F) : a && (a.current = F);
1271
+ },
1272
+ [a]
1273
+ ), [x, S] = P.useState(!1), { margin: I, cells: D, numCells: k, calculatedImageSettings: A } = he({
1274
+ value: u,
1275
+ level: w,
1276
+ minVersion: t,
1277
+ boostLevel: n,
1278
+ includeMargin: r,
1279
+ marginSize: o,
1280
+ imageSettings: f,
1281
+ size: i
1282
+ });
1283
+ P.useEffect(() => {
1284
+ if (C.current != null) {
1285
+ const F = C.current, L = F.getContext("2d");
1286
+ if (!L)
1287
+ return;
1288
+ let Y = D;
1289
+ const z = y.current, V = A != null && z !== null && z.complete && z.naturalHeight !== 0 && z.naturalWidth !== 0;
1290
+ V && A.excavation != null && (Y = ue(
1291
+ D,
1292
+ A.excavation
1293
+ ));
1294
+ const X = window.devicePixelRatio || 1;
1295
+ F.height = F.width = i * X;
1296
+ const K = i / k * X;
1297
+ L.scale(K, K), L.fillStyle = g, L.fillRect(0, 0, k, k), L.fillStyle = e, Oe ? L.fill(new Path2D(fe(Y, I))) : D.forEach(function(we, Ce) {
1298
+ we.forEach(function(ye, Ee) {
1299
+ ye && L.fillRect(Ee + I, Ce + I, 1, 1);
1300
+ });
1301
+ }), A && (L.globalAlpha = A.opacity), V && L.drawImage(
1302
+ z,
1303
+ A.x + I,
1304
+ A.y + I,
1305
+ A.w,
1306
+ A.h
1307
+ );
1308
+ }
1309
+ }), P.useEffect(() => {
1310
+ S(!1);
1311
+ }, [b]);
1312
+ const me = j({ height: i, width: i }, m);
1313
+ let W = null;
1314
+ return b != null && (W = /* @__PURE__ */ P.createElement(
1315
+ "img",
1316
+ {
1317
+ src: b,
1318
+ key: b,
1319
+ style: { display: "none" },
1320
+ onLoad: () => {
1321
+ S(!0);
1322
+ },
1323
+ ref: y,
1324
+ crossOrigin: A == null ? void 0 : A.crossOrigin
1325
+ }
1326
+ )), /* @__PURE__ */ P.createElement(P.Fragment, null, /* @__PURE__ */ P.createElement(
1327
+ "canvas",
1328
+ j({
1329
+ style: me,
1330
+ height: i,
1331
+ width: i,
1332
+ ref: p,
1333
+ role: "img"
1334
+ }, M)
1335
+ ), W);
1336
+ }
1337
+ );
1338
+ De.displayName = "QRCodeCanvas";
1339
+ var ge = P.forwardRef(
1340
+ function(s, a) {
1341
+ const c = s, {
1342
+ value: u,
1343
+ size: i = ae,
1344
+ level: w = se,
1345
+ bgColor: g = ie,
1346
+ fgColor: e = le,
1347
+ includeMargin: r = ce,
1348
+ minVersion: t = de,
1349
+ boostLevel: n,
1350
+ title: o,
1351
+ marginSize: f,
1352
+ imageSettings: d
1353
+ } = c, h = G(c, [
1354
+ "value",
1355
+ "size",
1356
+ "level",
1357
+ "bgColor",
1358
+ "fgColor",
1359
+ "includeMargin",
1360
+ "minVersion",
1361
+ "boostLevel",
1362
+ "title",
1363
+ "marginSize",
1364
+ "imageSettings"
1365
+ ]), { margin: m, cells: M, numCells: b, calculatedImageSettings: C } = he({
1366
+ value: u,
1367
+ level: w,
1368
+ minVersion: t,
1369
+ boostLevel: n,
1370
+ includeMargin: r,
1371
+ marginSize: f,
1372
+ imageSettings: d,
1373
+ size: i
1374
+ });
1375
+ let y = M, p = null;
1376
+ d != null && C != null && (C.excavation != null && (y = ue(
1377
+ M,
1378
+ C.excavation
1379
+ )), p = /* @__PURE__ */ P.createElement(
1380
+ "image",
1381
+ {
1382
+ href: d.src,
1383
+ height: C.h,
1384
+ width: C.w,
1385
+ x: C.x + m,
1386
+ y: C.y + m,
1387
+ preserveAspectRatio: "none",
1388
+ opacity: C.opacity,
1389
+ crossOrigin: C.crossOrigin
1390
+ }
1391
+ ));
1392
+ const x = fe(y, m);
1393
+ return /* @__PURE__ */ P.createElement(
1394
+ "svg",
1395
+ j({
1396
+ height: i,
1397
+ width: i,
1398
+ viewBox: `0 0 ${b} ${b}`,
1399
+ ref: a,
1400
+ role: "img"
1401
+ }, h),
1402
+ !!o && /* @__PURE__ */ P.createElement("title", null, o),
1403
+ /* @__PURE__ */ P.createElement(
1404
+ "path",
1405
+ {
1406
+ fill: g,
1407
+ d: `M0,0 h${b}v${b}H0z`,
1408
+ shapeRendering: "crispEdges"
1409
+ }
1410
+ ),
1411
+ /* @__PURE__ */ P.createElement("path", { fill: e, d: x, shapeRendering: "crispEdges" }),
1412
+ p
1413
+ );
1414
+ }
1415
+ );
1416
+ ge.displayName = "QRCodeSVG";
1417
+ function qe({
1418
+ value: l,
1419
+ size: s = 128,
1420
+ bgColor: a = "#ffffff",
1421
+ fgColor: c = "#000000",
1422
+ level: u = "M",
1423
+ includeMargin: i = !1,
1424
+ imageSettings: w,
1425
+ className: g = "",
1426
+ style: e = {}
1427
+ }) {
1428
+ return /* @__PURE__ */ v(
1429
+ "div",
1430
+ {
1431
+ className: g,
1432
+ style: {
1433
+ display: "inline-block",
1434
+ ...e
1435
+ },
1436
+ children: /* @__PURE__ */ v(
1437
+ ge,
1438
+ {
1439
+ value: l,
1440
+ size: s,
1441
+ bgColor: a,
1442
+ fgColor: c,
1443
+ level: u,
1444
+ includeMargin: i,
1445
+ imageSettings: w
1446
+ }
1447
+ )
1448
+ }
1449
+ );
1450
+ }
1451
+ function et({
1452
+ value: l,
1453
+ format: s = "CODE128",
1454
+ width: a = 2,
1455
+ height: c = 100,
1456
+ displayValue: u = !0,
1457
+ fontSize: i = 16,
1458
+ font: w = "monospace",
1459
+ textAlign: g = "center",
1460
+ textPosition: e = "bottom",
1461
+ textMargin: r = 2,
1462
+ margin: t = 10,
1463
+ marginTop: n,
1464
+ marginBottom: o,
1465
+ marginLeft: f,
1466
+ marginRight: d,
1467
+ background: h = "#ffffff",
1468
+ lineColor: m = "#000000",
1469
+ className: M = "",
1470
+ style: b = {}
1471
+ }) {
1472
+ const C = ee(null);
1473
+ return Re(() => {
1474
+ (async () => {
1475
+ if (C.current)
1476
+ try {
1477
+ C.current.innerHTML = "";
1478
+ const p = document.createElementNS("http://www.w3.org/2000/svg", "svg");
1479
+ C.current.appendChild(p);
1480
+ const x = (await import("./JsBarcode-ImX-0ZxL.js").then((S) => S.J)).default;
1481
+ x(p, l, {
1482
+ format: s,
1483
+ width: a,
1484
+ height: c,
1485
+ displayValue: u,
1486
+ fontSize: i,
1487
+ font: w,
1488
+ textAlign: g,
1489
+ textPosition: e,
1490
+ textMargin: r,
1491
+ margin: t,
1492
+ marginTop: n,
1493
+ marginBottom: o,
1494
+ marginLeft: f,
1495
+ marginRight: d,
1496
+ background: h,
1497
+ lineColor: m
1498
+ });
1499
+ } catch (p) {
1500
+ console.error("Barcode generation failed:", p);
1501
+ }
1502
+ })();
1503
+ }, [
1504
+ l,
1505
+ s,
1506
+ a,
1507
+ c,
1508
+ u,
1509
+ i,
1510
+ w,
1511
+ g,
1512
+ e,
1513
+ r,
1514
+ t,
1515
+ n,
1516
+ o,
1517
+ f,
1518
+ d,
1519
+ h,
1520
+ m
1521
+ ]), /* @__PURE__ */ v(
1522
+ "div",
1523
+ {
1524
+ ref: C,
1525
+ className: M,
1526
+ style: {
1527
+ display: "inline-block",
1528
+ ...b
1529
+ }
1530
+ }
1531
+ );
1532
+ }
1533
+ function tt({
1534
+ children: l,
1535
+ className: s = "",
1536
+ style: a = {}
1537
+ }) {
1538
+ return /* @__PURE__ */ v(
1539
+ "div",
1540
+ {
1541
+ className: s,
1542
+ style: {
1543
+ width: "100%",
1544
+ padding: "40px",
1545
+ backgroundColor: "#ffffff",
1546
+ pageBreakAfter: "always",
1547
+ breakAfter: "page",
1548
+ ...a
1549
+ },
1550
+ children: l
1551
+ }
1552
+ );
1553
+ }
1554
+ function rt() {
1555
+ const { config: l, setConfig: s, loading: a, setLoading: c, error: u, setError: i } = re();
1556
+ return {
1557
+ generate: async (t, n) => {
1558
+ try {
1559
+ c(!0), i(null);
1560
+ const o = new H(), f = t.outerHTML, d = await o.toBase64(f, {
1561
+ scale: (n == null ? void 0 : n.scale) || l.scale,
1562
+ backgroundColor: (n == null ? void 0 : n.backgroundColor) || "#ffffff"
1563
+ });
1564
+ return c(!1), d;
1565
+ } catch (o) {
1566
+ const f = o instanceof Error ? o.message : "Unknown error";
1567
+ throw i(f), c(!1), o;
1568
+ }
1569
+ },
1570
+ download: async (t, n) => {
1571
+ try {
1572
+ c(!0), i(null);
1573
+ const o = new H(), f = Array.isArray(t) ? t : [t];
1574
+ f.length === 1 ? await o.generatePage(f[0], {
1575
+ filename: (n == null ? void 0 : n.filename) || "document.pdf",
1576
+ format: (n == null ? void 0 : n.format) || l.format,
1577
+ orientation: (n == null ? void 0 : n.orientation) || l.orientation,
1578
+ scale: (n == null ? void 0 : n.scale) || l.scale,
1579
+ margin: (n == null ? void 0 : n.margin) || l.margin,
1580
+ backgroundColor: (n == null ? void 0 : n.backgroundColor) || "#ffffff"
1581
+ }) : await o.generateMultiplePages(f, {
1582
+ filename: (n == null ? void 0 : n.filename) || "document.pdf",
1583
+ format: (n == null ? void 0 : n.format) || l.format,
1584
+ orientation: (n == null ? void 0 : n.orientation) || l.orientation,
1585
+ scale: (n == null ? void 0 : n.scale) || l.scale,
1586
+ margin: (n == null ? void 0 : n.margin) || l.margin,
1587
+ backgroundColor: (n == null ? void 0 : n.backgroundColor) || "#ffffff"
1588
+ }), c(!1);
1589
+ } catch (o) {
1590
+ const f = o instanceof Error ? o.message : "Unknown error";
1591
+ throw i(f), c(!1), o;
1592
+ }
1593
+ },
1594
+ preview: (t, n) => {
1595
+ const o = document.getElementById(n);
1596
+ if (!o)
1597
+ throw new Error(`Container #${n} not found`);
1598
+ o.innerHTML = t.outerHTML;
1599
+ },
1600
+ config: l,
1601
+ updateConfig: (t) => {
1602
+ s(t);
1603
+ },
1604
+ loading: a,
1605
+ error: u,
1606
+ setError: i
1607
+ };
1608
+ }
1609
+ export {
1610
+ Ve as Badge,
1611
+ et as Barcode,
1612
+ xe as Box,
1613
+ Xe as Divider,
1614
+ T as Flex,
1615
+ _e as Grid,
1616
+ Qe as Heading,
1617
+ je as Image,
1618
+ Pe as PDFGenerator,
1619
+ He as PDFProvider,
1620
+ tt as Page,
1621
+ qe as QRCode,
1622
+ Ge as Table,
1623
+ Ye as TableCell,
1624
+ We as TableRow,
1625
+ $ as Text,
1626
+ Ke as TotalBox,
1627
+ rt as usePDF,
1628
+ re as usePDFContext
1629
+ };