closer to using these text_svgs
Some checks failed
/ test (push) Has been cancelled

This commit is contained in:
Chris Cochrun 2025-08-29 16:41:24 -05:00
parent 1446e35c58
commit 4ccb186189
6 changed files with 112 additions and 75 deletions

View file

@ -1,4 +1,5 @@
use miette::{IntoDiagnostic, Result};
use resvg::usvg::fontdb;
use std::{fs::File, io::BufReader, path::PathBuf, sync::Arc};
use cosmic::{
@ -30,7 +31,7 @@ use url::Url;
use crate::{
core::{service_items::ServiceItem, slide::Slide},
// ui::widgets::slide_text,
ui::text_svg,
BackgroundKind,
};
@ -148,6 +149,7 @@ impl Presenter {
};
let total_slides: usize =
items.iter().fold(0, |a, item| a + item.slides.len());
Self {
current_slide: items[0].slides[0].clone(),
current_item: 0,
@ -741,7 +743,12 @@ pub(crate) fn slide_view(
.align_x(Horizontal::Left)
} else {
// SVG based
let text = slide.text_svg.view().map(|m| Message::None);
let text: Element<Message> =
if let Some(text) = &slide.text_svg {
text.view().map(|_| Message::None).into()
} else {
Space::with_width(0).into()
};
Container::new(text)
.center(Length::Fill)
.align_x(Horizontal::Left)

View file

@ -1,4 +1,4 @@
use std::{io, path::PathBuf};
use std::{io, path::PathBuf, sync::Arc};
use cosmic::{
dialog::file_chooser::open::Dialog,
@ -31,7 +31,7 @@ use super::presenter::slide_view;
pub struct SongEditor {
pub song: Option<Song>,
title: String,
font_db: fontdb::Database,
font_db: Arc<fontdb::Database>,
fonts: Vec<(fontdb::ID, String)>,
fonts_combo: combo_box::State<String>,
font_sizes: combo_box::State<String>,
@ -72,11 +72,9 @@ pub enum Message {
}
impl SongEditor {
pub fn new() -> Self {
pub fn new(font_db: Arc<fontdb::Database>) -> Self {
let fonts = font_dir();
debug!(?fonts);
let mut font_db = fontdb::Database::new();
font_db.load_system_fonts();
let mut fonts: Vec<(fontdb::ID, String)> = font_db
.faces()
.map(|f| {
@ -447,7 +445,9 @@ order",
impl Default for SongEditor {
fn default() -> Self {
Self::new()
let mut fontdb = fontdb::Database::new();
fontdb.load_system_fonts();
Self::new(Arc::new(fontdb))
}
}

View file

@ -16,7 +16,7 @@ use cosmic::{
};
use resvg::{
tiny_skia::{self, Pixmap},
usvg::Tree,
usvg::{fontdb, Tree},
};
use tracing::{debug, error};
@ -230,6 +230,11 @@ impl TextSvg {
self
}
pub fn fontdb(mut self, fontdb: Arc<fontdb::Database>) -> Self {
self.fontdb = fontdb;
self
}
pub fn alignment(mut self, alignment: TextAlignment) -> Self {
self.alignment = alignment;
self
@ -285,7 +290,7 @@ impl TextSvg {
self.font.name,
self.font.size,
self.fill, stroke, text);
debug!(?final_svg);
// debug!(?final_svg);
let resvg_tree = Tree::from_str(
&final_svg,
&resvg::usvg::Options {
@ -301,21 +306,17 @@ impl TextSvg {
.expect("opops");
resvg::render(&resvg_tree, transform, &mut pixmap.as_mut());
// debug!(?pixmap);
let handle = Handle::from_bytes(pixmap.data().to_owned());
let handle = Handle::from_bytes(pixmap.take());
self.handle = Some(handle);
self
}
pub fn view<'a>(&self) -> Element<'a, Message> {
container(
Image::new(self.handle.clone().unwrap())
.content_fit(ContentFit::Contain)
.width(Length::Fill)
.height(Length::Fill),
)
.width(Length::Fill)
.height(Length::Fill)
.into()
Image::new(self.handle.clone().unwrap())
.content_fit(ContentFit::Contain)
.width(Length::Fill)
.height(Length::Fill)
.into()
}
fn text_spans(&self) -> Vec<String> {
@ -352,6 +353,26 @@ pub fn color(color: impl AsRef<str>) -> Color {
Color::from_hex_str(color)
}
pub fn text_svg_generator(
slide: &mut crate::core::slide::Slide,
fontdb: Arc<fontdb::Database>,
) {
if slide.text().len() > 0 {
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().clone())
.size(slide.font_size().try_into().unwrap()),
)
.fontdb(Arc::clone(&fontdb))
.build();
slide.text_svg = Some(text_svg);
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;