qreator 9.3.0 → 9.5.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.
@@ -1,9 +1,11 @@
1
- import { Data, EcLevel, Matrix } from "./typing/types";
1
+ import { BitMatrix, Data, EcLevel, Matrix } from "./typing/types";
2
2
  export declare function init(version: number): Matrix;
3
3
  export declare function fillFinders(matrix: Matrix): void;
4
+ export declare function zeroFillFinders(matrix: BitMatrix): void;
4
5
  export declare function fillAlignAndTiming(matrix: Matrix): void;
5
6
  export declare function fillStub(matrix: Matrix): void;
6
7
  export declare const fillReserved: (matrix: Matrix, ec_level: EcLevel, mask: number) => void;
7
8
  export declare const fillData: (matrix: Matrix, data: Data, mask: number) => void;
8
9
  export declare function calculatePenalty(matrix: Matrix): number;
9
- export declare function getMatrix(data: Data): number[][];
10
+ export declare function getMatrix(data: Data): BitMatrix;
11
+ export declare function clearMatrixCenter(matrix: BitMatrix, widthPct: number, heightPct: number): BitMatrix;
@@ -25751,35 +25751,50 @@
25751
25751
 
25752
25752
  function init(version) {
25753
25753
  const N = (version << 2) + 0b10001;
25754
- const matrix = [];
25755
- let zeros = Array(N).fill(0);
25756
- for (let i = 0; i < N; i++) {
25757
- matrix[i] = [...zeros];
25758
- }
25759
- return matrix;
25754
+ return Array(N).fill(null).map(() => Array(N).fill(0));
25760
25755
  }
25761
25756
  function fillFinders(matrix) {
25762
25757
  const N = matrix.length;
25763
- for (var i = -3; i <= 3; i++) {
25758
+ for (let i = -3; i <= 3; i++) {
25764
25759
  for (let j = -3; j <= 3; j++) {
25765
25760
  const max = Math.max(i, j);
25766
25761
  const min = Math.min(i, j);
25767
25762
  const pixel = (max == 2 && min >= -2) || (min == -2 && max <= 2)
25768
- ? 0x80
25769
- : 0x81;
25763
+ ? 128
25764
+ : 129;
25770
25765
  matrix[3 + i][3 + j] = pixel;
25771
25766
  matrix[3 + i][N - 4 + j] = pixel;
25772
25767
  matrix[N - 4 + i][3 + j] = pixel;
25773
25768
  }
25774
25769
  }
25775
- for (var i = 0; i < 8; i++) {
25770
+ for (let i = 0; i < 8; i++) {
25771
+ matrix[7][i] =
25772
+ matrix[i][7] =
25773
+ matrix[7][N - i - 1] =
25774
+ matrix[i][N - 8] =
25775
+ matrix[N - 8][i] =
25776
+ matrix[N - 1 - i][7] =
25777
+ 128;
25778
+ }
25779
+ }
25780
+ function zeroFillFinders(matrix) {
25781
+ const N = matrix.length;
25782
+ const zeroPixel = 0;
25783
+ for (let i = -3; i <= 3; i++) {
25784
+ for (let j = -3; j <= 3; j++) {
25785
+ matrix[3 + i][3 + j] = zeroPixel;
25786
+ matrix[3 + i][N - 4 + j] = zeroPixel;
25787
+ matrix[N - 4 + i][3 + j] = zeroPixel;
25788
+ }
25789
+ }
25790
+ for (let i = 0; i < 8; i++) {
25776
25791
  matrix[7][i] =
25777
25792
  matrix[i][7] =
25778
25793
  matrix[7][N - i - 1] =
25779
25794
  matrix[i][N - 8] =
25780
25795
  matrix[N - 8][i] =
25781
25796
  matrix[N - 1 - i][7] =
25782
- 0x80;
25797
+ zeroPixel;
25783
25798
  }
25784
25799
  }
25785
25800
  function fillAlignAndTiming(matrix) {
@@ -26093,7 +26108,22 @@
26093
26108
  }
26094
26109
  fillData(matrix, data, bestMask);
26095
26110
  fillReserved(matrix, data.ec_level, bestMask);
26096
- return matrix.map((row) => row.map((cell) => cell & 1));
26111
+ return matrix.map((row) => row.map((cell) => (cell & 1)));
26112
+ }
26113
+ function clearMatrixCenter(matrix, widthPct, heightPct) {
26114
+ matrix = matrix.map((x) => x.slice());
26115
+ const mW = matrix.length;
26116
+ const cW = Math.ceil(((mW * widthPct) / 100 + (mW % 2)) / 2) * 2 - (mW % 2);
26117
+ const mH = matrix[0]?.length ?? 0;
26118
+ const cH = Math.ceil(((mH * heightPct) / 100 + (mH % 2)) / 2) * 2 - (mH % 2);
26119
+ const clearStartX = Math.round((mW - cW) / 2);
26120
+ const clearStartY = Math.round((mH - cH) / 2);
26121
+ for (let x = clearStartX; x < clearStartX + cW; x += 1) {
26122
+ for (let y = clearStartY; y < clearStartY + cH; y += 1) {
26123
+ matrix[x][y] = 0;
26124
+ }
26125
+ }
26126
+ return matrix;
26097
26127
  }
26098
26128
 
26099
26129
  const EC_LEVELS = ["L", "M", "Q", "H"];
@@ -26134,7 +26164,7 @@
26134
26164
  throw new Error("Too much data");
26135
26165
  }
