@lightningjs/renderer 2.9.0 → 2.9.1

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 (218) hide show
  1. package/COPYING +1 -0
  2. package/LICENSE +202 -202
  3. package/NOTICE +3 -3
  4. package/README.md +147 -147
  5. package/dist/src/core/CoreNode.js +1 -1
  6. package/dist/src/core/CoreNode.js.map +1 -1
  7. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  8. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  9. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  10. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  11. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  12. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  13. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  14. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  15. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  16. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  17. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  18. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  19. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  20. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  21. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  22. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  23. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  24. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  25. package/dist/src/core/text-rendering/font-face-types/WebTrFontFace.js +7 -3
  26. package/dist/src/core/text-rendering/font-face-types/WebTrFontFace.js.map +1 -1
  27. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js +7 -0
  28. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js.map +1 -1
  29. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +5 -1
  30. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
  31. package/dist/src/core/textures/ImageTexture.js +1 -1
  32. package/dist/src/core/textures/SubTexture.d.ts +1 -0
  33. package/dist/src/core/textures/SubTexture.js +7 -0
  34. package/dist/src/core/textures/SubTexture.js.map +1 -1
  35. package/dist/src/core/textures/Texture.d.ts +9 -0
  36. package/dist/src/core/textures/Texture.js +23 -13
  37. package/dist/src/core/textures/Texture.js.map +1 -1
  38. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  39. package/exports/canvas.ts +39 -39
  40. package/exports/index.ts +89 -89
  41. package/exports/inspector.ts +24 -24
  42. package/exports/utils.ts +44 -44
  43. package/exports/webgl.ts +38 -38
  44. package/package.json +1 -1
  45. package/scripts/please-use-pnpm.js +13 -13
  46. package/src/common/CommonTypes.ts +139 -139
  47. package/src/common/EventEmitter.ts +77 -77
  48. package/src/common/IAnimationController.ts +92 -92
  49. package/src/common/IEventEmitter.ts +28 -28
  50. package/src/core/CoreNode.test.ts +199 -199
  51. package/src/core/CoreNode.ts +2335 -2335
  52. package/src/core/CoreShaderManager.ts +292 -292
  53. package/src/core/CoreTextNode.ts +455 -455
  54. package/src/core/CoreTextureManager.ts +548 -548
  55. package/src/core/Stage.ts +700 -700
  56. package/src/core/TextureMemoryManager.ts +277 -277
  57. package/src/core/animations/AnimationManager.ts +38 -38
  58. package/src/core/animations/CoreAnimation.ts +340 -340
  59. package/src/core/animations/CoreAnimationController.ts +157 -157
  60. package/src/core/lib/ContextSpy.ts +41 -41
  61. package/src/core/lib/ImageWorker.ts +270 -270
  62. package/src/core/lib/Matrix3d.ts +244 -244
  63. package/src/core/lib/RenderCoords.ts +86 -86
  64. package/src/core/lib/WebGlContextWrapper.ts +1322 -1322
  65. package/src/core/lib/textureCompression.ts +152 -152
  66. package/src/core/lib/textureSvg.ts +78 -78
  67. package/src/core/lib/utils.ts +306 -306
  68. package/src/core/platform.ts +61 -61
  69. package/src/core/renderers/CoreContextTexture.ts +43 -43
  70. package/src/core/renderers/CoreRenderOp.ts +22 -22
  71. package/src/core/renderers/CoreRenderer.ts +114 -114
  72. package/src/core/renderers/CoreShader.ts +41 -41
  73. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +364 -364
  74. package/src/core/renderers/canvas/CanvasCoreTexture.ts +150 -150
  75. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  76. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  77. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  78. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +79 -79
  79. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -50
  80. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +298 -298
  81. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  82. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +805 -805
  83. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  84. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  85. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  86. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  87. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  88. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  89. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  90. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  91. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  92. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  93. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  94. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  95. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  96. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  97. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  98. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  99. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  100. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  101. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  102. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  103. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  104. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  105. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  106. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  107. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  108. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  109. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  110. package/src/core/text-rendering/TrFontManager.ts +183 -183
  111. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -176
  112. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  113. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  114. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  115. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  116. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +94 -89
  117. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -509
  118. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +808 -798
  119. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  120. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  121. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  122. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  123. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  124. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  125. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  126. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  127. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +408 -403
  128. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  129. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  130. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  131. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  132. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  133. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  134. package/src/core/textures/ColorTexture.ts +102 -102
  135. package/src/core/textures/ImageTexture.ts +378 -378
  136. package/src/core/textures/NoiseTexture.ts +104 -104
  137. package/src/core/textures/RenderTexture.ts +85 -85
  138. package/src/core/textures/SubTexture.ts +179 -171
  139. package/src/core/textures/Texture.ts +435 -423
  140. package/src/core/utils.ts +227 -227
  141. package/src/env.d.ts +7 -7
  142. package/src/main-api/DynamicShaderController.ts +104 -104
  143. package/src/main-api/INode.ts +101 -101
  144. package/src/main-api/Inspector.ts +505 -505
  145. package/src/main-api/Renderer.ts +693 -693
  146. package/src/main-api/ShaderController.ts +80 -80
  147. package/src/main-api/utils.ts +45 -45
  148. package/src/utils.ts +248 -248
  149. package/dist/exports/core-api.d.ts +0 -74
  150. package/dist/exports/core-api.js +0 -96
  151. package/dist/exports/core-api.js.map +0 -1
  152. package/dist/exports/main-api.d.ts +0 -30
  153. package/dist/exports/main-api.js +0 -45
  154. package/dist/exports/main-api.js.map +0 -1
  155. package/dist/src/core/CoreExtension.d.ts +0 -12
  156. package/dist/src/core/CoreExtension.js +0 -29
  157. package/dist/src/core/CoreExtension.js.map +0 -1
  158. package/dist/src/main-api/ICoreDriver.d.ts +0 -24
  159. package/dist/src/main-api/ICoreDriver.js +0 -20
  160. package/dist/src/main-api/ICoreDriver.js.map +0 -1
  161. package/dist/src/main-api/RendererMain.d.ts +0 -378
  162. package/dist/src/main-api/RendererMain.js +0 -367
  163. package/dist/src/main-api/RendererMain.js.map +0 -1
  164. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.d.ts +0 -9
  165. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js +0 -38
  166. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js.map +0 -1
  167. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.d.ts +0 -56
  168. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js +0 -101
  169. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js.map +0 -1
  170. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.d.ts +0 -32
  171. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js +0 -28
  172. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js.map +0 -1
  173. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +0 -21
  174. package/dist/src/render-drivers/main/MainCoreDriver.js +0 -115
  175. package/dist/src/render-drivers/main/MainCoreDriver.js.map +0 -1
  176. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +0 -101
  177. package/dist/src/render-drivers/main/MainOnlyNode.js +0 -425
  178. package/dist/src/render-drivers/main/MainOnlyNode.js.map +0 -1
  179. package/dist/src/render-drivers/main/MainOnlyTextNode.d.ts +0 -47
  180. package/dist/src/render-drivers/main/MainOnlyTextNode.js +0 -204
  181. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +0 -1
  182. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +0 -93
  183. package/dist/src/render-drivers/threadx/NodeStruct.js +0 -290
  184. package/dist/src/render-drivers/threadx/NodeStruct.js.map +0 -1
  185. package/dist/src/render-drivers/threadx/SharedNode.d.ts +0 -40
  186. package/dist/src/render-drivers/threadx/SharedNode.js +0 -61
  187. package/dist/src/render-drivers/threadx/SharedNode.js.map +0 -1
  188. package/dist/src/render-drivers/threadx/TextNodeStruct.d.ts +0 -44
  189. package/dist/src/render-drivers/threadx/TextNodeStruct.js +0 -203
  190. package/dist/src/render-drivers/threadx/TextNodeStruct.js.map +0 -1
  191. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +0 -25
  192. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +0 -232
  193. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +0 -1
  194. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.d.ts +0 -24
  195. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js +0 -113
  196. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js.map +0 -1
  197. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +0 -46
  198. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +0 -160
  199. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +0 -1
  200. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.d.ts +0 -28
  201. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js +0 -55
  202. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js.map +0 -1
  203. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +0 -70
  204. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js +0 -32
  205. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +0 -1
  206. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.d.ts +0 -19
  207. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +0 -184
  208. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +0 -1
  209. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.d.ts +0 -27
  210. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +0 -109
  211. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +0 -1
  212. package/dist/src/render-drivers/threadx/worker/renderer.d.ts +0 -1
  213. package/dist/src/render-drivers/threadx/worker/renderer.js +0 -147
  214. package/dist/src/render-drivers/threadx/worker/renderer.js.map +0 -1
  215. package/dist/src/render-drivers/utils.d.ts +0 -12
  216. package/dist/src/render-drivers/utils.js +0 -74
  217. package/dist/src/render-drivers/utils.js.map +0 -1
  218. package/dist/tsconfig.tsbuildinfo +0 -1
