dsh-plugin-cyrene-live2d 0.2.9 → 0.3.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/README.md +8 -2
- package/assets/default-prefs.json +8 -8
- package/assets/icon.svg +89 -89
- package/assets/locale/notes/en.json +6 -6
- package/assets/locale/notes/zh-CN.json +6 -6
- package/assets/locale/reminder/en.json +6 -6
- package/assets/locale/reminder/zh-CN.json +6 -6
- package/assets/model/Cyrene.model3.json +441 -441
- package/assets/model/bongo-standard/Live2d_expression0.exp3.json +9 -9
- package/assets/model/bongo-standard/cat.cdi3.json +296 -296
- package/assets/model/bongo-standard/cat.model3.json +33 -33
- package/assets/model/bongo-standard/cat.physics3.json +232 -232
- package/assets/model/expressions/0.exp3.json +24 -24
- package/assets/model/expressions/1.exp3.json +11 -11
- package/assets/model/expressions/10.exp3.json +9 -9
- package/assets/model/expressions/11.exp3.json +9 -9
- package/assets/model/expressions/2.exp3.json +9 -9
- package/assets/model/expressions/3.exp3.json +9 -9
- package/assets/model/expressions/4.exp3.json +11 -11
- package/assets/model/expressions/5.exp3.json +11 -11
- package/assets/model/expressions/6.exp3.json +11 -11
- package/assets/model/expressions/7.exp3.json +11 -11
- package/assets/model/expressions/8.exp3.json +11 -11
- package/assets/model/expressions/9.exp3.json +9 -9
- package/assets/model/model.physics3.json +641 -641
- package/assets/model/motions/Tick3_3.motion3.json +1347 -1347
- package/assets/model/motions//345/212/250/344/275/2346_0.motion3.json +699 -699
- package/assets/model/motions//345/212/250/344/275/2346_1.motion3.json +1241 -1241
- package/assets/model/motions//345/212/250/344/275/2346_2.motion3.json +1117 -1117
- package/assets/model/motions//345/212/250/344/275/2346_3.motion3.json +1106 -1106
- package/assets/model/we-q/expressions/FileReferences_Expressions_0_File_0.exp3.json +19 -19
- package/assets/model/we-q/expressions/FileReferences_Expressions_1_File_0.exp3.json +41 -41
- package/assets/model/we-q/expressions/FileReferences_Expressions_2_File_0.exp3.json +104 -104
- package/assets/model/we-q/model0.model3.json +245 -245
- package/assets/model/we-q/motions/FileReferences_Motions_shake#1_0_File_0.motion3.json +520 -520
- package/assets/model/we-q/motions/FileReferences_Motions_talk#1_0_File_0.motion3.json +499 -499
- package/assets/model/we-q/motions/FileReferences_Motions_talk#1_1_File_0.motion3.json +453 -453
- package/assets/model/we-q/motions/FileReferences_Motions_wink#1_0_File_0.motion3.json +478 -478
- package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_0_File_0.motion3.json +450 -450
- package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_1_File_0.motion3.json +450 -450
- package/assets/model/we-q/motions/idle/FileReferences_Motions_Idleidle_0_File_0.motion3.json +422 -422
- package/assets/model/we-q/physics/FileReferences_Physics_0.physic3.json +236 -236
- package/assets/runtime/live2dcubismcore.min.js +8 -8
- package/assets/stickers.json +84 -84
- package/assets/vendor/cubism4.min.js +1 -1
- package/assets/vendor/pixi.min.js +9 -9
- package/assets/vendor/theme-cyrene-factory.js +4314 -4314
- package/assets/vendor/theme-cyrene.js +4317 -4317
- package/assets/voice/angry-00.opus +0 -0
- package/assets/voice/angry-02.opus +0 -0
- package/assets/voice/approval-00.opus +0 -0
- package/assets/voice/approval-01.opus +0 -0
- package/assets/voice/approval-02.opus +0 -0
- package/assets/voice/askAck-00.opus +0 -0
- package/assets/voice/away-01.opus +0 -0
- package/assets/voice/away-02.opus +0 -0
- package/assets/voice/away-03.opus +0 -0
- package/assets/voice/away-05.opus +0 -0
- package/assets/voice/back-00.opus +0 -0
- package/assets/voice/back-01.opus +0 -0
- package/assets/voice/backLong-05.opus +0 -0
- package/assets/voice/backShort-03.opus +0 -0
- package/assets/voice/backShort-04.opus +0 -0
- package/assets/voice/backShort-07.opus +0 -0
- package/assets/voice/backVeryLong-01.opus +0 -0
- package/assets/voice/backVeryLong-02.opus +0 -0
- package/assets/voice/backVeryLong-03.opus +0 -0
- package/assets/voice/backVeryLong-04.opus +0 -0
- package/assets/voice/backVeryLong-05.opus +0 -0
- package/assets/voice/backVeryLong-07.opus +0 -0
- package/assets/voice/backVeryLong-08.opus +0 -0
- package/assets/voice/balLow-00.opus +0 -0
- package/assets/voice/balLow-01.opus +0 -0
- package/assets/voice/birthday-02.opus +0 -0
- package/assets/voice/click-00.opus +0 -0
- package/assets/voice/click-01.opus +0 -0
- package/assets/voice/click-07.opus +0 -0
- package/assets/voice/click-10.opus +0 -0
- package/assets/voice/click-11.opus +0 -0
- package/assets/voice/click-12.opus +0 -0
- package/assets/voice/click-16.opus +0 -0
- package/assets/voice/click-17.opus +0 -0
- package/assets/voice/click-18.opus +0 -0
- package/assets/voice/comfort-03.opus +0 -0
- package/assets/voice/comfort-04.opus +0 -0
- package/assets/voice/cost10-00.opus +0 -0
- package/assets/voice/cost2-00.opus +0 -0
- package/assets/voice/cost5-00.opus +0 -0
- package/assets/voice/costTurn-01.opus +0 -0
- package/assets/voice/costTurn-02.opus +0 -0
- package/assets/voice/ctxFull-01.opus +0 -0
- package/assets/voice/ctxHigh-00.opus +0 -0
- package/assets/voice/dawn-01.opus +0 -0
- package/assets/voice/dawn-04.opus +0 -0
- package/assets/voice/dawn-06.opus +0 -0
- package/assets/voice/dawn-07.opus +0 -0
- package/assets/voice/deepWake-03.opus +0 -0
- package/assets/voice/deepWake-04.opus +0 -0
- package/assets/voice/deepWake-06.opus +0 -0
- package/assets/voice/deepWake-07.opus +0 -0
- package/assets/voice/done-05.opus +0 -0
- package/assets/voice/done-08.opus +0 -0
- package/assets/voice/done-09.opus +0 -0
- package/assets/voice/done-11.opus +0 -0
- package/assets/voice/empty-00.opus +0 -0
- package/assets/voice/encourage-02.opus +0 -0
- package/assets/voice/failed-01.opus +0 -0
- package/assets/voice/failed-05.opus +0 -0
- package/assets/voice/failkind-auth-01.opus +0 -0
- package/assets/voice/failkind-auth-02.opus +0 -0
- package/assets/voice/failkind-ctx-00.opus +0 -0
- package/assets/voice/failkind-missing-01.opus +0 -0
- package/assets/voice/focusEnd-02.opus +0 -0
- package/assets/voice/focusEnd-04.opus +0 -0
- package/assets/voice/forgive-00.opus +0 -0
- package/assets/voice/hello-00.opus +0 -0
- package/assets/voice/hello-03.opus +0 -0
- package/assets/voice/hold-00.opus +0 -0
- package/assets/voice/idleTap-00.opus +0 -0
- package/assets/voice/idleTapLong-02.opus +0 -0
- package/assets/voice/lateNight-00.opus +0 -0
- package/assets/voice/lateNight-02.opus +0 -0
- package/assets/voice/lateNight-03.opus +0 -0
- package/assets/voice/lateNight-06.opus +0 -0
- package/assets/voice/lateNight-07.opus +0 -0
- package/assets/voice/murmur-04.opus +0 -0
- package/assets/voice/night-01.opus +0 -0
- package/assets/voice/night-03.opus +0 -0
- package/assets/voice/night-05.opus +0 -0
- package/assets/voice/noon-04.opus +0 -0
- package/assets/voice/petArm-02.opus +0 -0
- package/assets/voice/petCheek-00.opus +0 -0
- package/assets/voice/petCheek-01.opus +0 -0
- package/assets/voice/petCheek-02.opus +0 -0
- package/assets/voice/petHead-01.opus +0 -0
- package/assets/voice/petHead-03.opus +0 -0
- package/assets/voice/petHead-04.opus +0 -0
- package/assets/voice/praised-01.opus +0 -0
- package/assets/voice/praised-03.opus +0 -0
- package/assets/voice/question-00.opus +0 -0
- package/assets/voice/question-03.opus +0 -0
- package/assets/voice/question-04.opus +0 -0
- package/assets/voice/question-06.opus +0 -0
- package/assets/voice/shy-00.opus +0 -0
- package/assets/voice/spot-0-0.opus +0 -0
- package/assets/voice/spot-0-1.opus +0 -0
- package/assets/voice/spot-0-2.opus +0 -0
- package/assets/voice/spot-3-0.opus +0 -0
- package/assets/voice/spot-4-0.opus +0 -0
- package/assets/voice/spot-4-2.opus +0 -0
- package/assets/voice/spot-5-2.opus +0 -0
- package/assets/voice/spot-6-0.opus +0 -0
- package/assets/voice/spot-6-1.opus +0 -0
- package/assets/voice/spot-7-0.opus +0 -0
- package/assets/voice/spot-7-2.opus +0 -0
- package/assets/voice/spot-8-1.opus +0 -0
- package/assets/voice/tagent-00.opus +0 -0
- package/assets/voice/thinkFast-01.opus +0 -0
- package/assets/voice/thinkFast-02.opus +0 -0
- package/assets/voice/thinkFast-03.opus +0 -0
- package/assets/voice/thinkSlow-01.opus +0 -0
- package/assets/voice/thinkSlow-03.opus +0 -0
- package/assets/voice/thinking-01.opus +0 -0
- package/assets/voice/thinking-02.opus +0 -0
- package/assets/voice/thinking-03.opus +0 -0
- package/assets/voice/thinking-09.opus +0 -0
- package/assets/voice/tread-00.opus +0 -0
- package/assets/voice/tread-01.opus +0 -0
- package/assets/voice/trun-02.opus +0 -0
- package/assets/voice/tsun-00.opus +0 -0
- package/assets/voice/tsun-01.opus +0 -0
- package/assets/voice/tsun-04.opus +0 -0
- package/assets/voice/twrite-00.opus +0 -0
- package/assets/voice/unknown-01.opus +0 -0
- package/assets/voice/waitApproval-00.opus +0 -0
- package/assets/voice/waitApproval-01.opus +0 -0
- package/assets/voice/waitApproval-02.opus +0 -0
- package/assets/voice/waitApproval-03.opus +0 -0
- package/assets/voice/waitApprovalLate-02.opus +0 -0
- package/assets/voice/waitApprovalLate-03.opus +0 -0
- package/assets/voice/waitAsk-00.opus +0 -0
- package/assets/voice/waitAsk-02.opus +0 -0
- package/assets/voice/waitAskLate-03.opus +0 -0
- package/assets/voice/wake-03.opus +0 -0
- package/assets/voice/wake-04.opus +0 -0
- package/assets/voice/wake-05.opus +0 -0
- package/assets/voice/wake-06.opus +0 -0
- package/lib/client.js +7 -7
- package/lib/features/notes.js +172 -172
- package/lib/features/reminder.js +216 -216
- package/lib/l2d-plan.js +295 -295
- package/lib/l2d-tools.js +293 -293
- package/lib/ledger.js +163 -163
- package/lib/mys/config.js +183 -183
- package/lib/mys/ds.js +209 -209
- package/lib/mys/http.js +85 -85
- package/lib/mys/mys.js +419 -419
- package/lib/mys/qr.js +434 -434
- package/lib/mys/scheduler.js +206 -206
- package/lib/mys/store.js +248 -248
- package/lib/mys/tz.js +174 -174
- package/lib/wavtrim.js +245 -245
- package/lib/worldbook/NOTICE.md +18 -18
- package/lib/worldbook/data/Cyrene.md +111 -111
- package/lib/worldbook/data/_glossary.md +181 -181
- package/lib/worldbook/data/characters.md +289 -289
- package/lib/worldbook/data/story.md +130 -130
- package/lib/worldbook/data/world.md +182 -182
- package/lib/worldbook.js +189 -189
- package/package.json +72 -69
package/lib/mys/qr.js
CHANGED
|
@@ -1,434 +1,434 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Small QR Code encoder (byte mode, error-correction level M, versions 1–15).
|
|
3
|
-
*
|
|
4
|
-
* Only what the login flow needs: about a hundred bytes of URL, scanned from a
|
|
5
|
-
* phone at close range. Levels and versions beyond that are deliberately absent
|
|
6
|
-
* so the tables stay auditable.
|
|
7
|
-
*
|
|
8
|
-
* The implementation follows ISO/IEC 18004: bit stream → Reed–Solomon blocks →
|
|
9
|
-
* matrix with function patterns → best of the eight data masks.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/** Reed–Solomon block layout and EC codewords per block, level M, versions 1–15. */
|
|
13
|
-
const BLOCKS_M = {
|
|
14
|
-
1: { ec: 10, groups: [[1, 16]] },
|
|
15
|
-
2: { ec: 16, groups: [[1, 28]] },
|
|
16
|
-
3: { ec: 26, groups: [[1, 44]] },
|
|
17
|
-
4: { ec: 18, groups: [[2, 32]] },
|
|
18
|
-
5: { ec: 24, groups: [[2, 43]] },
|
|
19
|
-
6: { ec: 16, groups: [[4, 27]] },
|
|
20
|
-
7: { ec: 18, groups: [[4, 31]] },
|
|
21
|
-
8: { ec: 22, groups: [[2, 38], [2, 39]] },
|
|
22
|
-
9: { ec: 22, groups: [[3, 36], [2, 37]] },
|
|
23
|
-
10: { ec: 26, groups: [[4, 43], [1, 44]] },
|
|
24
|
-
11: { ec: 30, groups: [[1, 50], [4, 51]] },
|
|
25
|
-
12: { ec: 22, groups: [[6, 36], [2, 37]] },
|
|
26
|
-
13: { ec: 22, groups: [[8, 37], [1, 38]] },
|
|
27
|
-
14: { ec: 24, groups: [[4, 40], [5, 41]] },
|
|
28
|
-
15: { ec: 24, groups: [[5, 41], [5, 42]] },
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** Alignment pattern centre coordinates, versions 1–15. */
|
|
32
|
-
const ALIGNMENT = {
|
|
33
|
-
1: [],
|
|
34
|
-
2: [6, 18],
|
|
35
|
-
3: [6, 22],
|
|
36
|
-
4: [6, 26],
|
|
37
|
-
5: [6, 30],
|
|
38
|
-
6: [6, 34],
|
|
39
|
-
7: [6, 22, 38],
|
|
40
|
-
8: [6, 24, 42],
|
|
41
|
-
9: [6, 26, 46],
|
|
42
|
-
10: [6, 28, 50],
|
|
43
|
-
11: [6, 30, 54],
|
|
44
|
-
12: [6, 32, 58],
|
|
45
|
-
13: [6, 34, 62],
|
|
46
|
-
14: [6, 26, 46, 66],
|
|
47
|
-
15: [6, 26, 48, 70],
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Galois field GF(256) with the QR primitive polynomial x^8+x^4+x^3+x^2+1.
|
|
51
|
-
const GF_EXP = new Uint8Array(512)
|
|
52
|
-
const GF_LOG = new Uint8Array(256)
|
|
53
|
-
;(() => {
|
|
54
|
-
let value = 1
|
|
55
|
-
for (let index = 0; index < 255; index += 1) {
|
|
56
|
-
GF_EXP[index] = value
|
|
57
|
-
GF_LOG[value] = index
|
|
58
|
-
value <<= 1
|
|
59
|
-
if (value & 0x100) value ^= 0x11d
|
|
60
|
-
}
|
|
61
|
-
for (let index = 255; index < 512; index += 1) GF_EXP[index] = GF_EXP[index - 255]
|
|
62
|
-
})()
|
|
63
|
-
|
|
64
|
-
function gfMul(left, right) {
|
|
65
|
-
if (left === 0 || right === 0) return 0
|
|
66
|
-
return GF_EXP[GF_LOG[left] + GF_LOG[right]]
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function rsGenerator(degree) {
|
|
70
|
-
let poly = [1]
|
|
71
|
-
for (let index = 0; index < degree; index += 1) {
|
|
72
|
-
const alpha = GF_EXP[index]
|
|
73
|
-
const next = new Array(poly.length + 1).fill(0)
|
|
74
|
-
for (let term = 0; term < poly.length; term += 1) {
|
|
75
|
-
next[term] ^= poly[term]
|
|
76
|
-
next[term + 1] ^= gfMul(poly[term], alpha)
|
|
77
|
-
}
|
|
78
|
-
poly = next
|
|
79
|
-
}
|
|
80
|
-
return poly
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function rsRemainder(data, ecLength) {
|
|
84
|
-
const generator = rsGenerator(ecLength)
|
|
85
|
-
const buffer = new Uint8Array(data.length + ecLength)
|
|
86
|
-
buffer.set(data)
|
|
87
|
-
for (let index = 0; index < data.length; index += 1) {
|
|
88
|
-
const factor = buffer[index]
|
|
89
|
-
if (factor === 0) continue
|
|
90
|
-
for (let term = 1; term < generator.length; term += 1) {
|
|
91
|
-
buffer[index + term] ^= gfMul(generator[term], factor)
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return buffer.subarray(data.length)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function utf8Bytes(text) {
|
|
98
|
-
return new TextEncoder().encode(text)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function characterCountBits(version) {
|
|
102
|
-
return version <= 9 ? 8 : 16
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function dataCapacity(version) {
|
|
106
|
-
const block = BLOCKS_M[version]
|
|
107
|
-
return block.groups.reduce((total, [count, length]) => total + count * length, 0)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function chooseVersion(byteLength) {
|
|
111
|
-
for (let version = 1; version <= 15; version += 1) {
|
|
112
|
-
const overhead = 4 + characterCountBits(version)
|
|
113
|
-
if (overhead + byteLength * 8 <= dataCapacity(version) * 8) return version
|
|
114
|
-
}
|
|
115
|
-
return null
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
class BitBuffer {
|
|
119
|
-
constructor() {
|
|
120
|
-
this.bits = []
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
push(value, length) {
|
|
124
|
-
for (let index = length - 1; index >= 0; index -= 1) {
|
|
125
|
-
this.bits.push((value >>> index) & 1)
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function buildCodewords(version, bytes) {
|
|
131
|
-
const buffer = new BitBuffer()
|
|
132
|
-
buffer.push(0b0100, 4)
|
|
133
|
-
buffer.push(bytes.length, characterCountBits(version))
|
|
134
|
-
for (const byte of bytes) buffer.push(byte, 8)
|
|
135
|
-
|
|
136
|
-
const capacityBits = dataCapacity(version) * 8
|
|
137
|
-
const terminator = Math.min(4, capacityBits - buffer.bits.length)
|
|
138
|
-
buffer.push(0, terminator)
|
|
139
|
-
while (buffer.bits.length % 8 !== 0) buffer.bits.push(0)
|
|
140
|
-
|
|
141
|
-
const data = new Uint8Array(dataCapacity(version))
|
|
142
|
-
for (let index = 0; index < buffer.bits.length; index += 8) {
|
|
143
|
-
let value = 0
|
|
144
|
-
for (let bit = 0; bit < 8; bit += 1) value = (value << 1) | buffer.bits[index + bit]
|
|
145
|
-
data[index / 8] = value
|
|
146
|
-
}
|
|
147
|
-
let pad = 0
|
|
148
|
-
for (let index = buffer.bits.length / 8; index < data.length; index += 1) {
|
|
149
|
-
data[index] = pad % 2 === 0 ? 0xec : 0x11
|
|
150
|
-
pad += 1
|
|
151
|
-
}
|
|
152
|
-
return data
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Split data into blocks, append EC codewords, and interleave both streams. */
|
|
156
|
-
function interleave(version, data) {
|
|
157
|
-
const block = BLOCKS_M[version]
|
|
158
|
-
const blocks = []
|
|
159
|
-
let offset = 0
|
|
160
|
-
for (const [count, length] of block.groups) {
|
|
161
|
-
for (let index = 0; index < count; index += 1) {
|
|
162
|
-
const slice = data.subarray(offset, offset + length)
|
|
163
|
-
offset += length
|
|
164
|
-
blocks.push({ data: slice, ec: rsRemainder(slice, block.ec) })
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const out = []
|
|
169
|
-
const maxData = Math.max(...blocks.map((item) => item.data.length))
|
|
170
|
-
for (let index = 0; index < maxData; index += 1) {
|
|
171
|
-
for (const item of blocks) if (index < item.data.length) out.push(item.data[index])
|
|
172
|
-
}
|
|
173
|
-
for (let index = 0; index < block.ec; index += 1) {
|
|
174
|
-
for (const item of blocks) out.push(item.ec[index])
|
|
175
|
-
}
|
|
176
|
-
return Uint8Array.from(out)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function bitLength(value) {
|
|
180
|
-
let length = 0
|
|
181
|
-
let rest = value
|
|
182
|
-
while (rest !== 0) {
|
|
183
|
-
rest >>>= 1
|
|
184
|
-
length += 1
|
|
185
|
-
}
|
|
186
|
-
return length
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function bchRemainder(value, poly, polyBits) {
|
|
190
|
-
let remainder = value
|
|
191
|
-
while (bitLength(remainder) > polyBits) {
|
|
192
|
-
// Align the generator's top bit (at `polyBits`) with the remainder's top bit.
|
|
193
|
-
remainder ^= poly << (bitLength(remainder) - polyBits - 1)
|
|
194
|
-
}
|
|
195
|
-
return remainder
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function formatBits(mask) {
|
|
199
|
-
const data = (0b00 << 3) | mask // level M = 00
|
|
200
|
-
const rem = bchRemainder(data << 10, 0b10100110111, 10)
|
|
201
|
-
return ((data << 10) | rem) ^ 0b101010000010010
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function versionBits(version) {
|
|
205
|
-
const rem = bchRemainder(version << 12, 0b1111100100101, 12)
|
|
206
|
-
return (version << 12) | rem
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const MASKS = [
|
|
210
|
-
(row, col) => (row + col) % 2 === 0,
|
|
211
|
-
(row) => row % 2 === 0,
|
|
212
|
-
(_, col) => col % 3 === 0,
|
|
213
|
-
(row, col) => (row + col) % 3 === 0,
|
|
214
|
-
(row, col) => (Math.floor(row / 2) + Math.floor(col / 3)) % 2 === 0,
|
|
215
|
-
(row, col) => ((row * col) % 2) + ((row * col) % 3) === 0,
|
|
216
|
-
(row, col) => (((row * col) % 2) + ((row * col) % 3)) % 2 === 0,
|
|
217
|
-
(row, col) => (((row + col) % 2) + ((row * col) % 3)) % 2 === 0,
|
|
218
|
-
]
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Build the full module matrix for one mask.
|
|
222
|
-
* `null` marks an unset module; function patterns are written first.
|
|
223
|
-
*/
|
|
224
|
-
function buildMatrix(version, codewords, mask) {
|
|
225
|
-
const size = version * 4 + 17
|
|
226
|
-
const modules = new Int8Array(size * size).fill(-1)
|
|
227
|
-
|
|
228
|
-
const set = (row, col, dark) => {
|
|
229
|
-
if (row < 0 || col < 0 || row >= size || col >= size) return
|
|
230
|
-
modules[row * size + col] = dark ? 1 : 0
|
|
231
|
-
}
|
|
232
|
-
const isSet = (row, col) => modules[row * size + col] !== -1
|
|
233
|
-
|
|
234
|
-
const finder = (row, col) => {
|
|
235
|
-
for (let dr = -1; dr <= 7; dr += 1) {
|
|
236
|
-
for (let dc = -1; dc <= 7; dc += 1) {
|
|
237
|
-
const r = row + dr
|
|
238
|
-
const c = col + dc
|
|
239
|
-
if (r < 0 || c < 0 || r >= size || c >= size) continue
|
|
240
|
-
const onRing = (dr >= 0 && dr <= 6 && (dc === 0 || dc === 6))
|
|
241
|
-
|| (dc >= 0 && dc <= 6 && (dr === 0 || dr === 6))
|
|
242
|
-
const onCore = dr >= 2 && dr <= 4 && dc >= 2 && dc <= 4
|
|
243
|
-
set(r, c, onRing || onCore)
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
finder(0, 0)
|
|
248
|
-
finder(size - 7, 0)
|
|
249
|
-
finder(0, size - 7)
|
|
250
|
-
|
|
251
|
-
// Alignment squares go down before the timing lines: a centre that sits on a
|
|
252
|
-
// timing line (versions 7+) replaces that stretch of it, which is what the
|
|
253
|
-
// spec draws.
|
|
254
|
-
for (const row of ALIGNMENT[version]) {
|
|
255
|
-
for (const col of ALIGNMENT[version]) {
|
|
256
|
-
if (row === 6 && col === 6) continue
|
|
257
|
-
if (isSet(row, col)) continue
|
|
258
|
-
for (let dr = -2; dr <= 2; dr += 1) {
|
|
259
|
-
for (let dc = -2; dc <= 2; dc += 1) {
|
|
260
|
-
const dark = Math.abs(dr) === 2 || Math.abs(dc) === 2 || (dr === 0 && dc === 0)
|
|
261
|
-
set(row + dr, col + dc, dark)
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
for (let index = 8; index < size - 8; index += 1) {
|
|
268
|
-
if (!isSet(index, 6)) set(index, 6, index % 2 === 0)
|
|
269
|
-
if (!isSet(6, index)) set(6, index, index % 2 === 0)
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const bits = formatBits(mask)
|
|
273
|
-
for (let index = 0; index < 15; index += 1) {
|
|
274
|
-
const dark = ((bits >> index) & 1) === 1
|
|
275
|
-
if (index < 6) set(index, 8, dark)
|
|
276
|
-
else if (index < 8) set(index + 1, 8, dark)
|
|
277
|
-
else set(size - 15 + index, 8, dark)
|
|
278
|
-
|
|
279
|
-
if (index < 8) set(8, size - index - 1, dark)
|
|
280
|
-
else if (index < 9) set(8, 15 - index, dark)
|
|
281
|
-
else set(8, 15 - index - 1, dark)
|
|
282
|
-
}
|
|
283
|
-
set(size - 8, 8, true)
|
|
284
|
-
|
|
285
|
-
if (version >= 7) {
|
|
286
|
-
const value = versionBits(version)
|
|
287
|
-
for (let index = 0; index < 18; index += 1) {
|
|
288
|
-
const dark = ((value >> index) & 1) === 1
|
|
289
|
-
set(Math.floor(index / 3), (index % 3) + size - 11, dark)
|
|
290
|
-
set((index % 3) + size - 11, Math.floor(index / 3), dark)
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// Data placement: two-module columns, bottom-up then top-down, skipping the
|
|
295
|
-
// vertical timing column and every function module.
|
|
296
|
-
let bit = 0
|
|
297
|
-
const totalBits = codewords.length * 8
|
|
298
|
-
const maskFn = MASKS[mask]
|
|
299
|
-
let upward = true
|
|
300
|
-
for (let right = size - 1; right >= 1; right -= 2) {
|
|
301
|
-
if (right === 6) right = 5
|
|
302
|
-
for (let step = 0; step < size; step += 1) {
|
|
303
|
-
const row = upward ? size - 1 - step : step
|
|
304
|
-
for (let column = 0; column < 2; column += 1) {
|
|
305
|
-
const col = right - column
|
|
306
|
-
if (isSet(row, col)) continue
|
|
307
|
-
let dark = false
|
|
308
|
-
if (bit < totalBits) {
|
|
309
|
-
dark = ((codewords[bit >> 3] >> (7 - (bit & 7))) & 1) === 1
|
|
310
|
-
}
|
|
311
|
-
bit += 1
|
|
312
|
-
modules[row * size + col] = (dark !== maskFn(row, col)) ? 1 : 0
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
upward = !upward
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
return { size, modules }
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function penalty({ size, modules }) {
|
|
322
|
-
const at = (row, col) => modules[row * size + col] === 1
|
|
323
|
-
let score = 0
|
|
324
|
-
|
|
325
|
-
for (let row = 0; row < size; row += 1) {
|
|
326
|
-
let run = 1
|
|
327
|
-
for (let col = 1; col < size; col += 1) {
|
|
328
|
-
if (at(row, col) === at(row, col - 1)) run += 1
|
|
329
|
-
else {
|
|
330
|
-
if (run >= 5) score += 3 + (run - 5)
|
|
331
|
-
run = 1
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
if (run >= 5) score += 3 + (run - 5)
|
|
335
|
-
}
|
|
336
|
-
for (let col = 0; col < size; col += 1) {
|
|
337
|
-
let run = 1
|
|
338
|
-
for (let row = 1; row < size; row += 1) {
|
|
339
|
-
if (at(row, col) === at(row - 1, col)) run += 1
|
|
340
|
-
else {
|
|
341
|
-
if (run >= 5) score += 3 + (run - 5)
|
|
342
|
-
run = 1
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
if (run >= 5) score += 3 + (run - 5)
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
for (let row = 0; row < size - 1; row += 1) {
|
|
349
|
-
for (let col = 0; col < size - 1; col += 1) {
|
|
350
|
-
const first = at(row, col)
|
|
351
|
-
if (first === at(row, col + 1) && first === at(row + 1, col) && first === at(row + 1, col + 1)) {
|
|
352
|
-
score += 3
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const finderA = [1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0]
|
|
358
|
-
const finderB = [0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1]
|
|
359
|
-
const window = new Array(11)
|
|
360
|
-
const matches = (get, start) => {
|
|
361
|
-
for (let index = 0; index < 11; index += 1) window[index] = get(start + index) ? 1 : 0
|
|
362
|
-
const isA = finderA.every((value, index) => window[index] === value)
|
|
363
|
-
if (isA) return true
|
|
364
|
-
return finderB.every((value, index) => window[index] === value)
|
|
365
|
-
}
|
|
366
|
-
for (let row = 0; row < size; row += 1) {
|
|
367
|
-
for (let col = 0; col <= size - 11; col += 1) {
|
|
368
|
-
if (matches((index) => at(row, index), col)) score += 40
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
for (let col = 0; col < size; col += 1) {
|
|
372
|
-
for (let row = 0; row <= size - 11; row += 1) {
|
|
373
|
-
if (matches((index) => at(index, col), row)) score += 40
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
let dark = 0
|
|
378
|
-
for (let index = 0; index < modules.length; index += 1) if (modules[index] === 1) dark += 1
|
|
379
|
-
const percent = (dark * 100) / (size * size)
|
|
380
|
-
score += Math.floor(Math.abs(percent - 50) / 5) * 10
|
|
381
|
-
return score
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
/**
|
|
385
|
-
* Encode text into a QR matrix.
|
|
386
|
-
*
|
|
387
|
-
* @param {string} text
|
|
388
|
-
* @returns {{version: number, size: number, modules: Uint8Array}}
|
|
389
|
-
*/
|
|
390
|
-
export function encodeQr(text) {
|
|
391
|
-
const bytes = utf8Bytes(String(text))
|
|
392
|
-
const version = chooseVersion(bytes.length)
|
|
393
|
-
if (version === null) throw new RangeError('二维码内容过长,无法编码')
|
|
394
|
-
const data = buildCodewords(version, bytes)
|
|
395
|
-
const codewords = interleave(version, data)
|
|
396
|
-
|
|
397
|
-
let best = null
|
|
398
|
-
let bestScore = Number.POSITIVE_INFINITY
|
|
399
|
-
for (let mask = 0; mask < 8; mask += 1) {
|
|
400
|
-
const matrix = buildMatrix(version, codewords, mask)
|
|
401
|
-
const score = penalty(matrix)
|
|
402
|
-
if (score < bestScore) {
|
|
403
|
-
bestScore = score
|
|
404
|
-
best = matrix
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
return { version, size: best.size, modules: best.modules }
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
* Render text as a self-contained SVG data URL.
|
|
412
|
-
*
|
|
413
|
-
* A single path with one `M` per dark run keeps the payload small enough to
|
|
414
|
-
* stay well inside the settings transport's message limits.
|
|
415
|
-
*/
|
|
416
|
-
export function qrSvgDataUrl(text, { scale = 6, margin = 3 } = {}) {
|
|
417
|
-
const { size, modules } = encodeQr(text)
|
|
418
|
-
const total = size + margin * 2
|
|
419
|
-
const parts = []
|
|
420
|
-
for (let row = 0; row < size; row += 1) {
|
|
421
|
-
let col = 0
|
|
422
|
-
while (col < size) {
|
|
423
|
-
if (modules[row * size + col] !== 1) {
|
|
424
|
-
col += 1
|
|
425
|
-
continue
|
|
426
|
-
}
|
|
427
|
-
const start = col
|
|
428
|
-
while (col < size && modules[row * size + col] === 1) col += 1
|
|
429
|
-
parts.push(`M${start + margin} ${row + margin}h${col - start}v1h-${col - start}z`)
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${total} ${total}" width="${total * scale}" height="${total * scale}" shape-rendering="crispEdges"><rect width="${total}" height="${total}" fill="#ffffff"/><path d="${parts.join('')}" fill="#000000"/></svg>`
|
|
433
|
-
return `data:image/svg+xml;base64,${Buffer.from(svg, 'utf8').toString('base64')}`
|
|
434
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Small QR Code encoder (byte mode, error-correction level M, versions 1–15).
|
|
3
|
+
*
|
|
4
|
+
* Only what the login flow needs: about a hundred bytes of URL, scanned from a
|
|
5
|
+
* phone at close range. Levels and versions beyond that are deliberately absent
|
|
6
|
+
* so the tables stay auditable.
|
|
7
|
+
*
|
|
8
|
+
* The implementation follows ISO/IEC 18004: bit stream → Reed–Solomon blocks →
|
|
9
|
+
* matrix with function patterns → best of the eight data masks.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Reed–Solomon block layout and EC codewords per block, level M, versions 1–15. */
|
|
13
|
+
const BLOCKS_M = {
|
|
14
|
+
1: { ec: 10, groups: [[1, 16]] },
|
|
15
|
+
2: { ec: 16, groups: [[1, 28]] },
|
|
16
|
+
3: { ec: 26, groups: [[1, 44]] },
|
|
17
|
+
4: { ec: 18, groups: [[2, 32]] },
|
|
18
|
+
5: { ec: 24, groups: [[2, 43]] },
|
|
19
|
+
6: { ec: 16, groups: [[4, 27]] },
|
|
20
|
+
7: { ec: 18, groups: [[4, 31]] },
|
|
21
|
+
8: { ec: 22, groups: [[2, 38], [2, 39]] },
|
|
22
|
+
9: { ec: 22, groups: [[3, 36], [2, 37]] },
|
|
23
|
+
10: { ec: 26, groups: [[4, 43], [1, 44]] },
|
|
24
|
+
11: { ec: 30, groups: [[1, 50], [4, 51]] },
|
|
25
|
+
12: { ec: 22, groups: [[6, 36], [2, 37]] },
|
|
26
|
+
13: { ec: 22, groups: [[8, 37], [1, 38]] },
|
|
27
|
+
14: { ec: 24, groups: [[4, 40], [5, 41]] },
|
|
28
|
+
15: { ec: 24, groups: [[5, 41], [5, 42]] },
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Alignment pattern centre coordinates, versions 1–15. */
|
|
32
|
+
const ALIGNMENT = {
|
|
33
|
+
1: [],
|
|
34
|
+
2: [6, 18],
|
|
35
|
+
3: [6, 22],
|
|
36
|
+
4: [6, 26],
|
|
37
|
+
5: [6, 30],
|
|
38
|
+
6: [6, 34],
|
|
39
|
+
7: [6, 22, 38],
|
|
40
|
+
8: [6, 24, 42],
|
|
41
|
+
9: [6, 26, 46],
|
|
42
|
+
10: [6, 28, 50],
|
|
43
|
+
11: [6, 30, 54],
|
|
44
|
+
12: [6, 32, 58],
|
|
45
|
+
13: [6, 34, 62],
|
|
46
|
+
14: [6, 26, 46, 66],
|
|
47
|
+
15: [6, 26, 48, 70],
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Galois field GF(256) with the QR primitive polynomial x^8+x^4+x^3+x^2+1.
|
|
51
|
+
const GF_EXP = new Uint8Array(512)
|
|
52
|
+
const GF_LOG = new Uint8Array(256)
|
|
53
|
+
;(() => {
|
|
54
|
+
let value = 1
|
|
55
|
+
for (let index = 0; index < 255; index += 1) {
|
|
56
|
+
GF_EXP[index] = value
|
|
57
|
+
GF_LOG[value] = index
|
|
58
|
+
value <<= 1
|
|
59
|
+
if (value & 0x100) value ^= 0x11d
|
|
60
|
+
}
|
|
61
|
+
for (let index = 255; index < 512; index += 1) GF_EXP[index] = GF_EXP[index - 255]
|
|
62
|
+
})()
|
|
63
|
+
|
|
64
|
+
function gfMul(left, right) {
|
|
65
|
+
if (left === 0 || right === 0) return 0
|
|
66
|
+
return GF_EXP[GF_LOG[left] + GF_LOG[right]]
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function rsGenerator(degree) {
|
|
70
|
+
let poly = [1]
|
|
71
|
+
for (let index = 0; index < degree; index += 1) {
|
|
72
|
+
const alpha = GF_EXP[index]
|
|
73
|
+
const next = new Array(poly.length + 1).fill(0)
|
|
74
|
+
for (let term = 0; term < poly.length; term += 1) {
|
|
75
|
+
next[term] ^= poly[term]
|
|
76
|
+
next[term + 1] ^= gfMul(poly[term], alpha)
|
|
77
|
+
}
|
|
78
|
+
poly = next
|
|
79
|
+
}
|
|
80
|
+
return poly
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function rsRemainder(data, ecLength) {
|
|
84
|
+
const generator = rsGenerator(ecLength)
|
|
85
|
+
const buffer = new Uint8Array(data.length + ecLength)
|
|
86
|
+
buffer.set(data)
|
|
87
|
+
for (let index = 0; index < data.length; index += 1) {
|
|
88
|
+
const factor = buffer[index]
|
|
89
|
+
if (factor === 0) continue
|
|
90
|
+
for (let term = 1; term < generator.length; term += 1) {
|
|
91
|
+
buffer[index + term] ^= gfMul(generator[term], factor)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return buffer.subarray(data.length)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function utf8Bytes(text) {
|
|
98
|
+
return new TextEncoder().encode(text)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function characterCountBits(version) {
|
|
102
|
+
return version <= 9 ? 8 : 16
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function dataCapacity(version) {
|
|
106
|
+
const block = BLOCKS_M[version]
|
|
107
|
+
return block.groups.reduce((total, [count, length]) => total + count * length, 0)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function chooseVersion(byteLength) {
|
|
111
|
+
for (let version = 1; version <= 15; version += 1) {
|
|
112
|
+
const overhead = 4 + characterCountBits(version)
|
|
113
|
+
if (overhead + byteLength * 8 <= dataCapacity(version) * 8) return version
|
|
114
|
+
}
|
|
115
|
+
return null
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
class BitBuffer {
|
|
119
|
+
constructor() {
|
|
120
|
+
this.bits = []
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
push(value, length) {
|
|
124
|
+
for (let index = length - 1; index >= 0; index -= 1) {
|
|
125
|
+
this.bits.push((value >>> index) & 1)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function buildCodewords(version, bytes) {
|
|
131
|
+
const buffer = new BitBuffer()
|
|
132
|
+
buffer.push(0b0100, 4)
|
|
133
|
+
buffer.push(bytes.length, characterCountBits(version))
|
|
134
|
+
for (const byte of bytes) buffer.push(byte, 8)
|
|
135
|
+
|
|
136
|
+
const capacityBits = dataCapacity(version) * 8
|
|
137
|
+
const terminator = Math.min(4, capacityBits - buffer.bits.length)
|
|
138
|
+
buffer.push(0, terminator)
|
|
139
|
+
while (buffer.bits.length % 8 !== 0) buffer.bits.push(0)
|
|
140
|
+
|
|
141
|
+
const data = new Uint8Array(dataCapacity(version))
|
|
142
|
+
for (let index = 0; index < buffer.bits.length; index += 8) {
|
|
143
|
+
let value = 0
|
|
144
|
+
for (let bit = 0; bit < 8; bit += 1) value = (value << 1) | buffer.bits[index + bit]
|
|
145
|
+
data[index / 8] = value
|
|
146
|
+
}
|
|
147
|
+
let pad = 0
|
|
148
|
+
for (let index = buffer.bits.length / 8; index < data.length; index += 1) {
|
|
149
|
+
data[index] = pad % 2 === 0 ? 0xec : 0x11
|
|
150
|
+
pad += 1
|
|
151
|
+
}
|
|
152
|
+
return data
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Split data into blocks, append EC codewords, and interleave both streams. */
|
|
156
|
+
function interleave(version, data) {
|
|
157
|
+
const block = BLOCKS_M[version]
|
|
158
|
+
const blocks = []
|
|
159
|
+
let offset = 0
|
|
160
|
+
for (const [count, length] of block.groups) {
|
|
161
|
+
for (let index = 0; index < count; index += 1) {
|
|
162
|
+
const slice = data.subarray(offset, offset + length)
|
|
163
|
+
offset += length
|
|
164
|
+
blocks.push({ data: slice, ec: rsRemainder(slice, block.ec) })
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const out = []
|
|
169
|
+
const maxData = Math.max(...blocks.map((item) => item.data.length))
|
|
170
|
+
for (let index = 0; index < maxData; index += 1) {
|
|
171
|
+
for (const item of blocks) if (index < item.data.length) out.push(item.data[index])
|
|
172
|
+
}
|
|
173
|
+
for (let index = 0; index < block.ec; index += 1) {
|
|
174
|
+
for (const item of blocks) out.push(item.ec[index])
|
|
175
|
+
}
|
|
176
|
+
return Uint8Array.from(out)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function bitLength(value) {
|
|
180
|
+
let length = 0
|
|
181
|
+
let rest = value
|
|
182
|
+
while (rest !== 0) {
|
|
183
|
+
rest >>>= 1
|
|
184
|
+
length += 1
|
|
185
|
+
}
|
|
186
|
+
return length
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function bchRemainder(value, poly, polyBits) {
|
|
190
|
+
let remainder = value
|
|
191
|
+
while (bitLength(remainder) > polyBits) {
|
|
192
|
+
// Align the generator's top bit (at `polyBits`) with the remainder's top bit.
|
|
193
|
+
remainder ^= poly << (bitLength(remainder) - polyBits - 1)
|
|
194
|
+
}
|
|
195
|
+
return remainder
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function formatBits(mask) {
|
|
199
|
+
const data = (0b00 << 3) | mask // level M = 00
|
|
200
|
+
const rem = bchRemainder(data << 10, 0b10100110111, 10)
|
|
201
|
+
return ((data << 10) | rem) ^ 0b101010000010010
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function versionBits(version) {
|
|
205
|
+
const rem = bchRemainder(version << 12, 0b1111100100101, 12)
|
|
206
|
+
return (version << 12) | rem
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const MASKS = [
|
|
210
|
+
(row, col) => (row + col) % 2 === 0,
|
|
211
|
+
(row) => row % 2 === 0,
|
|
212
|
+
(_, col) => col % 3 === 0,
|
|
213
|
+
(row, col) => (row + col) % 3 === 0,
|
|
214
|
+
(row, col) => (Math.floor(row / 2) + Math.floor(col / 3)) % 2 === 0,
|
|
215
|
+
(row, col) => ((row * col) % 2) + ((row * col) % 3) === 0,
|
|
216
|
+
(row, col) => (((row * col) % 2) + ((row * col) % 3)) % 2 === 0,
|
|
217
|
+
(row, col) => (((row + col) % 2) + ((row * col) % 3)) % 2 === 0,
|
|
218
|
+
]
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Build the full module matrix for one mask.
|
|
222
|
+
* `null` marks an unset module; function patterns are written first.
|
|
223
|
+
*/
|
|
224
|
+
function buildMatrix(version, codewords, mask) {
|
|
225
|
+
const size = version * 4 + 17
|
|
226
|
+
const modules = new Int8Array(size * size).fill(-1)
|
|
227
|
+
|
|
228
|
+
const set = (row, col, dark) => {
|
|
229
|
+
if (row < 0 || col < 0 || row >= size || col >= size) return
|
|
230
|
+
modules[row * size + col] = dark ? 1 : 0
|
|
231
|
+
}
|
|
232
|
+
const isSet = (row, col) => modules[row * size + col] !== -1
|
|
233
|
+
|
|
234
|
+
const finder = (row, col) => {
|
|
235
|
+
for (let dr = -1; dr <= 7; dr += 1) {
|
|
236
|
+
for (let dc = -1; dc <= 7; dc += 1) {
|
|
237
|
+
const r = row + dr
|
|
238
|
+
const c = col + dc
|
|
239
|
+
if (r < 0 || c < 0 || r >= size || c >= size) continue
|
|
240
|
+
const onRing = (dr >= 0 && dr <= 6 && (dc === 0 || dc === 6))
|
|
241
|
+
|| (dc >= 0 && dc <= 6 && (dr === 0 || dr === 6))
|
|
242
|
+
const onCore = dr >= 2 && dr <= 4 && dc >= 2 && dc <= 4
|
|
243
|
+
set(r, c, onRing || onCore)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
finder(0, 0)
|
|
248
|
+
finder(size - 7, 0)
|
|
249
|
+
finder(0, size - 7)
|
|
250
|
+
|
|
251
|
+
// Alignment squares go down before the timing lines: a centre that sits on a
|
|
252
|
+
// timing line (versions 7+) replaces that stretch of it, which is what the
|
|
253
|
+
// spec draws.
|
|
254
|
+
for (const row of ALIGNMENT[version]) {
|
|
255
|
+
for (const col of ALIGNMENT[version]) {
|
|
256
|
+
if (row === 6 && col === 6) continue
|
|
257
|
+
if (isSet(row, col)) continue
|
|
258
|
+
for (let dr = -2; dr <= 2; dr += 1) {
|
|
259
|
+
for (let dc = -2; dc <= 2; dc += 1) {
|
|
260
|
+
const dark = Math.abs(dr) === 2 || Math.abs(dc) === 2 || (dr === 0 && dc === 0)
|
|
261
|
+
set(row + dr, col + dc, dark)
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
for (let index = 8; index < size - 8; index += 1) {
|
|
268
|
+
if (!isSet(index, 6)) set(index, 6, index % 2 === 0)
|
|
269
|
+
if (!isSet(6, index)) set(6, index, index % 2 === 0)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const bits = formatBits(mask)
|
|
273
|
+
for (let index = 0; index < 15; index += 1) {
|
|
274
|
+
const dark = ((bits >> index) & 1) === 1
|
|
275
|
+
if (index < 6) set(index, 8, dark)
|
|
276
|
+
else if (index < 8) set(index + 1, 8, dark)
|
|
277
|
+
else set(size - 15 + index, 8, dark)
|
|
278
|
+
|
|
279
|
+
if (index < 8) set(8, size - index - 1, dark)
|
|
280
|
+
else if (index < 9) set(8, 15 - index, dark)
|
|
281
|
+
else set(8, 15 - index - 1, dark)
|
|
282
|
+
}
|
|
283
|
+
set(size - 8, 8, true)
|
|
284
|
+
|
|
285
|
+
if (version >= 7) {
|
|
286
|
+
const value = versionBits(version)
|
|
287
|
+
for (let index = 0; index < 18; index += 1) {
|
|
288
|
+
const dark = ((value >> index) & 1) === 1
|
|
289
|
+
set(Math.floor(index / 3), (index % 3) + size - 11, dark)
|
|
290
|
+
set((index % 3) + size - 11, Math.floor(index / 3), dark)
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Data placement: two-module columns, bottom-up then top-down, skipping the
|
|
295
|
+
// vertical timing column and every function module.
|
|
296
|
+
let bit = 0
|
|
297
|
+
const totalBits = codewords.length * 8
|
|
298
|
+
const maskFn = MASKS[mask]
|
|
299
|
+
let upward = true
|
|
300
|
+
for (let right = size - 1; right >= 1; right -= 2) {
|
|
301
|
+
if (right === 6) right = 5
|
|
302
|
+
for (let step = 0; step < size; step += 1) {
|
|
303
|
+
const row = upward ? size - 1 - step : step
|
|
304
|
+
for (let column = 0; column < 2; column += 1) {
|
|
305
|
+
const col = right - column
|
|
306
|
+
if (isSet(row, col)) continue
|
|
307
|
+
let dark = false
|
|
308
|
+
if (bit < totalBits) {
|
|
309
|
+
dark = ((codewords[bit >> 3] >> (7 - (bit & 7))) & 1) === 1
|
|
310
|
+
}
|
|
311
|
+
bit += 1
|
|
312
|
+
modules[row * size + col] = (dark !== maskFn(row, col)) ? 1 : 0
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
upward = !upward
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return { size, modules }
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function penalty({ size, modules }) {
|
|
322
|
+
const at = (row, col) => modules[row * size + col] === 1
|
|
323
|
+
let score = 0
|
|
324
|
+
|
|
325
|
+
for (let row = 0; row < size; row += 1) {
|
|
326
|
+
let run = 1
|
|
327
|
+
for (let col = 1; col < size; col += 1) {
|
|
328
|
+
if (at(row, col) === at(row, col - 1)) run += 1
|
|
329
|
+
else {
|
|
330
|
+
if (run >= 5) score += 3 + (run - 5)
|
|
331
|
+
run = 1
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (run >= 5) score += 3 + (run - 5)
|
|
335
|
+
}
|
|
336
|
+
for (let col = 0; col < size; col += 1) {
|
|
337
|
+
let run = 1
|
|
338
|
+
for (let row = 1; row < size; row += 1) {
|
|
339
|
+
if (at(row, col) === at(row - 1, col)) run += 1
|
|
340
|
+
else {
|
|
341
|
+
if (run >= 5) score += 3 + (run - 5)
|
|
342
|
+
run = 1
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (run >= 5) score += 3 + (run - 5)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
for (let row = 0; row < size - 1; row += 1) {
|
|
349
|
+
for (let col = 0; col < size - 1; col += 1) {
|
|
350
|
+
const first = at(row, col)
|
|
351
|
+
if (first === at(row, col + 1) && first === at(row + 1, col) && first === at(row + 1, col + 1)) {
|
|
352
|
+
score += 3
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const finderA = [1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0]
|
|
358
|
+
const finderB = [0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1]
|
|
359
|
+
const window = new Array(11)
|
|
360
|
+
const matches = (get, start) => {
|
|
361
|
+
for (let index = 0; index < 11; index += 1) window[index] = get(start + index) ? 1 : 0
|
|
362
|
+
const isA = finderA.every((value, index) => window[index] === value)
|
|
363
|
+
if (isA) return true
|
|
364
|
+
return finderB.every((value, index) => window[index] === value)
|
|
365
|
+
}
|
|
366
|
+
for (let row = 0; row < size; row += 1) {
|
|
367
|
+
for (let col = 0; col <= size - 11; col += 1) {
|
|
368
|
+
if (matches((index) => at(row, index), col)) score += 40
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
for (let col = 0; col < size; col += 1) {
|
|
372
|
+
for (let row = 0; row <= size - 11; row += 1) {
|
|
373
|
+
if (matches((index) => at(index, col), row)) score += 40
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
let dark = 0
|
|
378
|
+
for (let index = 0; index < modules.length; index += 1) if (modules[index] === 1) dark += 1
|
|
379
|
+
const percent = (dark * 100) / (size * size)
|
|
380
|
+
score += Math.floor(Math.abs(percent - 50) / 5) * 10
|
|
381
|
+
return score
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Encode text into a QR matrix.
|
|
386
|
+
*
|
|
387
|
+
* @param {string} text
|
|
388
|
+
* @returns {{version: number, size: number, modules: Uint8Array}}
|
|
389
|
+
*/
|
|
390
|
+
export function encodeQr(text) {
|
|
391
|
+
const bytes = utf8Bytes(String(text))
|
|
392
|
+
const version = chooseVersion(bytes.length)
|
|
393
|
+
if (version === null) throw new RangeError('二维码内容过长,无法编码')
|
|
394
|
+
const data = buildCodewords(version, bytes)
|
|
395
|
+
const codewords = interleave(version, data)
|
|
396
|
+
|
|
397
|
+
let best = null
|
|
398
|
+
let bestScore = Number.POSITIVE_INFINITY
|
|
399
|
+
for (let mask = 0; mask < 8; mask += 1) {
|
|
400
|
+
const matrix = buildMatrix(version, codewords, mask)
|
|
401
|
+
const score = penalty(matrix)
|
|
402
|
+
if (score < bestScore) {
|
|
403
|
+
bestScore = score
|
|
404
|
+
best = matrix
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return { version, size: best.size, modules: best.modules }
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Render text as a self-contained SVG data URL.
|
|
412
|
+
*
|
|
413
|
+
* A single path with one `M` per dark run keeps the payload small enough to
|
|
414
|
+
* stay well inside the settings transport's message limits.
|
|
415
|
+
*/
|
|
416
|
+
export function qrSvgDataUrl(text, { scale = 6, margin = 3 } = {}) {
|
|
417
|
+
const { size, modules } = encodeQr(text)
|
|
418
|
+
const total = size + margin * 2
|
|
419
|
+
const parts = []
|
|
420
|
+
for (let row = 0; row < size; row += 1) {
|
|
421
|
+
let col = 0
|
|
422
|
+
while (col < size) {
|
|
423
|
+
if (modules[row * size + col] !== 1) {
|
|
424
|
+
col += 1
|
|
425
|
+
continue
|
|
426
|
+
}
|
|
427
|
+
const start = col
|
|
428
|
+
while (col < size && modules[row * size + col] === 1) col += 1
|
|
429
|
+
parts.push(`M${start + margin} ${row + margin}h${col - start}v1h-${col - start}z`)
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${total} ${total}" width="${total * scale}" height="${total * scale}" shape-rendering="crispEdges"><rect width="${total}" height="${total}" fill="#ffffff"/><path d="${parts.join('')}" fill="#000000"/></svg>`
|
|
433
|
+
return `data:image/svg+xml;base64,${Buffer.from(svg, 'utf8').toString('base64')}`
|
|
434
|
+
}
|