+#include <fcntl.h> /* open */
+#include <stdio.h> /* printf, fprintf */
+#include <stdlib.h> /* malloc */
+#include <sys/stat.h> /* fstat */
+#include <unistd.h> /* read */
+
#include <cairo.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <svgtiny.h>
+
/**
* Render an svgtiny path using cairo.
*/
/* Initial viewport width and height */
int width = 1024, height = 1024;
float scale = 1.0;
- char* svgpath = "Ghostscript_Tiger.svg";
- char* pngpath = "Ghostscript_Tiger.png";
- size_t svgsize = 68630;
+ char* svgpath;
+ char* pngpath;
- FILE *fd;
+ int fd;
+ size_t svgsize;
char *buffer;
size_t bytesread;
+ struct stat sb;
svgtiny_code code;
struct svgtiny_diagram *diagram;
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 = fopen(svgpath, "rb");
- if (!fd) {
+ fd = open(svgpath, O_RDONLY);
+ if (fd == -1) {
+ perror(svgpath);
+ return 1;
+ }
+
+ if (fstat(fd, &sb)) {
perror(svgpath);
return 1;
}
+ svgsize = sb.st_size;
buffer = malloc(svgsize);
if (!buffer) {
return 1;
}
- bytesread = fread(buffer, 1, svgsize, fd);
+ bytesread = read(fd, buffer, svgsize);
if (bytesread != svgsize) {
perror(svgpath);
return 1;
}
- fclose(fd);
+ close(fd);
/* create svgtiny object */