laymur 0.28.0 → 0.29.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.
Files changed (81) hide show
  1. package/README.md +53 -4
  2. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.d.ts +14 -3
  3. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.js +38 -5
  4. package/dist/core/constraints/UICustomConstraint.js +1 -1
  5. package/dist/core/constraints/UIHeightConstraint/UIHeightConstraint.js +1 -1
  6. package/dist/core/constraints/UIHorizontalDistanceConstraint/UIHorizontalDistanceConstraint.js +1 -1
  7. package/dist/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.js +1 -1
  8. package/dist/core/constraints/UIHorizontalProportionConstraint/UIHorizontalProportionConstraint.js +1 -1
  9. package/dist/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.d.ts +6 -0
  10. package/dist/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.js +8 -0
  11. package/dist/core/constraints/UIVerticalDistanceConstraint/UIVerticalDistanceConstraint.js +1 -1
  12. package/dist/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.js +1 -1
  13. package/dist/core/constraints/UIVerticalProportionConstraint/UIVerticalProportionConstraint.js +1 -1
  14. package/dist/core/constraints/UIWidthConstraint/UIWidthConstraint.js +1 -1
  15. package/dist/core/elements/UICanvasElement/UICanvasElement.d.ts +54 -0
  16. package/dist/core/elements/UICanvasElement/UICanvasElement.js +70 -0
  17. package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +2 -0
  18. package/dist/core/elements/UIDummy/UIDummy.js +3 -1
  19. package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +42 -2
  20. package/dist/core/elements/UIGraphics/UIGraphics.Internal.js +2 -2
  21. package/dist/core/elements/UIGraphics/UIGraphics.d.ts +48 -34
  22. package/dist/core/elements/UIGraphics/UIGraphics.js +196 -93
  23. package/dist/core/elements/UIImage/UIImage.d.ts +11 -1
  24. package/dist/core/elements/UIImage/UIImage.js +14 -0
  25. package/dist/core/elements/UIScene/UIScene.Internal.d.ts +10 -1
  26. package/dist/core/elements/UIScene/UIScene.Internal.js +1 -0
  27. package/dist/core/elements/UIScene/UIScene.d.ts +11 -1
  28. package/dist/core/elements/UIScene/UIScene.js +29 -5
  29. package/dist/core/elements/UIText/UIText.Interfaces.d.ts +2 -2
  30. package/dist/core/elements/UIText/UIText.Internal.d.ts +14 -1
  31. package/dist/core/elements/UIText/UIText.Internal.js +12 -0
  32. package/dist/core/elements/UIText/UIText.Measuring.d.ts +9 -4
  33. package/dist/core/elements/UIText/UIText.Measuring.js +17 -6
  34. package/dist/core/elements/UIText/UIText.Rendering.d.ts +7 -16
  35. package/dist/core/elements/UIText/UIText.Rendering.js +212 -46
  36. package/dist/core/elements/UIText/UIText.d.ts +58 -28
  37. package/dist/core/elements/UIText/UIText.js +113 -138
  38. package/dist/core/index.d.ts +10 -4
  39. package/dist/core/index.js +5 -1
  40. package/dist/core/layers/UILayer/UILayer.d.ts +17 -3
  41. package/dist/core/layers/UILayer/UILayer.js +35 -4
  42. package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +2 -0
  43. package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +3 -0
  44. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.d.ts +46 -0
  45. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.js +86 -0
  46. package/dist/core/miscellaneous/color/UIColor.d.ts +17 -0
  47. package/dist/core/miscellaneous/color/UIColor.js +22 -0
  48. package/dist/core/miscellaneous/color/canvasColor.d.ts +9 -0
  49. package/dist/core/miscellaneous/color/canvasColor.js +15 -0
  50. package/dist/core/miscellaneous/insets/UIInsets.d.ts +3 -0
  51. package/dist/core/miscellaneous/insets/UIInsets.js +15 -0
  52. package/dist/core/miscellaneous/paint/UIGradient.Internal.d.ts +37 -0
  53. package/dist/core/miscellaneous/paint/UIGradient.Internal.js +14 -0
  54. package/dist/core/miscellaneous/paint/UIGradient.d.ts +18 -0
  55. package/dist/core/miscellaneous/paint/UIGradient.js +35 -0
  56. package/dist/core/miscellaneous/paint/UILinearGradient.d.ts +14 -0
  57. package/dist/core/miscellaneous/paint/UILinearGradient.js +22 -0
  58. package/dist/core/miscellaneous/paint/UIPaint.Internal.d.ts +21 -0
  59. package/dist/core/miscellaneous/paint/UIPaint.Internal.js +1 -0
  60. package/dist/core/miscellaneous/paint/UIPaint.d.ts +17 -0
  61. package/dist/core/miscellaneous/paint/UIPaint.js +21 -0
  62. package/dist/core/miscellaneous/paint/UIRadialGradient.d.ts +18 -0
  63. package/dist/core/miscellaneous/paint/UIRadialGradient.js +33 -0
  64. package/dist/core/miscellaneous/shared.d.ts +14 -0
  65. package/dist/core/miscellaneous/shared.js +11 -0
  66. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.d.ts +88 -30
  67. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.js +12 -8
  68. package/dist/core/miscellaneous/text-style/UITextStyle.d.ts +41 -66
  69. package/dist/core/miscellaneous/text-style/UITextStyle.js +165 -294
  70. package/dist/core/miscellaneous/texture/UITextureView.Internal.d.ts +2 -1
  71. package/dist/core/miscellaneous/texture/UITextureView.Internal.js +12 -3
  72. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +1 -1
  73. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +1 -1
  74. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +1 -1
  75. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +6 -2
  76. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +41 -9
  77. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +97 -35
  78. package/dist/particles/elements/UIEmitter.Internal.d.ts +3 -0
  79. package/dist/particles/elements/UIEmitter.Internal.js +1 -0
  80. package/dist/particles/elements/UIEmitter.js +3 -2
  81. package/package.json +1 -1
