qr-code-styling-node 1.11.0 → 1.12.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.
@@ -114,7 +114,10 @@ var dotTypes_default = {
114
114
  smallSquare: "small-square",
115
115
  verticalLines: "vertical-lines",
116
116
  horizontalLines: "horizontal-lines",
117
- fluid: "fluid"
117
+ fluid: "fluid",
118
+ wave: "wave",
119
+ blob: "blob",
120
+ circuit: "circuit"
118
121
  };
119
122
 
120
123
  // src/figures/shapes.ts
@@ -252,6 +255,102 @@ var fluid = (x, y, size, neighbors) => {
252
255
  shape.push({ op: "Z" });
253
256
  return shape;
254
257
  };
258
+ var bulgeArc = (x0, y0, x1, y1, sagitta) => {
259
+ const chord = Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
260
+ const r = chord * chord / (8 * sagitta) + sagitta / 2;
261
+ const nx = (y1 - y0) / chord;
262
+ const ny = -(x1 - x0) / chord;
263
+ const cx = (x0 + x1) / 2 - nx * (r - sagitta);
264
+ const cy = (y0 + y1) / 2 - ny * (r - sagitta);
265
+ return { op: "A", cx, cy, r, start: Math.atan2(y0 - cy, x0 - cx), end: Math.atan2(y1 - cy, x1 - cx) };
266
+ };
267
+ var wave = (x, y, size, { left, right }) => {
268
+ const half = size * 0.3;
269
+ const bulge = size * 0.1;
270
+ const cy = y + size / 2;
271
+ const x0 = left ? x : x + half;
272
+ const x1 = right ? x + size : x + size - half;
273
+ const shape = [{ op: "M", x: x0, y: cy - half }];
274
+ shape.push(bulgeArc(x0, cy - half, x1, cy - half, bulge));
275
+ if (right) shape.push({ op: "L", x: x1, y: cy + half });
276
+ else shape.push({ op: "A", cx: x1, cy, r: half, start: -Math.PI / 2, end: Math.PI / 2 });
277
+ shape.push(bulgeArc(x1, cy + half, x0, cy + half, bulge));
278
+ if (left) shape.push({ op: "L", x: x0, y: cy - half });
279
+ else shape.push({ op: "A", cx: x0, cy, r: half, start: Math.PI / 2, end: 3 * Math.PI / 2 });
280
+ shape.push({ op: "Z" });
281
+ return shape;
282
+ };
283
+ var noise = (a, b, salt) => {
284
+ let h = Math.imul(Math.round(a * 16) + 40503, 668265261) ^ Math.imul(Math.round(b * 16) + salt, 374761393);
285
+ h = Math.imul(h ^ h >>> 15, 2246822507);
286
+ h ^= h >>> 13;
287
+ return (h >>> 0) / 4294967296;
288
+ };
289
+ var blob = (x, y, size) => {
290
+ const inset = size * (0.03 + 0.07 * noise(x, y, 1));
291
+ const left = x + inset;
292
+ const top = y + inset;
293
+ const side = size - 2 * inset;
294
+ const radius = (k) => side * (0.25 + 0.25 * noise(x, y, k + 2));
295
+ const [tl, tr, br, bl] = [radius(0), radius(1), radius(2), radius(3)];
296
+ return [
297
+ { op: "M", x: left + tl, y: top },
298
+ { op: "L", x: left + side - tr, y: top },
299
+ { op: "A", cx: left + side - tr, cy: top + tr, r: tr, start: -Math.PI / 2, end: 0 },
300
+ { op: "L", x: left + side, y: top + side - br },
301
+ { op: "A", cx: left + side - br, cy: top + side - br, r: br, start: 0, end: Math.PI / 2 },
302
+ { op: "L", x: left + bl, y: top + side },
303
+ { op: "A", cx: left + bl, cy: top + side - bl, r: bl, start: Math.PI / 2, end: Math.PI },
304
+ { op: "L", x: left, y: top + tl },
305
+ { op: "A", cx: left + tl, cy: top + tl, r: tl, start: Math.PI, end: 3 * Math.PI / 2 },
306
+ { op: "Z" }
307
+ ];
308
+ };
309
+ var circuit = (x, y, size, neighbors) => {
310
+ const cx = x + size / 2;
311
+ const cy = y + size / 2;
312
+ const r = size * 0.36;
313
+ const w = size * 0.17;
314
+ const alpha = Math.asin(w / r);
315
+ const arms = [
316
+ { on: neighbors.top, angle: -Math.PI / 2 },
317
+ { on: neighbors.right, angle: 0 },
318
+ { on: neighbors.bottom, angle: Math.PI / 2 },
319
+ { on: neighbors.left, angle: Math.PI }
320
+ ].filter((arm) => arm.on);
321
+ if (!arms.length) {
322
+ return [
323
+ { op: "M", x: cx + r, y: cy },
324
+ { op: "A", cx, cy, r, start: 0, end: Math.PI },
325
+ { op: "A", cx, cy, r, start: Math.PI, end: 2 * Math.PI },
326
+ { op: "Z" }
327
+ ];
328
+ }
329
+ const point = (angle) => ({
330
+ x: cx + r * Math.cos(angle),
331
+ y: cy + r * Math.sin(angle)
332
+ });
333
+ const toEdge = (angle, at) => {
334
+ const reach = size / 2 - r * Math.cos(alpha);
335
+ return { x: at.x + reach * Math.cos(angle), y: at.y + reach * Math.sin(angle) };
336
+ };
337
+ const first = point(arms[0].angle + alpha);
338
+ const shape = [{ op: "M", x: first.x, y: first.y }];
339
+ for (let k = 0; k < arms.length; k++) {
340
+ const from = arms[k].angle + alpha;
341
+ const next = arms[(k + 1) % arms.length];
342
+ let to = next.angle - alpha;
343
+ if (k === arms.length - 1 || to <= from) to += 2 * Math.PI;
344
+ if (to - from > 1e-6) shape.push({ op: "A", cx, cy, r, start: from, end: to });
345
+ const a = point(next.angle - alpha);
346
+ const b = point(next.angle + alpha);
347
+ const ea = toEdge(next.angle, a);
348
+ const eb = toEdge(next.angle, b);
349
+ shape.push({ op: "L", x: ea.x, y: ea.y }, { op: "L", x: eb.x, y: eb.y }, { op: "L", x: b.x, y: b.y });
350
+ }
351
+ shape.push({ op: "Z" });
352
+ return shape;
353
+ };
255
354
 
