|
|
|
|
@ -1,10 +1,16 @@ |
|
|
|
|
//! A redneck font renderer
|
|
|
|
|
//!
|
|
|
|
|
//! This takes a janky ass raw image file with width info inserted
|
|
|
|
|
//! and turns it into a bunch of points for SDL2 to read.
|
|
|
|
|
|
|
|
|
|
use std::{fs::File, path::Path, io::Read}; |
|
|
|
|
|
|
|
|
|
use sdl2::rect::Point; |
|
|
|
|
|
|
|
|
|
const GLYPH_WIDTH: usize = 8; |
|
|
|
|
const GLYPH_HEIGHT: usize = 14; |
|
|
|
|
const GLYPH_AREA: usize = GLYPH_WIDTH * GLYPH_HEIGHT; |
|
|
|
|
#[allow(dead_code)] |
|
|
|
|
type Glyph = [[bool; GLYPH_WIDTH]; GLYPH_HEIGHT]; |
|
|
|
|
type Glyph = Vec<Point>; |
|
|
|
|
|
|
|
|
|
/// Reads the file and turns it into a Vec of u8s
|
|
|
|
|
fn read_file(file_name: String) -> Vec<u8> { |
|
|
|
|
@ -21,49 +27,56 @@ fn read_file(file_name: String) -> Vec<u8> { |
|
|
|
|
file_content |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get_font() { |
|
|
|
|
pub fn generate_glyph_atlas() -> Vec<Glyph> { |
|
|
|
|
// Retrieve font data from file
|
|
|
|
|
let file_path = String::from("./fonts/Terminus14x8.data"); |
|
|
|
|
println!("In file {file_path}"); |
|
|
|
|
|
|
|
|
|
let contents = read_file(file_path); |
|
|
|
|
|
|
|
|
|
// Get width of image for proper positioning of pixels
|
|
|
|
|
let width_left_byte = contents[0]; |
|
|
|
|
let width_right_byte = contents[1]; |
|
|
|
|
let number = [width_left_byte, width_right_byte]; |
|
|
|
|
let width = u16::from_be_bytes(number); |
|
|
|
|
|
|
|
|
|
println!("Left Byte: {width_left_byte}, Right Byte: {width_right_byte}, Byte Pair {width}"); |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
let gtable_prune = contents[2..].iter() |
|
|
|
|
.filter(|x| **x % 3 == 0) |
|
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
*/ |
|
|
|
|
// Remove useless Green and Blue data
|
|
|
|
|
let gtable_prune: Vec<u8> = contents[2..].iter() |
|
|
|
|
.enumerate() |
|
|
|
|
.filter(|x| x.0 % 3 == 0) |
|
|
|
|
.map(|(_, x)| *x) |
|
|
|
|
.collect(); |
|
|
|
|
|
|
|
|
|
println!("Pruned Glyph Table is {} long; it should be {}.", |
|
|
|
|
gtable_prune.len(), |
|
|
|
|
(contents.len() - 2) / 3 |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
let glyph_atlas: Vec<Glyph>; |
|
|
|
|
for glyph in 0..95 { |
|
|
|
|
let new_glyph: Glyph = [[false; GLYPH_WIDTH]; GLYPH_HEIGHT]; |
|
|
|
|
println!("Left Byte: {width_left_byte}, Right Byte: {width_right_byte}, Byte Pair: {width}"); |
|
|
|
|
let gtable_prune = &contents[2..]; |
|
|
|
|
|
|
|
|
|
let mut glyph_atlas: Vec<Glyph> = vec![]; |
|
|
|
|
|
|
|
|
|
for glyph in 0..96 { |
|
|
|
|
let mut new_glyph: Glyph = vec![]; |
|
|
|
|
|
|
|
|
|
for p in 0..GLYPH_AREA as u16 { |
|
|
|
|
let multiplier = p / GLYPH_WIDTH as u16 * width; |
|
|
|
|
let x = p % GLYPH_WIDTH as u16; |
|
|
|
|
let y = p / GLYPH_WIDTH as u16; |
|
|
|
|
|
|
|
|
|
let multiplier = y * width; |
|
|
|
|
let offset = glyph * GLYPH_WIDTH as u16; |
|
|
|
|
let position = (p % GLYPH_WIDTH as u16 + multiplier + offset) as usize; |
|
|
|
|
let position = (x as u16 + multiplier + offset) as usize; |
|
|
|
|
|
|
|
|
|
println!("Glyph num: {glyph:2.} | Addr: {p:3.} | Line: {multiplier:5.} | Pos: {position:5.}"); |
|
|
|
|
if gtable_prune[position] == 1 { |
|
|
|
|
new_glyph.push(Point::new(x as i32, y as i32)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* // Debug info
|
|
|
|
|
print!("Glyph num: {glyph:2.} | Addr: {p:3.} | Line: {multiplier:5.} | Pos: {position:5.}"); |
|
|
|
|
|
|
|
|
|
if gtable_prune[position] == 255 { |
|
|
|
|
if gtable_prune[position] == 1 { |
|
|
|
|
println!(" | X: {x:2.} | Y: {y:2.}"); |
|
|
|
|
} else { |
|
|
|
|
println!(); |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
glyph_atlas.push(new_glyph); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
glyph_atlas |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn generate_text(content: &str, glyph_atlas: Vec<Glyph>) -> Vec<Point> { |
|
|
|
|
|
|
|
|
|
unimplemented!() |
|
|
|
|
} |
|
|
|
|
|