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.
Files changed (97) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +447 -0
  3. package/dist/cache.d.ts +29 -0
  4. package/dist/cache.js +68 -0
  5. package/dist/fig/bytebuffer.d.ts +31 -0
  6. package/dist/fig/bytebuffer.js +98 -0
  7. package/dist/fig/container.d.ts +32 -0
  8. package/dist/fig/container.js +80 -0
  9. package/dist/fig/imagemeta.d.ts +17 -0
  10. package/dist/fig/imagemeta.js +96 -0
  11. package/dist/fig/invariants.d.ts +10 -0
  12. package/dist/fig/invariants.js +63 -0
  13. package/dist/fig/kiwi.d.ts +38 -0
  14. package/dist/fig/kiwi.js +144 -0
  15. package/dist/fig/parse.d.ts +37 -0
  16. package/dist/fig/parse.js +91 -0
  17. package/dist/fig/zip.d.ts +23 -0
  18. package/dist/fig/zip.js +95 -0
  19. package/dist/mcp/create.d.ts +12 -0
  20. package/dist/mcp/create.js +48 -0
  21. package/dist/mcp/respond.d.ts +76 -0
  22. package/dist/mcp/respond.js +124 -0
  23. package/dist/mcp/server.d.ts +3 -0
  24. package/dist/mcp/server.js +22 -0
  25. package/dist/mcp/tools/blob.d.ts +7 -0
  26. package/dist/mcp/tools/blob.js +55 -0
  27. package/dist/mcp/tools/components.d.ts +7 -0
  28. package/dist/mcp/tools/components.js +54 -0
  29. package/dist/mcp/tools/context.d.ts +24 -0
  30. package/dist/mcp/tools/context.js +48 -0
  31. package/dist/mcp/tools/find.d.ts +7 -0
  32. package/dist/mcp/tools/find.js +97 -0
  33. package/dist/mcp/tools/image.d.ts +6 -0
  34. package/dist/mcp/tools/image.js +100 -0
  35. package/dist/mcp/tools/instance.d.ts +7 -0
  36. package/dist/mcp/tools/instance.js +44 -0
  37. package/dist/mcp/tools/node.d.ts +7 -0
  38. package/dist/mcp/tools/node.js +49 -0
  39. package/dist/mcp/tools/overview.d.ts +7 -0
  40. package/dist/mcp/tools/overview.js +45 -0
  41. package/dist/mcp/tools/render.d.ts +6 -0
  42. package/dist/mcp/tools/render.js +128 -0
  43. package/dist/mcp/tools/style.d.ts +7 -0
  44. package/dist/mcp/tools/style.js +38 -0
  45. package/dist/mcp/tools/text.d.ts +7 -0
  46. package/dist/mcp/tools/text.js +89 -0
  47. package/dist/mcp/tools/tree.d.ts +7 -0
  48. package/dist/mcp/tools/tree.js +100 -0
  49. package/dist/mcp/tools/variables.d.ts +7 -0
  50. package/dist/mcp/tools/variables.js +71 -0
  51. package/dist/model/access.d.ts +28 -0
  52. package/dist/model/access.js +106 -0
  53. package/dist/model/components.d.ts +72 -0
  54. package/dist/model/components.js +157 -0
  55. package/dist/model/index.d.ts +73 -0
  56. package/dist/model/index.js +192 -0
  57. package/dist/model/instance.d.ts +87 -0
  58. package/dist/model/instance.js +208 -0
  59. package/dist/model/summarize.d.ts +83 -0
  60. package/dist/model/summarize.js +541 -0
  61. package/dist/model/text.d.ts +29 -0
  62. package/dist/model/text.js +61 -0
  63. package/dist/model/tree.d.ts +47 -0
  64. package/dist/model/tree.js +89 -0
  65. package/dist/model/variables.d.ts +50 -0
  66. package/dist/model/variables.js +141 -0
  67. package/dist/render/bounds.d.ts +64 -0
  68. package/dist/render/bounds.js +173 -0
  69. package/dist/render/color.d.ts +20 -0
  70. package/dist/render/color.js +41 -0
  71. package/dist/render/effects.d.ts +34 -0
  72. package/dist/render/effects.js +196 -0
  73. package/dist/render/export.d.ts +51 -0
  74. package/dist/render/export.js +573 -0
  75. package/dist/render/index.d.ts +42 -0
  76. package/dist/render/index.js +94 -0
  77. package/dist/render/instance.d.ts +12 -0
  78. package/dist/render/instance.js +72 -0
  79. package/dist/render/matrix.d.ts +46 -0
  80. package/dist/render/matrix.js +94 -0
  81. package/dist/render/node.d.ts +41 -0
  82. package/dist/render/node.js +95 -0
  83. package/dist/render/paint.d.ts +27 -0
  84. package/dist/render/paint.js +301 -0
  85. package/dist/render/path.d.ts +42 -0
  86. package/dist/render/path.js +156 -0
  87. package/dist/render/raster.d.ts +31 -0
  88. package/dist/render/raster.js +69 -0
  89. package/dist/render/report.d.ts +72 -0
  90. package/dist/render/report.js +106 -0
  91. package/dist/render/style.d.ts +31 -0
  92. package/dist/render/style.js +67 -0
  93. package/dist/render/svg.d.ts +60 -0
  94. package/dist/render/svg.js +150 -0
  95. package/dist/render/text.d.ts +50 -0
  96. package/dist/render/text.js +90 -0
  97. package/package.json +47 -0
