aboutsummaryrefslogtreecommitdiffstats
path: root/build.rs
diff options
context:
space:
mode:
authorLibravatarLarge Libravatar memdmp <memdmpmemewarenet>2025-01-12 02:41:00 +0100
committerLibravatarLarge Libravatar memdmp <memdmpmemewarenet>2025-01-12 02:41:00 +0100
commitc8d7879f4daa41a26c534f42ce2b7050e062db8c (patch)
tree0476d9a389c864ce53990951f5ae55a93d787ebf /build.rs
parentc89d4ae45320a00670c3496060d661a65183a9ed (diff)
downloadcosin25-invite-mountainbytes-c8d7879f4daa41a26c534f42ce2b7050e062db8c.tar.gz
cosin25-invite-mountainbytes-c8d7879f4daa41a26c534f42ce2b7050e062db8c.tar.bz2
cosin25-invite-mountainbytes-c8d7879f4daa41a26c534f42ce2b7050e062db8c.tar.lz
cosin25-invite-mountainbytes-c8d7879f4daa41a26c534f42ce2b7050e062db8c.zip

fix: make font rendering not wonky

Diffstat (limited to 'build.rs')
-rw-r--r--build.rs66
1 files changed, 50 insertions, 16 deletions
diff --git a/build.rs b/build.rs
index f26f98e..97179a0 100644
--- a/build.rs
+++ b/build.rs
@@ -1,4 +1,4 @@
-use rusttype::{Font, Point, Scale};
+use rusttype::{Font, Point, Rect, Scale};
use std::fs::{self, File};
use std::io::{self, Write};
@@ -8,12 +8,41 @@ use std::io::{self, Write};
struct FontMetadata {
pub charset: &'static str,
pub name: &'static str,
+ pub scale: Scale,
pub font: Font<'static>,
}
impl FontMetadata {
pub fn render_character(&self, scale: Scale, character: char) -> Vec<u8> {
let glyph = self.font.glyph(character).scaled(scale);
- let bounding_box = glyph.exact_bounding_box().unwrap();
+ let positioned_glyph = glyph.clone().positioned(Point { x: 0.0, y: 0.0 });
+ let pixel_bounding_box = {
+ let bounding_box = positioned_glyph.pixel_bounding_box();
+ if bounding_box.is_none() {
+ Rect {
+ min: Point { x: 0, y: 0 },
+ max: Point {
+ x: glyph.h_metrics().advance_width as i32,
+ y: 0,
+ },
+ }
+ } else {
+ bounding_box.unwrap()
+ }
+ };
+ let bounding_box = {
+ let bounding_box = glyph.exact_bounding_box();
+ if bounding_box.is_none() {
+ Rect {
+ min: Point { x: 0.0, y: 0.0 },
+ max: Point {
+ x: pixel_bounding_box.width() as f32,
+ y: 0.0,
+ },
+ }
+ } else {
+ bounding_box.unwrap()
+ }
+ };
let width = bounding_box.width() as u32;
let height = bounding_box.height() as u32;
@@ -29,22 +58,27 @@ impl FontMetadata {
a[i] = v;
}
}
- define_item(&mut image, 2 + ((width as usize) * (height as usize)), 0x00);
+ define_item(
+ &mut image,
+ 10 + ((width as usize) * (height as usize)),
+ 0x00,
+ );
image[0] = (width & 0b0000_0000_1111_1111) as u8;
image[1] = (width & 0b1111_1111_0000_0000 << 8) as u8;
if (image[0] as u16) | (image[1] as u16 >> 8) != width as u16 {
panic!("Width missmatch!");
}
+ // TODO: do we *really* need i32 values here? wouldn't i16 be sufficient?
+ image[2..6].copy_from_slice(&pixel_bounding_box.min.x.to_le_bytes());
+ image[6..10].copy_from_slice(&(pixel_bounding_box.min.y + (scale.y / 2.0) as i32).to_le_bytes());
- glyph
- .positioned(Point { x: 0.0, y: 0.0 })
- .draw(|gx: u32, gy: u32, v| {
- let bit = (v * 255.0) as u8;
- if gx < width {
- define_item(&mut image, (2 + (gy * width) + gx) as usize, bit);
- }
- });
+ positioned_glyph.draw(|gx: u32, gy: u32, v| {
+ let bit = (v * 255.0) as u8;
+ if gx < width {
+ define_item(&mut image, (10 + (gy * width) + gx) as usize, bit);
+ }
+ });
image
}
pub fn img_to_hex(image: Vec<u8>) -> Vec<u8> {
@@ -81,10 +115,8 @@ impl FontMetadata {
}
fn exec(font: FontMetadata) -> io::Result<()> {
- let scale = Scale::uniform(32.0); // Set the font size
-
for c in font.unique_chars() {
- let image = font.render_character(scale, c);
+ let image = font.render_character(font.scale, c);
save_bits_to_file(&font, c as u8, &image)?;
}
@@ -164,12 +196,14 @@ use crate::font::BakedFont;
FontMetadata {
name: "Galmuri",
font: { Font::try_from_vec(fs::read("assets/fonts/Galmuri11.ttf")?).unwrap() },
- charset: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz2053:",
+ charset: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz2053: ",
+ scale: Scale::uniform(64.0),
},
FontMetadata {
name: "CherryBombOne",
font: { Font::try_from_vec(fs::read("assets/fonts/CherryBombOne.ttf")?).unwrap() },
- charset: "UwUSpace",
+ charset: "UwUSpace ",
+ scale: Scale::uniform(64.0),
},
];
for font in fonts {