figme-mcp 1.0.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/LICENSE +21 -0
- package/README.md +447 -0
- package/dist/cache.d.ts +29 -0
- package/dist/cache.js +68 -0
- package/dist/fig/bytebuffer.d.ts +31 -0
- package/dist/fig/bytebuffer.js +98 -0
- package/dist/fig/container.d.ts +32 -0
- package/dist/fig/container.js +80 -0
- package/dist/fig/imagemeta.d.ts +17 -0
- package/dist/fig/imagemeta.js +96 -0
- package/dist/fig/invariants.d.ts +10 -0
- package/dist/fig/invariants.js +63 -0
- package/dist/fig/kiwi.d.ts +38 -0
- package/dist/fig/kiwi.js +144 -0
- package/dist/fig/parse.d.ts +37 -0
- package/dist/fig/parse.js +91 -0
- package/dist/fig/zip.d.ts +23 -0
- package/dist/fig/zip.js +95 -0
- package/dist/mcp/create.d.ts +12 -0
- package/dist/mcp/create.js +48 -0
- package/dist/mcp/respond.d.ts +76 -0
- package/dist/mcp/respond.js +124 -0
- package/dist/mcp/server.d.ts +3 -0
- package/dist/mcp/server.js +22 -0
- package/dist/mcp/tools/blob.d.ts +7 -0
- package/dist/mcp/tools/blob.js +55 -0
- package/dist/mcp/tools/components.d.ts +7 -0
- package/dist/mcp/tools/components.js +54 -0
- package/dist/mcp/tools/context.d.ts +24 -0
- package/dist/mcp/tools/context.js +48 -0
- package/dist/mcp/tools/find.d.ts +7 -0
- package/dist/mcp/tools/find.js +97 -0
- package/dist/mcp/tools/image.d.ts +6 -0
- package/dist/mcp/tools/image.js +100 -0
- package/dist/mcp/tools/instance.d.ts +7 -0
- package/dist/mcp/tools/instance.js +44 -0
- package/dist/mcp/tools/node.d.ts +7 -0
- package/dist/mcp/tools/node.js +49 -0
- package/dist/mcp/tools/overview.d.ts +7 -0
- package/dist/mcp/tools/overview.js +45 -0
- package/dist/mcp/tools/render.d.ts +6 -0
- package/dist/mcp/tools/render.js +128 -0
- package/dist/mcp/tools/style.d.ts +7 -0
- package/dist/mcp/tools/style.js +38 -0
- package/dist/mcp/tools/text.d.ts +7 -0
- package/dist/mcp/tools/text.js +89 -0
- package/dist/mcp/tools/tree.d.ts +7 -0
- package/dist/mcp/tools/tree.js +100 -0
- package/dist/mcp/tools/variables.d.ts +7 -0
- package/dist/mcp/tools/variables.js +71 -0
- package/dist/model/access.d.ts +28 -0
- package/dist/model/access.js +106 -0
- package/dist/model/components.d.ts +72 -0
- package/dist/model/components.js +157 -0
- package/dist/model/index.d.ts +73 -0
- package/dist/model/index.js +192 -0
- package/dist/model/instance.d.ts +87 -0
- package/dist/model/instance.js +208 -0
- package/dist/model/summarize.d.ts +83 -0
- package/dist/model/summarize.js +541 -0
- package/dist/model/text.d.ts +29 -0
- package/dist/model/text.js +61 -0
- package/dist/model/tree.d.ts +47 -0
- package/dist/model/tree.js +89 -0
- package/dist/model/variables.d.ts +50 -0
- package/dist/model/variables.js +141 -0
- package/dist/render/bounds.d.ts +64 -0
- package/dist/render/bounds.js +173 -0
- package/dist/render/color.d.ts +20 -0
- package/dist/render/color.js +41 -0
- package/dist/render/effects.d.ts +34 -0
- package/dist/render/effects.js +196 -0
- package/dist/render/export.d.ts +51 -0
- package/dist/render/export.js +573 -0
- package/dist/render/index.d.ts +42 -0
- package/dist/render/index.js +94 -0
- package/dist/render/instance.d.ts +12 -0
- package/dist/render/instance.js +72 -0
- package/dist/render/matrix.d.ts +46 -0
- package/dist/render/matrix.js +94 -0
- package/dist/render/node.d.ts +41 -0
- package/dist/render/node.js +95 -0
- package/dist/render/paint.d.ts +27 -0
- package/dist/render/paint.js +301 -0
- package/dist/render/path.d.ts +42 -0
- package/dist/render/path.js +156 -0
- package/dist/render/raster.d.ts +31 -0
- package/dist/render/raster.js +69 -0
- package/dist/render/report.d.ts +72 -0
- package/dist/render/report.js +106 -0
- package/dist/render/style.d.ts +31 -0
- package/dist/render/style.js +67 -0
- package/dist/render/svg.d.ts +60 -0
- package/dist/render/svg.js +150 -0
- package/dist/render/text.d.ts +50 -0
- package/dist/render/text.js +90 -0
- package/package.json +47 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { str } from '../model/access.js';
|
|
2
|
+
import { pageBackground } from './color.js';
|
|
3
|
+
import { DEFAULT_MAX_NODES, HARD_MAX_NODES, MAX_SVG_BYTES, exportSubtree, } from './export.js';
|
|
4
|
+
import { nodeType } from './node.js';
|
|
5
|
+
import { rasterize } from './raster.js';
|
|
6
|
+
export const DEFAULT_SCALE = 2;
|
|
7
|
+
export const DEFAULT_MAX_SIZE = 1568;
|
|
8
|
+
export const HARD_MAX_SIZE = 4096;
|
|
9
|
+
function resolveRoot(entry, guid) {
|
|
10
|
+
const t = entry.index.node(guid);
|
|
11
|
+
if (!t) {
|
|
12
|
+
throw new Error(`no node with guid ${JSON.stringify(guid)} in this file ` +
|
|
13
|
+
'(guids look like "2:1339"; use fig_find or fig_tree to discover them)');
|
|
14
|
+
}
|
|
15
|
+
if (nodeType(t.node) === 'DOCUMENT') {
|
|
16
|
+
throw new Error('cannot render the DOCUMENT node — render a page or a node inside one');
|
|
17
|
+
}
|
|
18
|
+
return t;
|
|
19
|
+
}
|
|
20
|
+
export async function renderNode(entry, guid, opts = {}) {
|
|
21
|
+
const root = resolveRoot(entry, guid);
|
|
22
|
+
const maxNodes = Math.min(opts.maxNodes ?? DEFAULT_MAX_NODES, HARD_MAX_NODES);
|
|
23
|
+
const range = entry.index.subtreeRange(root);
|
|
24
|
+
const subtreeSize = range.end - range.start;
|
|
25
|
+
if (subtreeSize > maxNodes) {
|
|
26
|
+
throw new Error(`node ${guid} covers ${subtreeSize} nodes, above maxNodes (${maxNodes}); ` +
|
|
27
|
+
'render a smaller node or raise maxNodes');
|
|
28
|
+
}
|
|
29
|
+
const background = opts.background === 'page' ? pageBackground(root) : undefined;
|
|
30
|
+
const startedExport = performance.now();
|
|
31
|
+
const exported = exportSubtree(entry, root, {
|
|
32
|
+
maxNodes,
|
|
33
|
+
collectBoxes: opts.collectBoxes,
|
|
34
|
+
background,
|
|
35
|
+
});
|
|
36
|
+
const renderMs = performance.now() - startedExport;
|
|
37
|
+
if (exported.svg.length > MAX_SVG_BYTES) {
|
|
38
|
+
throw new Error(`the generated SVG is ${exported.svg.length} bytes, above the ${MAX_SVG_BYTES}-byte limit; ` +
|
|
39
|
+
'render a smaller subtree (large image fills are embedded once per distinct paint)');
|
|
40
|
+
}
|
|
41
|
+
const bounds = exported.bounds;
|
|
42
|
+
const maxSize = Math.min(opts.maxSize ?? DEFAULT_MAX_SIZE, HARD_MAX_SIZE);
|
|
43
|
+
const longest = Math.max(bounds.w, bounds.h);
|
|
44
|
+
const requested = opts.scale ?? DEFAULT_SCALE;
|
|
45
|
+
const scale = longest * requested > maxSize && longest > 0 ? maxSize / longest : requested;
|
|
46
|
+
const width = Math.max(1, Math.round(bounds.w * scale));
|
|
47
|
+
const height = Math.max(1, Math.round(bounds.h * scale));
|
|
48
|
+
let png;
|
|
49
|
+
let rasterMs;
|
|
50
|
+
let rasterizerMissing = false;
|
|
51
|
+
if (opts.format !== 'svg') {
|
|
52
|
+
const startedRaster = performance.now();
|
|
53
|
+
let raster;
|
|
54
|
+
try {
|
|
55
|
+
raster = await rasterize(exported.svg, scale);
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
// resvg rejects malformed markup with a plain Error; report it instead of crashing.
|
|
59
|
+
exported.report.unsupported('svg-rejected', guid);
|
|
60
|
+
throw new Error(`the rasterizer rejected the generated SVG: ${err.message}`);
|
|
61
|
+
}
|
|
62
|
+
if (raster) {
|
|
63
|
+
png = raster.png;
|
|
64
|
+
rasterMs = performance.now() - startedRaster;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
rasterizerMissing = true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const report = exported.report.finish({
|
|
71
|
+
root: { guid: root.key, name: str(root.node, 'name'), type: nodeType(root.node) },
|
|
72
|
+
bounds,
|
|
73
|
+
width,
|
|
74
|
+
height,
|
|
75
|
+
scale: Math.round(scale * 1000) / 1000,
|
|
76
|
+
svgBytes: exported.svg.length,
|
|
77
|
+
renderMs: Math.round(renderMs * 10) / 10,
|
|
78
|
+
rasterMs: rasterMs === undefined ? undefined : Math.round(rasterMs * 10) / 10,
|
|
79
|
+
note: rasterizerMissing
|
|
80
|
+
? 'rasterizer unavailable — npm install @resvg/resvg-wasm to get PNG output'
|
|
81
|
+
: undefined,
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
svg: exported.svg,
|
|
85
|
+
png,
|
|
86
|
+
width,
|
|
87
|
+
height,
|
|
88
|
+
bounds,
|
|
89
|
+
report,
|
|
90
|
+
boxes: opts.collectBoxes ? exported.boxes : undefined,
|
|
91
|
+
rasterizerMissing,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export { rasterizer, rasterize } from './raster.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { NodeChange } from '../fig/parse.js';
|
|
2
|
+
import { type TreeNode } from '../model/tree.js';
|
|
3
|
+
import { type OverrideRecords } from '../model/instance.js';
|
|
4
|
+
export { applyProps, descend, mergeNode, resolveInstance, type AppliedProps, type OverrideRecords, type PropAssignments, type ResolvedInstance, } from '../model/instance.js';
|
|
5
|
+
/**
|
|
6
|
+
* How the instance's own box should be drawn: the symbol root with its override applied, then
|
|
7
|
+
* any of the shape fields the instance itself carries (13 835 instances have their own
|
|
8
|
+
* `fillGeometry`, already resolved). `instance` is the node as seen in its context — for a
|
|
9
|
+
* nested instance that is the merged node, whose `size` the enclosing instance may have changed.
|
|
10
|
+
*/
|
|
11
|
+
export declare function instanceShapeNode(instance: NodeChange, symbol: TreeNode, records: OverrideRecords): NodeChange;
|
|
12
|
+
//# sourceMappingURL=instance.d.ts.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { num, obj } from '../model/access.js';
|
|
2
|
+
import { overrideIdentity } from '../model/tree.js';
|
|
3
|
+
import { mergeNode } from '../model/instance.js';
|
|
4
|
+
export { applyProps, descend, mergeNode, resolveInstance, } from '../model/instance.js';
|
|
5
|
+
/**
|
|
6
|
+
* Fields that describe how a node paints ITSELF. When an instance carries one of these it has
|
|
7
|
+
* already been resolved for this instance; otherwise the symbol root's (overridden) value wins.
|
|
8
|
+
*/
|
|
9
|
+
const SHAPE_FIELDS = [
|
|
10
|
+
'fillGeometry',
|
|
11
|
+
'strokeGeometry',
|
|
12
|
+
'size',
|
|
13
|
+
'strokeAlign',
|
|
14
|
+
'effects',
|
|
15
|
+
'styleIdForEffect',
|
|
16
|
+
'blendMode',
|
|
17
|
+
'opacity',
|
|
18
|
+
'frameMaskDisabled',
|
|
19
|
+
'resizeToFit',
|
|
20
|
+
'cornerRadius',
|
|
21
|
+
];
|
|
22
|
+
/**
|
|
23
|
+
* Paints and the style reference beside them travel as one unit: an instance whose root fill was
|
|
24
|
+
* detached from the symbol's colour style carries its own `fillPaints` and NO reference (133 in
|
|
25
|
+
* the sample, every one a different colour from the style), and taking the paints without also
|
|
26
|
+
* dropping the symbol's reference would hand the fill straight back to the style (F19).
|
|
27
|
+
*/
|
|
28
|
+
const PAINT_UNITS = [
|
|
29
|
+
['fillPaints', 'styleIdForFill'],
|
|
30
|
+
['strokePaints', 'styleIdForStrokeFill'],
|
|
31
|
+
];
|
|
32
|
+
function sizeDiffers(a, b) {
|
|
33
|
+
if (!a || !b)
|
|
34
|
+
return false;
|
|
35
|
+
return (Math.abs((num(a, 'x') ?? 0) - (num(b, 'x') ?? 0)) > 1e-3 ||
|
|
36
|
+
Math.abs((num(a, 'y') ?? 0) - (num(b, 'y') ?? 0)) > 1e-3);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* How the instance's own box should be drawn: the symbol root with its override applied, then
|
|
40
|
+
* any of the shape fields the instance itself carries (13 835 instances have their own
|
|
41
|
+
* `fillGeometry`, already resolved). `instance` is the node as seen in its context — for a
|
|
42
|
+
* nested instance that is the merged node, whose `size` the enclosing instance may have changed.
|
|
43
|
+
*/
|
|
44
|
+
export function instanceShapeNode(instance, symbol, records) {
|
|
45
|
+
const base = mergeNode(symbol.node, records.get(overrideIdentity(symbol)));
|
|
46
|
+
const out = { ...base };
|
|
47
|
+
for (const field of SHAPE_FIELDS) {
|
|
48
|
+
if (instance[field] !== undefined)
|
|
49
|
+
out[field] = instance[field];
|
|
50
|
+
}
|
|
51
|
+
for (const [paints, style] of PAINT_UNITS) {
|
|
52
|
+
if (instance[paints] === undefined)
|
|
53
|
+
continue;
|
|
54
|
+
out[paints] = instance[paints];
|
|
55
|
+
if (instance[style] !== undefined)
|
|
56
|
+
out[style] = instance[style];
|
|
57
|
+
else
|
|
58
|
+
delete out[style];
|
|
59
|
+
}
|
|
60
|
+
// Geometry inherited from the symbol root describes the symbol's size, not this instance's
|
|
61
|
+
// (F20); the box is rebuilt from `size` instead.
|
|
62
|
+
if (sizeDiffers(obj(base, 'size'), obj(instance, 'size'))) {
|
|
63
|
+
if (instance['fillGeometry'] === undefined)
|
|
64
|
+
delete out['fillGeometry'];
|
|
65
|
+
if (instance['strokeGeometry'] === undefined)
|
|
66
|
+
delete out['strokeGeometry'];
|
|
67
|
+
}
|
|
68
|
+
// The instance's own transform is already applied by its group; never take the symbol's.
|
|
69
|
+
out['transform'] = instance['transform'];
|
|
70
|
+
out['type'] = instance['type'];
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 2×3 affine transforms in SVG parameter order (plan §4.1).
|
|
3
|
+
*
|
|
4
|
+
* SVG writes `matrix(a b c d e f)` meaning `x' = a·x + c·y + e`, `y' = b·x + d·y + f`.
|
|
5
|
+
* Figma stores the same transform as `{ m00 m01 m02 / m10 m11 m12 }` with
|
|
6
|
+
* `X = m00·x + m01·y + m02`, `Y = m10·x + m11·y + m12` (F1), so the mapping is
|
|
7
|
+
* m00→a, m10→b, m01→c, m11→d, m02→e, m12→f.
|
|
8
|
+
*
|
|
9
|
+
* This module imports nothing from the rest of `render/`: `svg.ts` and `path.ts` depend on it,
|
|
10
|
+
* never the other way round.
|
|
11
|
+
*/
|
|
12
|
+
import type { KiwiObject } from '../fig/kiwi.js';
|
|
13
|
+
export interface Mat {
|
|
14
|
+
readonly a: number;
|
|
15
|
+
readonly b: number;
|
|
16
|
+
readonly c: number;
|
|
17
|
+
readonly d: number;
|
|
18
|
+
readonly e: number;
|
|
19
|
+
readonly f: number;
|
|
20
|
+
}
|
|
21
|
+
/** An axis-aligned box. Coordinate space is whatever the caller is working in. */
|
|
22
|
+
export interface Box {
|
|
23
|
+
readonly x: number;
|
|
24
|
+
readonly y: number;
|
|
25
|
+
readonly w: number;
|
|
26
|
+
readonly h: number;
|
|
27
|
+
}
|
|
28
|
+
export declare const IDENTITY: Mat;
|
|
29
|
+
/** Figma `Matrix` → SVG matrix. An absent matrix is the identity (F1 + ground rule 8). */
|
|
30
|
+
export declare function fromFigma(m: KiwiObject | undefined): Mat;
|
|
31
|
+
/** `multiply(p, q)`: apply `q` first, then `p` — the same order as SVG nesting. */
|
|
32
|
+
export declare function multiply(p: Mat, q: Mat): Mat;
|
|
33
|
+
/** `undefined` for a singular matrix; callers fall back rather than emitting NaN. */
|
|
34
|
+
export declare function invert(m: Mat): Mat | undefined;
|
|
35
|
+
export declare function apply(m: Mat, x: number, y: number): [number, number];
|
|
36
|
+
export declare function scale(sx: number, sy: number): Mat;
|
|
37
|
+
export declare function translate(tx: number, ty: number): Mat;
|
|
38
|
+
export declare function isIdentity(m: Mat): boolean;
|
|
39
|
+
/** Bounding box of the four transformed corners (a rotation grows the box, as it should). */
|
|
40
|
+
export declare function transformBox(m: Mat, b: Box): Box;
|
|
41
|
+
export declare function unionBox(a: Box | undefined, b: Box | undefined): Box | undefined;
|
|
42
|
+
/** Grow a box by a per-side margin (left, top, right, bottom). Negative margins are ignored. */
|
|
43
|
+
export declare function expandBox(b: Box, l: number, t: number, r: number, bt: number): Box;
|
|
44
|
+
/** True when every component is finite — the guard before a box reaches the SVG. */
|
|
45
|
+
export declare function isFiniteBox(b: Box | undefined): b is Box;
|
|
46
|
+
//# sourceMappingURL=matrix.d.ts.map
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { num } from '../model/access.js';
|
|
2
|
+
export const IDENTITY = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 };
|
|
3
|
+
/** Figma `Matrix` → SVG matrix. An absent matrix is the identity (F1 + ground rule 8). */
|
|
4
|
+
export function fromFigma(m) {
|
|
5
|
+
if (!m)
|
|
6
|
+
return IDENTITY;
|
|
7
|
+
return {
|
|
8
|
+
a: num(m, 'm00') ?? 1,
|
|
9
|
+
b: num(m, 'm10') ?? 0,
|
|
10
|
+
c: num(m, 'm01') ?? 0,
|
|
11
|
+
d: num(m, 'm11') ?? 1,
|
|
12
|
+
e: num(m, 'm02') ?? 0,
|
|
13
|
+
f: num(m, 'm12') ?? 0,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** `multiply(p, q)`: apply `q` first, then `p` — the same order as SVG nesting. */
|
|
17
|
+
export function multiply(p, q) {
|
|
18
|
+
return {
|
|
19
|
+
a: p.a * q.a + p.c * q.b,
|
|
20
|
+
b: p.b * q.a + p.d * q.b,
|
|
21
|
+
c: p.a * q.c + p.c * q.d,
|
|
22
|
+
d: p.b * q.c + p.d * q.d,
|
|
23
|
+
e: p.a * q.e + p.c * q.f + p.e,
|
|
24
|
+
f: p.b * q.e + p.d * q.f + p.f,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** `undefined` for a singular matrix; callers fall back rather than emitting NaN. */
|
|
28
|
+
export function invert(m) {
|
|
29
|
+
const det = m.a * m.d - m.c * m.b;
|
|
30
|
+
if (!Number.isFinite(det) || Math.abs(det) < 1e-12)
|
|
31
|
+
return undefined;
|
|
32
|
+
return {
|
|
33
|
+
a: m.d / det,
|
|
34
|
+
b: -m.b / det,
|
|
35
|
+
c: -m.c / det,
|
|
36
|
+
d: m.a / det,
|
|
37
|
+
e: (m.c * m.f - m.d * m.e) / det,
|
|
38
|
+
f: (m.b * m.e - m.a * m.f) / det,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export function apply(m, x, y) {
|
|
42
|
+
return [m.a * x + m.c * y + m.e, m.b * x + m.d * y + m.f];
|
|
43
|
+
}
|
|
44
|
+
export function scale(sx, sy) {
|
|
45
|
+
return { a: sx, b: 0, c: 0, d: sy, e: 0, f: 0 };
|
|
46
|
+
}
|
|
47
|
+
export function translate(tx, ty) {
|
|
48
|
+
return { a: 1, b: 0, c: 0, d: 1, e: tx, f: ty };
|
|
49
|
+
}
|
|
50
|
+
export function isIdentity(m) {
|
|
51
|
+
return m.a === 1 && m.b === 0 && m.c === 0 && m.d === 1 && m.e === 0 && m.f === 0;
|
|
52
|
+
}
|
|
53
|
+
/** Bounding box of the four transformed corners (a rotation grows the box, as it should). */
|
|
54
|
+
export function transformBox(m, b) {
|
|
55
|
+
const xs = [];
|
|
56
|
+
const ys = [];
|
|
57
|
+
for (const [cx, cy] of [
|
|
58
|
+
[b.x, b.y],
|
|
59
|
+
[b.x + b.w, b.y],
|
|
60
|
+
[b.x + b.w, b.y + b.h],
|
|
61
|
+
[b.x, b.y + b.h],
|
|
62
|
+
]) {
|
|
63
|
+
const [px, py] = apply(m, cx, cy);
|
|
64
|
+
xs.push(px);
|
|
65
|
+
ys.push(py);
|
|
66
|
+
}
|
|
67
|
+
const minX = Math.min(...xs);
|
|
68
|
+
const minY = Math.min(...ys);
|
|
69
|
+
return { x: minX, y: minY, w: Math.max(...xs) - minX, h: Math.max(...ys) - minY };
|
|
70
|
+
}
|
|
71
|
+
export function unionBox(a, b) {
|
|
72
|
+
if (!a)
|
|
73
|
+
return b;
|
|
74
|
+
if (!b)
|
|
75
|
+
return a;
|
|
76
|
+
const x = Math.min(a.x, b.x);
|
|
77
|
+
const y = Math.min(a.y, b.y);
|
|
78
|
+
return { x, y, w: Math.max(a.x + a.w, b.x + b.w) - x, h: Math.max(a.y + a.h, b.y + b.h) - y };
|
|
79
|
+
}
|
|
80
|
+
/** Grow a box by a per-side margin (left, top, right, bottom). Negative margins are ignored. */
|
|
81
|
+
export function expandBox(b, l, t, r, bt) {
|
|
82
|
+
const left = Math.max(0, l);
|
|
83
|
+
const top = Math.max(0, t);
|
|
84
|
+
return {
|
|
85
|
+
x: b.x - left,
|
|
86
|
+
y: b.y - top,
|
|
87
|
+
w: b.w + left + Math.max(0, r),
|
|
88
|
+
h: b.h + top + Math.max(0, bt),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** True when every component is finite — the guard before a box reaches the SVG. */
|
|
92
|
+
export function isFiniteBox(b) {
|
|
93
|
+
return (!!b && Number.isFinite(b.x) && Number.isFinite(b.y) && Number.isFinite(b.w) && Number.isFinite(b.h));
|
|
94
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node classification and the structural predicates of plan §4.4, §4.6 and F10/F11/F16.
|
|
3
|
+
*
|
|
4
|
+
* Everything here takes a raw `NodeChange` rather than a `TreeNode`, because inside an instance
|
|
5
|
+
* the node being drawn is the symbol's node merged with that instance's overrides (F17) — a
|
|
6
|
+
* record with no tree position of its own.
|
|
7
|
+
*/
|
|
8
|
+
import type { NodeChange } from '../fig/parse.js';
|
|
9
|
+
export type NodeClass = 'container' | 'shape' | 'text' | 'skip';
|
|
10
|
+
export declare function nodeType(node: NodeChange): string;
|
|
11
|
+
export declare function classify(node: NodeChange, isRoot: boolean): NodeClass;
|
|
12
|
+
/**
|
|
13
|
+
* F10. Groups are stored as `FRAME` with `resizeToFit: true` and `frameMaskDisabled: false`,
|
|
14
|
+
* so testing `frameMaskDisabled` alone would wrongly clip every group (Pitfall 1).
|
|
15
|
+
*/
|
|
16
|
+
export declare function clipsChildren(node: NodeChange): boolean;
|
|
17
|
+
/** A BOOLEAN_OPERATION carries the combined geometry; its children are operands (F3). */
|
|
18
|
+
export declare function isBooleanOperation(node: NodeChange): boolean;
|
|
19
|
+
export declare function isInstance(node: NodeChange): boolean;
|
|
20
|
+
export declare function isMask(node: NodeChange): boolean;
|
|
21
|
+
/** Ground rule 8: an absent `visible` means visible. */
|
|
22
|
+
export declare function isVisible(node: NodeChange): boolean;
|
|
23
|
+
export declare function nodeOpacity(node: NodeChange): number;
|
|
24
|
+
export type StrokeAlign = 'CENTER' | 'INSIDE' | 'OUTSIDE' | 'OFFSET';
|
|
25
|
+
/**
|
|
26
|
+
* F16 — Figma bakes a stroke outline of DOUBLE the weight for INSIDE and OUTSIDE alignment and
|
|
27
|
+
* clips it at render time; only CENTER geometry is final. Measured across the sample: overshoot
|
|
28
|
+
* beyond the node box is exactly 1.00x weight for INSIDE (8 930 nodes) and OUTSIDE (125), and
|
|
29
|
+
* 0.50x for CENTER (2 737). `StrokeAlign` is `{ CENTER=0, INSIDE=1, OUTSIDE=2, OFFSET=3 }`, so
|
|
30
|
+
* an absent field means CENTER — the alignment that needs no clip.
|
|
31
|
+
*/
|
|
32
|
+
export declare function strokeAlign(node: NodeChange): StrokeAlign;
|
|
33
|
+
export declare function hasFillGeometry(node: NodeChange): boolean;
|
|
34
|
+
export declare function hasGeometry(node: NodeChange): boolean;
|
|
35
|
+
export interface Size {
|
|
36
|
+
readonly w: number;
|
|
37
|
+
readonly h: number;
|
|
38
|
+
}
|
|
39
|
+
/** Node-local box size. TEXT prefers `derivedTextData.layoutSize` when it is present. */
|
|
40
|
+
export declare function nodeSize(node: NodeChange): Size;
|
|
41
|
+
//# sourceMappingURL=node.d.ts.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { bool, num, obj, str } from '../model/access.js';
|
|
2
|
+
const CONTAINERS = new Set(['FRAME', 'SYMBOL', 'INSTANCE', 'SECTION', 'GROUP']);
|
|
3
|
+
const SHAPES = new Set([
|
|
4
|
+
'RECTANGLE',
|
|
5
|
+
'ROUNDED_RECTANGLE',
|
|
6
|
+
'ELLIPSE',
|
|
7
|
+
'LINE',
|
|
8
|
+
'VECTOR',
|
|
9
|
+
'STAR',
|
|
10
|
+
'REGULAR_POLYGON',
|
|
11
|
+
'BOOLEAN_OPERATION',
|
|
12
|
+
]);
|
|
13
|
+
/** Only these ever clip their children (F10). SECTION and groups never do. */
|
|
14
|
+
const CLIPPERS = new Set(['FRAME', 'SYMBOL', 'INSTANCE']);
|
|
15
|
+
export function nodeType(node) {
|
|
16
|
+
return str(node, 'type') ?? 'UNKNOWN';
|
|
17
|
+
}
|
|
18
|
+
export function classify(node, isRoot) {
|
|
19
|
+
const type = nodeType(node);
|
|
20
|
+
if (type === 'CANVAS')
|
|
21
|
+
return isRoot ? 'container' : 'skip';
|
|
22
|
+
if (CONTAINERS.has(type))
|
|
23
|
+
return 'container';
|
|
24
|
+
if (SHAPES.has(type))
|
|
25
|
+
return 'shape';
|
|
26
|
+
if (type === 'TEXT')
|
|
27
|
+
return 'text';
|
|
28
|
+
return 'skip';
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* F10. Groups are stored as `FRAME` with `resizeToFit: true` and `frameMaskDisabled: false`,
|
|
32
|
+
* so testing `frameMaskDisabled` alone would wrongly clip every group (Pitfall 1).
|
|
33
|
+
*/
|
|
34
|
+
export function clipsChildren(node) {
|
|
35
|
+
if (!CLIPPERS.has(nodeType(node)))
|
|
36
|
+
return false;
|
|
37
|
+
if (bool(node, 'frameMaskDisabled') === true)
|
|
38
|
+
return false;
|
|
39
|
+
if (bool(node, 'resizeToFit') === true)
|
|
40
|
+
return false;
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
/** A BOOLEAN_OPERATION carries the combined geometry; its children are operands (F3). */
|
|
44
|
+
export function isBooleanOperation(node) {
|
|
45
|
+
return nodeType(node) === 'BOOLEAN_OPERATION';
|
|
46
|
+
}
|
|
47
|
+
export function isInstance(node) {
|
|
48
|
+
return nodeType(node) === 'INSTANCE';
|
|
49
|
+
}
|
|
50
|
+
export function isMask(node) {
|
|
51
|
+
return bool(node, 'mask') === true;
|
|
52
|
+
}
|
|
53
|
+
/** Ground rule 8: an absent `visible` means visible. */
|
|
54
|
+
export function isVisible(node) {
|
|
55
|
+
return bool(node, 'visible') !== false;
|
|
56
|
+
}
|
|
57
|
+
export function nodeOpacity(node) {
|
|
58
|
+
const o = num(node, 'opacity');
|
|
59
|
+
return o === undefined ? 1 : o;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* F16 — Figma bakes a stroke outline of DOUBLE the weight for INSIDE and OUTSIDE alignment and
|
|
63
|
+
* clips it at render time; only CENTER geometry is final. Measured across the sample: overshoot
|
|
64
|
+
* beyond the node box is exactly 1.00x weight for INSIDE (8 930 nodes) and OUTSIDE (125), and
|
|
65
|
+
* 0.50x for CENTER (2 737). `StrokeAlign` is `{ CENTER=0, INSIDE=1, OUTSIDE=2, OFFSET=3 }`, so
|
|
66
|
+
* an absent field means CENTER — the alignment that needs no clip.
|
|
67
|
+
*/
|
|
68
|
+
export function strokeAlign(node) {
|
|
69
|
+
const raw = str(node, 'strokeAlign');
|
|
70
|
+
return raw === 'INSIDE' || raw === 'OUTSIDE' || raw === 'OFFSET' ? raw : 'CENTER';
|
|
71
|
+
}
|
|
72
|
+
export function hasFillGeometry(node) {
|
|
73
|
+
const geometry = node['fillGeometry'];
|
|
74
|
+
return Array.isArray(geometry) && geometry.length > 0;
|
|
75
|
+
}
|
|
76
|
+
export function hasGeometry(node) {
|
|
77
|
+
for (const field of ['fillGeometry', 'strokeGeometry']) {
|
|
78
|
+
const g = node[field];
|
|
79
|
+
if (Array.isArray(g) && g.length > 0)
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
/** Node-local box size. TEXT prefers `derivedTextData.layoutSize` when it is present. */
|
|
85
|
+
export function nodeSize(node) {
|
|
86
|
+
if (nodeType(node) === 'TEXT') {
|
|
87
|
+
const layout = obj(obj(node, 'derivedTextData'), 'layoutSize');
|
|
88
|
+
const lw = num(layout, 'x');
|
|
89
|
+
const lh = num(layout, 'y');
|
|
90
|
+
if (lw !== undefined && lh !== undefined)
|
|
91
|
+
return { w: lw, h: lh };
|
|
92
|
+
}
|
|
93
|
+
const size = obj(node, 'size');
|
|
94
|
+
return { w: num(size, 'x') ?? 0, h: num(size, 'y') ?? 0 };
|
|
95
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paints → SVG fill attributes (plan §4.7).
|
|
3
|
+
*
|
|
4
|
+
* `paintAttrs` returns the attributes to put on a `<path>`/`<rect>`, or `undefined` when the
|
|
5
|
+
* paint contributes nothing (invisible, or a type we cannot draw — already reported).
|
|
6
|
+
* `box` is the node-local box `{0,0,width,height}`, which is also the "normalized box" of F7.
|
|
7
|
+
*/
|
|
8
|
+
import type { CacheEntry } from '../cache.js';
|
|
9
|
+
import type { KiwiObject } from '../fig/kiwi.js';
|
|
10
|
+
import { type Box } from './matrix.js';
|
|
11
|
+
import { type Attrs, type SvgWriter } from './svg.js';
|
|
12
|
+
import { type ReportBuilder } from './report.js';
|
|
13
|
+
export interface PaintEnv {
|
|
14
|
+
readonly report: ReportBuilder;
|
|
15
|
+
readonly out: SvgWriter;
|
|
16
|
+
readonly entry: CacheEntry;
|
|
17
|
+
/** Image hash → data URI, or null when the bytes are missing or a format we cannot embed. */
|
|
18
|
+
readonly images: Map<string, string | null>;
|
|
19
|
+
/** OUTLINE masks paint everything opaque white (plan §4.10). */
|
|
20
|
+
readonly whiteout: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Attributes for one paint, or `undefined` when it draws nothing.
|
|
24
|
+
* Records the paint type in the report vocabulary either way.
|
|
25
|
+
*/
|
|
26
|
+
export declare function paintAttrs(paint: KiwiObject, box: Box, env: PaintEnv, guid: string): Attrs | undefined;
|
|
27
|
+
//# sourceMappingURL=paint.d.ts.map
|