26136
26166
  function fillTemplate(message, template) {
26137
- const blocks = new Uint8Array(template.data_len);
26167
+ const blocks = new Uint8ClampedArray(template.data_len);
26138
26168
  let messageUpdated;
26139
26169
  if (template.version < 10) {
26140
26170
  messageUpdated = message.data1;
@@ -26691,7 +26721,7 @@
26691
26721
  const defaults = type === "png" ? BITMAP_OPTIONS : VECTOR_OPTIONS;
26692
26722
  return { ...defaults, ...inOptions };
26693
26723
  }
26694
- function getSVGPath(matrix, size, margin = 0, borderRadius = 0) {
26724
+ function getDotsSVGPath(matrix, size, margin = 0, borderRadius = 0) {
26695
26725
  let rectangles = [];
26696
26726
  for (let x = 0; x < matrix.length; x++) {
26697
26727
  const column = matrix[x];
@@ -26705,7 +26735,7 @@
26705
26735
  rectangle.push(`M ${leftX} ${topY + borderRadius}`);
26706
26736
  rectangle.push(`L ${leftX} ${bottomY - borderRadius}`);
26707
26737
  if (borderRadius > 0) {
26708
- rectangle.push(`A ${borderRadius} ${borderRadius} 0 0 0 ${leftX + borderRadius} ${bottomY} `);
26738
+ rectangle.push(`A ${borderRadius} ${borderRadius} 0 0 0 ${leftX + borderRadius} ${bottomY}`);
26709
26739
  }
26710
26740
  rectangle.push(`L ${rightX - borderRadius} ${bottomY}`);
26711
26741
  if (borderRadius > 0) {
@@ -26750,7 +26780,11 @@
26750
26780
  const textDec = new TextDecoder();
26751
26781
  async function getPDF(text, inOptions) {
26752
26782
  const options = getOptions(inOptions);
26753
- const matrix = QR(text, options.ec_level, options.parse_url);
26783
+ let matrix = QR(text, options.ec_level, options.parse_url);
26784
+ zeroFillFinders(matrix);
26785
+ if (options.logo && options.logoWidth && options.logoHeight) {
26786
+ matrix = clearMatrixCenter(matrix, options.logoWidth, options.logoHeight);
26787
+ }
26754
26788
  return PDF({ matrix, ...options });
26755
26789
  }
26756
26790
  function colorToRGB(color) {
@@ -26758,29 +26792,27 @@
26758
26792
  const [red, green, blue] = colorString.get.rgb(color);
26759
26793
  return [red / 255, green / 255, blue / 255];
26760
26794
  }
26761
- return [
26762
- ((color >>> 24) % 256) / 255,
26763
- ((color >>> 16) % 256) / 255,
26764
- ((color >>> 8) % 256) / 255,
26765
- ];
26795
+ return [((color >>> 24) % 256) / 255, ((color >>> 16) % 256) / 255, ((color >>> 8) % 256) / 255];
26766
26796
  }
26767
26797
  function getOpacity(color) {
26768
26798
  if (typeof color === "string") {
26769
26799
  return colorString.get.rgb(color)[3];
26770
26800
  }
26771
- return ((color % 256) / 255);
26801
+ return (color % 256) / 255;
26772
26802
  }
26773
26803
  async function PDF({ matrix, margin, logo, logoWidth, logoHeight, color, bgColor, borderRadius, }) {
26774
26804
  const size = 9;
26805
+ const marginPx = margin * size;
26806
+ const matrixSizePx = matrix.length * size;
26807
+ const imageSizePx = matrixSizePx + 2 * marginPx;
26775
26808
  const document = await PDFDocument.create();
26776
- const pageSize = (matrix.length + 2 * margin) * size;
26777
- const page = document.addPage([pageSize, pageSize]);
26809
+ const page = document.addPage([imageSizePx, imageSizePx]);
26778
26810
  page.drawSquare({
26779
- size: pageSize,
26811
+ size: imageSizePx,
26780
26812
  color: rgb(...colorToRGB(bgColor)),
26781
26813
  });
26782
26814
  page.moveTo(0, page.getHeight());
26783
- const path = getSVGPath(matrix, size, margin * size, borderRadius);
26815
+ const path = getDotsSVGPath(matrix, size, marginPx, borderRadius);
26784
26816
  page.drawSvgPath(path, {
26785
26817
  color: rgb(...colorToRGB(color)),
26786
26818
  opacity: getOpacity(color),
@@ -26796,12 +26828,13 @@
26796
26828
  else {
26797
26829
  logoData = await document.embedJpg(logo);
26798
26830
  }
26831
+ const logoWidthPx = (logoWidth / 100) * matrixSizePx;
26832
+ const logoHeightPx = (logoHeight / 100) * matrixSizePx;
26799
26833
  page.drawImage(logoData, {
26800
- x: page.getWidth() / 2 - (logoWidth / 100) * (page.getWidth() / 2),
26801
- y: page.getHeight() / 2 -
26802
- (logoHeight / 100) * (page.getWidth() / 2),
26803
- width: (logoWidth / 100) * page.getWidth(),
26804
- height: (logoHeight / 100) * page.getHeight(),
26834
+ x: (imageSizePx - logoWidthPx) / 2,
26835
+ y: (imageSizePx - logoHeightPx) / 2,
26836
+ width: logoWidthPx,
26837
+ height: logoHeightPx,
26805
26838
  });
26806
26839
  }
26807
26840
  return document.save();