diff gui/skin/font.c @ 32734:cc58a1e919d9

Allow character in the font description file to be in UTF-8. A character defined in the font description file can be either an ASCII character, any character in the range of 0x80 to 0xFF or - to avoid character set problems, and that is recommended - a character in UTF-8 encoding now. Non-ASCII characters will be stored in the nonASCIIidx array. The indices 0..127 of this array correspond to the indices 128..255 of the Fnt array. (This also settles the "Translate messages shown in the GUI window(s) from UTF-8" issue.)
author ib
date Thu, 27 Jan 2011 18:04:19 +0000
parents 6ff3cc81d602
children 63844ef43932
line wrap: on
line diff
--- a/gui/skin/font.c	Thu Jan 27 17:58:58 2011 +0000
+++ b/gui/skin/font.c	Thu Jan 27 18:04:19 2011 +0000
@@ -46,7 +46,7 @@
  if ( ( Fonts[id]=calloc( 1,sizeof( bmpFont ) ) ) == NULL ) return -1;
 
  av_strlcpy( Fonts[id]->name,name,128 ); // FIXME: as defined in font.h
- for ( i=0;i<256;i++ )
+ for ( i=0;i<ASCII_CHRS+EXTRA_CHRS;i++ )
    Fonts[id]->Fnt[i].x=Fonts[id]->Fnt[i].y=Fonts[id]->Fnt[i].sx=Fonts[id]->Fnt[i].sy=-1;
 
  return id;
@@ -102,6 +102,19 @@
      int i;
      cutItem( command,command,'"',1 );
      if ( !command[0] ) i=(int)'"';
+     else if ( command[0] & 0x80 )
+      {
+       for ( i = 0; i < EXTRA_CHRS; i++ )
+        {
+         if ( !Fonts[id]->nonASCIIidx[i][0] )
+          {
+           strncpy( Fonts[id]->nonASCIIidx[i], command, 4 );
+           break;
+          }
+        }
+       if ( i == EXTRA_CHRS ) continue;
+       i += ASCII_CHRS;
+      }
      else i=(int)command[0];
      cutItem( param,tmp,',',0 ); Fonts[id]->Fnt[i].x=atoi( tmp );
      cutItem( param,tmp,',',1 ); Fonts[id]->Fnt[i].y=atoi( tmp );
@@ -137,17 +150,61 @@
  return -1;
 }
 
+// get Fnt index of character (utf8 or normal one) *str points to,
+// then move pointer to next/previous character
+int fntGetCharIndex( int id, unsigned char **str, gboolean utf8, int direction )
+{
+ unsigned char *p, uchar[4] = { 0, 0, 0, 0 };
+ int i, c = -1;
+
+ if ( **str & 0x80 )
+  {
+   if ( utf8 )
+    {
+     p = *str;
+     *str = g_utf8_next_char( *str );
+     strncpy( uchar, p, *str - p );
+
+     if ( direction < 0 ) *str = g_utf8_prev_char( p );
+    }
+   else
+    {
+     uchar[0] = **str;
+     *str += direction;
+    }
+
+   for ( i = 0; ( i < EXTRA_CHRS ) && Fonts[id]->nonASCIIidx[i][0]; i++ )
+    {
+     if ( strncmp( Fonts[id]->nonASCIIidx[i], uchar, 4 ) == 0 ) return i + ASCII_CHRS;
+     if ( !utf8 && ( Fonts[id]->nonASCIIidx[i][0] == (*uchar >> 6 | 0xc0) && Fonts[id]->nonASCIIidx[i][1] == (*uchar & 0x3f | 0x80) && Fonts[id]->nonASCIIidx[i][2] == 0 ) ) c = i + ASCII_CHRS;
+    }
+  }
+ else
+  {
+   c = **str;
+
+   if ( utf8 && ( direction < 0 ) ) *str = g_utf8_prev_char( *str );
+   else *str += direction;
+  }
+
+ return c;
+}
+
 int fntTextWidth( int id,char * str )
 {
  int size = 0;
- int i;
+ gboolean utf8;
+ unsigned char *p;
 
  if ( ( !Fonts[id] )||( !str[0] ) ) return 0;
 
- for ( i=0;i < (int)strlen( str );i++ )
+ utf8 = g_utf8_validate( str, -1, NULL);
+ p = (unsigned char *) str;
+
+ while ( *p )
   {
-   unsigned char c = (unsigned char)str[i];
-   if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
+   int c = fntGetCharIndex( id, &p, utf8, 1 );
+   if ( c == -1 || Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
    size+= Fonts[id]->Fnt[ c ].sx;
   }
  return size;
@@ -155,15 +212,20 @@
 
 int fntTextHeight( int id,char * str )
 {
- int max = 0,i;
+ int max = 0;
+ gboolean utf8;
+ unsigned char *p;
 
  if ( ( !Fonts[id] )||( !str[0] ) ) return 0;
 
- for ( i=0;i < (int)strlen( str );i++ )
+ utf8 = g_utf8_validate( str, -1, NULL);
+ p = (unsigned char *) str;
+
+ while ( *p )
   {
    int h;
-   unsigned char c = (unsigned char)str[i];
-   if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
+   int c = fntGetCharIndex( id, &p, utf8, 1 );
+   if ( c == -1 || Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
    h = Fonts[id]->Fnt[c].sy;
    if ( h > max ) max=h;
   }
@@ -173,12 +235,12 @@
 txSample * fntRender( wItem * item,int px,const char * fmt,... )
 {
  va_list         ap;
- unsigned char   p[512];
- unsigned int    c;
- int 	         i, dx = 0, tw, fbw, iw, id, ofs;
+ unsigned char * u, p[512];
+ int 	         c, i, dx = 0, tw, fbw, iw, id, ofs;
  int 		 x,y,fh,fw,fyc,yc;
  uint32_t      * ibuf;
  uint32_t      * obuf;
+ gboolean        utf8;
 
  va_start( ap,fmt );
  vsnprintf( p,512,fmt,ap );
@@ -224,12 +286,16 @@
 
  ofs=dx;
 
- for ( i=0;i < (int)strlen( p );i++ )
+ utf8 = g_utf8_validate( p, -1, NULL);
+ u = p;
+
+ while ( *u )
   {
-   c=(unsigned int)p[i];
-   fw=Fonts[id]->Fnt[c].sx;
+   c = fntGetCharIndex( id, &u, utf8, 1 );
 
-   if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
+   if ( c != -1 ) fw=Fonts[id]->Fnt[c].sx;
+
+   if ( c == -1 || fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
 
    fh=Fonts[id]->Fnt[c].sy;
    fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x;
@@ -249,12 +315,15 @@
  if ( ofs > 0 && tw > item->width )
   {
    dx=ofs;
-   for ( i=(int)strlen( p );i > 0;i-- )
+   u = p + strlen( p );
+
+   while ( u > p )
     {
-     c=(unsigned int)p[i];
-     fw=Fonts[id]->Fnt[c].sx;
+     c = fntGetCharIndex( id, &u, utf8, -1 );
 
-     if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
+     if ( c != -1) fw=Fonts[id]->Fnt[c].sx;
+
+     if ( c == -1 || fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
 
      fh=Fonts[id]->Fnt[c].sy;
      fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x;