@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,125 @@
|
|
|
1
|
+
const NUMBER = "[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?";
|
|
2
|
+
const NUMBER_RE = new RegExp(`^${NUMBER}$`);
|
|
3
|
+
const LENGTH_RE = new RegExp(`^(${NUMBER})([a-zA-Z%]*)$`);
|
|
4
|
+
const SEPARATOR_RE = /\s*,\s*|\s+/;
|
|
5
|
+
const POINT_NUMBER_RE = new RegExp(NUMBER, "y");
|
|
6
|
+
const POINT_SPACE_RE = /\s*/y;
|
|
7
|
+
/** Multipliers from absolute CSS units to px. */
|
|
8
|
+
const ABSOLUTE_UNIT_PX = {
|
|
9
|
+
"": 1,
|
|
10
|
+
px: 1,
|
|
11
|
+
pt: 96 / 72,
|
|
12
|
+
pc: 16,
|
|
13
|
+
in: 96,
|
|
14
|
+
cm: 96 / 2.54,
|
|
15
|
+
mm: 96 / 25.4,
|
|
16
|
+
q: 96 / 101.6,
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Parses one SVG length into px. Unitless and `px` are 1:1; `pt` ×96/72,
|
|
20
|
+
* `pc` ×16, `in` ×96, `cm` ×96/2.54, `mm` ×96/25.4, `q` ×96/101.6;
|
|
21
|
+
* `em` ×fontSize, `ex` ×fontSize/2 and `rem` ×rootFontSize (from
|
|
22
|
+
* `context`; `rem` only when the context has the root size). Units are read in
|
|
23
|
+
* any case. A trailing `%` yields a percent
|
|
24
|
+
* fraction and never touches the context. Returns null for empty text,
|
|
25
|
+
* junk, other units, or a non-finite number — the caller reports that;
|
|
26
|
+
* nothing is guessed.
|
|
27
|
+
*/
|
|
28
|
+
export function parseSvgLength(text, context) {
|
|
29
|
+
const match = LENGTH_RE.exec(text.trim());
|
|
30
|
+
if (match === null) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const numericText = match[1];
|
|
34
|
+
// Unit identifiers are ASCII case-insensitive (CSS Values 3, Dimensions).
|
|
35
|
+
const unit = match[2]?.toLowerCase();
|
|
36
|
+
if (numericText === undefined || unit === undefined) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const value = Number(numericText);
|
|
40
|
+
if (!Number.isFinite(value)) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
if (unit === "%") {
|
|
44
|
+
return { kind: "percent", fraction: value / 100 };
|
|
45
|
+
}
|
|
46
|
+
const unitPx = ABSOLUTE_UNIT_PX[unit];
|
|
47
|
+
if (unitPx !== undefined) {
|
|
48
|
+
return { kind: "absolute", value: value * unitPx };
|
|
49
|
+
}
|
|
50
|
+
if (unit === "em") {
|
|
51
|
+
return { kind: "absolute", value: value * context.fontSize };
|
|
52
|
+
}
|
|
53
|
+
if (unit === "ex") {
|
|
54
|
+
return { kind: "absolute", value: (value * context.fontSize) / 2 };
|
|
55
|
+
}
|
|
56
|
+
if (unit === "rem" && context.rootFontSize !== undefined) {
|
|
57
|
+
return { kind: "absolute", value: value * context.rootFontSize };
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
/** Parses a plain SVG number (optional sign, decimals, exponent). Null on junk or non-finite. */
|
|
62
|
+
export function parseSvgNumber(text) {
|
|
63
|
+
const trimmed = text.trim();
|
|
64
|
+
if (!NUMBER_RE.test(trimmed)) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
const value = Number(trimmed);
|
|
68
|
+
return Number.isFinite(value) ? value : null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Splits a comma/whitespace-separated number list, as `stroke-dasharray`
|
|
72
|
+
* and `viewBox` use. Returns null when any entry fails
|
|
73
|
+
* to parse or the list is empty; never returns a partial list.
|
|
74
|
+
*/
|
|
75
|
+
export function parseSvgNumberList(text) {
|
|
76
|
+
const trimmed = text.trim();
|
|
77
|
+
if (trimmed === "") {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const values = [];
|
|
81
|
+
for (const entry of trimmed.split(SEPARATOR_RE)) {
|
|
82
|
+
const value = parseSvgNumber(entry);
|
|
83
|
+
if (value === null) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
values.push(value);
|
|
87
|
+
}
|
|
88
|
+
return values;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Reads a `points` list by the SVG points grammar: numbers are split by
|
|
92
|
+
* whitespace, at most one comma, or nothing when the next number starts with
|
|
93
|
+
* a sign or a second dot (`10-20` is 10 then -20, `1.5.5` is 1.5 then 0.5),
|
|
94
|
+
* as path data allows. Returns null when the list is empty, has an empty
|
|
95
|
+
* entry or a trailing comma, or holds anything that is not a number.
|
|
96
|
+
*/
|
|
97
|
+
export function parsePointList(text) {
|
|
98
|
+
const values = [];
|
|
99
|
+
const skipSpace = (from) => {
|
|
100
|
+
POINT_SPACE_RE.lastIndex = from;
|
|
101
|
+
POINT_SPACE_RE.exec(text);
|
|
102
|
+
return POINT_SPACE_RE.lastIndex;
|
|
103
|
+
};
|
|
104
|
+
let index = skipSpace(0);
|
|
105
|
+
if (index === text.length)
|
|
106
|
+
return null;
|
|
107
|
+
for (;;) {
|
|
108
|
+
POINT_NUMBER_RE.lastIndex = index;
|
|
109
|
+
const match = POINT_NUMBER_RE.exec(text);
|
|
110
|
+
if (match === null)
|
|
111
|
+
return null;
|
|
112
|
+
const value = Number(match[0]);
|
|
113
|
+
if (!Number.isFinite(value))
|
|
114
|
+
return null;
|
|
115
|
+
values.push(value);
|
|
116
|
+
index = skipSpace(POINT_NUMBER_RE.lastIndex);
|
|
117
|
+
if (index === text.length)
|
|
118
|
+
return values;
|
|
119
|
+
if (text[index] === ",") {
|
|
120
|
+
index = skipSpace(index + 1);
|
|
121
|
+
if (index === text.length)
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Matrix } from "../transform/matrix.ts";
|
|
2
|
+
/** A parsed `viewBox`: the rectangle user coordinates map from. */
|
|
3
|
+
export interface ViewBox {
|
|
4
|
+
readonly minX: number;
|
|
5
|
+
readonly minY: number;
|
|
6
|
+
readonly width: number;
|
|
7
|
+
readonly height: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Parses a `viewBox` attribute: four numbers, with a positive width and
|
|
11
|
+
* height. Returns null when the text is not four numbers; a zero or
|
|
12
|
+
* negative size is a valid parse that renders nothing, which SVG defines
|
|
13
|
+
* and the caller handles.
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseViewBox(text: string): ViewBox | null;
|
|
16
|
+
/** How content fits a viewport, from `preserveAspectRatio`. */
|
|
17
|
+
export interface PreserveAspectRatio {
|
|
18
|
+
readonly align: "none" | "xMinYMin" | "xMinYMid" | "xMinYMax" | "xMidYMin" | "xMidYMid" | "xMidYMax" | "xMaxYMin" | "xMaxYMid" | "xMaxYMax";
|
|
19
|
+
readonly meetOrSlice: "meet" | "slice";
|
|
20
|
+
}
|
|
21
|
+
/** The default: scaled to fit, centered. */
|
|
22
|
+
export declare const DEFAULT_PRESERVE_ASPECT_RATIO: PreserveAspectRatio;
|
|
23
|
+
/**
|
|
24
|
+
* Parses `preserveAspectRatio`: an align value plus an optional `meet` or
|
|
25
|
+
* `slice`, after SVG 1.1's optional `defer` token, which browsers skip.
|
|
26
|
+
* Returns null on text the SVG grammar does not allow; an empty attribute is
|
|
27
|
+
* the default.
|
|
28
|
+
*/
|
|
29
|
+
export declare function parsePreserveAspectRatio(text: string): PreserveAspectRatio | null;
|
|
30
|
+
/**
|
|
31
|
+
* The transform a `viewBox` plus `preserveAspectRatio` puts on the
|
|
32
|
+
* children of a viewport `width` × `height`: user coordinates map into the
|
|
33
|
+
* viewport, scaled and placed as the alignment says.
|
|
34
|
+
*/
|
|
35
|
+
export declare function viewBoxTransform(viewBox: ViewBox, width: number, height: number, aspectRatio: PreserveAspectRatio): Matrix;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { parseSvgNumberList } from "./length.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parses a `viewBox` attribute: four numbers, with a positive width and
|
|
4
|
+
* height. Returns null when the text is not four numbers; a zero or
|
|
5
|
+
* negative size is a valid parse that renders nothing, which SVG defines
|
|
6
|
+
* and the caller handles.
|
|
7
|
+
*/
|
|
8
|
+
export function parseViewBox(text) {
|
|
9
|
+
const numbers = parseSvgNumberList(text);
|
|
10
|
+
if (numbers === null || numbers.length !== 4)
|
|
11
|
+
return null;
|
|
12
|
+
const [minX, minY, width, height] = numbers;
|
|
13
|
+
return { minX, minY, width, height };
|
|
14
|
+
}
|
|
15
|
+
/** The default: scaled to fit, centered. */
|
|
16
|
+
export const DEFAULT_PRESERVE_ASPECT_RATIO = {
|
|
17
|
+
align: "xMidYMid",
|
|
18
|
+
meetOrSlice: "meet",
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Parses `preserveAspectRatio`: an align value plus an optional `meet` or
|
|
22
|
+
* `slice`, after SVG 1.1's optional `defer` token, which browsers skip.
|
|
23
|
+
* Returns null on text the SVG grammar does not allow; an empty attribute is
|
|
24
|
+
* the default.
|
|
25
|
+
*/
|
|
26
|
+
export function parsePreserveAspectRatio(text) {
|
|
27
|
+
const words = text.trim().split(/\s+/).filter((part) => part.length > 0);
|
|
28
|
+
const parts = words[0] === "defer" ? words.slice(1) : words;
|
|
29
|
+
if (words.length > 0 && parts.length === 0)
|
|
30
|
+
return null;
|
|
31
|
+
if (parts.length === 0)
|
|
32
|
+
return DEFAULT_PRESERVE_ASPECT_RATIO;
|
|
33
|
+
if (parts.length > 2)
|
|
34
|
+
return null;
|
|
35
|
+
const align = parts[0];
|
|
36
|
+
if (align !== "none" && align !== "xMinYMin" && align !== "xMinYMid" && align !== "xMinYMax" &&
|
|
37
|
+
align !== "xMidYMin" && align !== "xMidYMid" && align !== "xMidYMax" &&
|
|
38
|
+
align !== "xMaxYMin" && align !== "xMaxYMid" && align !== "xMaxYMax") {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const meetOrSlice = parts[1] ?? "meet";
|
|
42
|
+
if (meetOrSlice !== "meet" && meetOrSlice !== "slice")
|
|
43
|
+
return null;
|
|
44
|
+
// With align none the meet or slice keyword is allowed and has no effect.
|
|
45
|
+
return { align, meetOrSlice: align === "none" ? "meet" : meetOrSlice };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The transform a `viewBox` plus `preserveAspectRatio` puts on the
|
|
49
|
+
* children of a viewport `width` × `height`: user coordinates map into the
|
|
50
|
+
* viewport, scaled and placed as the alignment says.
|
|
51
|
+
*/
|
|
52
|
+
export function viewBoxTransform(viewBox, width, height, aspectRatio) {
|
|
53
|
+
const scaleX = width / viewBox.width;
|
|
54
|
+
const scaleY = height / viewBox.height;
|
|
55
|
+
if (aspectRatio.align === "none") {
|
|
56
|
+
return [scaleX, 0, 0, scaleY, -viewBox.minX * scaleX, -viewBox.minY * scaleY];
|
|
57
|
+
}
|
|
58
|
+
const scale = aspectRatio.meetOrSlice === "meet"
|
|
59
|
+
? Math.min(scaleX, scaleY)
|
|
60
|
+
: Math.max(scaleX, scaleY);
|
|
61
|
+
const extraX = width - viewBox.width * scale;
|
|
62
|
+
const extraY = height - viewBox.height * scale;
|
|
63
|
+
const alignX = aspectRatio.align.startsWith("xMin")
|
|
64
|
+
? 0
|
|
65
|
+
: aspectRatio.align.startsWith("xMid") ? extraX / 2 : extraX;
|
|
66
|
+
const alignY = aspectRatio.align.endsWith("YMin")
|
|
67
|
+
? 0
|
|
68
|
+
: aspectRatio.align.endsWith("YMid") ? extraY / 2 : extraY;
|
|
69
|
+
return [
|
|
70
|
+
scale,
|
|
71
|
+
0,
|
|
72
|
+
0,
|
|
73
|
+
scale,
|
|
74
|
+
alignX - viewBox.minX * scale,
|
|
75
|
+
alignY - viewBox.minY * scale,
|
|
76
|
+
];
|
|
77
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moondraft/import",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Editable SVG and Figma import for Moondraft.",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./svg": {
|
|
14
|
+
"types": "./dist/svg.d.ts",
|
|
15
|
+
"default": "./dist/svg.js"
|
|
16
|
+
},
|
|
17
|
+
"./figma": {
|
|
18
|
+
"types": "./dist/figma.d.ts",
|
|
19
|
+
"default": "./dist/figma.js"
|
|
20
|
+
},
|
|
21
|
+
"./adapter": {
|
|
22
|
+
"types": "./dist/adapter/index.d.ts",
|
|
23
|
+
"default": "./dist/adapter/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc -b",
|
|
28
|
+
"prepack": "node scripts/check-pack.mjs",
|
|
29
|
+
"sync-taco": "node scripts/sync-taco.mjs"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@chenglou/pretext": "0.0.8",
|
|
33
|
+
"entities": "6.0.1",
|
|
34
|
+
"fflate": "0.8.3",
|
|
35
|
+
"fzstd": "0.1.1",
|
|
36
|
+
"svg-pathdata": "9.0.0"
|
|
37
|
+
},
|
|
38
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/chengluyu/design-core-renderer-rust-k3.git",
|
|
42
|
+
"directory": "packages/import"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist",
|
|
46
|
+
"LICENSE",
|
|
47
|
+
"SOURCE.md",
|
|
48
|
+
"README.md",
|
|
49
|
+
"CHANGELOG.md"
|
|
50
|
+
],
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@moondraft/editor": ">=0.4.0 <0.5.0"
|
|
56
|
+
}
|
|
57
|
+
}
|