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.
- package/package.json +1 -1
- package/src/core.js +56 -48
package/package.json
CHANGED
package/src/core.js
CHANGED
|
@@ -33,63 +33,71 @@ class QRGenerator {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
static async _rounded(options) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
<
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
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;
|