@lightningjs/renderer 2.13.0 → 2.13.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 (213) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +147 -147
  4. package/dist/exports/core-api.d.ts +74 -0
  5. package/dist/exports/core-api.js +96 -0
  6. package/dist/exports/core-api.js.map +1 -0
  7. package/dist/exports/main-api.d.ts +30 -0
  8. package/dist/exports/main-api.js +45 -0
  9. package/dist/exports/main-api.js.map +1 -0
  10. package/dist/src/core/CoreExtension.d.ts +12 -0
  11. package/dist/src/core/CoreExtension.js +29 -0
  12. package/dist/src/core/CoreExtension.js.map +1 -0
  13. package/dist/src/core/lib/ImageWorker.js +2 -1
  14. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  15. package/dist/src/core/lib/utils.d.ts +4 -0
  16. package/dist/src/core/lib/utils.js +46 -0
  17. package/dist/src/core/lib/utils.js.map +1 -1
  18. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  19. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  20. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  21. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  22. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  23. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  24. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  25. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  26. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  27. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  28. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  29. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  30. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  31. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  32. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  33. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  34. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  35. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  36. package/dist/src/core/textures/ImageTexture.d.ts +2 -2
  37. package/dist/src/core/textures/ImageTexture.js +24 -4
  38. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  39. package/dist/src/main-api/ICoreDriver.d.ts +24 -0
  40. package/dist/src/main-api/ICoreDriver.js +20 -0
  41. package/dist/src/main-api/ICoreDriver.js.map +1 -0
  42. package/dist/src/main-api/RendererMain.d.ts +378 -0
  43. package/dist/src/main-api/RendererMain.js +367 -0
  44. package/dist/src/main-api/RendererMain.js.map +1 -0
  45. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.d.ts +9 -0
  46. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js +38 -0
  47. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js.map +1 -0
  48. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.d.ts +56 -0
  49. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js +101 -0
  50. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js.map +1 -0
  51. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.d.ts +32 -0
  52. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js +28 -0
  53. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js.map +1 -0
  54. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +21 -0
  55. package/dist/src/render-drivers/main/MainCoreDriver.js +115 -0
  56. package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -0
  57. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +101 -0
  58. package/dist/src/render-drivers/main/MainOnlyNode.js +425 -0
  59. package/dist/src/render-drivers/main/MainOnlyNode.js.map +1 -0
  60. package/dist/src/render-drivers/main/MainOnlyTextNode.d.ts +47 -0
  61. package/dist/src/render-drivers/main/MainOnlyTextNode.js +204 -0
  62. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +1 -0
  63. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +93 -0
  64. package/dist/src/render-drivers/threadx/NodeStruct.js +290 -0
  65. package/dist/src/render-drivers/threadx/NodeStruct.js.map +1 -0
  66. package/dist/src/render-drivers/threadx/SharedNode.d.ts +40 -0
  67. package/dist/src/render-drivers/threadx/SharedNode.js +61 -0
  68. package/dist/src/render-drivers/threadx/SharedNode.js.map +1 -0
  69. package/dist/src/render-drivers/threadx/TextNodeStruct.d.ts +44 -0
  70. package/dist/src/render-drivers/threadx/TextNodeStruct.js +203 -0
  71. package/dist/src/render-drivers/threadx/TextNodeStruct.js.map +1 -0
  72. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +25 -0
  73. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +232 -0
  74. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -0
  75. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.d.ts +24 -0
  76. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js +113 -0
  77. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js.map +1 -0
  78. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +46 -0
  79. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +160 -0
  80. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +1 -0
  81. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.d.ts +28 -0
  82. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js +55 -0
  83. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js.map +1 -0
  84. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +70 -0
  85. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js +32 -0
  86. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +1 -0
  87. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.d.ts +19 -0
  88. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +184 -0
  89. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +1 -0
  90. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.d.ts +27 -0
  91. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +109 -0
  92. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +1 -0
  93. package/dist/src/render-drivers/threadx/worker/renderer.d.ts +1 -0
  94. package/dist/src/render-drivers/threadx/worker/renderer.js +147 -0
  95. package/dist/src/render-drivers/threadx/worker/renderer.js.map +1 -0
  96. package/dist/src/render-drivers/utils.d.ts +12 -0
  97. package/dist/src/render-drivers/utils.js +74 -0
  98. package/dist/src/render-drivers/utils.js.map +1 -0
  99. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  100. package/dist/tsconfig.tsbuildinfo +1 -0
  101. package/exports/canvas.ts +39 -39
  102. package/exports/index.ts +89 -89
  103. package/exports/inspector.ts +24 -24
  104. package/exports/utils.ts +44 -44
  105. package/exports/webgl.ts +38 -38
  106. package/package.json +2 -1
  107. package/scripts/please-use-pnpm.js +13 -13
  108. package/src/common/CommonTypes.ts +146 -146
  109. package/src/common/EventEmitter.ts +77 -77
  110. package/src/common/IAnimationController.ts +92 -92
  111. package/src/common/IEventEmitter.ts +28 -28
  112. package/src/core/CoreNode.test.ts +202 -202
  113. package/src/core/CoreNode.ts +2455 -2455
  114. package/src/core/CoreShaderManager.ts +292 -292
  115. package/src/core/CoreTextNode.ts +455 -455
  116. package/src/core/CoreTextureManager.ts +597 -597
  117. package/src/core/Stage.ts +743 -743
  118. package/src/core/TextureMemoryManager.ts +395 -395
  119. package/src/core/animations/AnimationManager.ts +38 -38
  120. package/src/core/animations/CoreAnimation.ts +340 -340
  121. package/src/core/animations/CoreAnimationController.ts +157 -157
  122. package/src/core/lib/ContextSpy.ts +41 -41
  123. package/src/core/lib/ImageWorker.ts +280 -279
  124. package/src/core/lib/Matrix3d.ts +244 -244
  125. package/src/core/lib/RenderCoords.ts +86 -86
  126. package/src/core/lib/WebGlContextWrapper.ts +1332 -1332
  127. package/src/core/lib/textureCompression.ts +152 -152
  128. package/src/core/lib/textureSvg.ts +78 -78
  129. package/src/core/lib/utils.ts +384 -310
  130. package/src/core/lib/validateImageBitmap.ts +76 -76
  131. package/src/core/platform.ts +63 -63
  132. package/src/core/renderers/CoreContextTexture.ts +43 -43
  133. package/src/core/renderers/CoreRenderOp.ts +22 -22
  134. package/src/core/renderers/CoreRenderer.ts +115 -115
  135. package/src/core/renderers/CoreShader.ts +41 -41
  136. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +375 -375
  137. package/src/core/renderers/canvas/CanvasCoreTexture.ts +153 -153
  138. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  139. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  140. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  141. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +86 -86
  142. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -50
  143. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +301 -301
  144. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  145. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +815 -815
  146. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  147. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  148. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  149. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  150. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  151. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  152. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  153. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  154. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  155. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  156. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  157. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  158. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  159. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  160. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  161. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  162. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  163. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  164. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  165. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  166. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  167. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  168. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  169. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  170. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  171. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  172. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  173. package/src/core/text-rendering/TrFontManager.ts +183 -183
  174. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -176
  175. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  176. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  177. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  178. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  179. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +94 -94
  180. package/src/core/text-rendering/font-face-types/utils.ts +39 -39
  181. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -509
  182. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +808 -808
  183. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  184. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  185. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  186. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  187. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  188. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  189. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  190. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  191. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +408 -408
  192. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  193. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  194. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  195. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  196. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  197. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  198. package/src/core/textures/ColorTexture.ts +102 -102
  199. package/src/core/textures/ImageTexture.ts +399 -376
  200. package/src/core/textures/NoiseTexture.ts +104 -104
  201. package/src/core/textures/RenderTexture.ts +85 -85
  202. package/src/core/textures/SubTexture.ts +205 -205
  203. package/src/core/textures/Texture.ts +337 -337
  204. package/src/core/utils.ts +227 -227
  205. package/src/env.d.ts +7 -7
  206. package/src/main-api/DynamicShaderController.ts +104 -104
  207. package/src/main-api/INode.ts +101 -101
  208. package/src/main-api/Inspector.ts +522 -522
  209. package/src/main-api/Renderer.ts +751 -751
  210. package/src/main-api/ShaderController.ts +80 -80
  211. package/src/main-api/utils.ts +45 -45
  212. package/src/utils.ts +248 -248
  213. package/COPYING +0 -1
