|
1 | 1 | // @flow |
| 2 | + |
| 3 | +// flowlint untyped-import:off |
2 | 4 | import qrcode from "../../libs/qrcode" |
| 5 | +// flowlint untyped-import:error |
3 | 6 |
|
4 | 7 | export type qrCodeOptions = { |
5 | | - padding: number, |
6 | | - width: number, |
7 | | - height: number, |
8 | | - typeNumber: number, // Use 0 for auto detect depending on input data length (size) |
9 | | - fill: string, |
10 | | - background: string, |
11 | | - ecl: string, |
12 | | - container: string, |
| 8 | + size: number, |
13 | 9 | content: string, |
| 10 | + padding?: number, |
| 11 | + typeNumber?: number, // Use 0 for auto detect depending on input data length (size) |
| 12 | + fill?: string, |
| 13 | + background?: string, |
| 14 | + ecl?: 'L' | 'M' | 'Q' | 'H', |
| 15 | + scalable?: boolean; |
14 | 16 | }; |
15 | 17 |
|
16 | 18 | /** Generates QR Code as SVG image */ |
17 | | -export function getQRCodeSvg(options: qrCodeOptions): string { |
| 19 | +export function getQRCodeSvgPath(options: qrCodeOptions): string { |
| 20 | + |
| 21 | + let content = options.content |
| 22 | + let padding = options.padding || 0 |
| 23 | + let qrcodeGenerator = qrcode(options.typeNumber || 0, options.ecl || "M") |
| 24 | + qrcodeGenerator.addData(content) |
| 25 | + qrcodeGenerator.make() |
| 26 | + |
| 27 | + let cellSize = (options.size) / (qrcodeGenerator.getModuleCount()) |
| 28 | + |
| 29 | + return qrcodeGenerator.createSvgPath({ |
| 30 | + // Round to two decimals |
| 31 | + cellSize: Math.round(cellSize * 100 - 50) / 100, |
| 32 | + background: options.background, |
| 33 | + fill: options.fill, |
| 34 | + }) |
| 35 | +} |
18 | 36 |
|
19 | 37 |
|
20 | | - //Gets text length |
21 | | - function _getUTF8Length(content) { |
22 | | - var result = encodeURI(content).toString().replace(/\%[0-9a-fA-F]{2}/g, 'a'); |
23 | | - return result.length + (result.length != content ? 3 : 0); |
24 | | - } |
| 38 | +/** Generates QR Code as SVG image */ |
| 39 | +export function getQRCodeSvg(options: qrCodeOptions): string { |
25 | 40 |
|
26 | | - //Generate QR Code matrix |
27 | 41 | var content = options.content |
28 | | - var qrcodeGenerator = qrcode(options.typeNumber, options.ecl) |
| 42 | + let padding = options.padding || 0 |
| 43 | + var qrcodeGenerator = qrcode(options.typeNumber || 0, options.ecl || "M") |
29 | 44 | qrcodeGenerator.addData(content) |
30 | 45 | qrcodeGenerator.make() |
31 | | - return qrcodeGenerator.createSvgPath({ |
| 46 | + let cellSize = (options.size - 2 * padding) / (qrcodeGenerator.getModuleCount()) |
| 47 | + |
| 48 | + return qrcodeGenerator.createSvgTag({ |
32 | 49 | // Round to two decimals |
33 | | - cellSize: Math.round(options.width / (qrcodeGenerator.getModuleCount() + 2 * options.padding) * 100) / 100, |
34 | | - background: null, |
| 50 | + cellSize: Math.round(cellSize * 100 - 50) / 100, |
| 51 | + background: options.background, |
35 | 52 | fill: options.fill, |
36 | | - scalable: true, |
| 53 | + scalable: options.scalable || false, |
37 | 54 | padding: options.padding |
38 | 55 | }) |
39 | 56 | } |
0 commit comments