@liquid-js/qr-code-styling 1.8.0 → 1.9.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.
package/lib/index.d.ts CHANGED
@@ -2,9 +2,4 @@ import { ErrorCorrectionPercents } from "./constants/errorCorrectionPercents";
2
2
  import { QRCodeStyling } from "./core/QRCodeStyling";
3
3
  import * as browserUtils from "./tools/browserUtils";
4
4
  export * from "./types";
5
- export { ErrorCorrectionPercents, QRCodeStyling };
6
- declare const _default: {
7
- QRCodeStyling: typeof QRCodeStyling;
8
- browserUtils: typeof browserUtils;
9
- };
10
- export default _default;
5
+ export { ErrorCorrectionPercents, QRCodeStyling, browserUtils };
@@ -610,7 +610,7 @@ class QRDot {
610
610
  }
611
611
  }
612
612
 
613
- function calculateImageSize({ originalHeight, originalWidth, maxHiddenDots, maxHiddenAxisDots, dotSize }) {
613
+ function calculateImageSize({ originalHeight, originalWidth, maxHiddenDots, maxHiddenAxisDots, dotSize, margin }) {
614
614
  const hideDots = { x: 0, y: 0 };
615
615
  const imageSize = { x: 0, y: 0 };
616
616
  if (originalHeight <= 0 || originalWidth <= 0 || maxHiddenDots <= 0 || dotSize <= 0) {
@@ -621,41 +621,65 @@ function calculateImageSize({ originalHeight, originalWidth, maxHiddenDots, maxH
621
621
  hideXDots: 0
622
622
  };
623
623
  }
624
- const k = originalHeight / originalWidth;
625
- //Getting the maximum possible axis hidden dots
626
- hideDots.x = Math.floor(Math.sqrt(maxHiddenDots / k));
627
- //The count of hidden dot's can't be less than 1
628
- if (hideDots.x <= 0)
629
- hideDots.x = 1;
630
- //Check the limit of the maximum allowed axis hidden dots
631
- if (maxHiddenAxisDots && maxHiddenAxisDots < hideDots.x)
632
- hideDots.x = maxHiddenAxisDots;
633
- //The count of dots should be odd
634
- if (hideDots.x % 2 === 0)
635
- hideDots.x--;
636
- imageSize.x = hideDots.x * dotSize;
637
- //Calculate opposite axis hidden dots based on axis value.
638
- //The value will be odd.
639
- //We use ceil to prevent dots covering by the image.
640
- hideDots.y = 1 + 2 * Math.ceil((hideDots.x * k - 1) / 2);
641
- imageSize.y = Math.round(imageSize.x * k);
642
- //If the result dots count is bigger than max - then decrease size and calculate again
643
- if (hideDots.y * hideDots.x > maxHiddenDots || (maxHiddenAxisDots && maxHiddenAxisDots < hideDots.y)) {
644
- if (maxHiddenAxisDots && maxHiddenAxisDots < hideDots.y) {
645
- hideDots.y = maxHiddenAxisDots;
646
- if (hideDots.y % 2 === 0)
647
- hideDots.x--;
624
+ if (maxHiddenAxisDots) {
625
+ // Adjust max size based on image aspect ratio and margin
626
+ const maxD = maxHiddenAxisDots - 2 * margin;
627
+ if (originalWidth > originalHeight) {
628
+ const newH = (maxD * originalHeight) / originalWidth + 2 * margin;
629
+ maxHiddenDots = Math.min(maxHiddenDots, maxHiddenAxisDots * newH);
648
630
  }
649
631
  else {
650
- hideDots.y -= 2;
632
+ const newW = (maxD * originalWidth) / originalHeight + 2 * margin;
633
+ maxHiddenDots = Math.min(maxHiddenDots, maxHiddenAxisDots * newW);
651
634
  }
652
- imageSize.y = hideDots.y * dotSize;
653
- hideDots.x = 1 + 2 * Math.ceil((hideDots.y / k - 1) / 2);
654
- imageSize.x = Math.round(imageSize.y / k);
635
+ }
636
+ const m2 = margin ** 2;
637
+ const w2 = originalWidth ** 2;
638
+ const h2 = originalHeight ** 2;
639
+ //Margin-adjuseted scale
640
+ const scale = (Math.sqrt(w2 * m2 + h2 * m2 + originalWidth * originalHeight * (maxHiddenDots - 2 * m2)) -
641
+ originalHeight * margin -
642
+ originalWidth * margin) /
643
+ (originalHeight * originalWidth);
644
+ hideDots.x = Math.max(1, Math.floor(originalWidth * scale + 2 * margin));
645
+ hideDots.y = Math.max(1, Math.floor(originalHeight * scale + 2 * margin));
646
+ if (hideDots.x > hideDots.y) {
647
+ //The count of dots should be odd
648
+ if (hideDots.x % 2 === 0)
649
+ hideDots.x--;
650
+ hideDots.y = Math.max(1, Math.ceil(((hideDots.x - 2 * margin) * originalHeight) / originalWidth + 2 * margin));
651
+ //The count of dots should be odd
652
+ if (hideDots.y % 2 === 0)
653
+ hideDots.y--;
654
+ }
655
+ else if (hideDots.x < hideDots.y) {
656
+ //The count of dots should be odd
657
+ if (hideDots.y % 2 === 0)
658
+ hideDots.y--;
659
+ hideDots.x = Math.max(1, Math.ceil(((hideDots.y - 2 * margin) * originalWidth) / originalHeight + 2 * margin));
660
+ //The count of dots should be odd
661
+ if (hideDots.x % 2 === 0)
662
+ hideDots.x--;
663
+ }
664
+ const maxW = (hideDots.x - 2 * margin) * dotSize;
665
+ const maxH = (hideDots.y - 2 * margin) * dotSize;
666
+ if (maxW <= 0 || maxH <= 0) {
667
+ return {
668
+ height: 0,
669
+ width: 0,
670
+ hideYDots: hideDots.y,
671
+ hideXDots: hideDots.x
672
+ };
673
+ }
674
+ imageSize.x = maxW;
675
+ imageSize.y = (maxW * originalHeight) / originalWidth;
676
+ if (imageSize.y > maxH) {
677
+ imageSize.y = maxH;
678
+ imageSize.x = (maxH * originalWidth) / originalHeight;
655
679
  }
656
680
  return {
657
- height: imageSize.y,
658
- width: imageSize.x,
681
+ height: Math.round(imageSize.y + 2 * margin * dotSize),
682
+ width: Math.round(imageSize.x + 2 * margin * dotSize),
659
683
  hideYDots: hideDots.y,
660
684
  hideXDots: hideDots.x
661
685
  };
@@ -774,7 +798,8 @@ class QRSVG {
774
798
  originalHeight: size.height,
775
799
  maxHiddenDots,
776
800
  maxHiddenAxisDots: count - 14,
777
- dotSize
801
+ dotSize,
802
+ margin: imageOptions.margin
778
803
  });
779
804
  }
780
805
  this.drawBackground();
@@ -796,7 +821,7 @@ class QRSVG {
796
821
  return true;
797
822
  });
798
823
  this.drawCorners();
799
- if (this._options.image) {
824
+ if (this._options.image && drawImageSize.width > 0 && drawImageSize.height > 0) {
800
825
  await this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
801
826
  }
802
827
  }