Ports ZIG/src/font/{Metrics,Atlas,Glyph}.zig and the whole sprite font
(font/sprite/**): box drawing, block elements, braille, branch/git
graph symbols, geometric shapes, powerline, and the symbols for legacy
computing (+ supplement, incl. the octants table).
Ghostty draws sprites with z2d; we use tiny-skia for path coverage,
composited onto an alpha8 canvas, while following the Zig geometry
(positions, thickness, rounding). Comptime discovery of draw functions
is replaced by explicit, overlap-checked codepoint range tables.
Upstream reference sheets are vendored in testdata/ and compared with a
tolerance: braille and U+1CD00 are pixel-identical; other sheets differ
only in antialiasing on thin diagonals/curves. Review renders are written
to target/sprite_sheets/.
The crate temporarily drops its config/unicode/terminal deps until the
rest of the font engine is ported (Phase 3).
Test Plan:
- cargo test -p ghostty-font (49 tests incl. reference sheet comparison)
- inspect target/sprite_sheets/review-all-18x36.png
73 lines
1.9 KiB
Rust
73 lines
1.9 KiB
Rust
//! The built-in "sprite font": box drawing, block elements, braille, powerline
|
|
//! and other glyphs rasterized by code, plus special sprites (underlines,
|
|
//! cursors). Port of `font/sprite.zig`.
|
|
|
|
pub mod canvas;
|
|
pub mod draw;
|
|
pub mod face;
|
|
|
|
pub use canvas::{Box, Canvas, Color, Point};
|
|
pub use face::Face;
|
|
|
|
/// Sprites are represented as special codepoints outside of the Unicode
|
|
/// codepoint range. Unicode maxes out at U+10FFFF (21 bits), and we use the
|
|
/// high 11 bits to hide our special characters.
|
|
///
|
|
/// These characters are ONLY used for rendering and NEVER used written to
|
|
/// text files or any other exported format, so we don't use the Private Use
|
|
/// Area of Unicode.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
#[repr(u32)]
|
|
pub enum Sprite {
|
|
Underline = Sprite::START,
|
|
UnderlineDouble,
|
|
UnderlineDotted,
|
|
UnderlineDashed,
|
|
UnderlineCurly,
|
|
|
|
Strikethrough,
|
|
|
|
Overline,
|
|
|
|
CursorRect,
|
|
CursorHollowRect,
|
|
CursorBar,
|
|
CursorUnderline,
|
|
}
|
|
|
|
impl Sprite {
|
|
/// Start 1 above the maximum Unicode codepoint.
|
|
pub const START: u32 = 0x1F_FFFF + 1;
|
|
pub const END: u32 = u32::MAX;
|
|
|
|
pub const ALL: [Sprite; 11] = [
|
|
Sprite::Underline,
|
|
Sprite::UnderlineDouble,
|
|
Sprite::UnderlineDotted,
|
|
Sprite::UnderlineDashed,
|
|
Sprite::UnderlineCurly,
|
|
Sprite::Strikethrough,
|
|
Sprite::Overline,
|
|
Sprite::CursorRect,
|
|
Sprite::CursorHollowRect,
|
|
Sprite::CursorBar,
|
|
Sprite::CursorUnderline,
|
|
];
|
|
|
|
/// Convert a codepoint in the sprite range to a `Sprite`.
|
|
pub fn from_u32(cp: u32) -> Option<Sprite> {
|
|
Sprite::ALL.iter().copied().find(|s| *s as u32 == cp)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn sprite_start() {
|
|
assert_eq!(Sprite::START, Sprite::Underline as u32);
|
|
assert_eq!(Sprite::START + 10, Sprite::CursorUnderline as u32);
|
|
}
|
|
}
|