@moondraft/import 0.0.0-stage → 0.2.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 +36 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +394 -0
- package/dist/adapter/convert.d.ts +144 -0
- package/dist/adapter/convert.js +2811 -0
- package/dist/adapter/figma-descriptions.d.ts +7 -0
- package/dist/adapter/figma-descriptions.js +34 -0
- package/dist/adapter/figma-grids.d.ts +12 -0
- package/dist/adapter/figma-grids.js +175 -0
- package/dist/adapter/figma-handoff.d.ts +12 -0
- package/dist/adapter/figma-handoff.js +167 -0
- package/dist/adapter/figma-hyperlinks.d.ts +13 -0
- package/dist/adapter/figma-hyperlinks.js +77 -0
- package/dist/adapter/figma.d.ts +12 -0
- package/dist/adapter/figma.js +40 -0
- package/dist/adapter/file-components.d.ts +20 -0
- package/dist/adapter/file-components.js +352 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +851 -0
- package/dist/adapter/image-crop.d.ts +18 -0
- package/dist/adapter/image-crop.js +30 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +49 -0
- package/dist/adapter/link.js +892 -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 +45 -0
- package/dist/adapter/variables.js +106 -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 +230 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +50 -0
- package/dist/report.d.ts +42 -0
- package/dist/report.js +142 -0
- package/dist/serialize.d.ts +7 -0
- package/dist/serialize.js +79 -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 +41 -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 +24 -0
- package/dist/taco/figma-import/import/carry.js +66 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
- package/dist/taco/figma-import/import/componentProps.js +329 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +85 -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/exportSettings.d.ts +4 -0
- package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +63 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
- package/dist/taco/figma-import/import/converters/gradient.js +108 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
- package/dist/taco/figma-import/import/converters/group.js +30 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +94 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
- package/dist/taco/figma-import/import/converters/layout.js +187 -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 +8 -0
- package/dist/taco/figma-import/import/converters/section.js +29 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
- package/dist/taco/figma-import/import/converters/shared.js +372 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
- package/dist/taco/figma-import/import/converters/text.js +707 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
- package/dist/taco/figma-import/import/converters/vector.js +609 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -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 +184 -0
- package/dist/taco/figma-import/import/instances.js +1530 -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 +48 -0
- package/dist/taco/figma-import/import/styles.js +48 -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 +137 -0
- package/dist/taco/figma-import/import/variables.js +769 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +520 -0
- package/dist/taco/figma-import/import/walk.d.ts +207 -0
- package/dist/taco/figma-import/import/walk.js +1464 -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 +12 -0
- package/dist/taco/figma-import/kiwi/message.js +147 -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 +5665 -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 +96 -0
- package/dist/taco/model/component-evaluation.js +555 -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 +376 -0
- package/dist/taco/model/instance-resolver.js +1778 -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 +79 -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 +2369 -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 +55 -4
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { ModelError } from "./codec.js";
|
|
2
|
+
import { getNodeBounds } from "./geometry.js";
|
|
3
|
+
import { transformIsInvertible } from "./node-transform.js";
|
|
4
|
+
import { openPathChain } from "./stroke-settings.js";
|
|
5
|
+
import { vectorNetworkFromSvgPathData } from "./vector-network.js";
|
|
6
|
+
/** The values of SVG's `fill-rule` property. */
|
|
7
|
+
export const SVG_FILL_RULES = ["nonzero", "evenodd"];
|
|
8
|
+
/** The values of SVG's `stroke-linecap` property. */
|
|
9
|
+
export const SVG_STROKE_CAPS = ["butt", "round", "square"];
|
|
10
|
+
/** The values of SVG's `stroke-linejoin` property that the model can draw. */
|
|
11
|
+
export const SVG_STROKE_JOINS = ["miter", "round", "bevel"];
|
|
12
|
+
const OPTION_KEYS = new Set([
|
|
13
|
+
"id", "name", "pathData", "style", "transform", "fillRule", "strokeCap",
|
|
14
|
+
"strokeJoin", "visible", "locked", "opacity",
|
|
15
|
+
]);
|
|
16
|
+
const CAP_FROM_SVG = {
|
|
17
|
+
butt: "none",
|
|
18
|
+
round: "round",
|
|
19
|
+
square: "square",
|
|
20
|
+
};
|
|
21
|
+
const ROOT = "$svgPath";
|
|
22
|
+
function refuse(message, path, cause) {
|
|
23
|
+
throw new ModelError("INVALID_SCHEMA", message, path, cause === undefined ? undefined : { cause });
|
|
24
|
+
}
|
|
25
|
+
/** A refused value for a message: strings are quoted, and nothing here can throw, unlike `JSON.stringify`. */
|
|
26
|
+
function shown(value) {
|
|
27
|
+
return typeof value === "string" ? JSON.stringify(value) : `a value of type ${typeof value}`;
|
|
28
|
+
}
|
|
29
|
+
function oneOf(value, allowed, fallback, path) {
|
|
30
|
+
if (value === undefined)
|
|
31
|
+
return fallback;
|
|
32
|
+
if (!allowed.includes(value)) {
|
|
33
|
+
refuse(`Expected one of ${allowed.join(", ")} but got ${shown(value)}`, path);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
function importedTransform(value, path) {
|
|
38
|
+
if (value === undefined)
|
|
39
|
+
return [1, 0, 0, 1, 0, 0];
|
|
40
|
+
if (!Array.isArray(value) || value.length !== 6) {
|
|
41
|
+
refuse("A transform matrix must contain six numbers", path);
|
|
42
|
+
}
|
|
43
|
+
value.forEach((entry, index) => {
|
|
44
|
+
if (typeof entry !== "number" || !Number.isFinite(entry)) {
|
|
45
|
+
refuse("Expected a finite number", `${path}[${String(index)}]`);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
const [a, b, c, d, e, f] = value;
|
|
49
|
+
const transform = [a, b, c, d, e, f];
|
|
50
|
+
if (!transformIsInvertible(transform)) {
|
|
51
|
+
refuse("A transform matrix must be invertible, so its determinant cannot be zero or too small to divide by", path);
|
|
52
|
+
}
|
|
53
|
+
const determinant = a * d - b * c;
|
|
54
|
+
const inverse = [d / determinant, -b / determinant, -c / determinant, a / determinant,
|
|
55
|
+
(c * f - d * e) / determinant, (b * e - a * f) / determinant];
|
|
56
|
+
if (!inverse.every(Number.isFinite)) {
|
|
57
|
+
refuse("A transform matrix must have an inverse of finite numbers", path);
|
|
58
|
+
}
|
|
59
|
+
return transform;
|
|
60
|
+
}
|
|
61
|
+
function importedNetwork(pathData, fillRule) {
|
|
62
|
+
const path = `${ROOT}.pathData`;
|
|
63
|
+
if (typeof pathData !== "string")
|
|
64
|
+
refuse("Expected a string of SVG path data", path);
|
|
65
|
+
if (pathData.trim().length === 0)
|
|
66
|
+
refuse("SVG path data is empty", path);
|
|
67
|
+
let network;
|
|
68
|
+
try {
|
|
69
|
+
network = vectorNetworkFromSvgPathData(pathData, fillRule);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
refuse(`SVG path data cannot be read: ${error instanceof Error ? error.message : String(error)}`, path, error);
|
|
73
|
+
}
|
|
74
|
+
if (network.segments.length === 0) {
|
|
75
|
+
refuse("SVG path data draws nothing: it has no line, curve or arc", path);
|
|
76
|
+
}
|
|
77
|
+
const finite = (value) => typeof value === "number"
|
|
78
|
+
? Number.isFinite(value)
|
|
79
|
+
: typeof value !== "object" || value === null || Object.values(value).every(finite);
|
|
80
|
+
if (!finite(network.vertices) || !finite(network.segments)) {
|
|
81
|
+
refuse("SVG path data has a number that is not finite", path);
|
|
82
|
+
}
|
|
83
|
+
return network;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* SVG's `stroke-linecap` ends every dash as well as the path, and the
|
|
87
|
+
* renderer draws a stroke's open ends with its dash ends wherever the path's
|
|
88
|
+
* own end points are not used (see `drawsEndsWithDashEnds`). So every stroke
|
|
89
|
+
* takes the cap as its dash ends; one that names its own keeps them (the
|
|
90
|
+
* caller spreads the stroke after this).
|
|
91
|
+
*/
|
|
92
|
+
function dashEnds(stroke, cap) {
|
|
93
|
+
return typeof stroke === "object" && stroke !== null ? { dashCap: cap } : {};
|
|
94
|
+
}
|
|
95
|
+
function isCentered(stroke) {
|
|
96
|
+
if (typeof stroke !== "object" || stroke === null)
|
|
97
|
+
return true;
|
|
98
|
+
return (stroke.position ?? "center") === "center";
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Whether the open ends of the path are drawn with each stroke's dash ends
|
|
102
|
+
* instead of the path's own end points (`startCap`, `endCap`). The end points
|
|
103
|
+
* are used only on one open chain whose strokes are all centered (see
|
|
104
|
+
* `PathNode.startCap`). So this is true for a path with open ends that has
|
|
105
|
+
* several open parts, closed and open parts together, or a stroke that is
|
|
106
|
+
* inside or outside. A closed path has no open end to draw.
|
|
107
|
+
*/
|
|
108
|
+
function drawsEndsWithDashEnds(network, strokes) {
|
|
109
|
+
if (openPathChain(network) !== null)
|
|
110
|
+
return !strokes.every(isCentered);
|
|
111
|
+
const uses = new Map();
|
|
112
|
+
for (const segment of network.segments) {
|
|
113
|
+
uses.set(segment.start, (uses.get(segment.start) ?? 0) + 1);
|
|
114
|
+
uses.set(segment.end, (uses.get(segment.end) ?? 0) + 1);
|
|
115
|
+
}
|
|
116
|
+
return [...uses.values()].some((count) => count === 1);
|
|
117
|
+
}
|
|
118
|
+
/** The dash ends a stroke names itself, if it does. */
|
|
119
|
+
function ownDashEnds(stroke) {
|
|
120
|
+
return typeof stroke === "object" && stroke !== null ? stroke.dashCap : undefined;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Builds a complete, editable path layer from one SVG `<path>`: its `d`
|
|
124
|
+
* attribute, its `fill-rule`, `stroke-linecap` and `stroke-linejoin`, and the
|
|
125
|
+
* matrix that places it in its parent. Relative, shorthand and quadratic
|
|
126
|
+
* commands are normalized and arcs stay arcs (see
|
|
127
|
+
* `vectorNetworkFromSvgPathData`). A path layer has no frame: its bounds come
|
|
128
|
+
* from the network under `transform` (`getNodeBounds`).
|
|
129
|
+
*
|
|
130
|
+
* Nothing is guessed. Each of these throws a `ModelError` with code
|
|
131
|
+
* `INVALID_SCHEMA` whose `path` names the option, such as
|
|
132
|
+
* `$svgPath.pathData`: path data that is not a string, is empty, cannot be
|
|
133
|
+
* read, draws no segment or holds a number that is not finite; a `transform`
|
|
134
|
+
* that is not six finite numbers, cannot be inverted (see
|
|
135
|
+
* `transformIsInvertible`) or puts the path beyond finite numbers; a `fillRule`,
|
|
136
|
+
* `strokeCap` or `strokeJoin` outside SVG's values; a `strokeJoin` for a style
|
|
137
|
+
* without strokes; a style that names a shared style (`styleId`) and no
|
|
138
|
+
* `strokes` of its own, whose strokes would be the shared style's; a stroke
|
|
139
|
+
* whose own `dashCap` differs from `strokeCap` on a path whose open ends are
|
|
140
|
+
* drawn with the dash ends (several open parts, or a stroke that is inside
|
|
141
|
+
* or outside), because one cap draws both there; and an option this function
|
|
142
|
+
* does not know.
|
|
143
|
+
*
|
|
144
|
+
* The rest of the layer (`id`, `name`, `style`, `opacity`) is checked when
|
|
145
|
+
* the layer enters a document, as for any other layer.
|
|
146
|
+
*/
|
|
147
|
+
export function pathNodeFromSvgPath(svgPath) {
|
|
148
|
+
if (typeof svgPath !== "object" || svgPath === null || Array.isArray(svgPath)) {
|
|
149
|
+
refuse("Expected an object that describes an SVG path", ROOT);
|
|
150
|
+
}
|
|
151
|
+
for (const key of Object.keys(svgPath)) {
|
|
152
|
+
if (!OPTION_KEYS.has(key))
|
|
153
|
+
refuse("Unknown option", `${ROOT}.${key}`);
|
|
154
|
+
}
|
|
155
|
+
const fillRule = oneOf(svgPath.fillRule, SVG_FILL_RULES, "nonzero", `${ROOT}.fillRule`);
|
|
156
|
+
const svgCap = oneOf(svgPath.strokeCap, SVG_STROKE_CAPS, "butt", `${ROOT}.strokeCap`);
|
|
157
|
+
const cap = CAP_FROM_SVG[svgCap];
|
|
158
|
+
const join = oneOf(svgPath.strokeJoin, SVG_STROKE_JOINS, "miter", `${ROOT}.strokeJoin`);
|
|
159
|
+
const transform = importedTransform(svgPath.transform, `${ROOT}.transform`);
|
|
160
|
+
const vectorNetwork = importedNetwork(svgPath.pathData, fillRule);
|
|
161
|
+
if (typeof svgPath.style !== "object" || svgPath.style === null || Array.isArray(svgPath.style)) {
|
|
162
|
+
refuse("Expected the layer's style", `${ROOT}.style`);
|
|
163
|
+
}
|
|
164
|
+
const strokes = svgPath.style.strokes;
|
|
165
|
+
if (strokes !== undefined && !Array.isArray(strokes)) {
|
|
166
|
+
refuse("Expected a list of strokes", `${ROOT}.style.strokes`);
|
|
167
|
+
}
|
|
168
|
+
const sharedStyleId = svgPath.style.styleId;
|
|
169
|
+
if (sharedStyleId !== undefined && typeof sharedStyleId !== "string") {
|
|
170
|
+
refuse(`Expected a shared style ID but got ${shown(sharedStyleId)}`, `${ROOT}.style.styleId`);
|
|
171
|
+
}
|
|
172
|
+
if (strokes === undefined && sharedStyleId !== undefined) {
|
|
173
|
+
// The layer would draw the shared style's strokes, which this function
|
|
174
|
+
// cannot see or change: SVG's join and cap would not be on them.
|
|
175
|
+
refuse(`The strokes would come from the shared style ${JSON.stringify(sharedStyleId)}, which cannot take SVG's join and cap; `
|
|
176
|
+
+ "give the strokes in style.strokes (an empty list for no stroke)", `${ROOT}.style.strokes`);
|
|
177
|
+
}
|
|
178
|
+
if (strokes !== undefined && drawsEndsWithDashEnds(vectorNetwork, strokes)) {
|
|
179
|
+
strokes.forEach((stroke, index) => {
|
|
180
|
+
const own = ownDashEnds(stroke);
|
|
181
|
+
if (own === undefined || own === cap)
|
|
182
|
+
return;
|
|
183
|
+
refuse("SVG has one line cap, and this path's open ends are drawn with each stroke's dash ends "
|
|
184
|
+
+ "(it has several open parts, or a stroke that is inside or outside), "
|
|
185
|
+
+ `so the dash ends ${shown(own)} cannot differ from the strokeCap ${JSON.stringify(svgCap)}`, `${ROOT}.style.strokes[${index}].dashCap`);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (svgPath.strokeJoin !== undefined && (strokes === undefined || strokes.length === 0)) {
|
|
189
|
+
refuse("A stroke join needs a stroke in style.strokes to be stored on", `${ROOT}.strokeJoin`);
|
|
190
|
+
}
|
|
191
|
+
const node = {
|
|
192
|
+
id: svgPath.id,
|
|
193
|
+
name: svgPath.name,
|
|
194
|
+
visible: svgPath.visible ?? true,
|
|
195
|
+
locked: svgPath.locked ?? false,
|
|
196
|
+
opacity: svgPath.opacity ?? 1,
|
|
197
|
+
type: "shape",
|
|
198
|
+
shape: "path",
|
|
199
|
+
vectorNetwork,
|
|
200
|
+
transform,
|
|
201
|
+
style: strokes === undefined
|
|
202
|
+
? svgPath.style
|
|
203
|
+
: { ...svgPath.style, strokes: strokes.map((stroke) => ({ join, ...dashEnds(stroke, cap), ...stroke })) },
|
|
204
|
+
startCap: cap,
|
|
205
|
+
endCap: cap,
|
|
206
|
+
};
|
|
207
|
+
// A large scale can carry finite path data past the largest number. The
|
|
208
|
+
// corners of the path's own box go farthest, so they are what is checked.
|
|
209
|
+
const box = getNodeBounds({ ...node, transform: [1, 0, 0, 1, 0, 0] });
|
|
210
|
+
const [a, b, c, d, e, f] = transform;
|
|
211
|
+
for (const x of [box.x, box.x + box.width]) {
|
|
212
|
+
for (const y of [box.y, box.y + box.height]) {
|
|
213
|
+
if (!Number.isFinite(a * x + c * y + e) || !Number.isFinite(b * x + d * y + f)) {
|
|
214
|
+
refuse("The transform puts the path where its coordinates are not finite numbers", `${ROOT}.transform`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return node;
|
|
219
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { AssetId, DesignDocument, Rect, TextParagraph, TextStyle, TextStyleOverrides, TextualNode } from "./types.ts";
|
|
2
|
+
export interface TextLineMetrics {
|
|
3
|
+
readonly width: number;
|
|
4
|
+
readonly ascent: number;
|
|
5
|
+
readonly descent: number;
|
|
6
|
+
readonly height: number;
|
|
7
|
+
}
|
|
8
|
+
export interface TextMeasurement {
|
|
9
|
+
readonly width: number;
|
|
10
|
+
readonly height: number;
|
|
11
|
+
readonly lineCount: number;
|
|
12
|
+
readonly lines: readonly TextLineMetrics[];
|
|
13
|
+
}
|
|
14
|
+
export interface TextFontRequest {
|
|
15
|
+
readonly font: string;
|
|
16
|
+
readonly text: string;
|
|
17
|
+
}
|
|
18
|
+
export interface TextListLayout {
|
|
19
|
+
readonly text: string;
|
|
20
|
+
readonly style: TextStyle;
|
|
21
|
+
readonly gap: number;
|
|
22
|
+
/** Additional body and marker inset contributed by the authored list level. */
|
|
23
|
+
readonly nestingOffset: number;
|
|
24
|
+
readonly left: number;
|
|
25
|
+
readonly right: number;
|
|
26
|
+
readonly firstLine: number;
|
|
27
|
+
readonly markerWidth: number;
|
|
28
|
+
readonly markerBoxWidth: number;
|
|
29
|
+
}
|
|
30
|
+
export declare const textMeasurementStats: {
|
|
31
|
+
layoutComputations: number;
|
|
32
|
+
fontRequestTextRebuilds: number;
|
|
33
|
+
resolvedStyleCopies: number;
|
|
34
|
+
};
|
|
35
|
+
export declare function resetTextMeasurementStats(): void;
|
|
36
|
+
export declare function resolveTextStyle(document: DesignDocument, styleId: string | undefined, overrides: TextStyleOverrides | undefined): TextStyle;
|
|
37
|
+
export declare function splitCssFontFamilies(fontFamily: string): readonly string[];
|
|
38
|
+
/** Returns the stable CSS family used for one document font asset identity. */
|
|
39
|
+
export declare function documentFontFamilyAlias(assetId: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Resolves embedded font identities into an asset-specific CSS family stack.
|
|
42
|
+
* The primary embedded face replaces the authored primary family, ordered
|
|
43
|
+
* embedded fallbacks follow it, and the authored CSS fallback tail remains
|
|
44
|
+
* last. Asset aliases keep same-family faces distinct and deterministic.
|
|
45
|
+
*/
|
|
46
|
+
export declare function resolveTextFontFamily(document: DesignDocument, style: TextStyle): string;
|
|
47
|
+
export declare function textStyleToCanvasFont(style: TextStyle, document?: DesignDocument): string;
|
|
48
|
+
export declare function clearTextMeasurementCache(): void;
|
|
49
|
+
/**
|
|
50
|
+
* The style a list marker of the paragraph is drawn in. A marker takes the
|
|
51
|
+
* shared style and the overrides it does not name itself from the first run
|
|
52
|
+
* of its paragraph, each on its own: a marker with a style and no overrides
|
|
53
|
+
* still takes the first run's overrides. A marker the renderer writes itself
|
|
54
|
+
* (a bullet or a number) takes both. The page compiler, the canvas estimate
|
|
55
|
+
* and `textLayoutKey` all resolve markers here, so they cannot disagree.
|
|
56
|
+
*/
|
|
57
|
+
export declare function resolveListMarkerStyle(document: DesignDocument, paragraph: Pick<TextParagraph, "runs" | "list">): TextStyle;
|
|
58
|
+
export declare function resolveTextListLayout(document: DesignDocument, paragraph: TextParagraph, paragraphIndex: number): TextListLayout | null;
|
|
59
|
+
/**
|
|
60
|
+
* The space above the paragraph at `index` of a text: its own `spacingBefore`
|
|
61
|
+
* where set, otherwise the text's `paragraphSpacing` for every paragraph but
|
|
62
|
+
* the first.
|
|
63
|
+
*/
|
|
64
|
+
export declare function paragraphSpaceAbove(node: Pick<TextualNode, "paragraphSpacing">, paragraph: Pick<TextParagraph, "spacingBefore">, index: number): number;
|
|
65
|
+
export declare function measureTextNode(document: DesignDocument, node: TextualNode, paragraphs?: readonly TextParagraph[]): TextMeasurement;
|
|
66
|
+
export declare function getTextNodeBaselines(document: DesignDocument, node: TextualNode, paragraphs?: readonly TextParagraph[]): readonly number[];
|
|
67
|
+
/** One laid-out line of a text, in the text layer's own coordinates (the top-left of its box at 0, 0). */
|
|
68
|
+
export interface TextLineBox {
|
|
69
|
+
readonly x: number;
|
|
70
|
+
readonly y: number;
|
|
71
|
+
readonly width: number;
|
|
72
|
+
readonly height: number;
|
|
73
|
+
/** The y of the line's baseline. */
|
|
74
|
+
readonly baseline: number;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Where each line of a text sits in its box, from the same layout that gives
|
|
78
|
+
* `measureTextNode` and `getTextNodeBaselines`: each line moved down by the
|
|
79
|
+
* text's vertical alignment and across by its paragraph's alignment (a
|
|
80
|
+
* justified line spans the box only where the renderer stretches it: not the
|
|
81
|
+
* last line before a hard break or the paragraph's end, and not a line with no
|
|
82
|
+
* gap between words; in a right-to-left paragraph such a line keeps to the
|
|
83
|
+
* right). Each line is placed between its paragraph's left and
|
|
84
|
+
* right indents (the first-line indent on a paragraph's first line), as the
|
|
85
|
+
* renderer paints it; a list's first line also includes its marker. It only reads the layout; it
|
|
86
|
+
* draws nothing and changes no text.
|
|
87
|
+
*/
|
|
88
|
+
export declare function getTextNodeLineBoxes(document: DesignDocument, node: TextualNode, paragraphs?: readonly TextParagraph[]): readonly TextLineBox[];
|
|
89
|
+
export declare function getTextNodeFontRequests(document: DesignDocument, node: TextualNode, paragraphs?: readonly TextParagraph[]): readonly TextFontRequest[];
|
|
90
|
+
/**
|
|
91
|
+
* The point of a text box that stays in place on the page when its content
|
|
92
|
+
* resizes it, as fractions of the frame's local width and height. As in Figma,
|
|
93
|
+
* the top edge stays put, and auto-width text keeps its alignment edge: the
|
|
94
|
+
* left edge for left-aligned or justified text, the center for centered text
|
|
95
|
+
* and the right edge for right-aligned text. When paragraphs disagree, the left
|
|
96
|
+
* edge stays put.
|
|
97
|
+
*/
|
|
98
|
+
export declare function textAutoResizeAnchor(paragraphs: readonly TextParagraph[]): {
|
|
99
|
+
readonly x: number;
|
|
100
|
+
readonly y: number;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* The frame of an auto-sized text box after its content changed its size.
|
|
104
|
+
* The box turns about its center, so keeping `frame.x` and `frame.y` would move
|
|
105
|
+
* rotated text across the page as it grows; this keeps the anchor from
|
|
106
|
+
* `textAutoResizeAnchor` fixed in page space instead.
|
|
107
|
+
*/
|
|
108
|
+
export declare function autoResizedTextFrame(node: Pick<TextualNode, "frame" | "rotation">, paragraphs: readonly TextParagraph[], size: {
|
|
109
|
+
readonly width: number;
|
|
110
|
+
readonly height: number;
|
|
111
|
+
}): Rect;
|
|
112
|
+
/**
|
|
113
|
+
* `textLayoutKey`, and the IDs of the font assets the key names
|
|
114
|
+
* (`fontAssetFingerprint`), a missing one included: the key of the text
|
|
115
|
+
* changes only when the entry of one of these assets does, or the text or
|
|
116
|
+
* the theme.
|
|
117
|
+
*/
|
|
118
|
+
export declare function textLayoutKeyAndFontAssets(document: DesignDocument, node: Pick<TextualNode, "autoResize" | "wrap" | "frame" | "paragraphs" | "paragraphSpacing">): {
|
|
119
|
+
readonly key: string;
|
|
120
|
+
readonly fontAssetIds: readonly AssetId[];
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Names everything in the document that decides the size of an auto-sized
|
|
124
|
+
* text: its words, the styles they resolve to in this document (so a theme
|
|
125
|
+
* or variable mode that changes a style changes the key), the font file each
|
|
126
|
+
* of their font assets stands for, the paragraph settings, how the box sizes
|
|
127
|
+
* itself, and its width when only its height follows the text. A list marker
|
|
128
|
+
* is named by the style the renderer draws it in (`resolveListMarkerStyle`),
|
|
129
|
+
* which can come from the first run of its paragraph. Two texts
|
|
130
|
+
* of one document with the same key are laid out alike by the renderer (the
|
|
131
|
+
* order in which the fields of a style or a paragraph were written is not
|
|
132
|
+
* part of the key), as
|
|
133
|
+
* long as it draws with the same font files; the files that are not in the
|
|
134
|
+
* document (which parts of a Google Fonts family have arrived, the fallback
|
|
135
|
+
* fonts the renderer picks itself) are told apart by `setDrawnTextFontSet`.
|
|
136
|
+
* The drawn sizes are kept by this key
|
|
137
|
+
* (`recordDrawnTextSize`), and anything else that remembers or counts
|
|
138
|
+
* something about one layout of a text uses it too, so a change of font,
|
|
139
|
+
* size, wrap width or sizing mode is never taken for the layout before it.
|
|
140
|
+
*/
|
|
141
|
+
export declare function textLayoutKey(document: DesignDocument, node: Pick<TextualNode, "autoResize" | "wrap" | "frame" | "paragraphs" | "paragraphSpacing">, paragraphs?: readonly TextParagraph[]): string;
|
|
142
|
+
export * from "./text-sizer.ts";
|