@flighthq/textshaper 0.5.1-next.920.da8f530 → 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 (69) 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 +24 -17
  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/textShaperCluster.js +7 -7
  35. package/dist/textShaperCluster.js.map +1 -1
  36. package/dist/textShaperItemize.d.ts +3 -2
  37. package/dist/textShaperItemize.d.ts.map +1 -1
  38. package/dist/textShaperItemize.js +3 -3
  39. package/dist/textShaperItemize.js.map +1 -1
  40. package/dist/textShaperPool.d.ts +1 -1
  41. package/dist/textShaperPool.d.ts.map +1 -1
  42. package/dist/textShaperPool.js +1 -1
  43. package/dist/textShaperPool.js.map +1 -1
  44. package/dist/textShaperRun.d.ts +21 -15
  45. package/dist/textShaperRun.d.ts.map +1 -1
  46. package/dist/textShaperRun.js +32 -42
  47. package/dist/textShaperRun.js.map +1 -1
  48. package/dist/textShaperSignals.d.ts +2 -1
  49. package/dist/textShaperSignals.d.ts.map +1 -1
  50. package/dist/textShaperSignals.js +2 -9
  51. package/dist/textShaperSignals.js.map +1 -1
  52. package/dist/textShaperTypes.d.ts +102 -0
  53. package/dist/textShaperTypes.d.ts.map +1 -0
  54. package/dist/textShaperTypes.js +16 -0
  55. package/dist/textShaperTypes.js.map +1 -0
  56. package/package.json +9 -6
  57. package/src/enableTextShaperGuards.test.ts +19 -15
  58. package/src/textShaper.test.ts +34 -115
  59. package/src/textShaperCache.test.ts +52 -76
  60. package/src/textShaperCluster.test.ts +12 -5
  61. package/src/textShaperItemize.test.ts +15 -27
  62. package/src/textShaperPool.test.ts +1 -1
  63. package/src/textShaperRun.test.ts +51 -113
  64. package/src/textShaperSignals.test.ts +1 -44
  65. package/dist/_textShaperHooks.d.ts +0 -4
  66. package/dist/_textShaperHooks.d.ts.map +0 -1
  67. package/dist/_textShaperHooks.js +0 -5
  68. package/dist/_textShaperHooks.js.map +0 -1
  69. package/src/_textShaperHooks.test.ts +0 -69
@@ -1,25 +1,29 @@
1
- import { addLogSink, createMemoryLogSink, getMemoryLogSinkEntries, removeLogSink } from '@flighthq/log/contract';
2
- import type { LogEntry } from '@flighthq/types/contract';
3
- import { describe, expect, it } from 'vitest';
1
+ import type { LogEntry, LogState } from '@flighthq/log/contract';
2
+ import { addLogSink, createLogState, createMemoryLogSink, getMemoryLogSinkEntries } from '@flighthq/log/contract';
3
+ import { beforeEach, describe, expect, it } from 'vitest';
4
4
 
5
- import { disableTextShaperGuards, enableTextShaperGuards } from './enableTextShaperGuards';
6
- import { acquireShapedRun, releaseShapedRun } from './textShaperPool';
5
+ import { disableTextShaperGuards, enableTextShaperGuards } from './enableTextShaperGuards.ts';
6
+ import { acquireShapedRun, releaseShapedRun } from './textShaperPool.ts';
7
+
8
+ let logState: LogState;
9
+
10
+ beforeEach(() => {
11
+ logState = createLogState();
12
+ });
7
13
 
8
14
  function captureLog(run: () => void): readonly LogEntry[] {
9
15
  const sink = createMemoryLogSink(8);
10
- addLogSink(sink.sink);
11
- try {
12
- run();
13
- return getMemoryLogSinkEntries(sink);
14
- } finally {
15
- removeLogSink(sink.sink);
16
- }
16
+ addLogSink(logState, sink.sink);
17
+ run();
18
+ const entries: LogEntry[] = [];
19
+ getMemoryLogSinkEntries(entries, sink);
20
+ return entries;
17
21
  }
18
22
 
