|
1 | | -import qrcodegen from "./codegen"; |
2 | | -import { |
| 1 | +export { generateSVG } from "./svg"; |
| 2 | +export type { QRCodeOptions, ImageSettings } from "./types"; |
| 3 | +export { |
3 | 4 | DEFAULT_BGCOLOR, |
4 | 5 | DEFAULT_FGCOLOR, |
5 | 6 | DEFAULT_LEVEL, |
6 | 7 | DEFAULT_MARGIN, |
7 | 8 | DEFAULT_SIZE, |
8 | | - ERROR_CORRECTION_LEVELS, |
9 | 9 | } from "./constants"; |
10 | | -import type { ImageSettings } from "./types"; |
11 | | -import { excavateModules, getImageSettings, generatePath } from "./utils"; |
12 | | - |
13 | | -export type QRCodeOptions = { |
14 | | - /** The content to encode in the QR code */ |
15 | | - value: string; |
16 | | - /** Width and height of the QR code in pixels. Defaults to 600 */ |
17 | | - size?: number; |
18 | | - /** Error correction level. "L" (7%), "M" (15%), "Q" (25%), or "H" (30%). Defaults to "L" */ |
19 | | - level?: string; |
20 | | - /** Background color of the QR code. Accepts any valid CSS color. Defaults to "#FFFFFF" */ |
21 | | - bgColor?: string; |
22 | | - /** Foreground color of the QR code. Accepts any valid CSS color. Defaults to "#000000" */ |
23 | | - fgColor?: string; |
24 | | - /** Quiet zone margin around the QR code in modules. Defaults to 4 */ |
25 | | - margin?: number; |
26 | | - /** Settings for embedding an image/logo in the QR code center */ |
27 | | - imageSettings?: ImageSettings; |
28 | | - /** Additional attributes to be added to the SVG element */ |
29 | | - [key: string]: unknown; |
30 | | -}; |
31 | | - |
32 | | -/** |
33 | | - * Generates an SVG QR code as a string. Use this when you need the raw SVG markup. |
34 | | - */ |
35 | | -export function generateSVG(options: QRCodeOptions): string { |
36 | | - const { |
37 | | - value, |
38 | | - size = DEFAULT_SIZE, |
39 | | - level = DEFAULT_LEVEL, |
40 | | - bgColor = DEFAULT_BGCOLOR, |
41 | | - fgColor = DEFAULT_FGCOLOR, |
42 | | - margin = DEFAULT_MARGIN, |
43 | | - imageSettings, |
44 | | - ...otherProps |
45 | | - } = options; |
46 | | - |
47 | | - const shouldUseHigherErrorLevel = |
48 | | - imageSettings?.excavate && (level === "L" || level === "M"); |
49 | | - |
50 | | - // Use a higher error correction level 'Q' when excavation is enabled |
51 | | - // to ensure the QR code remains scannable despite the removed modules. |
52 | | - const effectiveLevel = shouldUseHigherErrorLevel ? "Q" : level; |
53 | | - |
54 | | - let cells = qrcodegen.QrCode.encodeText( |
55 | | - value, |
56 | | - ERROR_CORRECTION_LEVELS[effectiveLevel] |
57 | | - ).getModules(); |
58 | | - |
59 | | - const numCells = cells.length + margin * 2; |
60 | | - const calculatedImageSettings = getImageSettings( |
61 | | - cells, |
62 | | - size, |
63 | | - margin, |
64 | | - imageSettings |
65 | | - ); |
66 | | - |
67 | | - let imageTag = ""; |
68 | | - if (imageSettings != null && calculatedImageSettings != null) { |
69 | | - if (calculatedImageSettings.excavation != null) { |
70 | | - cells = excavateModules(cells, calculatedImageSettings.excavation); |
71 | | - } |
72 | | - |
73 | | - // Create an SVG image tag |
74 | | - imageTag = `<image |
75 | | - href="${imageSettings.src}" |
76 | | - height="${calculatedImageSettings.h}" |
77 | | - width="${calculatedImageSettings.w}" |
78 | | - x="${calculatedImageSettings.x + margin}" |
79 | | - y="${calculatedImageSettings.y + margin}" |
80 | | - preserveAspectRatio="none" |
81 | | - />`; |
82 | | - } |
83 | | - |
84 | | - // Drawing strategy: instead of a rect per module, we're going to create a |
85 | | - // single path for the dark modules and layer that on top of a light rect, |
86 | | - // for a total of 2 DOM nodes. We pay a bit more in string concat but that's |
87 | | - // way faster than DOM ops. |
88 | | - // For level 1, 441 nodes -> 2 |
89 | | - // For level 40, 31329 -> 2 |
90 | | - const fgPath = generatePath(cells, margin); |
91 | | - |
92 | | - // Convert additional props to attribute string |
93 | | - let attributesString = ""; |
94 | | - for (const [key, value] of Object.entries(otherProps)) { |
95 | | - if (value != null) { |
96 | | - attributesString += ` ${key}="${value}"`; |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - return `<svg |
101 | | - xmlns="http://www.w3.org/2000/svg" |
102 | | - height="${size}" |
103 | | - width="${size}" |
104 | | - viewBox="0 0 ${numCells} ${numCells}" |
105 | | - role="img" |
106 | | - aria-label="QR Code" |
107 | | - data-generator="tinyqrc" |
108 | | - ${attributesString} |
109 | | - > |
110 | | - <title>QR Code</title> |
111 | | - <desc>Scan this QR code with your mobile device</desc> |
112 | | - <path |
113 | | - fill="${bgColor}" |
114 | | - d="M0,0 h${numCells}v${numCells}H0z" |
115 | | - shapeRendering="crispEdges" |
116 | | - /> |
117 | | - <path fill="${fgColor}" d="${fgPath}" shapeRendering="crispEdges" /> |
118 | | - ${imageTag} |
119 | | - </svg>`; |
120 | | -} |
0 commit comments