@lightningjs/renderer 0.8.4 → 0.9.0

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 (59) hide show
  1. package/dist/src/core/lib/Matrix3d.d.ts +38 -24
  2. package/dist/src/core/lib/Matrix3d.js +143 -166
  3. package/dist/src/core/lib/Matrix3d.js.map +1 -1
  4. package/dist/src/core/text-rendering/TextTextureRendererUtils.d.ts +19 -0
  5. package/dist/src/core/text-rendering/TextTextureRendererUtils.js +61 -0
  6. package/dist/src/core/text-rendering/TextTextureRendererUtils.js.map +1 -1
  7. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.d.ts +8 -2
  8. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +24 -3
  9. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  10. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.d.ts +2 -0
  11. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.js.map +1 -1
  12. package/dist/src/core/text-rendering/font-face-types/TrFontFace.d.ts +72 -1
  13. package/dist/src/core/text-rendering/font-face-types/TrFontFace.js +11 -1
  14. package/dist/src/core/text-rendering/font-face-types/TrFontFace.js.map +1 -1
  15. package/dist/src/core/text-rendering/font-face-types/WebTrFontFace.d.ts +5 -2
  16. package/dist/src/core/text-rendering/font-face-types/WebTrFontFace.js +4 -3
  17. package/dist/src/core/text-rendering/font-face-types/WebTrFontFace.js.map +1 -1
  18. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.d.ts +8 -0
  19. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +42 -16
  20. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  21. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.d.ts +7 -1
  22. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js +42 -11
  23. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js.map +1 -1
  24. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.d.ts +4 -0
  25. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js +26 -9
  26. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
  27. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.d.ts +2 -1
  28. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.js +32 -5
  29. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.js.map +1 -1
  30. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.d.ts +2 -1
  31. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +2 -1
  32. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
  33. package/dist/src/core/text-rendering/renderers/TextRenderer.d.ts +8 -4
  34. package/dist/src/core/text-rendering/renderers/TextRenderer.js +6 -5
  35. package/dist/src/core/text-rendering/renderers/TextRenderer.js.map +1 -1
  36. package/dist/src/main-api/RendererMain.js +2 -2
  37. package/dist/src/main-api/RendererMain.js.map +1 -1
  38. package/dist/src/render-drivers/main/MainOnlyTextNode.js +1 -3
  39. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +1 -1
  40. package/dist/src/render-drivers/threadx/TextNodeStruct.js +3 -1
  41. package/dist/src/render-drivers/threadx/TextNodeStruct.js.map +1 -1
  42. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  43. package/package.json +2 -2
  44. package/src/core/lib/Matrix3d.ts +144 -190
  45. package/src/core/text-rendering/TextRenderingUtils.ts +36 -0
  46. package/src/core/text-rendering/TextTextureRendererUtils.ts +74 -0
  47. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +41 -12
  48. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +2 -0
  49. package/src/core/text-rendering/font-face-types/TrFontFace.ts +86 -1
  50. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +13 -7
  51. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +52 -20
  52. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +59 -13
  53. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +30 -9
  54. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +38 -5
  55. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +5 -2
  56. package/src/core/text-rendering/renderers/TextRenderer.ts +14 -10
  57. package/src/main-api/RendererMain.ts +2 -2
  58. package/src/render-drivers/main/MainOnlyTextNode.ts +1 -3
  59. package/src/render-drivers/threadx/TextNodeStruct.ts +3 -1