@@ -1,176 +1,176 @@
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 type { Renderer } from '../../../Renderer';
21
- import { assertTruthy } from '../../../../utils.js';
22
- import type { Stage } from '../../../Stage.js';
23
- import { WebGlCoreRenderer } from '../../../renderers/webgl/WebGlCoreRenderer.js';
24
- import { ImageTexture } from '../../../textures/ImageTexture.js';
25
- import {
26
- TrFontFace,
27
- type NormalizedFontMetrics,
28
- type TrFontFaceOptions,
29
- } from '../TrFontFace.js';
30
- import { fetchJson } from '../utils.js';
31
- import type { FontShaper } from './internal/FontShaper.js';
32
- import { SdfFontShaper, type SdfFontData } from './internal/SdfFontShaper.js';
33
-
34
- type SdfFontType = 'ssdf' | 'msdf';
35
-
36
- declare module '../TrFontFace.js' {
37
- interface TrFontFaceMap {
38
- ssdf: SdfTrFontFace<'ssdf'>;
39
- msdf: SdfTrFontFace<'msdf'>;
40
- }
41
- }
42
-
43
- interface GlyphAtlasEntry {
44
- x: number;
45
- y: number;
46
- width: number;
47
- height: number;
48
- }
49
-
50
- export interface SdfTrFontFaceOptions extends TrFontFaceOptions {
51
- atlasUrl: string;
52
- atlasDataUrl: string;
53
- stage: Stage;
54
- }
55
-
56
- export class SdfTrFontFace<
57
- FontTypeT extends SdfFontType = SdfFontType,
58
- > extends TrFontFace {
59
- public readonly type: FontTypeT;
60
- public readonly texture: ImageTexture;
61
- /**
62
- * Height of the tallest character in the font including the whitespace above it
63
- * in SDF/vertex units.
64
- */
65
- public readonly maxCharHeight: number = 0;
66
- public readonly shaper: FontShaper | undefined;
67
- public readonly glyphMap: Map<number, SdfFontData['chars'][0]> = new Map();
68
- public data: SdfFontData | undefined;
69
-
70
- constructor(type: FontTypeT, options: SdfTrFontFaceOptions) {
71
- super(options);
72
- const { atlasUrl, atlasDataUrl, stage } = options;
73
- this.type = type;
74
- const renderer = stage.renderer;
75
- assertTruthy(
76
- renderer instanceof WebGlCoreRenderer,
77
- 'SDF Font Faces can only be used with the WebGL Renderer',
78
- );
79
-
80
- // Load image
81
- this.texture = stage.txManager.createTexture('ImageTexture', {
82
- src: atlasUrl,
83
- // IMPORTANT: The SDF shader requires the alpha channel to NOT be
84
- // premultiplied on the atlas texture. If it is premultiplied, the
85
- // rendering of SDF glyphs (especially single-channel SDF fonts) will
86
- // be very jagged.
87
- premultiplyAlpha: false,
88
- });
89
-
90
- // Load the texture
91
- stage.txManager.loadTexture(this.texture, true);
92
-
93
- // FIXME This is a stop-gap solution to avoid Font Face textures to be cleaned up
94
- // Ideally we do want to clean up the textures if they're not being used to save as much memory as possible
95
- // However, we need to make sure that the font face is reloaded if the texture is cleaned up and needed again
96
- // and make sure the SdfFontRenderer is properly guarded against textures being reloaded
97
- // for now this will do the trick and the increase on memory is not that big
98
- this.texture.preventCleanup = true;
99
-
100
- this.texture.on('loaded', () => {
101
- this.checkLoaded();
102
- // Make sure we mark the stage for a re-render (in case the font's texture was freed and reloaded)
103
- stage.requestRender();
104
- });
105
-
106
- // Set this.data to the fetched data from dataUrl
107
- fetchJson(atlasDataUrl)
108
- .then((response) => {
109
- this.data = JSON.parse(response as unknown as string) as SdfFontData;
110
- assertTruthy(this.data);
111
- // Add all the glyphs to the glyph map
112
-
113
- let maxCharHeight = 0;
114
- this.data.chars.forEach((glyph) => {
115
- this.glyphMap.set(glyph.id, glyph);
116
- const charHeight = glyph.yoffset + glyph.height;
117
- if (charHeight > maxCharHeight) {
118
- maxCharHeight = charHeight;
119
- }
120
- });
121
-
122
- (this.maxCharHeight as number) = maxCharHeight;
123
- // We know `data` is defined here, because we just set it
124
-
125
- (this.shaper as FontShaper) = new SdfFontShaper(
126
- this.data,
127
- this.glyphMap,
128
- );
129
-
130
- // If the metrics aren't provided explicitly in the font face options,
131
- // Gather them from the metrics added by the msdf-generator tool ()
132
- // If they are missing then we throw an error.
133
- if (!this.metrics) {
134
- if (this.data?.lightningMetrics) {
135
- const { ascender, descender, lineGap, unitsPerEm } =
136
- this.data.lightningMetrics;
137
- (this.metrics as NormalizedFontMetrics | null) = {
138
- ascender: ascender / unitsPerEm,
139
- descender: descender / unitsPerEm,
140
- lineGap: lineGap / unitsPerEm,
141
- };
142
- } else {
143
- throw new Error(
144
- `Font metrics not found in ${this.type} font ${this.fontFamily}. ` +
145
- 'Make sure you are using the latest version of the Lightning ' +
146
- '3 `msdf-generator` tool to generate your SDF fonts.',
147
- );
148
- }
149
- }
150
-
151
- this.checkLoaded();
152
- })
153
- .catch(console.error);
154
- }
155
-
156
- getAtlasEntry(glyphId: number): GlyphAtlasEntry {
157
- const glyph = this.glyphMap.get(glyphId);
158
- if (glyph === undefined) {
159
- throw new Error(`Glyph ${glyphId} not found in font ${this.fontFamily}`);
160
- }
161
- return {
162
- x: glyph.x,
163
- y: glyph.y,
164
- width: glyph.width,
165
- height: glyph.height,
166
- };
167
- }
168
-
169
- private checkLoaded(): void {
170
- if (this.loaded) return;
171
- if (this.texture.state === 'loaded' && this.data) {
172
- (this.loaded as boolean) = true;
173
- this.emit('loaded');
174
- }
175
- }
176
- }
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 type { Renderer } from '../../../Renderer';
21
+ import { assertTruthy } from '../../../../utils.js';
22
+ import type { Stage } from '../../../Stage.js';
23
+ import { WebGlCoreRenderer } from '../../../renderers/webgl/WebGlCoreRenderer.js';
24
+ import { ImageTexture } from '../../../textures/ImageTexture.js';
25
+ import {
26
+ TrFontFace,
27
+ type NormalizedFontMetrics,
28
+ type TrFontFaceOptions,
29
+ } from '../TrFontFace.js';
30
+ import { fetchJson } from '../utils.js';
31
+ import type { FontShaper } from './internal/FontShaper.js';
32
+ import { SdfFontShaper, type SdfFontData } from './internal/SdfFontShaper.js';
33
+
34
+ type SdfFontType = 'ssdf' | 'msdf';
35
+
36
+ declare module '../TrFontFace.js' {
37
+ interface TrFontFaceMap {
38
+ ssdf: SdfTrFontFace<'ssdf'>;
39
+ msdf: SdfTrFontFace<'msdf'>;
40
+ }
41
+ }
42
+
43
+ interface GlyphAtlasEntry {
44
+ x: number;
45
+ y: number;
46
+ width: number;
47
+ height: number;
48
+ }
49
+
50
+ export interface SdfTrFontFaceOptions extends TrFontFaceOptions {
51
+ atlasUrl: string;
52
+ atlasDataUrl: string;
53
+ stage: Stage;
54
+ }
55
+
56
+ export class SdfTrFontFace<
57
+ FontTypeT extends SdfFontType = SdfFontType,
58
+ > extends TrFontFace {
59
+ public readonly type: FontTypeT;
60
+ public readonly texture: ImageTexture;
61
+ /**
62
+ * Height of the tallest character in the font including the whitespace above it
63
+ * in SDF/vertex units.
64
+ */
65
+ public readonly maxCharHeight: number = 0;
66
+ public readonly shaper: FontShaper | undefined;
67
+ public readonly glyphMap: Map<number, SdfFontData['chars'][0]> = new Map();
68
+ public data: SdfFontData | undefined;
69
+
70
+ constructor(type: FontTypeT, options: SdfTrFontFaceOptions) {
71
+ super(options);
72
+ const { atlasUrl, atlasDataUrl, stage } = options;
73
+ this.type = type;
74
+ const renderer = stage.renderer;
75
+ assertTruthy(
76
+ renderer instanceof WebGlCoreRenderer,
77
+ 'SDF Font Faces can only be used with the WebGL Renderer',
78
+ );
79
+
80
+ // Load image
81
+ this.texture = stage.txManager.createTexture('ImageTexture', {
82
+ src: atlasUrl,
83
+ // IMPORTANT: The SDF shader requires the alpha channel to NOT be
84
+ // premultiplied on the atlas texture. If it is premultiplied, the
85
+ // rendering of SDF glyphs (especially single-channel SDF fonts) will
86
+ // be very jagged.
87
+ premultiplyAlpha: false,
88
+ });
89
+
90
+ // Load the texture
91
+ stage.txManager.loadTexture(this.texture, true);
92
+
93
+ // FIXME This is a stop-gap solution to avoid Font Face textures to be cleaned up
94
+ // Ideally we do want to clean up the textures if they're not being used to save as much memory as possible
95
+ // However, we need to make sure that the font face is reloaded if the texture is cleaned up and needed again
96
+ // and make sure the SdfFontRenderer is properly guarded against textures being reloaded
97
+ // for now this will do the trick and the increase on memory is not that big
98
+ this.texture.preventCleanup = true;
99
+
100
+ this.texture.on('loaded', () => {
101
+ this.checkLoaded();
102
+ // Make sure we mark the stage for a re-render (in case the font's texture was freed and reloaded)
103
+ stage.requestRender();
104
+ });
105
+
106
+ // Set this.data to the fetched data from dataUrl
107
+ fetchJson(atlasDataUrl)
108
+ .then((response) => {
109
+ this.data = JSON.parse(response as unknown as string) as SdfFontData;
110
+ assertTruthy(this.data);
111
+ // Add all the glyphs to the glyph map
112
+
113
+ let maxCharHeight = 0;
114
+ this.data.chars.forEach((glyph) => {
115
+ this.glyphMap.set(glyph.id, glyph);
116
+ const charHeight = glyph.yoffset + glyph.height;
117
+ if (charHeight > maxCharHeight) {
118
+ maxCharHeight = charHeight;
119
+ }
120
+ });
121
+
122
+ (this.maxCharHeight as number) = maxCharHeight;
123
+ // We know `data` is defined here, because we just set it
124
+
125
+ (this.shaper as FontShaper) = new SdfFontShaper(
126
+ this.data,
127
+ this.glyphMap,
128
+ );
129
+
130
+ // If the metrics aren't provided explicitly in the font face options,
131
+ // Gather them from the metrics added by the msdf-generator tool ()
132
+ // If they are missing then we throw an error.
133
+ if (!this.metrics) {
134
+ if (this.data?.lightningMetrics) {
135
+ const { ascender, descender, lineGap, unitsPerEm } =
136
+ this.data.lightningMetrics;
137
+ (this.metrics as NormalizedFontMetrics | null) = {
138
+ ascender: ascender / unitsPerEm,
139
+ descender: descender / unitsPerEm,
140
+ lineGap: lineGap / unitsPerEm,
141
+ };
142
+ } else {
143
+ throw new Error(
144
+ `Font metrics not found in ${this.type} font ${this.fontFamily}. ` +
145
+ 'Make sure you are using the latest version of the Lightning ' +
146
+ '3 `msdf-generator` tool to generate your SDF fonts.',
147
+ );
148
+ }
149
+ }
150
+
151
+ this.checkLoaded();
152
+ })
153
+ .catch(console.error);
154
+ }
155
+
156
+ getAtlasEntry(glyphId: number): GlyphAtlasEntry {
157
+ const glyph = this.glyphMap.get(glyphId);
158
+ if (glyph === undefined) {
159
+ throw new Error(`Glyph ${glyphId} not found in font ${this.fontFamily}`);
160
+ }
161
+ return {
162
+ x: glyph.x,
163
+ y: glyph.y,
164
+ width: glyph.width,
165
+ height: glyph.height,
166
+ };
167
+ }
168
+
169
+ private checkLoaded(): void {
170
+ if (this.loaded) return;
171
+ if (this.texture.state === 'loaded' && this.data) {
172
+ (this.loaded as boolean) = true;
173
+ this.emit('loaded');
174
+ }
175
+ }
176
+ }
@@ -1,139 +1,139 @@
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 type { PeekableIterator } from '../../../renderers/SdfTextRenderer/internal/PeekableGenerator.js';
21
-
22
- /**
23
- * HarfBuzz compatible glyph position info
24
- */
25
- export interface GlyphPosition {
26
- /**
27
- * how much the line advances after drawing this glyph when setting text in horizontal direction.
28
- */
29
- xAdvance: number;
30
- /**
31
- * how much the line advances after drawing this glyph when setting text in vertical direction.
32
- */
33
- yAdvance: number;
34
- /**
35
- * how much the glyph moves on the X-axis before drawing it, this should not affect how much the line advances.
36
- */
37
- xOffset: number;
38
- /**
39
- * how much the glyph moves on the Y-axis before drawing it, this should not affect how much the line advances.
40
- */
41
- yOffset: number;
42
- }
43
-
44
- /**
45
- * HarfBuzz compatible glyph info
46
- */
47
- export interface GlyphInfo {
48
- /**
49
- * Glyph ID
50
- */
51
- glyphId: number;
52
-
53
- /**
54
- * First codepoint in the cluster this glyph was mapped from.
55
- */
56
- codepoint: number;
57
-
58
- /**
59
- * the index of the character in the original text that corresponds to this hb_glyph_info_t
60
- * This is useful for mapping back from glyphs to characters.
61
- */
62
- cluster: number;
63
- }
64
-
65
- /**
66
- * HarfBuzz compatible glyph extents
67
- */
68
- export interface GlyphExtents {
69
- /**
70
- * The X offset from the origin to the leftmost part of the glyph.
71
- * This is negative for right-to-left scripts.
72
- */
73
- xBearing: number;
74
-
75
- /**
76
- * The Y offset from the origin to the topmost part of the glyph.
77
- * This is negative for bottom-to-top scripts.
78
- */
79
- yBearing: number;
80
-
81
- /**
82
- * Distance from the top extremum of the glyph to the bottom extremum.
83
- * This is always positive.
84
- */
85
- width: number;
86
-
87
- /**
88
- * Distance from the left extremum of the glyph to the right extremum.
89
- * This is always positive.
90
- */
91
- height: number;
92
- }
93
-
94
- export interface UnmappedCharacterInfo {
95
- mapped: false;
96
-
97
- /**
98
- * The Unicode code point of the character.
99
- */
100
- codepoint: number;
101
-
102
- /**
103
- * The index of the character in the original text that corresponds to this hb_glyph_info_t
104
- * This is useful for mapping back from glyphs to characters.
105
- */
106
- cluster: number;
107
- }
108
-
109
- export type MappedGlyphInfo = { mapped: true } & GlyphInfo &
110
- GlyphPosition &
111
- GlyphExtents;
112
-
113
- /**
114
- * For performance reasons, all properties are required to reduce need for checking for undefined values.
115
- */
116
- export interface FontShaperProps {
117
- /**
118
- * Letter spacing
119
- *
120
- * @default 0
121
- * @see https://developer.mozilla.org/en-US/docs/Web/CSS/letter-spacing
122
- **/
123
- letterSpacing: number;
124
- }
125
-
126
- export abstract class FontShaper {
127
- /**
128
- * Returns the glyph IDs of the next
129
- *
130
- * @remarks
131
- * If this returns undefined, a glyph for the next codepoint(s) could not be found.
132
- *
133
- * @param codepoints
134
- */
135
- abstract shapeText(
136
- props: FontShaperProps,
137
- codepoints: PeekableIterator<number>,
138
- ): Generator<MappedGlyphInfo | UnmappedCharacterInfo, void>;
139
- }
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 type { PeekableIterator } from '../../../renderers/SdfTextRenderer/internal/PeekableGenerator.js';
21
+
22
+ /**
23
+ * HarfBuzz compatible glyph position info
24
+ */
25
+ export interface GlyphPosition {
26
+ /**
27
+ * how much the line advances after drawing this glyph when setting text in horizontal direction.
28
+ */
29
+ xAdvance: number;
30
+ /**
31
+ * how much the line advances after drawing this glyph when setting text in vertical direction.
32
+ */
33
+ yAdvance: number;
34
+ /**
35
+ * how much the glyph moves on the X-axis before drawing it, this should not affect how much the line advances.
36
+ */
37
+ xOffset: number;
38
+ /**
39
+ * how much the glyph moves on the Y-axis before drawing it, this should not affect how much the line advances.
40
+ */
41
+ yOffset: number;
42
+ }
43
+
44
+ /**
45
+ * HarfBuzz compatible glyph info
46
+ */
47
+ export interface GlyphInfo {
48
+ /**
49
+ * Glyph ID
50
+ */
51
+ glyphId: number;
52
+
53
+ /**
54
+ * First codepoint in the cluster this glyph was mapped from.
55
+ */
56
+ codepoint: number;
57
+
58
+ /**
59
+ * the index of the character in the original text that corresponds to this hb_glyph_info_t
60
+ * This is useful for mapping back from glyphs to characters.
61
+ */
62
+ cluster: number;
63
+ }
64
+
65
+ /**
66
+ * HarfBuzz compatible glyph extents
67
+ */
68
+ export interface GlyphExtents {
69
+ /**
70
+ * The X offset from the origin to the leftmost part of the glyph.
71
+ * This is negative for right-to-left scripts.
72
+ */
73
+ xBearing: number;
74
+
75
+ /**
76
+ * The Y offset from the origin to the topmost part of the glyph.
77
+ * This is negative for bottom-to-top scripts.
78
+ */
79
+ yBearing: number;
80
+
81
+ /**
82
+ * Distance from the top extremum of the glyph to the bottom extremum.
83
+ * This is always positive.
84
+ */
85
+ width: number;
86
+
87
+ /**
88
+ * Distance from the left extremum of the glyph to the right extremum.
89
+ * This is always positive.
90
+ */
91
+ height: number;
92
+ }
93
+
94
+ export interface UnmappedCharacterInfo {
95
+ mapped: false;
96
+
97
+ /**
98
+ * The Unicode code point of the character.
99
+ */
100
+ codepoint: number;
101
+
102
+ /**
103
+ * The index of the character in the original text that corresponds to this hb_glyph_info_t
104
+ * This is useful for mapping back from glyphs to characters.
105
+ */
106
+ cluster: number;
107
+ }
108
+
109
+ export type MappedGlyphInfo = { mapped: true } & GlyphInfo &
110
+ GlyphPosition &
111
+ GlyphExtents;
112
+
113
+ /**
114
+ * For performance reasons, all properties are required to reduce need for checking for undefined values.
115
+ */
116
+ export interface FontShaperProps {
117
+ /**
118
+ * Letter spacing
119
+ *
120
+ * @default 0
121
+ * @see https://developer.mozilla.org/en-US/docs/Web/CSS/letter-spacing
122
+ **/
123
+ letterSpacing: number;
124
+ }
125
+
126
+ export abstract class FontShaper {
127
+ /**
128
+ * Returns the glyph IDs of the next
129
+ *
130
+ * @remarks
131
+ * If this returns undefined, a glyph for the next codepoint(s) could not be found.
132
+ *
133
+ * @param codepoints
134
+ */
135
+ abstract shapeText(
136
+ props: FontShaperProps,
137
+ codepoints: PeekableIterator<number>,
138
+ ): Generator<MappedGlyphInfo | UnmappedCharacterInfo, void>;
139
+ }