2 * Copyright (c) 2009, 2010 Aggelos Economopoulos. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
14 * 3. Neither the name of The DragonFly Project nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific, prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 struct xml_element el;
46 struct xml_attribute x, y, w, h, cl;
47 char x_val[MAX_VALSTR_LEN];
48 char y_val[MAX_VALSTR_LEN];
49 char w_val[MAX_VALSTR_LEN];
50 char h_val[MAX_VALSTR_LEN];
54 struct xml_element el;
55 struct xml_attribute x, y, cl;
56 struct xml_attribute fontsize, transform;
57 char x_val[MAX_VALSTR_LEN];
58 char y_val[MAX_VALSTR_LEN];
59 char fontsize_val[MAX_VALSTR_LEN];
60 char transform_val[MAX_VALSTR_LEN * 4];
64 struct xml_element el;
65 struct xml_attribute x1, y1, x2, y2, cl;
66 struct xml_attribute transform;
67 char x1_val[MAX_VALSTR_LEN], y1_val[MAX_VALSTR_LEN];
68 char x2_val[MAX_VALSTR_LEN], y2_val[MAX_VALSTR_LEN];
69 char transform_val[MAX_VALSTR_LEN * 6];
75 struct xml_element svg;
76 struct xml_attribute svg_attrs[2];
80 static char default_css[] =
82 "rect.generic { fill: green; stroke: black; stroke-width: 0.01;}"
83 "rect.thread { fill: yellow; stroke: black; stroke-width: 0.01;}"
84 "rect.inactive { fill: grey; stroke: black; stroke-width: 0.01;}"
85 "text.generic { fill: black; stroke: none;}]]>";
89 svg_transform_print(svg_transform_t tf, char *buf, size_t len)
91 static double eps = 0.0001;
101 if ((fabs(tf->tx) > eps) && (fabs(tf->ty) > eps)) {
102 c = snprintf(buf, len, "translate(%.20lf,%.20lf)", tf->tx,
109 if ((fabs(tf->sx - 1) > eps) && (fabs(tf->sy - 1) > eps)) {
110 c = snprintf(p, len, "%sscale(%.20lf,%.20lf)",
111 (p == buf) ? "" : " ", tf->sx, tf->sy);
117 if (fabs(tf->rot) > eps) {
118 c = snprintf(p, len, "%srotate(%.2lf)",
119 (p == buf) ? "" : " ", tf->rot);
130 svg_rect_init(struct svg_rect *rect, const char *cl)
132 xml_elem_init(&rect->el, "rect");
133 xml_attribute_init(&rect->x, "x", NULL);
134 xml_elem_set_attribute(&rect->el, &rect->x);
135 xml_attribute_init(&rect->y, "y", NULL);
136 xml_elem_set_attribute(&rect->el, &rect->y);
137 xml_attribute_init(&rect->w, "width", NULL);
138 xml_elem_set_attribute(&rect->el, &rect->w);
139 xml_attribute_init(&rect->h, "height", NULL);
140 xml_elem_set_attribute(&rect->el, &rect->h);
142 xml_attribute_init(&rect->cl, "class", cl);
143 xml_elem_set_attribute(&rect->el, &rect->cl);
148 * In the future, we might want to stick the rectangle in the
149 * <defs> element at this point and then <use> it in the rest
153 svg_rect_new(const char *cl)
157 if (!(r = malloc(sizeof(*r))))
159 svg_rect_init(r, cl);
165 svg_rect_draw(svg_document_t doc, struct svg_rect *rect, double x,
166 double y, double w, double h)
168 snprintf(&rect->x_val[0], sizeof(rect->x_val), "%.20lf", x);
169 xml_attribute_set_value(&rect->x, &rect->x_val[0]);
170 snprintf(&rect->y_val[0], sizeof(rect->y_val), "%lf", y);
171 xml_attribute_set_value(&rect->y, &rect->y_val[0]);
172 snprintf(&rect->w_val[0], sizeof(rect->w_val), "%.20lf", w);
173 xml_attribute_set_value(&rect->w, &rect->w_val[0]);
174 snprintf(&rect->h_val[0], sizeof(rect->h_val), "%lf", h);
175 xml_attribute_set_value(&rect->h, &rect->h_val[0]);
177 xml_elem_closed(doc->xml, &rect->el);
183 svg_text_init(struct svg_text *text, const char *cl)
185 xml_elem_init(&text->el, "text");
187 xml_attribute_init(&text->x, "x", NULL);
188 xml_elem_set_attribute(&text->el, &text->x);
189 xml_attribute_init(&text->y, "y", NULL);
190 xml_elem_set_attribute(&text->el, &text->y);
192 xml_attribute_init(&text->fontsize, "font-size", NULL);
193 xml_elem_set_attribute(&text->el, &text->fontsize);
194 xml_attribute_init(&text->transform, "transform", NULL);
195 xml_elem_set_attribute(&text->el, &text->transform);
198 xml_attribute_init(&text->cl, "class", cl);
199 xml_elem_set_attribute(&text->el, &text->cl);
205 svg_text_new(const char *cl)
209 if (!(text = malloc(sizeof(*text))))
211 svg_text_init(text, cl);
216 svg_text_draw(svg_document_t doc, svg_text_t text, svg_transform_t tf,
217 const char *str, double fontsize)
220 snprintf(&text->x_val[0], sizeof(text->x_val), "%.20lf", x);
221 xml_attribute_set_value(&text->x, &text->x_val[0]);
222 snprintf(&text->y_val[0], sizeof(text->y_val), "%.20lf", y);
223 xml_attribute_set_value(&text->y, &text->y_val[0]);
225 snprintf(&text->fontsize_val[0], sizeof(text->fontsize_val), "%.20lf",
227 xml_attribute_set_value(&text->fontsize, &text->fontsize_val[0]);
228 if (svg_transform_print(tf, &text->transform_val[0],
229 sizeof(text->transform_val)))
231 xml_attribute_set_value(&text->transform, &text->transform_val[0]);
232 xml_elem_set_value(&text->el, str);
234 xml_elem_closed(doc->xml, &text->el);
240 svg_line_init(struct svg_line *line, const char *cl)
242 xml_elem_init(&line->el, "line");
243 xml_attribute_init(&line->x1, "x1", NULL);
244 xml_elem_set_attribute(&line->el, &line->x1);
245 xml_attribute_init(&line->x2, "x2", NULL);
246 xml_elem_set_attribute(&line->el, &line->x2);
247 xml_attribute_init(&line->y1, "y1", NULL);
248 xml_elem_set_attribute(&line->el, &line->y1);
249 xml_attribute_init(&line->y2, "y2", NULL);
250 xml_elem_set_attribute(&line->el, &line->y2);
252 xml_attribute_init(&line->transform, "transform", NULL);
253 xml_elem_set_attribute(&line->el, &line->transform);
256 xml_attribute_init(&line->cl, "class", cl);
257 xml_elem_set_attribute(&line->el, &line->cl);
263 svg_line_new(const char *cl)
267 if (!(line = malloc(sizeof(*line))))
269 svg_line_init(line, cl);
274 svg_line_draw(svg_document_t doc, svg_line_t line, double x1, double _y1,
275 double x2, double y2, svg_transform_t tf)
277 snprintf(&line->x1_val[0], sizeof(line->x1_val), "%.20lf", x1);
278 xml_attribute_set_value(&line->x1, &line->x1_val[0]);
280 snprintf(&line->x2_val[0], sizeof(line->x2_val), "%.20lf", x2);
281 xml_attribute_set_value(&line->x2, &line->x2_val[0]);
283 snprintf(&line->y1_val[0], sizeof(line->y1_val), "%.10lf", _y1);
284 xml_attribute_set_value(&line->y1, &line->y1_val[0]);
286 snprintf(&line->y2_val[0], sizeof(line->y2_val), "%.20lf", y2);
287 xml_attribute_set_value(&line->y2, &line->y2_val[0]);
289 xml_attribute_set_value(&line->transform, &line->transform_val[0]);
290 if (svg_transform_print(tf,
291 &line->transform_val[0],
292 sizeof(line->transform_val)))
294 xml_elem_closed(doc->xml, &line->el);
299 svg_document_create(const char *path)
302 struct xml_element style, defs;
303 struct xml_attribute type;
305 if (!(svg = malloc(sizeof(*svg))))
307 if (!(svg->xml = xml_document_create(path))) {
311 svg->css = &default_css[0];
312 xml_attribute_init(&type, "type", "text/css");
313 xml_elem_init(&defs, "defs");
314 xml_elem_init(&style, "style");
315 xml_elem_set_attribute(&style, &type);
316 xml_elem_init(&svg->svg, "svg");
317 xml_attribute_init(&svg->svg_attrs[0], "version", "1.1");
318 xml_elem_set_attribute(&svg->svg, &svg->svg_attrs[0]);
319 xml_attribute_init(&svg->svg_attrs[1], "xmlns",
320 "http://www.w3.org/2000/svg");
321 xml_elem_set_attribute(&svg->svg, &svg->svg_attrs[1]);
322 xml_elem_begin(svg->xml, &svg->svg);
323 xml_elem_begin(svg->xml, &defs);
324 xml_elem_set_value(&style, svg->css);
325 xml_elem_closed(svg->xml, &style);
326 xml_elem_close(svg->xml, &defs);
332 svg_document_close(svg_document_t svg)
334 xml_elem_close(svg->xml, &svg->svg);
335 xml_document_close(svg->xml);