#include #include #include #include static resvg_render_tree *tree = NULL; static resvg_options opt; void draw_cb (const char *path) { if (!tree || !path) return; cairo_rectangle_t docExtents = { 0, 0, 0, 0 }; resvg_rect viewBox = resvg_get_image_viewbox(tree); docExtents.x = viewBox.x; docExtents.y = viewBox.y; docExtents.width = viewBox.width; docExtents.height = viewBox.height; cairo_surface_t* cairoSurface = cairo_recording_surface_create( CAIRO_CONTENT_COLOR_ALPHA, &docExtents ); cairo_t* cr = cairo_create( cairoSurface ); resvg_size svgSize = { viewBox.width, viewBox.height }; resvg_cairo_render_to_canvas(tree, &opt, svgSize, cr); cairo_destroy( cr ); cairo_surface_write_to_png( cairoSurface, path ); double x0, y0, w, h; cairo_recording_surface_ink_extents( cairoSurface, &x0, &y0, &w, &h); printf("cairo-ink-extent: {%f, %f, %f, %f}\n", x0, y0, w, h); cairo_surface_destroy( cairoSurface ); } void print_bbox() { resvg_rect viewBox = resvg_get_image_viewbox(tree); printf("viewBox: {%f, %f, %f, %f}\n", viewBox.x, viewBox.y, viewBox.width, viewBox.height); resvg_rect boundingBox; resvg_get_image_bbox(tree, &boundingBox); printf("boundingBox: {%f, %f, %f, %f}\n", boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height); } void parse_doc (const char *path) { resvg_init_options(&opt); opt.path = path; opt.font_family = "Liberation Sans"; opt.languages = "en"; int err = resvg_parse_tree_from_file(path, &opt, &tree); if (err != RESVG_OK) { fprintf(stderr, "Error id: %i\n", err); abort(); } } int main(int argc, char **argv) { if (argc < 3) abort(); parse_doc(argv[1]); print_bbox(); draw_cb(argv[2]); }