@lightningjs/renderer 3.0.0-beta10 → 3.0.0-beta11

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 (275) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +133 -133
  4. package/dist/src/core/CoreTextNode.d.ts +3 -0
  5. package/dist/src/core/CoreTextNode.js +45 -10
  6. package/dist/src/core/CoreTextNode.js.map +1 -1
  7. package/dist/src/core/platform.d.ts +10 -0
  8. package/dist/src/core/platform.js +81 -0
  9. package/dist/src/core/platform.js.map +1 -0
  10. package/dist/src/core/renderers/CoreShader.d.ts +9 -0
  11. package/dist/src/core/renderers/CoreShader.js +28 -0
  12. package/dist/src/core/renderers/CoreShader.js.map +1 -0
  13. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +33 -0
  14. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +250 -0
  15. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -0
  16. package/dist/src/core/renderers/canvas/CanvasCoreTexture.d.ts +16 -0
  17. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +124 -0
  18. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -0
  19. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.d.ts +13 -0
  20. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js +113 -192
  21. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js.map +1 -1
  22. package/dist/src/core/renderers/canvas/internal/ColorUtils.d.ts +0 -2
  23. package/dist/src/core/renderers/canvas/internal/ColorUtils.js +0 -14
  24. package/dist/src/core/renderers/canvas/internal/ColorUtils.js.map +1 -1
  25. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.d.ts +10 -0
  26. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js +43 -0
  27. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js.map +1 -0
  28. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.d.ts +12 -0
  29. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js +58 -0
  30. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js.map +1 -0
  31. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.d.ts +9 -0
  32. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js +38 -0
  33. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js.map +1 -0
  34. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +56 -0
  35. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +239 -0
  36. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -0
  37. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.d.ts +34 -0
  38. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js +114 -0
  39. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js.map +1 -0
  40. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +133 -0
  41. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +616 -0
  42. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -0
  43. package/dist/src/core/renderers/webgl/WebGlCoreShader.d.ts +83 -0
  44. package/dist/src/core/renderers/webgl/WebGlCoreShader.js +233 -0
  45. package/dist/src/core/renderers/webgl/WebGlCoreShader.js.map +1 -0
  46. package/dist/src/core/renderers/webgl/internal/ShaderUtils.js +35 -35
  47. package/dist/src/core/renderers/webgl/shaders/DefaultShader.d.ts +9 -0
  48. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +87 -0
  49. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js.map +1 -0
  50. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.d.ts +10 -0
  51. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +119 -0
  52. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js.map +1 -0
  53. package/dist/src/core/renderers/webgl/shaders/DynamicShader.d.ts +29 -0
  54. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +413 -0
  55. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js.map +1 -0
  56. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.d.ts +28 -0
  57. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +131 -0
  58. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js.map +1 -0
  59. package/dist/src/core/renderers/webgl/shaders/SdfShader.d.ts +47 -0
  60. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +160 -0
  61. package/dist/src/core/renderers/webgl/shaders/SdfShader.js.map +1 -0
  62. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.d.ts +31 -0
  63. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +71 -0
  64. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js.map +1 -0
  65. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.d.ts +30 -0
  66. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +58 -0
  67. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js.map +1 -0
  68. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.d.ts +31 -0
  69. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +71 -0
  70. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js.map +1 -0
  71. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.d.ts +31 -0
  72. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +71 -0
  73. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js.map +1 -0
  74. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.d.ts +31 -0
  75. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +71 -0
  76. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js.map +1 -0
  77. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.d.ts +9 -0
  78. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.js +136 -0
  79. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.js.map +1 -0
  80. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.d.ts +36 -0
  81. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +85 -0
  82. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js.map +1 -0
  83. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.d.ts +45 -0
  84. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +104 -0
  85. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js.map +1 -0
  86. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.d.ts +22 -0
  87. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +45 -0
  88. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js.map +1 -0
  89. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.d.ts +58 -0
  90. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +80 -0
  91. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js.map +1 -0
  92. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.d.ts +35 -0
  93. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +129 -0
  94. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js.map +1 -0
  95. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.d.ts +39 -0
  96. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +116 -0
  97. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js.map +1 -0
  98. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.d.ts +61 -0
  99. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +127 -0
  100. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js.map +1 -0
  101. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.d.ts +40 -0
  102. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +71 -0
  103. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js.map +1 -0
  104. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.d.ts +115 -0
  105. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.js +61 -0
  106. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.js.map +1 -0
  107. package/dist/src/core/shaders/templates/shaderUtils.d.ts +5 -0
  108. package/dist/src/core/shaders/templates/shaderUtils.js +41 -0
  109. package/dist/src/core/shaders/templates/shaderUtils.js.map +1 -0
  110. package/dist/src/core/shaders/webgl/Border.js +82 -82
  111. package/dist/src/core/shaders/webgl/Default.js +47 -47
  112. package/dist/src/core/shaders/webgl/DefaultBatched.js +61 -61
  113. package/dist/src/core/shaders/webgl/HolePunch.js +32 -32
  114. package/dist/src/core/shaders/webgl/LinearGradient.js +36 -36
  115. package/dist/src/core/shaders/webgl/RadialGradient.js +33 -33
  116. package/dist/src/core/shaders/webgl/Rounded.js +71 -71
  117. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +111 -111
  118. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +130 -130
  119. package/dist/src/core/shaders/webgl/RoundedWithShadow.js +54 -54
  120. package/dist/src/core/shaders/webgl/SdfShader.js +62 -62
  121. package/dist/src/core/shaders/webgl/Shadow.js +83 -83
  122. package/dist/src/core/shaders/webgl/Spinner.d.ts +1 -0
  123. package/dist/src/core/shaders/webgl/Spinner.js +2 -0
  124. package/dist/src/core/shaders/webgl/Spinner.js.map +1 -0
  125. package/dist/src/core/text-rendering/CanvasFontHandler.d.ts +16 -0
  126. package/dist/src/core/text-rendering/CanvasFontHandler.js +29 -0
  127. package/dist/src/core/text-rendering/CanvasFontHandler.js.map +1 -1
  128. package/dist/src/core/text-rendering/SdfFontHandler.d.ts +15 -0
  129. package/dist/src/core/text-rendering/SdfFontHandler.js +34 -2
  130. package/dist/src/core/text-rendering/SdfFontHandler.js.map +1 -1
  131. package/dist/src/core/text-rendering/TextRenderer.d.ts +2 -0
  132. package/dist/src/core/text-rendering/TextTextureRendererUtils.js.map +1 -1
  133. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +2 -2
  134. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  135. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +0 -5
  136. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  137. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.d.ts +1 -7
  138. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js +2 -50
  139. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js.map +1 -1
  140. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.d.ts +3 -2
  141. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js +83 -42
  142. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
  143. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.d.ts +1 -1
  144. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +8 -66
  145. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
  146. package/dist/src/core/text-rendering/renderers/TextRenderer.d.ts +4 -14
  147. package/dist/src/core/text-rendering/renderers/TextRenderer.js +0 -3
  148. package/dist/src/core/text-rendering/renderers/TextRenderer.js.map +1 -1
  149. package/dist/src/core/text-rendering/sdf/PeekableGenerator.d.ts +12 -0
  150. package/dist/src/core/text-rendering/sdf/PeekableGenerator.js +61 -0
  151. package/dist/src/core/text-rendering/sdf/PeekableGenerator.js.map +1 -0
  152. package/dist/src/core/text-rendering/sdf/SimpleFontShaper.d.ts +45 -0
  153. package/dist/src/core/text-rendering/sdf/SimpleFontShaper.js +69 -0
  154. package/dist/src/core/text-rendering/sdf/SimpleFontShaper.js.map +1 -0
  155. package/dist/src/main-api/DynamicShaderController.d.ts +29 -0
  156. package/dist/src/main-api/DynamicShaderController.js +58 -0
  157. package/dist/src/main-api/DynamicShaderController.js.map +1 -0
  158. package/dist/src/main-api/ShaderController.d.ts +31 -0
  159. package/dist/src/main-api/ShaderController.js +37 -0
  160. package/dist/src/main-api/ShaderController.js.map +1 -0
  161. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  162. package/exports/canvas-shaders.ts +28 -28
  163. package/exports/canvas.ts +45 -45
  164. package/exports/index.ts +82 -82
  165. package/exports/inspector.ts +24 -24
  166. package/exports/utils.ts +50 -50
  167. package/exports/webgl-shaders.ts +28 -28
  168. package/exports/webgl.ts +52 -52
  169. package/package.json +2 -1
  170. package/src/common/CommonTypes.ts +146 -146
  171. package/src/common/EventEmitter.ts +77 -77
  172. package/src/common/IAnimationController.ts +92 -92
  173. package/src/common/IEventEmitter.ts +28 -28
  174. package/src/core/CoreNode.test.ts +202 -202
  175. package/src/core/CoreNode.ts +2491 -2491
  176. package/src/core/CoreShaderManager.ts +188 -188
  177. package/src/core/CoreTextNode.ts +483 -443
  178. package/src/core/CoreTextureManager.ts +565 -565
  179. package/src/core/Stage.ts +906 -906
  180. package/src/core/TextureMemoryManager.ts +445 -445
  181. package/src/core/animations/AnimationManager.ts +38 -38
  182. package/src/core/animations/CoreAnimation.ts +291 -291
  183. package/src/core/animations/CoreAnimationController.ts +166 -166
  184. package/src/core/lib/ContextSpy.ts +41 -41
  185. package/src/core/lib/ImageWorker.ts +286 -286
  186. package/src/core/lib/Matrix3d.ts +244 -244
  187. package/src/core/lib/RenderCoords.ts +71 -71
  188. package/src/core/lib/WebGlContextWrapper.ts +1381 -1381
  189. package/src/core/lib/colorCache.ts +20 -20
  190. package/src/core/lib/colorParser.ts +85 -85
  191. package/src/core/lib/textureCompression.ts +152 -152
  192. package/src/core/lib/textureSvg.ts +78 -78
  193. package/src/core/lib/utils.ts +412 -412
  194. package/src/core/lib/validateImageBitmap.ts +87 -87
  195. package/src/core/platforms/Platform.ts +77 -77
  196. package/src/core/platforms/web/WebPlatform.ts +121 -121
  197. package/src/core/renderers/CoreContextTexture.ts +43 -43
  198. package/src/core/renderers/CoreRenderOp.ts +22 -22
  199. package/src/core/renderers/CoreRenderer.ts +110 -110
  200. package/src/core/renderers/CoreShaderNode.ts +175 -175
  201. package/src/core/renderers/CoreShaderProgram.ts +23 -23
  202. package/src/core/renderers/canvas/CanvasRenderer.ts +283 -283
  203. package/src/core/renderers/canvas/CanvasShaderNode.ts +96 -96
  204. package/src/core/renderers/canvas/CanvasTexture.ts +156 -156
  205. package/src/core/renderers/webgl/WebGlCtxRenderTexture.ts +91 -91
  206. package/src/core/renderers/webgl/WebGlCtxSubTexture.ts +50 -50
  207. package/src/core/renderers/webgl/WebGlCtxTexture.ts +310 -310
  208. package/src/core/renderers/webgl/WebGlRenderOp.ts +167 -167
  209. package/src/core/renderers/webgl/WebGlRenderer.ts +747 -747
  210. package/src/core/renderers/webgl/WebGlShaderNode.ts +435 -435
  211. package/src/core/renderers/webgl/WebGlShaderProgram.ts +341 -341
  212. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  213. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  214. package/src/core/renderers/webgl/internal/ShaderUtils.ts +281 -281
  215. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  216. package/src/core/shaders/canvas/Border.ts +75 -75
  217. package/src/core/shaders/canvas/HolePunch.ts +62 -62
  218. package/src/core/shaders/canvas/LinearGradient.ts +71 -71
  219. package/src/core/shaders/canvas/RadialGradient.ts +99 -99
  220. package/src/core/shaders/canvas/Rounded.ts +55 -55
  221. package/src/core/shaders/canvas/RoundedWithBorder.ts +74 -74
  222. package/src/core/shaders/canvas/RoundedWithBorderAndShadow.ts +90 -90
  223. package/src/core/shaders/canvas/RoundedWithShadow.ts +70 -70
  224. package/src/core/shaders/canvas/Shadow.ts +52 -52
  225. package/src/core/shaders/canvas/utils/render.ts +151 -151
  226. package/src/core/shaders/templates/BorderTemplate.ts +115 -115
  227. package/src/core/shaders/templates/HolePunchTemplate.ts +82 -82
  228. package/src/core/shaders/templates/LinearGradientTemplate.ts +71 -71
  229. package/src/core/shaders/templates/RadialGradientTemplate.ts +81 -81
  230. package/src/core/shaders/templates/RoundedTemplate.ts +98 -98
  231. package/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.ts +38 -38
  232. package/src/core/shaders/templates/RoundedWithBorderTemplate.ts +35 -35
  233. package/src/core/shaders/templates/RoundedWithShadowTemplate.ts +35 -35
  234. package/src/core/shaders/templates/ShadowTemplate.ts +106 -106
  235. package/src/core/shaders/utils.ts +46 -46
  236. package/src/core/shaders/webgl/Border.ts +116 -116
  237. package/src/core/shaders/webgl/Default.ts +89 -89
  238. package/src/core/shaders/webgl/DefaultBatched.ts +129 -129
  239. package/src/core/shaders/webgl/HolePunch.ts +75 -75
  240. package/src/core/shaders/webgl/LinearGradient.ts +82 -82
  241. package/src/core/shaders/webgl/RadialGradient.ts +85 -85
  242. package/src/core/shaders/webgl/Rounded.ts +117 -117
  243. package/src/core/shaders/webgl/RoundedWithBorder.ts +155 -155
  244. package/src/core/shaders/webgl/RoundedWithBorderAndShadow.ts +175 -175
  245. package/src/core/shaders/webgl/RoundedWithShadow.ts +98 -98
  246. package/src/core/shaders/webgl/SdfShader.ts +134 -134
  247. package/src/core/shaders/webgl/Shadow.ts +115 -115
  248. package/src/core/text-rendering/CanvasFontHandler.ts +210 -176
  249. package/src/core/text-rendering/CanvasTextRenderer.ts +622 -622
  250. package/src/core/text-rendering/SdfFontHandler.ts +554 -517
  251. package/src/core/text-rendering/SdfTextRenderer.ts +466 -466
  252. package/src/core/text-rendering/TextRenderer.ts +406 -404
  253. package/src/core/text-rendering/Utils.ts +257 -257
  254. package/src/core/text-rendering/canvas/Settings.ts +99 -99
  255. package/src/core/text-rendering/canvas/Utils.test.ts +206 -206
  256. package/src/core/text-rendering/canvas/Utils.ts +178 -178
  257. package/src/core/text-rendering/canvas/calculateRenderInfo.ts +299 -299
  258. package/src/core/text-rendering/canvas/draw.ts +165 -165
  259. package/src/core/text-rendering/sdf/Utils.test.ts +402 -402
  260. package/src/core/text-rendering/sdf/Utils.ts +436 -436
  261. package/src/core/text-rendering/sdf/index.ts +20 -20
  262. package/src/core/textures/ColorTexture.ts +102 -102
  263. package/src/core/textures/ImageTexture.ts +418 -418
  264. package/src/core/textures/NoiseTexture.ts +104 -104
  265. package/src/core/textures/RenderTexture.ts +85 -85
  266. package/src/core/textures/SubTexture.ts +205 -205
  267. package/src/core/textures/Texture.ts +381 -381
  268. package/src/core/utils.ts +227 -227
  269. package/src/env.d.ts +7 -7
  270. package/src/main-api/INode.ts +100 -100
  271. package/src/main-api/Inspector.ts +567 -567
  272. package/src/main-api/Renderer.ts +873 -873
  273. package/src/main-api/utils.ts +45 -45
  274. package/src/utils.ts +267 -267
  275. package/COPYING +0 -1
