somdigi-qr-generator 1.0.9 → 1.0.11

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/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  const QRGenerator = require("./src/core");
2
+ const ImageMerger = require("./src/image-merger");
2
3
 
3
4
  module.exports = {
4
- QRGenerator
5
+ QRGenerator,
6
+ ImageMerger
5
7
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "somdigi-qr-generator",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/src/core.js CHANGED
@@ -6,17 +6,19 @@ class QRGenerator {
6
6
  static async generate({
7
7
  data,
8
8
  size = 1024,
9
- margin = 1,
10
- dotRadius = 0.48,
9
+ margin = 2,
10
+ dotRadius = 0.45,
11
11
  gradient = null,
12
12
  errorCorrectionLevel = "M"
13
13
  }) {
14
14
  if (!data) throw new Error("data required");
15
15
 
16
+ // Generate QR matrix
16
17
  const qr = QRCode.create(data, { errorCorrectionLevel });
17
18
  const cells = qr.modules;
18
19
  const count = cells.size;
19
20
 
21
+ // REAL viewbox size (margin kiri + kanan)
20
22
  const viewSize = count + margin * 2;
21
23
  const fill = gradient ? "url(#g)" : "#000";
22
24
 
@@ -28,21 +30,24 @@ class QRGenerator {
28
30
  if (this.#isFinder(x, y, count)) continue;
29
31
 
30
32
  dots.push(
31
- `<circle cx="${x + margin + 0.5}" cy="${y + margin + 0.5}" r="${dotRadius}" fill="${fill}" />`
33
+ `<circle cx="${x + margin + 0.5}"
34
+ cy="${y + margin + 0.5}"
35
+ r="${dotRadius}"
36
+ fill="${fill}" />`
32
37
  );
33
38
  }
34
39
  }
35
40
 
36
41
  const svg = `
37
- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${viewSize} ${viewSize}">
42
+ <svg xmlns="http://www.w3.org/2000/svg"
43
+ viewBox="0 0 ${viewSize} ${viewSize}">
38
44
  ${gradient ? this.#gradientDef(gradient) : ""}
39
45
  <rect width="100%" height="100%" fill="white"/>
40
46
 
41
- ${this.#finder(viewSize, margin, fill)}
47
+ ${this.#finder(count, margin, fill)}
42
48
 
43
49
  ${dots.join("")}
44
- </svg>
45
- `;
50
+ </svg>`;
46
51
 
47
52
  return sharp(Buffer.from(svg))
48
53
  .resize(size, size)
@@ -51,22 +56,28 @@ class QRGenerator {
51
56
  }
52
57
 
53
58
  // =============================
54
- // FINDER (SOLID + OUTLINE)
59
+ // FINDER PATTERN (DINAMIS)
55
60
  // =============================
56
- static #finder(_, margin, fill) {
57
- const p = [
61
+ static #finder(size, margin, fill) {
62
+ const pos = [
58
63
  { x: 0, y: 0 },
59
- { x: 21, y: 0 },
60
- { x: 0, y: 21 }
64
+ { x: size - 7, y: 0 },
65
+ { x: 0, y: size - 7 }
61
66
  ];
62
67
 
63
- return p.map(v => `
64
- <rect x="${v.x + margin}" y="${v.y + margin}" width="7" height="7" rx="2" fill="${fill}"/>
65
- <rect x="${v.x + margin + 1}" y="${v.y + margin + 1}" width="5" height="5" rx="1.5" fill="white"/>
66
- <rect x="${v.x + margin + 2}" y="${v.y + margin + 2}" width="3" height="3" rx="1" fill="${fill}"/>
68
+ return pos.map(p => `
69
+ <rect x="${p.x + margin}" y="${p.y + margin}"
70
+ width="7" height="7" rx="2" fill="${fill}"/>
71
+ <rect x="${p.x + margin + 1}" y="${p.y + margin + 1}"
72
+ width="5" height="5" rx="1.5" fill="white"/>
73
+ <rect x="${p.x + margin + 2}" y="${p.y + margin + 2}"
74
+ width="3" height="3" rx="1" fill="${fill}"/>
67
75
  `).join("");
68
76
  }
69
77
 
78
+ // =============================
79
+ // EXCLUDE FINDER AREA
80
+ // =============================
70
81
  static #isFinder(x, y, size) {
71
82
  return (
72
83
  (x < 7 && y < 7) ||
@@ -75,10 +86,15 @@ class QRGenerator {
75
86
  );
76
87
  }
77
88
 
89
+ // =============================
90
+ // GRADIENT SUPPORT
91
+ // =============================
78
92
  static #gradientDef([from, to]) {
79
93
  return `
80
94
  <defs>
81
- <linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="100%">
95
+ <linearGradient id="g"
96
+ x1="0%" y1="0%"
97
+ x2="100%" y2="100%">
82
98
  <stop offset="0%" stop-color="${from}" />
83
99
  <stop offset="100%" stop-color="${to}" />
84
100
  </linearGradient>
@@ -0,0 +1,59 @@
1
+ const sharp = require("sharp");
2
+
3
+ class ImageMerger {
4
+
5
+ /**
6
+ * Merge QR image into frame image
7
+ * @param {Object} options
8
+ * @param {string|Buffer} options.qr Path / Buffer QR image
9
+ * @param {string|Buffer} options.frame Path / Buffer frame image
10
+ * @param {number} [options.qrScale=0.75] QR size relative to frame (0.5 - 0.9)
11
+ * @param {number} [options.offsetX=0] Move QR horizontally
12
+ * @param {number} [options.offsetY=0] Move QR vertically
13
+ * @returns {Promise<Buffer>}
14
+ */
15
+ static async merge({
16
+ qr,
17
+ frame,
18
+ qrScale = 0.75,
19
+ offsetX = 0,
20
+ offsetY = 0
21
+ }) {
22
+ if (!qr || !frame) {
23
+ throw new Error("qr and frame are required");
24
+ }
25
+
26
+ // Load frame
27
+ const frameImage = sharp(frame);
28
+ const frameMeta = await frameImage.metadata();
29
+
30
+ // Calculate QR size
31
+ const qrSize = Math.floor(
32
+ Math.min(frameMeta.width, frameMeta.height) * qrScale
33
+ );
34
+
35
+ // Resize QR
36
+ const qrBuffer = await sharp(qr)
37
+ .resize(qrSize, qrSize)
38
+ .png()
39
+ .toBuffer();
40
+
41
+ // Center position
42
+ const left = Math.floor((frameMeta.width - qrSize) / 2 + offsetX);
43
+ const top = Math.floor((frameMeta.height - qrSize) / 2 + offsetY);
44
+
45
+ // Composite
46
+ return frameImage
47
+ .composite([
48
+ {
49
+ input: qrBuffer,
50
+ left,
51
+ top
52
+ }
53
+ ])
54
+ .png({ quality: 100 })
55
+ .toBuffer();
56
+ }
57
+ }
58
+
59
+ module.exports = ImageMerger;