prepare for the release of rrdtool-1.2.9
[rrdtool.git] / src / rrd_gfx.c
index 2b32831..d01b904 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * RRDtool 1.2.4  Copyright by Tobi Oetiker, 1997-2005
+ * RRDtool 1.2.9  Copyright by Tobi Oetiker, 1997-2005
  ****************************************************************************
  * rrd_gfx.c  graphics wrapper for rrdtool
   **************************************************************************/
@@ -7,9 +7,9 @@
 /* #define DEBUG */
 
 #ifdef DEBUG
-# define DPRINT(x)    (void)(printf x, printf("\n"))
+# define DPRINTF(x,...)  fprintf(stderr, x, ## __VA_ARGS__);
 #else
-# define DPRINT(x)
+# define DPRINTF(x,...)
 #endif
 #include "rrd_tool.h"
 #include <png.h>
@@ -38,7 +38,7 @@ typedef struct gfx_string_s *gfx_string;
 struct gfx_string_s {
   unsigned int    width;
   unsigned int    height;
-  size_t          count;  /* number of characters */
+  int            count;  /* number of characters */
   gfx_char        glyphs;
   size_t          num_glyphs;
   FT_BBox         bbox;
@@ -373,18 +373,32 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text
   FT_UInt       previous;
   FT_Vector     ft_pen;
 
-  gfx_string    string;
+  gfx_string    string = (gfx_string) malloc (sizeof(struct gfx_string_s));
+
   gfx_char      glyph;          /* current glyph in table */
-  unsigned int  n;
+  int          n;
   int           error;
   int        gottab = 0;    
+
+#ifdef HAVE_MBSTOWCS
+  wchar_t      *cstr;
+  size_t       clen = strlen(text)+1;
+  cstr = malloc(sizeof(wchar_t) * clen); /* yes we are allocating probably too much here, I know */
+  string->count=mbstowcs(cstr,text,clen);
+  if ( string->count == -1){
+       string->count=mbstowcs(cstr,"Enc-Err",6);
+  }
+#else
+  char         *cstr = strdup(text);
+  string->count = strlen (text);
+#endif
+
   ft_pen.x = 0;   /* start at (0,0) !! */
   ft_pen.y = 0;
 
-  string = (gfx_string) malloc (sizeof(struct gfx_string_s));
+
   string->width = 0;
   string->height = 0;
-  string->count = strlen (text);
   string->glyphs = (gfx_char) calloc (string->count,sizeof(struct gfx_char_s));
   string->num_glyphs = 0;
   string->transform.xx = (FT_Fixed)( cos(M_PI*(rotation)/180.0)*0x10000);
@@ -395,15 +409,15 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text
   use_kerning = FT_HAS_KERNING(face);
   previous    = 0;
   glyph = string->glyphs;
-  for (n=0; n<string->count;glyph++) {
+  for (n=0; n<string->count;glyph++,n++) {
     FT_Vector   vec;
     /* handle the tabs ...
        have a witespace glyph inserted, but set its width such that the distance
     of the new right edge is x times tabwidth from 0,0 where x is an integer. */    
-    unsigned char letter = text[n];
+    unsigned int letter = cstr[n];
           
     gottab = 0;
-    if (letter == '\\' && n+1 < string->count && text[n+1] == 't'){
+    if (letter == '\\' && n+1 < string->count && cstr[n+1] == 't'){
             /* we have a tab here so skip the backslash and
                set t to ' ' so that we get a white space */
             gottab = 1;
@@ -419,7 +433,6 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text
     glyph->pos.x = 0;
     glyph->pos.y = 0;
     glyph->image = NULL;
-
     glyph->index = FT_Get_Char_Index( face, letter );
 
     /* compute glyph origin */
@@ -438,12 +451,12 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text
                             canvas->aa_type == AA_LIGHT ? FT_LOAD_TARGET_LIGHT :
                             FT_LOAD_TARGET_MONO : FT_LOAD_TARGET_MONO);
     if (error) {
-      fprintf (stderr, "couldn't load glyph:  %c\n", letter);
+      DPRINTF("couldn't load glyph:  %c\n", letter)
       continue;
     }
     error = FT_Get_Glyph (slot, &glyph->image);
     if (error) {
-      fprintf (stderr, "couldn't get glyph from slot:  %c\n", letter);
+      DPRINTF("couldn't get glyph %c from slot %d\n", letter, (int)slot)
       continue;
     }
     /* if we are in tabbing mode, we replace the tab with a space and shift the position
@@ -465,7 +478,7 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text
     FT_Vector_Transform (&vec, &string->transform);
     error = FT_Glyph_Transform (glyph->image, &string->transform, &vec);
     if (error) {
-      fprintf (stderr, "couldn't transform glyph\n");
+      DPRINTF("couldn't transform glyph id %d\n", letter)
       continue;
     }
 
@@ -475,16 +488,15 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text
                             canvas->aa_type == AA_LIGHT ? FT_RENDER_MODE_LIGHT :
                             FT_RENDER_MODE_MONO : FT_RENDER_MODE_MONO, 0, 1);
     if (error) {
-      fprintf (stderr, "couldn't convert glyph to bitmap\n");
+      DPRINTF("couldn't convert glyph id %d to bitmap\n", letter)
       continue;
     }
 
     /* increment number of glyphs */
     previous = glyph->index;
     string->num_glyphs++;
-    n++;
-    
   }
+  free(cstr);
 /*  printf ("number of glyphs = %d\n", string->num_glyphs);*/
   compute_string_bbox( string );
   /* the last character was a tab */  
@@ -494,7 +506,6 @@ gfx_string gfx_string_create(gfx_canvas_t *canvas, FT_Face face,const char *text
       string->width = string->bbox.xMax - string->bbox.xMin;
   } */
   string->height = string->bbox.yMax - string->bbox.yMin;
-
   return string;
 }
 
@@ -624,19 +635,19 @@ int           gfx_render_png (gfx_canvas_t *canvas,
            pen_x += vec.x/64;
            pen_y += vec.y/64;
             glyph = string->glyphs;
-            for(n=0; n<string->num_glyphs; ++n, ++glyph) {
+            for(n=0; n<string->num_glyphs; n++, glyph++) {
                 int gr;
                 FT_Glyph        image;
                 FT_BitmapGlyph  bit;
                /* long buf_x,comp_n; */
                /* make copy to transform */
                 if (! glyph->image) {
-                  fprintf (stderr, "no image\n");
+                  DPRINTF("no image\n")
                   continue;
                 }
                 error = FT_Glyph_Copy (glyph->image, &image);
                 if (error) {
-                  fprintf (stderr, "couldn't copy image\n");
+                  DPRINTF("couldn't copy image\n")
                   continue;
                 }
 
@@ -1399,7 +1410,7 @@ int       gfx_render_svg (gfx_canvas_t *canvas,
 "   \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
 "<!--\n"
 "   SVG file created by\n"
-"        RRDtool 1.2.4 Tobias Oetiker, http://tobi.oetiker.ch\n"
+"        RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
 "\n"
 "   The width/height attributes in the outhermost svg node\n"
 "   are just default sizes for the browser which is used\n"
@@ -1567,7 +1578,7 @@ static int eps_prologue(eps_state *state)
   gfx_node_t *node;
   fputs(
     "%!PS-Adobe-3.0 EPSF-3.0\n"
-    "%%Creator: RRDtool 1.2.4 Tobias Oetiker, http://tobi.oetiker.ch\n"
+    "%%Creator: RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch\n"
     /* can't like weird chars here */
     "%%Title: (RRDtool output)\n"
     "%%DocumentData: Clean7Bit\n"
@@ -1696,7 +1707,7 @@ static void eps_write_linearea(eps_state *state, gfx_node_t *node)
 static void eps_write_text(eps_state *state, gfx_node_t *node)
 {
   FILE *fp = state->fp;
-  const unsigned char *p;
+  const char *p;
   const char *ps_font = afm_get_font_postscript_name(node->filename);
   int lineLen = 0;
   pdf_coords g;
@@ -1712,28 +1723,41 @@ static void eps_write_text(eps_state *state, gfx_node_t *node)
          fputs("T1 ", fp);
   fputs("(", fp);
   lineLen = 20;
-  for (p = (const unsigned char*)node->text; *p; p++) {
-    if (lineLen > 70) {
+  p = node->text;
+  while (1) {
+    unsigned char ch = *(unsigned char*)p;
+    if (!ch)
+      break;
+    if (++lineLen > 70) {
       fputs("\\\n", fp); /* backslash and \n */
       lineLen = 0;
     }
-    switch (*p) {
+    switch (ch) {
+      case '%':
       case '(':
       case ')':
       case '\\':
+        fputc('\\', fp);
+        fputc(ch, fp);
+        break;
       case '\n':
+        fputs("\\n", fp);
+        break;
       case '\r':
+        fputs("\\r", fp);
+        break;
       case '\t':
-        fputc('\\', fp);
-        lineLen++;
-        /* fall-through */
+        fputs("\\t", fp);
+        break;
       default:
-        if (*p >= 126)
-          fprintf(fp, "\\%03o", *p);
-        else
-          fputc(*p, fp);
-        lineLen++;
-    }
+        if (ch >= 126 || ch < 32) {
+          fprintf(fp, "\\%03o", ch);
+          lineLen += 3;
+        } else {
+          fputc(ch, fp);
+        }
+      }
+      p++;
   }
   if (node->angle) {
         /* can't use svg_write_number as 2 decimals is far from enough to avoid
@@ -1782,6 +1806,7 @@ int       gfx_render_eps (gfx_canvas_t *canvas,
   state.linecap = -1;
   state.linejoin = -1;
   state.has_dash = 0;
+  state.line_width = 1;
   if (eps_prologue(&state) == -1)
     return -1;
   eps_set_color(&state, background);
@@ -1927,17 +1952,23 @@ static void pdf_put_string_contents(pdf_buffer *buf, const char *text)
 {
     const char *p = text;
     while (1) {
-       char ch = *p;
+       unsigned char ch = *(unsigned char*)p;
        switch (ch) {
            case 0: return;
            case '(':
            case ')':
            case '\\':
+               pdf_puts(buf, "\\");
+               pdf_put(buf, p, 1);
+               break;
            case '\n':
+               pdf_puts(buf, "\\n");
+               break;
            case '\r':
+               pdf_puts(buf, "\\r");
+               break;
            case '\t':
-               pdf_puts(buf, "\\");
-               pdf_put(buf, (char)ch, 1);
+               pdf_puts(buf, "\\t");
                break;
            default:
                if (ch >= 126 || ch < 32) {
@@ -2235,7 +2266,7 @@ static void pdf_init_document(pdf_state *state)
 
 static void pdf_setup_document(pdf_state *state)
 {
-  const char *creator = "RRDtool 1.2.4 Tobias Oetiker, http://tobi.oetiker.ch";
+  const char *creator = "RRDtool " PACKAGE_VERSION " Tobias Oetiker, http://tobi.oetiker.ch";
   /* all objects created by now, so init code can reference them */
   /* HEADER */
   pdf_puts(&state->pdf_header, "%PDF-1.3\n");