Skip to content

Commit bc3190a

Browse files
authored
fix(qrcode)!: honor border in console/array output (#101)
1 parent 71be1ee commit bc3190a

File tree

2 files changed

+110
-94
lines changed

2 files changed

+110
-94
lines changed

qrcode/_qrcode.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class QrCode {
168168
* The smallest possible QR Code version is automatically chosen for the output.
169169
* The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
170170
*/
171-
static from(content: string | URL | Uint8Array, { output = "array", border = 4, light = "white", dark = "black", ecl = "MEDIUM" }: { output?: string } & options = {}) {
171+
static from(content: string | URL | Uint8Array, { output = "array", border = 2, light = "white", dark = "black", ecl = "MEDIUM" }: { output?: string } & options = {}) {
172172
border = Math.max(0, border)
173173
const qr = QrCode.#encode(Segment.from(content instanceof URL ? content.href : content), { ecl })
174174
const size = qr.size + border * 2
@@ -178,29 +178,29 @@ class QrCode {
178178
for (let y = 0; y < qr.size; y++) {
179179
for (let x = 0; x < qr.size; x++) {
180180
if (qr.get({ x, y })) {
181-
paths.push(`M${x + border},${y + border}h1v1h-1z`)
181+
paths.push(`M${x + border * 2},${y + border * 2}h1v1h-1z`)
182182
}
183183
}
184184
}
185185
return `<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 ${size} ${size}" stroke="none"><rect width="100%" height="100%" fill="${light}"/><path d="${paths.join(" ")}" fill="${dark}"/></svg>`
186186
}
187187
case "console": {
188-
for (let y = 0; y < qr.size; y++) {
189-
const line = "%c\u2588\u2588".repeat(qr.size)
188+
for (let y = 0; y < size; y++) {
189+
const line = "%c\u2588\u2588".repeat(size)
190190
const colors = [] as string[]
191-
for (let x = 0; x < qr.size; x++) {
192-
colors.push(`color: ${qr.get({ x, y }) ? dark : light}`)
191+
for (let x = 0; x < size; x++) {
192+
colors.push(`color: ${qr.get({ x: x - border, y: y - border }) ? dark : light}`)
193193
}
194194
console.log(line, ...colors)
195195
}
196196
return
197197
}
198198
default: {
199199
const data = [] as boolean[][]
200-
for (let y = 0; y < qr.size; y++) {
201-
data[y] = new Array(qr.size).fill(false)
202-
for (let x = 0; x < qr.size; x++) {
203-
data[y][x] = qr.get({ x, y })
200+
for (let y = 0; y < size; y++) {
201+
data[y] = new Array(size).fill(false)
202+
for (let x = 0; x < size; x++) {
203+
data[y][x] = qr.get({ x: x - border, y: y - border })
204204
}
205205
}
206206
return data

0 commit comments

Comments
 (0)