diff --git a/src/main.rs b/src/main.rs index d272886..3b2e704 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ use core::str; use std::{ - borrow::Cow, collections::HashMap, fmt::{self, Write}, os::mikros::{address_space::ACTIVE_SPACE, ipc, syscalls} + borrow::Cow, collections::HashMap, fmt::{self, Write}, os::mikros::{address_space::ACTIVE_SPACE, ipc, syscalls}, usize }; use fontdue::{layout::{CoordinateSystem, GlyphRasterConfig, Layout, LayoutSettings, TextStyle, WrapStyle}, Font, FontSettings}; @@ -162,7 +162,8 @@ pub struct FramebufferWriter { layout: Layout, next_line_y: usize, glyph_cache: HashMap>, - cursor_height: usize, + cursor_top: usize, + cursor_bot: usize, } impl FramebufferWriter { @@ -180,7 +181,8 @@ impl FramebufferWriter { layout, next_line_y: 0, glyph_cache: HashMap::new(), - cursor_height: 0, + cursor_top: 0, + cursor_bot: 0, } } @@ -190,6 +192,10 @@ impl FramebufferWriter { let text_height = self.layout.height(); self.layout.append(&[&self.font], &TextStyle::new("\u{2588}", 12.0, 0)); + for row in self.cursor_top..self.cursor_bot { + fbuf.clear_row(row); + } + if (self.next_line_y + self.layout.height() as usize) > fbuf.yres { let excess_y = self.layout.height() as usize - (fbuf.yres - self.next_line_y); @@ -197,25 +203,13 @@ impl FramebufferWriter { self.next_line_y -= excess_y; } - for row in 0..usize::max(self.cursor_height+5,text_height as usize) { - fbuf.clear_row(self.next_line_y + row); - } - - //for row in 0..(fbuf.yres - self.next_line_y) { - // fbuf.clear_row(self.next_line_y + row); - //} - - self.cursor_height = (self.layout.height() - text_height) as usize; - let glyphs = self.layout.glyphs(); + let lines = self.layout.lines().unwrap(); - let mut first_line_top_offset = None; + let first_line = lines[0]; + let first_line_top_offset = (first_line.baseline_y - first_line.max_ascent) as usize; for line in self.layout.lines().unwrap() { - if first_line_top_offset.is_none() { - first_line_top_offset = Some((line.baseline_y - line.max_ascent) as usize); - } - let first_line_top_offset = first_line_top_offset.unwrap(); for glyph_pos in &glyphs[line.glyph_start..=line.glyph_end] { if glyph_pos.width == 0 || glyph_pos.height == 0 { continue; @@ -239,6 +233,11 @@ impl FramebufferWriter { } } + let cursor_glyph = glyphs.last().unwrap(); + + self.cursor_top = cursor_glyph.y as usize - first_line_top_offset + self.next_line_y; + self.cursor_bot = cursor_glyph.y as usize - first_line_top_offset + self.next_line_y + cursor_glyph.height; + let last_line = self.layout.lines().unwrap().last().unwrap(); fbuf.update_screen();