diff --git a/src/core/songs.rs b/src/core/songs.rs index 862e78d..5bf3e6a 100644 --- a/src/core/songs.rs +++ b/src/core/songs.rs @@ -1195,10 +1195,14 @@ impl Song { #[cfg(test)] mod test { - use std::fs::read_to_string; + use std::{fs::read_to_string, sync::Arc}; + + use crate::ui::text_svg::text_svg_generator_with_cache; use super::*; use pretty_assertions::assert_eq; + use rayon::iter::{IntoParallelIterator, ParallelIterator}; + use resvg::usvg::fontdb; #[test] pub fn test_song_lyrics() { @@ -1498,6 +1502,36 @@ You saved my soul" assert_eq!(name, VerseName::Verse { number: 5 }); } + #[tokio::test] + async fn test_song_to_slide() { + let song = test_song(); + let songs: Vec = (0..100) + .map(|index| { + let mut song = song.clone(); + song.id = index; + if let Some(map) = song.verse_map.as_mut() { + map.entry(VerseName::Verse { number: 1 }) + .and_modify(|lyric| { + lyric.push_str(&index.to_string()); + }); + } + song + }) + .collect(); + let fontdb = Arc::new(fontdb::Database::new()); + songs.into_par_iter().for_each(|song| { + let slides = song.to_slides().unwrap(); + for mut slide in slides { + text_svg_generator_with_cache( + &mut slide, + Arc::clone(&fontdb), + false, + ); + assert!(slide.text_svg.is_some()); + } + }); + } + // extern crate test; // use test::{Bencher, black_box}; diff --git a/src/ui/text_svg.rs b/src/ui/text_svg.rs index 4697d61..22b6d05 100644 --- a/src/ui/text_svg.rs +++ b/src/ui/text_svg.rs @@ -280,7 +280,7 @@ impl TextSvg { self } - pub fn build(mut self, size: Size) -> Self { + pub fn build(mut self, size: Size, cache: bool) -> Self { // debug!("starting..."); let mut path = dirs::data_local_dir().unwrap(); @@ -411,17 +411,18 @@ impl TextSvg { // text // )); - let hashed_title = rapidhash_v3(final_svg.as_bytes()); - path.push(PathBuf::from(hashed_title.to_string())); - path.set_extension("png"); + if cache { + 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; + 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(), @@ -438,8 +439,11 @@ impl TextSvg { .expect("opops"); resvg::render(&resvg_tree, transform, &mut pixmap.as_mut()); // debug!("rendered"); - if let Err(e) = pixmap.save_png(&path) { - error!(?e, "Couldn't save a copy of the text"); + + if cache { + if let Err(e) = pixmap.save_png(&path) { + error!(?e, "Couldn't save a copy of the text"); + } } // debug!("saved"); @@ -503,7 +507,28 @@ pub fn text_svg_generator( .size(slide.font_size().try_into().unwrap()), ) .fontdb(Arc::clone(&fontdb)) - .build(Size::new(1280.0, 720.0)); + .build(Size::new(1280.0, 720.0), true); + slide.text_svg = Some(text_svg); + } +} + +pub fn text_svg_generator_with_cache( + slide: &mut crate::core::slide::Slide, + fontdb: Arc, + cache: bool, +) { + if !slide.text().is_empty() { + let text_svg = TextSvg::new(slide.text()) + .alignment(slide.text_alignment()) + .fill("#fff") + .shadow(shadow(2, 2, 5, "#000000")) + .stroke(stroke(3, "#000")) + .font( + Font::from(slide.font()) + .size(slide.font_size().try_into().unwrap()), + ) + .fontdb(Arc::clone(&fontdb)) + .build(Size::new(1280.0, 720.0), cache); slide.text_svg = Some(text_svg); } }