Tizen 2.0 Release
[framework/graphics/cairo.git] / util / trace-to-xml.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <cairo-xml.h>
6 #include <cairo-script-interpreter.h>
7
8 #include <stdio.h>
9 #include <string.h>
10
11 static cairo_surface_t *
12 _surface_create (void *_closure,
13                  cairo_content_t content,
14                  double width, double height,
15                  long uid)
16 {
17     cairo_surface_t **closure = _closure;
18     cairo_surface_t *surface;
19     cairo_rectangle_t extents;
20
21     extents.x = extents.y = 0;
22     extents.width  = width;
23     extents.height = height;
24     surface = cairo_recording_surface_create (content, &extents);
25     if (*closure == NULL)
26         *closure = cairo_surface_reference (surface);
27
28     return surface;
29 }
30
31 static cairo_status_t
32 stdio_write (void *closure, const unsigned char *data, unsigned len)
33 {
34     if (fwrite (data, len, 1, closure) == 1)
35         return CAIRO_STATUS_SUCCESS;
36     else
37         return CAIRO_STATUS_WRITE_ERROR;
38 }
39
40 int
41 main (int argc, char **argv)
42 {
43     cairo_surface_t *surface = NULL;
44     const cairo_script_interpreter_hooks_t hooks = {
45         .closure = &surface,
46         .surface_create = _surface_create,
47     };
48     cairo_script_interpreter_t *csi;
49     FILE *in = stdin, *out = stdout;
50
51     if (argc >= 2 && strcmp (argv[1], "-"))
52         in = fopen (argv[1], "r");
53     if (argc >= 3 && strcmp (argv[2], "-"))
54         out = fopen (argv[2], "w");
55
56     csi = cairo_script_interpreter_create ();
57     cairo_script_interpreter_install_hooks (csi, &hooks);
58     cairo_script_interpreter_feed_stream (csi, in);
59     cairo_script_interpreter_finish (csi);
60     cairo_script_interpreter_destroy (csi);
61
62     if (surface != NULL) {
63         cairo_device_t *xml;
64
65         xml = cairo_xml_create_for_stream (stdio_write, out);
66         cairo_xml_for_recording_surface (xml, surface);
67         cairo_device_destroy (xml);
68
69         cairo_surface_destroy (surface);
70     }
71
72     if (in != stdin)
73         fclose (in);
74     if (out != stdout)
75         fclose (out);
76
77     return 0;
78 }