-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAbstractQRCodeWithImage.ts
149 lines (133 loc) · 4 KB
/
AbstractQRCodeWithImage.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import QRCodeRaw from './QRCodeRaw';
import type {
OptionsType as ParentOptionsType,
QRCodeDataType,
} from './QRCodeRaw';
import DimensionUtils from './utils/DimensionUtils';
export type ImageConfigType = {
source: string | typeof Image | HTMLCanvasElement | Promise<unknown>;
width: number | string; // 20 | 20%
height: number | string; // 20 | 20%
x: number | string; // 20 | 20% | center | left 20% | right 20%
y: number | string; // 20 | 20% | center | top 20% | bottom 20%,
border?: number;
};
export type OptionsType = ParentOptionsType & {
image?: ImageConfigType;
};
const DEFAULT_OPTIONS = {
image: null,
};
const DEFAULT_IMAGE_BORDER = 1;
export default class AbstractQRCodeWithImage extends QRCodeRaw {
image: ImageConfigType | null = null;
imageConfig: ImageConfigType | null = null;
constructor(value: string, options: Partial<OptionsType> = {}) {
super(value, options);
const params = { ...DEFAULT_OPTIONS, ...options };
this.image = params.image;
}
_clearCache(): void {
super._clearCache();
this.imageConfig = null;
}
_getImageSource(imageConfig: ImageConfigType): string | null {
const source = imageConfig.source;
if (typeof source === 'string') {
return source;
}
if (source instanceof Image) {
return source.src;
}
if (source instanceof HTMLCanvasElement) {
return source.toDataURL();
}
return null;
}
_getImageConfig(): ImageConfigType | null {
if (this.imageConfig) {
return this.imageConfig;
}
if (
!this.image ||
!this.image.source ||
!this.image.width ||
!this.image.height
) {
return null;
}
const dataSize = this.getDataSize();
if (!dataSize) {
return null;
}
const source = this._getImageSource(this.image);
if (!source) {
return null;
}
const dataSizeWithoutPadding = dataSize - this.padding * 2;
const width = DimensionUtils.calculateDimension(
this.image.width,
dataSizeWithoutPadding,
);
const height = DimensionUtils.calculateDimension(
this.image.height,
dataSizeWithoutPadding,
);
const x =
DimensionUtils.calculatePosition(
// @ts-expect-error make types stronger
this.image.x,
width,
dataSizeWithoutPadding,
) + this.padding;
const y =
DimensionUtils.calculatePosition(
// @ts-expect-error make types stronger
this.image.y,
height,
dataSizeWithoutPadding,
) + this.padding;
let border: number | null = DEFAULT_IMAGE_BORDER;
if (typeof this.image.border === 'number' || this.image.border === null) {
border = this.image.border;
}
this.imageConfig = { source, border, x, y, width, height };
return this.imageConfig;
}
getData(): QRCodeDataType | null {
if (this.qrCodeData) {
return this.qrCodeData;
}
const data = super.getData();
if (!data) {
return data;
}
// FIXME better handle string and number types
// @ts-expect-error make types stronger
const imageConfig: ImageConfigType = this._getImageConfig();
if (imageConfig !== null && imageConfig.width && imageConfig.height) {
if (typeof imageConfig.border === 'number') {
// @ts-expect-error make types stronger
const begX = Math.max(imageConfig.x - imageConfig.border, 0);
// @ts-expect-error make types stronger
const begY = Math.max(imageConfig.y - imageConfig.border, 0);
const endX = Math.min(
// @ts-expect-error make types stronger
begX + imageConfig.width + imageConfig.border * 2,
data.length,
);
const endY = Math.min(
// @ts-expect-error make types stronger
begY + imageConfig.height + imageConfig.border * 2,
data.length,
);
for (let y = begY; y < endY; y += 1) {
for (let x = begX; x < endX; x += 1) {
data[y][x] = this.invert ? true : false;
}
}
}
}
return data;
}
}