Commit dc07b70
authored
fix: Consistent text positioning when embedFont is false (#723)
## Problem
When using `embedFont: false`, consecutive `<text>` elements have
inconsistent `x` positions, causing uneven spacing between words. This
is especially noticeable in multi-line text where gaps can accumulate to
over 1px.
| Content | x | width | Expected x | Gap |
|---------|---|-------|------------|-----|
| `" "` (space) | 96.29 | 3.96 | - | - |
| `"behavior"` | 100.56 | 61.02 | 100.25 | +0.31px |
## Root Cause
Two issues in `src/text/index.ts`:
1. **Line 570**: `leftOffset = Math.round(leftOffset)` rounds x
positions to integers while widths remain fractional
2. **Line 344**: `measureGrapheme()` on multi-character strings uses
`getAdvanceWidth()` (includes kerning), but `currentWidth` accumulates
using `measureText()` (sum of individual graphemes)
## Changes
1. Only round `leftOffset` when `embedFont: true` (paths benefit from
pixel alignment)
2. Use `measureText()` for multi-character strings when `embedFont:
false` to match position accumulation
## Test
Added `test/embed-font.test.tsx` that verifies consecutive text elements
have consistent x positions (gap < 0.01px).1 parent 54a749b commit dc07b70
File tree
5 files changed
+84
-1
lines changed- src/text
- test
- __image_snapshots__
5 files changed
+84
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
341 | 341 | | |
342 | 342 | | |
343 | 343 | | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
344 | 350 | | |
345 | 351 | | |
346 | 352 | | |
| |||
585 | 591 | | |
586 | 592 | | |
587 | 593 | | |
588 | | - | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
589 | 600 | | |
590 | 601 | | |
591 | 602 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
0 commit comments