Skip to main content

FontChain

Struct FontChain 

Source
pub struct FontChain<'a> { /* private fields */ }
Expand description

The glyph source a backend draws from: a primary BitmapFont plus an ordered list of fallback fonts.

This is the only character-to-glyph path the bundled pixel backends have. A single font is a chain of one (FontChain::from(font)), so SoftwareBackendBuilder::font and GlBackendBuilder::font both take an impl Into<FontChain<'static>> and there is no second, chain-blind route that could quietly ignore a font’s declared repertoire.

resolve tries the primary font first, then each fallback in order, and only if every font misses substitutes the solid block ('█') from the first font in the chain that has one. This lets a caller layer, say, an ASCII or partial-coverage primary font with one or more broader fallback fonts, so a char missing from the primary doesn’t automatically become a solid block if some other font in the chain actually has it.

This type ships no bundled fallback font data: every font in the chain, primary or fallback, is supplied by the caller. Bundling a ready-to-use Latin-1/Extended or sub-cell (quadrant/sextant/braille) fallback font is a natural follow-up now that this mechanism is reachable end to end, but is out of scope here.

A fallback font only extends the chain’s repertoire if it declares coverage for the characters it is meant to answer for. A BitmapFont::new font is always resolved through the built-in CP437 table, so stacking several CP437 fonts in a chain never reaches past CP437: every font in the chain answers the identical question. To actually extend coverage (e.g. quadrants, sextants, braille, none of which CP437 has a mapping for), build the fallback font with BitmapFont::with_charset and an explicit table covering those codepoints. Until a chain does, retroglyph_core::symbols‘s quantize_quadrant/quantize_sextant glyphs render as a solid block on the pixel backends; see those functions’ docs.

§Examples

use retroglyph_window::font::{BitmapFont, FontChain};

static ASCII: [u8; 128 * 16] = [0; 128 * 16];
static QUADRANTS: [u8; 3 * 16] = [0; 3 * 16];
const QUADRANT_CHARSET: [(char, u8); 3] = [('▘', 0), ('▝', 1), ('▖', 2)];

const PRIMARY: BitmapFont = BitmapFont::new(&ASCII, 8, 16, 128);
const SUBCELL: BitmapFont = BitmapFont::with_charset(&QUADRANTS, 8, 16, 3, &QUADRANT_CHARSET);
static FALLBACKS: [BitmapFont; 1] = [SUBCELL];

let chain = FontChain::new(PRIMARY, &FALLBACKS);
let quadrant = chain.resolve('▘').expect("covered by the fallback font");
assert_eq!(quadrant.font_index(), 1);
assert!(!quadrant.is_notdef());

Implementations§

Source§

impl<'a> FontChain<'a>

Source

pub const fn new( primary: BitmapFont, fallbacks: &'a [BitmapFont], ) -> FontChain<'a>

Constructs a chain from a primary font and an ordered list of fallback fonts.

Source

pub fn fonts(&self) -> impl Iterator<Item = &BitmapFont>

The fonts in resolution order: the primary font first, then each fallback.

The position of a font in this iterator is its ResolvedGlyph::font_index.

Source

pub const fn font_count(&self) -> usize

The number of fonts in the chain (always at least one).

Source

pub fn glyph_size(&self) -> Option<(u8, u8)>

The glyph cell size ((width, height) in unscaled pixels) shared by every font in the chain, or None if the fonts disagree.

A grid has one cell size, so a chain whose fonts don’t agree on theirs has no single answer for how big a cell is; backends reject such a chain at build time rather than picking one font’s size and letting the others overflow or under-fill their cells.

Source

pub fn resolve(&self, ch: char) -> Option<ResolvedGlyph>

Resolves ch to a drawable glyph, trying the primary font first, then each fallback font in order.

If no font covers ch, this substitutes the solid block ('█') from the first font in the chain that covers it, flagged as ResolvedGlyph::is_notdef. None means the chain cannot draw ch at all, not even a substitute box, and the caller should draw nothing: a chain of narrow with_charset fonts (say, braille only) legitimately has no solid block to fall back to.

A returned glyph is always in range for its font, so ResolvedGlyph::rows and BitmapFont::glyph_pixels cannot panic on it.

Trait Implementations§

Source§

impl<'a> Clone for FontChain<'a>

Source§

fn clone(&self) -> FontChain<'a>

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<'a> Debug for FontChain<'a>

Source§

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

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

impl From<BitmapFont> for FontChain<'static>

Source§

fn from(font: BitmapFont) -> FontChain<'static>

Converts to this type from the input type.
Source§

impl<'a> PartialEq for FontChain<'a>

Source§

fn eq(&self, other: &FontChain<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Copy for FontChain<'a>

Source§

impl<'a> Eq for FontChain<'a>

Source§

impl<'a> StructuralPartialEq for FontChain<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for FontChain<'a>

§

impl<'a> RefUnwindSafe for FontChain<'a>

§

impl<'a> Send for FontChain<'a>

§

impl<'a> Sync for FontChain<'a>

§

impl<'a> Unpin for FontChain<'a>

§

impl<'a> UnsafeUnpin for FontChain<'a>

§

impl<'a> UnwindSafe for FontChain<'a>

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

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

§

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.
§

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.
§

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.
§

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.
§

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

§

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

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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> 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.
§

impl<C> WithAlpha for C
where C: Copy,

§

fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>

Wraps self with alpha, storing alpha before the color in memory (see [AlphaFirst]).
§

fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>

Wraps self with alpha, storing alpha after the color in memory (see [AlphaLast]).
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

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

§

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

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,