From a2a96adf7a19f8b2f7879fc19ff139b930fb102e Mon Sep 17 00:00:00 2001 From: Cory Frenette Date: Sun, 10 Dec 2023 22:12:46 -0500 Subject: [PATCH 1/4] implement svg text fix for native renderer Signed-off-by: Cory Frenette --- wgpu/src/image/vector.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 6582bb8240..1efc5342db 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -2,8 +2,9 @@ use crate::core::svg; use crate::core::{Color, Size}; use crate::image::atlas::{self, Atlas}; +use iced_graphics::text; use resvg::tiny_skia; -use resvg::usvg; +use resvg::usvg::{self, TreeTextToPath}; use std::collections::{HashMap, HashSet}; use std::fs; @@ -51,11 +52,23 @@ impl Cache { let svg = match handle.data() { svg::Data::Path(path) => { - let tree = fs::read_to_string(path).ok().and_then(|contents| { - usvg::Tree::from_str(&contents, &usvg::Options::default()) + let mut tree = + fs::read_to_string(path).ok().and_then(|contents| { + usvg::Tree::from_str( + &contents, + &usvg::Options::default(), + ) .ok() - }); - + }); + // If there are text nodes in the tree load fonts and convert the text to paths + if let Some(svg_tree) = &mut tree { + if svg_tree.has_text_nodes() { + let mut font_system = text::font_system() + .write() + .expect("Read font system"); + svg_tree.convert_text(font_system.raw().db_mut()); + } + } tree.map(Svg::Loaded).unwrap_or(Svg::NotFound) } svg::Data::Bytes(bytes) => { From bb30b137d8b836b7e877d938c4cc62feefb9113f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 11 Dec 2023 10:47:17 +0100 Subject: [PATCH 2/4] Fix `expect` message in `iced_tiny_skia::vector` --- tiny_skia/src/vector.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tiny_skia/src/vector.rs b/tiny_skia/src/vector.rs index 9c2893a240..fd1ab3de3f 100644 --- a/tiny_skia/src/vector.rs +++ b/tiny_skia/src/vector.rs @@ -96,7 +96,7 @@ impl Cache { if let Some(svg) = &mut svg { if svg.has_text_nodes() { let mut font_system = - text::font_system().write().expect("Read font system"); + text::font_system().write().expect("Write font system"); svg.convert_text(font_system.raw().db_mut()); } From 33f92b1be78e2f09290e36f0c9b77af899609bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 11 Dec 2023 10:47:53 +0100 Subject: [PATCH 3/4] Fix import styling in `iced_wgpu::image::vector` --- wgpu/src/image/vector.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 1efc5342db..f48190958e 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -1,8 +1,8 @@ use crate::core::svg; use crate::core::{Color, Size}; +use crate::graphics::text; use crate::image::atlas::{self, Atlas}; -use iced_graphics::text; use resvg::tiny_skia; use resvg::usvg::{self, TreeTextToPath}; use std::collections::{HashMap, HashSet}; From 04e8e529a0e80499b129395664f1806de8102d01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 11 Dec 2023 10:48:07 +0100 Subject: [PATCH 4/4] Convert SVG text nodes for in-memory SVGs in `iced_wgpu` --- wgpu/src/image/vector.rs | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index f48190958e..d9be50d707 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -50,27 +50,15 @@ impl Cache { return self.svgs.get(&handle.id()).unwrap(); } - let svg = match handle.data() { - svg::Data::Path(path) => { - let mut tree = - fs::read_to_string(path).ok().and_then(|contents| { - usvg::Tree::from_str( - &contents, - &usvg::Options::default(), - ) + let mut svg = match handle.data() { + svg::Data::Path(path) => fs::read_to_string(path) + .ok() + .and_then(|contents| { + usvg::Tree::from_str(&contents, &usvg::Options::default()) .ok() - }); - // If there are text nodes in the tree load fonts and convert the text to paths - if let Some(svg_tree) = &mut tree { - if svg_tree.has_text_nodes() { - let mut font_system = text::font_system() - .write() - .expect("Read font system"); - svg_tree.convert_text(font_system.raw().db_mut()); - } - } - tree.map(Svg::Loaded).unwrap_or(Svg::NotFound) - } + }) + .map(Svg::Loaded) + .unwrap_or(Svg::NotFound), svg::Data::Bytes(bytes) => { match usvg::Tree::from_data(bytes, &usvg::Options::default()) { Ok(tree) => Svg::Loaded(tree), @@ -79,6 +67,15 @@ impl Cache { } }; + if let Svg::Loaded(svg) = &mut svg { + if svg.has_text_nodes() { + let mut font_system = + text::font_system().write().expect("Write font system"); + + svg.convert_text(font_system.raw().db_mut()); + } + } + let _ = self.svgs.insert(handle.id(), svg); self.svgs.get(&handle.id()).unwrap() }