Skip to content

font/coretext: Use positions to correct glyph placement - #9883

Merged
mitchellh merged 10 commits into
ghostty-org:mainfrom
jacobsandlund:shaping-positions
Dec 17, 2025
Merged

mitchellh merged 10 commits into
ghostty-org:mainfrom
jacobsandlund:shaping-positions

Conversation

@jacobsandlund

@jacobsandlund jacobsandlund commented Dec 12, 2025 •

Copy link
Copy Markdown
Contributor

This PR uses positions to get correct glyph placement, which affects a small number of shaped runs but can even re-order glyphs from how they're appearing on main. This came from an investigation in jquast/wcwidth#155 (comment) that involved shaping text from the Universal Declaration of Human Rights in 500+ languages, taken from ucs-detect (but actually run with ttylang for more consistent results).

The full result of running this with the commented out debug lines enabled is here, showing 1704 differences (a small number compared to the number of cells being rendered): https://gist.github.com/jacobsandlund/9cb2c603308085fa03e758640583541e

Some positions are only a small offset that is applied to many letters from a script (e.g. Syriac letters are getting offset by some small amount).

There are more egregious cases, though. Consider Tai Tham vowels, that according to the Unicode core spec, are rendered visually ahead of the consonant they follow in the text. The Unicode spec oddly mentions this under the "New Tai Lue" section, contrasting it with other Brahmi-derived scripts such as Tai Tham:

This practice differs from the usual pattern for Brahmi-derived scripts, in which all dependent vowels are stored in logical order after their associated consonants, even when they are displayed to the left of those consonants.

For example "\u{1A2F}\u{1A70}" renders like this on main:

CleanShot 2025-12-12 at 09 25 16@2x

And it now renders like this on the PR:

CleanShot 2025-12-12 at 09 25 38@2x

This is the correct rendering as U+1A70 is "TAI THAM VOWEL SIGN OO" and needs to render ahead of the U+1A2F "TAI THAM LETTER DA"

Note, this PR just fixes CoreText. I'll work on Harfbuzz right after this, but I wanted to get this out first.

vtebench

vtebench does show some potential impact on dense cells:

output

dense_cells

This is the first time I've run vtebench, so to confirm it's consistent I ran it again:

output-2

dense_cells

AI disclaimer

I had been using Amp when I made this change, but looks like the thread just fell off of recent threads (about a week ago). I think Amp was the one to add the getPositionsPtr line and add position to the for loop. I manually restructured the code around a separate run_offset and cell_offset, and added the tests.

@jacobsandlund
jacobsandlund requested review from a team as code owners December 12, 2025 14:44
Comment thread src/font/shaper/coretext.zig Outdated
Comment on lines +392 to +394
// For debugging positions, turn this on:
//start_index: usize = 0,
//end_index: usize = 0,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can remove this and the below debug code if desired before merging, since it's several commented out lines

Comment on lines +1406 to +1407
// Disable grapheme clustering
t.modes.set(.grapheme_cluster, false);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This test depended on each glyph being a separate "x" / separate cell, so Grapheme clustering needs to be disabled, so I made that explicit.

Comment thread src/terminal/Terminal.zig
Comment on lines +8896 to 8897
// Enable grapheme clustering
t.modes.set(.grapheme_cluster, true);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just happened to catch that this comment was off.

@danielwe

Copy link
Copy Markdown
Contributor

Curious if this has any bearing on #8054?

@jacobsandlund

Copy link
Copy Markdown
Contributor Author

Curious if this has any bearing on #8054?

Quite possibly for Harfbuzz, which right now is only looking at the GlyphPosition {x,y}_advance, and ignoring {x,y}_offset. I'll look at Harfbuzz starting Wednesday.

@mitchellh mitchellh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks alright to me. It's always hard to context load on this stuff which is why we have the unit tests... given the rest pass without issue, I'm inclined to accept this and add regression tests as necessary.

You're also usually pretty thorough.

