@liquid-js/qr-code-styling 1.8.0 → 1.9.1

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2019 Denys Kozak
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Denys Kozak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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,10 +610,14 @@ 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
- if (originalHeight <= 0 || originalWidth <= 0 || maxHiddenDots <= 0 || dotSize <= 0) {
616
+ if (originalHeight <= 0 ||
617
+ originalWidth <= 0 ||
618
+ maxHiddenDots <= 0 ||
619
+ dotSize <= 0 ||
620
+ 4 * margin ** 2 > maxHiddenDots) {
617
621
  return {
618
622
  height: 0,
619
623
  width: 0,
@@ -621,41 +625,56 @@ function calculateImageSize({ originalHeight, originalWidth, maxHiddenDots, maxH
621
625
  hideXDots: 0
622
626
  };
623
627
  }
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--;
648
- }
649
- else {
650
- hideDots.y -= 2;
651
- }
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);
628
+ //If set, ensure maxHiddenAxisDots is odd and at least 1
629
+ if (maxHiddenAxisDots && maxHiddenAxisDots % 2 == 0)
630
+ maxHiddenAxisDots = Math.max(1, maxHiddenAxisDots - 1);
631
+ //Determine the bigger and smaller image dimension
632
+ const dimMax = Math.max(originalWidth, originalHeight);
633
+ const dimMin = Math.min(originalWidth, originalHeight);
634
+ //Find max image size along the bigger dimension
635
+ let lower = 0, upper = (maxHiddenAxisDots || maxHiddenDots) - 2 * margin;
636
+ do {
637
+ const size = lower + (upper - lower) / 2;
638
+ let dotsMax = Math.max(1, Math.ceil(size + 2 * margin));
639
+ if (dotsMax % 2 == 0)
640
+ dotsMax++;
641
+ let dotsMin = Math.max(1, Math.ceil((size * dimMin) / dimMax + 2 * margin));
642
+ if (dotsMin % 2 == 0)
643
+ dotsMin++;
644
+ if (dotsMax * dotsMin > maxHiddenDots)
645
+ upper = size;
646
+ else
647
+ lower = size;
648
+ } while (Math.abs(lower - upper) > 0.001);
649
+ //Calculate how many dots the given size corresponds to
650
+ const size = lower;
651
+ let hideMax = Math.max(1, Math.ceil(size + 2 * margin));
652
+ if (hideMax % 2 == 0)
653
+ hideMax++;
654
+ let hideMin = Math.max(1, Math.ceil((size * dimMin) / dimMax + 2 * margin));
655
+ if (hideMin % 2 == 0)
656
+ hideMin++;
657
+ //Set hidden dots along bigger and smaller dimension to the right axis
658
+ if (originalWidth > originalHeight) {
659
+ hideDots.x = hideMax;
660
+ hideDots.y = hideMin;
661
+ }
662
+ else {
663
+ hideDots.x = hideMin;
664
+ hideDots.y = hideMax;
665
+ }
666
+ //Fit image to the hidden dots
667
+ const maxW = (hideDots.x - 2 * margin) * dotSize;
668
+ const maxH = (hideDots.y - 2 * margin) * dotSize;
669
+ imageSize.x = maxW;
670
+ imageSize.y = (maxW * originalHeight) / originalWidth;
671
+ if (imageSize.y > maxH) {
672
+ imageSize.y = maxH;
673
+ imageSize.x = (maxH * originalWidth) / originalHeight;
655
674
  }
656
675
  return {
657
- height: imageSize.y,
658
- width: imageSize.x,
676
+ height: Math.round(imageSize.y + 2 * margin * dotSize),
677
+ width: Math.round(imageSize.x + 2 * margin * dotSize),
659
678
  hideYDots: hideDots.y,
660
679
  hideXDots: hideDots.x
661
680
  };
@@ -774,7 +793,8 @@ class QRSVG {
774
793
  originalHeight: size.height,
775
794
  maxHiddenDots,
776
795
  maxHiddenAxisDots: count - 14,
777
- dotSize
796
+ dotSize,
797
+ margin: imageOptions.margin
778
798
  });
779
799
  }
780
800
  this.drawBackground();
@@ -796,7 +816,7 @@ class QRSVG {
796
816
  return true;
797
817
  });
798
818
  this.drawCorners();
799
- if (this._options.image) {
819
+ if (this._options.image && drawImageSize.width > 0 && drawImageSize.height > 0) {
800
820
  await this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
801
821
  }
802
822
  }