@pjaudiomv/qrcode-svg 1.0.0 → 1.0.2

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/LICENSE CHANGED
@@ -1,5 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
+ Copyright (c) 2026 pjaudiomv
3
4
  Copyright (c) 2016 papnkukn
4
5
 
5
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
package/README.md CHANGED
@@ -1,280 +1,186 @@
1
- ## Introduction
2
-
3
- This library has been written to generate a SVG image of QR Code in Node.js, goals:
4
- * pure JavaScript
5
- * no browser requirement
6
- * no external dependencies
7
- * generate SVG output
8
-
9
- ## Getting Started
10
-
11
- Install the package:
12
- ```bash
13
- npm install @lemaik/qrcode-svg
14
- ```
15
-
16
- Inline example:
17
- ```javascript
18
- var QRCode = require("@lemaik/qrcode-svg");
19
- var svg = new QRCode("Hello World!").svg();
20
- ```
21
-
22
- More options:
23
- ```javascript
24
- var qrcode = new QRCode({
25
- content: "http://github.com/",
26
- padding: 4,
27
- width: 256,
28
- height: 256,
29
- color: "#000000",
30
- background: "#ffffff",
31
- ecl: "M",
32
- });
33
- qrcode.save("sample.svg", function(error) {
34
- if (error) throw error;
35
- console.log("Done!");
36
- });
37
- ```
38
-
39
- ## Options
40
-
41
- **List of options:**
42
- * **content** - QR Code content, the only **required** parameter
43
- * **padding** - white space padding, `4` modules by default, `0` for no border
44
- * **width** - QR Code width in pixels
45
- * **height** - QR Code height in pixels
46
- * **color** - color of modules (squares), color name or hex string, e.g. `#000000`
47
- * **background** - color of background, color name or hex string, e.g. `white`
48
- * **ecl** - error correction level: `L`, `M`, `H`, `Q`
49
- * **join** - join modules (squares) into one shape, into the SVG `path` element, **recommended** for web and responsive use, default: `false`
50
- * **predefined** - to create a squares as pattern, then populate the canvas, default: `false`, see the output examples below
51
- * **pretty** - apply indents and new lines, default: `true`
52
- * **swap** - swap X and Y modules, only if you have issues with some QR readers, default: `false`
53
- * **xmlDeclaration** - prepend XML declaration to the SVG document, i.e. `<?xml version="1.0" standalone="yes"?>`, default: `true`
54
- * **container** - wrapping element, default: `svg`, see below
55
-
56
- **Container options:**
57
- * **svg** - populate squares in a SVG document with `width` and `height` attriute, recommended for converting to raster images or PDF where QR Code is being static (exact size)
58
- * **svg-viewbox** - populate squares in a SVG document with `viewBox` attriute, **recommended** for responsive web pages
59
- * **g** - put squares in `g` element, useful when you need to put multiple QR Codes in a single SVG document
60
- * **none** - no wrapper
61
-
62
- ## SVG output
63
-
64
- ### Editable squares
65
-
66
- This mode is useful for designers to manipulate with particular squares.
67
- Thus, one can open the QR Code in an editor, select particular modules, move around, change color, etc.
68
- However, some old SVG viewers may generate minor gaps between the squares - the side effect when rendering an image at certain zoom level.
69
-
70
- Default options
71
- ```javascript
72
- var qrcode = new QRCode({
73
- content: "Pretty Fox",
74
- join: false,
75
- predefined: false
76
- });
77
- ```
78
-
79
- Output with `rect` elements
80
- ```xml
81
- <?xml version="1.0" standalone="yes"?>
82
- <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="256" height="256">
83
- <rect x="0" y="0" width="256" height="256" style="fill:#ffffff;shape-rendering:crispEdges;"/>
84
- <rect x="16" y="16" width="8" height="8" style="fill:#000000;shape-rendering:crispEdges;"/>
85
- <rect x="24" y="16" width="8" height="8" style="fill:#000000;shape-rendering:crispEdges;"/>
86
- <rect x="32" y="16" width="8" height="8" style="fill:#000000;shape-rendering:crispEdges;"/>
87
- ...
88
- </svg>
89
- ```
90
-
91
- ### Responsive web page
92
-
93
- Squares joined into one `path` shape produce a compact file size, i.e. 4-5x reduced compared with `rect` elements.
94
- A single `path` element will result in an optimized rendering, thus not producing any minor gaps between the squares.
95
- Also using the container with `viewBox` attribute may contribute to the responsive scaling on the web.
96
-
97
- Set `join` to `true`
98
- ```javascript
99
- var qrcode = new QRCode({
100
- content: "Pretty Fox",
101
- join: true,
102
- container: "svg-viewbox" //Useful but not required
103
- });
104
- ```
105
-
106
- Output with `path` element
107
- ```xml
108
- <?xml version="1.0" standalone="yes"?>
109
- <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 256 256">
110
- <rect x="0" y="0" width="256" height="256" style="fill:beige;shape-rendering:crispEdges;"/>
111
- <path x="0" y="0" style="fill:blue;shape-rendering:crispEdges;" d="M35.31,35.31 V44.14 H44.14 V35.31 H35.31 Z..." />
112
- </svg>
113
- ```
114
-
115
- ### Predefined pattern
116
-
117
- Algorithm defines the square pattern once before populating a canvas. Useful if you want to generate QR Code with candies.
118
- However, some SVG software and converters do not support `defs` or `use` elements.
119
-
120
- Set `predefined` to `true`
121
- ```javascript
122
- var qrcode = new QRCode({
123
- content: "Pretty Fox",
124
- predefined: true
125
- });
126
- ```
127
-
128
- Output with `defs` and `use` elements
129
- ```xml
130
- <?xml version="1.0" standalone="yes"?>
131
- <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="256" height="256">
132
- <defs><path id="qrmodule" d="M0 0 h8.827586206896552 v8.827586206896552 H0 z" style="fill:maroon;shape-rendering:crispEdges;" /></defs>
133
- <rect x="0" y="0" width="256" height="256" style="fill:beige;shape-rendering:crispEdges;"/>
134
- <use x="35.310344827586206" y="35.310344827586206" href="#qrmodule" />
135
- <use x="44.13793103448276" y="35.310344827586206" href="#qrmodule" />
136
- <use x="52.96551724137931" y="35.310344827586206" href="#qrmodule" />
137
- <use x="61.79310344827586" y="35.310344827586206" href="#qrmodule" />
138
- <use x="70.62068965517241" y="35.310344827586206" href="#qrmodule" />
139
- ...
140
- </svg>
141
- ```
142
-
143
- ## Command Line
144
-
145
- ```
146
- Usage:
147
- qrcode-svg [options] <content>
148
-
149
- Options:
150
- --help Print this message
151
- --version, -v Print version number
152
- --padding , -p [value] Offset in number of modules
153
- --width, -w [px] Image width in pixels
154
- --height, -h [px] Image height in pixels
155
- --color, -fg [color] Foreground color, hex or name
156
- --background [color] Background color, hex or name
157
- --ecl [value] Error correction level: L, M, H, Q
158
- --join Join modules into one SVG path, i.e. for crisp rendering
159
- --predefined Use 'defs' and 'use' elements in SVG, i.e. for compact output
160
- --no-prettify Avoid indenting and new lines in SVG, i.e. for compact output
161
- --viewbox Use 'viewBox' instead of 'width' and 'height' attributes
162
- --swap-fix Swap X and Y modules to fix issues with some QR readers
163
- --output, -o [file] Output file name
164
- --force, -f Force overwrite
165
-
166
- Examples:
167
- qrcode-svg http://github.com
168
- qrcode-svg -f -o hello.svg "Hello World"
169
- qrcode-svg -p 4 -w 256 -h 256 --join --viewbox "Responsive..."
170
- qrcode-svg --padding 2 --width 120 --height 120 "Little fox..."
171
- qrcode-svg --color blue --background #ececec "...jumps over"
172
- ```
173
-
174
- ## Usage Scenarios
175
-
176
- ### Convert to other formats
177
-
178
- Using [html-pdf](https://www.npmjs.com/package/html-pdf) to convert SVG to PDF (or PNG or JPEG)
179
- ```javascript
180
- var QRCode = require('@lemaik/qrcode-svg');
181
- var svg = new QRCode('hello').svg();
182
- ...
183
- var pdf = require('html-pdf');
184
- pdf.create(svg, { border: 0, type: 'pdf' }).toFile('output.pdf', function(err, res) {
185
- ...
186
- });
187
- ```
188
-
189
- ### ASCII modules
190
-
191
- QR Code in ASCII to output in a shell
192
- ```javascript
193
- var QRCode = require('@lemaik/qrcode-svg');
194
-
195
- var hello = new QRCode("Hello World!");
196
- var modules = hello.qrcode.modules;
197
-
198
- var ascii = '';
199
- var length = modules.length;
200
- for (var y = 0; y < length; y++) {
201
- for (var x = 0; x < length; x++) {
202
- var module = modules[x][y];
203
- ascii += (module ? 'x' : ' ');
204
- }
205
- ascii += '\r\n';
206
- }
207
- console.log(ascii);
208
- ```
209
-
210
- ```
211
-
212
-
213
- xxxxxxx xx x x xxxxxxx
214
- x x xxxx x x x x
215
- x xxx x xx xx x x xxx x
216
- x xxx x xx x xxx x
217
- x xxx x x x x x xxx x
218
- x x x xx xx x x
219
- xxxxxxx x x x x x xxxxxxx
220
- xx xx
221
- x x xx x x xx x x
222
- x x xx x xx x xx x
223
- x x xx x x x xx xx
224
- x xx xxx xx x x x x x
225
- xx xxxx xxxx x
226
- x x x xx x xx xx x xx xx
227
- x xx xxxx xxxx
228
- xx xx x x x x xx x
229
- xxxx xxxx xxxxxx x
230
- x x x
231
- xxxxxxx x xxx x x x x
232
- x x xxx x xx x x
233
- x xxx x xxxxxxxxxx
234
- x xxx x xxxxxxxxx x xx
235
- x xxx x xxx xx x x x
236
- x x x x x
237
- xxxxxxx xxx xxx x x x
238
-
239
-
240
- ```
241
-
242
- ### Web browser
243
-
244
- Use on a HTML page with JavaScript
245
- ```html
246
- <!DOCTYPE html>
247
- <html>
248
- <body>
249
- <div id="container"></div>
250
- <script src="dist/qrcode.min.js"></script>
251
- <script>
252
- var qrcode = new QRCode({
253
- content: "Hello World!",
254
- container: "svg-viewbox", //Responsive use
255
- join: true //Crisp rendering and 4-5x reduced file size
256
- });
257
- var svg = qrcode.svg();
258
- document.getElementById("container").innerHTML = svg;
259
- </script>
260
- </body>
261
- </html>
262
- ```
263
-
264
- ## Thanks
265
-
266
- Thanks to [davidshimjs](https://github.com/davidshimjs/qrcodejs) for the base library.
267
-
268
- Thanks to [Kazuhiko Arase](http://www.d-project.com/) for the original QR Code in JavaScript algorithm.
269
-
270
- Thanks to all contributors on the GitHub.
271
-
272
- ## Legal notice
273
-
274
- ```
275
- Licensed under the MIT license:
276
- http://www.opensource.org/licenses/mit-license.php
277
-
278
- The word "QR Code" is registered trademark of DENSO WAVE INCORPORATED
279
- http://www.denso-wave.com/qrcode/faqpatent-e.html
280
- ```
1
+ ## Introduction
2
+
3
+ A browser-only ES module for generating SVG QR codes in pure JavaScript.
4
+
5
+ - pure JavaScript, zero dependencies
6
+ - ESM only
7
+ - SVG output
8
+
9
+ ## Getting Started
10
+
11
+ Install the package:
12
+
13
+ ```bash
14
+ npm install @pjaudiomv/qrcode-svg
15
+ ```
16
+
17
+ Inline example:
18
+
19
+ ```javascript
20
+ import QRCode from '@pjaudiomv/qrcode-svg'
21
+
22
+ const svg = new QRCode('Hello World!').svg()
23
+ ```
24
+
25
+ More options:
26
+
27
+ ```javascript
28
+ const qrcode = new QRCode({
29
+ content: 'http://github.com/',
30
+ padding: 4,
31
+ width: 256,
32
+ height: 256,
33
+ color: '#000000',
34
+ background: '#ffffff',
35
+ ecl: 'M',
36
+ })
37
+ const svg = qrcode.svg()
38
+ ```
39
+
40
+ ## Options
41
+
42
+ **List of options:**
43
+
44
+ - **content** - QR Code content, the only **required** parameter
45
+ - **padding** - white space padding, `4` modules by default, `0` for no border
46
+ - **width** - QR Code width in pixels
47
+ - **height** - QR Code height in pixels
48
+ - **color** - color of modules (squares), color name or hex string, e.g. `#000000`
49
+ - **background** - color of background, color name or hex string, e.g. `white`
50
+ - **ecl** - error correction level: `L`, `M`, `H`, `Q`
51
+ - **join** - join modules (squares) into one shape, into the SVG `path` element, **recommended** for web and responsive use, default: `false`
52
+ - **predefined** - to create a squares as pattern, then populate the canvas, default: `false`, see the output examples below
53
+ - **pretty** - apply indents and new lines, default: `true`
54
+ - **swap** - swap X and Y modules, only if you have issues with some QR readers, default: `false`
55
+ - **xmlDeclaration** - prepend XML declaration to the SVG document, i.e. `<?xml version="1.0" standalone="yes"?>`, default: `true`
56
+ - **container** - wrapping element, default: `svg`, see below
57
+
58
+ **Container options:**
59
+
60
+ - **svg** - populate squares in a SVG document with `width` and `height` attributes, recommended for converting to raster images or PDF where QR Code is being static (exact size)
61
+ - **svg-viewbox** - populate squares in a SVG document with `viewBox` attribute, **recommended** for responsive web pages
62
+ - **g** - put squares in `g` element, useful when you need to put multiple QR Codes in a single SVG document
63
+ - **none** - no wrapper
64
+ - **path-data** - returns only the raw SVG path data string (requires `join: true`)
65
+
66
+ ## SVG output
67
+
68
+ ### Editable squares
69
+
70
+ This mode is useful for designers to manipulate with particular squares.
71
+ Thus, one can open the QR Code in an editor, select particular modules, move around, change color, etc.
72
+ However, some old SVG viewers may generate minor gaps between the squares - the side effect when rendering an image at certain zoom level.
73
+
74
+ Default options
75
+
76
+ ```javascript
77
+ const qrcode = new QRCode({
78
+ content: 'Pretty Fox',
79
+ join: false,
80
+ predefined: false,
81
+ })
82
+ ```
83
+
84
+ Output with `rect` elements
85
+
86
+ ```xml
87
+ <?xml version="1.0" standalone="yes"?>
88
+ <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="256" height="256">
89
+ <rect x="0" y="0" width="256" height="256" style="fill:#ffffff;shape-rendering:crispEdges;"/>
90
+ <rect x="16" y="16" width="8" height="8" style="fill:#000000;shape-rendering:crispEdges;"/>
91
+ <rect x="24" y="16" width="8" height="8" style="fill:#000000;shape-rendering:crispEdges;"/>
92
+ <rect x="32" y="16" width="8" height="8" style="fill:#000000;shape-rendering:crispEdges;"/>
93
+ ...
94
+ </svg>
95
+ ```
96
+
97
+ ### Responsive web page
98
+
99
+ Squares joined into one `path` shape produce a compact file size, i.e. 4-5x reduced compared with `rect` elements.
100
+ A single `path` element will result in an optimized rendering, thus not producing any minor gaps between the squares.
101
+ Also using the container with `viewBox` attribute may contribute to the responsive scaling on the web.
102
+
103
+ Set `join` to `true`
104
+
105
+ ```javascript
106
+ const qrcode = new QRCode({
107
+ content: 'Pretty Fox',
108
+ join: true,
109
+ container: 'svg-viewbox', //Useful but not required
110
+ })
111
+ ```
112
+
113
+ Output with `path` element
114
+
115
+ ```xml
116
+ <?xml version="1.0" standalone="yes"?>
117
+ <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 256 256">
118
+ <rect x="0" y="0" width="256" height="256" style="fill:beige;shape-rendering:crispEdges;"/>
119
+ <path x="0" y="0" style="fill:blue;shape-rendering:crispEdges;" d="M35.31,35.31 V44.14 H44.14 V35.31 H35.31 Z..." />
120
+ </svg>
121
+ ```
122
+
123
+ ### Predefined pattern
124
+
125
+ Algorithm defines the square pattern once before populating a canvas. Useful if you want to generate QR Code with candies.
126
+ However, some SVG software and converters do not support `defs` or `use` elements.
127
+
128
+ Set `predefined` to `true`
129
+
130
+ ```javascript
131
+ const qrcode = new QRCode({
132
+ content: 'Pretty Fox',
133
+ predefined: true,
134
+ })
135
+ ```
136
+
137
+ Output with `defs` and `use` elements
138
+
139
+ ```xml
140
+ <?xml version="1.0" standalone="yes"?>
141
+ <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="256" height="256">
142
+ <defs><path id="qrmodule" d="M0 0 h8.827586206896552 v8.827586206896552 H0 z" style="fill:maroon;shape-rendering:crispEdges;" /></defs>
143
+ <rect x="0" y="0" width="256" height="256" style="fill:beige;shape-rendering:crispEdges;"/>
144
+ <use x="35.310344827586206" y="35.310344827586206" href="#qrmodule" />
145
+ <use x="44.13793103448276" y="35.310344827586206" href="#qrmodule" />
146
+ ...
147
+ </svg>
148
+ ```
149
+
150
+ ## Usage in the browser
151
+
152
+ ```html
153
+ <!DOCTYPE html>
154
+ <html>
155
+ <body>
156
+ <div id="container"></div>
157
+ <script type="module">
158
+ import QRCode from './dist/qrcode.js'
159
+ const qrcode = new QRCode({
160
+ content: 'Hello World!',
161
+ container: 'svg-viewbox',
162
+ join: true,
163
+ })
164
+ document.getElementById('container').innerHTML = qrcode.svg()
165
+ </script>
166
+ </body>
167
+ </html>
168
+ ```
169
+
170
+ ## Thanks
171
+
172
+ Thanks to [davidshimjs](https://github.com/davidshimjs/qrcodejs) for the base library.
173
+
174
+ Thanks to [Kazuhiko Arase](http://www.d-project.com/) for the original QR Code in JavaScript algorithm.
175
+
176
+ Thanks to all contributors on the GitHub.
177
+
178
+ ## Legal notice
179
+
180
+ ```
181
+ Licensed under the MIT license:
182
+ http://www.opensource.org/licenses/mit-license.php
183
+
184
+ The word "QR Code" is registered trademark of DENSO WAVE INCORPORATED
185
+ http://www.denso-wave.com/qrcode/faqpatent-e.html
186
+ ```
@@ -0,0 +1,49 @@
1
+ export type ErrorCorrectionLevel = 'L' | 'M' | 'Q' | 'H'
2
+
3
+ export type Container =
4
+ | 'svg'
5
+ | 'svg-viewbox'
6
+ | 'g'
7
+ | 'none'
8
+ | 'path-data'
9
+
10
+ export interface QRCodeOptions {
11
+ /** QR Code content (required). */
12
+ content: string
13
+ /** White-space padding in modules. Default: `4`. */
14
+ padding?: number
15
+ /** Image width in pixels. Default: `256`. */
16
+ width?: number
17
+ /** Image height in pixels. Default: `256`. */
18
+ height?: number
19
+ /** Foreground color. Default: `'#000000'`. */
20
+ color?: string
21
+ /** Background color. Default: `'#ffffff'`. */
22
+ background?: string
23
+ /** Error correction level. Default: `'M'`. */
24
+ ecl?: ErrorCorrectionLevel
25
+ /** Join modules into a single `<path>` element. Default: `false`. */
26
+ join?: boolean
27
+ /** Use `<defs>`/`<use>` for module shapes. Default: `false`. */
28
+ predefined?: boolean
29
+ /** Pretty-print the SVG output. Default: `true`. */
30
+ pretty?: boolean
31
+ /** Swap X and Y (workaround for some readers). Default: `false`. */
32
+ swap?: boolean
33
+ /** Prepend the `<?xml ... ?>` declaration. Default: `true`. */
34
+ xmlDeclaration?: boolean
35
+ /** Wrapper element. Default: `'svg'`. */
36
+ container?: Container
37
+ /** QR version hint. Default: `4`. */
38
+ typeNumber?: number
39
+ }
40
+
41
+ export interface SvgOptions {
42
+ container?: Container
43
+ }
44
+
45
+ export default class QRCode {
46
+ constructor(options: QRCodeOptions | string)
47
+ options: Required<Pick<QRCodeOptions, 'padding' | 'width' | 'height' | 'color' | 'background' | 'ecl' | 'typeNumber'>> & QRCodeOptions
48
+ svg(options?: SvgOptions): string
49
+ }