@flighthq/textshaper 0.5.1-next.973.788d3d7 → 0.6.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 (67) hide show
  1. package/README.md +2 -2
  2. package/dist/.tsbuildinfo.contract +1 -0
  3. package/dist/.tsbuildinfo.runtime +1 -0
  4. package/dist/contract.d.ts +2 -10
  5. package/dist/contract.d.ts.map +1 -1
  6. package/dist/contract.js +2 -10
  7. package/dist/contract.js.map +1 -1
  8. package/dist/contract.textshaper-types.d.ts +2 -0
  9. package/dist/contract.textshaper-types.d.ts.map +1 -0
  10. package/dist/contract.textshaper-types.js +4 -0
  11. package/dist/contract.textshaper-types.js.map +1 -0
  12. package/dist/enableTextShaperGuards.d.ts +2 -1
  13. package/dist/enableTextShaperGuards.d.ts.map +1 -1
  14. package/dist/enableTextShaperGuards.js +6 -7
  15. package/dist/enableTextShaperGuards.js.map +1 -1
  16. package/dist/index.d.ts +10 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +9 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/runtime.d.ts +10 -0
  21. package/dist/runtime.d.ts.map +1 -0
  22. package/dist/runtime.js +9 -0
  23. package/dist/runtime.js.map +1 -0
  24. package/dist/textShaper.d.ts +9 -13
  25. package/dist/textShaper.d.ts.map +1 -1
  26. package/dist/textShaper.js +7 -37
  27. package/dist/textShaper.js.map +1 -1
  28. package/dist/textShaperCache.d.ts +4 -2
  29. package/dist/textShaperCache.d.ts.map +1 -1
  30. package/dist/textShaperCache.js +9 -14
  31. package/dist/textShaperCache.js.map +1 -1
  32. package/dist/textShaperCluster.d.ts +1 -1
  33. package/dist/textShaperCluster.d.ts.map +1 -1
  34. package/dist/textShaperItemize.d.ts +3 -2
  35. package/dist/textShaperItemize.d.ts.map +1 -1
  36. package/dist/textShaperItemize.js +3 -3
  37. package/dist/textShaperItemize.js.map +1 -1
  38. package/dist/textShaperPool.d.ts +1 -1
  39. package/dist/textShaperPool.d.ts.map +1 -1
  40. package/dist/textShaperPool.js +1 -1
  41. package/dist/textShaperPool.js.map +1 -1
  42. package/dist/textShaperRun.d.ts +21 -15
  43. package/dist/textShaperRun.d.ts.map +1 -1
  44. package/dist/textShaperRun.js +32 -42
  45. package/dist/textShaperRun.js.map +1 -1
  46. package/dist/textShaperSignals.d.ts +2 -1
  47. package/dist/textShaperSignals.d.ts.map +1 -1
  48. package/dist/textShaperSignals.js +2 -9
  49. package/dist/textShaperSignals.js.map +1 -1
  50. package/dist/textShaperTypes.d.ts +102 -0
  51. package/dist/textShaperTypes.d.ts.map +1 -0
  52. package/dist/textShaperTypes.js +16 -0
  53. package/dist/textShaperTypes.js.map +1 -0
  54. package/package.json +9 -6
  55. package/src/enableTextShaperGuards.test.ts +19 -15
  56. package/src/textShaper.test.ts +34 -115
  57. package/src/textShaperCache.test.ts +38 -64
  58. package/src/textShaperCluster.test.ts +2 -3
  59. package/src/textShaperItemize.test.ts +15 -27
  60. package/src/textShaperPool.test.ts +1 -1
  61. package/src/textShaperRun.test.ts +51 -113
  62. package/src/textShaperSignals.test.ts +1 -44
  63. package/dist/_textShaperHooks.d.ts +0 -4
  64. package/dist/_textShaperHooks.d.ts.map +0 -1
  65. package/dist/_textShaperHooks.js +0 -5
  66. package/dist/_textShaperHooks.js.map +0 -1
  67. package/src/_textShaperHooks.test.ts +0 -69
