qr-code-styling-node 1.10.0 → 1.11.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.
@@ -702,12 +702,33 @@ var QRCornerDot = class {
702
702
  }
703
703
  };
704
704
 
705
+ // src/tools/circle.ts
706
+ var isCircle = (options) => options.shape === "circle";
707
+ function getCircleLayout(count) {
708
+ let size = Math.ceil((count + 2) * Math.SQRT2) + 1;
709
+ if ((size - count) % 2) size++;
710
+ const pad2 = (size - count) / 2;
711
+ const radius = size / 2;
712
+ const center = count / 2;
713
+ const isDecoration = (i, j) => {
714
+ if (i >= -1 && i <= count && j >= -1 && j <= count) return false;
715
+ const dx = i + 0.5 - center;
716
+ const dy = j + 0.5 - center;
717
+ if (dx * dx + dy * dy > (radius - 0.5) * (radius - 0.5)) return false;
718
+ let h = Math.imul(i + 1013, 668265261) ^ Math.imul(j + 7919, 374761393) ^ Math.imul(count, 2654435769);
719
+ h = Math.imul(h ^ h >>> 15, 2246822507);
720
+ h ^= h >>> 13;
721
+ return (h >>> 0) % 100 < 45;
722
+ };
723
+ return { size, pad: pad2, isDecoration };
724
+ }
725
+
705
726
  // src/tools/qrArea.ts