256
355
  // src/figures/dot/canvas/QRDot.ts
257
356
  var QRDot = class {
@@ -295,11 +394,17 @@ var QRDot = class {
295
394
  };
296
395
  break;
297
396
  }
397
+ case dotTypes_default.blob:
398
+ drawFunction = (args) => this._drawShape(blob(args.x, args.y, args.size));
399
+ break;
400
+ case dotTypes_default.wave:
401
+ case dotTypes_default.circuit:
298
402
  case dotTypes_default.fluid:
299
403
  drawFunction = (args) => {
300
404
  const has = (dx, dy) => args.getNeighbor ? args.getNeighbor(dx, dy) : false;
405
+ const shapeOf = type === dotTypes_default.wave ? wave : type === dotTypes_default.circuit ? circuit : fluid;
301
406
  this._drawShape(
302
- fluid(args.x, args.y, args.size, {
407
+ shapeOf(args.x, args.y, args.size, {
303
408
  left: has(-1, 0),
304
409
  right: has(1, 0),
305
410
  top: has(0, -1),
@@ -748,12 +853,22 @@ function getQRArea(options, count) {
748
853
 
749
854
  // src/tools/cornerTypes.ts
750
855
  var SOLID_CORNER_DOT_TYPES = ["diamond", "small-square", "vertical-lines", "horizontal-lines"];
751
- var ROUND_DOT_TYPES = ["dots", "rounded", "extra-rounded", "classy-rounded", "fluid"];
752
- var needsSolidAlignment = (type) => type === "fluid" || SOLID_CORNER_DOT_TYPES.indexOf(type) !== -1;
856
+ var ROUND_DOT_TYPES = [
857
+ "dots",
858
+ "rounded",
859
+ "extra-rounded",
860
+ "classy-rounded",
861
+ "fluid",
862
+ "wave",
863
+ "blob",
864
+ "circuit"
865
+ ];
866
+ var SOLID_ROUND_DOT_TYPES = ["fluid", "wave", "blob", "circuit"];
867
+ var needsSolidAlignment = (type) => SOLID_ROUND_DOT_TYPES.indexOf(type) !== -1 || SOLID_CORNER_DOT_TYPES.indexOf(type) !== -1;
753
868
  function getCornerTypes(options) {
754
869
  var _a, _b, _c, _d, _e;
755
870
  const type = options.dotsOptions.type;
756
- if (((_a = options.art) == null ? void 0 : _a.mode) === "blend" || options.shape === "circle" || type === "fluid") {
871
+ if (((_a = options.art) == null ? void 0 : _a.mode) === "blend" || options.shape === "circle" || SOLID_ROUND_DOT_TYPES.indexOf(type) !== -1) {
757
872
  const round2 = ROUND_DOT_TYPES.indexOf(type) !== -1;
758
873
  return {
759
874
  square: ((_b = options.cornersSquareOptions) == null ? void 0 : _b.type) || (round2 ? "rounded" : "square"),
@@ -1864,11 +1979,17 @@ var QRDot2 = class {
1864
1979
  };
1865
1980
  break;
1866
1981
  }
1982
+ case dotTypes_default.blob:
1983
+ drawFunction = (args) => this._drawShape(blob(args.x, args.y, args.size));
1984
+ break;
1985
+ case dotTypes_default.wave:
1986
+ case dotTypes_default.circuit:
1867
1987
  case dotTypes_default.fluid:
1868
1988
  drawFunction = (args) => {
1869
1989
  const has = (dx, dy) => args.getNeighbor ? args.getNeighbor(dx, dy) : false;
1990
+ const shapeOf = type === dotTypes_default.wave ? wave : type === dotTypes_default.circuit ? circuit : fluid;
1870
1991
  this._drawShape(
1871
- fluid(args.x, args.y, args.size, {
1992
+ shapeOf(args.x, args.y, args.size, {
1872
1993
  left: has(-1, 0),
1873
1994
  right: has(1, 0),
1874
1995
  top: has(0, -1),
@@ -2105,6 +2226,85 @@ var QRDot2 = class {
2105
2226
  }
2106
2227
  };
2107
2228
 
2229
+ // src/tools/pathRecorder.ts
2230
+ var fmt = (value) => String(Math.round(value * 1e3) / 1e3);
2231
+ var PathRecorder = class {
2232
+ constructor() {
2233
+ this._m = [1, 0, 0, 1, 0, 0];
2234
+ this._parts = [];
2235
+ this._open = false;
2236
+ }
2237
+ get d() {
2238
+ return this._parts.join("");
2239
+ }
2240
+ beginPath() {
2241
+ }
2242
+ translate(x, y) {
2243
+ const [a, b, c, d, e, f] = this._m;
2244
+ this._m = [a, b, c, d, e + a * x + c * y, f + b * x + d * y];
2245
+ }
2246
+ rotate(angle) {
2247
+ const [a, b, c, d, e, f] = this._m;
2248
+ const cos = Math.cos(angle);
2249
+ const sin = Math.sin(angle);
2250
+ this._m = [a * cos + c * sin, b * cos + d * sin, c * cos - a * sin, d * cos - b * sin, e, f];
2251
+ }
2252
+ _point(x, y) {
2253
+ const [a, b, c, d, e, f] = this._m;
2254
+ return [a * x + c * y + e, b * x + d * y + f];
2255
+ }
2256
+ moveTo(x, y) {
2257
+ const [px, py] = this._point(x, y);
2258
+ this._parts.push(`M${fmt(px)} ${fmt(py)}`);
2259
+ this._open = true;
2260
+ }
2261
+ lineTo(x, y) {
2262
+ if (!this._open) return this.moveTo(x, y);
2263
+ const [px, py] = this._point(x, y);
2264
+ this._parts.push(`L${fmt(px)} ${fmt(py)}`);
2265
+ }
2266
+ closePath() {
2267
+ if (!this._open) return;
2268
+ this._parts.push("Z");
2269
+ this._open = false;
2270
+ }
2271
+ rect(x, y, width, height) {
2272
+ this.moveTo(x, y);
2273
+ this.lineTo(x + width, y);
2274
+ this.lineTo(x + width, y + height);
2275
+ this.lineTo(x, y + height);
2276
+ this.closePath();
2277
+ }
2278
+ arc(cx, cy, r, start, end, anticlockwise = false) {
2279
+ let sweep = anticlockwise ? start - end : end - start;
2280
+ const full = sweep >= 2 * Math.PI - 1e-9;
2281
+ if (!full) {
2282
+ sweep %= 2 * Math.PI;
2283
+ if (sweep < 0) sweep += 2 * Math.PI;
2284
+ }
2285
+ const direction = anticlockwise ? -1 : 1;
2286
+ const at = (angle) => this._point(cx + r * Math.cos(angle), cy + r * Math.sin(angle));
2287
+ const [sx, sy] = at(start);
2288
+ if (this._open) this._parts.push(`L${fmt(sx)} ${fmt(sy)}`);
2289
+ else {
2290
+ this._parts.push(`M${fmt(sx)} ${fmt(sy)}`);
2291
+ this._open = true;
2292
+ }
2293
+ const segment = (to, length) => {
2294
+ const [ex, ey] = at(to);
2295
+ this._parts.push(
2296
+ `A${fmt(r)} ${fmt(r)} 0 ${length > Math.PI ? 1 : 0} ${anticlockwise ? 0 : 1} ${fmt(ex)} ${fmt(ey)}`
2297
+ );
2298
+ };
2299
+ if (full) {
2300
+ segment(start + direction * Math.PI, Math.PI);
2301
+ segment(start + direction * 2 * Math.PI, Math.PI);
2302
+ } else if (sweep > 1e-9) {
2303
+ segment(start + direction * sweep, sweep);
2304
+ }
2305
+ }
2306
+ };
2307
+
2108
2308
  // src/figures/cornerSquare/svg/QRCornerSquare.ts
2109
2309
  var QRCornerSquare2 = class {
2110
2310
  constructor({ svg, type, window: window2 }) {
@@ -2630,7 +2830,7 @@ var QRSVG = class _QRSVG {
2630
2830
  }
2631
2831
  }
2632
2832
  drawDots(filter) {
2633
- var _a, _b;
2833
+ var _a, _b, _c;
2634
2834
  if (!this._qr) {
2635
2835
  throw "QR code is not defined";
2636
2836
  }
@@ -2647,13 +2847,17 @@ var QRSVG = class _QRSVG {
2647
2847
  window: this._window
2648
2848
  });
2649
2849
  const squareDot = new QRDot2({ svg: this._element, type: "square", window: this._window });
2850
+ const recorder = ((_a = options.svgOptions) == null ? void 0 : _a.mergePaths) ? new PathRecorder() : void 0;
2851
+ const recorded = recorder ? recorder : void 0;
2852
+ const typeDotMerged = recorded ? new QRDot({ context: recorded, type: options.dotsOptions.type }) : void 0;
2853
+ const squareDotMerged = recorded ? new QRDot({ context: recorded, type: "square" }) : void 0;
2650
2854
  const solidAlignment = needsSolidAlignment(options.dotsOptions.type);
2651
2855
  const role = solidAlignment ? getModuleRoles(count) : void 0;
2652
2856
  this._dotsClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
2653
2857
  this._dotsClipPath.setAttribute("id", "clip-path-dot-color");
2654
2858
  this._defs.appendChild(this._dotsClipPath);
2655
2859
  this._createColor({
2656
- options: (_a = options.dotsOptions) == null ? void 0 : _a.gradient,
2860
+ options: (_b = options.dotsOptions) == null ? void 0 : _b.gradient,
2657
2861
  color: options.dotsOptions.color,
2658
2862
  additionalRotation: 0,
2659
2863
  x: xBeginning - colorPad,
@@ -2674,10 +2878,11 @@ var QRSVG = class _QRSVG {
2674
2878
  };
2675
2879
  for (let i = from; i < to; i++) {
2676
2880
  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)) {
2881
+ if (inCode(i, j) ? filter && !filter(i, j) || !((_c = this._qr) == null ? void 0 : _c.isDark(i, j)) : !isDark(i, j)) {
2678
2882
  continue;
2679
2883
  }
2680
- const dot = role && inCode(i, j) && role(i, j) === "alignment" ? squareDot : typeDot;
2884
+ const alignment = role && inCode(i, j) && role(i, j) === "alignment";
2885
+ const dot = recorded ? alignment ? squareDotMerged : typeDotMerged : alignment ? squareDot : typeDot;
2681
2886
  dot.draw(
2682
2887
  xBeginning + i * dotSize,
2683
2888
  yBeginning + j * dotSize,
@@ -2691,11 +2896,14 @@ var QRSVG = class _QRSVG {
2691
2896
  return isDark(i + xOffset, j + yOffset);
2692
2897
  }
2693
2898
  );
2694
- if (dot._element && this._dotsClipPath) {
2899
+ if (!recorded && dot._element && this._dotsClipPath) {
2695
2900
  this._dotsClipPath.appendChild(dot._element);
2696
2901
  }
2697
2902
  }
2698
2903
  }
2904
+ if (recorder && recorder.d && this._dotsClipPath) {
2905
+ this._dotsClipPath.appendChild(this._svgElement("path", { d: recorder.d, "clip-rule": "evenodd" }));
2906
+ }
2699
2907
  }
2700
2908
  drawCorners() {
2701
2909
  if (!this._qr) {
@@ -2716,7 +2924,7 @@ var QRSVG = class _QRSVG {
2716
2924
  [1, 0, Math.PI / 2],
2717
2925
  [0, 1, -Math.PI / 2]
2718
2926
  ].forEach(([column, row, rotation], index) => {
2719
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
2927
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
2720
2928
  const x = xBeginning + column * dotSize * (count - 7);
2721
2929
  const y = yBeginning + row * dotSize * (count - 7);
2722
2930
  let cornersSquareClipPath = this._dotsClipPath;
@@ -2747,6 +2955,8 @@ var QRSVG = class _QRSVG {
2747
2955
  if (cornersSquare._element && cornersSquareClipPath) {
2748
2956
  cornersSquareClipPath.appendChild(cornersSquare._element);
2749
2957
  }
2958
+ } else if ((_g = options.svgOptions) == null ? void 0 : _g.mergePaths) {
2959
+ this._drawMergedMask(squareMask2, x, y, dotSize, cornersSquareClipPath);
2750
2960
  } else {
2751
2961
  const dot = new QRDot2({
2752
2962
  svg: this._element,
@@ -2755,7 +2965,7 @@ var QRSVG = class _QRSVG {
2755
2965
  });
2756
2966
  for (let i = 0; i < squareMask2.length; i++) {
2757
2967
  for (let j = 0; j < squareMask2[i].length; j++) {
2758
- if (!((_g = squareMask2[i]) == null ? void 0 : _g[j])) {
2968
+ if (!((_h = squareMask2[i]) == null ? void 0 : _h[j])) {
2759
2969
  continue;
2760
2970
  }
2761
2971
  dot.draw(
@@ -2773,14 +2983,14 @@ var QRSVG = class _QRSVG {
2773
2983
  }
2774
2984
  }
2775
2985
  }
2776
- if (((_h = options.cornersDotOptions) == null ? void 0 : _h.gradient) || ((_i = options.cornersDotOptions) == null ? void 0 : _i.color) || this._finderColors) {
2986
+ if (((_i = options.cornersDotOptions) == null ? void 0 : _i.gradient) || ((_j = options.cornersDotOptions) == null ? void 0 : _j.color) || this._finderColors) {
2777
2987
  cornersDotClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
2778
2988
  cornersDotClipPath.setAttribute("id", `clip-path-corners-dot-color-${column}-${row}`);
2779
2989
  this._defs.appendChild(cornersDotClipPath);
2780
2990
  this._cornersDotClipPath = cornersDotClipPath;
2781
2991
  this._createColor({
2782
- options: (_j = options.cornersDotOptions) == null ? void 0 : _j.gradient,
2783
- color: (_m = (_k = options.cornersDotOptions) == null ? void 0 : _k.color) != null ? _m : (_l = this._finderColors) == null ? void 0 : _l[index],
2992
+ options: (_k = options.cornersDotOptions) == null ? void 0 : _k.gradient,
2993
+ color: (_n = (_l = options.cornersDotOptions) == null ? void 0 : _l.color) != null ? _n : (_m = this._finderColors) == null ? void 0 : _m[index],
2784
2994
  additionalRotation: rotation,
2785
2995
  x: x + dotSize * 2,
2786
2996
  y: y + dotSize * 2,
@@ -2799,6 +3009,8 @@ var QRSVG = class _QRSVG {
2799
3009
  if (cornersDot._element && cornersDotClipPath) {
2800
3010
  cornersDotClipPath.appendChild(cornersDot._element);
2801
3011
  }
3012
+ } else if ((_o = options.svgOptions) == null ? void 0 : _o.mergePaths) {
3013
+ this._drawMergedMask(dotMask2, x, y, dotSize, cornersDotClipPath);
2802
3014
  } else {
2803
3015
  const dot = new QRDot2({
2804
3016
  svg: this._element,
@@ -2807,7 +3019,7 @@ var QRSVG = class _QRSVG {
2807
3019
  });
2808
3020
  for (let i = 0; i < dotMask2.length; i++) {
2809
3021
  for (let j = 0; j < dotMask2[i].length; j++) {
2810
- if (!((_n = dotMask2[i]) == null ? void 0 : _n[j])) {
3022
+ if (!((_p = dotMask2[i]) == null ? void 0 : _p[j])) {
2811
3023
  continue;
2812
3024
  }
2813
3025
  dot.draw(
@@ -2827,6 +3039,26 @@ var QRSVG = class _QRSVG {
2827
3039
  }
2828
3040
  });
2829
3041
  }
3042
+ /** Finder modules drawn with the dots' own figure, as one path (svgOptions.mergePaths). */
3043
+ _drawMergedMask(mask, x, y, dotSize, clipPath) {
3044
+ var _a;
3045
+ const recorder = new PathRecorder();
3046
+ const dot = new QRDot({
3047
+ context: recorder,
3048
+ type: this._options.dotsOptions.type
3049
+ });
3050
+ for (let i = 0; i < mask.length; i++) {
3051
+ for (let j = 0; j < mask[i].length; j++) {
3052
+ if (!((_a = mask[i]) == null ? void 0 : _a[j])) continue;
3053
+ dot.draw(x + i * dotSize, y + j * dotSize, dotSize, (xOffset, yOffset) => {
3054
+ var _a2;
3055
+ return !!((_a2 = mask[i + xOffset]) == null ? void 0 : _a2[j + yOffset]);
3056
+ });
3057
+ }
3058
+ }
3059
+ if (recorder.d && clipPath)
3060
+ clipPath.appendChild(this._svgElement("path", { d: recorder.d, "clip-rule": "evenodd" }));
3061
+ }
2830
3062
  _drawArtRects(rects) {
2831
3063
  rects.forEach(({ x, y, width, height, color, shape }) => {
2832
3064
  this._element.appendChild(
@@ -3509,9 +3741,9 @@ ${source}`;
3509
3741
  const name = shareOptions.name || "qr";
3510
3742
  const nav = typeof navigator !== "undefined" ? navigator : void 0;
3511
3743
  if ((nav == null ? void 0 : nav.share) && typeof File !== "undefined") {
3512
- const blob = await this.getRawData(extension);
3744
+ const blob2 = await this.getRawData(extension);
3513
3745
  const type = extension === "svg" ? "image/svg+xml" : `image/${extension}`;
3514
- const file = new File([blob], `${name}.${extension}`, { type });
3746
+ const file = new File([blob2], `${name}.${extension}`, { type });
3515
3747
  const data = { files: [file], title: shareOptions.title, text: shareOptions.text };
3516
3748
  if (!nav.canShare || nav.canShare(data)) {
3517
3749
  try {