]> gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - example.c
example.c: render to diagram width/height instead of the viewport's.
[libsvgtiny-pixbuf.git] / example.c
1 #include <stdio.h> /* fopen, fprintf, fread, printf */
2 #include <stdlib.h> /* malloc */
3
4 #include <cairo.h>
5 #include <gdk/gdk.h>
6 #include <gdk-pixbuf/gdk-pixbuf.h>
7 #include <svgtiny.h>
8
9 /*
10 * The width and height of the viewport that we'll render the SVG
11 * into. The final "picture" may not actually be this size; based on
12 * the height, width, viewBox, and preserveAspectRatio attributes in
13 * the SVG itself, libsvgtiny may scale, stretch, offset, etc. the
14 * paths to make them fit nicely into the viewport.
15 */
16 #define VIEWPORT_WIDTH 512
17 #define VIEWPORT_HEIGHT 512
18
19 /* Convenient typedefs for libsvgtiny */
20 typedef struct svgtiny_diagram diagram_t;
21 typedef struct svgtiny_shape shape_t;
22
23
24 /**
25 * @brief Render an svgtiny path using cairo.
26 *
27 * @param cr
28 * A pointer to a valid cairo context.
29 *
30 * @param path
31 * A pointer to an svgtiny shape that will be rendered on the
32 * cairo context's target surface.
33 */
34 static void render_path(cairo_t* cr, shape_t* path) {
35 unsigned int j;
36
37 cairo_new_path(cr);
38 for (j = 0; j != path->path_length; ) {
39 switch ((int) path->path[j]) {
40 case svgtiny_PATH_MOVE:
41 cairo_move_to(cr,
42 path->path[j + 1],
43 path->path[j + 2]);
44 j += 3;
45 break;
46 case svgtiny_PATH_CLOSE:
47 cairo_close_path(cr);
48 j += 1;
49 break;
50 case svgtiny_PATH_LINE:
51 cairo_line_to(cr,
52 path->path[j + 1],
53 path->path[j + 2]);
54 j += 3;
55 break;
56 case svgtiny_PATH_BEZIER:
57 cairo_curve_to(cr,
58 path->path[j + 1],
59 path->path[j + 2],
60 path->path[j + 3],
61 path->path[j + 4],
62 path->path[j + 5],
63 path->path[j + 6]);
64 j += 7;
65 break;
66 default:
67 printf("error ");
68 j += 1;
69 }
70 }
71 if (path->fill != svgtiny_TRANSPARENT) {
72 cairo_set_source_rgb(cr,
73 svgtiny_RED(path->fill) / 255.0,
74 svgtiny_GREEN(path->fill) / 255.0,
75 svgtiny_BLUE(path->fill) / 255.0);
76 cairo_fill_preserve(cr);
77 }
78 if (path->stroke != svgtiny_TRANSPARENT) {
79 cairo_set_source_rgb(cr,
80 svgtiny_RED(path->stroke) / 255.0,
81 svgtiny_GREEN(path->stroke) / 255.0,
82 svgtiny_BLUE(path->stroke) / 255.0);
83 cairo_set_line_width(cr, path->stroke_width);
84 cairo_stroke_preserve(cr);
85 }
86 }
87
88 /**
89 * @brief Parse an SVG file into a diagram_t structure.
90 *
91 * @param fp
92 * A pointer to an open file stream.
93 *
94 * @return If successful, a pointer to a @c diagram_t structure is
95 * returned; if not, @c NULL is returned. You are expected to @c
96 * svgtiny_free the result if it is valid.
97 */
98 static diagram_t* svgtiny_diagram_from_file(FILE* fp, int width, int height) {
99 diagram_t* diagram;
100
101 size_t bytecount;
102 char* buffer;
103 size_t bytesread;
104 svgtiny_code code;
105
106 /* Find the size of the file stream */
107 fseek(fp, 0L, SEEK_END);
108 bytecount = ftell(fp);
109 rewind(fp);
110
111 buffer = malloc(bytecount);
112 if (!buffer) {
113 fprintf(stderr, "Unable to allocate %zd bytes\n", bytecount);
114 return NULL;
115 }
116
117 bytesread = fread(buffer, 1, bytecount, fp);
118 if (bytesread != bytecount) {
119 fprintf(stderr, "Read only %zd of %zd bytes from stream\n",
120 bytesread,
121 bytecount);
122 }
123 fclose(fp);
124
125 diagram = svgtiny_create();
126 if (!diagram) {
127 fprintf(stderr, "svgtiny_create() failed\n");
128 return NULL;
129 }
130
131 code = svgtiny_parse(diagram, buffer, bytecount, "", width, height);
132 free(buffer);
133
134 if (code != svgtiny_OK) {
135 fprintf(stderr, "svgtiny_parse failed: ");
136 switch (code) {
137 case svgtiny_OUT_OF_MEMORY:
138 fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
139 break;
140 case svgtiny_LIBDOM_ERROR:
141 fprintf(stderr, "svgtiny_LIBDOM_ERROR");
142 break;
143 case svgtiny_NOT_SVG:
144 fprintf(stderr, "svgtiny_NOT_SVG");
145 break;
146 case svgtiny_SVG_ERROR:
147 fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
148 diagram->error_line,
149 diagram->error_message);
150 break;
151 default:
152 fprintf(stderr, "unknown svgtiny_code %i", code);
153 break;
154 }
155 fprintf(stderr, "\n");
156 return NULL;
157 }
158
159 return diagram;
160 }
161
162 /**
163 * @brief Create a cairo context from a libsvgtiny diagram.
164 *
165 * @param diagram
166 * A pointer to a valid libsvgtiny diagram.
167 *
168 * @return If successful, a pointer to a @c cairo_t context structure
169 * is returned; if not, @c NULL is returned. You are expected to @c
170 * cairo_destroy the result if it is valid.
171 */
172 static cairo_t* cairo_context_from_diagram(diagram_t* diagram) {
173 cairo_t* cr;
174 cairo_surface_t* surface;
175 cairo_status_t crs;
176 unsigned int i;
177
178 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
179 diagram->width,
180 diagram->height);
181
182 crs = cairo_surface_status(surface);
183 if (crs != CAIRO_STATUS_SUCCESS) {
184 fprintf(stderr,
185 "cairo_image_surface_create failed: %s\n",
186 cairo_status_to_string(crs));
187 cairo_surface_destroy(surface);
188 return NULL;
189 }
190
191 cr = cairo_create(surface);
192 crs = cairo_status(cr);
193
194 /* Immediately destroy the surface which is now accessible as
195 cr->target */
196 cairo_surface_destroy(surface);
197
198 if (crs != CAIRO_STATUS_SUCCESS) {
199 fprintf(stderr,
200 "cairo_create failed: %s\n",
201 cairo_status_to_string(crs));
202 cairo_destroy(cr);
203 return NULL;
204 }
205
206 cairo_set_source_rgb(cr, 1, 1, 1);
207 cairo_paint(cr);
208
209 /* Loop through the shapes in the diagram... */
210 for (i = 0; i != diagram->shape_count; i++) {
211
212 /* If this shape is a path, just render it. */
213 if (diagram->shape[i].path) {
214 render_path(cr, &diagram->shape[i]);
215 }
216
217 /* If this shape is text... */
218 if (diagram->shape[i].text) {
219 /* Figure out what color to use from the R/G/B components of the
220 shape's stroke. */
221 cairo_set_source_rgb(cr,
222 svgtiny_RED(diagram->shape[i].stroke) / 255.0,
223 svgtiny_GREEN(diagram->shape[i].stroke) / 255.0,
224 svgtiny_BLUE(diagram->shape[i].stroke) / 255.0);
225 /* Then move to the actual position of the text within the
226 shape... */
227 cairo_move_to(cr,
228 diagram->shape[i].text_x,
229 diagram->shape[i].text_y);
230
231 /* and draw it. */
232 cairo_show_text(cr, diagram->shape[i].text);
233 }
234 }
235
236
237 /* Check the status again just for good measure? */
238 crs = cairo_status(cr);
239 if (crs != CAIRO_STATUS_SUCCESS) {
240 fprintf(stderr,
241 "cairo error: %s\n",
242 cairo_status_to_string(crs));
243 cairo_destroy(cr);
244 return NULL;
245 }
246
247 return cr;
248 }
249
250 int main(int argc, char** argv) {
251 char* svgpath;
252 char* pngpath;
253 FILE* fp;
254
255 diagram_t* diagram;
256 cairo_t* cr = 0;
257
258 GdkPixbuf* pb;
259
260 /* Parse arguments, and maybe print usage */
261 if (argc < 3) {
262 printf("Usage: %s INPUT OUTPUT\n", argv[0]);
263 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
264 return 2;
265 }
266
267 svgpath = argv[1];
268 pngpath = argv[2];
269
270 fp = fopen(svgpath, "rb");
271 if (!fp) {
272 perror(svgpath);
273 return 1;
274 }
275
276 diagram = svgtiny_diagram_from_file(fp, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
277 if (!diagram) {
278 return 1;
279 }
280
281 cr = cairo_context_from_diagram(diagram);
282 if (!cr) {
283 svgtiny_free(diagram);
284 return 1;
285 }
286
287 /* We're using the diagram width and height and not the viewport
288 * width/height for the image. This has the potential to create an
289 * image with a different size and aspect ratio than the viewport,
290 * but since our viewport is entirely made-up... I don't know. This
291 * relies on libsvgtiny being good at scaling/stretching/etc an SVG
292 * that may only have partial width/height/viewBox information.
293 */
294 pb = gdk_pixbuf_get_from_surface(cairo_get_target(cr),
295 0,
296 0,
297 diagram->width,
298 diagram->height);
299
300
301 if (pb) {
302 gdk_pixbuf_save(pb, pngpath, "png", NULL, NULL);
303 g_object_unref(pb);
304 }
305
306 return 0;
307 }