1 /****************************************************************************
2 * RRDtool 1.3.1 Copyright by Tobi Oetiker, 1997-2008
3 ****************************************************************************
4 * rrd_gfx.c graphics wrapper for rrdtool
5 **************************************************************************/
9 /* stupid MSVC doesnt support variadic macros = no debug for now! */
14 # define RRDPRINTF(...) fprintf(stderr, __VA_ARGS__);
16 # define RRDPRINTF(...)
21 #include "rrd_graph.h"
24 /* create a new line */
34 gfx_dashed_line(im, X0, Y0, X1, Y1, width, color, 0, 0);
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,
72 /* create a new area */
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,
96 /* add a point to a line or to an area */
102 cairo_t *cr = im->cr;
104 gfx_area_fit(im, &x, &y);
105 cairo_line_to(cr, x, y);
111 cairo_t *cr = im->cr;
113 cairo_close_path(cr);
117 /* create a text node */
118 static PangoLayout *gfx_prep_text(
128 PangoFontDescription *font_desc;
129 cairo_t *cr = im->cr;
131 /* for performance reasons we might
132 want todo that only once ... tabs will always
135 long tab_count = strlen(text);
136 long tab_shift = fmod(x, tabwidth);
137 int border = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
141 PangoTabArray *tab_array;
142 PangoContext *pango_context;
144 tab_array = pango_tab_array_new(tab_count, (gboolean) (1));
145 for (i = 1; i <= tab_count; i++) {
146 pango_tab_array_set_tab(tab_array,
148 tabwidth * i - tab_shift + border);
151 cairo_set_source_rgba(cr, color.red, color.green, color.blue,
153 layout = pango_cairo_create_layout(cr);
154 pango_context = pango_layout_get_context(layout);
155 pango_cairo_context_set_font_options(pango_context, im->font_options);
156 pango_cairo_context_set_resolution(pango_context, 100);
158 /* pango_cairo_update_context(cr, pango_context); */
160 pango_layout_set_tabs(layout, tab_array);
161 font_desc = pango_font_description_from_string(font);
162 pango_font_description_set_size(font_desc, size * PANGO_SCALE);
163 pango_layout_set_font_description(layout, font_desc);
165 /* pango expects the string to be utf-8 encoded */
166 utf8_text = g_locale_to_utf8((const gchar *)text, -1, NULL, NULL, NULL);
168 /* In case of an error, i.e. utf8_text == NULL (locale settings messed
169 * up?), we fall back to a possible "invalid UTF-8 string" warning instead
170 * of provoking a failed assertion in libpango. */
172 pango_layout_set_markup(layout, utf8_text ? utf8_text : text, -1);
174 pango_layout_set_text(layout, utf8_text ? utf8_text : text, -1);
181 double gfx_get_text_width(
190 PangoRectangle log_rect;
191 gfx_color_t color = { 0, 0, 0, 0 };
192 layout = gfx_prep_text(im, start, color, font, size, tabwidth, text);
193 pango_layout_get_pixel_extents(layout, NULL, &log_rect);
194 pango_tab_array_free(pango_layout_get_tabs(layout));
195 g_object_unref(layout);
196 return log_rect.width;
208 enum gfx_h_align_en h_align,
209 enum gfx_v_align_en v_align,
213 PangoRectangle log_rect;
214 PangoRectangle ink_rect;
215 cairo_t *cr = im->cr;
220 cairo_translate(cr, x, y);
221 /* gfx_line(cr,-2,0,2,0,1,color);
222 gfx_line(cr,0,-2,0,2,1,color); */
223 layout = gfx_prep_text(im, x, color, font, size, tabwidth, text);
224 pango_layout_get_pixel_extents(layout, &ink_rect, &log_rect);
225 cairo_rotate(cr, -angle * G_PI / 180.0);
229 sx -= log_rect.width;
232 sx -= log_rect.width / 2;
244 sy -= log_rect.height / 2;
247 sy -= log_rect.height;
252 pango_cairo_update_layout(cr, layout);
253 cairo_move_to(cr, sx, sy);
254 pango_cairo_show_layout(cr, layout);
255 pango_tab_array_free(pango_layout_get_tabs(layout));
256 g_object_unref(layout);
262 struct gfx_color_t gfx_hex_to_col(
263 long unsigned int color)
265 struct gfx_color_t gfx_color;
267 gfx_color.red = 1.0 / 255.0 * ((color & 0xff000000) >> (3 * 8));
268 gfx_color.green = 1.0 / 255.0 * ((color & 0x00ff0000) >> (2 * 8));
269 gfx_color.blue = 1.0 / 255.0 * ((color & 0x0000ff00) >> (1 * 8));
270 gfx_color.alpha = 1.0 / 255.0 * (color & 0x000000ff);
281 cairo_t *cr = im->cr;
287 cairo_user_to_device(cr, x, y);
288 line_width = cairo_get_line_width(cr);
289 line_height = line_width;
290 cairo_user_to_device_distance(cr, &line_width, &line_height);
291 line_width = line_width / 2.0 - (long) (line_width / 2.0);
292 line_height = line_height / 2.0 - (long) (line_height / 2.0);
293 *x = (double) ((long) (*x + 0.5)) - line_width;
294 *y = (double) ((long) (*y + 0.5)) + line_height;
295 cairo_device_to_user(cr, x, y);
305 cairo_t *cr = im->cr;
309 cairo_user_to_device(cr, x, y);
310 *x = (double) ((long) (*x + 0.5));
311 *y = (double) ((long) (*y + 0.5));
312 cairo_device_to_user(cr, x, y);