diff --git a/src/ui/text_svg.rs b/src/ui/text_svg.rs index 038d2e0..9be41f4 100644 --- a/src/ui/text_svg.rs +++ b/src/ui/text_svg.rs @@ -4,7 +4,7 @@ use colors_transform::Rgb; use cosmic::{ iced::font::{Style, Weight}, prelude::*, - widget::{svg::Handle, Svg}, + widget::{responsive, svg::Handle, Svg}, }; use tracing::error; @@ -166,15 +166,25 @@ impl TextSvg { "".into() }; // text y coords are based on bottom left corner so we need to take height into consideration - let base = format!("{} + responsive(move |s| { + let text_pieces: Vec = self.text.lines().map(|t| format!("{}", t)).collect(); + let base = format!("{} {} -", shadow, self.font.name, self.font.size, self.fill, stroke, self.text); +", s.width, s.height, shadow, self.font.name, self.font.size, self.fill, stroke, self.text); Svg::new(Handle::from_memory( Box::leak(base.into_boxed_str()).as_bytes(), - )) + )).into() + }) .into() } + + fn text_spans(&self) -> Vec { + self.text + .lines() + .map(|t| format!("{}", t)) + .collect() + } } pub fn shadow( @@ -201,3 +211,27 @@ pub fn stroke(size: u16, color: impl Into) -> Stroke { pub fn color(color: impl AsRef) -> Color { Color::from_hex_str(color) } + +#[cfg(test)] +mod test { + use pretty_assertions::assert_eq; + + use super::TextSvg; + + #[test] + fn test_text_spans() { + let mut text = TextSvg::new(); + text.text = "This is +multiline +text." + .into(); + assert_eq!( + vec![ + String::from("This is"), + String::from("multiline"), + String::from("text."), + ], + text.text_spans() + ) + } +}