70 lines
1.8 KiB
C
70 lines
1.8 KiB
C
#include <librsvg/rsvg.h>
|
|
|
|
#include <hexagram/svg.h>
|
|
|
|
int hexagram_svg_render_to_surface(const char *path,
|
|
cairo_surface_t *surface,
|
|
double width,
|
|
double height,
|
|
const char *style) {
|
|
cairo_t *cr;
|
|
RsvgHandle *svg;
|
|
RsvgRectangle rect = {0, 0, width, height};
|
|
GError *err = NULL;
|
|
|
|
if ((cr = cairo_create(surface)) == NULL) {
|
|
goto error_cairo_create;
|
|
}
|
|
|
|
if ((svg = rsvg_handle_new_from_file(path, &err)) == NULL) {
|
|
goto error_rsvg_handle_new_from_file;
|
|
}
|
|
|
|
if (style) {
|
|
if (rsvg_handle_set_stylesheet(svg, (guint8 *)style, strlen(style), &err) != TRUE) {
|
|
goto error_rsvg_handle_set_stylesheet;
|
|
}
|
|
}
|
|
|
|
if (rsvg_handle_render_layer(svg, cr, NULL, &rect, &err) != TRUE) {
|
|
goto error_rsvg_handle_render_layer;
|
|
}
|
|
|
|
g_object_unref(svg);
|
|
cairo_destroy(cr);
|
|
|
|
return 0;
|
|
|
|
error_rsvg_handle_render_layer:
|
|
error_rsvg_handle_set_stylesheet:
|
|
g_object_unref(svg);
|
|
|
|
error_rsvg_handle_new_from_file:
|
|
cairo_destroy(cr);
|
|
|
|
error_cairo_create:
|
|
return -1;
|
|
}
|
|
|
|
cairo_surface_t *hexagram_svg_render_to_image(const char *path,
|
|
double width,
|
|
double height,
|
|
const char *style) {
|
|
cairo_surface_t *surface;
|
|
|
|
if ((surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height)) == NULL) {
|
|
goto error_cairo_image_surface_create;
|
|
}
|
|
|
|
if (hexagram_svg_render_to_surface(path, surface, width, height, style) < 0) {
|
|
goto error_render_to_surface;
|
|
}
|
|
|
|
return surface;
|
|
|
|
error_render_to_surface:
|
|
cairo_surface_destroy(surface);
|
|
|
|
error_cairo_image_surface_create:
|
|
return NULL;
|
|
}
|