Struct Font

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

A single font loaded from a file.

Implementations§

Source§

impl Font

Source

pub fn new(rusttype_font: Font<'static>) -> Font

Construct a new Font from a rusttype::Font.

Source

pub fn render_char(&self, chr: char, scale: f32) -> Option<(CharInfo, Bitmap)>

Renders a character from this font at a given scale into a pair of (CharInfo, Bitmap).

If the character isn’t handled by the font, None is returned.

Examples found in repository?
examples/basic_char.rs (line 7)
4fn main() {
5    let bytes = include_bytes!("Gudea-Regular.ttf");
6    let font = load_font_from_bytes(bytes.to_vec());
7    let (_, bitmap) = font.render_char('A', 40.0).unwrap();
8    for line in bitmap.lines() {
9        for &pixel in line {
10            if pixel == 0 {
11                print!(" ");
12            } else {
13                print!("#");
14            }
15        }
16        println!("");
17    }
18}
Source

pub fn make_atlas<I: Iterator<Item = char>>( &self, i: I, scale: f32, margin: u32, width: usize, height: usize, ) -> (Atlas, Bitmap, f32)

Creates an atlas for a set of characters rendered at a given scale.

margin is the distance between characters in pixels. width and height denote the starting size of the bitmap.

The resulting bitmap may be larger than width x height in order to fit all of the characters.

Examples found in repository?
examples/basic_atlas.rs (line 8)
4fn main() {
5    let bytes = include_bytes!("Gudea-Regular.ttf");
6    let font = load_font_from_bytes(bytes.to_vec());
7    let chars = font_atlas::ASCII.iter().cloned().chain(font_atlas::ASCII.iter().cloned());
8    let (_, bitmap, _) = font.make_atlas(chars, 20.0, 1, 128, 128);
9    for line in bitmap.lines() {
10        print!("{:03} ", line.len());
11        for &pixel in line {
12            if pixel == 0 {
13                print!(" ");
14            } else {
15                print!("#");
16            }
17        }
18        println!("");
19    }
20}

Trait Implementations§

Source§

impl Clone for Font

Source§

fn clone(&self) -> Font

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Font

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Font

§

impl RefUnwindSafe for Font

§

impl Send for Font

§

impl Sync for Font

§

impl Unpin 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 = 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.