@@ -1,5 +1,6 @@
1
1
  import { UITextStyle } from "../../miscellaneous/text-style/UITextStyle.js";
2
2
  import { detectBidiClass, reorderChunksVisually } from "./UIText.Bidi.js";
3
+ import { UITextLineBox } from "./UIText.Internal.js";
3
4
  /**
4
5
  * Applies font of a resolved style to a canvas context.
5
6
  * Shared by measuring and rendering so both stay in sync.
@@ -27,7 +28,7 @@ function isBreakableSpace(textChunk) {
27
28
  return textChunk.text !== "\n" && /^\s$/.test(textChunk.text);
28
29
  }
29
30
  /**
30
- * Rounds an overhang up; a negative or missing value means none, so a line never gets narrower
31
+ * Rounds an overhang up. A negative or missing value means none, so a line never gets narrower
31
32
  * than its advances.
32
33
  * @param value Overhang in pixels
33
34
  * @returns Overhang in whole pixels, at least 0
@@ -37,12 +38,15 @@ function ceilOverhang(value) {
37
38
  }
38
39
  /**
39
40
  * Measures single text chunk.
41
+ * With `UITextLineBox.FONT` the chunk reaches the ascent and descent of its font, or further
42
+ * where its glyphs do. A browser without font metrics measures the glyphs.
40
43
  * @param context Canvas context for measurement
41
44
  * @param text Chunk text
42
45
  * @param textStyle Resolved text style
46
+ * @param lineBox What the height of the chunk follows
43
47
  * @returns Chunk metrics
44
48
  */
