@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,840 @@
|
|
|
1
|
+
import { multiplyTransforms, getNodeBounds, } from "../../model/index.js";
|
|
2
|
+
import { attr, childElements, elementId, hrefIsExternal, hrefTargetId, hrefValue, isSvgElement, } from "../tree/elements.js";
|
|
3
|
+
import { computeElementStyle, initialComputedStyle } from "../style/computed.js";
|
|
4
|
+
import { IDENTITY_MATRIX, parseTransform, sameScale, } from "../transform/matrix.js";
|
|
5
|
+
import { parseSvgLength } from "../units/length.js";
|
|
6
|
+
import { parseViewBox, viewBoxTransform, DEFAULT_PRESERVE_ASPECT_RATIO, parsePreserveAspectRatio, } from "../units/viewbox.js";
|
|
7
|
+
import { IterationGuard, ITERATION_LIMIT, SvgImportRefusalError } from "../limits/limits.js";
|
|
8
|
+
import { ReportBuilder } from "../report/report.js";
|
|
9
|
+
import { layerChrome, layerPlacement, pathLayerFromData } from "../paths/paths.js";
|
|
10
|
+
import { rasterFramePlan } from "../shapes/shapes.js";
|
|
11
|
+
import { isNonScalingStroke } from "../paint/paint.js";
|
|
12
|
+
import { buildChildren, buildElement, shiftNode, unionBoxes, wrapInGroup } from "../structure/walk.js";
|
|
13
|
+
import { applyClipMaskFilter } from "../structure/effects.js";
|
|
14
|
+
const BLEND_NOTE = { what: "Blend modes on reused content", kept: "Drawn without the blend mode" };
|
|
15
|
+
const VIEWBOX_NOTE = { what: "Symbol viewBoxes that cannot be used", kept: "The symbol is drawn without its viewBox" };
|
|
16
|
+
const COPY_NOTE = { what: "Reused content that cannot be an instance", kept: "Made a copy instead of an instance" };
|
|
17
|
+
/** The refusal message for a `<use>` cycle, naming its ids. */
|
|
18
|
+
function cycleMessage(cycle) {
|
|
19
|
+
return `The <use> references form a cycle: ${cycle.map((entry) => `#${entry}`).join(" → ")}`;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolves a use's reference through any chain of uses to the ultimate
|
|
23
|
+
* target element, and the id the component decision is keyed on; or the
|
|
24
|
+
* ids of a chain that loops, ending with the id it returns to.
|
|
25
|
+
*/
|
|
26
|
+
function ultimateTarget(useElement, ids, countStep) {
|
|
27
|
+
const guard = new IterationGuard("use chain resolution", ITERATION_LIMIT);
|
|
28
|
+
let currentId = hrefTargetId(useElement);
|
|
29
|
+
let current = currentId === null ? undefined : ids.get(currentId);
|
|
30
|
+
const seen = new Set();
|
|
31
|
+
while (current !== undefined && isSvgElement(current, "use")) {
|
|
32
|
+
guard.step();
|
|
33
|
+
countStep();
|
|
34
|
+
if (currentId !== null && seen.has(currentId)) {
|
|
35
|
+
const chain = [...seen];
|
|
36
|
+
return { cycle: [...chain.slice(chain.indexOf(currentId)), currentId] };
|
|
37
|
+
}
|
|
38
|
+
if (currentId !== null)
|
|
39
|
+
seen.add(currentId);
|
|
40
|
+
currentId = hrefTargetId(current);
|
|
41
|
+
current = currentId === null ? undefined : ids.get(currentId);
|
|
42
|
+
}
|
|
43
|
+
if (currentId === null || current === undefined)
|
|
44
|
+
return null;
|
|
45
|
+
return { id: currentId, element: current };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The pre-pass over the `<use>` graph: counts uses per target, decides
|
|
49
|
+
* components, and records cycles with the ids named. Runs before any layer
|
|
50
|
+
* is built. A use that is built and reaches a cycle refuses the whole
|
|
51
|
+
* import, so a cycle never leaves anything half imported; one that nothing
|
|
52
|
+
* draws or references is never reached.
|
|
53
|
+
*/
|
|
54
|
+
export function planReuse(root, ids, limits) {
|
|
55
|
+
const countStep = () => limits.countReferenceStep();
|
|
56
|
+
// Cycles are recorded, not refused here: only a use that is built and
|
|
57
|
+
// reaches one refuses the import (see `resolveUse`).
|
|
58
|
+
const cycles = new Map();
|
|
59
|
+
const recordCycle = (cycle) => {
|
|
60
|
+
const message = cycleMessage(cycle);
|
|
61
|
+
for (const id of cycle.slice(0, -1))
|
|
62
|
+
if (!cycles.has(id))
|
|
63
|
+
cycles.set(id, message);
|
|
64
|
+
};
|
|
65
|
+
// The target ids each element's subtree references, computed bottom-up in
|
|
66
|
+
// one pass over the tree.
|
|
67
|
+
const containedCache = new Map();
|
|
68
|
+
const containedTargets = (element) => {
|
|
69
|
+
const cached = containedCache.get(element);
|
|
70
|
+
if (cached !== undefined)
|
|
71
|
+
return cached;
|
|
72
|
+
const found = [];
|
|
73
|
+
if (isSvgElement(element, "use")) {
|
|
74
|
+
const target = ultimateTarget(element, ids, countStep);
|
|
75
|
+
if (target !== null && "cycle" in target)
|
|
76
|
+
recordCycle(target.cycle);
|
|
77
|
+
else if (target !== null)
|
|
78
|
+
found.push(target.id);
|
|
79
|
+
}
|
|
80
|
+
for (const child of childElements(element))
|
|
81
|
+
found.push(...containedTargets(child));
|
|
82
|
+
const result = Object.freeze(found);
|
|
83
|
+
containedCache.set(element, result);
|
|
84
|
+
return result;
|
|
85
|
+
};
|
|
86
|
+
const targetsWithin = new Map();
|
|
87
|
+
for (const [id, element] of ids) {
|
|
88
|
+
targetsWithin.set(id, containedTargets(element));
|
|
89
|
+
}
|
|
90
|
+
// Cycle check over the id graph, recording each cycle it finds. Every
|
|
91
|
+
// cycle holds at least one recorded id: the first of its ids the search
|
|
92
|
+
// enters, which a later id on the cycle reaches while it is still open.
|
|
93
|
+
// The search keeps its own stack of open ids, each with the index of its
|
|
94
|
+
// next target, so a long chain of ids never deepens the call stack.
|
|
95
|
+
const state = new Map();
|
|
96
|
+
const stack = [];
|
|
97
|
+
const visit = (start) => {
|
|
98
|
+
if (state.has(start)) {
|
|
99
|
+
if (state.get(start) === "gray")
|
|
100
|
+
recordCycle([...stack.slice(stack.indexOf(start)), start]);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const next = [];
|
|
104
|
+
const open = (id) => {
|
|
105
|
+
state.set(id, "gray");
|
|
106
|
+
stack.push(id);
|
|
107
|
+
next.push(0);
|
|
108
|
+
};
|
|
109
|
+
open(start);
|
|
110
|
+
while (stack.length > 0) {
|
|
111
|
+
const id = stack[stack.length - 1];
|
|
112
|
+
const targets = targetsWithin.get(id) ?? [];
|
|
113
|
+
const index = next[next.length - 1];
|
|
114
|
+
if (index === targets.length) {
|
|
115
|
+
stack.pop();
|
|
116
|
+
next.pop();
|
|
117
|
+
state.set(id, "black");
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
next[next.length - 1] = index + 1;
|
|
121
|
+
const target = targets[index];
|
|
122
|
+
const current = state.get(target);
|
|
123
|
+
if (current === "gray")
|
|
124
|
+
recordCycle([...stack.slice(stack.indexOf(target)), target]);
|
|
125
|
+
else if (current === undefined)
|
|
126
|
+
open(target);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const guard = new IterationGuard("use graph walk", ITERATION_LIMIT);
|
|
130
|
+
const counts = new Map();
|
|
131
|
+
const collectUses = (element) => {
|
|
132
|
+
if (isSvgElement(element, "use")) {
|
|
133
|
+
guard.step();
|
|
134
|
+
const target = ultimateTarget(element, ids, countStep);
|
|
135
|
+
if (target !== null && "cycle" in target) {
|
|
136
|
+
recordCycle(target.cycle);
|
|
137
|
+
}
|
|
138
|
+
else if (target !== null) {
|
|
139
|
+
counts.set(target.id, (counts.get(target.id) ?? 0) + 1);
|
|
140
|
+
visit(target.id);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
for (const child of childElements(element))
|
|
144
|
+
collectUses(child);
|
|
145
|
+
};
|
|
146
|
+
collectUses(root);
|
|
147
|
+
// A target outside <defs> is also drawn in place, so it becomes an
|
|
148
|
+
// instance in place: every occurrence is the same kind of layer.
|
|
149
|
+
const drawnInPlace = new Set();
|
|
150
|
+
const markDrawn = (element, inDefs) => {
|
|
151
|
+
const id = elementId(element);
|
|
152
|
+
if (id !== null && !inDefs && counts.has(id) && !isSvgElement(element, "use")) {
|
|
153
|
+
drawnInPlace.add(id);
|
|
154
|
+
}
|
|
155
|
+
for (const child of childElements(element)) {
|
|
156
|
+
markDrawn(child, inDefs || isSvgElement(element, "defs"));
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
markDrawn(root, false);
|
|
160
|
+
const decisions = new Map();
|
|
161
|
+
const targets = new Map();
|
|
162
|
+
for (const [id, count] of counts) {
|
|
163
|
+
const target = ids.get(id);
|
|
164
|
+
if (target === undefined)
|
|
165
|
+
continue;
|
|
166
|
+
const isSymbol = isSvgElement(target, "symbol");
|
|
167
|
+
decisions.set(id, count >= 2 || isSymbol || drawnInPlace.has(id) ? "component" : "copy");
|
|
168
|
+
targets.set(id, target);
|
|
169
|
+
}
|
|
170
|
+
return { decisions, targets, cycles };
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* A `<use>` or `<symbol>` geometry attribute in user units. It is a length:
|
|
174
|
+
* absolute units, em and ex against the element's font size and rem against
|
|
175
|
+
* the root's (both in `fonts`), or a percentage of the
|
|
176
|
+
* viewport's width (`x`, `width`) or height (`y`, `height`). Null when the
|
|
177
|
+
* attribute is absent, "unreadable" when it is not a length, or is a
|
|
178
|
+
* percentage with no viewport to resolve it against.
|
|
179
|
+
*/
|
|
180
|
+
function geometryLength(element, name, fonts, viewport) {
|
|
181
|
+
const text = attr(element, name);
|
|
182
|
+
if (text === null)
|
|
183
|
+
return null;
|
|
184
|
+
const parsed = parseSvgLength(text, fonts);
|
|
185
|
+
if (parsed === null)
|
|
186
|
+
return "unreadable";
|
|
187
|
+
if (parsed.kind === "absolute")
|
|
188
|
+
return parsed.value;
|
|
189
|
+
if (viewport === null)
|
|
190
|
+
return "unreadable";
|
|
191
|
+
return parsed.fraction * (name === "x" || name === "width" ? viewport.width : viewport.height);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* What percentages inside a `<symbol>` resolve against: its viewBox, which
|
|
195
|
+
* establishes a new viewport (SVG 2 §8.9), else the viewport around it.
|
|
196
|
+
*/
|
|
197
|
+
function symbolViewport(size, ctx) {
|
|
198
|
+
return size === null ? ctx.viewport : { width: size.viewBox.width, height: size.viewBox.height };
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* A `<symbol>`'s own size: its `width`/`height`, else its viewBox's size.
|
|
202
|
+
* Without a viewBox, or with one that cannot be read or has a negative
|
|
203
|
+
* side, which voids it,
|
|
204
|
+
* the symbol still establishes a viewport (SVG 2 §5.6):
|
|
205
|
+
* `width`/`height` default to 100% of the viewport around it, and its
|
|
206
|
+
* content keeps its user units, so the viewport clips it and never scales
|
|
207
|
+
* it. That case returns a viewBox of the viewport itself and `scales` false.
|
|
208
|
+
*/
|
|
209
|
+
function symbolSize(symbol, sizeUse, ctx) {
|
|
210
|
+
const viewBoxText = attr(symbol, "viewBox");
|
|
211
|
+
const viewBox = viewBoxText === null ? null : parseViewBox(viewBoxText);
|
|
212
|
+
// Unreadable text or a negative side voids the viewBox (SVG 2 §8.6), as if it were absent.
|
|
213
|
+
const voided = viewBox === null || viewBox.width < 0 || viewBox.height < 0;
|
|
214
|
+
const lengthOf = (element, name) => {
|
|
215
|
+
const fontSize = ctx.styles.get(element)?.fontSize ?? initialComputedStyle().fontSize;
|
|
216
|
+
const value = geometryLength(element, name, { fontSize, rootFontSize: ctx.rules.rootFontSize }, ctx.viewport);
|
|
217
|
+
return typeof value === "number" ? value : null;
|
|
218
|
+
};
|
|
219
|
+
const length = (name) => {
|
|
220
|
+
const value = (sizeUse === null ? null : lengthOf(sizeUse, name)) ?? lengthOf(symbol, name);
|
|
221
|
+
return value !== null && value > 0 ? value : null;
|
|
222
|
+
};
|
|
223
|
+
if (voided) {
|
|
224
|
+
const width = length("width") ?? ctx.viewport?.width ?? null;
|
|
225
|
+
const height = length("height") ?? ctx.viewport?.height ?? null;
|
|
226
|
+
if (width === null || height === null || width <= 0 || height <= 0)
|
|
227
|
+
return null;
|
|
228
|
+
return { viewBox: { minX: 0, minY: 0, width, height }, width, height, scales: false };
|
|
229
|
+
}
|
|
230
|
+
if (viewBox.width <= 0 || viewBox.height <= 0)
|
|
231
|
+
return null;
|
|
232
|
+
return {
|
|
233
|
+
viewBox,
|
|
234
|
+
width: length("width") ?? viewBox.width,
|
|
235
|
+
height: length("height") ?? viewBox.height,
|
|
236
|
+
scales: true,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Why a `<symbol>` drawn by a use has a viewport with no area, or null when
|
|
241
|
+
* it has some. An explicit zero `width` or `height` (the use's, else the
|
|
242
|
+
* symbol's) or a viewBox side of zero disables rendering (SVG 2 §8.2, §8.6),
|
|
243
|
+
* so the occurrence draws nothing; an absent side is not zero.
|
|
244
|
+
*/
|
|
245
|
+
function emptySymbolReason(symbol, sizeUse, ctx) {
|
|
246
|
+
for (const side of ["width", "height"]) {
|
|
247
|
+
const lengthOf = (element) => {
|
|
248
|
+
const fontSize = ctx.styles.get(element)?.fontSize ?? initialComputedStyle().fontSize;
|
|
249
|
+
const value = geometryLength(element, side, { fontSize, rootFontSize: ctx.rules.rootFontSize }, ctx.viewport);
|
|
250
|
+
return typeof value === "number" ? value : null;
|
|
251
|
+
};
|
|
252
|
+
if (((sizeUse === null ? null : lengthOf(sizeUse)) ?? lengthOf(symbol)) === 0) {
|
|
253
|
+
return `the symbol's viewport ${side} is zero, so nothing inside it draws`;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const viewBoxText = attr(symbol, "viewBox");
|
|
257
|
+
const viewBox = viewBoxText === null ? null : parseViewBox(viewBoxText);
|
|
258
|
+
if (viewBox !== null && (viewBox.width === 0 || viewBox.height === 0)) {
|
|
259
|
+
return `the symbol's viewBox ${viewBox.width === 0 ? "width" : "height"} is zero, so nothing inside it draws`;
|
|
260
|
+
}
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* A `<symbol>`'s own style under the use that draws it, which its children
|
|
265
|
+
* inherit. `ctx` carries the symbol's own viewport, so a percentage set on
|
|
266
|
+
* the symbol resolves against its viewBox as its children's do.
|
|
267
|
+
*/
|
|
268
|
+
function symbolStyle(symbol, useStyle, ctx) {
|
|
269
|
+
return computeElementStyle(symbol, useStyle, ctx.rules, ctx.viewport);
|
|
270
|
+
}
|
|
271
|
+
/** The matrix that fits a `<symbol>`'s viewBox into a size, by its `preserveAspectRatio`. */
|
|
272
|
+
function symbolFit(symbol, size) {
|
|
273
|
+
return viewBoxTransform(size.viewBox, size.width, size.height, parsePreserveAspectRatio(attr(symbol, "preserveAspectRatio") ?? "") ?? DEFAULT_PRESERVE_ASPECT_RATIO);
|
|
274
|
+
}
|
|
275
|
+
/** Puts already-built top layers into a group's coordinates and relinks them among the subtree's nodes. */
|
|
276
|
+
function relinked(nodes, shifted) {
|
|
277
|
+
const byId = new Map(shifted.map((node) => [node.id, node]));
|
|
278
|
+
return nodes.map((node) => byId.get(node.id) ?? node);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Builds a component's tree from its target with one occurrence's style.
|
|
282
|
+
* Used for the shipped component (the first occurrence) and, under a probe
|
|
283
|
+
* context, for comparing a later occurrence against it.
|
|
284
|
+
*/
|
|
285
|
+
function buildComponentTree(target, baseStyle, ctx, sizeUse) {
|
|
286
|
+
const title = childElements(target).find((child) => isSvgElement(child, "title"));
|
|
287
|
+
const componentName = elementId(target) ?? title?.textContent?.trim() ?? "Component";
|
|
288
|
+
let componentCtx = { ...ctx, buildingComponentTree: true };
|
|
289
|
+
const targetPath = ctx.paths.get(target) ?? target.localName;
|
|
290
|
+
let children;
|
|
291
|
+
let subtreeNodes;
|
|
292
|
+
let naturalBounds;
|
|
293
|
+
let fittedSize = null;
|
|
294
|
+
let clipContent = false;
|
|
295
|
+
if (isSvgElement(target, "symbol")) {
|
|
296
|
+
// A `<symbol>`'s children become the component root's children directly.
|
|
297
|
+
// Without a usable viewBox the content keeps its user units and its
|
|
298
|
+
// percentages resolve against each use's viewport, so the component is
|
|
299
|
+
// built at the occurrence's own size; with a viewBox the content scales
|
|
300
|
+
// into whatever size a use gives it, so the symbol's own size sizes the
|
|
301
|
+
// component.
|
|
302
|
+
const viewBoxText = attr(target, "viewBox");
|
|
303
|
+
const viewBox = viewBoxText === null ? null : parseViewBox(viewBoxText);
|
|
304
|
+
const voided = viewBox === null || viewBox.width < 0 || viewBox.height < 0;
|
|
305
|
+
const size = symbolSize(target, voided ? sizeUse : null, ctx);
|
|
306
|
+
componentCtx = { ...componentCtx, viewport: symbolViewport(size, ctx) };
|
|
307
|
+
const style = symbolStyle(target, baseStyle, componentCtx);
|
|
308
|
+
clipContent = !style.overflowVisible;
|
|
309
|
+
const built = buildChildren(target, style, size === null ? IDENTITY_MATRIX : symbolFit(target, size), targetPath, componentCtx);
|
|
310
|
+
children = built.children;
|
|
311
|
+
subtreeNodes = built.nodes;
|
|
312
|
+
if (size !== null) {
|
|
313
|
+
naturalBounds = { x: 0, y: 0, width: size.width, height: size.height };
|
|
314
|
+
fittedSize = { width: size.width, height: size.height, scales: size.scales };
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
naturalBounds = unionBoxes(children.map((child) => getNodeBounds(child))) ?? { x: 0, y: 0, width: 0, height: 0 };
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
// Any other target element becomes a single child (a `<g>` as a group).
|
|
322
|
+
const built = buildElement(target, baseStyle, IDENTITY_MATRIX, targetPath, componentCtx);
|
|
323
|
+
children = built.top === null ? [] : [built.top];
|
|
324
|
+
subtreeNodes = built.nodes;
|
|
325
|
+
naturalBounds = unionBoxes(children.map((child) => getNodeBounds(child))) ?? { x: 0, y: 0, width: 0, height: 0 };
|
|
326
|
+
}
|
|
327
|
+
const constrained = children.map((child) => {
|
|
328
|
+
const shifted = shiftNode(child, -naturalBounds.x, -naturalBounds.y);
|
|
329
|
+
// A group has no constraints of its own — its size is its layers' — and
|
|
330
|
+
// the document codec rejects one carrying them. A symbol with no viewBox
|
|
331
|
+
// clips its content to a resized viewport and never scales it, so its
|
|
332
|
+
// content keeps its place and size.
|
|
333
|
+
if (shifted.type === "group")
|
|
334
|
+
return shifted;
|
|
335
|
+
return {
|
|
336
|
+
...shifted,
|
|
337
|
+
constraints: fittedSize?.scales === false
|
|
338
|
+
? { horizontal: "left", vertical: "top" }
|
|
339
|
+
: { horizontal: "scale", vertical: "scale" },
|
|
340
|
+
};
|
|
341
|
+
});
|
|
342
|
+
const rootId = ctx.idSource();
|
|
343
|
+
const componentId = ctx.idSource();
|
|
344
|
+
const root = {
|
|
345
|
+
id: rootId,
|
|
346
|
+
name: componentName,
|
|
347
|
+
visible: true,
|
|
348
|
+
locked: false,
|
|
349
|
+
opacity: 1,
|
|
350
|
+
type: "frame",
|
|
351
|
+
frame: { x: 0, y: 0, width: naturalBounds.width, height: naturalBounds.height },
|
|
352
|
+
rotation: 0,
|
|
353
|
+
children: constrained.map((child) => child.id),
|
|
354
|
+
layout: { kind: "absolute" },
|
|
355
|
+
// A <symbol> establishes a viewport, and SVG clips a viewport by default
|
|
356
|
+
// (overflow hidden), so its content is clipped to the symbol's box
|
|
357
|
+
// unless its overflow is visible.
|
|
358
|
+
...(clipContent ? { clipContent: true } : {}),
|
|
359
|
+
};
|
|
360
|
+
const definition = {
|
|
361
|
+
id: componentId,
|
|
362
|
+
key: ctx.idSource(),
|
|
363
|
+
name: componentName,
|
|
364
|
+
rootId,
|
|
365
|
+
};
|
|
366
|
+
return {
|
|
367
|
+
component: { definition, nodes: [root, ...relinked(subtreeNodes, constrained)] },
|
|
368
|
+
baseStyle,
|
|
369
|
+
naturalBounds,
|
|
370
|
+
symbolSize: fittedSize,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Flattens a component's tree in paint order, pairing each node with its
|
|
375
|
+
* override key: the layer's own node ID, the shape the model's instance
|
|
376
|
+
* resolver looks up — ordinary descendants key by their own ID alone and
|
|
377
|
+
* only nested instance IDs prefix a path, and a nested instance's own
|
|
378
|
+
* layers key by their own IDs in the nested component.
|
|
379
|
+
*/
|
|
380
|
+
function flattenComponent(component) {
|
|
381
|
+
const byId = new Map(component.nodes.map((node) => [node.id, node]));
|
|
382
|
+
const out = [];
|
|
383
|
+
const walk = (id) => {
|
|
384
|
+
const node = byId.get(id);
|
|
385
|
+
if (node === undefined)
|
|
386
|
+
return;
|
|
387
|
+
out.push({ path: id, node });
|
|
388
|
+
if ("children" in node) {
|
|
389
|
+
for (const childId of node.children)
|
|
390
|
+
walk(childId);
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
walk(component.definition.rootId);
|
|
394
|
+
return out;
|
|
395
|
+
}
|
|
396
|
+
/** A geometry fingerprint: frame and rotation, or the path transform. */
|
|
397
|
+
function geometryOf(node) {
|
|
398
|
+
if ("frame" in node) {
|
|
399
|
+
const { x, y, width, height } = node.frame;
|
|
400
|
+
return `${x},${y},${width},${height},${node.rotation}`;
|
|
401
|
+
}
|
|
402
|
+
if (node.type === "shape" && node.shape === "path")
|
|
403
|
+
return node.transform.join(",");
|
|
404
|
+
return "";
|
|
405
|
+
}
|
|
406
|
+
/** The style-bearing fields an override can carry, compared between two builds. */
|
|
407
|
+
function styleSnapshot(node) {
|
|
408
|
+
return {
|
|
409
|
+
fills: "style" in node ? node.style?.fills ?? null : null,
|
|
410
|
+
strokes: "style" in node ? node.style?.strokes ?? null : null,
|
|
411
|
+
visible: node.visible,
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Everything else about a layer that the two builds must share: every field
|
|
416
|
+
* but its id, its children's ids (the walk pairs those), its visibility, and
|
|
417
|
+
* its fills and strokes, which overrides carry. An inherited value such as
|
|
418
|
+
* `fill-rule` or `stroke-linecap` changes the path's network or caps here.
|
|
419
|
+
*/
|
|
420
|
+
function drawingRest(node) {
|
|
421
|
+
const rest = { ...node };
|
|
422
|
+
delete rest.id;
|
|
423
|
+
delete rest.children;
|
|
424
|
+
delete rest.visible;
|
|
425
|
+
if (typeof rest.style === "object" && rest.style !== null) {
|
|
426
|
+
const style = { ...rest.style };
|
|
427
|
+
delete style.fills;
|
|
428
|
+
delete style.strokes;
|
|
429
|
+
rest.style = style;
|
|
430
|
+
}
|
|
431
|
+
return JSON.stringify(rest);
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Compares the shipped component with an occurrence's probe build. Only
|
|
435
|
+
* fills, strokes and visibility may differ, and each becomes an override on
|
|
436
|
+
* the layer where it differs; anything else — structure, geometry, text
|
|
437
|
+
* content, a nested instance's own overrides — gives the reason the
|
|
438
|
+
* occurrence must be a copy instead.
|
|
439
|
+
*/
|
|
440
|
+
function occurrenceOverrides(component, probe) {
|
|
441
|
+
const left = flattenComponent(component);
|
|
442
|
+
const right = flattenComponent(probe);
|
|
443
|
+
if (left.length !== right.length)
|
|
444
|
+
return { reason: "its content has different layers" };
|
|
445
|
+
const overrides = {};
|
|
446
|
+
for (let index = 0; index < left.length; index += 1) {
|
|
447
|
+
const { path, node: before } = left[index];
|
|
448
|
+
const after = right[index].node;
|
|
449
|
+
if (before.type !== after.type)
|
|
450
|
+
return { reason: "its content has different layers" };
|
|
451
|
+
if (geometryOf(before) !== geometryOf(after)) {
|
|
452
|
+
return { reason: `the layer "${before.name}" has a different size or position` };
|
|
453
|
+
}
|
|
454
|
+
if (before.type === "text" && after.type === "text"
|
|
455
|
+
&& JSON.stringify(before.paragraphs) !== JSON.stringify(after.paragraphs)) {
|
|
456
|
+
return { reason: `the text "${before.name}" is styled differently` };
|
|
457
|
+
}
|
|
458
|
+
if (before.type === "instance" && after.type === "instance"
|
|
459
|
+
&& (before.componentId !== after.componentId
|
|
460
|
+
|| JSON.stringify(before.overrides ?? null) !== JSON.stringify(after.overrides ?? null))) {
|
|
461
|
+
return { reason: `the nested instance "${before.name}" is styled differently` };
|
|
462
|
+
}
|
|
463
|
+
if (drawingRest(before) !== drawingRest(after)) {
|
|
464
|
+
return { reason: `the layer "${before.name}" is drawn differently` };
|
|
465
|
+
}
|
|
466
|
+
const a = styleSnapshot(before);
|
|
467
|
+
const b = styleSnapshot(after);
|
|
468
|
+
const override = {};
|
|
469
|
+
if (JSON.stringify(a.fills) !== JSON.stringify(b.fills))
|
|
470
|
+
override.fills = b.fills ?? [];
|
|
471
|
+
if (JSON.stringify(a.strokes) !== JSON.stringify(b.strokes))
|
|
472
|
+
override.strokes = b.strokes ?? [];
|
|
473
|
+
if (a.visible !== b.visible)
|
|
474
|
+
override.visible = b.visible;
|
|
475
|
+
if (Object.keys(override).length > 0)
|
|
476
|
+
overrides[path] = override;
|
|
477
|
+
}
|
|
478
|
+
return { overrides };
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* A context that builds without side effects, for comparing an occurrence
|
|
482
|
+
* with the shipped component: throwaway ids, no report lines, no new
|
|
483
|
+
* images or components, and no cap counts of its own beyond the import-wide
|
|
484
|
+
* work (reference steps, path data, element builds and style work) the probe
|
|
485
|
+
* really does.
|
|
486
|
+
*/
|
|
487
|
+
export function probeContext(ctx) {
|
|
488
|
+
let next = 0;
|
|
489
|
+
return {
|
|
490
|
+
...ctx,
|
|
491
|
+
probing: true,
|
|
492
|
+
idSource: () => `probe:${String(++next)}`,
|
|
493
|
+
reportBuilder: new ReportBuilder(),
|
|
494
|
+
report: () => undefined,
|
|
495
|
+
limits: ctx.limits.forProbe(),
|
|
496
|
+
images: [],
|
|
497
|
+
components: [],
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
/** The component registry of one document: builds each component once, at its first occurrence. */
|
|
501
|
+
export class ComponentRegistry {
|
|
502
|
+
#built = new Map();
|
|
503
|
+
/**
|
|
504
|
+
* The component for one target, building it on first use. Under a probe
|
|
505
|
+
* context a missing component is built for comparison only and never
|
|
506
|
+
* registered.
|
|
507
|
+
*/
|
|
508
|
+
getOrBuild(targetId, target, baseStyle, ctx, sizeUse) {
|
|
509
|
+
const existing = this.#built.get(targetId);
|
|
510
|
+
if (existing !== undefined)
|
|
511
|
+
return existing;
|
|
512
|
+
ctx.limits.countComponent();
|
|
513
|
+
const built = buildComponentTree(target, baseStyle, ctx, sizeUse);
|
|
514
|
+
if (ctx.probing)
|
|
515
|
+
return built;
|
|
516
|
+
ctx.components.push(built.component);
|
|
517
|
+
this.#built.set(targetId, built);
|
|
518
|
+
return built;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Scales what a scaled instance does not scale by itself. The model scales
|
|
523
|
+
* an instance's geometry with its frame, but not stroke widths, so each
|
|
524
|
+
* stroked layer gets a strokes override scaled to match the SVG; corner
|
|
525
|
+
* radii, effects, text sizes and nested instances keep their component
|
|
526
|
+
* size and are reported. An uneven scale has no exact stroke width: the
|
|
527
|
+
* strokes take the geometric mean of the two scales and that is reported.
|
|
528
|
+
*/
|
|
529
|
+
function scaleOverrides(built, frame, overrides, elementPath, elementName, ctx) {
|
|
530
|
+
const root = built.component.nodes.find((node) => node.id === built.component.definition.rootId);
|
|
531
|
+
if (root === undefined || !("frame" in root) || root.frame.width <= 0 || root.frame.height <= 0)
|
|
532
|
+
return;
|
|
533
|
+
const scaleX = frame.width / root.frame.width;
|
|
534
|
+
const scaleY = frame.height / root.frame.height;
|
|
535
|
+
if (Math.abs(scaleX - 1) < 1e-9 && Math.abs(scaleY - 1) < 1e-9)
|
|
536
|
+
return;
|
|
537
|
+
const uniform = sameScale(scaleX, scaleY);
|
|
538
|
+
const scale = uniform ? scaleX : Math.sqrt(scaleX * scaleY);
|
|
539
|
+
const unscaled = new Set();
|
|
540
|
+
let stroked = false;
|
|
541
|
+
for (const { path, node } of flattenComponent(built.component).slice(1)) {
|
|
542
|
+
if (node.type === "instance")
|
|
543
|
+
unscaled.add("nested instances");
|
|
544
|
+
if (node.type === "text")
|
|
545
|
+
unscaled.add("text sizes");
|
|
546
|
+
if (!("style" in node) || node.style === undefined)
|
|
547
|
+
continue;
|
|
548
|
+
const style = node.style;
|
|
549
|
+
if ((style.cornerRadius ?? 0) > 0)
|
|
550
|
+
unscaled.add("corner radii");
|
|
551
|
+
if ((style.shadows?.length ?? 0) > 0 || (style.layerBlur ?? 0) > 0)
|
|
552
|
+
unscaled.add("effects");
|
|
553
|
+
const strokes = overrides[path]?.strokes ?? style.strokes;
|
|
554
|
+
if (strokes === undefined || strokes.length === 0)
|
|
555
|
+
continue;
|
|
556
|
+
// A non-scaling stroke keeps its width under the instance's scale.
|
|
557
|
+
if (strokes.every(isNonScalingStroke))
|
|
558
|
+
continue;
|
|
559
|
+
stroked = true;
|
|
560
|
+
overrides[path] = {
|
|
561
|
+
...overrides[path],
|
|
562
|
+
strokes: strokes.map((stroke) => isNonScalingStroke(stroke) ? stroke : ({
|
|
563
|
+
...stroke,
|
|
564
|
+
width: stroke.width * scale,
|
|
565
|
+
...(stroke.dash === undefined ? {} : { dash: stroke.dash.map((length) => length * scale) }),
|
|
566
|
+
})),
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
if (stroked && !uniform) {
|
|
570
|
+
ctx.report(elementPath, elementName, "approximated", "the instance is scaled unevenly, so its stroke widths use the average of the two scales", { what: "Strokes in unevenly scaled instances", kept: "One even stroke width is used" });
|
|
571
|
+
}
|
|
572
|
+
if (unscaled.size > 0) {
|
|
573
|
+
ctx.report(elementPath, elementName, "approximated", `the instance is scaled, but its ${[...unscaled].join(", ")} keep the component's size`, { what: "Sizes in scaled instances", kept: "Kept at the component's size" });
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
/** Builds an instance of one component for one occurrence, or the reason it must be a copy. */
|
|
577
|
+
function instantiate(built, occurrence, naturalFrame, target, elementPath, elementName, ctx) {
|
|
578
|
+
const plan = rasterFramePlan(naturalFrame, occurrence.matrix);
|
|
579
|
+
if (plan === null) {
|
|
580
|
+
return { reason: "its transform skews it or cannot be inverted, which an instance cannot show" };
|
|
581
|
+
}
|
|
582
|
+
let overrides = {};
|
|
583
|
+
// A symbol with no viewBox never scales its content: percentages in it
|
|
584
|
+
// resolve against each use's own viewport, so a use at another size than
|
|
585
|
+
// the component's draws different geometry, which the probe compares.
|
|
586
|
+
const resizedViewport = built.symbolSize !== null && built.symbolSize.scales === false
|
|
587
|
+
&& (Math.abs(naturalFrame.width - built.symbolSize.width) > 1e-6
|
|
588
|
+
|| Math.abs(naturalFrame.height - built.symbolSize.height) > 1e-6);
|
|
589
|
+
if (occurrence.baseStyle !== built.baseStyle || resizedViewport) {
|
|
590
|
+
const probe = buildComponentTree(target, occurrence.baseStyle, probeContext(ctx), occurrence.sizeUse);
|
|
591
|
+
const compared = occurrenceOverrides(built.component, probe.component);
|
|
592
|
+
if ("reason" in compared)
|
|
593
|
+
return compared;
|
|
594
|
+
overrides = compared.overrides;
|
|
595
|
+
}
|
|
596
|
+
if (built.symbolSize?.scales !== false)
|
|
597
|
+
scaleOverrides(built, plan.frame, overrides, elementPath, elementName, ctx);
|
|
598
|
+
ctx.limits.countInstance();
|
|
599
|
+
const instance = {
|
|
600
|
+
id: ctx.idSource(),
|
|
601
|
+
name: built.component.definition.name,
|
|
602
|
+
visible: true,
|
|
603
|
+
locked: false,
|
|
604
|
+
opacity: occurrence.chrome.opacity,
|
|
605
|
+
...(occurrence.chrome.blendMode === null ? {} : { blendMode: occurrence.chrome.blendMode }),
|
|
606
|
+
type: "instance",
|
|
607
|
+
frame: plan.frame,
|
|
608
|
+
rotation: plan.rotation,
|
|
609
|
+
...(plan.flipY ? { flipY: true } : {}),
|
|
610
|
+
componentId: built.component.definition.id,
|
|
611
|
+
...(Object.keys(overrides).length > 0 ? { overrides } : {}),
|
|
612
|
+
};
|
|
613
|
+
return { instance };
|
|
614
|
+
}
|
|
615
|
+
/** A use's x/y offset as a translation. */
|
|
616
|
+
function useOffset(use, style, elementPath, ctx) {
|
|
617
|
+
const offset = (name) => {
|
|
618
|
+
const value = geometryLength(use, name, { fontSize: style.fontSize, rootFontSize: ctx.rules.rootFontSize }, ctx.viewport);
|
|
619
|
+
if (value !== "unreadable")
|
|
620
|
+
return value ?? 0;
|
|
621
|
+
ctx.report(elementPath, "use", "approximated", `the use's ${name} "${attr(use, name) ?? ""}" cannot be read, so 0 is used`, { what: "Positions that cannot be read", kept: "0 is used" });
|
|
622
|
+
return 0;
|
|
623
|
+
};
|
|
624
|
+
return [1, 0, 0, 1, offset("x"), offset("y")];
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Follows a `<use>` through any chain of uses to the element it finally
|
|
628
|
+
* draws. Every use on the way adds its transform and x/y, its opacity and
|
|
629
|
+
* its inherited style, as SVG draws a use as a group holding its target.
|
|
630
|
+
* The middle uses are returned outermost first, so their clip-path, mask
|
|
631
|
+
* and filter can wrap the built content.
|
|
632
|
+
*/
|
|
633
|
+
function resolveUse(element, computed, matrix, elementPath, ctx) {
|
|
634
|
+
const guard = new IterationGuard("use chain resolution", ITERATION_LIMIT);
|
|
635
|
+
const top = layerChrome(computed);
|
|
636
|
+
let style = computed;
|
|
637
|
+
let occurrenceMatrix = multiplyTransforms(matrix, useOffset(element, computed, elementPath, ctx));
|
|
638
|
+
let opacity = top.opacity;
|
|
639
|
+
let current = element;
|
|
640
|
+
const middleUses = [];
|
|
641
|
+
for (;;) {
|
|
642
|
+
guard.step();
|
|
643
|
+
ctx.limits.countReferenceStep();
|
|
644
|
+
if (hrefIsExternal(current))
|
|
645
|
+
return { reason: "external references are never fetched" };
|
|
646
|
+
const id = hrefTargetId(current);
|
|
647
|
+
const cycle = id === null ? undefined : ctx.reuse.cycles.get(id);
|
|
648
|
+
if (cycle !== undefined)
|
|
649
|
+
throw new SvgImportRefusalError("use-cycle", cycle);
|
|
650
|
+
const next = id === null ? undefined : ctx.ids.get(id);
|
|
651
|
+
if (id === null || next === undefined) {
|
|
652
|
+
return { reason: `the reference ${hrefValue(current) ?? ""} is missing` };
|
|
653
|
+
}
|
|
654
|
+
if (!isSvgElement(next, "use")) {
|
|
655
|
+
return {
|
|
656
|
+
targetId: id,
|
|
657
|
+
target: next,
|
|
658
|
+
occurrence: {
|
|
659
|
+
baseStyle: style,
|
|
660
|
+
matrix: occurrenceMatrix,
|
|
661
|
+
chrome: { opacity, blendMode: top.blendMode },
|
|
662
|
+
sizeUse: isSvgElement(next, "symbol") ? current : null,
|
|
663
|
+
},
|
|
664
|
+
middleUses,
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
style = computeElementStyle(next, style, ctx.rules, ctx.viewport);
|
|
668
|
+
if (style.display === "none")
|
|
669
|
+
return { reason: `the referenced <use> #${id} has display:none` };
|
|
670
|
+
const transformText = style.cssTransformWins ? null : attr(next, "transform");
|
|
671
|
+
if (transformText !== null) {
|
|
672
|
+
const parsed = parseTransform(transformText);
|
|
673
|
+
if (parsed === null) {
|
|
674
|
+
// An invalid transform attribute is ignored, as a browser ignores it.
|
|
675
|
+
ctx.report(elementPath, "use", "left-out", `the transform "${transformText}" of the referenced <use> #${id} cannot be read, so it is ignored`, { what: "Transforms that cannot be read", kept: "Drawn without the transform" });
|
|
676
|
+
}
|
|
677
|
+
else {
|
|
678
|
+
occurrenceMatrix = multiplyTransforms(occurrenceMatrix, parsed);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
middleUses.push({ element: next, style, matrix: occurrenceMatrix });
|
|
682
|
+
occurrenceMatrix = multiplyTransforms(occurrenceMatrix, useOffset(next, style, elementPath, ctx));
|
|
683
|
+
const chrome = layerChrome(style);
|
|
684
|
+
opacity *= chrome.opacity;
|
|
685
|
+
if (chrome.blendMode !== null) {
|
|
686
|
+
ctx.report(elementPath, "use", "approximated", `the blend mode of the referenced <use> #${id} is left out`, BLEND_NOTE);
|
|
687
|
+
}
|
|
688
|
+
current = next;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
/** Applies a use's opacity and blend mode to the layer it made. */
|
|
692
|
+
function withUseChrome(top, chrome, elementPath, ctx) {
|
|
693
|
+
let blendMode = top.blendMode;
|
|
694
|
+
if (chrome.blendMode !== null) {
|
|
695
|
+
if (blendMode === undefined)
|
|
696
|
+
blendMode = chrome.blendMode;
|
|
697
|
+
else
|
|
698
|
+
ctx.report(elementPath, "use", "approximated", "the use's blend mode is left out: its content has its own", BLEND_NOTE);
|
|
699
|
+
}
|
|
700
|
+
return {
|
|
701
|
+
...top,
|
|
702
|
+
opacity: top.opacity * chrome.opacity,
|
|
703
|
+
...(blendMode === undefined ? {} : { blendMode }),
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Draws one use as a plain copy of its target: the target's layers at the
|
|
708
|
+
* use's place. A `<symbol>` becomes a group named after it, holding its
|
|
709
|
+
* content fitted into the use's size. The in-place instance hook stays off,
|
|
710
|
+
* so the copy is never turned back into an instance.
|
|
711
|
+
*/
|
|
712
|
+
function copyOfTarget(target, occurrence, elementPath, ctx) {
|
|
713
|
+
let copyCtx = { ...ctx, buildingComponentTree: true };
|
|
714
|
+
const targetPath = ctx.paths.get(target) ?? elementPath;
|
|
715
|
+
if (isSvgElement(target, "symbol")) {
|
|
716
|
+
const size = symbolSize(target, occurrence.sizeUse, ctx);
|
|
717
|
+
copyCtx = { ...copyCtx, viewport: symbolViewport(size, ctx) };
|
|
718
|
+
const contentMatrix = size === null
|
|
719
|
+
? occurrence.matrix
|
|
720
|
+
: multiplyTransforms(occurrence.matrix, symbolFit(target, size));
|
|
721
|
+
const style = symbolStyle(target, occurrence.baseStyle, copyCtx);
|
|
722
|
+
const { children, nodes } = buildChildren(target, style, contentMatrix, targetPath, copyCtx);
|
|
723
|
+
if (children.length === 0)
|
|
724
|
+
return { top: null, nodes };
|
|
725
|
+
const name = elementId(target) ?? "Symbol";
|
|
726
|
+
const viewportMask = size === null || style.overflowVisible ? null : symbolViewportMask(target, size, occurrence.matrix, targetPath, ctx);
|
|
727
|
+
if (viewportMask === null) {
|
|
728
|
+
const wrapped = wrapInGroup(target, children, name, ctx);
|
|
729
|
+
const group = withUseChrome(wrapped.group, occurrence.chrome, elementPath, ctx);
|
|
730
|
+
return { top: group, nodes: [group, ...relinked(nodes, wrapped.shifted)] };
|
|
731
|
+
}
|
|
732
|
+
// A <symbol> clips to its viewport, as its instances do (clipContent):
|
|
733
|
+
// a clip group whose mask is the viewport through the use's transform.
|
|
734
|
+
const content = wrapInGroup(target, children, "Content", ctx);
|
|
735
|
+
const clipped = wrapInGroup(target, [viewportMask, content.group], name, ctx);
|
|
736
|
+
const group = withUseChrome(clipped.group, occurrence.chrome, elementPath, ctx);
|
|
737
|
+
return { top: group, nodes: [group, ...clipped.shifted, ...relinked(nodes, content.shifted)] };
|
|
738
|
+
}
|
|
739
|
+
const built = buildElement(target, occurrence.baseStyle, occurrence.matrix, targetPath, copyCtx);
|
|
740
|
+
if (built.top === null)
|
|
741
|
+
return built;
|
|
742
|
+
const top = withUseChrome(built.top, occurrence.chrome, elementPath, ctx);
|
|
743
|
+
return { top, nodes: relinked(built.nodes, [top]) };
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* The viewport of a `<symbol>` drawn as a copy, as a mask layer: the
|
|
747
|
+
* rectangle from 0,0 to its width and height in the use's space, carried
|
|
748
|
+
* through the use's transform (a skew bakes into the path). Null when the
|
|
749
|
+
* transform collapses the viewport, so nothing inside it paints anyway.
|
|
750
|
+
*/
|
|
751
|
+
function symbolViewportMask(symbol, size, matrix, elementPath, ctx) {
|
|
752
|
+
const placement = layerPlacement(matrix);
|
|
753
|
+
if (placement === null)
|
|
754
|
+
return null;
|
|
755
|
+
const built = pathLayerFromData(ctx.idSource(), "Viewport", `M0 0H${String(size.width)}V${String(size.height)}H0Z`, initialComputedStyle(), placement, elementPath, symbol, ctx);
|
|
756
|
+
return built === null ? null : { ...built.node, mask: true };
|
|
757
|
+
}
|
|
758
|
+
/** The natural frame an occurrence's instance places: the symbol's viewport, or the content's bounds. */
|
|
759
|
+
function naturalFrameOf(built, target, occurrence, elementPath, ctx) {
|
|
760
|
+
if (!isSvgElement(target, "symbol") || built.symbolSize === null)
|
|
761
|
+
return built.naturalBounds;
|
|
762
|
+
const size = symbolSize(target, occurrence.sizeUse, ctx);
|
|
763
|
+
const naturalAspect = built.symbolSize.width / built.symbolSize.height;
|
|
764
|
+
if (built.symbolSize.scales && Math.abs(size.width / size.height - naturalAspect) > 1e-6) {
|
|
765
|
+
ctx.report(elementPath, "use", "approximated", "the instance size does not match the symbol's aspect ratio; it stretches instead of meeting", { what: "Symbol aspect ratios", kept: "The instance stretches to its size" });
|
|
766
|
+
}
|
|
767
|
+
return { x: 0, y: 0, width: size.width, height: size.height };
|
|
768
|
+
}
|
|
769
|
+
/** Handles one `<use>` element: an instance, an inline copy, or a report. */
|
|
770
|
+
export function useLayer(element, computed, matrix, elementPath, ctx) {
|
|
771
|
+
ctx.limits.countUseCopy();
|
|
772
|
+
// The use element's own transform attribute is already composed into
|
|
773
|
+
// `matrix` by the caller; x/y follow it.
|
|
774
|
+
const resolved = resolveUse(element, computed, matrix, elementPath, ctx);
|
|
775
|
+
if ("reason" in resolved) {
|
|
776
|
+
ctx.report(elementPath, "use", "left-out", resolved.reason, { what: "References that cannot be followed (use)", kept: "Not drawn" });
|
|
777
|
+
return { top: null, nodes: [] };
|
|
778
|
+
}
|
|
779
|
+
let built = useTargetLayer(resolved, elementPath, ctx);
|
|
780
|
+
// The innermost middle use wraps the content first.
|
|
781
|
+
for (const middle of [...resolved.middleUses].reverse()) {
|
|
782
|
+
built = applyClipMaskFilter(middle.element, middle.style, middle.matrix, elementPath, built, ctx);
|
|
783
|
+
}
|
|
784
|
+
return built;
|
|
785
|
+
}
|
|
786
|
+
/** Builds the element a use chain finally draws: an instance, an inline copy, or a report. */
|
|
787
|
+
function useTargetLayer(resolved, elementPath, ctx) {
|
|
788
|
+
const { targetId, target, occurrence } = resolved;
|
|
789
|
+
if (isSvgElement(target, "symbol")) {
|
|
790
|
+
const empty = emptySymbolReason(target, occurrence.sizeUse, ctx);
|
|
791
|
+
if (empty !== null) {
|
|
792
|
+
ctx.report(elementPath, "use", "left-out", empty, { what: "Empty symbols", kept: "Not drawn" });
|
|
793
|
+
return { top: null, nodes: [] };
|
|
794
|
+
}
|
|
795
|
+
const viewBoxText = attr(target, "viewBox");
|
|
796
|
+
const viewBox = viewBoxText === null ? null : parseViewBox(viewBoxText);
|
|
797
|
+
if (viewBoxText !== null && viewBox === null) {
|
|
798
|
+
ctx.report(elementPath, "use", "left-out", `the symbol's viewBox "${viewBoxText}" cannot be read, so it is ignored`, VIEWBOX_NOTE);
|
|
799
|
+
}
|
|
800
|
+
else if (viewBox !== null && (viewBox.width < 0 || viewBox.height < 0)) {
|
|
801
|
+
ctx.report(elementPath, "use", "left-out", `the symbol's viewBox "${viewBoxText ?? ""}" has a negative size, so it is ignored`, VIEWBOX_NOTE);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
// Clip content counts by geometry only, and a clip cannot force an
|
|
805
|
+
// instance's component content opaque, so a use inside one is a copy.
|
|
806
|
+
if (ctx.insideClipPath === true || (ctx.reuse.decisions.get(targetId) ?? "copy") === "copy") {
|
|
807
|
+
return copyOfTarget(target, occurrence, elementPath, ctx);
|
|
808
|
+
}
|
|
809
|
+
const built = ctx.componentRegistry.getOrBuild(targetId, target, occurrence.baseStyle, ctx, occurrence.sizeUse);
|
|
810
|
+
const natural = naturalFrameOf(built, target, occurrence, elementPath, ctx);
|
|
811
|
+
const outcome = instantiate(built, occurrence, natural, target, elementPath, "use", ctx);
|
|
812
|
+
if ("reason" in outcome) {
|
|
813
|
+
ctx.report(elementPath, "use", "approximated", `made a copy instead of an instance: ${outcome.reason}`, COPY_NOTE);
|
|
814
|
+
return copyOfTarget(target, occurrence, elementPath, ctx);
|
|
815
|
+
}
|
|
816
|
+
return { top: outcome.instance, nodes: [outcome.instance] };
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Handles an element that is a component target drawn in place: it becomes
|
|
820
|
+
* an instance too, so every occurrence is the same kind of layer. The
|
|
821
|
+
* component already holds the element's own transform, opacity, clip, mask
|
|
822
|
+
* and filter, so the instance is placed with the parent's matrix and adds
|
|
823
|
+
* none of them again. Returns null when the in-place occurrence cannot be an
|
|
824
|
+
* instance; the reason is reported and the caller builds the element normally.
|
|
825
|
+
*/
|
|
826
|
+
export function drawnTargetLayer(element, targetId, parentStyle, parentMatrix, elementPath, ctx) {
|
|
827
|
+
const occurrence = {
|
|
828
|
+
baseStyle: parentStyle,
|
|
829
|
+
matrix: parentMatrix,
|
|
830
|
+
chrome: { opacity: 1, blendMode: null },
|
|
831
|
+
sizeUse: null,
|
|
832
|
+
};
|
|
833
|
+
const built = ctx.componentRegistry.getOrBuild(targetId, element, parentStyle, ctx, null);
|
|
834
|
+
const outcome = instantiate(built, occurrence, built.naturalBounds, element, elementPath, element.localName, ctx);
|
|
835
|
+
if ("reason" in outcome) {
|
|
836
|
+
ctx.report(elementPath, element.localName, "approximated", `made a copy instead of an instance: ${outcome.reason}`, COPY_NOTE);
|
|
837
|
+
return null;
|
|
838
|
+
}
|
|
839
|
+
return { top: outcome.instance, nodes: [outcome.instance] };
|
|
840
|
+
}
|