@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,429 @@
|
|
|
1
|
+
import { multiplyTransforms, } from "../../model/index.js";
|
|
2
|
+
import { attr, childElements, hrefValue, isSvgElement, } from "../tree/elements.js";
|
|
3
|
+
import { applyMatrixToPoint, applyMatrixToVector, invertMatrix, matrixIsDegenerate, parseTransform, } from "../transform/matrix.js";
|
|
4
|
+
import { parseSvgLength, parseSvgNumber } from "../units/length.js";
|
|
5
|
+
import { IterationGuard, ITERATION_LIMIT } from "../limits/limits.js";
|
|
6
|
+
import { hexColor } from "../style/color.js";
|
|
7
|
+
const GRADIENT_ATTRIBUTES = [
|
|
8
|
+
"x1", "y1", "x2", "y2", "cx", "cy", "r", "fx", "fy", "fr",
|
|
9
|
+
"gradientUnits", "spreadMethod", "gradientTransform",
|
|
10
|
+
];
|
|
11
|
+
/**
|
|
12
|
+
* Follows a gradient's `href` chain: attributes come from the nearest
|
|
13
|
+
* gradient in the chain that sets them, stops from the nearest that has
|
|
14
|
+
* `<stop>` children. A cycle, a dangling href or an href to something that
|
|
15
|
+
* is not a gradient ends the chain with what was found; the caller's report covers any real loss.
|
|
16
|
+
* A link to another file also ends it, and is reported, as it is never fetched.
|
|
17
|
+
*/
|
|
18
|
+
function resolveGradient(id, ctx) {
|
|
19
|
+
const guard = new IterationGuard("gradient href resolution", ITERATION_LIMIT);
|
|
20
|
+
const attributes = {};
|
|
21
|
+
let stops = null;
|
|
22
|
+
let kind = null;
|
|
23
|
+
let current = ctx.ids.get(id);
|
|
24
|
+
if (current === undefined)
|
|
25
|
+
return { kind: "missing" };
|
|
26
|
+
const seen = new Set();
|
|
27
|
+
let first = null;
|
|
28
|
+
while (current !== undefined) {
|
|
29
|
+
guard.step();
|
|
30
|
+
ctx.limits.countReferenceStep();
|
|
31
|
+
if (isSvgElement(current, "linearGradient")) {
|
|
32
|
+
kind ??= "linear";
|
|
33
|
+
}
|
|
34
|
+
else if (isSvgElement(current, "radialGradient")) {
|
|
35
|
+
kind ??= "radial";
|
|
36
|
+
}
|
|
37
|
+
else if (first !== null) {
|
|
38
|
+
// A template link to anything but a gradient is invalid, so the
|
|
39
|
+
// gradient keeps what the chain gave so far (SVG 2 §14.3.4).
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
else if (isSvgElement(current, "pattern")) {
|
|
43
|
+
return { kind: "pattern" };
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return { kind: "not-paint-server", localName: current.localName };
|
|
47
|
+
}
|
|
48
|
+
first ??= current;
|
|
49
|
+
const currentId = attr(current, "id");
|
|
50
|
+
if (currentId !== null) {
|
|
51
|
+
if (seen.has(currentId))
|
|
52
|
+
break;
|
|
53
|
+
seen.add(currentId);
|
|
54
|
+
}
|
|
55
|
+
for (const name of GRADIENT_ATTRIBUTES) {
|
|
56
|
+
if (attributes[name] !== undefined)
|
|
57
|
+
continue;
|
|
58
|
+
const value = attr(current, name);
|
|
59
|
+
if (value !== null)
|
|
60
|
+
attributes[name] = value;
|
|
61
|
+
}
|
|
62
|
+
if (stops === null) {
|
|
63
|
+
const stopElements = childElements(current).filter((child) => isSvgElement(child, "stop"));
|
|
64
|
+
if (stopElements.length > 0)
|
|
65
|
+
stops = readStops(stopElements, ctx);
|
|
66
|
+
}
|
|
67
|
+
const href = hrefValue(current);
|
|
68
|
+
if (href === null)
|
|
69
|
+
break;
|
|
70
|
+
if (!href.startsWith("#")) {
|
|
71
|
+
// Opening never fetches, so a template in another file is left out;
|
|
72
|
+
// a browser could fetch it and draw this gradient differently.
|
|
73
|
+
ctx.report(current.localName, `the template gradient "${href}" is in another file, which is never fetched; only this file's gradient attributes are used`, { what: "Gradients based on another file", kept: "Only this file's gradient settings are used" });
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
current = ctx.ids.get(href.slice(1));
|
|
77
|
+
}
|
|
78
|
+
if (kind === null || first === null)
|
|
79
|
+
return { kind: "missing" };
|
|
80
|
+
if (stops === null || stops.length === 0)
|
|
81
|
+
return null;
|
|
82
|
+
return { element: first, kind, attributes, stops };
|
|
83
|
+
}
|
|
84
|
+
/** Reads `<stop>` elements into model stops, folding `stop-opacity` into the color's alpha. */
|
|
85
|
+
function readStops(stopElements, ctx) {
|
|
86
|
+
const stops = [];
|
|
87
|
+
let previous = 0;
|
|
88
|
+
for (const stop of stopElements) {
|
|
89
|
+
const offsetText = attr(stop, "offset") ?? "0";
|
|
90
|
+
let offset;
|
|
91
|
+
if (offsetText.endsWith("%")) {
|
|
92
|
+
// The "%" follows the number directly: "50 %" is not a percentage.
|
|
93
|
+
const number = offsetText.slice(0, -1);
|
|
94
|
+
offset = number === number.trimEnd() ? (parseSvgNumber(number) ?? Number.NaN) / 100 : Number.NaN;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
offset = parseSvgNumber(offsetText) ?? Number.NaN;
|
|
98
|
+
}
|
|
99
|
+
if (!Number.isFinite(offset)) {
|
|
100
|
+
// An offset that cannot be read takes its initial value, 0.
|
|
101
|
+
ctx.report("stop", `the stop offset "${offsetText}" cannot be read, so 0 is used`, { what: "Gradient stops that cannot be read", kept: "The stop is placed at the start" });
|
|
102
|
+
offset = 0;
|
|
103
|
+
}
|
|
104
|
+
offset = Math.min(1, Math.max(0, offset));
|
|
105
|
+
previous = Math.max(previous, offset);
|
|
106
|
+
// A stop inherits through its own ancestors (the gradient, defs, the
|
|
107
|
+
// root), never through the element that references the gradient.
|
|
108
|
+
const style = ctx.styles.get(stop);
|
|
109
|
+
if (style === undefined)
|
|
110
|
+
throw new Error("a <stop> has no computed style from the pre-pass");
|
|
111
|
+
const color = { ...style.stopColor, a: style.stopColor.a * style.stopOpacity };
|
|
112
|
+
stops.push({ offset: previous, color: hexColor(color) });
|
|
113
|
+
}
|
|
114
|
+
return stops;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The viewport length a user-space percentage is a fraction of: its width,
|
|
118
|
+
* its height, or for a radius its normalized diagonal, sqrt((w² + h²) / 2).
|
|
119
|
+
*/
|
|
120
|
+
function viewportLength(viewport, axis) {
|
|
121
|
+
if (axis === "x")
|
|
122
|
+
return viewport.width;
|
|
123
|
+
if (axis === "y")
|
|
124
|
+
return viewport.height;
|
|
125
|
+
return Math.sqrt((viewport.width ** 2 + viewport.height ** 2) / 2);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* A number that may be a fraction (`0.5`), a percentage (`50%`) or a user
|
|
129
|
+
* length (`24`, `1in`). Callers pass a missing attribute as its SVG default,
|
|
130
|
+
* which is a percentage, so in user space it resolves against the viewport.
|
|
131
|
+
* For `objectBoundingBox` a length with units is read into user units and
|
|
132
|
+
* the number is the fraction of the bounding box, as Chromium draws it:
|
|
133
|
+
* `x1="1em"` at a 100px font is `x1="100"`.
|
|
134
|
+
*/
|
|
135
|
+
function gradientNumber(text, fallback, unitIsFraction, viewport, axis, fonts) {
|
|
136
|
+
const trimmed = text.trim();
|
|
137
|
+
if (trimmed.endsWith("%")) {
|
|
138
|
+
const number = parseSvgNumber(trimmed.slice(0, -1));
|
|
139
|
+
if (number === null)
|
|
140
|
+
return fallback;
|
|
141
|
+
const fraction = number / 100;
|
|
142
|
+
if (unitIsFraction)
|
|
143
|
+
return fraction;
|
|
144
|
+
if (viewport === null)
|
|
145
|
+
return fallback;
|
|
146
|
+
return fraction * viewportLength(viewport, axis);
|
|
147
|
+
}
|
|
148
|
+
if (unitIsFraction) {
|
|
149
|
+
const length = parseSvgLength(trimmed, fonts);
|
|
150
|
+
if (length === null)
|
|
151
|
+
return fallback;
|
|
152
|
+
return length.kind === "absolute" ? length.value : length.fraction;
|
|
153
|
+
}
|
|
154
|
+
const length = parseSvgLength(trimmed, fonts);
|
|
155
|
+
if (length === null)
|
|
156
|
+
return fallback;
|
|
157
|
+
if (length.kind === "absolute")
|
|
158
|
+
return length.value;
|
|
159
|
+
if (viewport === null)
|
|
160
|
+
return fallback;
|
|
161
|
+
return length.fraction * viewportLength(viewport, axis);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The font sizes for a gradient's coordinates. Chromium reads `em` and `ex`
|
|
165
|
+
* against the computed font size of the gradient the paint names, also for
|
|
166
|
+
* a coordinate taken from a template gradient; the painted element's font
|
|
167
|
+
* size does not count.
|
|
168
|
+
*/
|
|
169
|
+
function gradientFonts(gradient, ctx) {
|
|
170
|
+
const style = ctx.styles.get(gradient.element);
|
|
171
|
+
if (style === undefined)
|
|
172
|
+
throw new Error("a gradient has no computed style from the pre-pass");
|
|
173
|
+
return { fontSize: style.fontSize, rootFontSize: ctx.rootFontSize };
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Resolves a `url(#id)` paint reference to a model paint.
|
|
177
|
+
* `localFromElement` maps the referencing element's local user space into
|
|
178
|
+
* the layer's local geometry space, and `localBounds` is the layer's own
|
|
179
|
+
* bounding box in that space (the box the model's gradient coordinates are
|
|
180
|
+
* fractions of). Approximations are reported through `ctx.report`.
|
|
181
|
+
*/
|
|
182
|
+
export function resolvePaintServer(id, localFromElement, localBounds, ctx) {
|
|
183
|
+
const resolved = resolveGradient(id, ctx);
|
|
184
|
+
if (resolved === null)
|
|
185
|
+
return null;
|
|
186
|
+
switch (resolved.kind) {
|
|
187
|
+
case "linear":
|
|
188
|
+
case "radial":
|
|
189
|
+
break;
|
|
190
|
+
default:
|
|
191
|
+
return resolved;
|
|
192
|
+
}
|
|
193
|
+
const gradientTransform = resolved.attributes.gradientTransform === undefined
|
|
194
|
+
? null
|
|
195
|
+
: parseTransform(resolved.attributes.gradientTransform);
|
|
196
|
+
const userSpace = resolved.attributes.gradientUnits === "userSpaceOnUse";
|
|
197
|
+
// An objectBoundingBox gradient on a bounding box with no width or height
|
|
198
|
+
// paints nothing, and the reference counts as invalid. The box is measured
|
|
199
|
+
// in the element's user space: a layer can be flat where the element's box
|
|
200
|
+
// is not, as a diagonal line's box has width and height.
|
|
201
|
+
if (!userSpace && boxIsFlat(localFromElement, localBounds)) {
|
|
202
|
+
return { kind: "paints-nothing" };
|
|
203
|
+
}
|
|
204
|
+
const spread = resolved.attributes.spreadMethod ?? "pad";
|
|
205
|
+
if (spread === "reflect" || spread === "repeat") {
|
|
206
|
+
ctx.report(resolved.element.localName, `spreadMethod "${spread}" is drawn as pad`, { what: "Repeating or mirrored gradients", kept: "The end colors extend instead" });
|
|
207
|
+
}
|
|
208
|
+
// SVG paints a gradient with one stop as that stop's color; the model
|
|
209
|
+
// needs at least two stops.
|
|
210
|
+
if (resolved.stops.length === 1)
|
|
211
|
+
return lastStopSolid(resolved);
|
|
212
|
+
if (resolved.kind === "linear") {
|
|
213
|
+
return linearPaint(resolved, gradientTransform, userSpace, localFromElement, localBounds, ctx);
|
|
214
|
+
}
|
|
215
|
+
return radialPaint(resolved, gradientTransform, userSpace, localFromElement, localBounds, ctx);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Whether the element's bounding box — the layer's box measured in the
|
|
219
|
+
* element's user space — has no width or height, so an objectBoundingBox
|
|
220
|
+
* gradient paints nothing on it.
|
|
221
|
+
*/
|
|
222
|
+
function boxIsFlat(localFromElement, localBounds) {
|
|
223
|
+
if (localBounds.width >= 1e-9 && localBounds.height >= 1e-9)
|
|
224
|
+
return false;
|
|
225
|
+
const inverse = invertMatrix(localFromElement);
|
|
226
|
+
if (inverse === null)
|
|
227
|
+
return true;
|
|
228
|
+
const corners = [
|
|
229
|
+
applyMatrixToPoint(inverse, localBounds.x, localBounds.y),
|
|
230
|
+
applyMatrixToPoint(inverse, localBounds.x + localBounds.width, localBounds.y),
|
|
231
|
+
applyMatrixToPoint(inverse, localBounds.x, localBounds.y + localBounds.height),
|
|
232
|
+
applyMatrixToPoint(inverse, localBounds.x + localBounds.width, localBounds.y + localBounds.height),
|
|
233
|
+
];
|
|
234
|
+
const xs = corners.map((corner) => corner.x);
|
|
235
|
+
const ys = corners.map((corner) => corner.y);
|
|
236
|
+
return Math.max(...xs) - Math.min(...xs) < 1e-9 || Math.max(...ys) - Math.min(...ys) < 1e-9;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The map from gradient-space points to the layer's local space. For
|
|
240
|
+
* `objectBoundingBox` the gradient space is the bounds' unit square; for
|
|
241
|
+
* `userSpaceOnUse` it is the element's user space. `gradientTransform`
|
|
242
|
+
* composes inside the gradient space in both cases.
|
|
243
|
+
*/
|
|
244
|
+
function gradientToLocal(gradientTransform, userSpace, localFromElement, localBounds) {
|
|
245
|
+
const bboxMap = [
|
|
246
|
+
localBounds.width, 0, 0, localBounds.height, localBounds.x, localBounds.y,
|
|
247
|
+
];
|
|
248
|
+
const base = userSpace ? localFromElement : bboxMap;
|
|
249
|
+
return gradientTransform === null ? base : multiplyTransforms(base, gradientTransform);
|
|
250
|
+
}
|
|
251
|
+
function linearPaint(gradient, gradientTransform, userSpace, localFromElement, localBounds, ctx) {
|
|
252
|
+
const viewport = ctx.viewport;
|
|
253
|
+
const unitIsFraction = !userSpace;
|
|
254
|
+
const fonts = gradientFonts(gradient, ctx);
|
|
255
|
+
const x1 = gradientNumber(gradient.attributes.x1 ?? "0%", 0, unitIsFraction, viewport, "x", fonts);
|
|
256
|
+
const y1 = gradientNumber(gradient.attributes.y1 ?? "0%", 0, unitIsFraction, viewport, "y", fonts);
|
|
257
|
+
const x2 = gradientNumber(gradient.attributes.x2 ?? "100%", 1, unitIsFraction, viewport, "x", fonts);
|
|
258
|
+
const y2 = gradientNumber(gradient.attributes.y2 ?? "0%", 0, unitIsFraction, viewport, "y", fonts);
|
|
259
|
+
const map = gradientToLocal(gradientTransform, userSpace, localFromElement, localBounds);
|
|
260
|
+
const p1 = applyMatrixToPoint(map, x1, y1);
|
|
261
|
+
const p2 = applyMatrixToPoint(map, x2, y2);
|
|
262
|
+
const dx = p2.x - p1.x;
|
|
263
|
+
const dy = p2.y - p1.y;
|
|
264
|
+
const length = Math.hypot(dx, dy);
|
|
265
|
+
if (length < 1e-9 || !Number.isFinite(length))
|
|
266
|
+
return lastStopSolid(gradient);
|
|
267
|
+
// The color changes along the gradient's normal, which the map carries by
|
|
268
|
+
// its inverse transpose (exact for a scale or a skew, where the end points
|
|
269
|
+
// alone would tilt the lines of equal color). Normalize before applying
|
|
270
|
+
// the map to avoid squaring a very long axis.
|
|
271
|
+
const nx = x2 - x1;
|
|
272
|
+
const ny = y2 - y1;
|
|
273
|
+
const sourceLength = Math.hypot(nx, ny);
|
|
274
|
+
const ux = nx / sourceLength, uy = ny / sourceLength;
|
|
275
|
+
const [a, b, c, d] = map;
|
|
276
|
+
const determinant = a * d - b * c;
|
|
277
|
+
const normal = matrixIsDegenerate(map)
|
|
278
|
+
? { x: dx / length / length, y: dy / length / length }
|
|
279
|
+
: {
|
|
280
|
+
x: (d * ux - b * uy) / determinant / sourceLength,
|
|
281
|
+
y: (a * uy - c * ux) / determinant / sourceLength,
|
|
282
|
+
};
|
|
283
|
+
const perUnit = Math.hypot(normal.x, normal.y);
|
|
284
|
+
if (!perUnit || !Number.isFinite(perUnit))
|
|
285
|
+
return lastStopSolid(gradient);
|
|
286
|
+
const lineLength = Math.abs(localBounds.width * (normal.x / perUnit))
|
|
287
|
+
+ Math.abs(localBounds.height * (normal.y / perUnit));
|
|
288
|
+
const centerX = localBounds.x + localBounds.width / 2;
|
|
289
|
+
const centerY = localBounds.y + localBounds.height / 2;
|
|
290
|
+
if (lineLength < 1e-9) {
|
|
291
|
+
// A flat shape perpendicular to the gradient sits on one line of equal
|
|
292
|
+
// color: sample the gradient at the shape's position, padded past the
|
|
293
|
+
// ends, as a browser paints it (a zero-length gradient, above, is the
|
|
294
|
+
// only case that takes the last stop).
|
|
295
|
+
const t = (centerX - p1.x) * normal.x + (centerY - p1.y) * normal.y;
|
|
296
|
+
const sampled = sampleStops(gradient.stops, t);
|
|
297
|
+
return sampled === null ? null : { kind: "solid", color: sampled.slice(0, 7), opacity: alphaOf(sampled) };
|
|
298
|
+
}
|
|
299
|
+
// Restrict the offset function t = normal · (point - p1) to the box.
|
|
300
|
+
// A flat box loses one component of its slope but keeps its offset at the origin.
|
|
301
|
+
const gx = localBounds.width ? normal.x : 0;
|
|
302
|
+
const gy = localBounds.height ? normal.y : 0;
|
|
303
|
+
const slope = Math.hypot(gx, gy);
|
|
304
|
+
const spanX = localBounds.width ? (gx / slope) / localBounds.width / slope : 0;
|
|
305
|
+
const spanY = localBounds.height ? (gy / slope) / localBounds.height / slope : 0;
|
|
306
|
+
const offset = (localBounds.width ? 0 : (localBounds.x - p1.x) * normal.x)
|
|
307
|
+
+ (localBounds.height ? 0 : (localBounds.y - p1.y) * normal.y);
|
|
308
|
+
const from = {
|
|
309
|
+
x: localBounds.width ? (p1.x - localBounds.x) / localBounds.width - offset * spanX : 0,
|
|
310
|
+
y: localBounds.height ? (p1.y - localBounds.y) / localBounds.height - offset * spanY : 0,
|
|
311
|
+
};
|
|
312
|
+
const to = { x: from.x + spanX, y: from.y + spanY };
|
|
313
|
+
if (![from.x, from.y, to.x, to.y].every(Number.isFinite))
|
|
314
|
+
return lastStopSolid(gradient);
|
|
315
|
+
const paint = { kind: "linear-gradient", from, to, stops: gradient.stops };
|
|
316
|
+
return { kind: "gradient", paint };
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* The color a gradient paints at offset `t`: padded to the first or last
|
|
320
|
+
* stop outside the stops' span, interpolated between the neighbouring stops
|
|
321
|
+
* inside it. Offsets must be in non-decreasing order, as `readStops` leaves
|
|
322
|
+
* them. Null only for a gradient with no stops.
|
|
323
|
+
*/
|
|
324
|
+
function sampleStops(stops, t) {
|
|
325
|
+
const first = stops[0];
|
|
326
|
+
const last = stops[stops.length - 1];
|
|
327
|
+
if (first === undefined || last === undefined)
|
|
328
|
+
return null;
|
|
329
|
+
if (t <= first.offset)
|
|
330
|
+
return first.color;
|
|
331
|
+
if (t >= last.offset)
|
|
332
|
+
return last.color;
|
|
333
|
+
for (let i = 0; i + 1 < stops.length; i++) {
|
|
334
|
+
const a = stops[i];
|
|
335
|
+
const b = stops[i + 1];
|
|
336
|
+
if (t >= a.offset && t <= b.offset && b.offset > a.offset) {
|
|
337
|
+
return mixColors(a.color, b.color, (t - a.offset) / (b.offset - a.offset));
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return last.color;
|
|
341
|
+
}
|
|
342
|
+
/** Mixes two `#RRGGBB[AA]` colors channel by channel, `t` of the way from `a` to `b`. */
|
|
343
|
+
function mixColors(a, b, t) {
|
|
344
|
+
const channels = (color) => [1, 3, 5, 7].map((at) => at === 7 && color.length !== 9 ? 255 : Number.parseInt(color.slice(at, at + 2), 16));
|
|
345
|
+
const from = channels(a);
|
|
346
|
+
const to = channels(b);
|
|
347
|
+
const mix = (i) => from[i] + (to[i] - from[i]) * t;
|
|
348
|
+
return hexColor({ r: mix(0), g: mix(1), b: mix(2), a: mix(3) / 255 });
|
|
349
|
+
}
|
|
350
|
+
/** The alpha of a `#RRGGBBAA` stop color as a separate opacity, undefined for opaque. */
|
|
351
|
+
function alphaOf(color) {
|
|
352
|
+
if (color.length !== 9)
|
|
353
|
+
return undefined;
|
|
354
|
+
const alpha = Number.parseInt(color.slice(7), 16) / 255;
|
|
355
|
+
return alpha >= 1 ? undefined : alpha;
|
|
356
|
+
}
|
|
357
|
+
/** The solid paint a gradient draws when it paints one color: its last stop's. */
|
|
358
|
+
function lastStopSolid(gradient) {
|
|
359
|
+
const lastStop = gradient.stops[gradient.stops.length - 1];
|
|
360
|
+
return lastStop === undefined
|
|
361
|
+
? null
|
|
362
|
+
: { kind: "solid", color: lastStop.color.slice(0, 7), opacity: alphaOf(lastStop.color) };
|
|
363
|
+
}
|
|
364
|
+
function radialPaint(gradient, gradientTransform, userSpace, localFromElement, localBounds, ctx) {
|
|
365
|
+
const viewport = ctx.viewport;
|
|
366
|
+
const unitIsFraction = !userSpace;
|
|
367
|
+
const fonts = gradientFonts(gradient, ctx);
|
|
368
|
+
const cx = gradientNumber(gradient.attributes.cx ?? "50%", 0.5, unitIsFraction, viewport, "x", fonts);
|
|
369
|
+
const cy = gradientNumber(gradient.attributes.cy ?? "50%", 0.5, unitIsFraction, viewport, "y", fonts);
|
|
370
|
+
let r = gradientNumber(gradient.attributes.r ?? "50%", 0.5, unitIsFraction, viewport, "diagonal", fonts);
|
|
371
|
+
if (r < 0) {
|
|
372
|
+
// SVG makes a negative radius an error; browsers use the initial 50%.
|
|
373
|
+
ctx.report(gradient.element.localName, `the radial gradient radius "${gradient.attributes.r?.trim() ?? ""}" is negative, so 50% is used`, { what: "Negative gradient radii", kept: "A radius of 50% is used" });
|
|
374
|
+
r = gradientNumber("50%", 0.5, unitIsFraction, viewport, "diagonal", fonts);
|
|
375
|
+
}
|
|
376
|
+
const fx = gradient.attributes.fx === undefined
|
|
377
|
+
? cx
|
|
378
|
+
: gradientNumber(gradient.attributes.fx, cx, unitIsFraction, viewport, "x", fonts);
|
|
379
|
+
const fy = gradient.attributes.fy === undefined
|
|
380
|
+
? cy
|
|
381
|
+
: gradientNumber(gradient.attributes.fy, cy, unitIsFraction, viewport, "y", fonts);
|
|
382
|
+
const fr = gradientNumber(gradient.attributes.fr ?? "0%", 0, unitIsFraction, viewport, "diagonal", fonts);
|
|
383
|
+
const map = gradientToLocal(gradientTransform, userSpace, localFromElement, localBounds);
|
|
384
|
+
const center = applyMatrixToPoint(map, cx, cy);
|
|
385
|
+
const focal = applyMatrixToPoint(map, fx, fy);
|
|
386
|
+
if (Math.hypot(focal.x - center.x, focal.y - center.y) > 1e-6 || fr > 1e-9) {
|
|
387
|
+
ctx.report(gradient.element.localName, "the radial gradient's focal point is dropped; it draws centered", { what: "Radial gradient focal points", kept: "The gradient is drawn centered" });
|
|
388
|
+
}
|
|
389
|
+
// A bbox gradient's radius r spans r of the width and r of the height; a
|
|
390
|
+
// user-space gradient's radius spans r units on both axes. The mapped
|
|
391
|
+
// radial is exact while the radius vectors stay a circle: orthogonal and
|
|
392
|
+
// equal length (rotation is harmless). Anything else draws axis-aligned
|
|
393
|
+
// and is reported.
|
|
394
|
+
const radiusX = applyMatrixToVector(map, userSpace ? r : r, 0);
|
|
395
|
+
const radiusY = applyMatrixToVector(map, 0, userSpace ? r : r);
|
|
396
|
+
const lengthX = Math.hypot(radiusX.dx, radiusX.dy);
|
|
397
|
+
const lengthY = Math.hypot(radiusY.dx, radiusY.dy);
|
|
398
|
+
const dot = radiusX.dx * radiusY.dx + radiusX.dy * radiusY.dy;
|
|
399
|
+
const approximated = Math.abs(dot) > 1e-9 * lengthX * lengthY
|
|
400
|
+
|| Math.abs(lengthX - lengthY) > 1e-9 * Math.max(lengthX, lengthY);
|
|
401
|
+
if (approximated) {
|
|
402
|
+
ctx.report(gradient.element.localName, "the radial gradient is skewed or non-uniformly scaled; it draws as an axis-aligned ellipse", { what: "Skewed radial gradients", kept: "Drawn as an upright ellipse" });
|
|
403
|
+
}
|
|
404
|
+
// SVG paints a zero radius as the last stop's color; the model needs a
|
|
405
|
+
// positive radius.
|
|
406
|
+
if (lengthX < 1e-9 || lengthY < 1e-9)
|
|
407
|
+
return lastStopSolid(gradient);
|
|
408
|
+
// A browser paints a user-space radial gradient on a flat layer (a line's
|
|
409
|
+
// stroke, say), but the model's center and radius are fractions of the
|
|
410
|
+
// layer's box, so a box without width or height cannot place it. Dropping
|
|
411
|
+
// the paint is a real loss, so it is reported as itself, not mistaken for
|
|
412
|
+
// an empty gradient.
|
|
413
|
+
if (localBounds.width < 1e-9 || localBounds.height < 1e-9) {
|
|
414
|
+
return { kind: "unsupported", reason: "a radial gradient cannot be placed on a zero-width or zero-height box" };
|
|
415
|
+
}
|
|
416
|
+
const paint = {
|
|
417
|
+
kind: "radial-gradient",
|
|
418
|
+
center: {
|
|
419
|
+
x: (center.x - localBounds.x) / localBounds.width,
|
|
420
|
+
y: (center.y - localBounds.y) / localBounds.height,
|
|
421
|
+
},
|
|
422
|
+
radius: {
|
|
423
|
+
x: lengthX / localBounds.width,
|
|
424
|
+
y: lengthY / localBounds.height,
|
|
425
|
+
},
|
|
426
|
+
stops: gradient.stops,
|
|
427
|
+
};
|
|
428
|
+
return { kind: "gradient", paint };
|
|
429
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type Rect, type Stroke, type VisualStyle } from "../../model/index.ts";
|
|
2
|
+
import type { SvgElementLike } from "../public-types.ts";
|
|
3
|
+
import type { Matrix } from "../transform/matrix.ts";
|
|
4
|
+
import type { ComputedStyle } from "../style/computed.ts";
|
|
5
|
+
import type { StyleRules, StyleViewport } from "../style/computed.ts";
|
|
6
|
+
import type { ReportNote } from "../report/report.ts";
|
|
7
|
+
/** Whether a stroke came from a non-scaling-stroke element. */
|
|
8
|
+
export declare function isNonScalingStroke(stroke: Stroke): boolean;
|
|
9
|
+
export interface PaintContext {
|
|
10
|
+
readonly ids: ReadonlyMap<string, SvgElementLike>;
|
|
11
|
+
/** The import-wide work caps that following references, reading path data and style work count against. */
|
|
12
|
+
readonly limits: {
|
|
13
|
+
readonly countReferenceStep: () => void;
|
|
14
|
+
readonly countPathNumbers: (count: number) => void;
|
|
15
|
+
readonly countStyleWork: (units: number) => void;
|
|
16
|
+
};
|
|
17
|
+
readonly rules: StyleRules;
|
|
18
|
+
/** Every element's computed style at its own place in the tree, from the pre-pass. */
|
|
19
|
+
readonly styles: ReadonlyMap<SvgElementLike, ComputedStyle>;
|
|
20
|
+
readonly viewport: StyleViewport | null;
|
|
21
|
+
readonly report: (elementPath: string, element: string, kind: "left-out" | "approximated", reason: string, note: ReportNote) => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Builds the model style of one layer from its computed style: solid fills
|
|
25
|
+
* and strokes, gradients, opacity, dashes, caps, joins and the miter limit.
|
|
26
|
+
* `strokeScale` is the factor the baked transform scales stroke geometry
|
|
27
|
+
* by; when it is not exact, the caller passes `strokeScaledApproximately`
|
|
28
|
+
* and a stroked layer is reported. A non-scaling stroke ignores both: its
|
|
29
|
+
* width and dashes stay in the outer viewport's units, which are the
|
|
30
|
+
* layer's baked units, so it maps exactly.
|
|
31
|
+
*/
|
|
32
|
+
export declare function visualStyleFromComputed(computed: ComputedStyle, localFromElement: Matrix, localBounds: Rect, strokeScale: number, strokeScaledApproximately: boolean, elementPath: string, element: SvgElementLike, ctx: PaintContext): VisualStyle;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { miterAngleFromLimit, } from "../../model/index.js";
|
|
2
|
+
import { hexColorAndOpacity } from "../style/color.js";
|
|
3
|
+
import { resolvePaintServer } from "./gradients.js";
|
|
4
|
+
/** What the paint module needs of the import beyond the element itself. */
|
|
5
|
+
/**
|
|
6
|
+
* The strokes built from a `vector-effect: non-scaling-stroke` element. The
|
|
7
|
+
* model has no such flag, so an instance's scale overrides look them up
|
|
8
|
+
* here and leave their width and dashes as they are.
|
|
9
|
+
*/
|
|
10
|
+
const nonScalingStrokes = new WeakSet();
|
|
11
|
+
/** Whether a stroke came from a non-scaling-stroke element. */
|
|
12
|
+
export function isNonScalingStroke(stroke) {
|
|
13
|
+
return nonScalingStrokes.has(stroke);
|
|
14
|
+
}
|
|
15
|
+
/** Maps SVG's `stroke-linecap` to the model's dash-cap names. */
|
|
16
|
+
function dashCapOf(linecap) {
|
|
17
|
+
return linecap === "butt" ? "none" : linecap;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Turns a computed `fill` or `stroke` value into model paints.
|
|
21
|
+
* `localFromElement` and `localBounds` place gradients in the layer's own
|
|
22
|
+
* geometry space, and `opacity` folds the matching `fill-opacity` or
|
|
23
|
+
* `stroke-opacity` in. A paint server that cannot map exactly is reported
|
|
24
|
+
* and the paint is dropped; nothing is guessed.
|
|
25
|
+
*/
|
|
26
|
+
function paintsFromValue(value, opacity, localFromElement, localBounds, elementPath, element, property, ctx) {
|
|
27
|
+
switch (value.kind) {
|
|
28
|
+
case "none":
|
|
29
|
+
return [];
|
|
30
|
+
case "color": {
|
|
31
|
+
const { color, opacity: alpha } = hexColorAndOpacity(value.color);
|
|
32
|
+
const combined = (alpha ?? 1) * opacity;
|
|
33
|
+
return [{
|
|
34
|
+
kind: "solid",
|
|
35
|
+
color,
|
|
36
|
+
...(combined >= 1 ? {} : { opacity: combined }),
|
|
37
|
+
}];
|
|
38
|
+
}
|
|
39
|
+
case "ref": {
|
|
40
|
+
// Opening never fetches, so a paint server in another file is an
|
|
41
|
+
// invalid reference, like a missing one.
|
|
42
|
+
const server = value.external ? { kind: "external" } : resolvePaintServer(value.id, localFromElement, localBounds, {
|
|
43
|
+
ids: ctx.ids,
|
|
44
|
+
limits: ctx.limits,
|
|
45
|
+
styles: ctx.styles,
|
|
46
|
+
viewport: ctx.viewport,
|
|
47
|
+
rootFontSize: ctx.rules.rootFontSize,
|
|
48
|
+
report: (_localName, reason, note) => {
|
|
49
|
+
ctx.report(elementPath, element.localName, "approximated", reason, note);
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
if (server === null) {
|
|
53
|
+
ctx.report(elementPath, element.localName, "left-out", `the ${property} gradient has no stops`, { what: "Gradients with no stops", kept: "Not painted" });
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
switch (server.kind) {
|
|
57
|
+
case "solid": {
|
|
58
|
+
const combined = (server.opacity ?? 1) * opacity;
|
|
59
|
+
return [{
|
|
60
|
+
kind: "solid",
|
|
61
|
+
color: server.color,
|
|
62
|
+
...(combined >= 1 ? {} : { opacity: combined }),
|
|
63
|
+
}];
|
|
64
|
+
}
|
|
65
|
+
case "gradient": {
|
|
66
|
+
const paint = server.paint;
|
|
67
|
+
return [opacity >= 1 ? paint : { ...paint, opacity }];
|
|
68
|
+
}
|
|
69
|
+
case "pattern":
|
|
70
|
+
ctx.report(elementPath, element.localName, "left-out", `pattern ${property} is not supported`, { what: "Pattern fills and strokes", kept: "Not painted" });
|
|
71
|
+
return [];
|
|
72
|
+
case "unsupported":
|
|
73
|
+
ctx.report(elementPath, element.localName, "left-out", server.reason, { what: "Unsupported fills and strokes", kept: "Not painted" });
|
|
74
|
+
return [];
|
|
75
|
+
case "external":
|
|
76
|
+
case "missing":
|
|
77
|
+
case "not-paint-server":
|
|
78
|
+
case "paints-nothing": {
|
|
79
|
+
// Each makes the reference invalid, so SVG paints the fallback color when one is given.
|
|
80
|
+
const problem = server.kind === "external"
|
|
81
|
+
? `the ${property} reference ${value.id} is in another file, which is never fetched`
|
|
82
|
+
: server.kind === "missing"
|
|
83
|
+
? `the ${property} reference #${value.id} is missing`
|
|
84
|
+
: server.kind === "paints-nothing"
|
|
85
|
+
? `an objectBoundingBox ${property} gradient paints nothing on a zero-width or zero-height box`
|
|
86
|
+
: `#${value.id} is a ${server.localName}, not a gradient or pattern`;
|
|
87
|
+
if (value.fallback !== null) {
|
|
88
|
+
const { color, opacity: alpha } = hexColorAndOpacity(value.fallback);
|
|
89
|
+
const combined = (alpha ?? 1) * opacity;
|
|
90
|
+
ctx.report(elementPath, element.localName, "approximated", `${problem}; its fallback color is used`, { what: "Fills and strokes that refer to nothing usable", kept: "Painted in their fallback color" });
|
|
91
|
+
return [{ kind: "solid", color, ...(combined >= 1 ? {} : { opacity: combined }) }];
|
|
92
|
+
}
|
|
93
|
+
ctx.report(elementPath, element.localName, "left-out", problem, { what: "Fills and strokes that refer to nothing usable", kept: "Not painted" });
|
|
94
|
+
return [];
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Whether the element has a `pathLength` a browser applies: a number that is
|
|
102
|
+
* not negative. It scales the dash lengths by the path's real length over
|
|
103
|
+
* it (SVG 2, the pathLength attribute); an unreadable or negative value is
|
|
104
|
+
* ignored.
|
|
105
|
+
*/
|
|
106
|
+
function hasReadablePathLength(element) {
|
|
107
|
+
const value = element.getAttribute("pathLength")?.trim() ?? "";
|
|
108
|
+
return /^[+]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/.test(value);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Builds the model style of one layer from its computed style: solid fills
|
|
112
|
+
* and strokes, gradients, opacity, dashes, caps, joins and the miter limit.
|
|
113
|
+
* `strokeScale` is the factor the baked transform scales stroke geometry
|
|
114
|
+
* by; when it is not exact, the caller passes `strokeScaledApproximately`
|
|
115
|
+
* and a stroked layer is reported. A non-scaling stroke ignores both: its
|
|
116
|
+
* width and dashes stay in the outer viewport's units, which are the
|
|
117
|
+
* layer's baked units, so it maps exactly.
|
|
118
|
+
*/
|
|
119
|
+
export function visualStyleFromComputed(computed, localFromElement, localBounds, strokeScale, strokeScaledApproximately, elementPath, element, ctx) {
|
|
120
|
+
const fills = paintsFromValue(computed.fill, computed.fillOpacity, localFromElement, localBounds, elementPath, element, "fill", ctx);
|
|
121
|
+
const strokePaints = paintsFromValue(computed.stroke, computed.strokeOpacity, localFromElement, localBounds, elementPath, element, "stroke", ctx);
|
|
122
|
+
let strokes = [];
|
|
123
|
+
if (computed.nonScalingStroke) {
|
|
124
|
+
strokeScale = 1;
|
|
125
|
+
strokeScaledApproximately = false;
|
|
126
|
+
}
|
|
127
|
+
if (strokePaints.length > 0 && computed.strokeWidth > 0) {
|
|
128
|
+
if (strokeScaledApproximately) {
|
|
129
|
+
ctx.report(elementPath, element.localName, "approximated", "the stroke width scales with the geometric mean of the transform's axis scales", { what: "Strokes under an uneven scale", kept: "One even stroke width is used" });
|
|
130
|
+
}
|
|
131
|
+
const width = computed.strokeWidth * strokeScale;
|
|
132
|
+
let dash = computed.strokeDasharray;
|
|
133
|
+
// A pattern whose dashes sum to zero draws a solid stroke (SVG 2 §13.5.6).
|
|
134
|
+
if (dash !== null && dash.every((entry) => entry === 0))
|
|
135
|
+
dash = null;
|
|
136
|
+
if (dash !== null && dash.length % 2 === 1)
|
|
137
|
+
dash = [...dash, ...dash];
|
|
138
|
+
if (computed.strokeDashoffset !== 0) {
|
|
139
|
+
ctx.report(elementPath, element.localName, "approximated", "stroke-dashoffset is dropped; dashes start at the path start", { what: "Dash offsets", kept: "Dashes start at the start of the path" });
|
|
140
|
+
}
|
|
141
|
+
if (dash !== null && hasReadablePathLength(element)) {
|
|
142
|
+
ctx.report(elementPath, element.localName, "approximated", "pathLength is not applied; the dash lengths stay in user units instead of being scaled to the path's length", { what: "Path lengths (pathLength)", kept: "Dash lengths are kept unscaled" });
|
|
143
|
+
}
|
|
144
|
+
strokes = strokePaints.map((paint) => ({
|
|
145
|
+
paint,
|
|
146
|
+
width,
|
|
147
|
+
...(dash === null ? {} : { dash: dash.map((entry) => entry * strokeScale) }),
|
|
148
|
+
join: computed.strokeLinejoin,
|
|
149
|
+
...(computed.strokeLinejoin === "miter"
|
|
150
|
+
? { miterAngle: miterAngleFromLimit(computed.strokeMiterlimit) }
|
|
151
|
+
: {}),
|
|
152
|
+
dashCap: dashCapOf(computed.strokeLinecap),
|
|
153
|
+
}));
|
|
154
|
+
if (computed.nonScalingStroke)
|
|
155
|
+
for (const stroke of strokes)
|
|
156
|
+
nonScalingStrokes.add(stroke);
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
...(fills.length > 0 ? { fills } : {}),
|
|
160
|
+
...(strokes.length > 0 ? { strokes } : {}),
|
|
161
|
+
};
|
|
162
|
+
}
|