|
|
|
|
@ -11,7 +11,7 @@ use sdl2::{ |
|
|
|
|
video::Window, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
use std::{fs::File, path::Path, io::Read}; |
|
|
|
|
use crate::file; |
|
|
|
|
|
|
|
|
|
type Glyph = Vec<Point>; |
|
|
|
|
|
|
|
|
|
@ -31,25 +31,11 @@ pub struct GlyphAtlas { |
|
|
|
|
metrics: GlyphMetrics, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Reads the file and turns it into a Vec of u8s
|
|
|
|
|
fn read_file(file_name: String) -> Vec<u8> { |
|
|
|
|
let path = Path::new(&file_name); |
|
|
|
|
|
|
|
|
|
if !path.exists() { |
|
|
|
|
return String::from("Not Found!").into(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let mut file_content = Vec::new(); |
|
|
|
|
let mut file = File::open(&file_name).expect("Unable to open file"); |
|
|
|
|
file.read_to_end(&mut file_content).expect("Unable to read"); |
|
|
|
|
|
|
|
|
|
file_content |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn generate_glyph_data() -> GlyphAtlas { |
|
|
|
|
pub fn generate_glyph_data() -> Result<GlyphAtlas, String> { |
|
|
|
|
// Retrieve font data from file
|
|
|
|
|
// TODO: Get crate path instead of working directory path
|
|
|
|
|
let file_path = String::from("./fonts/Terminus14x8.data"); |
|
|
|
|
let contents = read_file(file_path); |
|
|
|
|
let contents = file::read_file(file_path)?; |
|
|
|
|
|
|
|
|
|
// Get glyph metrics
|
|
|
|
|
let glyph_metrics = GlyphMetrics { width: 8, height: 16 }; |
|
|
|
|
@ -60,9 +46,9 @@ pub fn generate_glyph_data() -> GlyphAtlas { |
|
|
|
|
let width_right_byte = contents[1]; |
|
|
|
|
let width_bytes = [width_left_byte, width_right_byte]; |
|
|
|
|
let width = u16::from_be_bytes(width_bytes); |
|
|
|
|
// println!("Left Byte: {width_left_byte}, Right Byte: {width_right_byte}, Byte Pair: {width}");
|
|
|
|
|
println!("Left Byte: {width_left_byte}, Right Byte: {width_right_byte}, Byte Pair: {width}"); |
|
|
|
|
|
|
|
|
|
let gtable_prune = &contents[width as usize + 2 ..]; |
|
|
|
|
let pruned_glyph_table = &contents[width as usize + 2 ..]; |
|
|
|
|
|
|
|
|
|
// Generate the glyph atlas
|
|
|
|
|
let mut glyph_atlas: Vec<Glyph> = vec![]; |
|
|
|
|
@ -78,13 +64,13 @@ pub fn generate_glyph_data() -> GlyphAtlas { |
|
|
|
|
let offset = glyph * glyph_width as u16; |
|
|
|
|
let position = (x + multiplier + offset) as usize; |
|
|
|
|
|
|
|
|
|
if gtable_prune[position] == 1 { |
|
|
|
|
if pruned_glyph_table[position] == 1 { |
|
|
|
|
new_glyph.push(Point::new(x as i32, y as i32)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
glyph_atlas.push(new_glyph); |
|
|
|
|
} |
|
|
|
|
GlyphAtlas { glyphs: glyph_atlas, metrics: glyph_metrics } |
|
|
|
|
Ok(GlyphAtlas { glyphs: glyph_atlas, metrics: glyph_metrics }) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Method for generating points to render, using given string
|
|
|
|
|
@ -94,14 +80,14 @@ fn draw_text(glyph_atlas: &GlyphAtlas, content: &str, offset: Point) -> Vec<Poin |
|
|
|
|
let glyph_width = glyph_atlas.metrics.width; |
|
|
|
|
let glyph_height = glyph_atlas.metrics.height; |
|
|
|
|
|
|
|
|
|
let lines = content.split('\n'); |
|
|
|
|
for (y, chars) in lines.enumerate() { |
|
|
|
|
for (x, chara) in chars.chars().enumerate() { |
|
|
|
|
let content_lines = content.split('\n'); |
|
|
|
|
for (y, line) in content_lines.enumerate() { |
|
|
|
|
for (x, character) in line.chars().enumerate() { |
|
|
|
|
let index; |
|
|
|
|
if chara.is_lowercase() { |
|
|
|
|
index = chara.to_ascii_lowercase() as usize; |
|
|
|
|
if character.is_lowercase() { |
|
|
|
|
index = character.to_ascii_lowercase() as usize; |
|
|
|
|
} else { |
|
|
|
|
index = chara.to_ascii_uppercase() as usize; |
|
|
|
|
index = character.to_ascii_uppercase() as usize; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for pixel in &glyph_atlas.glyphs[index - 32] { |
|
|
|
|
@ -114,7 +100,9 @@ fn draw_text(glyph_atlas: &GlyphAtlas, content: &str, offset: Point) -> Vec<Poin |
|
|
|
|
); |
|
|
|
|
points.push(positioned_pixel); |
|
|
|
|
} |
|
|
|
|
// TODO: Limit X drawing
|
|
|
|
|
} |
|
|
|
|
// TODO: Limit Y drawing
|
|
|
|
|
} |
|
|
|
|
points |
|
|
|
|
} |
|
|
|
|
@ -127,9 +115,9 @@ pub fn draw_cursor(glyph_atlas: &GlyphAtlas, mut cursor_position: usize, content |
|
|
|
|
let mut y = 0; |
|
|
|
|
if cursor_position > 0 { |
|
|
|
|
cursor_position = cursor_position.checked_sub(1).unwrap_or(0); |
|
|
|
|
for (idx, chara) in content.chars().enumerate() { |
|
|
|
|
for (idx, character) in content.chars().enumerate() { |
|
|
|
|
x += 1; |
|
|
|
|
if chara == '\n' { |
|
|
|
|
if character == '\n' { |
|
|
|
|
x = 0; |
|
|
|
|
y += 1; |
|
|
|
|
} |
|
|
|
|
|