@lightningjs/renderer 0.7.1 → 0.7.3

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