@liquid-js/qr-code-styling 1.9.0 → 1.10.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/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.
@@ -4,6 +4,7 @@ export interface RequiredOptions extends Options {
4
4
  shape: ShapeType;
5
5
  width: number;
6
6
  height: number;
7
+ margin: number;
7
8
  data: string;
8
9
  qrOptions: {
9
10
  typeNumber: TypeNumber;
@@ -168,6 +168,7 @@ const defaultOptions = {
168
168
  shape: exports.ShapeType.square,
169
169
  width: undefined,
170
170
  height: undefined,
171
+ margin: 0,
171
172
  data: "",
172
173
  qrOptions: {
173
174
  typeNumber: TypeNumber[0],
@@ -613,7 +614,11 @@ class QRDot {
613
614
  function calculateImageSize({ originalHeight, originalWidth, maxHiddenDots, maxHiddenAxisDots, dotSize, margin }) {
614
615
  const hideDots = { x: 0, y: 0 };
615
616
  const imageSize = { x: 0, y: 0 };
616
- if (originalHeight <= 0 || originalWidth <= 0 || maxHiddenDots <= 0 || dotSize <= 0) {
617
+ if (originalHeight <= 0 ||
618
+ originalWidth <= 0 ||
619
+ maxHiddenDots <= 0 ||
620
+ dotSize <= 0 ||
621
+ 4 * margin ** 2 > maxHiddenDots) {
617
622
  return {
618
623
  height: 0,
619
624
  width: 0,
@@ -621,56 +626,47 @@ function calculateImageSize({ originalHeight, originalWidth, maxHiddenDots, maxH
621
626
  hideXDots: 0
622
627
  };
623
628
  }
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
- }
629
+ //If set, ensure maxHiddenAxisDots is odd and at least 1
630
+ if (maxHiddenAxisDots && maxHiddenAxisDots % 2 == 0)
631
+ maxHiddenAxisDots = Math.max(1, maxHiddenAxisDots - 1);
632
+ //Determine the bigger and smaller image dimension
633
+ const dimMax = Math.max(originalWidth, originalHeight);
634
+ const dimMin = Math.min(originalWidth, originalHeight);
635
+ //Find max image size along the bigger dimension
636
+ let lower = 0, upper = (maxHiddenAxisDots || maxHiddenDots) - 2 * margin;
637
+ do {
638
+ const size = lower + (upper - lower) / 2;
639
+ let dotsMax = Math.max(1, Math.ceil(size + 2 * margin));
640
+ if (dotsMax % 2 == 0)
641
+ dotsMax++;
642
+ let dotsMin = Math.max(1, Math.ceil((size * dimMin) / dimMax + 2 * margin));
643
+ if (dotsMin % 2 == 0)
644
+ dotsMin++;
645
+ if (dotsMax * dotsMin > maxHiddenDots)
646
+ upper = size;
647
+ else
648
+ lower = size;
649
+ } while (Math.abs(lower - upper) > 0.001);
650
+ //Calculate how many dots the given size corresponds to
651
+ const size = lower;
652
+ let hideMax = Math.max(1, Math.ceil(size + 2 * margin));
653
+ if (hideMax % 2 == 0)
654
+ hideMax++;
655
+ let hideMin = Math.max(1, Math.ceil((size * dimMin) / dimMax + 2 * margin));
656
+ if (hideMin % 2 == 0)
657
+ hideMin++;
658
+ //Set hidden dots along bigger and smaller dimension to the right axis
659
+ if (originalWidth > originalHeight) {
660
+ hideDots.x = hideMax;
661
+ hideDots.y = hideMin;
635
662
  }
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
+ else {
664
+ hideDots.x = hideMin;
665
+ hideDots.y = hideMax;
663
666
  }
667
+ //Fit image to the hidden dots
664
668
  const maxW = (hideDots.x - 2 * margin) * dotSize;
665
669
  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
670
  imageSize.x = maxW;
675
671
  imageSize.y = (maxW * originalHeight) / originalWidth;
676
672
  if (imageSize.y > maxH) {
@@ -779,7 +775,7 @@ class QRSVG {
779
775
  const count = qr.getModuleCount();
780
776
  const minSize = Math.min(this._options.width, this._options.height);
781
777
  const realQRSize = this._options.shape === exports.ShapeType.circle ? minSize / Math.sqrt(2) : minSize;
782
- const dotSize = Math.floor(realQRSize / count);
778
+ const dotSize = Math.floor(realQRSize / (count + 2 * this._options.margin));
783
779
  let drawImageSize = {
784
780
  hideXDots: 0,
785
781
  hideYDots: 0,
@@ -865,7 +861,7 @@ class QRSVG {
865
861
  }
866
862
  const minSize = Math.min(options.width, options.height);
867
863
  const realQRSize = options.shape === exports.ShapeType.circle ? minSize / Math.sqrt(2) : minSize;
868
- const dotSize = Math.floor(realQRSize / count);
864
+ const dotSize = Math.floor(realQRSize / (count + 2 * this._options.margin));
869
865
  const xBeginning = Math.floor((options.width - count * dotSize) / 2);
870
866
  const yBeginning = Math.floor((options.height - count * dotSize) / 2);
871
867
  const dot = new QRDot({ svg: this._element, type: options.dotsOptions.type, document: this._document });
@@ -903,7 +899,7 @@ class QRSVG {
903
899
  }
904
900
  }
905
901
  if (options.shape === exports.ShapeType.circle) {
906
- const additionalDots = Math.floor((minSize / dotSize - count) / 2);
902
+ const additionalDots = Math.floor((minSize / dotSize - count - 2 * this._options.margin) / 2);
907
903
  const fakeCount = count + additionalDots * 2;
908
904
  const xFakeBeginning = xBeginning - additionalDots * dotSize;
909
905
  const yFakeBeginning = yBeginning - additionalDots * dotSize;
@@ -955,7 +951,7 @@ class QRSVG {
955
951
  const count = this._qr.getModuleCount();
956
952
  const minSize = Math.min(options.width, options.height);
957
953
  const realQRSize = options.shape === exports.ShapeType.circle ? minSize / Math.sqrt(2) : minSize;
958
- const dotSize = Math.floor(realQRSize / count);
954
+ const dotSize = Math.floor(realQRSize / (count + 2 * this._options.margin));
959
955
  const cornersSquareSize = dotSize * 7;
960
956
  const cornersDotSize = dotSize * 3;
961
957
  const xBeginning = Math.floor((options.width - count * dotSize) / 2);
@@ -1181,7 +1177,7 @@ let QRCodeStyling$1 = class QRCodeStyling {
1181
1177
  if (!this._qr) {
1182
1178
  return;
1183
1179
  }
1184
- this._options.width = this._options.height = 10 * this._qr.getModuleCount();
1180
+ this._options.width = this._options.height = 10 * (this._qr.getModuleCount() + 2 * this._options.margin);
1185
1181
  const qrSVG = new QRSVG(this._options);
1186
1182
  this._svg = qrSVG.getElement();
1187
1183
  this._svgDrawingPromise = qrSVG.drawQR(this._qr).then(() => {