fix horyzontal alignement of lines and aread
[rrdtool.git] / src / rrd_gfx.c
index 6f04135..c4236de 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * RRDtool 1.3.1  Copyright by Tobi Oetiker, 1997-2008
+ * RRDtool 1.4.3  Copyright by Tobi Oetiker, 1997-2010
  ****************************************************************************
  * rrd_gfx.c  graphics wrapper for rrdtool
   **************************************************************************/
@@ -105,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)
 {
@@ -119,19 +155,17 @@ 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)
 {
-    static PangoLayout  *layout = NULL;
-    static PangoContext *pango_context = NULL;
-    static PangoFontMap *pango_fontmap = NULL;
-    static char* last_font = NULL;
-    static double last_size = -1;
+    PangoLayout  *layout = im->layout;
+    const PangoFontDescription *pfd;
+    cairo_t  *cr = im->cr;
+
     static double last_tabwidth = -1;
 
-    cairo_t  *cr = im->cr;
+
 
     /* for performance reasons we might
        want todo that only once ... tabs will always
@@ -143,25 +177,6 @@ static PangoLayout *gfx_prep_text(
     
     gchar    *utf8_text;
 
-    /* initialize pango only once ... */
-    if (!pango_fontmap){
-        pango_fontmap = pango_cairo_font_map_get_default ();
-    }
-    if (!pango_context){
-        // fprintf(stderr,"c");
-        pango_context = pango_cairo_font_map_create_context ((PangoCairoFontMap *) (pango_fontmap));
-        pango_cairo_context_set_resolution(pango_context, 100);
-    }
-    if (!layout){
-        // fprintf(stderr,"l");
-        layout =  pango_layout_new (pango_context);
-    }
-
-    pango_cairo_context_set_font_options(pango_context, im->font_options);
-
-    pango_cairo_update_context (cr, pango_context);
-
-        
     if (last_tabwidth < 0 || last_tabwidth != tabwidth){
         PangoTabArray *tab_array;
         // fprintf(stderr,"t");
@@ -175,27 +190,15 @@ static PangoLayout *gfx_prep_text(
         pango_layout_set_tabs(layout, tab_array);
         pango_tab_array_free(tab_array);
     }
+   pfd = pango_layout_get_font_description(layout);
 
-    if (last_font == NULL || strcmp(font,last_font) != 0){
-        PangoFontDescription *font_desc;
-        // fprintf(stderr,"f:%s",font);
-        if (last_font)
-           free(last_font);
-        last_font = strdup(font);       
-        font_desc = pango_font_description_from_string(font);
-        pango_layout_set_font_description(layout, font_desc);
-        pango_font_description_free(font_desc);
-   }
-
-   if (last_size < 0 || last_size != size ){
-        PangoFontDescription *font_desc;
-        font_desc =  pango_layout_get_font_description (layout);
-        pango_font_description_set_size(font_desc, size * PANGO_SCALE);
+   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,
+   cairo_new_path(cr);
+   cairo_set_source_rgba(cr, color.red, color.green, color.blue,
                           color.alpha);
 /*     layout = pango_cairo_create_layout(cr); */
 
@@ -224,15 +227,14 @@ 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 };
-    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);
 /*    g_object_unref(layout); */
     return log_rect.width;
@@ -243,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,
@@ -261,7 +262,7 @@ 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);
+    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;
@@ -328,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);
 }
 
@@ -347,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);
 }