Actually parse bdf directly, emit rom to be used for george-emu

This commit is contained in:
august kline 2024-04-10 17:17:38 -04:00
parent 86a72fc2ea
commit ca598bd732
8 changed files with 4101 additions and 2068 deletions

241
Cargo.lock generated
View File

@ -3,56 +3,34 @@
version = 3 version = 3
[[package]] [[package]]
name = "adler" name = "arrayvec"
version = "1.0.2" version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]] [[package]]
name = "bdf" name = "bdf-parser"
version = "0.6.0" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f550a6818e6f42ccd5883f44e45fff4f68415a0d09abdc81e7d1d78e0780af14" checksum = "c904362ac94c74c5b55de49db4911ff01dbc220cf417bd08e75c4df56a443578"
dependencies = [ dependencies = [
"bit-set", "bstr",
"nom",
"strum",
"thiserror", "thiserror",
] ]
[[package]]
name = "bit-set"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84527c7b0452f22545cc010e72d366a435561d2b28b978035550b3778c4d428d"
dependencies = [
"bit-vec",
]
[[package]]
name = "bit-vec"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bits"
version = "0.1.0"
dependencies = [
"bdf",
"bitvec",
"png",
]
[[package]] [[package]]
name = "bitvec" name = "bitvec"
version = "1.0.1" version = "0.19.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" checksum = "55f93d0ef3363c364d5976646a38f04cf67cfe1d4c8d160cdea02cab2c116b33"
dependencies = [ dependencies = [
"funty", "funty",
"radium", "radium",
@ -60,104 +38,162 @@ dependencies = [
"wyz", "wyz",
] ]
[[package]]
name = "bstr"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
dependencies = [
"lazy_static",
"memchr",
"regex-automata",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if",
]
[[package]]
name = "fdeflate"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f9bfee30e4dedf0ab8b422f03af778d9612b63f502710fc500a334ebe2de645"
dependencies = [
"simd-adler32",
]
[[package]]
name = "flate2"
version = "1.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]] [[package]]
name = "funty" name = "funty"
version = "2.0.0" version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7"
[[package]] [[package]]
name = "miniz_oxide" name = "george-font-converter"
version = "0.7.2" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7"
dependencies = [ dependencies = [
"adler", "bdf-parser",
"simd-adler32",
] ]
[[package]] [[package]]
name = "png" name = "heck"
version = "0.17.11" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f6c3c3e617595665b8ea2ff95a86066be38fb121ff920a9c0eb282abcd1da5a" checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lexical-core"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe"
dependencies = [
"arrayvec",
"bitflags", "bitflags",
"crc32fast", "cfg-if",
"fdeflate", "ryu",
"flate2", "static_assertions",
"miniz_oxide", ]
[[package]]
name = "memchr"
version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
[[package]]
name = "nom"
version = "6.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7413f999671bd4745a7b624bd370a569fb6bc574b23c83a3c5ed2e453f3d5e2"
dependencies = [
"bitvec",
"funty",
"lexical-core",
"memchr",
"version_check",
] ]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.78" version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.35" version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]] [[package]]
name = "radium" name = "radium"
version = "0.7.0" version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8"
[[package]] [[package]]
name = "simd-adler32" name = "regex-automata"
version = "0.3.7" version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
[[package]]
name = "ryu"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strum"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.48" version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -172,22 +208,22 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.57" version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.57" version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.58",
] ]
[[package]] [[package]]
@ -197,10 +233,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]] [[package]]
name = "wyz" name = "unicode-segmentation"
version = "0.5.1" version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
dependencies = [
"tap", [[package]]
] name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wyz"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214"

View File

@ -1,11 +1,9 @@
[package] [package]
name = "bits" name = "george-font-converter"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bdf = "0.6.0" bdf-parser = "0.1.0"
bitvec = "1.0.1"
png = "0.17.11"

3905
Cozette8-13.bdf Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 KiB

BIN
cozette.rom Normal file

Binary file not shown.

View File

