@moondraft/import 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/README.md +2 -2
- package/SOURCE.md +343 -4
- package/dist/adapter/convert.d.ts +26 -2
- package/dist/adapter/convert.js +817 -217
- package/dist/adapter/figma-descriptions.d.ts +7 -0
- package/dist/adapter/figma-descriptions.js +34 -0
- package/dist/adapter/figma-grids.d.ts +12 -0
- package/dist/adapter/figma-grids.js +175 -0
- package/dist/adapter/figma-handoff.d.ts +34 -0
- package/dist/adapter/figma-handoff.js +188 -0
- package/dist/adapter/figma-hyperlinks.d.ts +13 -0
- package/dist/adapter/figma-hyperlinks.js +77 -0
- package/dist/adapter/figma.d.ts +4 -1
- package/dist/adapter/figma.js +15 -4
- package/dist/adapter/file-components.d.ts +10 -3
- package/dist/adapter/file-components.js +126 -33
- package/dist/adapter/file-resources.js +545 -47
- package/dist/adapter/file-typography.d.ts +6 -0
- package/dist/adapter/file-typography.js +44 -0
- package/dist/adapter/image-crop.d.ts +18 -0
- package/dist/adapter/image-crop.js +30 -0
- package/dist/adapter/link.d.ts +12 -1
- package/dist/adapter/link.js +380 -113
- package/dist/adapter/payload.js +3 -3
- package/dist/adapter/variables.d.ts +2 -1
- package/dist/adapter/variables.js +5 -2
- package/dist/figma-file.d.ts +1 -0
- package/dist/figma-file.js +1 -0
- package/dist/figma.d.ts +1 -0
- package/dist/figma.js +191 -35
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/report.d.ts +59 -1
- package/dist/report.js +176 -30
- package/dist/serialize.d.ts +7 -0
- package/dist/serialize.js +79 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +3 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +1 -1
- package/dist/taco/figma-import/figFile.js +3 -0
- package/dist/taco/figma-import/import/carry.d.ts +14 -1
- package/dist/taco/figma-import/import/carry.js +34 -1
- package/dist/taco/figma-import/import/componentProps.d.ts +10 -4
- package/dist/taco/figma-import/import/componentProps.js +29 -13
- package/dist/taco/figma-import/import/componentSets.d.ts +2 -0
- package/dist/taco/figma-import/import/componentSets.js +1 -1
- package/dist/taco/figma-import/import/converters/effects.d.ts +3 -13
- package/dist/taco/figma-import/import/converters/effects.js +4 -34
- package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
- package/dist/taco/figma-import/import/converters/exportSettings.js +75 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +1 -1
- package/dist/taco/figma-import/import/converters/frame.js +15 -6
- package/dist/taco/figma-import/import/converters/gradient.d.ts +2 -0
- package/dist/taco/figma-import/import/converters/gradient.js +3 -1
- package/dist/taco/figma-import/import/converters/grid.d.ts +12 -0
- package/dist/taco/figma-import/import/converters/grid.js +97 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +2 -4
- package/dist/taco/figma-import/import/converters/group.js +6 -15
- package/dist/taco/figma-import/import/converters/image.d.ts +2 -1
- package/dist/taco/figma-import/import/converters/image.js +60 -16
- package/dist/taco/figma-import/import/converters/layout.d.ts +8 -7
- package/dist/taco/figma-import/import/converters/layout.js +66 -37
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +1 -1
- package/dist/taco/figma-import/import/converters/rectangle.js +3 -3
- package/dist/taco/figma-import/import/converters/section.d.ts +2 -3
- package/dist/taco/figma-import/import/converters/section.js +5 -11
- package/dist/taco/figma-import/import/converters/shared.d.ts +9 -5
- package/dist/taco/figma-import/import/converters/shared.js +23 -10
- package/dist/taco/figma-import/import/converters/text.d.ts +38 -1
- package/dist/taco/figma-import/import/converters/text.js +248 -32
- package/dist/taco/figma-import/import/converters/vector.d.ts +36 -15
- package/dist/taco/figma-import/import/converters/vector.js +214 -73
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +19 -3
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +147 -29
- package/dist/taco/figma-import/import/instances.d.ts +32 -5
- package/dist/taco/figma-import/import/instances.js +613 -130
- package/dist/taco/figma-import/import/styles.d.ts +2 -0
- package/dist/taco/figma-import/import/styles.js +6 -0
- package/dist/taco/figma-import/import/types.d.ts +8 -0
- package/dist/taco/figma-import/import/variables.d.ts +5 -2
- package/dist/taco/figma-import/import/variables.js +65 -31
- package/dist/taco/figma-import/import/variantTrees.d.ts +1 -1
- package/dist/taco/figma-import/import/variantTrees.js +46 -4
- package/dist/taco/figma-import/import/walk.d.ts +62 -7
- package/dist/taco/figma-import/import/walk.js +328 -122
- package/dist/taco/figma-import/kiwi/message.d.ts +4 -2
- package/dist/taco/figma-import/kiwi/message.js +59 -15
- package/dist/taco/model/codec.d.ts +2 -2
- package/dist/taco/model/codec.js +72 -8
- package/dist/taco/model/component-evaluation.d.ts +2 -0
- package/dist/taco/model/component-evaluation.js +7 -0
- package/dist/taco/model/instance-resolver.d.ts +6 -2
- package/dist/taco/model/instance-resolver.js +50 -18
- package/dist/taco/model/sha256.js +13 -8
- package/dist/taco/model/types.d.ts +57 -5
- package/dist/taco/model/types.js +4 -0
- package/dist/taco/svg-import/style/computed.js +1 -0
- package/package.json +3 -2
|
@@ -7,27 +7,6 @@ const MAX_SHADOWS = 16;
|
|
|
7
7
|
// Prototype-free, so an effect type such as "constructor" finds no entry.
|
|
8
8
|
const SHADOW_KINDS = Object.assign(Object.create(null), { DROP_SHADOW: "drop", INNER_SHADOW: "inner" });
|
|
9
9
|
const BLUR_KINDS = Object.assign(Object.create(null), { FOREGROUND_BLUR: "layerBlur", BACKGROUND_BLUR: "backgroundBlur" });
|
|
10
|
-
/**
|
|
11
|
-
* True when the renderer draws a node with `style` as a plain rectangle, the
|
|
12
|
-
* only shape whose drop shadow it shrinks for a negative spread
|
|
13
|
-
* (`drawShadowedRect` in packages/render-core/cpp/skia/skia_adapter.cc); every
|
|
14
|
-
* other shape's drop shadow ignores a negative spread (`buildPreparedShadows`).
|
|
15
|
-
* Frames are drawn so when their corners are square; rectangles also need at
|
|
16
|
-
* most one plain solid fill or a stroke with unequal per-side weights, or they are drawn as a path
|
|
17
|
-
* (`compileRectangle` and `needsRegionFillPaints` in packages/render-core/src/page-compiler.ts).
|
|
18
|
-
*/
|
|
19
|
-
export function drawnAsPlainRectangle(style, isFrame) {
|
|
20
|
-
const square = (style.cornerRadius ?? 0) === 0 && Object.values(style.cornerRadii ?? {}).every((radius) => radius === 0);
|
|
21
|
-
if (!square || isFrame)
|
|
22
|
-
return square;
|
|
23
|
-
// A stroke with unequal per-side weights makes the rectangle a container, whatever its fills (`hasSidedStroke`).
|
|
24
|
-
const sidedStroke = style.strokes?.some(({ sides }) => sides !== undefined
|
|
25
|
-
&& !(sides.right === sides.top && sides.bottom === sides.top && sides.left === sides.top)) ?? false;
|
|
26
|
-
if (sidedStroke)
|
|
27
|
-
return true;
|
|
28
|
-
const fills = style.fills ?? [];
|
|
29
|
-
return fills.length <= 1 && fills.every((paint) => paint.kind === "solid" && paint.visible !== false && (paint.blendMode ?? "normal") === "normal");
|
|
30
|
-
}
|
|
31
10
|
/**
|
|
32
11
|
* The node's Figma `effects` as Taco shadows, layer blur and background blur,
|
|
33
12
|
* shadows in Figma's order; a node with an effect style takes the style's
|
|
@@ -42,10 +21,10 @@ export function drawnAsPlainRectangle(style, isFrame) {
|
|
|
42
21
|
* (src/converter/style.py, `convert_effects`), absent offset, radius and
|
|
43
22
|
* spread are 0. Taco has no hidden shadow or blur, one layer blur and one
|
|
44
23
|
* background blur, and at most 16 shadows; what does not fit is reported.
|
|
45
|
-
*
|
|
46
|
-
*
|
|
24
|
+
* Moondraft patch: a negative spread is kept on every shape; the renderer
|
|
25
|
+
* shrinks drop shadows and grows inner shadows' holes by it.
|
|
47
26
|
*/
|
|
48
|
-
export function nodeEffects(node, walk
|
|
27
|
+
export function nodeEffects(node, walk) {
|
|
49
28
|
const where = `node ${node.id}`;
|
|
50
29
|
const shadows = [];
|
|
51
30
|
const boundZeroBlurs = new Map();
|
|
@@ -91,7 +70,7 @@ export function nodeEffects(node, walk, plainRectangle = false) {
|
|
|
91
70
|
const offset = field(effect, "offset", "object", at) ?? {};
|
|
92
71
|
const x = field(offset, "x", "number", `${at} offset`) ?? 0;
|
|
93
72
|
const y = field(offset, "y", "number", `${at} offset`) ?? 0;
|
|
94
|
-
|
|
73
|
+
const spread = field(effect, "spread", "number", at) ?? 0;
|
|
95
74
|
const colorObject = field(effect, "color", "object", at);
|
|
96
75
|
const color = colorObject && readNumbers(colorObject, ["r", "g", "b", "a"], `${at} color`);
|
|
97
76
|
if (!color)
|
|
@@ -101,15 +80,6 @@ export function nodeEffects(node, walk, plainRectangle = false) {
|
|
|
101
80
|
// Moondraft patch (../carry.ts): Moondraft knocks a drop shadow out under the node's fill, as Figma does unless
|
|
102
81
|
// showShadowBehindNode is set; such a shadow is marked, and the see-through fill is no longer reported.
|
|
103
82
|
const behind = shadowKind === "drop" && field(effect, "showShadowBehindNode", "boolean", at) === true;
|
|
104
|
-
if (shadowKind === "inner" && spread < 0) {
|
|
105
|
-
// The renderer grows an inner shadow only inward and treats a negative spread as 0.
|
|
106
|
-
walk.report("approximated", `${label} (${type}) spread ${String(spread)} is below 0; Taco draws inner shadows with spread 0`);
|
|
107
|
-
spread = 0;
|
|
108
|
-
}
|
|
109
|
-
if (shadowKind === "drop" && spread < 0 && !plainRectangle) {
|
|
110
|
-
walk.report("approximated", `${label} (${type}) spread ${String(spread)} is below 0; Taco shrinks drop shadows only on square-cornered rectangles and frames, so spread 0 is used`);
|
|
111
|
-
spread = 0;
|
|
112
|
-
}
|
|
113
83
|
const shadow = { kind: shadowKind, x, y, blur: radius, spread, color: hexColor(color) };
|
|
114
84
|
if (behind)
|
|
115
85
|
markShadowBehind(shadow);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ExportSetting } from "../../../model/types.ts";
|
|
2
|
+
import type { NodeWalk } from "../walk.ts";
|
|
3
|
+
/** Reads the export rows stored by a layer or by an instance's root override. */
|
|
4
|
+
export declare function exportSettingsOf(value: unknown, report: NodeWalk["report"]): ExportSetting[] | undefined;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { defaultExportQuality, exportConstraintProblem, exportSuffixProblem, MAX_EXPORT_SETTINGS } from "../../../model/export-settings.js";
|
|
2
|
+
import { isObject } from "../fields.js";
|
|
3
|
+
/**
|
|
4
|
+
* Figma row fields with no counterpart in a Taco row, each with the formats it
|
|
5
|
+
* affects and the value that changes the file: a row whose field holds
|
|
6
|
+
* another value (Figma's own default included) exports as Taco would anyway.
|
|
7
|
+
*/
|
|
8
|
+
const UNKEPT_FIELDS = [
|
|
9
|
+
{ field: "svgOutlineText", formats: ["svg"], kept: (value) => value !== false },
|
|
10
|
+
{ field: "svgDataName", formats: ["svg"], kept: (value) => value !== true },
|
|
11
|
+
{ field: "svgForceStrokeMasks", formats: ["svg"], kept: (value) => value !== true },
|
|
12
|
+
{ field: "useAbsoluteBounds", formats: ["png", "jpeg", "svg", "pdf"], kept: (value) => value !== true },
|
|
13
|
+
{ field: "colorProfile", formats: ["png", "jpeg", "svg", "pdf"], kept: (value) => value === undefined || value === "DOCUMENT" },
|
|
14
|
+
{ field: "quality", formats: ["jpeg", "pdf"], kept: (value) => value === undefined },
|
|
15
|
+
{ field: "useBicubicSampler", formats: ["png", "jpeg", "pdf"], kept: (value) => value !== false },
|
|
16
|
+
{ field: "pdfPrint", formats: ["pdf"], kept: (value) => value !== true },
|
|
17
|
+
];
|
|
18
|
+
/**
|
|
19
|
+
* Rows whose unkept fields are reported: the editor keeps the first 32 rows,
|
|
20
|
+
* and the adapter reports each later row whole, so its fields are not
|
|
21
|
+
* reported again.
|
|
22
|
+
*/
|
|
23
|
+
const FIELD_REPORTED_ROWS = 32;
|
|
24
|
+
/** Reads the export rows stored by a layer or by an instance's root override. */
|
|
25
|
+
export function exportSettingsOf(value, report) {
|
|
26
|
+
if (value === undefined)
|
|
27
|
+
return undefined;
|
|
28
|
+
if (!Array.isArray(value)) {
|
|
29
|
+
report("property-left-out", "the export settings are not a list");
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
const settings = [];
|
|
33
|
+
for (const row of value) {
|
|
34
|
+
if (!isObject(row)) {
|
|
35
|
+
report("property-left-out", "an invalid export setting is left out");
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const format = typeof row["imageType"] === "string" ? row["imageType"].toLowerCase() : undefined;
|
|
39
|
+
if (format !== "png" && format !== "jpeg" && format !== "svg" && format !== "pdf") {
|
|
40
|
+
report("property-left-out", `an export setting with format ${String(format)} is left out`);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const size = row["constraint"];
|
|
44
|
+
const type = isObject(size) ? size["type"] : undefined;
|
|
45
|
+
const unit = type === "CONTENT_WIDTH" ? "width" : type === "CONTENT_HEIGHT" ? "height"
|
|
46
|
+
: type === "CONTENT_SCALE" || size === undefined ? "scale" : undefined;
|
|
47
|
+
const amount = size === undefined ? 1 : isObject(size) ? size["value"] : undefined;
|
|
48
|
+
if (unit === undefined || typeof amount !== "number" || exportConstraintProblem({ type: unit, value: amount }) !== null) {
|
|
49
|
+
report("property-left-out", "an export setting with an invalid size is left out");
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const suffix = row["suffix"] ?? "";
|
|
53
|
+
if (typeof suffix !== "string" || exportSuffixProblem(suffix) !== null) {
|
|
54
|
+
report("property-left-out", "an export setting with an invalid suffix is left out");
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (settings.length === MAX_EXPORT_SETTINGS) {
|
|
58
|
+
report("property-left-out", "an export setting beyond the reader's list limit is left out");
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (settings.length < FIELD_REPORTED_ROWS)
|
|
62
|
+
for (const { field, formats, kept } of UNKEPT_FIELDS) {
|
|
63
|
+
if (formats.includes(format) && !kept(row[field]))
|
|
64
|
+
report("property-left-out", `an export setting's ${field} is left out`);
|
|
65
|
+
}
|
|
66
|
+
settings.push({
|
|
67
|
+
format, constraint: { type: unit, value: amount }, suffix,
|
|
68
|
+
quality: defaultExportQuality(format),
|
|
69
|
+
resampling: "detailed",
|
|
70
|
+
ignoreOverlappingLayers: row["contentsOnly"] !== false,
|
|
71
|
+
includeIdAttribute: row["svgIDMode"] === "ALWAYS",
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return settings.length === 0 ? undefined : settings;
|
|
75
|
+
}
|
|
@@ -2,7 +2,7 @@ import { type FigmaObject } from "../fields.ts";
|
|
|
2
2
|
import type { FigmaNodeConverter } from "../walk.ts";
|
|
3
3
|
/**
|
|
4
4
|
* A Figma FRAME as a Taco frame, with its HORIZONTAL or VERTICAL auto layout
|
|
5
|
-
* as a stack (see `
|
|
5
|
+
* as a stack (see `autoLayoutOf`) and free layout otherwise. Frames clip unless
|
|
6
6
|
* `frameMaskDisabled` is true (grida, packages/grida-canvas-io-figma/lib.ts:4246).
|
|
7
7
|
* Figma stores a group as a FRAME. `resizeToFit` alone does not prove it, so a
|
|
8
8
|
* FRAME counts as a group only when it also clips (`frameMaskDisabled` is
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
import { carry } from "../carry.js";
|
|
1
2
|
import { field } from "../fields.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
3
|
+
import { gridStyleReference } from "../styles.js";
|
|
4
|
+
import { nodeEffects } from "./effects.js";
|
|
5
|
+
import { autoLayoutOf } from "./layout.js";
|
|
4
6
|
import { convertGroup } from "./group.js";
|
|
5
7
|
import { framedBase, visualStyle } from "./shared.js";
|
|
6
8
|
/**
|
|
7
9
|
* A Figma FRAME as a Taco frame, with its HORIZONTAL or VERTICAL auto layout
|
|
8
|
-
* as a stack (see `
|
|
10
|
+
* as a stack (see `autoLayoutOf`) and free layout otherwise. Frames clip unless
|
|
9
11
|
* `frameMaskDisabled` is true (grida, packages/grida-canvas-io-figma/lib.ts:4246).
|
|
10
12
|
* Figma stores a group as a FRAME. `resizeToFit` alone does not prove it, so a
|
|
11
13
|
* FRAME counts as a group only when it also clips (`frameMaskDisabled` is
|
|
@@ -16,7 +18,7 @@ import { framedBase, visualStyle } from "./shared.js";
|
|
|
16
18
|
* fields is still a component or an instance, so it imports as a frame.
|
|
17
19
|
*/
|
|
18
20
|
export const convertFrame = (node, walk) => {
|
|
19
|
-
|
|
21
|
+
field(node.change, "stackMode", "string", `node ${node.id}`);
|
|
20
22
|
// The paint lists are checked here, so a malformed list is refused rather than read as "not a group".
|
|
21
23
|
for (const name of ["fillPaints", "strokePaints", "backgroundPaints"])
|
|
22
24
|
field(node.change, name, "list", `node ${node.id}`);
|
|
@@ -26,15 +28,19 @@ export const convertFrame = (node, walk) => {
|
|
|
26
28
|
if (base === null)
|
|
27
29
|
return null;
|
|
28
30
|
const visual = visualStyle(node, walk);
|
|
29
|
-
const style = { ...visual, ...nodeEffects(node, walk
|
|
31
|
+
const style = { ...visual, ...nodeEffects(node, walk) };
|
|
30
32
|
const frame = {
|
|
31
33
|
...base,
|
|
32
34
|
type: "frame",
|
|
33
35
|
children: walk.convertChildren(),
|
|
34
|
-
|
|
36
|
+
// An instance can inherit the track IDs its sizing refers to. Report grid fields once the main is known.
|
|
37
|
+
layout: autoLayoutOf(node.change, walk.report, node.type === "INSTANCE" ? () => { } : undefined) ?? { kind: "absolute" },
|
|
35
38
|
clipContent: node.change["frameMaskDisabled"] !== true,
|
|
36
39
|
...(Object.keys(style).length > 0 ? { style } : {}),
|
|
37
40
|
};
|
|
41
|
+
if (frame.layout.kind !== "absolute" && node.change["bordersTakeSpace"] === true) {
|
|
42
|
+
carry(frame, { strokesIncluded: true });
|
|
43
|
+
}
|
|
38
44
|
return frame;
|
|
39
45
|
};
|
|
40
46
|
/**
|
|
@@ -45,6 +51,9 @@ export const convertFrame = (node, walk) => {
|
|
|
45
51
|
export function isFigmaGroup(change) {
|
|
46
52
|
if (change["resizeToFit"] !== true || change["frameMaskDisabled"] !== false)
|
|
47
53
|
return false;
|
|
54
|
+
const grids = change["layoutGrids"];
|
|
55
|
+
if ((Array.isArray(grids) && grids.length > 0) || gridStyleReference(change) !== undefined)
|
|
56
|
+
return false;
|
|
48
57
|
const stackMode = change["stackMode"];
|
|
49
58
|
if (stackMode !== undefined && stackMode !== "NONE")
|
|
50
59
|
return false;
|
|
@@ -52,6 +52,8 @@ export interface Box {
|
|
|
52
52
|
readonly width: number;
|
|
53
53
|
readonly height: number;
|
|
54
54
|
}
|
|
55
|
+
/** A vector computes its paint bounds on the first gradient that needs them. */
|
|
56
|
+
export type PaintBounds = Box | (() => Box | undefined);
|
|
55
57
|
/**
|
|
56
58
|
* The paint transform `m` of a `width` by `height` node re-expressed on
|
|
57
59
|
* `box`'s unit box, so the gradient stays where it is in node pixels when it
|
|
@@ -44,7 +44,9 @@ function clipStops(stops) {
|
|
|
44
44
|
const out = [];
|
|
45
45
|
if (stops[0].position < 0)
|
|
46
46
|
out.push({ position: 0, color: sample(stops, 0) });
|
|
47
|
-
|
|
47
|
+
for (const stop of stops)
|
|
48
|
+
if (stop.position >= 0 && stop.position <= 1)
|
|
49
|
+
out.push(stop);
|
|
48
50
|
if (stops[stops.length - 1].position > 1)
|
|
49
51
|
out.push({ position: 1, color: sample(stops, 1) });
|
|
50
52
|
return out.length === 1 ? [{ position: 0, color: out[0].color }, { position: 1, color: out[0].color }] : out;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GridTrack } from "../../../model/types.ts";
|
|
2
|
+
import { type FigmaObject } from "../fields.ts";
|
|
3
|
+
type Lost = (field: string) => void;
|
|
4
|
+
export declare const gridChildFields: Set<string>;
|
|
5
|
+
export declare function reportUnknownGridFields(change: FigmaObject, lost: Lost): void;
|
|
6
|
+
/** Join sizing records to the ordered GUIDs. Children reference this same order by ID. */
|
|
7
|
+
export declare function gridAxis(change: FigmaObject, name: "gridRows" | "gridColumns", lost: Lost): {
|
|
8
|
+
indices: Map<string, number>;
|
|
9
|
+
tracks: GridTrack[];
|
|
10
|
+
};
|
|
11
|
+
export declare function gridCell(change: FigmaObject, rows: Map<string, number>, columns: Map<string, number>, lost: Lost, override?: boolean): Record<string, string | number>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { isObject } from "../fields.js";
|
|
2
|
+
const guid = (v) => isObject(v) && Number.isInteger(v.sessionID) && Number.isInteger(v.localID)
|
|
3
|
+
? `${v.sessionID}:${v.localID}` : undefined;
|
|
4
|
+
const frameFields = ["gridColumns", "gridRows", "gridColumnsSizing", "gridRowsSizing", "gridRowGap", "gridColumnGap"];
|
|
5
|
+
export const gridChildFields = new Set(["gridColumnAnchor", "gridRowAnchor", "gridColumnSpan", "gridRowSpan", "gridChildHorizontalAlign", "gridChildVerticalAlign"]);
|
|
6
|
+
const knownFields = new Set([...frameFields, ...gridChildFields]);
|
|
7
|
+
export function reportUnknownGridFields(change, lost) {
|
|
8
|
+
for (const name of Object.keys(change))
|
|
9
|
+
if (name.startsWith("grid") && !knownFields.has(name))
|
|
10
|
+
lost(name);
|
|
11
|
+
}
|
|
12
|
+
function entries(raw, field, lost) {
|
|
13
|
+
if (raw === undefined)
|
|
14
|
+
return [];
|
|
15
|
+
if (isObject(raw) && Array.isArray(raw.entries))
|
|
16
|
+
return raw.entries;
|
|
17
|
+
lost(field);
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
function trackSize(raw) {
|
|
21
|
+
if (!isObject(raw))
|
|
22
|
+
return;
|
|
23
|
+
if (raw.type === "HUG")
|
|
24
|
+
return { kind: "auto" };
|
|
25
|
+
const n = raw.value;
|
|
26
|
+
if (raw.type === "FLEX" && (n === undefined || typeof n === "number" && Number.isFinite(n) && n > 0))
|
|
27
|
+
return { kind: "fill", weight: n ?? 1 };
|
|
28
|
+
if (raw.type === "FIXED" && typeof n === "number" && Number.isFinite(n) && n >= 0)
|
|
29
|
+
return { kind: "fixed", size: n };
|
|
30
|
+
}
|
|
31
|
+
/** Join sizing records to the ordered GUIDs. Children reference this same order by ID. */
|
|
32
|
+
export function gridAxis(change, name, lost) {
|
|
33
|
+
const order = new Map();
|
|
34
|
+
for (const e of entries(change[name], name, lost)) {
|
|
35
|
+
const id = isObject(e) && guid(e.id);
|
|
36
|
+
if (!id || !isObject(e) || typeof e.position !== "string" || order.has(id)) {
|
|
37
|
+
lost(name);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
order.set(id, e.position);
|
|
41
|
+
}
|
|
42
|
+
const ids = [...order.keys()].sort((a, b) => order.get(a) < order.get(b) ? -1 : order.get(a) > order.get(b) ? 1 : 0);
|
|
43
|
+
const indices = new Map(ids.map((id, index) => [id, index]));
|
|
44
|
+
const tracks = Array.from({ length: Math.max(1, ids.length) }, () => ({ kind: "fill" }));
|
|
45
|
+
const field = name + "Sizing";
|
|
46
|
+
for (const e of entries(change[field], field, lost)) {
|
|
47
|
+
const id = isObject(e) && guid(e.id), i = id ? indices.get(id) : undefined;
|
|
48
|
+
if (i === undefined || !isObject(e) || !isObject(e.trackSize)) {
|
|
49
|
+
lost(field);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const { minSizing, maxSizing } = e.trackSize;
|
|
53
|
+
const min = trackSize(minSizing), max = trackSize(maxSizing);
|
|
54
|
+
if (minSizing !== undefined && (!min || max && JSON.stringify(min) !== JSON.stringify(max)))
|
|
55
|
+
lost(field + ".minSizing");
|
|
56
|
+
if (maxSizing !== undefined && !max)
|
|
57
|
+
lost(field + ".maxSizing");
|
|
58
|
+
if (!min && !max && minSizing === undefined && maxSizing === undefined)
|
|
59
|
+
lost(field);
|
|
60
|
+
tracks[i] = max ?? min ?? tracks[i];
|
|
61
|
+
}
|
|
62
|
+
return { indices, tracks };
|
|
63
|
+
}
|
|
64
|
+
export function gridCell(change, rows, columns, lost, override = false) {
|
|
65
|
+
if (!override && change.stackMode !== "GRID")
|
|
66
|
+
reportUnknownGridFields(change, lost);
|
|
67
|
+
const cell = {};
|
|
68
|
+
for (const [name, ids, align, key] of [["Row", rows, "Vertical", "alignY"], ["Column", columns, "Horizontal", "alignX"]]) {
|
|
69
|
+
const axis = name.toLowerCase(), anchorField = "grid" + name + "Anchor", anchor = change[anchorField];
|
|
70
|
+
if (anchor !== undefined) {
|
|
71
|
+
const id = guid(anchor), i = id && ids.get(id);
|
|
72
|
+
if (typeof i === "number" && i <= 65535)
|
|
73
|
+
cell[axis] = i;
|
|
74
|
+
else
|
|
75
|
+
lost(anchorField);
|
|
76
|
+
}
|
|
77
|
+
const spanField = "grid" + name + "Span", span = change[spanField];
|
|
78
|
+
if (span !== undefined) {
|
|
79
|
+
if (typeof span === "number" && Number.isInteger(span) && span > 0 && span <= 65535)
|
|
80
|
+
cell[axis + "Span"] = span;
|
|
81
|
+
else
|
|
82
|
+
lost(spanField);
|
|
83
|
+
}
|
|
84
|
+
const field = "gridChild" + align + "Align", value = change[field];
|
|
85
|
+
if (value === "MIN" || (override && value === "AUTO"))
|
|
86
|
+
cell[key] = "start";
|
|
87
|
+
else if (value === "CENTER")
|
|
88
|
+
cell[key] = "center";
|
|
89
|
+
else if (value === "MAX")
|
|
90
|
+
cell[key] = "end";
|
|
91
|
+
else if (value === "STRETCH")
|
|
92
|
+
cell[key] = "stretch";
|
|
93
|
+
else if (value !== undefined && value !== "AUTO")
|
|
94
|
+
lost(field);
|
|
95
|
+
}
|
|
96
|
+
return cell;
|
|
97
|
+
}
|
|
@@ -13,9 +13,7 @@ import type { FigmaNodeConverter } from "../walk.ts";
|
|
|
13
13
|
* `clipsContent: false`) and fig2sketch (src/converter/group.py:12 builds a
|
|
14
14
|
* plain Sketch group).
|
|
15
15
|
*
|
|
16
|
-
* A Taco group
|
|
17
|
-
*
|
|
18
|
-
* on its layers, so each layer imports its own and the group's are left out;
|
|
19
|
-
* the report says so when they differ from a layer's.
|
|
16
|
+
* A Taco group follows its layers' constraints (see `NodeBase.constraints`).
|
|
17
|
+
* The adapter reads the group's own constraints from its Figma metadata.
|
|
20
18
|
*/
|
|
21
19
|
export declare const convertGroup: FigmaNodeConverter;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { carry } from "../carry.js";
|
|
2
|
+
import { framedBase } from "./shared.js";
|
|
2
3
|
/**
|
|
3
4
|
* A Figma group (a FRAME that `convertFrame` finds to be a group) as a Taco
|
|
4
5
|
* group. Figma stores each child's transform in the group's own space, with
|
|
@@ -13,10 +14,8 @@ import { constraintsOf, framedBase } from "./shared.js";
|
|
|
13
14
|
* `clipsContent: false`) and fig2sketch (src/converter/group.py:12 builds a
|
|
14
15
|
* plain Sketch group).
|
|
15
16
|
*
|
|
16
|
-
* A Taco group
|
|
17
|
-
*
|
|
18
|
-
* on its layers, so each layer imports its own and the group's are left out;
|
|
19
|
-
* the report says so when they differ from a layer's.
|
|
17
|
+
* A Taco group follows its layers' constraints (see `NodeBase.constraints`).
|
|
18
|
+
* The adapter reads the group's own constraints from its Figma metadata.
|
|
20
19
|
*/
|
|
21
20
|
export const convertGroup = (node, walk) => {
|
|
22
21
|
const base = framedBase(node, walk, "Group");
|
|
@@ -24,16 +23,8 @@ export const convertGroup = (node, walk) => {
|
|
|
24
23
|
return null;
|
|
25
24
|
// `framedBase` sets no variable bindings; a group's type allows no width or height binding.
|
|
26
25
|
const { variableBindings: _none, constraints, ...fields } = base;
|
|
27
|
-
if (constraints !== undefined) {
|
|
28
|
-
const own = `${String(node.change["horizontalConstraint"] ?? "MIN")}/${String(node.change["verticalConstraint"] ?? "MIN")}`;
|
|
29
|
-
const ignore = () => undefined;
|
|
30
|
-
const differs = node.children.some((child) => {
|
|
31
|
-
const layer = constraintsOf(child.change, ignore);
|
|
32
|
-
return layer?.horizontal !== constraints.horizontal || layer.vertical !== constraints.vertical;
|
|
33
|
-
});
|
|
34
|
-
if (differs)
|
|
35
|
-
walk.report("property-left-out", `the group's constraints ${own} are left out: a Taco group follows its layers' constraints, and its layers have their own`);
|
|
36
|
-
}
|
|
37
26
|
const group = { ...fields, type: "group", children: walk.convertChildren() };
|
|
27
|
+
if (constraints !== undefined)
|
|
28
|
+
carry(group, { groupConstraints: { ...constraints } });
|
|
38
29
|
return group;
|
|
39
30
|
};
|
|
@@ -3,6 +3,7 @@ import { type FigmaObject } from "../fields.ts";
|
|
|
3
3
|
import type { NodeWalk } from "../walk.ts";
|
|
4
4
|
/** The MIME type of PNG, JPEG, GIF and WebP bytes, read from their first bytes; `undefined` for anything else. */
|
|
5
5
|
export declare function sniffImageMimeType(bytes: Uint8Array): string | undefined;
|
|
6
|
+
export declare function sniffVideoMimeType(bytes: Uint8Array): string | undefined;
|
|
6
7
|
export declare function hexText(bytes: Uint8Array): string;
|
|
7
8
|
/**
|
|
8
9
|
* A Figma IMAGE paint as a Taco image paint whose asset the host supplied
|
|
@@ -10,4 +11,4 @@ export declare function hexText(bytes: Uint8Array): string;
|
|
|
10
11
|
* is left out for a reason already reported for the same image. Rotation
|
|
11
12
|
* and image adjustments (`paintFilter`, `filterColorAdjust`) are reported, not kept.
|
|
12
13
|
*/
|
|
13
|
-
export declare function imagePaint(paint: FigmaObject, at: string, label: string, walk: NodeWalk): Paint | string | null;
|
|
14
|
+
export declare function imagePaint(paint: FigmaObject, at: string, label: string, walk: NodeWalk, fill: boolean): Paint | string | null;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { intrinsicSize } from "../../../svg-import/structure/image.js";
|
|
1
2
|
import { field } from "../fields.js";
|
|
2
|
-
import { markTile } from "../carry.js";
|
|
3
|
+
import { markImageCrop, markTile } from "../carry.js";
|
|
3
4
|
/** The MIME type of PNG, JPEG, GIF and WebP bytes, read from their first bytes; `undefined` for anything else. */
|
|
4
5
|
export function sniffImageMimeType(bytes) {
|
|
5
6
|
const starts = (offset, text) => bytes.length >= offset + text.length && [...text].every((char, i) => bytes[offset + i] === char.charCodeAt(0));
|
|
@@ -13,15 +14,20 @@ export function sniffImageMimeType(bytes) {
|
|
|
13
14
|
return "image/webp";
|
|
14
15
|
return undefined;
|
|
15
16
|
}
|
|
17
|
+
export function sniffVideoMimeType(bytes) {
|
|
18
|
+
if (bytes[0] === 0x1a && bytes[1] === 0x45 && bytes[2] === 0xdf && bytes[3] === 0xa3)
|
|
19
|
+
return "video/webm";
|
|
20
|
+
if (String.fromCharCode(...bytes.subarray(4, 8)) === "ftyp")
|
|
21
|
+
return String.fromCharCode(...bytes.subarray(8, 12)) === "qt " ? "video/quicktime" : "video/mp4";
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
16
24
|
export function hexText(bytes) {
|
|
17
25
|
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
18
26
|
}
|
|
19
27
|
/**
|
|
20
28
|
* Figma's `imageScaleMode` as a Taco fit. Kiwi leaves out an enum field at its
|
|
21
|
-
* first value, so an absent mode is STRETCH (Figma's "Crop")
|
|
22
|
-
*
|
|
23
|
-
* fig2sketch reads it (src/converter/style.py, `convert_image_paint`),
|
|
24
|
-
* FILL is cover, FIT is contain and STRETCH a stretch; Taco has no tiling.
|
|
29
|
+
* first value, so an absent mode is STRETCH (Figma's "Crop"). The crop
|
|
30
|
+
* matrix maps layer UVs to image UVs and is carried for the adapter.
|
|
25
31
|
*/
|
|
26
32
|
function imageFit(paint, at) {
|
|
27
33
|
const mode = field(paint, "imageScaleMode", "string", at) ?? "STRETCH";
|
|
@@ -31,9 +37,13 @@ function imageFit(paint, at) {
|
|
|
31
37
|
return { fit: "contain" };
|
|
32
38
|
if (mode === "STRETCH") {
|
|
33
39
|
const m = field(paint, "transform", "object", at);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
if (m === undefined)
|
|
41
|
+
return { fit: "fill" };
|
|
42
|
+
const crop = [m["m00"] ?? 1, m["m10"] ?? 0, m["m01"] ?? 0, m["m11"] ?? 1, m["m02"] ?? 0, m["m12"] ?? 0];
|
|
43
|
+
if (!crop.every(Number.isFinite))
|
|
44
|
+
return { fit: "fill", approximated: "the image crop matrix is invalid" };
|
|
45
|
+
const identity = [1, 0, 0, 1, 0, 0];
|
|
46
|
+
return crop.some((entry, i) => entry !== identity[i]) ? { fit: "fill", crop: crop } : { fit: "fill" };
|
|
37
47
|
}
|
|
38
48
|
if (mode === "TILE") {
|
|
39
49
|
const scale = field(paint, "scale", "number", at) ?? 1;
|
|
@@ -48,17 +58,43 @@ function imageFit(paint, at) {
|
|
|
48
58
|
* is left out for a reason already reported for the same image. Rotation
|
|
49
59
|
* and image adjustments (`paintFilter`, `filterColorAdjust`) are reported, not kept.
|
|
50
60
|
*/
|
|
51
|
-
export function imagePaint(paint, at, label, walk) {
|
|
52
|
-
const
|
|
53
|
-
const hash = image?.["hash"];
|
|
54
|
-
if (!(hash instanceof Uint8Array) || hash.length === 0)
|
|
55
|
-
return "the image paint has no image hash";
|
|
61
|
+
export function imagePaint(paint, at, label, walk, fill) {
|
|
62
|
+
const isVideo = paint.type === "VIDEO";
|
|
56
63
|
const width = field(paint, "originalImageWidth", "number", at);
|
|
57
64
|
const height = field(paint, "originalImageHeight", "number", at);
|
|
58
|
-
const
|
|
65
|
+
const size = width !== undefined && height !== undefined ? { width, height } : undefined;
|
|
66
|
+
const read = (name) => {
|
|
67
|
+
const ref = field(paint, name, "object", at), hash = ref?.hash;
|
|
68
|
+
if (!(hash instanceof Uint8Array) || !hash.length)
|
|
69
|
+
return "the image paint has no image hash";
|
|
70
|
+
const bytes = isVideo && typeof ref?.dataBlob === "number" ? walk.blob(ref.dataBlob) : undefined;
|
|
71
|
+
return walk.image(hexText(hash), size, bytes ? { bytes } : undefined);
|
|
72
|
+
};
|
|
73
|
+
let asset = read("image");
|
|
74
|
+
if (isVideo) {
|
|
75
|
+
if (!asset || typeof asset === "string" || asset.placeholder) {
|
|
76
|
+
const thumbnail = read("imageThumbnail");
|
|
77
|
+
if (thumbnail && typeof thumbnail !== "string")
|
|
78
|
+
asset = thumbnail;
|
|
79
|
+
}
|
|
80
|
+
const poster = asset && typeof asset !== "string" && !asset.placeholder ? asset : undefined;
|
|
81
|
+
const hash = field(paint, "video", "object", at)?.hash;
|
|
82
|
+
const clip = hash instanceof Uint8Array && hash.length ? walk.image(hexText(hash), size, { video: true, ...(poster && { poster }) }) : undefined;
|
|
83
|
+
if (clip && typeof clip !== "string") {
|
|
84
|
+
asset = clip;
|
|
85
|
+
if (!clip.poster)
|
|
86
|
+
walk.report("property-left-out", `${label}: no poster supplied on this video fill`);
|
|
87
|
+
}
|
|
88
|
+
else if (poster)
|
|
89
|
+
walk.report("approximated", `${label}: video media unavailable, kept its poster as an image`);
|
|
90
|
+
else if (!asset || typeof asset === "string")
|
|
91
|
+
return "video media and poster unavailable";
|
|
92
|
+
else
|
|
93
|
+
walk.report("property-left-out", `${label}: video media and poster unavailable`);
|
|
94
|
+
}
|
|
59
95
|
if (asset === null || typeof asset === "string")
|
|
60
96
|
return asset;
|
|
61
|
-
const { fit, approximated, tile } = imageFit(paint, at);
|
|
97
|
+
const { fit, approximated, tile, crop } = imageFit(paint, at);
|
|
62
98
|
if (approximated)
|
|
63
99
|
walk.report("approximated", `${label}: ${approximated}`);
|
|
64
100
|
const rotation = field(paint, "rotation", "number", at) ?? 0;
|
|
@@ -76,8 +112,16 @@ export function imagePaint(paint, at, label, walk) {
|
|
|
76
112
|
}
|
|
77
113
|
if (adjusted.size > 0)
|
|
78
114
|
walk.report("approximated", `${label}: the image adjustments (${[...adjusted].join(", ")}) are not kept`);
|
|
79
|
-
const imaged = { kind: "image", assetId: asset.
|
|
115
|
+
const imaged = { kind: "image", assetId: asset.id, ...(fit !== "cover" ? { fit } : {}) };
|
|
80
116
|
if (tile !== undefined)
|
|
81
117
|
markTile(imaged, tile);
|
|
118
|
+
if (crop && fill) {
|
|
119
|
+
const size = intrinsicSize(asset.mimeType, asset.bytes);
|
|
120
|
+
const w = size?.width || width || asset.width, h = size?.height || height || asset.height;
|
|
121
|
+
const aspect = w && h && w > 0 && h > 0 ? w / h : undefined;
|
|
122
|
+
markImageCrop(imaged, { matrix: crop, ...(aspect && { aspect }) });
|
|
123
|
+
}
|
|
124
|
+
else if (crop)
|
|
125
|
+
walk.report("approximated", `${label}: the image crop is not kept; the whole image is stretched to the box`);
|
|
82
126
|
return imaged;
|
|
83
127
|
}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import type { FrameNode, FramedNodeBase, NodeResizing, SceneNode,
|
|
1
|
+
import type { FrameNode, FramedNodeBase, NodeResizing, SceneNode, TextNode } from "../../../model/index.ts";
|
|
2
2
|
import { type FigmaObject } from "../fields.ts";
|
|
3
3
|
import type { FigmaImportReportKind } from "../types.ts";
|
|
4
4
|
type Report = (kind: FigmaImportReportKind, reason: string) => void;
|
|
5
5
|
/**
|
|
6
|
-
* A Figma
|
|
6
|
+
* A Figma grid or stack as a Taco auto layout, or `undefined`
|
|
7
7
|
* for free layout. The walk turns the stack off again unless every child in
|
|
8
8
|
* the flow was imported and is visible (see `stackBlocker`). An absent
|
|
9
9
|
* `stackPrimarySizing` hugs and an absent `stackCounterSizing` is fixed, as
|
|
10
10
|
* measured on real Figma files.
|
|
11
11
|
*/
|
|
12
|
-
export declare function
|
|
12
|
+
export declare function autoLayoutOf(change: FigmaObject, report: Report, lost?: (field: string) => void): Exclude<FrameNode["layout"], {
|
|
13
|
+
kind: "absolute";
|
|
14
|
+
}> | undefined;
|
|
13
15
|
/** `minSize` and `maxSize` as Taco size limits; Figma writes 0 for an unset limit. */
|
|
14
16
|
export declare function sizeLimitsOf(change: FigmaObject): NodeResizing;
|
|
15
17
|
/**
|
|
@@ -19,9 +21,8 @@ export declare function sizeLimitsOf(change: FigmaObject): NodeResizing;
|
|
|
19
21
|
* the cross axis. Taco has no grow weights, so any grow above 0 fills. A
|
|
20
22
|
* flow child loses its constraints, taken off `node` itself, as they apply
|
|
21
23
|
* only to absolute children.
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* line fills only its width, as its height is always 0.
|
|
24
|
+
* Upright, unlocked groups and paths in a non-wrapping stack carry Fill in
|
|
25
|
+
* metadata for the adapter. A line fills only its width, as its height is always 0.
|
|
25
26
|
*
|
|
26
27
|
* Figma keeps one sizing per axis, and Fill wins over the child's own Hug: a
|
|
27
28
|
* frame that hugs an axis, or text that sizes it to its content, shows the
|
|
@@ -30,5 +31,5 @@ export declare function sizeLimitsOf(change: FigmaObject): NodeResizing;
|
|
|
30
31
|
* sizes to its content, so such a child gets the fill and a fixed size on that
|
|
31
32
|
* axis instead, and the report says so.
|
|
32
33
|
*/
|
|
33
|
-
export declare function stackChildFields(change: FigmaObject, vertical: boolean, node: SceneNode, report: Report
|
|
34
|
+
export declare function stackChildFields(change: FigmaObject, vertical: boolean, wraps: boolean, node: SceneNode, report: Report): Pick<FramedNodeBase, "layoutPositioning" | "resizing"> & Partial<Pick<TextNode, "autoResize" | "wrap"> & Pick<FrameNode, "layout">>;
|
|
34
35
|
export {};
|