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,196 @@
|
|
|
1
|
+
import { bool, num, obj, objArr, str } from '../model/access.js';
|
|
2
|
+
import { alphaOf } from './color.js';
|
|
3
|
+
import { fmt } from './svg.js';
|
|
4
|
+
import { feat } from './report.js';
|
|
5
|
+
/** Effects that produce filter primitives rather than only a report entry. */
|
|
6
|
+
const DRAWN = new Set(['DROP_SHADOW', 'INNER_SHADOW', 'FOREGROUND_BLUR']);
|
|
7
|
+
function visibleEffects(node) {
|
|
8
|
+
return objArr(node, 'effects').filter((e) => bool(e, 'visible') !== false);
|
|
9
|
+
}
|
|
10
|
+
function channels(effect) {
|
|
11
|
+
const color = obj(effect, 'color');
|
|
12
|
+
return {
|
|
13
|
+
r: num(color, 'r') ?? 0,
|
|
14
|
+
g: num(color, 'g') ?? 0,
|
|
15
|
+
b: num(color, 'b') ?? 0,
|
|
16
|
+
a: alphaOf(color),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function colorMatrix(input, effect, result) {
|
|
20
|
+
const c = channels(effect);
|
|
21
|
+
return (`<feColorMatrix in="${input}" type="matrix" values="0 0 0 0 ${fmt(c.r)} 0 0 0 0 ${fmt(c.g)} ` +
|
|
22
|
+
`0 0 0 0 ${fmt(c.b)} 0 0 0 ${fmt(c.a)} 0" result="${result}"/>`);
|
|
23
|
+
}
|
|
24
|
+
/** Any non-zero alpha becomes a hard silhouette; 127 is what Figma's own export uses. */
|
|
25
|
+
function silhouette(result) {
|
|
26
|
+
return (`<feColorMatrix in="SourceAlpha" type="matrix" ` +
|
|
27
|
+
`values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="${result}"/>`);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Registers the node's filter and returns its id, or `undefined` when nothing needs one.
|
|
31
|
+
* `region` is the node's render bounds in node-local units — the filter clips its own output to
|
|
32
|
+
* it, so a region that is too small crops the shadow rather than moving it (Appendix E, R21).
|
|
33
|
+
*/
|
|
34
|
+
export function effectsFilter(node, region, out, report, guid, defKey) {
|
|
35
|
+
const effects = visibleEffects(node);
|
|
36
|
+
if (effects.length === 0)
|
|
37
|
+
return undefined;
|
|
38
|
+
const drawn = [];
|
|
39
|
+
for (const effect of effects) {
|
|
40
|
+
const type = str(effect, 'type') ?? 'DROP_SHADOW';
|
|
41
|
+
report.seen(feat.effect(type));
|
|
42
|
+
if (DRAWN.has(type)) {
|
|
43
|
+
drawn.push(effect);
|
|
44
|
+
}
|
|
45
|
+
else if (type === 'BACKGROUND_BLUR') {
|
|
46
|
+
// A backdrop blur needs what is *behind* the node, which an isolated SVG filter cannot
|
|
47
|
+
// reach. The fill is drawn flat instead.
|
|
48
|
+
report.approximated(feat.effect(type), guid);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
report.unsupported(feat.effect(type), guid);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (drawn.length === 0)
|
|
55
|
+
return undefined;
|
|
56
|
+
const parts = [];
|
|
57
|
+
let prev = 'bg';
|
|
58
|
+
parts.push('<feFlood flood-opacity="0" result="bg"/>');
|
|
59
|
+
// Drop shadows first, underneath the shape, in array order (the first is the lowest).
|
|
60
|
+
let k = 0;
|
|
61
|
+
for (const effect of drawn) {
|
|
62
|
+
if (str(effect, 'type') !== 'DROP_SHADOW')
|
|
63
|
+
continue;
|
|
64
|
+
const radius = num(effect, 'radius') ?? 0;
|
|
65
|
+
const spread = num(effect, 'spread') ?? 0;
|
|
66
|
+
const dx = num(obj(effect, 'offset'), 'x') ?? 0;
|
|
67
|
+
const dy = num(obj(effect, 'offset'), 'y') ?? 0;
|
|
68
|
+
const sigma = radius / 2;
|
|
69
|
+
const alpha = `ha${k}`;
|
|
70
|
+
parts.push(silhouette(alpha));
|
|
71
|
+
let cur = alpha;
|
|
72
|
+
if (spread > 0) {
|
|
73
|
+
parts.push(`<feMorphology in="${cur}" operator="dilate" radius="${fmt(spread)}" result="sp${k}"/>`);
|
|
74
|
+
cur = `sp${k}`;
|
|
75
|
+
}
|
|
76
|
+
else if (spread < 0) {
|
|
77
|
+
parts.push(`<feMorphology in="${cur}" operator="erode" radius="${fmt(-spread)}" result="sp${k}"/>`);
|
|
78
|
+
cur = `sp${k}`;
|
|
79
|
+
}
|
|
80
|
+
parts.push(`<feOffset in="${cur}" dx="${fmt(dx)}" dy="${fmt(dy)}" result="of${k}"/>`);
|
|
81
|
+
cur = `of${k}`;
|
|
82
|
+
if (sigma > 0) {
|
|
83
|
+
parts.push(`<feGaussianBlur in="${cur}" stdDeviation="${fmt(sigma)}" result="bl${k}"/>`);
|
|
84
|
+
cur = `bl${k}`;
|
|
85
|
+
}
|
|
86
|
+
if (bool(effect, 'showShadowBehindNode') !== true) {
|
|
87
|
+
// "Do not show the shadow behind the node": knock the silhouette back out of the blur.
|
|
88
|
+
parts.push(`<feComposite in="${cur}" in2="${alpha}" operator="out" result="ko${k}"/>`);
|
|
89
|
+
cur = `ko${k}`;
|
|
90
|
+
}
|
|
91
|
+
parts.push(colorMatrix(cur, effect, `co${k}`));
|
|
92
|
+
parts.push(`<feBlend in="co${k}" in2="${prev}" mode="normal" result="ds${k}"/>`);
|
|
93
|
+
prev = `ds${k}`;
|
|
94
|
+
k++;
|
|
95
|
+
}
|
|
96
|
+
parts.push(`<feBlend in="SourceGraphic" in2="${prev}" mode="normal" result="shape"/>`);
|
|
97
|
+
prev = 'shape';
|
|
98
|
+
for (const effect of drawn) {
|
|
99
|
+
if (str(effect, 'type') !== 'INNER_SHADOW')
|
|
100
|
+
continue;
|
|
101
|
+
const radius = num(effect, 'radius') ?? 0;
|
|
102
|
+
const spread = num(effect, 'spread') ?? 0;
|
|
103
|
+
const dx = num(obj(effect, 'offset'), 'x') ?? 0;
|
|
104
|
+
const dy = num(obj(effect, 'offset'), 'y') ?? 0;
|
|
105
|
+
const sigma = radius / 2;
|
|
106
|
+
const alpha = `ia${k}`;
|
|
107
|
+
parts.push(silhouette(alpha));
|
|
108
|
+
let cur = alpha;
|
|
109
|
+
if (spread > 0) {
|
|
110
|
+
parts.push(`<feMorphology in="${cur}" operator="erode" radius="${fmt(spread)}" result="isp${k}"/>`);
|
|
111
|
+
cur = `isp${k}`;
|
|
112
|
+
}
|
|
113
|
+
parts.push(`<feOffset in="${cur}" dx="${fmt(dx)}" dy="${fmt(dy)}" result="io${k}"/>`);
|
|
114
|
+
cur = `io${k}`;
|
|
115
|
+
if (sigma > 0) {
|
|
116
|
+
parts.push(`<feGaussianBlur in="${cur}" stdDeviation="${fmt(sigma)}" result="ib${k}"/>`);
|
|
117
|
+
cur = `ib${k}`;
|
|
118
|
+
}
|
|
119
|
+
// silhouette minus the offset, blurred silhouette = the band just inside the edge.
|
|
120
|
+
parts.push(`<feComposite in="${cur}" in2="${alpha}" operator="arithmetic" k2="-1" k3="1" result="ii${k}"/>`);
|
|
121
|
+
parts.push(colorMatrix(`ii${k}`, effect, `ic${k}`));
|
|
122
|
+
parts.push(`<feBlend in="ic${k}" in2="${prev}" mode="normal" result="is${k}"/>`);
|
|
123
|
+
prev = `is${k}`;
|
|
124
|
+
k++;
|
|
125
|
+
}
|
|
126
|
+
const blur = drawn.find((e) => str(e, 'type') === 'FOREGROUND_BLUR');
|
|
127
|
+
if (blur) {
|
|
128
|
+
const sigma = (num(blur, 'radius') ?? 0) / 2;
|
|
129
|
+
if (sigma > 0)
|
|
130
|
+
parts.push(`<feGaussianBlur in="${prev}" stdDeviation="${fmt(sigma)}"/>`);
|
|
131
|
+
}
|
|
132
|
+
return out.def(defKey, (id) => {
|
|
133
|
+
return (`<filter id="${id}" filterUnits="userSpaceOnUse" x="${fmt(region.x)}" y="${fmt(region.y)}" ` +
|
|
134
|
+
`width="${fmt(region.w)}" height="${fmt(region.h)}" color-interpolation-filters="sRGB">` +
|
|
135
|
+
`${parts.join('')}</filter>`);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The filter that turns any coverage into white-with-the-same-alpha, so a luminance `<mask>`
|
|
140
|
+
* reproduces Figma's ALPHA mask exactly (Appendix E, R9). Registered once per document.
|
|
141
|
+
*/
|
|
142
|
+
export function alphaToWhiteFilter(out) {
|
|
143
|
+
return out.def('filter:alpha-to-white', (id) => `<filter id="${id}"><feColorMatrix type="matrix" ` +
|
|
144
|
+
`values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0"/></filter>`);
|
|
145
|
+
}
|
|
146
|
+
// ------------------------------------------------------------------------------ blend modes
|
|
147
|
+
/** Figma blend mode → CSS `mix-blend-mode`. Only the two Figma-only modes are approximated. */
|
|
148
|
+
const BLEND_CSS = {
|
|
149
|
+
DARKEN: 'darken',
|
|
150
|
+
MULTIPLY: 'multiply',
|
|
151
|
+
COLOR_BURN: 'color-burn',
|
|
152
|
+
LIGHTEN: 'lighten',
|
|
153
|
+
SCREEN: 'screen',
|
|
154
|
+
COLOR_DODGE: 'color-dodge',
|
|
155
|
+
OVERLAY: 'overlay',
|
|
156
|
+
SOFT_LIGHT: 'soft-light',
|
|
157
|
+
HARD_LIGHT: 'hard-light',
|
|
158
|
+
DIFFERENCE: 'difference',
|
|
159
|
+
EXCLUSION: 'exclusion',
|
|
160
|
+
HUE: 'hue',
|
|
161
|
+
SATURATION: 'saturation',
|
|
162
|
+
COLOR: 'color',
|
|
163
|
+
LUMINOSITY: 'luminosity',
|
|
164
|
+
};
|
|
165
|
+
/** CSS has no linear-dodge / linear-burn; screen and multiply are the closest siblings. */
|
|
166
|
+
const BLEND_APPROXIMATE = {
|
|
167
|
+
LINEAR_DODGE: 'screen',
|
|
168
|
+
LINEAR_BURN: 'multiply',
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* §4.11 — the `style` attribute for a node's group, or `undefined` when nothing is needed.
|
|
172
|
+
*
|
|
173
|
+
* PASS_THROUGH (the default) lets children composite with what is behind the group; NORMAL makes
|
|
174
|
+
* the group composite as a unit, which is what `isolation: isolate` expresses. Both are verified
|
|
175
|
+
* to work in resvg (Appendix E, R16 and R17).
|
|
176
|
+
*/
|
|
177
|
+
export function blendStyle(node, isContainer, report, guid) {
|
|
178
|
+
const mode = str(node, 'blendMode') ?? 'PASS_THROUGH';
|
|
179
|
+
if (mode !== 'PASS_THROUGH')
|
|
180
|
+
report.seen(feat.blend(mode));
|
|
181
|
+
if (mode === 'PASS_THROUGH')
|
|
182
|
+
return undefined;
|
|
183
|
+
if (mode === 'NORMAL')
|
|
184
|
+
return isContainer ? 'isolation:isolate' : undefined;
|
|
185
|
+
const exact = BLEND_CSS[mode];
|
|
186
|
+
if (exact) {
|
|
187
|
+
return isContainer ? `mix-blend-mode:${exact};isolation:isolate` : `mix-blend-mode:${exact}`;
|
|
188
|
+
}
|
|
189
|
+
const near = BLEND_APPROXIMATE[mode];
|
|
190
|
+
if (near) {
|
|
191
|
+
report.approximated(feat.blend(mode), guid);
|
|
192
|
+
return isContainer ? `mix-blend-mode:${near};isolation:isolate` : `mix-blend-mode:${near}`;
|
|
193
|
+
}
|
|
194
|
+
report.unsupported(feat.blend(mode), guid);
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The exporter (plan §4.4): a node subtree → one SVG document.
|
|
3
|
+
*
|
|
4
|
+
* This is the project-specific part. Everything it draws comes from geometry Figma already
|
|
5
|
+
* derived at save time (F3, F5, F17): outlined strokes, combined booleans, per-glyph outlines
|
|
6
|
+
* and per-instance resolved geometry — so no vector networks, font files, boolean solvers or
|
|
7
|
+
* auto-layout engine are involved.
|
|
8
|
+
*
|
|
9
|
+
* Rendering degrades, it never crashes (ground rule 6): a node that cannot be drawn is skipped
|
|
10
|
+
* and recorded in the report.
|
|
11
|
+
*/
|
|
12
|
+
import type { CacheEntry } from '../cache.js';
|
|
13
|
+
import { type TreeNode } from '../model/tree.js';
|
|
14
|
+
import { type Box } from './matrix.js';
|
|
15
|
+
import { ReportBuilder } from './report.js';
|
|
16
|
+
export declare const DEFAULT_MAX_NODES = 20000;
|
|
17
|
+
export declare const HARD_MAX_NODES = 60000;
|
|
18
|
+
/**
|
|
19
|
+
* `maxNodes` counts nodes in the TREE, which is the number a caller can see with fig_tree. The
|
|
20
|
+
* traversal visits many more, because every INSTANCE expands into the symbol it points at
|
|
21
|
+
* (F17) — page `0:1` is 14 912 tree nodes but walks several times that. So the runtime guard is
|
|
22
|
+
* a separate, larger budget derived from `maxNodes`.
|
|
23
|
+
*/
|
|
24
|
+
export declare const VISIT_FACTOR = 12;
|
|
25
|
+
export declare const HARD_MAX_VISITS = 400000;
|
|
26
|
+
/** A render whose SVG exceeds this is refused rather than handed to the rasterizer. */
|
|
27
|
+
export declare const MAX_SVG_BYTES: number;
|
|
28
|
+
/** A stop that must abort the whole render rather than being isolated to one node. */
|
|
29
|
+
export declare class RenderBudgetError extends Error {
|
|
30
|
+
}
|
|
31
|
+
/** Where a drawn node ended up, in root-local units — input to the tester's attribution. */
|
|
32
|
+
export interface NodeBox {
|
|
33
|
+
readonly guid: string;
|
|
34
|
+
readonly type: string;
|
|
35
|
+
readonly name?: string;
|
|
36
|
+
readonly box: Box;
|
|
37
|
+
}
|
|
38
|
+
export interface ExportOptions {
|
|
39
|
+
readonly maxNodes?: number;
|
|
40
|
+
readonly collectBoxes?: boolean;
|
|
41
|
+
/** CSS colour painted behind everything; omitted for a transparent render. */
|
|
42
|
+
readonly background?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface ExportResult {
|
|
45
|
+
readonly svg: string;
|
|
46
|
+
readonly bounds: Box;
|
|
47
|
+
readonly report: ReportBuilder;
|
|
48
|
+
readonly boxes: NodeBox[];
|
|
49
|
+
}
|
|
50
|
+
export declare function exportSubtree(entry: CacheEntry, root: TreeNode, opts?: ExportOptions): ExportResult;
|
|
51
|
+
//# sourceMappingURL=export.d.ts.map
|