]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - example.c
example.c: take INPUT/OUTPUT filenames as arguments.
[libsvgtiny-pixbuf.git] / example.c
1 #include <fcntl.h> /* open */
2 #include <stdio.h> /* printf, fprintf */
3 #include <stdlib.h> /* malloc */
4 #include <sys/stat.h> /* fstat */
5 #include <unistd.h> /* read */
6
7 #include <cairo.h>
8 #include <svgtiny.h>
9
10
11 /**
12 * Render an svgtiny path using cairo.
13 */
14 void render_path(cairo_t *cr, float scale, struct svgtiny_shape *path) {
15 unsigned int j;
16
17 cairo_new_path(cr);
18 for (j = 0; j != path->path_length; ) {
19 switch ((int) path->path[j]) {
20 case svgtiny_PATH_MOVE:
21 cairo_move_to(cr,
22 scale * path->path[j + 1],
23 scale * path->path[j + 2]);
24 j += 3;
25 break;
26 case svgtiny_PATH_CLOSE:
27 cairo_close_path(cr);
28 j += 1;
29 break;
30 case svgtiny_PATH_LINE:
31 cairo_line_to(cr,
32 scale * path->path[j + 1],
33 scale * path->path[j + 2]);
34 j += 3;
35 break;
36 case svgtiny_PATH_BEZIER:
37 cairo_curve_to(cr,
38 scale * path->path[j + 1],
39 scale * path->path[j + 2],
40 scale * path->path[j + 3],
41 scale * path->path[j + 4],
42 scale * path->path[j + 5],
43 scale * path->path[j + 6]);
44 j += 7;
45 break;
46 default:
47 printf("error ");
48 j += 1;
49 }
50 }
51 if (path->fill != svgtiny_TRANSPARENT) {
52 cairo_set_source_rgb(cr,
53 svgtiny_RED(path->fill) / 255.0,
54 svgtiny_GREEN(path->fill) / 255.0,
55 svgtiny_BLUE(path->fill) / 255.0);
56 cairo_fill_preserve(cr);
57 }
58 if (path->stroke != svgtiny_TRANSPARENT) {
59 cairo_set_source_rgb(cr,
60 svgtiny_RED(path->stroke) / 255.0,
61 svgtiny_GREEN(path->stroke) / 255.0,
62 svgtiny_BLUE(path->stroke) / 255.0);
63 cairo_set_line_width(cr, scale * path->stroke_width);
64 cairo_stroke_preserve(cr);
65 }
66 }
67
68
69 int main(int argc, char** argv) {
70 int exit_code = 0;
71
72 /* Initial viewport width and height */
73 int width = 1024, height = 1024;
74 float scale = 1.0;
75 char* svgpath;
76 char* pngpath;
77
78 int fd;
79 size_t svgsize;
80 char *buffer;
81 size_t bytesread;
82 struct stat sb;
83
84 svgtiny_code code;
85 struct svgtiny_diagram *diagram;
86 cairo_surface_t *surface;
87 cairo_t *cr = 0;
88 cairo_status_t cr_status;
89
90 unsigned int i;
91
92 /* Parse arguments, and maybe print usage */
93 if (argc < 3) {
94 printf("Usage: %s INPUT OUTPUT\n", argv[0]);
95 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
96 return 2;
97 }
98
99 svgpath = argv[1];
100 pngpath = argv[2];
101
102 /* load file into memory buffer */
103 fd = open(svgpath, O_RDONLY);
104 if (fd == -1) {
105 perror(svgpath);
106 return 1;
107 }
108
109 if (fstat(fd, &sb)) {
110 perror(svgpath);
111 return 1;
112 }
113 svgsize = sb.st_size;
114
115 buffer = malloc(svgsize);
116 if (!buffer) {
117 fprintf(stderr, "Unable to allocate %zd bytes\n", svgsize);
118 return 1;
119 }
120
121 bytesread = read(fd, buffer, svgsize);
122 if (bytesread != svgsize) {
123 perror(svgpath);
124 return 1;
125 }
126 close(fd);
127
128
129 /* create svgtiny object */
130 diagram = svgtiny_create();
131 if (!diagram) {
132 fprintf(stderr, "svgtiny_create() failed\n");
133 return 1;
134 }
135
136 code = svgtiny_parse(diagram, buffer, svgsize, svgpath, width, height);
137 free(buffer);
138
139 if (code != svgtiny_OK) {
140 fprintf(stderr, "svgtiny_parse failed: ");
141 switch (code) {
142 case svgtiny_OUT_OF_MEMORY:
143 fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
144 break;
145 case svgtiny_LIBDOM_ERROR:
146 fprintf(stderr, "svgtiny_LIBDOM_ERROR");
147 break;
148 case svgtiny_NOT_SVG:
149 fprintf(stderr, "svgtiny_NOT_SVG");
150 break;
151 case svgtiny_SVG_ERROR:
152 fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
153 diagram->error_line,
154 diagram->error_message);
155 break;
156 default:
157 fprintf(stderr, "unknown svgtiny_code %i", code);
158 break;
159 }
160 fprintf(stderr, "\n");
161 return 1;
162 }
163
164
165 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
166 if (!surface) {
167 fprintf(stderr, "cairo_image_surface_create failed\n");
168 exit_code = 1;
169 goto cleanup;
170 }
171
172 cr = cairo_create(surface);
173 cr_status = cairo_status(cr);
174 if (cr_status != CAIRO_STATUS_SUCCESS) {
175 fprintf(stderr,
176 "cairo_create failed: %s\n",
177 cairo_status_to_string(cr_status));
178 exit_code = 1;
179 goto cleanup;
180 }
181
182 cairo_set_source_rgb(cr, 1, 1, 1);
183 cairo_paint(cr);
184
185 /* Loop through the shapes in the diagram... */
186 for (i = 0; i != diagram->shape_count; i++) {
187
188 /* If this shape is a path, just render it. */
189 if (diagram->shape[i].path) {
190 render_path(cr, scale, &diagram->shape[i]);
191 }
192
193 /* If this shape is text... */
194 if (diagram->shape[i].text) {
195 /* Figure out what color to use from the R/G/B components of the
196 shape's stroke. */
197 cairo_set_source_rgb(cr,
198 svgtiny_RED(diagram->shape[i].stroke) / 255.0,
199 svgtiny_GREEN(diagram->shape[i].stroke) / 255.0,
200 svgtiny_BLUE(diagram->shape[i].stroke) / 255.0);
201 /* Then move to the actual position of the text within the
202 shape... */
203 cairo_move_to(cr,
204 scale * diagram->shape[i].text_x,
205 scale * diagram->shape[i].text_y);
206
207 /* and draw it. */
208 cairo_show_text(cr, diagram->shape[i].text);
209 }
210 }
211
212 /* Check the status again. */
213 cr_status = cairo_status(cr);
214 if (cr_status != CAIRO_STATUS_SUCCESS) {
215 fprintf(stderr, "cairo error: %s\n",
216 cairo_status_to_string(cr_status));
217 exit_code = 1;
218 goto cleanup;
219 }
220
221 cr_status = cairo_surface_write_to_png(surface, pngpath);
222 if (cr_status != CAIRO_STATUS_SUCCESS) {
223 fprintf(stderr, "cairo error: %s\n",
224 cairo_status_to_string(cr_status));
225 exit_code = 1;
226 goto cleanup;
227 }
228
229 cleanup:
230 if (cr) {
231 cairo_destroy(cr);
232 }
233 cairo_surface_destroy(surface);
234 svgtiny_free(diagram);
235
236 return exit_code;
237 }