refactor(Font): use pretext layout() directly in row size resolver

createFontRowSizeResolver was reaching into TextLayoutEngine, which
internally called pretext's heavy layoutWithLines and then walked
per-grapheme cursors to build chars arrays. The resolver discarded all
that work and used only totalHeight.

Replace with direct prepare + layout from @chenglou/pretext — pretext
docs explicitly recommend layout() (not layoutWithLines) for the resize
hot path: pure arithmetic on cached segment widths, no canvas calls, no
string allocations.

Test spies on TextLayoutEngine.prototype.layout migrated to vi.mock-ed
pretext layout (pretext's ESM exports are frozen — vi.spyOn fails with
"Cannot redefine property").

TextLayoutEngine marked @deprecated since it has no remaining
consumers; slated for removal after a release cycle.
This commit is contained in:
Ilia Mashkov
2026-05-24 20:12:48 +03:00
parent 728380498b
commit f92577608a
3 changed files with 36 additions and 10 deletions
@@ -70,6 +70,14 @@ export interface LayoutResult {
* **Canvas requirement:** pretext calls `document.createElement('canvas').getContext('2d')` on
* first use and caches the context for the process lifetime. Tests must install a canvas mock
* (see `__mocks__/canvas.ts`) before the first `layout()` call.
*
* @deprecated No live consumers remain — the only previous caller
* (`createFontRowSizeResolver`) now invokes pretext's `prepare` + `layout`
* directly (per pretext's "hot-path resize function" guidance). If you need
* single-font height-only measurement, use `prepare` + `layout` from
* `@chenglou/pretext` directly. If you need per-grapheme x/width data, see
* `CharacterComparisonEngine` (dual-font) or revive a slimmer wrapper.
* Slated for removal once it has been absent from `main` for a release cycle.
*/
export class TextLayoutEngine {
/**