laymur 0.21.1 → 0.23.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 (29) hide show
  1. package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.js +2 -2
  2. package/dist/core/elements/UIElement/UIElement.d.ts +14 -6
  3. package/dist/core/elements/UIElement/UIElement.js +22 -12
  4. package/dist/core/elements/UIImage/UIImage.js +2 -2
  5. package/dist/core/elements/UINineSlice/UINineSlice.js +2 -2
  6. package/dist/core/elements/UIProgress/UIProgress.d.ts +2 -0
  7. package/dist/core/elements/UIProgress/UIProgress.js +6 -1
  8. package/dist/core/elements/UIText/UIText.Bidi.d.ts +25 -0
  9. package/dist/core/elements/UIText/UIText.Bidi.js +133 -0
  10. package/dist/core/elements/UIText/UIText.Interfaces.d.ts +2 -0
  11. package/dist/core/elements/UIText/UIText.Internal.d.ts +12 -0
  12. package/dist/core/elements/UIText/UIText.Internal.js +12 -1
  13. package/dist/core/elements/UIText/UIText.Measuring.d.ts +10 -1
  14. package/dist/core/elements/UIText/UIText.Measuring.js +60 -43
  15. package/dist/core/elements/UIText/UIText.Rendering.d.ts +3 -1
  16. package/dist/core/elements/UIText/UIText.Rendering.js +30 -5
  17. package/dist/core/elements/UIText/UIText.d.ts +8 -2
  18. package/dist/core/elements/UIText/UIText.js +39 -14
  19. package/dist/core/index.d.ts +1 -1
  20. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionDirectional.js +1 -1
  21. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.d.ts +4 -2
  22. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.js +1 -1
  23. package/dist/core/miscellaneous/text-style/UITextStyle.d.ts +3 -3
  24. package/dist/core/miscellaneous/texture/UITextureView.d.ts +2 -2
  25. package/dist/core/miscellaneous/texture/UITextureView.js +4 -4
  26. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +7 -0
  27. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +25 -7
  28. package/dist/index.js +1 -1
  29. package/package.json +1 -1
@@ -246,12 +246,12 @@ class UIAnimatedImage extends UIElement {
246
246
  }
247
247
  _private_subscribeSequenceEvents() {
248
248
  for (const frame of this._private_sequenceInternal) {
249
- frame.signalDiminsionsChanged.on(this._private_onTextureDimensionsChanged);
249
+ frame.signalDimensionsChanged.on(this._private_onTextureDimensionsChanged);
250
250
  }
251
251
  }
252
252
  _private_unsubscribeSequenceEvents() {
253
253
  for (const frame of this._private_sequenceInternal) {
254
- frame.signalDiminsionsChanged.off(this._private_onTextureDimensionsChanged);
254
+ frame.signalDimensionsChanged.off(this._private_onTextureDimensionsChanged);
255
255
  }
256
256
  }
257
257
  }
