summaryrefslogtreecommitdiff
path: root/util/trace-to-xml.c
blob: 763a56569ea2ce942202f7139f9670d9f26437a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <cairo-xml.h>
#include <cairo-script-interpreter.h>

#include <stdio.h>
#include <string.h>

static cairo_surface_t *
_surface_create (void *_closure,
		 cairo_content_t content,
		 double width, double height)
{
    cairo_surface_t **closure = _closure;
    cairo_surface_t *surface;
    cairo_rectangle_t extents;

    extents.x = extents.y = 0;
    extents.width  = width;
    extents.height = height;
    surface = cairo_meta_surface_create (content, &extents);
    if (*closure == NULL)
	*closure = cairo_surface_reference (surface);

    return surface;
}

static cairo_status_t
stdio_write (void *closure, const unsigned char *data, unsigned len)
{
    if (fwrite (data, len, 1, closure) == 1)
	return CAIRO_STATUS_SUCCESS;
    else
	return CAIRO_STATUS_WRITE_ERROR;
}

int
main (int argc, char **argv)
{
    cairo_surface_t *surface = NULL;
    const cairo_script_interpreter_hooks_t hooks = {
	.closure = &surface,
	.surface_create = _surface_create,
    };
    cairo_script_interpreter_t *csi;
    FILE *in = stdin, *out = stdout;

    if (argc >= 2 && strcmp (argv[1], "-"))
	in = fopen (argv[1], "r");
    if (argc >= 3 && strcmp (argv[2], "-"))
	out = fopen (argv[2], "w");

    csi = cairo_script_interpreter_create ();
    cairo_script_interpreter_install_hooks (csi, &hooks);
    cairo_script_interpreter_feed_stream (csi, in);
    cairo_script_interpreter_finish (csi);
    cairo_script_interpreter_destroy (csi);

    if (surface != NULL) {
	cairo_xml_t *xml;

	xml = cairo_xml_create_for_stream (stdio_write, out);
	cairo_xml_for_meta_surface (xml, surface);
	cairo_xml_destroy (xml);

	cairo_surface_destroy (surface);
    }

    if (in != stdin)
	fclose (in);
    if (out != stdout)
	fclose (out);

    return 0;
}