From 924cf8cb1c1bfd25cf00899af30d817c9e87767f Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 4 Aug 2023 13:59:57 -0400 Subject: [PATCH] example.c: refactor to eliminate copy/pasted code. --- example.c | 94 ++++++++++++++++++------------------------------------- 1 file changed, 31 insertions(+), 63 deletions(-) diff --git a/example.c b/example.c index aa71e23..5c1f5ff 100644 --- a/example.c +++ b/example.c @@ -250,69 +250,6 @@ static cairo_t* cairo_context_from_diagram(diagram_t* diagram) { return cr; } -int main(int argc, char** argv) { - char* svgpath; - char* pngpath; - FILE* fp; - - diagram_t* diagram; - cairo_t* cr = 0; - - GdkPixbuf* pb; - - /* Parse arguments, and maybe print usage */ - if (argc < 3) { - printf("Usage: %s INPUT OUTPUT\n", argv[0]); - printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n"); - return 2; - } - - svgpath = argv[1]; - pngpath = argv[2]; - - fp = fopen(svgpath, "rb"); - if (!fp) { - perror(svgpath); - return 1; - } - - diagram = svgtiny_diagram_from_file(fp, VIEWPORT_WIDTH, VIEWPORT_HEIGHT); - if (!diagram) { - return 1; - } - - cr = cairo_context_from_diagram(diagram); - if (!cr) { - svgtiny_free(diagram); - return 1; - } - - /* We're using the viewport width and height and not the diagram - * width/height for the image. The diagram can be of a different - * size and aspect ratio than the viewport, and our main use case is - * for icons that are generally square and reasonably sized. If the - * diagram is "small," then we want to scale it up until it fits - * nicely in the viewport before rendering it. That's as opposed to - * rendering the image small, and letting GDK scale it up. Of course - * this reasoning makes the assumption that the viewport is usually - * larger than the diagram. - */ - pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr), - 0, - 0, - VIEWPORT_WIDTH, - VIEWPORT_HEIGHT); - - - if (pb) { - gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL); - g_object_unref(pb); - } - - return 0; -} - - static GdkPixbuf* gdk_pixbuf_from_svg_file_stream(FILE *fp, GError **error) { diagram_t* diagram; cairo_t* cr = 0; @@ -397,3 +334,34 @@ void fill_info(GdkPixbufFormat* info) { info->license = "AGPL3"; } + +int main(int argc, char** argv) { + char* svgpath; + char* pngpath; + FILE* fp; + GdkPixbuf* pb; + + /* Parse arguments, and maybe print usage */ + if (argc < 3) { + printf("Usage: %s INPUT OUTPUT\n", argv[0]); + printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n"); + return 2; + } + + svgpath = argv[1]; + pngpath = argv[2]; + + fp = fopen(svgpath, "rb"); + if (!fp) { + perror(svgpath); + return 1; + } + + pb = gdk_pixbuf_from_svg_file_stream(fp, NULL); + if (pb) { + gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL); + g_object_unref(pb); + } + + return 0; +} -- 2.43.2