For context, I'm trying the gtk custom drawing example. I want to dump the Surface
(in my case an xlib surface) into a SVG file, and get an actual vector image, rather than a embedded PNG.
For example:
cairo_surface_t *svg_surface;
svg_surface = cairo_svg_surface_create("/path/myfile.svg", 200, 200);
cairo_t *cr;
cr = cairo_create(svg_surface);
cairo_set_source_surface(cr, surface, 0, 0);
cairo_paint(cr);
cairo_surface_flush(svg_surface);
cairo_surface_finish(svg_surface);
Produces a SVG with content like:
<svg ...>
<image ... xlink:href="data:image/png;base64,iVBORw0K...>
</svg>
Drawing into the SVG, for example:
cairo_move_to(svg_surface, 0, 0)
cairo_line_to(svg_surface, 100, 100)
cairo_stroke(svg_surface)
Produces:
<svg ...>
<path style="fill:none;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M 10 10 L 100 100 "/>
</svg>
Is there a way to draw the original surface into a SVG, and get an output like this, instead of the embedded PNG?
I know I can record all the drawing operations done in the original surface and then replay them into the SVG, but I'm hoping there is a better way to do that. If not, what's the fundamental reason this cannot be done?
I know I can record all the drawing operations done in the original surface and then replay them into the SVG, but I'm hoping there is a better way to do that. If not, what's the fundamental reason this cannot be done?
A xlib surface refers to a drawable on the X11 server (either a window or a pixmap). A drawable contains pixels, so that is all that you can get from it.
Doing what you want would require keeping a log of all drawing operations that were ever down, which would lead to unbounded memory usage.
You can use a cairo recording surface to get such a log, but this only logs the drawing operations that are done to this surface, not to another surface. If you can, I would recommend to just call your code that does the actual drawing again when you want to get an SVG.