@@ -1,436 +1,436 @@
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 { isZeroWidthSpace } from '../Utils.js';
21
- import * as SdfFontHandler from '../SdfFontHandler.js';
22
- import type { TextLineStruct, WrappedLinesStruct } from '../TextRenderer.js';
23
-
24
- export const measureLines = (
25
- lines: string[],
26
- fontFamily: string,
27
- letterSpacing: number,
28
- fontScale: number,
29
- maxLines: number,
30
- hasMaxLines: boolean,
31
- ): WrappedLinesStruct => {
32
- const measuredLines: TextLineStruct[] = [];
33
- const designLetterSpacing = letterSpacing * fontScale;
34
- let remainingLines = hasMaxLines === true ? maxLines : lines.length;
35
- let i = 0;
36
-
37
- while (remainingLines > 0) {
38
- const line = lines[i];
39
- if (line === undefined) {
40
- continue;
41
- }
42
- const width = measureText(line, fontFamily, designLetterSpacing);
43
- measuredLines.push([line, width]);
44
- i++;
45
- remainingLines--;
46
- }
47
-
48
- return [
49
- measuredLines,
50
- remainingLines,
51
- hasMaxLines === true ? lines.length - measuredLines.length > 0 : false,
52
- ];
53
- };
54
- /**
55
- * Wrap text for SDF rendering with proper width constraints
56
- */
57
- export const wrapText = (
58
- text: string,
59
- fontFamily: string,
60
- fontScale: number,
61
- maxWidth: number,
62
- letterSpacing: number,
63
- overflowSuffix: string,
64
- wordBreak: string,
65
- maxLines: number,
66
- hasMaxLines: boolean,
67
- ): WrappedLinesStruct => {
68
- const lines = text.split('\n');
69
- const wrappedLines: TextLineStruct[] = [];
70
- const maxWidthInDesignUnits = maxWidth / fontScale;
71
- const designLetterSpacing = letterSpacing * fontScale;
72
-
73
- // Calculate space width for line wrapping
74
- const spaceWidth = measureText(' ', fontFamily, designLetterSpacing);
75
-
76
- let wrappedLine: TextLineStruct[] = [];
77
- let remainingLines = maxLines;
78
- let hasRemainingText = true;
79
-
80
- for (let i = 0; i < lines.length; i++) {
81
- const line = lines[i]!;
82
-
83
- [wrappedLine, remainingLines, hasRemainingText] = wrapLine(
84
- line,
85
- fontFamily,
86
- maxWidthInDesignUnits,
87
- designLetterSpacing,
88
- spaceWidth,
89
- overflowSuffix,
90
- wordBreak,
91
- remainingLines,
92
- hasMaxLines,
93
- );
94
-
95
- wrappedLines.push(...wrappedLine);
96
- }
97
-
98
- return [wrappedLines, remainingLines, hasRemainingText];
99
- };
100
-
101
- /**
102
- * Wrap a single line of text for SDF rendering
103
- */
104
- export const wrapLine = (
105
- line: string,
106
- fontFamily: string,
107
- maxWidth: number,
108
- designLetterSpacing: number,
109
- spaceWidth: number,
110
- overflowSuffix: string,
111
- wordBreak: string,
112
- remainingLines: number,
113
- hasMaxLines: boolean,
114
- ): WrappedLinesStruct => {
115
- // Use the same space regex as Canvas renderer to handle ZWSP
116
- const spaceRegex = / |\u200B/g;
117
- const words = line.split(spaceRegex);
118
- const spaces = line.match(spaceRegex) || [];
119
- const wrappedLines: TextLineStruct[] = [];
120
- let currentLine = '';
121
- let currentLineWidth = 0;
122
- let hasRemainingText = true;
123
-
124
- let i = 0;
125
-
126
- for (; i < words.length; i++) {
127
- const word = words[i];
128
- if (word === undefined) {
129
- continue;
130
- }
131
- const space = spaces[i - 1] || '';
132
- const wordWidth = measureText(word, fontFamily, designLetterSpacing);
133
- // For width calculation, treat ZWSP as having 0 width but regular space functionality
134
- const effectiveSpaceWidth = space === '\u200B' ? 0 : spaceWidth;
135
- const totalWidth = currentLineWidth + effectiveSpaceWidth + wordWidth;
136
-
137
- if (
138
- (i === 0 && wordWidth <= maxWidth) ||
139
- (i > 0 && totalWidth <= maxWidth)
140
- ) {
141
- // Word fits on current line
142
- if (currentLine.length > 0) {
143
- // Add space - for ZWSP, don't add anything to output (it's invisible)
144
- if (space !== '\u200B') {
145
- currentLine += space;
146
- currentLineWidth += effectiveSpaceWidth;
147
- }
148
- }
149
- currentLine += word;
150
- currentLineWidth += wordWidth;
151
- } else {
152
- if (remainingLines === 1) {
153
- if (currentLine.length > 0) {
154
- // Add space - for ZWSP, don't add anything to output (it's invisible)
155
- if (space !== '\u200B') {
156
- currentLine += space;
157
- currentLineWidth += effectiveSpaceWidth;
158
- }
159
- }
160
- currentLine += word;
161
- currentLineWidth += wordWidth;
162
- remainingLines = 0;
163
- hasRemainingText = i < words.length;
164
- break;
165
- }
166
-
167
- if (wordBreak !== 'break-all' && currentLine.length > 0) {
168
- wrappedLines.push([currentLine, currentLineWidth]);
169
- currentLine = '';
170
- currentLineWidth = 0;
171
- remainingLines--;
172
- }
173
-
174
- if (wordBreak !== 'break-all') {
175
- currentLine = word;
176
- currentLineWidth = wordWidth;
177
- }
178
-
179
- if (wordBreak === 'break-word') {
180
- const [lines, rl, rt] = breakWord(
181
- word,
182
- fontFamily,
183
- maxWidth,
184
- designLetterSpacing,
185
- remainingLines,
186
- hasMaxLines,
187
- );
188
- remainingLines = rl;
189
- hasRemainingText = rt;
190
- if (lines.length === 1) {
191
- [currentLine, currentLineWidth] = lines[lines.length - 1]!;
192
- } else {
193
- for (let j = 0; j < lines.length; j++) {
194
- [currentLine, currentLineWidth] = lines[j]!;
195
- if (j < lines.length - 1) {
196
- wrappedLines.push(lines[j]!);
197
- }
198
- }
199
- }
200
- } else if (wordBreak === 'break-all') {
201
- const codepoint = word.codePointAt(0)!;
202
- const glyph = SdfFontHandler.getGlyph(fontFamily, codepoint);
203
- const firstLetterWidth =
204
- glyph !== null ? glyph.xadvance + designLetterSpacing : 0;
205
- let linebreak = false;
206
- if (
207
- currentLineWidth + firstLetterWidth + effectiveSpaceWidth >
208
- maxWidth
209
- ) {
210
- wrappedLines.push([currentLine, currentLineWidth]);
211
- remainingLines -= 1;
212
- currentLine = '';
213
- currentLineWidth = 0;
214
- linebreak = true;
215
- }
216
- const initial = maxWidth - currentLineWidth;
217
- const [lines, rl, rt] = breakAll(
218
- word,
219
- fontFamily,
220
- initial,
221
- maxWidth,
222
- designLetterSpacing,
223
- remainingLines,
224
- hasMaxLines,
225
- );
226
- remainingLines = rl;
227
- hasRemainingText = rt;
228
- if (linebreak === false) {
229
- const [text, width] = lines[0]!;
230
- currentLine += ' ' + text;
231
- currentLineWidth = width;
232
- wrappedLines.push([currentLine, currentLineWidth]);
233
- }
234
-
235
- for (let j = 1; j < lines.length; j++) {
236
- [currentLine, currentLineWidth] = lines[j]!;
237
- if (j < lines.length - 1) {
238
- wrappedLines.push([currentLine, currentLineWidth]);
239
- }
240
- }
241
- }
242
- }
243
- }
244
-
245
- // Add the last line if it has content
246
- if (currentLine.length > 0 && remainingLines === 0) {
247
- currentLine = truncateLineWithSuffix(
248
- currentLine,
249
- fontFamily,
250
- maxWidth,
251
- designLetterSpacing,
252
- overflowSuffix,
253
- );
254
- }
255
-
256
- if (currentLine.length > 0) {
257
- wrappedLines.push([currentLine, currentLineWidth]);
258
- } else {
259
- wrappedLines.push(['', 0]);
260
- }
261
- return [wrappedLines, remainingLines, hasRemainingText];
262
- };
263
-
264
- /**
265
- * Measure the width of text in SDF design units
266
- */
267
- export const measureText = (
268
- text: string,
269
- fontFamily: string,
270
- designLetterSpacing: number,
271
- ): number => {
272
- let width = 0;
273
- let prevCodepoint = 0;
274
- for (let i = 0; i < text.length; i++) {
275
- const char = text.charAt(i);
276
- const codepoint = text.codePointAt(i);
277
- if (codepoint === undefined) continue;
278
-
279
- // Skip zero-width spaces in width calculations
280
- if (isZeroWidthSpace(char)) {
281
- continue;
282
- }
283
-
284
- const glyph = SdfFontHandler.getGlyph(fontFamily, codepoint);
285
- if (glyph === null) continue;
286
-
287
- let advance = glyph.xadvance;
288
-
289
- // Add kerning if there's a previous character
290
- if (prevCodepoint !== 0) {
291
- const kerning = SdfFontHandler.getKerning(
292
- fontFamily,
293
- prevCodepoint,
294
- codepoint,
295
- );
296
- advance += kerning;
297
- }
298
-
299
- width += advance + designLetterSpacing;
300
- prevCodepoint = codepoint;
301
- }
302
-
303
- return width;
304
- };
305
-
306
- /**
307
- * Truncate a line with overflow suffix to fit within width
308
- */
309
- export const truncateLineWithSuffix = (
310
- line: string,
311
- fontFamily: string,
312
- maxWidth: number,
313
- designLetterSpacing: number,
314
- overflowSuffix: string,
315
- ): string => {
316
- const suffixWidth = measureText(
317
- overflowSuffix,
318
- fontFamily,
319
- designLetterSpacing,
320
- );
321
-
322
- if (suffixWidth >= maxWidth) {
323
- return overflowSuffix.substring(0, Math.max(1, overflowSuffix.length - 1));
324
- }
325
-
326
- let truncatedLine = line;
327
- while (truncatedLine.length > 0) {
328
- const lineWidth = measureText(
329
- truncatedLine,
330
- fontFamily,
331
- designLetterSpacing,
332
- );
333
- if (lineWidth + suffixWidth <= maxWidth) {
334
- return truncatedLine + overflowSuffix;
335
- }
336
- truncatedLine = truncatedLine.substring(0, truncatedLine.length - 1);
337
- }
338
-
339
- return overflowSuffix;
340
- };
341
-
342
- /**
343
- * wordbreak function: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#break-word
344
- */
345
- export const breakWord = (
346
- word: string,
347
- fontFamily: string,
348
- maxWidth: number,
349
- designLetterSpacing: number,
350
- remainingLines: number,
351
- hasMaxLines: boolean,
352
- ): WrappedLinesStruct => {
353
- const lines: TextLineStruct[] = [];
354
- let currentPart = '';
355
- let currentWidth = 0;
356
- let i = 0;
357
-
358
- for (let i = 0; i < word.length; i++) {
359
- const char = word.charAt(i);
360
- const codepoint = char.codePointAt(0);
361
- if (codepoint === undefined) continue;
362
-
363
- const glyph = SdfFontHandler.getGlyph(fontFamily, codepoint);
364
- if (glyph === null) continue;
365
-
366
- const charWidth = glyph.xadvance + designLetterSpacing;
367
-
368
- if (currentWidth + charWidth > maxWidth && currentPart.length > 0) {
369
- remainingLines--;
370
- if (remainingLines === 0) {
371
- break;
372
- }
373
- lines.push([currentPart, currentWidth]);
374
- currentPart = char;
375
- currentWidth = charWidth;
376
- } else {
377
- currentPart += char;
378
- currentWidth += charWidth;
379
- }
380
- }
381
-
382
- if (currentPart.length > 0) {
383
- lines.push([currentPart, currentWidth]);
384
- }
385
-
386
- return [lines, remainingLines, i < word.length - 1];
387
- };
388
-
389
- /**
390
- * wordbreak function: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#break-word
391
- */
392
- export const breakAll = (
393
- word: string,
394
- fontFamily: string,
395
- initial: number,
396
- maxWidth: number,
397
- designLetterSpacing: number,
398
- remainingLines: number,
399
- hasMaxLines: boolean,
400
- ): WrappedLinesStruct => {
401
- const lines: TextLineStruct[] = [];
402
- let currentPart = '';
403
- let currentWidth = 0;
404
- let max = initial;
405
- let i = 0;
406
- let hasRemainingText = false;
407
-
408
- for (; i < word.length; i++) {
409
- if (remainingLines === 0) {
410
- hasRemainingText = true;
411
- break;
412
- }
413
- const char = word.charAt(i);
414
- const codepoint = char.codePointAt(0)!;
415
- const glyph = SdfFontHandler.getGlyph(fontFamily, codepoint);
416
- if (glyph === null) continue;
417
-
418
- const charWidth = glyph.xadvance + designLetterSpacing;
419
- if (currentWidth + charWidth > max && currentPart.length > 0) {
420
- lines.push([currentPart, currentWidth]);
421
- currentPart = char;
422
- currentWidth = charWidth;
423
- max = maxWidth;
424
- remainingLines--;
425
- } else {
426
- currentPart += char;
427
- currentWidth += charWidth;
428
- }
429
- }
430
-
431
- if (currentPart.length > 0) {
432
- lines.push([currentPart, currentWidth]);
433
- }
434
-
435
- return [lines, remainingLines, hasRemainingText];
436
- };
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 { isZeroWidthSpace } from '../Utils.js';
21
+ import * as SdfFontHandler from '../SdfFontHandler.js';
22
+ import type { TextLineStruct, WrappedLinesStruct } from '../TextRenderer.js';
23
+
24
+ export const measureLines = (
25
+ lines: string[],
26
+ fontFamily: string,
27
+ letterSpacing: number,
28
+ fontScale: number,
29
+ maxLines: number,
30
+ hasMaxLines: boolean,
31
+ ): WrappedLinesStruct => {
32
+ const measuredLines: TextLineStruct[] = [];
33
+ const designLetterSpacing = letterSpacing * fontScale;
34
+ let remainingLines = hasMaxLines === true ? maxLines : lines.length;
35
+ let i = 0;
36
+
37
+ while (remainingLines > 0) {
38
+ const line = lines[i];
39
+ if (line === undefined) {
40
+ continue;
41
+ }
42
+ const width = measureText(line, fontFamily, designLetterSpacing);
43
+ measuredLines.push([line, width]);
44
+ i++;
45
+ remainingLines--;
46
+ }
47
+
48
+ return [
49
+ measuredLines,
50
+ remainingLines,
51
+ hasMaxLines === true ? lines.length - measuredLines.length > 0 : false,
52
+ ];
53
+ };
54
+ /**
55
+ * Wrap text for SDF rendering with proper width constraints
56
+ */
57
+ export const wrapText = (
58
+ text: string,
59
+ fontFamily: string,
60
+ fontScale: number,
61
+ maxWidth: number,
62
+ letterSpacing: number,
63
+ overflowSuffix: string,
64
+ wordBreak: string,
65
+ maxLines: number,
66
+ hasMaxLines: boolean,
67
+ ): WrappedLinesStruct => {
68
+ const lines = text.split('\n');
69
+ const wrappedLines: TextLineStruct[] = [];
70
+ const maxWidthInDesignUnits = maxWidth / fontScale;
71
+ const designLetterSpacing = letterSpacing * fontScale;
72
+
73
+ // Calculate space width for line wrapping
74
+ const spaceWidth = measureText(' ', fontFamily, designLetterSpacing);
75
+
76
+ let wrappedLine: TextLineStruct[] = [];
77
+ let remainingLines = maxLines;
78
+ let hasRemainingText = true;
79
+
80
+ for (let i = 0; i < lines.length; i++) {
81
+ const line = lines[i]!;
82
+
83
+ [wrappedLine, remainingLines, hasRemainingText] = wrapLine(
84
+ line,
85
+ fontFamily,
86
+ maxWidthInDesignUnits,
87
+ designLetterSpacing,
88
+ spaceWidth,
89
+ overflowSuffix,
90
+ wordBreak,
91
+ remainingLines,
92
+ hasMaxLines,
93
+ );
94
+
95
+ wrappedLines.push(...wrappedLine);
96
+ }
97
+
98
+ return [wrappedLines, remainingLines, hasRemainingText];
99
+ };
100
+
101
+ /**
102
+ * Wrap a single line of text for SDF rendering
103
+ */
104
+ export const wrapLine = (
105
+ line: string,
106
+ fontFamily: string,
107
+ maxWidth: number,
108
+ designLetterSpacing: number,
109
+ spaceWidth: number,
110
+ overflowSuffix: string,
111
+ wordBreak: string,
112
+ remainingLines: number,
113
+ hasMaxLines: boolean,
114
+ ): WrappedLinesStruct => {
115
+ // Use the same space regex as Canvas renderer to handle ZWSP
116
+ const spaceRegex = / |\u200B/g;
117
+ const words = line.split(spaceRegex);
118
+ const spaces = line.match(spaceRegex) || [];
119
+ const wrappedLines: TextLineStruct[] = [];
120
+ let currentLine = '';
121
+ let currentLineWidth = 0;
122
+ let hasRemainingText = true;
123
+
124
+ let i = 0;
125
+
126
+ for (; i < words.length; i++) {
127
+ const word = words[i];
128
+ if (word === undefined) {
129
+ continue;
130
+ }
131
+ const space = spaces[i - 1] || '';
132
+ const wordWidth = measureText(word, fontFamily, designLetterSpacing);
133
+ // For width calculation, treat ZWSP as having 0 width but regular space functionality
134
+ const effectiveSpaceWidth = space === '\u200B' ? 0 : spaceWidth;
135
+ const totalWidth = currentLineWidth + effectiveSpaceWidth + wordWidth;
136
+
137
+ if (
138
+ (i === 0 && wordWidth <= maxWidth) ||
139
+ (i > 0 && totalWidth <= maxWidth)
140
+ ) {
141
+ // Word fits on current line
142
+ if (currentLine.length > 0) {
143
+ // Add space - for ZWSP, don't add anything to output (it's invisible)
144
+ if (space !== '\u200B') {
145
+ currentLine += space;
146
+ currentLineWidth += effectiveSpaceWidth;
147
+ }
148
+ }
149
+ currentLine += word;
150
+ currentLineWidth += wordWidth;
151
+ } else {
152
+ if (remainingLines === 1) {
153
+ if (currentLine.length > 0) {
154
+ // Add space - for ZWSP, don't add anything to output (it's invisible)
155
+ if (space !== '\u200B') {
156
+ currentLine += space;
157
+ currentLineWidth += effectiveSpaceWidth;
158
+ }
159
+ }
160
+ currentLine += word;
161
+ currentLineWidth += wordWidth;
162
+ remainingLines = 0;
163
+ hasRemainingText = i < words.length;
164
+ break;
165
+ }
166
+
167
+ if (wordBreak !== 'break-all' && currentLine.length > 0) {
168
+ wrappedLines.push([currentLine, currentLineWidth]);
169
+ currentLine = '';
170
+ currentLineWidth = 0;
171
+ remainingLines--;
172
+ }
173
+
174
+ if (wordBreak !== 'break-all') {
175
+ currentLine = word;
176
+ currentLineWidth = wordWidth;
177
+ }
178
+
179
+ if (wordBreak === 'break-word') {
180
+ const [lines, rl, rt] = breakWord(
181
+ word,
182
+ fontFamily,
183
+ maxWidth,
184
+ designLetterSpacing,
185
+ remainingLines,
186
+ hasMaxLines,
187
+ );
188
+ remainingLines = rl;
189
+ hasRemainingText = rt;
190
+ if (lines.length === 1) {
191
+ [currentLine, currentLineWidth] = lines[lines.length - 1]!;
192
+ } else {
193
+ for (let j = 0; j < lines.length; j++) {
194
+ [currentLine, currentLineWidth] = lines[j]!;
195
+ if (j < lines.length - 1) {
196
+ wrappedLines.push(lines[j]!);
197
+ }
198
+ }
199
+ }
200
+ } else if (wordBreak === 'break-all') {
201
+ const codepoint = word.codePointAt(0)!;
202
+ const glyph = SdfFontHandler.getGlyph(fontFamily, codepoint);
203
+ const firstLetterWidth =
204
+ glyph !== null ? glyph.xadvance + designLetterSpacing : 0;
205
+ let linebreak = false;
206
+ if (
207
+ currentLineWidth + firstLetterWidth + effectiveSpaceWidth >
208
+ maxWidth
209
+ ) {
210
+ wrappedLines.push([currentLine, currentLineWidth]);
211
+ remainingLines -= 1;
212
+ currentLine = '';
213
+ currentLineWidth = 0;
214
+ linebreak = true;
215
+ }
216
+ const initial = maxWidth - currentLineWidth;
217
+ const [lines, rl, rt] = breakAll(
218
+ word,
219
+ fontFamily,
220
+ initial,
221
+ maxWidth,
222
+ designLetterSpacing,
223
+ remainingLines,
224
+ hasMaxLines,
225
+ );
226
+ remainingLines = rl;
227
+ hasRemainingText = rt;
228
+ if (linebreak === false) {
229
+ const [text, width] = lines[0]!;
230
+ currentLine += ' ' + text;
231
+ currentLineWidth = width;
232
+ wrappedLines.push([currentLine, currentLineWidth]);
233
+ }
234
+
235
+ for (let j = 1; j < lines.length; j++) {
236
+ [currentLine, currentLineWidth] = lines[j]!;
237
+ if (j < lines.length - 1) {
238
+ wrappedLines.push([currentLine, currentLineWidth]);
239
+ }
240
+ }
241
+ }
242
+ }
243
+ }
244
+
245
+ // Add the last line if it has content
246
+ if (currentLine.length > 0 && remainingLines === 0) {
247
+ currentLine = truncateLineWithSuffix(
248
+ currentLine,
249
+ fontFamily,
250
+ maxWidth,
251
+ designLetterSpacing,
252
+ overflowSuffix,
253
+ );
254
+ }
255
+
256
+ if (currentLine.length > 0) {
257
+ wrappedLines.push([currentLine, currentLineWidth]);
258
+ } else {
259
+ wrappedLines.push(['', 0]);
260
+ }
261
+ return [wrappedLines, remainingLines, hasRemainingText];
262
+ };
263
+
264
+ /**
265
+ * Measure the width of text in SDF design units
266
+ */
267
+ export const measureText = (
268
+ text: string,
269
+ fontFamily: string,
270
+ designLetterSpacing: number,
271
+ ): number => {
272
+ let width = 0;
273
+ let prevCodepoint = 0;
274
+ for (let i = 0; i < text.length; i++) {
275
+ const char = text.charAt(i);
276
+ const codepoint = text.codePointAt(i);
277
+ if (codepoint === undefined) continue;
278
+
279
+ // Skip zero-width spaces in width calculations
280
+ if (isZeroWidthSpace(char)) {
281
+ continue;
282
+ }
283
+
284
+ const glyph = SdfFontHandler.getGlyph(fontFamily, codepoint);
285
+ if (glyph === null) continue;
286
+
287
+ let advance = glyph.xadvance;
288
+
289
+ // Add kerning if there's a previous character
290
+ if (prevCodepoint !== 0) {
291
+ const kerning = SdfFontHandler.getKerning(
292
+ fontFamily,
293
+ prevCodepoint,
294
+ codepoint,
295
+ );
296
+ advance += kerning;
297
+ }
298
+
299
+ width += advance + designLetterSpacing;
300
+ prevCodepoint = codepoint;
301
+ }
302
+
303
+ return width;
304
+ };
305
+
306
+ /**
307
+ * Truncate a line with overflow suffix to fit within width
308
+ */
309
+ export const truncateLineWithSuffix = (
310
+ line: string,
311
+ fontFamily: string,
312
+ maxWidth: number,
313
+ designLetterSpacing: number,
314
+ overflowSuffix: string,
315
+ ): string => {
316
+ const suffixWidth = measureText(
317
+ overflowSuffix,
318
+ fontFamily,
319
+ designLetterSpacing,
320
+ );
321
+
322
+ if (suffixWidth >= maxWidth) {
323
+ return overflowSuffix.substring(0, Math.max(1, overflowSuffix.length - 1));
324
+ }
325
+
326
+ let truncatedLine = line;
327
+ while (truncatedLine.length > 0) {
328
+ const lineWidth = measureText(
329
+ truncatedLine,
330
+ fontFamily,
331
+ designLetterSpacing,
332
+ );
333
+ if (lineWidth + suffixWidth <= maxWidth) {
334
+ return truncatedLine + overflowSuffix;
335
+ }
336
+ truncatedLine = truncatedLine.substring(0, truncatedLine.length - 1);
337
+ }
338
+
339
+ return overflowSuffix;
340
+ };
341
+
342
+ /**
343
+ * wordbreak function: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#break-word
344
+ */
345
+ export const breakWord = (
346
+ word: string,
347
+ fontFamily: string,
348
+ maxWidth: number,
349
+ designLetterSpacing: number,
350
+ remainingLines: number,
351
+ hasMaxLines: boolean,
352
+ ): WrappedLinesStruct => {
353
+ const lines: TextLineStruct[] = [];
354
+ let currentPart = '';
355
+ let currentWidth = 0;
356
+ let i = 0;
357
+
358
+ for (let i = 0; i < word.length; i++) {
359
+ const char = word.charAt(i);
360
+ const codepoint = char.codePointAt(0);
361
+ if (codepoint === undefined) continue;
362
+
363
+ const glyph = SdfFontHandler.getGlyph(fontFamily, codepoint);
364
+ if (glyph === null) continue;
365
+
366
+ const charWidth = glyph.xadvance + designLetterSpacing;
367
+
368
+ if (currentWidth + charWidth > maxWidth && currentPart.length > 0) {
369
+ remainingLines--;
370
+ if (remainingLines === 0) {
371
+ break;
372
+ }
373
+ lines.push([currentPart, currentWidth]);
374
+ currentPart = char;
375
+ currentWidth = charWidth;
376
+ } else {
377
+ currentPart += char;
378
+ currentWidth += charWidth;
379
+ }
380
+ }
381
+
382
+ if (currentPart.length > 0) {
383
+ lines.push([currentPart, currentWidth]);
384
+ }
385
+
386
+ return [lines, remainingLines, i < word.length - 1];
387
+ };
388
+
389
+ /**
390
+ * wordbreak function: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#break-word
391
+ */
392
+ export const breakAll = (
393
+ word: string,
394
+ fontFamily: string,
395
+ initial: number,
396
+ maxWidth: number,
397
+ designLetterSpacing: number,
398
+ remainingLines: number,
399
+ hasMaxLines: boolean,
400
+ ): WrappedLinesStruct => {
401
+ const lines: TextLineStruct[] = [];
402
+ let currentPart = '';
403
+ let currentWidth = 0;
404
+ let max = initial;
405
+ let i = 0;
406
+ let hasRemainingText = false;
407
+
408
+ for (; i < word.length; i++) {
409
+ if (remainingLines === 0) {
410
+ hasRemainingText = true;
411
+ break;
412
+ }
413
+ const char = word.charAt(i);
414
+ const codepoint = char.codePointAt(0)!;
415
+ const glyph = SdfFontHandler.getGlyph(fontFamily, codepoint);
416
+ if (glyph === null) continue;
417
+
418
+ const charWidth = glyph.xadvance + designLetterSpacing;
419
+ if (currentWidth + charWidth > max && currentPart.length > 0) {
420
+ lines.push([currentPart, currentWidth]);
421
+ currentPart = char;
422
+ currentWidth = charWidth;
423
+ max = maxWidth;
424
+ remainingLines--;
425
+ } else {
426
+ currentPart += char;
427
+ currentWidth += charWidth;
428
+ }
429
+ }
430
+
431
+ if (currentPart.length > 0) {
432
+ lines.push([currentPart, currentWidth]);
433
+ }
434
+
435
+ return [lines, remainingLines, hasRemainingText];
436
+ };