Skip to content

Feature request: letterSpacing support in prepare() #78

@skyisle

Description

@skyisle

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions