@moondraft/import 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +169 -0
- package/dist/adapter/convert.d.ts +122 -0
- package/dist/adapter/convert.js +2302 -0
- package/dist/adapter/figma.d.ts +9 -0
- package/dist/adapter/figma.js +30 -0
- package/dist/adapter/file-components.d.ts +14 -0
- package/dist/adapter/file-components.js +262 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +562 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +45 -0
- package/dist/adapter/link.js +688 -0
- package/dist/adapter/payload.d.ts +31 -0
- package/dist/adapter/payload.js +78 -0
- package/dist/adapter/shapes.d.ts +31 -0
- package/dist/adapter/shapes.js +75 -0
- package/dist/adapter/variables.d.ts +44 -0
- package/dist/adapter/variables.js +103 -0
- package/dist/figma-file.d.ts +38 -0
- package/dist/figma-file.js +97 -0
- package/dist/figma-worker.d.ts +11 -0
- package/dist/figma-worker.js +12 -0
- package/dist/figma.d.ts +26 -0
- package/dist/figma.js +96 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +49 -0
- package/dist/report.d.ts +15 -0
- package/dist/report.js +69 -0
- package/dist/svg.d.ts +33 -0
- package/dist/svg.js +44 -0
- package/dist/syntax.d.ts +8 -0
- package/dist/syntax.js +19 -0
- package/dist/taco/figma-import/clipboard.d.ts +25 -0
- package/dist/taco/figma-import/clipboard.js +87 -0
- package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
- package/dist/taco/figma-import/clipboardSize.js +107 -0
- package/dist/taco/figma-import/container/compression.d.ts +9 -0
- package/dist/taco/figma-import/container/compression.js +174 -0
- package/dist/taco/figma-import/container/error.d.ts +15 -0
- package/dist/taco/figma-import/container/error.js +19 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +38 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
- package/dist/taco/figma-import/container/limits.d.ts +22 -0
- package/dist/taco/figma-import/container/limits.js +22 -0
- package/dist/taco/figma-import/container/zip.d.ts +14 -0
- package/dist/taco/figma-import/container/zip.js +154 -0
- package/dist/taco/figma-import/figFile.d.ts +34 -0
- package/dist/taco/figma-import/figFile.js +91 -0
- package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
- package/dist/taco/figma-import/figFileDocument.js +100 -0
- package/dist/taco/figma-import/import/blobs.d.ts +10 -0
- package/dist/taco/figma-import/import/blobs.js +23 -0
- package/dist/taco/figma-import/import/carry.d.ts +11 -0
- package/dist/taco/figma-import/import/carry.js +33 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +84 -0
- package/dist/taco/figma-import/import/componentProps.js +313 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +83 -0
- package/dist/taco/figma-import/import/componentSets.js +647 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
- package/dist/taco/figma-import/import/converters/effects.js +127 -0
- package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
- package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +55 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +72 -0
- package/dist/taco/figma-import/import/converters/gradient.js +106 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/group.js +39 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +83 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +34 -0
- package/dist/taco/figma-import/import/converters/layout.js +168 -0
- package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
- package/dist/taco/figma-import/import/converters/line.js +40 -0
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
- package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
- package/dist/taco/figma-import/import/converters/section.d.ts +9 -0
- package/dist/taco/figma-import/import/converters/section.js +35 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +68 -0
- package/dist/taco/figma-import/import/converters/shared.js +362 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +41 -0
- package/dist/taco/figma-import/import/converters/text.js +576 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +84 -0
- package/dist/taco/figma-import/import/converters/vector.js +468 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +98 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +385 -0
- package/dist/taco/figma-import/import/fields.d.ts +15 -0
- package/dist/taco/figma-import/import/fields.js +29 -0
- package/dist/taco/figma-import/import/instances.d.ts +163 -0
- package/dist/taco/figma-import/import/instances.js +1323 -0
- package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
- package/dist/taco/figma-import/import/nestingCycles.js +488 -0
- package/dist/taco/figma-import/import/slots.d.ts +56 -0
- package/dist/taco/figma-import/import/slots.js +96 -0
- package/dist/taco/figma-import/import/styles.d.ts +46 -0
- package/dist/taco/figma-import/import/styles.js +42 -0
- package/dist/taco/figma-import/import/tree.d.ts +66 -0
- package/dist/taco/figma-import/import/tree.js +194 -0
- package/dist/taco/figma-import/import/types.d.ts +232 -0
- package/dist/taco/figma-import/import/types.js +33 -0
- package/dist/taco/figma-import/import/variables.d.ts +136 -0
- package/dist/taco/figma-import/import/variables.js +762 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +480 -0
- package/dist/taco/figma-import/import/walk.d.ts +159 -0
- package/dist/taco/figma-import/import/walk.js +1297 -0
- package/dist/taco/figma-import/index.d.ts +22 -0
- package/dist/taco/figma-import/index.js +13 -0
- package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
- package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
- package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
- package/dist/taco/figma-import/kiwi/error.js +29 -0
- package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
- package/dist/taco/figma-import/kiwi/limits.js +18 -0
- package/dist/taco/figma-import/kiwi/message.d.ts +10 -0
- package/dist/taco/figma-import/kiwi/message.js +145 -0
- package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
- package/dist/taco/figma-import/kiwi/schema.js +206 -0
- package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
- package/dist/taco/figma-import/kiwi/types.js +18 -0
- package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
- package/dist/taco/model/agent-hidden-fields.js +29 -0
- package/dist/taco/model/asset-users.d.ts +23 -0
- package/dist/taco/model/asset-users.js +128 -0
- package/dist/taco/model/auto-layout-lines.d.ts +32 -0
- package/dist/taco/model/auto-layout-lines.js +70 -0
- package/dist/taco/model/codec.d.ts +444 -0
- package/dist/taco/model/codec.js +5635 -0
- package/dist/taco/model/color.d.ts +11 -0
- package/dist/taco/model/color.js +61 -0
- package/dist/taco/model/comment-operations.d.ts +102 -0
- package/dist/taco/model/comment-operations.js +227 -0
- package/dist/taco/model/component-evaluation.d.ts +94 -0
- package/dist/taco/model/component-evaluation.js +548 -0
- package/dist/taco/model/component-expressions.d.ts +242 -0
- package/dist/taco/model/component-expressions.js +1082 -0
- package/dist/taco/model/component-properties.d.ts +111 -0
- package/dist/taco/model/component-properties.js +339 -0
- package/dist/taco/model/component-samples.d.ts +83 -0
- package/dist/taco/model/component-samples.js +290 -0
- package/dist/taco/model/component-validity.d.ts +85 -0
- package/dist/taco/model/component-validity.js +397 -0
- package/dist/taco/model/components.d.ts +155 -0
- package/dist/taco/model/components.js +505 -0
- package/dist/taco/model/connector-ends.d.ts +20 -0
- package/dist/taco/model/connector-ends.js +188 -0
- package/dist/taco/model/connector-geometry.d.ts +74 -0
- package/dist/taco/model/connector-geometry.js +293 -0
- package/dist/taco/model/corner-radii.d.ts +26 -0
- package/dist/taco/model/corner-radii.js +47 -0
- package/dist/taco/model/derivations.d.ts +81 -0
- package/dist/taco/model/derivations.js +151 -0
- package/dist/taco/model/export-settings.d.ts +53 -0
- package/dist/taco/model/export-settings.js +156 -0
- package/dist/taco/model/expressions/ast.d.ts +153 -0
- package/dist/taco/model/expressions/ast.js +6 -0
- package/dist/taco/model/expressions/cache.d.ts +23 -0
- package/dist/taco/model/expressions/cache.js +43 -0
- package/dist/taco/model/expressions/check.d.ts +72 -0
- package/dist/taco/model/expressions/check.js +517 -0
- package/dist/taco/model/expressions/completion.d.ts +61 -0
- package/dist/taco/model/expressions/completion.js +421 -0
- package/dist/taco/model/expressions/evaluate.d.ts +32 -0
- package/dist/taco/model/expressions/evaluate.js +519 -0
- package/dist/taco/model/expressions/forms.d.ts +84 -0
- package/dist/taco/model/expressions/forms.js +200 -0
- package/dist/taco/model/expressions/index.d.ts +14 -0
- package/dist/taco/model/expressions/index.js +17 -0
- package/dist/taco/model/expressions/lexer.d.ts +42 -0
- package/dist/taco/model/expressions/lexer.js +213 -0
- package/dist/taco/model/expressions/library.d.ts +33 -0
- package/dist/taco/model/expressions/library.js +117 -0
- package/dist/taco/model/expressions/names.d.ts +43 -0
- package/dist/taco/model/expressions/names.js +99 -0
- package/dist/taco/model/expressions/parser.d.ts +15 -0
- package/dist/taco/model/expressions/parser.js +438 -0
- package/dist/taco/model/expressions/rename.d.ts +34 -0
- package/dist/taco/model/expressions/rename.js +46 -0
- package/dist/taco/model/expressions/source.d.ts +15 -0
- package/dist/taco/model/expressions/source.js +161 -0
- package/dist/taco/model/expressions/types.d.ts +119 -0
- package/dist/taco/model/expressions/types.js +102 -0
- package/dist/taco/model/expressions/values.d.ts +97 -0
- package/dist/taco/model/expressions/values.js +160 -0
- package/dist/taco/model/geometry-limits.d.ts +16 -0
- package/dist/taco/model/geometry-limits.js +67 -0
- package/dist/taco/model/geometry.d.ts +47 -0
- package/dist/taco/model/geometry.js +193 -0
- package/dist/taco/model/group-bounds.d.ts +38 -0
- package/dist/taco/model/group-bounds.js +144 -0
- package/dist/taco/model/id-table.d.ts +37 -0
- package/dist/taco/model/id-table.js +80 -0
- package/dist/taco/model/index.d.ts +25 -0
- package/dist/taco/model/index.js +27 -0
- package/dist/taco/model/instance-resolver.d.ts +372 -0
- package/dist/taco/model/instance-resolver.js +1748 -0
- package/dist/taco/model/layer-syntax.d.ts +340 -0
- package/dist/taco/model/layer-syntax.js +3506 -0
- package/dist/taco/model/layout/constraints.d.ts +84 -0
- package/dist/taco/model/layout/constraints.js +445 -0
- package/dist/taco/model/layout/frame-layout.d.ts +46 -0
- package/dist/taco/model/layout/frame-layout.js +618 -0
- package/dist/taco/model/layout/node-sizing.d.ts +163 -0
- package/dist/taco/model/layout/node-sizing.js +486 -0
- package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
- package/dist/taco/model/legacy-connector-caps.js +70 -0
- package/dist/taco/model/linear-call.d.ts +48 -0
- package/dist/taco/model/linear-call.js +58 -0
- package/dist/taco/model/new-nodes.d.ts +145 -0
- package/dist/taco/model/new-nodes.js +277 -0
- package/dist/taco/model/node-builder.d.ts +449 -0
- package/dist/taco/model/node-builder.js +2294 -0
- package/dist/taco/model/node-table-watch.d.ts +36 -0
- package/dist/taco/model/node-table-watch.js +63 -0
- package/dist/taco/model/node-transform.d.ts +39 -0
- package/dist/taco/model/node-transform.js +79 -0
- package/dist/taco/model/node-users-index.d.ts +10 -0
- package/dist/taco/model/node-users-index.js +82 -0
- package/dist/taco/model/operation-references.d.ts +80 -0
- package/dist/taco/model/operation-references.js +399 -0
- package/dist/taco/model/operations.d.ts +610 -0
- package/dist/taco/model/operations.js +2882 -0
- package/dist/taco/model/overlay-table.d.ts +71 -0
- package/dist/taco/model/overlay-table.js +171 -0
- package/dist/taco/model/override-remap.d.ts +69 -0
- package/dist/taco/model/override-remap.js +230 -0
- package/dist/taco/model/parent-table.d.ts +53 -0
- package/dist/taco/model/parent-table.js +193 -0
- package/dist/taco/model/problem-collector.d.ts +27 -0
- package/dist/taco/model/problem-collector.js +101 -0
- package/dist/taco/model/sha256.d.ts +10 -0
- package/dist/taco/model/sha256.js +74 -0
- package/dist/taco/model/shape-geometry.d.ts +96 -0
- package/dist/taco/model/shape-geometry.js +368 -0
- package/dist/taco/model/shared-node-table.d.ts +97 -0
- package/dist/taco/model/shared-node-table.js +546 -0
- package/dist/taco/model/slots.d.ts +113 -0
- package/dist/taco/model/slots.js +281 -0
- package/dist/taco/model/stroke-settings.d.ts +37 -0
- package/dist/taco/model/stroke-settings.js +90 -0
- package/dist/taco/model/svg-path-import.d.ts +89 -0
- package/dist/taco/model/svg-path-import.js +219 -0
- package/dist/taco/model/text-measurement.d.ts +142 -0
- package/dist/taco/model/text-measurement.js +846 -0
- package/dist/taco/model/text-reads.d.ts +66 -0
- package/dist/taco/model/text-reads.js +128 -0
- package/dist/taco/model/text-sizer.d.ts +358 -0
- package/dist/taco/model/text-sizer.js +996 -0
- package/dist/taco/model/theme-users.d.ts +59 -0
- package/dist/taco/model/theme-users.js +312 -0
- package/dist/taco/model/thumbnail.d.ts +15 -0
- package/dist/taco/model/thumbnail.js +34 -0
- package/dist/taco/model/types.d.ts +2347 -0
- package/dist/taco/model/types.js +366 -0
- package/dist/taco/model/unknown-property.d.ts +5 -0
- package/dist/taco/model/unknown-property.js +7 -0
- package/dist/taco/model/validated-documents.d.ts +22 -0
- package/dist/taco/model/validated-documents.js +22 -0
- package/dist/taco/model/variable-modes.d.ts +61 -0
- package/dist/taco/model/variable-modes.js +438 -0
- package/dist/taco/model/variable-references.d.ts +54 -0
- package/dist/taco/model/variable-references.js +183 -0
- package/dist/taco/model/variables.d.ts +24 -0
- package/dist/taco/model/variables.js +151 -0
- package/dist/taco/model/vector-network.d.ts +34 -0
- package/dist/taco/model/vector-network.js +411 -0
- package/dist/taco/svg-import/import-svg.d.ts +36 -0
- package/dist/taco/svg-import/import-svg.js +729 -0
- package/dist/taco/svg-import/index.d.ts +3 -0
- package/dist/taco/svg-import/index.js +3 -0
- package/dist/taco/svg-import/limits/limits.d.ts +79 -0
- package/dist/taco/svg-import/limits/limits.js +160 -0
- package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
- package/dist/taco/svg-import/paint/gradients.js +429 -0
- package/dist/taco/svg-import/paint/paint.d.ts +32 -0
- package/dist/taco/svg-import/paint/paint.js +162 -0
- package/dist/taco/svg-import/paths/paths.d.ts +58 -0
- package/dist/taco/svg-import/paths/paths.js +205 -0
- package/dist/taco/svg-import/public-types.d.ts +279 -0
- package/dist/taco/svg-import/public-types.js +56 -0
- package/dist/taco/svg-import/report/report.d.ts +44 -0
- package/dist/taco/svg-import/report/report.js +79 -0
- package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
- package/dist/taco/svg-import/reuse/reuse.js +840 -0
- package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
- package/dist/taco/svg-import/shapes/shapes.js +386 -0
- package/dist/taco/svg-import/structure/effects.d.ts +26 -0
- package/dist/taco/svg-import/structure/effects.js +618 -0
- package/dist/taco/svg-import/structure/image.d.ts +24 -0
- package/dist/taco/svg-import/structure/image.js +495 -0
- package/dist/taco/svg-import/structure/walk.d.ts +85 -0
- package/dist/taco/svg-import/structure/walk.js +544 -0
- package/dist/taco/svg-import/style/color.d.ts +40 -0
- package/dist/taco/svg-import/style/color.js +275 -0
- package/dist/taco/svg-import/style/computed.d.ts +214 -0
- package/dist/taco/svg-import/style/computed.js +1534 -0
- package/dist/taco/svg-import/style/css.d.ts +127 -0
- package/dist/taco/svg-import/style/css.js +541 -0
- package/dist/taco/svg-import/style/media.d.ts +25 -0
- package/dist/taco/svg-import/style/media.js +307 -0
- package/dist/taco/svg-import/style/properties.d.ts +43 -0
- package/dist/taco/svg-import/style/properties.js +135 -0
- package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
- package/dist/taco/svg-import/style/selector-reach.js +169 -0
- package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
- package/dist/taco/svg-import/style/selector-validity.js +307 -0
- package/dist/taco/svg-import/text/text.d.ts +29 -0
- package/dist/taco/svg-import/text/text.js +545 -0
- package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
- package/dist/taco/svg-import/transform/matrix.js +229 -0
- package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
- package/dist/taco/svg-import/tree/element-data.js +155 -0
- package/dist/taco/svg-import/tree/elements.d.ts +36 -0
- package/dist/taco/svg-import/tree/elements.js +121 -0
- package/dist/taco/svg-import/units/length.d.ts +43 -0
- package/dist/taco/svg-import/units/length.js +125 -0
- package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
- package/dist/taco/svg-import/units/viewbox.js +77 -0
- package/package.json +54 -4
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type BlendMode, type NodeId, type PathNode, type Rect, type VectorNetwork } from "../../model/index.ts";
|
|
2
|
+
import type { SvgElementLike } from "../public-types.ts";
|
|
3
|
+
import { type DecomposedMatrix, type Matrix } from "../transform/matrix.ts";
|
|
4
|
+
import type { ComputedStyle } from "../style/computed.ts";
|
|
5
|
+
import { type PaintContext } from "../paint/paint.ts";
|
|
6
|
+
/** What a layer carries from its computed style besides its paints. */
|
|
7
|
+
export interface LayerChrome {
|
|
8
|
+
readonly opacity: number;
|
|
9
|
+
readonly visible: boolean;
|
|
10
|
+
readonly blendMode: BlendMode | null;
|
|
11
|
+
}
|
|
12
|
+
/** Reads the node-level fields off a computed style. */
|
|
13
|
+
export declare function layerChrome(computed: ComputedStyle): LayerChrome;
|
|
14
|
+
/** The split of a layer's full matrix into what it keeps and what bakes. */
|
|
15
|
+
export interface LayerPlacement {
|
|
16
|
+
readonly parts: DecomposedMatrix;
|
|
17
|
+
/** The rotation and translation the layer keeps, as a matrix. */
|
|
18
|
+
readonly kept: Matrix;
|
|
19
|
+
/** The linear remainder baked into geometry, as a matrix (identity for a rigid transform). */
|
|
20
|
+
readonly bake: Matrix;
|
|
21
|
+
/** The factor stroke geometry scales by. */
|
|
22
|
+
readonly strokeScale: number;
|
|
23
|
+
/** True when the bake scales the axes differently, so stroke widths are approximate. */
|
|
24
|
+
readonly strokeApproximated: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Splits an element's full matrix. A degenerate matrix collapses the layer
|
|
28
|
+
* and returns null; the caller reports it and skips the element.
|
|
29
|
+
*/
|
|
30
|
+
export declare function layerPlacement(matrix: Matrix): LayerPlacement | null;
|
|
31
|
+
/**
|
|
32
|
+
* Bakes a linear map into a network's coordinates: vertices move, cubic
|
|
33
|
+
* handles follow, and arc segments scale exactly under a uniform scale
|
|
34
|
+
* (mirrors flip their sweep and reflect their axis angle) or take the
|
|
35
|
+
* mapped ellipse's radii and axis under anything else.
|
|
36
|
+
*/
|
|
37
|
+
export declare function bakeVectorNetwork(network: VectorNetwork, bake: Matrix): VectorNetwork;
|
|
38
|
+
/**
|
|
39
|
+
* How many numbers and commands a path's data holds, counted in one scan
|
|
40
|
+
* without parsing it: a number starts at a digit, or at a dot not inside a
|
|
41
|
+
* number, an exponent's digits belong to its number, and every other letter
|
|
42
|
+
* is one command. Every segment carries at least one number or command
|
|
43
|
+
* (a closepath carries no number), so the count bounds the commands,
|
|
44
|
+
* vertices and segments the data makes.
|
|
45
|
+
*/
|
|
46
|
+
export declare function countPathNumbers(pathData: string): number;
|
|
47
|
+
/**
|
|
48
|
+
* Builds a path layer from path data: parses and validates it through the
|
|
49
|
+
* model's converter, bakes the transform's scale and skew into the
|
|
50
|
+
* geometry, keeps rotation and translation on the node, and resolves its
|
|
51
|
+
* style. Returns null when the converter refuses the data; the reason is
|
|
52
|
+
* reported against the element's path. `numbersCounted` says the caller
|
|
53
|
+
* already counted the data's numbers against the cap, before building it.
|
|
54
|
+
*/
|
|
55
|
+
export declare function pathLayerFromData(id: NodeId, name: string, pathData: string, computed: ComputedStyle, placement: LayerPlacement, elementPath: string, element: SvgElementLike, ctx: PaintContext, numbersCounted?: boolean): {
|
|
56
|
+
readonly node: PathNode;
|
|
57
|
+
readonly localBounds: Rect;
|
|
58
|
+
} | null;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { getNodeBounds, ModelError, pathNodeFromSvgPath, } from "../../model/index.js";
|
|
2
|
+
import { applyMatrixToPoint, applyMatrixToVector, decomposeMatrix, matrixIsDegenerate, nearZero, negligibleNextTo, remainderIsIdentity, remainderIsUniformScale, remainderMatrix, remainderStrokeScale, rotationMatrix, sameScale, } from "../transform/matrix.js";
|
|
3
|
+
import { visualStyleFromComputed } from "../paint/paint.js";
|
|
4
|
+
/** Reads the node-level fields off a computed style. */
|
|
5
|
+
export function layerChrome(computed) {
|
|
6
|
+
return {
|
|
7
|
+
opacity: computed.opacity,
|
|
8
|
+
visible: computed.visibility === "visible",
|
|
9
|
+
blendMode: computed.blendMode,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Splits an element's full matrix. A degenerate matrix collapses the layer
|
|
14
|
+
* and returns null; the caller reports it and skips the element.
|
|
15
|
+
*/
|
|
16
|
+
export function layerPlacement(matrix) {
|
|
17
|
+
if (matrixIsDegenerate(matrix))
|
|
18
|
+
return null;
|
|
19
|
+
const parts = decomposeMatrix(matrix);
|
|
20
|
+
const kept = rotationMatrix(parts.rotation, parts.translateX, parts.translateY);
|
|
21
|
+
const bake = remainderMatrix(parts);
|
|
22
|
+
return {
|
|
23
|
+
parts,
|
|
24
|
+
kept,
|
|
25
|
+
bake,
|
|
26
|
+
strokeScale: remainderStrokeScale(parts),
|
|
27
|
+
strokeApproximated: !remainderIsIdentity(parts) && !remainderIsUniformScale(parts),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const RADIANS = Math.PI / 180;
|
|
31
|
+
/**
|
|
32
|
+
* An arc segment under a linear map. An affine map takes an ellipse to an
|
|
33
|
+
* ellipse, so the arc stays exact: the new radii are the singular values of
|
|
34
|
+
* the map times the ellipse's axes, the axis angle is the major axis of the
|
|
35
|
+
* result, and a mirror reverses the turning direction. The large-arc flag
|
|
36
|
+
* is kept, as the map carries the ellipse's angle parameter along.
|
|
37
|
+
*/
|
|
38
|
+
function transformArc(segment, linear) {
|
|
39
|
+
const [ma, mb, mc, md] = linear;
|
|
40
|
+
const phi = segment.xAxisRotation * RADIANS;
|
|
41
|
+
// The ellipse's axes as columns: rotate(φ) · diag(rx, ry), then the map.
|
|
42
|
+
const ax = (ma * Math.cos(phi) + mc * Math.sin(phi)) * segment.radiusX;
|
|
43
|
+
const ay = (mb * Math.cos(phi) + md * Math.sin(phi)) * segment.radiusX;
|
|
44
|
+
const bx = (-ma * Math.sin(phi) + mc * Math.cos(phi)) * segment.radiusY;
|
|
45
|
+
const by = (-mb * Math.sin(phi) + md * Math.cos(phi)) * segment.radiusY;
|
|
46
|
+
// The eigenvalues of A·Aᵀ are the squared radii; its major eigenvector the axis.
|
|
47
|
+
const p = ax * ax + bx * bx;
|
|
48
|
+
const q = ax * ay + bx * by;
|
|
49
|
+
const r = ay * ay + by * by;
|
|
50
|
+
const mean = (p + r) / 2;
|
|
51
|
+
const spread = Math.hypot((p - r) / 2, q);
|
|
52
|
+
const mirrored = ma * md - mb * mc < 0;
|
|
53
|
+
return {
|
|
54
|
+
...segment,
|
|
55
|
+
radiusX: Math.sqrt(mean + spread),
|
|
56
|
+
radiusY: Math.sqrt(Math.max(0, mean - spread)),
|
|
57
|
+
xAxisRotation: (0.5 * Math.atan2(2 * q, p - r)) / RADIANS,
|
|
58
|
+
sweep: mirrored ? !segment.sweep : segment.sweep,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Bakes a linear map into a network's coordinates: vertices move, cubic
|
|
63
|
+
* handles follow, and arc segments scale exactly under a uniform scale
|
|
64
|
+
* (mirrors flip their sweep and reflect their axis angle) or take the
|
|
65
|
+
* mapped ellipse's radii and axis under anything else.
|
|
66
|
+
*/
|
|
67
|
+
export function bakeVectorNetwork(network, bake) {
|
|
68
|
+
const [ux, , uy, uz] = bake;
|
|
69
|
+
if (nearZero(ux - 1) && nearZero(uy) && nearZero(uz - 1))
|
|
70
|
+
return network;
|
|
71
|
+
const uniform = negligibleNextTo(uy, uz) && sameScale(Math.abs(ux), Math.abs(uz)) && ux !== 0;
|
|
72
|
+
const mirrored = uniform && ux * uz < 0;
|
|
73
|
+
const vertices = network.vertices.map((vertex) => ({
|
|
74
|
+
...vertex,
|
|
75
|
+
...applyMatrixToPoint(bake, vertex.x, vertex.y),
|
|
76
|
+
}));
|
|
77
|
+
const segments = network.segments.map((segment) => {
|
|
78
|
+
if (segment.kind === "cubic") {
|
|
79
|
+
const startHandle = applyMatrixToVector(bake, segment.startHandle.dx, segment.startHandle.dy);
|
|
80
|
+
const endHandle = applyMatrixToVector(bake, segment.endHandle.dx, segment.endHandle.dy);
|
|
81
|
+
return {
|
|
82
|
+
...segment,
|
|
83
|
+
startHandle: { ...segment.startHandle, ...startHandle },
|
|
84
|
+
endHandle: { ...segment.endHandle, ...endHandle },
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (segment.kind === "elliptical-arc" && !uniform)
|
|
88
|
+
return transformArc(segment, bake);
|
|
89
|
+
if (segment.kind === "elliptical-arc") {
|
|
90
|
+
const scale = Math.abs(ux);
|
|
91
|
+
return {
|
|
92
|
+
...segment,
|
|
93
|
+
radiusX: segment.radiusX * scale,
|
|
94
|
+
radiusY: segment.radiusY * scale,
|
|
95
|
+
// A mirror reverses the arc's direction and reflects its axis
|
|
96
|
+
// angle; a half-turn (both scales negative) keeps both.
|
|
97
|
+
sweep: mirrored ? !segment.sweep : segment.sweep,
|
|
98
|
+
xAxisRotation: mirrored && segment.xAxisRotation !== 0 ? -segment.xAxisRotation : segment.xAxisRotation,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return segment;
|
|
102
|
+
});
|
|
103
|
+
return { ...network, vertices, segments };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The converter's refusal in plain words: a `ModelError` message ends with
|
|
107
|
+
* ` at <JSON path>` into the converter's own input (such as
|
|
108
|
+
* `$svgPath.pathData`), which means nothing to a person reading the report.
|
|
109
|
+
*/
|
|
110
|
+
function converterReason(error) {
|
|
111
|
+
if (error instanceof ModelError) {
|
|
112
|
+
const suffix = ` at ${error.path}`;
|
|
113
|
+
return error.message.endsWith(suffix) ? error.message.slice(0, -suffix.length) : error.message;
|
|
114
|
+
}
|
|
115
|
+
return error instanceof Error ? error.message : String(error);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* How many numbers and commands a path's data holds, counted in one scan
|
|
119
|
+
* without parsing it: a number starts at a digit, or at a dot not inside a
|
|
120
|
+
* number, an exponent's digits belong to its number, and every other letter
|
|
121
|
+
* is one command. Every segment carries at least one number or command
|
|
122
|
+
* (a closepath carries no number), so the count bounds the commands,
|
|
123
|
+
* vertices and segments the data makes.
|
|
124
|
+
*/
|
|
125
|
+
export function countPathNumbers(pathData) {
|
|
126
|
+
let count = 0;
|
|
127
|
+
let inNumber = false;
|
|
128
|
+
let seenDot = false;
|
|
129
|
+
for (let index = 0; index < pathData.length; index += 1) {
|
|
130
|
+
const code = pathData.charCodeAt(index);
|
|
131
|
+
const digit = code >= 48 && code <= 57;
|
|
132
|
+
if (digit) {
|
|
133
|
+
if (!inNumber)
|
|
134
|
+
count += 1;
|
|
135
|
+
inNumber = true;
|
|
136
|
+
}
|
|
137
|
+
else if (code === 46) {
|
|
138
|
+
// A second dot starts a new number, as in "0.5.5".
|
|
139
|
+
if (!inNumber || seenDot)
|
|
140
|
+
count += 1;
|
|
141
|
+
inNumber = true;
|
|
142
|
+
seenDot = true;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
else if ((code === 101 || code === 69) && inNumber) {
|
|
146
|
+
// An exponent: its sign and digits continue the number.
|
|
147
|
+
const next = pathData.charCodeAt(index + 1);
|
|
148
|
+
if (next === 43 || next === 45)
|
|
149
|
+
index += 1;
|
|
150
|
+
seenDot = true;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
inNumber = false;
|
|
155
|
+
// A letter that is not an exponent is a command, such as "Z".
|
|
156
|
+
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122))
|
|
157
|
+
count += 1;
|
|
158
|
+
}
|
|
159
|
+
if (!inNumber)
|
|
160
|
+
seenDot = false;
|
|
161
|
+
}
|
|
162
|
+
return count;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Builds a path layer from path data: parses and validates it through the
|
|
166
|
+
* model's converter, bakes the transform's scale and skew into the
|
|
167
|
+
* geometry, keeps rotation and translation on the node, and resolves its
|
|
168
|
+
* style. Returns null when the converter refuses the data; the reason is
|
|
169
|
+
* reported against the element's path. `numbersCounted` says the caller
|
|
170
|
+
* already counted the data's numbers against the cap, before building it.
|
|
171
|
+
*/
|
|
172
|
+
export function pathLayerFromData(id, name, pathData, computed, placement, elementPath, element, ctx, numbersCounted = false) {
|
|
173
|
+
if (!numbersCounted)
|
|
174
|
+
ctx.limits.countPathNumbers(countPathNumbers(pathData));
|
|
175
|
+
let converted;
|
|
176
|
+
try {
|
|
177
|
+
converted = pathNodeFromSvgPath({
|
|
178
|
+
id,
|
|
179
|
+
name,
|
|
180
|
+
pathData,
|
|
181
|
+
style: {},
|
|
182
|
+
fillRule: computed.fillRule,
|
|
183
|
+
strokeCap: computed.strokeLinecap,
|
|
184
|
+
opacity: computed.opacity,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
ctx.report(elementPath, element.localName, "left-out", converterReason(error), { what: "Path data that cannot be read", kept: "Not drawn" });
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
const network = bakeVectorNetwork(converted.vectorNetwork, placement.bake);
|
|
192
|
+
const localBounds = getNodeBounds({ ...converted, vectorNetwork: network, transform: [1, 0, 0, 1, 0, 0] });
|
|
193
|
+
const style = visualStyleFromComputed(computed, placement.bake, localBounds, placement.strokeScale, placement.strokeApproximated, elementPath, element, ctx);
|
|
194
|
+
const chrome = layerChrome(computed);
|
|
195
|
+
const node = {
|
|
196
|
+
...converted,
|
|
197
|
+
vectorNetwork: network,
|
|
198
|
+
transform: placement.kept,
|
|
199
|
+
style,
|
|
200
|
+
visible: chrome.visible,
|
|
201
|
+
opacity: chrome.opacity,
|
|
202
|
+
...(chrome.blendMode === null ? {} : { blendMode: chrome.blendMode }),
|
|
203
|
+
};
|
|
204
|
+
return { node, localBounds };
|
|
205
|
+
}
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import type { AssetId, ComponentDefinition, FrameNode, GroupNode, SceneNode } from "../model/index.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The element tree the importer reads. A browser `Element` from
|
|
4
|
+
* `new DOMParser().parseFromString(svg, "image/svg+xml")` satisfies this
|
|
5
|
+
* interface as is, without wrapping; tests can satisfy it with a small XML
|
|
6
|
+
* parser. The importer never touches the live DOM, the network, timers or
|
|
7
|
+
* globals: the same tree and `idSource` always give the same output.
|
|
8
|
+
*/
|
|
9
|
+
export interface SvgElementLike {
|
|
10
|
+
readonly nodeType: number;
|
|
11
|
+
readonly localName: string;
|
|
12
|
+
readonly namespaceURI: string | null;
|
|
13
|
+
getAttribute(name: string): string | null;
|
|
14
|
+
readonly attributes: ArrayLike<{
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly value: string;
|
|
17
|
+
}>;
|
|
18
|
+
/** Element children only, in document order. */
|
|
19
|
+
readonly children: ArrayLike<SvgElementLike>;
|
|
20
|
+
readonly textContent: string | null;
|
|
21
|
+
/**
|
|
22
|
+
* All child nodes, elements and text mixed, in document order. Text nodes
|
|
23
|
+
* have `nodeType` 3 (text) or 4 (CDATA) and carry their characters in
|
|
24
|
+
* `nodeValue`. The importer reads `<text>` content from here so it can keep
|
|
25
|
+
* the white space between inline `<tspan>` runs.
|
|
26
|
+
*/
|
|
27
|
+
readonly childNodes: ArrayLike<SvgElementLike | {
|
|
28
|
+
readonly nodeType: number;
|
|
29
|
+
readonly nodeValue: string | null;
|
|
30
|
+
}>;
|
|
31
|
+
}
|
|
32
|
+
/** The caps of one import. Every value is overridable through `SvgImportOptions.limits`. */
|
|
33
|
+
export interface SvgImportLimits {
|
|
34
|
+
/**
|
|
35
|
+
* Most bytes of input. `importSvgMarkup` counts the markup's UTF-8 bytes;
|
|
36
|
+
* `importSvg` approximates them from the tree (element names, attributes
|
|
37
|
+
* and text).
|
|
38
|
+
*/
|
|
39
|
+
readonly inputBytes: number;
|
|
40
|
+
/** Most elements in one document. */
|
|
41
|
+
readonly elements: number;
|
|
42
|
+
/** Deepest element nesting. */
|
|
43
|
+
readonly depth: number;
|
|
44
|
+
/**
|
|
45
|
+
* Most `<use>` elements turned into layers. Each `<use>` counts once where
|
|
46
|
+
* it is built: a use inside a component counts once, however many
|
|
47
|
+
* instances of that component there are.
|
|
48
|
+
*/
|
|
49
|
+
readonly useCopies: number;
|
|
50
|
+
/** Most components created by one import. */
|
|
51
|
+
readonly components: number;
|
|
52
|
+
/** Most instances created by one import. */
|
|
53
|
+
readonly instances: number;
|
|
54
|
+
/**
|
|
55
|
+
* Most style sheet work over the whole import: every class name token read
|
|
56
|
+
* from a `class` attribute counts one, every parsed style declaration
|
|
57
|
+
* counts one plus its value's length — in a `<style>` rule as the sheet is
|
|
58
|
+
* read, and in an inline `style` attribute every time the element's style
|
|
59
|
+
* is computed —, every time an element's style is computed, each `<style>`
|
|
60
|
+
* rule it matches counts one plus its declarations' value lengths, and
|
|
61
|
+
* every font family tried for a run of text counts one.
|
|
62
|
+
* This bounds files that pair many rules with many elements, a long
|
|
63
|
+
* `font-family` list with many text elements, a class attribute filled
|
|
64
|
+
* with tokens, or a style block of millions of declarations.
|
|
65
|
+
*/
|
|
66
|
+
readonly styleWork: number;
|
|
67
|
+
/**
|
|
68
|
+
* Most `href` steps over the whole import: each step taken along a
|
|
69
|
+
* gradient's or a `<use>`'s chain of references counts one, every time the
|
|
70
|
+
* chain is followed. This bounds files that pair long chains with many
|
|
71
|
+
* references to them.
|
|
72
|
+
*/
|
|
73
|
+
readonly referenceSteps: number;
|
|
74
|
+
/**
|
|
75
|
+
* Most numbers and commands of path data over the whole import, counted
|
|
76
|
+
* in each path's `d` (or a shape's points) before it is turned into
|
|
77
|
+
* vertices. Every path segment carries at least one number or command (a
|
|
78
|
+
* closepath carries none), so this bounds the commands, vertices and
|
|
79
|
+
* segments one import can create.
|
|
80
|
+
*/
|
|
81
|
+
readonly pathNumbers: number;
|
|
82
|
+
/**
|
|
83
|
+
* Most times an element is built over the whole import, counted each time
|
|
84
|
+
* its style is computed: once when the file is read, then again every time
|
|
85
|
+
* it is drawn, in place, in a component, in each copy of a `<use>` target,
|
|
86
|
+
* in a clip path, mask or marker each time one applies, and in each check
|
|
87
|
+
* of a component occurrence against its component. This bounds files whose
|
|
88
|
+
* few elements are drawn many times over.
|
|
89
|
+
*/
|
|
90
|
+
readonly elementBuilds: number;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The default caps: 10 MB, 10,000 elements, 64 levels, 1,000 `<use>` copies,
|
|
94
|
+
* 200 components, 5,000 instances, 1,000,000 units of style sheet work,
|
|
95
|
+
* 100,000 reference steps, 500,000 numbers and commands of path data and
|
|
96
|
+
* 100,000 element builds.
|
|
97
|
+
*/
|
|
98
|
+
export declare const DEFAULT_SVG_IMPORT_LIMITS: SvgImportLimits;
|
|
99
|
+
/** Why an import was refused. A refusal never leaves anything half imported. */
|
|
100
|
+
export declare const SVG_IMPORT_REFUSAL_CODES: readonly [
|
|
101
|
+
/** The root is not an `<svg>` element in the SVG namespace. */
|
|
102
|
+
"not-svg",
|
|
103
|
+
/** The input is over the byte cap. */
|
|
104
|
+
"too-large",
|
|
105
|
+
/**
|
|
106
|
+
* The markup is not well-formed XML: the parser threw, or returned a
|
|
107
|
+
* browser's error page — a `parsererror` element as the root (Firefox) or
|
|
108
|
+
* inside a generated `html` error page (Chrome, Safari). The message
|
|
109
|
+
* carries the parser's own text.
|
|
110
|
+
*/
|
|
111
|
+
"malformed",
|
|
112
|
+
/** The document has more elements than the cap. */
|
|
113
|
+
"too-many-elements",
|
|
114
|
+
/** The document nests deeper than the cap. */
|
|
115
|
+
"too-deep",
|
|
116
|
+
/** The document would create more `<use>` copies than the cap. */
|
|
117
|
+
"too-many-use-copies",
|
|
118
|
+
/** The document would create more components than the cap. */
|
|
119
|
+
"too-many-components",
|
|
120
|
+
/** The document would create more instances than the cap. */
|
|
121
|
+
"too-many-instances",
|
|
122
|
+
/** Matching the `<style>` rules to the elements would take more work than the cap. */
|
|
123
|
+
"too-much-style-work",
|
|
124
|
+
/** Following gradient and `<use>` reference chains would take more steps than the cap. */
|
|
125
|
+
"too-many-reference-steps",
|
|
126
|
+
/** The paths hold more numbers and commands of path data than the cap. */
|
|
127
|
+
"too-much-path-data",
|
|
128
|
+
/** Drawing the elements, counting each time one is drawn again, would build more than the cap. */
|
|
129
|
+
"too-many-element-builds",
|
|
130
|
+
/** `<use>` elements reference each other in a cycle; the message names the ids. */
|
|
131
|
+
"use-cycle"];
|
|
132
|
+
/** One of `SVG_IMPORT_REFUSAL_CODES`. */
|
|
133
|
+
export type SvgImportRefusalCode = (typeof SVG_IMPORT_REFUSAL_CODES)[number];
|
|
134
|
+
/** A refused import. `code` is stable for tests; `message` explains it to a person. */
|
|
135
|
+
export interface SvgImportRefusal {
|
|
136
|
+
readonly code: SvgImportRefusalCode;
|
|
137
|
+
readonly message: string;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Why an element is in the report. "left-out" and "approximated" lines are
|
|
141
|
+
* the things not imported exactly; an "auto-layout" line only notes that a
|
|
142
|
+
* `<g>` became an auto layout frame, so it is not a left-out item and a
|
|
143
|
+
* count of left-out things (such as an import toast's) excludes it.
|
|
144
|
+
*/
|
|
145
|
+
export type SvgImportReportKind =
|
|
146
|
+
/** The element (or one of its aspects, such as a pattern fill) was left out. */
|
|
147
|
+
"left-out"
|
|
148
|
+
/** The element came in, but something about it is drawn differently. */
|
|
149
|
+
| "approximated"
|
|
150
|
+
/** The `<g>` became a frame with a stack auto layout. */
|
|
151
|
+
| "auto-layout";
|
|
152
|
+
/** One entry of the import report: an element that was not imported exactly. */
|
|
153
|
+
export interface SvgImportReportEntry {
|
|
154
|
+
/** Where the element sits, such as `svg > g#icons > use[2]`. */
|
|
155
|
+
readonly elementPath: string;
|
|
156
|
+
/** The element's local name, such as `use`. */
|
|
157
|
+
readonly element: string;
|
|
158
|
+
/** Why it is in the report, in plain words. */
|
|
159
|
+
readonly reason: string;
|
|
160
|
+
readonly kind: SvgImportReportKind;
|
|
161
|
+
/**
|
|
162
|
+
* What was not imported exactly, in plain words and without this
|
|
163
|
+
* element's own values, such as "Markers" or "Filters": entries that share
|
|
164
|
+
* `what` and `kept` are the same kind of problem, so a list can group them.
|
|
165
|
+
*/
|
|
166
|
+
readonly what: string;
|
|
167
|
+
/** What the import did instead, in plain words, such as "The shape is kept without its markers". */
|
|
168
|
+
readonly kept: string;
|
|
169
|
+
/**
|
|
170
|
+
* The family of a font the host does not have, when that is what the
|
|
171
|
+
* entry is about. The app tracks missing fonts live from the document, so
|
|
172
|
+
* it can tell these entries apart without reading `reason`.
|
|
173
|
+
*/
|
|
174
|
+
readonly missingFont?: string;
|
|
175
|
+
/**
|
|
176
|
+
* The ID of the created layer the entry applies to: the element's own
|
|
177
|
+
* layer, or the nearest layer kept around it when the element was dropped
|
|
178
|
+
* or became part of a component (then the instance or copy that shows
|
|
179
|
+
* it). An element met more than once is listed once, with the layer of
|
|
180
|
+
* its first occurrence.
|
|
181
|
+
*/
|
|
182
|
+
readonly nodeId?: string;
|
|
183
|
+
}
|
|
184
|
+
/** Parses SVG markup into an element tree; the host passes its `DOMParser`, tests their own parser. */
|
|
185
|
+
export type SvgParse = (svgText: string) => SvgElementLike;
|
|
186
|
+
/**
|
|
187
|
+
* Whether a font can be used, answered by the host from its font registry.
|
|
188
|
+
* Text with no `font-family` asks for "sans-serif", SVG's initial value, so
|
|
189
|
+
* the host should answer yes for the generic families (`serif`,
|
|
190
|
+
* `sans-serif`, `monospace` and the rest) it maps to a real font.
|
|
191
|
+
*/
|
|
192
|
+
/** A `font-style` value: normal, italic or oblique. */
|
|
193
|
+
export type SvgTextFontStyle = "normal" | "italic" | "oblique";
|
|
194
|
+
export type SvgFontAvailable = (family: string, weight: number, style: SvgTextFontStyle) => boolean;
|
|
195
|
+
/** One text measurement request: the text of one run and the font it draws with. */
|
|
196
|
+
export interface SvgTextMeasureRequest {
|
|
197
|
+
readonly text: string;
|
|
198
|
+
readonly fontFamily: string;
|
|
199
|
+
readonly fontWeight: number;
|
|
200
|
+
/** The requested `font-style`; the model's runs carry italic only, so the style matters here for which face is measured. */
|
|
201
|
+
readonly style: SvgTextFontStyle;
|
|
202
|
+
readonly fontSize: number;
|
|
203
|
+
/** Extra space between characters in px; 0 when the SVG sets none. */
|
|
204
|
+
readonly letterSpacing: number;
|
|
205
|
+
}
|
|
206
|
+
/** Font metrics at `fontSize`, used to place a baseline and to size a text layer. */
|
|
207
|
+
export interface SvgTextMetrics {
|
|
208
|
+
/** The run's advance width. */
|
|
209
|
+
readonly width: number;
|
|
210
|
+
/** Baseline to the top of the font's bounding box. */
|
|
211
|
+
readonly ascent: number;
|
|
212
|
+
/** Baseline to the bottom of the font's bounding box. */
|
|
213
|
+
readonly descent: number;
|
|
214
|
+
}
|
|
215
|
+
/** Measures text for the importer. The host passes a canvas-based one; tests pass a stub. */
|
|
216
|
+
export type SvgMeasureText = (request: SvgTextMeasureRequest) => SvgTextMetrics;
|
|
217
|
+
/** What `importSvg` needs from the host. */
|
|
218
|
+
export interface SvgImportOptions {
|
|
219
|
+
/**
|
|
220
|
+
* The root frame's name, usually the file name without `.svg`. When omitted,
|
|
221
|
+
* the document's `<title>` is used, or "SVG".
|
|
222
|
+
*/
|
|
223
|
+
readonly name?: string;
|
|
224
|
+
/** Returns a fresh id for every node, asset and component, so the host controls ids. */
|
|
225
|
+
readonly idSource: () => string;
|
|
226
|
+
readonly fontAvailable: SvgFontAvailable;
|
|
227
|
+
readonly measureText: SvgMeasureText;
|
|
228
|
+
/** Overrides for single caps of `DEFAULT_SVG_IMPORT_LIMITS`, mostly for tests. */
|
|
229
|
+
readonly limits?: Partial<SvgImportLimits>;
|
|
230
|
+
/**
|
|
231
|
+
* Parses SVG markup into an element tree. `importSvgMarkup` requires it and
|
|
232
|
+
* parses the whole document with it; both entry points use it for nested
|
|
233
|
+
* `image/svg+xml` data-URI `<image>` elements. It may throw on malformed
|
|
234
|
+
* markup or return a browser's error page (a `parsererror` root, or one
|
|
235
|
+
* inside a generated `html` page): for the document that refuses the
|
|
236
|
+
* import as `malformed`, for a nested image it makes the image a report
|
|
237
|
+
* line with the parser's message. Without it, `importSvg` reports nested
|
|
238
|
+
* SVG images and leaves them out.
|
|
239
|
+
*/
|
|
240
|
+
readonly parseSvg?: SvgParse;
|
|
241
|
+
}
|
|
242
|
+
/** One component created by the import: its definition plus every node of its tree. */
|
|
243
|
+
export interface SvgImportComponent {
|
|
244
|
+
readonly definition: ComponentDefinition;
|
|
245
|
+
/** Every node of the component's layer tree, `definition.rootId`'s frame included. */
|
|
246
|
+
readonly nodes: readonly SceneNode[];
|
|
247
|
+
}
|
|
248
|
+
/** The decoded bytes of one data-URI `<image>`. Image paints refer to it by `id`. */
|
|
249
|
+
export interface SvgImportImage {
|
|
250
|
+
readonly id: AssetId;
|
|
251
|
+
readonly mimeType: string;
|
|
252
|
+
readonly data: Uint8Array;
|
|
253
|
+
/** Intrinsic size in pixels, when the image header names it. */
|
|
254
|
+
readonly width?: number;
|
|
255
|
+
readonly height?: number;
|
|
256
|
+
}
|
|
257
|
+
/** A successful import. */
|
|
258
|
+
export interface SvgImportResult {
|
|
259
|
+
/** The imported tree's root: a frame with clipping on, or a group when the SVG had no size and no viewBox. */
|
|
260
|
+
readonly root: FrameNode | GroupNode;
|
|
261
|
+
/** Every node of the main tree, `root` included, in the order they were created. */
|
|
262
|
+
readonly nodes: readonly SceneNode[];
|
|
263
|
+
/** New components to add to the document; empty when nothing was reused. */
|
|
264
|
+
readonly components: readonly SvgImportComponent[];
|
|
265
|
+
/** The decoded data-URI images, in the order they appeared. */
|
|
266
|
+
readonly images: readonly SvgImportImage[];
|
|
267
|
+
/** Every element that was not imported exactly, in document order. */
|
|
268
|
+
readonly report: readonly SvgImportReportEntry[];
|
|
269
|
+
/**
|
|
270
|
+
* The `font-style` each text run was requested with, by text layer ID, in
|
|
271
|
+
* run order (each imported text layer has one paragraph). The model's runs
|
|
272
|
+
* carry italic only, so a host that matches font faces — exactly, for a
|
|
273
|
+
* family with both an italic and an oblique face — reads the requested
|
|
274
|
+
* style here.
|
|
275
|
+
*/
|
|
276
|
+
readonly textRunStyles: ReadonlyMap<string, readonly SvgTextFontStyle[]>;
|
|
277
|
+
}
|
|
278
|
+
/** Whether an `importSvg` outcome refused the import. */
|
|
279
|
+
export declare function isSvgImportRefusal(outcome: SvgImportResult | SvgImportRefusal): outcome is SvgImportRefusal;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The default caps: 10 MB, 10,000 elements, 64 levels, 1,000 `<use>` copies,
|
|
3
|
+
* 200 components, 5,000 instances, 1,000,000 units of style sheet work,
|
|
4
|
+
* 100,000 reference steps, 500,000 numbers and commands of path data and
|
|
5
|
+
* 100,000 element builds.
|
|
6
|
+
*/
|
|
7
|
+
export const DEFAULT_SVG_IMPORT_LIMITS = {
|
|
8
|
+
inputBytes: 10 * 1024 * 1024,
|
|
9
|
+
elements: 10_000,
|
|
10
|
+
depth: 64,
|
|
11
|
+
useCopies: 1_000,
|
|
12
|
+
components: 200,
|
|
13
|
+
instances: 5_000,
|
|
14
|
+
styleWork: 1_000_000,
|
|
15
|
+
referenceSteps: 100_000,
|
|
16
|
+
pathNumbers: 500_000,
|
|
17
|
+
elementBuilds: 100_000,
|
|
18
|
+
};
|
|
19
|
+
/** Why an import was refused. A refusal never leaves anything half imported. */
|
|
20
|
+
export const SVG_IMPORT_REFUSAL_CODES = [
|
|
21
|
+
/** The root is not an `<svg>` element in the SVG namespace. */
|
|
22
|
+
"not-svg",
|
|
23
|
+
/** The input is over the byte cap. */
|
|
24
|
+
"too-large",
|
|
25
|
+
/**
|
|
26
|
+
* The markup is not well-formed XML: the parser threw, or returned a
|
|
27
|
+
* browser's error page — a `parsererror` element as the root (Firefox) or
|
|
28
|
+
* inside a generated `html` error page (Chrome, Safari). The message
|
|
29
|
+
* carries the parser's own text.
|
|
30
|
+
*/
|
|
31
|
+
"malformed",
|
|
32
|
+
/** The document has more elements than the cap. */
|
|
33
|
+
"too-many-elements",
|
|
34
|
+
/** The document nests deeper than the cap. */
|
|
35
|
+
"too-deep",
|
|
36
|
+
/** The document would create more `<use>` copies than the cap. */
|
|
37
|
+
"too-many-use-copies",
|
|
38
|
+
/** The document would create more components than the cap. */
|
|
39
|
+
"too-many-components",
|
|
40
|
+
/** The document would create more instances than the cap. */
|
|
41
|
+
"too-many-instances",
|
|
42
|
+
/** Matching the `<style>` rules to the elements would take more work than the cap. */
|
|
43
|
+
"too-much-style-work",
|
|
44
|
+
/** Following gradient and `<use>` reference chains would take more steps than the cap. */
|
|
45
|
+
"too-many-reference-steps",
|
|
46
|
+
/** The paths hold more numbers and commands of path data than the cap. */
|
|
47
|
+
"too-much-path-data",
|
|
48
|
+
/** Drawing the elements, counting each time one is drawn again, would build more than the cap. */
|
|
49
|
+
"too-many-element-builds",
|
|
50
|
+
/** `<use>` elements reference each other in a cycle; the message names the ids. */
|
|
51
|
+
"use-cycle",
|
|
52
|
+
];
|
|
53
|
+
/** Whether an `importSvg` outcome refused the import. */
|
|
54
|
+
export function isSvgImportRefusal(outcome) {
|
|
55
|
+
return typeof outcome === "object" && outcome !== null && "code" in outcome;
|
|
56
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { SvgImportReportEntry, SvgImportReportKind } from "../public-types.ts";
|
|
2
|
+
/** The plain words of one report line that a list groups by (see `SvgImportReportEntry`). */
|
|
3
|
+
export interface ReportNote {
|
|
4
|
+
readonly what: string;
|
|
5
|
+
readonly kept: string;
|
|
6
|
+
readonly missingFont?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Collects the report of one import. Every element that is not imported
|
|
10
|
+
* exactly appears once per reason, in the order the walk meets it: an
|
|
11
|
+
* element built twice (a component's content and a plain copy of it) adds
|
|
12
|
+
* an identical line only the first time.
|
|
13
|
+
*
|
|
14
|
+
* Each entry also gets the layer it applies to. Most lines are added while
|
|
15
|
+
* their element is built, before its layer exists, so they wait on a stack
|
|
16
|
+
* until the build of an element that keeps a layer ends (`placeSince`):
|
|
17
|
+
* the element's own layer, or the nearest one kept around it. Lines added
|
|
18
|
+
* before the walk (style and root lines) are placed at the end by their
|
|
19
|
+
* path (`placeRest`).
|
|
20
|
+
*
|
|
21
|
+
* The dedupe key stays the element and its reason, without the layer: the
|
|
22
|
+
* second build of an element is a component's content shown again or a
|
|
23
|
+
* plain copy, and listing it again would count one problem twice. Its line
|
|
24
|
+
* keeps the layer of the first occurrence.
|
|
25
|
+
*/
|
|
26
|
+
export declare class ReportBuilder {
|
|
27
|
+
#private;
|
|
28
|
+
/** Adds one entry, unless the same entry is already in the report. */
|
|
29
|
+
add(elementPath: string, element: string, kind: SvgImportReportKind, reason: string, note: ReportNote): void;
|
|
30
|
+
/** A mark to pass to `placeSince` when the current element's build ends. */
|
|
31
|
+
mark(): number;
|
|
32
|
+
/**
|
|
33
|
+
* Places every entry added since `mark` that has no layer yet on the layer
|
|
34
|
+
* `nodeId`, which the element at `elementPath` became.
|
|
35
|
+
*/
|
|
36
|
+
placeSince(mark: number, elementPath: string, nodeId: string): void;
|
|
37
|
+
/**
|
|
38
|
+
* Places the entries still without a layer: on the layer of their element
|
|
39
|
+
* or of its nearest ancestor that has one, else on the root layer.
|
|
40
|
+
*/
|
|
41
|
+
placeRest(rootId: string): void;
|
|
42
|
+
/** The collected entries, in document order. */
|
|
43
|
+
entries(): readonly SvgImportReportEntry[];
|
|
44
|
+
}
|