some more leak plugging
[rrdtool.git] / src / rrd_gfx.c
1 /****************************************************************************
2  * RRDtool 1.3.1  Copyright by Tobi Oetiker, 1997-2008
3  ****************************************************************************
4  * rrd_gfx.c  graphics wrapper for rrdtool
5   **************************************************************************/
6
7 /* #define DEBUG */
8
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 */
19
20 #include "rrd_tool.h"
21 #include "rrd_graph.h"
22
23
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 }
36
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;
52
53     dashes[0] = dash_on;
54     dashes[1] = dash_off;
55
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 }
71
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;
84
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 }
95
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;
103
104     gfx_area_fit(im, &x, &y);
105     cairo_line_to(cr, x, y);
106 }
107
108 void gfx_close_path(
109     image_desc_t *im)
110 {
111     cairo_t  *cr = im->cr;
112
113     cairo_close_path(cr);
114     cairo_fill(cr);
115 }
116
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     char *font,
123     double size,
124     double tabwidth,
125     const char *text)
126 {
127     PangoLayout *layout;
128     PangoFontDescription *font_desc;
129     cairo_t  *cr = im->cr;
130
131     /* for performance reasons we might
132        want todo that only once ... tabs will always
133        be the same */
134     long      i;
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;
138
139     gchar    *utf8_text;
140
141     PangoTabArray *tab_array;
142     PangoContext *pango_context;
143
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,
147                                 i, PANGO_TAB_LEFT,
148                                 tabwidth * i - tab_shift + border);
149     }
150     cairo_new_path(cr);
151     cairo_set_source_rgba(cr, color.red, color.green, color.blue,
152                           color.alpha);
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);
157
158 /*     pango_cairo_update_context(cr, pango_context); */
159
160     pango_layout_set_tabs(layout, tab_array);
161     pango_tab_array_free(tab_array);
162     font_desc = pango_font_description_from_string(font);
163     pango_font_description_set_size(font_desc, size * PANGO_SCALE);
164     pango_layout_set_font_description(layout, font_desc);
165     pango_font_description_free(font_desc);
166     /* pango expects the string to be utf-8 encoded */
167     utf8_text = g_locale_to_utf8((const gchar *) text, -1, NULL, NULL, NULL);
168
169     /* In case of an error, i.e. utf8_text == NULL (locale settings messed
170      * up?), we fall back to a possible "invalid UTF-8 string" warning instead
171      * of provoking a failed assertion in libpango. */
172     if (im->with_markup)
173         pango_layout_set_markup(layout, utf8_text ? utf8_text : text, -1);
174     else
175         pango_layout_set_text(layout, utf8_text ? utf8_text : text, -1);
176
177     g_free(utf8_text);
178     return layout;
179 }
180
181 /* Size Text Node */
182 double gfx_get_text_width(
183     image_desc_t *im,
184     double start,
185     char *font,
186     double size,
187     double tabwidth,
188     char *text)
189 {
190     PangoLayout *layout;
191     PangoRectangle log_rect;
192     gfx_color_t color = { 0, 0, 0, 0 };
193     layout = gfx_prep_text(im, start, color, font, size, tabwidth, text);
194     pango_layout_get_pixel_extents(layout, NULL, &log_rect);
195     g_object_unref(layout);
196     return log_rect.width;
197 }
198
199 void gfx_text(
200     image_desc_t *im,
201     double x,
202     double y,
203     gfx_color_t color,
204     char *font,
205     double size,
206     double tabwidth,
207     double angle,
208     enum gfx_h_align_en h_align,
209     enum gfx_v_align_en v_align,
210     const char *text)
211 {
212     PangoLayout *layout;
213     PangoRectangle log_rect;
214     PangoRectangle ink_rect;
215     cairo_t  *cr = im->cr;
216     double    sx = 0;
217     double    sy = 0;
218
219     cairo_save(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);
226     sx = log_rect.x;
227     switch (h_align) {
228     case GFX_H_RIGHT:
229         sx -= log_rect.width;
230         break;
231     case GFX_H_CENTER:
232         sx -= log_rect.width / 2;
233         break;
234     case GFX_H_LEFT:
235         break;
236     case GFX_H_NULL:
237         break;
238     }
239     sy = log_rect.y;
240     switch (v_align) {
241     case GFX_V_TOP:
242         break;
243     case GFX_V_CENTER:
244         sy -= log_rect.height / 2;
245         break;
246     case GFX_V_BOTTOM:
247         sy -= log_rect.height;
248         break;
249     case GFX_V_NULL:
250         break;
251     }
252     pango_cairo_update_layout(cr, layout);
253     cairo_move_to(cr, sx, sy);
254     pango_cairo_show_layout(cr, layout);
255     g_object_unref(layout);
256     cairo_restore(cr);
257
258 }
259
260 /* convert color */
261 struct gfx_color_t gfx_hex_to_col(
262     long unsigned int color)
263 {
264     struct gfx_color_t gfx_color;
265
266     gfx_color.red = 1.0 / 255.0 * ((color & 0xff000000) >> (3 * 8));
267     gfx_color.green = 1.0 / 255.0 * ((color & 0x00ff0000) >> (2 * 8));
268     gfx_color.blue = 1.0 / 255.0 * ((color & 0x0000ff00) >> (1 * 8));
269     gfx_color.alpha = 1.0 / 255.0 * (color & 0x000000ff);
270     return gfx_color;
271 }
272
273 /* gridfit_lines */
274
275 void gfx_line_fit(
276     image_desc_t *im,
277     double *x,
278     double *y)
279 {
280     cairo_t  *cr = im->cr;
281     double    line_width;
282     double    line_height;
283
284     if (!im->gridfit)
285         return;
286     cairo_user_to_device(cr, x, y);
287     line_width = cairo_get_line_width(cr);
288     line_height = line_width;
289     cairo_user_to_device_distance(cr, &line_width, &line_height);
290     line_width = line_width / 2.0 - (long) (line_width / 2.0);
291     line_height = line_height / 2.0 - (long) (line_height / 2.0);
292     *x = (double) ((long) (*x + 0.5)) - line_width;
293     *y = (double) ((long) (*y + 0.5)) + line_height;
294     cairo_device_to_user(cr, x, y);
295 }
296
297 /* gridfit_areas */
298
299 void gfx_area_fit(
300     image_desc_t *im,
301     double *x,
302     double *y)
303 {
304     cairo_t  *cr = im->cr;
305
306     if (!im->gridfit)
307         return;
308     cairo_user_to_device(cr, x, y);
309     *x = (double) ((long) (*x + 0.5));
310     *y = (double) ((long) (*y + 0.5));
311     cairo_device_to_user(cr, x, y);
312 }