1use crate::{
4 math,
5 types::{FontSize, Scalar},
6 ImageSize,
7};
8
9#[derive(Clone)]
11pub struct Character<'a, T: ImageSize> {
12 pub offset: [Scalar; 2],
14 pub advance_size: [Scalar; 2],
16 pub atlas_offset: [Scalar; 2],
18 pub atlas_size: [Scalar; 2],
20 pub texture: &'a T,
22 pub is_invalid: bool
24}
25
26impl<'a, T: ImageSize> Character<'a, T> {
27 pub fn left(&self) -> Scalar {
29 self.offset[0]
30 }
31
32 pub fn top(&self) -> Scalar {
34 self.offset[1]
35 }
36
37 pub fn advance_width(&self) -> Scalar {
39 self.advance_size[0]
40 }
41
42 pub fn advance_height(&self) -> Scalar {
44 self.advance_size[1]
45 }
46}
47
48pub trait CharacterCache {
50 type Texture: ImageSize;
52 type Error: core::fmt::Debug;
54
55 fn character(
57 &mut self,
58 font_size: FontSize,
59 ch: char,
60 ) -> Result<Character<'_, Self::Texture>, Self::Error>;
61
62 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}