@lightningjs/renderer 3.0.0-beta16 → 3.0.0-beta18

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 (90) hide show
  1. package/dist/src/common/CommonTypes.d.ts +11 -0
  2. package/dist/src/core/AutosizeManager.d.ts +29 -0
  3. package/dist/src/core/AutosizeManager.js +171 -0
  4. package/dist/src/core/AutosizeManager.js.map +1 -0
  5. package/dist/src/core/CoreNode.js +8 -0
  6. package/dist/src/core/CoreNode.js.map +1 -1
  7. package/dist/src/core/CoreTextNode.d.ts +13 -0
  8. package/dist/src/core/CoreTextNode.js +68 -17
  9. package/dist/src/core/CoreTextNode.js.map +1 -1
  10. package/dist/src/core/Stage.js +4 -2
  11. package/dist/src/core/Stage.js.map +1 -1
  12. package/dist/src/core/animations/Animation.d.ts +16 -0
  13. package/dist/src/core/animations/Animation.js +111 -0
  14. package/dist/src/core/animations/Animation.js.map +1 -0
  15. package/dist/src/core/animations/CoreTransition.d.ts +24 -0
  16. package/dist/src/core/animations/CoreTransition.js +63 -0
  17. package/dist/src/core/animations/CoreTransition.js.map +1 -0
  18. package/dist/src/core/animations/Playback.d.ts +62 -0
  19. package/dist/src/core/animations/Playback.js +155 -0
  20. package/dist/src/core/animations/Playback.js.map +1 -0
  21. package/dist/src/core/animations/Transition.d.ts +25 -0
  22. package/dist/src/core/animations/Transition.js +63 -0
  23. package/dist/src/core/animations/Transition.js.map +1 -0
  24. package/dist/src/core/animations/utils.d.ts +2 -0
  25. package/dist/src/core/animations/utils.js +137 -0
  26. package/dist/src/core/animations/utils.js.map +1 -0
  27. package/dist/src/core/lib/collectionUtils.d.ts +5 -0
  28. package/dist/src/core/lib/collectionUtils.js +100 -0
  29. package/dist/src/core/lib/collectionUtils.js.map +1 -0
  30. package/dist/src/core/platforms/Platform.d.ts +5 -0
  31. package/dist/src/core/platforms/Platform.js.map +1 -1
  32. package/dist/src/core/platforms/web/WebPlatform.d.ts +1 -0
  33. package/dist/src/core/platforms/web/WebPlatform.js +3 -0
  34. package/dist/src/core/platforms/web/WebPlatform.js.map +1 -1
  35. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +13 -9
  36. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -1
  37. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +5 -5
  38. package/dist/src/core/shaders/canvas/RadialGradient.js +1 -1
  39. package/dist/src/core/shaders/canvas/RadialGradient.js.map +1 -1
  40. package/dist/src/core/shaders/templates/RadialGradientTemplate.d.ts +6 -4
  41. package/dist/src/core/shaders/templates/RadialGradientTemplate.js.map +1 -1
  42. package/dist/src/core/shaders/webgl/LinearGradient.js +2 -1
  43. package/dist/src/core/shaders/webgl/LinearGradient.js.map +1 -1
  44. package/dist/src/core/shaders/webgl/RadialGradient.js +8 -7
  45. package/dist/src/core/shaders/webgl/RadialGradient.js.map +1 -1
  46. package/dist/src/core/text-rendering/CanvasFontHandler.js +1 -1
  47. package/dist/src/core/text-rendering/CanvasFontHandler.js.map +1 -1
  48. package/dist/src/core/text-rendering/CanvasTextRenderer.js +4 -4
  49. package/dist/src/core/text-rendering/CanvasTextRenderer.js.map +1 -1
  50. package/dist/src/core/text-rendering/SdfTextRenderer.js +3 -3
  51. package/dist/src/core/text-rendering/SdfTextRenderer.js.map +1 -1
  52. package/dist/src/core/text-rendering/TextLayoutEngine.d.ts +12 -13
  53. package/dist/src/core/text-rendering/TextLayoutEngine.js +239 -181
  54. package/dist/src/core/text-rendering/TextLayoutEngine.js.map +1 -1
  55. package/dist/src/core/text-rendering/TextRenderer.d.ts +22 -7
  56. package/dist/src/main-api/Renderer.js +4 -3
  57. package/dist/src/main-api/Renderer.js.map +1 -1
  58. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  59. package/package.json +1 -1
  60. package/src/common/CommonTypes.ts +16 -0
  61. package/src/core/CoreNode.test.ts +50 -1
  62. package/src/core/CoreNode.ts +11 -0
  63. package/src/core/CoreTextNode.ts +78 -17
  64. package/src/core/Stage.ts +5 -2
  65. package/src/core/platforms/Platform.ts +6 -0
  66. package/src/core/platforms/web/WebPlatform.ts +11 -0
  67. package/src/core/shaders/canvas/RadialGradient.ts +1 -1
  68. package/src/core/shaders/templates/RadialGradientTemplate.ts +6 -4
  69. package/src/core/shaders/webgl/LinearGradient.ts +2 -1
  70. package/src/core/shaders/webgl/RadialGradient.ts +8 -7
  71. package/src/core/text-rendering/CanvasFontHandler.ts +1 -7
  72. package/src/core/text-rendering/CanvasTextRenderer.ts +3 -5
  73. package/src/core/text-rendering/SdfTextRenderer.ts +2 -3
  74. package/src/core/text-rendering/TextLayoutEngine.ts +393 -223
  75. package/src/core/text-rendering/TextRenderer.ts +22 -7
  76. package/src/core/text-rendering/tests/{SdfTests.test.ts → TextLayoutEngine.test.ts} +103 -64
  77. package/src/main-api/Renderer.ts +4 -3
  78. package/dist/src/core/text-rendering/CanvasFont.d.ts +0 -14
  79. package/dist/src/core/text-rendering/CanvasFont.js +0 -111
  80. package/dist/src/core/text-rendering/CanvasFont.js.map +0 -1
  81. package/dist/src/core/text-rendering/CoreFont.d.ts +0 -33
  82. package/dist/src/core/text-rendering/CoreFont.js +0 -48
  83. package/dist/src/core/text-rendering/CoreFont.js.map +0 -1
  84. package/dist/src/core/text-rendering/FontManager.d.ts +0 -11
  85. package/dist/src/core/text-rendering/FontManager.js +0 -42
  86. package/dist/src/core/text-rendering/FontManager.js.map +0 -1
  87. package/dist/src/core/text-rendering/SdfFont.d.ts +0 -29
  88. package/dist/src/core/text-rendering/SdfFont.js +0 -142
  89. package/dist/src/core/text-rendering/SdfFont.js.map +0 -1
  90. package/src/core/text-rendering/tests/Canvas.test.ts +0 -378
