graphics/
character.rs

1//! A text character
2
3use crate::{
4    math,
5    types::{FontSize, Scalar},
6    ImageSize,
7};
8
9/// Holds rendered character data.
10#[derive(Clone)]
11pub struct Character<'a, T: ImageSize> {
12    /// The offset of character.
13    pub offset: [Scalar; 2],
14    /// The advance size of character, including space.
15    pub advance_size: [Scalar; 2],
16    /// The offset of character within texture atlas.
17    pub atlas_offset: [Scalar; 2],
18    /// The size of character within texture atlas.
19    pub atlas_size: [Scalar; 2],
20    /// The texture of the character.
21    pub texture: &'a T,
22    /// if this is an "invalid character" character
23    pub is_invalid: bool
24}
25
26impl<'a, T: ImageSize> Character<'a, T> {
27    /// The left offset.
28    pub fn left(&self) -> Scalar {
29        self.offset[0]
30    }
31
32    /// The top offset.
33    pub fn top(&self) -> Scalar {
34        self.offset[1]
35    }
36
37    /// Gets width of character, including space to the next one.
38    pub fn advance_width(&self) -> Scalar {
39        self.advance_size[0]
40    }
41
42    /// Sets height of character, including space to the next one.
43    pub fn advance_height(&self) -> Scalar {
44        self.advance_size[1]
45    }
46}
47
48/// Stores characters in a buffer and loads them by demand.
49pub trait CharacterCache {
50    /// The texture type associated with the character cache.
51    type Texture: ImageSize;
52    /// The error type associated with the character cache.
53    type Error: core::fmt::Debug;
54
55    /// Get reference to character.
56    fn character(
57        &mut self,
58        font_size: FontSize,
59        ch: char,
60    ) -> Result<Character<'_, Self::Texture>, Self::Error>;
61
62    /// Return the width for some given text.
63    fn width(&mut self, size: FontSize, text: &str) -> Result<math::Scalar, Self::Error> {
64        let mut width = 0.0;
65        for ch in text.chars() {
66            let character = self.character(size, ch)?;
67            width += character.advance_width();
68        }
69        Ok(width)
70    }
71}