@@ -0,0 +1,89 @@
1
+ /** Canonical guid string used at every API boundary: `"2:1339"`. */
2
+ export function guidKey(g) {
3
+ if (!g || g.sessionID === undefined || g.localID === undefined)
4
+ return undefined;
5
+ return `${g.sessionID}:${g.localID}`;
6
+ }
7
+ export function readGuid(obj, field) {
8
+ return guidKey(obj?.[field]);
9
+ }
10
+ /**
11
+ * The identity that instance override paths (`symbolData.symbolOverrides[].guidPath.guids` and
12
+ * `derivedSymbolData[].guidPath.guids`) use for a component descendant: its `overrideKey` when
13
+ * it has one — a node created by copying a component keeps the original's — else its own guid.
14
+ *
15
+ * Measured on the sample: 8 893 of 10 480 symbol descendants carry an explicit key; the other
16
+ * 1 587 are addressed by guid, by 30 061 of the file's 303 833 records.
17
+ */
18
+ export function overrideIdentity(t) {
19
+ return readGuid(t.node, 'overrideKey') ?? t.key;
20
+ }
21
+ /** Raw code-unit comparison — see pitfall §11.4. */
22
+ function comparePosition(a, b) {
23
+ const pa = a.node['parentIndex']?.['position'];
24
+ const pb = b.node['parentIndex']?.['position'];
25
+ const sa = typeof pa === 'string' ? pa : '';
26
+ const sb = typeof pb === 'string' ? pb : '';
27
+ if (sa < sb)
28
+ return -1;
29
+ if (sa > sb)
30
+ return 1;
31
+ // Deterministic tiebreak so identical positions never reorder between calls.
32
+ return a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
33
+ }
34
+ export function buildTree(nodeChanges) {
35
+ const byKey = new Map();
36
+ for (const node of nodeChanges) {
37
+ const key = guidKey(node['guid']);
38
+ if (key === undefined)
39
+ continue;
40
+ byKey.set(key, { key, node, children: [], parent: undefined, order: -1, depth: 0, page: undefined });
41
+ }
42
+ let root;
43
+ const orphans = [];
44
+ for (const entry of byKey.values()) {
45
+ const parentGuid = entry.node['parentIndex']?.['guid'];
46
+ const parent = parentGuid ? byKey.get(guidKey(parentGuid) ?? '') : undefined;
47
+ if (parent) {
48
+ parent.children.push(entry);
49
+ entry.parent = parent;
50
+ }
51
+ else if (entry.node['type'] === 'DOCUMENT' && !root) {
52
+ root = entry;
53
+ }
54
+ else {
55
+ orphans.push(entry);
56
+ }
57
+ }
58
+ for (const entry of byKey.values()) {
59
+ if (entry.children.length > 1)
60
+ entry.children.sort(comparePosition);
61
+ }
62
+ // Iterative DFS: numbering, depth and page assignment in one pass, no recursion depth limit.
63
+ const ordered = [];
64
+ const roots = root ? [root, ...orphans] : [...orphans];
65
+ for (const start of roots) {
66
+ const stack = [start];
67
+ while (stack.length) {
68
+ const t = stack.pop();
69
+ t.order = ordered.length;
70
+ ordered.push(t);
71
+ const parent = t.parent;
72
+ t.depth = parent ? parent.depth + 1 : 0;
73
+ t.page = t.node['type'] === 'CANVAS' ? t : parent?.page;
74
+ for (let i = t.children.length - 1; i >= 0; i--)
75
+ stack.push(t.children[i]);
76
+ }
77
+ }
78
+ return { root, byKey, ordered, orphans };
79
+ }
80
+ /** Names from the node up to (excluding) the DOCUMENT, root-first — a readable breadcrumb. */
81
+ export function breadcrumb(t, separator = ' / ') {
82
+ const parts = [];
83
+ for (let cur = t.parent; cur; cur = cur.parent) {
84
+ if (cur.node['type'] === 'DOCUMENT')
85
+ break;
86
+ parts.push(String(cur.node['name'] ?? cur.key));
87
+ }
88
+ return parts.reverse().join(separator);
89
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Figma Variables (fig-reading-solution.md §9.8).
3
+ *
4
+ * A VARIABLE_SET node is a collection with named modes; VARIABLE nodes hold one value per mode.
5
+ * Consumption sites carry a `VariableData` whose `value.alias` points at a variable either by
6
+ * local GUID or by library `assetRef`. Local aliases are followed (with a cycle guard); library
7
+ * aliases are surfaced as-is, since the library is not in the file.
8
+ */
9
+ import type { FileIndex } from './index.js';
10
+ import type { TreeNode } from './tree.js';
11
+ import type { KiwiObject } from '../fig/kiwi.js';
12
+ export interface VariableRef {
13
+ /** Local variable name when resolvable, else undefined. */
14
+ name?: string;
15
+ guid?: string;
16
+ /** Library asset reference `key@version` when the target is not in this file. */
17
+ assetRef?: string;
18
+ /** Resolved concrete value per mode name, when the chain ends locally. */
19
+ values?: Record<string, unknown>;
20
+ unresolved?: true;
21
+ }
22
+ export declare function variableSetOf(idx: FileIndex, variable: TreeNode): TreeNode | undefined;
23
+ /** Per-mode values of one VARIABLE node, following local alias chains. */
24
+ export declare function variableValues(idx: FileIndex, variable: TreeNode, depth?: number): Record<string, unknown>;
25
+ /** Resolve a `VariableID` (`{guid}` or `{assetRef}`) to a named local variable when possible. */
26
+ export declare function resolveVariableRef(idx: FileIndex, alias: KiwiObject | undefined, depth?: number): VariableRef;
27
+ /** A `VariableData` binding at a consumption site (`colorVar`, `radiusVar`, …). */
28
+ export declare function describeVariableData(idx: FileIndex, data: KiwiObject | undefined): unknown;
29
+ export interface VariableSetView {
30
+ guid: string;
31
+ name?: string;
32
+ key?: string;
33
+ modes: {
34
+ id: string;
35
+ name: string;
36
+ }[];
37
+ variableCount: number;
38
+ }
39
+ export declare function variableSetView(idx: FileIndex, set: TreeNode): VariableSetView;
40
+ export interface VariableView {
41
+ guid: string;
42
+ name?: string;
43
+ type?: string;
44
+ key?: string;
45
+ set?: string;
46
+ values: Record<string, unknown>;
47
+ scopes?: unknown;
48
+ }
49
+ export declare function variableView(idx: FileIndex, variable: TreeNode): VariableView;
50
+ //# sourceMappingURL=variables.d.ts.map
@@ -0,0 +1,141 @@
1
+ import { assetRefString, bool, colorHex, compact, num, obj, objArr, r2, str, jsonSafe } from './access.js';
2
+ import { guidKey } from './tree.js';
3
+ const MAX_ALIAS_DEPTH = 8;
4
+ /** A concrete (non-alias) `VariableAnyValue` rendered compactly; aliases return undefined. */
5
+ function concreteValue(value) {
6
+ if (!value)
7
+ return undefined;
8
+ const color = obj(value, 'colorValue');
9
+ if (color)
10
+ return colorHex(color);
11
+ const f = num(value, 'floatValue');
12
+ if (f !== undefined)
13
+ return r2(f);
14
+ const t = str(value, 'textValue');
15
+ if (t !== undefined)
16
+ return t;
17
+ const b = bool(value, 'boolValue');
18
+ if (b !== undefined)
19
+ return b;
20
+ const len = obj(value, 'lengthValue');
21
+ if (len)
22
+ return r2(num(len, 'value')) ?? jsonSafe(len);
23
+ const alias = obj(value, 'alias');
24
+ if (alias)
25
+ return undefined;
26
+ // Anything else (gradients, symbol ids, expressions…) is passed through verbatim.
27
+ const keys = Object.keys(value);
28
+ return keys.length ? { [keys[0]]: jsonSafe(value[keys[0]]) } : undefined;
29
+ }
30
+ /** Mode GUID → mode name for the set a variable belongs to. */
31
+ function modeNames(idx, variable) {
32
+ const out = new Map();
33
+ const set = variableSetOf(idx, variable);
34
+ if (!set)
35
+ return out;
36
+ for (const mode of objArr(set.node, 'variableSetModes')) {
37
+ const id = guidKey(obj(mode, 'id'));
38
+ if (id)
39
+ out.set(id, str(mode, 'name') ?? id);
40
+ }
41
+ return out;
42
+ }
43
+ export function variableSetOf(idx, variable) {
44
+ const ref = obj(variable.node, 'variableSetID');
45
+ if (!ref)
46
+ return undefined;
47
+ const byGuid = guidKey(obj(ref, 'guid'));
48
+ if (byGuid) {
49
+ const t = idx.node(byGuid);
50
+ if (t)
51
+ return t;
52
+ }
53
+ return idx.resolveAssetRef(ref);
54
+ }
55
+ /** Per-mode values of one VARIABLE node, following local alias chains. */
56
+ export function variableValues(idx, variable, depth = 0) {
57
+ const names = modeNames(idx, variable);
58
+ const out = {};
59
+ for (const entry of objArr(obj(variable.node, 'variableDataValues'), 'entries')) {
60
+ const modeId = guidKey(obj(entry, 'modeID')) ?? '?';
61
+ const data = obj(entry, 'variableData');
62
+ const value = obj(data, 'value');
63
+ const alias = obj(value, 'alias');
64
+ const label = names.get(modeId) ?? modeId;
65
+ if (alias) {
66
+ const ref = resolveVariableRef(idx, alias, depth + 1);
67
+ out[label] = ref.values && Object.keys(ref.values).length === 1
68
+ ? Object.values(ref.values)[0]
69
+ : compact({ alias: ref.name ?? ref.assetRef, guid: ref.guid, values: ref.values });
70
+ }
71
+ else {
72
+ out[label] = concreteValue(value);
73
+ }
74
+ }
75
+ return out;
76
+ }
77
+ /** Resolve a `VariableID` (`{guid}` or `{assetRef}`) to a named local variable when possible. */
78
+ export function resolveVariableRef(idx, alias, depth = 0) {
79
+ if (!alias)
80
+ return { unresolved: true };
81
+ const assetRef = assetRefString(alias);
82
+ const localByGuid = guidKey(obj(alias, 'guid'));
83
+ const target = (localByGuid ? idx.node(localByGuid) : undefined) ?? idx.resolveAssetRef(alias);
84
+ if (!target)
85
+ return compact({ guid: localByGuid, assetRef, unresolved: true });
86
+ const ref = compact({
87
+ name: str(target.node, 'name'),
88
+ guid: target.key,
89
+ assetRef,
90
+ });
91
+ if (depth < MAX_ALIAS_DEPTH) {
92
+ const values = variableValues(idx, target, depth);
93
+ if (Object.keys(values).length)
94
+ ref.values = values;
95
+ }
96
+ return ref;
97
+ }
98
+ /** A `VariableData` binding at a consumption site (`colorVar`, `radiusVar`, …). */
99
+ export function describeVariableData(idx, data) {
100
+ if (!data)
101
+ return undefined;
102
+ const value = obj(data, 'value');
103
+ const alias = obj(value, 'alias');
104
+ if (alias) {
105
+ const ref = resolveVariableRef(idx, alias);
106
+ return compact({
107
+ variable: ref.name,
108
+ guid: ref.guid,
109
+ assetRef: ref.assetRef,
110
+ values: ref.values,
111
+ resolvedType: str(data, 'resolvedDataType'),
112
+ });
113
+ }
114
+ const concrete = concreteValue(value);
115
+ if (concrete !== undefined)
116
+ return concrete;
117
+ return compact({ dataType: str(data, 'dataType'), resolvedType: str(data, 'resolvedDataType') });
118
+ }
119
+ export function variableSetView(idx, set) {
120
+ const modes = objArr(set.node, 'variableSetModes').map((m) => ({
121
+ id: guidKey(obj(m, 'id')) ?? '?',
122
+ name: str(m, 'name') ?? '',
123
+ }));
124
+ const key = str(set.node, 'key');
125
+ let variableCount = 0;
126
+ for (const v of idx.variables)
127
+ if (variableSetOf(idx, v)?.key === set.key)
128
+ variableCount++;
129
+ return compact({ guid: set.key, name: str(set.node, 'name'), key, modes, variableCount });
130
+ }
131
+ export function variableView(idx, variable) {
132
+ const set = variableSetOf(idx, variable);
133
+ return compact({
134
+ guid: variable.key,
135
+ name: str(variable.node, 'name'),
136
+ type: str(variable.node, 'variableResolvedType'),
137
+ key: str(variable.node, 'key'),
138
+ set: set ? (str(set.node, 'name') ?? set.key) : undefined,
139
+ values: variableValues(idx, variable),
140
+ });
141
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Content and render bounds in node-local coordinates (plan §4.5).
3
+ *
4
+ * `contentBounds` is the ink; `renderBounds` adds the margin an effect needs. Both are memoised
5
+ * per node AND instance scope, so a page subtree is walked once per render rather than once per
6
+ * parent, while the same symbol node measured under two instances — which may size it
7
+ * differently (F20) — gets two entries.
8
+ *
9
+ * The effect-margin rule is the initial one from the plan. Fixture `cf-effect-drop-shadow`
10
+ * calibrates it against Figma's own export size: this is the ONE function to change, and the
11
+ * only symptom of it being too small is a shadow cropped on one side (the `<filter>` region
12
+ * clips its own output — Appendix E, R21).
13
+ */
14
+ import type { CacheEntry } from '../cache.js';
15
+ import type { NodeChange } from '../fig/parse.js';
16
+ import type { TreeNode } from '../model/tree.js';
17
+ import { type Box } from './matrix.js';
18
+ export interface Margins {
19
+ readonly left: number;
20
+ readonly top: number;
21
+ readonly right: number;
22
+ readonly bottom: number;
23
+ }
24
+ /** How a tree node looks in the current context — inside an instance, merged with its records. */
25
+ export type EffectiveNode = (t: TreeNode) => NodeChange;
26
+ export declare class BoundsCache {
27
+ private readonly entry;
28
+ private readonly effective;
29
+ private readonly scope;
30
+ private readonly content;
31
+ private readonly render;
32
+ /**
33
+ * `effective` is the node as the exporter sees it right now and `scope` names the chain of
34
+ * instances it is being drawn under. A symbol's node has one effective shape per instance
35
+ * that expands it, so memo entries are keyed by scope and node together.
36
+ */
37
+ constructor(entry: CacheEntry, effective?: EffectiveNode, scope?: () => string);
38
+ private key;
39
+ private blob;
40
+ private fieldBounds;
41
+ /**
42
+ * Union of the fill and stroke path bounds, in node-local coordinates.
43
+ *
44
+ * An INSIDE stroke's stored geometry is a double-width band straddling the edge, which the
45
+ * exporter clips to the fill shape (F16). Counting it raw would inflate the render by the
46
+ * stroke weight on every side — the 134x40 frame 2:1339 would export as 134x41.
47
+ */
48
+ private geometryBounds;
49
+ private childrenBounds;
50
+ contentBounds(t: TreeNode): Box | undefined;
51
+ /** Per-side margin the node's visible effects need beyond its content. */
52
+ effectMargins(t: TreeNode): Margins;
53
+ renderBounds(t: TreeNode): Box | undefined;
54
+ /** Bounds of `t` expressed in the coordinate space of `ancestor` (excluding its transform). */
55
+ boundsIn(t: TreeNode, ancestor: TreeNode): Box | undefined;
56
+ }
57
+ /**
58
+ * Per-side margin a node's visible effects need beyond its content.
59
+ *
60
+ * Pure and node-based rather than tree-based, so the exporter can size a `<filter>` from the
61
+ * EFFECTIVE node — inside an instance the effects may come from an override (F17).
62
+ */
63
+ export declare function effectMargins(node: NodeChange): Margins;
64
+ //# sourceMappingURL=bounds.d.ts.map
@@ -0,0 +1,173 @@
1
+ import { bool, bytes, num, obj, objArr, str } from '../model/access.js';
2
+ import { expandBox, fromFigma, transformBox, unionBox } from './matrix.js';
3
+ import { decodeCommands, pathBounds } from './path.js';
4
+ import { clipsChildren, hasFillGeometry, isBooleanOperation, isInstance, isVisible, nodeSize, nodeType, strokeAlign, } from './node.js';
5
+ const NO_MARGIN = { left: 0, top: 0, right: 0, bottom: 0 };
6
+ export class BoundsCache {
7
+ entry;
8
+ effective;
9
+ scope;
10
+ content = new Map();
11
+ render = new Map();
12
+ /**
13
+ * `effective` is the node as the exporter sees it right now and `scope` names the chain of
14
+ * instances it is being drawn under. A symbol's node has one effective shape per instance
15
+ * that expands it, so memo entries are keyed by scope and node together.
16
+ */
17
+ constructor(entry, effective = (t) => t.node, scope = () => '') {
18
+ this.entry = entry;
19
+ this.effective = effective;
20
+ this.scope = scope;
21
+ }
22
+ key(t) {
23
+ return `${this.scope()}${t.key}`;
24
+ }
25
+ blob(index) {
26
+ if (index === undefined)
27
+ return undefined;
28
+ return bytes(this.entry.fig.blobs[index], 'bytes');
29
+ }
30
+ fieldBounds(node, field) {
31
+ let box;
32
+ for (const path of objArr(node, field)) {
33
+ const raw = this.blob(num(path, 'commandsBlob'));
34
+ if (!raw || raw.length === 0)
35
+ continue;
36
+ try {
37
+ box = unionBox(box, pathBounds(decodeCommands(raw)));
38
+ }
39
+ catch {
40
+ // A corrupt blob must not poison the bounds; emitPaths reports it separately.
41
+ }
42
+ }
43
+ return box;
44
+ }
45
+ /**
46
+ * Union of the fill and stroke path bounds, in node-local coordinates.
47
+ *
48
+ * An INSIDE stroke's stored geometry is a double-width band straddling the edge, which the
49
+ * exporter clips to the fill shape (F16). Counting it raw would inflate the render by the
50
+ * stroke weight on every side — the 134x40 frame 2:1339 would export as 134x41.
51
+ */
52
+ geometryBounds(node) {
53
+ const fill = this.fieldBounds(node, 'fillGeometry');
54
+ if (strokeAlign(node) === 'INSIDE' && hasFillGeometry(node))
55
+ return fill;
56
+ return unionBox(fill, this.fieldBounds(node, 'strokeGeometry'));
57
+ }
58
+ childrenBounds(children) {
59
+ let box;
60
+ for (const child of children) {
61
+ const childNode = this.effective(child);
62
+ if (!isVisible(childNode))
63
+ continue;
64
+ const childBox = this.renderBounds(child);
65
+ if (!childBox)
66
+ continue;
67
+ box = unionBox(box, transformBox(fromFigma(obj(childNode, 'transform')), childBox));
68
+ }
69
+ return box;
70
+ }
71
+ contentBounds(t) {
72
+ const key = this.key(t);
73
+ const memo = this.content.get(key);
74
+ if (memo !== undefined)
75
+ return memo ?? undefined;
76
+ // Guard against a cyclic parent chain in a damaged file.
77
+ this.content.set(key, null);
78
+ const node = this.effective(t);
79
+ let box;
80
+ if (nodeType(node) !== 'CANVAS') {
81
+ const size = nodeSize(node);
82
+ box = { x: 0, y: 0, w: size.w, h: size.h };
83
+ }
84
+ box = unionBox(box, this.geometryBounds(node));
85
+ if (isInstance(node)) {
86
+ // An instance has no children of its own (F17): its content is the symbol's, resized by
87
+ // this instance's derived geometry. The symbol's OWN children are the wrong size — the
88
+ // 16x16 instance 2:1340 points at a 22x22 symbol — so descending would inflate the render.
89
+ // `size` is present on every instance in the sample and is authoritative; the cost is
90
+ // that content deliberately overflowing a non-clipping instance can be cropped.
91
+ // (nothing to add: the box above already is the instance)
92
+ }
93
+ else if (!clipsChildren(node) && !isBooleanOperation(node)) {
94
+ // A clipping container bounds its own children; a boolean's children are operands (F3).
95
+ box = unionBox(box, this.childrenBounds(t.children));
96
+ }
97
+ this.content.set(key, box ?? null);
98
+ return box;
99
+ }
100
+ /** Per-side margin the node's visible effects need beyond its content. */
101
+ effectMargins(t) {
102
+ return effectMargins(this.effective(t));
103
+ }
104
+ renderBounds(t) {
105
+ const key = this.key(t);
106
+ const memo = this.render.get(key);
107
+ if (memo !== undefined)
108
+ return memo ?? undefined;
109
+ this.render.set(key, null);
110
+ const content = this.contentBounds(t);
111
+ if (!content)
112
+ return undefined;
113
+ const m = effectMargins(this.effective(t));
114
+ const box = m === NO_MARGIN ? content : expandBox(content, m.left, m.top, m.right, m.bottom);
115
+ this.render.set(key, box);
116
+ return box;
117
+ }
118
+ /** Bounds of `t` expressed in the coordinate space of `ancestor` (excluding its transform). */
119
+ boundsIn(t, ancestor) {
120
+ let box = this.renderBounds(t);
121
+ if (!box)
122
+ return undefined;
123
+ for (let cur = t; cur && cur !== ancestor; cur = cur.parent) {
124
+ box = transformBox(fromFigma(obj(cur.node, 'transform')), box);
125
+ }
126
+ return box;
127
+ }
128
+ }
129
+ /**
130
+ * Per-side margin a node's visible effects need beyond its content.
131
+ *
132
+ * Pure and node-based rather than tree-based, so the exporter can size a `<filter>` from the
133
+ * EFFECTIVE node — inside an instance the effects may come from an override (F17).
134
+ */
135
+ export function effectMargins(node) {
136
+ {
137
+ let left = 0;
138
+ let top = 0;
139
+ let right = 0;
140
+ let bottom = 0;
141
+ for (const effect of objArr(node, 'effects')) {
142
+ if (bool(effect, 'visible') === false)
143
+ continue;
144
+ const type = str(effect, 'type');
145
+ const radius = num(effect, 'radius') ?? 0;
146
+ if (type === 'DROP_SHADOW') {
147
+ const spread = num(effect, 'spread') ?? 0;
148
+ const dx = num(obj(effect, 'offset'), 'x') ?? 0;
149
+ const dy = num(obj(effect, 'offset'), 'y') ?? 0;
150
+ const reach = radius + spread;
151
+ left = Math.max(left, reach - dx);
152
+ right = Math.max(right, reach + dx);
153
+ top = Math.max(top, reach - dy);
154
+ bottom = Math.max(bottom, reach + dy);
155
+ }
156
+ else if (type === 'FOREGROUND_BLUR') {
157
+ left = Math.max(left, radius);
158
+ right = Math.max(right, radius);
159
+ top = Math.max(top, radius);
160
+ bottom = Math.max(bottom, radius);
161
+ }
162
+ // INNER_SHADOW and BACKGROUND_BLUR stay inside the shape and add nothing.
163
+ }
164
+ if (left === 0 && top === 0 && right === 0 && bottom === 0)
165
+ return NO_MARGIN;
166
+ return {
167
+ left: Math.max(0, left),
168
+ top: Math.max(0, top),
169
+ right: Math.max(0, right),
170
+ bottom: Math.max(0, bottom),
171
+ };
172
+ }
173
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Colour conversion for SVG output.
3
+ *
4
+ * Figma stores `{ r, g, b, a }` as floats 0..1. SVG wants `#rrggbb` plus a separate opacity,
5
+ * which keeps the hex stable when only alpha differs and lets paints de-duplicate better.
6
+ */
7
+ import type { KiwiObject } from '../fig/kiwi.js';
8
+ import type { TreeNode } from '../model/tree.js';
9
+ /** `{r,g,b}` → `#rrggbb` (lowercase; alpha is returned separately by `alphaOf`). */
10
+ export declare function hexOf(c: KiwiObject | undefined): string;
11
+ /** Alpha 0..1; an absent `a` means opaque. */
12
+ export declare function alphaOf(c: KiwiObject | undefined): number;
13
+ /** A CSS colour usable in a `fill` attribute or as resvg's `background` option. */
14
+ export declare function cssColor(c: KiwiObject | undefined): string;
15
+ /**
16
+ * F13 — the page background. Returns undefined when the node is not a page, when the
17
+ * background is disabled, or when it is fully transparent.
18
+ */
19
+ export declare function pageBackground(t: TreeNode | undefined): string | undefined;
20
+ //# sourceMappingURL=color.d.ts.map
@@ -0,0 +1,41 @@
1
+ import { bool, num, obj } from '../model/access.js';
2
+ function channel(v) {
3
+ const n = Math.max(0, Math.min(255, Math.round((v ?? 0) * 255)));
4
+ return n.toString(16).padStart(2, '0');
5
+ }
6
+ /** `{r,g,b}` → `#rrggbb` (lowercase; alpha is returned separately by `alphaOf`). */
7
+ export function hexOf(c) {
8
+ return `#${channel(num(c, 'r'))}${channel(num(c, 'g'))}${channel(num(c, 'b'))}`;
9
+ }
10
+ /** Alpha 0..1; an absent `a` means opaque. */
11
+ export function alphaOf(c) {
12
+ const a = num(c, 'a');
13
+ return a === undefined ? 1 : Math.max(0, Math.min(1, a));
14
+ }
15
+ /** A CSS colour usable in a `fill` attribute or as resvg's `background` option. */
16
+ export function cssColor(c) {
17
+ const a = alphaOf(c);
18
+ if (a >= 1)
19
+ return hexOf(c);
20
+ const ch = (k) => Math.max(0, Math.min(255, Math.round((num(c, k) ?? 0) * 255)));
21
+ return `rgba(${ch('r')},${ch('g')},${ch('b')},${Math.round(a * 1000) / 1000})`;
22
+ }
23
+ /**
24
+ * F13 — the page background. Returns undefined when the node is not a page, when the
25
+ * background is disabled, or when it is fully transparent.
26
+ */
27
+ export function pageBackground(t) {
28
+ for (let cur = t; cur; cur = cur.parent) {
29
+ if (cur.node['type'] !== 'CANVAS')
30
+ continue;
31
+ if (bool(cur.node, 'backgroundEnabled') === false)
32
+ return undefined;
33
+ const color = obj(cur.node, 'backgroundColor');
34
+ if (!color)
35
+ return undefined;
36
+ if (alphaOf(color) <= 0)
37
+ return undefined;
38
+ return cssColor(color);
39
+ }
40
+ return undefined;
41
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Effects → one `<filter>` per node (plan §4.9).
3
+ *
4
+ * The primitive chains below are the ones Figma's own SVG export emits, which is why they are
5
+ * written out literally rather than invented: resvg and Chromium both render them identically,
6
+ * and it means a geometry comparison against a Figma SVG compares like with like. Every
7
+ * primitive used here is verified in Appendix E (R19, R20).
8
+ *
9
+ * Figma's blur `radius` is a Gaussian `stdDeviation = radius / 2`.
10
+ */
11
+ import type { NodeChange } from '../fig/parse.js';
12
+ import type { Box } from './matrix.js';
13
+ import { type SvgWriter } from './svg.js';
14
+ import { type ReportBuilder } from './report.js';
15
+ /**
16
+ * Registers the node's filter and returns its id, or `undefined` when nothing needs one.
17
+ * `region` is the node's render bounds in node-local units — the filter clips its own output to
18
+ * it, so a region that is too small crops the shadow rather than moving it (Appendix E, R21).
19
+ */
20
+ export declare function effectsFilter(node: NodeChange, region: Box, out: SvgWriter, report: ReportBuilder, guid: string, defKey: string): string | undefined;
21
+ /**
22
+ * The filter that turns any coverage into white-with-the-same-alpha, so a luminance `<mask>`
23
+ * reproduces Figma's ALPHA mask exactly (Appendix E, R9). Registered once per document.
24
+ */
25
+ export declare function alphaToWhiteFilter(out: SvgWriter): string;
26
+ /**
27
+ * §4.11 — the `style` attribute for a node's group, or `undefined` when nothing is needed.
28
+ *
29
+ * PASS_THROUGH (the default) lets children composite with what is behind the group; NORMAL makes
30
+ * the group composite as a unit, which is what `isolation: isolate` expresses. Both are verified
31
+ * to work in resvg (Appendix E, R16 and R17).
32
+ */
33
+ export declare function blendStyle(node: NodeChange, isContainer: boolean, report: ReportBuilder, guid: string): string | undefined;
34
+ //# sourceMappingURL=effects.d.ts.map