use super::layer_info::LayerData; use super::style::{self, PathStyle, ViewMode}; use crate::intersection::{intersect_quad_bez_path, Quad}; use crate::LayerId; use glam::{DAffine2, DMat2, DVec2}; use kurbo::{Affine, BezPath, Rect, Shape}; use serde::{Deserialize, Serialize}; use std::fmt::Write; mod to_kurbo; fn glam_to_kurbo(transform: DAffine2) -> Affine { Affine::new(transform.to_cols_array()) } #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct Text { pub text: String, pub style: style::PathStyle, pub size: f64, pub line_width: Option, #[serde(skip)] pub editable: bool, #[serde(skip)] cached_path: Option, } impl LayerData for Text { fn render(&mut self, svg: &mut String, transforms: &mut Vec, view_mode: ViewMode) { let transform = self.transform(transforms, view_mode); let inverse = transform.inverse(); if !inverse.is_finite() { let _ = write!(svg, ""); return; } let _ = writeln!(svg, r#""#); if self.editable { let _ = write!( svg, r#""#, transform .to_cols_array() .iter() .enumerate() .map(|(i, entry)| { entry.to_string() + if i == 5 { "" } else { "," } }) .collect::(), ); } else { let mut path = self.to_bez_path(); path.apply_affine(glam_to_kurbo(transform)); let _ = write!(svg, r#""#, path.to_svg(), self.style.render(view_mode)); } let _ = svg.write_str(""); } fn bounding_box(&self, transform: glam::DAffine2) -> Option<[DVec2; 2]> { let mut path = self.bounding_box(&self.text).to_path(0.1); if transform.matrix2 == DMat2::ZERO { return None; } path.apply_affine(glam_to_kurbo(transform)); let kurbo::Rect { x0, y0, x1, y1 } = path.bounding_box(); Some([(x0, y0).into(), (x1, y1).into()]) } fn intersects_quad(&self, quad: Quad, path: &mut Vec, intersections: &mut Vec>) { if intersect_quad_bez_path(quad, &self.bounding_box(&self.text).to_path(0.), true) { intersections.push(path.clone()); } } } impl Text { pub fn transform(&self, transforms: &[DAffine2], mode: ViewMode) -> DAffine2 { let start = match mode { ViewMode::Outline => 0, _ => (transforms.len() as i32 - 1).max(0) as usize, }; transforms.iter().skip(start).cloned().reduce(|a, b| a * b).unwrap_or(DAffine2::IDENTITY) } pub fn new(text: String, style: PathStyle, size: f64) -> Self { let mut new = Self { text, style, size, line_width: None, editable: false, cached_path: None, }; new.regenerate_path(); new } /// Converts to a BezPath, populating the cache if necessary #[inline] pub fn to_bez_path(&mut self) -> BezPath { if self.cached_path.is_none() { self.regenerate_path(); } self.cached_path.clone().unwrap() } /// Converts to a bezpath, without populating the cache #[inline] pub fn to_bez_path_nonmut(&self) -> BezPath { self.cached_path.clone().unwrap_or_else(|| self.generate_path()) } #[inline] fn font_face() -> rustybuzz::Face<'static> { rustybuzz::Face::from_slice(include_bytes!("SourceSansPro/SourceSansPro-Regular.ttf"), 0).unwrap() } #[inline] fn generate_path(&self) -> BezPath { to_kurbo::to_kurbo(&self.text, Self::font_face(), self.size, self.line_width) } #[inline] pub fn bounding_box(&self, text: &str) -> Rect { let far = to_kurbo::bounding_box(text, Self::font_face(), self.size, self.line_width); Rect::new(0., 0., far.x, far.y) } pub fn regenerate_path(&mut self) { self.cached_path = Some(self.generate_path()); } pub fn update_text(&mut self, text: String) { self.text = text; self.regenerate_path(); } }