aboutsummaryrefslogtreecommitdiffstats
path: root/src/font.rs
blob: 7df05456f0ab07b9c8c78c389ba83f14a3ec1721 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
pub const UWU_HEIGHT: u32 = 24;
pub const UWU_WIDTH: u32 = 16;

use std::sync::LazyLock;

pub struct Glyph {
  x: i32,
}
fn insert_value(arr: &mut Vec<*const Glyph>, idx: usize, glyph: *const Glyph) {
  if arr.len() <= idx {
    while arr.len() <= idx {
      arr.push(0 as *const Glyph);
    }
  }
  arr[idx] = glyph;
}

pub struct RenderableCharacter {
  /** The width of the character, indicating where to break into a newline */
  pub width: u16,
  /** The height of the character, derived from data's length divided by width */
  pub height: u16,
  /** The raw alpha layer of the character */
  pub data: &'static [u8],
}
/**
  A trait describing a generated font. 
 */
pub trait BakedFont {
  fn get_char_bytes(character: char) -> &'static [u8];
  fn get_char(character: char) -> RenderableCharacter {
    let bytes = Self::get_char_bytes(character);
    let width = (bytes[0] as u16) | (bytes[1] as u16 >> 8);
    let data = &bytes[2..];
    RenderableCharacter {
      width,
      height: data.len() as u16 / width,
      data,
    }
  }
}
pub struct Font<'a> {
  pub texture: &'a [u8],
  pub glyphs: Vec<*const Glyph>,
}

pub const MEOW_FONT: LazyLock<Font> = LazyLock::new(|| Font {
  texture: include_bytes!("../assets/fonts/uwu-logo-20px.png"),
  glyphs: {
    let a: &mut Vec<*const Glyph> = &mut Vec::new();
    insert_value(a, 32, &Glyph { x: 2 });
    a.to_vec()
  },
});

mod sprite_sheets {
  use super::{MEOW_FONT, UWU_HEIGHT, UWU_WIDTH};
  use sdl2::{
    // image::LoadTexture,
    pixels::PixelFormatEnum,
    rect::Rect,
    render::{Canvas, TextureCreator},
    surface::Surface,
    video::WindowContext,
  };

  pub struct UwUFont<'a> {
    sheet: Surface<'a>,
  }

  fn uwu_get_n(n: u32) -> Rect {
    Rect::new((UWU_WIDTH * n) as i32, 0, UWU_WIDTH, UWU_HEIGHT)
  }

  #[inline]
  fn uwu_u() -> Rect {
    return uwu_get_n(0);
  }

  #[inline]
  fn uwu_w() -> Rect {
    return uwu_get_n(1);
  }

  #[inline]
  fn uwu_s() -> Rect {
    return uwu_get_n(2);
  }

  #[inline]
  fn uwu_p() -> Rect {
    return uwu_get_n(3);
  }

  #[inline]
  fn uwu_a() -> Rect {
    return uwu_get_n(4);
  }

  #[inline]
  fn uwu_c() -> Rect {
    return uwu_get_n(5);
  }

  #[inline]
  fn uwu_e() -> Rect {
    return uwu_get_n(6);
  }

  impl<'a> UwUFont<'a> {
    pub fn new(texture_creator: &'a TextureCreator<WindowContext>) -> UwUFont<'a> {
      let surface = Surface::new(112, 24, PixelFormatEnum::RGB24).unwrap();
      let mut canvas = Canvas::from_surface(surface).unwrap();
      let tc = canvas.texture_creator();
      // let tx = tc.load_texture_bytes(MEOW_FONT.texture).unwrap();
      // canvas.copy(&tx, None, None).unwrap();
      canvas.present();
      let sheet = canvas.into_surface();

      UwUFont { sheet }
    }

    pub fn pos_to_rect(x: i32, y: i32) -> Rect {
      return Rect::new(x, y, UWU_WIDTH, UWU_HEIGHT);
    }

    pub fn char_to_surface(&self, char: Rect) -> Surface {
      let mut char_surface = Surface::new(UWU_WIDTH, UWU_HEIGHT, PixelFormatEnum::RGB24).unwrap();
      self
        .sheet
        .blit(char, &mut char_surface, Self::pos_to_rect(0, 0))
        .unwrap();
      char_surface
    }

    pub fn u_surface(&self) -> Surface {
      return self.char_to_surface(uwu_u());
    }

    pub fn w_surface(&self) -> Surface {
      return self.char_to_surface(uwu_w());
    }

    pub fn p_surface(&self) -> Surface {
      return self.char_to_surface(uwu_p());
    }
  }
}

use std::str::Bytes;

pub use sprite_sheets::UwUFont;