@liquid-js/qr-code-styling 5.0.0-3 → 5.0.0-5

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.
@@ -50,7 +50,8 @@ function sanitizeGradient(gradient) {
50
50
  DotType.square = "square", DotType.smallSquare = "small-square", DotType.tinySquare = "tiny-square",
51
51
  DotType.diamond = "diamond", DotType.wave = "wave", DotType.heart = "heart", DotType.star = "star",
52
52
  DotType.weave = "weave", DotType.pentagon = "pentagon", DotType.hexagon = "hexagon",
53
- DotType.zebraHorizontal = "zebra-horizontal", DotType.zebraVertical = "zebra-vertical";
53
+ DotType.zebraHorizontal = "zebra-horizontal", DotType.zebraVertical = "zebra-vertical",
54
+ DotType.blocksHorizontal = "blocks-horizontal", DotType.blocksVertical = "blocks-vertical";
54
55
  }(DotType || (DotType = {})), function(CornerDotType) {
55
56
  CornerDotType.dot = "dot", CornerDotType.square = "square", CornerDotType.heart = "heart",
56
57
  CornerDotType.extraRounded = "extra-rounded", CornerDotType.classy = "classy", CornerDotType.inpoint = "inpoint",
@@ -175,7 +176,7 @@ let DotElements = {
175
176
  return element.setAttribute("d", svgPath`M ${x} ${y} v ${size} h ${size} a ${size} ${size} 0 0 0 ${-size} ${-size} z`),
176
177
  element;
177
178
  },
178
- cornersRounded: args => {
179
+ classyDot: args => {
179
180
  const {size, x, y, document} = args, element = document.createElementNS("http://www.w3.org/2000/svg", "path");
180
181
  return element.setAttribute("d", svgPath`M ${x} ${y} v ${size / 2} a ${size / 2} ${size / 2} 0 0 0 ${size / 2} ${size / 2} h ${size / 2} v ${-size / 2} a ${size / 2} ${size / 2} 0 0 0 ${-size / 2} ${-size / 2} z`),
181
182
  element;
@@ -337,8 +338,8 @@ let SquareElements = {
337
338
  [CornerSquareType.centerCircle]: args => SquareElements.centerCircle(args)
338
339
  };
339
340
 
340
- let compoundDots_rounded = ({x, y, size, document, getNeighbor}, {corner = DotElements.cornerRounded, end = DotElements.sideRounded} = {}) => {
341
- const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0, rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0, topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0, bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0, neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;
341
+ function roundedCorners({x, y, size, document, getNeighbor}, {corner = DotElements.cornerRounded, end = DotElements.sideRounded} = {}) {
342
+ let leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0, rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0, topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0, bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0, neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;
342
343
  if (0 === neighborsCount) return DotElements.dot({
343
344
  x,
344
345
  y,
@@ -375,15 +376,17 @@ let compoundDots_rounded = ({x, y, size, document, getNeighbor}, {corner = DotEl
375
376
  draw: end
376
377
  });
377
378
  }
378
- }, compoundDots_classy = ({x, y, size, document, getNeighbor}, {corner = DotElements.cornerRounded} = {}) => {
379
- const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0, rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0, topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0, bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
379
+ }
380
+
381
+ function classy({x, y, size, document, getNeighbor}, {corner = DotElements.cornerRounded} = {}) {
382
+ let leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0, rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0, topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0, bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
380
383
  return 0 === leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor ? rotateFigure({
381
384
  x,
382
385
  y,
383
386
  size,
384
387
  rotation: Math.PI / 2,
385
388
  document,
386
- draw: DotElements.cornersRounded
389
+ draw: DotElements.classyDot
387
390
  }) : leftNeighbor || topNeighbor ? rightNeighbor || bottomNeighbor ? DotElements.square({
388
391
  x,
389
392
  y,
@@ -404,8 +407,10 @@ let compoundDots_rounded = ({x, y, size, document, getNeighbor}, {corner = DotEl
404
407
  document,
405
408
  draw: corner
406
409
  });
407
- }, compoundDots_line = ({x, y, size, document, getNeighbor}, {stripe = size, rotation = 0} = {}) => {
408
- const topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0, bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
410
+ }
411
+
412
+ function stripes({x, y, size, document, getNeighbor}, {stripe = size, rotation = 0, endpoint = DotElements.sideRounded, dot = DotElements.dot} = {}) {
413
+ let topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0, bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
409
414
  return topNeighbor && bottomNeighbor ? rotateFigure({
410
415
  x,
411
416
  y,
@@ -426,7 +431,7 @@ let compoundDots_rounded = ({x, y, size, document, getNeighbor}, {corner = DotEl
426
431
  size,
427
432
  rotation,
428
433
  document,
429
- draw: args => DotElements.sideRounded(args, {
434
+ draw: args => endpoint(args, {
430
435
  height: stripe
431
436
  })
432
437
  })) : bottomNeighbor && !topNeighbor ? (rotation -= Math.PI / 2, rotateFigure({
@@ -435,7 +440,7 @@ let compoundDots_rounded = ({x, y, size, document, getNeighbor}, {corner = DotEl
435
440
  size,
436
441
  rotation,
437
442
  document,
438
- draw: args => DotElements.sideRounded(args, {
443
+ draw: args => endpoint(args, {
439
444
  height: stripe
440
445
  })
441
446
  })) : rotateFigure({
@@ -444,35 +449,39 @@ let compoundDots_rounded = ({x, y, size, document, getNeighbor}, {corner = DotEl
444
449
  size,
445
450
  rotation,
446
451
  document,
447
- draw: _ => DotElements.dot({
452
+ draw: _ => dot({
448
453
  x: x + (size - stripe) / 2,
449
454
  y: y + (size - stripe) / 2,
450
455
  size: stripe,
451
456
  document
452
457
  })
453
458
  });
454
- }, qrDotFigures = {
459
+ }
460
+
461
+ let qrDotFigures = {
455
462
  [DotType.dot]: args => DotElements.dot(args),
456
- [DotType.randomDot]: ({size, ...args}) => {
457
- const randomFactor = .25 * Math.random() + .75;
463
+ [DotType.randomDot]: ({size, x, y, getPRandom, ...args}) => {
464
+ const rnd = getPRandom || Math.random, randomFactor = .25 * rnd() + .75;
458
465
  return DotElements.dot({
459
466
  ...args,
460
- size: size * randomFactor
467
+ size: size * randomFactor,
468
+ x: x + size * rnd() * (1 - randomFactor),
469
+ y: y + size * rnd() * (1 - randomFactor)
461
470
  });
462
471
  },
463
- [DotType.rounded]: args => compoundDots_rounded(args),
464
- [DotType.extraRounded]: args => compoundDots_rounded(args, {
472
+ [DotType.rounded]: args => roundedCorners(args),
473
+ [DotType.extraRounded]: args => roundedCorners(args, {
465
474
  corner: DotElements.cornerExtraRounded
466
475
  }),
467
- [DotType.verticalLine]: args => compoundDots_line(args),
468
- [DotType.horizontalLine]: ({getNeighbor, ...args}) => compoundDots_line({
476
+ [DotType.verticalLine]: args => stripes(args),
477
+ [DotType.horizontalLine]: ({getNeighbor, ...args}) => stripes({
469
478
  ...args,
470
479
  getNeighbor: getNeighbor ? (x, y) => getNeighbor(y, x) : void 0
471
480
  }, {
472
481
  rotation: -Math.PI / 2
473
482
  }),
474
- [DotType.classy]: args => compoundDots_classy(args),
475
- [DotType.classyRounded]: args => compoundDots_classy(args, {
483
+ [DotType.classy]: args => classy(args),
484
+ [DotType.classyRounded]: args => classy(args, {
476
485
  corner: DotElements.cornerExtraRounded
477
486
  }),
478
487
  [DotType.square]: args => DotElements.square(args),
@@ -496,7 +505,7 @@ let compoundDots_rounded = ({x, y, size, document, getNeighbor}, {corner = DotEl
496
505
  height: .85 * args2.size
497
506
  })
498
507
  }),
499
- [DotType.wave]: args => compoundDots_rounded(args, {
508
+ [DotType.wave]: args => roundedCorners(args, {
500
509
  corner: DotElements.cornerExtraRounded,
501
510
  end: DotElements.wave
502
511
  }),
@@ -513,15 +522,63 @@ let compoundDots_rounded = ({x, y, size, document, getNeighbor}, {corner = DotEl
513
522
  outerRadius: args.size / 2,
514
523
  innerRadius: args.size / 2
515
524
  }),
516
- [DotType.zebraVertical]: args => compoundDots_line(args, {
525
+ [DotType.zebraVertical]: args => stripes(args, {
517
526
  stripe: .8 * args.size
518
527
  }),
519
- [DotType.zebraHorizontal]: ({getNeighbor, ...args}) => compoundDots_line({
528
+ [DotType.zebraHorizontal]: ({getNeighbor, ...args}) => stripes({
520
529
  ...args,
521
530
  getNeighbor: getNeighbor ? (x, y) => getNeighbor(y, x) : void 0
522
531
  }, {
523
532
  stripe: .8 * args.size,
524
533
  rotation: -Math.PI / 2
534
+ }),
535
+ [DotType.blocksVertical]: args => stripes(args, {
536
+ stripe: .8 * args.size,
537
+ endpoint: (_args, _params) => {
538
+ const rnd = args.getPRandom || Math.random, width = args.size * (1 - .25 * rnd()), x = args.x - (args.size - width) / 2;
539
+ return DotElements.square({
540
+ ..._args,
541
+ x
542
+ }, {
543
+ ..._params,
544
+ width
545
+ });
546
+ },
547
+ dot: _args => {
548
+ const rnd = args.getPRandom || Math.random, height = args.size * (1 - .25 * rnd()), y = args.y + (args.size - height) * rnd();
549
+ return DotElements.square({
550
+ ..._args,
551
+ y
552
+ }, {
553
+ height
554
+ });
555
+ }
556
+ }),
557
+ [DotType.blocksHorizontal]: ({getNeighbor, ...args}) => stripes({
558
+ ...args,
559
+ getNeighbor: getNeighbor ? (x, y) => getNeighbor(y, x) : void 0
560
+ }, {
561
+ stripe: .8 * args.size,
562
+ rotation: -Math.PI / 2,
563
+ endpoint: (_args, _params) => {
564
+ const rnd = args.getPRandom || Math.random, width = args.size * (1 - .25 * rnd()), x = args.x - (args.size - width) / 2;
565
+ return DotElements.square({
566
+ ..._args,
567
+ x
568
+ }, {
569
+ ..._params,
570
+ width
571
+ });
572
+ },
573
+ dot: _args => {
574
+ const rnd = args.getPRandom || Math.random, height = args.size * (1 - .25 * rnd()), y = args.y + (args.size - height) * rnd();
575
+ return DotElements.square({
576
+ ..._args,
577
+ y
578
+ }, {
579
+ height
580
+ });
581
+ }
525
582
  })
526
583
  };
527
584
 
@@ -598,6 +655,21 @@ function parseColor(cstr) {
598
655
  };
599
656
  }
600
657
 
658
+ function getRng(matrix, i, j, match = !0) {
659
+ let seed;
660
+ return () => {
661
+ if (null == seed) {
662
+ seed = 0;
663
+ let ix = 0;
664
+ for (let xOffset = -3; xOffset < 4; xOffset++) for (let yOffset = -3; yOffset < 4; yOffset++) ix++,
665
+ seed |= Number(matrix[(i + xOffset + matrix.length << yOffset) % matrix.length]?.[(j + yOffset + matrix.length << xOffset) % matrix.length] === match) << ix;
666
+ }
667
+ let t = seed += 1831565813;
668
+ return t = Math.imul(t ^ t >>> 15, 1 | t), t ^= t + Math.imul(t ^ t >>> 7, 61 | t),
669
+ ((t ^ t >>> 14) >>> 0) / 4294967296;
670
+ };
671
+ }
672
+
601
673
  let squareMask = [ [ 1, 1, 1, 1, 1, 1, 1 ], [ 1, 0, 0, 0, 0, 0, 1 ], [ 1, 0, 0, 0, 0, 0, 1 ], [ 1, 0, 0, 0, 0, 0, 1 ], [ 1, 0, 0, 0, 0, 0, 1 ], [ 1, 0, 0, 0, 0, 0, 1 ], [ 1, 1, 1, 1, 1, 1, 1 ] ], cornerSquareTypes = new Set(Object.values(CornerSquareType));
602
674
 
603
675
  let dotMask = [ [ 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 1, 1, 0, 0 ], [ 0, 0, 1, 1, 1, 0, 0 ], [ 0, 0, 1, 1, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0 ] ], cornerDotTypes = new Set(Object.values(CornerDotType));
@@ -605,6 +677,7 @@ let dotMask = [ [ 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 1,
605
677
  class QRSVG {
606
678
  options;
607
679
  _element;
680
+ _fakeMatrix;
608
681
  get element() {
609
682
  return this._element;
610
683
  }
@@ -746,16 +819,18 @@ class QRSVG {
746
819
  options.shape === ShapeType.circle ? (margin = this.options.backgroundOptions && this.options.backgroundOptions.margin || 0,
747
820
  additionalDots = Math.floor((minSize / dotSize - count - 2 * margin) / 2), fakeCount = count + 2 * additionalDots) : options.imageOptions.mode == ImageMode.background && (additionalDots = 1,
748
821
  fakeCount = count + 2 * additionalDots);
749
- let xFakeBeginning = xBeginning - additionalDots * dotSize, yFakeBeginning = yBeginning - additionalDots * dotSize, colorX = xFakeBeginning, colorY = yFakeBeginning, colorCount = count + 2 * additionalDots, fakeMatrix = new Array(fakeCount), center = Math.floor(fakeCount / 2);
822
+ let xFakeBeginning = xBeginning - additionalDots * dotSize, yFakeBeginning = yBeginning - additionalDots * dotSize, colorX = xFakeBeginning, colorY = yFakeBeginning, colorCount = count + 2 * additionalDots;
823
+ this._fakeMatrix = new Array(fakeCount);
824
+ let center = Math.floor(fakeCount / 2);
750
825
  for (let i = 0; i < fakeCount; i++) {
751
- fakeMatrix[i] = new Array(fakeCount);
826
+ this._fakeMatrix[i] = new Array(fakeCount);
752
827
  for (let j = 0; j < fakeCount; j++) {
753
828
  if (i > additionalDots - 1 && i < fakeCount - additionalDots && j > additionalDots - 1 && j < fakeCount - additionalDots) {
754
829
  let ii = i - additionalDots, jj = j - additionalDots;
755
- filter && !filter(ii, jj) ? fakeMatrix[i][j] = void 0 : fakeMatrix[i][j] = !!this.qr.isDark(jj, ii);
830
+ filter && !filter(ii, jj) ? this._fakeMatrix[i][j] = void 0 : this._fakeMatrix[i][j] = !!this.qr.isDark(jj, ii);
756
831
  continue;
757
832
  }
758
- options.shape === ShapeType.circle && Math.sqrt((i - center) * (i - center) + (j - center) * (j - center)) > center ? fakeMatrix[i][j] = void 0 : fakeMatrix[i][j] = i != additionalDots - 1 && i != fakeCount - additionalDots && j != additionalDots - 1 && j != fakeCount - additionalDots ? this.qr.isDark(j - 2 * additionalDots < 0 ? j : j >= count ? j - 2 * additionalDots : j - additionalDots, i - 2 * additionalDots < 0 ? i : i >= count ? i - 2 * additionalDots : i - additionalDots) : (j == additionalDots - 1 && (i < additionalDots + 8 || i > fakeCount - additionalDots - 9) || j == fakeCount - additionalDots && i < additionalDots + 8 || i == additionalDots - 1 && (j < additionalDots + 8 || j > fakeCount - additionalDots - 9) || i == fakeCount - additionalDots && j < additionalDots + 8) && void 0;
833
+ options.shape === ShapeType.circle && Math.sqrt((i - center) * (i - center) + (j - center) * (j - center)) > center ? this._fakeMatrix[i][j] = void 0 : this._fakeMatrix[i][j] = i != additionalDots - 1 && i != fakeCount - additionalDots && j != additionalDots - 1 && j != fakeCount - additionalDots ? this.qr.isDark(j - 2 * additionalDots < 0 ? j : j >= count ? j - 2 * additionalDots : j - additionalDots, i - 2 * additionalDots < 0 ? i : i >= count ? i - 2 * additionalDots : i - additionalDots) : (j == additionalDots - 1 && (i < additionalDots + 8 || i > fakeCount - additionalDots - 9) || j == fakeCount - additionalDots && i < additionalDots + 8 || i == additionalDots - 1 && (j < additionalDots + 8 || j > fakeCount - additionalDots - 9) || i == fakeCount - additionalDots && j < additionalDots + 8) && void 0;
759
834
  }
760
835
  }
761
836
  let typeNr = (count - 17) / 4, alignment = QRUtil.getPatternPosition(typeNr);
@@ -763,19 +838,21 @@ class QRSVG {
763
838
  let iAlign = alignment.find(v => i - additionalDots > v - 3 && i - additionalDots < v + 3);
764
839
  for (let j = 0; j < fakeCount; j++) {
765
840
  let jAlign = alignment.find(v => j - additionalDots > v - 3 && j - additionalDots < v + 3);
766
- null != fakeMatrix[i][j] && (this.lightDotsMask && (draw = iAlign && jAlign && (iAlign != alignment[0] && jAlign != alignment[0] || jAlign != alignment[0] && jAlign != alignment[alignment.length - 1] || iAlign != alignment[0] && iAlign != alignment[alignment.length - 1]) ? getQrDotFigure(DotType.square) : getQrDotFigure(DotType.tinySquare)),
767
- fakeMatrix[i][j] ? this.dotsMaskGroup && this.dotsMaskGroup.appendChild(draw({
841
+ null != this._fakeMatrix[i][j] && (this.lightDotsMask && (draw = iAlign && jAlign && (iAlign != alignment[0] && jAlign != alignment[0] || jAlign != alignment[0] && jAlign != alignment[alignment.length - 1] || iAlign != alignment[0] && iAlign != alignment[alignment.length - 1]) ? getQrDotFigure(DotType.square) : getQrDotFigure(DotType.tinySquare)),
842
+ this._fakeMatrix[i][j] ? this.dotsMaskGroup && this.dotsMaskGroup.appendChild(draw({
768
843
  x: xFakeBeginning + i * dotSize,
769
844
  y: yFakeBeginning + j * dotSize,
770
845
  size: dotSize,
771
846
  document: this.options.document,
772
- getNeighbor: (xOffset, yOffset) => !0 === fakeMatrix[i + xOffset]?.[j + yOffset]
847
+ getNeighbor: (xOffset, yOffset) => !0 === this._fakeMatrix[i + xOffset]?.[j + yOffset],
848
+ getPRandom: getRng(this._fakeMatrix, i, j)
773
849
  })) : this.lightDotsMask && this.lightDotsMaskGroup && this.lightDotsMaskGroup.appendChild(draw({
774
850
  x: xFakeBeginning + i * dotSize,
775
851
  y: yFakeBeginning + j * dotSize,
776
852
  size: dotSize,
777
853
  document: this.options.document,
778
- getNeighbor: (xOffset, yOffset) => !1 === fakeMatrix[i + xOffset]?.[j + yOffset]
854
+ getNeighbor: (xOffset, yOffset) => !1 === this._fakeMatrix[i + xOffset]?.[j + yOffset],
855
+ getPRandom: getRng(this._fakeMatrix, i, j, !1)
779
856
  })));
780
857
  }
781
858
  }
@@ -844,13 +921,15 @@ class QRSVG {
844
921
  y: y + j * dotSize,
845
922
  size: dotSize,
846
923
  document: this.options.document,
847
- getNeighbor: (xOffset, yOffset) => !!squareMask[i + xOffset]?.[j + yOffset]
924
+ getNeighbor: (xOffset, yOffset) => !!squareMask[i + xOffset]?.[j + yOffset],
925
+ getPRandom: getRng(this._fakeMatrix, x / dotSize + i, y / dotSize + j, !1)
848
926
  })) : this.lightDotsMask && !dotMask[i]?.[j] && this.lightDotsMaskGroup && this.lightDotsMaskGroup.appendChild(draw({
849
927
  x: x + i * dotSize,
850
928
  y: y + j * dotSize,
851
929
  size: dotSize,
852
930
  document: this.options.document,
853
- getNeighbor: (xOffset, yOffset) => !squareMask[i + xOffset]?.[j + yOffset] && !dotMask[i + xOffset]?.[j + yOffset]
931
+ getNeighbor: (xOffset, yOffset) => !squareMask[i + xOffset]?.[j + yOffset] && !dotMask[i + xOffset]?.[j + yOffset],
932
+ getPRandom: getRng(this._fakeMatrix, x / dotSize + i, y / dotSize + j, !1)
854
933
  }));
855
934
  if (this.lightDotsMask) for (let i = -1; i < 8; i++) for (let j = -1; j < 8; j++) -1 != i && 7 != i && -1 != j && 7 != j || this.lightDotsMaskGroup && this.lightDotsMaskGroup.appendChild(draw({
856
935
  x: x + i * dotSize,
@@ -860,7 +939,8 @@ class QRSVG {
860
939
  getNeighbor: (xOffset, yOffset) => {
861
940
  let ii = i + xOffset, jj = j + yOffset;
862
941
  return ii >= -1 && ii <= 7 && jj >= -1 && jj <= 7 && (-1 == ii || 7 == ii || -1 == jj || 7 == jj);
863
- }
942
+ },
943
+ getPRandom: getRng(this._fakeMatrix, x / dotSize + i, y / dotSize + j, !1)
864
944
  }));
865
945
  }
866
946
  (options.cornersDotOptions?.gradient || options.cornersDotOptions?.color) && ([cornersDotMask, cornersDotMaskGroup] = this.createMask(`mask-corners-dot-color-${column}-${row}`),
@@ -902,7 +982,8 @@ class QRSVG {
902
982
  y: y + j * dotSize,
903
983
  size: dotSize,
904
984
  document: this.options.document,
905
- getNeighbor: (xOffset, yOffset) => !!dotMask[i + xOffset]?.[j + yOffset]
985
+ getNeighbor: (xOffset, yOffset) => !!dotMask[i + xOffset]?.[j + yOffset],
986
+ getPRandom: getRng(this._fakeMatrix, x / dotSize + i, y / dotSize + j, !1)
906
987
  }));
907
988
  }
908
989
  });