1 /****************************************************************************
2 * RRDtool 1.2.23 Copyright by Tobi Oetiker, 1997-2007
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);
49 double dashes[] = { dash_on, dash_off };
55 cairo_set_line_width(cr, width);
56 gfx_line_fit(im, &x, &y);
57 gfx_line_fit(im, &X0, &Y0);
58 cairo_move_to(cr, X0, Y0);
59 gfx_line_fit(im, &X1, &Y1);
60 cairo_line_to(cr, X1, Y1);
61 if (dash_on > 0 || dash_off > 0)
62 cairo_set_dash(cr, dashes, 2, x);
63 cairo_set_source_rgba(cr, color.red, color.green, color.blue,
69 /* create a new area */
83 gfx_area_fit(im, &X0, &Y0);
84 cairo_move_to(cr, X0, Y0);
85 gfx_area_fit(im, &X1, &Y1);
86 cairo_line_to(cr, X1, Y1);
87 gfx_area_fit(im, &X2, &Y2);
88 cairo_line_to(cr, X2, Y2);
89 cairo_set_source_rgba(cr, color.red, color.green, color.blue,
93 /* add a point to a line or to an area */
101 gfx_area_fit(im, &x, &y);
102 cairo_line_to(cr, x, y);
108 cairo_t *cr = im->cr;
110 cairo_close_path(cr);
114 /* create a text node */
115 static PangoLayout *gfx_prep_text(
125 PangoFontDescription *font_desc;
126 cairo_t *cr = im->cr;
128 /* for performance reasons we might
129 want todo that only once ... tabs will always
132 long tab_count = strlen(text);
133 long tab_shift = fmod(x, tabwidth);
135 PangoTabArray *tab_array;
136 PangoContext *pango_context;
138 tab_array = pango_tab_array_new(tab_count, (gboolean) (1));
139 for (i = 1; i <= tab_count; i++) {
140 pango_tab_array_set_tab(tab_array,
141 i, PANGO_TAB_LEFT, tabwidth * i - tab_shift);
144 cairo_set_source_rgba(cr, color.red, color.green, color.blue,
146 layout = pango_cairo_create_layout(cr);
147 pango_context = pango_layout_get_context(layout);
148 pango_cairo_context_set_font_options(pango_context, im->font_options);
149 pango_cairo_update_context(cr, pango_context);
151 pango_layout_set_tabs(layout, tab_array);
152 font_desc = pango_font_description_from_string(font);
153 pango_font_description_set_size(font_desc, size * PANGO_SCALE);
154 pango_layout_set_font_description(layout, font_desc);
155 pango_layout_set_markup(layout, text, -1);
160 double gfx_get_text_width(
169 PangoRectangle log_rect;
170 gfx_color_t color = { 0, 0, 0, 0 };
173 /* turn \\t into tab */
174 while ((tab = strstr(text, "\\t"))) {
175 memmove(tab + 1, tab + 2, strlen(tab + 2));
178 layout = gfx_prep_text(im, start, color, font, size, tabwidth, text);
179 pango_layout_get_pixel_extents(layout, NULL, &log_rect);
180 pango_tab_array_free(pango_layout_get_tabs(layout));
181 g_object_unref(layout);
182 return log_rect.width;
194 enum gfx_h_align_en h_align,
195 enum gfx_v_align_en v_align,
199 PangoRectangle log_rect;
200 PangoRectangle ink_rect;
201 cairo_t *cr = im->cr;
206 cairo_translate(cr, x, y);
207 /* gfx_line(cr,-2,0,2,0,1,color);
208 gfx_line(cr,0,-2,0,2,1,color); */
209 layout = gfx_prep_text(im, x, color, font, size, tabwidth, text);
210 pango_layout_get_pixel_extents(layout, &ink_rect, &log_rect);
211 cairo_rotate(cr, -angle * G_PI / 180.0);
215 sx -= log_rect.width;
218 sx -= log_rect.width / 2;
230 sy -= log_rect.height / 2;
233 sy -= log_rect.height;
238 pango_cairo_update_layout(cr, layout);
239 cairo_move_to(cr, sx, sy);
240 pango_cairo_show_layout(cr, layout);
241 pango_tab_array_free(pango_layout_get_tabs(layout));
242 g_object_unref(layout);
248 struct gfx_color_t gfx_hex_to_col(
249 long unsigned int color)
251 struct gfx_color_t gfx_color;
253 gfx_color.red = 1.0 / 255.0 * ((color & 0xff000000) >> (3 * 8));
254 gfx_color.green = 1.0 / 255.0 * ((color & 0x00ff0000) >> (2 * 8));
255 gfx_color.blue = 1.0 / 255.0 * ((color & 0x0000ff00) >> (1 * 8));
256 gfx_color.alpha = 1.0 / 255.0 * (color & 0x000000ff);
267 cairo_t *cr = im->cr;
273 cairo_user_to_device(cr, x, y);
274 line_width = cairo_get_line_width(cr);
275 line_height = line_width;
276 cairo_user_to_device_distance(cr, &line_width, &line_height);
277 line_width = line_width / 2.0 - (long) (line_width / 2.0);
278 line_height = line_height / 2.0 - (long) (line_height / 2.0);
279 *x = (double) ((long) (*x + 0.5)) - line_width;
280 *y = (double) ((long) (*y + 0.5)) + line_height;
281 cairo_device_to_user(cr, x, y);
291 cairo_t *cr = im->cr;
295 cairo_user_to_device(cr, x, y);
296 *x = (double) ((long) (*x + 0.5));
297 *y = (double) ((long) (*y + 0.5));
298 cairo_device_to_user(cr, x, y);