From cc32082c211e8ddd79f4ba6294ada89bdfec4486 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 23 Nov 2023 23:13:17 +0100 Subject: [PATCH] wip --- Cargo.lock | 3 +++ kubi-ui/Cargo.toml | 10 ++++---- kubi-ui/src/font.rs | 9 ------- kubi-ui/src/lib.rs | 14 +++++++--- kubi-ui/src/texman.rs | 57 ----------------------------------------- kubi-ui/src/text.rs | 59 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 75 deletions(-) delete mode 100644 kubi-ui/src/font.rs delete mode 100644 kubi-ui/src/texman.rs create mode 100644 kubi-ui/src/text.rs diff --git a/Cargo.lock b/Cargo.lock index f473484..f4bc460 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -548,6 +548,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0793f5137567643cf65ea42043a538804ff0fbf288649e2141442b602d81f9bc" dependencies = [ "hashbrown 0.13.2", + "rayon", "ttf-parser 0.15.2", ] @@ -773,6 +774,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" dependencies = [ "ahash", + "rayon", ] [[package]] @@ -1046,6 +1048,7 @@ dependencies = [ "kubi-logging", "log", "nohash-hasher", + "rayon", "rect_packer", "winit", ] diff --git a/kubi-ui/Cargo.toml b/kubi-ui/Cargo.toml index 719428a..2bc036a 100644 --- a/kubi-ui/Cargo.toml +++ b/kubi-ui/Cargo.toml @@ -9,9 +9,10 @@ hashbrown = "0.14" nohash-hasher = "0.2" glam = "0.24" glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf", optional = true } -fontdue = { version = "0.7", optional = true } +fontdue = "0.7" rect_packer = { version = "0.2.1", optional = true } log = "0.4" +rayon = { version = "1.8", optional = true } [dev-dependencies] kubi-logging = { path = "../kubi-logging" } @@ -19,9 +20,8 @@ glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf" } winit = "0.29" [features] -default = ["builtin_elements", "texture_manager", "text_rendering", "builtin_font", "backend_glium"] +default = ["builtin_elements", "builtin_font", "backend_glium", "parallel"] backend_glium = ["dep:glium"] -texture_manager = ["dep:rect_packer"] -text_rendering = ["dep:fontdue", "texture_manager"] -builtin_font = ["text_rendering"] +builtin_font = [] builtin_elements = [] +parallel = ["dep:rayon", "fontdue/parallel"] diff --git a/kubi-ui/src/font.rs b/kubi-ui/src/font.rs deleted file mode 100644 index d5745ba..0000000 --- a/kubi-ui/src/font.rs +++ /dev/null @@ -1,9 +0,0 @@ -use fontdue::Font; - -#[cfg(feature = "builtin_font")] -const BIN_FONT: &[u8] = include_bytes!("../assets/font/ProggyTiny.ttf"); - -pub struct FontRenderer { - pub font_stack: Vec, - pub cache: () -} diff --git a/kubi-ui/src/lib.rs b/kubi-ui/src/lib.rs index c600b69..1f1b484 100644 --- a/kubi-ui/src/lib.rs +++ b/kubi-ui/src/lib.rs @@ -7,15 +7,19 @@ pub mod draw; pub mod backend; pub mod measure; pub mod state; -#[cfg(feature = "text_rendering")] -pub mod font; -#[cfg(feature = "texture_manager")] -pub mod texman; +pub mod text; use element::UiElement; use state::StateRepo; use event::UiEvent; use draw::{UiDrawCommands, UiDrawPlan}; +use text::TextRenderer; + +pub struct ElementContext<'a> { + pub state: &'a mut StateRepo, + pub draw: &'a mut UiDrawCommands, + pub text: &'a mut TextRenderer, +} pub struct KubiUi { mouse_position: Vec2, @@ -25,6 +29,7 @@ pub struct KubiUi { draw_commands: UiDrawCommands, draw_plan: UiDrawPlan, draw_plan_modified: bool, + font_renderer: TextRenderer, } impl KubiUi { @@ -38,6 +43,7 @@ impl KubiUi { draw_commands: UiDrawCommands::default(), draw_plan: UiDrawPlan::default(), draw_plan_modified: false, + font_renderer: TextRenderer::new(), } } diff --git a/kubi-ui/src/texman.rs b/kubi-ui/src/texman.rs deleted file mode 100644 index 44cdb52..0000000 --- a/kubi-ui/src/texman.rs +++ /dev/null @@ -1,57 +0,0 @@ -use rect_packer::{Packer, Config as PackerConfig}; -use glam::{IVec2, ivec2}; - -#[derive(Clone, Copy)] -pub struct TextureAllocation { - // pub index: usize, - pub position: IVec2, - pub size: IVec2, - in_texture_size: IVec2, -} - -impl TextureAllocation { - pub fn uv(&self) -> (f32, f32, f32, f32) { - let p0 = self.position.as_vec2() / self.in_texture_size.as_vec2(); - let p1 = (self.position + self.size).as_vec2() / self.in_texture_size.as_vec2(); - (p0.x, p0.y, p1.x, p1.y) - } -} - -pub struct TextureSpace { - size: IVec2, - packer: Packer, - // allocations: Vec, -} - -impl TextureSpace { - pub fn new(size: IVec2) -> Self { - Self { - size, - packer: Packer::new(PackerConfig { - width: size.x, - height: size.y, - border_padding: 1, - rectangle_padding: 1, - }), - // allocations: Vec::new(), - } - } - - pub fn size(&self) -> IVec2 { - self.size - } - - pub fn allocate(&mut self, size: IVec2) -> Option { - let position = self.packer.pack(size.x, size.y, false) - .map(|rect| ivec2(rect.x, rect.y))?; - Some(TextureAllocation { - position, - size, - in_texture_size: self.size - }) - } - - // pub fn lookup(&self, index: usize) -> Option { - // self.allocations.get(index).copied() - // } -} diff --git a/kubi-ui/src/text.rs b/kubi-ui/src/text.rs new file mode 100644 index 0000000..49ae40e --- /dev/null +++ b/kubi-ui/src/text.rs @@ -0,0 +1,59 @@ +use fontdue::{Font, Metrics}; +use hashbrown::HashMap; +use nohash_hasher::BuildNoHashHasher; + +#[cfg(feature = "parallel")] +use rayon::prelude::*; + +#[cfg(feature = "builtin_font")] +const BIN_FONT: &[u8] = include_bytes!("../assets/font/ProggyTiny.ttf"); + +// pub struct Glyph { +// pub metrics: Metrics, +// pub data: Box<[u8]> +// } + +// pub struct FontData { +// font: Font, +// cache: HashMap<(u8, char), Glyph, BuildNoHashHasher> +// } + +// impl FontData { +// pub fn new() -> Self { +// FontData { +// font: Font::from_bytes(BIN_FONT, fontdue::FontSettings::default()).unwrap(), +// cache: HashMap::default() +// } +// } + +// fn prebake(&mut self, size: u8) { +// self.cache = (33..=126).par_bridge().map(|c| { +// let (metrics, data) = self.font.rasterize( +// char::from(c), +// c as f32, +// ); +// Glyph { metrics, data: data.into_boxed_slice() } +// }).collect(); +// } +// } + +pub struct TextRenderer { + font_stack: Vec, + cache: () +} + +impl TextRenderer { + pub fn new() -> Self { + let font = Font::from_bytes(BIN_FONT, fontdue::FontSettings::default()).unwrap(); + TextRenderer { + font_stack: vec![font], + cache: () + } + } +} + +impl Default for TextRenderer { + fn default() -> Self { + Self::new() + } +}