@juit/qrcode 0.0.0
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.md +211 -0
- package/NOTICE.md +13 -0
- package/README.md +175 -0
- package/dist/encode.cjs +147 -0
- package/dist/encode.cjs.map +6 -0
- package/dist/encode.d.ts +11 -0
- package/dist/encode.mjs +122 -0
- package/dist/encode.mjs.map +6 -0
- package/dist/images/path.cjs +145 -0
- package/dist/images/path.cjs.map +6 -0
- package/dist/images/path.d.ts +6 -0
- package/dist/images/path.mjs +120 -0
- package/dist/images/path.mjs.map +6 -0
- package/dist/images/pdf.cjs +96 -0
- package/dist/images/pdf.cjs.map +6 -0
- package/dist/images/pdf.d.ts +3 -0
- package/dist/images/pdf.mjs +71 -0
- package/dist/images/pdf.mjs.map +6 -0
- package/dist/images/png.cjs +86 -0
- package/dist/images/png.cjs.map +6 -0
- package/dist/images/png.d.ts +3 -0
- package/dist/images/png.mjs +61 -0
- package/dist/images/png.mjs.map +6 -0
- package/dist/images/svg.cjs +64 -0
- package/dist/images/svg.cjs.map +6 -0
- package/dist/images/svg.d.ts +54 -0
- package/dist/images/svg.mjs +38 -0
- package/dist/images/svg.mjs.map +6 -0
- package/dist/index.cjs +110 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.mjs +74 -0
- package/dist/index.mjs.map +6 -0
- package/dist/matrix.cjs +360 -0
- package/dist/matrix.cjs.map +6 -0
- package/dist/matrix.d.ts +3 -0
- package/dist/matrix.mjs +335 -0
- package/dist/matrix.mjs.map +6 -0
- package/dist/qrcode.cjs +183 -0
- package/dist/qrcode.cjs.map +6 -0
- package/dist/qrcode.d.ts +20 -0
- package/dist/qrcode.mjs +158 -0
- package/dist/qrcode.mjs.map +6 -0
- package/dist/utils/crc32.cjs +53 -0
- package/dist/utils/crc32.cjs.map +6 -0
- package/dist/utils/crc32.d.ts +9 -0
- package/dist/utils/crc32.mjs +28 -0
- package/dist/utils/crc32.mjs.map +6 -0
- package/dist/utils/dataurl.cjs +35 -0
- package/dist/utils/dataurl.cjs.map +6 -0
- package/dist/utils/dataurl.d.ts +1 -0
- package/dist/utils/dataurl.mjs +10 -0
- package/dist/utils/dataurl.mjs.map +6 -0
- package/dist/utils/deflate.cjs +39 -0
- package/dist/utils/deflate.cjs.map +6 -0
- package/dist/utils/deflate.d.ts +2 -0
- package/dist/utils/deflate.mjs +14 -0
- package/dist/utils/deflate.mjs.map +6 -0
- package/dist/utils/ecc.cjs +93 -0
- package/dist/utils/ecc.cjs.map +6 -0
- package/dist/utils/ecc.d.ts +2 -0
- package/dist/utils/ecc.mjs +68 -0
- package/dist/utils/ecc.mjs.map +6 -0
- package/dist/utils/merge.cjs +41 -0
- package/dist/utils/merge.cjs.map +6 -0
- package/dist/utils/merge.d.ts +2 -0
- package/dist/utils/merge.mjs +16 -0
- package/dist/utils/merge.mjs.map +6 -0
- package/package.json +61 -0
- package/src/encode.ts +180 -0
- package/src/images/path.ts +131 -0
- package/src/images/pdf.ts +76 -0
- package/src/images/png.ts +105 -0
- package/src/images/svg.ts +102 -0
- package/src/index.ts +147 -0
- package/src/matrix.ts +392 -0
- package/src/qrcode.ts +217 -0
- package/src/utils/crc32.ts +50 -0
- package/src/utils/dataurl.ts +7 -0
- package/src/utils/deflate.ts +17 -0
- package/src/utils/ecc.ts +95 -0
- package/src/utils/merge.ts +13 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# Apache License
|
|
2
|
+
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
|
|
5
|
+
[http://www.apache.org/licenses/](http://www.apache.org/licenses/)
|
|
6
|
+
|
|
7
|
+
##### TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
### 1. Definitions.
|
|
10
|
+
|
|
11
|
+
"**License**" shall mean the terms and conditions for use, reproduction,
|
|
12
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
13
|
+
|
|
14
|
+
"**Licensor**" shall mean the copyright owner or entity authorized by
|
|
15
|
+
the copyright owner that is granting the License.
|
|
16
|
+
|
|
17
|
+
"**Legal Entity**" shall mean the union of the acting entity and all
|
|
18
|
+
other entities that control, are controlled by, or are under common
|
|
19
|
+
control with that entity. For the purposes of this definition,
|
|
20
|
+
"**control**" means **(i)** the power, direct or indirect, to cause the
|
|
21
|
+
direction or management of such entity, whether by contract or
|
|
22
|
+
otherwise, _or_ **(ii)** ownership of fifty percent (50%) or more of
|
|
23
|
+
the outstanding shares, _or_ **(iii)** beneficial ownership of such
|
|
24
|
+
entity.
|
|
25
|
+
|
|
26
|
+
"**You**" (or "**Your**") shall mean an individual or Legal Entity
|
|
27
|
+
exercising permissions granted by this License.
|
|
28
|
+
|
|
29
|
+
"**Source**" form shall mean the preferred form for making
|
|
30
|
+
modifications, including but not limited to software source code,
|
|
31
|
+
documentation source, and configuration files.
|
|
32
|
+
|
|
33
|
+
"**Object**" form shall mean any form resulting from mechanical
|
|
34
|
+
transformation or translation of a Source form, including but not
|
|
35
|
+
limited to compiled object code, generated documentation, and
|
|
36
|
+
conversions to other media types.
|
|
37
|
+
|
|
38
|
+
"**Work**" shall mean the work of authorship, whether in Source or
|
|
39
|
+
Object form, made available under the License, as indicated by a
|
|
40
|
+
copyright notice that is included in or attached to the work (an example
|
|
41
|
+
is provided in the Appendix below).
|
|
42
|
+
|
|
43
|
+
"**Derivative Works**" shall mean any work, whether in Source or Object
|
|
44
|
+
form, that is based on (or derived from) the Work and for which the
|
|
45
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
46
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
47
|
+
of this License, Derivative Works shall not include works that remain
|
|
48
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
49
|
+
the Work and Derivative Works thereof.
|
|
50
|
+
|
|
51
|
+
"**Contribution**" shall mean any work of authorship, including the
|
|
52
|
+
original version of the Work and any modifications or additions to that
|
|
53
|
+
Work or Derivative Works thereof, that is intentionally submitted to
|
|
54
|
+
Licensor for inclusion in the Work by the copyright owner or by an
|
|
55
|
+
individual or Legal Entity authorized to submit on behalf of the
|
|
56
|
+
copyright owner. For the purposes of this definition, "**submitted**"
|
|
57
|
+
means any form of electronic, verbal, or written communication sent to
|
|
58
|
+
the Licensor or its representatives, including but not limited to
|
|
59
|
+
communication on electronic mailing lists, source code control systems,
|
|
60
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
61
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
62
|
+
excluding communication that is conspicuously marked or otherwise
|
|
63
|
+
designated in writing by the copyright owner as "**Not a
|
|
64
|
+
Contribution**".
|
|
65
|
+
|
|
66
|
+
"**Contributor**" shall mean Licensor and any individual or Legal Entity
|
|
67
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
68
|
+
subsequently incorporated within the Work.
|
|
69
|
+
|
|
70
|
+
### 2. Grant of Copyright License.
|
|
71
|
+
|
|
72
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
73
|
+
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
|
74
|
+
royalty-free, irrevocable copyright license to reproduce, prepare
|
|
75
|
+
Derivative Works of, publicly display, publicly perform, sublicense,
|
|
76
|
+
and distribute the Work and such Derivative Works in Source or Object
|
|
77
|
+
form.
|
|
78
|
+
|
|
79
|
+
### 3. Grant of Patent License.
|
|
80
|
+
|
|
81
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
82
|
+
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
|
83
|
+
royalty-free, irrevocable (except as stated in this section) patent
|
|
84
|
+
license to make, have made, use, offer to sell, sell, import, and
|
|
85
|
+
otherwise transfer the Work, where such license applies only to those
|
|
86
|
+
patent claims licensable by such Contributor that are necessarily
|
|
87
|
+
infringed by their Contribution(s) alone or by combination of their
|
|
88
|
+
Contribution(s) with the Work to which such Contribution(s) was
|
|
89
|
+
submitted. If You institute patent litigation against any entity
|
|
90
|
+
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
|
91
|
+
the Work or a Contribution incorporated within the Work constitutes
|
|
92
|
+
direct or contributory patent infringement, then any patent licenses
|
|
93
|
+
granted to You under this License for that Work shall terminate as of
|
|
94
|
+
the date such litigation is filed.
|
|
95
|
+
|
|
96
|
+
### 4. Redistribution.
|
|
97
|
+
|
|
98
|
+
You may reproduce and distribute copies of the Work or Derivative Works
|
|
99
|
+
thereof in any medium, with or without modifications, and in Source or
|
|
100
|
+
Object form, provided that You meet the following conditions:
|
|
101
|
+
|
|
102
|
+
<ol type="a">
|
|
103
|
+
<li>You must give any other recipients of the Work or Derivative Works
|
|
104
|
+
a copy of this License; and
|
|
105
|
+
<li>You must cause any modified files to carry prominent notices
|
|
106
|
+
stating that You changed the files; and
|
|
107
|
+
<li>You must retain, in the Source form of any Derivative Works that
|
|
108
|
+
You distribute, all copyright, patent, trademark, and attribution
|
|
109
|
+
notices from the Source form of the Work, excluding those notices
|
|
110
|
+
that do not pertain to any part of the Derivative Works; and
|
|
111
|
+
<li>If the Work includes a "NOTICE" text file as part of its
|
|
112
|
+
distribution, then any Derivative Works that You distribute must
|
|
113
|
+
include a readable copy of the attribution notices contained within
|
|
114
|
+
such NOTICE file, excluding those notices that do not pertain to any
|
|
115
|
+
part of the Derivative Works, in at least one of the following
|
|
116
|
+
places: within a NOTICE text file distributed as part of the
|
|
117
|
+
Derivative Works; within the Source form or documentation, if
|
|
118
|
+
provided along with the Derivative Works; or, within a display
|
|
119
|
+
generated by the Derivative Works, if and wherever such third-party
|
|
120
|
+
notices normally appear. The contents of the NOTICE file are for
|
|
121
|
+
informational purposes only and do not modify the License. You may
|
|
122
|
+
add Your own attribution notices within Derivative Works that You
|
|
123
|
+
distribute, alongside or as an addendum to the NOTICE text from the
|
|
124
|
+
Work, provided that such additional attribution notices cannot be
|
|
125
|
+
construed as modifying the License.
|
|
126
|
+
</ol>
|
|
127
|
+
|
|
128
|
+
You may add Your own copyright statement to Your modifications and may
|
|
129
|
+
provide additional or different license terms and conditions for use,
|
|
130
|
+
reproduction, or distribution of Your modifications, or for any such
|
|
131
|
+
Derivative Works as a whole, provided Your use, reproduction, and
|
|
132
|
+
distribution of the Work otherwise complies with the conditions stated
|
|
133
|
+
in this License.
|
|
134
|
+
|
|
135
|
+
### 5. Submission of Contributions.
|
|
136
|
+
|
|
137
|
+
Unless You explicitly state otherwise, any Contribution intentionally
|
|
138
|
+
submitted for inclusion in the Work by You to the Licensor shall be
|
|
139
|
+
under the terms and conditions of this License, without any additional
|
|
140
|
+
terms or conditions. Notwithstanding the above, nothing herein shall
|
|
141
|
+
supersede or modify the terms of any separate license agreement you may
|
|
142
|
+
have executed with Licensor regarding such Contributions.
|
|
143
|
+
|
|
144
|
+
### 6. Trademarks.
|
|
145
|
+
|
|
146
|
+
This License does not grant permission to use the trade names,
|
|
147
|
+
trademarks, service marks, or product names of the Licensor, except as
|
|
148
|
+
required for reasonable and customary use in describing the origin of
|
|
149
|
+
the Work and reproducing the content of the NOTICE file.
|
|
150
|
+
|
|
151
|
+
### 7. Disclaimer of Warranty.
|
|
152
|
+
|
|
153
|
+
Unless required by applicable law or agreed to in writing, Licensor
|
|
154
|
+
provides the Work (and each Contributor provides its Contributions) on
|
|
155
|
+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
|
156
|
+
express or implied, including, without limitation, any warranties or
|
|
157
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR
|
|
158
|
+
A PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
159
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
160
|
+
associated with Your exercise of permissions under this License.
|
|
161
|
+
|
|
162
|
+
### 8. Limitation of Liability.
|
|
163
|
+
|
|
164
|
+
In no event and under no legal theory, whether in tort (including
|
|
165
|
+
negligence), contract, or otherwise, unless required by applicable law
|
|
166
|
+
(such as deliberate and grossly negligent acts) or agreed to in writing,
|
|
167
|
+
shall any Contributor be liable to You for damages, including any
|
|
168
|
+
direct, indirect, special, incidental, or consequential damages of any
|
|
169
|
+
character arising as a result of this License or out of the use or
|
|
170
|
+
inability to use the Work (including but not limited to damages for loss
|
|
171
|
+
of goodwill, work stoppage, computer failure or malfunction, or any and
|
|
172
|
+
all other commercial damages or losses), even if such Contributor has
|
|
173
|
+
been advised of the possibility of such damages.
|
|
174
|
+
|
|
175
|
+
### 9. Accepting Warranty or Additional Liability.
|
|
176
|
+
|
|
177
|
+
While redistributing the Work or Derivative Works thereof, You may
|
|
178
|
+
choose to offer, and charge a fee for, acceptance of support, warranty,
|
|
179
|
+
indemnity, or other liability obligations and/or rights consistent with
|
|
180
|
+
this License. However, in accepting such obligations, You may act only
|
|
181
|
+
on Your own behalf and on Your sole responsibility, not on behalf of any
|
|
182
|
+
other Contributor, and only if You agree to indemnify, defend, and hold
|
|
183
|
+
each Contributor harmless for any liability incurred by, or claims
|
|
184
|
+
asserted against, such Contributor by reason of your accepting any
|
|
185
|
+
such warranty or additional liability.
|
|
186
|
+
|
|
187
|
+
##### END OF TERMS AND CONDITIONS
|
|
188
|
+
|
|
189
|
+
## APPENDIX: How to apply the Apache License to your work.
|
|
190
|
+
|
|
191
|
+
To apply the Apache License to your work, attach the following
|
|
192
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
193
|
+
replaced with your own identifying information. (Don't include the
|
|
194
|
+
brackets!) The text should be enclosed in the appropriate comment
|
|
195
|
+
syntax for the file format. We also recommend that a file or class name
|
|
196
|
+
and description of purpose be included on the same "printed page" as the
|
|
197
|
+
copyright notice for easier identification within third-party archives.
|
|
198
|
+
|
|
199
|
+
> #### Copyright [yyyy] [name of copyright owner]
|
|
200
|
+
>
|
|
201
|
+
> Licensed under the Apache License, Version 2.0 (the "License");
|
|
202
|
+
> you may not use this file except in compliance with the License.
|
|
203
|
+
> You may obtain a copy of the License at
|
|
204
|
+
>
|
|
205
|
+
> [http://www.apache.org/licenses/](http://www.apache.org/licenses/)
|
|
206
|
+
>
|
|
207
|
+
> Unless required by applicable law or agreed to in writing, software
|
|
208
|
+
> distributed under the License is distributed on an "AS IS" BASIS,
|
|
209
|
+
> WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
210
|
+
> See the License for the specific language governing permissions and
|
|
211
|
+
> limitations under the License.
|
package/NOTICE.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Copyright 2021-2024 Juit GmbH
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
[http://www.apache.org/licenses/](http://www.apache.org/licenses/)
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
Juit GmbH's Own _QR Code Generator_
|
|
2
|
+
===================================
|
|
3
|
+
|
|
4
|
+
> First of all, kudos are due to [Alexey Ten](https://github.com/alexeyten)
|
|
5
|
+
> who wrote the _original_ [`qr-image`](https://www.npmjs.com/package/qr-image)
|
|
6
|
+
> package. Without his incredible work this project would not exist.
|
|
7
|
+
|
|
8
|
+
This package simply generate QR codes, using _modern_ web standards like
|
|
9
|
+
[`CompressionStream`](https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream)s,
|
|
10
|
+
`Promise`s (with `async`/`await`) and has zero dependencies on NodeJS or other
|
|
11
|
+
frameworks (therefore making it suitable for inclusion in runtimes like
|
|
12
|
+
[Cloudflare Workers](https://workers.cloudflare.com/)).
|
|
13
|
+
|
|
14
|
+
* [Main API](#main-api)
|
|
15
|
+
* [Arguments](#arguments)
|
|
16
|
+
* [Options](#options)
|
|
17
|
+
* [Bundler-friendly API](#bundler-friendly-api)
|
|
18
|
+
* [Low-level API](#low-level-api)
|
|
19
|
+
* [The `QRCode` object](#the-qrcode-object)
|
|
20
|
+
* [SVG paths](#svg-paths) (and [PDFKit](https://pdfkit.org/) integration)
|
|
21
|
+
* [Copyright Notice](NOTICE.md)
|
|
22
|
+
* [License](LICENSE.md)
|
|
23
|
+
|
|
24
|
+
# Main API
|
|
25
|
+
|
|
26
|
+
To generate a QR code simply use the `generate(...)` async function:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { generate } from `@juit/qrcode`
|
|
30
|
+
|
|
31
|
+
const image = await generate('https://www.juit.com/', 'png')
|
|
32
|
+
// `image` here is a Uint8Array containing the PDF data
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
#### Arguments
|
|
36
|
+
|
|
37
|
+
* `message`: a `string` or `Uint8Array` containing the data to be included in
|
|
38
|
+
the generated QR code
|
|
39
|
+
* `format`: the format of the QR code to generate, that is:
|
|
40
|
+
* `png`: a PNG image (as a `Uint8Array`)
|
|
41
|
+
* `pdf`: a PDF document (as a `Uint8Array` - uses compression)
|
|
42
|
+
* `svg`: a SVG image (as a `string`)
|
|
43
|
+
* `pngData`: the _data URL_ (as a `string`) of a PNG image
|
|
44
|
+
* `pdfData`: the _data URL_ (as a `string`) of a PDF document
|
|
45
|
+
* `svgData`: the _data URL_ (as a `string`) of a SVG image
|
|
46
|
+
* `options`: an optional object containing encoding options
|
|
47
|
+
|
|
48
|
+
#### Options
|
|
49
|
+
|
|
50
|
+
* `ecLevel`: the error correction level for the QR code
|
|
51
|
+
(default: `M`)
|
|
52
|
+
* `L`: approximately 7% of error correction capability
|
|
53
|
+
* `M`: approximately 15% of error correction capability
|
|
54
|
+
* `Q`: approximately 25% of error correction capability
|
|
55
|
+
* `H`: approximately 30% of error correction capability
|
|
56
|
+
* `url`: whether to optimize URLs in QR codes or not
|
|
57
|
+
(default: `false`)
|
|
58
|
+
* `scale`: the number of pixels used for each dot in the matrix
|
|
59
|
+
(default: `1` for PNG/SVG and `9` for PDF)
|
|
60
|
+
* `margin`: the size of the margin around the QR code in matrix modules
|
|
61
|
+
(default: `4` as per the QR code specification - the _quiet area_)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# Bundler-friendly API
|
|
66
|
+
|
|
67
|
+
If final bundle sizes are important for you, we also offer individual APIs for
|
|
68
|
+
each format we generate. The bundler's own _tree shaking_ algorithm should be
|
|
69
|
+
able to then remove all irrelevant code.
|
|
70
|
+
|
|
71
|
+
The main functions are:
|
|
72
|
+
|
|
73
|
+
* `generatePngQrCode(message, options?)`: \
|
|
74
|
+
asynchronously generates a QR code in PNG format (as a `Promise<Uint8Array>`)
|
|
75
|
+
* `generatePdfQrCode(message, options?)`: \
|
|
76
|
+
asynchronously generates a QR code in PDF format (as a `Promise<Uint8Array>`)
|
|
77
|
+
* `generateSvgQrCode(message, options?)`: \
|
|
78
|
+
synchronously generates a QR code in SVG format (as a `string`)
|
|
79
|
+
|
|
80
|
+
To generate data URLs on the other hand:
|
|
81
|
+
|
|
82
|
+
* `generatePngDataQrCode(message, options?)`: \
|
|
83
|
+
asynchronously generates a QR code as a data URL in PNG format (as a `Promise<string>`)
|
|
84
|
+
* `generatePdfDataQrCode(message, options?)`: \
|
|
85
|
+
asynchronously generates a QR code as a data URL in PDF format (as a `Promise<string>`)
|
|
86
|
+
* `generateSvgDataQrCode(message, options?)`: \
|
|
87
|
+
synchronously generates a QR code as a data URL in PDF format (as a `string`)
|
|
88
|
+
|
|
89
|
+
The message and options consumed by these functions are the same ones described
|
|
90
|
+
in the [main api](#main-api).
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# Low-level API
|
|
95
|
+
|
|
96
|
+
The low-level API splits the _generation_ of a QR code from its rendering as
|
|
97
|
+
an image.
|
|
98
|
+
|
|
99
|
+
* `generateQrCode(message, options?)`: \
|
|
100
|
+
generates a `QRCode` object from the given message
|
|
101
|
+
* `generatePng(qrcode, options?)`: \
|
|
102
|
+
asynchronously renders a previously generated `QRCode` as a PNG image
|
|
103
|
+
(as a `Promise<Uint8Array>`)
|
|
104
|
+
* `generatePdf(qrcode, options?)`: \
|
|
105
|
+
asynchronously renders a previously generated `QRCode` as a PDF document
|
|
106
|
+
(as a `Promise<Uint8Array>`)
|
|
107
|
+
* `generateSvg(qrcode, options?)`: \
|
|
108
|
+
synchronously renders a previously generated `QRCode` as a SVG image
|
|
109
|
+
(as a `string`)
|
|
110
|
+
|
|
111
|
+
You can combine those APIs to (for example) generate a single QR code for
|
|
112
|
+
a message, and then render it in multiple formats.
|
|
113
|
+
|
|
114
|
+
The message and options consumed by these functions are the same ones described
|
|
115
|
+
in the [main api](#main-api).
|
|
116
|
+
|
|
117
|
+
#### The `QRCode` object
|
|
118
|
+
|
|
119
|
+
The object returned by `generateQrCode(...)` contains the following properties:
|
|
120
|
+
|
|
121
|
+
* `version`: the version of the QR code (1...40)
|
|
122
|
+
* `ecLevel`: the error correction level for the QR code (`L`, `M`, `Q` or `H`)
|
|
123
|
+
* `size`: The size (in pixels, or QR code blokcs) of the QR code
|
|
124
|
+
* `matrix`: The QR code matrix (where `true` is black, `false` is white)
|
|
125
|
+
|
|
126
|
+
#### SVG paths
|
|
127
|
+
|
|
128
|
+
The `generateSvgPath(qrcode)` function generate a SVG _path_ for a QR code.
|
|
129
|
+
|
|
130
|
+
The returned SVG path will be a simple conversion of the QR code's own
|
|
131
|
+
_`matrix`_ into a square path of _**N**_ "pixels" (where _**N**_ is the size of
|
|
132
|
+
the QR code's _`size`_ of the matrix).
|
|
133
|
+
|
|
134
|
+
This can be scaled and positioned into a final SVG using the `scale(...)` and
|
|
135
|
+
`translate(...)` [basic SVG transformations](https://developer.mozilla.org/en-US/docs/Web/G/Tutorial/Basic_Transformations).
|
|
136
|
+
|
|
137
|
+
This is also particulary useful with [PDFKit](https://pdfkit.org/)'s own
|
|
138
|
+
implementation of [SVG paths](https://pdfkit.org/docs/vector.html#svg_paths).
|
|
139
|
+
|
|
140
|
+
We can use it together with `translate(...)` and `scale(...)` to draw our QR
|
|
141
|
+
code anywhere on the page, in any size. For example, to prepare a simple A4
|
|
142
|
+
document with a 10cm QR code smack in the middle we can:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
// generate the QR code _structure_ for our message
|
|
146
|
+
const code = generateQrCode('https://www.juit.com/')
|
|
147
|
+
|
|
148
|
+
// generate the SVG path for our QR code
|
|
149
|
+
const path = generateSvgPath(code)
|
|
150
|
+
|
|
151
|
+
// calculate how to translate and scale our QR code in the page
|
|
152
|
+
const dpcm = 72 / 2.54 // PDFKit uses 72dpi (inches) we want metric!
|
|
153
|
+
const size = 10 * dpcm // 10 cm (size of our QR code) in dots
|
|
154
|
+
const scale = size / code.size // scale factor for our QR code to be 10 cm
|
|
155
|
+
const x = ((21 - 10) / 2) * dpcm // center horizontally
|
|
156
|
+
const y = ((29.7 - 10) / 2) * dpcm // center vertically
|
|
157
|
+
|
|
158
|
+
// create a new A4 document, and stream it to "test.pdf"
|
|
159
|
+
const document = new PDFDocument({ size: 'A4' })
|
|
160
|
+
const stream = createWriteStream('test.pdf')
|
|
161
|
+
document.pipe(stream)
|
|
162
|
+
|
|
163
|
+
// draw our 10cm QR code right in the middle of the page
|
|
164
|
+
document
|
|
165
|
+
.translate(x, y) // move to x = 5.5cm, y = 9.85cm
|
|
166
|
+
.scale(scale) // scale our QR code to 10cm width and height
|
|
167
|
+
.path(path) // draw our QR code smack in the middle of the page
|
|
168
|
+
.fill('black') // fill our QR code in black
|
|
169
|
+
.end() // finish up and close the document
|
|
170
|
+
|
|
171
|
+
// wait for the stream to finish
|
|
172
|
+
stream.on('finish', () => {
|
|
173
|
+
// your PDF file is ready!
|
|
174
|
+
})
|
|
175
|
+
```
|
package/dist/encode.cjs
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// encode.ts
|
|
21
|
+
var encode_exports = {};
|
|
22
|
+
__export(encode_exports, {
|
|
23
|
+
encodeQrCodeMessage: () => encodeQrCodeMessage
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(encode_exports);
|
|
26
|
+
var ALPHANUM = function(s) {
|
|
27
|
+
const res = {};
|
|
28
|
+
for (let i = 0; i < s.length; i++) {
|
|
29
|
+
res[s[i]] = i;
|
|
30
|
+
}
|
|
31
|
+
return res;
|
|
32
|
+
}("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:");
|
|
33
|
+
function pushBits(arr, n, value) {
|
|
34
|
+
for (let bit = 1 << n - 1; bit; bit = bit >>> 1) {
|
|
35
|
+
arr.push(!!(bit & value));
|
|
36
|
+
}
|
|
37
|
+
return arr;
|
|
38
|
+
}
|
|
39
|
+
function binaryEncode(data) {
|
|
40
|
+
const len = data.length;
|
|
41
|
+
const bits = [];
|
|
42
|
+
for (let i = 0; i < len; i++) {
|
|
43
|
+
pushBits(bits, 8, data[i]);
|
|
44
|
+
}
|
|
45
|
+
const d = pushBits([false, true, false, false], 16, len);
|
|
46
|
+
const res = {
|
|
47
|
+
data27: d.concat(bits)
|
|
48
|
+
};
|
|
49
|
+
res.data10 = res.data27;
|
|
50
|
+
if (len < 256) {
|
|
51
|
+
const d2 = pushBits([false, true, false, false], 8, len);
|
|
52
|
+
res.data1 = d2.concat(bits);
|
|
53
|
+
}
|
|
54
|
+
return res;
|
|
55
|
+
}
|
|
56
|
+
function alphanumEncode(str) {
|
|
57
|
+
const len = str.length;
|
|
58
|
+
const bits = [];
|
|
59
|
+
for (let i = 0; i < len; i += 2) {
|
|
60
|
+
let b = 6;
|
|
61
|
+
let n = ALPHANUM[str[i]];
|
|
62
|
+
if (str[i + 1]) {
|
|
63
|
+
b = 11;
|
|
64
|
+
n = n * 45 + ALPHANUM[str[i + 1]];
|
|
65
|
+
}
|
|
66
|
+
pushBits(bits, b, n);
|
|
67
|
+
}
|
|
68
|
+
const d = pushBits([false, false, true, false], 13, len);
|
|
69
|
+
const res = {
|
|
70
|
+
data27: d.concat(bits)
|
|
71
|
+
};
|
|
72
|
+
if (len < 2048) {
|
|
73
|
+
const d2 = pushBits([false, false, true, false], 11, len);
|
|
74
|
+
res.data10 = d2.concat(bits);
|
|
75
|
+
}
|
|
76
|
+
if (len < 512) {
|
|
77
|
+
const d2 = pushBits([false, false, true, false], 9, len);
|
|
78
|
+
res.data1 = d2.concat(bits);
|
|
79
|
+
}
|
|
80
|
+
return res;
|
|
81
|
+
}
|
|
82
|
+
function numericEncode(str) {
|
|
83
|
+
const len = str.length;
|
|
84
|
+
const bits = [];
|
|
85
|
+
for (let i = 0; i < len; i += 3) {
|
|
86
|
+
const s = str.substring(i, i + 3);
|
|
87
|
+
const b = Math.ceil(s.length * 10 / 3);
|
|
88
|
+
pushBits(bits, b, parseInt(s, 10));
|
|
89
|
+
}
|
|
90
|
+
const d = pushBits([false, false, false, true], 14, len);
|
|
91
|
+
const res = {
|
|
92
|
+
data27: d.concat(bits)
|
|
93
|
+
};
|
|
94
|
+
if (len < 4096) {
|
|
95
|
+
const d2 = pushBits([false, false, false, true], 12, len);
|
|
96
|
+
res.data10 = d2.concat(bits);
|
|
97
|
+
}
|
|
98
|
+
if (len < 1024) {
|
|
99
|
+
const d2 = pushBits([false, false, false, true], 10, len);
|
|
100
|
+
res.data1 = d2.concat(bits);
|
|
101
|
+
}
|
|
102
|
+
return res;
|
|
103
|
+
}
|
|
104
|
+
function urlEncode(str) {
|
|
105
|
+
const slash = str.indexOf("/", 8) + 1 || str.length;
|
|
106
|
+
const res = encodeQrCodeMessage(str.slice(0, slash).toUpperCase(), false);
|
|
107
|
+
if (slash >= str.length)
|
|
108
|
+
return res;
|
|
109
|
+
const path = encodeQrCodeMessage(str.slice(slash), false);
|
|
110
|
+
res.data27 = res.data27.concat(path.data27);
|
|
111
|
+
if (res.data10 && path.data10) {
|
|
112
|
+
res.data10 = res.data10.concat(path.data10);
|
|
113
|
+
}
|
|
114
|
+
if (res.data1 && path.data1) {
|
|
115
|
+
res.data1 = res.data1.concat(path.data1);
|
|
116
|
+
}
|
|
117
|
+
return res;
|
|
118
|
+
}
|
|
119
|
+
function encodeQrCodeMessage(message, url) {
|
|
120
|
+
let data;
|
|
121
|
+
if (typeof message === "string") {
|
|
122
|
+
data = new TextEncoder().encode(message);
|
|
123
|
+
if (/^[0-9]+$/.test(message)) {
|
|
124
|
+
if (data.length > 7089)
|
|
125
|
+
throw new Error(`Too much numeric data (len=${data.length})`);
|
|
126
|
+
return numericEncode(message);
|
|
127
|
+
}
|
|
128
|
+
if (/^[0-9A-Z $%*+./:-]+$/.test(message)) {
|
|
129
|
+
if (data.length > 4296)
|
|
130
|
+
throw new Error(`Too much alphanumeric data (len=${data.length})`);
|
|
131
|
+
return alphanumEncode(message);
|
|
132
|
+
}
|
|
133
|
+
if (url && /^https?:/i.test(message)) {
|
|
134
|
+
return urlEncode(message);
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
data = message;
|
|
138
|
+
}
|
|
139
|
+
if (data.length > 2953)
|
|
140
|
+
throw new Error(`Too much binary data (len=${data.length})`);
|
|
141
|
+
return binaryEncode(data);
|
|
142
|
+
}
|
|
143
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
144
|
+
0 && (module.exports = {
|
|
145
|
+
encodeQrCodeMessage
|
|
146
|
+
});
|
|
147
|
+
//# sourceMappingURL=encode.cjs.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/encode.ts"],
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBA,IAAM,WAAoC,SAAS,GAAG;AACpD,QAAM,MAA8B,CAAC;AACrC,WAAS,IAAI,GAAG,IAAI,EAAE,QAAQ,KAAK;AACjC,QAAI,EAAE,CAAC,CAAE,IAAI;AAAA,EACf;AACA,SAAO;AACT,EAAG,+CAA+C;AAOlD,SAAS,SAAS,KAAgB,GAAW,OAA0B;AACrE,WAAS,MAAM,KAAM,IAAI,GAAI,KAAK,MAAM,QAAQ,GAAG;AACjD,QAAI,KAAK,CAAC,EAAG,MAAM,MAAM;AAAA,EAC3B;AACA,SAAO;AACT;AAGA,SAAS,aAAa,MAAiC;AACrD,QAAM,MAAM,KAAK;AACjB,QAAM,OAAkB,CAAC;AAEzB,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AAC5B,aAAS,MAAM,GAAG,KAAK,CAAC,CAAE;AAAA,EAC5B;AAEA,QAAM,IAAI,SAAS,CAAE,OAAO,MAAM,OAAO,KAAM,GAAG,IAAI,GAAG;AAEzD,QAAM,MAAqB;AAAA,IACzB,QAAQ,EAAE,OAAO,IAAI;AAAA,EACvB;AACA,MAAI,SAAS,IAAI;AAEjB,MAAI,MAAM,KAAK;AACb,UAAMA,KAAI,SAAS,CAAE,OAAO,MAAM,OAAO,KAA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,KAAK,SAAS;AAAM,cAAM,IAAI,MAAM,mCAAmC,KAAK,MAAM,GAAG;AACzF,aAAO,eAAe,OAAO;AAAA,IAC/B;AAEA,QAAI,OAAO,YAAY,KAAK,OAAO,GAAG;AACpC,aAAO,UAAU,OAAO;AAAA,IAC1B;AAAA,EACF,OAAO;AACL,WAAO;AAAA,EACT;AAEA,MAAI,KAAK,SAAS;AAAM,UAAM,IAAI,MAAM,6BAA6B,KAAK,MAAM,GAAG;AACnF,SAAO,aAAa,IAAI;AAC1B;",
|
|
5
|
+
"names": ["d"]
|
|
6
|
+
}
|
package/dist/encode.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Internal interface for encoding data */
|
|
2
|
+
export interface QrCodeMessage {
|
|
3
|
+
/** Data for QR code version 27 or greater */
|
|
4
|
+
data27: boolean[];
|
|
5
|
+
/** Data for QR code version 10 or greater (shorter) */
|
|
6
|
+
data10?: boolean[];
|
|
7
|
+
/** Data for QR code version 1 or greater (shortest) */
|
|
8
|
+
data1?: boolean[];
|
|
9
|
+
}
|
|
10
|
+
/** Generate a message for the specified text or binary data */
|
|
11
|
+
export declare function encodeQrCodeMessage(message: string | Uint8Array, url: boolean): QrCodeMessage;
|