@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,24 @@
|
|
|
1
|
+
import type { DocumentTheme, VariableCollection, VariableDefinition, VariableId, VariableModes, VariableProperty, VariableType, VariableValue, SceneNode } from "./types.ts";
|
|
2
|
+
export declare const VARIABLE_PROPERTY_TYPES: Readonly<Record<VariableProperty, VariableType>>;
|
|
3
|
+
export interface LocatedVariable {
|
|
4
|
+
readonly collectionId: string;
|
|
5
|
+
readonly collection: VariableCollection;
|
|
6
|
+
readonly variableId: VariableId;
|
|
7
|
+
readonly variable: VariableDefinition;
|
|
8
|
+
}
|
|
9
|
+
export declare function findVariable(theme: DocumentTheme, variableId: VariableId | string): LocatedVariable | undefined;
|
|
10
|
+
export declare function variableSupportsProperty(variable: VariableDefinition, property: VariableProperty): boolean;
|
|
11
|
+
export declare function nodeSupportsVariableProperty(node: SceneNode, property: VariableProperty): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The variable's value in the mode its collection shows under `modes` (the
|
|
14
|
+
* modes in effect at a layer, see `variableModesAt`): the inherited choice,
|
|
15
|
+
* else the collection's active mode, else its default mode. A mode without a
|
|
16
|
+
* value reads the default mode's value.
|
|
17
|
+
*/
|
|
18
|
+
export declare function resolveVariableValue(theme: DocumentTheme, variableId: VariableId | string, modes?: VariableModes): VariableValue | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* `resolveVariableValue` for a variable already found with `findVariable`.
|
|
21
|
+
* @since 0.26.0
|
|
22
|
+
*/
|
|
23
|
+
export declare function locatedVariableValue(located: LocatedVariable, modes?: VariableModes): VariableValue | undefined;
|
|
24
|
+
export declare function listVariablesForProperty(theme: DocumentTheme, property: VariableProperty): readonly LocatedVariable[];
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { entryOf } from "./id-table.js";
|
|
2
|
+
export const VARIABLE_PROPERTY_TYPES = {
|
|
3
|
+
visibility: "boolean",
|
|
4
|
+
opacity: "number",
|
|
5
|
+
width: "number",
|
|
6
|
+
height: "number",
|
|
7
|
+
cornerRadius: "number",
|
|
8
|
+
fillColor: "color",
|
|
9
|
+
strokeColor: "color",
|
|
10
|
+
strokeWidth: "number",
|
|
11
|
+
gap: "number",
|
|
12
|
+
wrapGap: "number",
|
|
13
|
+
columnGap: "number",
|
|
14
|
+
rowGap: "number",
|
|
15
|
+
paddingTop: "number",
|
|
16
|
+
paddingRight: "number",
|
|
17
|
+
paddingBottom: "number",
|
|
18
|
+
paddingLeft: "number",
|
|
19
|
+
fontFamily: "string",
|
|
20
|
+
fontSize: "number",
|
|
21
|
+
fontWeight: "number",
|
|
22
|
+
lineHeight: "number",
|
|
23
|
+
letterSpacing: "number",
|
|
24
|
+
textColor: "color",
|
|
25
|
+
};
|
|
26
|
+
export function findVariable(theme, variableId) {
|
|
27
|
+
const collections = theme.variableCollections;
|
|
28
|
+
if (collections === undefined)
|
|
29
|
+
return undefined;
|
|
30
|
+
let index = variablesByCollections.get(collections);
|
|
31
|
+
if (index === undefined) {
|
|
32
|
+
if (!Object.isFrozen(collections))
|
|
33
|
+
return searchVariable(collections, variableId);
|
|
34
|
+
index = new Map();
|
|
35
|
+
// The first collection that holds an ID wins, as a search in collection order finds it.
|
|
36
|
+
for (const [collectionId, collection] of Object.entries(collections)) {
|
|
37
|
+
for (const [id, variable] of Object.entries(collection.variables)) {
|
|
38
|
+
if (variable === undefined || index.has(id))
|
|
39
|
+
continue;
|
|
40
|
+
index.set(id, Object.freeze({ collectionId, collection, variableId: id, variable }));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
variablesByCollections.set(collections, index);
|
|
44
|
+
}
|
|
45
|
+
return index.get(variableId);
|
|
46
|
+
}
|
|
47
|
+
function searchVariable(collections, variableId) {
|
|
48
|
+
for (const [collectionId, collection] of Object.entries(collections)) {
|
|
49
|
+
const variable = entryOf(collection.variables, variableId);
|
|
50
|
+
if (variable !== undefined)
|
|
51
|
+
return { collectionId, collection, variableId: variableId, variable };
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Every variable of a frozen `variableCollections` record by ID. A frozen
|
|
57
|
+
* record never changes (this package freezes values all the way down), so
|
|
58
|
+
* the index stays valid for as long as the record is alive. Bindings look variables up for every
|
|
59
|
+
* bound layer an instance shows.
|
|
60
|
+
*/
|
|
61
|
+
const variablesByCollections = new WeakMap();
|
|
62
|
+
export function variableSupportsProperty(variable, property) {
|
|
63
|
+
return variable.type === VARIABLE_PROPERTY_TYPES[property]
|
|
64
|
+
&& (variable.scopes === undefined || variable.scopes.includes(property));
|
|
65
|
+
}
|
|
66
|
+
export function nodeSupportsVariableProperty(node, property) {
|
|
67
|
+
if (property === "visibility" || property === "opacity")
|
|
68
|
+
return true;
|
|
69
|
+
// A line's frame always has zero height; its length is the width.
|
|
70
|
+
if (property === "height" && node.type === "shape" && node.shape === "line")
|
|
71
|
+
return false;
|
|
72
|
+
// A group's size is always computed from its layers, so it never binds one.
|
|
73
|
+
if (property === "width" || property === "height") {
|
|
74
|
+
return "frame" in node && node.type !== "group";
|
|
75
|
+
}
|
|
76
|
+
if (property === "cornerRadius") {
|
|
77
|
+
return node.type === "frame"
|
|
78
|
+
|| (node.type === "shape" && (node.shape === "rectangle" || node.shape === "path" ||
|
|
79
|
+
node.shape === "polygon" || node.shape === "star"));
|
|
80
|
+
}
|
|
81
|
+
// A line is a single stroke and never fills.
|
|
82
|
+
if (property === "fillColor") {
|
|
83
|
+
return node.type === "frame" || (node.type === "shape" && node.shape !== "line");
|
|
84
|
+
}
|
|
85
|
+
if (property === "strokeColor" || property === "strokeWidth") {
|
|
86
|
+
return node.type === "frame"
|
|
87
|
+
|| node.type === "shape"
|
|
88
|
+
|| node.type === "connector"
|
|
89
|
+
|| node.type === "code";
|
|
90
|
+
}
|
|
91
|
+
if (["fontFamily", "fontSize"].includes(property)) {
|
|
92
|
+
return node.type === "text" || node.type === "code";
|
|
93
|
+
}
|
|
94
|
+
if (["lineHeight", "letterSpacing"].includes(property)) {
|
|
95
|
+
return node.type === "text" || node.type === "code";
|
|
96
|
+
}
|
|
97
|
+
if (["fontWeight", "textColor"].includes(property)) {
|
|
98
|
+
return node.type === "text";
|
|
99
|
+
}
|
|
100
|
+
if (node.type !== "frame" || node.layout.kind === "absolute")
|
|
101
|
+
return false;
|
|
102
|
+
if (property.startsWith("padding"))
|
|
103
|
+
return true;
|
|
104
|
+
if (property === "gap")
|
|
105
|
+
return node.layout.kind !== "grid";
|
|
106
|
+
if (property === "wrapGap")
|
|
107
|
+
return node.layout.kind !== "grid";
|
|
108
|
+
if (property === "columnGap" || property === "rowGap")
|
|
109
|
+
return node.layout.kind === "grid";
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The variable's value in the mode its collection shows under `modes` (the
|
|
114
|
+
* modes in effect at a layer, see `variableModesAt`): the inherited choice,
|
|
115
|
+
* else the collection's active mode, else its default mode. A mode without a
|
|
116
|
+
* value reads the default mode's value.
|
|
117
|
+
*/
|
|
118
|
+
export function resolveVariableValue(theme, variableId, modes) {
|
|
119
|
+
const located = findVariable(theme, variableId);
|
|
120
|
+
return located === undefined ? undefined : locatedVariableValue(located, modes);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* `resolveVariableValue` for a variable already found with `findVariable`.
|
|
124
|
+
* @since 0.26.0
|
|
125
|
+
*/
|
|
126
|
+
export function locatedVariableValue(located, modes) {
|
|
127
|
+
const { collectionId, collection, variable } = located;
|
|
128
|
+
const chosen = entryOf(modes, collectionId);
|
|
129
|
+
const modeId = chosen !== undefined && entryOf(collection.modes, chosen) !== undefined
|
|
130
|
+
? chosen
|
|
131
|
+
: collection.activeModeId ?? collection.defaultModeId;
|
|
132
|
+
const values = variable.values;
|
|
133
|
+
return entryOf(values, modeId)
|
|
134
|
+
?? entryOf(values, collection.defaultModeId);
|
|
135
|
+
}
|
|
136
|
+
export function listVariablesForProperty(theme, property) {
|
|
137
|
+
const matches = [];
|
|
138
|
+
for (const [collectionId, collection] of Object.entries(theme.variableCollections ?? {})) {
|
|
139
|
+
for (const [id, variable] of Object.entries(collection.variables)) {
|
|
140
|
+
if (!variableSupportsProperty(variable, property))
|
|
141
|
+
continue;
|
|
142
|
+
matches.push({
|
|
143
|
+
collectionId,
|
|
144
|
+
collection,
|
|
145
|
+
variableId: id,
|
|
146
|
+
variable,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return matches;
|
|
151
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type TransformMatrix, type VectorNetwork, type VectorRegion } from "./types.ts";
|
|
2
|
+
export type VectorWindingRule = VectorRegion["windingRule"];
|
|
3
|
+
/**
|
|
4
|
+
* Converts an SVG path into deterministic, node-local vector-network topology.
|
|
5
|
+
* Relative, shorthand, horizontal, vertical, and quadratic commands are
|
|
6
|
+
* normalized. Elliptical arcs remain exact compatibility segments.
|
|
7
|
+
*/
|
|
8
|
+
export declare function vectorNetworkFromSvgPathData(pathData: string, windingRule?: VectorWindingRule): VectorNetwork;
|
|
9
|
+
/** Projects one fillable region to SVG path data. */
|
|
10
|
+
export declare function vectorRegionToSvgPathData(network: VectorNetwork, region: VectorRegion): string;
|
|
11
|
+
/**
|
|
12
|
+
* Projects every network edge once for SVG-compatible stroking and bounds.
|
|
13
|
+
* Region loops preserve authored chains where possible; remaining graph edges
|
|
14
|
+
* become individual subpaths because SVG cannot encode a branched join.
|
|
15
|
+
*/
|
|
16
|
+
export declare function vectorNetworkToStrokePathData(network: VectorNetwork): string;
|
|
17
|
+
/** Projects the first matching region, useful for callers that retain a region ID. */
|
|
18
|
+
export declare function vectorRegionPathDataById(network: VectorNetwork, regionId: string): string | undefined;
|
|
19
|
+
/** Applies an SVG-compatible affine matrix to a point. */
|
|
20
|
+
export declare function transformVectorPoint(x: number, y: number, transform: TransformMatrix): {
|
|
21
|
+
readonly x: number;
|
|
22
|
+
readonly y: number;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Scales a vector network in its own coordinate space around one anchor.
|
|
26
|
+
* An elliptical arc keeps its exact shape: see `scaleEllipticalArc`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function scaleVectorNetwork(network: VectorNetwork, scale: {
|
|
29
|
+
readonly x: number;
|
|
30
|
+
readonly y: number;
|
|
31
|
+
}, anchor: {
|
|
32
|
+
readonly x: number;
|
|
33
|
+
readonly y: number;
|
|
34
|
+
}): VectorNetwork;
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import { SVGPathData, SVGPathDataTransformer } from "svg-pathdata";
|
|
2
|
+
import { vectorHandleId, vectorRegionId, vectorSegmentId, vectorVertexId, } from "./types.js";
|
|
3
|
+
const samePoint = (left, right) => left.x === right.x && left.y === right.y;
|
|
4
|
+
/**
|
|
5
|
+
* Converts an SVG path into deterministic, node-local vector-network topology.
|
|
6
|
+
* Relative, shorthand, horizontal, vertical, and quadratic commands are
|
|
7
|
+
* normalized. Elliptical arcs remain exact compatibility segments.
|
|
8
|
+
*/
|
|
9
|
+
export function vectorNetworkFromSvgPathData(pathData, windingRule = "nonzero") {
|
|
10
|
+
const commands = new SVGPathData(pathData)
|
|
11
|
+
.toAbs()
|
|
12
|
+
.normalizeST()
|
|
13
|
+
.qtToC()
|
|
14
|
+
.transform(SVGPathDataTransformer.NORMALIZE_HVZ(false, true, true, false))
|
|
15
|
+
.commands;
|
|
16
|
+
const vertices = [];
|
|
17
|
+
const segments = [];
|
|
18
|
+
const loops = [];
|
|
19
|
+
let currentPoint;
|
|
20
|
+
let subpathStart;
|
|
21
|
+
let currentVertex;
|
|
22
|
+
let subpathSegments = [];
|
|
23
|
+
const addVertex = (x, y) => {
|
|
24
|
+
const vertex = {
|
|
25
|
+
id: vectorVertexId(`v${vertices.length}`),
|
|
26
|
+
x,
|
|
27
|
+
y,
|
|
28
|
+
};
|
|
29
|
+
vertices.push(vertex);
|
|
30
|
+
return vertex;
|
|
31
|
+
};
|
|
32
|
+
const addSegment = (segment) => {
|
|
33
|
+
const id = vectorSegmentId(`s${segments.length}`);
|
|
34
|
+
const complete = { ...segment, id };
|
|
35
|
+
segments.push(complete);
|
|
36
|
+
subpathSegments.push({ segmentId: id, direction: "forward" });
|
|
37
|
+
return complete;
|
|
38
|
+
};
|
|
39
|
+
const startSubpathAtCurrentPoint = () => {
|
|
40
|
+
if (currentPoint === undefined) {
|
|
41
|
+
throw new TypeError("SVG path drawing commands must follow a move command");
|
|
42
|
+
}
|
|
43
|
+
const start = addVertex(currentPoint.x, currentPoint.y);
|
|
44
|
+
subpathStart = start;
|
|
45
|
+
currentVertex = start;
|
|
46
|
+
subpathSegments = [];
|
|
47
|
+
};
|
|
48
|
+
const finishSubpath = (closed) => {
|
|
49
|
+
if (subpathSegments.length > 0) {
|
|
50
|
+
loops.push({
|
|
51
|
+
segments: subpathSegments,
|
|
52
|
+
...(closed ? {} : { implicitClose: true }),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
subpathStart = undefined;
|
|
56
|
+
currentVertex = undefined;
|
|
57
|
+
subpathSegments = [];
|
|
58
|
+
};
|
|
59
|
+
const ensureActiveSubpath = () => {
|
|
60
|
+
if (subpathStart === undefined || currentVertex === undefined) {
|
|
61
|
+
startSubpathAtCurrentPoint();
|
|
62
|
+
}
|
|
63
|
+
return { start: subpathStart, current: currentVertex };
|
|
64
|
+
};
|
|
65
|
+
for (const command of commands) {
|
|
66
|
+
if (command.type === SVGPathData.MOVE_TO) {
|
|
67
|
+
finishSubpath(false);
|
|
68
|
+
currentPoint = { x: command.x, y: command.y };
|
|
69
|
+
startSubpathAtCurrentPoint();
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (command.type === SVGPathData.CLOSE_PATH) {
|
|
73
|
+
if (subpathStart === undefined || currentVertex === undefined)
|
|
74
|
+
continue;
|
|
75
|
+
if (subpathSegments.length > 0) {
|
|
76
|
+
if (samePoint(currentVertex, subpathStart)) {
|
|
77
|
+
if (currentVertex.id !== subpathStart.id) {
|
|
78
|
+
const lastIndex = segments.length - 1;
|
|
79
|
+
const last = segments[lastIndex];
|
|
80
|
+
if (last !== undefined && last.end === currentVertex.id) {
|
|
81
|
+
segments[lastIndex] = { ...last, end: subpathStart.id };
|
|
82
|
+
if (vertices.at(-1)?.id === currentVertex.id)
|
|
83
|
+
vertices.pop();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
addSegment({
|
|
89
|
+
kind: "line",
|
|
90
|
+
start: currentVertex.id,
|
|
91
|
+
end: subpathStart.id,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
currentPoint = { x: subpathStart.x, y: subpathStart.y };
|
|
96
|
+
finishSubpath(true);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const { current } = ensureActiveSubpath();
|
|
100
|
+
if (!("x" in command) || !("y" in command)) {
|
|
101
|
+
throw new TypeError(`Unsupported normalized SVG command ${command.type}`);
|
|
102
|
+
}
|
|
103
|
+
const endpoint = addVertex(command.x, command.y);
|
|
104
|
+
if (command.type === SVGPathData.LINE_TO) {
|
|
105
|
+
addSegment({ kind: "line", start: current.id, end: endpoint.id });
|
|
106
|
+
}
|
|
107
|
+
else if (command.type === SVGPathData.CURVE_TO) {
|
|
108
|
+
const index = segments.length;
|
|
109
|
+
addSegment({
|
|
110
|
+
kind: "cubic",
|
|
111
|
+
start: current.id,
|
|
112
|
+
end: endpoint.id,
|
|
113
|
+
startHandle: {
|
|
114
|
+
id: vectorHandleId(`h${index}-start`),
|
|
115
|
+
dx: command.x1 - current.x,
|
|
116
|
+
dy: command.y1 - current.y,
|
|
117
|
+
},
|
|
118
|
+
endHandle: {
|
|
119
|
+
id: vectorHandleId(`h${index}-end`),
|
|
120
|
+
dx: command.x2 - endpoint.x,
|
|
121
|
+
dy: command.y2 - endpoint.y,
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
else if (command.type === SVGPathData.ARC) {
|
|
126
|
+
if (samePoint(current, endpoint)) {
|
|
127
|
+
vertices.pop();
|
|
128
|
+
}
|
|
129
|
+
else if (command.rX === 0 || command.rY === 0) {
|
|
130
|
+
addSegment({ kind: "line", start: current.id, end: endpoint.id });
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
addSegment({
|
|
134
|
+
kind: "elliptical-arc",
|
|
135
|
+
start: current.id,
|
|
136
|
+
end: endpoint.id,
|
|
137
|
+
radiusX: Math.abs(command.rX),
|
|
138
|
+
radiusY: Math.abs(command.rY),
|
|
139
|
+
xAxisRotation: command.xRot,
|
|
140
|
+
largeArc: command.lArcFlag === 1,
|
|
141
|
+
sweep: command.sweepFlag === 1,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
throw new TypeError(`Unsupported normalized SVG command ${command.type}`);
|
|
147
|
+
}
|
|
148
|
+
currentPoint = { x: command.x, y: command.y };
|
|
149
|
+
currentVertex = samePoint(current, endpoint) && command.type === SVGPathData.ARC
|
|
150
|
+
? current
|
|
151
|
+
: endpoint;
|
|
152
|
+
}
|
|
153
|
+
finishSubpath(false);
|
|
154
|
+
return {
|
|
155
|
+
vertices,
|
|
156
|
+
segments,
|
|
157
|
+
regions: loops.length === 0
|
|
158
|
+
? []
|
|
159
|
+
: [{ id: vectorRegionId("r0"), windingRule, loops }],
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function indexNetwork(network) {
|
|
163
|
+
return {
|
|
164
|
+
vertices: new Map(network.vertices.map((vertex) => [vertex.id, vertex])),
|
|
165
|
+
segments: new Map(network.segments.map((segment) => [segment.id, segment])),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function finiteSvgNumber(value) {
|
|
169
|
+
if (!Number.isFinite(value))
|
|
170
|
+
throw new TypeError("SVG projection requires finite coordinates");
|
|
171
|
+
return Object.is(value, -0) ? "0" : String(value);
|
|
172
|
+
}
|
|
173
|
+
function pointForReference(index, segment, direction, endpoint) {
|
|
174
|
+
const forwardEndpoint = direction === "forward" ? endpoint : endpoint === "start" ? "end" : "start";
|
|
175
|
+
const id = segment[forwardEndpoint];
|
|
176
|
+
const vertex = index.vertices.get(id);
|
|
177
|
+
if (vertex === undefined)
|
|
178
|
+
throw new TypeError(`Missing vector vertex ${id}`);
|
|
179
|
+
return vertex;
|
|
180
|
+
}
|
|
181
|
+
function commandForReference(index, segment, direction) {
|
|
182
|
+
const end = pointForReference(index, segment, direction, "end");
|
|
183
|
+
if (segment.kind === "line") {
|
|
184
|
+
return `L${finiteSvgNumber(end.x)} ${finiteSvgNumber(end.y)}`;
|
|
185
|
+
}
|
|
186
|
+
if (segment.kind === "elliptical-arc") {
|
|
187
|
+
return [
|
|
188
|
+
"A",
|
|
189
|
+
finiteSvgNumber(segment.radiusX),
|
|
190
|
+
finiteSvgNumber(segment.radiusY),
|
|
191
|
+
finiteSvgNumber(segment.xAxisRotation),
|
|
192
|
+
segment.largeArc ? "1" : "0",
|
|
193
|
+
(direction === "forward" ? segment.sweep : !segment.sweep) ? "1" : "0",
|
|
194
|
+
finiteSvgNumber(end.x),
|
|
195
|
+
finiteSvgNumber(end.y),
|
|
196
|
+
].join(" ");
|
|
197
|
+
}
|
|
198
|
+
const start = pointForReference(index, segment, direction, "start");
|
|
199
|
+
const startHandle = direction === "forward" ? segment.startHandle : segment.endHandle;
|
|
200
|
+
const endHandle = direction === "forward" ? segment.endHandle : segment.startHandle;
|
|
201
|
+
return [
|
|
202
|
+
"C",
|
|
203
|
+
finiteSvgNumber(start.x + startHandle.dx),
|
|
204
|
+
finiteSvgNumber(start.y + startHandle.dy),
|
|
205
|
+
finiteSvgNumber(end.x + endHandle.dx),
|
|
206
|
+
finiteSvgNumber(end.y + endHandle.dy),
|
|
207
|
+
finiteSvgNumber(end.x),
|
|
208
|
+
finiteSvgNumber(end.y),
|
|
209
|
+
].join(" ");
|
|
210
|
+
}
|
|
211
|
+
function projectReferences(index, references, close) {
|
|
212
|
+
const firstReference = references[0];
|
|
213
|
+
if (firstReference === undefined)
|
|
214
|
+
return "";
|
|
215
|
+
const firstSegment = index.segments.get(firstReference.segmentId);
|
|
216
|
+
if (firstSegment === undefined) {
|
|
217
|
+
throw new TypeError(`Missing vector segment ${firstReference.segmentId}`);
|
|
218
|
+
}
|
|
219
|
+
const start = pointForReference(index, firstSegment, firstReference.direction, "start");
|
|
220
|
+
const commands = [
|
|
221
|
+
`M${finiteSvgNumber(start.x)} ${finiteSvgNumber(start.y)}`,
|
|
222
|
+
];
|
|
223
|
+
for (const reference of references) {
|
|
224
|
+
const segment = index.segments.get(reference.segmentId);
|
|
225
|
+
if (segment === undefined)
|
|
226
|
+
throw new TypeError(`Missing vector segment ${reference.segmentId}`);
|
|
227
|
+
commands.push(commandForReference(index, segment, reference.direction));
|
|
228
|
+
}
|
|
229
|
+
if (close)
|
|
230
|
+
commands.push("Z");
|
|
231
|
+
return commands.join(" ");
|
|
232
|
+
}
|
|
233
|
+
/** Projects one fillable region to SVG path data. */
|
|
234
|
+
export function vectorRegionToSvgPathData(network, region) {
|
|
235
|
+
const index = indexNetwork(network);
|
|
236
|
+
return region.loops
|
|
237
|
+
.map((loop) => projectReferences(index, loop.segments, true))
|
|
238
|
+
.filter((path) => path.length > 0)
|
|
239
|
+
.join(" ");
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Projects every network edge once for SVG-compatible stroking and bounds.
|
|
243
|
+
* Region loops preserve authored chains where possible; remaining graph edges
|
|
244
|
+
* become individual subpaths because SVG cannot encode a branched join.
|
|
245
|
+
*/
|
|
246
|
+
export function vectorNetworkToStrokePathData(network) {
|
|
247
|
+
const index = indexNetwork(network);
|
|
248
|
+
const used = new Set();
|
|
249
|
+
const paths = [];
|
|
250
|
+
for (const region of network.regions) {
|
|
251
|
+
for (const loop of region.loops) {
|
|
252
|
+
if (loop.segments.some((reference) => used.has(reference.segmentId)))
|
|
253
|
+
continue;
|
|
254
|
+
const projected = projectReferences(index, loop.segments, loop.implicitClose !== true);
|
|
255
|
+
if (projected.length > 0)
|
|
256
|
+
paths.push(projected);
|
|
257
|
+
loop.segments.forEach((reference) => used.add(reference.segmentId));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
for (const segment of network.segments) {
|
|
261
|
+
if (used.has(segment.id))
|
|
262
|
+
continue;
|
|
263
|
+
const reference = {
|
|
264
|
+
segmentId: segment.id,
|
|
265
|
+
direction: "forward",
|
|
266
|
+
};
|
|
267
|
+
const projected = projectReferences(index, [reference], false);
|
|
268
|
+
if (projected.length > 0)
|
|
269
|
+
paths.push(projected);
|
|
270
|
+
used.add(segment.id);
|
|
271
|
+
}
|
|
272
|
+
return paths.join(" ");
|
|
273
|
+
}
|
|
274
|
+
/** Projects the first matching region, useful for callers that retain a region ID. */
|
|
275
|
+
export function vectorRegionPathDataById(network, regionId) {
|
|
276
|
+
const region = network.regions.find((candidate) => candidate.id === regionId);
|
|
277
|
+
return region === undefined ? undefined : vectorRegionToSvgPathData(network, region);
|
|
278
|
+
}
|
|
279
|
+
/** Applies an SVG-compatible affine matrix to a point. */
|
|
280
|
+
export function transformVectorPoint(x, y, transform) {
|
|
281
|
+
return {
|
|
282
|
+
x: transform[0] * x + transform[2] * y + transform[4],
|
|
283
|
+
y: transform[1] * x + transform[3] * y + transform[5],
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* The elliptical arc that `arc` becomes when its ellipse is scaled by
|
|
288
|
+
* `scale.x` and `scale.y` along the network's axes. A scaled ellipse is an
|
|
289
|
+
* ellipse, so the result is exact: the images of the two radius vectors give
|
|
290
|
+
* the new radii and the new direction of the x axis. Scaling the two radii
|
|
291
|
+
* by the two factors is right only when the arc's axes lie along the
|
|
292
|
+
* network's axes.
|
|
293
|
+
*
|
|
294
|
+
* Exactly one negative factor mirrors the arc, which reverses the way round
|
|
295
|
+
* it is drawn, so the sweep flag flips; the large-arc flag stays, because the
|
|
296
|
+
* arc still covers the same share of its ellipse.
|
|
297
|
+
*
|
|
298
|
+
* A factor of exactly zero squashes the ellipse flat, and the model does not
|
|
299
|
+
* store an arc with a zero radius: the segment becomes a straight line
|
|
300
|
+
* between its two end points.
|
|
301
|
+
*
|
|
302
|
+
* The points box of vector edit mode has the same rule for any linear map
|
|
303
|
+
* (`transformEllipticalArc` in `packages/editor/src/vector-edit/point-box.ts`).
|
|
304
|
+
*/
|
|
305
|
+
function scaleEllipticalArc(arc, scale) {
|
|
306
|
+
if (scale.x === 0 || scale.y === 0) {
|
|
307
|
+
return { id: arc.id, kind: "line", start: arc.start, end: arc.end };
|
|
308
|
+
}
|
|
309
|
+
const radiusX = Math.abs(arc.radiusX);
|
|
310
|
+
const radiusY = Math.abs(arc.radiusY);
|
|
311
|
+
const turn = arc.xAxisRotation * Math.PI / 180;
|
|
312
|
+
const cos = Math.cos(turn);
|
|
313
|
+
const sin = Math.sin(turn);
|
|
314
|
+
// Where the scale sends the ellipse's two radius vectors.
|
|
315
|
+
const ux = radiusX * scale.x * cos;
|
|
316
|
+
const uy = radiusX * scale.y * sin;
|
|
317
|
+
const vx = -radiusY * scale.x * sin;
|
|
318
|
+
const vy = radiusY * scale.y * cos;
|
|
319
|
+
const uLength = Math.hypot(ux, uy);
|
|
320
|
+
const vLength = Math.hypot(vx, vy);
|
|
321
|
+
let nextX;
|
|
322
|
+
let nextY;
|
|
323
|
+
let angle;
|
|
324
|
+
if (Math.abs(ux * vx + uy * vy) <= 1e-12 * uLength * vLength) {
|
|
325
|
+
// The two images are still at right angles (one scale for both axes, or
|
|
326
|
+
// an arc whose axes lie along the network's axes): they are the new radii.
|
|
327
|
+
nextX = uLength;
|
|
328
|
+
nextY = vLength;
|
|
329
|
+
angle = Math.atan2(uy, ux);
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
// Otherwise the new axes are the principal directions of the image: the
|
|
333
|
+
// eigenvectors of the symmetric matrix [[p, q], [q, r]] that the two
|
|
334
|
+
// image vectors span.
|
|
335
|
+
const p = ux * ux + vx * vx;
|
|
336
|
+
const q = ux * uy + vx * vy;
|
|
337
|
+
const r = uy * uy + vy * vy;
|
|
338
|
+
const mean = (p + r) / 2;
|
|
339
|
+
const spread = Math.hypot((p - r) / 2, q);
|
|
340
|
+
const major = Math.sqrt(mean + spread);
|
|
341
|
+
const minor = Math.sqrt(Math.max(0, mean - spread));
|
|
342
|
+
const majorAngle = Math.atan2(2 * q, p - r) / 2;
|
|
343
|
+
// The old x radius stays the x radius when its image is nearer the long axis.
|
|
344
|
+
const alongMajor = ux * Math.cos(majorAngle) + uy * Math.sin(majorAngle);
|
|
345
|
+
const alongMinor = uy * Math.cos(majorAngle) - ux * Math.sin(majorAngle);
|
|
346
|
+
if (Math.abs(alongMajor) >= Math.abs(alongMinor)) {
|
|
347
|
+
nextX = major;
|
|
348
|
+
nextY = minor;
|
|
349
|
+
angle = majorAngle;
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
nextX = minor;
|
|
353
|
+
nextY = major;
|
|
354
|
+
angle = majorAngle + Math.PI / 2;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
// An axis direction repeats every half turn; take the one nearest the old value.
|
|
358
|
+
const degrees = angle * 180 / Math.PI;
|
|
359
|
+
const xAxisRotation = degrees - 180 * Math.round((degrees - arc.xAxisRotation) / 180);
|
|
360
|
+
if (![nextX, nextY, xAxisRotation].every(Number.isFinite)) {
|
|
361
|
+
throw new RangeError(`Scaling the elliptical arc ${arc.id} by ${String(scale.x)} and ${String(scale.y)} ` +
|
|
362
|
+
"gives radii outside the number range.");
|
|
363
|
+
}
|
|
364
|
+
if (nextX <= 0 || nextY <= 0) {
|
|
365
|
+
// A radius too small to store: the arc is flat, as for a zero factor.
|
|
366
|
+
return { id: arc.id, kind: "line", start: arc.start, end: arc.end };
|
|
367
|
+
}
|
|
368
|
+
return {
|
|
369
|
+
...arc,
|
|
370
|
+
radiusX: nextX,
|
|
371
|
+
radiusY: nextY,
|
|
372
|
+
xAxisRotation,
|
|
373
|
+
sweep: scale.x * scale.y < 0 ? !arc.sweep : arc.sweep,
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Scales a vector network in its own coordinate space around one anchor.
|
|
378
|
+
* An elliptical arc keeps its exact shape: see `scaleEllipticalArc`.
|
|
379
|
+
*/
|
|
380
|
+
export function scaleVectorNetwork(network, scale, anchor) {
|
|
381
|
+
if (![scale.x, scale.y, anchor.x, anchor.y].every(Number.isFinite)) {
|
|
382
|
+
throw new TypeError("Vector-network scale values must be finite.");
|
|
383
|
+
}
|
|
384
|
+
const vertices = network.vertices.map((vertex) => ({
|
|
385
|
+
...vertex,
|
|
386
|
+
x: anchor.x + (vertex.x - anchor.x) * scale.x,
|
|
387
|
+
y: anchor.y + (vertex.y - anchor.y) * scale.y,
|
|
388
|
+
}));
|
|
389
|
+
const segments = network.segments.map((segment) => {
|
|
390
|
+
if (segment.kind === "cubic") {
|
|
391
|
+
return {
|
|
392
|
+
...segment,
|
|
393
|
+
startHandle: {
|
|
394
|
+
...segment.startHandle,
|
|
395
|
+
dx: segment.startHandle.dx * scale.x,
|
|
396
|
+
dy: segment.startHandle.dy * scale.y,
|
|
397
|
+
},
|
|
398
|
+
endHandle: {
|
|
399
|
+
...segment.endHandle,
|
|
400
|
+
dx: segment.endHandle.dx * scale.x,
|
|
401
|
+
dy: segment.endHandle.dy * scale.y,
|
|
402
|
+
},
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
if (segment.kind === "elliptical-arc") {
|
|
406
|
+
return scaleEllipticalArc(segment, scale);
|
|
407
|
+
}
|
|
408
|
+
return segment;
|
|
409
|
+
});
|
|
410
|
+
return { ...network, vertices, segments };
|
|
411
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type SvgElementLike, type SvgImportOptions, type SvgImportRefusal, type SvgImportResult, type SvgParse } from "./public-types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The size an import of `markup` will give its root, read from the root
|
|
4
|
+
* start tag alone (`width`, `height` and `viewBox`, by the same rule the
|
|
5
|
+
* import uses), so it is known before the document is parsed. Null when the
|
|
6
|
+
* root is sized to its content (no usable width, height or viewBox, such as
|
|
7
|
+
* percentages with no viewBox) or the start tag cannot be read here; the
|
|
8
|
+
* import itself may still refuse a document this returns a size for.
|
|
9
|
+
*/
|
|
10
|
+
export declare function svgMarkupRootSize(markup: string): {
|
|
11
|
+
readonly width: number;
|
|
12
|
+
readonly height: number;
|
|
13
|
+
} | null;
|
|
14
|
+
/**
|
|
15
|
+
* Imports one SVG document from its markup: the common entry point. The
|
|
16
|
+
* byte cap is measured on the string before anything is parsed; the
|
|
17
|
+
* markup is parsed with `options.parseSvg` (the host passes its
|
|
18
|
+
* `DOMParser`). Markup that is not well-formed XML is refused with the code
|
|
19
|
+
* `malformed` and the parser's message, whether the parser throws or returns
|
|
20
|
+
* a browser's error page with a `parsererror` element, as `DOMParser` does.
|
|
21
|
+
* The same input with the same `idSource` always gives the same output, and
|
|
22
|
+
* a refusal means nothing was imported.
|
|
23
|
+
*/
|
|
24
|
+
export declare function importSvgMarkup(svgText: string, options: SvgImportOptions & {
|
|
25
|
+
readonly parseSvg: SvgParse;
|
|
26
|
+
}): SvgImportResult | SvgImportRefusal;
|
|
27
|
+
/**
|
|
28
|
+
* Imports one SVG document from an element tree the host already parsed — a
|
|
29
|
+
* browser `DOMParser` result or a test's own parse. A browser's error page
|
|
30
|
+
* (a `parsererror` root, or one inside a generated `html` page) is refused
|
|
31
|
+
* as `malformed`. The byte cap is measured approximately on the tree
|
|
32
|
+
* (element names, attributes and text). The same input with the same
|
|
33
|
+
* `idSource` always gives the same output, and a refusal means nothing was
|
|
34
|
+
* imported.
|
|
35
|
+
*/
|
|
36
|
+
export declare function importSvg(root: SvgElementLike, options: SvgImportOptions): SvgImportResult | SvgImportRefusal;
|