@liquid-js/qr-code-styling 1.9.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.
@@ -613,7 +613,11 @@ class QRDot {
613
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,56 +625,47 @@ function calculateImageSize({ originalHeight, originalWidth, maxHiddenDots, maxH
621
625
  hideXDots: 0
622
626
  };
623
627
  }
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);
630
- }
631
- else {
632
- const newW = (maxD * originalWidth) / originalHeight + 2 * margin;
633
- maxHiddenDots = Math.min(maxHiddenDots, maxHiddenAxisDots * newW);
634
- }
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;
635
661
  }
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--;
662
+ else {
663
+ hideDots.x = hideMin;
664
+ hideDots.y = hideMax;
663
665
  }
666
+ //Fit image to the hidden dots
664
667
  const maxW = (hideDots.x - 2 * margin) * dotSize;
665
668
  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
669
  imageSize.x = maxW;
675
670
  imageSize.y = (maxW * originalHeight) / originalWidth;
676
671
  if (imageSize.y > maxH) {