web engine - experimental web browser

Basic text shaping: kern table and horizontal advance #26

open opened by pierrelf.com

Phase 2: Pure Rust Font Engine#

Implement basic text shaping: position glyphs horizontally using advance widths (already available from hmtx) and kern pair adjustments.

kern table parsing#

  • Parse the kern table (both format 0 and format 3 subtables)
  • Format 0: array of kern pairs sorted by left/right glyph ID pair, binary-searchable
  • Extract kerning values (FUnits) for glyph pairs
  • Support both horizontal and cross-stream kerning
  • Handle multiple subtables (coverage field)

Text shaping pipeline#

  • Input: string of Unicode code points + font + size
  • Map each code point to a glyph ID via cmap
  • Look up advance width for each glyph from hmtx
  • Apply kern pair adjustments between consecutive glyphs
  • Output: Vec<ShapedGlyph> with glyph IDs and x/y positions

Data structures#

pub struct ShapedGlyph {
    pub glyph_id: u16,
    pub x_offset: f32,  // horizontal position in pixels
    pub y_offset: f32,  // vertical offset (usually 0 for basic shaping)
    pub x_advance: f32, // horizontal advance in pixels
}

Public API#

  • Font::shape_text(text: &str, size_px: f32) -> Vec<ShapedGlyph>
  • Font::kern_pair(left: u16, right: u16) -> i16 (raw kern value in font units)

Acceptance criteria#

  • kern table is parsed for fonts that contain it
  • Glyph advances are correctly read from hmtx
  • Kern pair adjustments are applied between glyph pairs
  • shape_text produces correctly positioned glyph runs
  • Fonts without kern tables work (zero kerning)
  • All existing tests continue to pass
  • cargo clippy --workspace -- -D warnings passes
  • cargo fmt --all --check passes
sign up or login to add to the discussion
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:meotu43t6usg4qdwzenk4s2t/sh.tangled.repo.issue/3mflhk7yign2y