}
}
-
-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);
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) {
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;