figdriver 0.0.0-stage → 0.4.0
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/CHANGELOG.md +86 -0
- package/LICENSE +21 -0
- package/README.md +87 -2
- package/cli/result.ts +231 -0
- package/dist/cli.js +4125 -0
- package/index.d.ts +12 -0
- package/index.js +8 -0
- package/package.json +78 -4
- package/src/core/color.ts +435 -0
- package/src/core/css.ts +668 -0
- package/src/core/foundations.ts +389 -0
- package/src/core/json.d.ts +4 -0
- package/src/core/layout.ts +684 -0
- package/src/core/naming.ts +129 -0
- package/src/core/protocol.ts +328 -0
- package/src/core/rerun.ts +130 -0
- package/src/core/reuse.ts +149 -0
- package/src/core/snapshot.ts +88 -0
- package/src/core/spec.ts +167 -0
- package/src/core/text.ts +37 -0
- package/src/core/tokens.ts +77 -0
- package/src/core/types.ts +181 -0
- package/src/core/users.ts +17 -0
- package/src/figma/helpers.ts +238 -0
- package/src/figma/import.ts +962 -0
- package/src/figma/inspect.ts +254 -0
- package/src/figma/kit.ts +562 -0
- package/src/figma/migrate.ts +301 -0
- package/src/figma/owned.ts +56 -0
- package/src/figma/read.ts +618 -0
- package/src/figma/rerun.ts +387 -0
- package/src/figma/review.ts +481 -0
- package/src/figma/tokens.ts +97 -0
- package/src/page/capture.ts +999 -0
package/src/core/css.ts
ADDED
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
import { blend, colorOf, methodOf, srgbOf, type Color, type Method, type RGBA } from "./color";
|
|
2
|
+
import type { El } from "./types";
|
|
3
|
+
|
|
4
|
+
export type RGB = { r: number; g: number; b: number };
|
|
5
|
+
|
|
6
|
+
export type { RGBA };
|
|
7
|
+
|
|
8
|
+
type Transform = [[number, number, number], [number, number, number]];
|
|
9
|
+
|
|
10
|
+
type Stop = { position: number; color: RGBA };
|
|
11
|
+
|
|
12
|
+
export type LinearGradient = { type: "GRADIENT_LINEAR"; gradientTransform: Transform; gradientStops: Stop[] };
|
|
13
|
+
|
|
14
|
+
export type RadialGradient = { type: "GRADIENT_RADIAL"; gradientTransform: Transform; gradientStops: Stop[] };
|
|
15
|
+
|
|
16
|
+
export type AngularGradient = { type: "GRADIENT_ANGULAR"; gradientTransform: Transform; gradientStops: Stop[] };
|
|
17
|
+
|
|
18
|
+
export type GradientPaintSpec = LinearGradient | RadialGradient | AngularGradient;
|
|
19
|
+
|
|
20
|
+
export const rgb = (hex: string): RGB => ({
|
|
21
|
+
r: parseInt(hex.slice(1, 3), 16) / 255,
|
|
22
|
+
g: parseInt(hex.slice(3, 5), 16) / 255,
|
|
23
|
+
b: parseInt(hex.slice(5, 7), 16) / 255,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export const hexOf = (c: RGB) =>
|
|
27
|
+
`#${[c.r, c.g, c.b]
|
|
28
|
+
.map((v) =>
|
|
29
|
+
Math.round(v * 255)
|
|
30
|
+
.toString(16)
|
|
31
|
+
.padStart(2, "0"),
|
|
32
|
+
)
|
|
33
|
+
.join("")}`;
|
|
34
|
+
|
|
35
|
+
/** A CSS color as gamma-encoded sRGB, clipped to the sRGB gamut as Chromium draws it on an sRGB display. */
|
|
36
|
+
export function parseColor(css: string): RGBA | undefined {
|
|
37
|
+
const c = colorOf(css);
|
|
38
|
+
|
|
39
|
+
return c && srgbOf(c);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const cssSrgb = (c: RGBA) =>
|
|
43
|
+
c.a === 1 ? hexOf(c) : `rgba(${Math.round(c.r * 255)}, ${Math.round(c.g * 255)}, ${Math.round(c.b * 255)}, ${c.a})`;
|
|
44
|
+
|
|
45
|
+
/** Replaces CSS Color 4 paint attributes in SVG markup with sRGB so Figma's SVG parser keeps their colors. */
|
|
46
|
+
export const svgWithSrgbColors = (svg: string) =>
|
|
47
|
+
svg.replace(/(\b(?:fill|stroke|stop-color|color)=")([^"]+)(")/g, (match, open, value, close) => {
|
|
48
|
+
if (/^(?:none|transparent|currentColor|url\(|#|rgba?\()/i.test(value)) return match;
|
|
49
|
+
const c = parseColor(value);
|
|
50
|
+
|
|
51
|
+
return c ? `${open}${cssSrgb(c)}${close}` : match;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export function splitTop(s: string) {
|
|
55
|
+
const out: string[] = [];
|
|
56
|
+
|
|
57
|
+
let depth = 0,
|
|
58
|
+
from = 0;
|
|
59
|
+
|
|
60
|
+
for (let i = 0; i < s.length; i++) {
|
|
61
|
+
if (s[i] === "(") depth++;
|
|
62
|
+
else if (s[i] === ")") depth--;
|
|
63
|
+
else if (s[i] === "," && depth === 0) {
|
|
64
|
+
out.push(s.slice(from, i).trim());
|
|
65
|
+
from = i + 1;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
out.push(s.slice(from).trim());
|
|
70
|
+
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const SIDES = new Map(Object.entries({ top: 0, right: 90, bottom: 180, left: 270 }));
|
|
75
|
+
const UNITS = new Map(Object.entries({ deg: 1, grad: 0.9, rad: 180 / Math.PI, turn: 360 }));
|
|
76
|
+
const NUMBER = "-?(?:\\d+\\.?\\d*|\\.\\d+)";
|
|
77
|
+
const ANGLE_RE = new RegExp(`^(${NUMBER})([a-z]+)$`);
|
|
78
|
+
const LENGTH_RE = new RegExp(`^(${NUMBER})(%|px)$`);
|
|
79
|
+
const CONIC_POS_RE = new RegExp(`^(${NUMBER})(%|deg|turn|grad|rad)$`);
|
|
80
|
+
const SHADOW_LEN_RE = new RegExp(`^${NUMBER}(px)?$`);
|
|
81
|
+
|
|
82
|
+
/** A gradient's direction in degrees. A corner keyword's angle depends on the box, so its 50% line joins the other two corners. */
|
|
83
|
+
function angleOf(arg: string, w: number, h: number) {
|
|
84
|
+
const [, n, unit = ""] = arg.match(ANGLE_RE) ?? [];
|
|
85
|
+
const scale = UNITS.get(unit);
|
|
86
|
+
|
|
87
|
+
if (scale !== undefined) return parseFloat(n) * scale;
|
|
88
|
+
const sides = arg.match(/^to (\w+)(?: (\w+))?$/);
|
|
89
|
+
|
|
90
|
+
if (!sides) return;
|
|
91
|
+
|
|
92
|
+
if (!sides[2]) return SIDES.get(sides[1]);
|
|
93
|
+
|
|
94
|
+
const x = sides.includes("right") ? 1 : -1,
|
|
95
|
+
y = sides.includes("bottom") ? 1 : -1;
|
|
96
|
+
|
|
97
|
+
return (Math.atan2(x * h, -y * w) * 180) / Math.PI;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** A gradient's color stops along its line, where 0 and 1 are its ends, and the color hint after each stop, if any. */
|
|
101
|
+
type Line = { stops: { at: number; color: Color }[]; hints: (number | undefined)[]; legacy: boolean };
|
|
102
|
+
|
|
103
|
+
const LEGACY_COLOR = /^(rgba?\(|hsla?\(|#|transparent$)/i;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Resolves stop and hint positions as CSS does. `positionOf` reads one position, such as `40%` or `12px`, and gives nothing for one
|
|
107
|
+
* it cannot read, which leaves the whole gradient out instead of drawing a wrong one.
|
|
108
|
+
*/
|
|
109
|
+
function lineOf(args: string[], positionOf: (v: string) => number | undefined): Line | undefined {
|
|
110
|
+
const entries: { color?: Color; at?: number }[] = [];
|
|
111
|
+
let legacy = true;
|
|
112
|
+
|
|
113
|
+
for (const arg of args) {
|
|
114
|
+
const [, css = "", rest = ""] = arg.match(/^([\w-]+\([^)]*\)|\S+)\s*(.*)$/) ?? [];
|
|
115
|
+
const at = rest.split(/\s+/).filter(Boolean);
|
|
116
|
+
const color = colorOf(css);
|
|
117
|
+
const positions = at.map(positionOf);
|
|
118
|
+
|
|
119
|
+
if (!color && !at.length) {
|
|
120
|
+
const hint = positionOf(css);
|
|
121
|
+
|
|
122
|
+
if (hint === undefined) return;
|
|
123
|
+
entries.push({ at: hint });
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!color || positions.includes(undefined) || positions.length > 2) return;
|
|
128
|
+
legacy &&= LEGACY_COLOR.test(css);
|
|
129
|
+
|
|
130
|
+
if (!positions.length) entries.push({ color });
|
|
131
|
+
|
|
132
|
+
for (const p of positions) entries.push({ color, at: p });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// A hint sits between two stops.
|
|
136
|
+
if (entries.some((e, i) => !e.color && !(entries[i - 1]?.color && entries[i + 1]?.color))) return;
|
|
137
|
+
const stops = entries.filter((e) => e.color);
|
|
138
|
+
|
|
139
|
+
if (stops.length < 2) return;
|
|
140
|
+
stops[0].at ??= 0;
|
|
141
|
+
stops[stops.length - 1].at ??= 1;
|
|
142
|
+
// A stop or hint placed before an earlier one sits on it.
|
|
143
|
+
let floor = -Infinity;
|
|
144
|
+
|
|
145
|
+
for (const e of entries) if (e.at !== undefined) floor = e.at = Math.max(e.at, floor);
|
|
146
|
+
stops.forEach((s, i) => {
|
|
147
|
+
if (s.at !== undefined) return;
|
|
148
|
+
const next = stops.findIndex((n, j) => j > i && n.at !== undefined);
|
|
149
|
+
s.at = stops[i - 1].at! + (stops[next].at! - stops[i - 1].at!) / (next - i + 1);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const hints = stops.map((s) => entries[entries.indexOf(s) + 1]).map((e) => (e && !e.color ? e.at : undefined));
|
|
153
|
+
|
|
154
|
+
return { stops: stops.map((s) => ({ at: s.at!, color: s.color! })), hints, legacy };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The stops with each color hint replaced as Chromium replaces it: by nine stops on the CSS hint curve, packed toward the hint, so
|
|
159
|
+
* the blend between them is straight. A hint at the midpoint changes nothing, and one on a stop takes that stop's far side.
|
|
160
|
+
*/
|
|
161
|
+
function hintless({ stops, hints }: Line, method: Method): Line["stops"] {
|
|
162
|
+
return stops.flatMap((s, i) => {
|
|
163
|
+
const hint = hints[i],
|
|
164
|
+
next = stops[i + 1];
|
|
165
|
+
|
|
166
|
+
if (hint === undefined || !next) return [s];
|
|
167
|
+
const [left, right, total] = [hint - s.at, next.at - hint, next.at - s.at];
|
|
168
|
+
|
|
169
|
+
if (left === right) return [s];
|
|
170
|
+
|
|
171
|
+
if (left === 0 || right === 0) return [s, { at: hint, color: left === 0 ? next.color : s.color }];
|
|
172
|
+
|
|
173
|
+
const at =
|
|
174
|
+
left > right
|
|
175
|
+
? [7, 8, 9, 10, 11, 12, 13]
|
|
176
|
+
.map((k) => s.at + (left * k) / 13)
|
|
177
|
+
.concat([hint + right / 3, hint + (right * 2) / 3])
|
|
178
|
+
: [s.at + left / 3, s.at + (left * 2) / 3].concat([0, 1, 2, 3, 4, 5, 6].map((k) => hint + (right * k) / 13));
|
|
179
|
+
|
|
180
|
+
const mix = blend(s.color, next.color, method);
|
|
181
|
+
|
|
182
|
+
return [
|
|
183
|
+
s,
|
|
184
|
+
...at.map((x) => ({ at: x, color: mix(((x - s.at) / total) ** (Math.log(0.5) / Math.log(left / total))) })),
|
|
185
|
+
];
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Figma blends neighboring stops in straight, gamma-encoded sRGB. A CSS gradient blends elsewhere, so Figma gets extra stops until
|
|
190
|
+
// every point of the gradient lands within one 8-bit step of the CSS color, premultiplied by alpha.
|
|
191
|
+
const TOLERANCE = 1 / 255;
|
|
192
|
+
const MAX_DEPTH = 10;
|
|
193
|
+
const MAX_MARKS = 1024;
|
|
194
|
+
const MAX_STOPS = 1024;
|
|
195
|
+
const PROBES = [1 / 32, 1 / 8, 0.25, 0.5, 0.75, 7 / 8, 31 / 32];
|
|
196
|
+
|
|
197
|
+
const off = (exact: RGBA, from: RGBA, to: RGBA, t: number) => {
|
|
198
|
+
const mix = (k: keyof RGBA) => from[k] + (to[k] - from[k]) * t;
|
|
199
|
+
const alpha = mix("a");
|
|
200
|
+
|
|
201
|
+
return Math.max(
|
|
202
|
+
Math.abs(alpha - exact.a),
|
|
203
|
+
...(["r", "g", "b"] as const).map((k) => Math.abs(mix(k) * alpha - exact[k] * exact.a)),
|
|
204
|
+
);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const sameColor = (a: RGBA, b: RGBA) => a.r === b.r && a.g === b.g && a.b === b.b && a.a === b.a;
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Figma stops that draw `line` over `[0, extent]` of the gradient as CSS draws it: blended by `method`, eased by hints, repeated for
|
|
211
|
+
* a `repeating-` gradient, and sharp where two stops share a position. Positions are fractions of `extent`.
|
|
212
|
+
*/
|
|
213
|
+
function figmaStops(line: Line, method: Method, repeating: boolean, extent = 1): Stop[] | undefined {
|
|
214
|
+
const stops = hintless(line, method);
|
|
215
|
+
const [first, last] = [stops[0].at, stops[stops.length - 1].at];
|
|
216
|
+
const period = last - first;
|
|
217
|
+
|
|
218
|
+
if (repeating && period <= 0) return;
|
|
219
|
+
const blends = stops.slice(1).map((s, i) => blend(stops[i].color, s.color, method));
|
|
220
|
+
const shifts = repeating ? Math.ceil((extent - first) / period) - Math.floor(-last / period) + 1 : 1;
|
|
221
|
+
|
|
222
|
+
if (shifts * stops.length > MAX_MARKS) return;
|
|
223
|
+
const marks = new Set([0, extent]);
|
|
224
|
+
|
|
225
|
+
for (let k = repeating ? Math.floor(-last / period) : 0, n = 0; n < shifts; k++, n++)
|
|
226
|
+
for (const s of stops) if (s.at + k * period > 0 && s.at + k * period < extent) marks.add(s.at + k * period);
|
|
227
|
+
|
|
228
|
+
/** The color function of the stretch of gradient around `x`, which no stop interrupts. */
|
|
229
|
+
const pieceAt = (x: number): ((at: number) => Color) => {
|
|
230
|
+
const shift = repeating ? Math.floor((x - first) / period) * period : 0;
|
|
231
|
+
const local = x - shift;
|
|
232
|
+
|
|
233
|
+
if (local <= first) return () => stops[0].color;
|
|
234
|
+
|
|
235
|
+
if (local >= last) return () => stops[stops.length - 1].color;
|
|
236
|
+
const i = stops.findIndex((s, j) => local >= s.at && local < stops[j + 1].at);
|
|
237
|
+
const [from, length] = [stops[i].at, stops[i + 1].at - stops[i].at];
|
|
238
|
+
|
|
239
|
+
return (at) => blends[i](Math.min(1, Math.max(0, (at - shift - from) / length)));
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const out: Stop[] = [];
|
|
243
|
+
|
|
244
|
+
const push = (x: number, color: RGBA) => {
|
|
245
|
+
const prev = out[out.length - 1];
|
|
246
|
+
|
|
247
|
+
if (!prev || prev.position !== x / extent || !sameColor(prev.color, color))
|
|
248
|
+
out.push({ position: x / extent, color });
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const sorted = [...marks].sort((a, b) => a - b).filter((x, i, arr) => i === 0 || x - arr[i - 1] > 1e-9);
|
|
252
|
+
|
|
253
|
+
for (const [i, x0] of sorted.slice(0, -1).entries()) {
|
|
254
|
+
const x1 = sorted[i + 1];
|
|
255
|
+
const piece = pieceAt((x0 + x1) / 2);
|
|
256
|
+
const at = (x: number) => srgbOf(piece(x));
|
|
257
|
+
|
|
258
|
+
const refine = (a: number, ca: RGBA, b: number, cb: RGBA, depth: number) => {
|
|
259
|
+
if (out.length > MAX_STOPS) return;
|
|
260
|
+
const m = (a + b) / 2;
|
|
261
|
+
const cm = at(m);
|
|
262
|
+
// Gamma curves climb steepest at their ends, so the checks reach close to both ends of the span.
|
|
263
|
+
const far = PROBES.some((t) => off(t === 0.5 ? cm : at(a + (b - a) * t), ca, cb, t) > TOLERANCE);
|
|
264
|
+
|
|
265
|
+
if (far && depth < MAX_DEPTH) {
|
|
266
|
+
refine(a, ca, m, cm, depth + 1);
|
|
267
|
+
refine(m, cm, b, cb, depth + 1);
|
|
268
|
+
} else push(b, cb);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const c0 = at(x0);
|
|
272
|
+
push(x0, c0);
|
|
273
|
+
refine(x0, c0, x1, at(x1), 0);
|
|
274
|
+
|
|
275
|
+
if (out.length > MAX_STOPS) return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Figma holds the end colors past the first and last stops, so stops that only repeat them go.
|
|
279
|
+
while (out.length > 2 && sameColor(out[0].color, out[1].color)) out.shift();
|
|
280
|
+
|
|
281
|
+
while (out.length > 2 && sameColor(out[out.length - 1].color, out[out.length - 2].color)) out.pop();
|
|
282
|
+
|
|
283
|
+
// Figma's renderer uses an unstable float32 sort above ~16 stops and can swap equal-position hard stops; keep positions strictly increasing.
|
|
284
|
+
const EPS = 1e-5;
|
|
285
|
+
|
|
286
|
+
for (let i = 1; i < out.length; i++) {
|
|
287
|
+
if (out[i].position - out[i - 1].position < EPS) {
|
|
288
|
+
if (out[i - 1].position + EPS <= 1) out[i].position = out[i - 1].position + EPS;
|
|
289
|
+
else out[i - 1].position = Math.max(0, out[i].position - EPS);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return out;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const lengthIn = (v: string, length: number) => {
|
|
297
|
+
const m = v.match(LENGTH_RE);
|
|
298
|
+
|
|
299
|
+
if (m) return m[2] === "%" ? parseFloat(m[1]) / 100 : parseFloat(m[1]) / length;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
/** A px or % length in px, a % taken of `size`. */
|
|
303
|
+
const pxIn = (v: string, size: number) => {
|
|
304
|
+
const f = lengthIn(v, 1);
|
|
305
|
+
|
|
306
|
+
return f !== undefined && v.endsWith("%") ? f * size : f;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
/** A gradient's Figma stops from the args after its configuration, each position read by `positionOf`, over `[0, extent]`. */
|
|
310
|
+
type StopsOf = (args: string[], positionOf: (v: string) => number | undefined, extent?: number) => Stop[] | undefined;
|
|
311
|
+
|
|
312
|
+
function linearOf(args: string[], w: number, h: number, stopsOf: StopsOf): LinearGradient | undefined {
|
|
313
|
+
const direction = angleOf(args[0], w, h);
|
|
314
|
+
|
|
315
|
+
if (direction !== undefined) args.shift();
|
|
316
|
+
const angle = direction ?? 180;
|
|
317
|
+
const t = (angle * Math.PI) / 180;
|
|
318
|
+
const length = Math.abs(w * Math.sin(t)) + Math.abs(h * Math.cos(t));
|
|
319
|
+
|
|
320
|
+
if (length === 0) return;
|
|
321
|
+
const gradientStops = stopsOf(args, (v) => lengthIn(v, length));
|
|
322
|
+
|
|
323
|
+
if (!gradientStops) return;
|
|
324
|
+
|
|
325
|
+
const a = (w * Math.sin(t)) / length,
|
|
326
|
+
b = (-h * Math.cos(t)) / length;
|
|
327
|
+
|
|
328
|
+
return {
|
|
329
|
+
type: "GRADIENT_LINEAR",
|
|
330
|
+
gradientTransform: [
|
|
331
|
+
[a, b, 0.5 - 0.5 * a - 0.5 * b],
|
|
332
|
+
[-b, a, 0.5 + 0.5 * b - 0.5 * a],
|
|
333
|
+
],
|
|
334
|
+
gradientStops,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const KEYWORD_AT = new Map(Object.entries({ left: 0, top: 0, center: 0.5, right: 1, bottom: 1 }));
|
|
339
|
+
|
|
340
|
+
/** The center an `at <position>` names, in px from the box's top left. */
|
|
341
|
+
function centerOf(position: string | undefined, w: number, h: number): [number, number] | undefined {
|
|
342
|
+
const words = position?.split(/\s+/).filter(Boolean) ?? [];
|
|
343
|
+
|
|
344
|
+
if (words.length > 2) return;
|
|
345
|
+
|
|
346
|
+
// A leading `top` or `bottom` names the y axis first. A lone keyword names its own axis, and the other axis centers.
|
|
347
|
+
const [x = "center", y = "center"] = /^(top|bottom)$/.test(words[0] ?? "") ? [words[1], words[0]] : words;
|
|
348
|
+
|
|
349
|
+
const along = (v: string, size: number) => {
|
|
350
|
+
const k = KEYWORD_AT.get(v);
|
|
351
|
+
|
|
352
|
+
return k === undefined ? pxIn(v, size) : k * size;
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const cx = along(x, w),
|
|
356
|
+
cy = along(y, h);
|
|
357
|
+
|
|
358
|
+
return cx === undefined || cy === undefined ? undefined : [cx, cy];
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** The ellipse a `radial-gradient` ends on, as radii in px. Corner sizes keep the side sizes' proportions, as CSS does. */
|
|
362
|
+
function radiiOf(size: string, [cx, cy]: [number, number], w: number, h: number): [number, number] | undefined {
|
|
363
|
+
const words = size.split(/\s+/).filter(Boolean);
|
|
364
|
+
const lengths = words.filter((v) => /\d/.test(v));
|
|
365
|
+
const circle = words.includes("circle") || (!words.includes("ellipse") && lengths.length === 1);
|
|
366
|
+
const keyword = words.find((v) => /^(closest|farthest)-(side|corner)$/.test(v)) ?? "farthest-corner";
|
|
367
|
+
|
|
368
|
+
if (words.some((v) => !/\d/.test(v) && !/^(circle|ellipse|(closest|farthest)-(side|corner))$/.test(v))) return;
|
|
369
|
+
|
|
370
|
+
if (lengths.length) {
|
|
371
|
+
const [rx, ry = rx] = lengths.map((v, i) => pxIn(v, i ? h : w));
|
|
372
|
+
|
|
373
|
+
return rx === undefined || ry === undefined || (circle && lengths.length > 1) ? undefined : [rx, ry];
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const xs = [cx, w - cx].map(Math.abs),
|
|
377
|
+
ys = [cy, h - cy].map(Math.abs);
|
|
378
|
+
|
|
379
|
+
const pick = keyword.startsWith("closest") ? Math.min : Math.max;
|
|
380
|
+
const corner = keyword.endsWith("corner");
|
|
381
|
+
|
|
382
|
+
if (circle) {
|
|
383
|
+
const r = corner ? pick(...xs.flatMap((x) => ys.map((y) => Math.hypot(x, y)))) : pick(...xs, ...ys);
|
|
384
|
+
|
|
385
|
+
return [r, r];
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const scale = corner ? Math.SQRT2 : 1;
|
|
389
|
+
|
|
390
|
+
return [pick(...xs) * scale, pick(...ys) * scale];
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function radialOf(
|
|
394
|
+
args: string[],
|
|
395
|
+
w: number,
|
|
396
|
+
h: number,
|
|
397
|
+
stopsOf: StopsOf,
|
|
398
|
+
repeating: boolean,
|
|
399
|
+
): RadialGradient | undefined {
|
|
400
|
+
const first = args[0].match(/^(.*?)\s*(?:\bat\s+(.*))?$/)!;
|
|
401
|
+
const config = !parseColor(args[0].split(/\s+/)[0]) && !/^[\w-]+\(/.test(args[0]);
|
|
402
|
+
|
|
403
|
+
if (config) args.shift();
|
|
404
|
+
const center = centerOf(config ? first[2] : undefined, w, h);
|
|
405
|
+
const radii = center && radiiOf(config ? first[1] : "", center, w, h);
|
|
406
|
+
|
|
407
|
+
if (!center || !radii || !radii[0] || !radii[1]) return;
|
|
408
|
+
|
|
409
|
+
const [cx, cy] = center;
|
|
410
|
+
// Figma holds the last color past the ending shape, but a repeating gradient keeps repeating out to the box's farthest corner.
|
|
411
|
+
const corners = [0, w].flatMap((x) => [0, h].map((y) => Math.hypot((x - cx) / radii[0], (y - cy) / radii[1])));
|
|
412
|
+
const extent = repeating ? Math.max(1, ...corners) : 1;
|
|
413
|
+
const [rx, ry] = [radii[0] * extent, radii[1] * extent];
|
|
414
|
+
const gradientStops = stopsOf(args, (v) => lengthIn(v, radii[0]), extent);
|
|
415
|
+
|
|
416
|
+
if (!gradientStops) return;
|
|
417
|
+
|
|
418
|
+
return {
|
|
419
|
+
type: "GRADIENT_RADIAL",
|
|
420
|
+
gradientTransform: [
|
|
421
|
+
[w / (2 * rx), 0, 0.5 - cx / (2 * rx)],
|
|
422
|
+
[0, h / (2 * ry), 0.5 - cy / (2 * ry)],
|
|
423
|
+
],
|
|
424
|
+
gradientStops,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function conicOf(args: string[], w: number, h: number, stopsOf: StopsOf): AngularGradient | undefined {
|
|
429
|
+
const config = args[0].match(/^(?:from\s+(\S+))?\s*(?:at\s+(.*))?$/);
|
|
430
|
+
|
|
431
|
+
if (config && args[0]) args.shift();
|
|
432
|
+
const from = config?.[1] ? angleOf(config[1], w, h) : 0;
|
|
433
|
+
const center = centerOf(config?.[2], w, h);
|
|
434
|
+
|
|
435
|
+
if (from === undefined || !center || !w || !h) return;
|
|
436
|
+
|
|
437
|
+
const gradientStops = stopsOf(args, (v) => {
|
|
438
|
+
const m = v.match(CONIC_POS_RE);
|
|
439
|
+
|
|
440
|
+
if (m) return m[2] === "%" ? parseFloat(m[1]) / 100 : (parseFloat(m[1]) * UNITS.get(m[2])!) / 360;
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
if (!gradientStops) return;
|
|
444
|
+
// Figma sweeps clockwise from 3 o'clock, CSS from 12 o'clock, so the start turns back a quarter.
|
|
445
|
+
const a = ((from - 90) * Math.PI) / 180;
|
|
446
|
+
const [cx, cy] = center;
|
|
447
|
+
const k = 1 / Math.max(w, h);
|
|
448
|
+
const [cos, sin] = [Math.cos(a), Math.sin(a)];
|
|
449
|
+
|
|
450
|
+
return {
|
|
451
|
+
type: "GRADIENT_ANGULAR",
|
|
452
|
+
gradientTransform: [
|
|
453
|
+
[k * cos * w, k * sin * h, 0.5 - k * (cos * cx + sin * cy)],
|
|
454
|
+
[-k * sin * w, k * cos * h, 0.5 - k * (cos * cy - sin * cx)],
|
|
455
|
+
],
|
|
456
|
+
gradientStops,
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const INTERPOLATION = /(?:^|\s)in\s+([a-z0-9-]+)(?:\s+([a-z]+)\s+hue)?(?=\s|$)/;
|
|
461
|
+
|
|
462
|
+
/** Resolves a CSS `background-size` and `background-position` layer to its pixel rectangle inside a `w × h` box. */
|
|
463
|
+
function bgBoxOf(w: number, h: number, bgSize?: string, bgPos?: string): [number, number, number, number] {
|
|
464
|
+
const size = bgSize?.trim().split(/\s+/).filter(Boolean) ?? [];
|
|
465
|
+
const [rawW = "auto", rawH = "auto"] = size;
|
|
466
|
+
const gw = rawW === "auto" || rawW === "cover" || rawW === "contain" ? w : pxIn(rawW, w);
|
|
467
|
+
const gh = rawH === "auto" || rawH === "cover" || rawH === "contain" ? h : pxIn(rawH, h);
|
|
468
|
+
|
|
469
|
+
if (!gw || !gh || gw <= 0 || gh <= 0) return [0, 0, w, h];
|
|
470
|
+
|
|
471
|
+
const pos = bgPos?.trim().split(/\s+/).filter(Boolean) ?? [];
|
|
472
|
+
const [rawX = "0%", rawY = pos.length ? "50%" : "0%"] = /^(top|bottom)$/.test(pos[0] ?? "") ? [pos[1], pos[0]] : pos;
|
|
473
|
+
|
|
474
|
+
const offset = (v: string, box: number, layer: number) => {
|
|
475
|
+
const k = KEYWORD_AT.get(v);
|
|
476
|
+
|
|
477
|
+
if (k !== undefined) return (box - layer) * k;
|
|
478
|
+
|
|
479
|
+
if (v.endsWith("%")) {
|
|
480
|
+
const f = lengthIn(v, 1);
|
|
481
|
+
|
|
482
|
+
return f === undefined ? 0 : (box - layer) * f;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return pxIn(v, box) ?? 0;
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
return [offset(rawX, w, gw), offset(rawY, h, gh), gw, gh];
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* A `linear-`, `radial-` or `conic-gradient()` layer, repeating or not, as a Figma gradient paint, or nothing for one it cannot
|
|
493
|
+
* draw. Its stops blend the way CSS blends them: in the space an `in` clause names, else sRGB when every stop is a legacy color and
|
|
494
|
+
* oklab otherwise. Optional `bgSize` and `bgPos` place the gradient inside a sub-rectangle of the `w × h` box.
|
|
495
|
+
*/
|
|
496
|
+
export function gradientOf(
|
|
497
|
+
layer: string,
|
|
498
|
+
w: number,
|
|
499
|
+
h: number,
|
|
500
|
+
bgSize?: string,
|
|
501
|
+
bgPos?: string,
|
|
502
|
+
): GradientPaintSpec | undefined {
|
|
503
|
+
const [, repeating, kind, body] = layer.match(/^(repeating-)?(linear|radial|conic)-gradient\((.*)\)$/) ?? [];
|
|
504
|
+
|
|
505
|
+
if (!kind) return;
|
|
506
|
+
const args = splitTop(body);
|
|
507
|
+
const [, space, hue] = args[0].match(INTERPOLATION) ?? [];
|
|
508
|
+
args[0] = args[0].replace(INTERPOLATION, " ").trim();
|
|
509
|
+
|
|
510
|
+
if (!args[0]) args.shift();
|
|
511
|
+
|
|
512
|
+
if (!args.length) return;
|
|
513
|
+
|
|
514
|
+
const [ox, oy, gw, gh] = bgBoxOf(w, h, bgSize, bgPos);
|
|
515
|
+
|
|
516
|
+
const stopsOf: StopsOf = (stopArgs, positionOf, extent) => {
|
|
517
|
+
const line = lineOf(stopArgs, positionOf);
|
|
518
|
+
const method = line && methodOf(space ?? (line.legacy ? "srgb" : "oklab"), hue);
|
|
519
|
+
|
|
520
|
+
return method && figmaStops(line, method, !!repeating, extent);
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
const paint =
|
|
524
|
+
kind === "linear"
|
|
525
|
+
? linearOf(args, gw, gh, stopsOf)
|
|
526
|
+
: kind === "radial"
|
|
527
|
+
? radialOf(args, gw, gh, stopsOf, !!repeating)
|
|
528
|
+
: conicOf(args, gw, gh, stopsOf);
|
|
529
|
+
|
|
530
|
+
if (!paint || (ox === 0 && oy === 0 && gw === w && gh === h)) return paint;
|
|
531
|
+
|
|
532
|
+
const [[a, b, c], [d, e, f]] = paint.gradientTransform;
|
|
533
|
+
const [sx, sy, tx, ty] = [w / gw, h / gh, -ox / gw, -oy / gh];
|
|
534
|
+
|
|
535
|
+
return {
|
|
536
|
+
...paint,
|
|
537
|
+
gradientTransform: [
|
|
538
|
+
[a * sx, b * sy, a * tx + b * ty + c],
|
|
539
|
+
[d * sx, e * sy, d * tx + e * ty + f],
|
|
540
|
+
],
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
export type ShadowSpec = {
|
|
545
|
+
type: "DROP_SHADOW" | "INNER_SHADOW";
|
|
546
|
+
color: RGBA;
|
|
547
|
+
x: number;
|
|
548
|
+
y: number;
|
|
549
|
+
radius: number;
|
|
550
|
+
spread: number;
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
/** Every layer of a `box-shadow`, outer and inset, or none when any layer cannot be read, so a shadow is never drawn half. */
|
|
554
|
+
export function shadowsOf(css: string): ShadowSpec[] {
|
|
555
|
+
if (!css || css === "none") return [];
|
|
556
|
+
const out: ShadowSpec[] = [];
|
|
557
|
+
|
|
558
|
+
for (const layer of splitTop(css)) {
|
|
559
|
+
const words: string[] = layer.match(/[\w-]+\([^)]*\)|\S+/g) ?? [];
|
|
560
|
+
const inset = words.includes("inset");
|
|
561
|
+
const rest = words.filter((v) => v !== "inset");
|
|
562
|
+
const colors = rest.filter((v) => !SHADOW_LEN_RE.test(v));
|
|
563
|
+
const lengths = rest.flatMap((v) => (SHADOW_LEN_RE.test(v) ? [parseFloat(v)] : []));
|
|
564
|
+
const color = colors.length === 1 ? parseColor(colors[0]) : undefined;
|
|
565
|
+
|
|
566
|
+
if (!color || lengths.length < 2 || lengths.length > 4 || (lengths[2] ?? 0) < 0) return [];
|
|
567
|
+
const [x, y, radius = 0, spread = 0] = lengths;
|
|
568
|
+
out.push({ type: inset ? "INNER_SHADOW" : "DROP_SHADOW", color, x, y, radius, spread });
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
return out;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/** A single outer shadow, the shape 0.2's elevations held. */
|
|
575
|
+
export function shadowOf(css: string): { color: RGBA; y: number; radius: number } | undefined {
|
|
576
|
+
const [only, ...more] = shadowsOf(css);
|
|
577
|
+
|
|
578
|
+
if (only?.type === "DROP_SHADOW" && !more.length) return { color: only.color, y: only.y, radius: only.radius };
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
export type BlendMode =
|
|
582
|
+
| "MULTIPLY"
|
|
583
|
+
| "SCREEN"
|
|
584
|
+
| "OVERLAY"
|
|
585
|
+
| "DARKEN"
|
|
586
|
+
| "LIGHTEN"
|
|
587
|
+
| "COLOR_DODGE"
|
|
588
|
+
| "COLOR_BURN"
|
|
589
|
+
| "HARD_LIGHT"
|
|
590
|
+
| "SOFT_LIGHT"
|
|
591
|
+
| "DIFFERENCE"
|
|
592
|
+
| "EXCLUSION"
|
|
593
|
+
| "HUE"
|
|
594
|
+
| "SATURATION"
|
|
595
|
+
| "COLOR"
|
|
596
|
+
| "LUMINOSITY"
|
|
597
|
+
| "LINEAR_DODGE"
|
|
598
|
+
| "LINEAR_BURN";
|
|
599
|
+
|
|
600
|
+
const BLEND_MODES = new Map<string, BlendMode>(
|
|
601
|
+
Object.entries({
|
|
602
|
+
multiply: "MULTIPLY",
|
|
603
|
+
screen: "SCREEN",
|
|
604
|
+
overlay: "OVERLAY",
|
|
605
|
+
darken: "DARKEN",
|
|
606
|
+
lighten: "LIGHTEN",
|
|
607
|
+
"color-dodge": "COLOR_DODGE",
|
|
608
|
+
"color-burn": "COLOR_BURN",
|
|
609
|
+
"hard-light": "HARD_LIGHT",
|
|
610
|
+
"soft-light": "SOFT_LIGHT",
|
|
611
|
+
difference: "DIFFERENCE",
|
|
612
|
+
exclusion: "EXCLUSION",
|
|
613
|
+
hue: "HUE",
|
|
614
|
+
saturation: "SATURATION",
|
|
615
|
+
color: "COLOR",
|
|
616
|
+
luminosity: "LUMINOSITY",
|
|
617
|
+
"plus-lighter": "LINEAR_DODGE",
|
|
618
|
+
"plus-darker": "LINEAR_BURN",
|
|
619
|
+
} as const),
|
|
620
|
+
);
|
|
621
|
+
|
|
622
|
+
/** Figma's blend mode for a CSS `mix-blend-mode`, or nothing for `normal` and modes Figma lacks. */
|
|
623
|
+
export const blendModeOf = (css?: string) => (css ? BLEND_MODES.get(css) : undefined);
|
|
624
|
+
|
|
625
|
+
/** The radius of a `filter: blur()`, in px, or nothing when the filter has none. */
|
|
626
|
+
export const blurOf = (filter?: string) => {
|
|
627
|
+
const m = filter?.match(/blur\(([\d.]+)px\)/);
|
|
628
|
+
|
|
629
|
+
return m ? parseFloat(m[1]) : undefined;
|
|
630
|
+
};
|
|
631
|
+
|
|
632
|
+
const FILTER_FN = /[\w-]+\([^)]*\)/g;
|
|
633
|
+
|
|
634
|
+
/** What of an element's CSS Figma draws only roughly or not at all, one note per property, for a Dev Mode annotation. */
|
|
635
|
+
export function approximations(el: Pick<El, "style" | "box">) {
|
|
636
|
+
const s = el.style;
|
|
637
|
+
const notes: string[] = [];
|
|
638
|
+
|
|
639
|
+
if (s.shadow !== "none" && !shadowsOf(s.shadow).length) notes.push(`box-shadow: ${s.shadow} (dropped)`);
|
|
640
|
+
|
|
641
|
+
if (s.bgImage !== "none")
|
|
642
|
+
for (const layer of splitTop(s.bgImage))
|
|
643
|
+
if (layer !== "none" && !layer.startsWith("url(") && !gradientOf(layer, el.box[2], el.box[3]))
|
|
644
|
+
notes.push(`background-image: ${layer} (dropped)`);
|
|
645
|
+
|
|
646
|
+
for (const [property, value] of [
|
|
647
|
+
["filter", s.filter],
|
|
648
|
+
["backdrop-filter", s.backdrop],
|
|
649
|
+
] as const) {
|
|
650
|
+
const other = (value && value !== "none" ? (value.match(FILTER_FN) ?? []) : []).filter(
|
|
651
|
+
(f) => !f.startsWith("blur("),
|
|
652
|
+
);
|
|
653
|
+
|
|
654
|
+
if (other.length) notes.push(`${property}: ${other.join(" ")} (dropped${blurOf(value) ? "; blur() kept" : ""})`);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (s.mixBlendMode && !blendModeOf(s.mixBlendMode)) notes.push(`mix-blend-mode: ${s.mixBlendMode} (dropped)`);
|
|
658
|
+
|
|
659
|
+
const m = s.transform
|
|
660
|
+
.match(/^matrix\(([^)]+)\)$/)?.[1]
|
|
661
|
+
.split(",")
|
|
662
|
+
.map(Number);
|
|
663
|
+
|
|
664
|
+
if (m && (Math.abs(Math.hypot(m[0], m[1]) - 1) > 0.01 || Math.abs(Math.hypot(m[2], m[3]) - 1) > 0.01))
|
|
665
|
+
notes.push(`transform: ${s.transform} (scale and skew dropped)`);
|
|
666
|
+
|
|
667
|
+
return notes;
|
|
668
|
+
}
|