@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
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { getVectorNetworkBounds, transformIsInvertible, vectorHandleId, vectorRegionId, vectorSegmentId, vectorVertexId, } from "../../../model/index.js";
|
|
2
2
|
import { field, isObject } from "../fields.js";
|
|
3
|
-
import { styleBackedList } from "../styles.js";
|
|
4
3
|
import { nodeEffects } from "./effects.js";
|
|
5
4
|
import { sizeLimitsOf } from "./layout.js";
|
|
6
5
|
import { isFigmaGroup } from "./frame.js";
|
|
7
|
-
import { layerBase, nodePaints, nodeStrokes, STROKE_CAPS } from "./shared.js";
|
|
8
|
-
import { NetworkRefused, regionBoxes, vectorDataNetwork } from "./vectorNetwork.js";
|
|
9
|
-
import { carry } from "../carry.js";
|
|
6
|
+
import { endCapOf, frameGeometry, framedBase, layerBase, nodePaints, nodeStrokes, STROKE_CAPS, visualStyle } from "./shared.js";
|
|
7
|
+
import { NetworkRefused, applyRegionStyles, regionBoxes, vectorDataNetwork, vectorStyleTable } from "./vectorNetwork.js";
|
|
8
|
+
import { carry, imageCropOf } from "../carry.js";
|
|
10
9
|
/**
|
|
11
10
|
* The most paths in one node's `fillGeometry` and the most drawing commands
|
|
12
11
|
* in all of them together. A node over either is left out and reported.
|
|
@@ -170,13 +169,14 @@ function addRegion(network, commands, windingRule, where) {
|
|
|
170
169
|
network.regions.push({ id: vectorRegionId(`r${String(network.regions.length)}`), windingRule, loops });
|
|
171
170
|
}
|
|
172
171
|
/** The node's `fillGeometry` as one network, a region per path; throws `GeometryRefused`. */
|
|
173
|
-
function fillGeometryNetwork(node, walk) {
|
|
172
|
+
function fillGeometryNetwork(node, walk, vectorData) {
|
|
174
173
|
const where = `node ${node.id}`;
|
|
175
174
|
const paths = field(node.change, "fillGeometry", "list", where) ?? [];
|
|
176
175
|
if (paths.length > VECTOR_GEOMETRY_LIMITS.paths) {
|
|
177
176
|
throw new GeometryRefused("too-many-paths", `fillGeometry has ${String(paths.length)} paths, over the limit of ${String(VECTOR_GEOMETRY_LIMITS.paths)}`);
|
|
178
177
|
}
|
|
179
178
|
const network = { vertices: [], segments: [], regions: [] };
|
|
179
|
+
const styled = [];
|
|
180
180
|
const budget = { left: VECTOR_GEOMETRY_LIMITS.commands };
|
|
181
181
|
paths.forEach((path, index) => {
|
|
182
182
|
const at = `fillGeometry[${String(index)}]`;
|
|
@@ -187,65 +187,27 @@ function fillGeometryNetwork(node, walk) {
|
|
|
187
187
|
if (rule !== "NONZERO" && rule !== "ODD")
|
|
188
188
|
throw new GeometryRefused("unknown-winding-rule", `${at} has winding rule ${rule}`);
|
|
189
189
|
const styleId = field(path, "styleID", "number", `${where} ${at}`) ?? 0;
|
|
190
|
-
if (styleId > 0)
|
|
191
|
-
walk.report("approximated", `${at} has its own style (styleID ${String(styleId)}), which is not imported; it takes the node's fills`);
|
|
192
190
|
const bytes = walk.blob(reference);
|
|
193
191
|
if (bytes === undefined)
|
|
194
192
|
throw new GeometryRefused("blob-missing", `${at} refers to commands blob ${String(reference)}, which the message does not have`);
|
|
195
193
|
const blobWhere = `${at} commands blob ${String(reference)}`;
|
|
194
|
+
const regionIndex = network.regions.length;
|
|
196
195
|
addRegion(network, decodeCommands(bytes, budget, walk.vectorCommands, blobWhere), rule === "ODD" ? "evenodd" : "nonzero", blobWhere);
|
|
196
|
+
if (styleId > 0 && network.regions.length > regionIndex)
|
|
197
|
+
styled.push({ index: regionIndex, styleId, at });
|
|
197
198
|
});
|
|
198
199
|
if (network.segments.length === 0) {
|
|
199
200
|
throw new GeometryRefused("no-geometry", paths.length === 0
|
|
200
201
|
? "the node has no fillGeometry and no vectorData.vectorNetworkBlob"
|
|
201
202
|
: "no fillGeometry path draws a line or curve");
|
|
202
203
|
}
|
|
204
|
+
if (styled.length > 0)
|
|
205
|
+
applyRegionStyles(node, walk, network, vectorStyleTable(vectorData, where), styled, true);
|
|
203
206
|
return network;
|
|
204
207
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
* The most paints and gradient stops `placedFills` may copy into a VECTOR's
|
|
209
|
-
* regions in total. Over it the node's gradients stay on one box, so a short
|
|
210
|
-
* paint list cannot be copied into thousands of regions.
|
|
211
|
-
*/
|
|
212
|
-
export const REGION_FILL_COPY_LIMIT = 100_000;
|
|
213
|
-
/**
|
|
214
|
-
* The node's fills, and the network with the regions that inherit them.
|
|
215
|
-
* Figma places a gradient on the node's box, but the renderer places a
|
|
216
|
-
* region's fills on that region's tight bounds (render-core
|
|
217
|
-
* `skia_adapter.cc`, `region_bounds`). When every inheriting region has one
|
|
218
|
-
* box the node's fills are placed on it; otherwise, if a fill is a
|
|
219
|
-
* gradient, each inheriting region gets its own copy placed on its own box,
|
|
220
|
-
* unless the copies would hold more than `REGION_FILL_COPY_LIMIT` paints
|
|
221
|
-
* and stops.
|
|
222
|
-
*/
|
|
223
|
-
function placedFills(node, walk, network, boxOf) {
|
|
224
|
-
const inheriting = network.regions.filter((region) => region.fills === undefined);
|
|
225
|
-
const boxes = new Map(inheriting.map((region) => [region, boxOf(region)]));
|
|
226
|
-
const first = boxes.values().next().value;
|
|
227
|
-
const shared = [...boxes.values()].every((box) => sameBox(box, first));
|
|
228
|
-
const fills = nodePaints(node, walk, "fillPaints", shared ? first : undefined);
|
|
229
|
-
if (shared || fills.every((paint) => !paint.kind.endsWith("-gradient")))
|
|
230
|
-
return { fills, network };
|
|
231
|
-
// Count the listed paints and their raw stops, so a paint discarded after its stops are read is counted too.
|
|
232
|
-
const listed = styleBackedList(node.change, "fillPaints", walk.styleList, `node ${node.id}`);
|
|
233
|
-
const perCopy = Array.isArray(listed)
|
|
234
|
-
? listed.reduce((sum, paint) => sum + 1 + (isObject(paint) && Array.isArray(paint["stops"]) ? paint["stops"].length : 0), 0)
|
|
235
|
-
: 0;
|
|
236
|
-
if (perCopy * inheriting.length > REGION_FILL_COPY_LIMIT) {
|
|
237
|
-
walk.report("approximated", `the fill gradients are placed on the node's box, not on each of its ${String(inheriting.length)} regions' boxes, because the copies would hold more than ${String(REGION_FILL_COPY_LIMIT)} paints and stops`);
|
|
238
|
-
return { fills, network };
|
|
239
|
-
}
|
|
240
|
-
// The node's own call above has already reported what these copies would.
|
|
241
|
-
const quiet = { ...walk, report: () => undefined };
|
|
242
|
-
const regions = network.regions.map((region) => region.fills !== undefined ? region
|
|
243
|
-
: { ...region, fills: nodePaints(node, quiet, "fillPaints", boxes.get(region)) });
|
|
244
|
-
return { fills, network: { ...network, regions } };
|
|
245
|
-
}
|
|
246
|
-
/** The box the renderer places a path's gradient strokes on: its regions' bounds together, else its edges' bounds. */
|
|
247
|
-
function strokeBox(network, boxOf) {
|
|
248
|
-
const boxes = network.regions.map(boxOf).filter((box) => box !== undefined);
|
|
208
|
+
/** The renderer places layer gradients on the path's region bounds, or on its edges when there are no regions. */
|
|
209
|
+
function pathPaintBox(network) {
|
|
210
|
+
const boxes = network.regions.map(regionBoxes(network)).filter((box) => box !== undefined);
|
|
249
211
|
if (boxes.length === 0)
|
|
250
212
|
return getVectorNetworkBounds(network) ?? undefined;
|
|
251
213
|
const left = Math.min(...boxes.map((box) => box.x)), top = Math.min(...boxes.map((box) => box.y));
|
|
@@ -279,11 +241,12 @@ export function pathTransform(node, walk) {
|
|
|
279
241
|
*/
|
|
280
242
|
export function pathStrokes(node, walk, box) {
|
|
281
243
|
const capName = field(node.change, "strokeCap", "string", `node ${node.id}`) ?? "NONE";
|
|
282
|
-
const
|
|
283
|
-
const
|
|
284
|
-
|
|
244
|
+
const dashCap = Object.hasOwn(STROKE_CAPS, capName) ? STROKE_CAPS[capName] : undefined;
|
|
245
|
+
const cap = node.type === "VECTOR" ? endCapOf(capName) : dashCap;
|
|
246
|
+
const strokes = nodeStrokes(node, walk, box).map((stroke) => ({ ...stroke, dashCap: dashCap ?? "none" }));
|
|
247
|
+
if (strokes.length > 0 && cap === undefined)
|
|
285
248
|
walk.report("approximated", `stroke cap ${capName} is not imported yet; the ends are flat`);
|
|
286
|
-
return { strokes, cap };
|
|
249
|
+
return { strokes, cap: cap ?? "none" };
|
|
287
250
|
}
|
|
288
251
|
/**
|
|
289
252
|
* A Figma VECTOR as a Taco path. The shape is the node's editable vector
|
|
@@ -303,6 +266,7 @@ export const convertVector = (node, walk) => {
|
|
|
303
266
|
if (transform === null)
|
|
304
267
|
return null;
|
|
305
268
|
let vectorNetwork;
|
|
269
|
+
let endpointCaps = {};
|
|
306
270
|
let fromNetwork = false;
|
|
307
271
|
let networkProblem;
|
|
308
272
|
const vectorData = field(node.change, "vectorData", "object", where);
|
|
@@ -316,7 +280,9 @@ export const convertVector = (node, walk) => {
|
|
|
316
280
|
report: (kind, reason) => { networkReports.push([kind, reason]); },
|
|
317
281
|
};
|
|
318
282
|
try {
|
|
319
|
-
|
|
283
|
+
const decoded = vectorDataNetwork(node, vectorData, networkBlob, networkWalk);
|
|
284
|
+
vectorNetwork = decoded.network;
|
|
285
|
+
endpointCaps = decoded.endpointCaps;
|
|
320
286
|
fromNetwork = true;
|
|
321
287
|
for (const [kind, reason] of networkReports)
|
|
322
288
|
walk.report(kind, reason);
|
|
@@ -333,7 +299,7 @@ export const convertVector = (node, walk) => {
|
|
|
333
299
|
}
|
|
334
300
|
if (vectorNetwork === undefined) {
|
|
335
301
|
try {
|
|
336
|
-
vectorNetwork = fillGeometryNetwork(node, walk);
|
|
302
|
+
vectorNetwork = fillGeometryNetwork(node, walk, vectorData);
|
|
337
303
|
}
|
|
338
304
|
catch (error) {
|
|
339
305
|
if (!(error instanceof GeometryRefused))
|
|
@@ -345,30 +311,203 @@ export const convertVector = (node, walk) => {
|
|
|
345
311
|
walk.report("approximated", `${networkProblem}; the fillGeometry outline is used instead`);
|
|
346
312
|
}
|
|
347
313
|
const base = layerBase(node, walk, "Vector", true);
|
|
348
|
-
|
|
349
|
-
const
|
|
350
|
-
|
|
314
|
+
let bounds;
|
|
315
|
+
const paintBox = () => {
|
|
316
|
+
if (bounds === undefined)
|
|
317
|
+
bounds = pathPaintBox(vectorNetwork) ?? null;
|
|
318
|
+
return bounds ?? undefined;
|
|
319
|
+
};
|
|
320
|
+
const fills = nodePaints(node, walk, "fillPaints", paintBox);
|
|
321
|
+
const { strokes, cap } = pathStrokes(node, walk, paintBox);
|
|
351
322
|
const radius = fromNetwork ? field(node.change, "cornerRadius", "number", where) ?? 0 : 0;
|
|
352
323
|
const style = {
|
|
353
324
|
...(fills.length > 0 ? { fills } : {}), ...(strokes.length > 0 ? { strokes } : {}), ...(radius > 0 ? { cornerRadius: radius } : {}),
|
|
354
325
|
...nodeEffects(node, walk),
|
|
355
326
|
};
|
|
356
|
-
const path = { ...base, type: "shape", shape: "path", vectorNetwork
|
|
357
|
-
|
|
327
|
+
const path = { ...base, type: "shape", shape: "path", vectorNetwork, transform, style, startCap: cap, endCap: cap };
|
|
328
|
+
const converted = lineWhereItFills(path, node.change, endpointCaps);
|
|
329
|
+
if (converted.shape === "path" && (node.type === "VECTOR" || Object.keys(endpointCaps).length > 0)) {
|
|
330
|
+
carry(converted, { vectorStrokeCaps: endpointCaps });
|
|
331
|
+
}
|
|
332
|
+
if (converted.shape === "path" && converted.vectorNetwork.regions.some((region) => region.fills?.some(imageCropOf))) {
|
|
333
|
+
const size = field(node.change, "size", "object", where);
|
|
334
|
+
if (size) {
|
|
335
|
+
const width = field(size, "x", "number", `${where} size`), height = field(size, "y", "number", `${where} size`);
|
|
336
|
+
if (width !== undefined && height !== undefined)
|
|
337
|
+
carry(converted, { regionCropSize: { width, height } });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return converted;
|
|
358
341
|
};
|
|
342
|
+
/** Moondraft's boolean group operation for each Figma `booleanOperation`. */
|
|
343
|
+
const BOOLEAN_OPERATIONS = { UNION: "union", SUBTRACT: "subtract", INTERSECT: "intersect", XOR: "exclude" };
|
|
344
|
+
/** The Figma layer types a boolean group combines as shapes (Moondraft imports them as paths or rectangles). */
|
|
345
|
+
const OPERAND_SHAPES = new Set(["VECTOR", "RECTANGLE", "ROUNDED_RECTANGLE", "ELLIPSE", "STAR", "REGULAR_POLYGON", "LINE"]);
|
|
359
346
|
/**
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
* a
|
|
347
|
+
* Moondraft patch 0010: how a Figma BOOLEAN_OPERATION imports. A live boolean
|
|
348
|
+
* group (`operation`, Moondraft's name for it) when Moondraft's boolean group
|
|
349
|
+
* can stand for it: an operation it has, a transform that only moves and
|
|
350
|
+
* turns, a node that does not fill its auto layout parent (a group cannot),
|
|
351
|
+
* and shown operands that are all shapes, nested boolean operations or groups
|
|
352
|
+
* of those (a boolean group skips any other layer, such as text, and a mask).
|
|
353
|
+
* Otherwise `why` says what keeps it flattened to its outline.
|
|
354
|
+
*/
|
|
355
|
+
export function liveBooleanOf(node, inFlow = false) {
|
|
356
|
+
const named = node.change["booleanOperation"];
|
|
357
|
+
const operation = typeof named === "string" ? BOOLEAN_OPERATIONS[named] : undefined;
|
|
358
|
+
if (operation === undefined)
|
|
359
|
+
return { why: typeof named === "string" ? `its operation ${named} has no boolean group` : "it names no operation" };
|
|
360
|
+
const geometry = frameGeometry(node);
|
|
361
|
+
if (geometry === null)
|
|
362
|
+
return { why: "it has no complete size or transform" };
|
|
363
|
+
const { m00, m01, m10, m11 } = geometry.m, tolerance = 1e-4;
|
|
364
|
+
if (Math.abs(m00 ** 2 + m10 ** 2 - 1) > tolerance || Math.abs(m01 ** 2 + m11 ** 2 - 1) > tolerance || Math.abs(m00 * m01 + m10 * m11) > tolerance) {
|
|
365
|
+
return { why: "its transform scales or skews, which a boolean group does not" };
|
|
366
|
+
}
|
|
367
|
+
// Moondraft's adapter keeps no flip on a frame, so the operands would land mirrored.
|
|
368
|
+
if (m00 * m11 - m01 * m10 < 0)
|
|
369
|
+
return { why: "it is flipped, which a boolean group does not keep" };
|
|
370
|
+
if (asksToFill(node.change))
|
|
371
|
+
return { why: "it fills its auto layout parent, which a boolean group does not" };
|
|
372
|
+
// A group's box is its layers' (the editor fits it to them), so in an auto layout flow it must be the operation's own.
|
|
373
|
+
if (inFlow && !operandsFill(node, geometry.sides))
|
|
374
|
+
return { why: "it sits in an auto layout flow, where a boolean group would take its operands' box, not its own" };
|
|
375
|
+
const unsupported = (layer) => {
|
|
376
|
+
for (const child of layer.children) {
|
|
377
|
+
if (child.change["visible"] === false)
|
|
378
|
+
continue;
|
|
379
|
+
if (child.change["mask"] === true)
|
|
380
|
+
return "an operand is a mask";
|
|
381
|
+
// A nested boolean operation is an operand either way: a boolean group, or the path it is flattened to.
|
|
382
|
+
if (OPERAND_SHAPES.has(child.type) || child.type === "BOOLEAN_OPERATION")
|
|
383
|
+
continue;
|
|
384
|
+
if (child.type === "GROUP" || (child.type === "FRAME" && isFigmaGroup(child.change))) {
|
|
385
|
+
const inside = unsupported(child);
|
|
386
|
+
if (inside !== undefined)
|
|
387
|
+
return inside;
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
return `an operand is a ${child.type}, which a boolean group does not combine`;
|
|
391
|
+
}
|
|
392
|
+
return undefined;
|
|
393
|
+
};
|
|
394
|
+
const why = unsupported(node);
|
|
395
|
+
return why === undefined ? { operation } : { why };
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Whether the boxes its operands will have (all of them: a group fits its hidden layers too) together are the node's
|
|
399
|
+
* own box. A nested boolean operation kept live is a boolean group fitted to its own operands, so its box is theirs.
|
|
400
|
+
*/
|
|
401
|
+
function operandsFill(node, sides) {
|
|
402
|
+
const bounds = fittedBounds(node);
|
|
403
|
+
const near = (a, b) => Math.abs(a - b) < 0.01;
|
|
404
|
+
return bounds !== null && near(bounds[0], 0) && near(bounds[1], 0) && near(bounds[2], sides.x) && near(bounds[3], sides.y);
|
|
405
|
+
}
|
|
406
|
+
/** The box [x0, y0, x1, y1], in the node's own space, of its operands as imported, or `null` if one has no geometry. */
|
|
407
|
+
function fittedBounds(node) {
|
|
408
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
409
|
+
for (const child of node.children) {
|
|
410
|
+
const geometry = frameGeometry(child);
|
|
411
|
+
if (geometry === null)
|
|
412
|
+
return null;
|
|
413
|
+
const { sides: { x: w, y: h }, m } = geometry;
|
|
414
|
+
let own = [0, 0, w, h];
|
|
415
|
+
if (child.type === "BOOLEAN_OPERATION" && "operation" in liveBooleanOf(child))
|
|
416
|
+
own = fittedBounds(child);
|
|
417
|
+
if (own === null)
|
|
418
|
+
return null;
|
|
419
|
+
const [a0, b0, a1, b1] = own;
|
|
420
|
+
for (const [x, y] of [[a0, b0], [a1, b0], [a0, b1], [a1, b1]]) {
|
|
421
|
+
const px = m.m00 * x + m.m01 * y + m.m02, py = m.m10 * x + m.m11 * y + m.m12;
|
|
422
|
+
x0 = Math.min(x0, px);
|
|
423
|
+
y0 = Math.min(y0, py);
|
|
424
|
+
x1 = Math.max(x1, px);
|
|
425
|
+
y1 = Math.max(y1, py);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return x0 <= x1 ? [x0, y0, x1, y1] : null;
|
|
429
|
+
}
|
|
430
|
+
/** Why the walk flattened a boolean operation it could otherwise keep live, by node. */
|
|
431
|
+
const flattenedBecause = new WeakMap();
|
|
432
|
+
/** Moondraft patch 0010: why a boolean operation was flattened, for the walk's report line, if it says. */
|
|
433
|
+
export function whyFlattened(node, inFlow = false) {
|
|
434
|
+
const live = liveBooleanOf(node, inFlow);
|
|
435
|
+
return "why" in live ? live.why : flattenedBecause.get(node);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* A Figma BOOLEAN_OPERATION. Moondraft patch 0010: one `liveBooleanOf` passes
|
|
439
|
+
* is a frame holding its operands, which the walk imports as its layers; it
|
|
440
|
+
* carries its operation (`metadata.figma.boolean`) for Moondraft's adapter,
|
|
441
|
+
* which makes it a boolean group. Its frame is the operation's own, and it
|
|
442
|
+
* takes the constraints its operands share, as the flattened path does. Its
|
|
443
|
+
* look (fills, strokes, effects) is the one the outline would have. The
|
|
444
|
+
* outline Figma stores for the result (`fillGeometry`) is carried too, as the
|
|
445
|
+
* group's first result, so it draws as the flattened path did until an edit
|
|
446
|
+
* recuts it; without one (Figma often stores none for a nested operation) the
|
|
447
|
+
* renderer cuts the result from the operands, and one in an auto layout flow
|
|
448
|
+
* whose operands do not fill its box carries `fixedBox`, for a frame of its
|
|
449
|
+
* own size around the group. Any other is a Taco path drawn
|
|
450
|
+
* from its `fillGeometry`, its operands not imported (the walk reports the
|
|
451
|
+
* flattening once, with the reason), or is left out without one, as before.
|
|
364
452
|
*/
|
|
365
453
|
export const convertBooleanOperation = (node, walk) => {
|
|
366
|
-
|
|
367
|
-
|
|
454
|
+
const hasOutline = (field(node.change, "fillGeometry", "list", `node ${node.id}`)?.length ?? 0) > 0;
|
|
455
|
+
const { vectorData, ...change } = node.change;
|
|
456
|
+
// Boolean results use the stored outline, which can still refer to the style table.
|
|
457
|
+
const outlineStyles = isObject(vectorData) ? { styleOverrideTable: vectorData["styleOverrideTable"] } : undefined;
|
|
458
|
+
const flat = { ...node, change: { ...change, ...operandConstraints(node), vectorData: outlineStyles } };
|
|
459
|
+
// Without an outline to flatten to, one in a flow stays live (its box becomes its operands'), rather than left out.
|
|
460
|
+
const live = liveBooleanOf(node, walk.inFlow === true && hasOutline);
|
|
461
|
+
// Kept live there, it sits in a frame of its own box (Moondraft's adapter), so that the flow places its siblings right.
|
|
462
|
+
const fixedBox = !hasOutline && walk.inFlow === true && "operation" in live && !operandsFill(node, frameGeometry(node).sides);
|
|
463
|
+
if (!("operation" in live)) {
|
|
464
|
+
if (hasOutline)
|
|
465
|
+
return convertVector(flat, walk);
|
|
466
|
+
walk.report("node-left-out", `the boolean operation has no fillGeometry outline to flatten it to, and ${live.why}`);
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
// The outline is the group's first result and the source of its look; its reports are kept for the layer it makes.
|
|
470
|
+
const reports = [];
|
|
471
|
+
const outline = hasOutline ? convertVector(flat, { ...walk, report: (kind, reason) => { reports.push([kind, reason]); } }) : null;
|
|
472
|
+
const forward = () => { for (const [kind, reason] of reports)
|
|
473
|
+
walk.report(kind, reason); };
|
|
474
|
+
if (hasOutline && (outline?.type !== "shape" || outline.shape !== "path")) {
|
|
475
|
+
forward();
|
|
476
|
+
return outline;
|
|
477
|
+
}
|
|
478
|
+
if (outline?.type === "shape" && outline.shape === "path" && outline.vectorNetwork.regions.some((region) => region.fills !== undefined)) {
|
|
479
|
+
// Keep authored region paints on a path so recutting the boolean cannot discard them.
|
|
480
|
+
flattenedBecause.set(node, "its regions are painted apart, which one boolean group look does not");
|
|
481
|
+
forward();
|
|
482
|
+
return outline;
|
|
483
|
+
}
|
|
484
|
+
// framedBase reports the layer diagnostics the outline's own conversion reported too: forward the outline's others.
|
|
485
|
+
const shared = new Set();
|
|
486
|
+
const base = framedBase(flat, { ...walk, report: (kind, reason) => { shared.add(`${kind}\n${reason}`); walk.report(kind, reason); } }, "Boolean", true);
|
|
487
|
+
if (base === null)
|
|
368
488
|
return null;
|
|
489
|
+
for (const [kind, reason] of reports)
|
|
490
|
+
if (!shared.has(`${kind}\n${reason}`))
|
|
491
|
+
walk.report(kind, reason);
|
|
492
|
+
const { cornerRadius: _radius, cornerRadii: _radii, cornerSmoothing: _smoothing, ...style } = outline?.type === "shape" ? outline.style
|
|
493
|
+
: { ...visualStyle(node, walk), ...nodeEffects(node, walk) };
|
|
494
|
+
const frame = { ...base, type: "frame", children: walk.convertChildren(), layout: { kind: "absolute" }, style, clipContent: false };
|
|
495
|
+
if (outline?.type === "shape" && outline.shape === "path") {
|
|
496
|
+
// The outline's transform in the operation's own space (its transform only moves and turns, see liveBooleanOf), so
|
|
497
|
+
// that it stays right when the walk moves the operation's frame (a group it is in is fitted to its layers).
|
|
498
|
+
const { m00, m01, m02, m10, m11, m12 } = frameGeometry(node).m;
|
|
499
|
+
const [a, b, c, d, e, f] = outline.transform;
|
|
500
|
+
const turned = [m00 * a + m10 * b, m01 * a + m11 * b, m00 * c + m10 * d, m01 * c + m11 * d,
|
|
501
|
+
m00 * (e - m02) + m10 * (f - m12), m01 * (e - m02) + m11 * (f - m12)];
|
|
502
|
+
// The outline is placed by the operation's own transform, so this is the identity but for float roundoff, which
|
|
503
|
+
// would split clipboard variants that differ only in rotation (variantTrees compares it exactly).
|
|
504
|
+
const identity = [1, 0, 0, 1, 0, 0];
|
|
505
|
+
const local = turned.every((v, i) => Math.abs(v - identity[i]) < 1e-6) ? identity : turned;
|
|
506
|
+
carry(frame, { boolean: { operation: live.operation, transform: local, vectorNetwork: outline.vectorNetwork } });
|
|
369
507
|
}
|
|
370
|
-
|
|
371
|
-
|
|
508
|
+
else
|
|
509
|
+
carry(frame, { boolean: { operation: live.operation, ...(fixedBox ? { fixedBox: true } : {}) } });
|
|
510
|
+
return frame;
|
|
372
511
|
};
|
|
373
512
|
/**
|
|
374
513
|
* The constraints a flattened boolean operation resizes by. Figma resizes a
|
|
@@ -412,7 +551,7 @@ function operandConstraints(node) {
|
|
|
412
551
|
* VECTORs and LINEs. A line's stroke is drawn centred, as Figma draws the
|
|
413
552
|
* stroke of an open path.
|
|
414
553
|
*/
|
|
415
|
-
export function lineWhereItFills(path, change) {
|
|
554
|
+
export function lineWhereItFills(path, change, endpointCaps) {
|
|
416
555
|
if (!asksToFill(change))
|
|
417
556
|
return path;
|
|
418
557
|
const tolerance = 1e-4;
|
|
@@ -432,6 +571,8 @@ export function lineWhereItFills(path, change) {
|
|
|
432
571
|
const centred = strokes?.map((stroke) => ({ ...stroke, position: "center" }));
|
|
433
572
|
// A segment drawn right to left keeps its caps on the ends they were on.
|
|
434
573
|
const reversed = end.x < start.x;
|
|
574
|
+
const ownStartCap = endpointCaps?.[start.id] ?? startCap;
|
|
575
|
+
const ownEndCap = endpointCaps?.[end.id] ?? endCap;
|
|
435
576
|
const limits = sizeLimitsOf(change);
|
|
436
577
|
const line = {
|
|
437
578
|
...base,
|
|
@@ -441,8 +582,8 @@ export function lineWhereItFills(path, change) {
|
|
|
441
582
|
rotation: 0,
|
|
442
583
|
...(Object.keys(limits).length > 0 ? { resizing: limits } : {}),
|
|
443
584
|
style: { ...rest, ...(centred === undefined ? {} : { strokes: centred }) },
|
|
444
|
-
startCap: reversed ?
|
|
445
|
-
endCap: reversed ?
|
|
585
|
+
startCap: reversed ? ownEndCap : ownStartCap,
|
|
586
|
+
endCap: reversed ? ownStartCap : ownEndCap,
|
|
446
587
|
};
|
|
447
588
|
return line;
|
|
448
589
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type VectorNetwork, type VectorRegion } from "../../../model/index.ts";
|
|
2
|
+
import type { StrokeCap } from "../../../model/types.ts";
|
|
2
3
|
import { type FigmaObject } from "../fields.ts";
|
|
3
4
|
import type { FigmaTreeNode } from "../tree.ts";
|
|
4
5
|
import type { NodeWalk } from "../walk.ts";
|
|
@@ -86,13 +87,28 @@ interface RawNetwork {
|
|
|
86
87
|
* segment count and that many segment indices.
|
|
87
88
|
*/
|
|
88
89
|
export declare function decodeNetworkBlob(bytes: Uint8Array, where: string): RawNetwork;
|
|
90
|
+
/** The node's `styleOverrideTable` entries by `styleID`. */
|
|
91
|
+
export declare function vectorStyleTable(vectorData: FigmaObject | undefined, where: string): Map<number, FigmaObject>;
|
|
92
|
+
interface RegionStyle {
|
|
93
|
+
readonly index: number;
|
|
94
|
+
readonly styleId: number;
|
|
95
|
+
readonly at: string;
|
|
96
|
+
}
|
|
97
|
+
/** Resolve and budget every region's source list before converting paints for any region. */
|
|
98
|
+
export declare function applyRegionStyles(node: FigmaTreeNode, walk: NodeWalk, network: Pick<VectorNetwork, "vertices" | "segments"> & {
|
|
99
|
+
regions: VectorRegion[];
|
|
100
|
+
}, styles: ReadonlyMap<number, FigmaObject>, styled: readonly RegionStyle[], outline?: boolean): void;
|
|
89
101
|
/**
|
|
90
102
|
* Builds a Taco network from the node's `vectorData`, scaled from
|
|
91
103
|
* `normalizedSize` to the node's `size` (grida,
|
|
92
104
|
* packages/grida-canvas-io-figma/lib.ts:5064-5120: x by size.x /
|
|
93
105
|
* normalizedSize.x, y likewise, an axis whose normalized size is 0 unscaled).
|
|
94
|
-
* Region styles become region fills; vertex styles give corner radii
|
|
95
|
-
*
|
|
106
|
+
* Region styles become region fills; vertex styles also give corner radii.
|
|
107
|
+
* Cap overrides are collected for open endpoints. Other style fields are
|
|
108
|
+
* reported. Throws `NetworkRefused`.
|
|
96
109
|
*/
|
|
97
|
-
export declare function vectorDataNetwork(node: FigmaTreeNode, vectorData: FigmaObject, reference: number, walk: NodeWalk):
|
|
110
|
+
export declare function vectorDataNetwork(node: FigmaTreeNode, vectorData: FigmaObject, reference: number, walk: NodeWalk): {
|
|
111
|
+
network: VectorNetwork;
|
|
112
|
+
endpointCaps: Record<string, StrokeCap>;
|
|
113
|
+
};
|
|
98
114
|
export {};
|