@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.
- package/README.md +2 -2
- package/dist/.tsbuildinfo.contract +1 -0
- package/dist/.tsbuildinfo.runtime +1 -0
- package/dist/contract.d.ts +2 -10
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +2 -10
- package/dist/contract.js.map +1 -1
- package/dist/contract.textshaper-types.d.ts +2 -0
- package/dist/contract.textshaper-types.d.ts.map +1 -0
- package/dist/contract.textshaper-types.js +4 -0
- package/dist/contract.textshaper-types.js.map +1 -0
- package/dist/enableTextShaperGuards.d.ts +2 -1
- package/dist/enableTextShaperGuards.d.ts.map +1 -1
- package/dist/enableTextShaperGuards.js +6 -7
- package/dist/enableTextShaperGuards.js.map +1 -1
- package/dist/index.d.ts +10 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/runtime.d.ts +10 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +9 -0
- package/dist/runtime.js.map +1 -0
- package/dist/textShaper.d.ts +9 -13
- package/dist/textShaper.d.ts.map +1 -1
- package/dist/textShaper.js +7 -37
- package/dist/textShaper.js.map +1 -1
- package/dist/textShaperCache.d.ts +4 -2
- package/dist/textShaperCache.d.ts.map +1 -1
- package/dist/textShaperCache.js +9 -14
- package/dist/textShaperCache.js.map +1 -1
- package/dist/textShaperCluster.d.ts +1 -1
- package/dist/textShaperCluster.d.ts.map +1 -1
- package/dist/textShaperItemize.d.ts +3 -2
- package/dist/textShaperItemize.d.ts.map +1 -1
- package/dist/textShaperItemize.js +3 -3
- package/dist/textShaperItemize.js.map +1 -1
- package/dist/textShaperPool.d.ts +1 -1
- package/dist/textShaperPool.d.ts.map +1 -1
- package/dist/textShaperPool.js +1 -1
- package/dist/textShaperPool.js.map +1 -1
- package/dist/textShaperRun.d.ts +21 -15
- package/dist/textShaperRun.d.ts.map +1 -1
- package/dist/textShaperRun.js +32 -42
- package/dist/textShaperRun.js.map +1 -1
- package/dist/textShaperSignals.d.ts +2 -1
- package/dist/textShaperSignals.d.ts.map +1 -1
- package/dist/textShaperSignals.js +2 -9
- package/dist/textShaperSignals.js.map +1 -1
- package/dist/textShaperTypes.d.ts +102 -0
- package/dist/textShaperTypes.d.ts.map +1 -0
- package/dist/textShaperTypes.js +16 -0
- package/dist/textShaperTypes.js.map +1 -0
- package/package.json +9 -6
- package/src/enableTextShaperGuards.test.ts +19 -15
- package/src/textShaper.test.ts +34 -115
- package/src/textShaperCache.test.ts +38 -64
- package/src/textShaperCluster.test.ts +2 -3
- package/src/textShaperItemize.test.ts +15 -27
- package/src/textShaperPool.test.ts +1 -1
- package/src/textShaperRun.test.ts +51 -113
- package/src/textShaperSignals.test.ts +1 -44
- package/dist/_textShaperHooks.d.ts +0 -4
- package/dist/_textShaperHooks.d.ts.map +0 -1
- package/dist/_textShaperHooks.js +0 -5
- package/dist/_textShaperHooks.js.map +0 -1
- package/src/_textShaperHooks.test.ts +0 -69
package/src/textShaper.test.ts
CHANGED
|
@@ -1,83 +1,54 @@
|
|
|
1
|
-
import
|
|
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
|
-
|
|
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
|
|
46
|
-
|
|
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('
|
|
64
|
-
|
|
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
|
-
|
|
28
|
+
const backend = partialBackend();
|
|
75
29
|
for (const operation of OPTIONAL_OPERATIONS) {
|
|
76
|
-
expect(hasTextShaperOperation(operation)).toBe(
|
|
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():
|
|
65
|
+
function partialBackend(): HostTextShaperCapability {
|
|
143
66
|
return {
|
|
144
67
|
measureText: (() => undefined) as never,
|
|
145
|
-
} as
|
|
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:
|
|
19
|
+
function _makeCountingBackend(): { backend: HostTextShaperCapability; readonly calls: number } {
|
|
22
20
|
let calls = 0;
|
|
23
21
|
return {
|
|
24
22
|
backend: {
|
|
@@ -34,30 +32,26 @@ function _makeCountingBackend(): { backend: TextShaperBackend; readonly calls: n
|
|
|
34
32
|
};
|
|
35
33
|
}
|
|
36
34
|
|
|
37
|
-
|
|
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
39
|
const tracker = _makeCountingBackend();
|
|
44
|
-
setTextShaperBackend(tracker.backend);
|
|
45
40
|
const cache = createTextShaperCache();
|
|
46
|
-
shapeTextRunCached(cache, 'hi', {});
|
|
47
|
-
shapeTextRunCached(cache, 'hi', {});
|
|
41
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', {});
|
|
42
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', {});
|
|
48
43
|
expect(tracker.calls).toBe(1);
|
|
49
44
|
clearTextShaperCache(cache);
|
|
50
|
-
shapeTextRunCached(cache, 'hi', {});
|
|
45
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', {});
|
|
51
46
|
expect(tracker.calls).toBe(2);
|
|
52
47
|
});
|
|
53
48
|
|
|
54
49
|
it('cache remains usable after clearing', () => {
|
|
55
50
|
const { backend } = _makeCountingBackend();
|
|
56
|
-
setTextShaperBackend(backend);
|
|
57
51
|
const cache = createTextShaperCache();
|
|
58
|
-
shapeTextRunCached(cache, 'hi', {});
|
|
52
|
+
shapeTextRunCached(backend, cache, 'hi', {});
|
|
59
53
|
clearTextShaperCache(cache);
|
|
60
|
-
expect(shapeTextRunCached(cache, 'hi', {})).not.toBeNull();
|
|
54
|
+
expect(shapeTextRunCached(backend, cache, 'hi', {})).not.toBeNull();
|
|
61
55
|
});
|
|
62
56
|
});
|
|
63
57
|
|
|
@@ -70,11 +64,10 @@ describe('createTextShaperCache', () => {
|
|
|
70
64
|
describe('disposeTextShaperCache', () => {
|
|
71
65
|
it('makes the cache unusable without calling the backend again', () => {
|
|
72
66
|
const tracker = _makeCountingBackend();
|
|
73
|
-
setTextShaperBackend(tracker.backend);
|
|
74
67
|
const cache = createTextShaperCache();
|
|
75
|
-
shapeTextRunCached(cache, 'hi', {});
|
|
68
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', {});
|
|
76
69
|
disposeTextShaperCache(cache);
|
|
77
|
-
expect(shapeTextRunCached(cache, 'hi', {})).toBeNull();
|
|
70
|
+
expect(shapeTextRunCached(tracker.backend, cache, 'hi', {})).toBeNull();
|
|
78
71
|
expect(tracker.calls).toBe(1);
|
|
79
72
|
});
|
|
80
73
|
|
|
@@ -91,99 +84,80 @@ describe('initializeTextShaperCache', () => {
|
|
|
91
84
|
});
|
|
92
85
|
});
|
|
93
86
|
|
|
94
|
-
function _hostWithAdvance(advanceWidth: number): HasTextShaper {
|
|
95
|
-
return {
|
|
96
|
-
text: {
|
|
97
|
-
shaper: {
|
|
98
|
-
measureText: () => advanceWidth,
|
|
99
|
-
shapeRun: () => ({ ..._stubRun, advanceWidth }),
|
|
100
|
-
},
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
87
|
describe('shapeTextRunCached', () => {
|
|
105
|
-
it('keeps cache entries isolated when callers interleave
|
|
106
|
-
const cache = createTextShaperCache();
|
|
107
|
-
const first = _hostWithAdvance(1);
|
|
108
|
-
const second = _hostWithAdvance(2);
|
|
109
|
-
expect(shapeTextRunCached(cache, 'hi', {}, undefined, first)?.advanceWidth).toBe(1);
|
|
110
|
-
expect(shapeTextRunCached(cache, 'hi', {}, undefined, second)?.advanceWidth).toBe(2);
|
|
111
|
-
expect(shapeTextRunCached(cache, 'hi', {}, undefined, first)?.advanceWidth).toBe(1);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('returns null when no backend is set', () => {
|
|
88
|
+
it('keeps cache entries isolated when callers interleave different hosts', () => {
|
|
115
89
|
const cache = createTextShaperCache();
|
|
116
|
-
|
|
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);
|
|
117
101
|
});
|
|
118
102
|
|
|
119
103
|
it('returns null when the backend is advances-only', () => {
|
|
120
|
-
setTextShaperBackend({ measureText: (t) => t.length });
|
|
121
104
|
const cache = createTextShaperCache();
|
|
122
|
-
expect(shapeTextRunCached(cache, 'hi', {})).toBeNull();
|
|
105
|
+
expect(shapeTextRunCached(_advancesOnly, cache, 'hi', {})).toBeNull();
|
|
123
106
|
});
|
|
124
107
|
|
|
125
108
|
it('returns a ShapedRun on success', () => {
|
|
126
109
|
const { backend } = _makeCountingBackend();
|
|
127
|
-
setTextShaperBackend(backend);
|
|
128
110
|
const cache = createTextShaperCache();
|
|
129
|
-
const run = shapeTextRunCached(cache, 'hi', {});
|
|
111
|
+
const run = shapeTextRunCached(backend, cache, 'hi', {});
|
|
130
112
|
expect(run).not.toBeNull();
|
|
131
113
|
expect(run!.glyphCount).toBe(2);
|
|
132
114
|
});
|
|
133
115
|
|
|
134
116
|
it('returns the same object on the second call (cache hit)', () => {
|
|
135
117
|
const { backend } = _makeCountingBackend();
|
|
136
|
-
setTextShaperBackend(backend);
|
|
137
118
|
const cache = createTextShaperCache();
|
|
138
|
-
const r1 = shapeTextRunCached(cache, 'hi', {});
|
|
139
|
-
const r2 = shapeTextRunCached(cache, 'hi', {});
|
|
119
|
+
const r1 = shapeTextRunCached(backend, cache, 'hi', {});
|
|
120
|
+
const r2 = shapeTextRunCached(backend, cache, 'hi', {});
|
|
140
121
|
expect(r1).toBe(r2);
|
|
141
122
|
});
|
|
142
123
|
|
|
143
124
|
it('calls the backend once for repeated identical inputs', () => {
|
|
144
125
|
const tracker = _makeCountingBackend();
|
|
145
|
-
setTextShaperBackend(tracker.backend);
|
|
146
126
|
const cache = createTextShaperCache();
|
|
147
|
-
shapeTextRunCached(cache, 'hi', {});
|
|
148
|
-
shapeTextRunCached(cache, 'hi', {});
|
|
127
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', {});
|
|
128
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', {});
|
|
149
129
|
expect(tracker.calls).toBe(1);
|
|
150
130
|
});
|
|
151
131
|
|
|
152
132
|
it('calls the backend again for different text', () => {
|
|
153
133
|
const tracker = _makeCountingBackend();
|
|
154
|
-
setTextShaperBackend(tracker.backend);
|
|
155
134
|
const cache = createTextShaperCache();
|
|
156
|
-
shapeTextRunCached(cache, 'hi', {});
|
|
157
|
-
shapeTextRunCached(cache, 'bye', {});
|
|
135
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', {});
|
|
136
|
+
shapeTextRunCached(tracker.backend, cache, 'bye', {});
|
|
158
137
|
expect(tracker.calls).toBe(2);
|
|
159
138
|
});
|
|
160
139
|
|
|
161
140
|
it('calls the backend again for different format', () => {
|
|
162
141
|
const tracker = _makeCountingBackend();
|
|
163
|
-
setTextShaperBackend(tracker.backend);
|
|
164
142
|
const cache = createTextShaperCache();
|
|
165
|
-
shapeTextRunCached(cache, 'hi', { size: 12 });
|
|
166
|
-
shapeTextRunCached(cache, 'hi', { size: 16 });
|
|
143
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', { size: 12 });
|
|
144
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', { size: 16 });
|
|
167
145
|
expect(tracker.calls).toBe(2);
|
|
168
146
|
});
|
|
169
147
|
|
|
170
148
|
it('calls the backend again for different options direction', () => {
|
|
171
149
|
const tracker = _makeCountingBackend();
|
|
172
|
-
setTextShaperBackend(tracker.backend);
|
|
173
150
|
const cache = createTextShaperCache();
|
|
174
|
-
shapeTextRunCached(cache, 'hi', {}, { direction: 'LeftToRight' });
|
|
175
|
-
shapeTextRunCached(cache, 'hi', {}, { direction: 'RightToLeft' });
|
|
151
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', {}, { direction: 'LeftToRight' });
|
|
152
|
+
shapeTextRunCached(tracker.backend, cache, 'hi', {}, { direction: 'RightToLeft' });
|
|
176
153
|
expect(tracker.calls).toBe(2);
|
|
177
154
|
});
|
|
178
155
|
|
|
179
|
-
it('does not cache null results (
|
|
156
|
+
it('does not cache null results (advances-only backend)', () => {
|
|
180
157
|
const cache = createTextShaperCache();
|
|
181
|
-
|
|
182
|
-
expect(shapeTextRunCached(cache, 'hi', {})).toBeNull();
|
|
183
|
-
// Install a backend; now the same call should succeed.
|
|
158
|
+
expect(shapeTextRunCached(_advancesOnly, cache, 'hi', {})).toBeNull();
|
|
184
159
|
const tracker = _makeCountingBackend();
|
|
185
|
-
|
|
186
|
-
expect(shapeTextRunCached(cache, 'hi', {})).not.toBeNull();
|
|
160
|
+
expect(shapeTextRunCached(tracker.backend, cache, 'hi', {})).not.toBeNull();
|
|
187
161
|
expect(tracker.calls).toBe(1);
|
|
188
162
|
});
|
|
189
163
|
});
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { getCaretPositionsForRun, getClusterForIndex, getIndexRangeForCluster } from './textShaperCluster';
|
|
1
|
+
import { getCaretPositionsForRun, getClusterForIndex, getIndexRangeForCluster } from './textShaperCluster.ts';
|
|
2
|
+
import type { ShapedRun } from './textShaperTypes.ts';
|
|
4
3
|
|
|
5
4
|
function _makeRun(glyphs: ReadonlyArray<{ cluster: number; xAdvance: number; xOffset?: number }>): ShapedRun {
|
|
6
5
|
return {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { TextFormat } from '@flighthq/textformat/contract';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
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
|
-
|
|
27
|
-
|
|
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
|
|
82
|
-
const
|
|
83
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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', () => {
|