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>
impl<T: Texture> FontCache<T>
Sourcepub fn new(font: Box<dyn FontProvider>, texture: T) -> Self
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?
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}Sourcepub fn clear(&mut self)
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.
Sourcepub fn render_glyph(
&mut self,
key: Glyph,
) -> Result<(Metrics, TextureGlyph), CacheError>
pub fn render_glyph( &mut self, key: Glyph, ) -> Result<(Metrics, TextureGlyph), CacheError>
Render a glyph to the texture
Sourcepub fn render_string<'a>(
&'a mut self,
string: &str,
) -> impl 'a + Iterator<Item = Result<(Metrics, TextureGlyph), CacheError>>
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?
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}Sourcepub fn cache_string(&mut self, string: &str) -> Result<(), CacheError>
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.
Sourcepub fn replace_texture(&mut self, texture: T) -> T
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
Sourcepub fn texture(&self) -> &T
pub fn texture(&self) -> &T
Examples found in repository?
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}