pub struct Fonts<'a> { /* private fields */ }Implementations§
Source§impl<'a> Fonts<'a>
impl<'a> Fonts<'a>
Sourcepub fn new(default_sm: ScalingMode) -> Self
pub fn new(default_sm: ScalingMode) -> Self
Creates a new Fonts instance to handle all your fonts with a given ScalingMode
You can also call Fonts::default which defaults to ScalingMode::Linear
Examples
With nearest mode
let mut fonts = Fonts::new(ScalingMode::Nearest);With linear mode
let mut fonts = Fonts::new(ScalingMode::Linear);Sourcepub fn fonts(&self) -> &Vec<Font<'_>>
pub fn fonts(&self) -> &Vec<Font<'_>>
Returns an immutable reference to the list of fonts that are currently loaded
Sourcepub fn cache_glyph(&self, c: char, size: u16)
pub fn cache_glyph(&self, c: char, size: u16)
Caches a glyph for a given character with a given font size
You don’t really need to call this function since caching happens automatically
Sourcepub fn load_font_from_bytes_with_scale(
&mut self,
name: &'a str,
bytes: &[u8],
scale: f32,
) -> FontResult<()>
pub fn load_font_from_bytes_with_scale( &mut self, name: &'a str, bytes: &[u8], scale: f32, ) -> FontResult<()>
Loads font from bytes with a given name and scale
§What Scale does
(copied from FontSettings::scale)
The scale in px the font geometry is optimized for. Fonts rendered at the scale defined here will be the most optimal in terms of looks and performance. Glyphs rendered smaller than this scale will look the same but perform slightly worse, while glyphs rendered larger than this will looks worse but perform slightly better. The units of the scale are pixels per Em unit.
Sourcepub fn load_font_from_bytes(
&mut self,
name: &'a str,
bytes: &[u8],
) -> FontResult<()>
pub fn load_font_from_bytes( &mut self, name: &'a str, bytes: &[u8], ) -> FontResult<()>
Loads font from bytes with a given name and a default scale of 100.0
Sourcepub fn load_font_from_file(
&mut self,
name: &'a str,
path: impl AsRef<Path>,
) -> IoResult<()>
pub fn load_font_from_file( &mut self, name: &'a str, path: impl AsRef<Path>, ) -> IoResult<()>
Loads font from a file with a given name and path and a default scale of 100.0
Sourcepub fn load_font_from_file_with_scale(
&mut self,
name: &'a str,
path: impl AsRef<Path>,
scale: f32,
) -> IoResult<()>
pub fn load_font_from_file_with_scale( &mut self, name: &'a str, path: impl AsRef<Path>, scale: f32, ) -> IoResult<()>
Loads font from a file with a given name, path and scale
Sourcepub fn unload_font_by_index(&mut self, index: usize)
pub fn unload_font_by_index(&mut self, index: usize)
Unloads a currently loaded font by its index
This will also re-index all the currently loaded fonts
Sourcepub fn unload_font_by_name(&mut self, name: &str)
pub fn unload_font_by_name(&mut self, name: &str)
Unloads a currently loaded font by it name
This will also re-index all the currently loaded fonts
Sourcepub fn get_font_by_index(&self, index: usize) -> Option<&Font<'_>>
pub fn get_font_by_index(&self, index: usize) -> Option<&Font<'_>>
Gets a currently loaded font by its index
Sourcepub fn get_index_by_char(&self, c: char) -> Option<usize>
pub fn get_index_by_char(&self, c: char) -> Option<usize>
Gets the first currently loaded font if it contains this character
Sourcepub fn get_index_by_name(&self, name: &str) -> Option<usize>
pub fn get_index_by_name(&self, name: &str) -> Option<usize>
Gets a currently loaded font index by its name
Sourcepub fn get_font_by_name(&self, name: &str) -> Option<&Font<'_>>
pub fn get_font_by_name(&self, name: &str) -> Option<&Font<'_>>
Gets a currently loaded font by its name
Sourcepub fn get_font_by_char(&self, c: char) -> Option<&Font<'_>>
pub fn get_font_by_char(&self, c: char) -> Option<&Font<'_>>
Gets the first currently loaded font if it contains this character
Sourcepub fn get_font_by_char_or_panic(&self, c: char) -> &Font<'_>
pub fn get_font_by_char_or_panic(&self, c: char) -> &Font<'_>
Gets the first currently loaded font if it contains this character, if no font that contains this character is found, it will return the first loaded font, if no fonts are loaded then it will panic
Sourcepub fn measure_text(&self, text: &str, size: u16) -> TextDimensions
pub fn measure_text(&self, text: &str, size: u16) -> TextDimensions
Measures text with a given font size
Example
let dimensions = fonts.measure_text("Some Text", 22);
println!("width: {}, height: {}, offset_y: {}",
dimensions.width,
dimensions.height,
dimensions.offset_y
)See TextDimensions
Sourcepub fn draw_text(
&self,
text: &str,
x: f32,
y: f32,
size: u16,
color: Color,
) -> TextDimensions
pub fn draw_text( &self, text: &str, x: f32, y: f32, size: u16, color: Color, ) -> TextDimensions
Draws text with a given font size, draws from TopLeft
Examples
fonts.draw_text("Some Text", 20.0, 20.0, 22, Color::from_rgba(255, 255, 255, 255));Sourcepub fn draw_text_ex(&self, params: &TextParams<'_>) -> TextDimensions
pub fn draw_text_ex(&self, params: &TextParams<'_>) -> TextDimensions
Draws text with given TextParams
Example
fonts.draw_text_ex(&TextParams {
text: "Some Text",
x: 20.0,
y: 20.0,
// Default Size
size: 22,
// Default Color
color: Color::from_rgba(255, 255, 255, 255),
// Default Draw method
draw: DrawFrom::TopLeft
});
// Does the same as above
fonts.draw_text_ex(&TextParams {
text: "Some Text",
x: 20.0,
y: 20.0,
..Default::default()
});See Self::draw_text