19
23
  describe('disableTextShaperGuards', () => {
20
24
  it('stops repeated releases from reporting', () => {
21
25
  const entries = captureLog(() => {
22
- enableTextShaperGuards();
26
+ enableTextShaperGuards(logState, () => 0);
23
27
  disableTextShaperGuards();
24
28
  const run = acquireShapedRun();
25
29
  releaseShapedRun(run);
@@ -32,7 +36,7 @@ describe('disableTextShaperGuards', () => {
32
36
  describe('enableTextShaperGuards', () => {
33
37
  it('warns when one acquired run is released twice', () => {
34
38
  const entries = captureLog(() => {
35
- enableTextShaperGuards();
39
+ enableTextShaperGuards(logState, () => 0);
36
40
  try {
37
41
  const run = acquireShapedRun();
38
42
  releaseShapedRun(run);
@@ -48,7 +52,7 @@ describe('enableTextShaperGuards', () => {
48
52
 
49
53
  it('stays silent for correctly paired acquire and release calls', () => {
50
54
  const entries = captureLog(() => {
51
- enableTextShaperGuards();
55
+ enableTextShaperGuards(logState, () => 0);
52
56
  try {
53
57
  const run = acquireShapedRun();
54
58
  releaseShapedRun(run);
@@ -1,83 +1,54 @@
1
- import type { HasTextShaper, TextFormat, TextShaperBackend, TextShaperOperation } from '@flighthq/types/contract';
2
-
3
- import {
4
- explainTextShaperOperation,
5
- getTextShaperBackend,
6
- hasTextShaperOperation,
7
- measureText,
8
- setTextShaperBackend,
9
- } from './textShaper';
10
-
11
- afterEach(() => {
12
- setTextShaperBackend(null);
13
- });
1
+ import { explainTextShaperOperation, hasTextShaperOperation, measureText } from './textShaper.ts';
2
+ import type { HostTextShaperCapability, TextShaperOperation } from './textShaperTypes.ts';
14
3
 
15
4
  describe('explainTextShaperOperation', () => {
16
- afterEach(() => {
17
- setTextShaperBackend(null);
18
- });
19
-
20
- // ★ With nothing installed, the getter returns null,
21
- // so a query resolving through the getter would report every operation implemented. It must not.
22
- it('reports none and no implementation when nothing is installed', () => {
23
- setTextShaperBackend(null);
24
- for (const operation of OPTIONAL_OPERATIONS) {
25
- expect(explainTextShaperOperation(operation)).toEqual({ implemented: false, layer: 'none', operation });
26
- }
27
- });
28
-
29
- it('reports a custom backend as implementing only what it provides', () => {
30
- setTextShaperBackend(partialBackend());
31
- for (const operation of OPTIONAL_OPERATIONS) {
32
- expect(hasTextShaperOperation(operation)).toBe(false);
33
- }
34
- expect(explainTextShaperOperation(OPTIONAL_OPERATIONS[0]).layer).toBe('none');
35
- });
36
-
37
- it('reports an operation the backend does provide', () => {
38
- const operation = OPTIONAL_OPERATIONS[0];
39
- setTextShaperBackend({ ...partialBackend(), [operation]: () => undefined } as TextShaperBackend);
40
- expect(explainTextShaperOperation(operation)).toEqual({ implemented: true, layer: 'custom', operation });
41
- });
42
-
43
- it('reports an explicitly supplied provider as the host layer', () => {
5
+ it('reports an operation the backend provides', () => {
44
6
  const operation = OPTIONAL_OPERATIONS[0];
45
- const backend = { ...partialBackend(), [operation]: () => undefined } as TextShaperBackend;
46
- setTextShaperBackend(partialBackend());
47
- expect(explainTextShaperOperation(operation, shaperHost(backend))).toEqual({
7
+ const backend = { ...partialBackend(), [operation]: () => undefined } as HostTextShaperCapability;
8
+ expect(explainTextShaperOperation(backend, operation)).toEqual({
48
9
  implemented: true,
49
10
  layer: 'host',
50
11
  operation,
51
12
  });
52
13
  });
53
- });
54
-
55
- describe('getTextShaperBackend', () => {
56
- it('returns an explicit host provider ahead of the legacy backend', () => {
57
- const legacy: TextShaperBackend = { measureText: () => 1 };
58
- const explicit: TextShaperBackend = { measureText: () => 2 };
59
- setTextShaperBackend(legacy);
60
- expect(getTextShaperBackend(shaperHost(explicit))).toBe(explicit);
61
- });
62
14
 
63
- it('returns null before a backend is set', () => {
64
- expect(getTextShaperBackend()).toBeNull();
15
+ it('reports none when the backend does not provide the operation', () => {
16
+ for (const operation of OPTIONAL_OPERATIONS) {
17
+ expect(explainTextShaperOperation(partialBackend(), operation)).toEqual({
18
+ implemented: false,
19
+ layer: 'none',
20
+ operation,
21
+ });
22
+ }
65
23
  });
66
24
  });
67
25
 
68
26
  describe('hasTextShaperOperation', () => {
69
- afterEach(() => {
70
- setTextShaperBackend(null);
71
- });
72
-
73
27
  it('agrees with explainTextShaperOperation for every optional operation', () => {
74
- setTextShaperBackend(partialBackend());
28
+ const backend = partialBackend();
75
29
  for (const operation of OPTIONAL_OPERATIONS) {
76
- expect(hasTextShaperOperation(operation)).toBe(explainTextShaperOperation(operation).implemented);
30
+ expect(hasTextShaperOperation(backend, operation)).toBe(
31
+ explainTextShaperOperation(backend, operation).implemented,
32
+ );
77
33
  }
78
34
  });
79
35
  });
80
36
 
37
+ describe('measureText', () => {
38
+ it('delegates to the backend', () => {
39
+ const backend: HostTextShaperCapability = { measureText: (text) => text.length * 7 };
40
+ expect(measureText(backend, 'abc', {})).toBe(21);
41
+ });
42
+
43
+ it('isolates callers that interleave different backends', () => {
44
+ const first: HostTextShaperCapability = { measureText: () => 1 };
45
+ const second: HostTextShaperCapability = { measureText: () => 2 };
46
+ expect(measureText(first, 'x', {})).toBe(1);
47
+ expect(measureText(second, 'x', {})).toBe(2);
48
+ expect(measureText(first, 'x', {})).toBe(1);
49
+ });
50
+ });
51
+
81
52
  // Per-operation availability for TextShaperBackend. The operations below are the ones the interface declares
82
53
  // OPTIONAL, so a host that omits them is compliant rather than broken — that is the absence-of-an-export
83
54
  // ruling, and this is the query that makes it observable.
@@ -90,61 +61,9 @@ const OPTIONAL_OPERATIONS: readonly TextShaperOperation[] = [
90
61
  'shapeRun',
91
62
  ];
92
63
 
93
- describe('measureText', () => {
94
- it('accepts an optional explicit shaper host without breaking the existing signature', () => {
95
- expectTypeOf(measureText).toEqualTypeOf<
96
- (text: string, format: Readonly<TextFormat>, host?: HasTextShaper) => number
97
- >();
98
- });
99
-
100
- it('gives an explicit host precedence over the legacy installed backend', () => {
101
- setTextShaperBackend({ measureText: () => 99 });
102
- expect(measureText('abc', {}, shaperHost({ measureText: (text) => text.length }))).toBe(3);
103
- });
104
-
105
- it('isolates callers that interleave different explicit hosts', () => {
106
- const first = shaperHost({ measureText: () => 1 });
107
- const second = shaperHost({ measureText: () => 2 });
108
- expect(measureText('x', {}, first)).toBe(1);
109
- expect(measureText('x', {}, second)).toBe(2);
110
- expect(measureText('x', {}, first)).toBe(1);
111
- });
112
-
113
- it('returns -1 when no backend is registered', () => {
114
- expect(measureText('hello', {})).toBe(-1);
115
- });
116
-
117
- it('delegates to the active backend', () => {
118
- setTextShaperBackend({ measureText: (text) => text.length * 7 });
119
- expect(measureText('abc', {})).toBe(21);
120
- });
121
- });
122
-
123
- describe('setTextShaperBackend', () => {
124
- it('stores the backend and clears it with null', () => {
125
- const backend: TextShaperBackend = { measureText: (text) => text.length };
126
- setTextShaperBackend(backend);
127
- expect(getTextShaperBackend()).toBe(backend);
128
- setTextShaperBackend(null);
129
- expect(getTextShaperBackend()).toBeNull();
130
- });
131
-
132
- it('replaces an existing backend (last write wins, no throw)', () => {
133
- const first: TextShaperBackend = { measureText: () => 1 };
134
- const second: TextShaperBackend = { measureText: () => 2 };
135
- setTextShaperBackend(first);
136
- setTextShaperBackend(second);
137
- expect(getTextShaperBackend()).toBe(second);
138
- });
139
- });
140
-
141
64
  // A host implementing only the REQUIRED members — partial support declared by absence.
142
- function partialBackend(): TextShaperBackend {
65
+ function partialBackend(): HostTextShaperCapability {
143
66
  return {
144
67
  measureText: (() => undefined) as never,
145
- } as TextShaperBackend;
146
- }
147
-
148
- function shaperHost(shaper: TextShaperBackend): HasTextShaper {
149
- return { text: { shaper } };
68
+ } as HostTextShaperCapability;
150
69
  }
@@ -1,13 +1,11 @@
1
- import type { HasTextShaper, ShapedRun, TextShaperBackend } from '@flighthq/types/contract';
2
-
3
- import { setTextShaperBackend } from './textShaper';
4
1
  import {
5
2
  clearTextShaperCache,
6
3
  createTextShaperCache,
7
4
  disposeTextShaperCache,
8
5
  initializeTextShaperCache,
9
6
  shapeTextRunCached,
10
- } from './textShaperCache';
7
+ } from './textShaperCache.ts';
8
+ import type { HostTextShaperCapability, ShapedRun } from './textShaperTypes.ts';
11
9
 
12
10
  const _stubRun: ShapedRun = {
13
11
  advanceWidth: 15,
@@ -18,7 +16,7 @@ const _stubRun: ShapedRun = {
18
16
  script: 'Latn',
19
17
  };
20
18
 
21
- function _makeCountingBackend(): { backend: TextShaperBackend; readonly calls: number } {
19
+ function _makeCountingBackend(): { backend: HostTextShaperCapability; readonly calls: number } {
22
20
  let calls = 0;
23
21
  return {
24
22
  backend: {
@@ -34,51 +32,49 @@ function _makeCountingBackend(): { backend: TextShaperBackend; readonly calls: n
34
32
  };
35
33
  }
36
34
 
37
- afterEach(() => {
38
- setTextShaperBackend(null);
39
- });
35
+ const _advancesOnly: HostTextShaperCapability = { measureText: (t) => t.length };
40
36
 
41
37
  describe('clearTextShaperCache', () => {
42
38
  it('removes all cached entries', () => {
43
- const { backend } = _makeCountingBackend();
44
- setTextShaperBackend(backend);
39
+ const tracker = _makeCountingBackend();
45
40
  const cache = createTextShaperCache();
46
- shapeTextRunCached(cache, 'hi', {});
47
- expect(cache._entries.size).toBe(1);
41
+ shapeTextRunCached(tracker.backend, cache, 'hi', {});
42
+ shapeTextRunCached(tracker.backend, cache, 'hi', {});
43
+ expect(tracker.calls).toBe(1);
48
44
  clearTextShaperCache(cache);
49
- expect(cache._entries.size).toBe(0);
45
+ shapeTextRunCached(tracker.backend, cache, 'hi', {});
46
+ expect(tracker.calls).toBe(2);
50
47
  });
51
48
 
52
49
  it('cache remains usable after clearing', () => {
53
50
  const { backend } = _makeCountingBackend();
54
- setTextShaperBackend(backend);
55
51
  const cache = createTextShaperCache();
56
- shapeTextRunCached(cache, 'hi', {});
52
+ shapeTextRunCached(backend, cache, 'hi', {});
57
53
  clearTextShaperCache(cache);
58
- shapeTextRunCached(cache, 'hi', {});
59
- expect(cache._entries.size).toBe(1);
54
+ expect(shapeTextRunCached(backend, cache, 'hi', {})).not.toBeNull();
60
55
  });
61
56
  });
62
57
 
63
58
  describe('createTextShaperCache', () => {
64
- it('returns an empty cache', () => {
65
- const cache = createTextShaperCache();
66
- expect(cache._entries.size).toBe(0);
67
- });
68
-
69
59
  it('allocates a new cache on each call', () => {
70
60
  expect(createTextShaperCache()).not.toBe(createTextShaperCache());
71
61
  });
72
62
  });
73
63
 
74
64
  describe('disposeTextShaperCache', () => {
75
- it('clears all entries', () => {
76
- const { backend } = _makeCountingBackend();
77
- setTextShaperBackend(backend);
65
+ it('makes the cache unusable without calling the backend again', () => {
66
+ const tracker = _makeCountingBackend();
78
67
  const cache = createTextShaperCache();
79
- shapeTextRunCached(cache, 'hi', {});
68
+ shapeTextRunCached(tracker.backend, cache, 'hi', {});
80
69
  disposeTextShaperCache(cache);
81
- expect(cache._entries.size).toBe(0);
70
+ expect(shapeTextRunCached(tracker.backend, cache, 'hi', {})).toBeNull();
71
+ expect(tracker.calls).toBe(1);
72
+ });
73
+
74
+ it('is idempotent', () => {
75
+ const cache = createTextShaperCache();
76
+ disposeTextShaperCache(cache);
77
+ expect(() => disposeTextShaperCache(cache)).not.toThrow();
82
78
  });
83
79
  });
84
80
 
@@ -88,100 +84,80 @@ describe('initializeTextShaperCache', () => {
88
84
  });
89
85
  });
90
86
 
91
- function _hostWithAdvance(advanceWidth: number): HasTextShaper {
92
- return {
93
- text: {
94
- shaper: {
95
- measureText: () => advanceWidth,
96
- shapeRun: () => ({ ..._stubRun, advanceWidth }),
97
- },
98
- },
99
- };
100
- }
101
87
  describe('shapeTextRunCached', () => {
102
- it('keeps cache entries isolated when callers interleave explicit hosts', () => {
88
+ it('keeps cache entries isolated when callers interleave different hosts', () => {
103
89
  const cache = createTextShaperCache();
104
- const first = _hostWithAdvance(1);
105
- const second = _hostWithAdvance(2);
106
- expect(shapeTextRunCached(cache, 'hi', {}, undefined, first)?.advanceWidth).toBe(1);
107
- expect(shapeTextRunCached(cache, 'hi', {}, undefined, second)?.advanceWidth).toBe(2);
108
- expect(shapeTextRunCached(cache, 'hi', {}, undefined, first)?.advanceWidth).toBe(1);
109
- });
110
-
111
- it('returns null when no backend is set', () => {
112
- const cache = createTextShaperCache();
113
- expect(shapeTextRunCached(cache, 'hi', {})).toBeNull();
90
+ const first: HostTextShaperCapability = {
91
+ measureText: () => 1,
92
+ shapeRun: () => ({ ..._stubRun, advanceWidth: 1 }),
93
+ };
94
+ const second: HostTextShaperCapability = {
95
+ measureText: () => 2,
96
+ shapeRun: () => ({ ..._stubRun, advanceWidth: 2 }),
97
+ };
98
+ expect(shapeTextRunCached(first, cache, 'hi', {})?.advanceWidth).toBe(1);
99
+ expect(shapeTextRunCached(second, cache, 'hi', {})?.advanceWidth).toBe(2);
100
+ expect(shapeTextRunCached(first, cache, 'hi', {})?.advanceWidth).toBe(1);
114
101
  });
115
102
 
116
103
  it('returns null when the backend is advances-only', () => {
117
- setTextShaperBackend({ measureText: (t) => t.length });
118
104
  const cache = createTextShaperCache();
119
- expect(shapeTextRunCached(cache, 'hi', {})).toBeNull();
105
+ expect(shapeTextRunCached(_advancesOnly, cache, 'hi', {})).toBeNull();
120
106
  });
121
107
 
122
108
  it('returns a ShapedRun on success', () => {
123
109
  const { backend } = _makeCountingBackend();
124
- setTextShaperBackend(backend);
125
110
  const cache = createTextShaperCache();
126
- const run = shapeTextRunCached(cache, 'hi', {});
111
+ const run = shapeTextRunCached(backend, cache, 'hi', {});
127
112
  expect(run).not.toBeNull();
128
113
  expect(run!.glyphCount).toBe(2);
129
114
  });
130
115
 
131
116
  it('returns the same object on the second call (cache hit)', () => {
132
117
  const { backend } = _makeCountingBackend();
133
- setTextShaperBackend(backend);
134
118
  const cache = createTextShaperCache();
135
- const r1 = shapeTextRunCached(cache, 'hi', {});
136
- const r2 = shapeTextRunCached(cache, 'hi', {});
119
+ const r1 = shapeTextRunCached(backend, cache, 'hi', {});
120
+ const r2 = shapeTextRunCached(backend, cache, 'hi', {});
137
121
  expect(r1).toBe(r2);
138
122
  });
139
123
 
140
124
  it('calls the backend once for repeated identical inputs', () => {
141
125
  const tracker = _makeCountingBackend();
142
- setTextShaperBackend(tracker.backend);
143
126
  const cache = createTextShaperCache();
144
- shapeTextRunCached(cache, 'hi', {});
145
- shapeTextRunCached(cache, 'hi', {});
127
+ shapeTextRunCached(tracker.backend, cache, 'hi', {});
128
+ shapeTextRunCached(tracker.backend, cache, 'hi', {});
146
129
  expect(tracker.calls).toBe(1);
147
130
  });
148
131
 
149
132
  it('calls the backend again for different text', () => {
150
133
  const tracker = _makeCountingBackend();
151
- setTextShaperBackend(tracker.backend);
152
134
  const cache = createTextShaperCache();
153
- shapeTextRunCached(cache, 'hi', {});
154
- shapeTextRunCached(cache, 'bye', {});
135
+ shapeTextRunCached(tracker.backend, cache, 'hi', {});
136
+ shapeTextRunCached(tracker.backend, cache, 'bye', {});
155
137
  expect(tracker.calls).toBe(2);
156
138
  });
157
139
 
158
140
  it('calls the backend again for different format', () => {
159
141
  const tracker = _makeCountingBackend();
160
- setTextShaperBackend(tracker.backend);
161
142
  const cache = createTextShaperCache();
162
- shapeTextRunCached(cache, 'hi', { size: 12 });
163
- shapeTextRunCached(cache, 'hi', { size: 16 });
143
+ shapeTextRunCached(tracker.backend, cache, 'hi', { size: 12 });
144
+ shapeTextRunCached(tracker.backend, cache, 'hi', { size: 16 });
164
145
  expect(tracker.calls).toBe(2);
165
146
  });
166
147
 
167
148
  it('calls the backend again for different options direction', () => {
168
149
  const tracker = _makeCountingBackend();
169
- setTextShaperBackend(tracker.backend);
170
150
  const cache = createTextShaperCache();
171
- shapeTextRunCached(cache, 'hi', {}, { direction: 'LeftToRight' });
172
- shapeTextRunCached(cache, 'hi', {}, { direction: 'RightToLeft' });
151
+ shapeTextRunCached(tracker.backend, cache, 'hi', {}, { direction: 'LeftToRight' });
152
+ shapeTextRunCached(tracker.backend, cache, 'hi', {}, { direction: 'RightToLeft' });
173
153
  expect(tracker.calls).toBe(2);
174
154
  });
175
155
 
176
- it('does not cache null results (no backend)', () => {
156
+ it('does not cache null results (advances-only backend)', () => {
177
157
  const cache = createTextShaperCache();
178
- // First call: no backend, returns null, should not be cached.
179
- expect(shapeTextRunCached(cache, 'hi', {})).toBeNull();
180
- expect(cache._entries.size).toBe(0);
181
- // Install a backend; now the same call should succeed.
182
- const { backend } = _makeCountingBackend();
183
- setTextShaperBackend(backend);
184
- expect(shapeTextRunCached(cache, 'hi', {})).not.toBeNull();
185
- expect(cache._entries.size).toBe(1);
158
+ expect(shapeTextRunCached(_advancesOnly, cache, 'hi', {})).toBeNull();
159
+ const tracker = _makeCountingBackend();
160
+ expect(shapeTextRunCached(tracker.backend, cache, 'hi', {})).not.toBeNull();
161
+ expect(tracker.calls).toBe(1);
186
162
  });
187
163
  });
@@ -1,8 +1,7 @@
1
- import type { ShapedRun } from '@flighthq/types/contract';
1
+ import { getCaretPositionsForRun, getClusterForIndex, getIndexRangeForCluster } from './textShaperCluster.ts';
2
+ import type { ShapedRun } from './textShaperTypes.ts';
2
3
 
3
- import { getCaretPositionsForRun, getClusterForIndex, getIndexRangeForCluster } from './textShaperCluster';
4
-
5
- function _makeRun(glyphs: ReadonlyArray<{ cluster: number; xAdvance: number }>): ShapedRun {
4
+ function _makeRun(glyphs: ReadonlyArray<{ cluster: number; xAdvance: number; xOffset?: number }>): ShapedRun {
6
5
  return {
7
6
  advanceWidth: glyphs.reduce((s, g) => s + g.xAdvance, 0),
8
7
  direction: 'LeftToRight',
@@ -12,7 +11,7 @@ function _makeRun(glyphs: ReadonlyArray<{ cluster: number; xAdvance: number }>):
12
11
  cluster: g.cluster,
13
12
  glyphId: g.cluster,
14
13
  xAdvance: g.xAdvance,
15
- xOffset: 0,
14
+ xOffset: g.xOffset ?? 0,
16
15
  yAdvance: 0,
17
16
  yOffset: 0,
18
17
  })),
@@ -48,6 +47,14 @@ describe('getCaretPositionsForRun', () => {
48
47
  const pos = getCaretPositionsForRun(run);
49
48
  expect(pos[pos.length - 1]).toBe(run.advanceWidth);
50
49
  });
50
+
51
+ it('uses pen advances rather than glyph drawing offsets', () => {
52
+ const run = _makeRun([
53
+ { cluster: 0, xAdvance: 8, xOffset: 3 },
54
+ { cluster: 1, xAdvance: 7, xOffset: -2 },
55
+ ]);
56
+ expect(getCaretPositionsForRun(run)).toEqual([0, 8, 15]);
57
+ });
51
58
  });
52
59
 
53
60
  describe('getClusterForIndex', () => {
@@ -1,7 +1,7 @@
1
- import type { HasTextShaper, ShapedRun, ShapeRunOptions, TextFormat } from '@flighthq/types/contract';
1
+ import type { TextFormat } from '@flighthq/textformat/contract';
2
2
 
3
- import { setTextShaperBackend } from './textShaper';
4
- import { itemizeText, shapeTextRuns } from './textShaperItemize';
3
+ import { itemizeText, shapeTextRuns } from './textShaperItemize.ts';
4
+ import type { HostTextShaperCapability, ShapedRun, ShapeRunOptions } from './textShaperTypes.ts';
5
5
 
6
6
  const _emptyRun: ShapedRun = {
7
7
  advanceWidth: 0,
@@ -12,7 +12,7 @@ const _emptyRun: ShapedRun = {
12
12
  script: 'Latn',
13
13
  };
14
14
 
15
- function _makeShapingBackend() {
15
+ function _makeShapingBackend(): HostTextShaperCapability {
16
16
  return {
17
17
  measureText: (t: string) => t.length * 8,
18
18
  shapeRun: (_t: string, _f: Readonly<TextFormat>, opts?: Readonly<ShapeRunOptions>): ShapedRun => ({
@@ -23,9 +23,8 @@ function _makeShapingBackend() {
23
23
  };
24
24
  }
25
25
 
26
- afterEach(() => {
27
- setTextShaperBackend(null);
28
- });
26
+ const _advancesOnly: HostTextShaperCapability = { measureText: (t: string) => t.length };
27
+ const _shapingBackend = _makeShapingBackend();
29
28
 
30
29
  describe('itemizeText', () => {
31
30
  it('returns empty array for empty string', () => {
@@ -40,7 +39,6 @@ describe('itemizeText', () => {
40
39
  expect(items[0].direction).toBe('LeftToRight');
41
40
  });
42
41
  it('splits Arabic from Latin', () => {
43
- // ASCII 'AB' (LTR/Latin) followed by Arabic 'سلام' (U+0633 U+0644 U+0627 U+0645, RTL/Arab)
44
42
  const arabic = 'سلام';
45
43
  const mixed = 'AB' + arabic;
46
44
  const items = itemizeText(mixed, {});
@@ -52,15 +50,12 @@ describe('itemizeText', () => {
52
50
  });
53
51
  it('treats neutral characters (space) as belonging to the current run', () => {
54
52
  const items = itemizeText('Hello World', {});
55
- // Space is neutral; it should be absorbed into the Latin run, not create a boundary.
56
53
  expect(items).toHaveLength(1);
57
54
  expect(items[0].start).toBe(0);
58
55
  expect(items[0].end).toBe(11);
59
56
  });
60
57
  it('respects explicit direction override from options', () => {
61
58
  const items = itemizeText('Hello', {}, { direction: 'RightToLeft' });
62
- // Latin characters are LTR strong; the explicit direction in options sets the base but
63
- // strong LTR chars override.
64
59
  expect(items).toHaveLength(1);
65
60
  expect(items[0].direction).toBe('LeftToRight');
66
61
  });
@@ -70,7 +65,6 @@ describe('itemizeText', () => {
70
65
  const items = itemizeText(text, {});
71
66
  const covered = items.reduce((s, item) => s + (item.end - item.start), 0);
72
67
  expect(covered).toBe(text.length);
73
- // Verify contiguous coverage
74
68
  for (let i = 1; i < items.length; i++) {
75
69
  expect(items[i].start).toBe(items[i - 1].end);
76
70
  }
@@ -78,41 +72,35 @@ describe('itemizeText', () => {
78
72
  });
79
73
 
80
74
  describe('shapeTextRuns', () => {
81
- it('threads an explicit host to every shaped item', () => {
82
- const host: HasTextShaper = { text: { shaper: _makeShapingBackend() } };
83
- setTextShaperBackend({ measureText: () => 0 });
84
- expect(shapeTextRuns('ABسلام', {}, undefined, host)).toHaveLength(2);
75
+ it('threads the explicit host to every shaped item', () => {
76
+ const arabic = 'سلام';
77
+ expect(shapeTextRuns(_shapingBackend, 'AB' + arabic, {})).toHaveLength(2);
85
78
  });
86
79
 
87
80
  it('returns empty array for empty string', () => {
88
- expect(shapeTextRuns('', {})).toEqual([]);
89
- });
90
- it('returns empty array when no backend is set', () => {
91
- expect(shapeTextRuns('Hello', {})).toEqual([]);
81
+ expect(shapeTextRuns(_shapingBackend, '', {})).toEqual([]);
92
82
  });
93
83
  it('returns empty array when backend is advances-only', () => {
94
- setTextShaperBackend({ measureText: (t: string) => t.length });
95
- expect(shapeTextRuns('Hello', {})).toEqual([]);
84
+ expect(shapeTextRuns(_advancesOnly, 'Hello', {})).toEqual([]);
96
85
  });
97
86
  it('returns one ShapedRun per item for a mixed-script string', () => {
98
- setTextShaperBackend(_makeShapingBackend());
99
87
  const arabic = 'سلام';
100
- const runs = shapeTextRuns('AB' + arabic, {});
88
+ const runs = shapeTextRuns(_shapingBackend, 'AB' + arabic, {});
101
89
  expect(runs).toHaveLength(2);
102
90
  expect(runs[0].direction).toBe('LeftToRight');
103
91
  expect(runs[1].direction).toBe('RightToLeft');
104
92
  });
105
93
  it('passes per-item direction and script to shapeRun', () => {
106
94
  const captured: { direction?: string; script?: string }[] = [];
107
- setTextShaperBackend({
95
+ const capturingBackend: HostTextShaperCapability = {
108
96
  measureText: () => 0,
109
97
  shapeRun: (_t: string, _f: Readonly<TextFormat>, opts?: Readonly<ShapeRunOptions>): ShapedRun => {
110
98
  captured.push({ direction: opts?.direction, script: opts?.script });
111
99
  return { ..._emptyRun };
112
100
  },
113
- });
101
+ };
114
102
  const arabic = 'سلام';
115
- shapeTextRuns('AB' + arabic, {});
103
+ shapeTextRuns(capturingBackend, 'AB' + arabic, {});
116
104
  expect(captured[0].direction).toBe('LeftToRight');
117
105
  expect(captured[1].direction).toBe('RightToLeft');
118
106
  expect(captured[1].script).toBe('Arab');
@@ -1,4 +1,4 @@
1
- import { acquireShapedRun, releaseShapedRun, setShapedRunReleaseGuard } from './textShaperPool';
1
+ import { acquireShapedRun, releaseShapedRun, setShapedRunReleaseGuard } from './textShaperPool.ts';
2
2
 
3
3
  describe('acquireShapedRun', () => {
4
4
  it('returns a ShapedRun with the expected shape', () => {