kernel - disklabel64 - Adjust partition base to be physically aligned
[dragonfly.git] / usr.bin / evtranalyze / svg.c
blobe3d8bfb47df04453a7c4fef7542d2613b07b8b5e
1 /*
2 * Copyright (c) 2009, 2010 Aggelos Economopoulos. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
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
13 * distribution.
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
29 * SUCH DAMAGE.
32 #include <assert.h>
33 #include <math.h>
34 #include <stdio.h>
35 #include <stdlib.h>
37 #include "xml.h"
38 #include "svg.h"
40 enum {
41 MAX_VALSTR_LEN = 30,
44 struct svg_rect {
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];
53 struct svg_text {
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];
63 struct svg_line {
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];
72 struct svg_document {
73 xml_document_t xml;
74 const char *css;
75 struct xml_element svg;
76 struct xml_attribute svg_attrs[2];
77 struct svg_text text;
80 static char default_css[] =
81 "<![CDATA["
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;}]]>";
87 static
88 int
89 svg_transform_print(svg_transform_t tf, char *buf, size_t len)
91 static double eps = 0.0001;
92 char *p;
93 int c;
95 if (!tf) {
96 assert(len >= 1);
97 buf[0] = '\0';
98 return 0;
100 p = buf;
101 if ((fabs(tf->tx) > eps) && (fabs(tf->ty) > eps)) {
102 c = snprintf(buf, len, "translate(%.20lf,%.20lf)", tf->tx,
103 tf->ty);
104 len -= c;
105 if (len <= 0)
106 return !0;
107 p += c;
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);
112 len -= c;
113 if (len <= 0)
114 return !0;
115 p += c;
117 if (fabs(tf->rot) > eps) {
118 c = snprintf(p, len, "%srotate(%.2lf)",
119 (p == buf) ? "" : " ", tf->rot);
120 len -= c;
121 if (len <= 0)
122 return !0;
123 p += c;
125 return 0;
128 static
129 void
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);
141 if (cl) {
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
150 * of the document.
152 struct svg_rect *
153 svg_rect_new(const char *cl)
155 struct svg_rect *r;
157 if (!(r = malloc(sizeof(*r))))
158 return r;
159 svg_rect_init(r, cl);
160 return r;
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);
178 return 0;
181 static
182 void
183 svg_text_init(struct svg_text *text, const char *cl)
185 xml_elem_init(&text->el, "text");
186 #if remove
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);
191 #endif
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);
197 if (cl) {
198 xml_attribute_init(&text->cl, "class", cl);
199 xml_elem_set_attribute(&text->el, &text->cl);
204 struct svg_text *
205 svg_text_new(const char *cl)
207 svg_text_t text;
209 if (!(text = malloc(sizeof(*text))))
210 return text;
211 svg_text_init(text, cl);
212 return text;
216 svg_text_draw(svg_document_t doc, svg_text_t text, svg_transform_t tf,
217 const char *str, double fontsize)
219 #if remove
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]);
224 #endif
225 snprintf(&text->fontsize_val[0], sizeof(text->fontsize_val), "%.20lf",
226 fontsize);
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)))
230 return !0;
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);
235 return 0;
238 static
239 void
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);
255 if (cl) {
256 xml_attribute_init(&line->cl, "class", cl);
257 xml_elem_set_attribute(&line->el, &line->cl);
262 struct svg_line *
263 svg_line_new(const char *cl)
265 svg_line_t line;
267 if (!(line = malloc(sizeof(*line))))
268 return line;
269 svg_line_init(line, cl);
270 return line;
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)))
293 return !0;
294 xml_elem_closed(doc->xml, &line->el);
295 return 0;
298 svg_document_t
299 svg_document_create(const char *path)
301 svg_document_t svg;
302 struct xml_element style, defs;
303 struct xml_attribute type;
305 if (!(svg = malloc(sizeof(*svg))))
306 return NULL;
307 if (!(svg->xml = xml_document_create(path))) {
308 free(svg);
309 return NULL;
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);
328 return svg;
332 svg_document_close(svg_document_t svg)
334 xml_elem_close(svg->xml, &svg->svg);
335 xml_document_close(svg->xml);
336 return 0;