Files
ghostty-rs/crates/ghostty-font/src/shape.rs
T
ddidderr fc7bb5c025 feat(font): port the Linux font engine
Ports the rest of ZIG/src/font for the freetype/fontconfig/harfbuzz
backend: face loading and rasterization (synthetic bold/italic, color
emoji bitmaps, glyph constraints for nerd font icons), fontconfig
discovery, DeferredFace, Collection, CodepointResolver, CodepointMap,
SharedGrid, opentype table parsers, the nerd font attribute table, and
the harfbuzz shaper with feature parsing and shaping cache.

Embedded fonts (JetBrains Mono 2.304 variable, Symbols Nerd Font 3.4.0,
Noto emoji) and the test fonts from Ghostty live in res/ with their OFL/
MIT licenses.

The crate opts into unsafe_code = "deny"; FFI is confined to ft.rs,
fontconfig.rs and hb.rs with SAFETY comments. Glyph resampling uses an
stb-style resampler that is not bit-identical to stb.

Deferred to the next font pass (needs ghostty-terminal/config):
shaper/run.zig, SharedGridSet.zig and the 23 terminal-driven shaper
tests. Config-facing types are mirrored locally in config_types.rs.

Test Plan:
- cargo test -p ghostty-font (139 tests)
2026-10-11 22:13:06 +02:00

72 lines
2.6 KiB
Rust

//! Shaping types shared by all shapers. Port of `font/shape.zig`.
//!
//! The shaper implementation for the Linux backend is HarfBuzz
//! ([`crate::shaper::harfbuzz::Shaper`]).
use crate::collection::Index;
pub use crate::shaper::cache::Cache;
pub use crate::shaper::feature::{DEFAULT_FEATURES, Feature, FeatureList};
pub use crate::shaper::{harfbuzz, noop};
/// Shaper implementation for our compile options (backend
/// `fontconfig_freetype` => HarfBuzz).
pub use crate::shaper::harfbuzz::Shaper;
/// A cell is a single single within a terminal that should be rendered
/// for a shaping call. Note all terminal cells may be present; only
/// cells that have a glyph that needs to be rendered.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Cell {
/// The X position of this shaper cell relative to the offset of the
/// run. Because runs are always within a single row, it is expected
/// that the caller can reconstruct the full position of the cell by
/// using the known Y position of the cell and adding the X position
/// to the run offset.
pub x: u16,
/// An additional offset to apply to the rendering.
pub x_offset: i16,
pub y_offset: i16,
/// The glyph index for this cell. The font index to use alongside
/// this cell is available in the text run. This glyph index is only
/// valid for a given GroupCache and FontIndex that was used to create
/// the runs.
pub glyph_index: u32,
}
/// Options for shapers.
#[derive(Debug, Clone, Default)]
pub struct Options {
/// Font features to use when shaping.
///
/// Note: eventually, this will move to font.Face probably as we may
/// want to support per-face feature configuration. For now, we only
/// support applying features globally.
pub features: Vec<String>,
}
/// A single text run. Port of `shaper/run.zig` `TextRun`.
///
/// TODO(integration): `run.zig` (RunIterator over terminal rows) is deferred
/// to F2; the `grid: *SharedGrid` field is omitted here and should be added
/// when `SharedGrid` and the run iterator are wired in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextRun {
/// A unique hash for this run. This can be used to cache the shaping
/// results.
pub hash: u64,
/// The offset in the row where this run started. This is added to the
/// X position of the final shaped cells to get the absolute position
/// in the row where they belong.
pub offset: u16,
/// The total number of cells produced by this run.
pub cells: u16,
/// The font index to use for the glyphs of this run.
pub font_index: Index,
}