be536c5183020778822e2d1ac885c748f817c314
1 /****************************************************************************
2 * RRDtool 1.3.1 Copyright by Tobi Oetiker, 1997-2008
3 ****************************************************************************
4 * rrd_gfx.c graphics wrapper for rrdtool
5 **************************************************************************/
7 /* #define DEBUG */
9 /* stupid MSVC doesnt support variadic macros = no debug for now! */
10 #ifdef _MSC_VER
11 # define RRDPRINTF()
12 #else
13 # ifdef DEBUG
14 # define RRDPRINTF(...) fprintf(stderr, __VA_ARGS__);
15 # else
16 # define RRDPRINTF(...)
17 # endif /* DEBUG */
18 #endif /* _MSC_VER */
20 #include "rrd_tool.h"
21 #include "rrd_graph.h"
24 /* create a new line */
25 void gfx_line(
26 image_desc_t *im,
27 double X0,
28 double Y0,
29 double X1,
30 double Y1,
31 double width,
32 gfx_color_t color)
33 {
34 gfx_dashed_line(im, X0, Y0, X1, Y1, width, color, 0, 0);
35 }
37 void gfx_dashed_line(
38 image_desc_t *im,
39 double X0,
40 double Y0,
41 double X1,
42 double Y1,
43 double width,
44 gfx_color_t color,
45 double dash_on,
46 double dash_off)
47 {
48 cairo_t *cr = im->cr;
49 double dashes[2];
50 double x = 0;
51 double y = 0;
53 dashes[0] = dash_on;
54 dashes[1] = dash_off;
56 cairo_save(cr);
57 cairo_new_path(cr);
58 cairo_set_line_width(cr, width);
59 gfx_line_fit(im, &x, &y);
60 gfx_line_fit(im, &X0, &Y0);
61 cairo_move_to(cr, X0, Y0);
62 gfx_line_fit(im, &X1, &Y1);
63 cairo_line_to(cr, X1, Y1);
64 if (dash_on > 0 || dash_off > 0)
65 cairo_set_dash(cr, dashes, 2, x);
66 cairo_set_source_rgba(cr, color.red, color.green, color.blue,
67 color.alpha);
68 cairo_stroke(cr);
69 cairo_restore(cr);
70 }
72 /* create a new area */
73 void gfx_new_area(
74 image_desc_t *im,
75 double X0,
76 double Y0,
77 double X1,
78 double Y1,
79 double X2,
80 double Y2,
81 gfx_color_t color)
82 {
83 cairo_t *cr = im->cr;
85 cairo_new_path(cr);
86 gfx_area_fit(im, &X0, &Y0);
87 cairo_move_to(cr, X0, Y0);
88 gfx_area_fit(im, &X1, &Y1);
89 cairo_line_to(cr, X1, Y1);
90 gfx_area_fit(im, &X2, &Y2);
91 cairo_line_to(cr, X2, Y2);
92 cairo_set_source_rgba(cr, color.red, color.green, color.blue,
93 color.alpha);
94 }
96 /* add a point to a line or to an area */
97 void gfx_add_point(
98 image_desc_t *im,
99 double x,
100 double y)
101 {
102 cairo_t *cr = im->cr;
104 gfx_area_fit(im, &x, &y);
105 cairo_line_to(cr, x, y);
106 }
108 void gfx_close_path(
109 image_desc_t *im)
110 {
111 cairo_t *cr = im->cr;
113 cairo_close_path(cr);
114 cairo_fill(cr);
115 }
117 /* create a text node */
118 static PangoLayout *gfx_prep_text(
119 image_desc_t *im,
120 double x,
121 gfx_color_t color,
122 PangoFontDescription *font_desc,
123 double tabwidth,
124 const char *text)
125 {
126 PangoLayout *layout = im->layout;
127 PangoFontDescription *pfd;
128 cairo_t *cr = im->cr;
130 static double last_tabwidth = -1;
134 /* for performance reasons we might
135 want todo that only once ... tabs will always
136 be the same */
137 long i;
138 long tab_count = strlen(text);
139 long tab_shift = fmod(x, tabwidth);
140 int border = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
142 gchar *utf8_text;
144 if (last_tabwidth < 0 || last_tabwidth != tabwidth){
145 PangoTabArray *tab_array;
146 // fprintf(stderr,"t");
147 last_tabwidth = tabwidth;
148 tab_array = pango_tab_array_new(tab_count, (gboolean) (1));
149 for (i = 1; i <= tab_count; i++) {
150 pango_tab_array_set_tab(tab_array,
151 i, PANGO_TAB_LEFT,
152 tabwidth * i - tab_shift + border);
153 }
154 pango_layout_set_tabs(layout, tab_array);
155 pango_tab_array_free(tab_array);
156 }
157 pfd = pango_layout_get_font_description(layout);
158 if (pfd && pango_font_description_equal (pfd,font_desc)){
159 pango_layout_set_font_description(layout, font_desc);
160 }
162 cairo_new_path(cr);
163 cairo_set_source_rgba(cr, color.red, color.green, color.blue,
164 color.alpha);
165 /* layout = pango_cairo_create_layout(cr); */
167 // pango_cairo_context_set_font_options(pango_context, im->font_options);
168 // pango_cairo_context_set_resolution(pango_context, 100);
170 /* pango_cairo_update_context(cr, pango_context); */
173 /* pango expects the string to be utf-8 encoded */
174 utf8_text = g_locale_to_utf8((const gchar *) text, -1, NULL, NULL, NULL);
176 /* In case of an error, i.e. utf8_text == NULL (locale settings messed
177 * up?), we fall back to a possible "invalid UTF-8 string" warning instead
178 * of provoking a failed assertion in libpango. */
179 if (im->with_markup)
180 pango_layout_set_markup(layout, utf8_text ? utf8_text : text, -1);
181 else
182 pango_layout_set_text(layout, utf8_text ? utf8_text : text, -1);
184 g_free(utf8_text);
185 return layout;
186 }
188 /* Size Text Node */
189 double gfx_get_text_width(
190 image_desc_t *im,
191 double start,
192 PangoFontDescription *font_desc,
193 double tabwidth,
194 char *text)
195 {
196 PangoLayout *layout;
197 PangoRectangle log_rect;
198 gfx_color_t color = { 0, 0, 0, 0 };
199 layout = gfx_prep_text(im, start, color, font_desc, tabwidth, text);
200 pango_layout_get_pixel_extents(layout, NULL, &log_rect);
201 /* g_object_unref(layout); */
202 return log_rect.width;
203 }
205 void gfx_text(
206 image_desc_t *im,
207 double x,
208 double y,
209 gfx_color_t color,
210 PangoFontDescription *font_desc,
211 double tabwidth,
212 double angle,
213 enum gfx_h_align_en h_align,
214 enum gfx_v_align_en v_align,
215 const char *text)
216 {
217 PangoLayout *layout;
218 PangoRectangle log_rect;
219 cairo_t *cr = im->cr;
220 double sx = 0;
221 double sy = 0;
223 cairo_save(cr);
224 cairo_translate(cr, x, y);
225 /* gfx_line(cr,-2,0,2,0,1,color);
226 gfx_line(cr,0,-2,0,2,1,color); */
227 layout = gfx_prep_text(im, x, color, font_desc, tabwidth, text);
228 pango_layout_get_pixel_extents(layout, NULL, &log_rect);
229 cairo_rotate(cr, -angle * G_PI / 180.0);
230 sx = log_rect.x;
231 switch (h_align) {
232 case GFX_H_RIGHT:
233 sx -= log_rect.width;
234 break;
235 case GFX_H_CENTER:
236 sx -= log_rect.width / 2;
237 break;
238 case GFX_H_LEFT:
239 break;
240 case GFX_H_NULL:
241 break;
242 }
243 sy = log_rect.y;
244 switch (v_align) {
245 case GFX_V_TOP:
246 break;
247 case GFX_V_CENTER:
248 sy -= log_rect.height / 2;
249 break;
250 case GFX_V_BOTTOM:
251 sy -= log_rect.height;
252 break;
253 case GFX_V_NULL:
254 break;
255 }
256 pango_cairo_update_layout(cr, layout);
257 cairo_move_to(cr, sx, sy);
258 pango_cairo_show_layout(cr, layout);
259 /* g_object_unref(layout); */
260 cairo_restore(cr);
262 }
264 /* convert color */
265 struct gfx_color_t gfx_hex_to_col(
266 long unsigned int color)
267 {
268 struct gfx_color_t gfx_color;
270 gfx_color.red = 1.0 / 255.0 * ((color & 0xff000000) >> (3 * 8));
271 gfx_color.green = 1.0 / 255.0 * ((color & 0x00ff0000) >> (2 * 8));
272 gfx_color.blue = 1.0 / 255.0 * ((color & 0x0000ff00) >> (1 * 8));
273 gfx_color.alpha = 1.0 / 255.0 * (color & 0x000000ff);
274 return gfx_color;
275 }
277 /* gridfit_lines */
279 void gfx_line_fit(
280 image_desc_t *im,
281 double *x,
282 double *y)
283 {
284 cairo_t *cr = im->cr;
285 double line_width;
286 double line_height;
288 if (!im->gridfit)
289 return;
290 cairo_user_to_device(cr, x, y);
291 line_width = cairo_get_line_width(cr);
292 line_height = line_width;
293 cairo_user_to_device_distance(cr, &line_width, &line_height);
294 line_width = line_width / 2.0 - (long) (line_width / 2.0);
295 line_height = line_height / 2.0 - (long) (line_height / 2.0);
296 *x = (double) ((long) (*x + 0.5)) - line_width;
297 *y = (double) ((long) (*y + 0.5)) + line_height;
298 cairo_device_to_user(cr, x, y);
299 }
301 /* gridfit_areas */
303 void gfx_area_fit(
304 image_desc_t *im,
305 double *x,
306 double *y)
307 {
308 cairo_t *cr = im->cr;
310 if (!im->gridfit)
311 return;
312 cairo_user_to_device(cr, x, y);
313 *x = (double) ((long) (*x + 0.5));
314 *y = (double) ((long) (*y + 0.5));
315 cairo_device_to_user(cr, x, y);
316 }