somdigi-qr-generator 1.0.4 → 1.0.5

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/core.js +56 -48
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "somdigi-qr-generator",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/src/core.js CHANGED
@@ -33,63 +33,71 @@ class QRGenerator {
33
33
  }
34
34
 
35
35
  static async _rounded(options) {
36
- const rawSVG = await QRCode.toString(options.data, {
37
- type: "svg",
38
- errorCorrectionLevel: "H",
39
- margin: 1
40
- });
41
-
42
- const viewBox = rawSVG.match(/viewBox="([^"]+)"/)[1];
43
- const paths = rawSVG.match(/<path[^>]*>/g) || [];
44
-
45
- const fill = options.gradient?.length
46
- ? "url(#g)"
47
- : "#000";
48
-
49
- const defs = options.gradient?.length
50
- ? `
51
- <defs>
52
- <linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="100%">
53
- ${options.gradient
54
- .map(
55
- (c, i) =>
56
- `<stop offset="${(i / (options.gradient.length - 1)) * 100}%" stop-color="${c}" />`
57
- )
58
- .join("")}
59
- </linearGradient>
60
- </defs>`
61
- : "";
62
-
63
- const dots = paths
64
- .map(p => {
65
- const m = p.match(/M(\d+) (\d+)/);
66
- if (!m) return "";
67
- return `<circle cx="${+m[1] + 0.5}" cy="${+m[2] + 0.5}" r="0.45" fill="${fill}" />`;
68
- })
69
- .join("");
70
-
71
- const logo = options.logo
72
- ? `
73
- <image
74
- href="${options.logo}"
75
- x="35%" y="35%"
76
- width="30%" height="30%"
77
- preserveAspectRatio="xMidYMid meet"
78
- />`
79
- : "";
80
-
81
- const svg = `
36
+ const rawSVG = await QRCode.toString(options.data, {
37
+ type: "svg",
38
+ errorCorrectionLevel: "H",
39
+ margin: 1
40
+ });
41
+
42
+ const viewBox = rawSVG.match(/viewBox="([^"]+)"/)[1];
43
+ const paths = rawSVG.match(/<path[^>]*>/g) || [];
44
+
45
+ const useGradient = options.gradient?.length > 1;
46
+ const fill = useGradient ? "url(#g)" : "#000";
47
+
48
+ const defs = useGradient
49
+ ? `
50
+ <defs>
51
+ <linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="100%">
52
+ ${options.gradient
53
+ .map(
54
+ (c, i) =>
55
+ `<stop offset="${(i / (options.gradient.length - 1)) * 100}%" stop-color="${c}" />`
56
+ )
57
+ .join("")}
58
+ </linearGradient>
59
+ </defs>`
60
+ : "";
61
+
62
+ const dots = paths
63
+ .map(p => {
64
+ const m = p.match(/M(\d+) (\d+)/);
65
+ if (!m) return "";
66
+ return `<circle cx="${+m[1] + 0.5}" cy="${+m[2] + 0.5}" r="0.45" fill="${fill}" />`;
67
+ })
68
+ .join("");
69
+
70
+ const logo = options.logo
71
+ ? `
72
+ <image
73
+ href="${options.logo}"
74
+ x="35%" y="35%"
75
+ width="30%" height="30%"
76
+ preserveAspectRatio="xMidYMid meet"
77
+ />`
78
+ : "";
79
+
80
+ const svg = `
82
81
  <svg xmlns="http://www.w3.org/2000/svg"
83
82
  width="${options.size ?? 512}"
84
83
  height="${options.size ?? 512}"
85
84
  viewBox="${viewBox}">
85
+
86
+ <!-- BACKGROUND WAJIB -->
87
+ <rect width="100%" height="100%" fill="#fff"/>
88
+
86
89
  ${defs}
87
90
  ${dots}
88
91
  ${logo}
89
92
  </svg>`;
90
93
 
91
- return sharp(Buffer.from(svg)).png().toBuffer();
92
- }
94
+ return sharp(Buffer.from(svg))
95
+ .png({
96
+ background: "#ffffff"
97
+ })
98
+ .toBuffer();
99
+ }
100
+
93
101
  }
94
102
 
95
103
  module.exports = QRGenerator;