Ghostty italic character rendering in monospace

Medium
Unlisted
Research in https://github.com/ghostty-org/ghostty how it redners italic characters. I think the challenge is that they extend outside of a single terminal cell, since it's monospace

Ghostty handles italic overflow through signed bearing offsets that allow glyphs to extend beyond their cell boundaries:

  1. Italic synthesis: Uses a 15° skew transformation matrix (tan(15°) ≈ 0.268) applied via CoreText/FreeType when no native italic font exists

  2. Glyph metrics: Each glyph stores offset_x and offset_y as signed integers — negative offset_x allows italic glyphs to extend left into the previous cell

  3. GPU rendering: The vertex shader simply applies these bearing offsets to position the glyph quad:

    cell_pos = cell_pos + size * corner + offset;  // offset can be negative!
    
  4. No clipping: Glyphs render freely in screen space — the renderer doesn't artificially clip to cell boundaries

For your Metal renderer, the key insight is:

  • Store glyph bearings as signed (i32) in your vertex data
  • In your vertex shader, add the bearing offset to the cell position
  • Let the glyph naturally overflow — no special handling needed

This is simpler than trying to clip or rescale; you just let the slanted glyph draw where the font metrics say it should go.

compare it to the approach we currently take to render italics

Now I have a complete picture. Here's the comparison:

Comparison: Tucanty vs Ghostty Italic Rendering

Aspect Tucanty (current) Ghostty
Font source Loads separate italic font files (Berkeley Mono Oblique) Uses native italic or synthesizes with 15° skew matrix
Glyph storage TextAtlasGlyph stores width, height, ascender, x, y — no horizontal bearing Stores offset_x (signed), offset_y (signed) for bearing
Cell positioning Positions at current_x, advances by glyph.width Positions at cell origin + signed bearing offset
Overflow handling Calculates left_padding in atlas, but discards it at render time Passes signed bearing to GPU, glyph extends naturally

The Problem in Tucanty

In renderer.rs lines 1079-1083, you correctly calculate left_padding for italic glyphs in the atlas:

let left_padding = if glyph_bearing_x < 0 { (-glyph_bearing_x) as u32 } else { 0 };

But in the render loop (lines 575-617), you position glyphs at current_x and advance by glyph_width_ndc — you never apply the bearing offset. The glyph is rendered aligned to the cell left edge, not offset by its bearing.

Fix

  1. Add bearing_x: i32 to TextAtlasGlyph
  2. In the render loop, offset pos_x by the bearing:
    let bearing_x_ndc = (glyph.bearing_x as f32 / window_width_px) * 2.0;
    let pos_x = current_x + bearing_x_ndc;
    
  3. Advance by cell width (not glyph width) to maintain the monospace grid
Let's try it. Before you make any changes, use the --capture flag and run `./scripts/test-attributes.sh combo` to see how italic, bold, underline render in the screenshot.

I see now — your current code bakes the bearing into the atlas cell by adding left_padding and bitmap_x_offset. This means the glyph in the atlas is already positioned correctly within a wider cell that accounts for the bearing.

The issue is that you're then rendering this wider glyph starting at current_x and advancing by glyph_width instead of the cell width, which can cause spacing drift.

Current approach: Stores a wider glyph in atlas (bearing baked in), renders it at cell position, advances by glyph width.

Ghostty approach: Stores glyph at its natural size, passes signed bearing to GPU, renders offset from cell position, advances by cell width.

Your approach should work for properly-designed monospace italic fonts like Berkeley Mono, but may have issues with:

  1. Synthesized italics
  2. Fonts where italic glyphs genuinely extend past the cell

The screenshots look fine, so Berkeley Mono is well-behaved. Want me to proceed with the Ghostty-style fix anyway for correctness with other fonts, or leave it as-is since it works?

Let's try the Ghostty approach, we can always revert if it doesn't work

Done! The Ghostty-style approach is now implemented:

  1. TextAtlasGlyph now has a bearing_x: i32 field
  2. Atlas storage: Glyphs are stored at their natural bitmap size (no padding)
  3. Render loop: Applies bearing_x offset when positioning, advances by cell width (not glyph width) to maintain the monospace grid

The before/after screenshots look identical, which is expected since Berkeley Mono Oblique is a well-designed monospace italic font. The real benefit comes with synthesized italics or fonts with more aggressive slant where glyphs genuinely extend past cell boundaries.

let's change to a different font to test it. can you use one of the macOS monospace fonts and check?
Explored 1 file

Let me check what monospace fonts are available on macOS and use Menlo (which has italic):

Explored 2 searches

SF Mono has separate regular and italic files. Let me temporarily modify the font paths to test:

Edited 1 file, ran 2 commands, ran 1 tool

Let me also test with some characters that are known to have more aggressive italic slants:

Ran 1 command, ran 1 tool

Works well with SF Mono. Let me revert to the original fonts:

Edited 1 file, ran 1 command

The Ghostty-style approach works with both Berkeley Mono and SF Mono. The key changes:

  1. Glyph stored at natural size — no padding baked into atlas
  2. Signed bearing_x passed through to render loop
  3. Position offset by bearing — pos_x = current_x + bearing_x_ndc
  4. **Advance by cell