Image and text bug fixes (#685)

* Image and text bugfixes

* Mark only the required layer types as dirty

* Fix doctest

* Disable selection if empty

* Cleanup naming

* Simplify cache deleting on export

* Minor css style change

* Nit

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
0HyperCube
2022-06-30 02:18:01 +01:00
committed by Keavon Chambers
co-authored by Keavon Chambers
parent 2e3e079982
commit 020f700c92
22 changed files with 243 additions and 128 deletions
+16 -19
View File
@@ -1,5 +1,5 @@
use super::layer_info::LayerData;
use super::style::{PathStyle, ViewMode};
use super::style::{PathStyle, RenderData, ViewMode};
use crate::intersection::{intersect_quad_bez_path, Quad};
use crate::LayerId;
pub use font_cache::{Font, FontCache};
@@ -33,12 +33,12 @@ pub struct TextLayer {
#[serde(skip)]
pub editable: bool,
#[serde(skip)]
cached_path: Option<BezPath>,
pub cached_path: Option<BezPath>,
}
impl LayerData for TextLayer {
fn render(&mut self, svg: &mut String, svg_defs: &mut String, transforms: &mut Vec<DAffine2>, view_mode: ViewMode, font_cache: &FontCache, _culling_bounds: Option<[DVec2; 2]>) {
let transform = self.transform(transforms, view_mode);
fn render(&mut self, svg: &mut String, svg_defs: &mut String, transforms: &mut Vec<DAffine2>, render_data: RenderData) {
let transform = self.transform(transforms, render_data.view_mode);
let inverse = transform.inverse();
if !inverse.is_finite() {
@@ -53,8 +53,8 @@ impl LayerData for TextLayer {
let _ = svg.write_str(r#")">"#);
if self.editable {
let font = font_cache.resolve_font(&self.font);
if let Some(url) = font.and_then(|font| font_cache.get_preview_url(font)) {
let font = render_data.font_cache.resolve_font(&self.font);
if let Some(url) = font.and_then(|font| render_data.font_cache.get_preview_url(font)) {
let _ = write!(svg, r#"<style>@font-face {{font-family: local-font;src: url({});}}")</style>"#, url);
}
@@ -70,7 +70,7 @@ impl LayerData for TextLayer {
font.map(|_| r#" style="font-family: local-font;""#).unwrap_or_default()
);
} else {
let buzz_face = self.load_face(font_cache);
let buzz_face = self.load_face(render_data.font_cache);
let mut path = self.to_bez_path(buzz_face);
@@ -86,7 +86,7 @@ impl LayerData for TextLayer {
svg,
r#"<path d="{}" {} />"#,
path.to_svg(),
self.path_style.render(view_mode, svg_defs, transform, bounds, transformed_bounds)
self.path_style.render(render_data.view_mode, svg_defs, transform, bounds, transformed_bounds)
);
}
let _ = svg.write_str("</g>");
@@ -139,7 +139,7 @@ impl TextLayer {
cached_path: None,
};
new.regenerate_path(new.load_face(font_cache));
new.cached_path = Some(new.generate_path(new.load_face(font_cache)));
new
}
@@ -147,8 +147,10 @@ impl TextLayer {
/// Converts to a [BezPath], populating the cache if necessary.
#[inline]
pub fn to_bez_path(&mut self, buzz_face: Option<Face>) -> BezPath {
if self.cached_path.is_none() {
self.regenerate_path(buzz_face);
if self.cached_path.as_ref().filter(|x| !x.is_empty()).is_none() {
let path = self.generate_path(buzz_face);
self.cached_path = Some(path.clone());
return path;
}
self.cached_path.clone().unwrap()
}
@@ -158,11 +160,11 @@ impl TextLayer {
pub fn to_bez_path_nonmut(&self, font_cache: &FontCache) -> BezPath {
let buzz_face = self.load_face(font_cache);
self.cached_path.clone().unwrap_or_else(|| self.generate_path(buzz_face))
self.cached_path.clone().filter(|x| !x.is_empty()).unwrap_or_else(|| self.generate_path(buzz_face))
}
#[inline]
fn generate_path(&self, buzz_face: Option<Face>) -> BezPath {
pub fn generate_path(&self, buzz_face: Option<Face>) -> BezPath {
to_kurbo::to_kurbo(&self.text, buzz_face, self.size, self.line_width)
}
@@ -172,15 +174,10 @@ impl TextLayer {
Rect::new(0., 0., far.x, far.y)
}
/// Populate the cache.
pub fn regenerate_path(&mut self, buzz_face: Option<Face>) {
self.cached_path = Some(self.generate_path(buzz_face));
}
pub fn update_text(&mut self, text: String, font_cache: &FontCache) {
let buzz_face = self.load_face(font_cache);
self.text = text;
self.regenerate_path(buzz_face);
self.cached_path = Some(self.generate_path(buzz_face));
}
}