Fonts

Struct Fonts 

Source
pub struct Fonts<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> Fonts<'a>

Source

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);
Source

pub fn fonts(&self) -> &Vec<Font<'_>>

Returns an immutable reference to the list of fonts that are currently loaded

Source

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

Source

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.

Source

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

See Self::load_font_from_bytes_with_scale

Source

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

See Self::load_font_from_bytes_with_scale

Source

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

See Self::load_font_from_bytes_with_scale

Source

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

Source

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

Source

pub fn get_font_by_index(&self, index: usize) -> Option<&Font<'_>>

Gets a currently loaded font by its index

Source

pub fn get_index_by_char(&self, c: char) -> Option<usize>

Gets the first currently loaded font if it contains this character

Source

pub fn get_index_by_name(&self, name: &str) -> Option<usize>

Gets a currently loaded font index by its name

Source

pub fn get_font_by_name(&self, name: &str) -> Option<&Font<'_>>

Gets a currently loaded font by its name

Source

pub fn get_font_by_char(&self, c: char) -> Option<&Font<'_>>

Gets the first currently loaded font if it contains this character

Source

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

Source

pub fn contains(&self, c: char) -> bool

Checks if any fonts supports this character

Source

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

Source

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));

See Self::draw_text_ex

Source

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

Trait Implementations§

Source§

impl<'a> Debug for Fonts<'a>

Source§

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

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

impl<'a> Default for Fonts<'a>

Source§

fn default() -> Self

Creates a new Fonts instance to handle all your font

Same as calling [Fonts::new(ScalingMode::Linear)]

Auto Trait Implementations§

§

impl<'a> Freeze for Fonts<'a>

§

impl<'a> !RefUnwindSafe for Fonts<'a>

§

impl<'a> Send for Fonts<'a>

§

impl<'a> !Sync for Fonts<'a>

§

impl<'a> Unpin for Fonts<'a>

§

impl<'a> UnwindSafe for Fonts<'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> 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, 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.