From 10646b914b4d4f82f315e14d25de17dcd75dca34 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 3 Aug 2023 20:45:41 -0400 Subject: [PATCH] example.c: factor out svgtiny_diagram_from_path(). --- example.c | 107 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 44 deletions(-) diff --git a/example.c b/example.c index f582682..f6deced 100644 --- a/example.c +++ b/example.c @@ -65,63 +65,51 @@ void render_path(cairo_t *cr, float scale, struct svgtiny_shape *path) { } } - -int main(int argc, char** argv) { - int exit_code = 0; - - /* Initial viewport width and height */ - int width = 1024, height = 1024; - float scale = 1.0; - char* svgpath; - char* pngpath; +/* + * @brief Parse an SVG file into an svgtiny_diagram structure. + * + * @param path + * The path to the SVG file. + * + * @return If successful, a pointer to an @c svgtiny_diagram structure + * is returned; if not, @c NULL is returned. You are expected to @c + * svgtiny_free the result if it is valid. + */ +struct svgtiny_diagram* svgtiny_diagram_from_path(char* path, + int width, + int height) { + struct svgtiny_diagram *diagram; int fd; - size_t svgsize; + size_t bytecount; char *buffer; size_t bytesread; struct stat sb; - svgtiny_code code; - struct svgtiny_diagram *diagram; - cairo_surface_t *surface; - cairo_t *cr = 0; - cairo_status_t cr_status; - - unsigned int i; - - /* 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]; /* load file into memory buffer */ - fd = open(svgpath, O_RDONLY); + fd = open(path, O_RDONLY); if (fd == -1) { - perror(svgpath); - return 1; + perror(path); + return NULL; } if (fstat(fd, &sb)) { - perror(svgpath); - return 1; + perror(path); + return NULL; } - svgsize = sb.st_size; + bytecount = sb.st_size; - buffer = malloc(svgsize); + buffer = malloc(bytecount); if (!buffer) { - fprintf(stderr, "Unable to allocate %zd bytes\n", svgsize); - return 1; + fprintf(stderr, "Unable to allocate %zd bytes\n", bytecount); + return NULL; } - bytesread = read(fd, buffer, svgsize); - if (bytesread != svgsize) { - perror(svgpath); - return 1; + bytesread = read(fd, buffer, bytecount); + if (bytesread != bytecount) { + perror(path); + return NULL; } close(fd); @@ -130,10 +118,10 @@ int main(int argc, char** argv) { diagram = svgtiny_create(); if (!diagram) { fprintf(stderr, "svgtiny_create() failed\n"); - return 1; + return NULL; } - code = svgtiny_parse(diagram, buffer, svgsize, svgpath, width, height); + code = svgtiny_parse(diagram, buffer, bytecount, path, width, height); free(buffer); if (code != svgtiny_OK) { @@ -158,11 +146,42 @@ int main(int argc, char** argv) { break; } fprintf(stderr, "\n"); - return 1; + return NULL; } + return diagram; +} + +int main(int argc, char** argv) { + int exit_code = 0; + + char* svgpath; + char* pngpath; + int pngwidth = 1024, pngheight = 1024; + + float scale = 1.0; + struct svgtiny_diagram *diagram; + cairo_surface_t *surface; + cairo_t *cr = 0; + cairo_status_t cr_status; + + unsigned int i; + + /* 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]; + + diagram = svgtiny_diagram_from_path(svgpath, pngwidth, pngheight); - surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); + surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, + pngwidth, + pngheight); if (!surface) { fprintf(stderr, "cairo_image_surface_create failed\n"); exit_code = 1; -- 2.43.2