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
package/src/frame.tsx
ADDED
|
@@ -0,0 +1,630 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: frame proportions */
|
|
2
|
+
import type { ReactElement, SVGProps } from "react";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import type { ColorConfig } from "../types";
|
|
5
|
+
import {
|
|
6
|
+
DEFAULT_FRAME_TEXT,
|
|
7
|
+
FRAME_FONT_SCALE_MAX,
|
|
8
|
+
FRAME_FONT_SCALE_MIN,
|
|
9
|
+
FRAME_TEXT_MAX_LENGTH,
|
|
10
|
+
} from "../types/frame";
|
|
11
|
+
import { type AnyQRFrame, CUSTOM_FRAME_STYLE } from "../types/frame-spec";
|
|
12
|
+
import { DEFAULT_BGCOLOR } from "./constants";
|
|
13
|
+
import { getCustomFrameLayout, getFrameSpecGapRatio } from "./frame-spec";
|
|
14
|
+
import {
|
|
15
|
+
estimateTextEm,
|
|
16
|
+
isShapedFrameStyle,
|
|
17
|
+
SHAPED_FRAMES,
|
|
18
|
+
} from "./frame-styles";
|
|
19
|
+
import { getSolidColor } from "./utils/color";
|
|
20
|
+
import { compact, roundTo } from "./utils/fmt";
|
|
21
|
+
|
|
22
|
+
/** System font stack for frame labels, so rendering never fetches a font. */
|
|
23
|
+
export const FRAME_FONT_FAMILY =
|
|
24
|
+
"system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif";
|
|
25
|
+
|
|
26
|
+
/** Light gap between the code and the frame, as a share of the code width */
|
|
27
|
+
export const FRAME_GAP_RATIO = 0.08;
|
|
28
|
+
|
|
29
|
+
const DEFAULT_FRAME_COLOR = "#000000";
|
|
30
|
+
const DEFAULT_BANNER_TEXT_COLOR = "#FFFFFF";
|
|
31
|
+
|
|
32
|
+
export interface FrameBox {
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
radius: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface FrameRect extends FrameBox {
|
|
41
|
+
type: "rect";
|
|
42
|
+
fill: string;
|
|
43
|
+
/** Area cut out of the rect (where the QR code shows through) */
|
|
44
|
+
hole?: FrameBox;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface FramePolygon {
|
|
48
|
+
type: "polygon";
|
|
49
|
+
points: [number, number][];
|
|
50
|
+
fill: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Any outline, as SVG path data in artwork coordinates. */
|
|
54
|
+
export interface FramePath {
|
|
55
|
+
type: "path";
|
|
56
|
+
d: string;
|
|
57
|
+
fill: string;
|
|
58
|
+
/** "evenodd" turns inner subpaths into holes */
|
|
59
|
+
fillRule?: "nonzero" | "evenodd";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type FrameShape = FrameRect | FramePolygon | FramePath;
|
|
63
|
+
|
|
64
|
+
export interface FrameLabel {
|
|
65
|
+
text: string;
|
|
66
|
+
/** Horizontal center of the label (its left or right end with `align`) */
|
|
67
|
+
x: number;
|
|
68
|
+
/** Alphabetic baseline of the label */
|
|
69
|
+
y: number;
|
|
70
|
+
fontSize: number;
|
|
71
|
+
/** Width the label must fit in */
|
|
72
|
+
maxWidth: number;
|
|
73
|
+
color: string;
|
|
74
|
+
/** Clockwise rotation in degrees around (x, y), for text set on a curve */
|
|
75
|
+
rotate?: number;
|
|
76
|
+
/** Which end of the text `x` is (default "middle") */
|
|
77
|
+
align?: "start" | "middle" | "end";
|
|
78
|
+
/** CSS font weight (default 700); PDF and EPS draw 600+ as bold */
|
|
79
|
+
fontWeight?: number;
|
|
80
|
+
/**
|
|
81
|
+
* The frame text this label draws: "text", "secondaryText", or a custom
|
|
82
|
+
* spec's label id. Every letter of curved text carries its label's field.
|
|
83
|
+
* Editors find a label by it (see `frameLabelMarkers`).
|
|
84
|
+
*/
|
|
85
|
+
field?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Attribute that marks frame labels in SVG drawn with `frameLabelMarkers`. */
|
|
89
|
+
export const FRAME_LABEL_ATTRIBUTE = "data-qrdx-frame-label";
|
|
90
|
+
|
|
91
|
+
export interface FrameLayout {
|
|
92
|
+
/** Full artwork width (frame included) */
|
|
93
|
+
width: number;
|
|
94
|
+
/** Full artwork height (frame included) */
|
|
95
|
+
height: number;
|
|
96
|
+
/** Where the QR code (with its own margin) sits inside the artwork */
|
|
97
|
+
qrX: number;
|
|
98
|
+
qrY: number;
|
|
99
|
+
qrSize: number;
|
|
100
|
+
/** Shapes painted behind the QR code, in order */
|
|
101
|
+
shapes: FrameShape[];
|
|
102
|
+
label: FrameLabel | null;
|
|
103
|
+
/** Further labels (a second line, or the letters of curved text) */
|
|
104
|
+
extraLabels?: FrameLabel[];
|
|
105
|
+
/** True when the frame fills its whole bounding box (no rounded corners) */
|
|
106
|
+
squareEdges: boolean;
|
|
107
|
+
/** Frame color, for print bleed */
|
|
108
|
+
color: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* True when `frame` draws something. A "custom" frame counts even when its
|
|
113
|
+
* spec is invalid: drawing it then throws a FrameSpecError instead of
|
|
114
|
+
* leaving the frame out silently.
|
|
115
|
+
*/
|
|
116
|
+
export function isFrameActive(
|
|
117
|
+
frame: AnyQRFrame | null | undefined
|
|
118
|
+
): frame is AnyQRFrame {
|
|
119
|
+
return Boolean(frame?.style && frame.style !== "none");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Light gap the frame keeps around the code, as a share of the code width
|
|
124
|
+
* (FRAME_GAP_RATIO for the built-in frames, the plate padding for custom).
|
|
125
|
+
*/
|
|
126
|
+
export function getFrameGapRatio(frame: AnyQRFrame): number {
|
|
127
|
+
return frame.style === CUSTOM_FRAME_STYLE
|
|
128
|
+
? getFrameSpecGapRatio(frame.spec)
|
|
129
|
+
: FRAME_GAP_RATIO;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The label a frame renders: trimmed and capped at 24 characters. */
|
|
133
|
+
export function getFrameText(frame: AnyQRFrame): string {
|
|
134
|
+
return (frame.text ?? DEFAULT_FRAME_TEXT)
|
|
135
|
+
.trim()
|
|
136
|
+
.slice(0, FRAME_TEXT_MAX_LENGTH);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The second label (no default), trimmed and capped at 24 characters. */
|
|
140
|
+
export function getFrameSecondaryText(frame: AnyQRFrame): string {
|
|
141
|
+
return (frame.secondaryText ?? "").trim().slice(0, FRAME_TEXT_MAX_LENGTH);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function clampFontScale(scale: number | undefined): number {
|
|
145
|
+
if (scale === undefined || !Number.isFinite(scale)) {
|
|
146
|
+
return 1;
|
|
147
|
+
}
|
|
148
|
+
return Math.min(FRAME_FONT_SCALE_MAX, Math.max(FRAME_FONT_SCALE_MIN, scale));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function makeLabel(
|
|
152
|
+
text: string,
|
|
153
|
+
centerX: number,
|
|
154
|
+
centerY: number,
|
|
155
|
+
rowHeight: number,
|
|
156
|
+
maxWidth: number,
|
|
157
|
+
color: string,
|
|
158
|
+
fontScale: number,
|
|
159
|
+
field: string
|
|
160
|
+
): FrameLabel | null {
|
|
161
|
+
if (!text) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
const fontSize = Math.min(
|
|
165
|
+
rowHeight * 0.52 * fontScale,
|
|
166
|
+
maxWidth / estimateTextEm(text)
|
|
167
|
+
);
|
|
168
|
+
return {
|
|
169
|
+
text,
|
|
170
|
+
x: centerX,
|
|
171
|
+
// Cap height of a sans-serif is ~0.7em, so this centers capitals.
|
|
172
|
+
y: centerY + fontSize * 0.35,
|
|
173
|
+
fontSize,
|
|
174
|
+
maxWidth,
|
|
175
|
+
color,
|
|
176
|
+
field,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Computes where the frame shapes, label and QR code go for a code of `size`
|
|
182
|
+
* pixels. The QR code keeps its full size; the frame is added around it.
|
|
183
|
+
* A "custom" frame is drawn from its spec (throws a FrameSpecError when the
|
|
184
|
+
* spec is invalid).
|
|
185
|
+
*/
|
|
186
|
+
export function getFrameLayout(
|
|
187
|
+
size: number,
|
|
188
|
+
frame: AnyQRFrame,
|
|
189
|
+
bgColor?: ColorConfig
|
|
190
|
+
): FrameLayout {
|
|
191
|
+
if (frame.style === CUSTOM_FRAME_STYLE) {
|
|
192
|
+
return getCustomFrameLayout(size, frame, bgColor);
|
|
193
|
+
}
|
|
194
|
+
const color = frame.color || DEFAULT_FRAME_COLOR;
|
|
195
|
+
const background = getSolidColor(bgColor, DEFAULT_BGCOLOR);
|
|
196
|
+
const text = getFrameText(frame);
|
|
197
|
+
|
|
198
|
+
const gap = size * FRAME_GAP_RATIO;
|
|
199
|
+
const border = size * 0.04;
|
|
200
|
+
const band = size * 0.22;
|
|
201
|
+
const width = size + 2 * (border + gap);
|
|
202
|
+
const innerWidth = width - 2 * border;
|
|
203
|
+
const labelWidth = innerWidth - 2 * gap;
|
|
204
|
+
const qrX = border + gap;
|
|
205
|
+
const fontScale = clampFontScale(frame.fontScale);
|
|
206
|
+
const fitLabel = (
|
|
207
|
+
label: string,
|
|
208
|
+
centerX: number,
|
|
209
|
+
centerY: number,
|
|
210
|
+
rowHeight: number,
|
|
211
|
+
maxWidth: number,
|
|
212
|
+
labelColor: string,
|
|
213
|
+
field = "text"
|
|
214
|
+
) =>
|
|
215
|
+
makeLabel(
|
|
216
|
+
label,
|
|
217
|
+
centerX,
|
|
218
|
+
centerY,
|
|
219
|
+
rowHeight,
|
|
220
|
+
maxWidth,
|
|
221
|
+
labelColor,
|
|
222
|
+
fontScale,
|
|
223
|
+
field
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
if (isShapedFrameStyle(frame.style)) {
|
|
227
|
+
return SHAPED_FRAMES[frame.style]({
|
|
228
|
+
size,
|
|
229
|
+
frame,
|
|
230
|
+
color,
|
|
231
|
+
background,
|
|
232
|
+
text,
|
|
233
|
+
secondaryText: getFrameSecondaryText(frame),
|
|
234
|
+
gap,
|
|
235
|
+
border,
|
|
236
|
+
band,
|
|
237
|
+
fontScale,
|
|
238
|
+
fitLabel,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
switch (frame.style) {
|
|
243
|
+
case "banner-top": {
|
|
244
|
+
const innerHeight = size + 2 * gap;
|
|
245
|
+
return {
|
|
246
|
+
width,
|
|
247
|
+
height: band + innerHeight + border,
|
|
248
|
+
qrX,
|
|
249
|
+
qrY: band + gap,
|
|
250
|
+
qrSize: size,
|
|
251
|
+
shapes: [
|
|
252
|
+
rect(border, band, innerWidth, innerHeight, 0, background),
|
|
253
|
+
ring(
|
|
254
|
+
box(0, 0, width, band + innerHeight + border, 0),
|
|
255
|
+
box(border, band, innerWidth, innerHeight, 0),
|
|
256
|
+
color
|
|
257
|
+
),
|
|
258
|
+
],
|
|
259
|
+
label: fitLabel(
|
|
260
|
+
text,
|
|
261
|
+
width / 2,
|
|
262
|
+
band / 2,
|
|
263
|
+
band,
|
|
264
|
+
labelWidth,
|
|
265
|
+
frame.textColor || DEFAULT_BANNER_TEXT_COLOR
|
|
266
|
+
),
|
|
267
|
+
squareEdges: true,
|
|
268
|
+
color,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
case "border":
|
|
273
|
+
case "rounded-border": {
|
|
274
|
+
const rowHeight = text ? size * 0.18 : 0;
|
|
275
|
+
const innerHeight = size + (text ? gap + rowHeight : 2 * gap);
|
|
276
|
+
const height = innerHeight + 2 * border;
|
|
277
|
+
const radius = frame.style === "rounded-border" ? size * 0.1 : 0;
|
|
278
|
+
const innerRadius = Math.max(radius - border, 0);
|
|
279
|
+
return {
|
|
280
|
+
width,
|
|
281
|
+
height,
|
|
282
|
+
qrX,
|
|
283
|
+
qrY: border + gap,
|
|
284
|
+
qrSize: size,
|
|
285
|
+
shapes: [
|
|
286
|
+
rect(
|
|
287
|
+
border,
|
|
288
|
+
border,
|
|
289
|
+
innerWidth,
|
|
290
|
+
innerHeight,
|
|
291
|
+
innerRadius,
|
|
292
|
+
background
|
|
293
|
+
),
|
|
294
|
+
ring(
|
|
295
|
+
box(0, 0, width, height, radius),
|
|
296
|
+
box(border, border, innerWidth, innerHeight, innerRadius),
|
|
297
|
+
color
|
|
298
|
+
),
|
|
299
|
+
],
|
|
300
|
+
label: fitLabel(
|
|
301
|
+
text,
|
|
302
|
+
width / 2,
|
|
303
|
+
border + gap + size + rowHeight / 2,
|
|
304
|
+
rowHeight,
|
|
305
|
+
labelWidth,
|
|
306
|
+
frame.textColor || color
|
|
307
|
+
),
|
|
308
|
+
squareEdges: radius === 0,
|
|
309
|
+
color,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
case "speech-bubble": {
|
|
314
|
+
const radius = size * 0.07;
|
|
315
|
+
const innerRadius = Math.max(radius - border, 0);
|
|
316
|
+
const spacing = size * 0.025;
|
|
317
|
+
const pointer = size * 0.08;
|
|
318
|
+
const bubbleY = width + spacing + pointer;
|
|
319
|
+
return {
|
|
320
|
+
width,
|
|
321
|
+
height: bubbleY + band,
|
|
322
|
+
qrX,
|
|
323
|
+
qrY: border + gap,
|
|
324
|
+
qrSize: size,
|
|
325
|
+
shapes: [
|
|
326
|
+
rect(border, border, innerWidth, innerWidth, innerRadius, background),
|
|
327
|
+
ring(
|
|
328
|
+
box(0, 0, width, width, radius),
|
|
329
|
+
box(border, border, innerWidth, innerWidth, innerRadius),
|
|
330
|
+
color
|
|
331
|
+
),
|
|
332
|
+
{
|
|
333
|
+
type: "polygon",
|
|
334
|
+
points: [
|
|
335
|
+
// Base dips into the bubble so no hairline shows between them
|
|
336
|
+
[width / 2 - pointer, bubbleY + pointer / 2],
|
|
337
|
+
[width / 2, width + spacing],
|
|
338
|
+
[width / 2 + pointer, bubbleY + pointer / 2],
|
|
339
|
+
],
|
|
340
|
+
fill: color,
|
|
341
|
+
},
|
|
342
|
+
rect(0, bubbleY, width, band, band / 2, color),
|
|
343
|
+
],
|
|
344
|
+
label: fitLabel(
|
|
345
|
+
text,
|
|
346
|
+
width / 2,
|
|
347
|
+
bubbleY + band / 2,
|
|
348
|
+
band,
|
|
349
|
+
labelWidth,
|
|
350
|
+
frame.textColor || DEFAULT_BANNER_TEXT_COLOR
|
|
351
|
+
),
|
|
352
|
+
squareEdges: false,
|
|
353
|
+
color,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
default: {
|
|
358
|
+
// banner-bottom
|
|
359
|
+
const innerHeight = size + 2 * gap;
|
|
360
|
+
return {
|
|
361
|
+
width,
|
|
362
|
+
height: border + innerHeight + band,
|
|
363
|
+
qrX,
|
|
364
|
+
qrY: border + gap,
|
|
365
|
+
qrSize: size,
|
|
366
|
+
shapes: [
|
|
367
|
+
rect(border, border, innerWidth, innerHeight, 0, background),
|
|
368
|
+
ring(
|
|
369
|
+
box(0, 0, width, border + innerHeight + band, 0),
|
|
370
|
+
box(border, border, innerWidth, innerHeight, 0),
|
|
371
|
+
color
|
|
372
|
+
),
|
|
373
|
+
],
|
|
374
|
+
label: fitLabel(
|
|
375
|
+
text,
|
|
376
|
+
width / 2,
|
|
377
|
+
border + innerHeight + band / 2,
|
|
378
|
+
band,
|
|
379
|
+
labelWidth,
|
|
380
|
+
frame.textColor || DEFAULT_BANNER_TEXT_COLOR
|
|
381
|
+
),
|
|
382
|
+
squareEdges: true,
|
|
383
|
+
color,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function box(
|
|
390
|
+
x: number,
|
|
391
|
+
y: number,
|
|
392
|
+
width: number,
|
|
393
|
+
height: number,
|
|
394
|
+
radius: number
|
|
395
|
+
): FrameBox {
|
|
396
|
+
return { x, y, width, height, radius };
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function rect(
|
|
400
|
+
x: number,
|
|
401
|
+
y: number,
|
|
402
|
+
width: number,
|
|
403
|
+
height: number,
|
|
404
|
+
radius: number,
|
|
405
|
+
fill: string
|
|
406
|
+
): FrameRect {
|
|
407
|
+
return { type: "rect", ...box(x, y, width, height, radius), fill };
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function ring(outer: FrameBox, hole: FrameBox, fill: string): FrameRect {
|
|
411
|
+
return { type: "rect", ...outer, fill, hole };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Output dimensions for a code of `size` pixels with `frame` applied
|
|
416
|
+
* (`size × size` when there is no frame).
|
|
417
|
+
*/
|
|
418
|
+
export function getFrameDimensions(
|
|
419
|
+
size: number,
|
|
420
|
+
frame?: AnyQRFrame | null
|
|
421
|
+
): { width: number; height: number } {
|
|
422
|
+
if (!isFrameActive(frame)) {
|
|
423
|
+
return { width: size, height: size };
|
|
424
|
+
}
|
|
425
|
+
const { width, height } = getFrameLayout(size, frame);
|
|
426
|
+
return { width, height };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Frame boxes, labels and the code's offset snap to 0.01 px
|
|
430
|
+
const round = (value: number) => roundTo(value, 2);
|
|
431
|
+
|
|
432
|
+
function boxPath({ x, y, width, height, radius }: FrameBox): string {
|
|
433
|
+
const r = round(Math.min(radius, width / 2, height / 2));
|
|
434
|
+
const [x0, y0, w, h] = [round(x), round(y), round(width), round(height)];
|
|
435
|
+
if (r <= 0) {
|
|
436
|
+
return compact`M${x0} ${y0}h${w}v${h}h${-w}z`;
|
|
437
|
+
}
|
|
438
|
+
return (
|
|
439
|
+
compact`M${x0 + r} ${y0}h${round(w - 2 * r)}a${r} ${r} 0 0 1 ${r} ${r}` +
|
|
440
|
+
compact`v${round(h - 2 * r)}a${r} ${r} 0 0 1 ${-r} ${r}` +
|
|
441
|
+
compact`h${round(-(w - 2 * r))}a${r} ${r} 0 0 1 ${-r} ${-r}` +
|
|
442
|
+
compact`v${round(-(h - 2 * r))}a${r} ${r} 0 0 1 ${r} ${-r}z`
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function renderShape(shape: FrameShape, key: number) {
|
|
447
|
+
if (shape.type === "rect" && shape.hole) {
|
|
448
|
+
return (
|
|
449
|
+
<path
|
|
450
|
+
d={boxPath(shape) + boxPath(shape.hole)}
|
|
451
|
+
fill={shape.fill}
|
|
452
|
+
fillRule="evenodd"
|
|
453
|
+
key={key}
|
|
454
|
+
/>
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
if (shape.type === "polygon") {
|
|
458
|
+
return (
|
|
459
|
+
<polygon
|
|
460
|
+
fill={shape.fill}
|
|
461
|
+
key={key}
|
|
462
|
+
points={shape.points
|
|
463
|
+
.map(([x, y]) => `${round(x)},${round(y)}`)
|
|
464
|
+
.join(" ")}
|
|
465
|
+
/>
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
if (shape.type === "path") {
|
|
469
|
+
return (
|
|
470
|
+
<path
|
|
471
|
+
d={shape.d}
|
|
472
|
+
fill={shape.fill}
|
|
473
|
+
key={key}
|
|
474
|
+
{...(shape.fillRule === "evenodd" && { fillRule: "evenodd" })}
|
|
475
|
+
/>
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
return (
|
|
479
|
+
<rect
|
|
480
|
+
fill={shape.fill}
|
|
481
|
+
height={round(shape.height)}
|
|
482
|
+
key={key}
|
|
483
|
+
width={round(shape.width)}
|
|
484
|
+
x={round(shape.x)}
|
|
485
|
+
y={round(shape.y)}
|
|
486
|
+
{...(shape.radius > 0 && {
|
|
487
|
+
rx: round(shape.radius),
|
|
488
|
+
ry: round(shape.radius),
|
|
489
|
+
})}
|
|
490
|
+
/>
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Wraps a rendered QR `<svg>` (sized `size × size`) in a frame. The QR element
|
|
496
|
+
* is nested as-is at its offset, so its modules are untouched.
|
|
497
|
+
*/
|
|
498
|
+
export function wrapWithFrame(
|
|
499
|
+
qrElement: ReactElement<SVGProps<SVGSVGElement>>,
|
|
500
|
+
options: {
|
|
501
|
+
size: number;
|
|
502
|
+
frame: AnyQRFrame;
|
|
503
|
+
bgColor?: ColorConfig;
|
|
504
|
+
svgProps?: SVGProps<SVGSVGElement>;
|
|
505
|
+
/** Mark each label with FRAME_LABEL_ATTRIBUTE (its `field`), for editors */
|
|
506
|
+
markLabels?: boolean;
|
|
507
|
+
}
|
|
508
|
+
): ReactElement {
|
|
509
|
+
const { size, frame, bgColor, svgProps, markLabels } = options;
|
|
510
|
+
const layout = getFrameLayout(size, frame, bgColor);
|
|
511
|
+
const labels = frameLabels(layout);
|
|
512
|
+
const width = round(layout.width);
|
|
513
|
+
const height = round(layout.height);
|
|
514
|
+
|
|
515
|
+
return (
|
|
516
|
+
<svg
|
|
517
|
+
height={height}
|
|
518
|
+
viewBox={`0 0 ${width} ${height}`}
|
|
519
|
+
width={width}
|
|
520
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
521
|
+
{...svgProps}
|
|
522
|
+
>
|
|
523
|
+
<g data-qrdx-frame={frame.style}>
|
|
524
|
+
{layout.shapes.map((shape, index) => renderShape(shape, index))}
|
|
525
|
+
</g>
|
|
526
|
+
{React.cloneElement(qrElement, {
|
|
527
|
+
x: round(layout.qrX),
|
|
528
|
+
y: round(layout.qrY),
|
|
529
|
+
width: size,
|
|
530
|
+
height: size,
|
|
531
|
+
})}
|
|
532
|
+
{labels.map((label, index) => (
|
|
533
|
+
<text
|
|
534
|
+
data-max-width={round(label.maxWidth)}
|
|
535
|
+
fill={label.color}
|
|
536
|
+
fontFamily={FRAME_FONT_FAMILY}
|
|
537
|
+
fontSize={round(label.fontSize)}
|
|
538
|
+
fontWeight={label.fontWeight ?? 700}
|
|
539
|
+
key={index}
|
|
540
|
+
textAnchor={label.align ?? "middle"}
|
|
541
|
+
x={round(label.x)}
|
|
542
|
+
y={round(label.y)}
|
|
543
|
+
{...(label.rotate && {
|
|
544
|
+
transform: `rotate(${round(label.rotate)} ${round(label.x)} ${round(label.y)})`,
|
|
545
|
+
})}
|
|
546
|
+
{...(markLabels &&
|
|
547
|
+
label.field && { [FRAME_LABEL_ATTRIBUTE]: label.field })}
|
|
548
|
+
>
|
|
549
|
+
{label.text}
|
|
550
|
+
</text>
|
|
551
|
+
))}
|
|
552
|
+
</svg>
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/** Every label of a layout: the main one first, then the extra ones. */
|
|
557
|
+
export function frameLabels(layout: FrameLayout): FrameLabel[] {
|
|
558
|
+
return [
|
|
559
|
+
...(layout.label ? [layout.label] : []),
|
|
560
|
+
...(layout.extraLabels ?? []),
|
|
561
|
+
];
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** Paints the frame shapes and label on a 2D canvas (QR code not included). */
|
|
565
|
+
export function drawFrameOnCanvas(
|
|
566
|
+
ctx: CanvasRenderingContext2D,
|
|
567
|
+
layout: FrameLayout
|
|
568
|
+
) {
|
|
569
|
+
const traceBox = ({ x, y, width, height, radius }: FrameBox) => {
|
|
570
|
+
const r = Math.min(radius, width / 2, height / 2);
|
|
571
|
+
ctx.moveTo(x + r, y);
|
|
572
|
+
ctx.arcTo(x + width, y, x + width, y + height, r);
|
|
573
|
+
ctx.arcTo(x + width, y + height, x, y + height, r);
|
|
574
|
+
ctx.arcTo(x, y + height, x, y, r);
|
|
575
|
+
ctx.arcTo(x, y, x + width, y, r);
|
|
576
|
+
ctx.closePath();
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
for (const shape of layout.shapes) {
|
|
580
|
+
if (shape.fill === "transparent" || shape.fill === "none") {
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
ctx.fillStyle = shape.fill;
|
|
584
|
+
if (shape.type === "path") {
|
|
585
|
+
if (typeof Path2D !== "undefined") {
|
|
586
|
+
ctx.fill(new Path2D(shape.d), shape.fillRule ?? "nonzero");
|
|
587
|
+
}
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
ctx.beginPath();
|
|
591
|
+
if (shape.type === "polygon") {
|
|
592
|
+
shape.points.forEach(([x, y], index) => {
|
|
593
|
+
if (index === 0) {
|
|
594
|
+
ctx.moveTo(x, y);
|
|
595
|
+
} else {
|
|
596
|
+
ctx.lineTo(x, y);
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
ctx.closePath();
|
|
600
|
+
ctx.fill();
|
|
601
|
+
} else {
|
|
602
|
+
traceBox(shape);
|
|
603
|
+
if (shape.hole) {
|
|
604
|
+
traceBox(shape.hole);
|
|
605
|
+
}
|
|
606
|
+
ctx.fill("evenodd");
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
for (const label of frameLabels(layout)) {
|
|
611
|
+
ctx.fillStyle = label.color;
|
|
612
|
+
ctx.font = `${label.fontWeight ?? 700} ${label.fontSize}px ${FRAME_FONT_FAMILY}`;
|
|
613
|
+
ctx.textAlign =
|
|
614
|
+
label.align === "start"
|
|
615
|
+
? "left"
|
|
616
|
+
: label.align === "end"
|
|
617
|
+
? "right"
|
|
618
|
+
: "center";
|
|
619
|
+
ctx.textBaseline = "alphabetic";
|
|
620
|
+
if (label.rotate) {
|
|
621
|
+
ctx.save();
|
|
622
|
+
ctx.translate(label.x, label.y);
|
|
623
|
+
ctx.rotate((label.rotate * Math.PI) / 180);
|
|
624
|
+
ctx.fillText(label.text, 0, 0, label.maxWidth);
|
|
625
|
+
ctx.restore();
|
|
626
|
+
} else {
|
|
627
|
+
ctx.fillText(label.text, label.x, label.y, label.maxWidth);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"kind": "qrdx.frame",
|
|
3
|
+
"version": 1,
|
|
4
|
+
"artboard": {
|
|
5
|
+
"width": 1240,
|
|
6
|
+
"height": 1420
|
|
7
|
+
},
|
|
8
|
+
"qr": {
|
|
9
|
+
"x": 120,
|
|
10
|
+
"y": 120,
|
|
11
|
+
"size": 1000,
|
|
12
|
+
"padding": 60
|
|
13
|
+
},
|
|
14
|
+
"colors": {
|
|
15
|
+
"frame": "#000000",
|
|
16
|
+
"text": "#FFFFFF"
|
|
17
|
+
},
|
|
18
|
+
"shapes": [
|
|
19
|
+
{
|
|
20
|
+
"type": "rect",
|
|
21
|
+
"x": 40,
|
|
22
|
+
"y": 40,
|
|
23
|
+
"width": 1160,
|
|
24
|
+
"height": 1160,
|
|
25
|
+
"fill": "background"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"type": "rect",
|
|
29
|
+
"x": 0,
|
|
30
|
+
"y": 0,
|
|
31
|
+
"width": 1240,
|
|
32
|
+
"height": 1420,
|
|
33
|
+
"fill": "frame",
|
|
34
|
+
"hole": {
|
|
35
|
+
"x": 40,
|
|
36
|
+
"y": 40,
|
|
37
|
+
"width": 1160,
|
|
38
|
+
"height": 1160
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
],
|
|
42
|
+
"labels": [
|
|
43
|
+
{
|
|
44
|
+
"type": "text",
|
|
45
|
+
"id": "cta",
|
|
46
|
+
"slot": "primary",
|
|
47
|
+
"text": "Scan me",
|
|
48
|
+
"color": "text",
|
|
49
|
+
"box": {
|
|
50
|
+
"x": 120,
|
|
51
|
+
"y": 1200,
|
|
52
|
+
"width": 1000,
|
|
53
|
+
"height": 220
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
],
|
|
57
|
+
"bleed": "frame"
|
|
58
|
+
}
|