@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,229 @@
|
|
|
1
|
+
import { multiplyTransforms, nodeLocalTransform } from "../../model/index.js";
|
|
2
|
+
import { parseSvgNumber } from "../units/length.js";
|
|
3
|
+
/** The identity matrix. */
|
|
4
|
+
export const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
|
|
5
|
+
/** How far a value may be from its exact form before it counts as different. */
|
|
6
|
+
const EPSILON = 1e-9;
|
|
7
|
+
/** Whether `value` is close enough to zero to treat as zero. */
|
|
8
|
+
export function nearZero(value) {
|
|
9
|
+
return Math.abs(value) < EPSILON;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Whether `value` (a skew term, or the difference of two scales) is
|
|
13
|
+
* negligible next to `scale`. Measured relative to the scale, so a tiny but
|
|
14
|
+
* real skew or uneven scale is still seen under a tiny scale.
|
|
15
|
+
*/
|
|
16
|
+
export function negligibleNextTo(value, scale) {
|
|
17
|
+
return Math.abs(value) <= EPSILON * Math.abs(scale);
|
|
18
|
+
}
|
|
19
|
+
/** Whether two scales are the same, relative to their size. */
|
|
20
|
+
export function sameScale(first, second) {
|
|
21
|
+
return negligibleNextTo(first - second, Math.max(Math.abs(first), Math.abs(second)));
|
|
22
|
+
}
|
|
23
|
+
/** Maps one point by the matrix. */
|
|
24
|
+
export function applyMatrixToPoint(matrix, x, y) {
|
|
25
|
+
const [a, b, c, d, e, f] = matrix;
|
|
26
|
+
return { x: a * x + c * y + e, y: b * x + d * y + f };
|
|
27
|
+
}
|
|
28
|
+
/** Maps a direction (a point relative to an origin) by the matrix's linear part. */
|
|
29
|
+
export function applyMatrixToVector(matrix, dx, dy) {
|
|
30
|
+
const [a, b, c, d] = matrix;
|
|
31
|
+
return { dx: a * dx + c * dy, dy: b * dx + d * dy };
|
|
32
|
+
}
|
|
33
|
+
/** The matrix's determinant; zero means the transform collapses the plane. */
|
|
34
|
+
export function matrixDeterminant(matrix) {
|
|
35
|
+
const [a, b, c, d] = matrix;
|
|
36
|
+
return a * d - b * c;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether the matrix maps everything onto a line or a point. The determinant
|
|
40
|
+
* is compared with the product of the column lengths, so the test measures
|
|
41
|
+
* the angle between the columns and not the size of the scale: a tiny but
|
|
42
|
+
* valid scale (a 1e-5 viewBox scale has determinant 1e-10) is invertible.
|
|
43
|
+
*/
|
|
44
|
+
export function matrixIsDegenerate(matrix) {
|
|
45
|
+
const [a, b, c, d] = matrix;
|
|
46
|
+
const determinant = matrixDeterminant(matrix);
|
|
47
|
+
if (!Number.isFinite(determinant))
|
|
48
|
+
return true;
|
|
49
|
+
return Math.abs(determinant) <= EPSILON * Math.hypot(a, b) * Math.hypot(c, d);
|
|
50
|
+
}
|
|
51
|
+
const DEGREES = 180 / Math.PI;
|
|
52
|
+
const RADIANS = Math.PI / 180;
|
|
53
|
+
/**
|
|
54
|
+
* Parses one `transform` attribute into a matrix: a list of `matrix`,
|
|
55
|
+
* `translate`, `scale`, `rotate` (degrees, optionally around a center),
|
|
56
|
+
* `skewX` and `skewY` functions, applied left to right. Returns null when
|
|
57
|
+
* the text does not follow SVG's grammar — the caller reports that.
|
|
58
|
+
*/
|
|
59
|
+
export function parseTransform(text) {
|
|
60
|
+
let matrix = IDENTITY_MATRIX;
|
|
61
|
+
let rest = text.trim();
|
|
62
|
+
if (rest.length === 0)
|
|
63
|
+
return null;
|
|
64
|
+
// `none` is CSS's no-op transform; keywords are ASCII case-insensitive.
|
|
65
|
+
if (rest.toLowerCase() === "none")
|
|
66
|
+
return IDENTITY_MATRIX;
|
|
67
|
+
const call = /^([a-zA-Z]+)\s*\(([^)]*)\)\s*/;
|
|
68
|
+
while (rest.length > 0) {
|
|
69
|
+
const match = call.exec(rest);
|
|
70
|
+
if (match === null)
|
|
71
|
+
return null;
|
|
72
|
+
const name = match[1] ?? "";
|
|
73
|
+
const argsText = match[2] ?? "";
|
|
74
|
+
const args = argsText.trim().length === 0
|
|
75
|
+
? []
|
|
76
|
+
// Split on comma-wsp, so two commas in a row leave an empty, invalid argument.
|
|
77
|
+
: argsText.trim().split(/\s*,\s*|\s+/).map((entry) => parseSvgNumber(entry));
|
|
78
|
+
if (!args.every((value) => value !== null))
|
|
79
|
+
return null;
|
|
80
|
+
const next = transformCall(name, args);
|
|
81
|
+
if (next === null)
|
|
82
|
+
return null;
|
|
83
|
+
matrix = multiplyTransforms(matrix, next);
|
|
84
|
+
rest = rest.slice(match[0].length).trimStart();
|
|
85
|
+
// One comma may separate two functions; it may not end the list.
|
|
86
|
+
if (rest.startsWith(",")) {
|
|
87
|
+
rest = rest.slice(1).trimStart();
|
|
88
|
+
if (rest.length === 0)
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return matrix;
|
|
93
|
+
}
|
|
94
|
+
/** One transform function as a matrix, or null when the name or argument count is wrong. */
|
|
95
|
+
function transformCall(name, args) {
|
|
96
|
+
const first = args[0] ?? 0;
|
|
97
|
+
switch (name) {
|
|
98
|
+
case "matrix":
|
|
99
|
+
if (args.length !== 6)
|
|
100
|
+
return null;
|
|
101
|
+
return [args[0] ?? 0, args[1] ?? 0, args[2] ?? 0, args[3] ?? 0, args[4] ?? 0, args[5] ?? 0];
|
|
102
|
+
case "translate":
|
|
103
|
+
if (args.length < 1 || args.length > 2)
|
|
104
|
+
return null;
|
|
105
|
+
return [1, 0, 0, 1, first, args[1] ?? 0];
|
|
106
|
+
case "scale":
|
|
107
|
+
if (args.length < 1 || args.length > 2)
|
|
108
|
+
return null;
|
|
109
|
+
return [first, 0, 0, args[1] ?? first, 0, 0];
|
|
110
|
+
case "rotate": {
|
|
111
|
+
if (args.length !== 1 && args.length !== 3)
|
|
112
|
+
return null;
|
|
113
|
+
const radians = first * RADIANS;
|
|
114
|
+
const cos = Math.cos(radians);
|
|
115
|
+
const sin = Math.sin(radians);
|
|
116
|
+
const rotation = [cos, sin, -sin, cos, 0, 0];
|
|
117
|
+
if (args.length === 1)
|
|
118
|
+
return rotation;
|
|
119
|
+
const cx = args[1] ?? 0;
|
|
120
|
+
const cy = args[2] ?? 0;
|
|
121
|
+
return multiplyTransforms(multiplyTransforms([1, 0, 0, 1, cx, cy], rotation), [1, 0, 0, 1, -cx, -cy]);
|
|
122
|
+
}
|
|
123
|
+
case "skewX":
|
|
124
|
+
if (args.length !== 1)
|
|
125
|
+
return null;
|
|
126
|
+
return [1, 0, Math.tan(first * RADIANS), 1, 0, 0];
|
|
127
|
+
case "skewY":
|
|
128
|
+
if (args.length !== 1)
|
|
129
|
+
return null;
|
|
130
|
+
return [1, Math.tan(first * RADIANS), 0, 1, 0, 0];
|
|
131
|
+
default:
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** The remainder as a matrix (no translation). */
|
|
136
|
+
export function remainderMatrix(parts) {
|
|
137
|
+
return [parts.ux, 0, parts.uy, parts.uz, 0, 0];
|
|
138
|
+
}
|
|
139
|
+
/** Whether the remainder leaves geometry unchanged. */
|
|
140
|
+
export function remainderIsIdentity(parts) {
|
|
141
|
+
return nearZero(parts.ux - 1) && nearZero(parts.uy) && nearZero(parts.uz - 1);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Whether the remainder is a plain positive scale on both axes, uniform or
|
|
145
|
+
* not. A rectangle, ellipse or text layer scales exactly under such a
|
|
146
|
+
* remainder; anything else is baked into path geometry.
|
|
147
|
+
*/
|
|
148
|
+
export function remainderIsPositiveScale(parts) {
|
|
149
|
+
return negligibleNextTo(parts.uy, parts.uz) && parts.ux > 0 && parts.uz > 0;
|
|
150
|
+
}
|
|
151
|
+
/** Whether the remainder scales both axes by the same positive factor. */
|
|
152
|
+
export function remainderIsUniformScale(parts) {
|
|
153
|
+
return remainderIsPositiveScale(parts) && sameScale(parts.ux, parts.uz);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The factor stroke widths, dashes and corner radii scale by when the
|
|
157
|
+
* remainder is baked: exact for a uniform scale, the geometric mean of the
|
|
158
|
+
* axis scales otherwise (the caller reports that approximation).
|
|
159
|
+
*/
|
|
160
|
+
export function remainderStrokeScale(parts) {
|
|
161
|
+
if (remainderIsUniformScale(parts))
|
|
162
|
+
return parts.ux;
|
|
163
|
+
return Math.sqrt(Math.abs(parts.ux * parts.uz));
|
|
164
|
+
}
|
|
165
|
+
/** Splits a matrix into `translate × rotate × remainder`. */
|
|
166
|
+
export function decomposeMatrix(matrix) {
|
|
167
|
+
const [a, b, c, d, e, f] = matrix;
|
|
168
|
+
const rotation = Math.atan2(b, a);
|
|
169
|
+
const cos = Math.cos(rotation);
|
|
170
|
+
const sin = Math.sin(rotation);
|
|
171
|
+
let degrees = rotation * DEGREES;
|
|
172
|
+
if (degrees <= -180)
|
|
173
|
+
degrees += 360;
|
|
174
|
+
if (nearZero(degrees))
|
|
175
|
+
degrees = 0;
|
|
176
|
+
return {
|
|
177
|
+
rotation: degrees,
|
|
178
|
+
ux: cos * a + sin * b,
|
|
179
|
+
uy: cos * c + sin * d,
|
|
180
|
+
uz: -sin * c + cos * d,
|
|
181
|
+
translateX: nearZero(e) ? 0 : e,
|
|
182
|
+
translateY: nearZero(f) ? 0 : f,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/** The rotation part of a decomposed matrix as a matrix, with an optional translation. */
|
|
186
|
+
export function rotationMatrix(degrees, tx = 0, ty = 0) {
|
|
187
|
+
const radians = degrees * RADIANS;
|
|
188
|
+
const cos = Math.cos(radians);
|
|
189
|
+
const sin = Math.sin(radians);
|
|
190
|
+
return [cos, sin, -sin, cos, tx, ty];
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The inverse of an invertible matrix, or null when it collapses the plane.
|
|
194
|
+
* `multiplyTransforms(invertMatrix(m), m)` is the identity.
|
|
195
|
+
*/
|
|
196
|
+
export function invertMatrix(matrix) {
|
|
197
|
+
const [a, b, c, d, e, f] = matrix;
|
|
198
|
+
if (matrixIsDegenerate(matrix))
|
|
199
|
+
return null;
|
|
200
|
+
const determinant = a * d - b * c;
|
|
201
|
+
return [
|
|
202
|
+
d / determinant,
|
|
203
|
+
-b / determinant,
|
|
204
|
+
-c / determinant,
|
|
205
|
+
a / determinant,
|
|
206
|
+
(c * f - d * e) / determinant,
|
|
207
|
+
(b * e - a * f) / determinant,
|
|
208
|
+
];
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The frame position (top-left) a framed layer takes so that the model's
|
|
212
|
+
* `nodeLocalTransform` maps the layer's local origin to `origin`. The model
|
|
213
|
+
* flips and rotates about the frame's middle and then translates by the
|
|
214
|
+
* frame position, so the position is `origin` minus where a frame at (0, 0)
|
|
215
|
+
* with the same size, rotation and flips sends its local origin.
|
|
216
|
+
*/
|
|
217
|
+
export function framePositionFor(rotationDegrees, flipY, width, height, origin) {
|
|
218
|
+
const atZero = frameLocalTransform({ x: 0, y: 0, width, height }, rotationDegrees, flipY);
|
|
219
|
+
return { x: origin.x - atZero[4], y: origin.y - atZero[5] };
|
|
220
|
+
}
|
|
221
|
+
/** The model's `nodeLocalTransform` for a framed layer with this frame, rotation and flip. */
|
|
222
|
+
export function frameLocalTransform(frame, rotationDegrees, flipY) {
|
|
223
|
+
return nodeLocalTransform({
|
|
224
|
+
type: "frame",
|
|
225
|
+
frame,
|
|
226
|
+
rotation: rotationDegrees,
|
|
227
|
+
...(flipY ? { flipY: true } : {}),
|
|
228
|
+
});
|
|
229
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { SvgElementLike } from "../public-types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* One parsed node as plain data, so a tree a host parsed (a browser
|
|
4
|
+
* `DOMParser` result) can be posted to a Web Worker, which has no DOM, and
|
|
5
|
+
* imported there. A tree is a flat list of nodes in document order, each
|
|
6
|
+
* with the index of its parent in the list (-1 for the root), so it can be
|
|
7
|
+
* posted in pieces. Elements keep their local name, namespace and attributes
|
|
8
|
+
* in document order (as flat name, value pairs, by qualified name); any
|
|
9
|
+
* other node keeps its node type and value.
|
|
10
|
+
*/
|
|
11
|
+
export type SvgNodeData = {
|
|
12
|
+
readonly nodeType: 1;
|
|
13
|
+
readonly parent: number;
|
|
14
|
+
readonly localName: string;
|
|
15
|
+
readonly namespaceURI: string | null;
|
|
16
|
+
readonly attributes: readonly string[];
|
|
17
|
+
} | {
|
|
18
|
+
readonly nodeType: number;
|
|
19
|
+
readonly parent: number;
|
|
20
|
+
readonly nodeValue: string | null;
|
|
21
|
+
};
|
|
22
|
+
/** A parsed tree as plain data: its root first, then every node in document order; see `SvgNodeData`. */
|
|
23
|
+
export type SvgTreeData = readonly SvgNodeData[];
|
|
24
|
+
/**
|
|
25
|
+
* How much one piece of `svgElementDataPieces` holds, counting each node
|
|
26
|
+
* and each attribute as one: small enough that copying and posting one
|
|
27
|
+
* piece takes well under a millisecond.
|
|
28
|
+
*/
|
|
29
|
+
export declare const SVG_TREE_DATA_PIECE = 500;
|
|
30
|
+
/**
|
|
31
|
+
* Copies `root` and everything under it into plain data, in document order,
|
|
32
|
+
* and yields it in pieces of about `SVG_TREE_DATA_PIECE` (the pieces put
|
|
33
|
+
* together are the tree). A host on a page copies and posts one piece at a
|
|
34
|
+
* time, between frames, so neither the copy nor one `postMessage` grows with
|
|
35
|
+
* the tree. The copy stops after the element that is one past
|
|
36
|
+
* `maxElements`: the importer counts elements in document order and refuses
|
|
37
|
+
* at that one (`too-many-elements`), so the cut tree is refused as the whole
|
|
38
|
+
* one is, without copying the rest. The walk keeps its own stack, so a deep
|
|
39
|
+
* tree cannot overflow the call stack.
|
|
40
|
+
*/
|
|
41
|
+
export declare function svgElementDataPieces(root: SvgElementLike, maxElements?: number, pieceSize?: number): Generator<SvgNodeData[], void, void>;
|
|
42
|
+
/** Copies `root` and everything under it into plain data at once; see `svgElementDataPieces`. */
|
|
43
|
+
export declare function svgElementData(root: SvgElementLike, maxElements?: number): SvgTreeData;
|
|
44
|
+
/**
|
|
45
|
+
* The element tree `svgElementData` (or the pieces of
|
|
46
|
+
* `svgElementDataPieces`) copied, read back as elements the importer takes
|
|
47
|
+
* (`importSvg`, or `importSvgMarkup` through its `parseSvg`). Each element
|
|
48
|
+
* is one object, so the importer's per-element bookkeeping sees the same
|
|
49
|
+
* element every time, and the import gives the same output as from the tree
|
|
50
|
+
* that was copied. Throws when the data is not such a tree.
|
|
51
|
+
*/
|
|
52
|
+
export declare function svgElementFromData(tree: SvgTreeData): SvgElementLike;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/** The DOM's element node type. */
|
|
2
|
+
const ELEMENT_NODE = 1;
|
|
3
|
+
/** Text and CDATA nodes: the ones an element's `textContent` is made of. */
|
|
4
|
+
const TEXT_NODE_TYPES = new Set([3, 4]);
|
|
5
|
+
function isElement(node) {
|
|
6
|
+
return node.nodeType === ELEMENT_NODE;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* How much one piece of `svgElementDataPieces` holds, counting each node
|
|
10
|
+
* and each attribute as one: small enough that copying and posting one
|
|
11
|
+
* piece takes well under a millisecond.
|
|
12
|
+
*/
|
|
13
|
+
export const SVG_TREE_DATA_PIECE = 500;
|
|
14
|
+
/**
|
|
15
|
+
* Copies `root` and everything under it into plain data, in document order,
|
|
16
|
+
* and yields it in pieces of about `SVG_TREE_DATA_PIECE` (the pieces put
|
|
17
|
+
* together are the tree). A host on a page copies and posts one piece at a
|
|
18
|
+
* time, between frames, so neither the copy nor one `postMessage` grows with
|
|
19
|
+
* the tree. The copy stops after the element that is one past
|
|
20
|
+
* `maxElements`: the importer counts elements in document order and refuses
|
|
21
|
+
* at that one (`too-many-elements`), so the cut tree is refused as the whole
|
|
22
|
+
* one is, without copying the rest. The walk keeps its own stack, so a deep
|
|
23
|
+
* tree cannot overflow the call stack.
|
|
24
|
+
*/
|
|
25
|
+
export function* svgElementDataPieces(root, maxElements = Infinity, pieceSize = SVG_TREE_DATA_PIECE) {
|
|
26
|
+
let piece = [];
|
|
27
|
+
let pieceWeight = 0;
|
|
28
|
+
let copied = 0;
|
|
29
|
+
let elements = 0;
|
|
30
|
+
const copy = (element, parent) => {
|
|
31
|
+
const attributes = [];
|
|
32
|
+
for (let index = 0; index < element.attributes.length; index += 1) {
|
|
33
|
+
const attribute = element.attributes[index];
|
|
34
|
+
attributes.push(attribute.name, attribute.value);
|
|
35
|
+
}
|
|
36
|
+
piece.push({ nodeType: ELEMENT_NODE, parent, localName: element.localName, namespaceURI: element.namespaceURI, attributes });
|
|
37
|
+
pieceWeight += 1 + element.attributes.length;
|
|
38
|
+
elements += 1;
|
|
39
|
+
return copied++;
|
|
40
|
+
};
|
|
41
|
+
const stack = [{ element: root, at: copy(root, -1), next: 0 }];
|
|
42
|
+
while (stack.length > 0 && elements <= maxElements) {
|
|
43
|
+
if (pieceWeight >= pieceSize) {
|
|
44
|
+
yield piece;
|
|
45
|
+
piece = [];
|
|
46
|
+
pieceWeight = 0;
|
|
47
|
+
}
|
|
48
|
+
const top = stack[stack.length - 1];
|
|
49
|
+
if (top.next >= top.element.childNodes.length) {
|
|
50
|
+
stack.pop();
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const child = top.element.childNodes[top.next];
|
|
54
|
+
top.next += 1;
|
|
55
|
+
if (isElement(child)) {
|
|
56
|
+
stack.push({ element: child, at: copy(child, top.at), next: 0 });
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
piece.push({ nodeType: child.nodeType, parent: top.at, nodeValue: child.nodeValue });
|
|
60
|
+
pieceWeight += 1;
|
|
61
|
+
copied += 1;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (piece.length > 0)
|
|
65
|
+
yield piece;
|
|
66
|
+
}
|
|
67
|
+
/** Copies `root` and everything under it into plain data at once; see `svgElementDataPieces`. */
|
|
68
|
+
export function svgElementData(root, maxElements = Infinity) {
|
|
69
|
+
const tree = [];
|
|
70
|
+
for (const piece of svgElementDataPieces(root, maxElements)) {
|
|
71
|
+
for (const node of piece)
|
|
72
|
+
tree.push(node);
|
|
73
|
+
}
|
|
74
|
+
return tree;
|
|
75
|
+
}
|
|
76
|
+
/** One element read back from its data, for the importer. */
|
|
77
|
+
class DataElement {
|
|
78
|
+
nodeType = ELEMENT_NODE;
|
|
79
|
+
localName;
|
|
80
|
+
namespaceURI;
|
|
81
|
+
attributes;
|
|
82
|
+
childNodes = [];
|
|
83
|
+
children = [];
|
|
84
|
+
values = new Map();
|
|
85
|
+
text = null;
|
|
86
|
+
constructor(data) {
|
|
87
|
+
this.localName = data.localName;
|
|
88
|
+
this.namespaceURI = data.namespaceURI;
|
|
89
|
+
const attributes = [];
|
|
90
|
+
for (let index = 0; index + 1 < data.attributes.length; index += 2) {
|
|
91
|
+
const name = data.attributes[index];
|
|
92
|
+
const value = data.attributes[index + 1];
|
|
93
|
+
attributes.push({ name, value });
|
|
94
|
+
// As the DOM's getAttribute: the first attribute of that qualified name.
|
|
95
|
+
if (!this.values.has(name))
|
|
96
|
+
this.values.set(name, value);
|
|
97
|
+
}
|
|
98
|
+
this.attributes = attributes;
|
|
99
|
+
}
|
|
100
|
+
getAttribute(name) {
|
|
101
|
+
return this.values.get(name) ?? null;
|
|
102
|
+
}
|
|
103
|
+
/** As the DOM's: the text and CDATA of every descendant, in document order. */
|
|
104
|
+
get textContent() {
|
|
105
|
+
if (this.text !== null)
|
|
106
|
+
return this.text;
|
|
107
|
+
const parts = [];
|
|
108
|
+
const stack = [this];
|
|
109
|
+
while (stack.length > 0) {
|
|
110
|
+
const node = stack.pop();
|
|
111
|
+
if (isElement(node)) {
|
|
112
|
+
for (let index = node.childNodes.length - 1; index >= 0; index -= 1)
|
|
113
|
+
stack.push(node.childNodes[index]);
|
|
114
|
+
}
|
|
115
|
+
else if (TEXT_NODE_TYPES.has(node.nodeType)) {
|
|
116
|
+
parts.push(node.nodeValue ?? "");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
this.text = parts.join("");
|
|
120
|
+
return this.text;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The element tree `svgElementData` (or the pieces of
|
|
125
|
+
* `svgElementDataPieces`) copied, read back as elements the importer takes
|
|
126
|
+
* (`importSvg`, or `importSvgMarkup` through its `parseSvg`). Each element
|
|
127
|
+
* is one object, so the importer's per-element bookkeeping sees the same
|
|
128
|
+
* element every time, and the import gives the same output as from the tree
|
|
129
|
+
* that was copied. Throws when the data is not such a tree.
|
|
130
|
+
*/
|
|
131
|
+
export function svgElementFromData(tree) {
|
|
132
|
+
const elements = [];
|
|
133
|
+
for (const [index, node] of tree.entries()) {
|
|
134
|
+
// A parent comes before its children, so it was read back already.
|
|
135
|
+
const parent = index === 0 ? null : elements[node.parent] ?? null;
|
|
136
|
+
if (index > 0 && parent === null)
|
|
137
|
+
throw new Error(`the SVG tree data's node ${String(index)} has no parent element before it`);
|
|
138
|
+
if (node.nodeType === ELEMENT_NODE && "localName" in node) {
|
|
139
|
+
const element = new DataElement(node);
|
|
140
|
+
elements.push(element);
|
|
141
|
+
parent?.childNodes.push(element);
|
|
142
|
+
parent?.children.push(element);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
if (parent === null)
|
|
146
|
+
throw new Error("the SVG tree data's root is not an element");
|
|
147
|
+
elements.push(null);
|
|
148
|
+
parent.childNodes.push({ nodeType: node.nodeType, nodeValue: "nodeValue" in node ? node.nodeValue : null });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const root = elements[0];
|
|
152
|
+
if (root === undefined || root === null)
|
|
153
|
+
throw new Error("the SVG tree data is empty");
|
|
154
|
+
return root;
|
|
155
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { SvgElementLike } from "../public-types.ts";
|
|
2
|
+
/** The SVG namespace, `http://www.w3.org/2000/svg`. */
|
|
3
|
+
export declare const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
4
|
+
/** The XLink namespace, still written by exporters for `xlink:href`. */
|
|
5
|
+
export declare const XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
|
|
6
|
+
/** Whether the element is in the SVG namespace (or no namespace, as tests may parse without one). */
|
|
7
|
+
export declare function isSvgElement(element: SvgElementLike, localName?: string): boolean;
|
|
8
|
+
/** One attribute's trimmed value, or null when the attribute is absent or empty. */
|
|
9
|
+
export declare function attr(element: SvgElementLike, name: string): string | null;
|
|
10
|
+
/** The element's `id`, or null. */
|
|
11
|
+
export declare function elementId(element: SvgElementLike): string | null;
|
|
12
|
+
/** The element's classes, split from its `class` attribute. */
|
|
13
|
+
export declare function elementClasses(element: SvgElementLike): readonly string[];
|
|
14
|
+
/** The element children, as a plain array. */
|
|
15
|
+
export declare function childElements(element: SvgElementLike): readonly SvgElementLike[];
|
|
16
|
+
/** A `url(#id)` reference's id, or null when the value is not one. */
|
|
17
|
+
export declare function urlReferenceId(value: string): string | null;
|
|
18
|
+
/** An `href` attribute's target id, or null for missing, non-fragment or external references. */
|
|
19
|
+
export declare function hrefTargetId(element: SvgElementLike): string | null;
|
|
20
|
+
/** Whether the `href` points outside this document (a full URL or another file). */
|
|
21
|
+
export declare function hrefIsExternal(element: SvgElementLike): boolean;
|
|
22
|
+
/** The raw `href` value, whichever spelling it used. */
|
|
23
|
+
export declare function hrefValue(element: SvgElementLike): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* One step of an `elementPath`: the tag, plus `#id` when it has one, plus
|
|
26
|
+
* `[n]` when it is not the first element sibling with that tag and has no
|
|
27
|
+
* id to disambiguate it, so `svg > g#icons > use[2]` reads like the
|
|
28
|
+
* document. A tag or id cut short no longer tells siblings apart, so a step
|
|
29
|
+
* with a cut name keeps its `[n]` too.
|
|
30
|
+
*/
|
|
31
|
+
export declare function pathStep(element: SvgElementLike, sameTagIndex: number): string;
|
|
32
|
+
/**
|
|
33
|
+
* The 1-based position of `element` among its siblings with the same tag, as
|
|
34
|
+
* an `elementPath` step spells it (long tags cut short count as one tag).
|
|
35
|
+
*/
|
|
36
|
+
export declare function sameTagIndex(parent: SvgElementLike | null, element: SvgElementLike): number;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/** The SVG namespace, `http://www.w3.org/2000/svg`. */
|
|
2
|
+
export const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
3
|
+
/** The XLink namespace, still written by exporters for `xlink:href`. */
|
|
4
|
+
export const XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
|
|
5
|
+
/** Whether the element is in the SVG namespace (or no namespace, as tests may parse without one). */
|
|
6
|
+
export function isSvgElement(element, localName) {
|
|
7
|
+
const inSvg = element.namespaceURI === SVG_NAMESPACE || element.namespaceURI === null;
|
|
8
|
+
return inSvg && (localName === undefined || element.localName === localName);
|
|
9
|
+
}
|
|
10
|
+
/** One attribute's trimmed value, or null when the attribute is absent or empty. */
|
|
11
|
+
export function attr(element, name) {
|
|
12
|
+
const value = element.getAttribute(name);
|
|
13
|
+
if (value === null)
|
|
14
|
+
return null;
|
|
15
|
+
const trimmed = value.trim();
|
|
16
|
+
return trimmed.length === 0 ? null : trimmed;
|
|
17
|
+
}
|
|
18
|
+
/** The element's `id`, or null. */
|
|
19
|
+
export function elementId(element) {
|
|
20
|
+
return attr(element, "id");
|
|
21
|
+
}
|
|
22
|
+
/** The element's classes, split from its `class` attribute. */
|
|
23
|
+
export function elementClasses(element) {
|
|
24
|
+
const value = attr(element, "class");
|
|
25
|
+
if (value === null)
|
|
26
|
+
return [];
|
|
27
|
+
return value.split(/\s+/).filter((name) => name.length > 0);
|
|
28
|
+
}
|
|
29
|
+
/** The element children, as a plain array. */
|
|
30
|
+
export function childElements(element) {
|
|
31
|
+
return Array.from({ length: element.children.length }, (_, index) => element.children[index]);
|
|
32
|
+
}
|
|
33
|
+
/** A `url(#id)` reference's id, or null when the value is not one. */
|
|
34
|
+
export function urlReferenceId(value) {
|
|
35
|
+
const match = /^url\(\s*(['"]?)#([^'")\s]+)\1\s*\)$/.exec(value.trim());
|
|
36
|
+
return match === null ? null : match[2] ?? null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The reference of an element's `href`, or of its `xlink:href` when `href`
|
|
40
|
+
* is absent. Per SVG 2, `href` wins whenever it is present, even empty or
|
|
41
|
+
* white space only, and an empty one is no reference at all — so presence,
|
|
42
|
+
* not the trimmed value, decides. Null when the winning attribute is absent
|
|
43
|
+
* or empty.
|
|
44
|
+
*/
|
|
45
|
+
function hrefAttribute(element) {
|
|
46
|
+
if (element.getAttribute("href") !== null)
|
|
47
|
+
return attr(element, "href");
|
|
48
|
+
return attr(element, "xlink:href");
|
|
49
|
+
}
|
|
50
|
+
/** An `href` attribute's target id, or null for missing, non-fragment or external references. */
|
|
51
|
+
export function hrefTargetId(element) {
|
|
52
|
+
const href = hrefAttribute(element);
|
|
53
|
+
if (href === null)
|
|
54
|
+
return null;
|
|
55
|
+
if (!href.startsWith("#"))
|
|
56
|
+
return null;
|
|
57
|
+
const id = href.slice(1);
|
|
58
|
+
return id.length === 0 ? null : id;
|
|
59
|
+
}
|
|
60
|
+
/** Whether the `href` points outside this document (a full URL or another file). */
|
|
61
|
+
export function hrefIsExternal(element) {
|
|
62
|
+
const href = hrefAttribute(element);
|
|
63
|
+
return href !== null && !href.startsWith("#");
|
|
64
|
+
}
|
|
65
|
+
/** The raw `href` value, whichever spelling it used. */
|
|
66
|
+
export function hrefValue(element) {
|
|
67
|
+
return hrefAttribute(element);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* One step of an `elementPath`: the tag, plus `#id` when it has one, plus
|
|
71
|
+
* `[n]` when it is not the first element sibling with that tag and has no
|
|
72
|
+
* id to disambiguate it, so `svg > g#icons > use[2]` reads like the
|
|
73
|
+
* document. A tag or id cut short no longer tells siblings apart, so a step
|
|
74
|
+
* with a cut name keeps its `[n]` too.
|
|
75
|
+
*/
|
|
76
|
+
export function pathStep(element, sameTagIndex) {
|
|
77
|
+
const name = pathName(element.localName);
|
|
78
|
+
const id = elementId(element);
|
|
79
|
+
const shortId = id === null ? null : pathName(id);
|
|
80
|
+
const cut = name !== element.localName || shortId !== id;
|
|
81
|
+
const suffix = sameTagIndex > 1 && (id === null || cut) ? `[${String(sameTagIndex)}]` : "";
|
|
82
|
+
return shortId === null ? `${name}${suffix}` : `${name}#${shortId}${suffix}`;
|
|
83
|
+
}
|
|
84
|
+
/** The longest tag or id an `elementPath` step spells out in full. */
|
|
85
|
+
const PATH_NAME_LENGTH = 64;
|
|
86
|
+
/**
|
|
87
|
+
* A tag or id cut to PATH_NAME_LENGTH characters plus an ellipsis. Every
|
|
88
|
+
* descendant's path repeats its ancestors' steps, so a long id copied whole
|
|
89
|
+
* would multiply the input's size by the number of report lines under it.
|
|
90
|
+
*/
|
|
91
|
+
function pathName(text) {
|
|
92
|
+
return text.length <= PATH_NAME_LENGTH ? text : `${text.slice(0, PATH_NAME_LENGTH)}…`;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Each parent's children numbered by tag, built in one pass the first time
|
|
96
|
+
* any of its children is asked for, so numbering every child of a parent is
|
|
97
|
+
* linear in its child count, not quadratic.
|
|
98
|
+
*/
|
|
99
|
+
const sameTagIndexes = new WeakMap();
|
|
100
|
+
/**
|
|
101
|
+
* The 1-based position of `element` among its siblings with the same tag, as
|
|
102
|
+
* an `elementPath` step spells it (long tags cut short count as one tag).
|
|
103
|
+
*/
|
|
104
|
+
export function sameTagIndex(parent, element) {
|
|
105
|
+
if (parent === null)
|
|
106
|
+
return 1;
|
|
107
|
+
let cached = sameTagIndexes.get(parent);
|
|
108
|
+
if (cached === undefined || cached.childCount !== parent.children.length) {
|
|
109
|
+
const counts = new Map();
|
|
110
|
+
const indexes = new Map();
|
|
111
|
+
for (const child of childElements(parent)) {
|
|
112
|
+
const tag = pathName(child.localName);
|
|
113
|
+
const index = (counts.get(tag) ?? 0) + 1;
|
|
114
|
+
counts.set(tag, index);
|
|
115
|
+
indexes.set(child, index);
|
|
116
|
+
}
|
|
117
|
+
cached = { childCount: parent.children.length, indexes };
|
|
118
|
+
sameTagIndexes.set(parent, cached);
|
|
119
|
+
}
|
|
120
|
+
return cached.indexes.get(element) ?? 1;
|
|
121
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** A parsed SVG length: absolute px, or a percentage as a fraction (50% → 0.5). */
|
|
2
|
+
export type SvgLength = {
|
|
3
|
+
readonly kind: "absolute";
|
|
4
|
+
readonly value: number;
|
|
5
|
+
} | {
|
|
6
|
+
readonly kind: "percent";
|
|
7
|
+
readonly fraction: number;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Font-relative units need the element's font size in px, and `rem` the
|
|
11
|
+
* root element's; without `rootFontSize` a `rem` length cannot be read.
|
|
12
|
+
*/
|
|
13
|
+
export interface SvgLengthContext {
|
|
14
|
+
readonly fontSize: number;
|
|
15
|
+
readonly rootFontSize?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Parses one SVG length into px. Unitless and `px` are 1:1; `pt` ×96/72,
|
|
19
|
+
* `pc` ×16, `in` ×96, `cm` ×96/2.54, `mm` ×96/25.4, `q` ×96/101.6;
|
|
20
|
+
* `em` ×fontSize, `ex` ×fontSize/2 and `rem` ×rootFontSize (from
|
|
21
|
+
* `context`; `rem` only when the context has the root size). Units are read in
|
|
22
|
+
* any case. A trailing `%` yields a percent
|
|
23
|
+
* fraction and never touches the context. Returns null for empty text,
|
|
24
|
+
* junk, other units, or a non-finite number — the caller reports that;
|
|
25
|
+
* nothing is guessed.
|
|
26
|
+
*/
|
|
27
|
+
export declare function parseSvgLength(text: string, context: SvgLengthContext): SvgLength | null;
|
|
28
|
+
/** Parses a plain SVG number (optional sign, decimals, exponent). Null on junk or non-finite. */
|
|
29
|
+
export declare function parseSvgNumber(text: string): number | null;
|
|
30
|
+
/**
|
|
31
|
+
* Splits a comma/whitespace-separated number list, as `stroke-dasharray`
|
|
32
|
+
* and `viewBox` use. Returns null when any entry fails
|
|
33
|
+
* to parse or the list is empty; never returns a partial list.
|
|
34
|
+
*/
|
|
35
|
+
export declare function parseSvgNumberList(text: string): readonly number[] | null;
|
|
36
|
+
/**
|
|
37
|
+
* Reads a `points` list by the SVG points grammar: numbers are split by
|
|
38
|
+
* whitespace, at most one comma, or nothing when the next number starts with
|
|
39
|
+
* a sign or a second dot (`10-20` is 10 then -20, `1.5.5` is 1.5 then 0.5),
|
|
40
|
+
* as path data allows. Returns null when the list is empty, has an empty
|
|
41
|
+
* entry or a trailing comma, or holds anything that is not a number.
|
|
42
|
+
*/
|
|
43
|
+
export declare function parsePointList(text: string): readonly number[] | null;
|