Compare commits

..

2 Commits

Author SHA1 Message Date
korin 32e9ccc97a glyph metrics 3 years ago
korin 85301aa13b text and cursor offset 3 years ago
  1. 1
      Cargo.toml
  2. 64
      src/editor_render.rs
  3. 30
      src/main.rs

@ -8,4 +8,5 @@ edition = "2021"
opt-level = 3
[dependencies]
clipboard = "0.5.0"
sdl2 = { version = "0.35.2" }

@ -7,9 +7,17 @@ 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;
pub struct GlyphMetrics {
width: usize,
height: usize,
}
impl GlyphMetrics {
fn area(&self) -> usize {
self.width * self.height
}
}
type Glyph = Vec<Point>;
/// Reads the file and turns it into a Vec of u8s
@ -27,16 +35,20 @@ fn read_file(file_name: String) -> Vec<u8> {
file_content
}
pub fn generate_glyph_atlas() -> Vec<Glyph> {
pub fn generate_glyph_data() -> (Vec<Glyph>, GlyphMetrics) {
// Retrieve font data from file
let file_path = String::from("./fonts/Terminus14x8.data");
let contents = read_file(file_path);
// Get glyph metrics
let glyph_metrics = GlyphMetrics { width: 8, height: 16 };
let glyph_width = glyph_metrics.width;
// 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);
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}");
let gtable_prune = &contents[width as usize + 2 ..];
@ -46,13 +58,14 @@ pub fn generate_glyph_atlas() -> Vec<Glyph> {
for glyph in 0..96 {
let mut new_glyph: Glyph = vec![];
for p in 0..GLYPH_AREA as u16 {
let x = p % GLYPH_WIDTH as u16;
let y = p / GLYPH_WIDTH as u16;
let glyph_area = glyph_metrics.area();
for p in 0..glyph_area as u16 {
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 = (x as u16 + multiplier + offset) as usize;
let offset = glyph * glyph_width as u16;
let position = (x + multiplier + offset) as usize;
if gtable_prune[position] == 1 {
new_glyph.push(Point::new(x as i32, y as i32));
@ -60,13 +73,16 @@ pub fn generate_glyph_atlas() -> Vec<Glyph> {
}
glyph_atlas.push(new_glyph);
}
glyph_atlas
(glyph_atlas, glyph_metrics)
}
/// Method for generating points to render, using given string
pub fn draw_text(content: &str, glyph_atlas: Vec<Glyph>) -> Vec<Point> {
pub fn draw_text(glyph_atlas: &Vec<Glyph>, glyph_metrics: &GlyphMetrics, content: &str, offset: Point) -> Vec<Point> {
let mut points: Vec<Point> = vec![];
let glyph_width = glyph_metrics.width;
let glyph_height = glyph_metrics.height;
let lines = content.split('\n');
for (y, chars) in lines.enumerate() {
for (x, chara) in chars.chars().enumerate() {
@ -78,12 +94,12 @@ pub fn draw_text(content: &str, glyph_atlas: Vec<Glyph>) -> Vec<Point> {
}
for pixel in &glyph_atlas[index - 32] {
let x_offset = x * GLYPH_WIDTH;
let y_offset = y * GLYPH_HEIGHT;
let x_glyph = x * glyph_width;
let y_glyph = y * glyph_height;
let positioned_pixel = Point::new(
pixel.x + x_offset as i32,
pixel.y + y_offset as i32,
pixel.x + x_glyph as i32 + offset.x,
pixel.y + y_glyph as i32 + offset.y
);
points.push(positioned_pixel);
}
@ -92,10 +108,12 @@ pub fn draw_text(content: &str, glyph_atlas: Vec<Glyph>) -> Vec<Point> {
points
}
pub fn draw_cursor(content: &str, mut cursor_position: usize) -> (Point, Point) {
pub fn draw_cursor(glyph_metrics: &GlyphMetrics, mut cursor_position: usize, content: &str, offset: Point) -> (Point, Point) {
let glyph_width = glyph_metrics.width;
let glyph_height = glyph_metrics.height;
let mut x = 0;
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() {
@ -106,15 +124,15 @@ pub fn draw_cursor(content: &str, mut cursor_position: usize) -> (Point, Point)
y += 1;
}
if idx == cursor_position {
let point_a = Point::new((x * GLYPH_WIDTH) as i32,
(y * GLYPH_HEIGHT) as i32
let point_a = Point::new((x * glyph_width) as i32 + offset.x,
(y * glyph_height) as i32 + offset.y
);
let point_b = Point::new(point_a.x,
point_a.y + GLYPH_HEIGHT as i32
point_a.y + glyph_height as i32
);
return (point_a, point_b)
}
}
}
(Point::new(0, 0), Point::new(0, GLYPH_HEIGHT as i32))
(Point::new(offset.x, offset.y), Point::new(offset.x, offset.y + glyph_height as i32))
}

@ -1,8 +1,10 @@
extern crate sdl2;
use clipboard::{ClipboardProvider, ClipboardContext};
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::rect::Point;
mod editor_render;
@ -16,7 +18,8 @@ struct ModifierKeys {
}
pub fn main() -> Result<(), String> {
let glyph_atlas = editor_render::generate_glyph_atlas();
let mut clipboard_context: ClipboardContext = ClipboardProvider::new().unwrap();
let (glyph_atlas, glyph_metrics) = editor_render::generate_glyph_data();
let sdl_context = sdl2::init()?;
let video_subsys = sdl_context.video()?;
@ -34,6 +37,8 @@ pub fn main() -> Result<(), String> {
let mut cursor_position = 0;
let mut selection_anchor: Option<usize> = None;
let pad_offset = Point::new(10, 10);
let mut draw_text = |text: &str, pos: usize| -> Result<(), String> {
// Draw background
canvas.set_draw_color(Color::RGB(32, 32, 32));
@ -41,12 +46,22 @@ pub fn main() -> Result<(), String> {
// Draw text
canvas.set_draw_color(Color::RGB(240, 240, 240));
let fb_text = editor_render::draw_text(text, glyph_atlas.clone());
let fb_text = editor_render::draw_text(
&glyph_atlas,
&glyph_metrics,
text,
pad_offset
);
canvas.draw_points(&fb_text[..])?;
// Draw cursor
canvas.set_draw_color(Color::RGB(64, 240, 240));
let fb_cursor = editor_render::draw_cursor(text, pos);
let fb_cursor = editor_render::draw_cursor(
&glyph_metrics,
pos,
text,
pad_offset
);
canvas.draw_line(fb_cursor.0, fb_cursor.1)?;
canvas.present();
@ -102,6 +117,7 @@ pub fn main() -> Result<(), String> {
};
match (modifier_keys.shift, modifier_keys.ctrl, modifier_keys.alt) {
// All modifiers up
(false, false, false) => {
match keycode {
// DELETE key
@ -134,6 +150,7 @@ pub fn main() -> Result<(), String> {
// Left/Back arrow
Some(Keycode::Left) => {
selection_anchor = None;
cursor_position = usize::checked_sub(cursor_position, 1)
.unwrap_or(0);
draw_text(&buffer, cursor_position)?
@ -141,6 +158,7 @@ pub fn main() -> Result<(), String> {
// Right/Forward arrow
Some(Keycode::Right) => {
selection_anchor = None;
cursor_position = (cursor_position + 1).min(buffer.len());
draw_text(&buffer, cursor_position)?
},
@ -159,11 +177,14 @@ pub fn main() -> Result<(), String> {
}
},
// CTRL down
(false, true, false) => {
match keycode {
Some(Keycode::Z) => println!("Undo"),
Some(Keycode::X) => println!("Cut"),
Some(Keycode::C) => println!("Copy"),
Some(Keycode::C) => {
clipboard_context.set_contents(buffer.clone()).unwrap()
},
Some(Keycode::V) => println!("Paste"),
// BACKSPACE key
@ -214,6 +235,7 @@ pub fn main() -> Result<(), String> {
}
}
}
format!("{selection_anchor:?}");
println!("{buffer}");
Ok(())
}

Loading…
Cancel
Save