qrdx 0.0.46 → 0.0.48
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/LICENSE +21 -0
- package/README.md +358 -300
- package/dist/index.js +27 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -7
- package/dist/index.mjs.map +1 -1
- package/dist/qr-code-canvas.js +12 -0
- package/dist/qr-code-canvas.js.map +1 -0
- package/dist/qr-code-canvas.mjs +12 -0
- package/dist/qr-code-canvas.mjs.map +1 -0
- package/dist/src/api.d.ts.map +1 -1
- package/dist/src/art-trace.d.ts +46 -0
- package/dist/src/art-trace.d.ts.map +1 -0
- package/dist/src/art.d.ts +138 -0
- package/dist/src/art.d.ts.map +1 -0
- package/dist/src/body-patterns.d.ts +34 -0
- package/dist/src/body-patterns.d.ts.map +1 -0
- package/dist/src/codegen.d.ts +5 -5
- package/dist/src/codegen.d.ts.map +1 -1
- package/dist/src/codeword-map.d.ts +40 -0
- package/dist/src/codeword-map.d.ts.map +1 -0
- package/dist/src/constants.d.ts +4 -7
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/corner-dot.d.ts +3 -1
- package/dist/src/corner-dot.d.ts.map +1 -1
- package/dist/src/corner-eye.d.ts +5 -2
- package/dist/src/corner-eye.d.ts.map +1 -1
- package/dist/src/detection.d.ts.map +1 -1
- package/dist/src/download.d.ts +50 -6
- package/dist/src/download.d.ts.map +1 -1
- package/dist/src/eps.d.ts +62 -0
- package/dist/src/eps.d.ts.map +1 -0
- package/dist/src/frame-icons.d.ts +11 -0
- package/dist/src/frame-icons.d.ts.map +1 -0
- package/dist/src/frame-import.d.ts +61 -0
- package/dist/src/frame-import.d.ts.map +1 -0
- package/dist/src/frame-spec.d.ts +24 -0
- package/dist/src/frame-spec.d.ts.map +1 -0
- package/dist/src/frame-styles.d.ts +50 -0
- package/dist/src/frame-styles.d.ts.map +1 -0
- package/dist/src/frame.d.ts +126 -0
- package/dist/src/frame.d.ts.map +1 -0
- package/dist/src/frames/builtin.d.ts +8 -0
- package/dist/src/frames/builtin.d.ts.map +1 -0
- package/dist/src/frames/official.d.ts +18 -0
- package/dist/src/frames/official.d.ts.map +1 -0
- package/dist/src/helpers.d.ts +15 -2
- package/dist/src/helpers.d.ts.map +1 -1
- package/dist/src/index.d.ts +25 -13
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/logo-budget.d.ts +54 -0
- package/dist/src/logo-budget.d.ts.map +1 -0
- package/dist/src/logo-cleanup.d.ts +25 -0
- package/dist/src/logo-cleanup.d.ts.map +1 -0
- package/dist/src/logo.d.ts +28 -0
- package/dist/src/logo.d.ts.map +1 -0
- package/dist/src/print.d.ts +73 -0
- package/dist/src/print.d.ts.map +1 -0
- package/dist/src/qr-code-canvas.d.ts +13 -0
- package/dist/src/qr-code-canvas.d.ts.map +1 -0
- package/dist/src/qr-code.d.ts +4 -2
- package/dist/src/qr-code.d.ts.map +1 -1
- package/dist/src/qr-outline.d.ts +41 -0
- package/dist/src/qr-outline.d.ts.map +1 -0
- package/dist/src/qr-shape.d.ts +72 -0
- package/dist/src/qr-shape.d.ts.map +1 -0
- package/dist/src/scan-risk.d.ts +40 -0
- package/dist/src/scan-risk.d.ts.map +1 -0
- package/dist/src/scan-score.d.ts +154 -0
- package/dist/src/scan-score.d.ts.map +1 -0
- package/dist/src/shapes.d.ts +36 -0
- package/dist/src/shapes.d.ts.map +1 -0
- package/dist/src/silhouette.d.ts +28 -0
- package/dist/src/silhouette.d.ts.map +1 -0
- package/dist/src/utils/color.d.ts.map +1 -1
- package/dist/src/utils/fmt.d.ts +24 -0
- package/dist/src/utils/fmt.d.ts.map +1 -0
- package/dist/src/utils/paint.d.ts +80 -0
- package/dist/src/utils/paint.d.ts.map +1 -0
- package/dist/src/utils/parse-color.d.ts +21 -0
- package/dist/src/utils/parse-color.d.ts.map +1 -0
- package/dist/src/utils/path-data.d.ts +24 -0
- package/dist/src/utils/path-data.d.ts.map +1 -0
- package/dist/src/utils/png.d.ts +16 -0
- package/dist/src/utils/png.d.ts.map +1 -0
- package/dist/src/utils/polygon-clip.d.ts +26 -0
- package/dist/src/utils/polygon-clip.d.ts.map +1 -0
- package/dist/src/utils/print-shared.d.ts +20 -0
- package/dist/src/utils/print-shared.d.ts.map +1 -0
- package/dist/src/utils/svg-geometry.d.ts +49 -0
- package/dist/src/utils/svg-geometry.d.ts.map +1 -0
- package/dist/src/utils/svg-gradient.d.ts +59 -0
- package/dist/src/utils/svg-gradient.d.ts.map +1 -0
- package/dist/src/utils/svg-painter.d.ts +152 -0
- package/dist/src/utils/svg-painter.d.ts.map +1 -0
- package/dist/src/utils/webp.d.ts +25 -0
- package/dist/src/utils/webp.d.ts.map +1 -0
- package/dist/src/utils.d.ts +18 -2
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/webp.d.ts +19 -0
- package/dist/src/webp.d.ts.map +1 -0
- package/dist/types/art.d.ts +64 -0
- package/dist/types/art.d.ts.map +1 -0
- package/dist/types/color.d.ts +125 -7
- package/dist/types/color.d.ts.map +1 -1
- package/dist/types/corner-dot.d.ts +52 -2
- package/dist/types/corner-dot.d.ts.map +1 -1
- package/dist/types/corner-eye.d.ts +43 -2
- package/dist/types/corner-eye.d.ts.map +1 -1
- package/dist/types/frame-spec.d.ts +3509 -0
- package/dist/types/frame-spec.d.ts.map +1 -0
- package/dist/types/frame.d.ts +68 -0
- package/dist/types/frame.d.ts.map +1 -0
- package/dist/types/image-pattern.d.ts +15 -1
- package/dist/types/image-pattern.d.ts.map +1 -1
- package/dist/types/index.d.ts +15 -9
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +6 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/qr.d.ts +1684 -4
- package/dist/types/qr.d.ts.map +1 -1
- package/dist/types/template.d.ts +2 -2
- package/dist/types/template.d.ts.map +1 -1
- package/package.json +70 -25
- package/src/api.tsx +67 -17
- package/src/art-trace.ts +554 -0
- package/src/art.tsx +2226 -0
- package/src/body-patterns.tsx +389 -0
- package/src/codegen.ts +10 -5
- package/src/codeword-map.ts +265 -0
- package/src/constants.ts +4 -8
- package/src/corner-dot.tsx +209 -33
- package/src/corner-eye.ts +328 -60
- package/src/detection.ts +5 -11
- package/src/download.tsx +174 -70
- package/src/eps.ts +701 -0
- package/src/frame-icons.ts +325 -0
- package/src/frame-import.ts +1198 -0
- package/src/frame-spec.ts +518 -0
- package/src/frame-styles.ts +1095 -0
- package/src/frame.tsx +630 -0
- package/src/frames/builtin/banner-bottom.json +58 -0
- package/src/frames/builtin/circle.json +81 -0
- package/src/frames/builtin/rounded-border.json +60 -0
- package/src/frames/builtin/ticket.json +51 -0
- package/src/frames/builtin.ts +20 -0
- package/src/frames/official/attendance.json +110 -0
- package/src/frames/official/audio-guide.json +95 -0
- package/src/frames/official/book-now.json +103 -0
- package/src/frames/official/check-in.json +110 -0
- package/src/frames/official/christmas.json +131 -0
- package/src/frames/official/donate.json +77 -0
- package/src/frames/official/event-ticket.json +77 -0
- package/src/frames/official/feedback-bubble.json +73 -0
- package/src/frames/official/follow-us.json +83 -0
- package/src/frames/official/get-the-app.json +107 -0
- package/src/frames/official/halloween.json +80 -0
- package/src/frames/official/new-year.json +94 -0
- package/src/frames/official/order-at-table.json +86 -0
- package/src/frames/official/product-info.json +119 -0
- package/src/frames/official/register-product.json +77 -0
- package/src/frames/official/review-us.json +127 -0
- package/src/frames/official/rsvp-card.json +112 -0
- package/src/frames/official/save-contact.json +117 -0
- package/src/frames/official/scan-to-pay.json +94 -0
- package/src/frames/official/shop-window.json +81 -0
- package/src/frames/official/sign-rider.json +81 -0
- package/src/frames/official/table-tent.json +75 -0
- package/src/frames/official/wifi-card.json +89 -0
- package/src/frames/official/winter.json +142 -0
- package/src/frames/official.ts +77 -0
- package/src/helpers.ts +168 -11
- package/src/index.tsx +150 -172
- package/src/logo-budget.ts +226 -0
- package/src/logo-cleanup.ts +146 -0
- package/src/logo.tsx +137 -0
- package/src/print.ts +794 -0
- package/src/qr-code-canvas.tsx +241 -0
- package/src/qr-code.tsx +51 -16
- package/src/qr-outline.ts +511 -0
- package/src/qr-shape.ts +255 -0
- package/src/scan-risk.ts +197 -0
- package/src/scan-score.ts +1629 -0
- package/src/shapes.ts +169 -0
- package/src/silhouette.ts +334 -0
- package/src/utils/color.ts +10 -1
- package/src/utils/fmt.ts +59 -0
- package/src/utils/paint.ts +317 -0
- package/src/utils/parse-color.ts +135 -0
- package/src/utils/path-data.ts +182 -0
- package/src/utils/png.ts +175 -0
- package/src/utils/polygon-clip.ts +163 -0
- package/src/utils/print-shared.ts +74 -0
- package/src/utils/svg-geometry.ts +838 -0
- package/src/utils/svg-gradient.ts +407 -0
- package/src/utils/svg-painter.ts +946 -0
- package/src/utils/webp.ts +556 -0
- package/src/utils.tsx +794 -385
- package/src/webp.ts +84 -0
- package/types/art.ts +74 -0
- package/types/color.ts +132 -12
- package/types/corner-dot.ts +41 -4
- package/types/corner-eye.ts +31 -4
- package/types/frame-spec.ts +748 -0
- package/types/frame.ts +67 -0
- package/types/image-pattern.ts +15 -0
- package/types/index.ts +106 -4
- package/types/qr.ts +180 -2
- package/types/template.ts +2 -2
- package/dist/cli.js +0 -4319
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: error correction budget */
|
|
2
|
+
// How much of a code a center logo may hide, counted the way decoders fail:
|
|
3
|
+
// every codeword with a module under the logo (or its cleared space) is one
|
|
4
|
+
// error in its Reed–Solomon block, and each block repairs only so many
|
|
5
|
+
// (codeword-map.ts). Area alone misjudges small codes: at H version 2 a
|
|
6
|
+
// 9×9 logo puts 16 damaged codewords into a block that repairs 14.
|
|
7
|
+
//
|
|
8
|
+
// The budget is the one the Logo QR badge uses (art.tsx): 60% of each
|
|
9
|
+
// block's repair capacity, which leaves room for print and camera errors.
|
|
10
|
+
import type { ErrorLevel, ImageSettings, QRCodeShape } from "../types";
|
|
11
|
+
import qrcodegen from "./codegen";
|
|
12
|
+
import { damagedCodewords, getCodewordMap } from "./codeword-map";
|
|
13
|
+
import { DEFAULT_IMG_SCALE, DEFAULT_LEVEL, ERROR_LEVEL_MAP } from "./constants";
|
|
14
|
+
import { getLayoutMargin } from "./qr-shape";
|
|
15
|
+
import { getImageSettings, resolveErrorLevel } from "./utils";
|
|
16
|
+
|
|
17
|
+
/** Share of each block's repair capacity a logo may use (as the badge). */
|
|
18
|
+
export const LOGO_EC_SAFETY = 0.6;
|
|
19
|
+
|
|
20
|
+
const LEVELS: readonly ErrorLevel[] = ["L", "M", "Q", "H"];
|
|
21
|
+
|
|
22
|
+
export interface LogoBlockDamage {
|
|
23
|
+
/** Codewords the logo spoils in its worst block */
|
|
24
|
+
worst: number;
|
|
25
|
+
/** Codewords that block can repair (ISO/IEC 18004 Table 9) */
|
|
26
|
+
correctable: number;
|
|
27
|
+
/** Most the logo may spoil per block: correctable × LOGO_EC_SAFETY */
|
|
28
|
+
budget: number;
|
|
29
|
+
/** The level the symbol is really drawn at (qrcodegen may boost it) */
|
|
30
|
+
level: ErrorLevel;
|
|
31
|
+
version: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface LogoCode {
|
|
35
|
+
value: string;
|
|
36
|
+
/** Pixels across the artwork the logo's pixels are laid out in */
|
|
37
|
+
size: number;
|
|
38
|
+
margin: number;
|
|
39
|
+
level: ErrorLevel;
|
|
40
|
+
shape?: QRCodeShape;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface BudgetContext {
|
|
44
|
+
code: LogoCode;
|
|
45
|
+
cells: boolean[][];
|
|
46
|
+
layoutMargin: number;
|
|
47
|
+
map: ReturnType<typeof getCodewordMap>;
|
|
48
|
+
level: ErrorLevel;
|
|
49
|
+
version: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The symbol and its codeword map, encoded once per code and level. */
|
|
53
|
+
function budgetContext(code: LogoCode): BudgetContext {
|
|
54
|
+
const qr = qrcodegen.QrCode.encodeText(
|
|
55
|
+
code.value,
|
|
56
|
+
ERROR_LEVEL_MAP[code.level]
|
|
57
|
+
);
|
|
58
|
+
// qrcodegen raises the level when the data fits anyway: count with that
|
|
59
|
+
const level = LEVELS[qr.errorCorrectionLevel.ordinal] ?? code.level;
|
|
60
|
+
return {
|
|
61
|
+
code,
|
|
62
|
+
cells: qr.getModules(),
|
|
63
|
+
layoutMargin: getLayoutMargin(qr.size, code.margin, code.shape),
|
|
64
|
+
map: getCodewordMap(qr.version, level),
|
|
65
|
+
level,
|
|
66
|
+
version: qr.version,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function damageIn(
|
|
71
|
+
context: BudgetContext,
|
|
72
|
+
imageSettings: ImageSettings
|
|
73
|
+
): LogoBlockDamage | null {
|
|
74
|
+
const { cells, map } = context;
|
|
75
|
+
const n = cells.length;
|
|
76
|
+
const settings = getImageSettings(
|
|
77
|
+
cells,
|
|
78
|
+
context.code.size,
|
|
79
|
+
context.layoutMargin,
|
|
80
|
+
imageSettings
|
|
81
|
+
);
|
|
82
|
+
if (!settings) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
const box = settings.excavation ?? {
|
|
86
|
+
x: Math.floor(settings.x),
|
|
87
|
+
y: Math.floor(settings.y),
|
|
88
|
+
w: Math.ceil(settings.x + settings.w) - Math.floor(settings.x),
|
|
89
|
+
h: Math.ceil(settings.y + settings.h) - Math.floor(settings.y),
|
|
90
|
+
};
|
|
91
|
+
const modules: number[] = [];
|
|
92
|
+
for (let y = Math.max(0, box.y); y < Math.min(n, box.y + box.h); y++) {
|
|
93
|
+
for (let x = Math.max(0, box.x); x < Math.min(n, box.x + box.w); x++) {
|
|
94
|
+
modules.push(y * n + x);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const counts = damagedCodewords(map, modules);
|
|
98
|
+
return {
|
|
99
|
+
worst: Math.max(0, ...counts),
|
|
100
|
+
correctable: map.correctable,
|
|
101
|
+
budget: Math.floor(map.correctable * LOGO_EC_SAFETY),
|
|
102
|
+
level: context.level,
|
|
103
|
+
version: context.version,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Codewords `imageSettings` spoils per Reed–Solomon block of `code`: every
|
|
109
|
+
* codeword with a module inside the cleared area (or under the logo when
|
|
110
|
+
* nothing is cleared). Null when the logo falls outside the code.
|
|
111
|
+
*/
|
|
112
|
+
export function getLogoBlockDamage(
|
|
113
|
+
code: LogoCode,
|
|
114
|
+
imageSettings: ImageSettings
|
|
115
|
+
): LogoBlockDamage | null {
|
|
116
|
+
return damageIn(budgetContext(code), imageSettings);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const within = (damage: LogoBlockDamage | null) =>
|
|
120
|
+
!damage || damage.worst <= damage.budget;
|
|
121
|
+
|
|
122
|
+
/** True when the logo stays inside every block's budget at `code.level`. */
|
|
123
|
+
export function logoFitsBudget(
|
|
124
|
+
code: LogoCode,
|
|
125
|
+
imageSettings: ImageSettings
|
|
126
|
+
): boolean {
|
|
127
|
+
return within(getLogoBlockDamage(code, imageSettings));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Largest logo width (pixels, up to `requested`) for which `layout(width)`
|
|
132
|
+
* fits the budget at every one of `levels` (with autoLevel the code may be
|
|
133
|
+
* drawn at its own level or at H). 0 when no width fits.
|
|
134
|
+
*/
|
|
135
|
+
export function fitLogoWidth(
|
|
136
|
+
code: Omit<LogoCode, "level">,
|
|
137
|
+
levels: readonly ErrorLevel[],
|
|
138
|
+
requested: number,
|
|
139
|
+
layout: (width: number) => ImageSettings
|
|
140
|
+
): number {
|
|
141
|
+
const contexts = levels.map((level) => budgetContext({ ...code, level }));
|
|
142
|
+
const fits = (width: number) => {
|
|
143
|
+
const settings = layout(width);
|
|
144
|
+
return contexts.every((context) => within(damageIn(context, settings)));
|
|
145
|
+
};
|
|
146
|
+
if (fits(requested)) {
|
|
147
|
+
return requested;
|
|
148
|
+
}
|
|
149
|
+
// Half-percent steps of the code's width: fine enough to see on a slider
|
|
150
|
+
const step = code.size * 0.005;
|
|
151
|
+
for (let width = requested - step; width > 0; width -= step) {
|
|
152
|
+
if (fits(width)) {
|
|
153
|
+
return width;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface RenderedLogoCode extends Omit<LogoCode, "level"> {
|
|
160
|
+
level?: ErrorLevel;
|
|
161
|
+
/** The renderer's autoLevel (a logo over 7% of the code raises it to H) */
|
|
162
|
+
autoLevel?: boolean;
|
|
163
|
+
/** QRCodeSVG's isOGContext (an excavated logo raises L/M to Q) */
|
|
164
|
+
isOGContext?: boolean;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* `imageSettings` passed straight to a renderer (QRCodeSVG, QRCodeCanvas,
|
|
169
|
+
* getQRAsSVG), held to the same budget getQRData gives `logo` +
|
|
170
|
+
* `logoOptions`: unchanged when it fits, else shrunk around its own center
|
|
171
|
+
* (aspect ratio and clear space kept) to the largest size that does, or
|
|
172
|
+
* undefined when not even a sliver fits. A logo already capped by getQRData
|
|
173
|
+
* comes back as it is.
|
|
174
|
+
*/
|
|
175
|
+
export function capImageSettings(
|
|
176
|
+
code: RenderedLogoCode,
|
|
177
|
+
imageSettings: ImageSettings | undefined
|
|
178
|
+
): ImageSettings | undefined {
|
|
179
|
+
if (!imageSettings) {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
const defaultSize = Math.floor(code.size * DEFAULT_IMG_SCALE);
|
|
183
|
+
const width = imageSettings.width || defaultSize;
|
|
184
|
+
const height = imageSettings.height || defaultSize;
|
|
185
|
+
const layout = (next: number): ImageSettings => {
|
|
186
|
+
if (next === width) {
|
|
187
|
+
return imageSettings;
|
|
188
|
+
}
|
|
189
|
+
const nextHeight = (height * next) / width;
|
|
190
|
+
return {
|
|
191
|
+
...imageSettings,
|
|
192
|
+
width: next,
|
|
193
|
+
height: nextHeight,
|
|
194
|
+
...(imageSettings.x != null && {
|
|
195
|
+
x: imageSettings.x + (width - next) / 2,
|
|
196
|
+
}),
|
|
197
|
+
...(imageSettings.y != null && {
|
|
198
|
+
y: imageSettings.y + (height - nextHeight) / 2,
|
|
199
|
+
}),
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
const requested = code.level ?? DEFAULT_LEVEL;
|
|
203
|
+
const base = {
|
|
204
|
+
value: code.value,
|
|
205
|
+
size: code.size,
|
|
206
|
+
margin: code.margin,
|
|
207
|
+
shape: code.shape,
|
|
208
|
+
};
|
|
209
|
+
const drawnAt = (settings: ImageSettings) =>
|
|
210
|
+
resolveErrorLevel({ ...code, level: requested, imageSettings: settings });
|
|
211
|
+
const fit = (level: ErrorLevel) => {
|
|
212
|
+
const fitted = fitLogoWidth(base, [level], width, layout);
|
|
213
|
+
return fitted > 0 ? layout(fitted) : undefined;
|
|
214
|
+
};
|
|
215
|
+
// As getQRData's cap: a raised level (autoLevel, isOGContext) holds only
|
|
216
|
+
// while the logo still raises it, else size it for the requested level.
|
|
217
|
+
const drawn = drawnAt(imageSettings);
|
|
218
|
+
if (drawn === requested) {
|
|
219
|
+
return fit(requested);
|
|
220
|
+
}
|
|
221
|
+
const atDrawn = fit(drawn);
|
|
222
|
+
if (atDrawn && drawnAt(atDrawn) === drawn) {
|
|
223
|
+
return atDrawn;
|
|
224
|
+
}
|
|
225
|
+
return fit(requested);
|
|
226
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: color distances */
|
|
2
|
+
// Removes a logo's solid background (a white or colored box around the
|
|
3
|
+
// mark, as JPEGs and many PNGs have). Detection is Logo QR's: the four
|
|
4
|
+
// corners share one opaque color (art-trace's backdropColor). Unlike Logo
|
|
5
|
+
// QR's global key, only pixels connected to the border fade out, so the
|
|
6
|
+
// same color inside the mark (white letters on a white-boxed logo) stays.
|
|
7
|
+
import { backdropColor, type RGB } from "./art-trace";
|
|
8
|
+
|
|
9
|
+
export interface LogoPixels {
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
/** RGBA, 4 bytes per pixel */
|
|
13
|
+
data: Uint8ClampedArray | Uint8Array;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** sRGB distance (0–1 channels) below which a pixel is all background */
|
|
17
|
+
const CLEAR = 0.06;
|
|
18
|
+
/** Above this a pixel is all logo; in between it fades (anti-aliased edges) */
|
|
19
|
+
const SOLID = 0.16;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Longest side the browser helper works at (larger logos are scaled down):
|
|
23
|
+
* plenty for a logo at 30% of a print, and the PNG stays small enough to
|
|
24
|
+
* save with a design.
|
|
25
|
+
*/
|
|
26
|
+
export const LOGO_CLEANUP_MAX_SIDE = 512;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* `pixels` with the background made transparent, or null when the image has
|
|
30
|
+
* no solid background (transparent or mixed corners). Edge pixels keep
|
|
31
|
+
* their own color: the background's share is taken out as they fade.
|
|
32
|
+
*/
|
|
33
|
+
export function removeLogoBackdrop(pixels: LogoPixels): LogoPixels | null {
|
|
34
|
+
const { width, height } = pixels;
|
|
35
|
+
if (!(width > 0 && height > 0) || pixels.data.length < width * height * 4) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
const source = Uint8Array.from(pixels.data);
|
|
39
|
+
const backdrop = backdropColor({ width, height, data: source });
|
|
40
|
+
if (!backdrop) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const distance = (p: number) => {
|
|
44
|
+
const i = p * 4;
|
|
45
|
+
return Math.hypot(
|
|
46
|
+
(source[i] as number) / 255 - backdrop[0],
|
|
47
|
+
(source[i + 1] as number) / 255 - backdrop[1],
|
|
48
|
+
(source[i + 2] as number) / 255 - backdrop[2]
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Flood fill from the border through background-like pixels
|
|
53
|
+
const reached = new Uint8Array(width * height);
|
|
54
|
+
const queue = new Int32Array(width * height);
|
|
55
|
+
let head = 0;
|
|
56
|
+
let tail = 0;
|
|
57
|
+
const visit = (p: number) => {
|
|
58
|
+
if (reached[p] || (source[p * 4 + 3] as number) === 0) return;
|
|
59
|
+
if (distance(p) >= SOLID) return;
|
|
60
|
+
reached[p] = 1;
|
|
61
|
+
queue[tail++] = p;
|
|
62
|
+
};
|
|
63
|
+
for (let x = 0; x < width; x++) {
|
|
64
|
+
visit(x);
|
|
65
|
+
visit((height - 1) * width + x);
|
|
66
|
+
}
|
|
67
|
+
for (let y = 0; y < height; y++) {
|
|
68
|
+
visit(y * width);
|
|
69
|
+
visit(y * width + width - 1);
|
|
70
|
+
}
|
|
71
|
+
while (head < tail) {
|
|
72
|
+
const p = queue[head++] as number;
|
|
73
|
+
const x = p % width;
|
|
74
|
+
if (x > 0) visit(p - 1);
|
|
75
|
+
if (x < width - 1) visit(p + 1);
|
|
76
|
+
if (p >= width) visit(p - width);
|
|
77
|
+
if (p < (height - 1) * width) visit(p + width);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const out = new Uint8ClampedArray(source);
|
|
81
|
+
const background: RGB = backdrop;
|
|
82
|
+
for (let p = 0; p < width * height; p++) {
|
|
83
|
+
if (!reached[p]) continue;
|
|
84
|
+
const keep = Math.min(
|
|
85
|
+
1,
|
|
86
|
+
Math.max(0, (distance(p) - CLEAR) / (SOLID - CLEAR))
|
|
87
|
+
);
|
|
88
|
+
const i = p * 4;
|
|
89
|
+
out[i + 3] = Math.round((source[i + 3] as number) * keep);
|
|
90
|
+
if (keep > 0 && keep < 1) {
|
|
91
|
+
// Take the background's share out of the edge color
|
|
92
|
+
for (let c = 0; c < 3; c++) {
|
|
93
|
+
const value = (source[i + c] as number) / 255;
|
|
94
|
+
const own = (value - (1 - keep) * (background[c] as number)) / keep;
|
|
95
|
+
out[i + c] = Math.round(Math.min(1, Math.max(0, own)) * 255);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return { width, height, data: out };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Browser: the logo at `src` (a data URL or a same-origin or CORS-enabled
|
|
104
|
+
* URL) with its solid background removed, as a PNG data URL, or null when
|
|
105
|
+
* it has none or can't be read. Logos larger than 512 px are scaled down.
|
|
106
|
+
*/
|
|
107
|
+
export async function removeLogoBackground(
|
|
108
|
+
src: string
|
|
109
|
+
): Promise<string | null> {
|
|
110
|
+
if (typeof document === "undefined" || typeof Image === "undefined") {
|
|
111
|
+
throw new Error("removeLogoBackground needs a browser (canvas)");
|
|
112
|
+
}
|
|
113
|
+
const image = await new Promise<HTMLImageElement | null>((resolve) => {
|
|
114
|
+
const img = new Image();
|
|
115
|
+
img.crossOrigin = "anonymous";
|
|
116
|
+
img.onload = () => resolve(img);
|
|
117
|
+
img.onerror = () => resolve(null);
|
|
118
|
+
img.src = src;
|
|
119
|
+
});
|
|
120
|
+
if (!image) return null;
|
|
121
|
+
const naturalWidth = image.naturalWidth || 512;
|
|
122
|
+
const naturalHeight = image.naturalHeight || 512;
|
|
123
|
+
const scale = Math.min(
|
|
124
|
+
1,
|
|
125
|
+
LOGO_CLEANUP_MAX_SIDE / Math.max(naturalWidth, naturalHeight)
|
|
126
|
+
);
|
|
127
|
+
const canvas = document.createElement("canvas");
|
|
128
|
+
canvas.width = Math.max(1, Math.round(naturalWidth * scale));
|
|
129
|
+
canvas.height = Math.max(1, Math.round(naturalHeight * scale));
|
|
130
|
+
const ctx = canvas.getContext("2d", { willReadFrequently: true });
|
|
131
|
+
if (!ctx) return null;
|
|
132
|
+
try {
|
|
133
|
+
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
|
|
134
|
+
const frame = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
135
|
+
const cleaned = removeLogoBackdrop(frame);
|
|
136
|
+
if (!cleaned) return null;
|
|
137
|
+
frame.data.set(cleaned.data);
|
|
138
|
+
ctx.putImageData(frame, 0, 0);
|
|
139
|
+
return canvas.toDataURL("image/png");
|
|
140
|
+
} catch {
|
|
141
|
+
// A tainted canvas (a cross-origin logo without CORS)
|
|
142
|
+
return null;
|
|
143
|
+
} finally {
|
|
144
|
+
canvas.remove();
|
|
145
|
+
}
|
|
146
|
+
}
|
package/src/logo.tsx
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: logo geometry */
|
|
2
|
+
// The center logo as SVG: an optional plate behind it and a crop to its
|
|
3
|
+
// shape (imageSettings.shape and .plate). Everything is plain SVG (a filled
|
|
4
|
+
// shape, a <clipPath> and an <image>), which the PDF and EPS exporters
|
|
5
|
+
// draw too: their painters clip to the same outline.
|
|
6
|
+
import type { JSX } from "react";
|
|
7
|
+
import type { ImageSettings, LogoShape } from "../types";
|
|
8
|
+
import { hashString } from "./shapes";
|
|
9
|
+
import { fmt } from "./utils/fmt";
|
|
10
|
+
|
|
11
|
+
/** Rounded corners' radius, as a share of the logo's shorter side. */
|
|
12
|
+
export const LOGO_ROUNDED_RADIUS = 0.22;
|
|
13
|
+
/** With a plate, the logo is inset this share of the box on each side. */
|
|
14
|
+
export const LOGO_PLATE_INSET = 0.14;
|
|
15
|
+
|
|
16
|
+
interface Box {
|
|
17
|
+
x: number;
|
|
18
|
+
y: number;
|
|
19
|
+
w: number;
|
|
20
|
+
h: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The outline of `shape` filling `box`, as an SVG element. */
|
|
24
|
+
function shapeElement(
|
|
25
|
+
shape: LogoShape,
|
|
26
|
+
box: Box,
|
|
27
|
+
props: Record<string, string> = {}
|
|
28
|
+
): JSX.Element {
|
|
29
|
+
if (shape === "circle") {
|
|
30
|
+
return (
|
|
31
|
+
<circle
|
|
32
|
+
cx={fmt(box.x + box.w / 2)}
|
|
33
|
+
cy={fmt(box.y + box.h / 2)}
|
|
34
|
+
r={fmt(Math.min(box.w, box.h) / 2)}
|
|
35
|
+
{...props}
|
|
36
|
+
/>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const radius =
|
|
40
|
+
shape === "rounded" ? Math.min(box.w, box.h) * LOGO_ROUNDED_RADIUS : 0;
|
|
41
|
+
return (
|
|
42
|
+
<rect
|
|
43
|
+
height={fmt(box.h)}
|
|
44
|
+
width={fmt(box.w)}
|
|
45
|
+
x={fmt(box.x)}
|
|
46
|
+
y={fmt(box.y)}
|
|
47
|
+
{...(radius > 0 && { rx: fmt(radius), ry: fmt(radius) })}
|
|
48
|
+
{...props}
|
|
49
|
+
/>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** `box` shrunk by `share` of its size on every side. */
|
|
54
|
+
function inset(box: Box, share: number): Box {
|
|
55
|
+
const dx = box.w * share;
|
|
56
|
+
const dy = box.h * share;
|
|
57
|
+
return { x: box.x + dx, y: box.y + dy, w: box.w - 2 * dx, h: box.h - 2 * dy };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The logo in `box` (the user units of the surrounding SVG): the image,
|
|
62
|
+
* cropped to `settings.shape`, on a `settings.plate` plate when one is set.
|
|
63
|
+
* A plain square logo is a bare <image>, exactly as before.
|
|
64
|
+
*/
|
|
65
|
+
export function renderLogo(
|
|
66
|
+
settings: Pick<ImageSettings, "src" | "shape" | "plate">,
|
|
67
|
+
box: Box
|
|
68
|
+
): JSX.Element {
|
|
69
|
+
const shape = settings.shape ?? "square";
|
|
70
|
+
const plate = settings.plate;
|
|
71
|
+
const imageBox = plate ? inset(box, LOGO_PLATE_INSET) : box;
|
|
72
|
+
const image = (clipId?: string) => (
|
|
73
|
+
<image
|
|
74
|
+
height={fmt(imageBox.h)}
|
|
75
|
+
href={settings.src}
|
|
76
|
+
preserveAspectRatio="none"
|
|
77
|
+
width={fmt(imageBox.w)}
|
|
78
|
+
x={fmt(imageBox.x)}
|
|
79
|
+
y={fmt(imageBox.y)}
|
|
80
|
+
{...(clipId && { clipPath: `url(#${clipId})` })}
|
|
81
|
+
/>
|
|
82
|
+
);
|
|
83
|
+
if (shape === "square" && !plate) {
|
|
84
|
+
return image();
|
|
85
|
+
}
|
|
86
|
+
// Ids are global in a page: derive them from the geometry, so two codes
|
|
87
|
+
// with the same logo box share an identical clip
|
|
88
|
+
const clipId =
|
|
89
|
+
shape === "square"
|
|
90
|
+
? undefined
|
|
91
|
+
: `qrdx-logo-${hashString(
|
|
92
|
+
`${shape}|${fmt(imageBox.x)}|${fmt(imageBox.y)}|${fmt(imageBox.w)}|${fmt(imageBox.h)}`
|
|
93
|
+
)}`;
|
|
94
|
+
return (
|
|
95
|
+
<g>
|
|
96
|
+
{plate && shapeElement(shape, box, { fill: plate })}
|
|
97
|
+
{clipId && (
|
|
98
|
+
<clipPath id={clipId}>{shapeElement(shape, imageBox)}</clipPath>
|
|
99
|
+
)}
|
|
100
|
+
{image(clipId)}
|
|
101
|
+
</g>
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Traces `shape` filling `box` on a canvas path (for `QRCodeCanvas`), so the
|
|
107
|
+
* caller can fill it (the plate) or clip to it (the logo).
|
|
108
|
+
*/
|
|
109
|
+
export function traceLogoShape(
|
|
110
|
+
ctx: CanvasRenderingContext2D,
|
|
111
|
+
shape: LogoShape,
|
|
112
|
+
box: Box
|
|
113
|
+
) {
|
|
114
|
+
ctx.beginPath();
|
|
115
|
+
if (shape === "circle") {
|
|
116
|
+
ctx.arc(
|
|
117
|
+
box.x + box.w / 2,
|
|
118
|
+
box.y + box.h / 2,
|
|
119
|
+
Math.min(box.w, box.h) / 2,
|
|
120
|
+
0,
|
|
121
|
+
Math.PI * 2
|
|
122
|
+
);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const radius =
|
|
126
|
+
shape === "rounded" ? Math.min(box.w, box.h) * LOGO_ROUNDED_RADIUS : 0;
|
|
127
|
+
if (radius > 0 && typeof ctx.roundRect === "function") {
|
|
128
|
+
ctx.roundRect(box.x, box.y, box.w, box.h, radius);
|
|
129
|
+
} else {
|
|
130
|
+
ctx.rect(box.x, box.y, box.w, box.h);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The box the logo image fills inside `box` (inset when on a plate). */
|
|
135
|
+
export function logoImageBox(box: Box, plate: string | undefined): Box {
|
|
136
|
+
return plate ? inset(box, LOGO_PLATE_INSET) : box;
|
|
137
|
+
}
|