qrdx 0.0.45 → 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 +63 -20
- 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 -4231
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: SVG gradient geometry */
|
|
2
|
+
// SVG <linearGradient> / <radialGradient> for the vector exporters. Both PDF
|
|
3
|
+
// (ShadingType 2/3 painted with `sh`) and PostScript LanguageLevel 3 (`shfill`)
|
|
4
|
+
// shade between two points or two circles in their own coordinate space, so
|
|
5
|
+
// a gradient is written once in gradient space and placed on each shape with
|
|
6
|
+
// a matrix, which keeps gradientUnits, gradientTransform and per-shape
|
|
7
|
+
// transforms exact.
|
|
8
|
+
import { fmt, roundTo, SCALE_DECIMALS } from "./fmt";
|
|
9
|
+
import { parseColor, type RGBAColor, rgbToCmyk } from "./parse-color";
|
|
10
|
+
import {
|
|
11
|
+
IDENTITY,
|
|
12
|
+
type Matrix,
|
|
13
|
+
multiply,
|
|
14
|
+
parseTransform,
|
|
15
|
+
type Segment,
|
|
16
|
+
type SvgNode,
|
|
17
|
+
} from "./svg-geometry";
|
|
18
|
+
|
|
19
|
+
export interface ShadingStop {
|
|
20
|
+
/** 0–1, never below the previous stop's offset */
|
|
21
|
+
offset: number;
|
|
22
|
+
color: RGBAColor;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A gradient element with the attributes and stops it inherits via `href`. */
|
|
26
|
+
export interface SvgGradient {
|
|
27
|
+
kind: "linear" | "radial";
|
|
28
|
+
attrs: Record<string, string>;
|
|
29
|
+
stops: ShadingStop[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A gradient placed on one shape. */
|
|
33
|
+
export interface PlacedGradient {
|
|
34
|
+
kind: "linear" | "radial";
|
|
35
|
+
/**
|
|
36
|
+
* Shading `Coords` in gradient space: `[x1 y1 x2 y2]` for linear,
|
|
37
|
+
* `[fx fy fr cx cy r]` (focal circle to end circle) for radial.
|
|
38
|
+
*/
|
|
39
|
+
coords: number[];
|
|
40
|
+
/** Gradient space → the shape's user space */
|
|
41
|
+
matrix: Matrix;
|
|
42
|
+
stops: ShadingStop[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type ShadingColorSpace = "rgb" | "cmyk";
|
|
46
|
+
|
|
47
|
+
const SHARED_ATTRIBUTES = [
|
|
48
|
+
"gradientUnits",
|
|
49
|
+
"gradientTransform",
|
|
50
|
+
"spreadMethod",
|
|
51
|
+
];
|
|
52
|
+
const GEOMETRY_ATTRIBUTES = {
|
|
53
|
+
linear: ["x1", "y1", "x2", "y2"],
|
|
54
|
+
radial: ["cx", "cy", "r", "fx", "fy", "fr"],
|
|
55
|
+
} as const;
|
|
56
|
+
|
|
57
|
+
const BLACK: RGBAColor = { r: 0, g: 0, b: 0, a: 1 };
|
|
58
|
+
|
|
59
|
+
function presentation(node: SvgNode, name: string): string | undefined {
|
|
60
|
+
for (const declaration of (node.attrs.style ?? "").split(";")) {
|
|
61
|
+
const [key, ...rest] = declaration.split(":");
|
|
62
|
+
if (key?.trim() === name) {
|
|
63
|
+
return rest.join(":").trim();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return node.attrs[name];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function gradientKind(node: SvgNode): SvgGradient["kind"] | null {
|
|
70
|
+
if (node.name === "linearGradient") {
|
|
71
|
+
return "linear";
|
|
72
|
+
}
|
|
73
|
+
return node.name === "radialGradient" ? "radial" : null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function readStops(node: SvgNode): ShadingStop[] {
|
|
77
|
+
const stops: ShadingStop[] = [];
|
|
78
|
+
let previous = 0;
|
|
79
|
+
for (const child of node.children) {
|
|
80
|
+
if (child.name !== "stop") {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const raw = (child.attrs.offset ?? "0").trim();
|
|
84
|
+
const number = Number.parseFloat(raw);
|
|
85
|
+
const value = Number.isFinite(number)
|
|
86
|
+
? raw.endsWith("%")
|
|
87
|
+
? number / 100
|
|
88
|
+
: number
|
|
89
|
+
: 0;
|
|
90
|
+
// SVG clamps offsets to 0–1 and never lets one go below the one before.
|
|
91
|
+
previous = Math.max(previous, Math.min(Math.max(value, 0), 1));
|
|
92
|
+
const color = presentation(child, "stop-color") ?? "#000000";
|
|
93
|
+
const parsed =
|
|
94
|
+
parseColor(color === "currentColor" ? "#000000" : color) ?? BLACK;
|
|
95
|
+
const opacity = Number.parseFloat(
|
|
96
|
+
presentation(child, "stop-opacity") ?? "1"
|
|
97
|
+
);
|
|
98
|
+
stops.push({
|
|
99
|
+
offset: previous,
|
|
100
|
+
color: Number.isFinite(opacity)
|
|
101
|
+
? { ...parsed, a: parsed.a * Math.min(Math.max(opacity, 0), 1) }
|
|
102
|
+
: parsed,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return stops;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Every gradient in the tree by id, with `href` inheritance resolved. */
|
|
109
|
+
export function collectGradients(root: SvgNode): Map<string, SvgGradient> {
|
|
110
|
+
const elements = new Map<string, SvgNode>();
|
|
111
|
+
const visit = (node: SvgNode) => {
|
|
112
|
+
const id = node.attrs.id;
|
|
113
|
+
if (gradientKind(node) && id && !elements.has(id)) {
|
|
114
|
+
elements.set(id, node);
|
|
115
|
+
}
|
|
116
|
+
for (const child of node.children) {
|
|
117
|
+
visit(child);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
visit(root);
|
|
121
|
+
|
|
122
|
+
const gradients = new Map<string, SvgGradient>();
|
|
123
|
+
for (const [id, element] of elements) {
|
|
124
|
+
const kind = gradientKind(element) as SvgGradient["kind"];
|
|
125
|
+
const attrs: Record<string, string> = {};
|
|
126
|
+
let stops: ShadingStop[] = [];
|
|
127
|
+
const seen = new Set<SvgNode>();
|
|
128
|
+
let current: SvgNode | undefined = element;
|
|
129
|
+
while (current && !seen.has(current)) {
|
|
130
|
+
seen.add(current);
|
|
131
|
+
const names =
|
|
132
|
+
gradientKind(current) === kind
|
|
133
|
+
? [...SHARED_ATTRIBUTES, ...GEOMETRY_ATTRIBUTES[kind]]
|
|
134
|
+
: SHARED_ATTRIBUTES;
|
|
135
|
+
for (const name of names) {
|
|
136
|
+
const value = current.attrs[name];
|
|
137
|
+
if (attrs[name] === undefined && value !== undefined) {
|
|
138
|
+
attrs[name] = value;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (stops.length === 0) {
|
|
142
|
+
stops = readStops(current);
|
|
143
|
+
}
|
|
144
|
+
const href: string | undefined =
|
|
145
|
+
current.attrs.href ?? current.attrs["xlink:href"];
|
|
146
|
+
current = href?.startsWith("#") ? elements.get(href.slice(1)) : undefined;
|
|
147
|
+
}
|
|
148
|
+
gradients.set(id, { kind, attrs, stops });
|
|
149
|
+
}
|
|
150
|
+
return gradients;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Tight bounding box of segments (cubic extrema included). */
|
|
154
|
+
export function boundingBox(segments: Segment[]) {
|
|
155
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
156
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
157
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
158
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
159
|
+
const add = (x: number, y: number) => {
|
|
160
|
+
minX = Math.min(minX, x);
|
|
161
|
+
maxX = Math.max(maxX, x);
|
|
162
|
+
minY = Math.min(minY, y);
|
|
163
|
+
maxY = Math.max(maxY, y);
|
|
164
|
+
};
|
|
165
|
+
// Parameters in (0, 1) where one axis of a cubic turns.
|
|
166
|
+
const extrema = (p0: number, p1: number, p2: number, p3: number) => {
|
|
167
|
+
const a = -p0 + 3 * p1 - 3 * p2 + p3;
|
|
168
|
+
const b = 2 * (p0 - 2 * p1 + p2);
|
|
169
|
+
const c = p1 - p0;
|
|
170
|
+
if (Math.abs(a) < 1e-12) {
|
|
171
|
+
return Math.abs(b) < 1e-12 ? [] : [-c / b];
|
|
172
|
+
}
|
|
173
|
+
const discriminant = b * b - 4 * a * c;
|
|
174
|
+
if (discriminant < 0) {
|
|
175
|
+
return [];
|
|
176
|
+
}
|
|
177
|
+
const root = Math.sqrt(discriminant);
|
|
178
|
+
return [(-b + root) / (2 * a), (-b - root) / (2 * a)];
|
|
179
|
+
};
|
|
180
|
+
const at = (t: number, p0: number, p1: number, p2: number, p3: number) => {
|
|
181
|
+
const u = 1 - t;
|
|
182
|
+
return (
|
|
183
|
+
u * u * u * p0 + 3 * u * u * t * p1 + 3 * u * t * t * p2 + t ** 3 * p3
|
|
184
|
+
);
|
|
185
|
+
};
|
|
186
|
+
let x = 0;
|
|
187
|
+
let y = 0;
|
|
188
|
+
let start: [number, number] = [0, 0];
|
|
189
|
+
for (const segment of segments) {
|
|
190
|
+
if (segment.op === "M") {
|
|
191
|
+
start = [segment.x, segment.y];
|
|
192
|
+
}
|
|
193
|
+
if (segment.op === "M" || segment.op === "L") {
|
|
194
|
+
add(segment.x, segment.y);
|
|
195
|
+
x = segment.x;
|
|
196
|
+
y = segment.y;
|
|
197
|
+
} else if (segment.op === "C") {
|
|
198
|
+
add(segment.x, segment.y);
|
|
199
|
+
for (const t of [
|
|
200
|
+
...extrema(x, segment.x1, segment.x2, segment.x),
|
|
201
|
+
...extrema(y, segment.y1, segment.y2, segment.y),
|
|
202
|
+
]) {
|
|
203
|
+
if (t > 0 && t < 1) {
|
|
204
|
+
add(
|
|
205
|
+
at(t, x, segment.x1, segment.x2, segment.x),
|
|
206
|
+
at(t, y, segment.y1, segment.y2, segment.y)
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
x = segment.x;
|
|
211
|
+
y = segment.y;
|
|
212
|
+
} else {
|
|
213
|
+
[x, y] = start;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return Number.isFinite(minX)
|
|
217
|
+
? { x: minX, y: minY, width: maxX - minX, height: maxY - minY }
|
|
218
|
+
: null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const determinant = (m: Matrix) => m[0] * m[3] - m[1] * m[2];
|
|
222
|
+
|
|
223
|
+
/** Inverse of an affine matrix, or null when it is singular. */
|
|
224
|
+
export function invertMatrix(m: Matrix): Matrix | null {
|
|
225
|
+
const det = determinant(m);
|
|
226
|
+
if (!Number.isFinite(det) || Math.abs(det) < 1e-12) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
const [a, b, c, d, e, f] = m;
|
|
230
|
+
return [
|
|
231
|
+
d / det,
|
|
232
|
+
-b / det,
|
|
233
|
+
-c / det,
|
|
234
|
+
a / det,
|
|
235
|
+
(c * f - d * e) / det,
|
|
236
|
+
(b * e - a * f) / det,
|
|
237
|
+
];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Places a gradient on a shape (segments in the shape's user space, `viewport`
|
|
242
|
+
* for percentage lengths). Returns the solid color SVG paints instead when the
|
|
243
|
+
* gradient is degenerate (one stop, a zero-length vector, a zero radius), or
|
|
244
|
+
* null when the shape gets no paint (no stops, all stops transparent, a
|
|
245
|
+
* bounding box with no width or height).
|
|
246
|
+
*
|
|
247
|
+
* `spreadMethod` reflect/repeat are drawn as pad, and stop opacity is ignored
|
|
248
|
+
* (PostScript has no transparency; the PDF stays consistent with it).
|
|
249
|
+
*/
|
|
250
|
+
export function placeGradient(
|
|
251
|
+
gradient: SvgGradient,
|
|
252
|
+
segments: Segment[],
|
|
253
|
+
viewport: [number, number]
|
|
254
|
+
): PlacedGradient | RGBAColor | null {
|
|
255
|
+
const { kind, attrs, stops } = gradient;
|
|
256
|
+
const first = stops[0];
|
|
257
|
+
const last = stops.at(-1);
|
|
258
|
+
if (!(first && last) || stops.every((stop) => stop.color.a === 0)) {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
if (stops.length === 1) {
|
|
262
|
+
return first.color;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const userSpace = attrs.gradientUnits === "userSpaceOnUse";
|
|
266
|
+
let base: Matrix = IDENTITY;
|
|
267
|
+
if (!userSpace) {
|
|
268
|
+
const box = boundingBox(segments);
|
|
269
|
+
if (!box || box.width <= 0 || box.height <= 0) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
base = [box.width, 0, 0, box.height, box.x, box.y];
|
|
273
|
+
}
|
|
274
|
+
const matrix = multiply(base, parseTransform(attrs.gradientTransform));
|
|
275
|
+
if (!invertMatrix(matrix)) {
|
|
276
|
+
return last.color;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const [width, height] = viewport;
|
|
280
|
+
const diagonal = Math.sqrt((width * width + height * height) / 2);
|
|
281
|
+
// Percentages resolve against the viewport in user space and are plain
|
|
282
|
+
// fractions of the bounding box otherwise.
|
|
283
|
+
const parse = (raw: string, reference: number) => {
|
|
284
|
+
const number = Number.parseFloat(raw);
|
|
285
|
+
if (!Number.isFinite(number)) {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
return raw.trim().endsWith("%")
|
|
289
|
+
? (number / 100) * (userSpace ? reference : 1)
|
|
290
|
+
: number;
|
|
291
|
+
};
|
|
292
|
+
const length = (name: string, fallback: string, reference: number) =>
|
|
293
|
+
parse(attrs[name] ?? fallback, reference) ??
|
|
294
|
+
parse(fallback, reference) ??
|
|
295
|
+
0;
|
|
296
|
+
|
|
297
|
+
if (kind === "linear") {
|
|
298
|
+
const coords = [
|
|
299
|
+
length("x1", "0%", width),
|
|
300
|
+
length("y1", "0%", height),
|
|
301
|
+
length("x2", "100%", width),
|
|
302
|
+
length("y2", "0%", height),
|
|
303
|
+
];
|
|
304
|
+
const [x1, y1, x2, y2] = coords as [number, number, number, number];
|
|
305
|
+
if (x1 === x2 && y1 === y2) {
|
|
306
|
+
return last.color;
|
|
307
|
+
}
|
|
308
|
+
return { kind, coords, matrix, stops };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const cx = length("cx", "50%", width);
|
|
312
|
+
const cy = length("cy", "50%", height);
|
|
313
|
+
const r = length("r", "50%", diagonal);
|
|
314
|
+
const fx = attrs.fx === undefined ? cx : length("fx", "50%", width);
|
|
315
|
+
const fy = attrs.fy === undefined ? cy : length("fy", "50%", height);
|
|
316
|
+
const fr = Math.max(0, length("fr", "0%", diagonal));
|
|
317
|
+
if (r < 0) {
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
if (r === 0) {
|
|
321
|
+
return last.color;
|
|
322
|
+
}
|
|
323
|
+
return { kind, coords: [fx, fy, fr, cx, cy, r], matrix, stops };
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** Shading matrix for PDF and PostScript (6 decimals: it scales). */
|
|
327
|
+
export function formatMatrix(matrix: Matrix): string {
|
|
328
|
+
return matrix.map((value) => fmt(value, SCALE_DECIMALS)).join(" ");
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function components(color: RGBAColor, space: ShadingColorSpace): string {
|
|
332
|
+
const values =
|
|
333
|
+
space === "cmyk"
|
|
334
|
+
? rgbToCmyk(color.r, color.g, color.b)
|
|
335
|
+
: [color.r / 255, color.g / 255, color.b / 255];
|
|
336
|
+
return values.map((value) => fmt(value, 4)).join(" ");
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const exponential = (c0: string, c1: string) =>
|
|
340
|
+
`<< /FunctionType 2 /Domain [0 1] /C0 [${c0}] /C1 [${c1}] /N 1 >>`;
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Color function over t in [0, 1] for the stops: the first color up to the
|
|
344
|
+
* first stop, linear blends between stops (equal offsets make a hard edge),
|
|
345
|
+
* the last color after the last stop. One piece is a Type 2 (exponential)
|
|
346
|
+
* function; several are stitched with a Type 3 function.
|
|
347
|
+
*/
|
|
348
|
+
function stopFunction(stops: ShadingStop[], space: ShadingColorSpace) {
|
|
349
|
+
const pieces: { from: number; to: number; c0: string; c1: string }[] = [];
|
|
350
|
+
const offsets = stops.map((stop) => roundTo(stop.offset, 4));
|
|
351
|
+
const colors = stops.map((stop) => components(stop.color, space));
|
|
352
|
+
const firstOffset = offsets[0] as number;
|
|
353
|
+
const lastOffset = offsets.at(-1) as number;
|
|
354
|
+
if (firstOffset > 0) {
|
|
355
|
+
const color = colors[0] as string;
|
|
356
|
+
pieces.push({ from: 0, to: firstOffset, c0: color, c1: color });
|
|
357
|
+
}
|
|
358
|
+
for (let i = 0; i + 1 < stops.length; i++) {
|
|
359
|
+
const from = offsets[i] as number;
|
|
360
|
+
const to = offsets[i + 1] as number;
|
|
361
|
+
if (to > from) {
|
|
362
|
+
pieces.push({
|
|
363
|
+
from,
|
|
364
|
+
to,
|
|
365
|
+
c0: colors[i] as string,
|
|
366
|
+
c1: colors[i + 1] as string,
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
if (lastOffset < 1) {
|
|
371
|
+
const color = colors.at(-1) as string;
|
|
372
|
+
pieces.push({ from: lastOffset, to: 1, c0: color, c1: color });
|
|
373
|
+
}
|
|
374
|
+
if (pieces.length === 1) {
|
|
375
|
+
const [piece] = pieces as [(typeof pieces)[number]];
|
|
376
|
+
return exponential(piece.c0, piece.c1);
|
|
377
|
+
}
|
|
378
|
+
return [
|
|
379
|
+
"<< /FunctionType 3 /Domain [0 1]",
|
|
380
|
+
`/Functions [${pieces.map((piece) => exponential(piece.c0, piece.c1)).join(" ")}]`,
|
|
381
|
+
`/Bounds [${pieces
|
|
382
|
+
.slice(1)
|
|
383
|
+
.map((piece) => fmt(piece.from, 4))
|
|
384
|
+
.join(" ")}]`,
|
|
385
|
+
`/Encode [${pieces.map(() => "0 1").join(" ")}] >>`,
|
|
386
|
+
].join(" ");
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Shading dictionary for a placed gradient, valid both as a PDF object and as
|
|
391
|
+
* PostScript LanguageLevel 3 `shfill` operand. Its `Coords` are in gradient
|
|
392
|
+
* space: concatenate `gradient.matrix` (and the shape's matrix) first.
|
|
393
|
+
* Colors use the same space as the writer's solid fills (DeviceCMYK through
|
|
394
|
+
* `rgbToCmyk` in "cmyk" mode), so the blend runs between the converted stops.
|
|
395
|
+
*/
|
|
396
|
+
export function shadingDictionary(
|
|
397
|
+
gradient: PlacedGradient,
|
|
398
|
+
space: ShadingColorSpace
|
|
399
|
+
): string {
|
|
400
|
+
return [
|
|
401
|
+
`<< /ShadingType ${gradient.kind === "linear" ? 2 : 3}`,
|
|
402
|
+
`/ColorSpace /Device${space === "cmyk" ? "CMYK" : "RGB"}`,
|
|
403
|
+
`/Coords [${gradient.coords.map((value) => fmt(value, SCALE_DECIMALS)).join(" ")}]`,
|
|
404
|
+
`/Function ${stopFunction(gradient.stops, space)}`,
|
|
405
|
+
"/Extend [true true] >>",
|
|
406
|
+
].join(" ");
|
|
407
|
+
}
|