]>
gitweb.michael.orlitzky.com - libsvgtiny-pixbuf.git/blob - example.c
7 * Render an svgtiny path using cairo.
9 void render_path(cairo_t
*cr
, float scale
, struct svgtiny_shape
*path
) {
13 for (j
= 0; j
!= path
->path_length
; ) {
14 switch ((int) path
->path
[j
]) {
15 case svgtiny_PATH_MOVE
:
17 scale
* path
->path
[j
+ 1],
18 scale
* path
->path
[j
+ 2]);
21 case svgtiny_PATH_CLOSE
:
25 case svgtiny_PATH_LINE
:
27 scale
* path
->path
[j
+ 1],
28 scale
* path
->path
[j
+ 2]);
31 case svgtiny_PATH_BEZIER
:
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]);
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
);
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
);
64 int main(int argc
, char** argv
) {
67 /* Initial viewport width and height */
68 int width
= 1024, height
= 1024;
70 char* svgpath
= "Ghostscript_Tiger.svg";
71 char* pngpath
= "Ghostscript_Tiger.png";
72 size_t svgsize
= 68630;
79 struct svgtiny_diagram
*diagram
;
80 cairo_surface_t
*surface
;
82 cairo_status_t cr_status
;
86 /* load file into memory buffer */
87 fd
= fopen(svgpath
, "rb");
93 buffer
= malloc(svgsize
);
95 fprintf(stderr
, "Unable to allocate %zd bytes\n", svgsize
);
99 bytesread
= fread(buffer
, 1, svgsize
, fd
);
100 if (bytesread
!= svgsize
) {
107 /* create svgtiny object */
108 diagram
= svgtiny_create();
110 fprintf(stderr
, "svgtiny_create() failed\n");
114 code
= svgtiny_parse(diagram
, buffer
, svgsize
, svgpath
, width
, height
);
117 if (code
!= svgtiny_OK
) {
118 fprintf(stderr
, "svgtiny_parse failed: ");
120 case svgtiny_OUT_OF_MEMORY
:
121 fprintf(stderr
, "svgtiny_OUT_OF_MEMORY");
123 case svgtiny_LIBDOM_ERROR
:
124 fprintf(stderr
, "svgtiny_LIBDOM_ERROR");
126 case svgtiny_NOT_SVG
:
127 fprintf(stderr
, "svgtiny_NOT_SVG");
129 case svgtiny_SVG_ERROR
:
130 fprintf(stderr
, "svgtiny_SVG_ERROR: line %i: %s",
132 diagram
->error_message
);
135 fprintf(stderr
, "unknown svgtiny_code %i", code
);
138 fprintf(stderr
, "\n");
143 surface
= cairo_image_surface_create(CAIRO_FORMAT_RGB24
, width
, height
);
145 fprintf(stderr
, "cairo_image_surface_create failed\n");
150 cr
= cairo_create(surface
);
151 cr_status
= cairo_status(cr
);
152 if (cr_status
!= CAIRO_STATUS_SUCCESS
) {
154 "cairo_create failed: %s\n",
155 cairo_status_to_string(cr_status
));
160 cairo_set_source_rgb(cr
, 1, 1, 1);
163 /* Loop through the shapes in the diagram... */
164 for (i
= 0; i
!= diagram
->shape_count
; i
++) {
166 /* If this shape is a path, just render it. */
167 if (diagram
->shape
[i
].path
) {
168 render_path(cr
, scale
, &diagram
->shape
[i
]);
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
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
182 scale
* diagram
->shape
[i
].text_x
,
183 scale
* diagram
->shape
[i
].text_y
);
186 cairo_show_text(cr
, diagram
->shape
[i
].text
);
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
));
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
));
211 cairo_surface_destroy(surface
);
212 svgtiny_free(diagram
);