adding hasing the string as filename for better caching
Some checks are pending
/ test (push) Waiting to run

This commit is contained in:
Chris Cochrun 2025-09-09 09:26:28 -05:00
parent 9971ccdf30
commit 64a9937776
3 changed files with 34 additions and 20 deletions

10
Cargo.lock generated
View file

@ -3858,6 +3858,7 @@ dependencies = [
"libcosmic", "libcosmic",
"miette", "miette",
"pretty_assertions", "pretty_assertions",
"rapidhash",
"rayon", "rayon",
"resvg 0.45.1", "resvg 0.45.1",
"rodio", "rodio",
@ -5342,6 +5343,15 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93e7e49bb0bf967717f7bd674458b3d6b0c5f48ec7e3038166026a69fc22223" checksum = "f93e7e49bb0bf967717f7bd674458b3d6b0c5f48ec7e3038166026a69fc22223"
[[package]]
name = "rapidhash"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d126f24bc587b080d7823a831d0fe832090a606fd3fd244ecbe23c561104ab8"
dependencies = [
"rustversion",
]
[[package]] [[package]]
name = "rav1e" name = "rav1e"
version = "0.7.1" version = "0.7.1"

View file

@ -34,6 +34,7 @@ colors-transform = "0.2.11"
rayon = "1.11.0" rayon = "1.11.0"
resvg = "0.45.1" resvg = "0.45.1"
image = "0.25.8" image = "0.25.8"
rapidhash = "4.0.0"
# femtovg = { version = "0.16.0", features = ["wgpu"] } # femtovg = { version = "0.16.0", features = ["wgpu"] }
# wgpu = "26.0.1" # wgpu = "26.0.1"
# mupdf = "0.5.0" # mupdf = "0.5.0"

View file

@ -15,6 +15,7 @@ use cosmic::{
prelude::*, prelude::*,
widget::{container, image::Handle, Image}, widget::{container, image::Handle, Image},
}; };
use rapidhash::v3::rapidhash_v3;
use resvg::{ use resvg::{
tiny_skia::{self, Pixmap}, tiny_skia::{self, Pixmap},
usvg::{fontdb, Tree}, usvg::{fontdb, Tree},
@ -244,20 +245,10 @@ impl TextSvg {
pub fn build(mut self) -> Self { pub fn build(mut self) -> Self {
debug!("starting..."); debug!("starting...");
// let mut path = dirs::data_local_dir().unwrap(); let mut path = dirs::data_local_dir().unwrap();
// path.push(PathBuf::from("lumina")); path.push(PathBuf::from("lumina"));
// path.push(PathBuf::from("temp")); 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");
// 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 { let shadow = if let Some(shadow) = &self.shadow {
format!("<filter id=\"shadow\"><feDropShadow dx=\"{}\" dy=\"{}\" stdDeviation=\"{}\" flood-color=\"{}\"/></filter>", format!("<filter id=\"shadow\"><feDropShadow dx=\"{}\" dy=\"{}\" stdDeviation=\"{}\" flood-color=\"{}\"/></filter>",
shadow.offset_x, shadow.offset_x,
@ -307,6 +298,18 @@ impl TextSvg {
self.font.name, self.font.name,
font_size, font_size,
self.fill, stroke, text); 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..."); debug!("text string built...");
let resvg_tree = Tree::from_data( let resvg_tree = Tree::from_data(
&final_svg.as_bytes(), &final_svg.as_bytes(),
@ -322,15 +325,15 @@ impl TextSvg {
Pixmap::new(size.width as u32, size.height as u32) Pixmap::new(size.width as u32, size.height as u32)
.expect("opops"); .expect("opops");
resvg::render(&resvg_tree, transform, &mut pixmap.as_mut()); resvg::render(&resvg_tree, transform, &mut pixmap.as_mut());
// let _ = pixmap.save_png(&path); let _ = pixmap.save_png(&path);
debug!("rendered"); debug!("rendered");
// let handle = Handle::from_path(path); let handle = Handle::from_path(path);
let handle = Handle::from_rgba( // let handle = Handle::from_rgba(
size.width as u32, // size.width as u32,
size.height as u32, // size.height as u32,
pixmap.take(), // pixmap.take(),
); // );
self.handle = Some(handle); self.handle = Some(handle);
debug!("stored"); debug!("stored");
self self