Git init
[framework/graphics/cairo.git] / util / xml-to-trace.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <expat.h>
4 #include <assert.h>
5
6 struct trace {
7     FILE *stream;
8     char tail_buf[80];
9     const char *tail;
10     int surface_depth;
11 };
12
13 static void
14 start_element (void *closure,
15                const char *element,
16                const char **attr)
17 {
18     struct trace *trace = closure;
19
20     if (strcmp (element, "surface") == 0) {
21         const char *content = "COLOR_ALPHA";
22         const char *width = NULL;
23         const char *height = NULL;
24
25         while (*attr) {
26             if (strcmp (*attr, "content") == 0) {
27                 content = *++attr;
28             } else if (strcmp (*attr, "width") == 0) {
29                 width = *++attr;
30             } else if (strcmp (*attr, "height") == 0) {
31                 height = *++attr;
32             } else {
33                 fprintf (stderr, "unknown surface attribute '%s'\n", *attr);
34                 attr++;
35             }
36             attr++;
37         }
38
39         fprintf (trace->stream, "<< /content //%s", content);
40         if (width != NULL && height != NULL) {
41             fprintf (trace->stream,
42                      " /width %s /height %s",
43                      width, height);
44         }
45         if (trace->surface_depth++ == 0)
46             fprintf (trace->stream, " >> surface context\n");
47         else
48             fprintf (trace->stream, " >> surface dup context\n");
49     } else if (strcmp (element, "image") == 0) {
50         const char *format = "ARGB24";
51         const char *width = NULL;
52         const char *height = NULL;
53
54         while (*attr) {
55             if (strcmp (*attr, "format") == 0) {
56                 format = *++attr;
57             } else if (strcmp (*attr, "width") == 0) {
58                 width = *++attr;
59             } else if (strcmp (*attr, "height") == 0) {
60                 height = *++attr;
61             } else {
62                 fprintf (stderr, "unknown image attribute '%s'\n", *attr);
63                 attr++;
64             }
65             attr++;
66         }
67
68         fprintf (trace->stream,
69                  "<< /format //%s /width %s /height %s /mime-type (image/png) /source <{",
70                  format, width, height);
71         assert (trace->tail == NULL);
72         trace->tail = "}> >> image pattern\n";
73     } else if (strcmp (element, "solid") == 0) {
74         trace->tail = " rgba\n";
75     } else if (strcmp (element, "linear") == 0) {
76         const char *x1 = NULL;
77         const char *x2 = NULL;
78         const char *y1 = NULL;
79         const char *y2 = NULL;
80
81         while (*attr) {
82             if (strcmp (*attr, "x1") == 0) {
83                 x1 = *++attr;
84             } else if (strcmp (*attr, "x2") == 0) {
85                 x2 = *++attr;
86             } else if (strcmp (*attr, "y1") == 0) {
87                 y1 = *++attr;
88             } else if (strcmp (*attr, "y2") == 0) {
89                 y2 = *++attr;
90             } else {
91                 fprintf (stderr, "unknown linear attribute '%s'\n", *attr);
92                 attr++;
93             }
94             attr++;
95         }
96
97         fprintf (trace->stream, "%s %s %s %s linear\n", x1, y1, x2, y2);
98     } else if (strcmp (element, "radial") == 0) {
99         const char *x1 = NULL;
100         const char *y1 = NULL;
101         const char *r1 = NULL;
102         const char *y2 = NULL;
103         const char *x2 = NULL;
104         const char *r2 = NULL;
105
106         while (*attr) {
107             if (strcmp (*attr, "x1") == 0) {
108                 x1 = *++attr;
109             } else if (strcmp (*attr, "y1") == 0) {
110                 y1 = *++attr;
111             } else if (strcmp (*attr, "r1") == 0) {
112                 r1 = *++attr;
113             } else if (strcmp (*attr, "x2") == 0) {
114                 x2 = *++attr;
115             } else if (strcmp (*attr, "y2") == 0) {
116                 y2 = *++attr;
117             } else if (strcmp (*attr, "r2") == 0) {
118                 r2 = *++attr;
119             } else {
120                 fprintf (stderr, "unknown radial attribute '%s'\n", *attr);
121                 attr++;
122             }
123             attr++;
124         }
125
126         fprintf (trace->stream,
127                  "%s %s %s %s %s %s radial\n",
128                  x1, y1, r1, x2, y2, r2);
129     } else if (strcmp (element, "matrix") == 0) {
130         fprintf (trace->stream, "[ ");
131         trace->tail = " ] set-matrix\n";
132     } else if (strcmp (element, "extend") == 0) {
133         trace->tail = " set-extend\n";
134     } else if (strcmp (element, "filter") == 0) {
135         trace->tail = " set-filter\n";
136     } else if (strcmp (element, "operator") == 0) {
137         trace->tail = " set-operator\n";
138     } else if (strcmp (element, "tolerance") == 0) {
139         trace->tail = " set-tolerance\n";
140     } else if (strcmp (element, "fill-rule") == 0) {
141         trace->tail = " set-fill-rule\n";
142     } else if (strcmp (element, "line-cap") == 0) {
143         trace->tail = " set-line-cap\n";
144     } else if (strcmp (element, "line-join") == 0) {
145         trace->tail = " set-line-join\n";
146     } else if (strcmp (element, "line-width") == 0) {
147         trace->tail = " set-line-width\n";
148     } else if (strcmp (element, "miter-limit") == 0) {
149         trace->tail = " set-miter-limit\n";
150     } else if (strcmp (element, "antialias") == 0) {
151         trace->tail = " set-antialias\n";
152     } else if (strcmp (element, "color-stop") == 0) {
153         trace->tail = " add-color-stop\n";
154     } else if (strcmp (element, "path") == 0) {
155         /* need to reset the matrix to identity before the path */
156         fprintf (trace->stream, "identity set-matrix ");
157         trace->tail = "\n";
158     } else if (strcmp (element, "dash") == 0) {
159         const char *offset = "0";
160
161         while (*attr) {
162             if (strcmp (*attr, "offset") == 0) {
163                 offset = *++attr;
164             }
165             attr++;
166         }
167
168         fprintf (trace->stream, "[");
169         sprintf (trace->tail_buf, "] %s set-dash\n", offset);
170         trace->tail = trace->tail_buf;
171     } else {
172     }
173 }
174
175 static void
176 cdata (void *closure,
177        const XML_Char *s,
178        int len)
179 {
180     struct trace *trace = closure;
181
182     if (trace->tail)
183         fwrite (s, len, 1, trace->stream);
184 }
185
186 static void
187 end_element (void *closure,
188              const char *element)
189 {
190     struct trace *trace = closure;
191
192     if (trace->tail) {
193         fprintf (trace->stream, "%s", trace->tail);
194         trace->tail = NULL;
195     }
196
197     if (strcmp (element, "paint") == 0) {
198         fprintf (trace->stream, "paint\n");
199     } else if (strcmp (element, "mask") == 0) {
200         fprintf (trace->stream, "mask\n");
201     } else if (strcmp (element, "stroke") == 0) {
202         fprintf (trace->stream, "stroke\n");
203     } else if (strcmp (element, "fill") == 0) {
204         fprintf (trace->stream, "fill\n");
205     } else if (strcmp (element, "glyphs") == 0) {
206         fprintf (trace->stream, "show-glyphs\n");
207     } else if (strcmp (element, "clip") == 0) {
208         fprintf (trace->stream, "clip\n");
209     } else if (strcmp (element, "source-pattern") == 0) {
210         fprintf (trace->stream, "set-source\n");
211     } else if (strcmp (element, "mask-pattern") == 0) {
212     } else if (strcmp (element, "surface") == 0) {
213         if (--trace->surface_depth == 0)
214             fprintf (trace->stream, "pop\n");
215         else
216             fprintf (trace->stream, "pop pattern\n");
217     }
218 }
219
220 int
221 main (int argc, char **argv)
222 {
223     struct trace trace;
224     XML_Parser p;
225     char buf[8192];
226     int done = 0;
227     FILE *in = stdin;
228
229     trace.stream = stdout;
230     trace.tail = NULL;
231     trace.surface_depth = 0;
232
233     if (argc >= 2 && strcmp (argv[1], "-"))
234         in = fopen (argv[1], "r");
235     if (argc >= 3 && strcmp (argv[2], "-"))
236         trace.stream = fopen (argv[2], "w");
237
238     p = XML_ParserCreate (NULL);
239     XML_SetUserData (p, &trace);
240     XML_SetElementHandler (p, start_element, end_element);
241     XML_SetCharacterDataHandler (p, cdata);
242     do {
243         int len;
244
245         len = fread (buf, 1, sizeof (buf), in);
246         done = feof (stdin);
247
248         if (XML_Parse (p, buf, len, done) == XML_STATUS_ERROR) {
249             fprintf (stderr, "Parse error at line %ld:\n%s\n",
250                      XML_GetCurrentLineNumber (p),
251                      XML_ErrorString (XML_GetErrorCode (p)));
252             exit (-1);
253         }
254     } while (! done);
255     XML_ParserFree (p);
256
257     if (in != stdin)
258         fclose (in);
259     if (trace.stream != stdout)
260         fclose (trace.stream);
261
262     return 0;
263 }