resvg icon indicating copy to clipboard operation
resvg copied to clipboard

Single `<text>` node fails to be parsed

Open Daaiid opened this issue 1 month ago • 1 comments

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());
}

Daaiid avatar Nov 04 '25 14:11 Daaiid

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.

RazrFalcon avatar Dec 01 '25 19:12 RazrFalcon

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 []

santhoshtr avatar Dec 06 '25 11:12 santhoshtr

I can't reproduce it either, so the issue probably must be something as @RazrFalcon suggested.

LaurenzV avatar Dec 06 '25 11:12 LaurenzV

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.

LaurenzV avatar Dec 06 '25 11:12 LaurenzV

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.

santhoshtr avatar Dec 06 '25 15:12 santhoshtr

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.

LaurenzV avatar Dec 06 '25 15:12 LaurenzV