@@ -37,22 +37,30 @@ export declare abstract class UIElement extends UIDummy {
37
37
  get transparencyMode(): UITransparencyMode;
38
38
  /** Color blending mode (only affects BLEND transparency) */
39
39
  get blendMode(): UIBlendMode;
40
+ /** Hue shift in degrees */
41
+ get hue(): number;
42
+ /** Saturation multiplier (1 = unchanged, 0 = grayscale) */
43
+ get saturation(): number;
44
+ /** Lightness offset (0 = unchanged) */
45
+ get lightness(): number;
46
+ /**
47
+ * Controls visibility and interactivity.
48
+ *
49
+ * Redeclared here because declaring only a setter would shadow the accessor
50
+ * pair inherited from {@link UIDummy}, making reads return undefined.
51
+ */
52
+ get mode(): UIMode;
40
53
  /** Alpha blending mode */
41
54
  set transparencyMode(value: UITransparencyMode);
42
55
  /** Color blending mode (only affects BLEND transparency) */
43
56
  set blendMode(value: UIBlendMode);
44
57
  /** Hue shift in degrees */
45
- get hue(): number;
46
- /** Hue shift in degrees */
47
58
  set hue(value: number);
48
59
  /** Saturation multiplier (1 = unchanged, 0 = grayscale) */
49
- get saturation(): number;
50
- /** Saturation multiplier (1 = unchanged, 0 = grayscale) */
51
60
  set saturation(value: number);
52
61
  /** Lightness offset (0 = unchanged) */
53
- get lightness(): number;
54
- /** Lightness offset (0 = unchanged) */
55
62
  set lightness(value: number);
63
+ /** Controls visibility and interactivity */
56
64
  set mode(value: UIMode);
57
65
  /** Removes element and frees resources */
58
66
  destroy(): void;
@@ -43,6 +43,27 @@ class UIElement extends UIDummy {
43
43
  get blendMode() {
44
44
  return this._private_blendModeInternal;
45
45
  }
46
+ /** Hue shift in degrees */
47
+ get hue() {
48
+ return this._private_hueInternal;
49
+ }
50
+ /** Saturation multiplier (1 = unchanged, 0 = grayscale) */
51
+ get saturation() {
52
+ return this._private_saturationInternal;
53
+ }
54
+ /** Lightness offset (0 = unchanged) */
55
+ get lightness() {
56
+ return this._private_lightnessInternal;
57
+ }
58
+ /**
59
+ * Controls visibility and interactivity.
60
+ *
61
+ * Redeclared here because declaring only a setter would shadow the accessor
62
+ * pair inherited from {@link UIDummy}, making reads return undefined.
63
+ */
64
+ get mode() {
65
+ return this.modeInternal;
66
+ }
46
67
  /** Alpha blending mode */
47
68
  set transparencyMode(value) {
48
69
  if (this._private_transparencyModeInternal !== value) {
@@ -58,10 +79,6 @@ class UIElement extends UIDummy {
58
79
  }
59
80
  }
60
81
  /** Hue shift in degrees */
61
- get hue() {
62
- return this._private_hueInternal;
63
- }
64
- /** Hue shift in degrees */
65
82
  set hue(value) {
66
83
  if (this._private_hueInternal !== value) {
67
84
  this._private_hueInternal = value;
@@ -69,10 +86,6 @@ class UIElement extends UIDummy {
69
86
  }
70
87
  }
71
88
  /** Saturation multiplier (1 = unchanged, 0 = grayscale) */
72
- get saturation() {
73
- return this._private_saturationInternal;
74
- }
75
- /** Saturation multiplier (1 = unchanged, 0 = grayscale) */
76
89
  set saturation(value) {
77
90
  if (this._private_saturationInternal !== value) {
78
91
  this._private_saturationInternal = value;
@@ -80,16 +93,13 @@ class UIElement extends UIDummy {
80
93
  }
81
94
  }
82
95
  /** Lightness offset (0 = unchanged) */
83
- get lightness() {
84
- return this._private_lightnessInternal;
85
- }
86
- /** Lightness offset (0 = unchanged) */
87
96
  set lightness(value) {
88
97
  if (this._private_lightnessInternal !== value) {
89
98
  this._private_lightnessInternal = value;
90
99
  this._private_hslDirty = true;
91
100
  }
92
101
  }
102
+ /** Controls visibility and interactivity */
93
103
  set mode(value) {
94
104
  if (this.modeInternal !== value) {
95
105
  this._private_visibilityDirty = isUIModeVisible(this.modeInternal) !== isUIModeVisible(value);
@@ -33,7 +33,7 @@ class UIImage extends UIElement {
33
33
  this.texture = textureView;
34
34
  this._private_textureTransform = textureTransform;
35
35
  this._private_colorInternal = color;
36
- this.texture.signalDiminsionsChanged.on(this._private_onTextureDimensionsChanged);
36
+ this.texture.signalDimensionsChanged.on(this._private_onTextureDimensionsChanged);
37
37
  }
38
38
  /** Multiplicative tint. Alpha channel controls opacity. */
39
39
  get color() {
@@ -45,7 +45,7 @@ class UIImage extends UIElement {
45
45
  }
46
46
  /** Removes image and frees resources */
47
47
  destroy() {
48
- this.texture.signalDiminsionsChanged.off(this._private_onTextureDimensionsChanged);
48
+ this.texture.signalDimensionsChanged.off(this._private_onTextureDimensionsChanged);
49
49
  super.destroy();
50
50
  }
51
51
  setPlaneTransform() {
@@ -53,7 +53,7 @@ class UINineSlice extends UIElement {
53
53
  this._private_sliceRegionsVector = sliceRegionsVector;
54
54
  this._private_lastElementWidth = this.width;
55
55
  this._private_lastElementHeight = this.height;
56
- this.texture.signalDiminsionsChanged.on(this._private_onTextureDimensionsChanged);
56
+ this.texture.signalDimensionsChanged.on(this._private_onTextureDimensionsChanged);
57
57
  }
58
58
  /** Multiplicative tint. Alpha channel controls opacity. */
59
59
  get color() {
@@ -92,7 +92,7 @@ class UINineSlice extends UIElement {
92
92
  }
93
93
  /** Removes nine slice and frees resources */
94
94
  destroy() {
95
- this.texture.signalDiminsionsChanged.off(this._private_onTextureDimensionsChanged);
95
+ this.texture.signalDimensionsChanged.off(this._private_onTextureDimensionsChanged);
96
96
  super.destroy();
97
97
  }
98
98
  setPlaneTransform() {
@@ -39,6 +39,8 @@ export declare class UIProgress extends UIElement {
39
39
  set maskFunction(value: UIProgressMaskFunction);
40
40
  /** Progress from 0 (empty) to 1 (full). Clamped. */
41
41
  set progress(value: number);
42
+ /** Removes element and frees resources */
43
+ destroy(): void;
42
44
  protected setPlaneTransform(): void;
43
45
  private readonly onTextureDimensionsChanged;
44
46
  private rebuildPlane;
@@ -56,7 +56,7 @@ class UIProgress extends UIElement {
56
56
  this._private_colorInternal = color;
57
57
  this._private_progressInternal = progress;
58
58
  this._private_maskFunctionInternal = maskFunction;
59
- this.texture.signalDiminsionsChanged.on(this._private_onTextureDimensionsChanged);
59
+ this.texture.signalDimensionsChanged.on(this._private_onTextureDimensionsChanged);
60
60
  }
61
61
  /** Multiplicative tint. Alpha channel controls opacity. */
62
62
  get color() {
@@ -90,6 +90,11 @@ class UIProgress extends UIElement {
90
90
  this._private_progressDirty = true;
91
91
  }
92
92
  }
93
+ /** Removes element and frees resources */
94
+ destroy() {
95
+ this.texture.signalDimensionsChanged.off(this._private_onTextureDimensionsChanged);
96
+ super.destroy();
97
+ }
93
98
  setPlaneTransform() {
94
99
  let properties;
95
100
  if (this._private_colorInternal.dirty) {
@@ -0,0 +1,25 @@
1
+ import type { UITextSpan } from "../../miscellaneous/text-span/UITextSpan";
2
+ import type { UITextChunk } from "./UIText.Interfaces";
3
+ /**
4
+ * Detects the directional class used to order a chunk among its neighbours.
5
+ * Digit only chunks are ordered left to right in both paragraph directions.
6
+ * @param text Chunk text
7
+ * @returns Directional class of the chunk
8
+ */
9
+ export declare function detectBidiClass(text: string): number;
10
+ /**
11
+ * Detects the paragraph direction of the content.
12
+ * Implements a simplified UAX#9 P2/P3: the first strong character of the content wins.
13
+ * @param textSpans Text spans in logical order
14
+ * @returns Whether the paragraph reads right to left
15
+ */
16
+ export declare function detectIsRTL(textSpans: UITextSpan[]): boolean;
17
+ /**
18
+ * Reorders chunks from logical order into visual left to right order.
19
+ * Implements UAX#9 L2 at chunk granularity with simplified neutral resolution (N1/N2).
20
+ * Shaping and bidi inside a single chunk are handled by the canvas itself.
21
+ * @param textChunks Chunks of a single line in logical order
22
+ * @param isRTL Whether the paragraph reads right to left
23
+ * @returns Chunks in visual order, or the same array when reordering is not needed
24
+ */
25
+ export declare function reorderChunksVisually(textChunks: UITextChunk[], isRTL: boolean): UITextChunk[];
@@ -0,0 +1,133 @@
1
+ /** No strong directional character */
2
+ const BIDI_NEUTRAL = 0;
3
+ /** Resolves to left to right */
4
+ const BIDI_LTR = 1;
5
+ /** Resolves to right to left */
6
+ const BIDI_RTL = 2;
7
+ /** Hebrew, Arabic, Syriac, Thaana, NKo, Arabic Extended and presentation forms */
8
+ const RTL_PATTERN = /[\u0591-\u07FF\u0860-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/;
9
+ /** Latin, Greek, Cyrillic, Armenian, Indic, South East Asian, CJK, Hangul */
10
+ const LTR_PATTERN = /[A-Za-z\u00C0-\u058F\u0900-\u1FFF\u2C00-\uD7FF]/;
11
+ /** European digits, laid out left to right inside any paragraph */
12
+ const NUMBER_PATTERN = /[0-9]/;
13
+ /**
14
+ * Detects the strong directional class of a text.
15
+ * Implements a simplified UAX#9 P2: the first strong character wins.
16
+ * @param text Text to inspect
17
+ * @returns Directional class, neutral when no strong character is present
18
+ */
19
+ function detectStrongClass(text) {
20
+ const rtlMatch = RTL_PATTERN.exec(text);
21
+ const ltrMatch = LTR_PATTERN.exec(text);
22
+ if (rtlMatch === null) {
23
+ return ltrMatch === null ? BIDI_NEUTRAL : BIDI_LTR;
24
+ }
25
+ if (ltrMatch === null) {
26
+ return BIDI_RTL;
27
+ }
28
+ return rtlMatch.index < ltrMatch.index ? BIDI_RTL : BIDI_LTR;
29
+ }
30
+ /**
31
+ * Finds the directional class of the closest non neutral chunk.
32
+ * @param textChunks Chunks of a single line
33
+ * @param index Index to start from
34
+ * @param step Search direction
35
+ * @param fallback Class returned when no strong chunk is found
36
+ * @returns Directional class of the closest strong chunk
37
+ */
38
+ function findStrongNeighbourClass(textChunks, index, step, fallback) {
39
+ for (let i = index; i >= 0 && i < textChunks.length; i += step) {
40
+ if (textChunks[i].bidiClass !== BIDI_NEUTRAL) {
41
+ return textChunks[i].bidiClass;
42
+ }
43
+ }
44
+ return fallback;
45
+ }
46
+ /**
47
+ * Reverses a closed range of chunks in place.
48
+ * @param textChunks Chunks to modify
49
+ * @param from First index of the range
50
+ * @param to Last index of the range
51
+ */
52
+ function reverseChunkRange(textChunks, from, to) {
53
+ while (from < to) {
54
+ const buffer = textChunks[from];
55
+ textChunks[from] = textChunks[to];
56
+ textChunks[to] = buffer;
57
+ from++;
58
+ to--;
59
+ }
60
+ }
61
+ /**
62
+ * Detects the directional class used to order a chunk among its neighbours.
63
+ * Digit only chunks are ordered left to right in both paragraph directions.
64
+ * @param text Chunk text
65
+ * @returns Directional class of the chunk
66
+ */
67
+ function detectBidiClass(text) {
68
+ const strongClass = detectStrongClass(text);
69
+ if (strongClass !== BIDI_NEUTRAL) {
70
+ return strongClass;
71
+ }
72
+ return NUMBER_PATTERN.test(text) ? BIDI_LTR : BIDI_NEUTRAL;
73
+ }
74
+ /**
75
+ * Detects the paragraph direction of the content.
76
+ * Implements a simplified UAX#9 P2/P3: the first strong character of the content wins.
77
+ * @param textSpans Text spans in logical order
78
+ * @returns Whether the paragraph reads right to left
79
+ */
80
+ function detectIsRTL(textSpans) {
81
+ for (const textSpan of textSpans) {
82
+ const strongClass = detectStrongClass(textSpan.text);
83
+ if (strongClass !== BIDI_NEUTRAL) {
84
+ return strongClass === BIDI_RTL;
85
+ }
86
+ }
87
+ return false;
88
+ }
89
+ /**
90
+ * Reorders chunks from logical order into visual left to right order.
91
+ * Implements UAX#9 L2 at chunk granularity with simplified neutral resolution (N1/N2).
92
+ * Shaping and bidi inside a single chunk are handled by the canvas itself.
93
+ * @param textChunks Chunks of a single line in logical order
94
+ * @param isRTL Whether the paragraph reads right to left
95
+ * @returns Chunks in visual order, or the same array when reordering is not needed
96
+ */
97
+ function reorderChunksVisually(textChunks, isRTL) {
98
+ if (!isRTL && !textChunks.some((textChunk) => textChunk.bidiClass === BIDI_RTL)) {
99
+ return textChunks;
100
+ }
101
+ const baseClass = isRTL ? BIDI_RTL : BIDI_LTR;
102
+ const levels = [];
103
+ let maxLevel = 0;
104
+ for (let i = 0; i < textChunks.length; i++) {
105
+ let bidiClass = textChunks[i].bidiClass;
106
+ if (bidiClass === BIDI_NEUTRAL) {
107
+ const beforeClass = findStrongNeighbourClass(textChunks, i - 1, -1, baseClass);
108
+ const afterClass = findStrongNeighbourClass(textChunks, i + 1, 1, baseClass);
109
+ bidiClass = beforeClass === afterClass ? beforeClass : baseClass;
110
+ }
111
+ const level = bidiClass === BIDI_RTL ? 1 : isRTL ? 2 : 0;
112
+ levels.push(level);
113
+ maxLevel = Math.max(maxLevel, level);
114
+ }
115
+ const result = textChunks.slice();
116
+ for (let level = maxLevel; level >= 1; level--) {
117
+ let start = -1;
118
+ for (let i = 0; i <= levels.length; i++) {
119
+ if (i < levels.length && levels[i] >= level) {
120
+ if (start === -1) {
121
+ start = i;
122
+ }
123
+ }
124
+ else if (start !== -1) {
125
+ reverseChunkRange(result, start, i - 1);
126
+ start = -1;
127
+ }
128
+ }
129
+ }
130
+ return result;
131
+ }
132
+
133
+ export { detectBidiClass, detectIsRTL, reorderChunksVisually };
@@ -24,6 +24,8 @@ export interface UITextChunk {
24
24
  style: UITextStyleConfig;
25
25
  /** Chunk metrics */
26
26
  metrics: UIChunkMetrics;
27
+ /** Resolved bidirectional class used for visual reordering */
28
+ bidiClass: number;
27
29
  }
28
30
  /** Line of text chunks */
29
31
  export interface UITextLine extends UIChunkMetrics {
@@ -2,6 +2,15 @@ import type { UIColorConfig } from "../../miscellaneous/color/UIColor.Internal";
2
2
  import type { UIInsetsConfig } from "../../miscellaneous/insets/UIInsets.Internal";
3
3
  import type { UITextStyleConfig } from "../../miscellaneous/text-style/UITextStyle.Internal";
4
4
  import type { UIElementOptions } from "../UIElement/UIElement.Internal";
5
+ /** Base direction used for bidirectional text reordering */
6
+ export declare enum UITextDirection {
7
+ /** Detect from the first strong directional character of the content */
8
+ AUTO = 0,
9
+ /** Force left to right */
10
+ LTR = 1,
11
+ /** Force right to left */
12
+ RTL = 2
13
+ }
5
14
  /** Controls how text adapts to element size */
6
15
  export declare enum UITextResizeMode {
7
16
  /** Break text into multiple lines when narrower than content*/
@@ -21,6 +30,9 @@ export interface UITextOptions extends UIElementOptions {
21
30
  resizeMode: UITextResizeMode;
22
31
  /** Maximum line width before wrapping in pixels */
23
32
  maxLineWidth: number;
33
+ /** Base direction used for bidirectional text reordering */
34
+ direction: UITextDirection;
24
35
  }
25
36
  export declare const TEXT_DEFAULT_MAX_LINE_WIDTH = 1024;
26
37
  export declare const TEXT_DEFAULT_RESIZE_MODE = UITextResizeMode.SCALE;
38
+ export declare const TEXT_DEFAULT_DIRECTION = UITextDirection.AUTO;
@@ -1,3 +1,13 @@
1
+ /** Base direction used for bidirectional text reordering */
2
+ var UITextDirection;
3
+ (function (UITextDirection) {
4
+ /** Detect from the first strong directional character of the content */
5
+ UITextDirection[UITextDirection["AUTO"] = 0] = "AUTO";
6
+ /** Force left to right */
7
+ UITextDirection[UITextDirection["LTR"] = 1] = "LTR";
8
+ /** Force right to left */
9
+ UITextDirection[UITextDirection["RTL"] = 2] = "RTL";
10
+ })(UITextDirection || (UITextDirection = {}));
1
11
  /** Controls how text adapts to element size */
2
12
  var UITextResizeMode;
3
13
  (function (UITextResizeMode) {
@@ -8,5 +18,6 @@ var UITextResizeMode;
8
18
  })(UITextResizeMode || (UITextResizeMode = {}));
9
19
  const TEXT_DEFAULT_MAX_LINE_WIDTH = 1024;
10
20
  const TEXT_DEFAULT_RESIZE_MODE = UITextResizeMode.SCALE;
21
+ const TEXT_DEFAULT_DIRECTION = UITextDirection.AUTO;
11
22
 
12
- export { TEXT_DEFAULT_MAX_LINE_WIDTH, TEXT_DEFAULT_RESIZE_MODE, UITextResizeMode };
23
+ export { TEXT_DEFAULT_DIRECTION, TEXT_DEFAULT_MAX_LINE_WIDTH, TEXT_DEFAULT_RESIZE_MODE, UITextDirection, UITextResizeMode };
@@ -3,6 +3,13 @@ import type { UITextSpan } from "../../miscellaneous/text-span/UITextSpan";
3
3
  import { UITextStyle } from "../../miscellaneous/text-style/UITextStyle";
4
4
  import type { UITextStyleConfig } from "../../miscellaneous/text-style/UITextStyle.Internal";
5
5
  import type { UIChunkMetrics, UITextChunk, UITextLine, UITextSize } from "./UIText.Interfaces";
6
+ /**
7
+ * Applies font of a resolved style to a canvas context.
8
+ * Shared by measuring and rendering so both stay in sync.
9
+ * @param context Canvas context to configure
10
+ * @param textStyle Resolved text style
11
+ */
12
+ export declare function applyFontStyle(context: UICanvasRenderingContext2D, textStyle: UITextStyleConfig): void;
6
13
  /**
7
14
  * Splits text into renderable chunks.
8
15
  * @param text Text to split
@@ -26,6 +33,7 @@ export declare function measureTextChunk(context: UICanvasRenderingContext2D, te
26
33
  export declare function buildTextLines(maxWidth: number, textChunks: UITextChunk[]): UITextLine[];
27
34
  /**
28
35
  * Calculates total text size from lines.
36
+ * The box spans from the ascent of the first line to the descent of the last one.
29
37
  * @param lines Text lines
30
38
  * @returns Total width and height
31
39
  */
@@ -35,9 +43,10 @@ export declare function calculateTextSize(lines: UITextLine[]): UITextSize;
35
43
  * @param context Canvas context for measurement
36
44
  * @param textSpans Text spans with styles
37
45
  * @param commonStyle Fallback style
46
+ * @param isRTL Whether the paragraph reads right to left
38
47
  * @returns Measured text chunks
39
48
  */
40
- export declare function buildTextChunks(context: UICanvasRenderingContext2D, textSpans: UITextSpan[], commonStyle: UITextStyle): UITextChunk[];
49
+ export declare function buildTextChunks(context: UICanvasRenderingContext2D, textSpans: UITextSpan[], commonStyle: UITextStyle, isRTL: boolean): UITextChunk[];
41
50
  /**
42
51
  * Calculates text layout parameters.
43
52
  * @param textChunks Measured text chunks
@@ -1,5 +1,15 @@
1
1
  import { UITextStyle } from '../../miscellaneous/text-style/UITextStyle.js';
2
+ import { detectBidiClass } from './UIText.Bidi.js';
2
3
 
4
+ /**
5
+ * Applies font of a resolved style to a canvas context.
6
+ * Shared by measuring and rendering so both stay in sync.
7
+ * @param context Canvas context to configure
8
+ * @param textStyle Resolved text style
9
+ */
10
+ function applyFontStyle(context, textStyle) {
11
+ context.font = `${textStyle.fontStyle} ${textStyle.fontWeight} ${textStyle.fontSize}px "${textStyle.fontFamily}"`;
12
+ }
3
13
  /**
4
14
  * Splits text into renderable chunks.
5
15
  * @param text Text to split
@@ -16,25 +26,44 @@ function splitTextIntoChunks(text) {
16
26
  * @returns Chunk metrics
17
27
  */
18
28
  function measureTextChunk(context, text, textStyle) {
19
- context.font = `${textStyle.fontStyle} ${textStyle.fontWeight} ${textStyle.fontSize}px "${textStyle.fontFamily}"`;
29
+ applyFontStyle(context, textStyle);
20
30
  if (text === "\n") {
21
31
  return {
22
- width: Math.ceil(1),
32
+ width: 1,
23
33
  height: textStyle.lineHeight,
24
34
  baseline: 0,
25
35
  lineHeight: textStyle.lineHeight,
26
36
  };
27
37
  }
28
38
  const metrics = context.measureText(text);
29
- const width = Math.ceil(metrics.width);
30
- const height = Math.ceil(metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent);
39
+ const ascent = Math.ceil(metrics.actualBoundingBoxAscent);
40
+ const descent = Math.ceil(metrics.actualBoundingBoxDescent);
31
41
  return {
32
- width,
33
- height,
34
- baseline: metrics.actualBoundingBoxDescent,
42
+ width: Math.ceil(metrics.width),
43
+ height: ascent + descent,
44
+ baseline: ascent,
35
45
  lineHeight: textStyle.lineHeight,
36
46
  };
37
47
  }
48
+ /**
49
+ * Appends a line built from accumulated metrics.
50
+ * Ascent and descent are separate because the tallest and deepest chunks may differ.
51
+ * @param lines Lines to append to
52
+ * @param chunks Chunks of the line
53
+ * @param width Line width in pixels
54
+ * @param ascent Maximum chunk ascent in pixels
55
+ * @param descent Maximum chunk descent in pixels
56
+ * @param lineHeight Maximum chunk line height in pixels
57
+ */
58
+ function pushTextLine(lines, chunks, width, ascent, descent, lineHeight) {
59
+ lines.push({
60
+ chunks,
61
+ width,
62
+ height: ascent + descent,
63
+ baseline: ascent,
64
+ lineHeight,
65
+ });
66
+ }
38
67
  /**
39
68
  * Builds text lines with word wrapping.
40
69
  * @param maxWidth Maximum line width in pixels
@@ -45,62 +74,47 @@ function buildTextLines(maxWidth, textChunks) {
45
74
  const lines = [];
46
75
  let currentLine = [];
47
76
  let currentWidth = 0;
48
- let currentHeight = 0;
49
- let currentBaseline = 0;
77
+ let currentAscent = 0;
78
+ let currentDescent = 0;
50
79
  let currentLineHeight = 0;
51
80
  for (const textChunk of textChunks) {
81
+ const ascent = textChunk.metrics.baseline;
82
+ const descent = textChunk.metrics.height - textChunk.metrics.baseline;
52
83
  if (textChunk.text === "\n") {
53
84
  if (currentLine.length > 0) {
54
- lines.push({
55
- chunks: currentLine,
56
- width: currentWidth,
57
- height: currentHeight,
58
- baseline: currentBaseline,
59
- lineHeight: currentLineHeight,
60
- });
85
+ pushTextLine(lines, currentLine, currentWidth, currentAscent, currentDescent, currentLineHeight);
61
86
  }
62
87
  currentLine = [];
63
88
  currentWidth = 0;
64
- currentHeight = 0;
65
- currentBaseline = 0;
89
+ currentAscent = 0;
90
+ currentDescent = 0;
66
91
  currentLineHeight = 0;
67
92
  continue;
68
93
  }
69
94
  if (currentLine.length === 0 || currentWidth + textChunk.metrics.width <= maxWidth) {
70
95
  currentLine.push(textChunk);
71
96
  currentWidth += textChunk.metrics.width;
72
- currentHeight = Math.max(currentHeight, textChunk.metrics.height);
73
- currentBaseline = Math.max(currentBaseline, textChunk.metrics.baseline);
97
+ currentAscent = Math.max(currentAscent, ascent);
98
+ currentDescent = Math.max(currentDescent, descent);
74
99
  currentLineHeight = Math.max(currentLineHeight, textChunk.metrics.lineHeight);
75
100
  }
76
101
  else {
77
- lines.push({
78
- chunks: currentLine,
79
- width: currentWidth,
80
- height: currentHeight,
81
- baseline: currentBaseline,
82
- lineHeight: currentLineHeight,
83
- });
102
+ pushTextLine(lines, currentLine, currentWidth, currentAscent, currentDescent, currentLineHeight);
84
103
  currentLine = [textChunk];
85
104
  currentWidth = textChunk.metrics.width;
86
- currentHeight = textChunk.metrics.height;
87
- currentBaseline = textChunk.metrics.baseline;
105
+ currentAscent = ascent;
106
+ currentDescent = descent;
88
107
  currentLineHeight = textChunk.metrics.lineHeight;
89
108
  }
90
109
  }
91
110
  if (currentLine.length > 0) {
92
- lines.push({
93
- chunks: currentLine,
94
- width: currentWidth,
95
- height: currentHeight,
96
- baseline: currentBaseline,
97
- lineHeight: currentLineHeight,
98
- });
111
+ pushTextLine(lines, currentLine, currentWidth, currentAscent, currentDescent, currentLineHeight);
99
112
  }
100
113
  return lines;
101
114
  }
102
115
  /**
103
116
  * Calculates total text size from lines.
117
+ * The box spans from the ascent of the first line to the descent of the last one.
104
118
  * @param lines Text lines
105
119
  * @returns Total width and height
106
120
  */
@@ -109,21 +123,23 @@ function calculateTextSize(lines) {
109
123
  return { width: 0, height: 0 };
110
124
  }
111
125
  const maxLineWidth = Math.max(...lines.map((line) => line.width));
112
- let totalHeight = 0;
113
- for (let i = 0; i < lines.length; i++) {
114
- const line = lines[i];
115
- totalHeight += i === 0 ? line.height : line.lineHeight;
126
+ const lastLine = lines[lines.length - 1];
127
+ let totalHeight = lines[0].baseline;
128
+ for (let i = 1; i < lines.length; i++) {
129
+ totalHeight += lines[i].lineHeight;
116
130
  }
117
- return { width: maxLineWidth, height: totalHeight };
131
+ return { width: maxLineWidth, height: totalHeight + lastLine.height - lastLine.baseline };
118
132
  }
119
133
  /**
120
134
  * Builds measured text chunks from spans.
121
135
  * @param context Canvas context for measurement
122
136
  * @param textSpans Text spans with styles
123
137
  * @param commonStyle Fallback style
138
+ * @param isRTL Whether the paragraph reads right to left
124
139
  * @returns Measured text chunks
125
140
  */
126
- function buildTextChunks(context, textSpans, commonStyle) {
141
+ function buildTextChunks(context, textSpans, commonStyle, isRTL) {
142
+ context.direction = isRTL ? "rtl" : "ltr";
127
143
  const textChunks = [];
128
144
  for (const textSpan of textSpans) {
129
145
  const textStyle = UITextStyle.resolve(textSpan.style, commonStyle);
@@ -133,6 +149,7 @@ function buildTextChunks(context, textSpans, commonStyle) {
133
149
  text: textChunk,
134
150
  style: textStyle,
135
151
  metrics: textChunkMetrics,
152
+ bidiClass: detectBidiClass(textChunk),
136
153
  });
137
154
  }
138
155
  }
@@ -150,4 +167,4 @@ function calculateTextContentParameters(textChunks, maxLineWidth) {
150
167
  return { lines, size };
151
168
  }
152
169
 
153
- export { buildTextChunks, buildTextLines, calculateTextContentParameters, calculateTextSize, measureTextChunk, splitTextIntoChunks };
170
+ export { applyFontStyle, buildTextChunks, buildTextLines, calculateTextContentParameters, calculateTextSize, measureTextChunk, splitTextIntoChunks };
@@ -12,9 +12,11 @@ import type { UITextLine } from "./UIText.Interfaces";
12
12
  export declare function renderTextChunk(x: number, y: number, text: string, style: UITextStyleConfig, context: UICanvasRenderingContext2D): void;
13
13
  /**
14
14
  * Renders text lines to canvas.
15
+ * Chunks are laid out left to right after visual reordering, so x stays the left edge.
15
16
  * @param paddingTop Top padding in pixels
16
17
  * @param paddingLeft Left padding in pixels
17
18
  * @param lines Text lines to render
19
+ * @param isRTL Whether the paragraph reads right to left
18
20
  * @param context Canvas rendering context
19
21
  */
20
- export declare function renderTextLines(paddingTop: number, paddingLeft: number, lines: UITextLine[], context: UICanvasRenderingContext2D): void;
22
+ export declare function renderTextLines(paddingTop: number, paddingLeft: number, lines: UITextLine[], isRTL: boolean, context: UICanvasRenderingContext2D): void;
@@ -1,5 +1,22 @@
1
1
  import { TEXT_STYLE_DEFAULT_ALIGN } from '../../miscellaneous/text-style/UITextStyle.Internal.js';
2
+ import { reorderChunksVisually } from './UIText.Bidi.js';
3
+ import { applyFontStyle } from './UIText.Measuring.js';
2
4
 
5
+ /**
6
+ * Resolves logical alignment into a physical one.
7
+ * @param align Alignment to resolve
8
+ * @param isRTL Whether the paragraph reads right to left
9
+ * @returns Physical alignment
10
+ */
11
+ function resolveAlign(align, isRTL) {
12
+ if (align === "start") {
13
+ return isRTL ? "right" : "left";
14
+ }
15
+ if (align === "end") {
16
+ return isRTL ? "left" : "right";
17
+ }
18
+ return align;
19
+ }
3
20
  /**
4
21
  * Renders single text chunk.
5
22
  * @param x X coordinate in pixels
@@ -9,13 +26,16 @@ import { TEXT_STYLE_DEFAULT_ALIGN } from '../../miscellaneous/text-style/UITextS
9
26
  * @param context Canvas rendering context
10
27
  */
11
28
  function renderTextChunk(x, y, text, style, context) {
12
- context.font = `${style.fontStyle} ${style.fontWeight} ${style.fontSize}px "${style.fontFamily}"`;
29
+ applyFontStyle(context, style);
13
30
  if (style.enableShadow) {
14
31
  context.shadowOffsetX = style.shadowOffsetX;
15
32
  context.shadowOffsetY = style.shadowOffsetY;
16
33
  context.shadowBlur = style.shadowBlur;
17
34
  context.shadowColor = style.shadowColor;
18
35
  }
36
+ else {
37
+ context.shadowColor = "transparent";
38
+ }
19
39
  if (style.enableStroke) {
20
40
  context.strokeStyle = style.strokeColor;
21
41
  context.lineWidth = style.strokeThickness;
@@ -26,12 +46,17 @@ function renderTextChunk(x, y, text, style, context) {
26
46
  }
27
47
  /**
28
48
  * Renders text lines to canvas.
49
+ * Chunks are laid out left to right after visual reordering, so x stays the left edge.
29
50
  * @param paddingTop Top padding in pixels
30
51
  * @param paddingLeft Left padding in pixels
31
52
  * @param lines Text lines to render
53
+ * @param isRTL Whether the paragraph reads right to left
32
54
  * @param context Canvas rendering context
33
55
  */
34
- function renderTextLines(paddingTop, paddingLeft, lines, context) {
56
+ function renderTextLines(paddingTop, paddingLeft, lines, isRTL, context) {
57
+ context.clearRect(0, 0, context.canvas.width, context.canvas.height);
58
+ context.direction = isRTL ? "rtl" : "ltr";
59
+ context.textAlign = "left";
35
60
  let currentY = paddingTop;
36
61
  let textWidth = 0;
37
62
  for (const line of lines) {
@@ -41,7 +66,7 @@ function renderTextLines(paddingTop, paddingLeft, lines, context) {
41
66
  const line = lines[i];
42
67
  const firstAlign = line.chunks[0]?.style.align ?? TEXT_STYLE_DEFAULT_ALIGN;
43
68
  const isUniformAlign = line.chunks.every((c) => c.style.align === firstAlign);
44
- const lineAlign = isUniformAlign ? firstAlign : TEXT_STYLE_DEFAULT_ALIGN;
69
+ const lineAlign = resolveAlign(isUniformAlign ? firstAlign : TEXT_STYLE_DEFAULT_ALIGN, isRTL);
45
70
  let offsetX = 0;
46
71
  if (lineAlign === "center") {
47
72
  offsetX = (textWidth - line.width) / 2;
@@ -50,8 +75,8 @@ function renderTextLines(paddingTop, paddingLeft, lines, context) {
50
75
  offsetX = textWidth - line.width;
51
76
  }
52
77
  let currentX = paddingLeft + offsetX;
53
- currentY += i === 0 ? line.height : line.lineHeight;
54
- for (const chunk of line.chunks) {
78
+ currentY += i === 0 ? line.baseline : line.lineHeight;
79
+ for (const chunk of reorderChunksVisually(line.chunks, isRTL)) {
55
80
  renderTextChunk(currentX, currentY, chunk.text, chunk.style, context);
56
81
  currentX += chunk.metrics.width;
57
82
  }
@@ -9,7 +9,7 @@ import { UITextStyle } from "../../miscellaneous/text-style/UITextStyle";
9
9
  import { UIElement } from "../UIElement/UIElement";
10
10
  import type { UITextContent } from "./UIText.Interfaces";
11
11
  import type { UITextOptions } from "./UIText.Internal";
12
- import { UITextResizeMode } from "./UIText.Internal";
12
+ import { UITextDirection, UITextResizeMode } from "./UIText.Internal";
13
13
  /** Canvas-based text rendering element */
14
14
  export declare class UIText extends UIElement {
15
15
  /** Default style applied to all text spans */
@@ -25,10 +25,12 @@ export declare class UIText extends UIElement {
25
25
  private maxLineWidthDirty;
26
26
  private resizeModeInternal;
27
27
  private resizeModeDirty;
28
+ private directionInternal;
29
+ private directionDirty;
30
+ private isRTL;
28
31
  private lastWidth;
29
32
  private lastHeight;
30
33
  private textChunks;
31
- private textChunksDirty;
32
34
  /**
33
35
  * Creates a new UIText instance.
34
36
  *
@@ -47,6 +49,8 @@ export declare class UIText extends UIElement {
47
49
  get padding(): UIInsets;
48
50
  /** Controls how text adapts to element size */
49
51
  get resizeMode(): UITextResizeMode;
52
+ /** Base direction used for bidirectional text reordering */
53
+ get direction(): UITextDirection;
50
54
  /** Multiplicative tint. Alpha channel controls opacity. */
51
55
  set color(value: UIColorConfig);
52
56
  /** Text content as string, span config, or array */
@@ -57,6 +61,8 @@ export declare class UIText extends UIElement {
57
61
  set padding(value: UIInsetsConfig);
58
62
  /** Controls how text adapts to size constraints */
59
63
  set resizeMode(value: UITextResizeMode);
64
+ /** Base direction used for bidirectional text reordering */
65
+ set direction(value: UITextDirection);
60
66
  /** Removes element and frees resources */
61
67
  destroy(): void;
62
68
  protected onWillRender(renderer: WebGLRenderer, deltaTime: number): void;
@@ -6,7 +6,8 @@ import { UITextSpan } from '../../miscellaneous/text-span/UITextSpan.js';
6
6
  import { UITextStyle } from '../../miscellaneous/text-style/UITextStyle.js';
7
7
  import source from '../../shaders/UIImage.glsl.js';
8
8
  import { UIElement } from '../UIElement/UIElement.js';
9
- import { TEXT_DEFAULT_MAX_LINE_WIDTH, TEXT_DEFAULT_RESIZE_MODE, UITextResizeMode } from './UIText.Internal.js';
9
+ import { detectIsRTL } from './UIText.Bidi.js';
10
+ import { TEXT_DEFAULT_MAX_LINE_WIDTH, TEXT_DEFAULT_RESIZE_MODE, TEXT_DEFAULT_DIRECTION, UITextResizeMode, UITextDirection } from './UIText.Internal.js';
10
11
  import { calculateTextContentParameters, buildTextChunks } from './UIText.Measuring.js';
11
12
  import { renderTextLines } from './UIText.Rendering.js';
12
13
 
@@ -37,13 +38,15 @@ class UIText extends UIElement {
37
38
  this._private_contentDirty = false;
38
39
  this._private_maxLineWidthDirty = false;
39
40
  this._private_resizeModeDirty = false;
41
+ this._private_directionDirty = false;
42
+ this._private_isRTL = false;
40
43
  this._private_textChunks = [];
41
- this._private_textChunksDirty = false;
42
44
  this._private_colorInternal = color;
43
45
  this._private_paddingInternal = new UIInsets(options.padding);
44
46
  this.commonStyle = new UITextStyle(options.commonStyle);
45
47
  this._private_maxLineWidthInternal = options.maxLineWidth ?? TEXT_DEFAULT_MAX_LINE_WIDTH;
46
48
  this._private_resizeModeInternal = options.resizeMode ?? TEXT_DEFAULT_RESIZE_MODE;
49
+ this._private_directionInternal = options.direction ?? TEXT_DEFAULT_DIRECTION;
47
50
  this._private_canvas = canvas;
48
51
  this._private_context = context;
49
52
  this._private_texture = texture;
@@ -82,6 +85,10 @@ class UIText extends UIElement {
82
85
  get resizeMode() {
83
86
  return this._private_resizeModeInternal;
84
87
  }
88
+ /** Base direction used for bidirectional text reordering */
89
+ get direction() {
90
+ return this._private_directionInternal;
91
+ }
85
92
  /** Multiplicative tint. Alpha channel controls opacity. */
86
93
  set color(value) {
87
94
  this._private_colorInternal.set(value);
@@ -109,7 +116,6 @@ class UIText extends UIElement {
109
116
  ];
110
117
  }
111
118
  this._private_contentDirty = true;
112
- this._private_textChunksDirty = true;
113
119
  }
114
120
  }
115
121
  /** Maximum line width before wrapping in pixels */
@@ -130,6 +136,13 @@ class UIText extends UIElement {
130
136
  this._private_resizeModeDirty = true;
131
137
  }
132
138
  }
139
+ /** Base direction used for bidirectional text reordering */
140
+ set direction(value) {
141
+ if (this._private_directionInternal !== value) {
142
+ this._private_directionInternal = value;
143
+ this._private_directionDirty = true;
144
+ }
145
+ }
133
146
  /** Removes element and frees resources */
134
147
  destroy() {
135
148
  this._private_texture.dispose();
@@ -149,6 +162,7 @@ class UIText extends UIElement {
149
162
  _private_tryToRenderTextScaleMode() {
150
163
  if (!this._private_colorInternal.dirty &&
151
164
  !this._private_resizeModeDirty &&
165
+ !this._private_directionDirty &&
152
166
  !this._private_maxLineWidthDirty &&
153
167
  !this._private_paddingInternal.dirty &&
154
168
  !(this.solverWrapper.dirty &&
@@ -165,14 +179,17 @@ class UIText extends UIElement {
165
179
  this.width = desiredWidth;
166
180
  this.height = (desiredHeight / desiredWidth) * Math.round(this.width);
167
181
  this.width = (desiredWidth / desiredHeight) * Math.round(this.height);
168
- const resolutionDirty = this._private_lastWidth !== desiredHeight || this._private_lastHeight !== desiredWidth;
182
+ const safeWidth = Math.max(Math.round(desiredWidth), 2);
183
+ const safeHeight = Math.max(Math.round(desiredHeight), 2);
184
+ const resolutionDirty = this._private_canvas.width !== safeWidth || this._private_canvas.height !== safeHeight;
169
185
  if (resolutionDirty) {
170
- this._private_canvas.height = desiredHeight;
171
- this._private_canvas.width = desiredWidth;
186
+ this._private_canvas.width = safeWidth;
187
+ this._private_canvas.height = safeHeight;
172
188
  }
173
- renderTextLines(this._private_paddingInternal.top, this._private_paddingInternal.left, lines, this._private_context);
189
+ renderTextLines(this._private_paddingInternal.top, this._private_paddingInternal.left, lines, this._private_isRTL, this._private_context);
174
190
  this._private_updateProperties(resolutionDirty);
175
191
  this._private_resizeModeDirty = false;
192
+ this._private_directionDirty = false;
176
193
  this._private_maxLineWidthDirty = false;
177
194
  this._private_paddingInternal.setDirtyFalse();
178
195
  this._private_setDimensionsDirtyFalse();
@@ -181,6 +198,7 @@ class UIText extends UIElement {
181
198
  _private_tryToRenderTextBreakMode() {
182
199
  if (!this._private_colorInternal.dirty &&
183
200
  !this._private_resizeModeDirty &&
201
+ !this._private_directionDirty &&
184
202
  !this._private_maxLineWidthDirty &&
185
203
  !this._private_paddingInternal.dirty &&
186
204
  !this._private_checkDimensionsDirty() &&
@@ -198,14 +216,15 @@ class UIText extends UIElement {
198
216
  this.width = realTextSize.width + paddingH;
199
217
  const safeWidth = Math.max(Math.round(this.width), 2);
200
218
  const safeHeight = Math.max(Math.round(this.height), 2);
201
- const resolutionDirty = this._private_lastWidth !== safeWidth || this._private_lastHeight !== safeHeight;
219
+ const resolutionDirty = this._private_canvas.width !== safeWidth || this._private_canvas.height !== safeHeight;
202
220
  if (resolutionDirty) {
203
221
  this._private_canvas.width = safeWidth;
204
222
  this._private_canvas.height = safeHeight;
205
223
  }
206
- renderTextLines(this._private_paddingInternal.top, this._private_paddingInternal.left, realTextLines, this._private_context);
224
+ renderTextLines(this._private_paddingInternal.top, this._private_paddingInternal.left, realTextLines, this._private_isRTL, this._private_context);
207
225
  this._private_updateProperties(resolutionDirty);
208
226
  this._private_resizeModeDirty = false;
227
+ this._private_directionDirty = false;
209
228
  this._private_maxLineWidthDirty = false;
210
229
  this._private_paddingInternal.setDirtyFalse();
211
230
  this._private_setDimensionsDirtyFalse();
@@ -221,18 +240,21 @@ class UIText extends UIElement {
221
240
  if (resolutionDirty) {
222
241
  this._private_texture.dispose();
223
242
  this._private_texture = createCanvasTexture(this._private_canvas);
224
- this._private_texture.needsUpdate = true;
225
243
  properties ?? (properties = {});
226
244
  properties["texture"] = this._private_texture;
227
245
  }
246
+ this._private_texture.needsUpdate = true;
228
247
  if (properties) {
229
248
  this.sceneWrapper.setProperties(this.planeHandler, properties);
230
249
  }
231
250
  }
232
251
  _private_buildTextChunks() {
233
- if (this._private_textChunksDirty) {
234
- this._private_textChunks = buildTextChunks(this._private_context, this._private_contentInternal, this.commonStyle);
235
- this._private_textChunksDirty = false;
252
+ if (this._private_directionDirty || this._private_checkContentDirty()) {
253
+ this._private_isRTL =
254
+ this._private_directionInternal === UITextDirection.AUTO
255
+ ? detectIsRTL(this._private_contentInternal)
256
+ : this._private_directionInternal === UITextDirection.RTL;
257
+ this._private_textChunks = buildTextChunks(this._private_context, this._private_contentInternal, this.commonStyle, this._private_isRTL);
236
258
  }
237
259
  return this._private_textChunks;
238
260
  }
@@ -245,10 +267,13 @@ class UIText extends UIElement {
245
267
  this._private_lastHeight = Math.round(this.height);
246
268
  }
247
269
  _private_checkContentDirty() {
248
- return this._private_contentDirty || this.content.some((span) => span.dirty);
270
+ return (this._private_contentDirty ||
271
+ this.commonStyle.dirty ||
272
+ this.content.some((span) => span.dirty));
249
273
  }
250
274
  _private_setContentDirtyFalse() {
251
275
  this._private_contentDirty = false;
276
+ this.commonStyle.setDirtyFalse();
252
277
  for (const span of this.content) {
253
278
  span.setDirtyFalse();
254
279
  }
@@ -25,7 +25,7 @@ export { UIProgress } from "./elements/UIProgress/UIProgress";
25
25
  export { UIScene } from "./elements/UIScene/UIScene";
26
26
  export { UISceneUpdateMode } from "./elements/UIScene/UIScene.Internal";
27
27
  export { UIText } from "./elements/UIText/UIText";
28
- export { UITextResizeMode } from "./elements/UIText/UIText.Internal";
28
+ export { UITextDirection, UITextResizeMode } from "./elements/UIText/UIText.Internal";
29
29
  export { UIFullscreenLayer } from "./layers/UIFullscreenLayer/UIFullscreenLayer";
30
30
  export type { UIFullscreenLayerInputEventData } from "./layers/UIFullscreenLayer/UIFullscreenLayer.Internal";
31
31
  export { UILayer } from "./layers/UILayer/UILayer";
@@ -40,7 +40,7 @@ class UIProgressMaskFunctionDirectional extends UIProgressMaskFunction {
40
40
  this.direction.set(1, 0);
41
41
  }
42
42
  else if (Math.abs(squaredLength - 1) > EPSILON) {
43
- this.direction.divideScalar(squaredLength);
43
+ this.direction.divideScalar(Math.sqrt(squaredLength));
44
44
  }
45
45
  }
46
46
  }
@@ -1,9 +1,11 @@
1
+ /** Text alignment. `start` and `end` are resolved against the text direction. */
2
+ export type UITextAlign = "start" | "left" | "center" | "right" | "end";
1
3
  /** Complete text style configuration with all properties resolved */
2
4
  export interface UITextStyleConfig {
3
5
  /** Text color */
4
6
  color: string;
5
7
  /** Text alignment */
6
- align: "left" | "center" | "right";
8
+ align: UITextAlign;
7
9
  /** Font family name */
8
10
  fontFamily: string;
9
11
  /** Font size in pixels */
@@ -32,7 +34,7 @@ export interface UITextStyleConfig {
32
34
  strokeThickness: number;
33
35
  }
34
36
  export declare const TEXT_STYLE_DEFAULT_COLOR = "#000000";
35
- export declare const TEXT_STYLE_DEFAULT_ALIGN: "left";
37
+ export declare const TEXT_STYLE_DEFAULT_ALIGN: "start";
36
38
  export declare const TEXT_STYLE_DEFAULT_FONT_FAMILY = "Arial";
37
39
  export declare const TEXT_STYLE_DEFAULT_FONT_SIZE = 16;
38
40
  export declare const TEXT_STYLE_DEFAULT_FONT_STYLE: "normal";
@@ -1,5 +1,5 @@
1
1
  const TEXT_STYLE_DEFAULT_COLOR = "#000000";
2
- const TEXT_STYLE_DEFAULT_ALIGN = "left";
2
+ const TEXT_STYLE_DEFAULT_ALIGN = "start";
3
3
  const TEXT_STYLE_DEFAULT_FONT_FAMILY = "Arial";
4
4
  const TEXT_STYLE_DEFAULT_FONT_SIZE = 16;
5
5
  const TEXT_STYLE_DEFAULT_FONT_STYLE = "normal";
@@ -1,4 +1,4 @@
1
- import type { UITextStyleConfig } from "./UITextStyle.Internal";
1
+ import type { UITextAlign, UITextStyleConfig } from "./UITextStyle.Internal";
2
2
  /** Text style configuration for UI text rendering */
3
3
  export declare class UITextStyle {
4
4
  private colorInternal?;
@@ -22,7 +22,7 @@ export declare class UITextStyle {
22
22
  /** Text color */
23
23
  get color(): string | undefined;
24
24
  /** Text alignment */
25
- get align(): "left" | "center" | "right" | undefined;
25
+ get align(): UITextAlign | undefined;
26
26
  /** Font family name */
27
27
  get fontFamily(): string | undefined;
28
28
  /** Font size in pixels */
@@ -50,7 +50,7 @@ export declare class UITextStyle {
50
50
  /** Stroke thickness in pixels */
51
51
  get strokeThickness(): number | undefined;
52
52
  set color(value: string | undefined);
53
- set align(value: "left" | "center" | "right" | undefined);
53
+ set align(value: UITextAlign | undefined);
54
54
  set fontFamily(value: string | undefined);
55
55
  set fontSize(value: number | undefined);
56
56
  set fontStyle(value: "normal" | "italic" | "oblique" | undefined);
@@ -19,7 +19,7 @@ export declare class UITextureView {
19
19
  private textureDirtyInternal;
20
20
  private uvTransformDirtyInternal;
21
21
  private trimDirtyInternal;
22
- private readonly signalDiminsionsChangedInternal;
22
+ private readonly signalDimensionsChangedInternal;
23
23
  /** @param config Texture or atlas configuration */
24
24
  constructor(config?: UITextureConfig);
25
25
  /** Original width before trimming in world units */
@@ -43,7 +43,7 @@ export declare class UITextureView {
43
43
  get uvTransformDirty(): boolean;
44
44
  /** Whether trim values changed since last check */
45
45
  get trimDirty(): boolean;
46
- get signalDiminsionsChanged(): FerrsignView3<number, number, UITextureView>;
46
+ get signalDimensionsChanged(): FerrsignView3<number, number, UITextureView>;
47
47
  setTextureDirtyFalse(): void;
48
48
  /**
49
49
  * Calculates UV transform matrix for shader sampling.
@@ -23,7 +23,7 @@ class UITextureView {
23
23
  this._private_textureDirtyInternal = false;
24
24
  this._private_uvTransformDirtyInternal = false;
25
25
  this._private_trimDirtyInternal = false;
26
- this._private_signalDiminsionsChangedInternal = new Ferrsign3();
26
+ this._private_signalDimensionsChangedInternal = new Ferrsign3();
27
27
  if (config !== undefined) {
28
28
  this.set(config);
29
29
  }
@@ -79,8 +79,8 @@ class UITextureView {
79
79
  get trimDirty() {
80
80
  return this._private_trimDirtyInternal;
81
81
  }
82
- get signalDiminsionsChanged() {
83
- return this._private_signalDiminsionsChangedInternal;
82
+ get signalDimensionsChanged() {
83
+ return this._private_signalDimensionsChangedInternal;
84
84
  }
85
85
  setTextureDirtyFalse() {
86
86
  this._private_textureDirtyInternal = false;
@@ -141,7 +141,7 @@ class UITextureView {
141
141
  this._private_setFromAtlasConfig(config);
142
142
  }
143
143
  if (this.width !== lastWidth || this.height !== lastHeight) {
144
- this._private_signalDiminsionsChangedInternal.emit(this.width, this.height, this);
144
+ this._private_signalDimensionsChangedInternal.emit(this.width, this.height, this);
145
145
  }
146
146
  }
147
147
  _private_setFromTexture(texture) {
@@ -210,6 +210,13 @@ export declare class UISolverWrapper implements UISolverWrapperView {
210
210
  *
211
211
  * Re-adds all variables and constraints to a fresh solver instance.
212
212
  * Used for recovery when the solver enters an invalid state.
213
+ *
214
+ * The solver is normally dropped because some constraint was rejected by kiwi,
215
+ * and that same constraint is still registered here. Replaying it would throw
216
+ * again, so every replay is guarded individually: a constraint kiwi refuses is
217
+ * skipped and stays registered but unapplied, which is exactly the state it was
218
+ * already in. The fresh solver is only published once the replay is complete,
219
+ * so a rejection can never leave a half-built solver in place.
213
220
  */
214
221
  private rebuildSolver;
215
222
  }
@@ -477,26 +477,44 @@ class UISolverWrapper {
477
477
  *
478
478
  * Re-adds all variables and constraints to a fresh solver instance.
479
479
  * Used for recovery when the solver enters an invalid state.
480
+ *
481
+ * The solver is normally dropped because some constraint was rejected by kiwi,
482
+ * and that same constraint is still registered here. Replaying it would throw
483
+ * again, so every replay is guarded individually: a constraint kiwi refuses is
484
+ * skipped and stays registered but unapplied, which is exactly the state it was
485
+ * already in. The fresh solver is only published once the replay is complete,
486
+ * so a rejection can never leave a half-built solver in place.
480
487
  */
481
488
  _private_rebuildSolver() {
482
489
  if (this._private_solver) {
483
490
  throw new Error("UISolverWrapper.rebuildSolver: solver already exists");
484
491
  }
485
- this._private_solver = new Solver();
492
+ const solver = new Solver();
486
493
  for (const description of this._private_variables.values()) {
487
- if (description.constraint) {
488
- this._private_solver.addConstraint(description.constraint);
494
+ try {
495
+ if (description.constraint) {
496
+ solver.addConstraint(description.constraint);
497
+ }
498
+ else {
499
+ solver.addEditVariable(description.variable, convertPriority(description.priority));
500
+ solver.suggestValue(description.variable, description.value);
501
+ }
489
502
  }
490
- else {
491
- this._private_solver.addEditVariable(description.variable, convertPriority(description.priority));
492
- this._private_solver.suggestValue(description.variable, description.value);
503
+ catch {
504
+ continue;
493
505
  }
494
506
  }
495
507
  for (const description of this._private_constraints.values()) {
496
508
  if (description.enabled) {
497
- this._private_solver.addConstraint(description.constraint);
509
+ try {
510
+ solver.addConstraint(description.constraint);
511
+ }
512
+ catch {
513
+ continue;
514
+ }
498
515
  }
499
516
  }
517
+ this._private_solver = solver;
500
518
  this._private_recalculationRequired = true;
501
519
  this.dirty = true;
502
520
  }
package/dist/index.js CHANGED
@@ -24,7 +24,7 @@ export { UIProgress } from './core/elements/UIProgress/UIProgress.js';
24
24
  export { UIScene } from './core/elements/UIScene/UIScene.js';
25
25
  export { UISceneUpdateMode } from './core/elements/UIScene/UIScene.Internal.js';
26
26
  export { UIText } from './core/elements/UIText/UIText.js';
27
- export { UITextResizeMode } from './core/elements/UIText/UIText.Internal.js';
27
+ export { UITextDirection, UITextResizeMode } from './core/elements/UIText/UIText.Internal.js';
28
28
  export { UIFullscreenLayer } from './core/layers/UIFullscreenLayer/UIFullscreenLayer.js';
29
29
  export { UILayer } from './core/layers/UILayer/UILayer.js';
30
30
  export { UIAreaCircle } from './core/miscellaneous/area/UIAreaCircle.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "laymur",
3
- "version": "0.21.1",
3
+ "version": "0.23.0",
4
4
  "description": "Constraint-based UI library for Three.js mobile advertisements",
5
5
  "keywords": [
6
6
  "ui",