qrdx 0.0.46 → 0.0.47
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 +353 -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 +22 -5
- 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 +24 -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.d.ts +18 -2
- package/dist/src/utils.d.ts.map +1 -1
- 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 +68 -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 +123 -52
- 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 +141 -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.tsx +794 -385
- 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,946 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: SVG painting */
|
|
2
|
+
// Walks the SVG this library renders and hands vector shapes, text and images
|
|
3
|
+
// to an output backend (PDF via jsPDF, EPS as PostScript).
|
|
4
|
+
import { angularSpan, arcsOverlap, flattenOver } from "./paint";
|
|
5
|
+
import { parseColor, type RGBAColor } from "./parse-color";
|
|
6
|
+
import {
|
|
7
|
+
applyMatrix,
|
|
8
|
+
ellipseSegments,
|
|
9
|
+
type Matrix,
|
|
10
|
+
multiply,
|
|
11
|
+
parseNumbers,
|
|
12
|
+
parsePathData,
|
|
13
|
+
parseTransform,
|
|
14
|
+
rectSegments,
|
|
15
|
+
type Segment,
|
|
16
|
+
type SvgNode,
|
|
17
|
+
} from "./svg-geometry";
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
boundingBox,
|
|
21
|
+
invertMatrix,
|
|
22
|
+
type PlacedGradient,
|
|
23
|
+
placeGradient,
|
|
24
|
+
type SvgGradient,
|
|
25
|
+
} from "./svg-gradient";
|
|
26
|
+
|
|
27
|
+
/** A path segment mapped through an affine `matrix`. */
|
|
28
|
+
function transformSegment(segment: Segment, matrix: Matrix): Segment {
|
|
29
|
+
if (segment.op === "Z") {
|
|
30
|
+
return segment;
|
|
31
|
+
}
|
|
32
|
+
if (segment.op === "C") {
|
|
33
|
+
const [x1, y1] = applyMatrix(matrix, segment.x1, segment.y1);
|
|
34
|
+
const [x2, y2] = applyMatrix(matrix, segment.x2, segment.y2);
|
|
35
|
+
const [x, y] = applyMatrix(matrix, segment.x, segment.y);
|
|
36
|
+
return { op: "C", x1, y1, x2, y2, x, y };
|
|
37
|
+
}
|
|
38
|
+
const [x, y] = applyMatrix(matrix, segment.x, segment.y);
|
|
39
|
+
return { op: segment.op, x, y };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface PaintState {
|
|
43
|
+
matrix: Matrix;
|
|
44
|
+
fill: string;
|
|
45
|
+
fillRule: string;
|
|
46
|
+
stroke: string;
|
|
47
|
+
strokeWidth: number;
|
|
48
|
+
fontSize: number;
|
|
49
|
+
fontWeight: string;
|
|
50
|
+
textAnchor: string;
|
|
51
|
+
/** Viewport size, for percentage lengths */
|
|
52
|
+
viewport: [number, number];
|
|
53
|
+
/** Inherited `fill-opacity` (1 when unset) */
|
|
54
|
+
fillOpacity?: number;
|
|
55
|
+
/**
|
|
56
|
+
* `opacity` of this element times its ancestors' (1 when unset). Exact for
|
|
57
|
+
* shapes that don't overlap inside a see-through group, as in QR codes.
|
|
58
|
+
*/
|
|
59
|
+
opacity?: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type PaintColor = RGBAColor | null;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A fill: a solid color (its `a` folds in fill-opacity and opacity), a
|
|
66
|
+
* gradient placed on the shape being painted (`alpha` multiplies its stops'
|
|
67
|
+
* own alpha), or a userSpaceOnUse `<pattern>` (angular gradients and image
|
|
68
|
+
* fills) whose content is painted inside the shape.
|
|
69
|
+
*/
|
|
70
|
+
export type FillPaint =
|
|
71
|
+
| { color: RGBAColor }
|
|
72
|
+
| { gradient: PlacedGradient; alpha: number }
|
|
73
|
+
| { pattern: SvgNode; alpha: number };
|
|
74
|
+
|
|
75
|
+
const WHITE: RGBAColor = { r: 255, g: 255, b: 255, a: 1 };
|
|
76
|
+
|
|
77
|
+
/** The code's background gradient (utils.tsx ids it "bg-color-…"). */
|
|
78
|
+
const BACKGROUND_PAINT = /^url\(\s*#bg-color-/;
|
|
79
|
+
|
|
80
|
+
const URL_REFERENCE = /^url\(\s*#([^)\s]+)\s*\)/;
|
|
81
|
+
|
|
82
|
+
const SKIPPED_ELEMENTS = new Set([
|
|
83
|
+
"defs",
|
|
84
|
+
"clipPath",
|
|
85
|
+
"mask",
|
|
86
|
+
"pattern",
|
|
87
|
+
"linearGradient",
|
|
88
|
+
"radialGradient",
|
|
89
|
+
"symbol",
|
|
90
|
+
"title",
|
|
91
|
+
"desc",
|
|
92
|
+
"metadata",
|
|
93
|
+
"style",
|
|
94
|
+
"script",
|
|
95
|
+
"use",
|
|
96
|
+
"foreignObject",
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
export function readStyle(node: SvgNode, name: string): string | undefined {
|
|
100
|
+
const inline = node.attrs.style;
|
|
101
|
+
if (inline) {
|
|
102
|
+
for (const declaration of inline.split(";")) {
|
|
103
|
+
const [key, ...rest] = declaration.split(":");
|
|
104
|
+
if (key?.trim() === name) {
|
|
105
|
+
return rest.join(":").trim();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return node.attrs[name];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function length(
|
|
113
|
+
value: string | undefined,
|
|
114
|
+
reference: number,
|
|
115
|
+
fallback = 0
|
|
116
|
+
): number {
|
|
117
|
+
if (value === undefined || value === "") {
|
|
118
|
+
return fallback;
|
|
119
|
+
}
|
|
120
|
+
const number = Number.parseFloat(value);
|
|
121
|
+
if (Number.isNaN(number)) {
|
|
122
|
+
return fallback;
|
|
123
|
+
}
|
|
124
|
+
return value.trim().endsWith("%") ? (number / 100) * reference : number;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function collectGradientColors(
|
|
128
|
+
node: SvgNode,
|
|
129
|
+
out: Map<string, string>
|
|
130
|
+
): Map<string, string> {
|
|
131
|
+
if (
|
|
132
|
+
(node.name === "linearGradient" || node.name === "radialGradient") &&
|
|
133
|
+
node.attrs.id
|
|
134
|
+
) {
|
|
135
|
+
const stop = node.children.find((child) => child.name === "stop");
|
|
136
|
+
const color = stop && (readStyle(stop, "stop-color") ?? "#000000");
|
|
137
|
+
if (color) {
|
|
138
|
+
out.set(node.attrs.id, color);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
for (const child of node.children) {
|
|
142
|
+
collectGradientColors(child, out);
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function collectImageHrefs(
|
|
148
|
+
node: SvgNode,
|
|
149
|
+
out: Set<string>
|
|
150
|
+
): Set<string> {
|
|
151
|
+
if (node.name === "image") {
|
|
152
|
+
const href = node.attrs.href ?? node.attrs["xlink:href"];
|
|
153
|
+
if (href) {
|
|
154
|
+
out.add(href);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
for (const child of node.children) {
|
|
158
|
+
collectImageHrefs(child, out);
|
|
159
|
+
}
|
|
160
|
+
return out;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function imageFormat(href: string): "PNG" | "JPEG" | null {
|
|
164
|
+
if (/^data:image\/png[;,]/i.test(href)) {
|
|
165
|
+
return "PNG";
|
|
166
|
+
}
|
|
167
|
+
if (/^data:image\/jpe?g[;,]/i.test(href)) {
|
|
168
|
+
return "JPEG";
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Converts logos jsPDF can't embed (SVG, WebP, URLs) to PNG in the browser. */
|
|
174
|
+
export async function rasterizeImage(href: string): Promise<string | null> {
|
|
175
|
+
if (typeof document === "undefined" || typeof Image === "undefined") {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
return await new Promise((resolve) => {
|
|
179
|
+
const img = new Image();
|
|
180
|
+
img.crossOrigin = "anonymous";
|
|
181
|
+
img.onload = () => {
|
|
182
|
+
const canvas = document.createElement("canvas");
|
|
183
|
+
const longest = Math.max(
|
|
184
|
+
img.naturalWidth || 512,
|
|
185
|
+
img.naturalHeight || 512
|
|
186
|
+
);
|
|
187
|
+
const scale = Math.min(1024 / longest, 4);
|
|
188
|
+
canvas.width = Math.round((img.naturalWidth || 512) * scale);
|
|
189
|
+
canvas.height = Math.round((img.naturalHeight || 512) * scale);
|
|
190
|
+
const ctx = canvas.getContext("2d");
|
|
191
|
+
if (!ctx) {
|
|
192
|
+
resolve(null);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
|
196
|
+
try {
|
|
197
|
+
resolve(canvas.toDataURL("image/png"));
|
|
198
|
+
} catch {
|
|
199
|
+
resolve(null);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
img.onerror = () => resolve(null);
|
|
203
|
+
img.src = href;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface ImageBox {
|
|
208
|
+
x: number;
|
|
209
|
+
y: number;
|
|
210
|
+
width: number;
|
|
211
|
+
height: number;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Where an `<image>` draws, in its user space: its x/y/width/height box,
|
|
216
|
+
* or, with an explicit preserveAspectRatio other than "none" and the
|
|
217
|
+
* picture's own size, the picture fitted into it (`meet`) or covering it
|
|
218
|
+
* (`slice`, which then clips to the box). Without the attribute the picture
|
|
219
|
+
* is stretched to the box, as the engine's logos ask ("none").
|
|
220
|
+
*/
|
|
221
|
+
export function imagePlacement(
|
|
222
|
+
node: SvgNode,
|
|
223
|
+
viewport: [number, number],
|
|
224
|
+
intrinsic?: { width: number; height: number } | null
|
|
225
|
+
): { draw: ImageBox; clip: ImageBox | null } | null {
|
|
226
|
+
const box = {
|
|
227
|
+
x: length(node.attrs.x, viewport[0]),
|
|
228
|
+
y: length(node.attrs.y, viewport[1]),
|
|
229
|
+
width: length(node.attrs.width, viewport[0]),
|
|
230
|
+
height: length(node.attrs.height, viewport[1]),
|
|
231
|
+
};
|
|
232
|
+
if (box.width <= 0 || box.height <= 0) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
const aspect = (node.attrs.preserveAspectRatio ?? "").trim();
|
|
236
|
+
if (
|
|
237
|
+
!aspect ||
|
|
238
|
+
aspect.startsWith("none") ||
|
|
239
|
+
!intrinsic ||
|
|
240
|
+
!(intrinsic.width > 0 && intrinsic.height > 0)
|
|
241
|
+
) {
|
|
242
|
+
return { draw: box, clip: null };
|
|
243
|
+
}
|
|
244
|
+
const [align = "xMidYMid", mode = "meet"] = aspect.split(/\s+/);
|
|
245
|
+
const slice = mode === "slice";
|
|
246
|
+
const scale = (slice ? Math.max : Math.min)(
|
|
247
|
+
box.width / intrinsic.width,
|
|
248
|
+
box.height / intrinsic.height
|
|
249
|
+
);
|
|
250
|
+
const width = intrinsic.width * scale;
|
|
251
|
+
const height = intrinsic.height * scale;
|
|
252
|
+
const fraction = (axis: "x" | "y") => {
|
|
253
|
+
const part = axis === "x" ? align.slice(0, 4) : align.slice(4);
|
|
254
|
+
return part.endsWith("Min") ? 0 : part.endsWith("Max") ? 1 : 0.5;
|
|
255
|
+
};
|
|
256
|
+
return {
|
|
257
|
+
draw: {
|
|
258
|
+
x: box.x + (box.width - width) * fraction("x"),
|
|
259
|
+
y: box.y + (box.height - height) * fraction("y"),
|
|
260
|
+
width,
|
|
261
|
+
height,
|
|
262
|
+
},
|
|
263
|
+
clip: slice ? box : null,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** `<clipPath>` elements by id, anywhere in the tree. */
|
|
268
|
+
function collectClipPaths(
|
|
269
|
+
node: SvgNode,
|
|
270
|
+
out: Map<string, SvgNode>
|
|
271
|
+
): Map<string, SvgNode> {
|
|
272
|
+
if (node.name === "clipPath" && node.attrs.id) {
|
|
273
|
+
out.set(node.attrs.id, node);
|
|
274
|
+
}
|
|
275
|
+
for (const child of node.children) {
|
|
276
|
+
collectClipPaths(child, out);
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** `<pattern>` elements by id, anywhere in the tree. */
|
|
282
|
+
function collectPatterns(
|
|
283
|
+
node: SvgNode,
|
|
284
|
+
out: Map<string, SvgNode>
|
|
285
|
+
): Map<string, SvgNode> {
|
|
286
|
+
if (node.name === "pattern" && node.attrs.id) {
|
|
287
|
+
out.set(node.attrs.id, node);
|
|
288
|
+
}
|
|
289
|
+
for (const child of node.children) {
|
|
290
|
+
collectPatterns(child, out);
|
|
291
|
+
}
|
|
292
|
+
return out;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Outline of one clip-path child, in its own user space. */
|
|
296
|
+
function clipShapeSegments(
|
|
297
|
+
node: SvgNode,
|
|
298
|
+
viewport: [number, number]
|
|
299
|
+
): Segment[] {
|
|
300
|
+
const [vw, vh] = viewport;
|
|
301
|
+
const attr = (name: string, reference: number) =>
|
|
302
|
+
length(node.attrs[name], reference);
|
|
303
|
+
switch (node.name) {
|
|
304
|
+
case "path":
|
|
305
|
+
return parsePathData(node.attrs.d ?? "");
|
|
306
|
+
case "rect": {
|
|
307
|
+
const rx = node.attrs.rx ?? node.attrs.ry;
|
|
308
|
+
const ry = node.attrs.ry ?? node.attrs.rx;
|
|
309
|
+
return rectSegments(
|
|
310
|
+
attr("x", vw),
|
|
311
|
+
attr("y", vh),
|
|
312
|
+
attr("width", vw),
|
|
313
|
+
attr("height", vh),
|
|
314
|
+
length(rx, vw),
|
|
315
|
+
length(ry, vh)
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
case "circle": {
|
|
319
|
+
const r = attr("r", Math.hypot(vw, vh) / Math.SQRT2);
|
|
320
|
+
return ellipseSegments(attr("cx", vw), attr("cy", vh), r, r);
|
|
321
|
+
}
|
|
322
|
+
case "ellipse":
|
|
323
|
+
return ellipseSegments(
|
|
324
|
+
attr("cx", vw),
|
|
325
|
+
attr("cy", vh),
|
|
326
|
+
attr("rx", vw),
|
|
327
|
+
attr("ry", vh)
|
|
328
|
+
);
|
|
329
|
+
default:
|
|
330
|
+
return [];
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export abstract class SvgPainter {
|
|
335
|
+
protected readonly gradients: Map<string, string>;
|
|
336
|
+
/** Gradients the backend shades natively (see `resolveFill`) */
|
|
337
|
+
protected readonly shadings: Map<string, SvgGradient> | null;
|
|
338
|
+
/** `<clipPath>`s of the document being walked (collected at its root) */
|
|
339
|
+
private clipPaths = new Map<string, SvgNode>();
|
|
340
|
+
/** `<pattern>`s of the document being walked (collected at its root) */
|
|
341
|
+
private patterns = new Map<string, SvgNode>();
|
|
342
|
+
/**
|
|
343
|
+
* Opaque color see-through paint is flattened over where a backend can't
|
|
344
|
+
* blend (PostScript; a PDF gradient whose stops differ in alpha; a PDF
|
|
345
|
+
* angular fan): the code's background on paper. The background itself is
|
|
346
|
+
* flattened over `paper`: the shape filled with the background gradient
|
|
347
|
+
* (`url(#bg-color-…)`) or with the background color. Exporters set all
|
|
348
|
+
* three from the props.
|
|
349
|
+
*/
|
|
350
|
+
backdrop: RGBAColor = WHITE;
|
|
351
|
+
paper: RGBAColor = WHITE;
|
|
352
|
+
/** The background's color as drawn (alpha included), when it is solid */
|
|
353
|
+
background: RGBAColor | null = null;
|
|
354
|
+
/** True while painting the background shape (and its pattern) */
|
|
355
|
+
protected onPaper = false;
|
|
356
|
+
/** True while painting an angular gradient's wedge fan */
|
|
357
|
+
protected inConic = false;
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* `gradients` maps gradient ids to the color they flatten to. Pass
|
|
361
|
+
* `shadings` (from `collectGradients`) when the backend can shade them.
|
|
362
|
+
*/
|
|
363
|
+
constructor(
|
|
364
|
+
gradients: Map<string, string>,
|
|
365
|
+
shadings: Map<string, SvgGradient> | null = null
|
|
366
|
+
) {
|
|
367
|
+
this.gradients = gradients;
|
|
368
|
+
this.shadings = shadings;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
resolveColor(value: string | undefined): PaintColor {
|
|
372
|
+
if (!value || value === "none") {
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
const reference = URL_REFERENCE.exec(value);
|
|
376
|
+
if (reference) {
|
|
377
|
+
// Gradients are flattened to their first stop; patterns are skipped.
|
|
378
|
+
return this.resolveColor(this.gradients.get(reference[1] as string));
|
|
379
|
+
}
|
|
380
|
+
const color = parseColor(value === "currentColor" ? "#000000" : value);
|
|
381
|
+
return color && color.a > 0 ? color : null;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** The gradient a `url(#id)` paint shades natively, if any. */
|
|
385
|
+
shadedGradient(value: string | undefined): SvgGradient | undefined {
|
|
386
|
+
const id = value ? URL_REFERENCE.exec(value)?.[1] : undefined;
|
|
387
|
+
return id ? this.shadings?.get(id) : undefined;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Resolves a fill for these segments: a gradient this painter shades is
|
|
392
|
+
* placed on the shape (or becomes the solid color SVG paints for a
|
|
393
|
+
* degenerate gradient), a userSpaceOnUse pattern is returned to paint
|
|
394
|
+
* inside the shape, and anything else goes through `resolveColor`.
|
|
395
|
+
* `alpha` (fill-opacity times opacity) is folded into solid colors and
|
|
396
|
+
* returned alongside gradients and patterns.
|
|
397
|
+
*/
|
|
398
|
+
resolveFill(
|
|
399
|
+
value: string | undefined,
|
|
400
|
+
segments: Segment[],
|
|
401
|
+
viewport: [number, number],
|
|
402
|
+
alpha = 1
|
|
403
|
+
): FillPaint | null {
|
|
404
|
+
if (alpha <= 0) {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
const pattern = this.patternOf(value);
|
|
408
|
+
if (pattern) {
|
|
409
|
+
return { pattern, alpha };
|
|
410
|
+
}
|
|
411
|
+
const gradient = this.shadedGradient(value);
|
|
412
|
+
if (gradient) {
|
|
413
|
+
const placed = placeGradient(gradient, segments, viewport);
|
|
414
|
+
if (!placed) {
|
|
415
|
+
return null;
|
|
416
|
+
}
|
|
417
|
+
if ("coords" in placed) {
|
|
418
|
+
return { gradient: placed, alpha };
|
|
419
|
+
}
|
|
420
|
+
const color = { ...placed, a: placed.a * alpha };
|
|
421
|
+
return color.a > 0 ? { color } : null;
|
|
422
|
+
}
|
|
423
|
+
const color = this.resolveColor(value);
|
|
424
|
+
if (!color) {
|
|
425
|
+
return null;
|
|
426
|
+
}
|
|
427
|
+
const withAlpha = { ...color, a: color.a * alpha };
|
|
428
|
+
return withAlpha.a > 0 ? { color: withAlpha } : null;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** The userSpaceOnUse `<pattern>` a `url(#id)` paint points to, if any. */
|
|
432
|
+
patternOf(value: string | undefined): SvgNode | undefined {
|
|
433
|
+
const id = value ? URL_REFERENCE.exec(value)?.[1] : undefined;
|
|
434
|
+
const pattern = id ? this.patterns.get(id) : undefined;
|
|
435
|
+
if (
|
|
436
|
+
pattern?.attrs.patternUnits !== "userSpaceOnUse" ||
|
|
437
|
+
(pattern.attrs.patternContentUnits ?? "userSpaceOnUse") !==
|
|
438
|
+
"userSpaceOnUse" ||
|
|
439
|
+
pattern.attrs.viewBox
|
|
440
|
+
) {
|
|
441
|
+
return undefined;
|
|
442
|
+
}
|
|
443
|
+
return pattern;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* `color` made opaque for a backend without transparency: over paper
|
|
448
|
+
* while painting the background, over the backdrop otherwise.
|
|
449
|
+
*/
|
|
450
|
+
flatten(color: RGBAColor): RGBAColor {
|
|
451
|
+
if (color.a >= 1) {
|
|
452
|
+
return color;
|
|
453
|
+
}
|
|
454
|
+
const background = this.background;
|
|
455
|
+
const isBackground =
|
|
456
|
+
background !== null &&
|
|
457
|
+
Math.abs(background.r - color.r) < 0.5 &&
|
|
458
|
+
Math.abs(background.g - color.g) < 0.5 &&
|
|
459
|
+
Math.abs(background.b - color.b) < 0.5 &&
|
|
460
|
+
Math.abs(background.a - color.a) < 0.002;
|
|
461
|
+
return flattenOver(
|
|
462
|
+
color,
|
|
463
|
+
this.onPaper || isBackground ? this.paper : this.backdrop
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Paints a pattern's content inside `segments` (the shape, in the user
|
|
469
|
+
* space of `state`): clip to the shape, then walk the tiles that meet it.
|
|
470
|
+
* The tile at (x, y) holds the content with its origin at the tile's
|
|
471
|
+
* corner, and content past a tile is cut off when there is more than one.
|
|
472
|
+
* A pattern marked `data-qrdx-conic` (an angular gradient's wedge fan)
|
|
473
|
+
* only walks the wedges whose `data-arc` meets the shape.
|
|
474
|
+
* Returns false when this backend can't clip (nothing is painted).
|
|
475
|
+
*/
|
|
476
|
+
paintPattern(
|
|
477
|
+
segments: Segment[],
|
|
478
|
+
state: PaintState,
|
|
479
|
+
pattern: SvgNode,
|
|
480
|
+
alpha: number
|
|
481
|
+
): boolean {
|
|
482
|
+
const box = boundingBox(segments);
|
|
483
|
+
if (!box || box.width <= 0 || box.height <= 0) {
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
const [vw, vh] = state.viewport;
|
|
487
|
+
const tileX = length(pattern.attrs.x, vw);
|
|
488
|
+
const tileY = length(pattern.attrs.y, vh);
|
|
489
|
+
const tileWidth = length(pattern.attrs.width, vw);
|
|
490
|
+
const tileHeight = length(pattern.attrs.height, vh);
|
|
491
|
+
if (!(tileWidth > 0 && tileHeight > 0)) {
|
|
492
|
+
return true;
|
|
493
|
+
}
|
|
494
|
+
const transform = parseTransform(pattern.attrs.patternTransform);
|
|
495
|
+
const inverse = invertMatrix(transform);
|
|
496
|
+
if (!inverse) {
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
if (!this.beginClip(segments, state.matrix, state.fillRule === "evenodd")) {
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
const wasConic = this.inConic;
|
|
503
|
+
this.inConic = pattern.attrs["data-qrdx-conic"] !== undefined;
|
|
504
|
+
try {
|
|
505
|
+
// The shape's box in pattern space (before patternTransform)
|
|
506
|
+
const corners = [
|
|
507
|
+
applyMatrix(inverse, box.x, box.y),
|
|
508
|
+
applyMatrix(inverse, box.x + box.width, box.y),
|
|
509
|
+
applyMatrix(inverse, box.x, box.y + box.height),
|
|
510
|
+
applyMatrix(inverse, box.x + box.width, box.y + box.height),
|
|
511
|
+
];
|
|
512
|
+
const xs = corners.map(([x]) => x);
|
|
513
|
+
const ys = corners.map(([, y]) => y);
|
|
514
|
+
const minX = Math.min(...xs);
|
|
515
|
+
const maxX = Math.max(...xs);
|
|
516
|
+
const minY = Math.min(...ys);
|
|
517
|
+
const maxY = Math.max(...ys);
|
|
518
|
+
const firstColumn = Math.floor((minX - tileX) / tileWidth);
|
|
519
|
+
const lastColumn = Math.ceil((maxX - tileX) / tileWidth) - 1;
|
|
520
|
+
const firstRow = Math.floor((minY - tileY) / tileHeight);
|
|
521
|
+
const lastRow = Math.ceil((maxY - tileY) / tileHeight) - 1;
|
|
522
|
+
const tiles = (lastColumn - firstColumn + 1) * (lastRow - firstRow + 1);
|
|
523
|
+
if (tiles > 4096) {
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
526
|
+
const conic = (pattern.attrs["data-qrdx-conic"] ?? "")
|
|
527
|
+
.trim()
|
|
528
|
+
.split(/\s+/)
|
|
529
|
+
.map(Number);
|
|
530
|
+
const base = multiply(state.matrix, transform);
|
|
531
|
+
for (let row = firstRow; row <= lastRow; row++) {
|
|
532
|
+
for (let column = firstColumn; column <= lastColumn; column++) {
|
|
533
|
+
const originX = tileX + column * tileWidth;
|
|
534
|
+
const originY = tileY + row * tileHeight;
|
|
535
|
+
const matrix = multiply(base, [1, 0, 0, 1, originX, originY]);
|
|
536
|
+
const contentBox = {
|
|
537
|
+
x: minX - originX,
|
|
538
|
+
y: minY - originY,
|
|
539
|
+
width: maxX - minX,
|
|
540
|
+
height: maxY - minY,
|
|
541
|
+
};
|
|
542
|
+
const span =
|
|
543
|
+
conic.length === 2 && conic.every(Number.isFinite)
|
|
544
|
+
? angularSpan(conic[0] as number, conic[1] as number, contentBox)
|
|
545
|
+
: null;
|
|
546
|
+
const clipped =
|
|
547
|
+
tiles > 1 &&
|
|
548
|
+
this.beginClip(
|
|
549
|
+
[
|
|
550
|
+
{ op: "M", x: 0, y: 0 },
|
|
551
|
+
{ op: "L", x: tileWidth, y: 0 },
|
|
552
|
+
{ op: "L", x: tileWidth, y: tileHeight },
|
|
553
|
+
{ op: "L", x: 0, y: tileHeight },
|
|
554
|
+
{ op: "Z" },
|
|
555
|
+
],
|
|
556
|
+
matrix,
|
|
557
|
+
false
|
|
558
|
+
);
|
|
559
|
+
try {
|
|
560
|
+
const contentState: PaintState = {
|
|
561
|
+
matrix,
|
|
562
|
+
viewport: state.viewport,
|
|
563
|
+
fill: "#000000",
|
|
564
|
+
fillRule: "nonzero",
|
|
565
|
+
stroke: "none",
|
|
566
|
+
strokeWidth: 1,
|
|
567
|
+
fontSize: 16,
|
|
568
|
+
fontWeight: "normal",
|
|
569
|
+
textAnchor: "start",
|
|
570
|
+
fillOpacity: 1,
|
|
571
|
+
opacity: alpha,
|
|
572
|
+
};
|
|
573
|
+
// A fan with one alpha sits in a <g opacity>: look inside it
|
|
574
|
+
const visit = (children: SvgNode[], parent: PaintState) => {
|
|
575
|
+
for (const child of children) {
|
|
576
|
+
if (child.name === "g" && !child.attrs.transform) {
|
|
577
|
+
const own = Number.parseFloat(child.attrs.opacity ?? "1");
|
|
578
|
+
visit(child.children, {
|
|
579
|
+
...parent,
|
|
580
|
+
opacity:
|
|
581
|
+
(parent.opacity ?? 1) * (Number.isFinite(own) ? own : 1),
|
|
582
|
+
});
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
if (span && child.attrs["data-arc"]) {
|
|
586
|
+
const [from, sweep] = child.attrs["data-arc"]
|
|
587
|
+
.split(/\s+/)
|
|
588
|
+
.map(Number) as [number, number];
|
|
589
|
+
if (!arcsOverlap([from, from + sweep], span)) {
|
|
590
|
+
continue;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
this.walk(child, parent);
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
visit(pattern.children, contentState);
|
|
597
|
+
} finally {
|
|
598
|
+
if (clipped) {
|
|
599
|
+
this.endClip();
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
} finally {
|
|
605
|
+
this.inConic = wasConic;
|
|
606
|
+
this.endClip();
|
|
607
|
+
}
|
|
608
|
+
return true;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Paints the output rectangle `box` ([x0, y0, x1, y1] in output
|
|
613
|
+
* coordinates) with a gradient `fill`, placed in the user space of `root`
|
|
614
|
+
* as walked with `state`, so a background gradient continues into the
|
|
615
|
+
* bleed. Returns false (painting nothing) when `fill` is not a gradient
|
|
616
|
+
* this painter shades.
|
|
617
|
+
*/
|
|
618
|
+
paintBleed(
|
|
619
|
+
root: SvgNode,
|
|
620
|
+
state: PaintState,
|
|
621
|
+
fill: string,
|
|
622
|
+
box: [number, number, number, number]
|
|
623
|
+
): boolean {
|
|
624
|
+
if (!this.shadedGradient(fill)) {
|
|
625
|
+
return false;
|
|
626
|
+
}
|
|
627
|
+
const [matrix, viewport] = this.viewportMatrix(
|
|
628
|
+
{ ...root, attrs: { ...root.attrs, x: "0", y: "0" } },
|
|
629
|
+
{
|
|
630
|
+
...state,
|
|
631
|
+
matrix: multiply(state.matrix, parseTransform(root.attrs.transform)),
|
|
632
|
+
}
|
|
633
|
+
);
|
|
634
|
+
const inverse = invertMatrix(matrix);
|
|
635
|
+
if (!inverse) {
|
|
636
|
+
return false;
|
|
637
|
+
}
|
|
638
|
+
const [x0, y0, x1, y1] = box;
|
|
639
|
+
const corners = [
|
|
640
|
+
[x0, y0],
|
|
641
|
+
[x1, y0],
|
|
642
|
+
[x1, y1],
|
|
643
|
+
[x0, y1],
|
|
644
|
+
].map(([x, y]) => applyMatrix(inverse, x as number, y as number));
|
|
645
|
+
const segments: Segment[] = corners.map(([x, y], index) => ({
|
|
646
|
+
op: index === 0 ? "M" : "L",
|
|
647
|
+
x,
|
|
648
|
+
y,
|
|
649
|
+
}));
|
|
650
|
+
segments.push({ op: "Z" });
|
|
651
|
+
this.paint(
|
|
652
|
+
segments,
|
|
653
|
+
{ name: "rect", attrs: {}, children: [], text: "" },
|
|
654
|
+
{
|
|
655
|
+
...state,
|
|
656
|
+
matrix,
|
|
657
|
+
viewport,
|
|
658
|
+
fill,
|
|
659
|
+
fillRule: "nonzero",
|
|
660
|
+
stroke: "none",
|
|
661
|
+
}
|
|
662
|
+
);
|
|
663
|
+
return true;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
abstract paint(segments: Segment[], node: SvgNode, state: PaintState): void;
|
|
667
|
+
|
|
668
|
+
abstract text(node: SvgNode, state: PaintState): void;
|
|
669
|
+
|
|
670
|
+
abstract image(node: SvgNode, state: PaintState): void;
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* Restricts painting to `segments` (device space via `matrix`) until the
|
|
674
|
+
* matching `endClip`. Backends that can't clip leave this as is, so they
|
|
675
|
+
* paint the element unclipped.
|
|
676
|
+
*/
|
|
677
|
+
beginClip(_segments: Segment[], _matrix: Matrix, _evenOdd: boolean): boolean {
|
|
678
|
+
return false;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** Ends the clip a `beginClip` that returned true started. */
|
|
682
|
+
endClip(): void {
|
|
683
|
+
// Nothing to undo when beginClip didn't clip
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* The `clip-path="url(#id)"` outline of `node` (userSpaceOnUse: the
|
|
688
|
+
* node's own user space), or null when it has none or it can't be found.
|
|
689
|
+
*/
|
|
690
|
+
private clipOf(
|
|
691
|
+
node: SvgNode,
|
|
692
|
+
state: PaintState
|
|
693
|
+
): { segments: Segment[]; matrix: Matrix; evenOdd: boolean } | null {
|
|
694
|
+
const reference = readStyle(node, "clip-path");
|
|
695
|
+
const id = reference ? URL_REFERENCE.exec(reference)?.[1] : undefined;
|
|
696
|
+
const clip = id ? this.clipPaths.get(id) : undefined;
|
|
697
|
+
if (!clip) {
|
|
698
|
+
return null;
|
|
699
|
+
}
|
|
700
|
+
const segments: Segment[] = [];
|
|
701
|
+
let evenOdd = false;
|
|
702
|
+
for (const child of clip.children) {
|
|
703
|
+
const shape = clipShapeSegments(child, state.viewport);
|
|
704
|
+
if (shape.length === 0) {
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
const transform = parseTransform(child.attrs.transform);
|
|
708
|
+
const isIdentity =
|
|
709
|
+
transform[0] === 1 &&
|
|
710
|
+
transform[1] === 0 &&
|
|
711
|
+
transform[2] === 0 &&
|
|
712
|
+
transform[3] === 1 &&
|
|
713
|
+
transform[4] === 0 &&
|
|
714
|
+
transform[5] === 0;
|
|
715
|
+
segments.push(
|
|
716
|
+
...(isIdentity
|
|
717
|
+
? shape
|
|
718
|
+
: shape.map((segment) => transformSegment(segment, transform)))
|
|
719
|
+
);
|
|
720
|
+
evenOdd ||= readStyle(child, "clip-rule") === "evenodd";
|
|
721
|
+
}
|
|
722
|
+
return segments.length > 0
|
|
723
|
+
? { segments, matrix: state.matrix, evenOdd }
|
|
724
|
+
: null;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
viewportMatrix(node: SvgNode, state: PaintState): [Matrix, [number, number]] {
|
|
728
|
+
const x = length(node.attrs.x, state.viewport[0]);
|
|
729
|
+
const y = length(node.attrs.y, state.viewport[1]);
|
|
730
|
+
const width = length(
|
|
731
|
+
node.attrs.width,
|
|
732
|
+
state.viewport[0],
|
|
733
|
+
state.viewport[0]
|
|
734
|
+
);
|
|
735
|
+
const height = length(
|
|
736
|
+
node.attrs.height,
|
|
737
|
+
state.viewport[1],
|
|
738
|
+
state.viewport[1]
|
|
739
|
+
);
|
|
740
|
+
const viewBox = parseNumbers(node.attrs.viewBox);
|
|
741
|
+
if (viewBox.length !== 4 || !(viewBox[2] && viewBox[3])) {
|
|
742
|
+
return [multiply(state.matrix, [1, 0, 0, 1, x, y]), [width, height]];
|
|
743
|
+
}
|
|
744
|
+
const [vbX, vbY, vbWidth, vbHeight] = viewBox as [
|
|
745
|
+
number,
|
|
746
|
+
number,
|
|
747
|
+
number,
|
|
748
|
+
number,
|
|
749
|
+
];
|
|
750
|
+
let scaleX = width / vbWidth;
|
|
751
|
+
let scaleY = height / vbHeight;
|
|
752
|
+
let offsetX = x;
|
|
753
|
+
let offsetY = y;
|
|
754
|
+
if ((node.attrs.preserveAspectRatio ?? "").trim() !== "none") {
|
|
755
|
+
const uniform = node.attrs.preserveAspectRatio?.includes("slice")
|
|
756
|
+
? Math.max(scaleX, scaleY)
|
|
757
|
+
: Math.min(scaleX, scaleY);
|
|
758
|
+
offsetX += (width - vbWidth * uniform) / 2;
|
|
759
|
+
offsetY += (height - vbHeight * uniform) / 2;
|
|
760
|
+
scaleX = uniform;
|
|
761
|
+
scaleY = uniform;
|
|
762
|
+
}
|
|
763
|
+
return [
|
|
764
|
+
multiply(state.matrix, [
|
|
765
|
+
scaleX,
|
|
766
|
+
0,
|
|
767
|
+
0,
|
|
768
|
+
scaleY,
|
|
769
|
+
offsetX - vbX * scaleX,
|
|
770
|
+
offsetY - vbY * scaleY,
|
|
771
|
+
]),
|
|
772
|
+
[vbWidth, vbHeight],
|
|
773
|
+
];
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
walk(node: SvgNode, parent: PaintState, isRoot = false) {
|
|
777
|
+
if (isRoot) {
|
|
778
|
+
this.clipPaths = collectClipPaths(node, new Map());
|
|
779
|
+
this.patterns = collectPatterns(node, new Map());
|
|
780
|
+
}
|
|
781
|
+
if (SKIPPED_ELEMENTS.has(node.name)) {
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
if (
|
|
785
|
+
readStyle(node, "display") === "none" ||
|
|
786
|
+
readStyle(node, "visibility") === "hidden"
|
|
787
|
+
) {
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
let matrix = multiply(parent.matrix, parseTransform(node.attrs.transform));
|
|
792
|
+
let viewport = parent.viewport;
|
|
793
|
+
if (node.name === "svg") {
|
|
794
|
+
const [viewportMatrix, size] = this.viewportMatrix(
|
|
795
|
+
isRoot ? { ...node, attrs: { ...node.attrs, x: "0", y: "0" } } : node,
|
|
796
|
+
{ ...parent, matrix }
|
|
797
|
+
);
|
|
798
|
+
matrix = viewportMatrix;
|
|
799
|
+
viewport = size;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
const fontSize = readStyle(node, "font-size");
|
|
803
|
+
const ownOpacity = Number.parseFloat(readStyle(node, "opacity") ?? "1");
|
|
804
|
+
const ownFillOpacity = readStyle(node, "fill-opacity");
|
|
805
|
+
const fillOpacity =
|
|
806
|
+
ownFillOpacity === undefined
|
|
807
|
+
? (parent.fillOpacity ?? 1)
|
|
808
|
+
: Number.parseFloat(ownFillOpacity);
|
|
809
|
+
const state: PaintState = {
|
|
810
|
+
fillOpacity: Number.isFinite(fillOpacity)
|
|
811
|
+
? Math.min(Math.max(fillOpacity, 0), 1)
|
|
812
|
+
: 1,
|
|
813
|
+
opacity:
|
|
814
|
+
(parent.opacity ?? 1) *
|
|
815
|
+
(Number.isFinite(ownOpacity)
|
|
816
|
+
? Math.min(Math.max(ownOpacity, 0), 1)
|
|
817
|
+
: 1),
|
|
818
|
+
matrix,
|
|
819
|
+
viewport,
|
|
820
|
+
fill: readStyle(node, "fill") ?? parent.fill,
|
|
821
|
+
fillRule: readStyle(node, "fill-rule") ?? parent.fillRule,
|
|
822
|
+
stroke: readStyle(node, "stroke") ?? parent.stroke,
|
|
823
|
+
strokeWidth:
|
|
824
|
+
readStyle(node, "stroke-width") === undefined
|
|
825
|
+
? parent.strokeWidth
|
|
826
|
+
: length(readStyle(node, "stroke-width"), viewport[0], 1),
|
|
827
|
+
fontSize:
|
|
828
|
+
fontSize === undefined ? parent.fontSize : length(fontSize, 16, 16),
|
|
829
|
+
fontWeight: readStyle(node, "font-weight") ?? parent.fontWeight,
|
|
830
|
+
textAnchor: readStyle(node, "text-anchor") ?? parent.textAnchor,
|
|
831
|
+
};
|
|
832
|
+
// The code's background sits on paper (see `flatten`); a heart or
|
|
833
|
+
// silhouette code's background is a path
|
|
834
|
+
const wasOnPaper = this.onPaper;
|
|
835
|
+
if (
|
|
836
|
+
(node.name === "rect" ||
|
|
837
|
+
node.name === "circle" ||
|
|
838
|
+
node.name === "path") &&
|
|
839
|
+
BACKGROUND_PAINT.test(readStyle(node, "fill") ?? "")
|
|
840
|
+
) {
|
|
841
|
+
this.onPaper = true;
|
|
842
|
+
}
|
|
843
|
+
try {
|
|
844
|
+
// clip-path: paint the element inside the clip outline only
|
|
845
|
+
const clip = node.attrs["clip-path"] ? this.clipOf(node, state) : null;
|
|
846
|
+
if (clip && this.beginClip(clip.segments, clip.matrix, clip.evenOdd)) {
|
|
847
|
+
try {
|
|
848
|
+
this.paintNode(node, state);
|
|
849
|
+
} finally {
|
|
850
|
+
this.endClip();
|
|
851
|
+
}
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
this.paintNode(node, state);
|
|
855
|
+
} finally {
|
|
856
|
+
this.onPaper = wasOnPaper;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
private paintNode(node: SvgNode, state: PaintState) {
|
|
861
|
+
const [vw, vh] = state.viewport;
|
|
862
|
+
const attr = (name: string, reference: number) =>
|
|
863
|
+
length(node.attrs[name], reference);
|
|
864
|
+
|
|
865
|
+
switch (node.name) {
|
|
866
|
+
case "path":
|
|
867
|
+
this.paint(parsePathData(node.attrs.d ?? ""), node, state);
|
|
868
|
+
return;
|
|
869
|
+
case "rect": {
|
|
870
|
+
const rx = node.attrs.rx ?? node.attrs.ry;
|
|
871
|
+
const ry = node.attrs.ry ?? node.attrs.rx;
|
|
872
|
+
this.paint(
|
|
873
|
+
rectSegments(
|
|
874
|
+
attr("x", vw),
|
|
875
|
+
attr("y", vh),
|
|
876
|
+
attr("width", vw),
|
|
877
|
+
attr("height", vh),
|
|
878
|
+
length(rx, vw),
|
|
879
|
+
length(ry, vh)
|
|
880
|
+
),
|
|
881
|
+
node,
|
|
882
|
+
state
|
|
883
|
+
);
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
case "circle": {
|
|
887
|
+
const r = attr("r", Math.hypot(vw, vh) / Math.SQRT2);
|
|
888
|
+
this.paint(
|
|
889
|
+
ellipseSegments(attr("cx", vw), attr("cy", vh), r, r),
|
|
890
|
+
node,
|
|
891
|
+
state
|
|
892
|
+
);
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
case "ellipse":
|
|
896
|
+
this.paint(
|
|
897
|
+
ellipseSegments(
|
|
898
|
+
attr("cx", vw),
|
|
899
|
+
attr("cy", vh),
|
|
900
|
+
attr("rx", vw),
|
|
901
|
+
attr("ry", vh)
|
|
902
|
+
),
|
|
903
|
+
node,
|
|
904
|
+
state
|
|
905
|
+
);
|
|
906
|
+
return;
|
|
907
|
+
case "line":
|
|
908
|
+
this.paint(
|
|
909
|
+
[
|
|
910
|
+
{ op: "M", x: attr("x1", vw), y: attr("y1", vh) },
|
|
911
|
+
{ op: "L", x: attr("x2", vw), y: attr("y2", vh) },
|
|
912
|
+
],
|
|
913
|
+
node,
|
|
914
|
+
{ ...state, fill: "none" }
|
|
915
|
+
);
|
|
916
|
+
return;
|
|
917
|
+
case "polygon":
|
|
918
|
+
case "polyline": {
|
|
919
|
+
const points = parseNumbers(node.attrs.points);
|
|
920
|
+
const segments: Segment[] = [];
|
|
921
|
+
for (let i = 0; i + 1 < points.length; i += 2) {
|
|
922
|
+
segments.push({
|
|
923
|
+
op: i === 0 ? "M" : "L",
|
|
924
|
+
x: points[i] as number,
|
|
925
|
+
y: points[i + 1] as number,
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
if (node.name === "polygon" && segments.length > 0) {
|
|
929
|
+
segments.push({ op: "Z" });
|
|
930
|
+
}
|
|
931
|
+
this.paint(segments, node, state);
|
|
932
|
+
return;
|
|
933
|
+
}
|
|
934
|
+
case "text":
|
|
935
|
+
this.text(node, state);
|
|
936
|
+
return;
|
|
937
|
+
case "image":
|
|
938
|
+
this.image(node, state);
|
|
939
|
+
return;
|
|
940
|
+
default:
|
|
941
|
+
for (const child of node.children) {
|
|
942
|
+
this.walk(child, state);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
}
|