Only change I'd like to request is to either remove the debug stuff or move it to a function that we don't call (I'd prefer the latter so we can easily turn it back on).

@jacobsandlund

Copy link
Copy Markdown
Contributor Author

@mitchellh I pulled out the debugging into a function here: 139a23a

Note I still have some commented out logic for setting the start_index and end_index. I could remove that and then the log could just include the current codepoint, but that makes the log much less useful, so I left it.

@mitchellh mitchellh added this to the 1.3.0 milestone Dec 17, 2025
@mitchellh
mitchellh enabled auto-merge December 17, 2025 16:36
@mitchellh
mitchellh merged commit c84113d into ghostty-org:main Dec 17, 2025
mitchellh added a commit that referenced this pull request Jan 27, 2026
…changes (#10332)

This PR incorporates the recent changes made to CoreText in the
following PRs:

* #9883
* #10179
* #10295

Tests have been added verifying the behavior. Below are all the changes
in shaping from the above PRs but now using HarfBuzz.

## Tai Tham vowels (#9883)

Browser:

ᨯᩰ

Before:

(Note that this worked correctly with the old logic for HarfBuzz in
contrast to CoreText)

<img width="498" height="96" alt="CleanShot 2026-01-15 at 09 50 50@2x"
src="https://github.com/user-attachments/assets/a87abb4e-7829-4df0-9cb6-9fc254c10ba2"
/>

After:

<img width="496" height="96" alt="CleanShot 2026-01-15 at 09 51 07@2x"
src="https://github.com/user-attachments/assets/64cb8b12-0b99-4e9f-8189-d793469048f6"
/>


## Tibetan characters (discussion
#8054)

Browser:

ༀ

Before (with Noto Serif Tibetan):

<img width="586" height="154" alt="CleanShot 2026-01-16 at 09 28 32@2x"
src="https://github.com/user-attachments/assets/5b7df9c1-13ff-45fd-977e-9e152063517c"
/>

After:

<img width="604" height="202" alt="CleanShot 2026-01-16 at 09 28 46@2x"
src="https://github.com/user-attachments/assets/3832b8b0-1ce6-448d-a063-622127cfb213"
/>


## Tai Tham ligature (#10179
and #10295)

Browser:

ᩉ᩠ᨿᩩ

Before:

<img width="860" height="144" alt="CleanShot 2026-01-15 at 09 57 30@2x"
src="https://github.com/user-attachments/assets/56c85a06-1853-4f88-992b-568b7f1d4b4e"
/>

After:

<img width="860" height="108" alt="CleanShot 2026-01-15 at 09 57 44@2x"
src="https://github.com/user-attachments/assets/fe236aad-ac2c-4665-aef4-bc996e3b2938"
/>

## Javanese ligature (#10295)

Browser:

ᩉ᩠ᨿᩩ

Before:

<img width="856" height="128" alt="CleanShot 2026-01-15 at 09 59 04@2x"
src="https://github.com/user-attachments/assets/73c60445-a80d-4003-ae2b-c8d53f3c9cf9"
/>

After:

<img width="866" height="114" alt="CleanShot 2026-01-15 at 09 59 23@2x"
src="https://github.com/user-attachments/assets/fce80429-096a-455e-aabd-00bf33fdae54"
/>

## Chakma ligature (#10295)

Browser:
𑄝𑄖𑄳𑄠𑄬

Before:

<img width="1298" height="104" alt="CleanShot 2026-01-15 at 10 00 31@2x"
src="https://github.com/user-attachments/assets/72e2be85-fb4b-4ca0-98d7-30279fd2613f"
/>

After:

<img width="1310" height="84" alt="CleanShot 2026-01-15 at 10 00 52@2x"
src="https://github.com/user-attachments/assets/a5e2a89c-7f1e-474c-9d61-b90c6a6ffedd"
/>

## Bengali ligature (#10295)

Browser:
রাষ্ট্রে

Before:
<img width="1268" height="94" alt="CleanShot 2026-01-15 at 10 02 16@2x"
src="https://github.com/user-attachments/assets/2946701c-a41b-45c5-b442-915dad45d380"
/>

After:
<img width="1274" height="110" alt="CleanShot 2026-01-15 at 10 02 32@2x"
src="https://github.com/user-attachments/assets/c1684149-cca4-43a7-b384-13e3d4854765"
/>

## Devanagari string (test changed in
#10295)

Browser:
अपार्टमेंट

Before:

<img width="560" height="98" alt="CleanShot 2026-01-16 at 09 31 45@2x"
src="https://github.com/user-attachments/assets/8df36427-c1fe-4f3c-9e2a-7e9b798411b4"
/>

After:

<img width="570" height="88" alt="CleanShot 2026-01-16 at 09 32 06@2x"
src="https://github.com/user-attachments/assets/972ed525-f341-400b-a09c-29b0a9135502"
/>


## AI disclaimer

I used Amp to copy the logic from CoreText, but had to help it along. I
took over after I noticed the different behavior with HarfBuzz's default
cluster level.

[Adopt CoreText changes to HarfBuzz
shaper](https://ampcode.com/threads/T-019bbccd-74d3-76c8-add4-6270f0f5375f)
[Rename debug_codepoints, explore HarfBuzz
alternatives](https://ampcode.com/threads/T-019bbce3-bed6-70fa-9eec-b0d1ee448ee9)
[Fix cluster indexing in codepoints
array](https://ampcode.com/threads/T-019bbced-d67b-7239-b507-9b3bd027faeb)
@jacobsandlund
jacobsandlund deleted the shaping-positions branch July 28, 2026 17:42
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.

3 participants