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,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Appendix D. Parameterised keys are formed with the helpers below so a typo cannot silently
|
|
3
|
+
* create a new feature that the coverage matrix will never recognise.
|
|
4
|
+
*/
|
|
5
|
+
export const FEATURES = [
|
|
6
|
+
'image-missing',
|
|
7
|
+
'image-crop',
|
|
8
|
+
'image-rotation',
|
|
9
|
+
'image-filters',
|
|
10
|
+
'gradient-singular',
|
|
11
|
+
'emoji',
|
|
12
|
+
'glyph-rotation',
|
|
13
|
+
'text-without-outlines',
|
|
14
|
+
'text-property-without-outlines',
|
|
15
|
+
'text-stroke',
|
|
16
|
+
'text-decoration',
|
|
17
|
+
'text-truncation',
|
|
18
|
+
'stroke-dashed',
|
|
19
|
+
'stroke-without-geometry',
|
|
20
|
+
'vector-without-geometry',
|
|
21
|
+
'geometry:corrupt',
|
|
22
|
+
'geometry:synthesised',
|
|
23
|
+
'mask-hidden',
|
|
24
|
+
'oversize',
|
|
25
|
+
'svg-rejected',
|
|
26
|
+
'instance-unresolved',
|
|
27
|
+
'instance-recursive',
|
|
28
|
+
'instance-property',
|
|
29
|
+
'instance-swap',
|
|
30
|
+
'node-failed',
|
|
31
|
+
];
|
|
32
|
+
/** Prefixes of the parameterised keys built by `feat` below. */
|
|
33
|
+
export const FEATURE_PREFIXES = [
|
|
34
|
+
'node-type:',
|
|
35
|
+
'paint:',
|
|
36
|
+
'effect:',
|
|
37
|
+
'blend:',
|
|
38
|
+
'mask:',
|
|
39
|
+
'stroke-align:',
|
|
40
|
+
'image-mode:',
|
|
41
|
+
'image-format:',
|
|
42
|
+
];
|
|
43
|
+
/** True when `feature` is part of the frozen vocabulary; used to guard against typos. */
|
|
44
|
+
export function isKnownFeature(feature) {
|
|
45
|
+
if (FEATURES.includes(feature))
|
|
46
|
+
return true;
|
|
47
|
+
return FEATURE_PREFIXES.some((p) => feature.startsWith(p) && feature.length > p.length);
|
|
48
|
+
}
|
|
49
|
+
export const feat = {
|
|
50
|
+
nodeType: (type) => `node-type:${type}`,
|
|
51
|
+
paint: (type) => `paint:${type}`,
|
|
52
|
+
effect: (type) => `effect:${type}`,
|
|
53
|
+
blend: (mode) => `blend:${mode}`,
|
|
54
|
+
mask: (type) => `mask:${type}`,
|
|
55
|
+
strokeAlign: (align) => `stroke-align:${align}`,
|
|
56
|
+
imageMode: (mode) => `image-mode:${mode}`,
|
|
57
|
+
imageFormat: (mime) => `image-format:${mime}`,
|
|
58
|
+
};
|
|
59
|
+
const MAX_EXAMPLES = 5;
|
|
60
|
+
export class ReportBuilder {
|
|
61
|
+
nodesVisited = 0;
|
|
62
|
+
nodesDrawn = 0;
|
|
63
|
+
unsupportedBuckets = new Map();
|
|
64
|
+
approximatedBuckets = new Map();
|
|
65
|
+
present = new Set();
|
|
66
|
+
static bump(map, feature, guid) {
|
|
67
|
+
let bucket = map.get(feature);
|
|
68
|
+
if (!bucket) {
|
|
69
|
+
bucket = { count: 0, examples: [] };
|
|
70
|
+
map.set(feature, bucket);
|
|
71
|
+
}
|
|
72
|
+
bucket.count += 1;
|
|
73
|
+
if (guid && bucket.examples.length < MAX_EXAMPLES && !bucket.examples.includes(guid)) {
|
|
74
|
+
bucket.examples.push(guid);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** Not drawn at all. Always also record it as present. */
|
|
78
|
+
unsupported(feature, guid) {
|
|
79
|
+
ReportBuilder.bump(this.unsupportedBuckets, feature, guid);
|
|
80
|
+
this.present.add(feature);
|
|
81
|
+
}
|
|
82
|
+
/** Drawn, but not exactly like Figma. */
|
|
83
|
+
approximated(feature, guid) {
|
|
84
|
+
ReportBuilder.bump(this.approximatedBuckets, feature, guid);
|
|
85
|
+
this.present.add(feature);
|
|
86
|
+
}
|
|
87
|
+
/** Record a feature as occurring in the subtree, whether or not it was drawn exactly. */
|
|
88
|
+
seen(feature) {
|
|
89
|
+
this.present.add(feature);
|
|
90
|
+
}
|
|
91
|
+
static entries(map) {
|
|
92
|
+
return [...map.entries()]
|
|
93
|
+
.map(([feature, b]) => ({ feature, count: b.count, examples: b.examples }))
|
|
94
|
+
.sort((a, b) => b.count - a.count || a.feature.localeCompare(b.feature));
|
|
95
|
+
}
|
|
96
|
+
finish(base) {
|
|
97
|
+
return {
|
|
98
|
+
...base,
|
|
99
|
+
nodesVisited: this.nodesVisited,
|
|
100
|
+
nodesDrawn: this.nodesDrawn,
|
|
101
|
+
unsupported: ReportBuilder.entries(this.unsupportedBuckets),
|
|
102
|
+
approximated: ReportBuilder.entries(this.approximatedBuckets),
|
|
103
|
+
featuresPresent: [...this.present].sort(),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colour and effect styles (plan F19).
|
|
3
|
+
*
|
|
4
|
+
* A node that references a local style — `styleIdForFill`, `styleIdForStrokeFill`,
|
|
5
|
+
* `styleIdForEffect` — draws the STYLE's current paints or effects. The copy cached on the node
|
|
6
|
+
* beside the reference is what the node looked like when it was last touched, and it can be
|
|
7
|
+
* stale (1 779 nodes and 1 064 override records in the sample disagree with their style) or
|
|
8
|
+
* missing altogether (3 905 override records and 265 text-run entries carry only the reference).
|
|
9
|
+
* Measured on the sample's card icon: Figma's export is #333333, the "Black" style, while the
|
|
10
|
+
* override's cached paint says #F18D00.
|
|
11
|
+
*
|
|
12
|
+
* A style that is not in the file (a library style) cannot be followed, and neither can the
|
|
13
|
+
* "no style" sentinel a detached override writes; both leave the cached copy in charge.
|
|
14
|
+
*/
|
|
15
|
+
import type { KiwiObject } from '../fig/kiwi.js';
|
|
16
|
+
import type { NodeChange } from '../fig/parse.js';
|
|
17
|
+
import type { FileIndex } from '../model/index.js';
|
|
18
|
+
/** The paints of a local colour style, or undefined when the reference cannot be followed. */
|
|
19
|
+
export declare function stylePaints(index: FileIndex, ref: unknown): KiwiObject[] | undefined;
|
|
20
|
+
/** The effects of a local effect style, or undefined when the reference cannot be followed. */
|
|
21
|
+
export declare function styleEffects(index: FileIndex, ref: unknown): KiwiObject[] | undefined;
|
|
22
|
+
/** The node with every style reference it carries resolved to the style's live value. */
|
|
23
|
+
export declare function applyStyles(index: FileIndex, node: NodeChange): NodeChange;
|
|
24
|
+
/**
|
|
25
|
+
* §4.7.5 — a `Path.styleID`, or the run style of a glyph, selects an entry of the node's
|
|
26
|
+
* `styleOverrideTable`, which carries only the fields that differ from the node's own. An entry
|
|
27
|
+
* may name a colour style instead of carrying paints, so the style is followed first. Returns
|
|
28
|
+
* undefined when nothing overrides the field, and the caller falls back to the node's paints.
|
|
29
|
+
*/
|
|
30
|
+
export declare function runPaints(index: FileIndex, node: NodeChange, styleID: number | undefined, field: 'fillPaints' | 'strokePaints'): KiwiObject[] | undefined;
|
|
31
|
+
//# sourceMappingURL=style.d.ts.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { guidKey } from '../model/tree.js';
|
|
2
|
+
import { num, obj, objArr, str } from '../model/access.js';
|
|
3
|
+
function styleNode(index, ref, styleType) {
|
|
4
|
+
if (!ref || typeof ref !== 'object')
|
|
5
|
+
return undefined;
|
|
6
|
+
const r = ref;
|
|
7
|
+
const local = guidKey(obj(r, 'guid'));
|
|
8
|
+
const t = index.resolveAssetRef(r) ?? (local ? index.node(local) : undefined);
|
|
9
|
+
return t && str(t.node, 'styleType') === styleType ? t : undefined;
|
|
10
|
+
}
|
|
11
|
+
/** The paints of a local colour style, or undefined when the reference cannot be followed. */
|
|
12
|
+
export function stylePaints(index, ref) {
|
|
13
|
+
const t = styleNode(index, ref, 'FILL');
|
|
14
|
+
if (!t || !Array.isArray(t.node['fillPaints']))
|
|
15
|
+
return undefined;
|
|
16
|
+
return objArr(t.node, 'fillPaints');
|
|
17
|
+
}
|
|
18
|
+
/** The effects of a local effect style, or undefined when the reference cannot be followed. */
|
|
19
|
+
export function styleEffects(index, ref) {
|
|
20
|
+
const t = styleNode(index, ref, 'EFFECT');
|
|
21
|
+
if (!t || !Array.isArray(t.node['effects']))
|
|
22
|
+
return undefined;
|
|
23
|
+
return objArr(t.node, 'effects');
|
|
24
|
+
}
|
|
25
|
+
/** The node with every style reference it carries resolved to the style's live value. */
|
|
26
|
+
export function applyStyles(index, node) {
|
|
27
|
+
const fill = stylePaints(index, node['styleIdForFill']);
|
|
28
|
+
const stroke = stylePaints(index, node['styleIdForStrokeFill']);
|
|
29
|
+
const effects = styleEffects(index, node['styleIdForEffect']);
|
|
30
|
+
if (!fill && !stroke && !effects)
|
|
31
|
+
return node;
|
|
32
|
+
const out = { ...node };
|
|
33
|
+
if (fill)
|
|
34
|
+
out['fillPaints'] = fill;
|
|
35
|
+
if (stroke)
|
|
36
|
+
out['strokePaints'] = stroke;
|
|
37
|
+
if (effects)
|
|
38
|
+
out['effects'] = effects;
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* §4.7.5 — a `Path.styleID`, or the run style of a glyph, selects an entry of the node's
|
|
43
|
+
* `styleOverrideTable`, which carries only the fields that differ from the node's own. An entry
|
|
44
|
+
* may name a colour style instead of carrying paints, so the style is followed first. Returns
|
|
45
|
+
* undefined when nothing overrides the field, and the caller falls back to the node's paints.
|
|
46
|
+
*/
|
|
47
|
+
export function runPaints(index, node, styleID, field) {
|
|
48
|
+
if (!styleID)
|
|
49
|
+
return undefined;
|
|
50
|
+
const styleField = field === 'fillPaints' ? 'styleIdForFill' : 'styleIdForStrokeFill';
|
|
51
|
+
const tables = [
|
|
52
|
+
objArr(obj(node, 'vectorData'), 'styleOverrideTable'),
|
|
53
|
+
objArr(obj(node, 'textData'), 'styleOverrideTable'),
|
|
54
|
+
];
|
|
55
|
+
for (const table of tables) {
|
|
56
|
+
for (const override of table) {
|
|
57
|
+
if (num(override, 'styleID') !== styleID)
|
|
58
|
+
continue;
|
|
59
|
+
const styled = stylePaints(index, override[styleField]);
|
|
60
|
+
if (styled)
|
|
61
|
+
return styled;
|
|
62
|
+
if (Array.isArray(override[field]))
|
|
63
|
+
return objArr(override, field);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SVG serialization (plan §4.3): escaping, number formatting, a well-formedness-checked writer
|
|
3
|
+
* and a `<defs>` registry that de-duplicates gradients, patterns, filters, clips and masks.
|
|
4
|
+
*
|
|
5
|
+
* `fmt` throws on a non-finite number on purpose. resvg does NOT reject a `NaN` attribute — it
|
|
6
|
+
* silently drops that element and renders everything else (Appendix E, R24), so this guard is
|
|
7
|
+
* the only place such a bug can be caught.
|
|
8
|
+
*/
|
|
9
|
+
import type { Box, Mat } from './matrix.js';
|
|
10
|
+
export type Attrs = Record<string, string | number | undefined>;
|
|
11
|
+
export declare function esc(s: string): string;
|
|
12
|
+
/** 3 decimals, trailing zeros stripped, `-0` normalised to `0`. Throws on NaN/Infinity. */
|
|
13
|
+
export declare function fmt(n: number): string;
|
|
14
|
+
/** `matrix(a b c d e f)` for a `transform` attribute. */
|
|
15
|
+
export declare function toAttr(m: Mat): string;
|
|
16
|
+
export interface FinishOptions {
|
|
17
|
+
/** Solid page background painted behind everything; omitted for transparent output. */
|
|
18
|
+
readonly background?: string;
|
|
19
|
+
/** Pixel dimensions of the output (the viewBox is in user units). */
|
|
20
|
+
readonly width: number;
|
|
21
|
+
readonly height: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Streaming writer. The tag stack is checked on every `close`, so a malformed document is a
|
|
25
|
+
* loud error here rather than a mysterious parse failure inside the rasterizer.
|
|
26
|
+
*/
|
|
27
|
+
export declare class SvgWriter {
|
|
28
|
+
private body;
|
|
29
|
+
private stack;
|
|
30
|
+
private readonly defsBody;
|
|
31
|
+
private readonly defsByKey;
|
|
32
|
+
private idCounter;
|
|
33
|
+
/** Ids are `p1`, `p2`, … — never derived from layer names (which are arbitrary text). */
|
|
34
|
+
nextId(): string;
|
|
35
|
+
get depth(): number;
|
|
36
|
+
open(tag: string, attrs?: Attrs): void;
|
|
37
|
+
close(tag: string): void;
|
|
38
|
+
element(tag: string, attrs?: Attrs, inner?: string): void;
|
|
39
|
+
/** Pre-serialized markup. The caller is responsible for its escaping and balance. */
|
|
40
|
+
raw(markup: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* Close every element opened past `depth`. Used to recover from a node that threw halfway
|
|
43
|
+
* through: the document stays well-formed and the rest of the render continues.
|
|
44
|
+
*/
|
|
45
|
+
unwindTo(depth: number): void;
|
|
46
|
+
/**
|
|
47
|
+
* Register a `<defs>` entry once per `key` and return its id. `build` receives the id it must
|
|
48
|
+
* use. Re-entrant: `build` may itself register defs (a mask whose content needs a clipPath).
|
|
49
|
+
*/
|
|
50
|
+
def(key: string, build: (id: string) => string): string;
|
|
51
|
+
/**
|
|
52
|
+
* Run `fn` with output redirected to a fresh buffer and return what it wrote. Used to render
|
|
53
|
+
* mask and clip content into a `<defs>` entry through the ordinary emit path.
|
|
54
|
+
*/
|
|
55
|
+
capture(fn: () => void): string;
|
|
56
|
+
/** Total characters written so far — checked against MAX_SVG_BYTES while exporting. */
|
|
57
|
+
get length(): number;
|
|
58
|
+
finish(view: Box, opts: FinishOptions): string;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=svg.d.ts.map
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
const ESCAPES = {
|
|
2
|
+
'&': '&',
|
|
3
|
+
'<': '<',
|
|
4
|
+
'>': '>',
|
|
5
|
+
'"': '"',
|
|
6
|
+
"'": ''',
|
|
7
|
+
};
|
|
8
|
+
export function esc(s) {
|
|
9
|
+
return s.replace(/[&<>"']/g, (ch) => ESCAPES[ch] ?? ch);
|
|
10
|
+
}
|
|
11
|
+
/** 3 decimals, trailing zeros stripped, `-0` normalised to `0`. Throws on NaN/Infinity. */
|
|
12
|
+
export function fmt(n) {
|
|
13
|
+
if (!Number.isFinite(n)) {
|
|
14
|
+
throw new Error(`non-finite number in SVG output: ${String(n)}`);
|
|
15
|
+
}
|
|
16
|
+
const r = Math.round(n * 1000) / 1000;
|
|
17
|
+
if (r === 0)
|
|
18
|
+
return '0';
|
|
19
|
+
// Very large magnitudes would come back in exponential form, which SVG does not accept.
|
|
20
|
+
if (Math.abs(r) >= 1e15)
|
|
21
|
+
return r.toFixed(0);
|
|
22
|
+
let s = r.toFixed(3);
|
|
23
|
+
if (s.includes('.'))
|
|
24
|
+
s = s.replace(/0+$/, '').replace(/\.$/, '');
|
|
25
|
+
return s === '-0' ? '0' : s;
|
|
26
|
+
}
|
|
27
|
+
/** `matrix(a b c d e f)` for a `transform` attribute. */
|
|
28
|
+
export function toAttr(m) {
|
|
29
|
+
return `matrix(${fmt(m.a)} ${fmt(m.b)} ${fmt(m.c)} ${fmt(m.d)} ${fmt(m.e)} ${fmt(m.f)})`;
|
|
30
|
+
}
|
|
31
|
+
function attrsToString(attrs) {
|
|
32
|
+
if (!attrs)
|
|
33
|
+
return '';
|
|
34
|
+
let out = '';
|
|
35
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
36
|
+
if (v === undefined)
|
|
37
|
+
continue;
|
|
38
|
+
const text = typeof v === 'number' ? fmt(v) : esc(v);
|
|
39
|
+
out += ` ${k}="${text}"`;
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Streaming writer. The tag stack is checked on every `close`, so a malformed document is a
|
|
45
|
+
* loud error here rather than a mysterious parse failure inside the rasterizer.
|
|
46
|
+
*/
|
|
47
|
+
export class SvgWriter {
|
|
48
|
+
body = [];
|
|
49
|
+
stack = [];
|
|
50
|
+
defsBody = [];
|
|
51
|
+
defsByKey = new Map();
|
|
52
|
+
idCounter = 0;
|
|
53
|
+
/** Ids are `p1`, `p2`, … — never derived from layer names (which are arbitrary text). */
|
|
54
|
+
nextId() {
|
|
55
|
+
this.idCounter += 1;
|
|
56
|
+
return `p${this.idCounter}`;
|
|
57
|
+
}
|
|
58
|
+
get depth() {
|
|
59
|
+
return this.stack.length;
|
|
60
|
+
}
|
|
61
|
+
open(tag, attrs) {
|
|
62
|
+
this.body.push(`<${tag}${attrsToString(attrs)}>`);
|
|
63
|
+
this.stack.push(tag);
|
|
64
|
+
}
|
|
65
|
+
close(tag) {
|
|
66
|
+
const open = this.stack.pop();
|
|
67
|
+
if (open !== tag) {
|
|
68
|
+
throw new Error(`SVG nesting error: closed <${tag}> while <${open ?? 'nothing'}> was open`);
|
|
69
|
+
}
|
|
70
|
+
this.body.push(`</${tag}>`);
|
|
71
|
+
}
|
|
72
|
+
element(tag, attrs, inner) {
|
|
73
|
+
if (inner === undefined) {
|
|
74
|
+
this.body.push(`<${tag}${attrsToString(attrs)}/>`);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this.body.push(`<${tag}${attrsToString(attrs)}>${inner}</${tag}>`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Pre-serialized markup. The caller is responsible for its escaping and balance. */
|
|
81
|
+
raw(markup) {
|
|
82
|
+
this.body.push(markup);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Close every element opened past `depth`. Used to recover from a node that threw halfway
|
|
86
|
+
* through: the document stays well-formed and the rest of the render continues.
|
|
87
|
+
*/
|
|
88
|
+
unwindTo(depth) {
|
|
89
|
+
while (this.stack.length > depth) {
|
|
90
|
+
const tag = this.stack.pop();
|
|
91
|
+
this.body.push(`</${tag}>`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Register a `<defs>` entry once per `key` and return its id. `build` receives the id it must
|
|
96
|
+
* use. Re-entrant: `build` may itself register defs (a mask whose content needs a clipPath).
|
|
97
|
+
*/
|
|
98
|
+
def(key, build) {
|
|
99
|
+
const existing = this.defsByKey.get(key);
|
|
100
|
+
if (existing)
|
|
101
|
+
return existing;
|
|
102
|
+
const id = this.nextId();
|
|
103
|
+
this.defsByKey.set(key, id);
|
|
104
|
+
const slot = this.defsBody.length;
|
|
105
|
+
this.defsBody.push(undefined); // reserve the slot before building, so nesting keeps order
|
|
106
|
+
this.defsBody[slot] = build(id);
|
|
107
|
+
return id;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Run `fn` with output redirected to a fresh buffer and return what it wrote. Used to render
|
|
111
|
+
* mask and clip content into a `<defs>` entry through the ordinary emit path.
|
|
112
|
+
*/
|
|
113
|
+
capture(fn) {
|
|
114
|
+
const savedBody = this.body;
|
|
115
|
+
const savedDepth = this.stack.length;
|
|
116
|
+
this.body = [];
|
|
117
|
+
try {
|
|
118
|
+
fn();
|
|
119
|
+
if (this.stack.length !== savedDepth) {
|
|
120
|
+
throw new Error(`SVG nesting error: capture left ${this.stack.length - savedDepth} element(s) open`);
|
|
121
|
+
}
|
|
122
|
+
return this.body.join('');
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
this.body = savedBody;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** Total characters written so far — checked against MAX_SVG_BYTES while exporting. */
|
|
129
|
+
get length() {
|
|
130
|
+
let n = 0;
|
|
131
|
+
for (const s of this.body)
|
|
132
|
+
n += s.length;
|
|
133
|
+
for (const s of this.defsBody)
|
|
134
|
+
n += s ? s.length : 0;
|
|
135
|
+
return n;
|
|
136
|
+
}
|
|
137
|
+
finish(view, opts) {
|
|
138
|
+
if (this.stack.length) {
|
|
139
|
+
throw new Error(`SVG nesting error: <${this.stack.join('>, <')}> left open`);
|
|
140
|
+
}
|
|
141
|
+
const head = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"' +
|
|
142
|
+
` width="${fmt(opts.width)}" height="${fmt(opts.height)}"` +
|
|
143
|
+
` viewBox="${fmt(view.x)} ${fmt(view.y)} ${fmt(view.w)} ${fmt(view.h)}">`;
|
|
144
|
+
const defs = this.defsBody.length ? `<defs>${this.defsBody.join('')}</defs>` : '';
|
|
145
|
+
const bg = opts.background
|
|
146
|
+
? `<rect x="${fmt(view.x)}" y="${fmt(view.y)}" width="${fmt(view.w)}" height="${fmt(view.h)}" fill="${esc(opts.background)}"/>`
|
|
147
|
+
: '';
|
|
148
|
+
return `${head}${defs}${bg}${this.body.join('')}</svg>`;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text → outlined paths (plan §4.8, fact F5).
|
|
3
|
+
*
|
|
4
|
+
* Figma stores a per-glyph outline for every text node, so no font file is ever needed. The
|
|
5
|
+
* outline blob uses the same command grammar as F4 but in **em units with y pointing up**, while
|
|
6
|
+
* `position` is the pen point on the baseline in node-local pixels with kerning already applied.
|
|
7
|
+
* A glyph point (gx, gy) therefore lands at:
|
|
8
|
+
*
|
|
9
|
+
* X = position.x + gx · fontSize
|
|
10
|
+
* Y = position.y − gy · fontSize ← note the sign flip
|
|
11
|
+
*
|
|
12
|
+
* Verified on the digit "2" (node 2:1336): its flat base bar sits at em y = 0 and its top arc at
|
|
13
|
+
* y = 0.751, which at fontSize 12 from a baseline at y = 12.72 puts the ink in rows 3..13 of an
|
|
14
|
+
* 18-pixel-tall box. Getting the sign wrong mirrors every glyph below its baseline.
|
|
15
|
+
*
|
|
16
|
+
* Do NOT use `advance` to place glyphs — `position` already includes kerning (Pitfall 12).
|
|
17
|
+
*
|
|
18
|
+
* Two more things the derived data encodes (F21):
|
|
19
|
+
* - Truncation. `truncationStartIndex` indexes the GLYPH array, not the characters: the glyphs
|
|
20
|
+
* from that index on are the cut-off tail, and the one just before it is the ellipsis Figma
|
|
21
|
+
* inserted (which has no `firstCharacter`). All 920 truncated texts in the sample agree.
|
|
22
|
+
* - Run styles. A glyph's `styleID` is never set for a styled run (12 576 of 12 576 in the
|
|
23
|
+
* sample); the run is `textData.characterStyleIDs[firstCharacter]`, one entry per character
|
|
24
|
+
* with an implicit 0 past the end of the array.
|
|
25
|
+
*/
|
|
26
|
+
import type { NodeChange } from '../fig/parse.js';
|
|
27
|
+
import type { Box } from './matrix.js';
|
|
28
|
+
import { type ReportBuilder } from './report.js';
|
|
29
|
+
/** Glyph outlines sharing one `styleID`, already flattened to node-local pixel path data. */
|
|
30
|
+
export interface GlyphRun {
|
|
31
|
+
readonly styleID: number;
|
|
32
|
+
readonly d: string;
|
|
33
|
+
}
|
|
34
|
+
export interface DecorationRun {
|
|
35
|
+
readonly styleID: number;
|
|
36
|
+
readonly rects: readonly Box[];
|
|
37
|
+
}
|
|
38
|
+
export interface TextDraw {
|
|
39
|
+
readonly glyphs: GlyphRun[];
|
|
40
|
+
readonly decorations: DecorationRun[];
|
|
41
|
+
}
|
|
42
|
+
export type BlobLookup = (index: number | undefined) => Uint8Array | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* `undefined` when the node carries no glyph outlines at all — the caller decides whether that
|
|
45
|
+
* is an empty text node (draw nothing) or a missing-outline case worth reporting.
|
|
46
|
+
*/
|
|
47
|
+
export declare function buildText(node: NodeChange, blob: BlobLookup, report: ReportBuilder, guid: string): TextDraw | undefined;
|
|
48
|
+
/** True when the node has text to draw but no outlines for it (§4.14). */
|
|
49
|
+
export declare function hasCharactersWithoutOutlines(node: NodeChange): boolean;
|
|
50
|
+
//# sourceMappingURL=text.d.ts.map
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { arr, num, obj, objArr, str } from '../model/access.js';
|
|
2
|
+
import { decodeCommands, toPathData } from './path.js';
|
|
3
|
+
import {} from './report.js';
|
|
4
|
+
/**
|
|
5
|
+
* `undefined` when the node carries no glyph outlines at all — the caller decides whether that
|
|
6
|
+
* is an empty text node (draw nothing) or a missing-outline case worth reporting.
|
|
7
|
+
*/
|
|
8
|
+
export function buildText(node, blob, report, guid) {
|
|
9
|
+
const derived = obj(node, 'derivedTextData');
|
|
10
|
+
const glyphs = objArr(derived, 'glyphs');
|
|
11
|
+
if (!derived || glyphs.length === 0)
|
|
12
|
+
return undefined;
|
|
13
|
+
const cut = num(derived, 'truncationStartIndex');
|
|
14
|
+
const truncated = cut !== undefined && cut >= 0;
|
|
15
|
+
if (truncated)
|
|
16
|
+
report.seen('text-truncation');
|
|
17
|
+
const runStyles = arr(obj(node, 'textData'), 'characterStyleIDs') ?? [];
|
|
18
|
+
const byStyle = new Map();
|
|
19
|
+
let rotationReported = false;
|
|
20
|
+
for (const [i, glyph] of glyphs.entries()) {
|
|
21
|
+
if (truncated && i >= cut)
|
|
22
|
+
break;
|
|
23
|
+
if ((arr(glyph, 'emojiCodePoints') ?? []).length > 0) {
|
|
24
|
+
// The outline blob of an emoji is empty; the artwork is a bitmap set we do not decode.
|
|
25
|
+
report.approximated('emoji', guid);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const rotation = num(glyph, 'rotation') ?? 0;
|
|
29
|
+
if (rotation !== 0 && !rotationReported) {
|
|
30
|
+
report.approximated('glyph-rotation', guid);
|
|
31
|
+
rotationReported = true;
|
|
32
|
+
}
|
|
33
|
+
const raw = blob(num(glyph, 'commandsBlob'));
|
|
34
|
+
if (!raw || raw.length === 0)
|
|
35
|
+
continue;
|
|
36
|
+
let cmds;
|
|
37
|
+
try {
|
|
38
|
+
cmds = decodeCommands(raw);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
report.unsupported('geometry:corrupt', guid);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (cmds.length === 0)
|
|
45
|
+
continue;
|
|
46
|
+
const fontSize = num(glyph, 'fontSize') ?? 0;
|
|
47
|
+
if (fontSize === 0)
|
|
48
|
+
continue;
|
|
49
|
+
const position = obj(glyph, 'position');
|
|
50
|
+
const d = toPathData(cmds, {
|
|
51
|
+
a: fontSize,
|
|
52
|
+
b: 0,
|
|
53
|
+
c: 0,
|
|
54
|
+
d: -fontSize, // em units are y-up; node-local pixels are y-down
|
|
55
|
+
e: num(position, 'x') ?? 0,
|
|
56
|
+
f: num(position, 'y') ?? 0,
|
|
57
|
+
});
|
|
58
|
+
const first = num(glyph, 'firstCharacter');
|
|
59
|
+
const runStyle = first === undefined ? undefined : runStyles[first];
|
|
60
|
+
const styleID = (typeof runStyle === 'number' ? runStyle : undefined) ?? num(glyph, 'styleID') ?? 0;
|
|
61
|
+
const bucket = byStyle.get(styleID);
|
|
62
|
+
if (bucket)
|
|
63
|
+
bucket.push(d);
|
|
64
|
+
else
|
|
65
|
+
byStyle.set(styleID, [d]);
|
|
66
|
+
}
|
|
67
|
+
const decorations = [];
|
|
68
|
+
for (const decoration of objArr(derived, 'decorations')) {
|
|
69
|
+
const rects = [];
|
|
70
|
+
for (const rect of objArr(decoration, 'rects')) {
|
|
71
|
+
const w = num(rect, 'w') ?? 0;
|
|
72
|
+
const h = num(rect, 'h') ?? 0;
|
|
73
|
+
if (w <= 0 || h <= 0)
|
|
74
|
+
continue;
|
|
75
|
+
rects.push({ x: num(rect, 'x') ?? 0, y: num(rect, 'y') ?? 0, w, h });
|
|
76
|
+
}
|
|
77
|
+
if (rects.length)
|
|
78
|
+
decorations.push({ styleID: num(decoration, 'styleID') ?? 0, rects });
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
// One path per style run keeps the SVG small: 137 glyphs become one or two <path> elements.
|
|
82
|
+
glyphs: [...byStyle.entries()].map(([styleID, parts]) => ({ styleID, d: parts.join(' ') })),
|
|
83
|
+
decorations,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** True when the node has text to draw but no outlines for it (§4.14). */
|
|
87
|
+
export function hasCharactersWithoutOutlines(node) {
|
|
88
|
+
const characters = str(obj(node, 'textData'), 'characters');
|
|
89
|
+
return characters !== undefined && characters.length > 0;
|
|
90
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "figme-mcp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "MCP server that reads local Figma .fig files, fully offline",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=22.15"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"figme-mcp": "dist/mcp/server.js"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"!dist/**/*.map",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc -p tsconfig.json",
|
|
21
|
+
"prepublishOnly": "npm run build",
|
|
22
|
+
"typecheck": "tsc -p tsconfig.test.json",
|
|
23
|
+
"test": "npm run build && node --test",
|
|
24
|
+
"test:fast": "node --test",
|
|
25
|
+
"start": "node dist/mcp/server.js",
|
|
26
|
+
"smoke": "node scripts/smoke.mjs",
|
|
27
|
+
"crosscheck": "node --max-old-space-size=8192 scripts/crosscheck.mjs",
|
|
28
|
+
"render": "node scripts/render.mjs",
|
|
29
|
+
"visual": "node scripts/visual.mjs",
|
|
30
|
+
"visual:update": "node scripts/visual.mjs --update"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
34
|
+
"zod": "^4.5.4"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^26.4.0",
|
|
38
|
+
"@types/pngjs": "^6.0.5",
|
|
39
|
+
"kiwi-schema": "^0.5.0",
|
|
40
|
+
"pixelmatch": "^7.2.0",
|
|
41
|
+
"pngjs": "^7.0.0",
|
|
42
|
+
"typescript": "^7.0.2"
|
|
43
|
+
},
|
|
44
|
+
"optionalDependencies": {
|
|
45
|
+
"@resvg/resvg-wasm": "2.6.2"
|
|
46
|
+
}
|
|
47
|
+
}
|