From 5d372568e2456efb6378ba881422b9e92b4daf52 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 23 Jan 2026 05:01:58 +0000 Subject: [PATCH] Finished partial BDF output --- src/main.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4d4892c..01e8952 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use std::fmt::Display; mod bit_reader; fn main() { - let font_file = include_bytes!("u8g2_font_logisoso24_tr.u8g2font"); + let font_file = include_bytes!("u8g2_font_8x13_tr.u8g2font"); let header_bytes = &font_file[0..23]; let mut data_bytes = &font_file[23..]; @@ -120,6 +120,8 @@ fn main() { for i in glyphs.iter() { bdf_file.push_str(&format!("STARTCHAR {}\n", i.unicode as char)); bdf_file.push_str(&format!("ENCODING {}\n", i.unicode)); + bdf_file.push_str(&format!("BBX {} {} {} {}\n", i.glyph_width, i.glyph_height, i.glyph_x, i.glyph_y)); + bdf_file.push_str("BITMAP\n"); let mut decoded_bits = Vec::new(); @@ -143,17 +145,37 @@ fn main() { } } - let mut bdf_bitmap: Vec> = Vec::new(); + let mut decoded_bits_iter = decoded_bits.iter(); - while !decoded_bits.is_empty() { - for j in decoded_bits.iter().by_ref().take(i.glyph_width as usize) { - + for _ in 0..i.glyph_height { + let bitmap_row = decoded_bits_iter.by_ref().take(i.glyph_width as usize).cloned().collect::>(); + let (chunks, remainder) = bitmap_row.as_chunks::<8>(); + let mut bitmap_row_bytes = Vec::new(); + + for k in chunks { + bitmap_row_bytes.push(k.iter().fold(0_u8, |v, b| (v << 1) + (*b as u8))); } + + if remainder.len() != 0 { + let bits = remainder.len(); + let mut last_byte = remainder.iter().fold(0_u8, |v, b| (v << 1) + (*b as u8)); + last_byte = last_byte << 8-bits; + bitmap_row_bytes.push(last_byte); + } + let mut bitmap_hex = String::new(); + + for byte in bitmap_row_bytes { + bitmap_hex.push_str(&format!("{:X}", byte)); + } + bitmap_hex.push('\n'); + bdf_file.push_str(&bitmap_hex); } + bdf_file.push_str("ENDCHAR\n"); } bdf_file.push_str("ENDFONT"); + println!("{bdf_file}"); } #[derive(Debug)]