@ -1,58 +1,64 @@
#![allow(clippy::println_empty_string)] #![allow(clippy::println_empty_string)]
use std::{ use std::{
fs::File, fs::File,
io::{self, Write}, io::{Read, Write},
path::PathBuf, ops::Neg,
u8, u8,
}; };
extern crate png; // takes all charaters in bdf and returns a vec of each character row byte in order, normalized to
// width & height of the font (only works with 8 or fewer pixel wide fonts, should work for any height)
fn bdf_to_vec(path: &str) -> Vec<u8> {
let mut bdf_vec = Vec::new();
let file = File::open(path).unwrap();
let mut bdf_font_bytes = Vec::new();
for byte in file.bytes() {
bdf_font_bytes.push(byte.unwrap());
}
let bdf_font = bdf_parser::BdfFont::parse(&bdf_font_bytes).unwrap();
for glyph in bdf_font.glyphs.iter() {
let glyph_offset_x = glyph.bounding_box.offset.x;
let glyph_offset_y = glyph.bounding_box.offset.y;
let glyph_height = glyph.bounding_box.size.y;
let font_height = bdf_font.metadata.bounding_box.size.y;
let font_offset_y = bdf_font.metadata.bounding_box.offset.y;
const MAX_ROWS: usize = 13; let top_space = font_height + font_offset_y - glyph_height - glyph_offset_y;
const MAX_CHARS: u16 = 0x100; // Non inclusive, first char is 0x00 for _i in 0..top_space {
bdf_vec.push(0x00);
fn to_bitmap(path: &str, max_chars: u16, max_rows: usize) -> Result<Vec<u8>, io::Error> {
let decoder = png::Decoder::new(File::open(path)?);
let mut reader = decoder.read_info()?;
let mut buf = vec![0; reader.output_buffer_size()];
let info = reader.next_frame(&mut buf)?;
let bytes = &buf[..info.buffer_size()];
let compressed: Vec<u8> = bytes.iter().step_by(4).cloned().collect(); // We're not using color, so we skip duplicate color components
let mut finalbuf = Vec::new();
for (i, _bit) in compressed.iter().step_by(8).enumerate() {
let mut byte = 0u8;
for j in (0..8).rev() {
if compressed[i * 8 + j] == 255 {
byte |= 0x80 >> j;
} }
} for bitmap in glyph.bitmap.iter() {
finalbuf.push(byte); bdf_vec.push(bitmap >> glyph_offset_x);
}
Ok(finalbuf)
} }
fn reorder_bitmap(bitmap: &[u8]) -> Vec<u8> { let bottom_space = font_offset_y.neg() + glyph_offset_y;
let mut reordered: Vec<u8> = Vec::new(); for _i in 0..bottom_space {
for row in 0..MAX_ROWS { bdf_vec.push(0x00);
for character in 0x00..MAX_CHARS {
let mut byte = 0u8;
for i in 0..8 {
// character number + row offset & bitmask = pixel
if bitmap[character as usize + (row * 0xFF)] & 0x80 >> i > 0 {
byte |= 0x80 >> i;
} }
} }
reordered.push(byte); bdf_vec
}
}
reordered
} }
fn main() -> std::io::Result<()> { // takes an vec of ordered characters and translates them for use with the character rom
let compressed = to_bitmap("./cozette.png", MAX_CHARS, MAX_ROWS)?; // TODO: make this work for any arbitrary char rom pin format using some kinda interface
let reordered = reorder_bitmap(&compressed); fn reorder_bitmap(bitmap: &[u8], font_height: usize) -> Vec<u8> {
let mut outfile = File::create("./cozette.bin")?; let mut rom = vec![0; 0x8000]; // create vec the size of character rom
outfile.set_len(0xFFFF)?;
outfile.write_all(&reordered)?; for row in 0..font_height {
Ok(()) for ascii_address in 0..u8::MAX {
// first 8 bits of address pick character
// next 5 bits pick row, and TODO: final 2 pick character set
let byte = bitmap[ascii_address as usize * font_height + row];
let rom_index: u16 = ((row as u16) << 8) + ascii_address as u16;
rom[rom_index as usize] = byte;
}
}
rom
}
fn main() {
let bits = bdf_to_vec("Cozette8-13.bdf");
let mut rom = File::create("./cozette.rom").unwrap();
let reordered = reorder_bitmap(&bits, 13);
rom.write_all(&reordered).unwrap();
} }