New, hopefully better, implementation of PART drawing
[rrdtool.git] / src / rrd_gfx.c
index 715d8d2..1a45439 100644 (file)
@@ -15,6 +15,7 @@
 #include <png.h>
 #include <ft2build.h>
 #include FT_FREETYPE_H
+#include <math.h>
 
 #include "rrd_gfx.h"
 
@@ -107,6 +108,52 @@ gfx_node_t   *gfx_new_area   (gfx_canvas_t *canvas,
 
   return node;
 }
+/* create an arc section (2*M_PI is full circle) */
+gfx_node_t   *gfx_arc_sect   (gfx_canvas_t *canvas, 
+                             double centerx, double centery,
+                             double radiusx, double radiusy,
+                             double start, double end,
+                             gfx_color_t color) {
+
+  gfx_node_t *node;
+  ArtVpath *vec;
+  int counter;
+  double position;
+
+/* 20 is too low, 100 is overkill */
+#define AMOUNT_OF_VECTORS 50
+
+  node = gfx_new_node(canvas,GFX_AREA);
+  if (node == NULL) return NULL;
+  vec = art_new(ArtVpath, AMOUNT_OF_VECTORS+4);
+  if (vec == NULL) return NULL;
+
+  vec[0].code = ART_MOVETO;
+  vec[0].x = centerx;
+  vec[0].y = centery;
+
+  for (counter=0;counter<=AMOUNT_OF_VECTORS;) {
+    position=start + counter*(end-start)/AMOUNT_OF_VECTORS;
+
+    counter++;
+    vec[counter].code = ART_LINETO;
+    vec[counter].x = centerx + sin(position)*radiusx;
+    vec[counter].y = centery - cos(position)*radiusy;
+  }
+
+  vec[AMOUNT_OF_VECTORS+2].code = ART_LINETO;
+  vec[AMOUNT_OF_VECTORS+2].x = centerx;
+  vec[AMOUNT_OF_VECTORS+2].y = centery;
+
+  vec[AMOUNT_OF_VECTORS+3].code = ART_END;
+  
+  node->points = AMOUNT_OF_VECTORS+4;
+  node->points_max = AMOUNT_OF_VECTORS+4;
+  node->color = color;
+  node->path  = vec;
+
+  return node;
+}
 
 /* add a point to a line or to an area */
 int           gfx_add_point  (gfx_node_t *node, 
@@ -210,6 +257,10 @@ double gfx_get_text_width ( double start, char* font, double size,
       
     }
     error = FT_Load_Glyph( face, glyph_index, 0 );
+    if ( error ) {
+      FT_Done_FreeType(library);
+      return -1;
+    }
     if (! previous) {
       text_width -= (double)slot->metrics.horiBearingX / 64.0; /* add just char width */       
     }
@@ -218,6 +269,7 @@ double gfx_get_text_width ( double start, char* font, double size,
   text_width -= (double)slot->metrics.horiAdvance / 64.0; /* remove last step */
   text_width += (double)slot->metrics.width / 64.0; /* add just char width */
   text_width += (double)slot->metrics.horiBearingX / 64.0; /* add just char width */
+  FT_Done_FreeType(library);
   return text_width;
 }
  
@@ -289,14 +341,15 @@ int           gfx_render_png (gfx_canvas_t *canvas,
                                  (char *)node->filename,
                                  0,
                                  &face );
+           if ( error ) break;
             use_kerning = FT_HAS_KERNING(face);
 
-           if ( error ) break;
             error = FT_Set_Char_Size(face,   /* handle to face object            */
                                      (long)(node->size*64),
                                      (long)(node->size*64),
                                      (long)(100*zoom),
                                      (long)(100*zoom));
+            if ( error ) break;
             pen_x = node->x * zoom;
             pen_y = node->y * zoom;
             slot = face->glyph;
@@ -313,6 +366,7 @@ int           gfx_render_png (gfx_canvas_t *canvas,
                     
                 }
                 error = FT_Load_Glyph( face, glyph_index, 0 );
+                if ( error ) break;
                if (previous == 0){
                  pen_x -= (double)slot->metrics.horiBearingX / 64.0; /* adjust pos for first char */   
                  text_width -= (double)slot->metrics.horiBearingX / 64.0; /* add just char width */    
@@ -352,6 +406,7 @@ int           gfx_render_png (gfx_canvas_t *canvas,
                     
                 }
                 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_RENDER );
+                if ( error ) break;
                 gr = slot->bitmap.num_grays -1;
                 for (iy=0; iy < slot->bitmap.rows; iy++){
                     long buf_y = iy+(pen_y+0.5)-slot->bitmap_top;
@@ -380,6 +435,7 @@ int           gfx_render_png (gfx_canvas_t *canvas,
     }  
     gfx_save_png(buffer,fp , pys_width,pys_height,bytes_per_pixel);
     art_free(buffer);
+    FT_Done_FreeType( library );
     return 0;    
 }