sammycage / lunasvg

SVG rendering and manipulation library in C++
MIT License
866 stars 124 forks source link

Accessing tree structure of Document #71

Closed Jelim16 closed 2 years ago

Jelim16 commented 2 years ago

Hello, first of all I would like to thank you for this amazing library. I managed to build it with Visual Studio 2022 and use within MFC executable. Now I am trying to render some SVG sprites file into bitmap in order to load it into CImageList. Is it possible to somehow access tree structure of lunasvg::Document to find out how many sprites are inside?

Example of SVG with 3 separate sprites, each one to be loaded as single image in CImageList:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="300" height="100">
    <svg id="circle" width="100" height="100">
      <circle cx="50" cy="50" r="40" stroke="black" stroke-width="1" fill="red" />
    </svg> 

    <svg id="square" width="100" height="100" transform="translate(100,0)">
      <rect width="80" height="80" x="10" y="10" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)" />
    </svg>

    <svg id="triangle" width="100" height="100" transform="translate(200,0)">
      <polygon points="50,10 10,90 90,90" style="fill:lime;stroke:purple;stroke-width:1" />
    </svg>
</svg>
sammycage commented 2 years ago

Is it possible to somehow access tree structure of lunasvg::Document to find out how many sprites are inside?

No... Because DOM Tree is destroyed after loading and the Layout Tree is private. But since SVG is the same XML, you can use any XML library to do the job.

FYI I am currently working on a way to render individual element.

Example :

Bitmap Document::renderElement(std::string& id); // Renders an Element by ID onto the bitmap

Bitmap bitmap = document->renderElement("triangle"); // Renders an Element by the ID triangle onto the bitmap.
Jelim16 commented 2 years ago

OK, thank you.