diff options
feat: get_char should return a character with render-ready information
-rw-r--r-- | build.rs | 5 | ||||
-rw-r--r-- | src/font.rs | 16 |
2 files changed, 18 insertions, 3 deletions
@@ -2,6 +2,8 @@ use rusttype::{Font, Point, Scale}; use std::fs::{self, File}; use std::io::{self, Write}; +// TODO: Build a Rust Macro that does what this does but without a build.rs + #[derive(Clone)] struct FontMetadata { pub charset: &'static str, @@ -103,9 +105,8 @@ fn generate_struct(font: &FontMetadata) -> io::Result<String> { let name = font.name; let mut contents = format!( "pub struct {name} {{}} -impl {name} {{}} impl BakedFont for {name} {{ - fn get_char(c: char) -> &'static [u8] {{ + fn get_char_bytes(c: char) -> &'static [u8] {{ match c as u8 {{ " ); diff --git a/src/font.rs b/src/font.rs index 546fabe..d4e8d88 100644 --- a/src/font.rs +++ b/src/font.rs @@ -15,8 +15,22 @@ fn insert_value(arr: &mut Vec<*const Glyph>, idx: usize, glyph: *const Glyph) { arr[idx] = glyph; } +pub struct RenderableCharacter { + pub width: u16, + pub data: &'static [u8], +} +/** + A trait describing a generated font. + */ pub trait BakedFont { - fn get_char(character: char) -> &'static [u8]; + fn get_char_bytes(character: char) -> &'static [u8]; + fn get_char(character: char) -> RenderableCharacter { + let bytes = Self::get_char_bytes(character); + RenderableCharacter { + width: (bytes[0] as u16) | (bytes[1] as u16 >> 8) , + data: &bytes[2..] + } + } } pub struct Font<'a> { pub texture: &'a [u8], |