706
727
  function getQRArea(options, count) {
707
728
  var _a;
708
729
  const placement = (_a = options.art) == null ? void 0 : _a.placement;
709
730
  if (placement && placement.size > 0) {
710
- const dotSize2 = Math.floor(placement.size / count);
731
+ const dotSize2 = Math.floor(placement.size / (options.shape === "circle" ? getCircleLayout(count).size : count));
711
732
  return {
712
733
  count,
713
734
  dotSize: dotSize2,
@@ -716,7 +737,7 @@ function getQRArea(options, count) {
716
737
  };
717
738
  }
718
739
  const minSize = Math.min(options.width, options.height) - options.margin * 2;
719
- const dotSize = Math.floor(minSize / count);
740
+ const dotSize = Math.floor(minSize / (options.shape === "circle" ? getCircleLayout(count).size : count));
720
741
  return {
721
742
  count,
722
743
  dotSize,
@@ -728,11 +749,11 @@ function getQRArea(options, count) {
728
749
  // src/tools/cornerTypes.ts
729
750
  var SOLID_CORNER_DOT_TYPES = ["diamond", "small-square", "vertical-lines", "horizontal-lines"];
730
751
  var ROUND_DOT_TYPES = ["dots", "rounded", "extra-rounded", "classy-rounded", "fluid"];
731
- var needsSolidAlignment = (type) => SOLID_CORNER_DOT_TYPES.indexOf(type) !== -1;
752
+ var needsSolidAlignment = (type) => type === "fluid" || SOLID_CORNER_DOT_TYPES.indexOf(type) !== -1;
732
753
  function getCornerTypes(options) {
733
754
  var _a, _b, _c, _d, _e;
734
755
  const type = options.dotsOptions.type;
735
- if (((_a = options.art) == null ? void 0 : _a.mode) === "blend" || type === "fluid") {
756
+ if (((_a = options.art) == null ? void 0 : _a.mode) === "blend" || options.shape === "circle" || type === "fluid") {
736
757
  const round2 = ROUND_DOT_TYPES.indexOf(type) !== -1;
737
758
  return {
738
759
  square: ((_b = options.cornersSquareOptions) == null ? void 0 : _b.type) || (round2 ? "rounded" : "square"),
@@ -1486,10 +1507,18 @@ var QRCanvas = class _QRCanvas {
1486
1507
  } else if (options.backgroundOptions.color) {
1487
1508
  canvasContext.fillStyle = options.backgroundOptions.color;
1488
1509
  }
1510
+ if (isCircle(options)) {
1511
+ const size = Math.min(this._canvas.width, this._canvas.height);
1512
+ canvasContext.beginPath();
1513
+ canvasContext.arc(this._canvas.width / 2, this._canvas.height / 2, size / 2, 0, Math.PI * 2);
1514
+ canvasContext.fill();
1515
+ return;
1516
+ }
1489
1517
  canvasContext.fillRect(0, 0, this._canvas.width, this._canvas.height);
1490
1518
  }
1491
1519
  }
1492
1520
  drawDots(filter) {
1521
+ var _a;
1493
1522
  if (!this._qr) {
1494
1523
  throw "QR code is not defined";
1495
1524
  }
@@ -1503,28 +1532,38 @@ var QRCanvas = class _QRCanvas {
1503
1532
  throw "The canvas is too small.";
1504
1533
  }
1505
1534
  const { dotSize, x: xBeginning, y: yBeginning } = getQRArea(options, count);
1535
+ const colorPad = isCircle(options) ? getCircleLayout(count).pad * dotSize : 0;
1506
1536
  const typeDot = new QRDot({ context: canvasContext, type: options.dotsOptions.type });
1507
1537
  const squareDot = new QRDot({ context: canvasContext, type: "square" });
1508
1538
  const solidAlignment = needsSolidAlignment(options.dotsOptions.type);
1509
1539
  const role = solidAlignment ? getModuleRoles(count) : void 0;
1510
1540
  canvasContext.beginPath();
1511
- for (let i = 0; i < count; i++) {
1512
- for (let j = 0; j < count; j++) {
1513
- if (filter && !filter(i, j)) {
1514
- continue;
1515
- }
1516
- if (!this._qr.isDark(i, j)) {
1541
+ const circle = isCircle(options) ? getCircleLayout(count) : void 0;
1542
+ const from = circle ? -circle.pad : 0;
1543
+ const to = circle ? count + circle.pad : count;
1544
+ const inCode = (i, j) => i >= 0 && j >= 0 && i < count && j < count;
1545
+ const isDark = (i, j) => {
1546
+ if (filter && !filter(i, j)) return false;
1547
+ if (inCode(i, j)) return !!this._qr && this._qr.isDark(i, j);
1548
+ return !!circle && circle.isDecoration(i, j);
1549
+ };
1550
+ for (let i = from; i < to; i++) {
1551
+ for (let j = from; j < to; j++) {
1552
+ if (inCode(i, j) ? filter && !filter(i, j) || !((_a = this._qr) == null ? void 0 : _a.isDark(i, j)) : !isDark(i, j)) {
1517
1553
  continue;
1518
1554
  }
1519
- const dot = role && role(i, j) === "alignment" ? squareDot : typeDot;
1555
+ const dot = role && inCode(i, j) && role(i, j) === "alignment" ? squareDot : typeDot;
1520
1556
  dot.draw(
1521
1557
  xBeginning + i * dotSize,
1522
1558
  yBeginning + j * dotSize,
1523
1559
  dotSize,
1524
1560
  (xOffset, yOffset) => {
1525
- if (i + xOffset < 0 || j + yOffset < 0 || i + xOffset >= count || j + yOffset >= count) return false;
1526
- if (filter && !filter(i + xOffset, j + yOffset)) return false;
1527
- return !!this._qr && this._qr.isDark(i + xOffset, j + yOffset);
1561
+ if (!circle) {
1562
+ if (i + xOffset < 0 || j + yOffset < 0 || i + xOffset >= count || j + yOffset >= count) return false;
1563
+ if (filter && !filter(i + xOffset, j + yOffset)) return false;
1564
+ return !!this._qr && this._qr.isDark(i + xOffset, j + yOffset);
1565
+ }
1566
+ return isDark(i + xOffset, j + yOffset);
1528
1567
  }
1529
1568
  );
1530
1569
  }
@@ -1535,9 +1574,9 @@ var QRCanvas = class _QRCanvas {
1535
1574
  context: canvasContext,
1536
1575
  options: gradientOptions,
1537
1576
  additionalRotation: 0,
1538
- x: xBeginning,
1539
- y: yBeginning,
1540
- size: count * dotSize
1577
+ x: xBeginning - colorPad,
1578
+ y: yBeginning - colorPad,
1579
+ size: count * dotSize + 2 * colorPad
1541
1580
  });
1542
1581
  gradientOptions.colorStops.forEach(({ offset, color }) => {
1543
1582
  gradient.addColorStop(offset, color);
@@ -2562,6 +2601,21 @@ var QRSVG = class _QRSVG {
2562
2601
  const gradientOptions = (_a = options.backgroundOptions) == null ? void 0 : _a.gradient;
2563
2602
  const color = (_b = options.backgroundOptions) == null ? void 0 : _b.color;
2564
2603
  if (gradientOptions || color) {
2604
+ if (isCircle(options)) {
2605
+ const size = Math.min(options.width, options.height);
2606
+ this._createColor({
2607
+ options: gradientOptions,
2608
+ color,
2609
+ additionalRotation: 0,
2610
+ x: (options.width - size) / 2,
2611
+ y: (options.height - size) / 2,
2612
+ height: size,
2613
+ width: size,
2614
+ name: "background-color",
2615
+ radius: size / 2
2616
+ });
2617
+ return;
2618
+ }
2565
2619
  this._createColor({
2566
2620
  options: gradientOptions,
2567
2621
  color,
@@ -2586,6 +2640,7 @@ var QRSVG = class _QRSVG {
2586
2640
  throw "The canvas is too small.";
2587
2641
  }
2588
2642
  const { dotSize, x: xBeginning, y: yBeginning } = getQRArea(options, count);
2643
+ const colorPad = isCircle(options) ? getCircleLayout(count).pad * dotSize : 0;
2589
2644
  const typeDot = new QRDot2({
2590
2645
  svg: this._element,
2591
2646
  type: options.dotsOptions.type,
@@ -2601,30 +2656,39 @@ var QRSVG = class _QRSVG {
2601
2656
  options: (_a = options.dotsOptions) == null ? void 0 : _a.gradient,
2602
2657
  color: options.dotsOptions.color,
2603
2658
  additionalRotation: 0,
2604
- x: xBeginning,
2605
- y: yBeginning,
2606
- height: count * dotSize,
2607
- width: count * dotSize,
2659
+ x: xBeginning - colorPad,
2660
+ y: yBeginning - colorPad,
2661
+ height: count * dotSize + 2 * colorPad,
2662
+ width: count * dotSize + 2 * colorPad,
2608
2663
  name: "dot-color",
2609
2664
  imageHref: this._dotsImageHref
2610
2665
  });
2611
- for (let i = 0; i < count; i++) {
2612
- for (let j = 0; j < count; j++) {
2613
- if (filter && !filter(i, j)) {
2614
- continue;
2615
- }
2616
- if (!((_b = this._qr) == null ? void 0 : _b.isDark(i, j))) {
2666
+ const circle = isCircle(options) ? getCircleLayout(count) : void 0;
2667
+ const from = circle ? -circle.pad : 0;
2668
+ const to = circle ? count + circle.pad : count;
2669
+ const inCode = (i, j) => i >= 0 && j >= 0 && i < count && j < count;
2670
+ const isDark = (i, j) => {
2671
+ if (filter && !filter(i, j)) return false;
2672
+ if (inCode(i, j)) return !!this._qr && this._qr.isDark(i, j);
2673
+ return !!circle && circle.isDecoration(i, j);
2674
+ };
2675
+ for (let i = from; i < to; i++) {
2676
+ for (let j = from; j < to; j++) {
2677
+ if (inCode(i, j) ? filter && !filter(i, j) || !((_b = this._qr) == null ? void 0 : _b.isDark(i, j)) : !isDark(i, j)) {
2617
2678
  continue;
2618
2679
  }
2619
- const dot = role && role(i, j) === "alignment" ? squareDot : typeDot;
2680
+ const dot = role && inCode(i, j) && role(i, j) === "alignment" ? squareDot : typeDot;
2620
2681
  dot.draw(
2621
2682
  xBeginning + i * dotSize,
2622
2683
  yBeginning + j * dotSize,
2623
2684
  dotSize,
2624
2685
  (xOffset, yOffset) => {
2625
- if (i + xOffset < 0 || j + yOffset < 0 || i + xOffset >= count || j + yOffset >= count) return false;
2626
- if (filter && !filter(i + xOffset, j + yOffset)) return false;
2627
- return !!this._qr && this._qr.isDark(i + xOffset, j + yOffset);
2686
+ if (!circle) {
2687
+ if (i + xOffset < 0 || j + yOffset < 0 || i + xOffset >= count || j + yOffset >= count) return false;
2688
+ if (filter && !filter(i + xOffset, j + yOffset)) return false;
2689
+ return !!this._qr && this._qr.isDark(i + xOffset, j + yOffset);
2690
+ }
2691
+ return isDark(i + xOffset, j + yOffset);
2628
2692
  }
2629
2693
  );
2630
2694
  if (dot._element && this._dotsClipPath) {
@@ -2870,7 +2934,8 @@ var QRSVG = class _QRSVG {
2870
2934
  height,
2871
2935
  width,
2872
2936
  name,
2873
- imageHref
2937
+ imageHref,
2938
+ radius
2874
2939
  }) {
2875
2940
  const size = width > height ? width : height;
2876
2941
  if (imageHref) {
@@ -2890,6 +2955,10 @@ var QRSVG = class _QRSVG {
2890
2955
  rect2.setAttribute("height", String(height));
2891
2956
  rect2.setAttribute("width", String(width));
2892
2957
  rect2.setAttribute("clip-path", `url('#clip-path-${name}')`);
2958
+ if (radius) {
2959
+ rect2.setAttribute("rx", String(radius));
2960
+ rect2.setAttribute("ry", String(radius));
2961
+ }
2893
2962
  if (options) {
2894
2963
  let gradient;
2895
2964
  if (options.type === gradientTypes_default.radial) {
@@ -3068,7 +3137,93 @@ function sanitizeOptions(options) {
3068
3137
 
3069
3138
  // src/core/QRCodeStyling.ts
3070
3139
  import qrcode from "qrcode-generator";
3071
- var QRCodeStyling = class _QRCodeStyling {
3140
+
3141
+ // src/tools/utf8.ts
3142
+ function toUTF8Bytes(text) {
3143
+ const bytes = [];
3144
+ for (let i = 0; i < text.length; i++) {
3145
+ let code = text.charCodeAt(i);
3146
+ if (code >= 55296 && code < 56320 && i + 1 < text.length) {
3147
+ const next = text.charCodeAt(i + 1);
3148
+ if (next >= 56320 && next < 57344) {
3149
+ code = 65536 + (code - 55296 << 10) + (next - 56320);
3150
+ i++;
3151
+ }
3152
+ }
3153
+ if (code < 128) bytes.push(code);
3154
+ else if (code < 2048) bytes.push(192 | code >> 6, 128 | code & 63);
3155
+ else if (code < 65536) bytes.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
3156
+ else
3157
+ bytes.push(240 | code >> 18, 128 | code >> 12 & 63, 128 | code >> 6 & 63, 128 | code & 63);
3158
+ }
3159
+ return bytes;
3160
+ }
3161
+ var needsUTF8 = (text) => {
3162
+ for (let i = 0; i < text.length; i++) if (text.charCodeAt(i) > 255) return true;
3163
+ return false;
3164
+ };
3165
+
3166
+ // src/tools/payloads.ts
3167
+ var escapeWifi = (value) => value.replace(/([\\;,:"])/g, "\\$1");
3168
+ var escapeText = (value) => value.replace(/\\/g, "\\\\").replace(/;/g, "\\;").replace(/,/g, "\\,").replace(/\r?\n/g, "\\n");
3169
+ var list = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [value];
3170
+ function wifi({ ssid, password, encryption, hidden }) {
3171
+ const type = encryption || (password ? "WPA" : "nopass");
3172
+ let data = `WIFI:T:${type};S:${escapeWifi(ssid)};`;
3173
+ if (type !== "nopass" && password) data += `P:${escapeWifi(password)};`;
3174
+ if (hidden) data += "H:true;";
3175
+ return `${data};`;
3176
+ }
3177
+ function vCard(contact) {
3178
+ const first = contact.firstName || "";
3179
+ const last = contact.lastName || "";
3180
+ const fullName = contact.name || [first, last].filter(Boolean).join(" ");
3181
+ const lines = [
3182
+ "BEGIN:VCARD",
3183
+ "VERSION:3.0",
3184
+ `N:${escapeText(last)};${escapeText(first)};;;`,
3185
+ `FN:${escapeText(fullName)}`
3186
+ ];
3187
+ if (contact.organization) lines.push(`ORG:${escapeText(contact.organization)}`);
3188
+ if (contact.title) lines.push(`TITLE:${escapeText(contact.title)}`);
3189
+ list(contact.phone).forEach((phone2) => lines.push(`TEL;TYPE=CELL:${escapeText(phone2)}`));
3190
+ list(contact.email).forEach((email2) => lines.push(`EMAIL:${escapeText(email2)}`));
3191
+ if (contact.url) lines.push(`URL:${escapeText(contact.url)}`);
3192
+ if (contact.address) {
3193
+ const { street = "", city = "", region = "", postalCode = "", country = "" } = contact.address;
3194
+ lines.push(`ADR;TYPE=WORK:;;${[street, city, region, postalCode, country].map(escapeText).join(";")}`);
3195
+ }
3196
+ if (contact.note) lines.push(`NOTE:${escapeText(contact.note)}`);
3197
+ lines.push("END:VCARD");
3198
+ return lines.join("\r\n");
3199
+ }
3200
+ var pad = (value) => value < 10 ? `0${value}` : String(value);
3201
+ var formatDate = (value, allDay) => {
3202
+ const date = typeof value === "string" ? new Date(value) : value;
3203
+ if (isNaN(date.getTime())) throw new Error(`Invalid date: ${value}`);
3204
+ if (allDay) return `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}`;
3205
+ return `${date.getUTCFullYear()}${pad(date.getUTCMonth() + 1)}${pad(date.getUTCDate())}T${pad(date.getUTCHours())}${pad(date.getUTCMinutes())}${pad(date.getUTCSeconds())}Z`;
3206
+ };
3207
+ function event({ title, start, end, allDay, location, description }) {
3208
+ const dateType = allDay ? ";VALUE=DATE" : "";
3209
+ const lines = ["BEGIN:VEVENT", `SUMMARY:${escapeText(title)}`, `DTSTART${dateType}:${formatDate(start, allDay)}`];
3210
+ if (end) lines.push(`DTEND${dateType}:${formatDate(end, allDay)}`);
3211
+ if (location) lines.push(`LOCATION:${escapeText(location)}`);
3212
+ if (description) lines.push(`DESCRIPTION:${escapeText(description)}`);
3213
+ lines.push("END:VEVENT");
3214
+ return lines.join("\r\n");
3215
+ }
3216
+ function email({ to, subject, body }) {
3217
+ const query = [subject && `subject=${encodeURIComponent(subject)}`, body && `body=${encodeURIComponent(body)}`].filter(Boolean).join("&");
3218
+ return `mailto:${to}${query ? `?${query}` : ""}`;
3219
+ }
3220
+ var phone = (number) => `tel:${number.replace(/[^\d+*#]/g, "")}`;
3221
+ var sms = ({ number, message }) => `SMSTO:${number.replace(/[^\d+]/g, "")}:${message || ""}`;
3222
+ var geo = ({ latitude, longitude }) => `geo:${latitude},${longitude}`;
3223
+ var payloads_default = { wifi, vCard, event, email, phone, sms, geo };
3224
+
3225
+ // src/core/QRCodeStyling.ts
3226
+ var _QRCodeStyling = class _QRCodeStyling {
3072
3227
  constructor(options) {
3073
3228
  this._options = options ? sanitizeOptions(mergeDeep(QROptions_default, options)) : QROptions_default;
3074
3229
  this.update();
@@ -3121,7 +3276,19 @@ var QRCodeStyling = class _QRCodeStyling {
3121
3276
  return;
3122
3277
  }
3123
3278
  this._qr = qrcode(this._options.qrOptions.typeNumber, this._options.qrOptions.errorCorrectionLevel);
3124
- this._qr.addData(this._options.data, this._options.qrOptions.mode || getMode(this._options.data));
3279
+ const data = this._options.data;
3280
+ if (needsUTF8(data)) {
3281
+ const generator = qrcode;
3282
+ const latin1 = generator.stringToBytes;
3283
+ generator.stringToBytes = toUTF8Bytes;
3284
+ try {
3285
+ this._qr.addData(data, this._options.qrOptions.mode || getMode(data));
3286
+ } finally {
3287
+ generator.stringToBytes = latin1;
3288
+ }
3289
+ } else {
3290
+ this._qr.addData(data, this._options.qrOptions.mode || getMode(data));
3291
+ }
3125
3292
  this._qr.make();
3126
3293
  if (this._options.type === drawTypes_default.canvas && !this._canUseCanvas()) {
3127
3294
  this._canvas = void 0;
@@ -3233,12 +3400,12 @@ ${serializer.serializeToString(svg.getElement())}`;
3233
3400
  }
3234
3401
  async _decode(decoder) {
3235
3402
  const { data, width, height } = await this._getPixels();
3236
- const pad = Math.max(16, Math.round(Math.min(width, height) / 8));
3237
- const paddedWidth = width + pad * 2;
3238
- const paddedHeight = height + pad * 2;
3403
+ const pad2 = Math.max(16, Math.round(Math.min(width, height) / 8));
3404
+ const paddedWidth = width + pad2 * 2;
3405
+ const paddedHeight = height + pad2 * 2;
3239
3406
  const padded = new Uint8ClampedArray(paddedWidth * paddedHeight * 4).fill(255);
3240
3407
  for (let y = 0; y < height; y++) {
3241
- padded.set(data.subarray(y * width * 4, (y + 1) * width * 4), ((y + pad) * paddedWidth + pad) * 4);
3408
+ padded.set(data.subarray(y * width * 4, (y + 1) * width * 4), ((y + pad2) * paddedWidth + pad2) * 4);
3242
3409
  }
3243
3410
  const result = decoder(padded, paddedWidth, paddedHeight, { inversionAttempts: "attemptBoth" });
3244
3411
  return result ? result.data : void 0;
@@ -3331,7 +3498,37 @@ ${source}`;
3331
3498
  downloadURI(url, `${name}.${extension}`);
3332
3499
  }
3333
3500
  }
3501
+ /**
3502
+ * Opens the phone's share sheet with the QR code as a file (Web Share API). Where sharing files is not
3503
+ * supported (most desktop browsers), downloads it instead. Resolves to "shared", "downloaded", or
3504
+ * "cancelled" when the user closes the share sheet.
3505
+ */
3506
+ async share(shareOptions = {}) {
3507
+ if (!this._qr) throw "QR code is empty";
3508
+ const extension = shareOptions.extension || "png";
3509
+ const name = shareOptions.name || "qr";
3510
+ const nav = typeof navigator !== "undefined" ? navigator : void 0;
3511
+ if ((nav == null ? void 0 : nav.share) && typeof File !== "undefined") {
3512
+ const blob = await this.getRawData(extension);
3513
+ const type = extension === "svg" ? "image/svg+xml" : `image/${extension}`;
3514
+ const file = new File([blob], `${name}.${extension}`, { type });
3515
+ const data = { files: [file], title: shareOptions.title, text: shareOptions.text };
3516
+ if (!nav.canShare || nav.canShare(data)) {
3517
+ try {
3518
+ await nav.share(data);
3519
+ return "shared";
3520
+ } catch (error) {
3521
+ if ((error == null ? void 0 : error.name) === "AbortError") return "cancelled";
3522
+ }
3523
+ }
3524
+ }
3525
+ await this.download({ name, extension });
3526
+ return "downloaded";
3527
+ }
3334
3528
  };
3529
+ /** Builders for Wi-Fi, contact, event, e-mail, phone, SMS and location codes: pass the result as `data`. */
3530
+ _QRCodeStyling.payload = payloads_default;
3531
+ var QRCodeStyling = _QRCodeStyling;
3335
3532
 
3336
3533
  // src/types/index.ts
3337
3534
  var JSDom = class {
@@ -3354,6 +3551,7 @@ export {
3354
3551
  errorCorrectionLevels_default as errorCorrectionLevels,
3355
3552
  errorCorrectionPercents_default as errorCorrectionPercents,
3356
3553
  modes_default as modes,
3554
+ payloads_default as payload,
3357
3555
  qrTypes_default as qrTypes
3358
3556
  };
3359
3557
  //# sourceMappingURL=qr-code-styling.mjs.map