Skip to main content

Font

Struct Font 

Source
pub struct Font { /* private fields */ }
Expand description

A bitmap font the cook made from a BMFont: its metrics, its glyphs by character, its kerning pairs, and the glyph a character it lacks draws. Each glyph is a frame of a sprite of an atlas, which Font::atlas and Font::sprite name.

It needs no GPU, so a game can make it before its window opens, and it lays text out on its own (Font::layout, Font::measure). To be drawn, its atlas and the atlas’s pages must be loaded too: Font::load_now_in loads all three. It has no placeholder.

Implementations§

Source§

impl Font

Source

pub fn load_now_in( server: &mut AssetServer, path: &str, scope: Scope, ctx: &mut FinalizeCtx<'_>, ) -> Result<Handle<Font>, AssetError>

Loads the font file at path, then the atlas file it names, at once, and starts loading the atlas’s pages, all in scope: what a font needs to be drawn. It blocks until the two files are read, as AssetServer::load_now_in does; the pages, textures, are made on the GPU by a later finish.

server.load::<Font>(path) loads the font file alone: its texts are then not drawn until its atlas and pages are loaded too.

§Errors

Any AssetError of the font or its atlas, and AssetError::Invalid when the atlas has no sprite of the font’s name, or that sprite has fewer frames than the font’s glyphs name.

Source

pub fn face(&self) -> &str

Its face’s name, as BMFont gave it: it may be empty.

Source

pub fn size(&self) -> i32

Its size, as BMFont gave it: a negative size is BMFont’s “match the characters’ height”.

Source

pub fn line_height(&self) -> u32

The pixels from one line’s top to the next’s.

Source

pub fn base(&self) -> u32

The pixels from a line’s top to its baseline.

Source

pub fn atlas(&self) -> &str

The path of its atlas file, such as atlas/fonts.kbj, among the cooked files.

Source

pub fn sprite(&self) -> &str

The name of the sprite of its atlas whose frames are its glyphs, such as fonts/ui.

Source

pub fn glyph(&self, character: char) -> Option<Glyph>

The glyph of character, if the font has one.

Source

pub fn fallback(&self) -> Glyph

The glyph a character the font lacks draws: BMFont’s fallback glyph, if the font has one, else its ?. It has a frame.

Source

pub fn kerning(&self, first: char, second: char) -> i32

The pixels added to first’s advance when second follows it: 0 for a pair the font does not kern.

Source§

impl Font

Source

pub fn measure(&self, text: &str, layout: &TextLayout) -> UVec2

The size of text’s block as layout lays it out, in the font’s pixels: as wide as its widest line, or layout.max_width if that is wider, and a line height for each line. A text has one line, and one more for each \n and each wrap: "" is one line of width 0.

The block is the pen’s: a glyph may reach past it by its offsets.

Source

pub fn layout( &self, text: &str, layout: &TextLayout, out: &mut Vec<PlacedGlyph>, ) -> UVec2

Lays text out as layout says, appending a PlacedGlyph to out for each character drawn, spaces too, in the text’s order, and returns the block’s size, as Font::measure does.

  • \n breaks a line, and a \r takes no room. A control character the font has no glyph for takes no room either, and places nothing.
  • The pen starts each line at 0. Each glyph’s rectangle is placed at the pen plus its Glyph::offset, and the pen then moves by its advance. Before a glyph, the pen moves by the kerning of the glyph before it on the line and this one (Font::kerning).
  • A character the font lacks draws its fallback, with no kerning on either side, and is missing.
  • A line’s width is the pen after its last character that is not a space, at least 0: spaces at a line’s end take no room.
  • With markup, a tag places nothing, takes no room, and kerns through: A{c:#f00}V kerns as AV. A run lasts across lines.

Trait Implementations§

Source§

impl Asset for Font

Source§

const EXTENSIONS: &'static [&'static str]

The extensions, without the dot, that a path of this type ends in.
Source§

type Decoded = Font

What Asset::decode makes.
Source§

fn decode(bytes: &[u8], ctx: &DecodeCtx<'_>) -> Result<Font, AssetError>

Decodes a file’s bytes. Read more
Source§

fn finalize(decoded: Font, _: &mut FinalizeCtx<'_>) -> Result<Self, AssetError>

Makes the asset from what Asset::decode made. Read more
Source§

fn placeholder(ctx: &mut FinalizeCtx<'_>) -> Result<Self, AssetError>

Makes the stand-in for an asset that failed to load, when the server shows placeholders (OnFailure::Placeholder). None by default. Read more
Source§

fn release(self, ctx: &mut FinalizeCtx<'_>) -> Result<(), AssetError>

Frees what the asset holds outside the server, such as its GPU texture, once every scope it was loaded in is unloaded. The next AssetServer::finish_completed_loads calls it, with that frame’s context; a placeholder is freed the same way. A reload’s finish calls it too, on the value, or the placeholder, that the reload replaces, while the handle stays and names the new value. By default the asset is only dropped. Read more
Source§

impl Clone for Font

Source§

fn clone(&self) -> Self

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 Font

Source§

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

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

impl PartialEq for Font

Source§

fn eq(&self, other: &Self) -> 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 Font

Auto Trait Implementations§

§

impl Forget for Font

§

impl Freeze for Font

§

impl Move for Font

§

impl RefUnwindSafe for Font

§

impl Send for Font

§

impl Sync for Font

§

impl Unpin for Font

§

impl UnsafeUnpin for Font

§

impl UnwindSafe for Font

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
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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.