fix horyzontal alignement of lines and aread
[rrdtool.git] / src / rrd_gfx.c
index 8a18ce4..c4236de 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * RRDtool 1.2.23  Copyright by Tobi Oetiker, 1997-2007
+ * RRDtool 1.4.3  Copyright by Tobi Oetiker, 1997-2010
  ****************************************************************************
  * rrd_gfx.c  graphics wrapper for rrdtool
   **************************************************************************/
@@ -46,10 +46,13 @@ void gfx_dashed_line(
     double dash_off)
 {
     cairo_t  *cr = im->cr;
-    double    dashes[] = { dash_on, dash_off };
+    double    dashes[2];
     double    x = 0;
     double    y = 0;
 
+    dashes[0] = dash_on;
+    dashes[1] = dash_off;
+
     cairo_save(cr);
     cairo_new_path(cr);
     cairo_set_line_width(cr, width);
@@ -102,6 +105,42 @@ void gfx_add_point(
     cairo_line_to(cr, x, y);
 }
 
+/* add a point to a line or to an area */
+void gfx_add_rect_fadey(
+    image_desc_t *im,
+    double x1,double y1,
+       double x2,double y2,
+       double py,
+    gfx_color_t color1,
+       gfx_color_t color2,
+       double height)
+{
+    cairo_t  *cr = im->cr;
+    
+       cairo_new_path(cr);
+    gfx_area_fit(im, &x1, &y1);
+    gfx_area_fit(im, &x2, &y2);
+    cairo_line_to(cr, x1, y1);
+    cairo_line_to(cr, x1, y2);
+       cairo_line_to(cr, x2, y2);
+       cairo_line_to(cr, x2, y1);
+       cairo_close_path(cr);
+       cairo_pattern_t* p;
+       if (height < 0) {
+               p = cairo_pattern_create_linear(x1,y1,x2,y1+height);
+       } else if (height > 0) {
+               p = cairo_pattern_create_linear(x1,(y2+py)/2+height,x2,(y2+py)/2);
+       } else {
+               p = cairo_pattern_create_linear(x1,y1,x2,(y2+py)/2);
+       }
+       //cairo_pattern_t* p = cairo_pattern_create_linear(x1,py+50,x2,py);
+       cairo_pattern_add_color_stop_rgba(p, 1, color1.red,color1.green,color1.blue,color1.alpha);
+       cairo_pattern_add_color_stop_rgba(p, 0, color2.red,color2.green,color2.blue,color2.alpha);
+    cairo_set_source(cr, p);
+       cairo_pattern_destroy(p);
+       cairo_fill(cr);
+}
+
 void gfx_close_path(
     image_desc_t *im)
 {
@@ -116,43 +155,71 @@ static PangoLayout *gfx_prep_text(
     image_desc_t *im,
     double x,
     gfx_color_t color,
-    char *font,
-    double size,
+    PangoFontDescription *font_desc,
     double tabwidth,
     const char *text)
 {
-    PangoLayout *layout;
-    PangoFontDescription *font_desc;
+    PangoLayout  *layout = im->layout;
+    const PangoFontDescription *pfd;
     cairo_t  *cr = im->cr;
 
+    static double last_tabwidth = -1;
+
+
+
     /* for performance reasons we might
        want todo that only once ... tabs will always
        be the same */
     long      i;
     long      tab_count = strlen(text);
     long      tab_shift = fmod(x, tabwidth);
+    int       border = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
+    
+    gchar    *utf8_text;
 
-    PangoTabArray *tab_array;
-    PangoContext *pango_context;
-
-    tab_array = pango_tab_array_new(tab_count, (gboolean) (1));
-    for (i = 1; i <= tab_count; i++) {
-        pango_tab_array_set_tab(tab_array,
-                                i, PANGO_TAB_LEFT, tabwidth * i - tab_shift);
+    if (last_tabwidth < 0 || last_tabwidth != tabwidth){
+        PangoTabArray *tab_array;
+        // fprintf(stderr,"t");
+        last_tabwidth = tabwidth;
+        tab_array = pango_tab_array_new(tab_count, (gboolean) (1));
+        for (i = 1; i <= tab_count; i++) {
+             pango_tab_array_set_tab(tab_array,
+                                     i, PANGO_TAB_LEFT,
+                                     tabwidth * i - tab_shift + border);
+        }
+        pango_layout_set_tabs(layout, tab_array);
+        pango_tab_array_free(tab_array);
     }
-    cairo_new_path(cr);
-    cairo_set_source_rgba(cr, color.red, color.green, color.blue,
+   pfd = pango_layout_get_font_description(layout);
+
+   if (!pfd || !pango_font_description_equal (pfd,font_desc)){
+        pango_layout_set_font_description(layout, font_desc);
+  }
+//   fprintf(stderr,"%s\n",pango_font_description_to_string(pango_layout_get_font_description(layout))); 
+
+   cairo_new_path(cr);
+   cairo_set_source_rgba(cr, color.red, color.green, color.blue,
                           color.alpha);
-    layout = pango_cairo_create_layout(cr);
-    pango_context = pango_layout_get_context(layout);
-    pango_cairo_context_set_font_options(pango_context, im->font_options);
-    pango_cairo_update_context(cr, pango_context);
-
-    pango_layout_set_tabs(layout, tab_array);
-    font_desc = pango_font_description_from_string(font);
-    pango_font_description_set_size(font_desc, size * PANGO_SCALE);
-    pango_layout_set_font_description(layout, font_desc);
-    pango_layout_set_markup(layout, text, -1);
+/*     layout = pango_cairo_create_layout(cr); */
+
+//    pango_cairo_context_set_font_options(pango_context, im->font_options);
+//    pango_cairo_context_set_resolution(pango_context, 100);
+
+/*     pango_cairo_update_context(cr, pango_context); */
+
+
+    /* pango expects the string to be utf-8 encoded */
+    utf8_text = g_locale_to_utf8((const gchar *) text, -1, NULL, NULL, NULL);
+
+    /* In case of an error, i.e. utf8_text == NULL (locale settings messed
+     * up?), we fall back to a possible "invalid UTF-8 string" warning instead
+     * of provoking a failed assertion in libpango. */
+    if (im->with_markup)
+        pango_layout_set_markup(layout, utf8_text ? utf8_text : text, -1);
+    else
+        pango_layout_set_text(layout, utf8_text ? utf8_text : text, -1);
+
+    g_free(utf8_text);
     return layout;
 }
 
@@ -160,25 +227,16 @@ static PangoLayout *gfx_prep_text(
 double gfx_get_text_width(
     image_desc_t *im,
     double start,
-    char *font,
-    double size,
+    PangoFontDescription *font_desc,
     double tabwidth,
     char *text)
 {
     PangoLayout *layout;
     PangoRectangle log_rect;
     gfx_color_t color = { 0, 0, 0, 0 };
-    char     *tab;
-
-    /* turn \\t into tab */
-    while ((tab = strstr(text, "\\t"))) {
-        memmove(tab + 1, tab + 2, strlen(tab + 2));
-        tab[0] = (char) 9;
-    }
-    layout = gfx_prep_text(im, start, color, font, size, tabwidth, text);
+    layout = gfx_prep_text(im, start, color, font_desc, tabwidth, text);
     pango_layout_get_pixel_extents(layout, NULL, &log_rect);
-    pango_tab_array_free(pango_layout_get_tabs(layout));
-    g_object_unref(layout);
+/*    g_object_unref(layout); */
     return log_rect.width;
 }
 
@@ -187,8 +245,7 @@ void gfx_text(
     double x,
     double y,
     gfx_color_t color,
-    char *font,
-    double size,
+    PangoFontDescription *font_desc,
     double tabwidth,
     double angle,
     enum gfx_h_align_en h_align,
@@ -197,7 +254,6 @@ void gfx_text(
 {
     PangoLayout *layout;
     PangoRectangle log_rect;
-    PangoRectangle ink_rect;
     cairo_t  *cr = im->cr;
     double    sx = 0;
     double    sy = 0;
@@ -206,8 +262,8 @@ void gfx_text(
     cairo_translate(cr, x, y);
 /*    gfx_line(cr,-2,0,2,0,1,color);
     gfx_line(cr,0,-2,0,2,1,color); */
-    layout = gfx_prep_text(im, x, color, font, size, tabwidth, text);
-    pango_layout_get_pixel_extents(layout, &ink_rect, &log_rect);
+    layout = gfx_prep_text(im, x, color, font_desc, tabwidth, text);
+    pango_layout_get_pixel_extents(layout, NULL, &log_rect);
     cairo_rotate(cr, -angle * G_PI / 180.0);
     sx = log_rect.x;
     switch (h_align) {
@@ -238,8 +294,7 @@ void gfx_text(
     pango_cairo_update_layout(cr, layout);
     cairo_move_to(cr, sx, sy);
     pango_cairo_show_layout(cr, layout);
-    pango_tab_array_free(pango_layout_get_tabs(layout));
-    g_object_unref(layout);
+/*    g_object_unref(layout); */
     cairo_restore(cr);
 
 }
@@ -274,10 +329,10 @@ void gfx_line_fit(
     line_width = cairo_get_line_width(cr);
     line_height = line_width;
     cairo_user_to_device_distance(cr, &line_width, &line_height);
-    line_width = line_width / 2.0 - (long) (line_width / 2.0);
-    line_height = line_height / 2.0 - (long) (line_height / 2.0);
-    *x = (double) ((long) (*x + 0.5)) - line_width;
-    *y = (double) ((long) (*y + 0.5)) + line_height;
+    line_width = line_width / 2.0 - ceil(line_width / 2.0);
+    line_height = line_height / 2.0 - ceil(line_height / 2.0);
+    *x = floor(*x - 0.5) - line_width;
+    *y = ceil(*y + 0.5) + line_height;
     cairo_device_to_user(cr, x, y);
 }
 
@@ -293,7 +348,7 @@ void gfx_area_fit(
     if (!im->gridfit)
         return;
     cairo_user_to_device(cr, x, y);
-    *x = (double) ((long) (*x + 0.5));
-    *y = (double) ((long) (*y + 0.5));
+    *x = floor(*x);
+    *y = ceil(*y);
     cairo_device_to_user(cr, x, y);
 }