gdc.cpp

Go to the documentation of this file.
00001 /* GDCHART 0.94b  GDC.C  12 Nov 1998 */
00002 
00003 #define GDC_INCL
00004 #define GDC_LIB
00005 #include "gdc.h"
00006 
00007 #include <string.h>
00008 
00009 
00010 
00011 struct  GDC_FONT_T      GDC_fontc[GDC_numfonts] = { {(gdFontPtr)NULL, 8,  5},
00012         {(gdFontPtr)NULL, 8,  5},
00013         {(gdFontPtr)NULL, 12, 6},
00014         {(gdFontPtr)NULL, 13, 7},
00015         {(gdFontPtr)NULL, 16, 8},
00016         {(gdFontPtr)NULL, 15, 9} };
00017 
00018 /* ------------------------------------------------------------------- *\ 
00019  * convert from enum GDC_font_size to gd fonts
00020  * for now load them all
00021  *      #defines and #ifdefs might enable loading only needed fonts
00022 \* ------------------------------------------------------------------- */
00023 void
00024 load_font_conversions()
00025 {
00026         GDC_fontc[GDC_pad].f     = gdFontTiny;
00027         GDC_fontc[GDC_TINY].f    = gdFontTiny;
00028         GDC_fontc[GDC_SMALL].f   = gdFontSmall;
00029         GDC_fontc[GDC_MEDBOLD].f = gdFontMediumBold;
00030         GDC_fontc[GDC_LARGE].f   = gdFontLarge;
00031         GDC_fontc[GDC_GIANT].f   = gdFontGiant;
00032 }
00033 
00034 /* ------------------------------------------------------------------ *\ 
00035  * count (natural) substrings (new line sep)
00036 \* ------------------------------------------------------------------ */
00037 short
00038 cnt_nl( char    *nstr,
00039                 int             *len )                  /* strlen - max seg */
00040 {
00041         short   c           = 1;
00042         short   max_seg_len = 0;
00043         short   tmplen      = 0;
00044 
00045         if( !nstr )
00046                 {
00047                 if( len )
00048                         *len = 0;
00049                 return 0;
00050                 }
00051 
00052         while( *nstr )
00053                 {
00054                 if( *nstr == '\n' )
00055                         {
00056                         ++c;
00057                         max_seg_len = MAX( tmplen, max_seg_len );
00058                         tmplen = 0;
00059                         }
00060                 else
00061                         ++tmplen;
00062                 ++nstr;
00063                 }
00064 
00065         if( len )
00066                 *len = MAX( tmplen, max_seg_len );              /* don't forget last seg */
00067         return c;
00068 }
00069 
00070 /* ------------------------------------------------------------------ *\ 
00071  * gd out a string with '\n's
00072 \* ------------------------------------------------------------------ */
00073 void
00074 GDCImageStringNL( gdImagePtr            im,
00075                                   struct GDC_FONT_T     *f,
00076                                   int                           x,
00077                                   int                           y,
00078                                   char                          *str,
00079                                   int                           clr,
00080                                   GDC_justify_t         justify )
00081 {
00082         int             i;
00083         int             len;
00084         int     max_len;
00085         short   strs_num = cnt_nl( str, &max_len );
00086 
00087 #ifdef WIN32
00088         unsigned char    sub_str[1000];
00089 #else
00090         unsigned char    sub_str[max_len+1];
00091 #endif
00092         len      = -1;
00093         strs_num = -1;
00094         i = -1;
00095         do
00096                 {
00097                 ++i;
00098                 ++len;
00099                 sub_str[len] = *(str+i);
00100                 if( *(str+i) == '\n' ||
00101                         *(str+i) == '\0' )
00102                         {
00103                         int     xpos;
00104 
00105                         sub_str[len] = '\0';
00106                         ++strs_num;
00107                         switch( justify )
00108                           {
00109                           case GDC_JUSTIFY_LEFT:        xpos = x;                                        break;
00110                           case GDC_JUSTIFY_RIGHT:       xpos = x+f->w*(max_len-len); break;
00111                           case GDC_JUSTIFY_CENTER:
00112                           default:                                      xpos = x+f->w*(max_len-len)/2;
00113                           }
00114                         gdImageString( im,
00115                                                    f->f,
00116                                                    xpos,
00117                                                    y + (f->h-1)*strs_num,
00118                                                    sub_str,
00119                                                    clr );
00120                         len = -1;
00121                         }
00122                 }
00123         while( *(str+i) );
00124 }
00125 
00126 /* ------------------------------------------------------------------------ */
00127 void
00128 GDC_destroy_image(void *im)
00129 {
00130         if( im )
00131                 gdImageDestroy( (gdImagePtr)im );
00132 }
00133 
00134 /* ------------------------------------------------------------------------ */
00135 void
00136 out_err( int                    GIFWIDTH,
00137                  int                    GIFHEIGHT,
00138                  FILE                   *fptr,
00139                  unsigned long  BGColor,
00140                  unsigned long  LineColor,
00141                  char                   *err_str )
00142 {
00143 
00144         gdImagePtr      im;
00145         int                     lineclr;
00146         int                     bgclr;
00147 
00148 
00149         if( (GDC_hold_img & GDC_REUSE_IMAGE) &&
00150                 GDC_image != (void*)NULL )
00151                 im = (gdImage *) GDC_image;
00152         else
00153                 im = gdImageCreate( GIFWIDTH, GIFHEIGHT );
00154 
00155         bgclr    = gdImageColorAllocate( im, l2gdcal(BGColor) );
00156         lineclr  = gdImageColorAllocate( im, l2gdcal(LineColor) );
00157 
00158         gdImageString( im,
00159                                    gdFontMediumBold,
00160                                    GIFWIDTH/2 - GDC_fontc[GDC_MEDBOLD].w*strlen((const char *)err_str)/2,
00161                                    GIFHEIGHT/3,
00162                                    (unsigned char *) err_str,
00163                                    lineclr );
00164 
00165         /* usually GDC_generate_gif is used in conjunction with hard or hold options */
00166         if( GDC_generate_gif )
00167                 {
00168                 fflush(fptr);                   // clear anything buffered 
00169                 gdImageGif(im, fptr);
00170                 }
00171 
00172         if( GDC_hold_img & GDC_EXPOSE_IMAGE )
00173                 GDC_image = (void*)im;
00174         else
00175                 gdImageDestroy(im);
00176         return;
00177 }

Generated on Sat Dec 15 00:00:57 2007 for BulmaGes by  doxygen 1.5.1