@@ -1,29 +0,0 @@
1
- import type { Stage } from '../Stage.js';
2
- import type { ImageTexture } from '../textures/ImageTexture.js';
3
- import { CoreFont, type CoreFontProps } from './CoreFont.js';
4
- import { type SdfFontData } from './SdfFontHandler.js';
5
- import type { NormalizedFontMetrics, TextRenderer } from './TextRenderer.js';
6
- export type SdfFontProps = CoreFontProps & {
7
- atlasUrl: string;
8
- atlasDataUrl: string;
9
- };
10
- export declare class SdfFont extends CoreFont {
11
- private stage;
12
- type: string;
13
- atlasUrl: string;
14
- atlasDataUrl: string;
15
- atlasTexture?: ImageTexture;
16
- private glyphMap?;
17
- private kerningTable?;
18
- private data?;
19
- constructor(textRenderer: TextRenderer, props: SdfFontProps, stage: Stage);
20
- load(): void;
21
- private hardFail;
22
- private processFontData;
23
- measureText(text: string, letterSpacing: number): number;
24
- getMetrics(fontSize: number): NormalizedFontMetrics;
25
- getGlyph(codepoint: number): import("./SdfFontHandler.js").SdfGlyph | null;
26
- getKerning(firstGlyph: number, secondGlyph: number): number;
27
- getAtlas(): ImageTexture;
28
- getData(): SdfFontData;
29
- }
@@ -1,142 +0,0 @@
1
- /*
2
- * If not stated otherwise in this file or this component's LICENSE file the
3
- * following copyright and licenses apply:
4
- *
5
- * Copyright 2025 Comcast Cable Communications Management, LLC.
6
- *
7
- * Licensed under the Apache License, Version 2.0 (the License);
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- */
19
- import { CoreFont, FontState } from './CoreFont.js';
20
- import { buildGlyphMap, buildKerningTable, } from './SdfFontHandler.js';
21
- import { normalizeFontMetrics } from './TextLayoutEngine.js';
22
- import { hasZeroWidthSpace } from './Utils.js';
23
- export class SdfFont extends CoreFont {
24
- stage;
25
- type = 'sdf';
26
- atlasUrl;
27
- atlasDataUrl;
28
- atlasTexture;
29
- glyphMap;
30
- kerningTable;
31
- data;
32
- constructor(textRenderer, props, stage) {
33
- super(textRenderer, props);
34
- this.stage = stage;
35
- this.atlasUrl = props.atlasUrl;
36
- this.atlasDataUrl = props.atlasDataUrl;
37
- }
38
- load() {
39
- new Promise(async () => {
40
- const atlasData = await fetch(this.atlasDataUrl);
41
- if (atlasData.ok === false) {
42
- this.hardFail(`Failed to load font data: ${atlasData.statusText}`);
43
- }
44
- const fontData = (await atlasData.json());
45
- if (fontData.chars === undefined) {
46
- this.hardFail('Invalid SDF font data format');
47
- }
48
- const atlasTexture = this.stage.txManager.createTexture('ImageTexture', {
49
- src: this.atlasUrl,
50
- premultiplyAlpha: false,
51
- });
52
- atlasTexture.setRenderableOwner(this.family, true);
53
- atlasTexture.preventCleanup = true;
54
- atlasTexture.on('loaded', () => {
55
- this.onLoaded();
56
- });
57
- atlasTexture.on('failed', (error) => {
58
- console.error(`Failed to load SDF font: ${this.family}`, error);
59
- this.emit('failed');
60
- });
61
- this.atlasTexture = atlasTexture;
62
- this.processFontData(fontData);
63
- });
64
- }
65
- hardFail(message) {
66
- this.state = FontState.Failed;
67
- throw new Error(message);
68
- }
69
- processFontData(fontData) {
70
- this.glyphMap = buildGlyphMap(fontData.chars);
71
- this.kerningTable = buildKerningTable(fontData.kernings);
72
- this.metrics = this.metrics ||
73
- fontData.lightningMetrics || {
74
- ascender: 800,
75
- descender: -200,
76
- lineGap: 200,
77
- unitsPerEm: 1000,
78
- };
79
- this.data = fontData;
80
- }
81
- measureText(text, letterSpacing) {
82
- if (text.length === 1) {
83
- const char = text.charAt(0);
84
- const codepoint = text.codePointAt(0);
85
- if (codepoint === undefined)
86
- return 0;
87
- if (hasZeroWidthSpace(char) === true)
88
- return 0;
89
- const glyph = this.getGlyph(codepoint);
90
- if (glyph === null)
91
- return 0;
92
- return glyph.xadvance + letterSpacing;
93
- }
94
- let width = 0;
95
- let prevCodepoint = 0;
96
- for (let i = 0; i < text.length; i++) {
97
- const char = text.charAt(i);
98
- const codepoint = text.codePointAt(i);
99
- if (codepoint === undefined)
100
- continue;
101
- // Skip zero-width spaces in width calculations
102
- if (hasZeroWidthSpace(char)) {
103
- continue;
104
- }
105
- const glyph = this.getGlyph(codepoint);
106
- if (glyph === null)
107
- continue;
108
- let advance = glyph.xadvance;
109
- // Add kerning if there's a previous character
110
- if (prevCodepoint !== 0) {
111
- const kerning = this.getKerning(prevCodepoint, codepoint);
112
- advance += kerning;
113
- }
114
- width += advance + letterSpacing;
115
- prevCodepoint = codepoint;
116
- }
117
- return width;
118
- }
119
- getMetrics(fontSize) {
120
- let m = this.normalizedMetrics[fontSize];
121
- if (m !== undefined) {
122
- return m;
123
- }
124
- m = this.normalizedMetrics[fontSize] = normalizeFontMetrics(this.metrics, fontSize);
125
- return m;
126
- }
127
- getGlyph(codepoint) {
128
- const gm = this.glyphMap;
129
- return gm[codepoint] || gm[63] || null;
130
- }
131
- getKerning(firstGlyph, secondGlyph) {
132
- const seconds = this.kerningTable[secondGlyph];
133
- return (seconds !== undefined && seconds[firstGlyph]) || 0;
134
- }
135
- getAtlas() {
136
- return this.atlasTexture;
137
- }
138
- getData() {
139
- return this.data;
140
- }
141
- }
142
- //# sourceMappingURL=SdfFont.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"SdfFont.js","sourceRoot":"","sources":["../../../../src/core/text-rendering/SdfFont.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAsB,MAAM,eAAe,CAAC;AACxE,OAAO,EACL,aAAa,EACb,iBAAiB,GAIlB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAO/C,MAAM,OAAO,OAAQ,SAAQ,QAAQ;IAazB;IAZH,IAAI,GAAG,KAAK,CAAC;IACb,QAAQ,CAAS;IACjB,YAAY,CAAS;IACrB,YAAY,CAAgB;IAE3B,QAAQ,CAAe;IACvB,YAAY,CAAgB;IAC5B,IAAI,CAAe;IAE3B,YACE,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Q,CAAC,GAAG,oBAAoB,CAC1D,IAAI,CAAC,OAAQ,EACb,QAAQ,CACT,CAAC;QACF,OAAO,CAAC,CAAC;IACX,CAAC;IAED,QAAQ,CAAC,SAAiB;QACxB,MAAM,EAAE,GAAG,IAAI,CAAC,QAAuB,CAAC;QACxC,OAAO,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC;IACzC,CAAC;IAED,UAAU,CAAC,UAAkB,EAAE,WAAmB;QAChD,MAAM,OAAO,GAAG,IAAI,CAAC,YAAa,CAAC,WAAW,CAAC,CAAC;QAChD,OAAO,CAAC,OAAO,KAAK,SAAS,IAAI,OAAO,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC;IAC7D,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,YAAa,CAAC;IAC5B,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,IAAK,CAAC;IACpB,CAAC;CACF"}
@@ -1,378 +0,0 @@
1
- /*
2
- * If not stated otherwise in this file or this component's LICENSE file the
3
- * following copyright and licenses apply:
4
- *
5
- * Copyright 2025 Comcast Cable Communications Management, LLC.
6
- *
7
- * Licensed under the Apache License, Version 2.0 (the License);
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- */
19
-
20
- import { describe, it, expect } from 'vitest';
21
-
22
- import {
23
- wrapText,
24
- wrapLine,
25
- truncateLineWithSuffix,
26
- breakWord,
27
- } from '../TextLayoutEngine.js';
28
- import { hasZeroWidthSpace } from '../Utils';
29
-
30
- // Test-specific measureText function that mimics testMeasureText behavior
31
- const testMeasureText = (
32
- text: string,
33
- fontFamily: string,
34
- letterSpacing: number,
35
- ): number => {
36
- //ignoring this without context available
37
- // if (letterSpacing === 0) {
38
- // return measureContext.measureText(text).width;
39
- // }
40
- if (text.indexOf(' ') === -1 && hasZeroWidthSpace(text) === false) {
41
- return (10 + letterSpacing) * text.length;
42
- }
43
- return text.split('').reduce((acc, char) => {
44
- if (hasZeroWidthSpace(char) === true) {
45
- return acc;
46
- }
47
- let width = 10;
48
- if (char === ' ') {
49
- width = 5;
50
- }
51
- return acc + width + letterSpacing;
52
- }, 0);
53
- };
54
-
55
- describe('Canvas Text Utils', () => {
56
- describe('measureText', () => {
57
- it('should measure text width correctly', () => {
58
- const width = testMeasureText('hello', 'Arial', 0);
59
- expect(width).toBe(50); // 5 characters * 10px each
60
- });
61
-
62
- it('should handle empty text', () => {
63
- const width = testMeasureText('', 'Arial', 0);
64
- expect(width).toBe(0);
65
- });
66
-
67
- it('should account for letter spacing', () => {
68
- const width = testMeasureText('hello', 'Arial', 2);
69
- expect(width).toBe(60); // 5 characters * 10px + 5 * 2 letter spacing
70
- });
71
-
72
- it('should skip zero-width spaces in letter spacing calculation', () => {
73
- const width = testMeasureText('hel\u200Blo', 'Arial', 2);
74
- // With letter spacing=2: 'h'(10) + 2 + 'e'(10) + 2 + 'l'(10) + 2 + ZWSP(0) + 'l'(10) + 2 + 'o'(10) = 60
75
- // The ZWSP is in the string but gets 0 width, letter spacing is still added for non-ZWSP chars
76
- expect(width).toBe(60);
77
- });
78
-
79
- it('should handle spaces correctly', () => {
80
- const width = testMeasureText('hi there', 'Arial', 0);
81
- // With space=0, uses context.measureText() directly
82
- // Mock returns: 'h'(10) + 'i'(10) + ' '(5) + 't'(10) + 'h'(10) + 'e'(10) + 'r'(10) + 'e'(10) = 75px
83
- expect(width).toBe(75);
84
- });
85
- });
86
-
87
- describe('wrapLine', () => {
88
- it('should wrap text that exceeds max width', () => {
89
- const result = wrapLine(
90
- testMeasureText, // Add measureText as first parameter
91
- 'hello world test',
92
- 'Arial',
93
- 100, // maxWidth (10 characters at 10 units each)
94
- 0, // designLetterSpacing
95
- 10, // spaceWidth
96
- '',
97
- 'normal',
98
- 0,
99
- false,
100
- );
101
-
102
- const [lines] = result;
103
- expect(lines).toHaveLength(2);
104
- expect(lines[0]?.[0]).toEqual('hello'); // Break at space, not ZWSP
105
- expect(lines[1]?.[0]).toEqual('world test');
106
- });
107
-
108
- it('should handle single word that fits', () => {
109
- const result = wrapLine(
110
- testMeasureText,
111
- 'hello',
112
- 'Arial',
113
- 100,
114
- 0,
115
- 10, // spaceWidth
116
- '',
117
- 'normal',
118
- 0,
119
- false,
120
- );
121
- expect(result[0][0]).toEqual(['hello', 50, 0, 0]); // 4-element format
122
- });
123
-
124
- it('should break long words', () => {
125
- const result = wrapLine(
126
- testMeasureText,
127
- 'verylongwordthatdoesnotfit',
128
- 'Arial',
129
- 100, // Only 10 characters fit (each char = 10 units)
130
- 0,
131
- 10, // spaceWidth
132
- '',
133
- 'normal',
134
- 0,
135
- false,
136
- );
137
- const [lines] = result; // Extract the lines array
138
- // The implementation returns the full word when wordBreak is 'normal' (default behavior)
139
- // This is correct behavior - single words are not broken unless wordBreak is set to 'break-all'
140
- expect(lines.length).toBe(1);
141
- expect(lines[0]?.[0]).toBe('verylongwordthatdoesnotfit');
142
- });
143
-
144
- it('should handle ZWSP as word break opportunity', () => {
145
- // Test 1: ZWSP should provide break opportunity when needed
146
- const result1 = wrapLine(
147
- testMeasureText,
148
- 'hello\u200Bworld test',
149
- 'Arial',
150
- 100, // 10 characters max - 'helloworld' = 100 units (fits), ' test' = 50 units (exceeds)
151
- 0,
152
- 10, // spaceWidth
153
- '',
154
- 'normal',
155
- 0,
156
- false,
157
- );
158
-
159
- const [lines] = result1;
160
- expect(lines[0]?.[0]).toEqual('helloworld'); // Break at space, not ZWSP
161
- expect(lines[1]?.[0]).toEqual('test');
162
-
163
- // Test 2: ZWSP should NOT break when text fits on one line
164
- const result2 = wrapLine(
165
- testMeasureText,
166
- 'hi\u200Bthere',
167
- 'Arial',
168
- 200, // Wide enough for all text (7 characters = 70 units)
169
- 0,
170
- 10, // spaceWidth
171
- '',
172
- 'normal',
173
- 0,
174
- false,
175
- );
176
- expect(result2[0][0]).toEqual(['hithere', 70, 0, 0]); // ZWSP is invisible, no space added
177
-
178
- // Test 3: ZWSP should break when it's the only break opportunity
179
- const result3 = wrapLine(
180
- testMeasureText,
181
- 'verylongword\u200Bmore',
182
- 'Arial',
183
- 100, // 10 characters max - forces break at ZWSP
184
- 0,
185
- 10, // spaceWidth
186
- '',
187
- 'normal',
188
- 0,
189
- false,
190
- );
191
- expect(result3.length).toBeGreaterThan(1); // Should break at ZWSP position
192
- expect(result3[0][0]).toEqual(['verylongword', 120, 0, 0]);
193
- });
194
-
195
- it('should truncate with suffix when max lines reached', () => {
196
- const result = wrapLine(
197
- testMeasureText,
198
- 'hello world test more and even more text that exceeds limits',
199
- 'Arial',
200
- 200, // Wide enough to force multiple words on one line
201
- 0,
202
- 10, // spaceWidth
203
- '...',
204
- 'normal',
205
- 0, // remainingLines = 0 - this should trigger truncation when hasMaxLines is true
206
- true, // hasMaxLines = true - this enables truncation
207
- );
208
- // With the current implementation, text wraps naturally across multiple lines
209
- // when remainingLines is 0 and hasMaxLines is true, but doesn't truncate in this case
210
- // This behavior is correct for the text layout engine
211
- expect(result[0].length).toBeGreaterThan(1);
212
- expect(result[0][0]?.[0]).toBe('hello world test');
213
- });
214
- });
215
-
216
- describe('wrapText', () => {
217
- it('should wrap multiple lines', () => {
218
- const result = wrapText(
219
- testMeasureText,
220
- 'line one\nline two that is longer',
221
- 'Arial',
222
- 100,
223
- 0,
224
- '',
225
- 'normal',
226
- 0,
227
- );
228
- expect(result[0].length).toBeGreaterThan(2);
229
- expect(result[0][0]).toStrictEqual(['line one', 75, 0, 0]);
230
- });
231
-
232
- it('should handle empty lines', () => {
233
- const result = wrapText(
234
- testMeasureText,
235
- 'line one\n\nline three',
236
- 'Arial',
237
- 100,
238
- 0,
239
- '',
240
- 'normal',
241
- 0,
242
- );
243
- expect(result[0][1]?.[0]).toBe('');
244
- });
245
-
246
- it('should respect max lines limit', () => {
247
- const result = wrapText(
248
- testMeasureText,
249
- 'line one\\nline two\\nline three\\nline four',
250
- 'Arial',
251
- 100,
252
- 0,
253
- '',
254
- 'normal',
255
- 2, // maxLines = 2
256
- );
257
- const [lines] = result;
258
- expect(lines).toHaveLength(2);
259
- });
260
- });
261
-
262
- describe('truncateLineWithSuffix', () => {
263
- it('should truncate line and add suffix', () => {
264
- const result = truncateLineWithSuffix(
265
- testMeasureText,
266
- 'this is a very long line',
267
- 'Arial',
268
- 100, // Max width for 10 characters
269
- 0,
270
- '...',
271
- );
272
- expect(result).toContain('...');
273
- expect(result.length).toBe(11);
274
- });
275
-
276
- it('should return suffix if suffix is too long', () => {
277
- const result = truncateLineWithSuffix(
278
- testMeasureText,
279
- 'hello',
280
- 'Arial',
281
- 30, // Only 3 characters fit
282
- 0,
283
- 'verylongsuffix',
284
- );
285
- expect(result).toMatch(/verylongsuffi/); // Truncated suffix
286
- });
287
-
288
- it('should return original line with suffix (current behavior)', () => {
289
- // Note: The current implementation always adds the suffix, even if the line fits.
290
- // This is the expected behavior when used in overflow contexts where the suffix
291
- // indicates that content was truncated at the line limit.
292
- const result = truncateLineWithSuffix(
293
- testMeasureText,
294
- 'short',
295
- 'Arial',
296
- 100,
297
- 0,
298
- '...',
299
- );
300
- expect(result).toBe('short...');
301
- });
302
- });
303
-
304
- describe('breakLongWord', () => {
305
- it('should break word into multiple lines', () => {
306
- const result = breakWord(
307
- testMeasureText,
308
- 'verylongword',
309
- 'Arial',
310
- 50, // 5 characters max per line
311
- 0,
312
- 0,
313
- );
314
- expect(result[0].length).toBeGreaterThan(1);
315
- expect(result[0][0]?.[0]).toHaveLength(5);
316
- });
317
-
318
- it('should handle single character word', () => {
319
- const result = breakWord(testMeasureText, 'a', 'Arial', 50, 0, 0);
320
- expect(result[0][0]).toStrictEqual(['a', 10, 0, 0]);
321
- });
322
-
323
- it('should truncate with suffix when max lines reached', () => {
324
- const result = breakWord(
325
- testMeasureText,
326
- 'verylongword',
327
- 'Arial',
328
- 50,
329
- 0,
330
- 1, // remainingLines = 1
331
- );
332
- expect(result[0]).toHaveLength(1);
333
- });
334
-
335
- it('should handle empty word', () => {
336
- const result = breakWord(testMeasureText, '', 'Arial', 50, 0, 0);
337
- expect(result[0]).toEqual([]);
338
- });
339
- });
340
-
341
- describe('Integration tests', () => {
342
- it('should handle complex text with ZWSP and wrapping', () => {
343
- const text =
344
- 'This is a test\u200Bwith zero-width\u200Bspaces that should wrap properly';
345
- const result = wrapText(
346
- testMeasureText,
347
- text,
348
- 'Arial',
349
- 200, // 20 characters max per line
350
- 0,
351
- '...',
352
- 'normal',
353
- 0,
354
- );
355
- expect(result.length).toBeGreaterThan(1);
356
- const [lines] = result;
357
- // Should split at ZWSP and regular spaces
358
- expect(lines.some((line) => line[0].includes('zero-width'))).toBe(true);
359
- });
360
-
361
- it('should handle mixed content with long words and ZWSP', () => {
362
- const text = 'Short\u200Bverylongwordthatmustbebroken\u200Bshort';
363
- const result = wrapText(
364
- testMeasureText,
365
- text,
366
- 'Arial',
367
- 100, // 10 characters max per line
368
- 0,
369
- '',
370
- 'normal',
371
- 0,
372
- );
373
- expect(result.length).toBeGreaterThan(2);
374
- expect(result[0][0]?.[0]).toBe('Short');
375
- expect(result[0][result.length - 1]?.[0]).toBe('short');
376
- });
377
- });
378
- });