-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathswissqrcode.ts
71 lines (56 loc) · 1.54 KB
/
swissqrcode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
import { SVG } from "svg-engine";
import { renderQRCode, renderSwissCross } from "swissqrbill:shared:qr-code.js";
// eslint-disable-next-line eslint-plugin-unused-imports/no-unused-imports
import type { ValidationError } from "swissqrbill:errors";
import type { Data } from "swissqrbill:types";
export class SwissQRCode {
public instance: SVG;
/**
* Creates a Swiss QR Code.
*
* @param data The data to be encoded in the QR code.
* @param size The size of the QR code in mm.
* @throws { ValidationError } Throws an error if the data is invalid.
*/
constructor(data: Data, size: number = 46) {
this.instance = new SVG();
this.instance.width(`${size}mm`);
this.instance.height(`${size}mm`);
renderQRCode(data, size, (xPos, yPos, blockSize) => {
this.instance
.addRect(
`${xPos}mm`,
`${yPos}mm`,
`${blockSize}mm`,
`${blockSize}mm`
)
.fill("black");
});
renderSwissCross(size, (xPos, yPos, width, height, fillColor) => {
this.instance
.addRect(
`${xPos}mm`,
`${yPos}mm`,
`${width}mm`,
`${height}mm`
)
.fill(fillColor);
});
}
/**
* Outputs the SVG as a string.
*
* @returns The outerHTML of the SVG element.
*/
public toString(): string {
return this.instance.outerHTML;
}
/**
* Returns the SVG element.
*
* @returns The SVG element.
*/
public get element(): SVGElement {
return this.instance.element as unknown as SVGElement;
}
}