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(...)
24 /* create a new line */
34 gfx_dashed_line(cr, X0, Y0, X1, Y1, width, color, 0, 0);
48 double dashes[] = { dash_on, dash_off };
54 cairo_set_line_width(cr, width);
55 gfx_line_fit(cr, &x, &y);
56 gfx_line_fit(cr, &X0, &Y0);
57 cairo_move_to(cr, X0, Y0);
58 gfx_line_fit(cr, &X1, &Y1);
59 cairo_line_to(cr, X1, Y1);
60 if (dash_on > 0 || dash_off > 0)
61 cairo_set_dash(cr, dashes, 2, x);
62 cairo_set_source_rgba(cr, color.red, color.green, color.blue,
68 /* create a new area */
80 gfx_area_fit(cr, &X0, &Y0);
81 cairo_move_to(cr, X0, Y0);
82 gfx_area_fit(cr, &X1, &Y1);
83 cairo_line_to(cr, X1, Y1);
84 gfx_area_fit(cr, &X2, &Y2);
85 cairo_line_to(cr, X2, Y2);
86 cairo_set_source_rgba(cr, color.red, color.green, color.blue,
90 /* add a point to a line or to an area */
96 gfx_area_fit(cr, &x, &y);
97 cairo_line_to(cr, x, y);
103 cairo_close_path(cr);
107 /* create a text node */
108 static PangoLayout *gfx_prep_text(
118 PangoFontDescription *font_desc;
120 /* for performance reasons we might
121 want todo that only once ... tabs will always
124 long tab_count = strlen(text);
125 long tab_shift = fmod(x, tabwidth);
127 PangoTabArray *tab_array;
129 tab_array = pango_tab_array_new(tab_count, (gboolean) (1));
130 for (i = 1; i <= tab_count; i++) {
131 pango_tab_array_set_tab(tab_array,
132 i, PANGO_TAB_LEFT, tabwidth * i - tab_shift);
135 cairo_set_source_rgba(cr, color.red, color.green, color.blue,
137 layout = pango_cairo_create_layout(cr);
139 pango_layout_set_tabs(layout, tab_array);
140 font_desc = pango_font_description_from_string(font);
141 pango_font_description_set_size(font_desc, size * PANGO_SCALE);
142 pango_layout_set_font_description(layout, font_desc);
143 pango_layout_set_markup(layout, text, -1);
148 double gfx_get_text_width(
157 PangoRectangle log_rect;
158 gfx_color_t color = { 0, 0, 0, 0 };
161 /* turn \\t into tab */
162 while (tab = strstr(text, "\\t")) {
163 memmove(tab + 1, tab + 2, strlen(tab + 2));
166 layout = gfx_prep_text(cr, start, color, font, size, tabwidth, text);
167 pango_layout_get_pixel_extents(layout, NULL, &log_rect);
168 pango_tab_array_free(pango_layout_get_tabs(layout));
169 g_object_unref(layout);
170 return log_rect.width;
182 enum gfx_h_align_en h_align,
183 enum gfx_v_align_en v_align,
187 PangoRectangle log_rect;
188 PangoRectangle ink_rect;
193 cairo_translate(cr, x, y);
194 /* gfx_line(cr,-2,0,2,0,1,color);
195 gfx_line(cr,0,-2,0,2,1,color); */
196 layout = gfx_prep_text(cr, x, color, font, size, tabwidth, text);
197 pango_layout_get_pixel_extents(layout, &ink_rect, &log_rect);
198 cairo_rotate(cr, -angle * G_PI / 180.0);
203 sx -= log_rect.width;
206 sx -= log_rect.width / 2;
218 sy -= log_rect.height / 2;
221 sy -= log_rect.height;
226 pango_cairo_update_layout(cr, layout);
227 cairo_move_to(cr, sx, sy);
228 pango_cairo_show_layout(cr, layout);
229 pango_tab_array_free(pango_layout_get_tabs(layout));
230 g_object_unref(layout);
236 struct gfx_color_t gfx_hex_to_col(
237 long unsigned int color)
239 struct gfx_color_t gfx_color;
241 gfx_color.red = 1.0 / 255.0 * ((color & 0xff000000) >> (3 * 8));
242 gfx_color.green = 1.0 / 255.0 * ((color & 0x00ff0000) >> (2 * 8));
243 gfx_color.blue = 1.0 / 255.0 * ((color & 0x0000ff00) >> (1 * 8));
244 gfx_color.alpha = 1.0 / 255.0 * (color & 0x000000ff);
258 cairo_user_to_device(cr, x, y);
259 line_width = cairo_get_line_width(cr);
260 line_height = line_width;
261 cairo_user_to_device_distance(cr, &line_width, &line_height);
262 line_width = line_width / 2.0 - (long) (line_width / 2.0);
263 line_height = line_height / 2.0 - (long) (line_height / 2.0);
264 *x = (double) ((long) (*x + 0.5)) + line_width;
265 *y = (double) ((long) (*y + 0.5)) + line_height;
266 cairo_device_to_user(cr, x, y);
276 cairo_user_to_device(cr, x, y);
277 *x = (double) ((long) (*x + 0.5));
278 *y = (double) ((long) (*y + 0.5));
279 cairo_device_to_user(cr, x, y);