Pergunta

I'd like to be able to add objects (filled rectangles, outlines, etc.) to a cairo SVG context in such a way that when I open the SVG file with Inkscape, each would be recognized as an individual object so that I can move/edit it. Ideally, I'd also like to group objects that would appear as "layers" in Inkscape.

Is this possible to do through the cairo API (to some extent), or would I have to generate each element individually, and then stitch them all together to an Inkscape SVG format? Maybe cairo is not the right tool here? What's the best approach, then?

Foi útil?

Solução

Cairo provides an API for rendering SVG, but not for generating it.

SVG is just XML, so you could use any off-the-shelf XML library to generate your SVG content. If the content is fairly simple, and you would like a python-based solution, I would recommend lxml. In this case, you would be using Python's ETree API to generate XML content.

On the other hand, if the content you're generating is complex, such that you need to position elements dynamically, compute bounding boxes of groups and/or text, and other complex tasks, then I would recommend Batik, which implement's the SVG DOM and provides such methods. In this case, you would be using the DOM API to generate content. Here are some resources on DOM:

Batik is written in Java, and so the most straightforward approach would be to use Java to develop against it, but you could also use Jython, which is Python for Java, if you prefer to stick with the python language.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top