pub struct FontSystem { /* private fields */ }Expand description
The font system — owns font discovery, caching, and measurement.
Chrome equivalent: FontCache (singleton per renderer process).
In Kozan: one per View (per-thread, matching Chrome’s threading model).
All font metrics and text measurements go through this system. No hardcoded values — everything comes from real font files.
Uses RefCell for interior mutability so the TextMeasurer trait
(which takes &self) can perform real Parley shaping (which needs &mut).
Safe because layout is single-threaded per View.
Implementations§
Source§impl FontSystem
impl FontSystem
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new font system, discovering system fonts.
Chrome: FontCache::Create() → enumerates platform fonts.
Parley/Fontique scans the system font directories.
Sourcepub fn register_font(&self, data: impl Into<FontBlob>) -> Vec<String>
pub fn register_font(&self, data: impl Into<FontBlob>) -> Vec<String>
Register custom font data (TTF/OTF/TTC bytes) into the font collection.
Chrome equivalent: FontFaceCache::Add() — registers a @font-face
source so the font becomes available for CSS font-family matching.
After registration, the font’s family name is automatically discovered
from the font’s name table and becomes usable via font-family in CSS.
Accepts anything convertible to Arc<dyn AsRef<[u8]> + Send + Sync>:
&'static [u8]— zero-copy forinclude_bytes!()(Arc wraps the pointer, not the data)Vec<u8>— for runtime-loaded fonts (one allocation into Arc)Arc<[u8]>— if you already have shared font data
Returns the family names that were registered.
§Example
// Static — zero copy, the bytes live in the binary:
ctx.register_font(include_bytes!("../assets/Cairo.ttf") as &[u8]);
// Runtime — from a file:
ctx.register_font(std::fs::read("font.ttf").unwrap());Sourcepub fn shape_text(&self, text: &str, query: &FontQuery) -> TextMetrics
pub fn shape_text(&self, text: &str, query: &FontQuery) -> TextMetrics
Measure the advance width of a text run.
Chrome: CachingWordShaper::Width() → HarfBuzzShaper::Shape().
Parley: builds a layout, shapes with HarfRust, returns width.
Sourcepub fn query_metrics(&self, query: &FontQuery) -> FontMetrics
pub fn query_metrics(&self, query: &FontQuery) -> FontMetrics
Get font metrics (ascent, descent, line-gap) for a given font query.
Chrome: SimpleFontData::GetFontMetrics() → reads from font’s
OS/2 and hhea tables via Skia.
Source§impl FontSystem
impl FontSystem
Sourcepub fn shape_glyphs(
&self,
text: &str,
query: &FontQuery,
color: [u8; 4],
) -> Vec<ShapedTextRun>
pub fn shape_glyphs( &self, text: &str, query: &FontQuery, color: [u8; 4], ) -> Vec<ShapedTextRun>
Shape text and extract owned glyph runs for rendering.
Chrome: layout shapes text → stores ShapeResult → paint reads it.
This is the ONLY place text shaping happens. The renderer just
draws the pre-shaped glyphs — zero font logic in the GPU layer.
Handles automatically via Parley + HarfRust:
- Arabic letter joining (initial/medial/final forms)
- RTL bidi reordering
- Ligatures and kerning
- Font fallback (system fonts via Fontique)
- CJK, Thai, Devanagari — all complex scripts
Trait Implementations§
Source§impl Default for FontSystem
impl Default for FontSystem
Source§impl TextMeasurer for FontSystem
Real TextMeasurer implementation using Parley font shaping.
impl TextMeasurer for FontSystem
Real TextMeasurer implementation using Parley font shaping.
Uses RefCell interior mutability to satisfy the &self trait
while performing real &mut Parley operations internally.
Source§fn measure(&self, text: &str, font_size: f32) -> TextMetrics
fn measure(&self, text: &str, font_size: f32) -> TextMetrics
Source§fn font_metrics(&self, font_size: f32) -> FontMetrics
fn font_metrics(&self, font_size: f32) -> FontMetrics
Source§fn shape_text(&self, text: &str, query: &FontQuery) -> TextMetrics
fn shape_text(&self, text: &str, query: &FontQuery) -> TextMetrics
FontQuery (correct font family + weight).
Default delegates to measure() ignoring family/weight.Source§fn query_metrics(&self, query: &FontQuery) -> FontMetrics
fn query_metrics(&self, query: &FontQuery) -> FontMetrics
FontQuery (correct font family + weight).
Default delegates to font_metrics() ignoring family/weight.Source§fn measure_wrapped(
&self,
text: &str,
font_size: f32,
max_width: Option<f32>,
) -> WrappedTextMetrics
fn measure_wrapped( &self, text: &str, font_size: f32, max_width: Option<f32>, ) -> WrappedTextMetrics
Source§fn shape_glyphs(
&self,
text: &str,
query: &FontQuery,
color: [u8; 4],
) -> Vec<ShapedTextRun>
fn shape_glyphs( &self, text: &str, query: &FontQuery, color: [u8; 4], ) -> Vec<ShapedTextRun>
Auto Trait Implementations§
impl !Freeze for FontSystem
impl !RefUnwindSafe for FontSystem
impl Send for FontSystem
impl !Sync for FontSystem
impl Unpin for FontSystem
impl UnsafeUnpin for FontSystem
impl !UnwindSafe for FontSystem
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more