@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,846 @@
|
|
|
1
|
+
import { clearCache, layoutNextLineRange, materializeLineRange, measureNaturalWidth, prepareWithSegments, } from "@chenglou/pretext";
|
|
2
|
+
import { layoutNextRichInlineLineRange, materializeRichInlineLineRange, prepareRichInline, } from "@chenglou/pretext/rich-inline";
|
|
3
|
+
import { resizeRotatedRect } from "./geometry.js";
|
|
4
|
+
import { entryOf, putEntry } from "./id-table.js";
|
|
5
|
+
let lineMetricContext = null;
|
|
6
|
+
const styleMetricCache = new Map();
|
|
7
|
+
export const textMeasurementStats = {
|
|
8
|
+
layoutComputations: 0,
|
|
9
|
+
fontRequestTextRebuilds: 0,
|
|
10
|
+
resolvedStyleCopies: 0,
|
|
11
|
+
};
|
|
12
|
+
export function resetTextMeasurementStats() {
|
|
13
|
+
textMeasurementStats.layoutComputations = 0;
|
|
14
|
+
textMeasurementStats.fontRequestTextRebuilds = 0;
|
|
15
|
+
textMeasurementStats.resolvedStyleCopies = 0;
|
|
16
|
+
}
|
|
17
|
+
let layoutCache = new WeakMap();
|
|
18
|
+
let resolvedStyleCache = new WeakMap();
|
|
19
|
+
export function resolveTextStyle(document, styleId, overrides) {
|
|
20
|
+
const selected = entryOf(document.theme.textStyles, styleId ?? document.theme.defaultTextStyleId)
|
|
21
|
+
?? entryOf(document.theme.textStyles, document.theme.defaultTextStyleId);
|
|
22
|
+
if (selected === undefined) {
|
|
23
|
+
throw new Error("The document does not define its default text style.");
|
|
24
|
+
}
|
|
25
|
+
if (overrides === undefined)
|
|
26
|
+
return selected;
|
|
27
|
+
let byOverrides = resolvedStyleCache.get(selected);
|
|
28
|
+
if (byOverrides === undefined) {
|
|
29
|
+
byOverrides = new WeakMap();
|
|
30
|
+
resolvedStyleCache.set(selected, byOverrides);
|
|
31
|
+
}
|
|
32
|
+
const cached = byOverrides.get(overrides);
|
|
33
|
+
if (cached !== undefined)
|
|
34
|
+
return cached;
|
|
35
|
+
const { fontAssetId: selectedFontAssetId, fontFallbackAssetIds: selectedFontFallbackAssetIds, ...selectedProperties } = selected;
|
|
36
|
+
const { fontAssetId: overriddenFontAssetId, fontFallbackAssetIds: overriddenFontFallbackAssetIds, ...overriddenProperties } = overrides;
|
|
37
|
+
const fontAssetId = Object.hasOwn(overrides, "fontAssetId")
|
|
38
|
+
? overriddenFontAssetId ?? undefined
|
|
39
|
+
: selectedFontAssetId;
|
|
40
|
+
const fontFallbackAssetIds = Object.hasOwn(overrides, "fontFallbackAssetIds")
|
|
41
|
+
? overriddenFontFallbackAssetIds
|
|
42
|
+
: selectedFontFallbackAssetIds;
|
|
43
|
+
const resolved = {
|
|
44
|
+
...selectedProperties,
|
|
45
|
+
...overriddenProperties,
|
|
46
|
+
...(fontAssetId === undefined ? {} : { fontAssetId }),
|
|
47
|
+
...(fontFallbackAssetIds === undefined || fontFallbackAssetIds.length === 0
|
|
48
|
+
? {}
|
|
49
|
+
: { fontFallbackAssetIds }),
|
|
50
|
+
};
|
|
51
|
+
byOverrides.set(overrides, resolved);
|
|
52
|
+
return resolved;
|
|
53
|
+
}
|
|
54
|
+
export function splitCssFontFamilies(fontFamily) {
|
|
55
|
+
const families = [];
|
|
56
|
+
let start = 0;
|
|
57
|
+
let quote = null;
|
|
58
|
+
let escaped = false;
|
|
59
|
+
for (let index = 0; index < fontFamily.length; index += 1) {
|
|
60
|
+
const character = fontFamily[index];
|
|
61
|
+
if (escaped) {
|
|
62
|
+
escaped = false;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (character === "\\") {
|
|
66
|
+
escaped = true;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (quote === null && (character === "'" || character === '"')) {
|
|
70
|
+
quote = character;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (character === quote) {
|
|
74
|
+
quote = null;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (quote === null && character === ",") {
|
|
78
|
+
const family = fontFamily.slice(start, index).trim();
|
|
79
|
+
if (family.length > 0)
|
|
80
|
+
families.push(family);
|
|
81
|
+
start = index + 1;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const finalFamily = fontFamily.slice(start).trim();
|
|
85
|
+
if (finalFamily.length > 0)
|
|
86
|
+
families.push(finalFamily);
|
|
87
|
+
return families;
|
|
88
|
+
}
|
|
89
|
+
/** Returns the stable CSS family used for one document font asset identity. */
|
|
90
|
+
export function documentFontFamilyAlias(assetId) {
|
|
91
|
+
let encoded = "";
|
|
92
|
+
for (let index = 0; index < assetId.length; index += 1) {
|
|
93
|
+
encoded += assetId.charCodeAt(index).toString(16).padStart(4, "0");
|
|
94
|
+
}
|
|
95
|
+
return `TacoDocumentFont_${encoded}`;
|
|
96
|
+
}
|
|
97
|
+
function fontAssetFamily(document, assetId) {
|
|
98
|
+
const asset = entryOf(document.assets, assetId);
|
|
99
|
+
if (asset?.kind !== "font") {
|
|
100
|
+
throw new Error(`Text style references missing font asset ${assetId}.`);
|
|
101
|
+
}
|
|
102
|
+
return JSON.stringify(documentFontFamilyAlias(assetId));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Resolves embedded font identities into an asset-specific CSS family stack.
|
|
106
|
+
* The primary embedded face replaces the authored primary family, ordered
|
|
107
|
+
* embedded fallbacks follow it, and the authored CSS fallback tail remains
|
|
108
|
+
* last. Asset aliases keep same-family faces distinct and deterministic.
|
|
109
|
+
*/
|
|
110
|
+
export function resolveTextFontFamily(document, style) {
|
|
111
|
+
const fallbackAssetIds = style.fontFallbackAssetIds ?? [];
|
|
112
|
+
if (style.fontAssetId === undefined && fallbackAssetIds.length === 0) {
|
|
113
|
+
return style.fontFamily;
|
|
114
|
+
}
|
|
115
|
+
const authoredFamilies = splitCssFontFamilies(style.fontFamily);
|
|
116
|
+
const authoredPrimary = authoredFamilies[0] ?? style.fontFamily;
|
|
117
|
+
const primary = style.fontAssetId === undefined
|
|
118
|
+
? authoredPrimary
|
|
119
|
+
: fontAssetFamily(document, style.fontAssetId);
|
|
120
|
+
const fallbacks = fallbackAssetIds.map((assetId) => fontAssetFamily(document, assetId));
|
|
121
|
+
return [primary, ...fallbacks, ...authoredFamilies.slice(1)].join(", ");
|
|
122
|
+
}
|
|
123
|
+
export function textStyleToCanvasFont(style, document) {
|
|
124
|
+
const hasEmbeddedIdentity = style.fontAssetId !== undefined
|
|
125
|
+
|| (style.fontFallbackAssetIds?.length ?? 0) > 0;
|
|
126
|
+
const family = document !== undefined && hasEmbeddedIdentity
|
|
127
|
+
? resolveTextFontFamily(document, style)
|
|
128
|
+
: authoredCanvasFamily(style.fontFamily);
|
|
129
|
+
return `${style.italic ? "italic" : "normal"} ${style.fontWeight} ${style.fontSize}px ${family}`;
|
|
130
|
+
}
|
|
131
|
+
function authoredCanvasFamily(fontFamily) {
|
|
132
|
+
return fontFamily.includes(",")
|
|
133
|
+
? fontFamily
|
|
134
|
+
: `"${fontFamily.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The font the browser canvas estimate measures `style` with: as
|
|
138
|
+
* `textStyleToCanvasFont`, but a font asset's face (`documentFontFamilyAlias`)
|
|
139
|
+
* is named only once the page holds it loaded. An alias with no loaded face
|
|
140
|
+
* makes the canvas measure with the browser's default face, so the authored
|
|
141
|
+
* family stands in for it until then: the face it names is the one the
|
|
142
|
+
* text is drawn with. Font requests (`getTextNodeFontRequests`) keep the
|
|
143
|
+
* alias, which is the face they ask for.
|
|
144
|
+
*/
|
|
145
|
+
function textStyleToMeasureFont(style, document) {
|
|
146
|
+
const fallbackAssetIds = style.fontFallbackAssetIds ?? [];
|
|
147
|
+
if (document === undefined || (style.fontAssetId === undefined && fallbackAssetIds.length === 0)) {
|
|
148
|
+
return textStyleToCanvasFont(style, document);
|
|
149
|
+
}
|
|
150
|
+
const faces = pageFontFaces();
|
|
151
|
+
if (faces === undefined)
|
|
152
|
+
return textStyleToCanvasFont(style, document);
|
|
153
|
+
const authoredFamilies = splitCssFontFamilies(style.fontFamily);
|
|
154
|
+
const authoredPrimary = authoredFamilies.length > 1
|
|
155
|
+
? authoredFamilies[0]
|
|
156
|
+
: authoredCanvasFamily(authoredFamilies[0] ?? style.fontFamily);
|
|
157
|
+
const loaded = (assetId) => {
|
|
158
|
+
const alias = fontAssetFamily(document, assetId);
|
|
159
|
+
return aliasFaceLoaded(faces, documentFontFamilyAlias(assetId)) ? alias : undefined;
|
|
160
|
+
};
|
|
161
|
+
const primary = (style.fontAssetId === undefined ? undefined : loaded(style.fontAssetId)) ?? authoredPrimary;
|
|
162
|
+
const fallbacks = fallbackAssetIds.flatMap((assetId) => loaded(assetId) ?? []);
|
|
163
|
+
const family = [primary, ...fallbacks, ...authoredFamilies.slice(1)].join(", ");
|
|
164
|
+
return `${style.italic ? "italic" : "normal"} ${style.fontWeight} ${style.fontSize}px ${family}`;
|
|
165
|
+
}
|
|
166
|
+
function pageFontFaces() {
|
|
167
|
+
const runtime = globalThis;
|
|
168
|
+
return runtime.document?.fonts ?? runtime.fonts;
|
|
169
|
+
}
|
|
170
|
+
/** Which aliases have a loaded face, for the face set and size it was read at; cleared when faces finish loading. */
|
|
171
|
+
let loadedAliases = null;
|
|
172
|
+
const listenedFaceSets = new WeakSet();
|
|
173
|
+
function aliasFaceLoaded(faces, alias) {
|
|
174
|
+
if (loadedAliases === null || loadedAliases.faces !== faces || loadedAliases.size !== faces.size) {
|
|
175
|
+
loadedAliases = { faces, size: faces.size, known: new Map() };
|
|
176
|
+
if (!listenedFaceSets.has(faces)) {
|
|
177
|
+
listenedFaceSets.add(faces);
|
|
178
|
+
const forget = () => { loadedAliases = null; };
|
|
179
|
+
faces.addEventListener?.("loadingdone", forget);
|
|
180
|
+
faces.addEventListener?.("loadingerror", forget);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const known = loadedAliases.known.get(alias);
|
|
184
|
+
if (known !== undefined)
|
|
185
|
+
return known;
|
|
186
|
+
let found = false;
|
|
187
|
+
for (const face of faces) {
|
|
188
|
+
if (face.status === "loaded" && unquotedFamily(face.family) === alias) {
|
|
189
|
+
found = true;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
loadedAliases.known.set(alias, found);
|
|
194
|
+
return found;
|
|
195
|
+
}
|
|
196
|
+
function unquotedFamily(family) {
|
|
197
|
+
const trimmed = family.trim();
|
|
198
|
+
return trimmed.length >= 2 && (trimmed[0] === '"' || trimmed[0] === "'") && trimmed.at(-1) === trimmed[0]
|
|
199
|
+
? trimmed.slice(1, -1)
|
|
200
|
+
: trimmed;
|
|
201
|
+
}
|
|
202
|
+
function styleLineHeight(style) {
|
|
203
|
+
return Math.max(0, style.fontSize * style.lineHeight);
|
|
204
|
+
}
|
|
205
|
+
function getLineMetricContext() {
|
|
206
|
+
if (lineMetricContext !== null)
|
|
207
|
+
return lineMetricContext;
|
|
208
|
+
const runtime = globalThis;
|
|
209
|
+
const context = runtime.OffscreenCanvas === undefined
|
|
210
|
+
? runtime.document?.createElement("canvas").getContext("2d")
|
|
211
|
+
: new runtime.OffscreenCanvas(1, 1).getContext("2d");
|
|
212
|
+
if (context === null || context === undefined) {
|
|
213
|
+
throw new Error("Text measurement requires OffscreenCanvas or a DOM canvas context.");
|
|
214
|
+
}
|
|
215
|
+
lineMetricContext = context;
|
|
216
|
+
return context;
|
|
217
|
+
}
|
|
218
|
+
function styleVerticalMetrics(document, style) {
|
|
219
|
+
const font = textStyleToMeasureFont(style, document);
|
|
220
|
+
const height = styleLineHeight(style);
|
|
221
|
+
const key = `${font}\n${height}`;
|
|
222
|
+
const cached = styleMetricCache.get(key);
|
|
223
|
+
if (cached !== undefined)
|
|
224
|
+
return cached;
|
|
225
|
+
const context = getLineMetricContext();
|
|
226
|
+
context.font = font;
|
|
227
|
+
const measured = context.measureText("Mg");
|
|
228
|
+
const fontAscent = measured.fontBoundingBoxAscent
|
|
229
|
+
?? measured.actualBoundingBoxAscent
|
|
230
|
+
?? style.fontSize * 0.8;
|
|
231
|
+
const fontDescent = measured.fontBoundingBoxDescent
|
|
232
|
+
?? measured.actualBoundingBoxDescent
|
|
233
|
+
?? style.fontSize * 0.2;
|
|
234
|
+
const leading = height - fontAscent - fontDescent;
|
|
235
|
+
const metrics = {
|
|
236
|
+
ascent: fontAscent + leading / 2,
|
|
237
|
+
descent: fontDescent + leading / 2,
|
|
238
|
+
height,
|
|
239
|
+
};
|
|
240
|
+
styleMetricCache.set(key, metrics);
|
|
241
|
+
return metrics;
|
|
242
|
+
}
|
|
243
|
+
function lineMetrics(document, width, styles) {
|
|
244
|
+
const metrics = styles.map((style) => styleVerticalMetrics(document, style));
|
|
245
|
+
const ascent = Math.max(0, ...metrics.map((entry) => entry.ascent));
|
|
246
|
+
const descent = Math.max(0, ...metrics.map((entry) => entry.descent));
|
|
247
|
+
return { width, ascent, descent, height: ascent + descent };
|
|
248
|
+
}
|
|
249
|
+
export function clearTextMeasurementCache() {
|
|
250
|
+
clearCache();
|
|
251
|
+
styleMetricCache.clear();
|
|
252
|
+
layoutCache = new WeakMap();
|
|
253
|
+
resolvedStyleCache = new WeakMap();
|
|
254
|
+
}
|
|
255
|
+
function splitParagraphAtHardBreaks(document, paragraph) {
|
|
256
|
+
const defaultStyle = resolveTextStyle(document, undefined, undefined);
|
|
257
|
+
const lines = [{
|
|
258
|
+
pieces: [],
|
|
259
|
+
fallbackStyle: defaultStyle,
|
|
260
|
+
}];
|
|
261
|
+
let endsWithHardBreak = false;
|
|
262
|
+
for (const run of paragraph.runs) {
|
|
263
|
+
const style = resolveTextStyle(document, run.styleId, run.overrides);
|
|
264
|
+
if (run.text.length > 0)
|
|
265
|
+
endsWithHardBreak = run.text.endsWith("\n");
|
|
266
|
+
const parts = run.text.split("\n");
|
|
267
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
268
|
+
const current = lines.at(-1);
|
|
269
|
+
if (current === undefined)
|
|
270
|
+
continue;
|
|
271
|
+
current.fallbackStyle = style;
|
|
272
|
+
const text = parts[index] ?? "";
|
|
273
|
+
if (text.length > 0)
|
|
274
|
+
current.pieces.push({ text, style });
|
|
275
|
+
if (index < parts.length - 1) {
|
|
276
|
+
lines.push({ pieces: [], fallbackStyle: style });
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
// CSS pre-wrap paints the empty line before a terminal break, but does not
|
|
281
|
+
// create another line box after that break. An explicit empty paragraph is
|
|
282
|
+
// still represented by the initial line above.
|
|
283
|
+
if (endsWithHardBreak && lines.length > 1)
|
|
284
|
+
lines.pop();
|
|
285
|
+
return lines;
|
|
286
|
+
}
|
|
287
|
+
function sameTextMetrics(document, left, right) {
|
|
288
|
+
return textStyleToMeasureFont(left, document) === textStyleToMeasureFont(right, document)
|
|
289
|
+
&& (left.letterSpacing ?? 0) === (right.letterSpacing ?? 0)
|
|
290
|
+
&& styleLineHeight(left) === styleLineHeight(right);
|
|
291
|
+
}
|
|
292
|
+
function naturalTextWidth(document, text, style) {
|
|
293
|
+
const letterSpacing = style.letterSpacing ?? 0;
|
|
294
|
+
return measureNaturalWidth(prepareWithSegments(text, textStyleToMeasureFont(style, document), {
|
|
295
|
+
whiteSpace: "pre-wrap",
|
|
296
|
+
...(letterSpacing === 0 ? {} : { letterSpacing }),
|
|
297
|
+
}));
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* The style a list marker of the paragraph is drawn in. A marker takes the
|
|
301
|
+
* shared style and the overrides it does not name itself from the first run
|
|
302
|
+
* of its paragraph, each on its own: a marker with a style and no overrides
|
|
303
|
+
* still takes the first run's overrides. A marker the renderer writes itself
|
|
304
|
+
* (a bullet or a number) takes both. The page compiler, the canvas estimate
|
|
305
|
+
* and `textLayoutKey` all resolve markers here, so they cannot disagree.
|
|
306
|
+
*/
|
|
307
|
+
export function resolveListMarkerStyle(document, paragraph) {
|
|
308
|
+
const firstRun = paragraph.runs[0];
|
|
309
|
+
const marker = paragraph.list?.marker;
|
|
310
|
+
return resolveTextStyle(document, marker?.styleId ?? firstRun?.styleId, marker?.overrides ?? firstRun?.overrides);
|
|
311
|
+
}
|
|
312
|
+
export function resolveTextListLayout(document, paragraph, paragraphIndex) {
|
|
313
|
+
if (paragraph.list === undefined)
|
|
314
|
+
return null;
|
|
315
|
+
const marker = paragraph.list.marker;
|
|
316
|
+
const style = resolveListMarkerStyle(document, paragraph);
|
|
317
|
+
const text = marker?.text
|
|
318
|
+
?? (paragraph.list.kind === "ordered"
|
|
319
|
+
? `${paragraph.list.start ?? paragraphIndex + 1}.`
|
|
320
|
+
: "•");
|
|
321
|
+
const markerWidth = naturalTextWidth(document, text, style);
|
|
322
|
+
const gap = marker?.gap ?? style.fontSize * 0.35;
|
|
323
|
+
const minimumHangingIndent = markerWidth + gap;
|
|
324
|
+
const authoredFirstLine = paragraph.indent?.firstLine ?? -minimumHangingIndent;
|
|
325
|
+
const firstLine = Math.min(authoredFirstLine, -minimumHangingIndent);
|
|
326
|
+
const nestingOffset = paragraph.list.level * style.fontSize * style.lineHeight;
|
|
327
|
+
return {
|
|
328
|
+
text,
|
|
329
|
+
style,
|
|
330
|
+
gap,
|
|
331
|
+
nestingOffset,
|
|
332
|
+
left: nestingOffset + Math.max(paragraph.indent?.left ?? 0, -firstLine),
|
|
333
|
+
right: paragraph.indent?.right ?? 0,
|
|
334
|
+
firstLine,
|
|
335
|
+
markerWidth,
|
|
336
|
+
markerBoxWidth: Math.max(markerWidth, -firstLine - gap),
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
function paragraphInsets(document, paragraph, paragraphIndex, firstLine) {
|
|
340
|
+
const list = resolveTextListLayout(document, paragraph, paragraphIndex);
|
|
341
|
+
if (list !== null)
|
|
342
|
+
return { left: list.left, right: list.right };
|
|
343
|
+
const indent = paragraph.indent;
|
|
344
|
+
if (indent === undefined)
|
|
345
|
+
return { left: 0, right: 0 };
|
|
346
|
+
const firstLineIndent = firstLine ? indent.firstLine : 0;
|
|
347
|
+
return {
|
|
348
|
+
left: indent.left + firstLineIndent,
|
|
349
|
+
right: indent.right,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function availableLineWidth(document, node, paragraph, paragraphIndex, firstLine) {
|
|
353
|
+
if (node.wrap === "none")
|
|
354
|
+
return Number.POSITIVE_INFINITY;
|
|
355
|
+
const insets = paragraphInsets(document, paragraph, paragraphIndex, firstLine);
|
|
356
|
+
return Math.max(1, node.frame.width - insets.left - insets.right);
|
|
357
|
+
}
|
|
358
|
+
function outerLineWidth(document, paragraph, paragraphIndex, firstLine, textWidth) {
|
|
359
|
+
const insets = paragraphInsets(document, paragraph, paragraphIndex, firstLine);
|
|
360
|
+
return Math.max(0, insets.left) + textWidth + Math.max(0, insets.right);
|
|
361
|
+
}
|
|
362
|
+
function stylesForLine(document, paragraph, paragraphIndex, firstLine, styles) {
|
|
363
|
+
if (!firstLine)
|
|
364
|
+
return styles;
|
|
365
|
+
const list = resolveTextListLayout(document, paragraph, paragraphIndex);
|
|
366
|
+
return list === null ? styles : [...styles, list.style];
|
|
367
|
+
}
|
|
368
|
+
// Scripts written right to left, whose letters are strong right-to-left
|
|
369
|
+
// characters in the Unicode bidirectional algorithm.
|
|
370
|
+
const RIGHT_TO_LEFT_LETTER = /[]|(?=[\p{L}\p{Nl}])[\p{Script=Hebrew}\p{Script=Arabic}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Nko}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Adlam}\p{Script=Hanifi_Rohingya}\p{Script=Mende_Kikakui}\p{Script=Imperial_Aramaic}\p{Script=Phoenician}\p{Script=Kharoshthi}\p{Script=Old_South_Arabian}\p{Script=Old_North_Arabian}\p{Script=Avestan}\p{Script=Inscriptional_Parthian}\p{Script=Inscriptional_Pahlavi}\p{Script=Psalter_Pahlavi}\p{Script=Old_Turkic}\p{Script=Nabataean}\p{Script=Palmyrene}\p{Script=Manichaean}\p{Script=Hatran}\p{Script=Old_Hungarian}\p{Script=Sogdian}\p{Script=Old_Sogdian}\p{Script=Lydian}\p{Script=Cypriot}\p{Script=Meroitic_Cursive}\p{Script=Meroitic_Hieroglyphs}]/u;
|
|
371
|
+
const STRONG_CHARACTER = /[\p{L}\p{Nl}\p{Mc}]/u;
|
|
372
|
+
/**
|
|
373
|
+
* The direction the renderer lays a paragraph out in: its authored direction,
|
|
374
|
+
* or for `auto` (and when omitted) the direction of its first strong character
|
|
375
|
+
* (a letter, or a left-to-right or right-to-left mark), as ICU's
|
|
376
|
+
* ubidi_getBaseDirection gives it; left to right when it has none.
|
|
377
|
+
*/
|
|
378
|
+
function paragraphDirection(paragraph) {
|
|
379
|
+
if (paragraph.direction === "ltr" || paragraph.direction === "rtl")
|
|
380
|
+
return paragraph.direction;
|
|
381
|
+
for (const run of paragraph.runs) {
|
|
382
|
+
for (const character of run.text) {
|
|
383
|
+
if (STRONG_CHARACTER.test(character))
|
|
384
|
+
return RIGHT_TO_LEFT_LETTER.test(character) ? "rtl" : "ltr";
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return "ltr";
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Whether the renderer (SkParagraph) can stretch a justified line of this text:
|
|
391
|
+
* it widens only the spaces between words, or the gaps around ideographs, so a
|
|
392
|
+
* line with neither keeps its natural width.
|
|
393
|
+
*/
|
|
394
|
+
function lineCanStretch(text) {
|
|
395
|
+
const trimmed = text.replace(/^[\s\u200b]+|[\s\u200b]+$/gu, "");
|
|
396
|
+
return /[\s]/u.test(trimmed) || (/\p{Ideographic}/u.test(trimmed) && [...trimmed].length > 1);
|
|
397
|
+
}
|
|
398
|
+
function measureUniformHardLine(document, node, paragraph, paragraphIndex, line, isFirstParagraphLine, stretchable) {
|
|
399
|
+
const style = line.pieces[0]?.style ?? line.fallbackStyle;
|
|
400
|
+
const text = line.pieces.map((piece) => piece.text).join("");
|
|
401
|
+
if (text.length === 0) {
|
|
402
|
+
stretchable?.push(false);
|
|
403
|
+
return [lineMetrics(document, outerLineWidth(document, paragraph, paragraphIndex, isFirstParagraphLine, 0), stylesForLine(document, paragraph, paragraphIndex, isFirstParagraphLine, [style]))];
|
|
404
|
+
}
|
|
405
|
+
const letterSpacing = style.letterSpacing ?? 0;
|
|
406
|
+
const prepared = prepareWithSegments(text, textStyleToMeasureFont(style, document), {
|
|
407
|
+
whiteSpace: "pre-wrap",
|
|
408
|
+
...(letterSpacing === 0 ? {} : { letterSpacing }),
|
|
409
|
+
});
|
|
410
|
+
const measurements = [];
|
|
411
|
+
let cursor = { segmentIndex: 0, graphemeIndex: 0 };
|
|
412
|
+
while (true) {
|
|
413
|
+
const firstLine = isFirstParagraphLine && measurements.length === 0;
|
|
414
|
+
const range = layoutNextLineRange(prepared, cursor, availableLineWidth(document, node, paragraph, paragraphIndex, firstLine));
|
|
415
|
+
if (range === null)
|
|
416
|
+
break;
|
|
417
|
+
stretchable?.push(lineCanStretch(materializeLineRange(prepared, range).text));
|
|
418
|
+
measurements.push(lineMetrics(document, outerLineWidth(document, paragraph, paragraphIndex, firstLine, range.width), stylesForLine(document, paragraph, paragraphIndex, firstLine, [style])));
|
|
419
|
+
cursor = range.end;
|
|
420
|
+
}
|
|
421
|
+
if (measurements.length === 0)
|
|
422
|
+
stretchable?.push(false);
|
|
423
|
+
return measurements.length === 0
|
|
424
|
+
? [lineMetrics(document, outerLineWidth(document, paragraph, paragraphIndex, isFirstParagraphLine, 0), stylesForLine(document, paragraph, paragraphIndex, isFirstParagraphLine, [style]))]
|
|
425
|
+
: measurements;
|
|
426
|
+
}
|
|
427
|
+
function richInlineItem(document, piece) {
|
|
428
|
+
const letterSpacing = piece.style.letterSpacing ?? 0;
|
|
429
|
+
return {
|
|
430
|
+
// rich-inline intentionally implements white-space: normal. NBSP preserves
|
|
431
|
+
// authored spaces while ZWSP restores the pre-wrap break opportunity after
|
|
432
|
+
// each space. Hard line breaks have already been split by the caller.
|
|
433
|
+
text: piece.text
|
|
434
|
+
.replaceAll(" ", "\u00a0\u200b")
|
|
435
|
+
.replaceAll("\t", `${"\u00a0".repeat(8)}\u200b`),
|
|
436
|
+
font: textStyleToMeasureFont(piece.style, document),
|
|
437
|
+
...(letterSpacing === 0 ? {} : { letterSpacing }),
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
function measureRichHardLine(document, node, paragraph, paragraphIndex, line, isFirstParagraphLine, stretchable) {
|
|
441
|
+
if (line.pieces.length === 0) {
|
|
442
|
+
stretchable?.push(false);
|
|
443
|
+
return [lineMetrics(document, outerLineWidth(document, paragraph, paragraphIndex, isFirstParagraphLine, 0), stylesForLine(document, paragraph, paragraphIndex, isFirstParagraphLine, [line.fallbackStyle]))];
|
|
444
|
+
}
|
|
445
|
+
if (node.wrap === "none") {
|
|
446
|
+
const width = line.pieces.reduce((total, piece) => {
|
|
447
|
+
const letterSpacing = piece.style.letterSpacing ?? 0;
|
|
448
|
+
const prepared = prepareWithSegments(piece.text, textStyleToMeasureFont(piece.style, document), {
|
|
449
|
+
whiteSpace: "pre-wrap",
|
|
450
|
+
...(letterSpacing === 0 ? {} : { letterSpacing }),
|
|
451
|
+
});
|
|
452
|
+
return total + measureNaturalWidth(prepared);
|
|
453
|
+
}, 0);
|
|
454
|
+
// One line per hard line: it ends at a break or the paragraph, so it is never stretched.
|
|
455
|
+
stretchable?.push(false);
|
|
456
|
+
return [lineMetrics(document, outerLineWidth(document, paragraph, paragraphIndex, isFirstParagraphLine, width), stylesForLine(document, paragraph, paragraphIndex, isFirstParagraphLine, line.pieces.map((piece) => piece.style)))];
|
|
457
|
+
}
|
|
458
|
+
// Pretext's rich-inline helper owns line breaking across differently styled
|
|
459
|
+
// runs. richInlineItem adapts its normal-whitespace input to our pre-wrap
|
|
460
|
+
// model; uniform-metric text can use Pretext's native pre-wrap path above.
|
|
461
|
+
const flow = prepareRichInline(line.pieces.map((piece) => richInlineItem(document, piece)));
|
|
462
|
+
const measurements = [];
|
|
463
|
+
let cursor;
|
|
464
|
+
while (true) {
|
|
465
|
+
const firstLine = isFirstParagraphLine && measurements.length === 0;
|
|
466
|
+
const range = layoutNextRichInlineLineRange(flow, availableLineWidth(document, node, paragraph, paragraphIndex, firstLine), cursor);
|
|
467
|
+
if (range === null)
|
|
468
|
+
break;
|
|
469
|
+
if (stretchable !== null) {
|
|
470
|
+
const lineText = materializeRichInlineLineRange(flow, range).fragments.map((fragment) => fragment.text).join("");
|
|
471
|
+
stretchable.push(lineCanStretch(lineText));
|
|
472
|
+
}
|
|
473
|
+
const styles = range.fragments.map((fragment) => (line.pieces[fragment.itemIndex]?.style ?? line.fallbackStyle));
|
|
474
|
+
measurements.push(lineMetrics(document, outerLineWidth(document, paragraph, paragraphIndex, firstLine, range.width), stylesForLine(document, paragraph, paragraphIndex, firstLine, styles.length === 0 ? [line.fallbackStyle] : styles)));
|
|
475
|
+
cursor = range.end;
|
|
476
|
+
}
|
|
477
|
+
if (measurements.length === 0)
|
|
478
|
+
stretchable?.push(false);
|
|
479
|
+
return measurements.length === 0
|
|
480
|
+
? [lineMetrics(document, outerLineWidth(document, paragraph, paragraphIndex, isFirstParagraphLine, 0), stylesForLine(document, paragraph, paragraphIndex, isFirstParagraphLine, [line.fallbackStyle]))]
|
|
481
|
+
: measurements;
|
|
482
|
+
}
|
|
483
|
+
function measureHardLine(document, node, paragraph, paragraphIndex, line, isFirstParagraphLine, stretchable) {
|
|
484
|
+
const firstStyle = line.pieces[0]?.style;
|
|
485
|
+
const hasUniformMetrics = firstStyle === undefined
|
|
486
|
+
|| line.pieces.every((piece) => sameTextMetrics(document, piece.style, firstStyle));
|
|
487
|
+
return hasUniformMetrics
|
|
488
|
+
? measureUniformHardLine(document, node, paragraph, paragraphIndex, line, isFirstParagraphLine, stretchable)
|
|
489
|
+
: measureRichHardLine(document, node, paragraph, paragraphIndex, line, isFirstParagraphLine, stretchable);
|
|
490
|
+
}
|
|
491
|
+
function cachedTextLayout(document, node, paragraphs) {
|
|
492
|
+
const cached = layoutCache.get(paragraphs);
|
|
493
|
+
if (cached !== undefined &&
|
|
494
|
+
cached.document === document &&
|
|
495
|
+
cached.wrap === node.wrap &&
|
|
496
|
+
cached.paragraphSpacing === node.paragraphSpacing &&
|
|
497
|
+
cached.width === node.frame.width) {
|
|
498
|
+
return cached.layout;
|
|
499
|
+
}
|
|
500
|
+
const layout = measureTextLayout(document, node, paragraphs);
|
|
501
|
+
layoutCache.set(paragraphs, {
|
|
502
|
+
document,
|
|
503
|
+
wrap: node.wrap,
|
|
504
|
+
paragraphSpacing: node.paragraphSpacing,
|
|
505
|
+
width: node.frame.width,
|
|
506
|
+
layout,
|
|
507
|
+
});
|
|
508
|
+
return layout;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* The space above the paragraph at `index` of a text: its own `spacingBefore`
|
|
512
|
+
* where set, otherwise the text's `paragraphSpacing` for every paragraph but
|
|
513
|
+
* the first.
|
|
514
|
+
*/
|
|
515
|
+
export function paragraphSpaceAbove(node, paragraph, index) {
|
|
516
|
+
return paragraph.spacingBefore ?? (index > 0 ? node.paragraphSpacing ?? 0 : 0);
|
|
517
|
+
}
|
|
518
|
+
function measureTextLayout(document, node, paragraphs = node.paragraphs) {
|
|
519
|
+
textMeasurementStats.layoutComputations += 1;
|
|
520
|
+
let width = 0;
|
|
521
|
+
let height = 0;
|
|
522
|
+
let lineCount = 0;
|
|
523
|
+
const measuredLines = [];
|
|
524
|
+
const baselines = [];
|
|
525
|
+
const lineParagraphs = [];
|
|
526
|
+
const lineJustified = [];
|
|
527
|
+
const measuredParagraphs = paragraphs.length === 0
|
|
528
|
+
? [{ runs: [{ text: "" }] }]
|
|
529
|
+
: paragraphs;
|
|
530
|
+
for (const [paragraphIndex, paragraph] of measuredParagraphs.entries()) {
|
|
531
|
+
height += paragraphSpaceAbove(node, paragraph, paragraphIndex);
|
|
532
|
+
const hardLines = splitParagraphAtHardBreaks(document, paragraph);
|
|
533
|
+
let isFirstParagraphLine = true;
|
|
534
|
+
for (const hardLine of hardLines) {
|
|
535
|
+
const stretchable = paragraph.align === "justify" ? [] : null;
|
|
536
|
+
const lines = measureHardLine(document, node, paragraph, paragraphIndex, hardLine, isFirstParagraphLine, stretchable);
|
|
537
|
+
for (const [lineIndex, line] of lines.entries()) {
|
|
538
|
+
lineJustified.push(lineIndex < lines.length - 1 && (stretchable?.[lineIndex] ?? false));
|
|
539
|
+
width = Math.max(width, line.width);
|
|
540
|
+
baselines.push(height + line.ascent);
|
|
541
|
+
height += line.height;
|
|
542
|
+
lineCount += 1;
|
|
543
|
+
measuredLines.push(line);
|
|
544
|
+
lineParagraphs.push(paragraphIndex);
|
|
545
|
+
}
|
|
546
|
+
isFirstParagraphLine = false;
|
|
547
|
+
}
|
|
548
|
+
height += paragraph.spacingAfter ?? 0;
|
|
549
|
+
}
|
|
550
|
+
return { width, height, lineCount, lines: measuredLines, baselines, lineParagraphs, lineJustified };
|
|
551
|
+
}
|
|
552
|
+
export function measureTextNode(document, node, paragraphs = node.paragraphs) {
|
|
553
|
+
const { baselines: _, lineParagraphs: _paragraphs, lineJustified: _justified, ...measurement } = cachedTextLayout(document, node, paragraphs);
|
|
554
|
+
return measurement;
|
|
555
|
+
}
|
|
556
|
+
export function getTextNodeBaselines(document, node, paragraphs = node.paragraphs) {
|
|
557
|
+
const measurement = cachedTextLayout(document, node, paragraphs);
|
|
558
|
+
const freeHeight = Math.max(0, node.frame.height - measurement.height);
|
|
559
|
+
const offset = node.verticalAlign === "top"
|
|
560
|
+
? 0
|
|
561
|
+
: node.verticalAlign === "middle"
|
|
562
|
+
? freeHeight / 2
|
|
563
|
+
: freeHeight;
|
|
564
|
+
return measurement.baselines.map((baseline) => baseline + offset);
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Where each line of a text sits in its box, from the same layout that gives
|
|
568
|
+
* `measureTextNode` and `getTextNodeBaselines`: each line moved down by the
|
|
569
|
+
* text's vertical alignment and across by its paragraph's alignment (a
|
|
570
|
+
* justified line spans the box only where the renderer stretches it: not the
|
|
571
|
+
* last line before a hard break or the paragraph's end, and not a line with no
|
|
572
|
+
* gap between words; in a right-to-left paragraph such a line keeps to the
|
|
573
|
+
* right). Each line is placed between its paragraph's left and
|
|
574
|
+
* right indents (the first-line indent on a paragraph's first line), as the
|
|
575
|
+
* renderer paints it; a list's first line also includes its marker. It only reads the layout; it
|
|
576
|
+
* draws nothing and changes no text.
|
|
577
|
+
*/
|
|
578
|
+
export function getTextNodeLineBoxes(document, node, paragraphs = node.paragraphs) {
|
|
579
|
+
const layout = cachedTextLayout(document, node, paragraphs);
|
|
580
|
+
const boxWidth = node.frame.width;
|
|
581
|
+
const freeHeight = Math.max(0, node.frame.height - layout.height);
|
|
582
|
+
const offset = node.verticalAlign === "top" ? 0 : node.verticalAlign === "middle" ? freeHeight / 2 : freeHeight;
|
|
583
|
+
// A line's own text width: its measured width without the positive insets it counts.
|
|
584
|
+
const lineTextWidth = (index) => {
|
|
585
|
+
const paragraphIndex = layout.lineParagraphs[index] ?? 0;
|
|
586
|
+
const paragraph = paragraphs[paragraphIndex];
|
|
587
|
+
if (paragraph === undefined)
|
|
588
|
+
return Math.max(0, layout.lines[index]?.width ?? 0);
|
|
589
|
+
const firstLine = index === 0 || layout.lineParagraphs[index - 1] !== paragraphIndex;
|
|
590
|
+
const insets = paragraphInsets(document, paragraph, paragraphIndex, firstLine);
|
|
591
|
+
return Math.max(0, (layout.lines[index]?.width ?? 0) - Math.max(0, insets.left) - Math.max(0, insets.right));
|
|
592
|
+
};
|
|
593
|
+
const paragraphWidestLine = (paragraphIndex) => layout.lineParagraphs.reduce((widest, lineParagraph, index) => (lineParagraph === paragraphIndex ? Math.max(widest, lineTextWidth(index)) : widest), 0);
|
|
594
|
+
return layout.lines.map((line, index) => {
|
|
595
|
+
const paragraphIndex = layout.lineParagraphs[index] ?? 0;
|
|
596
|
+
const align = paragraphs[paragraphIndex]?.align ?? "left";
|
|
597
|
+
const baseline = (layout.baselines[index] ?? 0) + offset;
|
|
598
|
+
// The renderer lays each line out between its paragraph's insets (with the
|
|
599
|
+
// first-line indent on a paragraph's first line) and aligns it there; the
|
|
600
|
+
// measured width also counts the positive insets, so take them back off.
|
|
601
|
+
const paragraph = paragraphs[paragraphIndex];
|
|
602
|
+
const firstLine = index === 0 || layout.lineParagraphs[index - 1] !== paragraphIndex;
|
|
603
|
+
const insets = paragraph === undefined
|
|
604
|
+
? { left: 0, right: 0 }
|
|
605
|
+
: paragraphInsets(document, paragraph, paragraphIndex, firstLine);
|
|
606
|
+
// A no-wrap paragraph is painted as one block at its first line's inset,
|
|
607
|
+
// so its later hard lines keep the first-line indent too.
|
|
608
|
+
const paintInsets = paragraph === undefined || firstLine || node.wrap !== "none"
|
|
609
|
+
? insets
|
|
610
|
+
: paragraphInsets(document, paragraph, paragraphIndex, true);
|
|
611
|
+
const lineLeft = paintInsets.left;
|
|
612
|
+
const lineBox = Math.max(1, boxWidth - lineLeft - paintInsets.right);
|
|
613
|
+
const textWidth = lineTextWidth(index);
|
|
614
|
+
const stretched = layout.lineJustified[index] === true;
|
|
615
|
+
const width = stretched ? Math.max(textWidth, lineBox) : textWidth;
|
|
616
|
+
// SkParagraph (TextLine::format and TextLine::justify) right-aligns every
|
|
617
|
+
// justified line it does not stretch when the paragraph runs right to left.
|
|
618
|
+
const place = align !== "justify"
|
|
619
|
+
? align
|
|
620
|
+
: !stretched && paragraph !== undefined && paragraphDirection(paragraph) === "rtl" ? "right" : "left";
|
|
621
|
+
let x;
|
|
622
|
+
if (node.wrap === "none") {
|
|
623
|
+
// The paragraph is laid out once at the wider of its box and its widest
|
|
624
|
+
// line plus one unit of slack, and placed so that block meets the box's
|
|
625
|
+
// aligned edge; only right-to-left justified lines align to the block.
|
|
626
|
+
const block = align === "justify" && place === "right"
|
|
627
|
+
? Math.max(lineBox, paragraphWidestLine(paragraphIndex) + 1)
|
|
628
|
+
: lineBox;
|
|
629
|
+
x = lineLeft + (place === "center" ? (lineBox - width) / 2 : place === "right" ? block - width : 0);
|
|
630
|
+
}
|
|
631
|
+
else {
|
|
632
|
+
// A wrapped line is only moved when it is narrower than its box.
|
|
633
|
+
x = lineLeft + Math.max(0, place === "center" ? (lineBox - width) / 2 : place === "right" ? lineBox - width : 0);
|
|
634
|
+
}
|
|
635
|
+
let right = x + width;
|
|
636
|
+
// A list's marker is painted on its first line, right-aligned in the box
|
|
637
|
+
// before the body's left edge.
|
|
638
|
+
const list = firstLine && paragraph !== undefined ? resolveTextListLayout(document, paragraph, paragraphIndex) : null;
|
|
639
|
+
if (list !== null) {
|
|
640
|
+
const markerEnd = list.left + list.firstLine + list.markerBoxWidth;
|
|
641
|
+
x = Math.min(x, markerEnd - list.markerWidth);
|
|
642
|
+
right = Math.max(right, markerEnd);
|
|
643
|
+
}
|
|
644
|
+
return { x, y: baseline - line.ascent, width: right - x, height: line.height, baseline };
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
export function getTextNodeFontRequests(document, node, paragraphs = node.paragraphs) {
|
|
648
|
+
const charactersByFont = new Map();
|
|
649
|
+
const append = (style, text) => {
|
|
650
|
+
const font = textStyleToCanvasFont(style, document);
|
|
651
|
+
let characters = charactersByFont.get(font);
|
|
652
|
+
if (characters === undefined) {
|
|
653
|
+
characters = new Set();
|
|
654
|
+
charactersByFont.set(font, characters);
|
|
655
|
+
}
|
|
656
|
+
for (const character of text)
|
|
657
|
+
characters.add(character);
|
|
658
|
+
};
|
|
659
|
+
paragraphs.forEach((paragraph, paragraphIndex) => {
|
|
660
|
+
paragraph.runs.forEach((run) => {
|
|
661
|
+
append(resolveTextStyle(document, run.styleId, run.overrides), run.text);
|
|
662
|
+
});
|
|
663
|
+
if (paragraph.list !== undefined) {
|
|
664
|
+
const marker = paragraph.list.marker;
|
|
665
|
+
const markerStyle = resolveListMarkerStyle(document, paragraph);
|
|
666
|
+
const markerText = marker?.text
|
|
667
|
+
?? (paragraph.list.kind === "ordered"
|
|
668
|
+
? `${paragraph.list.start ?? paragraphIndex + 1}.`
|
|
669
|
+
: "•");
|
|
670
|
+
append(markerStyle, markerText);
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
return [...charactersByFont].map(([font, characters]) => ({
|
|
674
|
+
font,
|
|
675
|
+
text: characters.size === 0
|
|
676
|
+
? " "
|
|
677
|
+
: [...characters].slice(0, 1_024).join(""),
|
|
678
|
+
}));
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* The point of a text box that stays in place on the page when its content
|
|
682
|
+
* resizes it, as fractions of the frame's local width and height. As in Figma,
|
|
683
|
+
* the top edge stays put, and auto-width text keeps its alignment edge: the
|
|
684
|
+
* left edge for left-aligned or justified text, the center for centered text
|
|
685
|
+
* and the right edge for right-aligned text. When paragraphs disagree, the left
|
|
686
|
+
* edge stays put.
|
|
687
|
+
*/
|
|
688
|
+
export function textAutoResizeAnchor(paragraphs) {
|
|
689
|
+
const aligns = new Set(paragraphs.map((paragraph) => paragraph.align ?? "left"));
|
|
690
|
+
const align = aligns.size === 1 ? [...aligns][0] : "left";
|
|
691
|
+
return { x: align === "center" ? 0.5 : align === "right" ? 1 : 0, y: 0 };
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* The frame of an auto-sized text box after its content changed its size.
|
|
695
|
+
* The box turns about its center, so keeping `frame.x` and `frame.y` would move
|
|
696
|
+
* rotated text across the page as it grows; this keeps the anchor from
|
|
697
|
+
* `textAutoResizeAnchor` fixed in page space instead.
|
|
698
|
+
*/
|
|
699
|
+
export function autoResizedTextFrame(node, paragraphs, size) {
|
|
700
|
+
return resizeRotatedRect(node.frame, node.rotation, size, textAutoResizeAnchor(paragraphs));
|
|
701
|
+
}
|
|
702
|
+
const fontAssetFingerprints = new WeakMap();
|
|
703
|
+
/** The font asset IDs `fontAssetFingerprint` reads while `textLayoutKeyAndFontAssets` runs. */
|
|
704
|
+
let readFontAssetIds;
|
|
705
|
+
/**
|
|
706
|
+
* `textLayoutKey`, and the IDs of the font assets the key names
|
|
707
|
+
* (`fontAssetFingerprint`), a missing one included: the key of the text
|
|
708
|
+
* changes only when the entry of one of these assets does, or the text or
|
|
709
|
+
* the theme.
|
|
710
|
+
*/
|
|
711
|
+
export function textLayoutKeyAndFontAssets(document, node) {
|
|
712
|
+
const outer = readFontAssetIds;
|
|
713
|
+
const read = new Set();
|
|
714
|
+
readFontAssetIds = read;
|
|
715
|
+
try {
|
|
716
|
+
const key = textLayoutKey(document, node);
|
|
717
|
+
return { key, fontAssetIds: [...read] };
|
|
718
|
+
}
|
|
719
|
+
finally {
|
|
720
|
+
readFontAssetIds = outer;
|
|
721
|
+
if (outer !== undefined)
|
|
722
|
+
for (const id of read)
|
|
723
|
+
outer.add(id);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
/** A 32-bit FNV-1a hash of `text`, started from `seed`. */
|
|
727
|
+
function hashText(text, seed) {
|
|
728
|
+
let hash = seed >>> 0;
|
|
729
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
730
|
+
hash = Math.imul(hash ^ text.charCodeAt(index), 0x01000193) >>> 0;
|
|
731
|
+
}
|
|
732
|
+
return hash;
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Names the font file a font asset stands for, so two assets with the same
|
|
736
|
+
* ID and other bytes are told apart: a local font by the SHA-256 of its file
|
|
737
|
+
* (its ID comes from the PostScript name alone, and another version of the
|
|
738
|
+
* font has other metrics), a URL by its address, a Google Fonts family by
|
|
739
|
+
* its name, and a file stored in the document by its length and two hashes
|
|
740
|
+
* of its data. The family, weight, style and file type are part of it.
|
|
741
|
+
* A missing asset has its own name, so it is never taken for a font.
|
|
742
|
+
*/
|
|
743
|
+
function fontAssetFingerprint(document, assetId) {
|
|
744
|
+
readFontAssetIds?.add(assetId);
|
|
745
|
+
const asset = entryOf(document.assets, assetId);
|
|
746
|
+
if (asset === undefined || asset.kind !== "font")
|
|
747
|
+
return `no font asset ${assetId}`;
|
|
748
|
+
let fingerprint = fontAssetFingerprints.get(asset);
|
|
749
|
+
if (fingerprint === undefined) {
|
|
750
|
+
const source = asset.source;
|
|
751
|
+
fingerprint = JSON.stringify([
|
|
752
|
+
asset.mimeType,
|
|
753
|
+
asset.family,
|
|
754
|
+
asset.weight,
|
|
755
|
+
asset.style,
|
|
756
|
+
source.kind === "data"
|
|
757
|
+
? ["data", source.data.length, hashText(source.data, 0x811c9dc5), hashText(source.data, 0x9747b28c)]
|
|
758
|
+
: source.kind === "url"
|
|
759
|
+
? ["url", source.url]
|
|
760
|
+
: source.kind === "google-fonts"
|
|
761
|
+
? ["google-fonts", source.family]
|
|
762
|
+
: source.kind === "web"
|
|
763
|
+
? ["web", source.families, source.stylesheetUrl ?? null]
|
|
764
|
+
: ["local", source.postScriptName, source.styleName, source.sha256],
|
|
765
|
+
]);
|
|
766
|
+
fontAssetFingerprints.set(asset, fingerprint);
|
|
767
|
+
}
|
|
768
|
+
return fingerprint;
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* The parts of a text style that decide where its glyphs go, and the font
|
|
772
|
+
* files its font assets stand for in this document (`fontAssetFingerprint`).
|
|
773
|
+
*/
|
|
774
|
+
function layoutStyle(document, style) {
|
|
775
|
+
const { name: _name, color: _color, underline: _underline, strikethrough: _strikethrough, ...layout } = style;
|
|
776
|
+
if (layout.fontAssetId === undefined && (layout.fontFallbackAssetIds ?? []).length === 0)
|
|
777
|
+
return layout;
|
|
778
|
+
return [
|
|
779
|
+
layout,
|
|
780
|
+
layout.fontAssetId === undefined ? null : fontAssetFingerprint(document, layout.fontAssetId),
|
|
781
|
+
(layout.fontFallbackAssetIds ?? []).map((assetId) => fontAssetFingerprint(document, assetId)),
|
|
782
|
+
];
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* `value` with the keys of every object in it in alphabetical order, and
|
|
786
|
+
* without the keys whose value is `undefined`. Two styles or paragraphs that
|
|
787
|
+
* hold the same fields, written in another order, then give the same text.
|
|
788
|
+
*/
|
|
789
|
+
function inKeyOrder(value) {
|
|
790
|
+
if (Array.isArray(value))
|
|
791
|
+
return value.map(inKeyOrder);
|
|
792
|
+
if (value === null || typeof value !== "object")
|
|
793
|
+
return value;
|
|
794
|
+
const record = value;
|
|
795
|
+
const ordered = {};
|
|
796
|
+
for (const key of Object.keys(record).sort()) {
|
|
797
|
+
if (record[key] !== undefined)
|
|
798
|
+
putEntry(ordered, key, inKeyOrder(record[key]));
|
|
799
|
+
}
|
|
800
|
+
return ordered;
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* Names everything in the document that decides the size of an auto-sized
|
|
804
|
+
* text: its words, the styles they resolve to in this document (so a theme
|
|
805
|
+
* or variable mode that changes a style changes the key), the font file each
|
|
806
|
+
* of their font assets stands for, the paragraph settings, how the box sizes
|
|
807
|
+
* itself, and its width when only its height follows the text. A list marker
|
|
808
|
+
* is named by the style the renderer draws it in (`resolveListMarkerStyle`),
|
|
809
|
+
* which can come from the first run of its paragraph. Two texts
|
|
810
|
+
* of one document with the same key are laid out alike by the renderer (the
|
|
811
|
+
* order in which the fields of a style or a paragraph were written is not
|
|
812
|
+
* part of the key), as
|
|
813
|
+
* long as it draws with the same font files; the files that are not in the
|
|
814
|
+
* document (which parts of a Google Fonts family have arrived, the fallback
|
|
815
|
+
* fonts the renderer picks itself) are told apart by `setDrawnTextFontSet`.
|
|
816
|
+
* The drawn sizes are kept by this key
|
|
817
|
+
* (`recordDrawnTextSize`), and anything else that remembers or counts
|
|
818
|
+
* something about one layout of a text uses it too, so a change of font,
|
|
819
|
+
* size, wrap width or sizing mode is never taken for the layout before it.
|
|
820
|
+
*/
|
|
821
|
+
export function textLayoutKey(document, node, paragraphs = node.paragraphs) {
|
|
822
|
+
return JSON.stringify(inKeyOrder([
|
|
823
|
+
node.autoResize,
|
|
824
|
+
node.autoResize === "height" ? [node.frame.width, node.wrap] : null,
|
|
825
|
+
layoutStyle(document, resolveTextStyle(document, undefined, undefined)),
|
|
826
|
+
paragraphs.map((paragraph) => {
|
|
827
|
+
const { runs, list, ...settings } = paragraph;
|
|
828
|
+
return [
|
|
829
|
+
settings,
|
|
830
|
+
list === undefined ? null : {
|
|
831
|
+
...list,
|
|
832
|
+
...(list.marker === undefined ? {} : {
|
|
833
|
+
marker: {
|
|
834
|
+
...list.marker,
|
|
835
|
+
style: layoutStyle(document, resolveListMarkerStyle(document, paragraph)),
|
|
836
|
+
},
|
|
837
|
+
}),
|
|
838
|
+
},
|
|
839
|
+
runs.map((run) => [run.text, layoutStyle(document, resolveTextStyle(document, run.styleId, run.overrides))]),
|
|
840
|
+
];
|
|
841
|
+
}),
|
|
842
|
+
// Left out when unset, so the keys of texts without it stay as they were.
|
|
843
|
+
...(node.paragraphSpacing === undefined ? [] : [{ paragraphSpacing: node.paragraphSpacing }]),
|
|
844
|
+
]));
|
|
845
|
+
}
|
|
846
|
+
export * from "./text-sizer.js";
|