@@ -1,6 +1,3 @@
1
- import type { FontMetrics, GlyphExtents, HasTextShaper, ShapedRun, TextShaperBackend } from '@flighthq/types/contract';
2
-
3
- import { setTextShaperBackend } from './textShaper';
4
1
  import {
5
2
  clearShapedRun,
6
3
  createShapedRun,
@@ -16,7 +13,8 @@ import {
16
13
  initializeShapedRun,
17
14
  shapeTextRun,
18
15
  shapeTextRunInto,
19
- } from './textShaperRun';
16
+ } from './textShaperRun.ts';
17
+ import type { FontMetrics, GlyphExtents, HostTextShaperCapability, ShapedRun } from './textShaperTypes.ts';
20
18
 
21
19
  const _testGlyphs = [
22
20
  { cluster: 0, glyphId: 10, xAdvance: 8, xOffset: 0, yAdvance: 0, yOffset: 0 },
@@ -45,9 +43,8 @@ const _testMetrics: FontMetrics = {
45
43
 
46
44
  const _testExtents: GlyphExtents = { height: 10, width: 6, xBearing: 0, yBearing: -8 };
47
45
 
48
- function _makeFullBackend(): TextShaperBackend {
46
+ function _makeFullBackend(): HostTextShaperCapability {
49
47
  return {
50
- // Code point 65 ('A') maps to glyph 10 and back; everything else is unknown.
51
48
  getCodePointForGlyph: (id) => (id === 10 ? 65 : -1),
52
49
  getFontMetrics: () => ({ ..._testMetrics }),
53
50
  getGlyphExtents: (id) => (id === 10 ? { ..._testExtents } : null),
@@ -58,23 +55,25 @@ function _makeFullBackend(): TextShaperBackend {
58
55
  };
59
56
  }
60
57
 
61
- afterEach(() => {
62
- setTextShaperBackend(null);
63
- });
58
+ function _makeAdvancesOnlyBackend(): HostTextShaperCapability {
59
+ return { measureText: (t) => t.length };
60
+ }
61
+
62
+ const _backend = _makeFullBackend();
63
+ const _advancesOnly = _makeAdvancesOnlyBackend();
64
64
 
65
65
  describe('clearShapedRun', () => {
66
66
  it('resets all fields and empties the glyphs array', () => {
67
67
  const run = createShapedRun();
68
68
  (run as { advanceWidth: number }).advanceWidth = 42;
69
69
  (run.glyphs as unknown[]).push(_testGlyphs[0]);
70
- const returned = clearShapedRun(run);
70
+ clearShapedRun(run);
71
71
  expect(run.advanceWidth).toBe(0);
72
72
  expect(run.glyphCount).toBe(0);
73
73
  expect(run.glyphs).toHaveLength(0);
74
74
  expect(run.direction).toBe('LeftToRight');
75
75
  expect(run.script).toBe('');
76
76
  expect(run.font).toBeNull();
77
- expect(returned).toBe(run);
78
77
  });
79
78
 
80
79
  it('retains the same glyphs array reference after clearing', () => {
@@ -102,39 +101,26 @@ describe('createShapedRun', () => {
102
101
  });
103
102
 
104
103
  describe('getCodePointForGlyph', () => {
105
- it('returns -1 when no backend is set', () => {
106
- expect(getCodePointForGlyph(10, {})).toBe(-1);
107
- });
108
-
109
104
  it('returns -1 when the backend does not implement getCodePointForGlyph', () => {
110
- setTextShaperBackend({ measureText: (t) => t.length });
111
- expect(getCodePointForGlyph(10, {})).toBe(-1);
105
+ expect(getCodePointForGlyph(_advancesOnly, 10, {})).toBe(-1);
112
106
  });
113
107
 
114
108
  it('delegates to the backend and returns the resolved code point', () => {
115
- setTextShaperBackend(_makeFullBackend());
116
- expect(getCodePointForGlyph(10, {})).toBe(65);
109
+ expect(getCodePointForGlyph(_backend, 10, {})).toBe(65);
117
110
  });
118
111
 
119
112
  it('returns -1 for glyph ids the backend cannot reverse-map', () => {
120
- setTextShaperBackend(_makeFullBackend());
121
- expect(getCodePointForGlyph(999, {})).toBe(-1);
113
+ expect(getCodePointForGlyph(_backend, 999, {})).toBe(-1);
122
114
  });
123
115
  });
124
116
 
125
117
  describe('getFontMetrics', () => {
126
- it('returns null when no backend is set', () => {
127
- expect(getFontMetrics({})).toBeNull();
128
- });
129
-
130
118
  it('returns null when the backend is advances-only (no getFontMetrics)', () => {
131
- setTextShaperBackend({ measureText: (t) => t.length });
132
- expect(getFontMetrics({})).toBeNull();
119
+ expect(getFontMetrics(_advancesOnly, {})).toBeNull();
133
120
  });
134
121
 
135
122
  it('delegates to the backend', () => {
136
- setTextShaperBackend(_makeFullBackend());
137
- const m = getFontMetrics({ size: 16 });
123
+ const m = getFontMetrics(_backend, { size: 16 });
138
124
  expect(m).not.toBeNull();
139
125
  expect(m!.ascent).toBe(10);
140
126
  expect(m!.unitsPerEm).toBe(1000);
@@ -142,106 +128,85 @@ describe('getFontMetrics', () => {
142
128
  });
143
129
 
144
130
  describe('getFontMetricsInto', () => {
145
- it('returns false and does not modify out when no backend is set', () => {
131
+ it('returns false and does not modify out when the backend has no getFontMetrics', () => {
146
132
  const out: FontMetrics = { ..._testMetrics, ascent: 99 };
147
- expect(getFontMetricsInto({}, out)).toBe(false);
133
+ expect(getFontMetricsInto(_advancesOnly, {}, out)).toBe(false);
148
134
  expect(out.ascent).toBe(99);
149
135
  });
150
136
 
151
137
  it('writes all fields into out and returns true on success', () => {
152
- setTextShaperBackend(_makeFullBackend());
153
138
  const out = { ..._testMetrics };
154
- expect(getFontMetricsInto({}, out)).toBe(true);
139
+ expect(getFontMetricsInto(_backend, {}, out)).toBe(true);
155
140
  expect(out.ascent).toBe(_testMetrics.ascent);
156
141
  expect(out.capHeight).toBe(_testMetrics.capHeight);
157
142
  });
158
143
  });
159
144
 
160
145
  describe('getFontUnitScale', () => {
161
- it('returns -1 when no backend is set', () => {
162
- expect(getFontUnitScale({})).toBe(-1);
146
+ it('returns -1 when the backend has no getFontMetrics', () => {
147
+ expect(getFontUnitScale(_advancesOnly, {})).toBe(-1);
163
148
  });
164
149
 
165
150
  it('returns size / unitsPerEm', () => {
166
- setTextShaperBackend(_makeFullBackend());
167
- // default size is 12 per the function; unitsPerEm is 1000.
168
- expect(getFontUnitScale({})).toBeCloseTo(12 / 1000);
169
- expect(getFontUnitScale({ size: 20 })).toBeCloseTo(20 / 1000);
151
+ expect(getFontUnitScale(_backend, {})).toBeCloseTo(12 / 1000);
152
+ expect(getFontUnitScale(_backend, { size: 20 })).toBeCloseTo(20 / 1000);
170
153
  });
171
154
  });
172
155
 
173
156
  describe('getGlyphExtents', () => {
174
- it('returns null when no backend is set', () => {
175
- expect(getGlyphExtents(10, {})).toBeNull();
176
- });
177
-
178
157
  it('returns null when the backend is advances-only', () => {
179
- setTextShaperBackend({ measureText: (t) => t.length });
180
- expect(getGlyphExtents(10, {})).toBeNull();
158
+ expect(getGlyphExtents(_advancesOnly, 10, {})).toBeNull();
181
159
  });
182
160
 
183
161
  it('delegates to the backend', () => {
184
- setTextShaperBackend(_makeFullBackend());
185
- const e = getGlyphExtents(10, {});
162
+ const e = getGlyphExtents(_backend, 10, {});
186
163
  expect(e).not.toBeNull();
187
164
  expect(e!.width).toBe(6);
188
165
  });
189
166
 
190
167
  it('returns null for unknown glyph ids', () => {
191
- setTextShaperBackend(_makeFullBackend());
192
- expect(getGlyphExtents(999, {})).toBeNull();
168
+ expect(getGlyphExtents(_backend, 999, {})).toBeNull();
193
169
  });
194
170
  });
195
171
 
196
172
  describe('getGlyphExtentsBatch', () => {
197
- it('returns 0 and writes nothing when no backend is set', () => {
198
- const out: GlyphExtents[] = [];
199
- expect(getGlyphExtentsBatch([10, 20], {}, out)).toBe(0);
200
- });
201
-
202
173
  it('returns 0 when the backend is advances-only', () => {
203
- setTextShaperBackend({ measureText: (t) => t.length });
204
174
  const out: GlyphExtents[] = [];
205
- expect(getGlyphExtentsBatch([10], {}, out)).toBe(0);
175
+ expect(getGlyphExtentsBatch(_advancesOnly, [10], {}, out)).toBe(0);
206
176
  });
207
177
 
208
178
  it('resolves known glyphs and counts only those that resolved', () => {
209
- setTextShaperBackend(_makeFullBackend());
210
179
  const out: GlyphExtents[] = [];
211
- // 10 resolves; 999 is unknown.
212
- const resolved = getGlyphExtentsBatch([10, 999], {}, out);
180
+ const resolved = getGlyphExtentsBatch(_backend, [10, 999], {}, out);
213
181
  expect(resolved).toBe(1);
214
182
  expect(out).toHaveLength(2);
215
183
  expect(out[0].width).toBe(_testExtents.width);
216
184
  });
217
185
 
218
186
  it('writes zeroed extents for unknown glyphs so out is fully populated', () => {
219
- setTextShaperBackend(_makeFullBackend());
220
187
  const out: GlyphExtents[] = [];
221
- getGlyphExtentsBatch([999, 10], {}, out);
188
+ getGlyphExtentsBatch(_backend, [999, 10], {}, out);
222
189
  expect(out[0]).toEqual({ height: 0, width: 0, xBearing: 0, yBearing: 0 });
223
190
  expect(out[1].height).toBe(_testExtents.height);
224
191
  });
225
192
 
226
193
  it('returns 0 for an empty glyph id list without touching the backend', () => {
227
- setTextShaperBackend(_makeFullBackend());
228
194
  const out: GlyphExtents[] = [];
229
- expect(getGlyphExtentsBatch([], {}, out)).toBe(0);
195
+ expect(getGlyphExtentsBatch(_backend, [], {}, out)).toBe(0);
230
196
  expect(out).toHaveLength(0);
231
197
  });
232
198
  });
233
199
 
234
200
  describe('getGlyphExtentsInto', () => {
235
- it('returns false and does not modify out when no backend is set', () => {
201
+ it('returns false and does not modify out when the backend has no getGlyphExtents', () => {
236
202
  const out: GlyphExtents = { height: 1, width: 1, xBearing: 1, yBearing: 1 };
237
- expect(getGlyphExtentsInto(10, {}, out)).toBe(false);
203
+ expect(getGlyphExtentsInto(_advancesOnly, 10, {}, out)).toBe(false);
238
204
  expect(out.width).toBe(1);
239
205
  });
240
206
 
241
207
  it('writes all fields into out and returns true on success', () => {
242
- setTextShaperBackend(_makeFullBackend());
243
208
  const out: GlyphExtents = { height: 0, width: 0, xBearing: 0, yBearing: 0 };
244
- expect(getGlyphExtentsInto(10, {}, out)).toBe(true);
209
+ expect(getGlyphExtentsInto(_backend, 10, {}, out)).toBe(true);
245
210
  expect(out.width).toBe(_testExtents.width);
246
211
  expect(out.height).toBe(_testExtents.height);
247
212
  expect(out.yBearing).toBe(_testExtents.yBearing);
@@ -249,44 +214,30 @@ describe('getGlyphExtentsInto', () => {
249
214
  });
250
215
 
251
216
  describe('getGlyphIndexForCodePoint', () => {
252
- it('returns -1 when no backend is set', () => {
253
- expect(getGlyphIndexForCodePoint(65, {})).toBe(-1);
254
- });
255
-
256
217
  it('returns -1 when the backend does not implement getGlyphIndexForCodePoint', () => {
257
- setTextShaperBackend({ measureText: (t) => t.length });
258
- expect(getGlyphIndexForCodePoint(65, {})).toBe(-1);
218
+ expect(getGlyphIndexForCodePoint(_advancesOnly, 65, {})).toBe(-1);
259
219
  });
260
220
 
261
221
  it('delegates to the backend and returns the glyph id', () => {
262
- setTextShaperBackend(_makeFullBackend());
263
- expect(getGlyphIndexForCodePoint(65, {})).toBe(10);
222
+ expect(getGlyphIndexForCodePoint(_backend, 65, {})).toBe(10);
264
223
  });
265
224
 
266
225
  it('returns -1 for code points with no glyph in the font', () => {
267
- setTextShaperBackend(_makeFullBackend());
268
- expect(getGlyphIndexForCodePoint(0x2603, {})).toBe(-1);
226
+ expect(getGlyphIndexForCodePoint(_backend, 0x2603, {})).toBe(-1);
269
227
  });
270
228
  });
271
229
 
272
230
  describe('getGlyphName', () => {
273
- it('returns an empty string when no backend is set', () => {
274
- expect(getGlyphName(10, {})).toBe('');
275
- });
276
-
277
231
  it('returns an empty string when the backend does not implement getGlyphName', () => {
278
- setTextShaperBackend({ measureText: (t) => t.length });
279
- expect(getGlyphName(10, {})).toBe('');
232
+ expect(getGlyphName(_advancesOnly, 10, {})).toBe('');
280
233
  });
281
234
 
282
235
  it('delegates to the backend and returns the PostScript glyph name', () => {
283
- setTextShaperBackend(_makeFullBackend());
284
- expect(getGlyphName(10, {})).toBe('A');
236
+ expect(getGlyphName(_backend, 10, {})).toBe('A');
285
237
  });
286
238
 
287
239
  it('returns an empty string for glyph ids the backend cannot name', () => {
288
- setTextShaperBackend(_makeFullBackend());
289
- expect(getGlyphName(999, {})).toBe('');
240
+ expect(getGlyphName(_backend, 999, {})).toBe('');
290
241
  });
291
242
  });
292
243
 
@@ -297,24 +248,12 @@ describe('initializeShapedRun', () => {
297
248
  });
298
249
 
299
250
  describe('shapeTextRun', () => {
300
- it('uses the explicitly supplied host instead of the legacy installed backend', () => {
301
- setTextShaperBackend({ measureText: () => 0 });
302
- const host: HasTextShaper = { text: { shaper: _makeFullBackend() } };
303
- expect(shapeTextRun('ab', {}, undefined, host)?.glyphCount).toBe(2);
304
- });
305
-
306
- it('returns null when no backend is set', () => {
307
- expect(shapeTextRun('hi', {})).toBeNull();
308
- });
309
-
310
251
  it('returns null when the backend is advances-only (no shapeRun)', () => {
311
- setTextShaperBackend({ measureText: (t) => t.length });
312
- expect(shapeTextRun('hi', {})).toBeNull();
252
+ expect(shapeTextRun(_advancesOnly, 'hi', {})).toBeNull();
313
253
  });
314
254
 
315
255
  it('delegates to backend.shapeRun', () => {
316
- setTextShaperBackend(_makeFullBackend());
317
- const run = shapeTextRun('ab', {});
256
+ const run = shapeTextRun(_backend, 'ab', {});
318
257
  expect(run).not.toBeNull();
319
258
  expect(run!.glyphCount).toBe(2);
320
259
  expect(run!.direction).toBe('LeftToRight');
@@ -322,28 +261,28 @@ describe('shapeTextRun', () => {
322
261
 
323
262
  it('passes options to the backend', () => {
324
263
  let capturedOptions: unknown;
325
- setTextShaperBackend({
264
+ const backend: HostTextShaperCapability = {
326
265
  measureText: () => 0,
327
266
  shapeRun: (_t, _f, opts) => {
328
267
  capturedOptions = opts;
329
268
  return { ..._testRun, glyphs: [] };
330
269
  },
331
- });
332
- shapeTextRun('x', {}, { direction: 'RightToLeft', script: 'Arab' });
270
+ };
271
+ shapeTextRun(backend, 'x', {}, { direction: 'RightToLeft', script: 'Arab' });
333
272
  expect(capturedOptions).toMatchObject({ direction: 'RightToLeft', script: 'Arab' });
334
273
  });
335
274
  });
275
+
336
276
  describe('shapeTextRunInto', () => {
337
- it('returns false and does not modify out when no backend is set', () => {
277
+ it('returns false and does not modify out when the backend has no shapeRun', () => {
338
278
  const out = createShapedRun();
339
- expect(shapeTextRunInto('hi', {}, out)).toBe(false);
279
+ expect(shapeTextRunInto(_advancesOnly, 'hi', {}, out)).toBe(false);
340
280
  expect(out.glyphCount).toBe(0);
341
281
  });
342
282
 
343
283
  it('writes run fields and glyphs into out, returns true', () => {
344
- setTextShaperBackend(_makeFullBackend());
345
284
  const out = createShapedRun();
346
- expect(shapeTextRunInto('ab', {}, out)).toBe(true);
285
+ expect(shapeTextRunInto(_backend, 'ab', {}, out)).toBe(true);
347
286
  expect(out.advanceWidth).toBe(15);
348
287
  expect(out.glyphCount).toBe(2);
349
288
  expect(out.glyphs).toHaveLength(2);
@@ -353,23 +292,22 @@ describe('shapeTextRunInto', () => {
353
292
 
354
293
  it('forwards options to the backend', () => {
355
294
  let capturedOptions: unknown;
356
- setTextShaperBackend({
295
+ const backend: HostTextShaperCapability = {
357
296
  measureText: () => 0,
358
297
  shapeRun: (_t, _f, opts) => {
359
298
  capturedOptions = opts;
360
299
  return { ..._testRun, glyphs: [..._testGlyphs] };
361
300
  },
362
- });
301
+ };
363
302
  const out = createShapedRun();
364
- shapeTextRunInto('x', {}, out, { direction: 'RightToLeft', script: 'Arab' });
303
+ shapeTextRunInto(backend, 'x', {}, out, { direction: 'RightToLeft', script: 'Arab' });
365
304
  expect(capturedOptions).toMatchObject({ direction: 'RightToLeft', script: 'Arab' });
366
305
  });
367
306
 
368
307
  it('retains the existing glyphs array reference', () => {
369
- setTextShaperBackend(_makeFullBackend());
370
308
  const out = createShapedRun();
371
309
  const originalGlyphs = out.glyphs;
372
- shapeTextRunInto('ab', {}, out);
310
+ shapeTextRunInto(_backend, 'ab', {}, out);
373
311
  expect(out.glyphs).toBe(originalGlyphs);
374
312
  });
375
313
  });
@@ -1,35 +1,18 @@
1
- import type { TextShaperBackend } from '@flighthq/types/contract';
2
-
3
- import { setTextShaperBackend } from './textShaper';
4
1
  import {
5
2
  disposeTextShaperSignals,
6
3
  enableTextShaperSignals,
7
4
  getTextShaperSignals,
8
5
  initializeTextShaperSignals,
9
- } from './textShaperSignals';
10
-
11
- const _stubBackend: TextShaperBackend = { measureText: () => 0 };
12
- const _stubBackend2: TextShaperBackend = { measureText: () => 1 };
6
+ } from './textShaperSignals.ts';
13
7
 
14
8
  afterEach(() => {
15
9
  disposeTextShaperSignals();
16
- setTextShaperBackend(null);
17
10
  });
18
11
 
19
12
  describe('disposeTextShaperSignals', () => {
20
13
  it('is a no-op when signals have not been enabled', () => {
21
14
  expect(() => disposeTextShaperSignals()).not.toThrow();
22
15
  });
23
- it('clears all listeners after dispose', () => {
24
- const sigs = enableTextShaperSignals();
25
- let fired = false;
26
- sigs.onBackendChanged.emit = () => {
27
- fired = true;
28
- };
29
- disposeTextShaperSignals();
30
- setTextShaperBackend(_stubBackend);
31
- expect(fired).toBe(false);
32
- });
33
16
  it('getTextShaperSignals returns null after dispose', () => {
34
17
  enableTextShaperSignals();
35
18
  disposeTextShaperSignals();
@@ -66,29 +49,3 @@ describe('initializeTextShaperSignals', () => {
66
49
  expect(typeof initializeTextShaperSignals).toBe('function');
67
50
  });
68
51
  });
69
- describe('setTextShaperBackend', () => {
70
- it('emits onBackendChanged with the new backend when signals are enabled', () => {
71
- const sigs = enableTextShaperSignals();
72
- const received: (TextShaperBackend | null)[] = [];
73
- sigs.onBackendChanged.emit = (b) => received.push(b);
74
- setTextShaperBackend(_stubBackend);
75
- expect(received).toEqual([_stubBackend]);
76
- });
77
- it('emits onBackendChanged with null when cleared', () => {
78
- const sigs = enableTextShaperSignals();
79
- const received: (TextShaperBackend | null)[] = [];
80
- sigs.onBackendChanged.emit = (b) => received.push(b);
81
- setTextShaperBackend(_stubBackend);
82
- setTextShaperBackend(null);
83
- expect(received).toEqual([_stubBackend, null]);
84
- });
85
- it('does not emit when signals are not enabled', () => {
86
- // Verifies setTextShaperBackend does not throw when no signals enabled.
87
- expect(() => setTextShaperBackend(_stubBackend2)).not.toThrow();
88
- });
89
- it('installs the backend', () => {
90
- enableTextShaperSignals();
91
- setTextShaperBackend(_stubBackend);
92
- expect(_stubBackend).toBeDefined();
93
- });
94
- });
@@ -1,4 +0,0 @@
1
- import type { TextShaperBackend } from '@flighthq/types/contract';
2
- export declare let _textShaperBackendHook: ((backend: TextShaperBackend | null) => void) | null;
3
- export declare function _setTextShaperBackendHook(hook: ((backend: TextShaperBackend | null) => void) | null): void;
4
- //# sourceMappingURL=_textShaperHooks.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_textShaperHooks.d.ts","sourceRoot":"","sources":["../src/_textShaperHooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE,eAAO,IAAI,sBAAsB,EAAE,CAAC,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI,KAAK,IAAI,CAAC,GAAG,IAAW,CAAC;AAE/F,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI,KAAK,IAAI,CAAC,GAAG,IAAI,GAAG,IAAI,CAE1G"}
@@ -1,5 +0,0 @@
1
- export let _textShaperBackendHook = null;
2
- export function _setTextShaperBackendHook(hook) {
3
- _textShaperBackendHook = hook;
4
- }
5
- //# sourceMappingURL=_textShaperHooks.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_textShaperHooks.js","sourceRoot":"","sources":["../src/_textShaperHooks.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,IAAI,sBAAsB,GAAyD,IAAI,CAAC;AAE/F,MAAM,UAAU,yBAAyB,CAAC,IAA0D;IAClG,sBAAsB,GAAG,IAAI,CAAC;AAChC,CAAC"}
@@ -1,69 +0,0 @@
1
- import type { TextShaperBackend } from '@flighthq/types/contract';
2
-
3
- import { _setTextShaperBackendHook, _textShaperBackendHook } from './_textShaperHooks';
4
- import { setTextShaperBackend } from './textShaper';
5
-
6
- const _testBackend: TextShaperBackend = {
7
- measureText: (text) => text.length,
8
- };
9
-
10
- afterEach(() => {
11
- // Restore module state so an installed hook does not leak across tests.
12
- _setTextShaperBackendHook(null);
13
- setTextShaperBackend(null);
14
- });
15
-
16
- describe('_setTextShaperBackendHook', () => {
17
- it('installs the hook so setTextShaperBackend invokes it with the new backend', () => {
18
- let received: TextShaperBackend | null = _testBackend;
19
- let calls = 0;
20
- _setTextShaperBackendHook((backend) => {
21
- received = backend;
22
- calls++;
23
- });
24
- setTextShaperBackend(_testBackend);
25
- expect(calls).toBe(1);
26
- expect(received).toBe(_testBackend);
27
- });
28
-
29
- it('passes null through to the hook when the backend is cleared', () => {
30
- let received: TextShaperBackend | null = _testBackend;
31
- _setTextShaperBackendHook((backend) => {
32
- received = backend;
33
- });
34
- setTextShaperBackend(null);
35
- expect(received).toBeNull();
36
- });
37
-
38
- it('exposes the installed hook on the _textShaperBackendHook slot', () => {
39
- expect(_textShaperBackendHook).toBeNull();
40
- const hook = (): void => {};
41
- _setTextShaperBackendHook(hook);
42
- expect(_textShaperBackendHook).toBe(hook);
43
- });
44
-
45
- it('clears the hook when passed null so setTextShaperBackend no longer dispatches', () => {
46
- let calls = 0;
47
- _setTextShaperBackendHook(() => {
48
- calls++;
49
- });
50
- _setTextShaperBackendHook(null);
51
- expect(_textShaperBackendHook).toBeNull();
52
- setTextShaperBackend(_testBackend);
53
- expect(calls).toBe(0);
54
- });
55
-
56
- it('replaces a previously installed hook (last write wins)', () => {
57
- let firstCalls = 0;
58
- let secondCalls = 0;
59
- _setTextShaperBackendHook(() => {
60
- firstCalls++;
61
- });
62
- _setTextShaperBackendHook(() => {
63
- secondCalls++;
64
- });
65
- setTextShaperBackend(_testBackend);
66
- expect(firstCalls).toBe(0);
67
- expect(secondCalls).toBe(1);
68
- });
69
- });