Summary
Canvas measureText() does not account for letter-spacing, so text width calculations are inaccurate when letter-spacing is applied. Since pretext already segments text into graphemes, it seems like a natural place to handle this correction.
Proposal
Add an optional letterSpacing parameter (in px) to PrepareOptions:
const prepared = prepare(text, font, { letterSpacing: 2 })
During prepare(), each segment's width would be adjusted by graphemeCount × letterSpacing. The layout() hot path would remain unchanged since it already operates on cached widths.
Edge cases to consider
- CSS does not add spacing after the last character in a line
- Interaction with
overflow-wrap: break-word grapheme-level breaking
Why pretext is the right layer for this
Since canvas measureText() ignores letter-spacing entirely, any consumer of pretext that needs letter-spacing would have to reimplement segment-level grapheme counting — which pretext already does internally. Baking it in would keep measurement accurate and self-contained.
Summary
Canvas
measureText()does not account forletter-spacing, so text width calculations are inaccurate when letter-spacing is applied. Since pretext already segments text into graphemes, it seems like a natural place to handle this correction.Proposal
Add an optional
letterSpacingparameter (in px) toPrepareOptions:During
prepare(), each segment's width would be adjusted bygraphemeCount × letterSpacing. Thelayout()hot path would remain unchanged since it already operates on cached widths.Edge cases to consider
overflow-wrap: break-wordgrapheme-level breakingWhy pretext is the right layer for this
Since canvas
measureText()ignores letter-spacing entirely, any consumer of pretext that needs letter-spacing would have to reimplement segment-level grapheme counting — which pretext already does internally. Baking it in would keep measurement accurate and self-contained.