From: Michael Orlitzky Date: Fri, 4 Aug 2023 00:45:41 +0000 (-0400) Subject: example.c: factor out svgtiny_diagram_from_path(). X-Git-Tag: 0.0.1~52 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=10646b914b4d4f82f315e14d25de17dcd75dca34;p=libsvgtiny-pixbuf.git example.c: factor out svgtiny_diagram_from_path(). --- 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;