@@ -1,71 +1,69 @@
1
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
-
3
- // Matrix3d is a 3x3 matrix in column-major order because that's how WebGL likes it.
4
- // The matrix is stored in a Float32Array in the following order:
5
- // | 0 3 6 |
6
- // | 1 4 7 |
7
- // | 2 5 8 |
8
- // The following constants are used to index into the array in a row-major way.
9
- const m0 = 0;
10
- const m1 = 3;
11
- const m2 = 6;
12
- const m3 = 1;
13
- const m4 = 4;
14
- const m5 = 7;
15
- const m6 = 2;
16
- const m7 = 5;
17
- const m8 = 8;
18
-
19
- /**
20
- * A 3x3 matrix representing a 2D transformation.
1
+ /*
2
+ * If not stated otherwise in this file or this component's LICENSE file the
3
+ * following copyright and licenses apply:
21
4
  *
22
- * @remarks
23
- * The matrix is stored in column-major order in the `data` property which can
24
- * be passed directly to a WebGL shader uniform.
5
+ * Copyright 2023 Comcast Cable Communications Management, LLC.
25
6
  *
26
- * The matrix is stored in a Float32Array in the following index order:
27
- * | 0 3 6 |
28
- * | 1 4 7 |
29
- * | 2 5 8 |
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
30
10
  *
31
- * Only the first two rows are really used for the transformation. The last row is
32
- * generally always `[0, 0, 1]` if you only use the 2D transformation methods
33
- * provided by this class.
11
+ * http://www.apache.org/licenses/LICENSE-2.0
34
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
+ /**
21
+ * A 3D matrix representing a 2D graphics transformation
22
+ *
23
+ * @remarks
35
24
  * For convenience, entries in the first two rows can be accessed by the following
36
- * getter properties:
25
+ * properties:
37
26
  * | ta tb tx |
38
27
  * | tc td ty |
39
28
  * | 0 0 1 |
29
+ *
30
+ * This matrix is optimized for 2D transformations and hence the last row will
31
+ * always be considered [0, 0 ,1].
32
+ *
33
+ * To access a column major array for WebGL, use the {@link getFloatArr} method.
40
34
  */
