Skip to main content

FontSystem

Struct FontSystem 

Source
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

Source

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.

Source

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 for include_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());
Source

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.

Source

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

Source

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

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Measure the advance width of a text run (no wrapping). Read more
Source§

fn font_metrics(&self, font_size: f32) -> FontMetrics

Get font metrics (ascent, descent, lineGap) for a given font size. Read more
Source§

fn shape_text(&self, text: &str, query: &FontQuery) -> TextMetrics

Measure text width using a full FontQuery (correct font family + weight). Default delegates to measure() ignoring family/weight.
Source§

fn query_metrics(&self, query: &FontQuery) -> FontMetrics

Get font metrics using a full 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

Measure text with a width constraint, performing line wrapping. Read more
Source§

fn shape_glyphs( &self, text: &str, query: &FontQuery, color: [u8; 4], ) -> Vec<ShapedTextRun>

Shape text and extract glyph runs for rendering. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> MaybeBoxed<Box<T>> for T

Source§

fn maybe_boxed(self) -> Box<T>

Convert
Source§

impl<T> MaybeBoxed<T> for T

Source§

fn maybe_boxed(self) -> T

Convert
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,