From 64a993777683d5863b558aeb319d62ecb440b8b8 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Tue, 9 Sep 2025 09:26:28 -0500 Subject: [PATCH] adding hasing the string as filename for better caching --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/ui/text_svg.rs | 43 +++++++++++++++++++++++-------------------- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 106d5e9..d50f6aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3858,6 +3858,7 @@ dependencies = [ "libcosmic", "miette", "pretty_assertions", + "rapidhash", "rayon", "resvg 0.45.1", "rodio", @@ -5342,6 +5343,15 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f93e7e49bb0bf967717f7bd674458b3d6b0c5f48ec7e3038166026a69fc22223" +[[package]] +name = "rapidhash" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d126f24bc587b080d7823a831d0fe832090a606fd3fd244ecbe23c561104ab8" +dependencies = [ + "rustversion", +] + [[package]] name = "rav1e" version = "0.7.1" diff --git a/Cargo.toml b/Cargo.toml index b9bc246..8ab9d45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ colors-transform = "0.2.11" rayon = "1.11.0" resvg = "0.45.1" image = "0.25.8" +rapidhash = "4.0.0" # femtovg = { version = "0.16.0", features = ["wgpu"] } # wgpu = "26.0.1" # mupdf = "0.5.0" diff --git a/src/ui/text_svg.rs b/src/ui/text_svg.rs index ff15aa5..9c06e2c 100644 --- a/src/ui/text_svg.rs +++ b/src/ui/text_svg.rs @@ -15,6 +15,7 @@ use cosmic::{ prelude::*, widget::{container, image::Handle, Image}, }; +use rapidhash::v3::rapidhash_v3; use resvg::{ tiny_skia::{self, Pixmap}, usvg::{fontdb, Tree}, @@ -244,20 +245,10 @@ impl TextSvg { pub fn build(mut self) -> Self { debug!("starting..."); - // let mut path = dirs::data_local_dir().unwrap(); - // path.push(PathBuf::from("lumina")); - // path.push(PathBuf::from("temp")); - // let file_title = - // &self.text.lines().next().unwrap().trim_end(); - // path.push(PathBuf::from(file_title)); - // path.set_extension("png"); + let mut path = dirs::data_local_dir().unwrap(); + path.push(PathBuf::from("lumina")); + path.push(PathBuf::from("temp")); - // if path.exists() { - // debug!("cached"); - // let handle = Handle::from_path(path); - // self.handle = Some(handle); - // return self; - // } let shadow = if let Some(shadow) = &self.shadow { format!("", shadow.offset_x, @@ -307,6 +298,18 @@ impl TextSvg { self.font.name, font_size, self.fill, stroke, text); + + let hashed_title = rapidhash_v3(final_svg.as_bytes()); + path.push(PathBuf::from(hashed_title.to_string())); + path.set_extension("png"); + + if path.exists() { + debug!("cached"); + let handle = Handle::from_path(path); + self.handle = Some(handle); + return self; + } + debug!("text string built..."); let resvg_tree = Tree::from_data( &final_svg.as_bytes(), @@ -322,15 +325,15 @@ impl TextSvg { Pixmap::new(size.width as u32, size.height as u32) .expect("opops"); resvg::render(&resvg_tree, transform, &mut pixmap.as_mut()); - // let _ = pixmap.save_png(&path); + let _ = pixmap.save_png(&path); debug!("rendered"); - // let handle = Handle::from_path(path); - let handle = Handle::from_rgba( - size.width as u32, - size.height as u32, - pixmap.take(), - ); + let handle = Handle::from_path(path); + // let handle = Handle::from_rgba( + // size.width as u32, + // size.height as u32, + // pixmap.take(), + // ); self.handle = Some(handle); debug!("stored"); self