Skip to main content

FontCache

Struct FontCache 

Source
pub struct FontCache<T: Texture> { /* private fields */ }
Expand description

The main structure for maintaing a cache of rendered glyphs

FontCache is specifically an intermediary step. It doesn’t understand how to read font files or how to break up a string into glyphs: that’s handled by the FontProvider. It doesn’t handle sending glyphs to the GPU: if you want to do that, provide a Texture that stores its data on the GPU. What it does do is keep track of which glyphs have already been rendered, where they were stored, and provide a consistent API over a variety of ways of rendering characters.

Implementations§

Source§

impl<T: Texture> FontCache<T>

Source

pub fn new(font: Box<dyn FontProvider>, texture: T) -> Self

Create a new FontCache that pulls from the given provider and renders to the provided texture

Examples found in repository?
examples/render_image.rs (line 13)
6fn main() {
7    let font_data = include_bytes!("DejaVuSans.ttf");
8    let font = Font::from_bytes(font_data as &[u8]).expect("Error constructing Font");
9    let font = SizedFont::new(font, 24.0);
10
11    let image = ImageBuffer::new(200, 200);
12
13    let mut cache = FontCache::new(Box::new(font), image);
14    cache.render_string("Hello, world!").for_each(|r| {
15        r.unwrap();
16    });
17    cache.render_string("こんにちは世界!").for_each(|r| {
18        r.unwrap();
19    });
20    cache.render_string("Привет, мир!").for_each(|r| {
21        r.unwrap();
22    });
23    cache
24        .texture()
25        .save("result.png")
26        .expect("Failed to save file");
27}
Source

pub fn clear(&mut self)

Forget the position of the characters in the texture, and re-set the cursor.

This doesn’t set any data in the Texture! Old glyphs may continue to work, but this is akin to a use-after-free.

Source

pub fn render_glyph( &mut self, key: Glyph, ) -> Result<(Metrics, TextureGlyph), CacheError>

Render a glyph to the texture

Source

pub fn render_string<'a>( &'a mut self, string: &str, ) -> impl 'a + Iterator<Item = Result<(Metrics, TextureGlyph), CacheError>>

Attempt to convert a string into a series of glyphs or errors

Before being converted, the string is normalized if the “unicode-normalilzation” feature is activated, and whitespace characters are removed.

Examples found in repository?
examples/render_image.rs (line 14)
6fn main() {
7    let font_data = include_bytes!("DejaVuSans.ttf");
8    let font = Font::from_bytes(font_data as &[u8]).expect("Error constructing Font");
9    let font = SizedFont::new(font, 24.0);
10
11    let image = ImageBuffer::new(200, 200);
12
13    let mut cache = FontCache::new(Box::new(font), image);
14    cache.render_string("Hello, world!").for_each(|r| {
15        r.unwrap();
16    });
17    cache.render_string("こんにちは世界!").for_each(|r| {
18        r.unwrap();
19    });
20    cache.render_string("Привет, мир!").for_each(|r| {
21        r.unwrap();
22    });
23    cache
24        .texture()
25        .save("result.png")
26        .expect("Failed to save file");
27}
Source

pub fn cache_string(&mut self, string: &str) -> Result<(), CacheError>

Cache a string or return an error if one occurred

This can be useful if the entire domain of the possible glyphs is known beforehand (like a bitmap font.) Under the hood, this just calls [render_string] and ignores the returned glyphs.

Source

pub fn replace_texture(&mut self, texture: T) -> T

Swap out the internal texture for another one

This will clear the cache automatically, to avoid holding references to invalid areas of the texture

Source

pub fn texture(&self) -> &T

Examples found in repository?
examples/render_image.rs (line 24)
6fn main() {
7    let font_data = include_bytes!("DejaVuSans.ttf");
8    let font = Font::from_bytes(font_data as &[u8]).expect("Error constructing Font");
9    let font = SizedFont::new(font, 24.0);
10
11    let image = ImageBuffer::new(200, 200);
12
13    let mut cache = FontCache::new(Box::new(font), image);
14    cache.render_string("Hello, world!").for_each(|r| {
15        r.unwrap();
16    });
17    cache.render_string("こんにちは世界!").for_each(|r| {
18        r.unwrap();
19    });
20    cache.render_string("Привет, мир!").for_each(|r| {
21        r.unwrap();
22    });
23    cache
24        .texture()
25        .save("result.png")
26        .expect("Failed to save file");
27}
Source

pub fn font(&self) -> &dyn FontProvider

Auto Trait Implementations§

§

impl<T> Freeze for FontCache<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for FontCache<T>

§

impl<T> !Send for FontCache<T>

§

impl<T> !Sync for FontCache<T>

§

impl<T> Unpin for FontCache<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for FontCache<T>
where T: UnsafeUnpin,

§

impl<T> !UnwindSafe for FontCache<T>

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.