@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,509 +1,509 @@
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 { EventEmitter } from '../../../common/EventEmitter.js';
21
- import { assertTruthy } from '../../../utils.js';
22
- import type { CoreNode } from '../../CoreNode.js';
23
- import type { CoreTextNode } from '../../CoreTextNode.js';
24
- import type { Stage } from '../../Stage.js';
25
- import {
26
- getNormalizedRgbaComponents,
27
- getNormalizedAlphaComponent,
28
- } from '../../lib/utils.js';
29
- import { type FontFamilyMap } from '../TrFontManager.js';
30
- import type { TrFontFace } from '../font-face-types/TrFontFace.js';
31
- import { WebTrFontFace } from '../font-face-types/WebTrFontFace.js';
32
- import {
33
- LightningTextTextureRenderer,
34
- type RenderInfo,
35
- } from './LightningTextTextureRenderer.js';
36
- import {
37
- TextRenderer,
38
- type TextRendererState,
39
- type TrFontProps,
40
- type TrPropSetters,
41
- type TrProps,
42
- } from './TextRenderer.js';
43
-
44
- const resolvedGlobal = typeof self === 'undefined' ? globalThis : self;
45
-
46
- /**
47
- * Global font set regardless of if run in the main thread or a web worker
48
- */
49
- const globalFontSet: FontFaceSet = (resolvedGlobal.document?.fonts ||
50
- (resolvedGlobal as any).fonts) as FontFaceSet;
51
-
52
- declare module './TextRenderer.js' {
53
- interface TextRendererMap {
54
- canvas: CanvasTextRenderer;
55
- }
56
- }
57
-
58
- function getFontCssString(props: TrProps): string {
59
- const { fontFamily, fontStyle, fontWeight, fontStretch, fontSize } = props;
60
- return [fontStyle, fontWeight, fontStretch, `${fontSize}px`, fontFamily].join(
61
- ' ',
62
- );
63
- }
64
-
65
- export interface CanvasTextRendererState extends TextRendererState {
66
- node: CoreTextNode;
67
- props: TrProps;
68
- fontInfo:
69
- | {
70
- fontFace: WebTrFontFace;
71
- cssString: string;
72
- loaded: boolean;
73
- }
74
- | undefined;
75
- textureNode: CoreNode | undefined;
76
- lightning2TextRenderer: LightningTextTextureRenderer;
77
- renderInfo: RenderInfo | undefined;
78
- }
79
-
80
- export class CanvasTextRenderer extends TextRenderer<CanvasTextRendererState> {
81
- protected canvas: OffscreenCanvas | HTMLCanvasElement;
82
- protected context:
83
- | OffscreenCanvasRenderingContext2D
84
- | CanvasRenderingContext2D;
85
- /**
86
- * Font family map used to store web font faces that were added to the
87
- * canvas text renderer.
88
- */
89
- private fontFamilies: FontFamilyMap = {};
90
- private fontFamilyArray: FontFamilyMap[] = [this.fontFamilies];
91
-
92
- public type: 'canvas' | 'sdf' = 'canvas';
93
-
94
- constructor(stage: Stage) {
95
- super(stage);
96
- if (typeof OffscreenCanvas !== 'undefined') {
97
- this.canvas = new OffscreenCanvas(0, 0);
98
- } else {
99
- this.canvas = document.createElement('canvas');
100
- }
101
-
102
- let context = this.canvas.getContext('2d', {
103
- willReadFrequently: true,
104
- }) as OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D | null;
105
- if (!context) {
106
- // A browser may appear to support OffscreenCanvas but not actually support the Canvas '2d' context
107
- // Here we try getting the context again after falling back to an HTMLCanvasElement.
108
- // See: https://github.com/lightning-js/renderer/issues/26#issuecomment-1750438486
109
- this.canvas = document.createElement('canvas');
110
- context = this.canvas.getContext('2d', {
111
- willReadFrequently: true,
112
- });
113
- }
114
- assertTruthy(context);
115
- this.context = context;
116
-
117
- // Install the default 'san-serif' font face
118
- this.addFontFace(
119
- new WebTrFontFace({
120
- fontFamily: 'sans-serif',
121
- descriptors: {},
122
- fontUrl: '',
123
- }),
124
- );
125
- }
126
-
127
- //#region Overrides
128
- override getPropertySetters(): Partial<
129
- TrPropSetters<CanvasTextRendererState>
130
- > {
131
- return {
132
- fontFamily: (state, value) => {
133
- state.props.fontFamily = value;
134
- state.fontInfo = undefined;
135
- this.invalidateLayoutCache(state);
136
- },
137
- fontWeight: (state, value) => {
138
- state.props.fontWeight = value;
139
- state.fontInfo = undefined;
140
- this.invalidateLayoutCache(state);
141
- },
142
- fontStyle: (state, value) => {
143
- state.props.fontStyle = value;
144
- state.fontInfo = undefined;
145
- this.invalidateLayoutCache(state);
146
- },
147
- fontStretch: (state, value) => {
148
- state.props.fontStretch = value;
149
- state.fontInfo = undefined;
150
- this.invalidateLayoutCache(state);
151
- },
152
- fontSize: (state, value) => {
153
- state.props.fontSize = value;
154
- state.fontInfo = undefined;
155
- this.invalidateLayoutCache(state);
156
- },
157
- text: (state, value) => {
158
- state.props.text = value;
159
- this.invalidateLayoutCache(state);
160
- },
161
- textAlign: (state, value) => {
162
- state.props.textAlign = value;
163
- this.invalidateLayoutCache(state);
164
- },
165
- color: (state, value) => {
166
- state.props.color = value;
167
- this.invalidateLayoutCache(state);
168
- },
169
- x: (state, value) => {
170
- state.props.x = value;
171
- },
172
- y: (state, value) => {
173
- state.props.y = value;
174
- },
175
- contain: (state, value) => {
176
- state.props.contain = value;
177
- this.invalidateLayoutCache(state);
178
- },
179
- width: (state, value) => {
180
- state.props.width = value;
181
- // Only invalidate layout cache if we're containing in the horizontal direction
182
- if (state.props.contain !== 'none') {
183
- this.invalidateLayoutCache(state);
184
- }
185
- },
186
- height: (state, value) => {
187
- state.props.height = value;
188
- // Only invalidate layout cache if we're containing in the vertical direction
189
- if (state.props.contain === 'both') {
190
- this.invalidateLayoutCache(state);
191
- }
192
- },
193
- offsetY: (state, value) => {
194
- state.props.offsetY = value;
195
- this.invalidateLayoutCache(state);
196
- },
197
- scrollY: (state, value) => {
198
- state.props.scrollY = value;
199
- },
200
- letterSpacing: (state, value) => {
201
- state.props.letterSpacing = value;
202
- this.invalidateLayoutCache(state);
203
- },
204
- lineHeight: (state, value) => {
205
- state.props.lineHeight = value;
206
- this.invalidateLayoutCache(state);
207
- },
208
- maxLines: (state, value) => {
209
- state.props.maxLines = value;
210
- this.invalidateLayoutCache(state);
211
- },
212
- textBaseline: (state, value) => {
213
- state.props.textBaseline = value;
214
- this.invalidateLayoutCache(state);
215
- },
216
- verticalAlign: (state, value) => {
217
- state.props.verticalAlign = value;
218
- this.invalidateLayoutCache(state);
219
- },
220
- overflowSuffix: (state, value) => {
221
- state.props.overflowSuffix = value;
222
- this.invalidateLayoutCache(state);
223
- },
224
- };
225
- }
226
-
227
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
228
- override canRenderFont(props: TrFontProps): boolean {
229
- // The canvas renderer can render any font because it automatically
230
- // falls back to system fonts. The CanvasTextRenderer should be
231
- // checked last if other renderers are preferred.
232
- return true;
233
- }
234
-
235
- override isFontFaceSupported(fontFace: TrFontFace): boolean {
236
- return fontFace instanceof WebTrFontFace;
237
- }
238
-
239
- override addFontFace(fontFace: TrFontFace): void {
240
- // Make sure the font face is an Canvas font face (it should have already passed
241
- // the `isFontFaceSupported` check)
242
- assertTruthy(fontFace instanceof WebTrFontFace);
243
-
244
- const fontFamily = fontFace.fontFamily;
245
-
246
- // Add the font face to the document
247
- // Except for the 'sans-serif' font family, which the Renderer provides
248
- // as a special default fallback.
249
- if (fontFamily !== 'sans-serif') {
250
- // @ts-expect-error `add()` method should be available from a FontFaceSet
251
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
252
- globalFontSet.add(fontFace.fontFace);
253
- }
254
-
255
- let faceSet = this.fontFamilies[fontFamily];
256
- if (!faceSet) {
257
- faceSet = new Set();
258
- this.fontFamilies[fontFamily] = faceSet;
259
- }
260
- faceSet.add(fontFace);
261
- }
262
-
263
- override createState(
264
- props: TrProps,
265
- node: CoreTextNode,
266
- ): CanvasTextRendererState {
267
- return {
268
- node,
269
- props,
270
- status: 'initialState',
271
- updateScheduled: false,
272
- emitter: new EventEmitter(),
273
- textureNode: undefined,
274
- lightning2TextRenderer: new LightningTextTextureRenderer(
275
- this.canvas,
276
- this.context,
277
- ),
278
- renderInfo: undefined,
279
- forceFullLayoutCalc: false,
280
- textW: 0,
281
- textH: 0,
282
- fontInfo: undefined,
283
- isRenderable: false,
284
- debugData: {
285
- updateCount: 0,
286
- layoutCount: 0,
287
- drawCount: 0,
288
- lastLayoutNumCharacters: 0,
289
- layoutSum: 0,
290
- drawSum: 0,
291
- bufferSize: 0,
292
- },
293
- };
294
- }
295
-
296
- override updateState(state: CanvasTextRendererState): void {
297
- // On the first update call we need to set the status to loading
298
- if (state.status === 'initialState') {
299
- this.setStatus(state, 'loading');
300
- // check if we're on screen
301
- // if (this.isValidOnScreen(state) === true) {
302
- // this.setStatus(state, 'loading');
303
- // }
304
- }
305
-
306
- if (state.status === 'loaded') {
307
- // If we're loaded, we don't need to do anything
308
- return;
309
- }
310
-
311
- // If fontInfo is invalid, we need to establish it
312
- if (!state.fontInfo) {
313
- return this.loadFont(state);
314
- }
315
-
316
- // If we're waiting for a font face to load, don't render anything
317
- if (!state.fontInfo.loaded) {
318
- return;
319
- }
320
-
321
- if (!state.renderInfo) {
322
- state.renderInfo = this.calculateRenderInfo(state);
323
- state.textH = state.renderInfo.lineHeight * state.renderInfo.lines.length;
324
- state.textW = state.renderInfo.width;
325
- this.renderSingleCanvasPage(state);
326
- }
327
-
328
- // handle scrollable text !!!
329
- // if (state.isScrollable === true) {
330
- // return this.renderScrollableCanvasPages(state);
331
- // }
332
-
333
- // handle single page text
334
- }
335
-
336
- renderSingleCanvasPage(state: CanvasTextRendererState): void {
337
- assertTruthy(state.renderInfo);
338
- const node = state.node;
339
-
340
- const texture = this.stage.txManager.createTexture('ImageTexture', {
341
- premultiplyAlpha: true,
342
- src: function (
343
- this: CanvasTextRenderer,
344
- lightning2TextRenderer: LightningTextTextureRenderer,
345
- renderInfo: RenderInfo,
346
- ) {
347
- // load the canvas texture
348
- assertTruthy(renderInfo);
349
- lightning2TextRenderer.draw(renderInfo, {
350
- lines: renderInfo.lines,
351
- lineWidths: renderInfo.lineWidths,
352
- });
353
- if (this.canvas.width === 0 || this.canvas.height === 0) {
354
- return null;
355
- }
356
- return this.context.getImageData(
357
- 0,
358
- 0,
359
- this.canvas.width,
360
- this.canvas.height,
361
- );
362
- }.bind(this, state.lightning2TextRenderer, state.renderInfo),
363
- });
364
-
365
- if (state.textureNode) {
366
- // Use the existing texture node
367
- state.textureNode.texture = texture;
368
- // Update the alpha
369
- state.textureNode.alpha = getNormalizedAlphaComponent(state.props.color);
370
- } else {
371
- // Create a new texture node
372
- const textureNode = this.stage.createNode({
373
- parent: node,
374
- texture,
375
- autosize: true,
376
- // The alpha channel of the color is ignored when rasterizing the text
377
- // texture so we need to pass it directly to the texture node.
378
- alpha: getNormalizedAlphaComponent(state.props.color),
379
- });
380
- state.textureNode = textureNode;
381
- }
382
-
383
- this.setStatus(state, 'loaded');
384
- }
385
-
386
- loadFont = (state: CanvasTextRendererState): void => {
387
- const cssString = getFontCssString(state.props);
388
-
389
- const trFontFace = this.stage.fontManager.resolveFontFace(
390
- this.fontFamilyArray,
391
- state.props,
392
- 'canvas',
393
- ) as WebTrFontFace | undefined;
394
- assertTruthy(trFontFace, `Could not resolve font face for ${cssString}`);
395
- state.fontInfo = {
396
- fontFace: trFontFace,
397
- cssString: cssString,
398
- // TODO: For efficiency we would use this here but it's not reliable on WPE -> document.fonts.check(cssString),
399
- loaded: false,
400
- };
401
- // If font is not loaded, set up a handler to update the font info when the font loads
402
- if (!state.fontInfo.loaded) {
403
- globalFontSet
404
- .load(cssString)
405
- .then(this.onFontLoaded.bind(this, state, cssString))
406
- .catch(this.onFontLoadError.bind(this, state, cssString));
407
- return;
408
- }
409
- };
410
-
411
- calculateRenderInfo(state: CanvasTextRendererState): RenderInfo {
412
- state.lightning2TextRenderer.settings = {
413
- text: state.props.text,
414
- textAlign: state.props.textAlign,
415
- fontFamily: state.props.fontFamily,
416
- trFontFace: state.fontInfo?.fontFace,
417
- fontSize: state.props.fontSize,
418
- fontStyle: [
419
- state.props.fontStretch,
420
- state.props.fontStyle,
421
- state.props.fontWeight,
422
- ].join(' '),
423
- textColor: getNormalizedRgbaComponents(state.props.color),
424
- offsetY: state.props.offsetY,
425
- wordWrap: state.props.contain !== 'none',
426
- wordWrapWidth:
427
- state.props.contain === 'none' ? undefined : state.props.width,
428
- letterSpacing: state.props.letterSpacing,
429
- lineHeight: state.props.lineHeight ?? null,
430
- maxLines: state.props.maxLines,
431
- maxHeight:
432
- state.props.contain === 'both'
433
- ? state.props.height - state.props.offsetY
434
- : null,
435
- textBaseline: state.props.textBaseline,
436
- verticalAlign: state.props.verticalAlign,
437
- overflowSuffix: state.props.overflowSuffix,
438
- w: state.props.contain !== 'none' ? state.props.width : undefined,
439
- };
440
- state.renderInfo = state.lightning2TextRenderer.calculateRenderInfo();
441
- return state.renderInfo;
442
- }
443
-
444
- override renderQuads(): void {
445
- // Do nothing. The renderer will render the child node(s) that were created
446
- // in the state update.
447
- return;
448
- }
449
-
450
- override destroyState(state: CanvasTextRendererState): void {
451
- if (state.status === 'destroyed') {
452
- return;
453
- }
454
- super.destroyState(state);
455
-
456
- if (state.textureNode) {
457
- state.textureNode.destroy();
458
- delete state.textureNode;
459
- }
460
- delete state.renderInfo;
461
- }
462
- //#endregion Overrides
463
-
464
- /**
465
- * Invalidate the layout cache stored in the state. This will cause the text
466
- * to be re-rendered on the next update.
467
- *
468
- * @remarks
469
- * This also invalidates the visible window cache.
470
- *
471
- * @param state
472
- */
473
- private invalidateLayoutCache(state: CanvasTextRendererState): void {
474
- state.renderInfo = undefined;
475
- this.setStatus(state, 'loading');
476
- this.scheduleUpdateState(state);
477
- }
478
-
479
- private onFontLoaded(
480
- state: CanvasTextRendererState,
481
- cssString: string,
482
- ): void {
483
- if (cssString !== state.fontInfo?.cssString || !state.fontInfo) {
484
- return;
485
- }
486
- state.fontInfo.loaded = true;
487
- this.scheduleUpdateState(state);
488
- }
489
-
490
- private onFontLoadError(
491
- state: CanvasTextRendererState,
492
- cssString: string,
493
- error: Error,
494
- ): void {
495
- if (cssString !== state.fontInfo?.cssString || !state.fontInfo) {
496
- return;
497
- }
498
-
499
- // Font didn't actually load, but we'll log the error and mark it as loaded
500
- // because the browser can still render with a fallback font.
501
- state.fontInfo.loaded = true;
502
-
503
- console.error(
504
- `CanvasTextRenderer: Error loading font '${state.fontInfo.cssString}'`,
505
- error,
506
- );
507
- this.scheduleUpdateState(state);
508
- }
509
- }
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 { EventEmitter } from '../../../common/EventEmitter.js';
21
+ import { assertTruthy } from '../../../utils.js';
22
+ import type { CoreNode } from '../../CoreNode.js';
23
+ import type { CoreTextNode } from '../../CoreTextNode.js';
24
+ import type { Stage } from '../../Stage.js';
25
+ import {
26
+ getNormalizedRgbaComponents,
27
+ getNormalizedAlphaComponent,
28
+ } from '../../lib/utils.js';
29
+ import { type FontFamilyMap } from '../TrFontManager.js';
30
+ import type { TrFontFace } from '../font-face-types/TrFontFace.js';
31
+ import { WebTrFontFace } from '../font-face-types/WebTrFontFace.js';
32
+ import {
33
+ LightningTextTextureRenderer,
34
+ type RenderInfo,
35
+ } from './LightningTextTextureRenderer.js';
36
+ import {
37
+ TextRenderer,
38
+ type TextRendererState,
39
+ type TrFontProps,
40
+ type TrPropSetters,
41
+ type TrProps,
42
+ } from './TextRenderer.js';
43
+
44
+ const resolvedGlobal = typeof self === 'undefined' ? globalThis : self;
45
+
46
+ /**
47
+ * Global font set regardless of if run in the main thread or a web worker
48
+ */
49
+ const globalFontSet: FontFaceSet = (resolvedGlobal.document?.fonts ||
50
+ (resolvedGlobal as any).fonts) as FontFaceSet;
51
+
52
+ declare module './TextRenderer.js' {
53
+ interface TextRendererMap {
54
+ canvas: CanvasTextRenderer;
55
+ }
56
+ }
57
+
58
+ function getFontCssString(props: TrProps): string {
59
+ const { fontFamily, fontStyle, fontWeight, fontStretch, fontSize } = props;
60
+ return [fontStyle, fontWeight, fontStretch, `${fontSize}px`, fontFamily].join(
61
+ ' ',
62
+ );
63
+ }
64
+
65
+ export interface CanvasTextRendererState extends TextRendererState {
66
+ node: CoreTextNode;
67
+ props: TrProps;
68
+ fontInfo:
69
+ | {
70
+ fontFace: WebTrFontFace;
71
+ cssString: string;
72
+ loaded: boolean;
73
+ }
74
+ | undefined;
75
+ textureNode: CoreNode | undefined;
76
+ lightning2TextRenderer: LightningTextTextureRenderer;
77
+ renderInfo: RenderInfo | undefined;
78
+ }
79
+
80
+ export class CanvasTextRenderer extends TextRenderer<CanvasTextRendererState> {
81
+ protected canvas: OffscreenCanvas | HTMLCanvasElement;
82
+ protected context:
83
+ | OffscreenCanvasRenderingContext2D
84
+ | CanvasRenderingContext2D;
85
+ /**
86
+ * Font family map used to store web font faces that were added to the
87
+ * canvas text renderer.
88
+ */
89
+ private fontFamilies: FontFamilyMap = {};
90
+ private fontFamilyArray: FontFamilyMap[] = [this.fontFamilies];
91
+
92
+ public type: 'canvas' | 'sdf' = 'canvas';
93
+
94
+ constructor(stage: Stage) {
95
+ super(stage);
96
+ if (typeof OffscreenCanvas !== 'undefined') {
97
+ this.canvas = new OffscreenCanvas(0, 0);
98
+ } else {
99
+ this.canvas = document.createElement('canvas');
100
+ }
101
+
102
+ let context = this.canvas.getContext('2d', {
103
+ willReadFrequently: true,
104
+ }) as OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D | null;
105
+ if (!context) {
106
+ // A browser may appear to support OffscreenCanvas but not actually support the Canvas '2d' context
107
+ // Here we try getting the context again after falling back to an HTMLCanvasElement.
108
+ // See: https://github.com/lightning-js/renderer/issues/26#issuecomment-1750438486
109
+ this.canvas = document.createElement('canvas');
110
+ context = this.canvas.getContext('2d', {
111
+ willReadFrequently: true,
112
+ });
113
+ }
114
+ assertTruthy(context);
115
+ this.context = context;
116
+
117
+ // Install the default 'san-serif' font face
118
+ this.addFontFace(
119
+ new WebTrFontFace({
120
+ fontFamily: 'sans-serif',
121
+ descriptors: {},
122
+ fontUrl: '',
123
+ }),
124
+ );
125
+ }
126
+
127
+ //#region Overrides
128
+ override getPropertySetters(): Partial<
129
+ TrPropSetters<CanvasTextRendererState>
130
+ > {
131
+ return {
132
+ fontFamily: (state, value) => {
133
+ state.props.fontFamily = value;
134
+ state.fontInfo = undefined;
135
+ this.invalidateLayoutCache(state);
136
+ },
137
+ fontWeight: (state, value) => {
138
+ state.props.fontWeight = value;
139
+ state.fontInfo = undefined;
140
+ this.invalidateLayoutCache(state);
141
+ },
142
+ fontStyle: (state, value) => {
143
+ state.props.fontStyle = value;
144
+ state.fontInfo = undefined;
145
+ this.invalidateLayoutCache(state);
146
+ },
147
+ fontStretch: (state, value) => {
148
+ state.props.fontStretch = value;
149
+ state.fontInfo = undefined;
150
+ this.invalidateLayoutCache(state);
151
+ },
152
+ fontSize: (state, value) => {
153
+ state.props.fontSize = value;
154
+ state.fontInfo = undefined;
155
+ this.invalidateLayoutCache(state);
156
+ },
157
+ text: (state, value) => {
158
+ state.props.text = value;
159
+ this.invalidateLayoutCache(state);
160
+ },
161
+ textAlign: (state, value) => {
162
+ state.props.textAlign = value;
163
+ this.invalidateLayoutCache(state);
164
+ },
165
+ color: (state, value) => {
166
+ state.props.color = value;
167
+ this.invalidateLayoutCache(state);
168
+ },
169
+ x: (state, value) => {
170
+ state.props.x = value;
171
+ },
172
+ y: (state, value) => {
173
+ state.props.y = value;
174
+ },
175
+ contain: (state, value) => {
176
+ state.props.contain = value;
177
+ this.invalidateLayoutCache(state);
178
+ },
179
+ width: (state, value) => {
180
+ state.props.width = value;
181
+ // Only invalidate layout cache if we're containing in the horizontal direction
182
+ if (state.props.contain !== 'none') {
183
+ this.invalidateLayoutCache(state);
184
+ }
185
+ },
186
+ height: (state, value) => {
187
+ state.props.height = value;
188
+ // Only invalidate layout cache if we're containing in the vertical direction
189
+ if (state.props.contain === 'both') {
190
+ this.invalidateLayoutCache(state);
191
+ }
192
+ },
193
+ offsetY: (state, value) => {
194
+ state.props.offsetY = value;
195
+ this.invalidateLayoutCache(state);
196
+ },
197
+ scrollY: (state, value) => {
198
+ state.props.scrollY = value;
199
+ },
200
+ letterSpacing: (state, value) => {
201
+ state.props.letterSpacing = value;
202
+ this.invalidateLayoutCache(state);
203
+ },
204
+ lineHeight: (state, value) => {
205
+ state.props.lineHeight = value;
206
+ this.invalidateLayoutCache(state);
207
+ },
208
+ maxLines: (state, value) => {
209
+ state.props.maxLines = value;
210
+ this.invalidateLayoutCache(state);
211
+ },
212
+ textBaseline: (state, value) => {
213
+ state.props.textBaseline = value;
214
+ this.invalidateLayoutCache(state);
215
+ },
216
+ verticalAlign: (state, value) => {
217
+ state.props.verticalAlign = value;
218
+ this.invalidateLayoutCache(state);
219
+ },
220
+ overflowSuffix: (state, value) => {
221
+ state.props.overflowSuffix = value;
222
+ this.invalidateLayoutCache(state);
223
+ },
224
+ };
225
+ }
226
+
227
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
228
+ override canRenderFont(props: TrFontProps): boolean {
229
+ // The canvas renderer can render any font because it automatically
230
+ // falls back to system fonts. The CanvasTextRenderer should be
231
+ // checked last if other renderers are preferred.
232
+ return true;
233
+ }
234
+
235
+ override isFontFaceSupported(fontFace: TrFontFace): boolean {
236
+ return fontFace instanceof WebTrFontFace;
237
+ }
238
+
239
+ override addFontFace(fontFace: TrFontFace): void {
240
+ // Make sure the font face is an Canvas font face (it should have already passed
241
+ // the `isFontFaceSupported` check)
242
+ assertTruthy(fontFace instanceof WebTrFontFace);
243
+
244
+ const fontFamily = fontFace.fontFamily;
245
+
246
+ // Add the font face to the document
247
+ // Except for the 'sans-serif' font family, which the Renderer provides
248
+ // as a special default fallback.
249
+ if (fontFamily !== 'sans-serif') {
250
+ // @ts-expect-error `add()` method should be available from a FontFaceSet
251
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
252
+ globalFontSet.add(fontFace.fontFace);
253
+ }
254
+
255
+ let faceSet = this.fontFamilies[fontFamily];
256
+ if (!faceSet) {
257
+ faceSet = new Set();
258
+ this.fontFamilies[fontFamily] = faceSet;
259
+ }
260
+ faceSet.add(fontFace);
261
+ }
262
+
263
+ override createState(
264
+ props: TrProps,
265
+ node: CoreTextNode,
266
+ ): CanvasTextRendererState {
267
+ return {
268
+ node,
269
+ props,
270
+ status: 'initialState',
271
+ updateScheduled: false,
272
+ emitter: new EventEmitter(),
273
+ textureNode: undefined,
274
+ lightning2TextRenderer: new LightningTextTextureRenderer(
275
+ this.canvas,
276
+ this.context,
277
+ ),
278
+ renderInfo: undefined,
279
+ forceFullLayoutCalc: false,
280
+ textW: 0,
281
+ textH: 0,
282
+ fontInfo: undefined,
283
+ isRenderable: false,
284
+ debugData: {
285
+ updateCount: 0,
286
+ layoutCount: 0,
287
+ drawCount: 0,
288
+ lastLayoutNumCharacters: 0,
289
+ layoutSum: 0,
290
+ drawSum: 0,
291
+ bufferSize: 0,
292
+ },
293
+ };
294
+ }
295
+
296
+ override updateState(state: CanvasTextRendererState): void {
297
+ // On the first update call we need to set the status to loading
298
+ if (state.status === 'initialState') {
299
+ this.setStatus(state, 'loading');
300
+ // check if we're on screen
301
+ // if (this.isValidOnScreen(state) === true) {
302
+ // this.setStatus(state, 'loading');
303
+ // }
304
+ }
305
+
306
+ if (state.status === 'loaded') {
307
+ // If we're loaded, we don't need to do anything
308
+ return;
309
+ }
310
+
311
+ // If fontInfo is invalid, we need to establish it
312
+ if (!state.fontInfo) {
313
+ return this.loadFont(state);
314
+ }
315
+
316
+ // If we're waiting for a font face to load, don't render anything
317
+ if (!state.fontInfo.loaded) {
318
+ return;
319
+ }
320
+
321
+ if (!state.renderInfo) {
322
+ state.renderInfo = this.calculateRenderInfo(state);
323
+ state.textH = state.renderInfo.lineHeight * state.renderInfo.lines.length;
324
+ state.textW = state.renderInfo.width;
325
+ this.renderSingleCanvasPage(state);
326
+ }
327
+
328
+ // handle scrollable text !!!
329
+ // if (state.isScrollable === true) {
330
+ // return this.renderScrollableCanvasPages(state);
331
+ // }
332
+
333
+ // handle single page text
334
+ }
335
+
336
+ renderSingleCanvasPage(state: CanvasTextRendererState): void {
337
+ assertTruthy(state.renderInfo);
338
+ const node = state.node;
339
+
340
+ const texture = this.stage.txManager.createTexture('ImageTexture', {
341
+ premultiplyAlpha: true,
342
+ src: function (
343
+ this: CanvasTextRenderer,
344
+ lightning2TextRenderer: LightningTextTextureRenderer,
345
+ renderInfo: RenderInfo,
346
+ ) {
347
+ // load the canvas texture
348
+ assertTruthy(renderInfo);
349
+ lightning2TextRenderer.draw(renderInfo, {
350
+ lines: renderInfo.lines,
351
+ lineWidths: renderInfo.lineWidths,
352
+ });
353
+ if (this.canvas.width === 0 || this.canvas.height === 0) {
354
+ return null;
355
+ }
356
+ return this.context.getImageData(
357
+ 0,
358
+ 0,
359
+ this.canvas.width,
360
+ this.canvas.height,
361
+ );
362
+ }.bind(this, state.lightning2TextRenderer, state.renderInfo),
363
+ });
364
+
365
+ if (state.textureNode) {
366
+ // Use the existing texture node
367
+ state.textureNode.texture = texture;
368
+ // Update the alpha
369
+ state.textureNode.alpha = getNormalizedAlphaComponent(state.props.color);
370
+ } else {
371
+ // Create a new texture node
372
+ const textureNode = this.stage.createNode({
373
+ parent: node,
374
+ texture,
375
+ autosize: true,
376
+ // The alpha channel of the color is ignored when rasterizing the text
377
+ // texture so we need to pass it directly to the texture node.
378
+ alpha: getNormalizedAlphaComponent(state.props.color),
379
+ });
380
+ state.textureNode = textureNode;
381
+ }
382
+
383
+ this.setStatus(state, 'loaded');
384
+ }
385
+
386
+ loadFont = (state: CanvasTextRendererState): void => {
387
+ const cssString = getFontCssString(state.props);
388
+
389
+ const trFontFace = this.stage.fontManager.resolveFontFace(
390
+ this.fontFamilyArray,
391
+ state.props,
392
+ 'canvas',
393
+ ) as WebTrFontFace | undefined;
394
+ assertTruthy(trFontFace, `Could not resolve font face for ${cssString}`);
395
+ state.fontInfo = {
396
+ fontFace: trFontFace,
397
+ cssString: cssString,
398
+ // TODO: For efficiency we would use this here but it's not reliable on WPE -> document.fonts.check(cssString),
399
+ loaded: false,
400
+ };
401
+ // If font is not loaded, set up a handler to update the font info when the font loads
402
+ if (!state.fontInfo.loaded) {
403
+ globalFontSet
404
+ .load(cssString)
405
+ .then(this.onFontLoaded.bind(this, state, cssString))
406
+ .catch(this.onFontLoadError.bind(this, state, cssString));
407
+ return;
408
+ }
409
+ };
410
+
411
+ calculateRenderInfo(state: CanvasTextRendererState): RenderInfo {
412
+ state.lightning2TextRenderer.settings = {
413
+ text: state.props.text,
414
+ textAlign: state.props.textAlign,
415
+ fontFamily: state.props.fontFamily,
416
+ trFontFace: state.fontInfo?.fontFace,
417
+ fontSize: state.props.fontSize,
418
+ fontStyle: [
419
+ state.props.fontStretch,
420
+ state.props.fontStyle,
421
+ state.props.fontWeight,
422
+ ].join(' '),
423
+ textColor: getNormalizedRgbaComponents(state.props.color),
424
+ offsetY: state.props.offsetY,
425
+ wordWrap: state.props.contain !== 'none',
426
+ wordWrapWidth:
427
+ state.props.contain === 'none' ? undefined : state.props.width,
428
+ letterSpacing: state.props.letterSpacing,
429
+ lineHeight: state.props.lineHeight ?? null,
430
+ maxLines: state.props.maxLines,
431
+ maxHeight:
432
+ state.props.contain === 'both'
433
+ ? state.props.height - state.props.offsetY
434
+ : null,
435
+ textBaseline: state.props.textBaseline,
436
+ verticalAlign: state.props.verticalAlign,
437
+ overflowSuffix: state.props.overflowSuffix,
438
+ w: state.props.contain !== 'none' ? state.props.width : undefined,
439
+ };
440
+ state.renderInfo = state.lightning2TextRenderer.calculateRenderInfo();
441
+ return state.renderInfo;
442
+ }
443
+
444
+ override renderQuads(): void {
445
+ // Do nothing. The renderer will render the child node(s) that were created
446
+ // in the state update.
447
+ return;
448
+ }
449
+
450
+ override destroyState(state: CanvasTextRendererState): void {
451
+ if (state.status === 'destroyed') {
452
+ return;
453
+ }
454
+ super.destroyState(state);
455
+
456
+ if (state.textureNode) {
457
+ state.textureNode.destroy();
458
+ delete state.textureNode;
459
+ }
460
+ delete state.renderInfo;
461
+ }
462
+ //#endregion Overrides
463
+
464
+ /**
465
+ * Invalidate the layout cache stored in the state. This will cause the text
466
+ * to be re-rendered on the next update.
467
+ *
468
+ * @remarks
469
+ * This also invalidates the visible window cache.
470
+ *
471
+ * @param state
472
+ */
473
+ private invalidateLayoutCache(state: CanvasTextRendererState): void {
474
+ state.renderInfo = undefined;
475
+ this.setStatus(state, 'loading');
476
+ this.scheduleUpdateState(state);
477
+ }
478
+
479
+ private onFontLoaded(
480
+ state: CanvasTextRendererState,
481
+ cssString: string,
482
+ ): void {
483
+ if (cssString !== state.fontInfo?.cssString || !state.fontInfo) {
484
+ return;
485
+ }
486
+ state.fontInfo.loaded = true;
487
+ this.scheduleUpdateState(state);
488
+ }
489
+
490
+ private onFontLoadError(
491
+ state: CanvasTextRendererState,
492
+ cssString: string,
493
+ error: Error,
494
+ ): void {
495
+ if (cssString !== state.fontInfo?.cssString || !state.fontInfo) {
496
+ return;
497
+ }
498
+
499
+ // Font didn't actually load, but we'll log the error and mark it as loaded
500
+ // because the browser can still render with a fallback font.
501
+ state.fontInfo.loaded = true;
502
+
503
+ console.error(
504
+ `CanvasTextRenderer: Error loading font '${state.fontInfo.cssString}'`,
505
+ error,
506
+ );
507
+ this.scheduleUpdateState(state);
508
+ }
509
+ }