]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - svgtiny_test.c
Implement svgtiny_free() and fix memory usage.
[libsvgtiny.git] / svgtiny_test.c
1 /*
2 * This file is part of Libsvgtiny
3 * Licensed under the MIT License,
4 * http://opensource.org/licenses/mit-license.php
5 * Copyright 2008 James Bursa <james@semichrome.net>
6 */
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include "svgtiny.h"
13
14
15 int main(int argc, char *argv[])
16 {
17 FILE *fd;
18 struct stat sb;
19 char *buffer;
20 size_t size;
21 size_t n;
22 struct svgtiny_diagram *diagram;
23 svgtiny_code code;
24
25 if (argc != 2) {
26 fprintf(stderr, "Usage: %s FILE\n", argv[0]);
27 return 1;
28 }
29
30 /* load file into memory buffer */
31 fd = fopen(argv[1], "rb");
32 if (!fd) {
33 perror(argv[1]);
34 return 1;
35 }
36
37 if (stat(argv[1], &sb)) {
38 perror(argv[1]);
39 return 1;
40 }
41 size = sb.st_size;
42
43 buffer = malloc(size);
44 if (!buffer) {
45 fprintf(stderr, "Unable to allocate %lld bytes\n",
46 (long long) size);
47 return 1;
48 }
49
50 n = fread(buffer, 1, size, fd);
51 if (n != size) {
52 perror(argv[1]);
53 return 1;
54 }
55
56 fclose(fd);
57
58 /* create svgtiny object */
59 diagram = svgtiny_create();
60 if (!diagram) {
61 fprintf(stderr, "svgtiny_create failed\n");
62 return 1;
63 }
64
65 /* parse */
66 code = svgtiny_parse(diagram, buffer, size, argv[1], 1000, 1000);
67 if (code != svgtiny_OK)
68 fprintf(stderr, "svgtiny_parse failed: %i\n", code);
69
70 free(buffer);
71
72 printf("viewbox 0 0 %i %i\n", diagram->width, diagram->height);
73
74 for (unsigned int i = 0; i != diagram->shape_count; i++) {
75 if (diagram->shape[i].fill == svgtiny_TRANSPARENT)
76 printf("fill none ");
77 else
78 printf("fill #%.6x ", diagram->shape[i].fill);
79 if (diagram->shape[i].stroke == svgtiny_TRANSPARENT)
80 printf("stroke none ");
81 else
82 printf("stroke #%.6x ", diagram->shape[i].stroke);
83 if (diagram->shape[i].path) {
84 printf("path '");
85 for (unsigned int j = 0;
86 j != diagram->shape[i].path_length; ) {
87 switch ((int) diagram->shape[i].path[j]) {
88 case svgtiny_PATH_MOVE:
89 printf("M %g %g ",
90 diagram->shape[i].path[j + 1],
91 diagram->shape[i].path[j + 2]);
92 j += 3;
93 break;
94 case svgtiny_PATH_CLOSE:
95 printf("Z ");
96 j += 1;
97 break;
98 case svgtiny_PATH_LINE:
99 printf("L %g %g ",
100 diagram->shape[i].path[j + 1],
101 diagram->shape[i].path[j + 2]);
102 j += 3;
103 break;
104 case svgtiny_PATH_BEZIER:
105 printf("C %g %g %g %g %g %g ",
106 diagram->shape[i].path[j + 1],
107 diagram->shape[i].path[j + 2],
108 diagram->shape[i].path[j + 3],
109 diagram->shape[i].path[j + 4],
110 diagram->shape[i].path[j + 5],
111 diagram->shape[i].path[j + 6]);
112 j += 7;
113 break;
114 default:
115 printf("error ");
116 j += 1;
117 }
118 }
119 printf("' ");
120 } else if (diagram->shape[i].text) {
121 printf("text %g %g '%s' ",
122 diagram->shape[i].text_x,
123 diagram->shape[i].text_y,
124 diagram->shape[i].text);
125 }
126 printf("\n");
127 }
128
129 svgtiny_free(diagram);
130
131 return 0;
132 }
133