Single `<text>` node fails to be parsed
Bug found during PR #967. See PR for additional details.
The following SVG document with a single text node doesn't get parsed as expected:
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'>
<text x='20' y='35'>Some Text</text>
</svg>
The resulting Tree has no children. Debugging the parsing sequence revealed that the core XML is parsed correctly but parsing it to SVG seems to be erroneous.
A failing test to reproduce the issue:
#[test]
fn has_text_nodes() {
let svg = "
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'>
<text x='20' y='35'>Some Text</text>
</svg>
";
let tree = usvg::Tree::from_str(&svg, &usvg::Options::default()).unwrap();
assert!(tree.root().has_children());
assert!(tree.has_text_nodes());
}
Works fine to me. My guess would be that you run Linux and do not have Times New Roman installed.
Or maybe you have disabled the text build feature.
I can reproduce the issue. Also fails when text is inside group node:
<g transform="translate(272.352371 -67.908951)" style="fill: rgb(0.000000%,0.000000%,0.000000%);">
<text font-size="9.962646">top</text>
</g>
Group node reports children as []
I can't reproduce it either, so the issue probably must be something as @RazrFalcon suggested.
Actually, it can't work, because you are not loading any fonts. You would have to do something like this:
#[test]
fn has_text_nodes() {
let svg = "
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'>
<text x='20' y='35'>Some Text</text>
</svg>
";
let mut options = usvg::Options::default();
options.fontdb_mut().load_system_fonts();
let tree = usvg::Tree::from_str(&svg, &options).unwrap();
assert!(tree.root().has_children());
assert!(tree.has_text_nodes());
}
I guess this should be better documented.
Thanks that worked for me. Adding options.fontdb_mut().load_system_fonts(); helped. However, as I am using usvg just for the purpose of data extraction from an svg, load_system_fonts call is strange to read in the code.
Well, you can also load your custom fonts (see the various methods available). But you need to use some font, otherwise the text nodes can't be resolved.