feat: letterSpacing option in prepare()#108
Open
ImpulseB23 wants to merge 2 commits intochenglou:mainfrom
Open
feat: letterSpacing option in prepare()#108ImpulseB23 wants to merge 2 commits intochenglou:mainfrom
ImpulseB23 wants to merge 2 commits intochenglou:mainfrom
Conversation
There was a problem hiding this comment.
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
PrepareOptionswithletterSpacing?: numberand thread it throughprepareInternal() -> 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #78
Adds
letterSpacing(in px) toPrepareOptions. Duringprepare(), each segment's cached width is adjusted bygraphemeCount * letterSpacing, with trailing letter-spacing trimmed at line boundaries vialineEndFitAdvance. Thelayout()hot path is untouched.Handles prefix currencies, CJK, pre-wrap, negative spacing (tighter tracking), and overflow-wrap grapheme-level breaking. Grapheme counting uses
Intl.Segmenterand is gated behindletterSpacing !== 0so there's zero overhead on the default path.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.