@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,468 @@
|
|
|
1
|
+
import { getVectorNetworkBounds, transformIsInvertible, vectorHandleId, vectorRegionId, vectorSegmentId, vectorVertexId, } from "../../../model/index.js";
|
|
2
|
+
import { field, isObject } from "../fields.js";
|
|
3
|
+
import { styleBackedList } from "../styles.js";
|
|
4
|
+
import { nodeEffects } from "./effects.js";
|
|
5
|
+
import { sizeLimitsOf } from "./layout.js";
|
|
6
|
+
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";
|
|
10
|
+
/**
|
|
11
|
+
* The most paths in one node's `fillGeometry` and the most drawing commands
|
|
12
|
+
* in all of them together. A node over either is left out and reported.
|
|
13
|
+
*/
|
|
14
|
+
export const VECTOR_GEOMETRY_LIMITS = { paths: 1_000, commands: 100_000 };
|
|
15
|
+
/**
|
|
16
|
+
* Why a VECTOR's geometry is not imported, named at the start of the report
|
|
17
|
+
* reason as `geometry refused (<name>)`:
|
|
18
|
+
* - `no-geometry`: no `fillGeometry` path draws anything.
|
|
19
|
+
* - `not-a-path`: a `fillGeometry` entry is not a `Path` with a `commandsBlob`.
|
|
20
|
+
* - `unknown-winding-rule`: a `windingRule` other than `NONZERO` or `ODD`.
|
|
21
|
+
* - `blob-missing`: the `commandsBlob` is not in the message's `blobs`.
|
|
22
|
+
* - `blob-too-short`: the blob ends inside a command's coordinates.
|
|
23
|
+
* - `unknown-command`: a command code other than 0 to 4.
|
|
24
|
+
* - `not-finite`: a coordinate is NaN or infinite.
|
|
25
|
+
* - `draw-before-move`: a line or curve before the first moveTo.
|
|
26
|
+
* - `too-many-paths`, `too-many-commands`: over `VECTOR_GEOMETRY_LIMITS`.
|
|
27
|
+
* - `import-command-limit`: the whole import is over `limits.vectorCommands`.
|
|
28
|
+
*/
|
|
29
|
+
export const VECTOR_GEOMETRY_PROBLEMS = [
|
|
30
|
+
"no-geometry",
|
|
31
|
+
"not-a-path",
|
|
32
|
+
"unknown-winding-rule",
|
|
33
|
+
"blob-missing",
|
|
34
|
+
"blob-too-short",
|
|
35
|
+
"unknown-command",
|
|
36
|
+
"not-finite",
|
|
37
|
+
"draw-before-move",
|
|
38
|
+
"too-many-paths",
|
|
39
|
+
"too-many-commands",
|
|
40
|
+
"import-command-limit",
|
|
41
|
+
];
|
|
42
|
+
class GeometryRefused extends Error {
|
|
43
|
+
problem;
|
|
44
|
+
constructor(problem, detail) {
|
|
45
|
+
super(`geometry refused (${problem}): ${detail}`);
|
|
46
|
+
this.problem = problem;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The commands of a `commandsBlob`, as grida reads them
|
|
51
|
+
* (packages/grida-canvas-io-figma/fig-kiwi/blob-parser.ts:12-107): a command
|
|
52
|
+
* code byte, then its coordinates as little-endian float32 numbers. 0 closes
|
|
53
|
+
* the subpath (no coordinates), 1 moves to (x, y), 2 draws a line to (x, y),
|
|
54
|
+
* 3 a quadratic curve through (cx, cy) to (x, y), 4 a cubic curve through
|
|
55
|
+
* (cx1, cy1) and (cx2, cy2) to (x, y).
|
|
56
|
+
*/
|
|
57
|
+
const COMMAND_ARGUMENTS = [0, 2, 2, 4, 6];
|
|
58
|
+
const COMMAND_NAMES = ["close", "moveTo", "lineTo", "quadratic curve", "cubic curve"];
|
|
59
|
+
function decodeCommands(bytes, budget, shared, where) {
|
|
60
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
61
|
+
const commands = [];
|
|
62
|
+
let offset = 0;
|
|
63
|
+
while (offset < bytes.length) {
|
|
64
|
+
if (budget.left === 0) {
|
|
65
|
+
throw new GeometryRefused("too-many-commands", `the fillGeometry paths have more than ${String(VECTOR_GEOMETRY_LIMITS.commands)} commands`);
|
|
66
|
+
}
|
|
67
|
+
// Not `=== 0`: a fractional, negative or NaN limit would never reach exactly 0.
|
|
68
|
+
if (!(shared.left > 0)) {
|
|
69
|
+
throw new GeometryRefused("import-command-limit", `the import has read ${String(shared.limit)} vector commands, its limit (limits.vectorCommands)`);
|
|
70
|
+
}
|
|
71
|
+
budget.left -= 1;
|
|
72
|
+
shared.left -= 1;
|
|
73
|
+
const code = bytes[offset];
|
|
74
|
+
const count = COMMAND_ARGUMENTS[code];
|
|
75
|
+
if (count === undefined)
|
|
76
|
+
throw new GeometryRefused("unknown-command", `${where} has command code ${String(code)} at byte ${String(offset)}`);
|
|
77
|
+
if (offset + 1 + count * 4 > bytes.length) {
|
|
78
|
+
throw new GeometryRefused("blob-too-short", `${where} ends at byte ${String(bytes.length)}, inside the ${COMMAND_NAMES[code]} at byte ${String(offset)}`);
|
|
79
|
+
}
|
|
80
|
+
const values = [];
|
|
81
|
+
for (let i = 0; i < count; i++) {
|
|
82
|
+
const value = view.getFloat32(offset + 1 + i * 4, true);
|
|
83
|
+
if (!Number.isFinite(value))
|
|
84
|
+
throw new GeometryRefused("not-finite", `${where} has a coordinate that is ${String(value)} in the ${COMMAND_NAMES[code]} at byte ${String(offset)}`);
|
|
85
|
+
values.push(value);
|
|
86
|
+
}
|
|
87
|
+
commands.push({ code, values });
|
|
88
|
+
offset += 1 + count * 4;
|
|
89
|
+
}
|
|
90
|
+
return commands;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Adds one path's commands to the network as one region. Ids number on
|
|
94
|
+
* across paths. As in `vectorNetworkFromSvgPathData`, a close whose current
|
|
95
|
+
* point is the subpath's start ends the last segment at the start vertex, a
|
|
96
|
+
* close elsewhere adds a line back to it, and an open subpath is closed for
|
|
97
|
+
* filling only (`implicitClose`). A quadratic curve becomes the cubic that
|
|
98
|
+
* draws it: each handle is 2/3 of the way to the control point.
|
|
99
|
+
*/
|
|
100
|
+
function addRegion(network, commands, windingRule, where) {
|
|
101
|
+
const { vertices, segments } = network;
|
|
102
|
+
const loops = [];
|
|
103
|
+
let moveTo;
|
|
104
|
+
let start;
|
|
105
|
+
let current;
|
|
106
|
+
let loop = [];
|
|
107
|
+
const addVertex = (x, y) => {
|
|
108
|
+
const vertex = { id: vectorVertexId(`v${String(vertices.length)}`), x, y };
|
|
109
|
+
vertices.push(vertex);
|
|
110
|
+
return vertex;
|
|
111
|
+
};
|
|
112
|
+
const finish = (closed) => {
|
|
113
|
+
if (loop.length > 0)
|
|
114
|
+
loops.push({ segments: loop, ...(closed ? {} : { implicitClose: true }) });
|
|
115
|
+
loop = [];
|
|
116
|
+
start = current = undefined;
|
|
117
|
+
};
|
|
118
|
+
for (const { code, values } of commands) {
|
|
119
|
+
if (code === 1) {
|
|
120
|
+
finish(false);
|
|
121
|
+
moveTo = { x: values[0], y: values[1] };
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (code === 0) {
|
|
125
|
+
if (start !== undefined && current !== undefined && loop.length > 0) {
|
|
126
|
+
if (current !== start && current.x === start.x && current.y === start.y) {
|
|
127
|
+
const last = segments[segments.length - 1];
|
|
128
|
+
segments[segments.length - 1] = { ...last, end: start.id };
|
|
129
|
+
vertices.pop();
|
|
130
|
+
}
|
|
131
|
+
else if (current !== start) {
|
|
132
|
+
const id = vectorSegmentId(`s${String(segments.length)}`);
|
|
133
|
+
segments.push({ id, kind: "line", start: current.id, end: start.id });
|
|
134
|
+
loop.push({ segmentId: id, direction: "forward" });
|
|
135
|
+
}
|
|
136
|
+
moveTo = { x: start.x, y: start.y };
|
|
137
|
+
}
|
|
138
|
+
finish(true);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (current === undefined) {
|
|
142
|
+
if (moveTo === undefined)
|
|
143
|
+
throw new GeometryRefused("draw-before-move", `${where} draws a ${COMMAND_NAMES[code]} before its first moveTo`);
|
|
144
|
+
start = current = addVertex(moveTo.x, moveTo.y);
|
|
145
|
+
}
|
|
146
|
+
const from = current;
|
|
147
|
+
const end = addVertex(values[values.length - 2], values[values.length - 1]);
|
|
148
|
+
const id = vectorSegmentId(`s${String(segments.length)}`);
|
|
149
|
+
if (code === 2) {
|
|
150
|
+
segments.push({ id, kind: "line", start: from.id, end: end.id });
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
// Control points: the cubic's own, or a quadratic's one control point raised to a cubic.
|
|
154
|
+
const [c1x, c1y, c2x, c2y] = code === 4
|
|
155
|
+
? values
|
|
156
|
+
: [from.x + 2 / 3 * (values[0] - from.x), from.y + 2 / 3 * (values[1] - from.y),
|
|
157
|
+
end.x + 2 / 3 * (values[0] - end.x), end.y + 2 / 3 * (values[1] - end.y)];
|
|
158
|
+
segments.push({
|
|
159
|
+
id, kind: "cubic", start: from.id, end: end.id,
|
|
160
|
+
startHandle: { id: vectorHandleId(`h${String(segments.length)}-start`), dx: c1x - from.x, dy: c1y - from.y },
|
|
161
|
+
endHandle: { id: vectorHandleId(`h${String(segments.length)}-end`), dx: c2x - end.x, dy: c2y - end.y },
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
loop.push({ segmentId: id, direction: "forward" });
|
|
165
|
+
current = end;
|
|
166
|
+
moveTo = { x: end.x, y: end.y };
|
|
167
|
+
}
|
|
168
|
+
finish(false);
|
|
169
|
+
if (loops.length > 0)
|
|
170
|
+
network.regions.push({ id: vectorRegionId(`r${String(network.regions.length)}`), windingRule, loops });
|
|
171
|
+
}
|
|
172
|
+
/** The node's `fillGeometry` as one network, a region per path; throws `GeometryRefused`. */
|
|
173
|
+
function fillGeometryNetwork(node, walk) {
|
|
174
|
+
const where = `node ${node.id}`;
|
|
175
|
+
const paths = field(node.change, "fillGeometry", "list", where) ?? [];
|
|
176
|
+
if (paths.length > VECTOR_GEOMETRY_LIMITS.paths) {
|
|
177
|
+
throw new GeometryRefused("too-many-paths", `fillGeometry has ${String(paths.length)} paths, over the limit of ${String(VECTOR_GEOMETRY_LIMITS.paths)}`);
|
|
178
|
+
}
|
|
179
|
+
const network = { vertices: [], segments: [], regions: [] };
|
|
180
|
+
const budget = { left: VECTOR_GEOMETRY_LIMITS.commands };
|
|
181
|
+
paths.forEach((path, index) => {
|
|
182
|
+
const at = `fillGeometry[${String(index)}]`;
|
|
183
|
+
const reference = isObject(path) ? field(path, "commandsBlob", "number", `${where} ${at}`) : undefined;
|
|
184
|
+
if (!isObject(path) || reference === undefined)
|
|
185
|
+
throw new GeometryRefused("not-a-path", `${at} is not a Path with a commandsBlob`);
|
|
186
|
+
const rule = field(path, "windingRule", "string", `${where} ${at}`) ?? "NONZERO";
|
|
187
|
+
if (rule !== "NONZERO" && rule !== "ODD")
|
|
188
|
+
throw new GeometryRefused("unknown-winding-rule", `${at} has winding rule ${rule}`);
|
|
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
|
+
const bytes = walk.blob(reference);
|
|
193
|
+
if (bytes === undefined)
|
|
194
|
+
throw new GeometryRefused("blob-missing", `${at} refers to commands blob ${String(reference)}, which the message does not have`);
|
|
195
|
+
const blobWhere = `${at} commands blob ${String(reference)}`;
|
|
196
|
+
addRegion(network, decodeCommands(bytes, budget, walk.vectorCommands, blobWhere), rule === "ODD" ? "evenodd" : "nonzero", blobWhere);
|
|
197
|
+
});
|
|
198
|
+
if (network.segments.length === 0) {
|
|
199
|
+
throw new GeometryRefused("no-geometry", paths.length === 0
|
|
200
|
+
? "the node has no fillGeometry and no vectorData.vectorNetworkBlob"
|
|
201
|
+
: "no fillGeometry path draws a line or curve");
|
|
202
|
+
}
|
|
203
|
+
return network;
|
|
204
|
+
}
|
|
205
|
+
const sameBox = (a, b) => a === b || (a !== undefined && b !== undefined
|
|
206
|
+
&& Math.abs(a.x - b.x) < 1e-9 && Math.abs(a.y - b.y) < 1e-9 && Math.abs(a.width - b.width) < 1e-9 && Math.abs(a.height - b.height) < 1e-9);
|
|
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);
|
|
249
|
+
if (boxes.length === 0)
|
|
250
|
+
return getVectorNetworkBounds(network) ?? undefined;
|
|
251
|
+
const left = Math.min(...boxes.map((box) => box.x)), top = Math.min(...boxes.map((box) => box.y));
|
|
252
|
+
const right = Math.max(...boxes.map((box) => box.x + box.width)), bottom = Math.max(...boxes.map((box) => box.y + box.height));
|
|
253
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* A path node's Figma `transform` as its Taco transform (Taco's [a, b, c, d,
|
|
257
|
+
* e, f] is Figma's [m00, m10, m01, m11, m02, m12]), or `null` after reporting
|
|
258
|
+
* the node left out when the transform is incomplete or squashes it flat.
|
|
259
|
+
*/
|
|
260
|
+
export function pathTransform(node, walk) {
|
|
261
|
+
const where = `node ${node.id}`;
|
|
262
|
+
const m = field(node.change, "transform", "object", where);
|
|
263
|
+
const values = m && ["m00", "m10", "m01", "m11", "m02", "m12"].map((key) => field(m, key, "number", `${where} transform`));
|
|
264
|
+
if (!values || values.some((value) => value === undefined)) {
|
|
265
|
+
walk.report("node-left-out", "the node has no complete transform");
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
const transform = values;
|
|
269
|
+
if (!transformIsInvertible(transform)) {
|
|
270
|
+
walk.report("node-left-out", "the transform squashes the node flat");
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
return transform;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* A path node's strokes, gradients placed on `box`, and the cap of its ends
|
|
277
|
+
* and dashes from Figma's `strokeCap`; a cap Taco has no match for is
|
|
278
|
+
* reported and imported flat.
|
|
279
|
+
*/
|
|
280
|
+
export function pathStrokes(node, walk, box) {
|
|
281
|
+
const capName = field(node.change, "strokeCap", "string", `node ${node.id}`) ?? "NONE";
|
|
282
|
+
const cap = Object.hasOwn(STROKE_CAPS, capName) ? STROKE_CAPS[capName] : "none";
|
|
283
|
+
const strokes = nodeStrokes(node, walk, box).map((stroke) => ({ ...stroke, dashCap: cap }));
|
|
284
|
+
if (strokes.length > 0 && !Object.hasOwn(STROKE_CAPS, capName))
|
|
285
|
+
walk.report("approximated", `stroke cap ${capName} is not imported yet; the ends are flat`);
|
|
286
|
+
return { strokes, cap };
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* A Figma VECTOR as a Taco path. The shape is the node's editable vector
|
|
290
|
+
* network (`vectorData.vectorNetworkBlob`, what a paste carries) when it has
|
|
291
|
+
* one, else its `fillGeometry`, the outline Figma fills (also when the
|
|
292
|
+
* network is refused, which is reported); either is in the
|
|
293
|
+
* node's own space. Figma's `transform` places it exactly as the path's
|
|
294
|
+
* transform (Taco's [a, b, c, d, e, f] is Figma's [m00, m10, m01, m11, m02,
|
|
295
|
+
* m12]), so scale and skew are kept. Corner radii are already in a
|
|
296
|
+
* `fillGeometry` outline; with a network the node's `cornerRadius` is the
|
|
297
|
+
* path's corner radius. `strokeGeometry` (the stroke outlined) is not
|
|
298
|
+
* read: Taco draws the strokes from the path.
|
|
299
|
+
*/
|
|
300
|
+
export const convertVector = (node, walk) => {
|
|
301
|
+
const where = `node ${node.id}`;
|
|
302
|
+
const transform = pathTransform(node, walk);
|
|
303
|
+
if (transform === null)
|
|
304
|
+
return null;
|
|
305
|
+
let vectorNetwork;
|
|
306
|
+
let fromNetwork = false;
|
|
307
|
+
let networkProblem;
|
|
308
|
+
const vectorData = field(node.change, "vectorData", "object", where);
|
|
309
|
+
const networkBlob = vectorData && field(vectorData, "vectorNetworkBlob", "number", `${where} vectorData`);
|
|
310
|
+
const hasFillGeometry = (field(node.change, "fillGeometry", "list", where)?.length ?? 0) > 0;
|
|
311
|
+
if (vectorData !== undefined && networkBlob !== undefined) {
|
|
312
|
+
// Reports about the network are kept only if the network is used.
|
|
313
|
+
const networkReports = [];
|
|
314
|
+
const networkWalk = {
|
|
315
|
+
...walk,
|
|
316
|
+
report: (kind, reason) => { networkReports.push([kind, reason]); },
|
|
317
|
+
};
|
|
318
|
+
try {
|
|
319
|
+
vectorNetwork = vectorDataNetwork(node, vectorData, networkBlob, networkWalk);
|
|
320
|
+
fromNetwork = true;
|
|
321
|
+
for (const [kind, reason] of networkReports)
|
|
322
|
+
walk.report(kind, reason);
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
if (!(error instanceof NetworkRefused))
|
|
326
|
+
throw error;
|
|
327
|
+
if (!hasFillGeometry) {
|
|
328
|
+
walk.report("node-left-out", error.message);
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
networkProblem = error.message;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (vectorNetwork === undefined) {
|
|
335
|
+
try {
|
|
336
|
+
vectorNetwork = fillGeometryNetwork(node, walk);
|
|
337
|
+
}
|
|
338
|
+
catch (error) {
|
|
339
|
+
if (!(error instanceof GeometryRefused))
|
|
340
|
+
throw error;
|
|
341
|
+
walk.report("node-left-out", networkProblem === undefined ? error.message : `${networkProblem}; ${error.message}`);
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
if (networkProblem !== undefined)
|
|
345
|
+
walk.report("approximated", `${networkProblem}; the fillGeometry outline is used instead`);
|
|
346
|
+
}
|
|
347
|
+
const base = layerBase(node, walk, "Vector", true);
|
|
348
|
+
const boxOf = regionBoxes(vectorNetwork);
|
|
349
|
+
const { fills, network } = placedFills(node, walk, vectorNetwork, boxOf);
|
|
350
|
+
const { strokes, cap } = pathStrokes(node, walk, strokeBox(network, boxOf));
|
|
351
|
+
const radius = fromNetwork ? field(node.change, "cornerRadius", "number", where) ?? 0 : 0;
|
|
352
|
+
const style = {
|
|
353
|
+
...(fills.length > 0 ? { fills } : {}), ...(strokes.length > 0 ? { strokes } : {}), ...(radius > 0 ? { cornerRadius: radius } : {}),
|
|
354
|
+
...nodeEffects(node, walk),
|
|
355
|
+
};
|
|
356
|
+
const path = { ...base, type: "shape", shape: "path", vectorNetwork: network, transform, style, startCap: cap, endCap: cap };
|
|
357
|
+
return lineWhereItFills(path, node.change);
|
|
358
|
+
};
|
|
359
|
+
/**
|
|
360
|
+
* A Figma BOOLEAN_OPERATION as a Taco path drawn from its `fillGeometry`, the
|
|
361
|
+
* outline Figma stores for the result; Taco has no live boolean, so the
|
|
362
|
+
* operands are not imported (the walk reports the flattening once). Without
|
|
363
|
+
* a `fillGeometry` outline the node is left out, as before.
|
|
364
|
+
*/
|
|
365
|
+
export const convertBooleanOperation = (node, walk) => {
|
|
366
|
+
if ((field(node.change, "fillGeometry", "list", `node ${node.id}`)?.length ?? 0) === 0) {
|
|
367
|
+
walk.report("node-left-out", "the boolean operation has no fillGeometry outline, and Taco has no live boolean");
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
const { vectorData: _vectorData, ...change } = node.change;
|
|
371
|
+
return convertVector({ ...node, change: { ...change, ...operandConstraints(node) } }, walk);
|
|
372
|
+
};
|
|
373
|
+
/**
|
|
374
|
+
* The constraints a flattened boolean operation resizes by. Figma resizes a
|
|
375
|
+
* boolean operation like a group: each operand follows its own constraints
|
|
376
|
+
* and the result is drawn around them, so the operation's own constraints
|
|
377
|
+
* (often left unset, which reads as MIN) do not place it. The flattened path
|
|
378
|
+
* takes, on each axis, the constraint every operand shares (an operand that
|
|
379
|
+
* is itself a boolean operation or group, including a Figma group stored as
|
|
380
|
+
* a FRAME, shares its own operands'; hidden operands do not count); where
|
|
381
|
+
* they differ, or there are no shown operands, the node's own constraint stays.
|
|
382
|
+
* A check icon made as a union of two SCALE strokes, in a component shown
|
|
383
|
+
* smaller, otherwise kept its full size and was clipped.
|
|
384
|
+
*/
|
|
385
|
+
function operandConstraints(node) {
|
|
386
|
+
// Hidden operands take no part in the result, so their constraints do not count.
|
|
387
|
+
const shown = (layer) => layer.children.filter((child) => child.change["visible"] !== false);
|
|
388
|
+
const shared = (name, layer) => {
|
|
389
|
+
const own = layer.change[name] ?? "MIN";
|
|
390
|
+
const looksThrough = layer.type === "BOOLEAN_OPERATION" || layer.type === "GROUP" || (layer.type === "FRAME" && isFigmaGroup(layer.change));
|
|
391
|
+
if (!looksThrough)
|
|
392
|
+
return own;
|
|
393
|
+
const values = new Set(shown(layer).map((child) => shared(name, child)));
|
|
394
|
+
return values.size === 1 ? [...values][0] : own;
|
|
395
|
+
};
|
|
396
|
+
const constraints = {};
|
|
397
|
+
for (const name of ["horizontalConstraint", "verticalConstraint"]) {
|
|
398
|
+
const values = new Set(shown(node).map((child) => shared(name, child)));
|
|
399
|
+
if (values.size === 1)
|
|
400
|
+
constraints[name] = [...values][0];
|
|
401
|
+
}
|
|
402
|
+
return constraints;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* `path` as a Taco line when the Figma node asks to fill its auto layout
|
|
406
|
+
* parent (`stackChildPrimaryGrow` above 0 or `stackChildAlignSelf` STRETCH)
|
|
407
|
+
* and the path is one straight level segment that is only moved (not turned,
|
|
408
|
+
* flipped, scaled or skewed) and fills no region; else `path` as it is. A
|
|
409
|
+
* path cannot fill its parent in Taco, while a line fills along its length:
|
|
410
|
+
* the renderer draws it from (0, 0) to (width, 0) of its box, so it follows
|
|
411
|
+
* the filled width with its stroke width kept. HeroUI's dividers are such
|
|
412
|
+
* VECTORs and LINEs. A line's stroke is drawn centred, as Figma draws the
|
|
413
|
+
* stroke of an open path.
|
|
414
|
+
*/
|
|
415
|
+
export function lineWhereItFills(path, change) {
|
|
416
|
+
if (!asksToFill(change))
|
|
417
|
+
return path;
|
|
418
|
+
const tolerance = 1e-4;
|
|
419
|
+
const [a, b, c, d, e, f] = path.transform;
|
|
420
|
+
if (Math.abs(a - 1) > tolerance || Math.abs(b) > tolerance || Math.abs(c) > tolerance || Math.abs(d - 1) > tolerance)
|
|
421
|
+
return path;
|
|
422
|
+
const { vertices, segments, regions } = path.vectorNetwork;
|
|
423
|
+
const segment = segments[0];
|
|
424
|
+
if (vertices.length !== 2 || segments.length !== 1 || segment?.kind !== "line" || regions.length > 0)
|
|
425
|
+
return path;
|
|
426
|
+
const start = vertices.find((vertex) => vertex.id === segment.start);
|
|
427
|
+
const end = vertices.find((vertex) => vertex.id === segment.end);
|
|
428
|
+
if (start === undefined || end === undefined || start === end || Math.abs(start.y - end.y) > tolerance)
|
|
429
|
+
return path;
|
|
430
|
+
const { vectorNetwork: _network, transform: _transform, shape: _shape, style, startCap = "round", endCap = "round", ...base } = path;
|
|
431
|
+
const { fills: _fills, cornerRadius: _radius, strokes, ...rest } = style;
|
|
432
|
+
const centred = strokes?.map((stroke) => ({ ...stroke, position: "center" }));
|
|
433
|
+
// A segment drawn right to left keeps its caps on the ends they were on.
|
|
434
|
+
const reversed = end.x < start.x;
|
|
435
|
+
const limits = sizeLimitsOf(change);
|
|
436
|
+
const line = {
|
|
437
|
+
...base,
|
|
438
|
+
type: "shape",
|
|
439
|
+
shape: "line",
|
|
440
|
+
frame: { x: e + Math.min(start.x, end.x), y: f + start.y, width: Math.abs(end.x - start.x), height: 0 },
|
|
441
|
+
rotation: 0,
|
|
442
|
+
...(Object.keys(limits).length > 0 ? { resizing: limits } : {}),
|
|
443
|
+
style: { ...rest, ...(centred === undefined ? {} : { strokes: centred }) },
|
|
444
|
+
startCap: reversed ? endCap : startCap,
|
|
445
|
+
endCap: reversed ? startCap : endCap,
|
|
446
|
+
};
|
|
447
|
+
return line;
|
|
448
|
+
}
|
|
449
|
+
/** Whether the Figma node grows along its stack's main axis or stretches across it. */
|
|
450
|
+
function asksToFill(change) {
|
|
451
|
+
const grow = change["stackChildPrimaryGrow"];
|
|
452
|
+
return (typeof grow === "number" && Number.isFinite(grow) && grow > 0) || change["stackChildAlignSelf"] === "STRETCH";
|
|
453
|
+
}
|
|
454
|
+
/** Moondraft patch (../carry.ts): a STAR or REGULAR_POLYGON as its `fillGeometry` path (as a VECTOR), its parameters carried. */
|
|
455
|
+
export const convertStarOrPolygon = (node, walk) => {
|
|
456
|
+
const converted = convertVector(node, walk);
|
|
457
|
+
if (converted === null)
|
|
458
|
+
return null;
|
|
459
|
+
const where = `node ${node.id}`;
|
|
460
|
+
const count = field(node.change, "count", "number", where);
|
|
461
|
+
const size = field(node.change, "size", "object", where);
|
|
462
|
+
const width = size && field(size, "x", "number", `${where} size`), height = size && field(size, "y", "number", `${where} size`);
|
|
463
|
+
if (count === undefined || !(width !== undefined && width > 0) || !(height !== undefined && height > 0))
|
|
464
|
+
return converted;
|
|
465
|
+
const ratio = field(node.change, "starInnerScale", "number", where) ?? 0.382;
|
|
466
|
+
carry(converted, node.type === "STAR" ? { shape: { type: "star", count, ratio, width, height } } : { shape: { type: "polygon", count, width, height } });
|
|
467
|
+
return converted;
|
|
468
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { type VectorNetwork, type VectorRegion } from "../../../model/index.ts";
|
|
2
|
+
import { type FigmaObject } from "../fields.ts";
|
|
3
|
+
import type { FigmaTreeNode } from "../tree.ts";
|
|
4
|
+
import type { NodeWalk } from "../walk.ts";
|
|
5
|
+
import type { Box } from "./gradient.ts";
|
|
6
|
+
/**
|
|
7
|
+
* A function giving the tight bounds of a region's outline in node pixels,
|
|
8
|
+
* which the renderer places the region's gradient fills on, or `undefined`
|
|
9
|
+
* when it has none. The network's vertices and segments are indexed once, so
|
|
10
|
+
* each region costs only its own loop entries, not the whole network.
|
|
11
|
+
*/
|
|
12
|
+
export declare function regionBoxes(network: Pick<VectorNetwork, "vertices" | "segments">): (region: VectorRegion) => Box | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* The most vertices, segments, regions, loops (over all regions) and loop
|
|
15
|
+
* entries (over all loops) one `vectorNetworkBlob` may hold. A blob over any
|
|
16
|
+
* of them is refused before anything is allocated for it.
|
|
17
|
+
*/
|
|
18
|
+
export declare const VECTOR_NETWORK_LIMITS: {
|
|
19
|
+
readonly vertices: 100000;
|
|
20
|
+
readonly segments: 100000;
|
|
21
|
+
readonly regions: 10000;
|
|
22
|
+
readonly loops: 10000;
|
|
23
|
+
readonly loopEntries: 200000;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Why a VECTOR's editable vector network is not imported, named at the start
|
|
27
|
+
* of the report reason as `geometry refused (<name>)`:
|
|
28
|
+
* - `network-blob-missing`: the `vectorNetworkBlob` is not in the message's `blobs`.
|
|
29
|
+
* - `network-no-size`: the node has no `size` or its `vectorData` no `normalizedSize`, so the network's scale is unknown.
|
|
30
|
+
* - `network-too-short`: the blob ends inside its header, a vertex, a segment, a region or a loop.
|
|
31
|
+
* - `network-trailing-bytes`: bytes are left after the last region.
|
|
32
|
+
* - `network-too-many-vertices`, `-segments`, `-regions`, `-loops`, `-loop-entries`: over `VECTOR_NETWORK_LIMITS`.
|
|
33
|
+
* - `network-not-finite`: a coordinate or tangent is NaN or infinite.
|
|
34
|
+
* - `network-vertex-out-of-range`: a segment names a vertex the blob does not have.
|
|
35
|
+
* - `network-segment-out-of-range`: a loop names a segment the blob does not have.
|
|
36
|
+
* - `network-empty-region`, `network-empty-loop`: a region with no loops, a loop with no segments.
|
|
37
|
+
* - `network-loop-repeats-segment`: a loop names one segment twice.
|
|
38
|
+
* - `network-loop-broken`: consecutive segments of a loop share no vertex.
|
|
39
|
+
* - `network-no-segments`: the network draws nothing.
|
|
40
|
+
* - `import-command-limit`: the network's segments, each counted as one drawing
|
|
41
|
+
* command, are more than the import has left of `limits.vectorCommands`.
|
|
42
|
+
*
|
|
43
|
+
* The winding rule is one bit (see `decodeNetworkBlob`), so no value of it is unknown.
|
|
44
|
+
*/
|
|
45
|
+
export declare const VECTOR_NETWORK_PROBLEMS: readonly ["network-blob-missing", "network-no-size", "network-too-short", "network-trailing-bytes", "network-too-many-vertices", "network-too-many-segments", "network-too-many-regions", "network-too-many-loops", "network-too-many-loop-entries", "network-not-finite", "network-vertex-out-of-range", "network-segment-out-of-range", "network-empty-region", "network-empty-loop", "network-loop-repeats-segment", "network-loop-broken", "network-no-segments", "import-command-limit"];
|
|
46
|
+
export type VectorNetworkProblem = (typeof VECTOR_NETWORK_PROBLEMS)[number];
|
|
47
|
+
export declare class NetworkRefused extends Error {
|
|
48
|
+
readonly problem: VectorNetworkProblem;
|
|
49
|
+
constructor(problem: VectorNetworkProblem, detail: string);
|
|
50
|
+
}
|
|
51
|
+
interface RawVertex {
|
|
52
|
+
readonly styleId: number;
|
|
53
|
+
readonly x: number;
|
|
54
|
+
readonly y: number;
|
|
55
|
+
}
|
|
56
|
+
interface RawSegment {
|
|
57
|
+
readonly styleId: number;
|
|
58
|
+
readonly start: number;
|
|
59
|
+
readonly startDx: number;
|
|
60
|
+
readonly startDy: number;
|
|
61
|
+
readonly end: number;
|
|
62
|
+
readonly endDx: number;
|
|
63
|
+
readonly endDy: number;
|
|
64
|
+
}
|
|
65
|
+
interface RawRegion {
|
|
66
|
+
readonly styleId: number;
|
|
67
|
+
readonly windingRule: VectorRegion["windingRule"];
|
|
68
|
+
readonly loops: readonly (readonly number[])[];
|
|
69
|
+
}
|
|
70
|
+
interface RawNetwork {
|
|
71
|
+
readonly vertices: RawVertex[];
|
|
72
|
+
readonly segments: RawSegment[];
|
|
73
|
+
readonly regions: RawRegion[];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Decodes a `vectorNetworkBlob`, as fig2sketch (src/figformat/vector_network.py:4-69)
|
|
77
|
+
* and grida (packages/grida-canvas-io-figma/fig-kiwi/blob-parser.ts:150-243)
|
|
78
|
+
* read it; every number is little-endian, u32 for counts, indices and style
|
|
79
|
+
* ids, f32 for coordinates:
|
|
80
|
+
* - header: vertex count, segment count, region count;
|
|
81
|
+
* - each vertex (12 bytes): style id, x, y;
|
|
82
|
+
* - each segment (28 bytes): style id, start vertex, start tangent dx, dy,
|
|
83
|
+
* end vertex, end tangent dx, dy (each tangent relative to its own vertex);
|
|
84
|
+
* - each region: one u32 holding the style id above bit 0 and the winding
|
|
85
|
+
* rule in bit 0 (1 NONZERO, 0 ODD), the loop count, then per loop a
|
|
86
|
+
* segment count and that many segment indices.
|
|
87
|
+
*/
|
|
88
|
+
export declare function decodeNetworkBlob(bytes: Uint8Array, where: string): RawNetwork;
|
|
89
|
+
/**
|
|
90
|
+
* Builds a Taco network from the node's `vectorData`, scaled from
|
|
91
|
+
* `normalizedSize` to the node's `size` (grida,
|
|
92
|
+
* packages/grida-canvas-io-figma/lib.ts:5064-5120: x by size.x /
|
|
93
|
+
* normalizedSize.x, y likewise, an axis whose normalized size is 0 unscaled).
|
|
94
|
+
* Region styles become region fills; vertex styles give corner radii; other
|
|
95
|
+
* style fields are reported. Throws `NetworkRefused`.
|
|
96
|
+
*/
|
|
97
|
+
export declare function vectorDataNetwork(node: FigmaTreeNode, vectorData: FigmaObject, reference: number, walk: NodeWalk): VectorNetwork;
|
|
98
|
+
export {};
|