type-render
korin 3 years ago
parent 7a47787747
commit 9d38959258
  1. 71
      src/font_build.rs
  2. 32
      src/main.rs

@ -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!()
}

@ -12,8 +12,6 @@ use sdl2::render::TextureQuery;
//use _catlas::*;
mod font_build;
#[allow(unused_imports)]
use font_build::*;
static SCREEN_WIDTH: u32 = 1680;
static SCREEN_HEIGHT: u32 = 945;
@ -31,32 +29,6 @@ macro_rules! rect(
)
);
// Scale fonts to a reasonable size when they're too big (though they might look less smooth)
/*
fn get_centered_rect(rect_width: u32, rect_height: u32, cons_width: u32, cons_height: u32) -> Rect {
let wr = rect_width as f32 / cons_width as f32;
let hr = rect_height as f32 / cons_height as f32;
let (w, h) = if wr > 1f32 || hr > 1f32 {
if wr > hr {
println!("Scaling down! The text will look worse!");
let h = (rect_height as f32 / wr) as i32;
(cons_width as i32, h)
} else {
println!("Scaling down! The text will look worse!");
let w = (rect_width as f32 / hr) as i32;
(w, cons_height as i32)
}
} else {
(rect_width as i32, rect_height as i32)
};
let cx = (SCREEN_WIDTH as i32 - w) / 2;
let cy = (SCREEN_HEIGHT as i32 - h) / 2;
rect!(cx, cy, w, h)
}
*/
fn get_corner_rect(rect_width: u32, rect_height: u32) -> Rect {
let (w,h) = (rect_width as i32, rect_height as i32);
let cx = 15;
@ -66,7 +38,7 @@ fn get_corner_rect(rect_width: u32, rect_height: u32) -> Rect {
pub fn main() -> Result<(), String> {
font_build::get_font();
font_build::generate_glyph_atlas();
let font_path: &Path = Path::new("./fonts/Monoid-Regular.ttf");
@ -119,6 +91,8 @@ pub fn main() -> Result<(), String> {
Ok(())
};
draw_text(" ")?;
let mut modifier_keys = ModifierKeys {alt: false, ctrl: false, shift: false};
'mainloop: loop {

Loading…
Cancel
Save