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
impl Font
Sourcepub fn load_now_in(
server: &mut AssetServer,
path: &str,
scope: Scope,
ctx: &mut FinalizeCtx<'_>,
) -> Result<Handle<Font>, AssetError>
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.
Sourcepub fn size(&self) -> i32
pub fn size(&self) -> i32
Its size, as BMFont gave it: a negative size is BMFont’s “match the characters’ height”.
Sourcepub fn line_height(&self) -> u32
pub fn line_height(&self) -> u32
The pixels from one line’s top to the next’s.
Sourcepub fn atlas(&self) -> &str
pub fn atlas(&self) -> &str
The path of its atlas file, such as atlas/fonts.kbj, among the
cooked files.
Sourcepub fn sprite(&self) -> &str
pub fn sprite(&self) -> &str
The name of the sprite of its atlas whose frames are its glyphs, such
as fonts/ui.
Sourcepub fn glyph(&self, character: char) -> Option<Glyph>
pub fn glyph(&self, character: char) -> Option<Glyph>
The glyph of character, if the font has one.
Source§impl Font
impl Font
Sourcepub fn measure(&self, text: &str, layout: &TextLayout) -> UVec2
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.
Sourcepub fn layout(
&self,
text: &str,
layout: &TextLayout,
out: &mut Vec<PlacedGlyph>,
) -> UVec2
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.
\nbreaks a line, and a\rtakes 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}Vkerns asAV. A run lasts across lines.
Trait Implementations§
Source§impl Asset for Font
impl Asset for Font
Source§const EXTENSIONS: &'static [&'static str]
const EXTENSIONS: &'static [&'static str]
Source§type Decoded = Font
type Decoded = Font
Asset::decode makes.Source§fn decode(bytes: &[u8], ctx: &DecodeCtx<'_>) -> Result<Font, AssetError>
fn decode(bytes: &[u8], ctx: &DecodeCtx<'_>) -> Result<Font, AssetError>
Source§fn finalize(decoded: Font, _: &mut FinalizeCtx<'_>) -> Result<Self, AssetError>
fn finalize(decoded: Font, _: &mut FinalizeCtx<'_>) -> Result<Self, AssetError>
Asset::decode made. Read moreSource§fn placeholder(ctx: &mut FinalizeCtx<'_>) -> Result<Self, AssetError>
fn placeholder(ctx: &mut FinalizeCtx<'_>) -> Result<Self, AssetError>
OnFailure::Placeholder).
None by default. Read moreSource§fn release(self, ctx: &mut FinalizeCtx<'_>) -> Result<(), AssetError>
fn release(self, ctx: &mut FinalizeCtx<'_>) -> Result<(), AssetError>
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