Skip to content

feat: letterSpacing option in prepare()#108

Open
ImpulseB23 wants to merge 2 commits intochenglou:mainfrom
ImpulseB23:feat/letter-spacing
Open

feat: letterSpacing option in prepare()#108
ImpulseB23 wants to merge 2 commits intochenglou:mainfrom
ImpulseB23:feat/letter-spacing

Conversation

@ImpulseB23
Copy link
Copy Markdown

Closes #78

Adds letterSpacing (in px) to PrepareOptions. During prepare(), each segment's cached width is adjusted by graphemeCount * letterSpacing, with trailing letter-spacing trimmed at line boundaries via lineEndFitAdvance. The layout() hot path is untouched.

Handles prefix currencies, CJK, pre-wrap, negative spacing (tighter tracking), and overflow-wrap grapheme-level breaking. Grapheme counting uses Intl.Segmenter and is gated behind letterSpacing !== 0 so there's zero overhead on the default path.

const prepared = prepare(text, font, { letterSpacing: 2 })
const { height } = layout(prepared, width, lineHeight)

Tested in Chromium on Windows across various texts, widths, and spacing values (0-10px). All match browser line counts. 87/87 tests pass including 7 new letter-spacing tests.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an optional letterSpacing (px) to prepare() so cached segment widths can account for CSS-like tracking without changing the layout() API surface.

Changes:

  • Extend PrepareOptions with letterSpacing?: number and thread it through prepareInternal() -> measureAnalysis().
  • Adjust measured segment widths (and overflow-wrap grapheme widths/prefix widths) to include letter-spacing effects.
  • Add unit tests covering basic width deltas, zero/negative spacing, and a few wrapping scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/layout.ts Implements letterSpacing during prepare-time measurement by adjusting cached widths and line-end advances.
src/layout.test.ts Adds tests for width deltas and basic wrapping behavior when letterSpacing is set.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: letterSpacing support in prepare()

2 participants