45
- export function measureTextChunk(context, text, textStyle) {
49
+ export function measureTextChunk(context, text, textStyle, lineBox) {
46
50
  applyFontStyle(context, textStyle);
47
51
  if (text === "\n") {
48
52
  return {
@@ -55,9 +59,15 @@ export function measureTextChunk(context, text, textStyle) {
55
59
  };
56
60
  }
57
61
  const metrics = context.measureText(text);
58
- const ascent = Math.ceil(metrics.actualBoundingBoxAscent);
59
- const descent = Math.ceil(metrics.actualBoundingBoxDescent);
62
+ let ascent = Math.ceil(metrics.actualBoundingBoxAscent);
63
+ let descent = Math.ceil(metrics.actualBoundingBoxDescent);
60
64
  const width = Math.ceil(metrics.width);
65
+ if (lineBox === UITextLineBox.FONT &&
66
+ Number.isFinite(metrics.fontBoundingBoxAscent) &&
67
+ Number.isFinite(metrics.fontBoundingBoxDescent)) {
68
+ ascent = Math.max(ascent, Math.ceil(metrics.fontBoundingBoxAscent));
69
+ descent = Math.max(descent, Math.ceil(metrics.fontBoundingBoxDescent));
70
+ }
61
71
  return {
62
72
  width,
63
73
  height: ascent + descent,
@@ -196,16 +206,17 @@ export function calculateTextSize(lines) {
196
206
  * @param textSpans Text spans with styles
197
207
  * @param commonStyle Fallback style
198
208
  * @param isRTL Whether the paragraph reads right to left
209
+ * @param lineBox What the height of a line follows
199
210
  * @returns Measured text chunks
200
211
  */
201
- export function buildTextChunks(context, textSpans, commonStyle, isRTL) {
212
+ export function buildTextChunks(context, textSpans, commonStyle, isRTL, lineBox) {
202
213
  context.direction = isRTL ? "rtl" : "ltr";
203
214
  context.textAlign = "left";
204
215
  const textChunks = [];
205
216
  for (const textSpan of textSpans) {
206
217
  const textStyle = UITextStyle.resolve(textSpan.style, commonStyle);
207
218
  for (const textChunk of splitTextIntoChunks(textSpan.text)) {
208
- const textChunkMetrics = measureTextChunk(context, textChunk, textStyle);
219
+ const textChunkMetrics = measureTextChunk(context, textChunk, textStyle, lineBox);
209
220
  textChunks.push({
210
221
  text: textChunk,
211
222
  style: textStyle,
@@ -1,22 +1,13 @@
1
1
  import type { UICanvasRenderingContext2D } from "../../miscellaneous/canvas.js";
2
- import type { UITextStyleConfig } from "../../miscellaneous/text-style/UITextStyle.Internal.js";
3
2
  import type { UITextLine } from "./UIText.Interfaces.js";
4
3
  /**
5
- * Renders single text chunk.
6
- * @param x X coordinate in pixels
7
- * @param y Y coordinate in pixels
8
- * @param text Chunk text
9
- * @param style Resolved text style
10
- * @param context Canvas rendering context
11
- */
12
- export declare function renderTextChunk(x: number, y: number, text: string, style: UITextStyleConfig, context: UICanvasRenderingContext2D): void;
13
- /**
14
- * Renders text lines to canvas.
15
- * Chunks are laid out left to right after visual reordering, so x stays the left edge.
16
- * @param paddingTop Top padding in pixels
17
- * @param paddingLeft Left padding in pixels
4
+ * Draws text lines on a cleared canvas whose transform maps text pixels onto it. Shadows of all
5
+ * chunks go first, then outlines from the outermost, then the glyphs, so the layers of one span
6
+ * never cover those of another.
7
+ * @param context Canvas context
18
8
  * @param lines Text lines to render
19
9
  * @param isRTL Whether the paragraph reads right to left
20
- * @param context Canvas rendering context
10
+ * @param paddingTop Top padding in pixels
11
+ * @param paddingLeft Left padding in pixels
21
12
  */
22
- export declare function renderTextLines(paddingTop: number, paddingLeft: number, lines: UITextLine[], isRTL: boolean, context: UICanvasRenderingContext2D): void;
13
+ export declare function renderTextLines(context: UICanvasRenderingContext2D, lines: UITextLine[], isRTL: boolean, paddingTop: number, paddingLeft: number): void;
@@ -1,6 +1,13 @@
1
- import { TEXT_STYLE_DEFAULT_ALIGN } from "../../miscellaneous/text-style/UITextStyle.Internal.js";
1
+ import { createCanvas, getCanvas2DContext } from "../../miscellaneous/canvas.js";
2
+ import { toCanvasPaint } from "../../miscellaneous/paint/UIPaint.js";
3
+ import { TEXT_STYLE_DEFAULT_ALIGN, UITextPaintBox, } from "../../miscellaneous/text-style/UITextStyle.Internal.js";
2
4
  import { reorderChunksVisually } from "./UIText.Bidi.js";
3
- import { applyFontStyle } from "./UIText.Measuring.js";
5
+ import { applyFontStyle, calculateTextSize } from "./UIText.Measuring.js";
6
+ /** Color the silhouette casting a shadow is drawn with. The shadow takes only its alpha. */
7
+ const SILHOUETTE_COLOR = "#000000";
8
+ /** Canvas the silhouettes are drawn on, shared by all texts, created on first use and shrunk after */
9
+ let silhouetteCanvas;
10
+ let silhouetteContext;
4
11
  /**
5
12
  * Resolves logical alignment into a physical one.
6
13
  * @param align Alignment to resolve
@@ -17,50 +24,24 @@ function resolveAlign(align, isRTL) {
17
24
  return align;
18
25
  }
19
26
  /**
20
- * Renders single text chunk.
21
- * @param x X coordinate in pixels
22
- * @param y Y coordinate in pixels
23
- * @param text Chunk text
24
- * @param style Resolved text style
25
- * @param context Canvas rendering context
26
- */
27
- export function renderTextChunk(x, y, text, style, context) {
28
- applyFontStyle(context, style);
29
- if (style.enableShadow) {
30
- context.shadowOffsetX = style.shadowOffsetX;
31
- context.shadowOffsetY = style.shadowOffsetY;
32
- context.shadowBlur = style.shadowBlur;
33
- context.shadowColor = style.shadowColor;
34
- }
35
- else {
36
- context.shadowColor = "transparent";
37
- }
38
- if (style.enableStroke) {
39
- context.strokeStyle = style.strokeColor;
40
- context.lineWidth = style.strokeThickness;
41
- context.strokeText(text, x, y);
42
- }
43
- context.fillStyle = style.color;
44
- context.fillText(text, x, y);
45
- }
46
- /**
47
- * Renders text lines to canvas.
48
- * Chunks are laid out left to right after visual reordering, so x stays the left edge.
27
+ * Places the visible chunks of the lines. Chunks are laid out left to right after visual
28
+ * reordering, so x stays the left edge.
29
+ * @param lines Text lines
30
+ * @param isRTL Whether the paragraph reads right to left
49
31
  * @param paddingTop Top padding in pixels
50
32
  * @param paddingLeft Left padding in pixels
51
- * @param lines Text lines to render
52
- * @param isRTL Whether the paragraph reads right to left
53
- * @param context Canvas rendering context
33
+ * @returns Chunks with visible glyphs and their places
54
34
  */
55
- export function renderTextLines(paddingTop, paddingLeft, lines, isRTL, context) {
56
- context.clearRect(0, 0, context.canvas.width, context.canvas.height);
57
- context.direction = isRTL ? "rtl" : "ltr";
58
- context.textAlign = "left";
35
+ function placeChunks(lines, isRTL, paddingTop, paddingLeft) {
36
+ const textSize = calculateTextSize(lines);
37
+ const textBox = {
38
+ x: paddingLeft,
39
+ y: paddingTop,
40
+ width: textSize.width,
41
+ height: textSize.height,
42
+ };
43
+ const placed = [];
59
44
  let currentY = paddingTop;
60
- let textWidth = 0;
61
- for (const line of lines) {
62
- textWidth = Math.max(line.width, textWidth);
63
- }
64
45
  for (let i = 0; i < lines.length; i++) {
65
46
  const line = lines[i];
66
47
  const firstAlign = line.chunks[0]?.style.align ?? TEXT_STYLE_DEFAULT_ALIGN;
@@ -68,16 +49,201 @@ export function renderTextLines(paddingTop, paddingLeft, lines, isRTL, context)
68
49
  const lineAlign = resolveAlign(isUniformAlign ? firstAlign : TEXT_STYLE_DEFAULT_ALIGN, isRTL);
69
50
  let offsetX = 0;
70
51
  if (lineAlign === "center") {
71
- offsetX = (textWidth - line.width) / 2;
52
+ offsetX = (textSize.width - line.width) / 2;
72
53
  }
73
54
  else if (lineAlign === "right") {
74
- offsetX = textWidth - line.width;
55
+ offsetX = textSize.width - line.width;
75
56
  }
76
- let currentX = paddingLeft + offsetX + line.overhangLeft;
77
57
  currentY += i === 0 ? line.baseline : line.lineHeight;
58
+ const lineBox = {
59
+ x: paddingLeft + offsetX,
60
+ y: currentY - line.baseline,
61
+ width: line.width,
62
+ height: line.height,
63
+ };
64
+ let currentX = paddingLeft + offsetX + line.overhangLeft;
78
65
  for (const chunk of reorderChunksVisually(line.chunks, isRTL)) {
79
- renderTextChunk(currentX, currentY, chunk.text, chunk.style, context);
66
+ if (chunk.text.trim() !== "") {
67
+ placed.push({
68
+ chunk,
69
+ x: currentX,
70
+ y: currentY,
71
+ paintBox: chunk.style.paintBox === UITextPaintBox.TEXT ? textBox : lineBox,
72
+ });
73
+ }
80
74
  currentX += chunk.metrics.width;
81
75
  }
82
76
  }
77
+ return placed;
78
+ }
79
+ /**
80
+ * Total width of the outlines of a chunk up to a depth, inclusive.
81
+ * @param chunk Measured chunk
82
+ * @param depth Index of the outermost outline counted
83
+ * @returns Width outside the glyphs in pixels
84
+ */
85
+ function outlineWidth(chunk, depth) {
86
+ let width = 0;
87
+ for (let i = 0; i <= depth; i++) {
88
+ width += chunk.style.strokes[i].thickness;
89
+ }
90
+ return width;
91
+ }
92
+ /**
93
+ * Gives the shared silhouette canvas, cleared and sized like the target, with its transform.
94
+ * @param target Canvas context the shadows are drawn on
95
+ * @returns Silhouette canvas context
96
+ * @throws If the browser gives no 2D context
97
+ */
98
+ function prepareSilhouette(target) {
99
+ const { width, height } = target.canvas;
100
+ if (silhouetteCanvas === undefined || silhouetteContext === undefined) {
101
+ silhouetteCanvas = createCanvas(width, height);
102
+ const context = getCanvas2DContext(silhouetteCanvas);
103
+ if (!context) {
104
+ throw new Error("UIText.renderTextLines: failed to create canvas context for shadows");
105
+ }
106
+ silhouetteContext = context;
107
+ }
108
+ else if (silhouetteCanvas.width !== width || silhouetteCanvas.height !== height) {
109
+ silhouetteCanvas.width = width;
110
+ silhouetteCanvas.height = height;
111
+ }
112
+ silhouetteContext.setTransform(1, 0, 0, 1, 0, 0);
113
+ silhouetteContext.clearRect(0, 0, width, height);
114
+ silhouetteContext.setTransform(target.getTransform());
115
+ silhouetteContext.fillStyle = SILHOUETTE_COLOR;
116
+ silhouetteContext.strokeStyle = SILHOUETTE_COLOR;
117
+ return silhouetteContext;
118
+ }
119
+ /**
120
+ * Draws the outlined glyphs of chunks in one color, as one shape.
121
+ * @param context Silhouette canvas context
122
+ * @param chunks Placed chunks
123
+ */
124
+ function drawSilhouette(context, chunks) {
125
+ for (const { chunk, x, y } of chunks) {
126
+ applyFontStyle(context, chunk.style);
127
+ const strokeCount = chunk.style.strokes.length;
128
+ if (strokeCount > 0) {
129
+ context.lineWidth = outlineWidth(chunk, strokeCount - 1) * 2;
130
+ context.lineJoin = chunk.style.strokes[strokeCount - 1].join;
131
+ context.strokeText(chunk.text, x, y);
132
+ }
133
+ context.fillText(chunk.text, x, y);
134
+ }
135
+ }
136
+ /**
137
+ * Draws the shadows. A shadow is cast by one silhouette of all the outlined glyphs that have it,
138
+ * so overlapping glyphs, outlines and neighbouring spans cast it once. The silhouette is drawn
139
+ * one canvas width to the left of the target and the shadow offset brings the shadow back, so
140
+ * only the shadow lands. Shadow offsets and blur ignore the canvas transform and are scaled by
141
+ * hand. The shared silhouette canvas is shrunk afterwards, since a canvas keeps its memory while
142
+ * it is large.
143
+ * @param context Canvas context
144
+ * @param placed Placed chunks
145
+ */
146
+ function drawShadows(context, placed) {
147
+ let depthCount = 0;
148
+ for (const { chunk } of placed) {
149
+ depthCount = Math.max(depthCount, chunk.style.shadows.length);
150
+ }
151
+ if (depthCount === 0) {
152
+ return;
153
+ }
154
+ const { a: scaleX, d: scaleY } = context.getTransform();
155
+ const { width, height } = context.canvas;
156
+ for (let depth = depthCount - 1; depth >= 0; depth--) {
157
+ const groups = new Map();
158
+ for (const item of placed) {
159
+ const shadow = item.chunk.style.shadows[depth];
160
+ if (shadow === undefined) {
161
+ continue;
162
+ }
163
+ const key = `${shadow.color}|${shadow.offsetX}|${shadow.offsetY}|${shadow.blur}`;
164
+ const group = groups.get(key);
165
+ if (group === undefined) {
166
+ groups.set(key, { shadow, chunks: [item] });
167
+ }
168
+ else {
169
+ group.chunks.push(item);
170
+ }
171
+ }
172
+ for (const { shadow, chunks } of groups.values()) {
173
+ const silhouette = prepareSilhouette(context);
174
+ drawSilhouette(silhouette, chunks);
175
+ context.save();
176
+ context.setTransform(1, 0, 0, 1, 0, 0);
177
+ context.shadowColor = shadow.color;
178
+ context.shadowOffsetX = width + shadow.offsetX * scaleX;
179
+ context.shadowOffsetY = shadow.offsetY * scaleY;
180
+ context.shadowBlur = (shadow.blur * (scaleX + scaleY)) / 2;
181
+ context.drawImage(silhouette.canvas, -width, 0, width, height);
182
+ context.restore();
183
+ }
184
+ }
185
+ if (silhouetteCanvas !== undefined) {
186
+ silhouetteCanvas.width = 1;
187
+ silhouetteCanvas.height = 1;
188
+ }
189
+ }
190
+ /**
191
+ * Draws the outlines, the outermost of every chunk first, so an inner outline of one chunk is
192
+ * never covered by an outer outline of its neighbour. A stroke is centered on its path, so it is
193
+ * twice as wide as the outlines it reaches across.
194
+ * @param context Canvas context
195
+ * @param placed Placed chunks
196
+ */
197
+ function drawStrokes(context, placed) {
198
+ let depthCount = 0;
199
+ for (const { chunk } of placed) {
200
+ depthCount = Math.max(depthCount, chunk.style.strokes.length);
201
+ }
202
+ for (let depth = depthCount - 1; depth >= 0; depth--) {
203
+ for (const { chunk, x, y, paintBox } of placed) {
204
+ if (depth >= chunk.style.strokes.length) {
205
+ continue;
206
+ }
207
+ const stroke = chunk.style.strokes[depth];
208
+ const width = outlineWidth(chunk, depth);
209
+ if (width <= 0) {
210
+ continue;
211
+ }
212
+ applyFontStyle(context, chunk.style);
213
+ context.strokeStyle = toCanvasPaint(context, stroke.paint, paintBox);
214
+ context.lineWidth = width * 2;
215
+ context.lineJoin = stroke.join;
216
+ context.strokeText(chunk.text, x, y);
217
+ }
218
+ }
219
+ }
220
+ /**
221
+ * Draws the glyphs.
222
+ * @param context Canvas context
223
+ * @param placed Placed chunks
224
+ */
225
+ function drawFills(context, placed) {
226
+ for (const { chunk, x, y, paintBox } of placed) {
227
+ applyFontStyle(context, chunk.style);
228
+ context.fillStyle = toCanvasPaint(context, chunk.style.color, paintBox);
229
+ context.fillText(chunk.text, x, y);
230
+ }
231
+ }
232
+ /**
233
+ * Draws text lines on a cleared canvas whose transform maps text pixels onto it. Shadows of all
234
+ * chunks go first, then outlines from the outermost, then the glyphs, so the layers of one span
235
+ * never cover those of another.
236
+ * @param context Canvas context
237
+ * @param lines Text lines to render
238
+ * @param isRTL Whether the paragraph reads right to left
239
+ * @param paddingTop Top padding in pixels
240
+ * @param paddingLeft Left padding in pixels
241
+ */
242
+ export function renderTextLines(context, lines, isRTL, paddingTop, paddingLeft) {
243
+ context.direction = isRTL ? "rtl" : "ltr";
244
+ context.textAlign = "left";
245
+ const placed = placeChunks(lines, isRTL, paddingTop, paddingLeft);
246
+ drawShadows(context, placed);
247
+ drawStrokes(context, placed);
248
+ drawFills(context, placed);
83
249
  }
@@ -1,27 +1,29 @@
1
- import type { WebGLRenderer } from "three";
1
+ import type { FerrsignView1 } from "ferrsign";
2
2
  import type { UILayer } from "../../layers/UILayer/UILayer.js";
3
- import type { UIColorConfig } from "../../miscellaneous/color/UIColor.Internal.js";
4
- import { UIColor } from "../../miscellaneous/color/UIColor.js";
3
+ import type { UICanvasRenderingContext2D } from "../../miscellaneous/canvas.js";
5
4
  import type { UIInsetsConfig } from "../../miscellaneous/insets/UIInsets.Internal.js";
6
5
  import { UIInsets } from "../../miscellaneous/insets/UIInsets.js";
6
+ import type { UIContentAspectElement } from "../../miscellaneous/shared.js";
7
7
  import { UITextSpan } from "../../miscellaneous/text-span/UITextSpan.js";
8
8
  import { UITextStyle } from "../../miscellaneous/text-style/UITextStyle.js";
9
- import { UIElement } from "../UIElement/UIElement.js";
9
+ import { UICanvasElement } from "../UICanvasElement/UICanvasElement.js";
10
10
  import type { UITextContent } from "./UIText.Interfaces.js";
11
- import type { UITextOptions } from "./UIText.Internal.js";
11
+ import type { UITextLineBox, UITextOptions } from "./UIText.Internal.js";
12
12
  import { UITextDirection, UITextResizeMode } from "./UIText.Internal.js";
13
- /** Canvas-based text rendering element */
14
- export declare class UIText extends UIElement {
13
+ /**
14
+ * Text element. The text is laid out in pixels of its style, which are the content units of the
15
+ * canvas it is drawn on.
16
+ */
17
+ export declare class UIText extends UICanvasElement implements UIContentAspectElement {
15
18
  /** Default style applied to all text spans */
16
19
  readonly commonStyle: UITextStyle;
17
- private readonly colorInternal;
20
+ /** Size of the laid out text with its padding, in pixels of the style */
21
+ protected contentWidth: number;
22
+ protected contentHeight: number;
18
23
  private readonly paddingInternal;
19
24
  private autoPaddingInternal;
20
- /** Padding last set by auto padding; any other value means it was changed by hand */
21
- private lastAutoPadding;
22
- private readonly canvas;
23
- private readonly context;
24
- private texture;
25
+ /** Version of the padding as auto padding last set it. Any other version is a change by hand. */
26
+ private autoPaddingVersion;
25
27
  private contentInternal;
26
28
  private contentDirty;
27
29
  private maxLineWidthInternal;
@@ -31,9 +33,14 @@ export declare class UIText extends UIElement {
31
33
  private directionInternal;
32
34
  private directionDirty;
33
35
  private isRTL;
36
+ private lineBoxInternal;
37
+ private lineBoxDirty;
34
38
  private lastWidth;
35
39
  private lastHeight;
36
40
  private textChunks;
41
+ private textLines;
42
+ private contentAspectInternal;
43
+ private readonly signalContentAspectChangedInternal;
37
44
  /**
38
45
  * Creates a new UIText instance.
39
46
  *
@@ -42,16 +49,14 @@ export declare class UIText extends UIElement {
42
49
  * @param options - Configuration options
43
50
  */
44
51
  constructor(layer: UILayer, content: UITextContent, options?: Partial<UITextOptions>);
45
- /** Multiplicative tint. Alpha channel controls opacity. */
46
- get color(): UIColor;
47
52
  /** Text content as array of styled spans */
48
53
  get content(): UITextSpan[];
49
54
  /** Maximum line width before wrapping in pixels */
50
55
  get maxLineWidth(): number;
51
- /** Text padding in world units */
56
+ /** Text padding in pixels of the style */
52
57
  get padding(): UIInsets;
53
58
  /**
54
- * Whether the padding follows the stroke and shadow of the styles. On by default, off once
59
+ * Whether the padding follows the outlines and shadows of the styles. On by default, off once
55
60
  * the padding is set by hand.
56
61
  */
57
62
  get autoPadding(): boolean;
@@ -59,8 +64,15 @@ export declare class UIText extends UIElement {
59
64
  get resizeMode(): UITextResizeMode;
60
65
  /** Base direction used for bidirectional text reordering */
61
66
  get direction(): UITextDirection;
62
- /** Multiplicative tint. Alpha channel controls opacity. */
63
- set color(value: UIColorConfig);
67
+ /** What sets the height of a line */
68
+ get lineBox(): UITextLineBox;
69
+ /**
70
+ * Aspect of the text laid out at `maxLineWidth` with its padding. A `UIAspectConstraint`
71
+ * created without an aspect follows it as the text changes.
72
+ */
73
+ get contentAspect(): number;
74
+ /** Signal fired with the new aspect when the content aspect changes */
75
+ get signalContentAspectChanged(): FerrsignView1<number>;
64
76
  /**
65
77
  * Text content as string, span config, or array. Text without visible characters keeps the
66
78
  * element at least 1 world unit wide and high.
@@ -68,10 +80,10 @@ export declare class UIText extends UIElement {
68
80
  set content(value: UITextContent);
69
81
  /** Maximum line width before wrapping in pixels */
70
82
  set maxLineWidth(value: number);
71
- /** Text padding in world units. Turns auto padding off. */
83
+ /** Text padding in pixels of the style. Turns auto padding off. */
72
84
  set padding(value: UIInsetsConfig);
73
85
  /**
74
- * Whether the padding follows the stroke and shadow of the styles. On by default, off once
86
+ * Whether the padding follows the outlines and shadows of the styles. On by default, off once
75
87
  * the padding is set by hand.
76
88
  */
77
89
  set autoPadding(value: boolean);
@@ -79,17 +91,35 @@ export declare class UIText extends UIElement {
79
91
  set resizeMode(value: UITextResizeMode);
80
92
  /** Base direction used for bidirectional text reordering */
81
93
  set direction(value: UITextDirection);
82
- /** Frees resources of the text */
83
- protected destroyInternal(): void;
84
- protected onWillRender(renderer: WebGLRenderer, deltaTime: number): void;
85
- private tryToRenderTextScaleMode;
86
- private tryToRenderTextBreakMode;
94
+ /** What sets the height of a line */
95
+ set lineBox(value: UITextLineBox);
96
+ protected updateContent(layoutChanged: boolean): boolean;
97
+ protected drawContent(context: UICanvasRenderingContext2D): void;
98
+ /**
99
+ * Lays the text out again when anything it depends on changed.
100
+ * @param layoutChanged Whether the solver changed the layout since the last call
101
+ * @returns True when the text was laid out again
102
+ */
103
+ private layout;
104
+ private layoutScaleMode;
105
+ private layoutBreakMode;
106
+ /**
107
+ * Reports a new content aspect. Called before the element suggests its size, so constraints
108
+ * following the aspect are updated by then.
109
+ * @param aspect Aspect of the text laid out at `maxLineWidth`
110
+ */
111
+ private updateContentAspect;
112
+ /**
113
+ * Keeps the result of a layout.
114
+ * @param lines Laid out lines
115
+ * @param width Width of the content with its padding in pixels
116
+ * @param height Height of the content with its padding in pixels
117
+ */
118
+ private setContent;
87
119
  /** The element size for a text size: the padding added, at least the minimum each way */
88
120
  private elementSize;
89
- private updateProperties;
90
121
  private updateAutoPadding;
91
122
  private buildTextChunks;
92
- private consumeDimensionsChange;
93
123
  private setDimensionsDirtyFalse;
94
124
  private checkContentDirty;
95
125
  private setContentDirtyFalse;