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