@@ -1,403 +1,408 @@
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 2023 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 { assertTruthy } from '../../../../../utils.js';
21
- import type { Bound } from '../../../../lib/utils.js';
22
- import type {
23
- FontShaperProps,
24
- MappedGlyphInfo,
25
- UnmappedCharacterInfo,
26
- } from '../../../font-face-types/SdfTrFontFace/internal/FontShaper.js';
27
- import type { TrProps, TextRendererState } from '../../TextRenderer.js';
28
- import type { SdfTextRendererState } from '../SdfTextRenderer.js';
29
- import { PeekableIterator } from './PeekableGenerator.js';
30
- import { getUnicodeCodepoints } from './getUnicodeCodepoints.js';
31
- import { measureText } from './measureText.js';
32
-
33
- export function layoutText(
34
- curLineIndex: number,
35
- startX: number,
36
- startY: number,
37
- text: TrProps['text'],
38
- textAlign: TrProps['textAlign'],
39
- width: TrProps['width'],
40
- height: TrProps['height'],
41
- fontSize: TrProps['fontSize'],
42
- lineHeight: number,
43
- letterSpacing: TrProps['letterSpacing'],
44
- /**
45
- * Mutated
46
- */
47
- vertexBuffer: NonNullable<SdfTextRendererState['vertexBuffer']>,
48
- contain: TrProps['contain'],
49
- /**
50
- * Mutated
51
- */
52
- lineCache: SdfTextRendererState['lineCache'],
53
- rwSdf: Bound,
54
- trFontFace: SdfTextRendererState['trFontFace'],
55
- forceFullLayoutCalc: TextRendererState['forceFullLayoutCalc'],
56
- scrollable: TrProps['scrollable'],
57
- overflowSuffix: TrProps['overflowSuffix'],
58
- maxLines: TrProps['maxLines'],
59
- ): {
60
- bufferNumFloats: number;
61
- bufferNumQuads: number;
62
- layoutNumCharacters: number;
63
- fullyProcessed: boolean;
64
- maxX: number;
65
- maxY: number;
66
- numLines: number;
67
- } {
68
- assertTruthy(trFontFace, 'Font face must be loaded');
69
- assertTruthy(trFontFace.loaded, 'Font face must be loaded');
70
- assertTruthy(trFontFace.data, 'Font face must be loaded');
71
- assertTruthy(trFontFace.shaper, 'Font face must be loaded');
72
-
73
- // Regardless of fontSize (or other scaling properties), we layout the vertices of each glyph
74
- // using the fixed coordinate space determined by font size used to produce the atlas.
75
- // Scaling for display is handled by shader uniforms inexpensively.
76
- // So we have:
77
- // - vertex space: the space in which the vertices of each glyph are laid out
78
- // - screen space: the screen pixel space
79
- // Input properties such as x, y, w, fontSize, letterSpacing, etc. are all expressed in screen space.
80
- // We convert these to the vertex space by dividing them the `fontSizeRatio` factor.
81
-
82
- /**
83
- * See above
84
- */
85
- const fontSizeRatio = fontSize / trFontFace.data.info.size;
86
-
87
- /**
88
- * `lineHeight` in vertex coordinates
89
- */
90
- const vertexLineHeight = lineHeight / fontSizeRatio;
91
- /**
92
- * `w` in vertex coordinates
93
- */
94
- const vertexW = width / fontSizeRatio;
95
- /**
96
- * `letterSpacing` in vertex coordinates
97
- */
98
- const vertexLSpacing = letterSpacing / fontSizeRatio;
99
-
100
- const startingLineCacheEntry = lineCache[curLineIndex];
101
- const startingCodepointIndex = startingLineCacheEntry?.codepointIndex || 0;
102
- const startingMaxX = startingLineCacheEntry?.maxX || 0;
103
- const startingMaxY = startingLineCacheEntry?.maxY || 0;
104
-
105
- let maxX = startingMaxX;
106
- let maxY = startingMaxY;
107
- let curX = startX;
108
- let curY = startY;
109
-
110
- let bufferOffset = 0;
111
- /**
112
- * Buffer offset to last word boundry. This is -1 when we aren't in a word boundry.
113
- */
114
- const lastWord: {
115
- codepointIndex: number;
116
- bufferOffset: number;
117
- xStart: number;
118
- } = {
119
- codepointIndex: -1,
120
- bufferOffset: -1,
121
- xStart: -1,
122
- };
123
-
124
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
125
- const shaper = trFontFace.shaper;
126
-
127
- const shaperProps: FontShaperProps = {
128
- letterSpacing: vertexLSpacing,
129
- };
130
-
131
- // Get glyphs
132
- let glyphs = shaper.shapeText(
133
- shaperProps,
134
- new PeekableIterator(
135
- getUnicodeCodepoints(text, startingCodepointIndex),
136
- startingCodepointIndex,
137
- ),
138
- );
139
-
140
- let glyphResult:
141
- | IteratorResult<MappedGlyphInfo | UnmappedCharacterInfo, void>
142
- | undefined;
143
-
144
- let curLineBufferStart = -1;
145
-
146
- const bufferLineInfos: {
147
- bufferStart: number;
148
- bufferEnd: number;
149
- }[] = [];
150
-
151
- const vertexTruncateHeight = height / fontSizeRatio;
152
- const overflowSuffVertexWidth = measureText(
153
- overflowSuffix,
154
- shaperProps,
155
- shaper,
156
- );
157
-
158
- // Line-by-line layout
159
- let moreLines = true;
160
- while (moreLines) {
161
- const nextLineWillFit =
162
- (maxLines === 0 || curLineIndex + 1 < maxLines) &&
163
- (contain !== 'both' ||
164
- scrollable ||
165
- curY + vertexLineHeight + trFontFace.maxCharHeight <=
166
- vertexTruncateHeight);
167
- const lineVertexW = nextLineWillFit
168
- ? vertexW
169
- : vertexW - overflowSuffVertexWidth;
170
- /**
171
- * Vertex X position to the beginning of the last word boundary. This becomes -1 when we start traversing a word.
172
- */
173
- let xStartLastWordBoundary = 0;
174
-
175
- const lineIsBelowWindowTop = curY + trFontFace.maxCharHeight >= rwSdf.y1;
176
- const lineIsAboveWindowBottom = curY <= rwSdf.y2;
177
- const lineIsWithinWindow = lineIsBelowWindowTop && lineIsAboveWindowBottom;
178
- // Layout glyphs in this line
179
- // Any break statements in this while loop will trigger a line break
180
- while ((glyphResult = glyphs.next()) && !glyphResult.done) {
181
- const glyph = glyphResult.value;
182
-
183
- if (curLineIndex === lineCache.length) {
184
- lineCache.push({
185
- codepointIndex: glyph.cluster,
186
- maxY,
187
- maxX,
188
- });
189
- } else if (curLineIndex > lineCache.length) {
190
- throw new Error('Unexpected lineCache length');
191
- }
192
-
193
- // If we encounter a word boundary (white space or newline) we invalidate
194
- // the lastWord and set the xStartLastWordBoundary if we haven't already.
195
- if (
196
- glyph.codepoint === 32 ||
197
- glyph.codepoint === 10 ||
198
- glyph.codepoint === 8203
199
- ) {
200
- if (lastWord.codepointIndex !== -1) {
201
- lastWord.codepointIndex = -1;
202
- xStartLastWordBoundary = curX;
203
- }
204
- } else if (lastWord.codepointIndex === -1) {
205
- lastWord.codepointIndex = glyph.cluster;
206
- lastWord.bufferOffset = bufferOffset;
207
- lastWord.xStart = xStartLastWordBoundary;
208
- }
209
-
210
- if (glyph.mapped) {
211
- // Mapped glyph
212
- const charEndX = curX + glyph.xOffset + glyph.width;
213
- // Word wrap check
214
- if (
215
- // We are containing the text
216
- contain !== 'none' &&
217
- // The current glyph reaches outside the contained width
218
- charEndX >= lineVertexW &&
219
- // There is a last word that we can break to the next line
220
- lastWord.codepointIndex !== -1 &&
221
- // Prevents infinite loop when a single word is longer than the width
222
- lastWord.xStart > 0
223
- ) {
224
- // The current word is about to go off the edge of the container width
225
- // Reinitialize the iterator starting at the last word
226
- // and proceeding to the next line
227
- if (nextLineWillFit) {
228
- glyphs = shaper.shapeText(
229
- shaperProps,
230
- new PeekableIterator(
231
- getUnicodeCodepoints(text, lastWord.codepointIndex),
232
- lastWord.codepointIndex,
233
- ),
234
- );
235
- bufferOffset = lastWord.bufferOffset;
236
- break;
237
- } else {
238
- glyphs = shaper.shapeText(
239
- shaperProps,
240
- new PeekableIterator(getUnicodeCodepoints(overflowSuffix, 0), 0),
241
- );
242
- curX = lastWord.xStart;
243
- bufferOffset = lastWord.bufferOffset;
244
- // HACK: For the rest of the line when inserting the overflow suffix,
245
- // set contain = 'none' to prevent an infinite loop.
246
- contain = 'none';
247
- }
248
- } else {
249
- // This glyph fits, so we can add it to the buffer
250
- const quadX = curX + glyph.xOffset;
251
- const quadY = curY + glyph.yOffset;
252
-
253
- // Only add to buffer for rendering if the line is within the render window
254
- if (lineIsWithinWindow) {
255
- if (curLineBufferStart === -1) {
256
- curLineBufferStart = bufferOffset;
257
- }
258
-
259
- const atlasEntry = trFontFace.getAtlasEntry(glyph.glyphId);
260
-
261
- // Add texture coordinates
262
- const u = atlasEntry.x / trFontFace.data.common.scaleW;
263
- const v = atlasEntry.y / trFontFace.data.common.scaleH;
264
- const uvWidth = atlasEntry.width / trFontFace.data.common.scaleW;
265
- const uvHeight = atlasEntry.height / trFontFace.data.common.scaleH;
266
-
267
- // TODO: (Performance) We can optimize this by using ELEMENT_ARRAY_BUFFER
268
- // eliminating the need to duplicate vertices
269
-
270
- // Top-left
271
- vertexBuffer[bufferOffset++] = quadX;
272
- vertexBuffer[bufferOffset++] = quadY;
273
- vertexBuffer[bufferOffset++] = u;
274
- vertexBuffer[bufferOffset++] = v;
275
-
276
- // Top-right
277
- vertexBuffer[bufferOffset++] = quadX + glyph.width;
278
- vertexBuffer[bufferOffset++] = quadY;
279
- vertexBuffer[bufferOffset++] = u + uvWidth;
280
- vertexBuffer[bufferOffset++] = v;
281
-
282
- // Bottom-left
283
- vertexBuffer[bufferOffset++] = quadX;
284
- vertexBuffer[bufferOffset++] = quadY + glyph.height;
285
- vertexBuffer[bufferOffset++] = u;
286
- vertexBuffer[bufferOffset++] = v + uvHeight;
287
-
288
- // Bottom-right
289
- vertexBuffer[bufferOffset++] = quadX + glyph.width;
290
- vertexBuffer[bufferOffset++] = quadY + glyph.height;
291
- vertexBuffer[bufferOffset++] = u + uvWidth;
292
- vertexBuffer[bufferOffset++] = v + uvHeight;
293
- }
294
-
295
- maxY = Math.max(maxY, quadY + glyph.height);
296
- maxX = Math.max(maxX, quadX + glyph.width);
297
- curX += glyph.xAdvance;
298
- }
299
- } else {
300
- // Unmapped character
301
-
302
- // Handle newlines
303
- if (glyph.codepoint === 10) {
304
- if (nextLineWillFit) {
305
- // The whole line fit, so we can break to the next line
306
- break;
307
- } else {
308
- // The whole line won't fit, so we need to add the overflow suffix
309
- glyphs = shaper.shapeText(
310
- shaperProps,
311
- new PeekableIterator(getUnicodeCodepoints(overflowSuffix, 0), 0),
312
- );
313
- // HACK: For the rest of the line when inserting the overflow suffix,
314
- // set contain = 'none' to prevent an infinite loop.
315
- contain = 'none';
316
- }
317
- }
318
- }
319
- }
320
-
321
- // Prepare for the next line...
322
- if (curLineBufferStart !== -1) {
323
- bufferLineInfos.push({
324
- bufferStart: curLineBufferStart,
325
- bufferEnd: bufferOffset,
326
- });
327
- curLineBufferStart = -1;
328
- }
329
- curX = 0;
330
- curY += vertexLineHeight;
331
- curLineIndex++;
332
- lastWord.codepointIndex = -1;
333
- xStartLastWordBoundary = 0;
334
-
335
- // Figure out if there are any more lines to render...
336
- if (!forceFullLayoutCalc && contain === 'both' && curY > rwSdf.y2) {
337
- // Stop layout calculation early (for performance purposes) if:
338
- // - We're not forcing a full layout calculation (for width/height calculation)
339
- // - ...and we're containing the text vertically+horizontally (contain === 'both')
340
- // - ...and we have a render window
341
- // - ...and the next line is below the bottom of the render window
342
- moreLines = false;
343
- } else if (glyphResult && glyphResult.done) {
344
- // If we've reached the end of the text, we know we're done
345
- moreLines = false;
346
- } else if (!nextLineWillFit) {
347
- // If we're contained vertically+horizontally (contain === 'both')
348
- // but not scrollable and the next line won't fit, we're done.
349
- moreLines = false;
350
- }
351
- }
352
-
353
- // Use textAlign to determine if we need to adjust the x position of the text
354
- // in the buffer line by line
355
- if (textAlign === 'center') {
356
- const vertexTextW = contain === 'none' ? maxX : vertexW;
357
-
358
- for (let i = 0; i < bufferLineInfos.length; i++) {
359
- const line = bufferLineInfos[i]!;
360
- // - 4 = the x position of a rightmost vertex
361
- const lineWidth =
362
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
363
- vertexBuffer[line.bufferEnd - 4]! - vertexBuffer[line.bufferStart]!;
364
-
365
- const xOffset = (vertexTextW - lineWidth) / 2;
366
- for (let j = line.bufferStart; j < line.bufferEnd; j += 4) {
367
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
368
- vertexBuffer[j]! += xOffset;
369
- }
370
- }
371
- } else if (textAlign === 'right') {
372
- const vertexTextW = contain === 'none' ? maxX : vertexW;
373
-
374
- for (let i = 0; i < bufferLineInfos.length; i++) {
375
- const line = bufferLineInfos[i]!;
376
- const lineWidth =
377
- line.bufferEnd === line.bufferStart
378
- ? 0
379
- : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
380
- vertexBuffer[line.bufferEnd - 4]! - vertexBuffer[line.bufferStart]!;
381
-
382
- const xOffset = vertexTextW - lineWidth;
383
- for (let j = line.bufferStart; j < line.bufferEnd; j += 4) {
384
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
385
- vertexBuffer[j]! += xOffset;
386
- }
387
- }
388
- }
389
-
390
- assertTruthy(glyphResult);
391
-
392
- return {
393
- bufferNumFloats: bufferOffset,
394
- bufferNumQuads: bufferOffset / 16,
395
- layoutNumCharacters: glyphResult.done
396
- ? text.length - startingCodepointIndex
397
- : glyphResult.value.cluster - startingCodepointIndex + 1,
398
- fullyProcessed: !!glyphResult.done,
399
- maxX,
400
- maxY,
401
- numLines: lineCache.length,
402
- };
403
- }
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 2023 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 { assertTruthy } from '../../../../../utils.js';
21
+ import type { Bound } from '../../../../lib/utils.js';
22
+ import type {
23
+ FontShaperProps,
24
+ MappedGlyphInfo,
25
+ UnmappedCharacterInfo,
26
+ } from '../../../font-face-types/SdfTrFontFace/internal/FontShaper.js';
27
+ import type { TrProps, TextRendererState } from '../../TextRenderer.js';
28
+ import type { SdfTextRendererState } from '../SdfTextRenderer.js';
29
+ import { PeekableIterator } from './PeekableGenerator.js';
30
+ import { getUnicodeCodepoints } from './getUnicodeCodepoints.js';
31
+ import { measureText } from './measureText.js';
32
+
33
+ export function layoutText(
34
+ curLineIndex: number,
35
+ startX: number,
36
+ startY: number,
37
+ text: TrProps['text'],
38
+ textAlign: TrProps['textAlign'],
39
+ width: TrProps['width'],
40
+ height: TrProps['height'],
41
+ fontSize: TrProps['fontSize'],
42
+ lineHeight: number,
43
+ letterSpacing: TrProps['letterSpacing'],
44
+ /**
45
+ * Mutated
46
+ */
47
+ vertexBuffer: NonNullable<SdfTextRendererState['vertexBuffer']>,
48
+ contain: TrProps['contain'],
49
+ /**
50
+ * Mutated
51
+ */
52
+ lineCache: SdfTextRendererState['lineCache'],
53
+ rwSdf: Bound,
54
+ trFontFace: SdfTextRendererState['trFontFace'],
55
+ forceFullLayoutCalc: TextRendererState['forceFullLayoutCalc'],
56
+ scrollable: TrProps['scrollable'],
57
+ overflowSuffix: TrProps['overflowSuffix'],
58
+ maxLines: TrProps['maxLines'],
59
+ ): {
60
+ bufferNumFloats: number;
61
+ bufferNumQuads: number;
62
+ layoutNumCharacters: number;
63
+ fullyProcessed: boolean;
64
+ maxX: number;
65
+ maxY: number;
66
+ numLines: number;
67
+ } {
68
+ assertTruthy(trFontFace, 'Font face must be loaded');
69
+ assertTruthy(trFontFace.loaded, 'Font face must be loaded');
70
+ assertTruthy(trFontFace.data, 'Font face must be loaded');
71
+ assertTruthy(trFontFace.shaper, 'Font face must be loaded');
72
+
73
+ // Regardless of fontSize (or other scaling properties), we layout the vertices of each glyph
74
+ // using the fixed coordinate space determined by font size used to produce the atlas.
75
+ // Scaling for display is handled by shader uniforms inexpensively.
76
+ // So we have:
77
+ // - vertex space: the space in which the vertices of each glyph are laid out
78
+ // - screen space: the screen pixel space
79
+ // Input properties such as x, y, w, fontSize, letterSpacing, etc. are all expressed in screen space.
80
+ // We convert these to the vertex space by dividing them the `fontSizeRatio` factor.
81
+
82
+ /**
83
+ * See above
84
+ */
85
+ const fontSizeRatio = fontSize / trFontFace.data.info.size;
86
+
87
+ /**
88
+ * `lineHeight` in vertex coordinates
89
+ */
90
+ const vertexLineHeight = lineHeight / fontSizeRatio;
91
+ /**
92
+ * `w` in vertex coordinates
93
+ */
94
+ const vertexW = width / fontSizeRatio;
95
+ /**
96
+ * `letterSpacing` in vertex coordinates
97
+ */
98
+ const vertexLSpacing = letterSpacing / fontSizeRatio;
99
+
100
+ const startingLineCacheEntry = lineCache[curLineIndex];
101
+ const startingCodepointIndex = startingLineCacheEntry?.codepointIndex || 0;
102
+ const startingMaxX = startingLineCacheEntry?.maxX || 0;
103
+ const startingMaxY = startingLineCacheEntry?.maxY || 0;
104
+
105
+ let maxX = startingMaxX;
106
+ let maxY = startingMaxY;
107
+ let curX = startX;
108
+ let curY = startY;
109
+
110
+ let bufferOffset = 0;
111
+ /**
112
+ * Buffer offset to last word boundry. This is -1 when we aren't in a word boundry.
113
+ */
114
+ const lastWord: {
115
+ codepointIndex: number;
116
+ bufferOffset: number;
117
+ xStart: number;
118
+ } = {
119
+ codepointIndex: -1,
120
+ bufferOffset: -1,
121
+ xStart: -1,
122
+ };
123
+
124
+ const shaper = trFontFace.shaper;
125
+
126
+ const shaperProps: FontShaperProps = {
127
+ letterSpacing: vertexLSpacing,
128
+ };
129
+
130
+ // HACK: The space is used as a word boundary. When a text ends with a space, we need to
131
+ // add an extra space to ensure the space is included in the line width calculation.
132
+ if (text.endsWith(' ')) {
133
+ text += ' ';
134
+ }
135
+
136
+ // Get glyphs
137
+ let glyphs = shaper.shapeText(
138
+ shaperProps,
139
+ new PeekableIterator(
140
+ getUnicodeCodepoints(text, startingCodepointIndex),
141
+ startingCodepointIndex,
142
+ ),
143
+ );
144
+
145
+ let glyphResult:
146
+ | IteratorResult<MappedGlyphInfo | UnmappedCharacterInfo, void>
147
+ | undefined;
148
+
149
+ let curLineBufferStart = -1;
150
+
151
+ const bufferLineInfos: {
152
+ bufferStart: number;
153
+ bufferEnd: number;
154
+ }[] = [];
155
+
156
+ const vertexTruncateHeight = height / fontSizeRatio;
157
+ const overflowSuffVertexWidth = measureText(
158
+ overflowSuffix,
159
+ shaperProps,
160
+ shaper,
161
+ );
162
+
163
+ // Line-by-line layout
164
+ let moreLines = true;
165
+ while (moreLines) {
166
+ const nextLineWillFit =
167
+ (maxLines === 0 || curLineIndex + 1 < maxLines) &&
168
+ (contain !== 'both' ||
169
+ scrollable ||
170
+ curY + vertexLineHeight + trFontFace.maxCharHeight <=
171
+ vertexTruncateHeight);
172
+ const lineVertexW = nextLineWillFit
173
+ ? vertexW
174
+ : vertexW - overflowSuffVertexWidth;
175
+ /**
176
+ * Vertex X position to the beginning of the last word boundary. This becomes -1 when we start traversing a word.
177
+ */
178
+ let xStartLastWordBoundary = 0;
179
+
180
+ const lineIsBelowWindowTop = curY + trFontFace.maxCharHeight >= rwSdf.y1;
181
+ const lineIsAboveWindowBottom = curY <= rwSdf.y2;
182
+ const lineIsWithinWindow = lineIsBelowWindowTop && lineIsAboveWindowBottom;
183
+ // Layout glyphs in this line
184
+ // Any break statements in this while loop will trigger a line break
185
+ while ((glyphResult = glyphs.next()) && !glyphResult.done) {
186
+ const glyph = glyphResult.value;
187
+
188
+ if (curLineIndex === lineCache.length) {
189
+ lineCache.push({
190
+ codepointIndex: glyph.cluster,
191
+ maxY,
192
+ maxX,
193
+ });
194
+ } else if (curLineIndex > lineCache.length) {
195
+ throw new Error('Unexpected lineCache length');
196
+ }
197
+
198
+ // If we encounter a word boundary (white space or newline) we invalidate
199
+ // the lastWord and set the xStartLastWordBoundary if we haven't already.
200
+ if (
201
+ glyph.codepoint === 32 ||
202
+ glyph.codepoint === 10 ||
203
+ glyph.codepoint === 8203
204
+ ) {
205
+ if (lastWord.codepointIndex !== -1) {
206
+ lastWord.codepointIndex = -1;
207
+ xStartLastWordBoundary = curX;
208
+ }
209
+ } else if (lastWord.codepointIndex === -1) {
210
+ lastWord.codepointIndex = glyph.cluster;
211
+ lastWord.bufferOffset = bufferOffset;
212
+ lastWord.xStart = xStartLastWordBoundary;
213
+ }
214
+
215
+ if (glyph.mapped) {
216
+ // Mapped glyph
217
+ const charEndX = curX + glyph.xOffset + glyph.width;
218
+ // Word wrap check
219
+ if (
220
+ // We are containing the text
221
+ contain !== 'none' &&
222
+ // The current glyph reaches outside the contained width
223
+ charEndX >= lineVertexW &&
224
+ // There is a last word that we can break to the next line
225
+ lastWord.codepointIndex !== -1 &&
226
+ // Prevents infinite loop when a single word is longer than the width
227
+ lastWord.xStart > 0
228
+ ) {
229
+ // The current word is about to go off the edge of the container width
230
+ // Reinitialize the iterator starting at the last word
231
+ // and proceeding to the next line
232
+ if (nextLineWillFit) {
233
+ glyphs = shaper.shapeText(
234
+ shaperProps,
235
+ new PeekableIterator(
236
+ getUnicodeCodepoints(text, lastWord.codepointIndex),
237
+ lastWord.codepointIndex,
238
+ ),
239
+ );
240
+ bufferOffset = lastWord.bufferOffset;
241
+ break;
242
+ } else {
243
+ glyphs = shaper.shapeText(
244
+ shaperProps,
245
+ new PeekableIterator(getUnicodeCodepoints(overflowSuffix, 0), 0),
246
+ );
247
+ curX = lastWord.xStart;
248
+ bufferOffset = lastWord.bufferOffset;
249
+ // HACK: For the rest of the line when inserting the overflow suffix,
250
+ // set contain = 'none' to prevent an infinite loop.
251
+ contain = 'none';
252
+ }
253
+ } else {
254
+ // This glyph fits, so we can add it to the buffer
255
+ const quadX = curX + glyph.xOffset;
256
+ const quadY = curY + glyph.yOffset;
257
+
258
+ // Only add to buffer for rendering if the line is within the render window
259
+ if (lineIsWithinWindow) {
260
+ if (curLineBufferStart === -1) {
261
+ curLineBufferStart = bufferOffset;
262
+ }
263
+
264
+ const atlasEntry = trFontFace.getAtlasEntry(glyph.glyphId);
265
+
266
+ // Add texture coordinates
267
+ const u = atlasEntry.x / trFontFace.data.common.scaleW;
268
+ const v = atlasEntry.y / trFontFace.data.common.scaleH;
269
+ const uvWidth = atlasEntry.width / trFontFace.data.common.scaleW;
270
+ const uvHeight = atlasEntry.height / trFontFace.data.common.scaleH;
271
+
272
+ // TODO: (Performance) We can optimize this by using ELEMENT_ARRAY_BUFFER
273
+ // eliminating the need to duplicate vertices
274
+
275
+ // Top-left
276
+ vertexBuffer[bufferOffset++] = quadX;
277
+ vertexBuffer[bufferOffset++] = quadY;
278
+ vertexBuffer[bufferOffset++] = u;
279
+ vertexBuffer[bufferOffset++] = v;
280
+
281
+ // Top-right
282
+ vertexBuffer[bufferOffset++] = quadX + glyph.width;
283
+ vertexBuffer[bufferOffset++] = quadY;
284
+ vertexBuffer[bufferOffset++] = u + uvWidth;
285
+ vertexBuffer[bufferOffset++] = v;
286
+
287
+ // Bottom-left
288
+ vertexBuffer[bufferOffset++] = quadX;
289
+ vertexBuffer[bufferOffset++] = quadY + glyph.height;
290
+ vertexBuffer[bufferOffset++] = u;
291
+ vertexBuffer[bufferOffset++] = v + uvHeight;
292
+
293
+ // Bottom-right
294
+ vertexBuffer[bufferOffset++] = quadX + glyph.width;
295
+ vertexBuffer[bufferOffset++] = quadY + glyph.height;
296
+ vertexBuffer[bufferOffset++] = u + uvWidth;
297
+ vertexBuffer[bufferOffset++] = v + uvHeight;
298
+ }
299
+
300
+ maxY = Math.max(maxY, quadY + glyph.height);
301
+ maxX = Math.max(maxX, quadX + glyph.width);
302
+ curX += glyph.xAdvance;
303
+ }
304
+ } else {
305
+ // Unmapped character
306
+
307
+ // Handle newlines
308
+ if (glyph.codepoint === 10) {
309
+ if (nextLineWillFit) {
310
+ // The whole line fit, so we can break to the next line
311
+ break;
312
+ } else {
313
+ // The whole line won't fit, so we need to add the overflow suffix
314
+ glyphs = shaper.shapeText(
315
+ shaperProps,
316
+ new PeekableIterator(getUnicodeCodepoints(overflowSuffix, 0), 0),
317
+ );
318
+ // HACK: For the rest of the line when inserting the overflow suffix,
319
+ // set contain = 'none' to prevent an infinite loop.
320
+ contain = 'none';
321
+ }
322
+ }
323
+ }
324
+ }
325
+
326
+ // Prepare for the next line...
327
+ if (curLineBufferStart !== -1) {
328
+ bufferLineInfos.push({
329
+ bufferStart: curLineBufferStart,
330
+ bufferEnd: bufferOffset,
331
+ });
332
+ curLineBufferStart = -1;
333
+ }
334
+ curX = 0;
335
+ curY += vertexLineHeight;
336
+ curLineIndex++;
337
+ lastWord.codepointIndex = -1;
338
+ xStartLastWordBoundary = 0;
339
+
340
+ // Figure out if there are any more lines to render...
341
+ if (!forceFullLayoutCalc && contain === 'both' && curY > rwSdf.y2) {
342
+ // Stop layout calculation early (for performance purposes) if:
343
+ // - We're not forcing a full layout calculation (for width/height calculation)
344
+ // - ...and we're containing the text vertically+horizontally (contain === 'both')
345
+ // - ...and we have a render window
346
+ // - ...and the next line is below the bottom of the render window
347
+ moreLines = false;
348
+ } else if (glyphResult && glyphResult.done) {
349
+ // If we've reached the end of the text, we know we're done
350
+ moreLines = false;
351
+ } else if (!nextLineWillFit) {
352
+ // If we're contained vertically+horizontally (contain === 'both')
353
+ // but not scrollable and the next line won't fit, we're done.
354
+ moreLines = false;
355
+ }
356
+ }
357
+
358
+ // Use textAlign to determine if we need to adjust the x position of the text
359
+ // in the buffer line by line
360
+ if (textAlign === 'center') {
361
+ const vertexTextW = contain === 'none' ? maxX : vertexW;
362
+
363
+ for (let i = 0; i < bufferLineInfos.length; i++) {
364
+ const line = bufferLineInfos[i]!;
365
+ // - 4 = the x position of a rightmost vertex
366
+ const lineWidth =
367
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
368
+ vertexBuffer[line.bufferEnd - 4]! - vertexBuffer[line.bufferStart]!;
369
+
370
+ const xOffset = (vertexTextW - lineWidth) / 2;
371
+ for (let j = line.bufferStart; j < line.bufferEnd; j += 4) {
372
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
373
+ vertexBuffer[j]! += xOffset;
374
+ }
375
+ }
376
+ } else if (textAlign === 'right') {
377
+ const vertexTextW = contain === 'none' ? maxX : vertexW;
378
+
379
+ for (let i = 0; i < bufferLineInfos.length; i++) {
380
+ const line = bufferLineInfos[i]!;
381
+ const lineWidth =
382
+ line.bufferEnd === line.bufferStart
383
+ ? 0
384
+ : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
385
+ vertexBuffer[line.bufferEnd - 4]! - vertexBuffer[line.bufferStart]!;
386
+
387
+ const xOffset = vertexTextW - lineWidth;
388
+ for (let j = line.bufferStart; j < line.bufferEnd; j += 4) {
389
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
390
+ vertexBuffer[j]! += xOffset;
391
+ }
392
+ }
393
+ }
394
+
395
+ assertTruthy(glyphResult);
396
+
397
+ return {
398
+ bufferNumFloats: bufferOffset,
399
+ bufferNumQuads: bufferOffset / 16,
400
+ layoutNumCharacters: glyphResult.done
401
+ ? text.length - startingCodepointIndex
402
+ : glyphResult.value.cluster - startingCodepointIndex + 1,
403
+ fullyProcessed: !!glyphResult.done,
404
+ maxX,
405
+ maxY,
406
+ numLines: lineCache.length,
407
+ };
408
+ }