@moondraft/import 0.0.0-stage → 0.1.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 +7 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +169 -0
- package/dist/adapter/convert.d.ts +122 -0
- package/dist/adapter/convert.js +2302 -0
- package/dist/adapter/figma.d.ts +9 -0
- package/dist/adapter/figma.js +30 -0
- package/dist/adapter/file-components.d.ts +14 -0
- package/dist/adapter/file-components.js +262 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +562 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +45 -0
- package/dist/adapter/link.js +688 -0
- package/dist/adapter/payload.d.ts +31 -0
- package/dist/adapter/payload.js +78 -0
- package/dist/adapter/shapes.d.ts +31 -0
- package/dist/adapter/shapes.js +75 -0
- package/dist/adapter/variables.d.ts +44 -0
- package/dist/adapter/variables.js +103 -0
- package/dist/figma-file.d.ts +38 -0
- package/dist/figma-file.js +97 -0
- package/dist/figma-worker.d.ts +11 -0
- package/dist/figma-worker.js +12 -0
- package/dist/figma.d.ts +26 -0
- package/dist/figma.js +96 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +49 -0
- package/dist/report.d.ts +15 -0
- package/dist/report.js +69 -0
- package/dist/svg.d.ts +33 -0
- package/dist/svg.js +44 -0
- package/dist/syntax.d.ts +8 -0
- package/dist/syntax.js +19 -0
- package/dist/taco/figma-import/clipboard.d.ts +25 -0
- package/dist/taco/figma-import/clipboard.js +87 -0
- package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
- package/dist/taco/figma-import/clipboardSize.js +107 -0
- package/dist/taco/figma-import/container/compression.d.ts +9 -0
- package/dist/taco/figma-import/container/compression.js +174 -0
- package/dist/taco/figma-import/container/error.d.ts +15 -0
- package/dist/taco/figma-import/container/error.js +19 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +38 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
- package/dist/taco/figma-import/container/limits.d.ts +22 -0
- package/dist/taco/figma-import/container/limits.js +22 -0
- package/dist/taco/figma-import/container/zip.d.ts +14 -0
- package/dist/taco/figma-import/container/zip.js +154 -0
- package/dist/taco/figma-import/figFile.d.ts +34 -0
- package/dist/taco/figma-import/figFile.js +91 -0
- package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
- package/dist/taco/figma-import/figFileDocument.js +100 -0
- package/dist/taco/figma-import/import/blobs.d.ts +10 -0
- package/dist/taco/figma-import/import/blobs.js +23 -0
- package/dist/taco/figma-import/import/carry.d.ts +11 -0
- package/dist/taco/figma-import/import/carry.js +33 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +84 -0
- package/dist/taco/figma-import/import/componentProps.js +313 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +83 -0
- package/dist/taco/figma-import/import/componentSets.js +647 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
- package/dist/taco/figma-import/import/converters/effects.js +127 -0
- package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
- package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +55 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +72 -0
- package/dist/taco/figma-import/import/converters/gradient.js +106 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/group.js +39 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +83 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +34 -0
- package/dist/taco/figma-import/import/converters/layout.js +168 -0
- package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
- package/dist/taco/figma-import/import/converters/line.js +40 -0
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
- package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
- package/dist/taco/figma-import/import/converters/section.d.ts +9 -0
- package/dist/taco/figma-import/import/converters/section.js +35 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +68 -0
- package/dist/taco/figma-import/import/converters/shared.js +362 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +41 -0
- package/dist/taco/figma-import/import/converters/text.js +576 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +84 -0
- package/dist/taco/figma-import/import/converters/vector.js +468 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +98 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +385 -0
- package/dist/taco/figma-import/import/fields.d.ts +15 -0
- package/dist/taco/figma-import/import/fields.js +29 -0
- package/dist/taco/figma-import/import/instances.d.ts +163 -0
- package/dist/taco/figma-import/import/instances.js +1323 -0
- package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
- package/dist/taco/figma-import/import/nestingCycles.js +488 -0
- package/dist/taco/figma-import/import/slots.d.ts +56 -0
- package/dist/taco/figma-import/import/slots.js +96 -0
- package/dist/taco/figma-import/import/styles.d.ts +46 -0
- package/dist/taco/figma-import/import/styles.js +42 -0
- package/dist/taco/figma-import/import/tree.d.ts +66 -0
- package/dist/taco/figma-import/import/tree.js +194 -0
- package/dist/taco/figma-import/import/types.d.ts +232 -0
- package/dist/taco/figma-import/import/types.js +33 -0
- package/dist/taco/figma-import/import/variables.d.ts +136 -0
- package/dist/taco/figma-import/import/variables.js +762 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +480 -0
- package/dist/taco/figma-import/import/walk.d.ts +159 -0
- package/dist/taco/figma-import/import/walk.js +1297 -0
- package/dist/taco/figma-import/index.d.ts +22 -0
- package/dist/taco/figma-import/index.js +13 -0
- package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
- package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
- package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
- package/dist/taco/figma-import/kiwi/error.js +29 -0
- package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
- package/dist/taco/figma-import/kiwi/limits.js +18 -0
- package/dist/taco/figma-import/kiwi/message.d.ts +10 -0
- package/dist/taco/figma-import/kiwi/message.js +145 -0
- package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
- package/dist/taco/figma-import/kiwi/schema.js +206 -0
- package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
- package/dist/taco/figma-import/kiwi/types.js +18 -0
- package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
- package/dist/taco/model/agent-hidden-fields.js +29 -0
- package/dist/taco/model/asset-users.d.ts +23 -0
- package/dist/taco/model/asset-users.js +128 -0
- package/dist/taco/model/auto-layout-lines.d.ts +32 -0
- package/dist/taco/model/auto-layout-lines.js +70 -0
- package/dist/taco/model/codec.d.ts +444 -0
- package/dist/taco/model/codec.js +5635 -0
- package/dist/taco/model/color.d.ts +11 -0
- package/dist/taco/model/color.js +61 -0
- package/dist/taco/model/comment-operations.d.ts +102 -0
- package/dist/taco/model/comment-operations.js +227 -0
- package/dist/taco/model/component-evaluation.d.ts +94 -0
- package/dist/taco/model/component-evaluation.js +548 -0
- package/dist/taco/model/component-expressions.d.ts +242 -0
- package/dist/taco/model/component-expressions.js +1082 -0
- package/dist/taco/model/component-properties.d.ts +111 -0
- package/dist/taco/model/component-properties.js +339 -0
- package/dist/taco/model/component-samples.d.ts +83 -0
- package/dist/taco/model/component-samples.js +290 -0
- package/dist/taco/model/component-validity.d.ts +85 -0
- package/dist/taco/model/component-validity.js +397 -0
- package/dist/taco/model/components.d.ts +155 -0
- package/dist/taco/model/components.js +505 -0
- package/dist/taco/model/connector-ends.d.ts +20 -0
- package/dist/taco/model/connector-ends.js +188 -0
- package/dist/taco/model/connector-geometry.d.ts +74 -0
- package/dist/taco/model/connector-geometry.js +293 -0
- package/dist/taco/model/corner-radii.d.ts +26 -0
- package/dist/taco/model/corner-radii.js +47 -0
- package/dist/taco/model/derivations.d.ts +81 -0
- package/dist/taco/model/derivations.js +151 -0
- package/dist/taco/model/export-settings.d.ts +53 -0
- package/dist/taco/model/export-settings.js +156 -0
- package/dist/taco/model/expressions/ast.d.ts +153 -0
- package/dist/taco/model/expressions/ast.js +6 -0
- package/dist/taco/model/expressions/cache.d.ts +23 -0
- package/dist/taco/model/expressions/cache.js +43 -0
- package/dist/taco/model/expressions/check.d.ts +72 -0
- package/dist/taco/model/expressions/check.js +517 -0
- package/dist/taco/model/expressions/completion.d.ts +61 -0
- package/dist/taco/model/expressions/completion.js +421 -0
- package/dist/taco/model/expressions/evaluate.d.ts +32 -0
- package/dist/taco/model/expressions/evaluate.js +519 -0
- package/dist/taco/model/expressions/forms.d.ts +84 -0
- package/dist/taco/model/expressions/forms.js +200 -0
- package/dist/taco/model/expressions/index.d.ts +14 -0
- package/dist/taco/model/expressions/index.js +17 -0
- package/dist/taco/model/expressions/lexer.d.ts +42 -0
- package/dist/taco/model/expressions/lexer.js +213 -0
- package/dist/taco/model/expressions/library.d.ts +33 -0
- package/dist/taco/model/expressions/library.js +117 -0
- package/dist/taco/model/expressions/names.d.ts +43 -0
- package/dist/taco/model/expressions/names.js +99 -0
- package/dist/taco/model/expressions/parser.d.ts +15 -0
- package/dist/taco/model/expressions/parser.js +438 -0
- package/dist/taco/model/expressions/rename.d.ts +34 -0
- package/dist/taco/model/expressions/rename.js +46 -0
- package/dist/taco/model/expressions/source.d.ts +15 -0
- package/dist/taco/model/expressions/source.js +161 -0
- package/dist/taco/model/expressions/types.d.ts +119 -0
- package/dist/taco/model/expressions/types.js +102 -0
- package/dist/taco/model/expressions/values.d.ts +97 -0
- package/dist/taco/model/expressions/values.js +160 -0
- package/dist/taco/model/geometry-limits.d.ts +16 -0
- package/dist/taco/model/geometry-limits.js +67 -0
- package/dist/taco/model/geometry.d.ts +47 -0
- package/dist/taco/model/geometry.js +193 -0
- package/dist/taco/model/group-bounds.d.ts +38 -0
- package/dist/taco/model/group-bounds.js +144 -0
- package/dist/taco/model/id-table.d.ts +37 -0
- package/dist/taco/model/id-table.js +80 -0
- package/dist/taco/model/index.d.ts +25 -0
- package/dist/taco/model/index.js +27 -0
- package/dist/taco/model/instance-resolver.d.ts +372 -0
- package/dist/taco/model/instance-resolver.js +1748 -0
- package/dist/taco/model/layer-syntax.d.ts +340 -0
- package/dist/taco/model/layer-syntax.js +3506 -0
- package/dist/taco/model/layout/constraints.d.ts +84 -0
- package/dist/taco/model/layout/constraints.js +445 -0
- package/dist/taco/model/layout/frame-layout.d.ts +46 -0
- package/dist/taco/model/layout/frame-layout.js +618 -0
- package/dist/taco/model/layout/node-sizing.d.ts +163 -0
- package/dist/taco/model/layout/node-sizing.js +486 -0
- package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
- package/dist/taco/model/legacy-connector-caps.js +70 -0
- package/dist/taco/model/linear-call.d.ts +48 -0
- package/dist/taco/model/linear-call.js +58 -0
- package/dist/taco/model/new-nodes.d.ts +145 -0
- package/dist/taco/model/new-nodes.js +277 -0
- package/dist/taco/model/node-builder.d.ts +449 -0
- package/dist/taco/model/node-builder.js +2294 -0
- package/dist/taco/model/node-table-watch.d.ts +36 -0
- package/dist/taco/model/node-table-watch.js +63 -0
- package/dist/taco/model/node-transform.d.ts +39 -0
- package/dist/taco/model/node-transform.js +79 -0
- package/dist/taco/model/node-users-index.d.ts +10 -0
- package/dist/taco/model/node-users-index.js +82 -0
- package/dist/taco/model/operation-references.d.ts +80 -0
- package/dist/taco/model/operation-references.js +399 -0
- package/dist/taco/model/operations.d.ts +610 -0
- package/dist/taco/model/operations.js +2882 -0
- package/dist/taco/model/overlay-table.d.ts +71 -0
- package/dist/taco/model/overlay-table.js +171 -0
- package/dist/taco/model/override-remap.d.ts +69 -0
- package/dist/taco/model/override-remap.js +230 -0
- package/dist/taco/model/parent-table.d.ts +53 -0
- package/dist/taco/model/parent-table.js +193 -0
- package/dist/taco/model/problem-collector.d.ts +27 -0
- package/dist/taco/model/problem-collector.js +101 -0
- package/dist/taco/model/sha256.d.ts +10 -0
- package/dist/taco/model/sha256.js +74 -0
- package/dist/taco/model/shape-geometry.d.ts +96 -0
- package/dist/taco/model/shape-geometry.js +368 -0
- package/dist/taco/model/shared-node-table.d.ts +97 -0
- package/dist/taco/model/shared-node-table.js +546 -0
- package/dist/taco/model/slots.d.ts +113 -0
- package/dist/taco/model/slots.js +281 -0
- package/dist/taco/model/stroke-settings.d.ts +37 -0
- package/dist/taco/model/stroke-settings.js +90 -0
- package/dist/taco/model/svg-path-import.d.ts +89 -0
- package/dist/taco/model/svg-path-import.js +219 -0
- package/dist/taco/model/text-measurement.d.ts +142 -0
- package/dist/taco/model/text-measurement.js +846 -0
- package/dist/taco/model/text-reads.d.ts +66 -0
- package/dist/taco/model/text-reads.js +128 -0
- package/dist/taco/model/text-sizer.d.ts +358 -0
- package/dist/taco/model/text-sizer.js +996 -0
- package/dist/taco/model/theme-users.d.ts +59 -0
- package/dist/taco/model/theme-users.js +312 -0
- package/dist/taco/model/thumbnail.d.ts +15 -0
- package/dist/taco/model/thumbnail.js +34 -0
- package/dist/taco/model/types.d.ts +2347 -0
- package/dist/taco/model/types.js +366 -0
- package/dist/taco/model/unknown-property.d.ts +5 -0
- package/dist/taco/model/unknown-property.js +7 -0
- package/dist/taco/model/validated-documents.d.ts +22 -0
- package/dist/taco/model/validated-documents.js +22 -0
- package/dist/taco/model/variable-modes.d.ts +61 -0
- package/dist/taco/model/variable-modes.js +438 -0
- package/dist/taco/model/variable-references.d.ts +54 -0
- package/dist/taco/model/variable-references.js +183 -0
- package/dist/taco/model/variables.d.ts +24 -0
- package/dist/taco/model/variables.js +151 -0
- package/dist/taco/model/vector-network.d.ts +34 -0
- package/dist/taco/model/vector-network.js +411 -0
- package/dist/taco/svg-import/import-svg.d.ts +36 -0
- package/dist/taco/svg-import/import-svg.js +729 -0
- package/dist/taco/svg-import/index.d.ts +3 -0
- package/dist/taco/svg-import/index.js +3 -0
- package/dist/taco/svg-import/limits/limits.d.ts +79 -0
- package/dist/taco/svg-import/limits/limits.js +160 -0
- package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
- package/dist/taco/svg-import/paint/gradients.js +429 -0
- package/dist/taco/svg-import/paint/paint.d.ts +32 -0
- package/dist/taco/svg-import/paint/paint.js +162 -0
- package/dist/taco/svg-import/paths/paths.d.ts +58 -0
- package/dist/taco/svg-import/paths/paths.js +205 -0
- package/dist/taco/svg-import/public-types.d.ts +279 -0
- package/dist/taco/svg-import/public-types.js +56 -0
- package/dist/taco/svg-import/report/report.d.ts +44 -0
- package/dist/taco/svg-import/report/report.js +79 -0
- package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
- package/dist/taco/svg-import/reuse/reuse.js +840 -0
- package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
- package/dist/taco/svg-import/shapes/shapes.js +386 -0
- package/dist/taco/svg-import/structure/effects.d.ts +26 -0
- package/dist/taco/svg-import/structure/effects.js +618 -0
- package/dist/taco/svg-import/structure/image.d.ts +24 -0
- package/dist/taco/svg-import/structure/image.js +495 -0
- package/dist/taco/svg-import/structure/walk.d.ts +85 -0
- package/dist/taco/svg-import/structure/walk.js +544 -0
- package/dist/taco/svg-import/style/color.d.ts +40 -0
- package/dist/taco/svg-import/style/color.js +275 -0
- package/dist/taco/svg-import/style/computed.d.ts +214 -0
- package/dist/taco/svg-import/style/computed.js +1534 -0
- package/dist/taco/svg-import/style/css.d.ts +127 -0
- package/dist/taco/svg-import/style/css.js +541 -0
- package/dist/taco/svg-import/style/media.d.ts +25 -0
- package/dist/taco/svg-import/style/media.js +307 -0
- package/dist/taco/svg-import/style/properties.d.ts +43 -0
- package/dist/taco/svg-import/style/properties.js +135 -0
- package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
- package/dist/taco/svg-import/style/selector-reach.js +169 -0
- package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
- package/dist/taco/svg-import/style/selector-validity.js +307 -0
- package/dist/taco/svg-import/text/text.d.ts +29 -0
- package/dist/taco/svg-import/text/text.js +545 -0
- package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
- package/dist/taco/svg-import/transform/matrix.js +229 -0
- package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
- package/dist/taco/svg-import/tree/element-data.js +155 -0
- package/dist/taco/svg-import/tree/elements.d.ts +36 -0
- package/dist/taco/svg-import/tree/elements.js +121 -0
- package/dist/taco/svg-import/units/length.d.ts +43 -0
- package/dist/taco/svg-import/units/length.js +125 -0
- package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
- package/dist/taco/svg-import/units/viewbox.js +77 -0
- package/package.json +54 -4
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import { getPathBounds, vectorHandleId, vectorRegionId, vectorSegmentId, vectorRegionToSvgPathData, vectorVertexId, } from "../../../model/index.js";
|
|
2
|
+
import { field, isObject } from "../fields.js";
|
|
3
|
+
import { nodePaints } from "./shared.js";
|
|
4
|
+
/**
|
|
5
|
+
* A function giving the tight bounds of a region's outline in node pixels,
|
|
6
|
+
* which the renderer places the region's gradient fills on, or `undefined`
|
|
7
|
+
* when it has none. The network's vertices and segments are indexed once, so
|
|
8
|
+
* each region costs only its own loop entries, not the whole network.
|
|
9
|
+
*/
|
|
10
|
+
export function regionBoxes(network) {
|
|
11
|
+
const vertexById = new Map(network.vertices.map((vertex) => [vertex.id, vertex]));
|
|
12
|
+
const segmentById = new Map(network.segments.map((segment) => [segment.id, segment]));
|
|
13
|
+
return (region) => {
|
|
14
|
+
const vertices = new Map();
|
|
15
|
+
const segments = new Map();
|
|
16
|
+
for (const loop of region.loops) {
|
|
17
|
+
for (const reference of loop.segments) {
|
|
18
|
+
const segment = segmentById.get(reference.segmentId);
|
|
19
|
+
if (segment === undefined)
|
|
20
|
+
continue;
|
|
21
|
+
segments.set(segment.id, segment);
|
|
22
|
+
for (const id of [segment.start, segment.end]) {
|
|
23
|
+
const vertex = vertexById.get(id);
|
|
24
|
+
if (vertex !== undefined)
|
|
25
|
+
vertices.set(id, vertex);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const own = { vertices: [...vertices.values()], segments: [...segments.values()], regions: [] };
|
|
30
|
+
return getPathBounds(vectorRegionToSvgPathData(own, region)) ?? undefined;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The most vertices, segments, regions, loops (over all regions) and loop
|
|
35
|
+
* entries (over all loops) one `vectorNetworkBlob` may hold. A blob over any
|
|
36
|
+
* of them is refused before anything is allocated for it.
|
|
37
|
+
*/
|
|
38
|
+
export const VECTOR_NETWORK_LIMITS = {
|
|
39
|
+
vertices: 100_000, segments: 100_000, regions: 10_000, loops: 10_000, loopEntries: 200_000,
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Why a VECTOR's editable vector network is not imported, named at the start
|
|
43
|
+
* of the report reason as `geometry refused (<name>)`:
|
|
44
|
+
* - `network-blob-missing`: the `vectorNetworkBlob` is not in the message's `blobs`.
|
|
45
|
+
* - `network-no-size`: the node has no `size` or its `vectorData` no `normalizedSize`, so the network's scale is unknown.
|
|
46
|
+
* - `network-too-short`: the blob ends inside its header, a vertex, a segment, a region or a loop.
|
|
47
|
+
* - `network-trailing-bytes`: bytes are left after the last region.
|
|
48
|
+
* - `network-too-many-vertices`, `-segments`, `-regions`, `-loops`, `-loop-entries`: over `VECTOR_NETWORK_LIMITS`.
|
|
49
|
+
* - `network-not-finite`: a coordinate or tangent is NaN or infinite.
|
|
50
|
+
* - `network-vertex-out-of-range`: a segment names a vertex the blob does not have.
|
|
51
|
+
* - `network-segment-out-of-range`: a loop names a segment the blob does not have.
|
|
52
|
+
* - `network-empty-region`, `network-empty-loop`: a region with no loops, a loop with no segments.
|
|
53
|
+
* - `network-loop-repeats-segment`: a loop names one segment twice.
|
|
54
|
+
* - `network-loop-broken`: consecutive segments of a loop share no vertex.
|
|
55
|
+
* - `network-no-segments`: the network draws nothing.
|
|
56
|
+
* - `import-command-limit`: the network's segments, each counted as one drawing
|
|
57
|
+
* command, are more than the import has left of `limits.vectorCommands`.
|
|
58
|
+
*
|
|
59
|
+
* The winding rule is one bit (see `decodeNetworkBlob`), so no value of it is unknown.
|
|
60
|
+
*/
|
|
61
|
+
export const VECTOR_NETWORK_PROBLEMS = [
|
|
62
|
+
"network-blob-missing",
|
|
63
|
+
"network-no-size",
|
|
64
|
+
"network-too-short",
|
|
65
|
+
"network-trailing-bytes",
|
|
66
|
+
"network-too-many-vertices",
|
|
67
|
+
"network-too-many-segments",
|
|
68
|
+
"network-too-many-regions",
|
|
69
|
+
"network-too-many-loops",
|
|
70
|
+
"network-too-many-loop-entries",
|
|
71
|
+
"network-not-finite",
|
|
72
|
+
"network-vertex-out-of-range",
|
|
73
|
+
"network-segment-out-of-range",
|
|
74
|
+
"network-empty-region",
|
|
75
|
+
"network-empty-loop",
|
|
76
|
+
"network-loop-repeats-segment",
|
|
77
|
+
"network-loop-broken",
|
|
78
|
+
"network-no-segments",
|
|
79
|
+
"import-command-limit",
|
|
80
|
+
];
|
|
81
|
+
export class NetworkRefused extends Error {
|
|
82
|
+
problem;
|
|
83
|
+
constructor(problem, detail) {
|
|
84
|
+
super(`geometry refused (${problem}): ${detail}`);
|
|
85
|
+
this.problem = problem;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const VERTEX_BYTES = 12;
|
|
89
|
+
const SEGMENT_BYTES = 28;
|
|
90
|
+
/**
|
|
91
|
+
* Decodes a `vectorNetworkBlob`, as fig2sketch (src/figformat/vector_network.py:4-69)
|
|
92
|
+
* and grida (packages/grida-canvas-io-figma/fig-kiwi/blob-parser.ts:150-243)
|
|
93
|
+
* read it; every number is little-endian, u32 for counts, indices and style
|
|
94
|
+
* ids, f32 for coordinates:
|
|
95
|
+
* - header: vertex count, segment count, region count;
|
|
96
|
+
* - each vertex (12 bytes): style id, x, y;
|
|
97
|
+
* - each segment (28 bytes): style id, start vertex, start tangent dx, dy,
|
|
98
|
+
* end vertex, end tangent dx, dy (each tangent relative to its own vertex);
|
|
99
|
+
* - each region: one u32 holding the style id above bit 0 and the winding
|
|
100
|
+
* rule in bit 0 (1 NONZERO, 0 ODD), the loop count, then per loop a
|
|
101
|
+
* segment count and that many segment indices.
|
|
102
|
+
*/
|
|
103
|
+
export function decodeNetworkBlob(bytes, where) {
|
|
104
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
105
|
+
let offset = 0;
|
|
106
|
+
const need = (count, what) => {
|
|
107
|
+
if (count > bytes.length - offset) {
|
|
108
|
+
throw new NetworkRefused("network-too-short", `${where} ends at byte ${String(bytes.length)}, inside ${what} at byte ${String(offset)}`);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const u32 = () => { const value = view.getUint32(offset, true); offset += 4; return value; };
|
|
112
|
+
const f32 = (what) => {
|
|
113
|
+
const value = view.getFloat32(offset, true);
|
|
114
|
+
if (!Number.isFinite(value))
|
|
115
|
+
throw new NetworkRefused("network-not-finite", `${where} has ${what} that is ${String(value)} at byte ${String(offset)}`);
|
|
116
|
+
offset += 4;
|
|
117
|
+
return value;
|
|
118
|
+
};
|
|
119
|
+
const bounded = (count, limit, problem, what) => {
|
|
120
|
+
if (count > limit)
|
|
121
|
+
throw new NetworkRefused(problem, `${where} has ${String(count)} ${what}, over the limit of ${String(limit)}`);
|
|
122
|
+
};
|
|
123
|
+
need(12, "the header");
|
|
124
|
+
const vertexCount = u32();
|
|
125
|
+
const segmentCount = u32();
|
|
126
|
+
const regionCount = u32();
|
|
127
|
+
bounded(vertexCount, VECTOR_NETWORK_LIMITS.vertices, "network-too-many-vertices", "vertices");
|
|
128
|
+
bounded(segmentCount, VECTOR_NETWORK_LIMITS.segments, "network-too-many-segments", "segments");
|
|
129
|
+
bounded(regionCount, VECTOR_NETWORK_LIMITS.regions, "network-too-many-regions", "regions");
|
|
130
|
+
// Each region takes at least 8 bytes, so all three counts are checked before any list is built.
|
|
131
|
+
need(vertexCount * VERTEX_BYTES + segmentCount * SEGMENT_BYTES + regionCount * 8, `the ${String(vertexCount)} vertices, ${String(segmentCount)} segments and ${String(regionCount)} regions it counts`);
|
|
132
|
+
const vertices = [];
|
|
133
|
+
for (let i = 0; i < vertexCount; i++) {
|
|
134
|
+
const styleId = u32();
|
|
135
|
+
vertices.push({ styleId, x: f32(`vertex ${String(i)} x`), y: f32(`vertex ${String(i)} y`) });
|
|
136
|
+
}
|
|
137
|
+
const segments = [];
|
|
138
|
+
for (let i = 0; i < segmentCount; i++) {
|
|
139
|
+
const at = `segment ${String(i)}`;
|
|
140
|
+
const styleId = u32();
|
|
141
|
+
const start = u32();
|
|
142
|
+
const startDx = f32(`${at} start tangent dx`);
|
|
143
|
+
const startDy = f32(`${at} start tangent dy`);
|
|
144
|
+
const end = u32();
|
|
145
|
+
const endDx = f32(`${at} end tangent dx`);
|
|
146
|
+
const endDy = f32(`${at} end tangent dy`);
|
|
147
|
+
for (const vertex of [start, end]) {
|
|
148
|
+
if (vertex >= vertexCount) {
|
|
149
|
+
throw new NetworkRefused("network-vertex-out-of-range", `${where} ${at} names vertex ${String(vertex)}, but the blob has ${String(vertexCount)} vertices`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
segments.push({ styleId, start, startDx, startDy, end, endDx, endDy });
|
|
153
|
+
}
|
|
154
|
+
const regions = [];
|
|
155
|
+
let loopTotal = 0;
|
|
156
|
+
let entryTotal = 0;
|
|
157
|
+
for (let i = 0; i < regionCount; i++) {
|
|
158
|
+
const at = `region ${String(i)}`;
|
|
159
|
+
need(8, `${at}`);
|
|
160
|
+
const styleAndRule = u32();
|
|
161
|
+
const loopCount = u32();
|
|
162
|
+
if (loopCount === 0)
|
|
163
|
+
throw new NetworkRefused("network-empty-region", `${where} ${at} has no loops`);
|
|
164
|
+
loopTotal += loopCount;
|
|
165
|
+
bounded(loopTotal, VECTOR_NETWORK_LIMITS.loops, "network-too-many-loops", "loops");
|
|
166
|
+
need(loopCount * 4, `the ${String(loopCount)} loops of ${at}`);
|
|
167
|
+
const loops = [];
|
|
168
|
+
for (let j = 0; j < loopCount; j++) {
|
|
169
|
+
const loopAt = `${at} loop ${String(j)}`;
|
|
170
|
+
need(4, loopAt);
|
|
171
|
+
const entryCount = u32();
|
|
172
|
+
if (entryCount === 0)
|
|
173
|
+
throw new NetworkRefused("network-empty-loop", `${where} ${loopAt} has no segments`);
|
|
174
|
+
entryTotal += entryCount;
|
|
175
|
+
bounded(entryTotal, VECTOR_NETWORK_LIMITS.loopEntries, "network-too-many-loop-entries", "loop entries");
|
|
176
|
+
need(entryCount * 4, `the ${String(entryCount)} segment indices of ${loopAt}`);
|
|
177
|
+
const loop = [];
|
|
178
|
+
for (let k = 0; k < entryCount; k++) {
|
|
179
|
+
const segment = u32();
|
|
180
|
+
if (segment >= segmentCount) {
|
|
181
|
+
throw new NetworkRefused("network-segment-out-of-range", `${where} ${loopAt} names segment ${String(segment)}, but the blob has ${String(segmentCount)} segments`);
|
|
182
|
+
}
|
|
183
|
+
loop.push(segment);
|
|
184
|
+
}
|
|
185
|
+
loops.push(loop);
|
|
186
|
+
}
|
|
187
|
+
regions.push({ styleId: styleAndRule >>> 1, windingRule: (styleAndRule & 1) === 1 ? "nonzero" : "evenodd", loops });
|
|
188
|
+
}
|
|
189
|
+
if (offset !== bytes.length) {
|
|
190
|
+
throw new NetworkRefused("network-trailing-bytes", `${where} has ${String(bytes.length - offset)} bytes after its last region, from byte ${String(offset)}`);
|
|
191
|
+
}
|
|
192
|
+
return { vertices, segments, regions };
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* A loop's segment indices as Taco references with directions. Figma lists
|
|
196
|
+
* a loop's segments in order but not their direction (fig2sketch,
|
|
197
|
+
* src/converter/shape_path.py:308-331), so the first segment runs towards the
|
|
198
|
+
* vertex it shares with the second and each next one starts where the last
|
|
199
|
+
* ended. A loop that does not end where it started closes for filling only.
|
|
200
|
+
*/
|
|
201
|
+
function orderLoop(network, indices, where) {
|
|
202
|
+
if (new Set(indices).size !== indices.length) {
|
|
203
|
+
throw new NetworkRefused("network-loop-repeats-segment", `${where} names a segment more than once`);
|
|
204
|
+
}
|
|
205
|
+
const segments = indices.map((index) => network.segments[index]);
|
|
206
|
+
const first = segments[0];
|
|
207
|
+
const second = segments[1];
|
|
208
|
+
const firstForward = second === undefined || first.end === second.start || first.end === second.end;
|
|
209
|
+
if (!firstForward && first.start !== second.start && first.start !== second.end) {
|
|
210
|
+
throw new NetworkRefused("network-loop-broken", `${where}: segments ${String(indices[0])} and ${String(indices[1])} share no vertex`);
|
|
211
|
+
}
|
|
212
|
+
const references = [];
|
|
213
|
+
const startVertex = firstForward ? first.start : first.end;
|
|
214
|
+
let current = startVertex;
|
|
215
|
+
segments.forEach((segment, i) => {
|
|
216
|
+
let direction;
|
|
217
|
+
if (i === 0)
|
|
218
|
+
direction = firstForward ? "forward" : "reverse";
|
|
219
|
+
else if (segment.start === current)
|
|
220
|
+
direction = "forward";
|
|
221
|
+
else if (segment.end === current)
|
|
222
|
+
direction = "reverse";
|
|
223
|
+
else
|
|
224
|
+
throw new NetworkRefused("network-loop-broken", `${where}: segment ${String(indices[i])} does not start at vertex ${String(current)}, where segment ${String(indices[i - 1])} ends`);
|
|
225
|
+
current = direction === "forward" ? segment.end : segment.start;
|
|
226
|
+
references.push({ segmentId: vectorSegmentId(`s${String(indices[i])}`), direction });
|
|
227
|
+
});
|
|
228
|
+
return startVertex === current ? { segments: references } : { segments: references, implicitClose: true };
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* The segments outside every region, as one loop when they form exactly one
|
|
232
|
+
* closed chain (each vertex they touch joins two of them, and all are
|
|
233
|
+
* connected), else `undefined`. Figma fills such a chain with the node's
|
|
234
|
+
* fills, as fig2sketch reads it (src/converter/shape_path.py:177-223).
|
|
235
|
+
*/
|
|
236
|
+
function closedChainOutsideRegions(network, used) {
|
|
237
|
+
const free = network.segments.map((_, index) => index).filter((index) => !used.has(index));
|
|
238
|
+
if (free.length === 0)
|
|
239
|
+
return undefined;
|
|
240
|
+
const first = network.segments[free[0]];
|
|
241
|
+
if (free.length === 1)
|
|
242
|
+
return first.start === first.end ? orderLoop(network, free, "the segment outside every region") : undefined;
|
|
243
|
+
const touching = new Map();
|
|
244
|
+
for (const index of free) {
|
|
245
|
+
const { start, end } = network.segments[index];
|
|
246
|
+
if (start === end)
|
|
247
|
+
return undefined;
|
|
248
|
+
for (const vertex of [start, end]) {
|
|
249
|
+
const list = touching.get(vertex);
|
|
250
|
+
if (list)
|
|
251
|
+
list.push(index);
|
|
252
|
+
else
|
|
253
|
+
touching.set(vertex, [index]);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
for (const list of touching.values())
|
|
257
|
+
if (list.length !== 2)
|
|
258
|
+
return undefined;
|
|
259
|
+
// Walk from the first free segment, always taking the other segment at the vertex just reached.
|
|
260
|
+
const order = [free[0]];
|
|
261
|
+
const seen = new Set(order);
|
|
262
|
+
let vertex = first.end;
|
|
263
|
+
while (order.length < free.length) {
|
|
264
|
+
const next = touching.get(vertex).find((index) => !seen.has(index));
|
|
265
|
+
if (next === undefined)
|
|
266
|
+
return undefined; // back at the start with segments left: more than one chain
|
|
267
|
+
order.push(next);
|
|
268
|
+
seen.add(next);
|
|
269
|
+
const segment = network.segments[next];
|
|
270
|
+
vertex = segment.start === vertex ? segment.end : segment.start;
|
|
271
|
+
}
|
|
272
|
+
return orderLoop(network, order, "the segments outside every region");
|
|
273
|
+
}
|
|
274
|
+
/** The node's `styleOverrideTable` entries by `styleID`. */
|
|
275
|
+
function styleTable(vectorData, where) {
|
|
276
|
+
const table = new Map();
|
|
277
|
+
(field(vectorData, "styleOverrideTable", "list", `${where} vectorData`) ?? []).forEach((entry, index) => {
|
|
278
|
+
const id = isObject(entry) ? field(entry, "styleID", "number", `${where} vectorData styleOverrideTable[${String(index)}]`) : undefined;
|
|
279
|
+
if (isObject(entry) && id !== undefined)
|
|
280
|
+
table.set(id, entry);
|
|
281
|
+
});
|
|
282
|
+
return table;
|
|
283
|
+
}
|
|
284
|
+
/** Style fields that only steer editing (how handles move together), not the drawing. */
|
|
285
|
+
const EDITING_ONLY_STYLE_FIELDS = new Set(["styleID", "handleMirroring"]);
|
|
286
|
+
/** Style field values that draw nothing different: no corner rounding. */
|
|
287
|
+
const NEUTRAL_STYLE_VALUES = { cornerRadius: 0, cornerSmoothing: 0 };
|
|
288
|
+
/**
|
|
289
|
+
* Builds a Taco network from the node's `vectorData`, scaled from
|
|
290
|
+
* `normalizedSize` to the node's `size` (grida,
|
|
291
|
+
* packages/grida-canvas-io-figma/lib.ts:5064-5120: x by size.x /
|
|
292
|
+
* normalizedSize.x, y likewise, an axis whose normalized size is 0 unscaled).
|
|
293
|
+
* Region styles become region fills; vertex styles give corner radii; other
|
|
294
|
+
* style fields are reported. Throws `NetworkRefused`.
|
|
295
|
+
*/
|
|
296
|
+
export function vectorDataNetwork(node, vectorData, reference, walk) {
|
|
297
|
+
const where = `node ${node.id}`;
|
|
298
|
+
const bytes = walk.blob(reference);
|
|
299
|
+
if (bytes === undefined)
|
|
300
|
+
throw new NetworkRefused("network-blob-missing", `vectorData refers to vector network blob ${String(reference)}, which the message does not have`);
|
|
301
|
+
const size = field(node.change, "size", "object", where);
|
|
302
|
+
const normalized = field(vectorData, "normalizedSize", "object", `${where} vectorData`);
|
|
303
|
+
const sizeX = size && field(size, "x", "number", `${where} size`);
|
|
304
|
+
const sizeY = size && field(size, "y", "number", `${where} size`);
|
|
305
|
+
const normalX = normalized && field(normalized, "x", "number", `${where} vectorData normalizedSize`);
|
|
306
|
+
const normalY = normalized && field(normalized, "y", "number", `${where} vectorData normalizedSize`);
|
|
307
|
+
if (sizeX === undefined || sizeY === undefined || normalX === undefined || normalY === undefined) {
|
|
308
|
+
throw new NetworkRefused("network-no-size", "the node needs a size and its vectorData a normalizedSize to place the network");
|
|
309
|
+
}
|
|
310
|
+
const sx = normalX === 0 ? 1 : sizeX / normalX;
|
|
311
|
+
const sy = normalY === 0 ? 1 : sizeY / normalY;
|
|
312
|
+
const raw = decodeNetworkBlob(bytes, `vector network blob ${String(reference)}`);
|
|
313
|
+
if (raw.segments.length === 0)
|
|
314
|
+
throw new NetworkRefused("network-no-segments", "the vector network has no segments");
|
|
315
|
+
// Each segment is one drawing command of the import-wide `limits.vectorCommands`, as each fillGeometry command is.
|
|
316
|
+
const shared = walk.vectorCommands;
|
|
317
|
+
if (!(shared.left >= raw.segments.length)) {
|
|
318
|
+
throw new NetworkRefused("import-command-limit", `the import has read ${String(shared.limit)} vector commands, its limit (limits.vectorCommands)`);
|
|
319
|
+
}
|
|
320
|
+
shared.left -= raw.segments.length;
|
|
321
|
+
const styles = styleTable(vectorData, where);
|
|
322
|
+
const styleOf = (id, what) => {
|
|
323
|
+
if (id === 0)
|
|
324
|
+
return undefined;
|
|
325
|
+
const style = styles.get(id);
|
|
326
|
+
if (style === undefined)
|
|
327
|
+
walk.report("approximated", `${what} has style ${String(id)}, which is not in the styleOverrideTable; the node's style is used`);
|
|
328
|
+
return style;
|
|
329
|
+
};
|
|
330
|
+
// A field with the node's own value, or one that draws nothing different, loses nothing.
|
|
331
|
+
const reportLeftOver = (style, used, what) => {
|
|
332
|
+
const left = Object.keys(style).filter((key) => !EDITING_ONLY_STYLE_FIELDS.has(key) && !used.includes(key)
|
|
333
|
+
&& style[key] !== node.change[key] && style[key] !== NEUTRAL_STYLE_VALUES[key]);
|
|
334
|
+
if (left.length > 0)
|
|
335
|
+
walk.report("property-left-out", `${what} style ${String(style["styleID"])}: ${left.join(", ")} not imported yet`);
|
|
336
|
+
};
|
|
337
|
+
const vertices = raw.vertices.map((vertex, index) => {
|
|
338
|
+
const style = styleOf(vertex.styleId, `vertex ${String(index)}`);
|
|
339
|
+
const radius = style && field(style, "cornerRadius", "number", `${where} vertex ${String(index)} style`);
|
|
340
|
+
if (style)
|
|
341
|
+
reportLeftOver(style, ["cornerRadius"], `vertex ${String(index)}`);
|
|
342
|
+
return { id: vectorVertexId(`v${String(index)}`), x: vertex.x * sx, y: vertex.y * sy, ...(radius !== undefined && radius > 0 ? { cornerRadius: radius } : {}) };
|
|
343
|
+
});
|
|
344
|
+
const segments = raw.segments.map((segment, index) => {
|
|
345
|
+
const style = styleOf(segment.styleId, `segment ${String(index)}`);
|
|
346
|
+
if (style)
|
|
347
|
+
reportLeftOver(style, [], `segment ${String(index)}`);
|
|
348
|
+
const id = vectorSegmentId(`s${String(index)}`);
|
|
349
|
+
const start = vectorVertexId(`v${String(segment.start)}`);
|
|
350
|
+
const end = vectorVertexId(`v${String(segment.end)}`);
|
|
351
|
+
if (segment.startDx === 0 && segment.startDy === 0 && segment.endDx === 0 && segment.endDy === 0)
|
|
352
|
+
return { id, kind: "line", start, end };
|
|
353
|
+
return {
|
|
354
|
+
id, kind: "cubic", start, end,
|
|
355
|
+
startHandle: { id: vectorHandleId(`h${String(index)}-start`), dx: segment.startDx * sx, dy: segment.startDy * sy },
|
|
356
|
+
endHandle: { id: vectorHandleId(`h${String(index)}-end`), dx: segment.endDx * sx, dy: segment.endDy * sy },
|
|
357
|
+
};
|
|
358
|
+
});
|
|
359
|
+
const used = new Set();
|
|
360
|
+
const boxOf = regionBoxes({ vertices, segments });
|
|
361
|
+
const regions = raw.regions.map((region, index) => {
|
|
362
|
+
const at = `region ${String(index)}`;
|
|
363
|
+
const loops = region.loops.map((loop, loopIndex) => {
|
|
364
|
+
loop.forEach((segment) => used.add(segment));
|
|
365
|
+
return orderLoop(raw, loop, `vector network ${at} loop ${String(loopIndex)}`);
|
|
366
|
+
});
|
|
367
|
+
const style = styleOf(region.styleId, at);
|
|
368
|
+
const hasFills = style !== undefined && style["fillPaints"] !== undefined;
|
|
369
|
+
const shape = { id: vectorRegionId(`r${String(index)}`), windingRule: region.windingRule, loops };
|
|
370
|
+
const fills = hasFills
|
|
371
|
+
? nodePaints({ id: `${node.id} ${at} style ${String(region.styleId)}`, change: { ...style, size: node.change["size"] } }, walk, "fillPaints", boxOf(shape))
|
|
372
|
+
: undefined;
|
|
373
|
+
if (style)
|
|
374
|
+
reportLeftOver(style, ["fillPaints"], at);
|
|
375
|
+
return { ...shape, ...(fills ? { fills } : {}) };
|
|
376
|
+
});
|
|
377
|
+
const outside = closedChainOutsideRegions(raw, used);
|
|
378
|
+
if (outside) {
|
|
379
|
+
regions.push({ id: vectorRegionId(`r${String(regions.length)}`), windingRule: "nonzero", loops: [outside] });
|
|
380
|
+
}
|
|
381
|
+
else if (used.size < raw.segments.length && regions.length === 0 && (field(node.change, "fillPaints", "list", where)?.length ?? 0) > 0) {
|
|
382
|
+
walk.report("approximated", "the network has no regions and its segments are not one closed chain, so it is stroked but not filled");
|
|
383
|
+
}
|
|
384
|
+
return { vertices, segments, regions };
|
|
385
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** A decoded Kiwi message or struct, read field by field: unknown fields are ignored. */
|
|
2
|
+
export type FigmaObject = Readonly<Record<string, unknown>>;
|
|
3
|
+
export declare function isObject(value: unknown): value is FigmaObject;
|
|
4
|
+
interface FieldKinds {
|
|
5
|
+
number: number;
|
|
6
|
+
string: string;
|
|
7
|
+
boolean: boolean;
|
|
8
|
+
object: FigmaObject;
|
|
9
|
+
list: readonly unknown[];
|
|
10
|
+
}
|
|
11
|
+
/** Reads a field: `undefined` when absent; a field of the wrong kind refuses the message. */
|
|
12
|
+
export declare function field<K extends keyof FieldKinds>(object: FigmaObject, name: string, kind: K, where: string): FieldKinds[K] | undefined;
|
|
13
|
+
/** Reads a `GUID {sessionID, localID}` as the id text `sessionID:localID`. */
|
|
14
|
+
export declare function guidText(value: unknown, where: string): string;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { FigmaImportError } from "./types.js";
|
|
2
|
+
export function isObject(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof Uint8Array);
|
|
4
|
+
}
|
|
5
|
+
const FIELD_CHECKS = {
|
|
6
|
+
number: (value) => typeof value === "number" && Number.isFinite(value),
|
|
7
|
+
string: (value) => typeof value === "string",
|
|
8
|
+
boolean: (value) => typeof value === "boolean",
|
|
9
|
+
object: isObject,
|
|
10
|
+
list: Array.isArray,
|
|
11
|
+
};
|
|
12
|
+
/** Reads a field: `undefined` when absent; a field of the wrong kind refuses the message. */
|
|
13
|
+
export function field(object, name, kind, where) {
|
|
14
|
+
const value = object[name];
|
|
15
|
+
if (value === undefined)
|
|
16
|
+
return undefined;
|
|
17
|
+
if (!FIELD_CHECKS[kind](value))
|
|
18
|
+
throw new FigmaImportError("not-node-changes", `${where}: field "${name}" is not a ${kind === "number" ? "finite number" : kind}`);
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
/** Reads a `GUID {sessionID, localID}` as the id text `sessionID:localID`. */
|
|
22
|
+
export function guidText(value, where) {
|
|
23
|
+
const session = isObject(value) ? value["sessionID"] : undefined;
|
|
24
|
+
const local = isObject(value) ? value["localID"] : undefined;
|
|
25
|
+
if (!Number.isInteger(session) || !Number.isInteger(local)) {
|
|
26
|
+
throw new FigmaImportError("not-node-changes", `${where}: guid is not a GUID with integer sessionID and localID`);
|
|
27
|
+
}
|
|
28
|
+
return `${String(session)}:${String(local)}`;
|
|
29
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { type InstanceSlots, type ComponentId, type ComponentPropertyId, type ComponentPropertyValue, type FrameNode, type LayerOverride, type NodeId, type SceneNode, type VariableId } from "../../model/index.ts";
|
|
2
|
+
import { type FigmaObject } from "./fields.ts";
|
|
3
|
+
import { type FigmaStyleList } from "./styles.ts";
|
|
4
|
+
import { type ImportedProperty, type PropertyReport } from "./componentProps.ts";
|
|
5
|
+
import type { FigmaTreeNode } from "./tree.ts";
|
|
6
|
+
import type { SlotSizeCheck } from "./slots.ts";
|
|
7
|
+
import type { ShownBy } from "./variables.ts";
|
|
8
|
+
/** One imported component: its Figma SYMBOL, its Taco root and its layers by the keys override paths use. */
|
|
9
|
+
export interface ImportedComponent {
|
|
10
|
+
readonly symbol: FigmaTreeNode;
|
|
11
|
+
readonly componentId: ComponentId;
|
|
12
|
+
readonly root: FrameNode;
|
|
13
|
+
readonly properties: readonly ImportedProperty[];
|
|
14
|
+
readonly variantDefs: ReadonlySet<string>;
|
|
15
|
+
/** Each layer by its `overrideKey`, or its guid when it has none (the first layer wins). */
|
|
16
|
+
readonly layers: ReadonlyMap<string, ImportedLayer>;
|
|
17
|
+
/** The instance swap properties a nested instance's `componentId` reads, so a value of one shows its component there. */
|
|
18
|
+
readonly boundSwaps: ReadonlySet<ComponentPropertyId>;
|
|
19
|
+
/** The name of the flattened boolean operation each operand inside it is in, by the operand's `overrideKey` (the first wins). */
|
|
20
|
+
readonly flattened: ReadonlyMap<string, string>;
|
|
21
|
+
}
|
|
22
|
+
/** A layer of a component: the Figma node it came from and the Taco node. */
|
|
23
|
+
export interface ImportedLayer {
|
|
24
|
+
readonly figma: FigmaTreeNode;
|
|
25
|
+
readonly node: SceneNode;
|
|
26
|
+
}
|
|
27
|
+
/** The component bindings the model drops from a layer the override is applied to, so the override must write each again. */
|
|
28
|
+
export declare function bindingsDroppedBy(override: Readonly<Record<string, unknown>>): Set<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Writes `null` in each override for a binding the model would keep from a nested instance's own override (`mergeOverrides`
|
|
31
|
+
* in the model) of a field the override sets plainly. Runs once every instance has its overrides, since the nested instances
|
|
32
|
+
* along a path are finished in file order.
|
|
33
|
+
*/
|
|
34
|
+
export declare function unbindInnerBindings(overrides: Record<string, LayerOverride> | undefined, nested: ReadonlyMap<string, readonly Through[]>): void;
|
|
35
|
+
/** The key an override path step names a layer by: its `overrideKey`, else its guid. */
|
|
36
|
+
export declare function overrideKeyOf(node: FigmaTreeNode): string;
|
|
37
|
+
/** A slot an instance fills, with the host frame the walk made from the content frame's children. */
|
|
38
|
+
export interface HostedSlotFill {
|
|
39
|
+
/** The override path to the nested instance whose slot this fills, as Figma keys; empty for the instance's own slots. */
|
|
40
|
+
readonly keys: readonly string[];
|
|
41
|
+
/** The id of the SLOT def the value is for. */
|
|
42
|
+
readonly defId: string;
|
|
43
|
+
readonly host: FrameNode;
|
|
44
|
+
/** The content frame (`isSlotContent`) the host was made from. */
|
|
45
|
+
readonly content: FigmaTreeNode;
|
|
46
|
+
}
|
|
47
|
+
/** What finishing an instance needs from the walk. */
|
|
48
|
+
export interface InstanceContext {
|
|
49
|
+
/** Captures an authored override before conversion to literal differences.
|
|
50
|
+
* @param path - Resolved reader layer path.
|
|
51
|
+
* @param change - Fields authored at this occurrence.
|
|
52
|
+
* @returns Nothing.
|
|
53
|
+
*/
|
|
54
|
+
fileOverride?(path: string, change: FigmaObject): void;
|
|
55
|
+
/** The slots the instance fills, each with its host. */
|
|
56
|
+
readonly slotFills?: readonly HostedSlotFill[];
|
|
57
|
+
/** The imported component made from a SYMBOL, or `undefined` when that symbol was not imported. */
|
|
58
|
+
component(symbolId: string): ImportedComponent | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Swaps the walk's nesting check (`swapsToCut`) left out of this instance, as `container>target` SYMBOL ids: a layer of the
|
|
61
|
+
* component made from `container` may not show `target`, by an override or an instance swap property value. Each with the
|
|
62
|
+
* reason, which follows "a swap" or "an instance swap value" in the report.
|
|
63
|
+
*/
|
|
64
|
+
readonly refusedSwaps?: ReadonlyMap<string, string>;
|
|
65
|
+
/** Converts a layer on its own, with `fields` over its own, without children and reports, or `null` when it cannot be converted. */
|
|
66
|
+
convertAlone(node: FigmaTreeNode, fields?: Readonly<Record<string, unknown>>, shownBy?: ShownBy): SceneNode | null;
|
|
67
|
+
/** Every converted layer by its Taco id, for laying out a resized instance by its component's constraints. */
|
|
68
|
+
readonly nodes: Readonly<Record<NodeId, SceneNode>>;
|
|
69
|
+
/** A report entry about the instance, counted once per kind and reason. */
|
|
70
|
+
report: PropertyReport;
|
|
71
|
+
/** One override field that is not imported; counted once per field name. */
|
|
72
|
+
leftOutField(name: string): void;
|
|
73
|
+
/** Whether a `styleIdForText` value names a Figma text style in the message. */
|
|
74
|
+
hasTextStyle(reference: unknown): boolean;
|
|
75
|
+
/** The lists of the Figma effect and colour styles in the message, which `styleIdForEffect`, `styleIdForFill` and `styleIdForStrokeFill` overrides name. */
|
|
76
|
+
styleList: FigmaStyleList;
|
|
77
|
+
/** Whether a text layer, a text style it or one of its ranges names, or one of its ranges binds `LINE_HEIGHT` to a variable. */
|
|
78
|
+
bindsLineHeight(figma: FigmaTreeNode): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* The variable bindings `bindLayerVariables` gives `layer` converted from `figma`, without changing `layer`, the
|
|
81
|
+
* reason it reports for the bindings it leaves out, if any, the approximation it reports, if any, and the bound copy of
|
|
82
|
+
* `layer` (a bound font style sets its text's weight). With `shownBy`, as that instance shows the layer.
|
|
83
|
+
*/
|
|
84
|
+
bindingsOf(layer: SceneNode, figma: FigmaTreeNode, shownBy?: ShownBy): {
|
|
85
|
+
readonly bindings: Readonly<Record<string, VariableId>>;
|
|
86
|
+
readonly leftOut?: string;
|
|
87
|
+
readonly approximated?: string;
|
|
88
|
+
readonly layer: SceneNode;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
type SizingAxis = "width" | "height";
|
|
92
|
+
/**
|
|
93
|
+
* A sizing override of a layer inside nested instances: the nested instances
|
|
94
|
+
* that hold that layer (`Through`), outermost first, and the axes that set it
|
|
95
|
+
* back to its component's own Hug or Fixed, which matter only where one of
|
|
96
|
+
* them overrides it.
|
|
97
|
+
*/
|
|
98
|
+
export interface HeldSizing {
|
|
99
|
+
readonly axes: readonly SizingAxis[];
|
|
100
|
+
readonly holders: readonly Through[];
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Takes off each `LayerOverride.sizing` axis in `held` that no holder's own
|
|
104
|
+
* override changes, as it then only repeats the component's sizing. Then
|
|
105
|
+
* writes each axis the override leaves out as the outermost holder that
|
|
106
|
+
* overrides it has it, since the outer `sizing` replaces a holder's whole
|
|
107
|
+
* (`mergeOverrides` in the model's instance resolver) where Figma keeps the
|
|
108
|
+
* other axis. Runs once every instance has its overrides, like
|
|
109
|
+
* `unbindInnerBindings`. A holder's axis read here may itself be one this
|
|
110
|
+
* takes off later, which keeps an axis that repeats what is there anyway.
|
|
111
|
+
*/
|
|
112
|
+
export declare function dropUnheldSizing(overrides: Record<string, LayerOverride> | undefined, held: ReadonlyMap<string, HeldSizing>): void;
|
|
113
|
+
/**
|
|
114
|
+
* Takes off each `LayerOverride.sizing` axis where the layer fills its stack,
|
|
115
|
+
* as Fill wins over a frame's own Hug or Fixed in Figma (see
|
|
116
|
+
* `stackChildFields`) and Taco refuses a frame that both fills and hugs; under
|
|
117
|
+
* a nested instance's component root, the nested instance's Fill counts. Runs
|
|
118
|
+
* once the walk has settled the stacks, which is when a layer's Fill is known;
|
|
119
|
+
* `nodes` are the converted layers by their Taco ids and `roots` the ids of
|
|
120
|
+
* the component roots.
|
|
121
|
+
*/
|
|
122
|
+
export declare function dropSizingWhereFilled(overrides: Record<string, LayerOverride> | undefined, nodes: Readonly<Record<NodeId, SceneNode>>, roots: ReadonlySet<NodeId>): void;
|
|
123
|
+
/** A nested instance an override path goes through, and the path key of the target inside it, where its own overrides name it. */
|
|
124
|
+
export interface Through {
|
|
125
|
+
readonly instance: SceneNode;
|
|
126
|
+
readonly inner: string;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The instance fields made from a Figma INSTANCE once every component is
|
|
130
|
+
* imported: its property values (`componentPropAssignments`) and its
|
|
131
|
+
* overrides. `self` is the instance converted as a frame; where its own
|
|
132
|
+
* paints, strokes and effects differ from the component root's they become
|
|
133
|
+
* an override under the root's path, and so does a Hug or Fixed axis that
|
|
134
|
+
* differs. Each `symbolOverrides` path step names a layer by its
|
|
135
|
+
* `overrideKey` (or guid); a step past the first goes through a nested
|
|
136
|
+
* instance into the component it shows, after any swap the same instance
|
|
137
|
+
* makes on a shorter path, by an override or an instance swap property value. Fields become `LayerOverride` fields where Taco
|
|
138
|
+
* has them (`overriddenSymbolID` a swap, `componentPropAssignments` property
|
|
139
|
+
* values, paints, effects, visibility, opacity, text); the rest are counted
|
|
140
|
+
* through `leftOutField`. A swap in `refusedSwaps` is left out, and so is
|
|
141
|
+
* every override and property value set inside the component it would show.
|
|
142
|
+
* `swaps` are the swaps kept, as `container>target` SYMBOL ids, for the
|
|
143
|
+
* walk's nesting check.
|
|
144
|
+
*/
|
|
145
|
+
export declare function instanceFields(instance: FigmaTreeNode, component: ImportedComponent, self: FrameNode, context: InstanceContext): {
|
|
146
|
+
readonly fields: {
|
|
147
|
+
readonly overrides?: Record<string, LayerOverride>;
|
|
148
|
+
readonly propertyValues?: Record<string, ComponentPropertyValue>;
|
|
149
|
+
readonly slots?: InstanceSlots;
|
|
150
|
+
};
|
|
151
|
+
readonly swaps: ReadonlySet<string>;
|
|
152
|
+
/** Each slot frame filled, with the content frame it shows, for the size check once stacks are settled. */
|
|
153
|
+
readonly filledSlots: readonly SlotSizeCheck[];
|
|
154
|
+
readonly nested: ReadonlyMap<string, readonly Through[]>;
|
|
155
|
+
readonly heldSizing: ReadonlyMap<string, HeldSizing>;
|
|
156
|
+
};
|
|
157
|
+
/** Whether Figma fields (a layer's, a text style's or a styled range's) bind `variableField` to a variable. */
|
|
158
|
+
export declare function bindsField(fields: FigmaObject, variableField: string): boolean;
|
|
159
|
+
/** The SYMBOL a Figma INSTANCE names (`symbolData.symbolID`), or `undefined`. */
|
|
160
|
+
export declare function shownSymbol(instance: FigmaTreeNode): string | undefined;
|
|
161
|
+
/** Every SYMBOL a node's change names: its instance's component, override swaps and instance swap values and defaults. */
|
|
162
|
+
export declare function symbolsNamedBy(change: FigmaObject): string[];
|
|
163
|
+
export {};
|