Handle the height of letters correctly

This commit is contained in:
Gwilym Kuiper 2022-04-05 22:44:32 +01:00
parent e7e35f8ad8
commit 2c2931fa97
2 changed files with 17 additions and 4 deletions

View file

@ -22,11 +22,17 @@ pub fn load_font(font_data: &[u8], pixels_per_em: f32) -> TokenStream {
) )
.expect("Invalid font data"); .expect("Invalid font data");
let line_metrics = font.horizontal_line_metrics(pixels_per_em).unwrap();
let line_height = line_metrics.new_line_size as i32;
let ascent = line_metrics.ascent as i32;
let font = (0..128) let font = (0..128)
.map(|i| font.rasterize(char::from_u32(i).unwrap(), pixels_per_em)) .map(|i| font.rasterize(char::from_u32(i).unwrap(), pixels_per_em))
.map(|(metrics, bitmap)| { .map(|(metrics, bitmap)| {
let width = metrics.width; let width = metrics.width;
let height = metrics.height; let height = metrics.height;
LetterData { LetterData {
width, width,
height, height,
@ -57,6 +63,6 @@ pub fn load_font(font_data: &[u8], pixels_per_em: f32) -> TokenStream {
}); });
quote![ quote![
agb::display::Font::new(&[#(#font),*]) agb::display::Font::new(&[#(#font),*], #line_height, #ascent)
] ]
} }

View file

@ -33,11 +33,17 @@ impl FontLetter {
pub struct Font { pub struct Font {
letters: &'static [FontLetter], letters: &'static [FontLetter],
line_height: i32,
ascent: i32,
} }
impl Font { impl Font {
pub const fn new(letters: &'static [FontLetter]) -> Self { pub const fn new(letters: &'static [FontLetter], line_height: i32, ascent: i32) -> Self {
Self { letters } Self {
letters,
line_height,
ascent,
}
} }
fn letter(&self, letter: char) -> &'static FontLetter { fn letter(&self, letter: char) -> &'static FontLetter {
@ -89,11 +95,12 @@ impl Font {
let letter = self.letter(c); let letter = self.letter(c);
let xmin = (current_x_pos + letter.xmin as i32).max(0); let xmin = (current_x_pos + letter.xmin as i32).max(0);
let y_start = current_y_pos + self.ascent - letter.height as i32 - letter.ymin as i32;
for letter_y in 0..(letter.height as i32) { for letter_y in 0..(letter.height as i32) {
for letter_x in 0..(letter.width as i32) { for letter_x in 0..(letter.width as i32) {
let x = letter_x + xmin; let x = letter_x + xmin;
let y = current_y_pos + letter_y; let y = y_start + letter_y;
let px = letter.data[(letter_x + letter_y * letter.width as i32) as usize]; let px = letter.data[(letter_x + letter_y * letter.width as i32) as usize];