@moondraft/import 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +394 -0
- package/dist/adapter/convert.d.ts +144 -0
- package/dist/adapter/convert.js +2811 -0
- package/dist/adapter/figma-descriptions.d.ts +7 -0
- package/dist/adapter/figma-descriptions.js +34 -0
- package/dist/adapter/figma-grids.d.ts +12 -0
- package/dist/adapter/figma-grids.js +175 -0
- package/dist/adapter/figma-handoff.d.ts +12 -0
- package/dist/adapter/figma-handoff.js +167 -0
- package/dist/adapter/figma-hyperlinks.d.ts +13 -0
- package/dist/adapter/figma-hyperlinks.js +77 -0
- package/dist/adapter/figma.d.ts +12 -0
- package/dist/adapter/figma.js +40 -0
- package/dist/adapter/file-components.d.ts +20 -0
- package/dist/adapter/file-components.js +352 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +851 -0
- package/dist/adapter/image-crop.d.ts +18 -0
- package/dist/adapter/image-crop.js +30 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +49 -0
- package/dist/adapter/link.js +892 -0
- package/dist/adapter/payload.d.ts +31 -0
- package/dist/adapter/payload.js +78 -0
- package/dist/adapter/shapes.d.ts +31 -0
- package/dist/adapter/shapes.js +75 -0
- package/dist/adapter/variables.d.ts +45 -0
- package/dist/adapter/variables.js +106 -0
- package/dist/figma-file.d.ts +38 -0
- package/dist/figma-file.js +97 -0
- package/dist/figma-worker.d.ts +11 -0
- package/dist/figma-worker.js +12 -0
- package/dist/figma.d.ts +26 -0
- package/dist/figma.js +230 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +50 -0
- package/dist/report.d.ts +42 -0
- package/dist/report.js +142 -0
- package/dist/serialize.d.ts +7 -0
- package/dist/serialize.js +79 -0
- package/dist/svg.d.ts +33 -0
- package/dist/svg.js +44 -0
- package/dist/syntax.d.ts +8 -0
- package/dist/syntax.js +19 -0
- package/dist/taco/figma-import/clipboard.d.ts +25 -0
- package/dist/taco/figma-import/clipboard.js +87 -0
- package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
- package/dist/taco/figma-import/clipboardSize.js +107 -0
- package/dist/taco/figma-import/container/compression.d.ts +9 -0
- package/dist/taco/figma-import/container/compression.js +174 -0
- package/dist/taco/figma-import/container/error.d.ts +15 -0
- package/dist/taco/figma-import/container/error.js +19 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +41 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
- package/dist/taco/figma-import/container/limits.d.ts +22 -0
- package/dist/taco/figma-import/container/limits.js +22 -0
- package/dist/taco/figma-import/container/zip.d.ts +14 -0
- package/dist/taco/figma-import/container/zip.js +154 -0
- package/dist/taco/figma-import/figFile.d.ts +34 -0
- package/dist/taco/figma-import/figFile.js +91 -0
- package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
- package/dist/taco/figma-import/figFileDocument.js +100 -0
- package/dist/taco/figma-import/import/blobs.d.ts +10 -0
- package/dist/taco/figma-import/import/blobs.js +23 -0
- package/dist/taco/figma-import/import/carry.d.ts +24 -0
- package/dist/taco/figma-import/import/carry.js +66 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
- package/dist/taco/figma-import/import/componentProps.js +329 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +85 -0
- package/dist/taco/figma-import/import/componentSets.js +647 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
- package/dist/taco/figma-import/import/converters/effects.js +127 -0
- package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
- package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
- package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
- package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +63 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
- package/dist/taco/figma-import/import/converters/gradient.js +108 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
- package/dist/taco/figma-import/import/converters/group.js +30 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +94 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
- package/dist/taco/figma-import/import/converters/layout.js +187 -0
- package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
- package/dist/taco/figma-import/import/converters/line.js +40 -0
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
- package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
- package/dist/taco/figma-import/import/converters/section.d.ts +8 -0
- package/dist/taco/figma-import/import/converters/section.js +29 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
- package/dist/taco/figma-import/import/converters/shared.js +372 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
- package/dist/taco/figma-import/import/converters/text.js +707 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
- package/dist/taco/figma-import/import/converters/vector.js +609 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -0
- package/dist/taco/figma-import/import/fields.d.ts +15 -0
- package/dist/taco/figma-import/import/fields.js +29 -0
- package/dist/taco/figma-import/import/instances.d.ts +184 -0
- package/dist/taco/figma-import/import/instances.js +1530 -0
- package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
- package/dist/taco/figma-import/import/nestingCycles.js +488 -0
- package/dist/taco/figma-import/import/slots.d.ts +56 -0
- package/dist/taco/figma-import/import/slots.js +96 -0
- package/dist/taco/figma-import/import/styles.d.ts +48 -0
- package/dist/taco/figma-import/import/styles.js +48 -0
- package/dist/taco/figma-import/import/tree.d.ts +66 -0
- package/dist/taco/figma-import/import/tree.js +194 -0
- package/dist/taco/figma-import/import/types.d.ts +232 -0
- package/dist/taco/figma-import/import/types.js +33 -0
- package/dist/taco/figma-import/import/variables.d.ts +137 -0
- package/dist/taco/figma-import/import/variables.js +769 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +520 -0
- package/dist/taco/figma-import/import/walk.d.ts +207 -0
- package/dist/taco/figma-import/import/walk.js +1464 -0
- package/dist/taco/figma-import/index.d.ts +22 -0
- package/dist/taco/figma-import/index.js +13 -0
- package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
- package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
- package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
- package/dist/taco/figma-import/kiwi/error.js +29 -0
- package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
- package/dist/taco/figma-import/kiwi/limits.js +18 -0
- package/dist/taco/figma-import/kiwi/message.d.ts +12 -0
- package/dist/taco/figma-import/kiwi/message.js +147 -0
- package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
- package/dist/taco/figma-import/kiwi/schema.js +206 -0
- package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
- package/dist/taco/figma-import/kiwi/types.js +18 -0
- package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
- package/dist/taco/model/agent-hidden-fields.js +29 -0
- package/dist/taco/model/asset-users.d.ts +23 -0
- package/dist/taco/model/asset-users.js +128 -0
- package/dist/taco/model/auto-layout-lines.d.ts +32 -0
- package/dist/taco/model/auto-layout-lines.js +70 -0
- package/dist/taco/model/codec.d.ts +444 -0
- package/dist/taco/model/codec.js +5665 -0
- package/dist/taco/model/color.d.ts +11 -0
- package/dist/taco/model/color.js +61 -0
- package/dist/taco/model/comment-operations.d.ts +102 -0
- package/dist/taco/model/comment-operations.js +227 -0
- package/dist/taco/model/component-evaluation.d.ts +96 -0
- package/dist/taco/model/component-evaluation.js +555 -0
- package/dist/taco/model/component-expressions.d.ts +242 -0
- package/dist/taco/model/component-expressions.js +1082 -0
- package/dist/taco/model/component-properties.d.ts +111 -0
- package/dist/taco/model/component-properties.js +339 -0
- package/dist/taco/model/component-samples.d.ts +83 -0
- package/dist/taco/model/component-samples.js +290 -0
- package/dist/taco/model/component-validity.d.ts +85 -0
- package/dist/taco/model/component-validity.js +397 -0
- package/dist/taco/model/components.d.ts +155 -0
- package/dist/taco/model/components.js +505 -0
- package/dist/taco/model/connector-ends.d.ts +20 -0
- package/dist/taco/model/connector-ends.js +188 -0
- package/dist/taco/model/connector-geometry.d.ts +74 -0
- package/dist/taco/model/connector-geometry.js +293 -0
- package/dist/taco/model/corner-radii.d.ts +26 -0
- package/dist/taco/model/corner-radii.js +47 -0
- package/dist/taco/model/derivations.d.ts +81 -0
- package/dist/taco/model/derivations.js +151 -0
- package/dist/taco/model/export-settings.d.ts +53 -0
- package/dist/taco/model/export-settings.js +156 -0
- package/dist/taco/model/expressions/ast.d.ts +153 -0
- package/dist/taco/model/expressions/ast.js +6 -0
- package/dist/taco/model/expressions/cache.d.ts +23 -0
- package/dist/taco/model/expressions/cache.js +43 -0
- package/dist/taco/model/expressions/check.d.ts +72 -0
- package/dist/taco/model/expressions/check.js +517 -0
- package/dist/taco/model/expressions/completion.d.ts +61 -0
- package/dist/taco/model/expressions/completion.js +421 -0
- package/dist/taco/model/expressions/evaluate.d.ts +32 -0
- package/dist/taco/model/expressions/evaluate.js +519 -0
- package/dist/taco/model/expressions/forms.d.ts +84 -0
- package/dist/taco/model/expressions/forms.js +200 -0
- package/dist/taco/model/expressions/index.d.ts +14 -0
- package/dist/taco/model/expressions/index.js +17 -0
- package/dist/taco/model/expressions/lexer.d.ts +42 -0
- package/dist/taco/model/expressions/lexer.js +213 -0
- package/dist/taco/model/expressions/library.d.ts +33 -0
- package/dist/taco/model/expressions/library.js +117 -0
- package/dist/taco/model/expressions/names.d.ts +43 -0
- package/dist/taco/model/expressions/names.js +99 -0
- package/dist/taco/model/expressions/parser.d.ts +15 -0
- package/dist/taco/model/expressions/parser.js +438 -0
- package/dist/taco/model/expressions/rename.d.ts +34 -0
- package/dist/taco/model/expressions/rename.js +46 -0
- package/dist/taco/model/expressions/source.d.ts +15 -0
- package/dist/taco/model/expressions/source.js +161 -0
- package/dist/taco/model/expressions/types.d.ts +119 -0
- package/dist/taco/model/expressions/types.js +102 -0
- package/dist/taco/model/expressions/values.d.ts +97 -0
- package/dist/taco/model/expressions/values.js +160 -0
- package/dist/taco/model/geometry-limits.d.ts +16 -0
- package/dist/taco/model/geometry-limits.js +67 -0
- package/dist/taco/model/geometry.d.ts +47 -0
- package/dist/taco/model/geometry.js +193 -0
- package/dist/taco/model/group-bounds.d.ts +38 -0
- package/dist/taco/model/group-bounds.js +144 -0
- package/dist/taco/model/id-table.d.ts +37 -0
- package/dist/taco/model/id-table.js +80 -0
- package/dist/taco/model/index.d.ts +25 -0
- package/dist/taco/model/index.js +27 -0
- package/dist/taco/model/instance-resolver.d.ts +376 -0
- package/dist/taco/model/instance-resolver.js +1778 -0
- package/dist/taco/model/layer-syntax.d.ts +340 -0
- package/dist/taco/model/layer-syntax.js +3506 -0
- package/dist/taco/model/layout/constraints.d.ts +84 -0
- package/dist/taco/model/layout/constraints.js +445 -0
- package/dist/taco/model/layout/frame-layout.d.ts +46 -0
- package/dist/taco/model/layout/frame-layout.js +618 -0
- package/dist/taco/model/layout/node-sizing.d.ts +163 -0
- package/dist/taco/model/layout/node-sizing.js +486 -0
- package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
- package/dist/taco/model/legacy-connector-caps.js +70 -0
- package/dist/taco/model/linear-call.d.ts +48 -0
- package/dist/taco/model/linear-call.js +58 -0
- package/dist/taco/model/new-nodes.d.ts +145 -0
- package/dist/taco/model/new-nodes.js +277 -0
- package/dist/taco/model/node-builder.d.ts +449 -0
- package/dist/taco/model/node-builder.js +2294 -0
- package/dist/taco/model/node-table-watch.d.ts +36 -0
- package/dist/taco/model/node-table-watch.js +63 -0
- package/dist/taco/model/node-transform.d.ts +39 -0
- package/dist/taco/model/node-transform.js +79 -0
- package/dist/taco/model/node-users-index.d.ts +10 -0
- package/dist/taco/model/node-users-index.js +82 -0
- package/dist/taco/model/operation-references.d.ts +80 -0
- package/dist/taco/model/operation-references.js +399 -0
- package/dist/taco/model/operations.d.ts +610 -0
- package/dist/taco/model/operations.js +2882 -0
- package/dist/taco/model/overlay-table.d.ts +71 -0
- package/dist/taco/model/overlay-table.js +171 -0
- package/dist/taco/model/override-remap.d.ts +69 -0
- package/dist/taco/model/override-remap.js +230 -0
- package/dist/taco/model/parent-table.d.ts +53 -0
- package/dist/taco/model/parent-table.js +193 -0
- package/dist/taco/model/problem-collector.d.ts +27 -0
- package/dist/taco/model/problem-collector.js +101 -0
- package/dist/taco/model/sha256.d.ts +10 -0
- package/dist/taco/model/sha256.js +79 -0
- package/dist/taco/model/shape-geometry.d.ts +96 -0
- package/dist/taco/model/shape-geometry.js +368 -0
- package/dist/taco/model/shared-node-table.d.ts +97 -0
- package/dist/taco/model/shared-node-table.js +546 -0
- package/dist/taco/model/slots.d.ts +113 -0
- package/dist/taco/model/slots.js +281 -0
- package/dist/taco/model/stroke-settings.d.ts +37 -0
- package/dist/taco/model/stroke-settings.js +90 -0
- package/dist/taco/model/svg-path-import.d.ts +89 -0
- package/dist/taco/model/svg-path-import.js +219 -0
- package/dist/taco/model/text-measurement.d.ts +142 -0
- package/dist/taco/model/text-measurement.js +846 -0
- package/dist/taco/model/text-reads.d.ts +66 -0
- package/dist/taco/model/text-reads.js +128 -0
- package/dist/taco/model/text-sizer.d.ts +358 -0
- package/dist/taco/model/text-sizer.js +996 -0
- package/dist/taco/model/theme-users.d.ts +59 -0
- package/dist/taco/model/theme-users.js +312 -0
- package/dist/taco/model/thumbnail.d.ts +15 -0
- package/dist/taco/model/thumbnail.js +34 -0
- package/dist/taco/model/types.d.ts +2369 -0
- package/dist/taco/model/types.js +366 -0
- package/dist/taco/model/unknown-property.d.ts +5 -0
- package/dist/taco/model/unknown-property.js +7 -0
- package/dist/taco/model/validated-documents.d.ts +22 -0
- package/dist/taco/model/validated-documents.js +22 -0
- package/dist/taco/model/variable-modes.d.ts +61 -0
- package/dist/taco/model/variable-modes.js +438 -0
- package/dist/taco/model/variable-references.d.ts +54 -0
- package/dist/taco/model/variable-references.js +183 -0
- package/dist/taco/model/variables.d.ts +24 -0
- package/dist/taco/model/variables.js +151 -0
- package/dist/taco/model/vector-network.d.ts +34 -0
- package/dist/taco/model/vector-network.js +411 -0
- package/dist/taco/svg-import/import-svg.d.ts +36 -0
- package/dist/taco/svg-import/import-svg.js +729 -0
- package/dist/taco/svg-import/index.d.ts +3 -0
- package/dist/taco/svg-import/index.js +3 -0
- package/dist/taco/svg-import/limits/limits.d.ts +79 -0
- package/dist/taco/svg-import/limits/limits.js +160 -0
- package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
- package/dist/taco/svg-import/paint/gradients.js +429 -0
- package/dist/taco/svg-import/paint/paint.d.ts +32 -0
- package/dist/taco/svg-import/paint/paint.js +162 -0
- package/dist/taco/svg-import/paths/paths.d.ts +58 -0
- package/dist/taco/svg-import/paths/paths.js +205 -0
- package/dist/taco/svg-import/public-types.d.ts +279 -0
- package/dist/taco/svg-import/public-types.js +56 -0
- package/dist/taco/svg-import/report/report.d.ts +44 -0
- package/dist/taco/svg-import/report/report.js +79 -0
- package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
- package/dist/taco/svg-import/reuse/reuse.js +840 -0
- package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
- package/dist/taco/svg-import/shapes/shapes.js +386 -0
- package/dist/taco/svg-import/structure/effects.d.ts +26 -0
- package/dist/taco/svg-import/structure/effects.js +618 -0
- package/dist/taco/svg-import/structure/image.d.ts +24 -0
- package/dist/taco/svg-import/structure/image.js +495 -0
- package/dist/taco/svg-import/structure/walk.d.ts +85 -0
- package/dist/taco/svg-import/structure/walk.js +544 -0
- package/dist/taco/svg-import/style/color.d.ts +40 -0
- package/dist/taco/svg-import/style/color.js +275 -0
- package/dist/taco/svg-import/style/computed.d.ts +214 -0
- package/dist/taco/svg-import/style/computed.js +1534 -0
- package/dist/taco/svg-import/style/css.d.ts +127 -0
- package/dist/taco/svg-import/style/css.js +541 -0
- package/dist/taco/svg-import/style/media.d.ts +25 -0
- package/dist/taco/svg-import/style/media.js +307 -0
- package/dist/taco/svg-import/style/properties.d.ts +43 -0
- package/dist/taco/svg-import/style/properties.js +135 -0
- package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
- package/dist/taco/svg-import/style/selector-reach.js +169 -0
- package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
- package/dist/taco/svg-import/style/selector-validity.js +307 -0
- package/dist/taco/svg-import/text/text.d.ts +29 -0
- package/dist/taco/svg-import/text/text.js +545 -0
- package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
- package/dist/taco/svg-import/transform/matrix.js +229 -0
- package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
- package/dist/taco/svg-import/tree/element-data.js +155 -0
- package/dist/taco/svg-import/tree/elements.d.ts +36 -0
- package/dist/taco/svg-import/tree/elements.js +121 -0
- package/dist/taco/svg-import/units/length.d.ts +43 -0
- package/dist/taco/svg-import/units/length.js +125 -0
- package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
- package/dist/taco/svg-import/units/viewbox.js +77 -0
- package/package.json +55 -4
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import {} from "../../model/index.js";
|
|
2
|
+
import { attr, isSvgElement } from "../tree/elements.js";
|
|
3
|
+
import { parseSvgLength } from "../units/length.js";
|
|
4
|
+
import { computeElementStyle } from "../style/computed.js";
|
|
5
|
+
import { hexColor } from "../style/color.js";
|
|
6
|
+
import { applyMatrixToPoint, negligibleNextTo, sameScale, rotationMatrix, } from "../transform/matrix.js";
|
|
7
|
+
import { layerChrome } from "../paths/paths.js";
|
|
8
|
+
const TEXT_STROKE_NOTE = { what: "Strokes on text", kept: "The text is kept without its stroke" };
|
|
9
|
+
const OVERLINE_NOTE = { what: "Overlines", kept: "The text is kept without the overline" };
|
|
10
|
+
const OBLIQUE_NOTE = { what: "Slanted text angles (oblique)", kept: "The font's own italic or slant is used" };
|
|
11
|
+
const GLYPH_POSITIONS_NOTE = { what: "Letter-by-letter positions", kept: "Only the first position is used" };
|
|
12
|
+
/**
|
|
13
|
+
* Applies SVG white-space handling to one text chunk. By default runs of
|
|
14
|
+
* white space collapse to one space; with `xml:space="preserve"` each
|
|
15
|
+
* tab and newline becomes a space and nothing collapses.
|
|
16
|
+
*/
|
|
17
|
+
function handleWhitespace(text, preserve) {
|
|
18
|
+
return preserve ? text.replace(/[\t\n\r]/g, " ") : text.replace(/[\t\n\r ]+/g, " ");
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Reads a text position attribute's first value and whether one follows it,
|
|
22
|
+
* scanning the text value by value without splitting it into an array:
|
|
23
|
+
* values split where a white space run or a comma with white space around
|
|
24
|
+
* it sits, and each value scanned counts one unit of style work, so a
|
|
25
|
+
* position attribute with millions of values is refused instead of growing
|
|
26
|
+
* memory without end.
|
|
27
|
+
*/
|
|
28
|
+
function scanPositionList(text, ctx) {
|
|
29
|
+
let parts = 0;
|
|
30
|
+
let first = "";
|
|
31
|
+
let start = 0;
|
|
32
|
+
let index = 0;
|
|
33
|
+
const flush = (end) => {
|
|
34
|
+
ctx.limits.countStyleWork(1);
|
|
35
|
+
if (parts === 0)
|
|
36
|
+
first = text.slice(start, end);
|
|
37
|
+
parts += 1;
|
|
38
|
+
};
|
|
39
|
+
while (index < text.length) {
|
|
40
|
+
const ch = text.charAt(index);
|
|
41
|
+
if (ch !== "," && !/\s/.test(ch)) {
|
|
42
|
+
index += 1;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
flush(index);
|
|
46
|
+
while (index < text.length && /\s/.test(text.charAt(index)))
|
|
47
|
+
index += 1;
|
|
48
|
+
if (text.charAt(index) === ",")
|
|
49
|
+
index += 1;
|
|
50
|
+
while (index < text.length && /\s/.test(text.charAt(index)))
|
|
51
|
+
index += 1;
|
|
52
|
+
start = index;
|
|
53
|
+
}
|
|
54
|
+
flush(text.length);
|
|
55
|
+
return { first, more: parts > 1 };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The first length of a text position attribute in user units, or null when
|
|
59
|
+
* it is absent. Units resolve as SVG does: `em`/`ex` against the element's
|
|
60
|
+
* font size, percentages against the viewport's width (`x`, `dx`) or height
|
|
61
|
+
* (`y`, `dy`). A value that cannot be read is reported and counts as absent.
|
|
62
|
+
*/
|
|
63
|
+
function positionLength(element, name, style, elementPath, ctx) {
|
|
64
|
+
const text = attr(element, name);
|
|
65
|
+
if (text === null)
|
|
66
|
+
return null;
|
|
67
|
+
const parsed = parseSvgLength(scanPositionList(text, ctx).first, { fontSize: style.fontSize, rootFontSize: ctx.rules.rootFontSize });
|
|
68
|
+
if (parsed !== null) {
|
|
69
|
+
if (parsed.kind === "absolute")
|
|
70
|
+
return parsed.value;
|
|
71
|
+
const viewport = ctx.viewport;
|
|
72
|
+
if (viewport !== null) {
|
|
73
|
+
return parsed.fraction * (name === "x" || name === "dx" ? viewport.width : viewport.height);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
ctx.report(elementPath, element.localName, "left-out", `the ${name} "${text}" cannot be read`, { what: "Text positions that cannot be read", kept: "The text is placed where it would flow" });
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
/** Whether any position attribute holds a per-glyph list (more than one value). */
|
|
80
|
+
function hasGlyphList(element, ctx) {
|
|
81
|
+
return ["x", "y", "dx", "dy"].some((name) => {
|
|
82
|
+
const text = attr(element, name);
|
|
83
|
+
return text !== null && scanPositionList(text, ctx).more;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/** Reports the text features this element carries that the model cannot draw. */
|
|
87
|
+
function reportTextFeatures(element, elementPath, ctx) {
|
|
88
|
+
if (attr(element, "rotate") !== null) {
|
|
89
|
+
ctx.report(elementPath, element.localName, "left-out", "per-glyph rotate is not supported", { what: "Rotated letters", kept: "The letters are kept upright" });
|
|
90
|
+
}
|
|
91
|
+
if (attr(element, "textLength") !== null || attr(element, "lengthAdjust") !== null) {
|
|
92
|
+
ctx.report(elementPath, element.localName, "left-out", "textLength is not supported; the text keeps its natural length", { what: "Text lengths (textLength)", kept: "The text keeps its natural length" });
|
|
93
|
+
}
|
|
94
|
+
// writing-mode, baseline-shift and the other text properties the model
|
|
95
|
+
// cannot draw are reported by the cascade, wherever they are set.
|
|
96
|
+
}
|
|
97
|
+
/** Walks one inline element's content, adding runs and splitting lines at moving `<tspan>`s. */
|
|
98
|
+
function collectInline(element, style, elementPath, ctx, draft, runOpacity) {
|
|
99
|
+
const preserve = style.preserveWhitespace;
|
|
100
|
+
for (const child of Array.from({ length: element.childNodes.length }, (_, i) => element.childNodes[i])) {
|
|
101
|
+
if (!("localName" in child)) {
|
|
102
|
+
// Only text (3) and CDATA (4) draw; a comment or processing instruction a browser parser keeps does not.
|
|
103
|
+
if (child.nodeType !== 3 && child.nodeType !== 4)
|
|
104
|
+
continue;
|
|
105
|
+
const text = joinedRunText(draft.current, handleWhitespace(child.nodeValue ?? "", preserve), preserve);
|
|
106
|
+
if (text.length > 0)
|
|
107
|
+
draft.current.runs.push({ text, style, element, preserve, opacity: runOpacity });
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const childElement = child;
|
|
111
|
+
if (!isSvgElement(childElement, "tspan") && !isSvgElement(childElement, "a") && !isSvgElement(childElement, "textPath")) {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const childStyle = computeElementStyle(childElement, style, ctx.rules, ctx.viewport);
|
|
115
|
+
// SVG draws nothing of an inline element with display:none, nor of its content.
|
|
116
|
+
if (childStyle.display === "none") {
|
|
117
|
+
ctx.report(elementPath, childElement.localName, "left-out", "display:none", { what: "Hidden elements (display:none)", kept: "Not drawn" });
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
// Runs draw only their fill, so a stroke or an overline set on an inline
|
|
121
|
+
// element is reported there. An inherited stroke, and a text decoration
|
|
122
|
+
// line that propagates from an ancestor, were already reported on the
|
|
123
|
+
// element that set them.
|
|
124
|
+
if (childStyle.stroke.kind !== "none" && style.stroke.kind === "none") {
|
|
125
|
+
ctx.report(elementPath, childElement.localName, "left-out", "a stroke on text is not supported", TEXT_STROKE_NOTE);
|
|
126
|
+
}
|
|
127
|
+
if (childStyle.overline && !style.overline) {
|
|
128
|
+
ctx.report(elementPath, childElement.localName, "left-out", "an overline text decoration is not supported", OVERLINE_NOTE);
|
|
129
|
+
}
|
|
130
|
+
if (childStyle.obliqueAngle !== null && style.obliqueAngle === null) {
|
|
131
|
+
ctx.report(elementPath, childElement.localName, "approximated", obliqueAngleReason(childStyle.obliqueAngle), OBLIQUE_NOTE);
|
|
132
|
+
}
|
|
133
|
+
// Opacity is not inherited, so each <tspan>'s own value multiplies into
|
|
134
|
+
// the opacity of the runs inside it. It goes into the run color, which
|
|
135
|
+
// matches SVG's group opacity except where the run's glyphs overlap.
|
|
136
|
+
const childOpacity = runOpacity * childStyle.opacity;
|
|
137
|
+
if (childStyle.opacity !== 1) {
|
|
138
|
+
ctx.report(elementPath, childElement.localName, "approximated", "the opacity goes into the run color, so glyphs that overlap blend with each other", { what: "Opacity on parts of a text", kept: "The opacity goes into the text color" });
|
|
139
|
+
}
|
|
140
|
+
if (childElement.localName === "textPath") {
|
|
141
|
+
ctx.report(elementPath, "textPath", "left-out", "text on a path is not supported; its text imports as plain text", { what: "Text on a path", kept: "The text is kept as plain text" });
|
|
142
|
+
const text = joinedRunText(draft.current, handleWhitespace(childElement.textContent ?? "", childStyle.preserveWhitespace), childStyle.preserveWhitespace);
|
|
143
|
+
if (text.length > 0) {
|
|
144
|
+
draft.current.runs.push({
|
|
145
|
+
text, style: childStyle, element: childElement, preserve: childStyle.preserveWhitespace, opacity: childOpacity,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
// Text positioning (x, y, dx, dy, and the rotate and textLength reports)
|
|
151
|
+
// exists on a <tspan> only; on an <a> browsers ignore it, so it is
|
|
152
|
+
// neither applied nor reported there.
|
|
153
|
+
if (isSvgElement(childElement, "tspan")) {
|
|
154
|
+
reportTextFeatures(childElement, elementPath, ctx);
|
|
155
|
+
const move = tspanMove(childElement, childStyle, draft.current, elementPath, ctx);
|
|
156
|
+
if (move !== null) {
|
|
157
|
+
draft.lines.push(draft.current);
|
|
158
|
+
draft.current = { anchorX: move.x, baselineY: move.y, anchor: move.anchor, continuesChunk: move.continuesChunk, runs: [] };
|
|
159
|
+
}
|
|
160
|
+
const dx = positionLength(childElement, "dx", childStyle, elementPath, ctx);
|
|
161
|
+
if (dx !== null && dx !== 0) {
|
|
162
|
+
ctx.report(elementPath, childElement.localName, "approximated", "a dx letter adjustment is dropped", { what: "Letter shifts (dx)", kept: "The letters keep their normal spacing" });
|
|
163
|
+
}
|
|
164
|
+
if (hasGlyphList(childElement, ctx)) {
|
|
165
|
+
ctx.report(elementPath, childElement.localName, "approximated", "per-glyph position lists use their first value", GLYPH_POSITIONS_NOTE);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
collectInline(childElement, childStyle, elementPath, ctx, draft, childOpacity);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* A text node's white space after the runs before it on the line: with
|
|
173
|
+
* white space collapsing, a space that follows a space ending the previous
|
|
174
|
+
* run is dropped, so `A <tspan> B</tspan>` keeps one space, as SVG does.
|
|
175
|
+
*/
|
|
176
|
+
function joinedRunText(line, text, preserve) {
|
|
177
|
+
if (preserve || !text.startsWith(" "))
|
|
178
|
+
return text;
|
|
179
|
+
const previous = line.runs[line.runs.length - 1];
|
|
180
|
+
if (previous === undefined || previous.preserve || !previous.text.endsWith(" "))
|
|
181
|
+
return text;
|
|
182
|
+
return text.slice(1);
|
|
183
|
+
}
|
|
184
|
+
/** The report for an explicit oblique angle, which the model cannot draw. */
|
|
185
|
+
function obliqueAngleReason(angle) {
|
|
186
|
+
return `the oblique angle ${String(angle)}deg is not supported; the text draws with the face's own slant`;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Where the current line's text ends in user units: its anchor plus the
|
|
190
|
+
* measured width of its runs, less the part an anchor of middle or end
|
|
191
|
+
* puts before the anchor point.
|
|
192
|
+
*/
|
|
193
|
+
/** The measured width of a line's runs, in user units. */
|
|
194
|
+
function lineWidth(line, elementPath, ctx) {
|
|
195
|
+
let width = 0;
|
|
196
|
+
for (const run of line.runs) {
|
|
197
|
+
width += ctx.measureText({
|
|
198
|
+
text: run.text,
|
|
199
|
+
fontFamily: resolveFont(run.style, elementPath, run.element, ctx),
|
|
200
|
+
fontWeight: run.style.fontWeight,
|
|
201
|
+
style: run.style.fontStyle,
|
|
202
|
+
fontSize: run.style.fontSize,
|
|
203
|
+
letterSpacing: run.style.letterSpacing ?? 0,
|
|
204
|
+
}).width;
|
|
205
|
+
}
|
|
206
|
+
return width;
|
|
207
|
+
}
|
|
208
|
+
function currentTextEnd(line, elementPath, ctx) {
|
|
209
|
+
const width = lineWidth(line, elementPath, ctx);
|
|
210
|
+
const before = line.anchor === "middle" ? width / 2 : line.anchor === "end" ? width : 0;
|
|
211
|
+
return line.anchorX - before + width;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Anchors each text chunk once: a middle- or end-anchored line that dy-only
|
|
215
|
+
* continuation lines follow is shifted by their combined width, and they
|
|
216
|
+
* with it, as a browser anchors the whole chunk (probed in Chrome for
|
|
217
|
+
* Testing 151). Runs after the lines are trimmed, so the widths are the
|
|
218
|
+
* trimmed ones.
|
|
219
|
+
*/
|
|
220
|
+
function anchorContinuedChunks(lines, elementPath, ctx) {
|
|
221
|
+
for (let i = 0; i < lines.length; i++) {
|
|
222
|
+
const line = lines[i];
|
|
223
|
+
if (line.continuesChunk || line.anchor === "start")
|
|
224
|
+
continue;
|
|
225
|
+
let trailing = 0;
|
|
226
|
+
let j = i + 1;
|
|
227
|
+
while (j < lines.length && lines[j].continuesChunk) {
|
|
228
|
+
trailing += lineWidth(lines[j], elementPath, ctx);
|
|
229
|
+
j++;
|
|
230
|
+
}
|
|
231
|
+
if (trailing === 0)
|
|
232
|
+
continue;
|
|
233
|
+
const shift = line.anchor === "end" ? trailing : trailing / 2;
|
|
234
|
+
for (let k = i; k < j; k++)
|
|
235
|
+
lines[k].anchorX -= shift;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/** Where a `<tspan>` moves to, or null when it stays on the current line. */
|
|
239
|
+
function tspanMove(element, style, current, elementPath, ctx) {
|
|
240
|
+
const x = positionLength(element, "x", style, elementPath, ctx);
|
|
241
|
+
const y = positionLength(element, "y", style, elementPath, ctx);
|
|
242
|
+
const dy = positionLength(element, "dy", style, elementPath, ctx);
|
|
243
|
+
if (x === null && y === null && (dy === null || dy === 0))
|
|
244
|
+
return null;
|
|
245
|
+
return {
|
|
246
|
+
// Without its own x the new line continues where the text so far ends
|
|
247
|
+
// and does not anchor there (probed in Chrome for Testing 151); an
|
|
248
|
+
// absolute x anchors the new line. A dy-only shift also stays in the
|
|
249
|
+
// current text chunk, so a middle or end anchor spans it.
|
|
250
|
+
x: x ?? currentTextEnd(current, elementPath, ctx),
|
|
251
|
+
// dy shifts from the absolute y when both are set, as SVG applies it after y.
|
|
252
|
+
y: (y ?? current.baselineY) + (dy ?? 0),
|
|
253
|
+
anchor: x === null ? "start" : style.textAnchor,
|
|
254
|
+
continuesChunk: x === null && y === null,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
/** The resolved family for a run: the first family the host has, or the first family kept as missing. */
|
|
258
|
+
function resolveFont(style, elementPath, element, ctx) {
|
|
259
|
+
for (const family of style.fontFamily) {
|
|
260
|
+
ctx.rules.countFontLookup();
|
|
261
|
+
if (ctx.fontAvailable(family, style.fontWeight, style.fontStyle)) {
|
|
262
|
+
return family;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const first = style.fontFamily[0] ?? "sans-serif";
|
|
266
|
+
ctx.report(elementPath, element.localName, "approximated", `the font "${first}" is not available; it stays on the layer as a missing font instead of being swapped`, { what: first, kept: "Marked as missing until it is replaced", missingFont: first });
|
|
267
|
+
return first;
|
|
268
|
+
}
|
|
269
|
+
/** The style overrides of one run, in the model's terms. */
|
|
270
|
+
function runOverrides(run, family, fontSize, letterSpacing, layerVisible, elementPath, ctx) {
|
|
271
|
+
const { style } = run;
|
|
272
|
+
let color = "#000000";
|
|
273
|
+
const hidden = layerVisible && style.visibility !== "visible";
|
|
274
|
+
if (ctx.insideClipPath === true) {
|
|
275
|
+
// Clip content counts by geometry only (CSS Masking §6.1): the glyphs
|
|
276
|
+
// clip whatever their fill or opacity, and a hidden run adds nothing.
|
|
277
|
+
color = hidden ? "#00000000" : "#000000";
|
|
278
|
+
}
|
|
279
|
+
else if (style.fill.kind === "color") {
|
|
280
|
+
// The fill's alpha, fill-opacity and the <tspan>s' opacity are the run's
|
|
281
|
+
// paint opacity, so they go into the run color; the <text>'s `opacity`
|
|
282
|
+
// goes on the layer. A hidden run inside a shown layer keeps its advance
|
|
283
|
+
// but draws no glyphs, as SVG does.
|
|
284
|
+
color = hexColor({ ...style.fill.color, a: hidden ? 0 : style.fill.color.a * style.fillOpacity * run.opacity });
|
|
285
|
+
}
|
|
286
|
+
else if (style.fill.kind === "none") {
|
|
287
|
+
// No fill paints no glyphs; the run keeps its advance.
|
|
288
|
+
color = "#00000000";
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
ctx.report(elementPath, run.element.localName, "left-out", "a gradient or pattern text fill is not supported; the text draws in black", { what: "Gradient and pattern text fills", kept: "The text is drawn in black" });
|
|
292
|
+
}
|
|
293
|
+
return {
|
|
294
|
+
fontFamily: family,
|
|
295
|
+
fontSize,
|
|
296
|
+
fontWeight: style.fontWeight,
|
|
297
|
+
color,
|
|
298
|
+
...(style.fontStyle === "normal" ? {} : { italic: true }),
|
|
299
|
+
...(style.textDecoration.underline ? { underline: true } : {}),
|
|
300
|
+
...(style.textDecoration.lineThrough ? { strikethrough: true } : {}),
|
|
301
|
+
...(letterSpacing === null ? {} : { letterSpacing }),
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Trims the white space at one line's start and/or end, as SVG does at
|
|
306
|
+
* text-chunk boundaries; preserved white space stays.
|
|
307
|
+
*/
|
|
308
|
+
function trimLine(line, start, end) {
|
|
309
|
+
const first = line.runs[0];
|
|
310
|
+
if (start && first !== undefined && !first.preserve) {
|
|
311
|
+
first.text = first.text.replace(/^ +/, "");
|
|
312
|
+
}
|
|
313
|
+
const last = line.runs[line.runs.length - 1];
|
|
314
|
+
if (end && last !== undefined && !last.preserve) {
|
|
315
|
+
last.text = last.text.replace(/ +$/, "");
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Trims the white space of each line at text-chunk boundaries (SVG 2, white
|
|
320
|
+
* space): the first line with text of a chunk at its start, the last at its
|
|
321
|
+
* end. A dy-only continuation keeps the space at its edge, which the chunk's
|
|
322
|
+
* anchoring measures; the one case where it drops it is a space also ending
|
|
323
|
+
* the text so far, which collapses to one, as SVG collapses it.
|
|
324
|
+
*/
|
|
325
|
+
function trimChunks(lines) {
|
|
326
|
+
const hasText = lines.map((line) => line.runs.some((run) => run.text.trim().length > 0));
|
|
327
|
+
for (let i = 0; i < lines.length; i++) {
|
|
328
|
+
if (!hasText[i])
|
|
329
|
+
continue;
|
|
330
|
+
const line = lines[i];
|
|
331
|
+
let chunkStart = i;
|
|
332
|
+
while (chunkStart > 0 && lines[chunkStart].continuesChunk)
|
|
333
|
+
chunkStart--;
|
|
334
|
+
let trimStart = true;
|
|
335
|
+
for (let j = chunkStart; j < i; j++) {
|
|
336
|
+
if (hasText[j]) {
|
|
337
|
+
trimStart = false;
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
let trimEnd = true;
|
|
342
|
+
for (let j = i + 1; j < lines.length && lines[j].continuesChunk; j++) {
|
|
343
|
+
if (hasText[j]) {
|
|
344
|
+
trimEnd = false;
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
// A space starting a continuation collapses with a space ending the text
|
|
349
|
+
// so far in the same chunk (unless either side preserves white space).
|
|
350
|
+
if (!trimStart && line.runs[0] !== undefined && !line.runs[0].preserve) {
|
|
351
|
+
for (let j = i - 1; j >= chunkStart; j--) {
|
|
352
|
+
const previous = lines[j];
|
|
353
|
+
if (!hasText[j])
|
|
354
|
+
continue;
|
|
355
|
+
const lastRun = previous.runs[previous.runs.length - 1];
|
|
356
|
+
if (!lastRun.preserve && lastRun.text.endsWith(" ")) {
|
|
357
|
+
line.runs[0].text = line.runs[0].text.replace(/^ /, "");
|
|
358
|
+
}
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
trimLine(line, trimStart, trimEnd);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Builds the text layers of one `<text>` element: one layer when every
|
|
367
|
+
* `<tspan>` stays on the same line, or one layer per line wrapped in a
|
|
368
|
+
* group when a `<tspan>` moves. Returns null for text without content.
|
|
369
|
+
*/
|
|
370
|
+
export function textLayer(element, computed, matrix, placement, elementPath, ctx) {
|
|
371
|
+
reportTextFeatures(element, elementPath, ctx);
|
|
372
|
+
if (hasGlyphList(element, ctx)) {
|
|
373
|
+
ctx.report(elementPath, element.localName, "approximated", "per-glyph position lists use their first value", GLYPH_POSITIONS_NOTE);
|
|
374
|
+
}
|
|
375
|
+
if (computed.stroke.kind !== "none") {
|
|
376
|
+
ctx.report(elementPath, element.localName, "left-out", "a stroke on text is not supported", TEXT_STROKE_NOTE);
|
|
377
|
+
}
|
|
378
|
+
if (computed.overline) {
|
|
379
|
+
ctx.report(elementPath, element.localName, "left-out", "an overline text decoration is not supported", OVERLINE_NOTE);
|
|
380
|
+
}
|
|
381
|
+
if (computed.obliqueAngle !== null) {
|
|
382
|
+
ctx.report(elementPath, element.localName, "approximated", obliqueAngleReason(computed.obliqueAngle), OBLIQUE_NOTE);
|
|
383
|
+
}
|
|
384
|
+
// The first glyph sits at (x + dx, y + dy); the rest of the text follows it.
|
|
385
|
+
const startX = (positionLength(element, "x", computed, elementPath, ctx) ?? 0)
|
|
386
|
+
+ (positionLength(element, "dx", computed, elementPath, ctx) ?? 0);
|
|
387
|
+
const startY = (positionLength(element, "y", computed, elementPath, ctx) ?? 0)
|
|
388
|
+
+ (positionLength(element, "dy", computed, elementPath, ctx) ?? 0);
|
|
389
|
+
const draft = {
|
|
390
|
+
lines: [],
|
|
391
|
+
current: { anchorX: startX, baselineY: startY, anchor: computed.textAnchor, continuesChunk: false, runs: [] },
|
|
392
|
+
};
|
|
393
|
+
collectInline(element, computed, elementPath, ctx, draft, 1);
|
|
394
|
+
draft.lines.push(draft.current);
|
|
395
|
+
trimChunks(draft.lines);
|
|
396
|
+
anchorContinuedChunks(draft.lines, elementPath, ctx);
|
|
397
|
+
const fontScale = placement.strokeScale;
|
|
398
|
+
// A reflection is kept as a flip, so only unequal axis scales or a skew
|
|
399
|
+
// make the font size approximate.
|
|
400
|
+
const { ux, uy, uz } = placement.parts;
|
|
401
|
+
if (!sameScale(ux, Math.abs(uz)) || !negligibleNextTo(uy, uz)) {
|
|
402
|
+
ctx.report(elementPath, element.localName, "approximated", "the font size scales with the geometric mean of the transform's axis scales", { what: "Text under an uneven scale", kept: "One even font size is used" });
|
|
403
|
+
}
|
|
404
|
+
const layers = [];
|
|
405
|
+
for (const line of draft.lines) {
|
|
406
|
+
const text = line.runs.map((run) => run.text).join("");
|
|
407
|
+
if (text.trim().length === 0)
|
|
408
|
+
continue;
|
|
409
|
+
const layer = textLineLayer(line, computed, matrix, placement, fontScale, elementPath, ctx);
|
|
410
|
+
if (layer !== null)
|
|
411
|
+
layers.push(layer);
|
|
412
|
+
}
|
|
413
|
+
if (layers.length === 0)
|
|
414
|
+
return null;
|
|
415
|
+
const idName = attr(element, "id");
|
|
416
|
+
if (layers.length === 1) {
|
|
417
|
+
const only = layers[0];
|
|
418
|
+
const named = idName === null ? only : { ...only, name: idName };
|
|
419
|
+
return { top: named, nodes: [named] };
|
|
420
|
+
}
|
|
421
|
+
const group = fitTextGroup(ctx.idSource(), idName ?? layers[0].name, layers, computed);
|
|
422
|
+
// The group carries the element's opacity and blend mode, so its lines
|
|
423
|
+
// drop them rather than apply them a second time.
|
|
424
|
+
const shifted = layers.map(({ blendMode: _blendMode, ...layer }) => ({
|
|
425
|
+
...layer,
|
|
426
|
+
opacity: 1,
|
|
427
|
+
frame: { ...layer.frame, x: layer.frame.x - group.frame.x, y: layer.frame.y - group.frame.y },
|
|
428
|
+
}));
|
|
429
|
+
return { top: group, nodes: [group, ...shifted] };
|
|
430
|
+
}
|
|
431
|
+
/** One line of text as a text layer, placed so its anchor point stays where SVG puts it. */
|
|
432
|
+
function textLineLayer(line, computed, matrix, placement, fontScale, elementPath, ctx) {
|
|
433
|
+
const runs = [];
|
|
434
|
+
// The requested font-style of each run, in run order: the model's runs
|
|
435
|
+
// carry italic only, so the host matches faces from this list (see
|
|
436
|
+
// `SvgImportResult.textRunStyles`).
|
|
437
|
+
const runStyles = [];
|
|
438
|
+
let totalWidth = 0;
|
|
439
|
+
// The line's box and baseline come from its tallest run: the largest
|
|
440
|
+
// ascent above the baseline and the largest descent below it.
|
|
441
|
+
let ascent = 0;
|
|
442
|
+
let descent = 0;
|
|
443
|
+
// Visibility is inherited and a <tspan> may set it back to visible, so the
|
|
444
|
+
// layer shows when any run does, and only the hidden runs draw nothing.
|
|
445
|
+
const lineVisible = line.runs.some((run) => run.style.visibility === "visible");
|
|
446
|
+
for (const run of line.runs) {
|
|
447
|
+
// A zero font size draws no glyphs and takes no space.
|
|
448
|
+
if (run.style.fontSize === 0)
|
|
449
|
+
continue;
|
|
450
|
+
const family = resolveFont(run.style, elementPath, run.element, ctx);
|
|
451
|
+
const fontSize = run.style.fontSize * fontScale;
|
|
452
|
+
const letterSpacing = run.style.letterSpacing === null ? null : run.style.letterSpacing * fontScale;
|
|
453
|
+
const metrics = ctx.measureText({
|
|
454
|
+
text: run.text,
|
|
455
|
+
fontFamily: family,
|
|
456
|
+
fontWeight: run.style.fontWeight,
|
|
457
|
+
style: run.style.fontStyle,
|
|
458
|
+
fontSize,
|
|
459
|
+
letterSpacing: letterSpacing ?? 0,
|
|
460
|
+
});
|
|
461
|
+
ascent = Math.max(ascent, metrics.ascent);
|
|
462
|
+
descent = Math.max(descent, metrics.descent);
|
|
463
|
+
totalWidth += metrics.width;
|
|
464
|
+
runs.push({
|
|
465
|
+
text: run.text,
|
|
466
|
+
overrides: runOverrides(run, family, fontSize, letterSpacing, lineVisible, elementPath, ctx),
|
|
467
|
+
});
|
|
468
|
+
runStyles.push(run.style.fontStyle);
|
|
469
|
+
}
|
|
470
|
+
if (runs.length === 0)
|
|
471
|
+
return null;
|
|
472
|
+
const anchor = applyMatrixToPoint(matrix, line.anchorX, line.baselineY);
|
|
473
|
+
const height = ascent + descent;
|
|
474
|
+
// The anchor point sits inside the layer's box: at the left, centre or
|
|
475
|
+
// right edge on the first baseline.
|
|
476
|
+
let localX = 0;
|
|
477
|
+
if (line.anchor === "middle")
|
|
478
|
+
localX = totalWidth / 2;
|
|
479
|
+
if (line.anchor === "end")
|
|
480
|
+
localX = totalWidth;
|
|
481
|
+
const localY = ascent;
|
|
482
|
+
const half = { x: totalWidth / 2, y: height / 2 };
|
|
483
|
+
// A mirroring transform decomposes with a negative y scale: the layer
|
|
484
|
+
// keeps it as a vertical flip, which the model applies about the box's
|
|
485
|
+
// centre before the rotation.
|
|
486
|
+
const flipY = placement.parts.uz < 0;
|
|
487
|
+
const rotation = rotationMatrix(placement.parts.rotation);
|
|
488
|
+
const rotated = applyMatrixToPoint(rotation, localX - half.x, (localY - half.y) * (flipY ? -1 : 1));
|
|
489
|
+
const position = {
|
|
490
|
+
x: anchor.x - half.x - rotated.x,
|
|
491
|
+
y: anchor.y - half.y - rotated.y,
|
|
492
|
+
};
|
|
493
|
+
const paragraphs = [{
|
|
494
|
+
runs,
|
|
495
|
+
align: line.anchor === "start" ? "left" : line.anchor === "middle" ? "center" : "right",
|
|
496
|
+
}];
|
|
497
|
+
const chrome = layerChrome(computed);
|
|
498
|
+
const text = line.runs.map((run) => run.text).join("").trim();
|
|
499
|
+
const node = {
|
|
500
|
+
id: ctx.idSource(),
|
|
501
|
+
name: text.length === 0 ? "Text" : text,
|
|
502
|
+
visible: lineVisible,
|
|
503
|
+
locked: false,
|
|
504
|
+
opacity: chrome.opacity,
|
|
505
|
+
type: "text",
|
|
506
|
+
frame: { x: position.x, y: position.y, width: totalWidth, height },
|
|
507
|
+
rotation: placement.parts.rotation,
|
|
508
|
+
...(flipY ? { flipY: true } : {}),
|
|
509
|
+
...(chrome.blendMode === null ? {} : { blendMode: chrome.blendMode }),
|
|
510
|
+
paragraphs,
|
|
511
|
+
verticalAlign: "top",
|
|
512
|
+
overflow: "visible",
|
|
513
|
+
autoResize: "width-and-height",
|
|
514
|
+
wrap: "none",
|
|
515
|
+
};
|
|
516
|
+
ctx.textRunStyles.set(node.id, runStyles);
|
|
517
|
+
return node;
|
|
518
|
+
}
|
|
519
|
+
/** Wraps the line layers of one `<text>` in a group fitted to their bounds. */
|
|
520
|
+
function fitTextGroup(id, name, layers, computed) {
|
|
521
|
+
let minX = Infinity;
|
|
522
|
+
let minY = Infinity;
|
|
523
|
+
let maxX = -Infinity;
|
|
524
|
+
let maxY = -Infinity;
|
|
525
|
+
for (const layer of layers) {
|
|
526
|
+
minX = Math.min(minX, layer.frame.x);
|
|
527
|
+
minY = Math.min(minY, layer.frame.y);
|
|
528
|
+
maxX = Math.max(maxX, layer.frame.x + layer.frame.width);
|
|
529
|
+
maxY = Math.max(maxY, layer.frame.y + layer.frame.height);
|
|
530
|
+
}
|
|
531
|
+
const chrome = layerChrome(computed);
|
|
532
|
+
return {
|
|
533
|
+
id,
|
|
534
|
+
name,
|
|
535
|
+
// The group shows when any line does; each line carries its own visibility.
|
|
536
|
+
visible: layers.some((layer) => layer.visible),
|
|
537
|
+
locked: false,
|
|
538
|
+
opacity: chrome.opacity,
|
|
539
|
+
type: "group",
|
|
540
|
+
frame: { x: minX, y: minY, width: maxX - minX, height: maxY - minY },
|
|
541
|
+
rotation: 0,
|
|
542
|
+
children: layers.map((layer) => layer.id),
|
|
543
|
+
...(chrome.blendMode === null ? {} : { blendMode: chrome.blendMode }),
|
|
544
|
+
};
|
|
545
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type TransformMatrix } from "../../model/index.ts";
|
|
2
|
+
/**
|
|
3
|
+
* An SVG affine matrix `[a, b, c, d, e, f]`, the model's `TransformMatrix`:
|
|
4
|
+
* it maps (x, y) to (a·x + c·y + e, b·x + d·y + f).
|
|
5
|
+
*/
|
|
6
|
+
export type Matrix = TransformMatrix;
|
|
7
|
+
/** The identity matrix. */
|
|
8
|
+
export declare const IDENTITY_MATRIX: Matrix;
|
|
9
|
+
/** Whether `value` is close enough to zero to treat as zero. */
|
|
10
|
+
export declare function nearZero(value: number): boolean;
|
|
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 declare function negligibleNextTo(value: number, scale: number): boolean;
|
|
17
|
+
/** Whether two scales are the same, relative to their size. */
|
|
18
|
+
export declare function sameScale(first: number, second: number): boolean;
|
|
19
|
+
/** Maps one point by the matrix. */
|
|
20
|
+
export declare function applyMatrixToPoint(matrix: Matrix, x: number, y: number): {
|
|
21
|
+
readonly x: number;
|
|
22
|
+
readonly y: number;
|
|
23
|
+
};
|
|
24
|
+
/** Maps a direction (a point relative to an origin) by the matrix's linear part. */
|
|
25
|
+
export declare function applyMatrixToVector(matrix: Matrix, dx: number, dy: number): {
|
|
26
|
+
readonly dx: number;
|
|
27
|
+
readonly dy: number;
|
|
28
|
+
};
|
|
29
|
+
/** The matrix's determinant; zero means the transform collapses the plane. */
|
|
30
|
+
export declare function matrixDeterminant(matrix: Matrix): number;
|
|
31
|
+
/**
|
|
32
|
+
* Whether the matrix maps everything onto a line or a point. The determinant
|
|
33
|
+
* is compared with the product of the column lengths, so the test measures
|
|
34
|
+
* the angle between the columns and not the size of the scale: a tiny but
|
|
35
|
+
* valid scale (a 1e-5 viewBox scale has determinant 1e-10) is invertible.
|
|
36
|
+
*/
|
|
37
|
+
export declare function matrixIsDegenerate(matrix: Matrix): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Parses one `transform` attribute into a matrix: a list of `matrix`,
|
|
40
|
+
* `translate`, `scale`, `rotate` (degrees, optionally around a center),
|
|
41
|
+
* `skewX` and `skewY` functions, applied left to right. Returns null when
|
|
42
|
+
* the text does not follow SVG's grammar — the caller reports that.
|
|
43
|
+
*/
|
|
44
|
+
export declare function parseTransform(text: string): Matrix | null;
|
|
45
|
+
/**
|
|
46
|
+
* The result of splitting a matrix into the part a layer keeps and the part
|
|
47
|
+
* baked into geometry. Every matrix decomposes as
|
|
48
|
+
* `translate × rotate × remainder`, where `remainder` is the upper-triangular
|
|
49
|
+
* linear map [ux, uy; 0, uz] of a QR decomposition: `ux` scales x, `uy`
|
|
50
|
+
* skews, `uz` scales y (and is negative for a mirrored transform).
|
|
51
|
+
*/
|
|
52
|
+
export interface DecomposedMatrix {
|
|
53
|
+
/** Clockwise degrees about the origin, normalized to (-180, 180]. */
|
|
54
|
+
readonly rotation: number;
|
|
55
|
+
/** Scale on x before the skew. */
|
|
56
|
+
readonly ux: number;
|
|
57
|
+
/** The skew term; zero for a transform without skew. */
|
|
58
|
+
readonly uy: number;
|
|
59
|
+
/** Scale on y after the skew; negative when the transform mirrors. */
|
|
60
|
+
readonly uz: number;
|
|
61
|
+
readonly translateX: number;
|
|
62
|
+
readonly translateY: number;
|
|
63
|
+
}
|
|
64
|
+
/** The remainder as a matrix (no translation). */
|
|
65
|
+
export declare function remainderMatrix(parts: DecomposedMatrix): Matrix;
|
|
66
|
+
/** Whether the remainder leaves geometry unchanged. */
|
|
67
|
+
export declare function remainderIsIdentity(parts: DecomposedMatrix): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Whether the remainder is a plain positive scale on both axes, uniform or
|
|
70
|
+
* not. A rectangle, ellipse or text layer scales exactly under such a
|
|
71
|
+
* remainder; anything else is baked into path geometry.
|
|
72
|
+
*/
|
|
73
|
+
export declare function remainderIsPositiveScale(parts: DecomposedMatrix): boolean;
|
|
74
|
+
/** Whether the remainder scales both axes by the same positive factor. */
|
|
75
|
+
export declare function remainderIsUniformScale(parts: DecomposedMatrix): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* The factor stroke widths, dashes and corner radii scale by when the
|
|
78
|
+
* remainder is baked: exact for a uniform scale, the geometric mean of the
|
|
79
|
+
* axis scales otherwise (the caller reports that approximation).
|
|
80
|
+
*/
|
|
81
|
+
export declare function remainderStrokeScale(parts: DecomposedMatrix): number;
|
|
82
|
+
/** Splits a matrix into `translate × rotate × remainder`. */
|
|
83
|
+
export declare function decomposeMatrix(matrix: Matrix): DecomposedMatrix;
|
|
84
|
+
/** The rotation part of a decomposed matrix as a matrix, with an optional translation. */
|
|
85
|
+
export declare function rotationMatrix(degrees: number, tx?: number, ty?: number): Matrix;
|
|
86
|
+
/**
|
|
87
|
+
* The inverse of an invertible matrix, or null when it collapses the plane.
|
|
88
|
+
* `multiplyTransforms(invertMatrix(m), m)` is the identity.
|
|
89
|
+
*/
|
|
90
|
+
export declare function invertMatrix(matrix: Matrix): Matrix | null;
|
|
91
|
+
/**
|
|
92
|
+
* The frame position (top-left) a framed layer takes so that the model's
|
|
93
|
+
* `nodeLocalTransform` maps the layer's local origin to `origin`. The model
|
|
94
|
+
* flips and rotates about the frame's middle and then translates by the
|
|
95
|
+
* frame position, so the position is `origin` minus where a frame at (0, 0)
|
|
96
|
+
* with the same size, rotation and flips sends its local origin.
|
|
97
|
+
*/
|
|
98
|
+
export declare function framePositionFor(rotationDegrees: number, flipY: boolean, width: number, height: number, origin: {
|
|
99
|
+
readonly x: number;
|
|
100
|
+
readonly y: number;
|
|
101
|
+
}): {
|
|
102
|
+
readonly x: number;
|
|
103
|
+
readonly y: number;
|
|
104
|
+
};
|
|
105
|
+
/** The model's `nodeLocalTransform` for a framed layer with this frame, rotation and flip. */
|
|
106
|
+
export declare function frameLocalTransform(frame: {
|
|
107
|
+
readonly x: number;
|
|
108
|
+
readonly y: number;
|
|
109
|
+
readonly width: number;
|
|
110
|
+
readonly height: number;
|
|
111
|
+
}, rotationDegrees: number, flipY: boolean): Matrix;
|