Skip to main content

RhythmFontSpec

Struct RhythmFontSpec 

Source
pub struct RhythmFontSpec { /* private fields */ }
Available on crate feature gpui only.
Expand description

The pre-resolve identity of a RhythmFont: the requested Font, size, line rhythms, and grid size as one hashable value — the cache key for caller-owned typography catalogs.

The crate deliberately keeps no font cache (gpui already caches Font → FontId and metrics); an app reusing resolved values across a document owns the map and its invalidation. Register font families before resolving any spec for them: gpui caches failed lookups, so clearing this caller-owned map after late registration cannot repair an earlier miss. Rebuild the map when typography settings change:

use std::collections::HashMap;

use gpui::{font, px, TextSystem};
use rhythm_gpui::{RhythmFont, RhythmFontSpec, RhythmGrid};

fn body(cache: &mut HashMap<RhythmFontSpec, RhythmFont>, ts: &TextSystem) -> RhythmFont {
    let spec = RhythmFontSpec::new(font("Noto Serif"), px(16.), 3, RhythmGrid::new(px(8.)));
    cache
        .entry(spec.clone())
        .or_insert_with(|| spec.resolve(ts))
        .clone()
}

Implementations§

Source§

impl RhythmFontSpec

Source

pub fn new( font: Font, font_size: Pixels, line_rhythms: u32, grid: RhythmGrid, ) -> Self

The spec for resolving font at font_size on grid with a line_rhythms-unit line height.

§Panics

Panics when the font weight is zero, negative, or non-finite, when font_size is zero, negative, or non-finite, or when line_rhythms is zero.

Source

pub fn font(&self) -> &Font

The requested gpui font configuration.

Source

pub const fn font_size(&self) -> Pixels

The font size the metrics will be resolved at.

Source

pub const fn line_rhythms(&self) -> u32

Line height in whole rhythm units.

Source

pub fn grid(&self) -> RhythmGrid

The grid the font will be bound to.

Source

pub fn resolve(&self, text_system: &TextSystem) -> RhythmFont

Resolve the spec into a RhythmFontRhythmFont::resolve with this identity; see it for the fallback-resolution caveats.

Source

pub fn resolve_covering( &self, text_system: &TextSystem, others: &[RhythmFontSpec], ) -> RhythmFont

Resolve this spec at a line height covering the ascent/descent envelope of every font in others — the catalog-build step that fixes one row budget over an explicit face set rather than per shaped line.

A line shapes to the maxima over its explicit font runs, so a style needs a line height covering the tallest mixture of every face the caller knows its runs can explicitly select: bold, inline code, or an explicit CJK or emoji face. others is that closed, caller-supplied catalog. Each listed Font is resolved through gpui — including the family fallback gpui chooses when that request is missing — but this method neither inspects text nor discovers glyph-level fallback faces selected later by the platform shaper.

gpui shapes all TextRuns in a line at one font size, so every spec must use this spec’s font_size and grid. Compatibility is checked before any font is resolved, then the resolved metrics are folded with RhythmLineMetrics::covering. The result is this spec’s font at the covering count — metrics, cap height, and baselines stay the primary face’s, only the line height grows. Nothing is shaped, so the count is a startup constant and every block’s height follows from its line count, which is what a virtualized renderer needs.

Keep placing each shaped line with its own ascent/descent at this font’s line_rhythms; with the height settled here, no explicit-run mixture of the covered set outgrows it. Resolve the run faces at that same count when their own metrics are used for placement — spec on the returned font carries it, and reproduces these metrics as usual.

use gpui::{font, px, FontWeight, TextSystem};
use rhythm_gpui::{RhythmFontSpec, RhythmGrid};

fn body(text_system: &TextSystem) -> rhythm_gpui::RhythmFont {
    let grid = RhythmGrid::new(px(8.));
    let mut bold = font("Georgia");
    bold.weight = FontWeight::BOLD;
    RhythmFontSpec::new(font("Georgia"), px(16.), 3, grid).resolve_covering(
        text_system,
        &[
            RhythmFontSpec::new(bold, px(16.), 3, grid),
            RhythmFontSpec::new(font("Menlo"), px(16.), 3, grid),
            RhythmFontSpec::new(font("Apple Color Emoji"), px(16.), 3, grid),
        ],
    )
}
§Panics

Panics when a spec in others was built with a different font size or grid size.

Trait Implementations§

Source§

impl Clone for RhythmFontSpec

Source§

fn clone(&self) -> RhythmFontSpec

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RhythmFontSpec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for RhythmFontSpec

Source§

impl Hash for RhythmFontSpec

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for RhythmFontSpec

Source§

fn eq(&self, other: &RhythmFontSpec) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for RhythmFontSpec

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more