Diff for /np2/sdl/fontmng.c between versions 1.1 and 1.5

version 1.1, 2003/11/21 06:51:12 version 1.5, 2005/02/13 05:31:26
Line 4 Line 4
 #include        "codecnv.h"  #include        "codecnv.h"
   
   
   #if !defined(RESOURCE_US)               // use TTF
   
 #define FONTMNG_CACHE           64  #define FONTMNG_CACHE           64
   
   
Line 173  static BOOL fdatgetcache(FNTMNG fhdl, co Line 175  static BOOL fdatgetcache(FNTMNG fhdl, co
                 fhdl->cachehead = pos;                  fhdl->cachehead = pos;
         }          }
         if (pfdat) {          if (pfdat) {
                 *pfdat = (FNTDAT)(((BYTE *)(fhdl + 1)) + (pos * fhdl->fontalign));                  *pfdat = (FNTDAT)(((UINT8 *)(fhdl + 1)) + (pos * fhdl->fontalign));
         }          }
         return(r);          return(r);
 }  }
Line 218  static void getlength1(FNTMNG fhdl, FNTD Line 220  static void getlength1(FNTMNG fhdl, FNTD
         SDL_Surface     *text;          SDL_Surface     *text;
   
         if (fhdl->fonttype & FDAT_PROPORTIONAL) {          if (fhdl->fonttype & FDAT_PROPORTIONAL) {
                 codecnv_sjis2utf(utext, 2, string, length);                  codecnv_euctoucs2(utext, NELEMENTS(utext), string, length);
                 text = TTF_RenderUNICODE_Solid(fhdl->ttf_font, utext, white);                  text = TTF_RenderUNICODE_Solid(fhdl->ttf_font, utext, white);
                 setfdathead(fhdl, fdat, length, text);                  setfdathead(fhdl, fdat, length, text);
                 if (text) {                  if (text) {
Line 230  static void getlength1(FNTMNG fhdl, FNTD Line 232  static void getlength1(FNTMNG fhdl, FNTD
         }          }
 }  }
   
 static BYTE getpixeldepth(SDL_Surface *s, int x, int y) {  static UINT8 getpixeldepth(SDL_Surface *s, int x, int y) {
   
         int             bpp;          int             bpp;
 const BYTE      *ptr;  const UINT8     *ptr;
   
         if ((x >= 0) && (x < s->w) && (y >= 0) && (y < s->h)) {          if ((x >= 0) && (x < s->w) && (y >= 0) && (y < s->h)) {
                 bpp = s->format->BytesPerPixel;                  bpp = s->format->BytesPerPixel;
                 ptr = (BYTE *)s->pixels + (y * s->pitch) + (x * bpp);                  ptr = (UINT8 *)s->pixels + (y * s->pitch) + (x * bpp);
                 switch(bpp) {                  switch(bpp) {
                         case 1:                          case 1:
                                 return((ptr[0] != 0)?FDAT_DEPTH:0);                                  return((ptr[0] != 0)?FDAT_DEPTH:0);
Line 255  static void getfont1(FNTMNG fhdl, FNTDAT Line 257  static void getfont1(FNTMNG fhdl, FNTDAT
   
         UINT16          utext[2];          UINT16          utext[2];
         SDL_Surface     *text;          SDL_Surface     *text;
         BYTE            *dst;          UINT8           *dst;
         int                     x;          int                     x;
         int                     y;          int                     y;
         int                     depth;          int                     depth;
   
         codecnv_sjis2utf(utext, 2, string, length);          codecnv_euctoucs2(utext, NELEMENTS(utext), string, length);
         text = TTF_RenderUNICODE_Solid(fhdl->ttf_font, utext, white);          text = TTF_RenderUNICODE_Solid(fhdl->ttf_font, utext, white);
         setfdathead(fhdl, fdat, length, text);          setfdathead(fhdl, fdat, length, text);
         dst = (BYTE *)(fdat + 1);          dst = (UINT8 *)(fdat + 1);
         if (text) {          if (text) {
                 if (fhdl->fonttype & FDAT_ALIAS) {                  if (fhdl->fonttype & FDAT_ALIAS) {
                         for (y=0; y<fdat->height; y++) {                          for (y=0; y<fdat->height; y++) {
Line 272  static void getfont1(FNTMNG fhdl, FNTDAT Line 274  static void getfont1(FNTMNG fhdl, FNTDAT
                                         depth += getpixeldepth(text, x*2+1, y*2+0);                                          depth += getpixeldepth(text, x*2+1, y*2+0);
                                         depth += getpixeldepth(text, x*2+0, y*2+1);                                          depth += getpixeldepth(text, x*2+0, y*2+1);
                                         depth += getpixeldepth(text, x*2+1, y*2+1);                                          depth += getpixeldepth(text, x*2+1, y*2+1);
                                         *dst++ = (BYTE)((depth + 2) / 4);                                          *dst++ = (UINT8)((depth + 2) / 4);
                                 }                                  }
                         }                          }
                 }                  }
Line 290  static void getfont1(FNTMNG fhdl, FNTDAT Line 292  static void getfont1(FNTMNG fhdl, FNTDAT
         }          }
 }  }
   
 BOOL fontmng_getsize(void *hdl, const char *string, POINT_T *pt) {  BRESULT fontmng_getsize(void *hdl, const char *string, POINT_T *pt) {
   
           int             width;
           int             leng;
           _FNTDAT fdat;
   
           if ((hdl == NULL) || (string == NULL)) {
                   goto fmgs_exit;
           }
   
           width = 0;
           while(1) {
                   leng = milstr_charsize(string);
                   if (!leng) {
                           break;
                   }
                   getlength1((FNTMNG)hdl, &fdat, string, leng);
                   string += leng;
                   width += fdat.pitch;
           }
   
           if (pt) {
                   pt->x = width;
                   pt->y = ((FNTMNG)hdl)->fontsize;
           }
           return(SUCCESS);
   
   fmgs_exit:
           return(FAILURE);
   }
   
   BRESULT fontmng_getdrawsize(void *hdl, const char *string, POINT_T *pt) {
   
           int             width;
           int             posx;
           int             leng;
           _FNTDAT fdat;
   
           if ((hdl == NULL) || (string == NULL)) {
                   goto fmgds_exit;
           }
   
           width = 0;
           posx = 0;
           while(1) {
                   leng = milstr_charsize(string);
                   if (!leng) {
                           break;
                   }
                   getlength1((FNTMNG)hdl, &fdat, string, leng);
                   string += leng;
                   width = posx + max(fdat.width, fdat.pitch);
                   posx += fdat.pitch;
           }
           if (pt) {
                   pt->x = width;
                   pt->y = ((FNTMNG)hdl)->fontsize;
           }
           return(SUCCESS);
   
   fmgds_exit:
           return(FAILURE);
   }
   
   FNTDAT fontmng_get(void *hdl, const char *string) {
   
           FNTMNG  fhdl;
           FNTDAT  fdat;
           int             leng;
   
           if ((hdl == NULL) || (string == NULL)) {
                   goto fmg_err;
           }
           fhdl = (FNTMNG)hdl;
   
   #if defined(FONTMNG_CACHE)
           if (fdatgetcache(fhdl, string, &fdat)) {
                   return(fdat);
           }
   #else
           fdat = (FNTDAT)(fhdl + 1);
   #endif
   
           leng = milstr_charsize(string);
           getfont1(fhdl, fdat, string, leng);
           return(fdat);
   
   fmg_err:
           return(NULL);
   }
   
   #else
   
   #if defined(SIZE_QVGA)
   #include        "ank10.res"
   #else
   #include        "ank12.res"
   #endif
   
   typedef struct {
           int             fontsize;
           UINT    fonttype;
   } _FNTMNG, *FNTMNG;
   
   BOOL fontmng_init(void) {
   
           return(SUCCESS);
   }
   
   void fontmng_setdeffontname(const char *name) {
   
           (void)name;
   }
   
   void *fontmng_create(int size, UINT type, const char *fontface) {
   
           int             fontalign;
           int             allocsize;
           FNTMNG  ret;
   
           if (size < ANKFONTSIZE) {
                   goto fmc_err1;
           }
           fontalign = sizeof(_FNTDAT) + (size * size);
           fontalign = (fontalign + 3) & (~3);
           allocsize = sizeof(_FNTMNG) + fontalign;
           ret = (FNTMNG)_MALLOC(allocsize, "font mng");
           if (ret == NULL) {
                   goto fmc_err1;
           }
           ZeroMemory(ret, allocsize);
           ret->fontsize = size;
           ret->fonttype = type;
           return(ret);
   
   fmc_err1:
           (void)fontface;
           return(NULL);
   }
   
   void fontmng_destroy(void *hdl) {
   
           if (hdl) {
                   _MFREE(hdl);
           }
   }
   
   static void setfdathead(FNTMNG fhdl, FNTDAT fdat, int width) {
   
           if (fhdl->fonttype & FDAT_PROPORTIONAL) {
                   fdat->width = width;
                   fdat->pitch = width + 1;
                   fdat->height = fhdl->fontsize;
           }
           else {
                   fdat->width = max(width, fhdl->fontsize >> 1);
                   fdat->pitch = (fhdl->fontsize >> 1) + 1;
                   fdat->height = fhdl->fontsize;
           }
   }
   
   static void getlength1(FNTMNG fhdl, FNTDAT fdat,
                                                                                           const char *string, int length) {
   
           int             c;
   
           c = string[0] - 0x20;
           if ((c < 0) || (c >= 0x60)) {
                   c = 0x1f;                                                       // ?
           }
           setfdathead(fhdl, fdat, ankfont[c * ANKFONTSIZE]);
   }
   
   static void getfont1(FNTMNG fhdl, FNTDAT fdat,
                                                                                           const char *string, int length) {
   
           int             c;
   const UINT8     *src;
           int             width;
           UINT8   *dst;
           int             x;
           int             y;
   
           c = string[0] - 0x20;
           if ((c < 0) || (c >= 0x60)) {
                   c = 0x1f;                                                       // ?
           }
           src = ankfont + (c * ANKFONTSIZE);
           width = *src++;
           setfdathead(fhdl, fdat, width);
           dst = (UINT8 *)(fdat + 1);
           ZeroMemory(dst, fdat->width * fdat->height);
           dst += ((fdat->height - ANKFONTSIZE) / 2) * fdat->width;
           dst += (fdat->width - width) / 2;
           for (y=0; y<(ANKFONTSIZE - 1); y++) {
                   dst += fdat->width;
                   for (x=0; x<width; x++) {
                           dst[x] = (src[0] & (0x80 >> x))?0xff:0x00;
                   }
                   src++;
           }
   }
   
   BRESULT fontmng_getsize(void *hdl, const char *string, POINT_T *pt) {
   
         FNTMNG  fhdl;          FNTMNG  fhdl;
         char    buf[4];          char    buf[4];
Line 334  fmgs_exit: Line 539  fmgs_exit:
         return(FAILURE);          return(FAILURE);
 }  }
   
 BOOL fontmng_getdrawsize(void *hdl, const char *string, POINT_T *pt) {  BRESULT fontmng_getdrawsize(void *hdl, const char *string, POINT_T *pt) {
   
         FNTMNG  fhdl;          FNTMNG  fhdl;
         char    buf[4];          char    buf[4];
Line 391  FNTDAT fontmng_get(void *hdl, const char Line 596  FNTDAT fontmng_get(void *hdl, const char
                 goto fmg_err;                  goto fmg_err;
         }          }
         fhdl = (FNTMNG)hdl;          fhdl = (FNTMNG)hdl;
   
 #if defined(FONTMNG_CACHE)  
         if (fdatgetcache(fhdl, string, &fdat)) {  
                 return(fdat);  
         }  
 #else  
         fdat = (FNTDAT)(fhdl + 1);          fdat = (FNTDAT)(fhdl + 1);
 #endif  
   
         leng = 1;          leng = 1;
         if (((((string[0] ^ 0x20) - 0xa1) & 0xff) < 0x3c) &&          if (((((string[0] ^ 0x20) - 0xa1) & 0xff) < 0x3c) &&
Line 411  FNTDAT fontmng_get(void *hdl, const char Line 609  FNTDAT fontmng_get(void *hdl, const char
 fmg_err:  fmg_err:
         return(NULL);          return(NULL);
 }  }
   #endif
   

Removed from v.1.1  
changed lines
  Added in v.1.5


RetroPC.NET-CVS <cvs@retropc.net>