From 4b66b825ff23f14203f3cf62589777248e00c452 Mon Sep 17 00:00:00 2001 From: Axel Kappel <69117984+Kl4rry@users.noreply.github.com> Date: Mon, 2 Dec 2024 23:30:09 +0100 Subject: [PATCH] make rendering go fast and fix colors --- Cargo.lock | 1 + crates/ferrite-gui/Cargo.toml | 1 + crates/ferrite-gui/src/backend.rs | 23 ++++++++++++++--------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 80decd9..28626f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -935,6 +935,7 @@ version = "0.1.0" dependencies = [ "anyhow", "cgmath", + "cosmic-text", "ferrite-cli", "ferrite-core", "ferrite-tui", diff --git a/crates/ferrite-gui/Cargo.toml b/crates/ferrite-gui/Cargo.toml index 72dfd7d..811e414 100644 --- a/crates/ferrite-gui/Cargo.toml +++ b/crates/ferrite-gui/Cargo.toml @@ -12,6 +12,7 @@ ferrite-tui = { workspace = true } ferrite-utility = { workspace = true } glyphon = "0.7.0" pollster = "0.3.0" +cosmic-text = { version = "0.12.1", features = ["shape-run-cache"] } tracing = { workspace = true } tui = { workspace = true, default-features = false } unicode-width = { workspace = true } diff --git a/crates/ferrite-gui/src/backend.rs b/crates/ferrite-gui/src/backend.rs index 3b727c2..69334ae 100644 --- a/crates/ferrite-gui/src/backend.rs +++ b/crates/ferrite-gui/src/backend.rs @@ -27,7 +27,7 @@ pub struct WgpuBackend { cell_height: f32, pub columns: u16, pub lines: u16, - // buffer + pub redraw: bool, buffer: Buffer, cells: Vec<(Vec, bool)>, } @@ -103,6 +103,7 @@ impl WgpuBackend { lines, buffer, cells, + redraw: true, } } @@ -150,23 +151,25 @@ impl WgpuBackend { continue; } let mut attr_list = AttrsList::new(default_attrs); - attr_list.add_span(1..3, default_attrs.color(glyphon::Color::rgb(255, 0, 255))); - attr_list.add_span(2..9, default_attrs.color(glyphon::Color::rgb(255, 0, 0))); - eprintln!("{:#?}", attr_list.spans()); let mut line_text = String::new(); + let mut idx = 0; for cell in line { - /*let mut attrs = default_attrs; + let mut attrs = default_attrs; if let tui::style::Color::Rgb(r, g, b) = cell.fg { let color = glyphon::Color::rgb(r, g, b); - }*/ - line_text.push_str(cell.symbol()); + attrs = attrs.color(color); + } + let symbol = cell.symbol(); + line_text.push_str(symbol); + attr_list.add_span(idx..(idx + symbol.len()), attrs); + idx += symbol.len(); } - //self.buffer.set_rich_text(font_system, spans, default_attrs, shaping); - self.buffer.lines[i].set_text( + self.buffer.lines[i] = BufferLine::new( &line_text, glyphon::cosmic_text::LineEnding::Lf, attr_list, + Shaping::Advanced, ); *dirty = false; } @@ -179,6 +182,7 @@ impl WgpuBackend { horizontal: 0.0, }); self.buffer.shape_until_scroll(&mut self.font_system, false); + self.font_system.shape_run_cache.trim(1024); eprintln!("shape: {:?}", Instant::now().duration_since(start)); self.viewport.update( @@ -233,6 +237,7 @@ impl Backend for WgpuBackend { let (line, dirty) = &mut self.cells[line as usize]; line[column as usize] = cell.clone(); *dirty = true; + self.redraw = true; } Ok(()) }