diff --git a/ableos/Cargo.lock b/ableos/Cargo.lock index 6a110c4..4ae525a 100644 --- a/ableos/Cargo.lock +++ b/ableos/Cargo.lock @@ -39,6 +39,7 @@ dependencies = [ "shadeable", "spin", "uart_16550", + "unicode-width", "vga", "volatile 0.2.7", "wasmi", @@ -456,6 +457,12 @@ dependencies = [ "x86_64", ] +[[package]] +name = "unicode-width" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" + [[package]] name = "unicode-xid" version = "0.2.2" diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index a9e006f..2cd59d0 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -37,7 +37,7 @@ vga = "*" log= "*" pretty-hex = "0.2.1" - +unicode-width = "0.1.7" [dependencies.shadeable] diff --git a/ableos/assets/fonts/OpenSansEmoji.ttf b/ableos/assets/fonts/OpenSansEmoji.ttf new file mode 100644 index 0000000..57d86a6 Binary files /dev/null and b/ableos/assets/fonts/OpenSansEmoji.ttf differ diff --git a/ableos/src/graphics/mod.rs b/ableos/src/graphics/mod.rs index c705c02..4af266c 100644 --- a/ableos/src/graphics/mod.rs +++ b/ableos/src/graphics/mod.rs @@ -37,7 +37,7 @@ impl ScreenBuffer { } #[inline] pub fn set_pixel(&mut self, x: usize, y: usize, color: Rgba64) { - self.buff[y * self.size.y + x] = color; + self.buff[y * self.size.x + x] = color; } pub fn clear(&mut self) { @@ -52,18 +52,11 @@ pub trait VgaBuffer { impl VgaBuffer for ScreenBuffer { fn copy_to_buffer(&self) -> GraphicsReturn { let mode = VGAE.lock(); - - /* - if self.size.y * 640 + self.size.x > 640 * 480 { - return GraphicsReturn::ImproperScreenSize; - } - - */ - for y in 0..self.size.y { for x in 0..self.size.x { - use shadeable::pixel_format::into_vga_16; - let vga_color = into_vga_16(self.buff[y * self.size.y + x]); + use shadeable::pixel_format::{get_color16, into_vga_16}; + let vga_color = get_color16(self.buff[y * self.size.x + x]); + // let vga_color = into_vga_16(self.buff[y * self.size.x + x]); mode.set_pixel(x, y, vga_color); } } diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index 9166af0..5e6f71e 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -4,13 +4,13 @@ use alloc::format; -use shadeable::pixel_format::{into_vga_16, new_rgba64}; +use shadeable::pixel_format::{from_vga_16, into_vga_16, new_rgba64}; use vga::{colors::Color16, writers::GraphicsWriter}; use crate::{ graphics::VgaBuffer, relib::network::socket::{SimpleSock, SocketReturns}, - vga_e::{self, VGAE}, + vga_e::{self, num_to_vga16, VGAE}, }; use log::*; @@ -58,22 +58,26 @@ pub fn kernel_main() -> ! { log::set_max_level(LevelFilter::Trace); // crate::wasm::evaluate(); - let mut abcde = ScreenBuffer::new(480, 640); + let mut abcde = ScreenBuffer::new(640, 480); // abcde.clear(); // abcde.copy_to_buffer(); + VGAE.lock().clear_screen(Color16::Black); trace!("length of screen buffer {}", abcde.buff.len()); trace!("Screen size {:?}", abcde.size); - abcde.set_pixel(10, 10, new_rgba64(255, 0, 0, 255)); - for y in 30..60 { - for x in 1..=639 { - abcde.set_pixel(x, y, new_rgba64(255, 255, 8, 255)); + for y in 0..480 { + for x in 0..640 { + let segment_x = x * 4 / 640; + let segment_y = y * 4 / 480; + let segment = segment_x + segment_y * 4; + + abcde.set_pixel(x, y, from_vga_16(num_to_vga16(segment as u8))); } } - // info!("{:?}", abcde.buff); - abcde.copy_to_buffer(); + + // abcde.copy_to_buffer(); { let mut xyz = SimpleSock::new(); @@ -101,19 +105,18 @@ pub fn kernel_main() -> ! { let mut sock_print_id = SimpleSock::new(); { - // sock_print_id.write(format!("原 画 フ ァ イ ル 集").into()); - sock_print_id.write(format!("simple sockets are based").into()); - sock_print_id.write(format!("🤯 🍆 💦").into()); + sock_print_id.write(format!("a原b画cフdァeイfル 集").into()); + // sock_print_id.write(format!("simple sockets are based").into()); // sock_print_id.write(format!("1....2....3....4....5....6....7....8....9").into()); } // crate::relib::image::mono_bitmap::bruh(); - if false { - for x in (*String::from_utf8_lossy(&sock_print_id.peek().unwrap())) - .chars() - .enumerate() - { - vga_e::draw_char(x.1, x.0); + if true { + let mut prev = None; + for current in (*String::from_utf8_lossy(&sock_print_id.peek().unwrap())).chars() { + vga_e::draw_char(prev, current, 0); + + prev = Some(current); } } diff --git a/ableos/src/vga_e.rs b/ableos/src/vga_e.rs index 8d66140..b10a693 100644 --- a/ableos/src/vga_e.rs +++ b/ableos/src/vga_e.rs @@ -1,3 +1,6 @@ +use alloc::string::ToString; +use unicode_width::UnicodeWidthStr; + use { ab_glyph::{Font, FontRef, Glyph}, vga::{ @@ -16,39 +19,11 @@ lazy_static::lazy_static! { pub static ref VGAE_BUFF_OFFSET_Y: spin::Mutex = spin::Mutex::new(0); } -pub fn test_it_fucko() { - let mode = Graphics640x480x16::new(); - mode.set_mode(); - mode.clear_screen(Color16::Black); - - mode.draw_line((80, 60), (80, 420), Color16::White); - mode.draw_line((80, 60), (540, 60), Color16::White); - - { - let offset = 110; - mode.draw_character(offset - 15 - 4, 60 + 15 - 4, 'x', Color16::Red); - - mode.draw_line( - (offset.try_into().unwrap(), 60), - (offset.try_into().unwrap(), 90), - Color16::White, - ); - } - - mode.draw_line((80, 420), (540, 420), Color16::White); - mode.draw_line((540, 420), (540, 60), Color16::White); - mode.draw_line((80, 90), (540, 90), Color16::White); - - for (offset, character) in "ableOS".chars().enumerate() { - mode.draw_character(270 + offset * 8, 72, character, Color16::White) - } -} - pub trait GraphicsAPI { fn add_shader() {} } -pub fn draw_char(character: char, _offset: usize) { +pub fn draw_char(previous_character: Option, character: char, _offset: usize) { let mode = *VGAE.lock(); let mut offset_x = VGAE_BUFF_OFFSET_X.lock(); let mut offset_y = VGAE_BUFF_OFFSET_Y.lock(); @@ -64,7 +39,7 @@ pub fn draw_char(character: char, _offset: usize) { .unwrap(); let font2 = FontRef::try_from_slice(include_bytes!( - "../../ableos/assets/fonts/unifont_upper-14.0.01.ttf" + "../../ableos/assets/fonts/unifont_upper-14.0.01.ttf" // "../../ableos/assets/fonts/OpenSansEmoji.ttf" )) .unwrap(); @@ -87,13 +62,30 @@ pub fn draw_char(character: char, _offset: usize) { match character { '\n' => {} _ => { - let q_glyph: Glyph = used_font.glyph_id(character).with_scale_and_position( + let previous_glyph: Glyph = used_font + .glyph_id(previous_character.unwrap_or(' ')) + .with_scale_and_position( + 20.0 * font_scale, + ab_glyph::point( + *offset_x as f32 * (10.0 * font_scale), + *offset_y as f32 + (18.0 * font_scale), + ), + ); + + let mut q_glyph: Glyph = used_font.glyph_id(character).with_scale_and_position( 20.0 * font_scale, ab_glyph::point( *offset_x as f32 * (10.0 * font_scale), *offset_y as f32 + (18.0 * font_scale), ), ); + { + // figure it our latter bruh + let spacing = used_font.kern_unscaled(previous_glyph.id, q_glyph.id); + info!("{spacing} {:?} {:?}", previous_glyph.id, q_glyph.id); + q_glyph.position.x += spacing * font_scale; + } + if let Some(q) = used_font.outline_glyph(q_glyph) { q.draw(|x, y, c| { if c > 0.015 { @@ -111,3 +103,26 @@ pub fn draw_char(character: char, _offset: usize) { } } } +pub fn num_to_vga16(num: u8) -> Color16 { + use Color16::*; + match num { + 0 => Black, + 1 => Blue, + 2 => Green, + 3 => Cyan, + 4 => Red, + 5 => Magenta, + 6 => Brown, + 7 => LightGrey, + 8 => DarkGrey, + 9 => LightBlue, + 10 => LightGreen, + 11 => LightCyan, + 12 => LightRed, + 13 => Pink, + 14 => Yellow, + 15 => White, + // NOTE: Leasve the in + _ => Color16::Pink, + } +} diff --git a/shadeable/src/pixel_format.rs b/shadeable/src/pixel_format.rs index aa65b51..9075b95 100644 --- a/shadeable/src/pixel_format.rs +++ b/shadeable/src/pixel_format.rs @@ -164,24 +164,28 @@ pub fn into_vga_16(rgba_64: Rgba64) -> Color16 { } pub fn from_vga_16(color: Color16) -> Rgba64 { + use Color16::*; match color { - Color16::Black => new_rgba64(0, 0, 0, 0), - Color16::Blue => new_rgba64(0, 0, 255, 0), - Color16::Green => new_rgba64(0, 255, 0, 0), - Color16::Cyan => new_rgba64(0, 255, 255, 0), - Color16::Red => new_rgba64(255, 0, 0, 0), - Color16::Magenta => new_rgba64(255, 0, 255, 0), - Color16::Brown => new_rgba64(165, 42, 42, 0), - Color16::Pink => new_rgba64(255, 105, 180, 0), - Color16::LightGrey => new_rgba64(211, 211, 211, 0), - Color16::DarkGrey => new_rgba64(105, 105, 105, 0), - Color16::Yellow => new_rgba64(255, 255, 0, 0), - Color16::White => new_rgba64(255, 255, 255, 0), - // To be colored - Color16::LightBlue => new_rgba64(0, 255, 255, 0), - Color16::LightGreen => new_rgba64(0, 255, 255, 0), - Color16::LightCyan => new_rgba64(0, 255, 255, 0), - Color16::LightRed => new_rgba64(0, 255, 255, 0), + Black => new_rgba64(0, 0, 0, 0), + DarkGrey => new_rgba64(105, 105, 105, 0), + LightGrey => new_rgba64(211, 211, 211, 0), + // + Blue => new_rgba64(0, 0, 255, 0), + Green => new_rgba64(0, 255, 0, 0), + Red => new_rgba64(255, 0, 0, 0), + // + Yellow => new_rgba64(255, 255, 0, 0), + Cyan => new_rgba64(0, 255, 255, 0), + Magenta => new_rgba64(255, 0, 255, 0), + + Brown => new_rgba64(165, 42, 42, 0), + Pink => new_rgba64(255, 105, 180, 0), + White => new_rgba64(255, 255, 255, 0), + + LightBlue => new_rgba64(173, 216, 230, 0), + LightGreen => new_rgba64(144, 238, 144, 0), + LightCyan => new_rgba64(88, 100, 100, 0), + LightRed => new_rgba64(255, 204, 203, 0), } } @@ -193,25 +197,29 @@ fn euclideand(c1: (u8, u8, u8), c2: (u8, u8, u8)) -> u8 { let (rd, gd, bd) = (r2 - r1, g2 - g1, b2 - b1); sqrt((rd * rd + gd * gd + bd * bd) as f64) as u8 } +pub fn get_color16(c: Rgba64) -> Color16 { + let r = get_r(c); + let g = get_g(c); + let b = get_b(c); -fn get_color16(c: (u8, u8, u8)) -> Color16 { + let c = (r, g, b); let palette: [(u8, u8, u8); 16] = [ (0, 0, 0), + (105, 105, 105), + (211, 211, 211), (0, 0, 255), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), - (128, 128, 128), + (0, 255, 0), + (255, 0, 0), + (255, 255, 0), + (0, 255, 255), + (255, 0, 255), + (165, 42, 42), + (255, 105, 180), + (255, 255, 255), + (173, 216, 230), + (144, 238, 144), + (88, 100, 100), + (255, 204, 203), ]; let mut minc = euclideand(c, palette[0]); let mut retval = 0; @@ -222,23 +230,24 @@ fn get_color16(c: (u8, u8, u8)) -> Color16 { minc = eucd } } + use Color16::*; match retval { - 0 => Color16::Black, - 1 => Color16::Red, - 2 => Color16::Red, - 3 => Color16::Red, - 4 => Color16::Red, - 5 => Color16::Red, - 6 => Color16::Red, - 7 => Color16::Red, - 8 => Color16::Red, - 9 => Color16::Red, - 10 => Color16::Blue, - 11 => Color16::Blue, - 12 => Color16::Blue, - 13 => Color16::Blue, - 14 => Color16::Blue, - 15 => Color16::Blue, - _ => Color16::Pink, + 0 => Black, + 1 => Blue, + 2 => Green, + 3 => Cyan, + 4 => Red, + 5 => Magenta, + 6 => Brown, + 7 => LightGrey, + 8 => DarkGrey, + 9 => LightBlue, + 10 => LightGreen, + 11 => LightCyan, + 12 => LightRed, + 13 => Pink, + 14 => Yellow, + 15 => White, + _ => Green, } }