41
35
  export class Matrix3d {
42
- public data: Float32Array;
36
+ public ta: number;
37
+ public tb: number;
38
+ public tx: number;
39
+ public tc: number;
40
+ public td: number;
41
+ public ty: number;
42
+ private _floatArr: Float32Array | null = null;
43
+ /**
44
+ * Potential Mutation Flag
45
+ *
46
+ * @remarks
47
+ * This flag is set to true whenever the matrix is potentially modified.
48
+ * We don't waste CPU trying to identify if each operation actually modifies
49
+ * the matrix. Instead, we set this flag to true whenever we think the matrix
50
+ * is modified. This signals that the `floatArr` should to be updated.
51
+ */
52
+ private mutation: boolean;
43
53
 
44
54
  /**
45
55
  * Creates a new 3x3 matrix.
46
56
  *
47
57
  * @param entries Row-major 3x3 matrix
48
58
  */
49
- public constructor(
50
- entries?:
51
- | [number, number, number, number, number, number, number, number, number]
52
- | Float32Array,
53
- ) {
54
- if (entries) {
55
- // Transpose the input matrix so that it's in column-major order.
56
- this.data = new Float32Array(9);
57
- this.data[m0] = entries[0]!;
58
- this.data[m1] = entries[3]!;
59
- this.data[m2] = entries[6]!;
60
- this.data[m3] = entries[1]!;
61
- this.data[m4] = entries[4]!;
62
- this.data[m5] = entries[7]!;
63
- this.data[m6] = entries[2]!;
64
- this.data[m7] = entries[5]!;
65
- this.data[m8] = entries[8]!;
66
- } else {
67
- this.data = new Float32Array(9);
68
- }
59
+ public constructor() {
60
+ this.ta = 0;
61
+ this.tb = 0;
62
+ this.tx = 0;
63
+ this.tc = 0;
64
+ this.td = 0;
65
+ this.ty = 0;
66
+ this.mutation = true;
69
67
  }
70
68
 
71
69
  /**
@@ -83,54 +81,22 @@ export class Matrix3d {
83
81
  }
84
82
 
85
83
  public static multiply(a: Matrix3d, b: Matrix3d, out?: Matrix3d): Matrix3d {
86
- const e0 =
87
- a.data[m0]! * b.data[m0]! +
88
- a.data[m1]! * b.data[m3]! +
89
- a.data[m2]! * b.data[m6]!;
90
- const e1 =
91
- a.data[m0]! * b.data[m1]! +
92
- a.data[m1]! * b.data[m4]! +
93
- a.data[m2]! * b.data[m7]!;
94
- const e2 =
95
- a.data[m0]! * b.data[m2]! +
96
- a.data[m1]! * b.data[m5]! +
97
- a.data[m2]! * b.data[m8]!;
98
- const e3 =
99
- a.data[m3]! * b.data[m0]! +
100
- a.data[m4]! * b.data[m3]! +
101
- a.data[m5]! * b.data[m6]!;
102
- const e4 =
103
- a.data[m3]! * b.data[m1]! +
104
- a.data[m4]! * b.data[m4]! +
105
- a.data[m5]! * b.data[m7]!;
106
- const e5 =
107
- a.data[m3]! * b.data[m2]! +
108
- a.data[m4]! * b.data[m5]! +
109
- a.data[m5]! * b.data[m8]!;
110
- const e6 =
111
- a.data[m6]! * b.data[m0]! +
112
- a.data[m7]! * b.data[m3]! +
113
- a.data[m8]! * b.data[m6]!;
114
- const e7 =
115
- a.data[m6]! * b.data[m1]! +
116
- a.data[m7]! * b.data[m4]! +
117
- a.data[m8]! * b.data[m7]!;
118
- const e8 =
119
- a.data[m6]! * b.data[m2]! +
120
- a.data[m7]! * b.data[m5]! +
121
- a.data[m8]! * b.data[m8]!;
84
+ const e0 = a.ta * b.ta + a.tb * b.tc;
85
+ const e1 = a.ta * b.tb + a.tb * b.td;
86
+ const e2 = a.ta * b.tx + a.tb * b.ty + a.tx;
87
+ const e3 = a.tc * b.ta + a.td * b.tc;
88
+ const e4 = a.tc * b.tb + a.td * b.td;
89
+ const e5 = a.tc * b.tx + a.td * b.ty + a.ty;
122
90
  if (!out) {
123
91
  out = new Matrix3d();
124
92
  }
125
- out.data[m0] = e0;
126
- out.data[m1] = e1;
127
- out.data[m2] = e2;
128
- out.data[m3] = e3;
129
- out.data[m4] = e4;
130
- out.data[m5] = e5;
131
- out.data[m6] = e6;
132
- out.data[m7] = e7;
133
- out.data[m8] = e8;
93
+ out.ta = e0;
94
+ out.tb = e1;
95
+ out.tx = e2;
96
+ out.tc = e3;
97
+ out.td = e4;
98
+ out.ty = e5;
99
+ out.mutation = true;
134
100
  return out;
135
101
  }
136
102
 
@@ -138,15 +104,13 @@ export class Matrix3d {
138
104
  if (!out) {
139
105
  out = new Matrix3d();
140
106
  }
141
- out.data[m0] = 1;
142
- out.data[m1] = 0;
143
- out.data[m2] = 0;
144
- out.data[m3] = 0;
145
- out.data[m4] = 1;
146
- out.data[m5] = 0;
147
- out.data[m6] = 0;
148
- out.data[m7] = 0;
149
- out.data[m8] = 1;
107
+ out.ta = 1;
108
+ out.tb = 0;
109
+ out.tx = 0;
110
+ out.tc = 0;
111
+ out.td = 1;
112
+ out.ty = 0;
113
+ out.mutation = true;
150
114
  return out;
151
115
  }
152
116
 
@@ -154,15 +118,13 @@ export class Matrix3d {
154
118
  if (!out) {
155
119
  out = new Matrix3d();
156
120
  }
157
- out.data[m0] = 1;
158
- out.data[m1] = 0;
159
- out.data[m2] = x;
160
- out.data[m3] = 0;
161
- out.data[m4] = 1;
162
- out.data[m5] = y;
163
- out.data[m6] = 0;
164
- out.data[m7] = 0;
165
- out.data[m8] = 1;
121
+ out.ta = 1;
122
+ out.tb = 0;
123
+ out.tx = x;
124
+ out.tc = 0;
125
+ out.td = 1;
126
+ out.ty = y;
127
+ out.mutation = true;
166
128
  return out;
167
129
  }
168
130
 
@@ -170,15 +132,13 @@ export class Matrix3d {
170
132
  if (!out) {
171
133
  out = new Matrix3d();
172
134
  }
173
- out.data[m0] = sx;
174
- out.data[m1] = 0;
175
- out.data[m2] = 0;
176
- out.data[m3] = 0;
177
- out.data[m4] = sy;
178
- out.data[m5] = 0;
179
- out.data[m6] = 0;
180
- out.data[m7] = 0;
181
- out.data[m8] = 1;
135
+ out.ta = sx;
136
+ out.tb = 0;
137
+ out.tx = 0;
138
+ out.tc = 0;
139
+ out.td = sy;
140
+ out.ty = 0;
141
+ out.mutation = true;
182
142
  return out;
183
143
  }
184
144
 
@@ -188,49 +148,43 @@ export class Matrix3d {
188
148
  if (!out) {
189
149
  out = new Matrix3d();
190
150
  }
191
- out.data[m0] = cos;
192
- out.data[m1] = -sin;
193
- out.data[m2] = 0;
194
- out.data[m3] = sin;
195
- out.data[m4] = cos;
196
- out.data[m5] = 0;
197
- out.data[m6] = 0;
198
- out.data[m7] = 0;
199
- out.data[m8] = 1;
151
+ out.ta = cos;
152
+ out.tb = -sin;
153
+ out.tx = 0;
154
+ out.tc = sin;
155
+ out.td = cos;
156
+ out.ty = 0;
157
+ out.mutation = true;
200
158
  return out;
201
159
  }
202
160
 
203
- public static copy(
204
- src: Matrix3d,
205
- dst?: Matrix3d,
206
- transpose?: boolean,
207
- ): Matrix3d {
161
+ public static copy(src: Matrix3d, dst?: Matrix3d): Matrix3d {
208
162
  if (!dst) {
209
163
  dst = new Matrix3d();
210
164
  }
211
- dst.data[0] = src.data[0]!;
212
- dst.data[1] = src.data[1]!;
213
- dst.data[2] = src.data[2]!;
214
- dst.data[3] = src.data[3]!;
215
- dst.data[4] = src.data[4]!;
216
- dst.data[5] = src.data[5]!;
217
- dst.data[6] = src.data[6]!;
218
- dst.data[7] = src.data[7]!;
219
- dst.data[8] = src.data[8]!;
165
+ dst.ta = src.ta;
166
+ dst.tc = src.tc;
167
+ dst.tb = src.tb;
168
+ dst.td = src.td;
169
+ dst.tx = src.tx;
170
+ dst.ty = src.ty;
171
+ dst.mutation = true;
220
172
  return dst;
221
173
  }
222
174
 
223
175
  public translate(x: number, y: number): Matrix3d {
224
- this.data[m2] = this.data[m0]! * x + this.data[m1]! * y + this.data[m2]!;
225
- this.data[m5] = this.data[m3]! * x + this.data[m4]! * y + this.data[m5]!;
176
+ this.tx = this.ta * x + this.tb * y + this.tx;
177
+ this.ty = this.tc * x + this.td * y + this.ty;
178
+ this.mutation = true;
226
179
  return this;
227
180
  }
228
181
 
229
182
  public scale(sx: number, sy: number): Matrix3d {
230
- this.data[m0] = this.data[m0]! * sx;
231
- this.data[m1] = this.data[m1]! * sy;
232
- this.data[m3] = this.data[m3]! * sx;
233
- this.data[m4] = this.data[m4]! * sy;
183
+ this.ta = this.ta * sx;
184
+ this.tb = this.tb * sy;
185
+ this.tc = this.tc * sx;
186
+ this.td = this.td * sy;
187
+ this.mutation = true;
234
188
  return this;
235
189
  }
236
190
 
@@ -240,14 +194,15 @@ export class Matrix3d {
240
194
  }
241
195
  const cos = Math.cos(angle);
242
196
  const sin = Math.sin(angle);
243
- const e0 = this.data[m0]! * cos + this.data[m1]! * sin;
244
- const e1 = this.data[m1]! * cos - this.data[m0]! * sin;
245
- const e3 = this.data[m3]! * cos + this.data[m4]! * sin;
246
- const e4 = this.data[m4]! * cos - this.data[m3]! * sin;
247
- this.data[m0] = e0;
248
- this.data[m1] = e1;
249
- this.data[m3] = e3;
250
- this.data[m4] = e4;
197
+ const e0 = this.ta * cos + this.tb * sin;
198
+ const e1 = this.tb * cos - this.ta * sin;
199
+ const e3 = this.tc * cos + this.td * sin;
200
+ const e4 = this.td * cos - this.tc * sin;
201
+ this.ta = e0;
202
+ this.tb = e1;
203
+ this.tc = e3;
204
+ this.td = e4;
205
+ this.mutation = true;
251
206
  return this;
252
207
  }
253
208
 
@@ -255,35 +210,34 @@ export class Matrix3d {
255
210
  return Matrix3d.multiply(this, other, this);
256
211
  }
257
212
 
258
- get tx(): number {
259
- return this.data[m2]!;
260
- }
261
-
262
- get ty(): number {
263
- return this.data[m5]!;
264
- }
265
-
266
- get ta(): number {
267
- return this.data[m0]!;
268
- }
269
-
270
- get tb(): number {
271
- return this.data[m1]!;
272
- }
273
-
274
- get tc(): number {
275
- return this.data[m3]!;
276
- }
277
-
278
- get td(): number {
279
- return this.data[m4]!;
280
- }
281
-
282
- transformPoint(x: number, y: number): [number, number] {
283
- return [
284
- this.data[m0]! * x + this.data[m1]! * y + this.data[m2]!,
285
- this.data[m3]! * x + this.data[m4]! * y + this.data[m3]!,
286
- ];
213
+ /**
214
+ * Returns the matrix as a Float32Array in column-major order.
215
+ *
216
+ * @remarks
217
+ * This method is optimized to avoid unnecessary allocations. The same array
218
+ * is returned every time this method is called, and is updated in place.
219
+ *
220
+ * WARNING: Use the array only for passing directly to a WebGL shader uniform
221
+ * during a frame render. Do not modify or hold onto the array for longer than
222
+ * a frame.
223
+ */
224
+ getFloatArr(): Float32Array {
225
+ if (!this._floatArr) {
226
+ this._floatArr = new Float32Array(9);
227
+ }
228
+ if (this.mutation) {
229
+ this._floatArr[0] = this.ta;
230
+ this._floatArr[1] = this.tc;
231
+ this._floatArr[2] = 0;
232
+ this._floatArr[3] = this.tb;
233
+ this._floatArr[4] = this.td;
234
+ this._floatArr[5] = 0;
235
+ this._floatArr[6] = this.tx;
236
+ this._floatArr[7] = this.ty;
237
+ this._floatArr[8] = 1;
238
+ this.mutation = false;
239
+ }
240
+ return this._floatArr;
287
241
  }
288
242
  }
289
243
 
@@ -0,0 +1,36 @@
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 2024 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
+ import type { NormalizedFontMetrics } from './font-face-types/TrFontFace.js';
20
+
21
+ /**
22
+ * Calculate the default line height given normalized font metrics
23
+ *
24
+ * @remarks
25
+ * This method may be used for both the WebTrFontFace and SdfTrFontFace font types.
26
+ *
27
+ * @param metrics
28
+ * @param fontSize
29
+ * @returns
30
+ */
31
+ export function calcDefaultLineHeight(
32
+ metrics: NormalizedFontMetrics,
33
+ fontSize: number,
34
+ ): number {
35
+ return fontSize * (metrics.ascender - metrics.descender + metrics.lineGap);
36
+ }
@@ -17,6 +17,9 @@
17
17
  * limitations under the License.
18
18
  */
19
19
 
20
+ import type { NormalizedFontMetrics } from './font-face-types/TrFontFace.js';
21
+ import type { WebTrFontFace } from './font-face-types/WebTrFontFace.js';
22
+
20
23
  /**
21
24
  * Returns CSS font setting string for use in canvas context.
22
25
  *
@@ -120,6 +123,77 @@ export function measureText(
120
123
  }, 0);
121
124
  }
122
125
 
126
+ /**
127
+ * Get the font metrics for a font face.
128
+ *
129
+ * @remarks
130
+ * This function will attempt to grab the explicitly defined metrics from the
131
+ * font face first. If the font face does not have metrics defined, it will
132
+ * attempt to calculate the metrics using the browser's measureText method.
133
+ *
134
+ * If the browser does not support the font metrics API, it will use some
135
+ * default values.
136
+ *
137
+ * @param context
138
+ * @param fontFace
139
+ * @param fontSize
140
+ * @returns
141
+ */
142
+ export function getWebFontMetrics(
143
+ context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
144
+ fontFace: WebTrFontFace,
145
+ fontSize: number,
146
+ ): NormalizedFontMetrics {
147
+ if (fontFace.metrics) {
148
+ return fontFace.metrics;
149
+ }
150
+ // If the font face doesn't have metrics defined, we fallback to using the
151
+ // browser's measureText method to calculate take a best guess at the font
152
+ // actual font's metrics.
153
+ // - fontBoundingBox[Ascent|Descent] is the best estimate but only supported
154
+ // in Chrome 87+ (2020), Firefox 116+ (2023), and Safari 11.1+ (2018).
155
+ // - It is an estimate as it can vary between browsers.
156
+ // - actualBoundingBox[Ascent|Descent] is less accurate and supported in
157
+ // Chrome 77+ (2019), Firefox 74+ (2020), and Safari 11.1+ (2018).
158
+ // - If neither are supported, we'll use some default values which will
159
+ // get text on the screen but likely not be great.
160
+ // NOTE: It's been decided not to rely on fontBoundingBox[Ascent|Descent]
161
+ // as it's browser support is limited and it also tends to produce higher than
162
+ // expected values. It is instead HIGHLY RECOMMENDED that developers provide
163
+ // explicit metrics in the font face definition.
164
+ const browserMetrics = context.measureText(
165
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
166
+ );
167
+ console.warn(
168
+ `Font metrics not provided for Canvas Web font ${fontFace.fontFamily}. ` +
169
+ 'Using fallback values. It is HIGHLY recommended you use the latest ' +
170
+ 'version of the Lightning 3 `msdf-generator` tool to extract the default ' +
171
+ 'metrics for the font and provide them in the Canvas Web font definition.',
172
+ );
173
+ let metrics: NormalizedFontMetrics;
174
+ if (
175
+ browserMetrics.actualBoundingBoxDescent &&
176
+ browserMetrics.actualBoundingBoxAscent
177
+ ) {
178
+ metrics = {
179
+ ascender: browserMetrics.actualBoundingBoxAscent / fontSize,
180
+ descender: -browserMetrics.actualBoundingBoxDescent / fontSize,
181
+ lineGap: 0.2,
182
+ };
183
+ } else {
184
+ // If the browser doesn't support the font metrics API, we'll use some
185
+ // default values.
186
+ metrics = {
187
+ ascender: 0.8,
188
+ descender: -0.2,
189
+ lineGap: 0.2,
190
+ };
191
+ }
192
+ // Save the calculated metrics to the font face for future use.
193
+ (fontFace.metrics as NormalizedFontMetrics | null) = metrics;
194
+ return metrics;
195
+ }
196
+
123
197
  export interface WrapTextResult {
124
198
  l: string[];
125
199
  n: number[];
@@ -22,7 +22,12 @@ import { assertTruthy } from '../../../../utils.js';
22
22
  import type { Stage } from '../../../Stage.js';
23
23
  import { WebGlCoreRenderer } from '../../../renderers/webgl/WebGlCoreRenderer.js';
24
24
  import { ImageTexture } from '../../../textures/ImageTexture.js';
25
- import { TrFontFace, type TrFontFaceDescriptors } from '../TrFontFace.js';
25
+ import {
26
+ TrFontFace,
27
+ type NormalizedFontMetrics,
28
+ type TrFontFaceDescriptors,
29
+ type TrFontFaceOptions,
30
+ } from '../TrFontFace.js';
26
31
  import type { FontShaper } from './internal/FontShaper.js';
27
32
  import { SdfFontShaper, type SdfFontData } from './internal/SdfFontShaper.js';
28
33
 
@@ -42,6 +47,12 @@ interface GlyphAtlasEntry {
42
47
  height: number;
43
48
  }
44
49
 
50
+ export interface SdfTrFontFaceOptions extends TrFontFaceOptions {
51
+ atlasUrl: string;
52
+ atlasDataUrl: string;
53
+ stage: Stage;
54
+ }
55
+
45
56
  export class SdfTrFontFace<
46
57
  FontTypeT extends SdfFontType = SdfFontType,
47
58
  > extends TrFontFace {
@@ -49,21 +60,16 @@ export class SdfTrFontFace<
49
60
  public readonly texture: ImageTexture;
50
61
  /**
51
62
  * Height of the tallest character in the font including the whitespace above it
63
+ * in SDF/vertex units.
52
64
  */
53
65
  public readonly maxCharHeight: number = 0;
54
66
  public readonly data: SdfFontData | undefined;
55
67
  public readonly shaper: FontShaper | undefined;
56
68
  public readonly glyphMap: Map<number, SdfFontData['chars'][0]> = new Map();
57
69
 
58
- constructor(
59
- fontFamily: string,
60
- descriptors: Partial<TrFontFaceDescriptors>,
61
- type: FontTypeT,
62
- stage: Stage,
63
- atlasUrl: string,
64
- atlasDataUrl: string,
65
- ) {
66
- super(fontFamily, descriptors);
70
+ constructor(type: FontTypeT, options: SdfTrFontFaceOptions) {
71
+ super(options);
72
+ const { atlasUrl, atlasDataUrl, stage } = options;
67
73
  this.type = type;
68
74
  const renderer = stage.renderer;
69
75
  assertTruthy(
@@ -95,10 +101,11 @@ export class SdfTrFontFace<
95
101
  .then(async (response) => {
96
102
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
97
103
  (this.data as SdfFontData) = await response.json();
104
+ assertTruthy(this.data);
98
105
  // Add all the glyphs to the glyph map
99
106
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
100
107
  let maxCharHeight = 0;
101
- this.data!.chars.forEach((glyph) => {
108
+ this.data.chars.forEach((glyph) => {
102
109
  this.glyphMap.set(glyph.id, glyph);
103
110
  const charHeight = glyph.yoffset + glyph.height;
104
111
  if (charHeight > maxCharHeight) {
@@ -110,9 +117,31 @@ export class SdfTrFontFace<
110
117
  // We know `data` is defined here, because we just set it
111
118
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
112
119
  (this.shaper as FontShaper) = new SdfFontShaper(
113
- this.data!,
120
+ this.data,
114
121
  this.glyphMap,
115
122
  );
123
+
124
+ // If the metrics aren't provided explicitly in the font face options,
125
+ // Gather them from the metrics added by the msdf-generator tool ()
126
+ // If they are missing then we throw an error.
127
+ if (!this.metrics) {
128
+ if (this.data?.lightningMetrics) {
129
+ const { ascender, descender, lineGap, unitsPerEm } =
130
+ this.data.lightningMetrics;
131
+ (this.metrics as NormalizedFontMetrics | null) = {
132
+ ascender: ascender / unitsPerEm,
133
+ descender: descender / unitsPerEm,
134
+ lineGap: lineGap / unitsPerEm,
135
+ };
136
+ } else {
137
+ throw new Error(
138
+ `Font metrics not found in ${this.type} font ${this.fontFamily}. ` +
139
+ 'Make sure you are using the latest version of the Lightning ' +
140
+ '3 `msdf-generator` tool to generate your SDF fonts.',
141
+ );
142
+ }
143
+ }
144
+
116
145
  this.checkLoaded();
117
146
  })
118
147
  .catch(console.error);
@@ -19,6 +19,7 @@
19
19
 
20
20
  import type { PeekableIterator } from '../../../renderers/SdfTextRenderer/internal/PeekableGenerator.js';
21
21
  import { SpecialCodepoints } from '../../../renderers/SdfTextRenderer/internal/SpecialCodepoints.js';
22
+ import type { FontMetrics } from '../../TrFontFace.js';
22
23
  import {
23
24
  FontShaper,
24
25
  type FontShaperProps,
@@ -166,4 +167,5 @@ export interface SdfFontData {
166
167
  fieldType: 'sdf' | 'msdf';
167
168
  distanceRange: number;
168
169
  };
170
+ lightningMetrics?: FontMetrics;
169
171
  }