Skip to content

Commit

Permalink
Merge pull request #2161 from cfrenette/bug/svg-text-not-rendered
Browse files Browse the repository at this point in the history
Implement a fix for the native renderer not rendering text in SVGs
  • Loading branch information
hecrj authored Dec 11, 2023
2 parents eaaea41 + 04e8e52 commit 7868968
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion tiny_skia/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
26 changes: 18 additions & 8 deletions wgpu/src/image/vector.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::core::svg;
use crate::core::{Color, Size};
use crate::graphics::text;
use crate::image::atlas::{self, Atlas};

use resvg::tiny_skia;
use resvg::usvg;
use resvg::usvg::{self, TreeTextToPath};
use std::collections::{HashMap, HashSet};
use std::fs;

Expand Down Expand Up @@ -49,15 +50,15 @@ impl Cache {
return self.svgs.get(&handle.id()).unwrap();
}

let svg = match handle.data() {
svg::Data::Path(path) => {
let tree = fs::read_to_string(path).ok().and_then(|contents| {
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()
});

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),
Expand All @@ -66,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()
}
Expand Down

0 comments on commit 7868968

Please sign in to comment.