]>
gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - 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 */
12 * Render an svgtiny path using cairo.
14 void render_path(cairo_t
*cr
, float scale
, struct svgtiny_shape
*path
) {
18 for (j
= 0; j
!= path
->path_length
; ) {
19 switch ((int) path
->path
[j
]) {
20 case svgtiny_PATH_MOVE
:
22 scale
* path
->path
[j
+ 1],
23 scale
* path
->path
[j
+ 2]);
26 case svgtiny_PATH_CLOSE
:
30 case svgtiny_PATH_LINE
:
32 scale
* path
->path
[j
+ 1],
33 scale
* path
->path
[j
+ 2]);
36 case svgtiny_PATH_BEZIER
:
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]);
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
);
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
);
69 int main(int argc
, char** argv
) {
72 /* Initial viewport width and height */
73 int width
= 1024, height
= 1024;
85 struct svgtiny_diagram
*diagram
;
86 cairo_surface_t
*surface
;
88 cairo_status_t cr_status
;
92 /* Parse arguments, and maybe print usage */
94 printf("Usage: %s INPUT OUTPUT\n", argv
[0]);
95 printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
102 /* load file into memory buffer */
103 fd
= open(svgpath
, O_RDONLY
);
109 if (fstat(fd
, &sb
)) {
113 svgsize
= sb
.st_size
;
115 buffer
= malloc(svgsize
);
117 fprintf(stderr
, "Unable to allocate %zd bytes\n", svgsize
);
121 bytesread
= read(fd
, buffer
, svgsize
);
122 if (bytesread
!= svgsize
) {
129 /* create svgtiny object */
130 diagram
= svgtiny_create();
132 fprintf(stderr
, "svgtiny_create() failed\n");
136 code
= svgtiny_parse(diagram
, buffer
, svgsize
, svgpath
, width
, height
);
139 if (code
!= svgtiny_OK
) {
140 fprintf(stderr
, "svgtiny_parse failed: ");
142 case svgtiny_OUT_OF_MEMORY
:
143 fprintf(stderr
, "svgtiny_OUT_OF_MEMORY");
145 case svgtiny_LIBDOM_ERROR
:
146 fprintf(stderr
, "svgtiny_LIBDOM_ERROR");
148 case svgtiny_NOT_SVG
:
149 fprintf(stderr
, "svgtiny_NOT_SVG");
151 case svgtiny_SVG_ERROR
:
152 fprintf(stderr
, "svgtiny_SVG_ERROR: line %i: %s",
154 diagram
->error_message
);
157 fprintf(stderr
, "unknown svgtiny_code %i", code
);
160 fprintf(stderr
, "\n");
165 surface
= cairo_image_surface_create(CAIRO_FORMAT_RGB24
, width
, height
);
167 fprintf(stderr
, "cairo_image_surface_create failed\n");
172 cr
= cairo_create(surface
);
173 cr_status
= cairo_status(cr
);
174 if (cr_status
!= CAIRO_STATUS_SUCCESS
) {
176 "cairo_create failed: %s\n",
177 cairo_status_to_string(cr_status
));
182 cairo_set_source_rgb(cr
, 1, 1, 1);
185 /* Loop through the shapes in the diagram... */
186 for (i
= 0; i
!= diagram
->shape_count
; i
++) {
188 /* If this shape is a path, just render it. */
189 if (diagram
->shape
[i
].path
) {
190 render_path(cr
, scale
, &diagram
->shape
[i
]);
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
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
204 scale
* diagram
->shape
[i
].text_x
,
205 scale
* diagram
->shape
[i
].text_y
);
208 cairo_show_text(cr
, diagram
->shape
[i
].text
);
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
));
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
));
233 cairo_surface_destroy(surface
);
234 svgtiny_free(diagram
);