Convert SVG text nodes for in-memory SVGs in iced_wgpu

This commit is contained in:
Héctor Ramón Jiménez 2023-12-11 10:48:07 +01:00
parent 33f92b1be7
commit 04e8e529a0
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -50,27 +50,15 @@ impl Cache {
return self.svgs.get(&handle.id()).unwrap(); return self.svgs.get(&handle.id()).unwrap();
} }
let svg = match handle.data() { let mut svg = match handle.data() {
svg::Data::Path(path) => { svg::Data::Path(path) => fs::read_to_string(path)
let mut tree = .ok()
fs::read_to_string(path).ok().and_then(|contents| { .and_then(|contents| {
usvg::Tree::from_str( usvg::Tree::from_str(&contents, &usvg::Options::default())
&contents,
&usvg::Options::default(),
)
.ok() .ok()
}); })
// If there are text nodes in the tree load fonts and convert the text to paths .map(Svg::Loaded)
if let Some(svg_tree) = &mut tree { .unwrap_or(Svg::NotFound),
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) => { svg::Data::Bytes(bytes) => {
match usvg::Tree::from_data(bytes, &usvg::Options::default()) { match usvg::Tree::from_data(bytes, &usvg::Options::default()) {
Ok(tree) => Svg::Loaded(tree), 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); let _ = self.svgs.insert(handle.id(), svg);
self.svgs.get(&handle.id()).unwrap() self.svgs.get(&handle.id()).unwrap()
} }