[MPlayer-cvslog] r32821 - in trunk: DOCS/xml/de/skin.xml DOCS/xml/en/skin.xml gui/skin/font.c gui/skin/font.h
ib
subversion at mplayerhq.hu
Thu Jan 27 19:04:19 CET 2011
Author: ib
Date: Thu Jan 27 19:04:19 2011
New Revision: 32821
Log:
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.)
Modified:
trunk/gui/skin/font.c
trunk/gui/skin/font.h
Changes in other areas also in this revision:
Modified:
trunk/DOCS/xml/de/skin.xml
trunk/DOCS/xml/en/skin.xml
Modified: trunk/gui/skin/font.c
==============================================================================
--- trunk/gui/skin/font.c Thu Jan 27 18:58:58 2011 (r32820)
+++ trunk/gui/skin/font.c Thu Jan 27 19:04:19 2011 (r32821)
@@ -46,7 +46,7 @@ int fntAddNewFont( char * name )
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 fntRead( char * path,char * fname )
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 @@ int fntFindID( char * name )
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 fntTextWidth( int id,char * str )
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 @@ int fntTextHeight( int id,char * str )
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 @@ txSample * fntRender( wItem * item,int p
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 @@ txSample * fntRender( wItem * item,int p
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;
Modified: trunk/gui/skin/font.h
==============================================================================
--- trunk/gui/skin/font.h Thu Jan 27 18:58:58 2011 (r32820)
+++ trunk/gui/skin/font.h Thu Jan 27 19:04:19 2011 (r32821)
@@ -19,9 +19,13 @@
#ifndef MPLAYER_GUI_FONT_H
#define MPLAYER_GUI_FONT_H
+#include <gtk/gtk.h>
#include "gui/bitmap.h"
#include "gui/app.h"
+#define ASCII_CHRS 128 // number of ASCII characters
+#define EXTRA_CHRS 128 // (arbitrary) number of non-ASCII characters
+
#define fntAlignLeft 0
#define fntAlignCenter 1
#define fntAlignRight 2
@@ -34,9 +38,10 @@ typedef struct
typedef struct
{
- fntChar Fnt[256];
- txSample Bitmap;
- char name[128];
+ fntChar Fnt[ASCII_CHRS + EXTRA_CHRS];
+ unsigned char nonASCIIidx[EXTRA_CHRS][4];
+ txSample Bitmap;
+ char name[128];
} bmpFont;
extern txSample Bitmap;
@@ -45,6 +50,7 @@ extern bmpFont * Fonts[26];
int fntAddNewFont( char * name );
void fntFreeFont( void );
int fntFindID( char * name );
+int fntGetCharIndex( int id, unsigned char **str, gboolean utf8, int direction );
int fntTextHeight( int id, char * str );
int fntTextWidth( int id, char * str );
More information about the MPlayer-cvslog
mailing list