2001-08-25 21:04:29 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2002-05-23 19:15:32 +00:00
|
|
|
#include <inttypes.h>
|
2001-08-25 21:04:29 +00:00
|
|
|
|
2003-01-09 20:47:14 +00:00
|
|
|
#include "../app.h"
|
2001-08-25 21:04:29 +00:00
|
|
|
#include "skin.h"
|
|
|
|
#include "font.h"
|
|
|
|
#include "cut.h"
|
2002-02-23 15:12:55 +00:00
|
|
|
#include "../../mp_msg.h"
|
2001-08-25 21:04:29 +00:00
|
|
|
|
|
|
|
int items;
|
|
|
|
|
2002-05-28 11:55:17 +00:00
|
|
|
bmpFont * Fonts[26] = { NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL };
|
2001-08-25 21:04:29 +00:00
|
|
|
|
|
|
|
int fntAddNewFont( char * name )
|
|
|
|
{
|
|
|
|
int id;
|
2002-05-28 11:55:17 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for( id=0;id<26;id++ )
|
|
|
|
if ( !Fonts[id] ) break;
|
|
|
|
|
|
|
|
if ( id == 25 ) return -2;
|
|
|
|
|
|
|
|
if ( ( Fonts[id]=calloc( 1,sizeof( bmpFont ) ) ) == NULL ) return -1;
|
|
|
|
|
2004-06-25 16:49:53 +00:00
|
|
|
strlcpy( Fonts[id]->name,name,128 ); // FIXME: as defined in font.h
|
2002-05-28 11:55:17 +00:00
|
|
|
for ( i=0;i<256;i++ )
|
|
|
|
Fonts[id]->Fnt[i].x=Fonts[id]->Fnt[i].y=Fonts[id]->Fnt[i].sx=Fonts[id]->Fnt[i].sy=-1;
|
|
|
|
|
2001-08-25 21:04:29 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2001-09-01 19:57:44 +00:00
|
|
|
void fntFreeFont( void )
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
2001-09-01 19:57:44 +00:00
|
|
|
int i;
|
2002-05-28 11:55:17 +00:00
|
|
|
for( i=0;i < 25;i++ )
|
2001-09-01 19:57:44 +00:00
|
|
|
{
|
|
|
|
if ( Fonts[i] )
|
|
|
|
{
|
|
|
|
if ( Fonts[i]->Bitmap.Image ) free( Fonts[i]->Bitmap.Image );
|
|
|
|
free( Fonts[i] );
|
|
|
|
Fonts[i]=NULL;
|
|
|
|
}
|
|
|
|
}
|
2001-08-25 21:04:29 +00:00
|
|
|
}
|
|
|
|
|
2002-05-28 11:55:17 +00:00
|
|
|
int fntRead( char * path,char * fname )
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
|
|
|
FILE * f;
|
|
|
|
unsigned char tmp[512];
|
|
|
|
unsigned char * ptmp;
|
|
|
|
unsigned char command[32];
|
|
|
|
unsigned char param[256];
|
2002-02-23 15:12:55 +00:00
|
|
|
int c,linenumber = 0;
|
2002-05-28 11:55:17 +00:00
|
|
|
int id = fntAddNewFont( fname );
|
|
|
|
|
|
|
|
if ( id < 0 ) return id;
|
2001-08-25 21:04:29 +00:00
|
|
|
|
2004-06-25 16:49:53 +00:00
|
|
|
strlcpy( tmp,path,sizeof( tmp ) );
|
|
|
|
strlcat( tmp,fname,sizeof( tmp ) ); strlcat( tmp,".fnt",sizeof( tmp ) );
|
2002-05-28 11:55:17 +00:00
|
|
|
if ( ( f=fopen( tmp,"rt" ) ) == NULL )
|
|
|
|
{ free( Fonts[id] ); return -3; }
|
|
|
|
|
2001-08-25 21:04:29 +00:00
|
|
|
while ( !feof( f ) )
|
|
|
|
{
|
|
|
|
fgets( tmp,255,f ); linenumber++;
|
|
|
|
|
|
|
|
c=tmp[ strlen( tmp ) - 1 ]; if ( ( c == '\n' )||( c == '\r' ) ) tmp[ strlen( tmp ) - 1 ]=0;
|
|
|
|
c=tmp[ strlen( tmp ) - 1 ]; if ( ( c == '\n' )||( c == '\r' ) ) tmp[ strlen( tmp ) - 1 ]=0;
|
2002-02-23 15:12:55 +00:00
|
|
|
for ( c=0;c < (int)strlen( tmp );c++ )
|
2002-05-28 11:55:17 +00:00
|
|
|
if ( tmp[c] == ';' ) { tmp[c]=0; break; }
|
|
|
|
if ( !tmp[0] ) continue;
|
2003-01-17 22:39:43 +00:00
|
|
|
ptmp=trimleft( tmp );
|
2002-05-28 11:55:17 +00:00
|
|
|
if ( !tmp[0] ) continue;
|
2001-08-25 21:04:29 +00:00
|
|
|
ptmp=strswap( ptmp,'\t',' ' );
|
2003-01-17 22:39:43 +00:00
|
|
|
ptmp=trim( ptmp );
|
2001-08-25 21:04:29 +00:00
|
|
|
cutItem( ptmp,command,'=',0 ); cutItem( ptmp,param,'=',1 );
|
|
|
|
if ( command[0] == '"' )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
cutItem( command,command,'"',1 );
|
|
|
|
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 );
|
|
|
|
cutItem( param,tmp,',',2 ); Fonts[id]->Fnt[i].sx=atoi( tmp );
|
|
|
|
cutItem( param,tmp,',',3 ); Fonts[id]->Fnt[i].sy=atoi( tmp );
|
2002-02-23 15:12:55 +00:00
|
|
|
mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[font] char: '%s' params: %d,%d %dx%d\n",command,Fonts[id]->Fnt[i].x,Fonts[id]->Fnt[i].y,Fonts[id]->Fnt[i].sx,Fonts[id]->Fnt[i].sy );
|
2001-08-25 21:04:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( !strcmp( command,"image" ) )
|
|
|
|
{
|
2004-06-25 16:49:53 +00:00
|
|
|
strlcpy( tmp,path,sizeof( tmp ) ); strlcat( tmp,param,sizeof( tmp ) );
|
2002-02-23 15:12:55 +00:00
|
|
|
mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[font] font imagefile: %s\n",tmp );
|
2002-05-28 11:55:17 +00:00
|
|
|
if ( skinBPRead( tmp,&Fonts[id]->Bitmap ) ) return -4;
|
2001-08-25 21:04:29 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-28 11:55:17 +00:00
|
|
|
}
|
|
|
|
|
2001-08-25 21:04:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fntFindID( char * name )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for ( i=0;i < 25;i++ )
|
2002-05-28 11:55:17 +00:00
|
|
|
if ( Fonts[i] )
|
|
|
|
if ( !strcmp( name,Fonts[i]->name ) ) return i;
|
2001-08-25 21:04:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fntTextWidth( int id,char * str )
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
int i;
|
2002-05-28 11:55:17 +00:00
|
|
|
|
|
|
|
if ( ( !Fonts[id] )||( !str[0] ) ) return 0;
|
|
|
|
|
2003-01-09 20:47:14 +00:00
|
|
|
for ( i=0;i < (int)strlen( str );i++ )
|
2002-09-10 12:44:47 +00:00
|
|
|
{
|
|
|
|
unsigned char c = (unsigned char)str[i];
|
|
|
|
if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
|
|
|
|
size+= Fonts[id]->Fnt[ c ].sx;
|
|
|
|
}
|
2001-08-25 21:04:29 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fntTextHeight( int id,char * str )
|
|
|
|
{
|
|
|
|
int max = 0,i;
|
2002-05-28 11:55:17 +00:00
|
|
|
|
|
|
|
if ( ( !Fonts[id] )||( !str[0] ) ) return 0;
|
|
|
|
|
2002-02-23 15:12:55 +00:00
|
|
|
for ( i=0;i < (int)strlen( str );i++ )
|
2002-05-28 11:55:17 +00:00
|
|
|
{
|
2002-09-10 12:44:47 +00:00
|
|
|
int h;
|
|
|
|
unsigned char c = (unsigned char)str[i];
|
|
|
|
if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
|
|
|
|
h = Fonts[id]->Fnt[c].sy;
|
2002-05-28 11:55:17 +00:00
|
|
|
if ( h > max ) max=h;
|
|
|
|
}
|
2001-08-25 21:04:29 +00:00
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2003-01-09 20:47:14 +00:00
|
|
|
txSample * fntRender( wItem * item,int px,char * fmt,... )
|
|
|
|
{
|
2003-01-11 01:30:07 +00:00
|
|
|
txSample * tmp = NULL;
|
|
|
|
va_list ap;
|
|
|
|
unsigned char p[512];
|
|
|
|
unsigned int c;
|
|
|
|
int i, dx = 0, s, tw, fbw, iw, id, ofs;
|
|
|
|
int x,y,fh,fw,fyc,yc;
|
|
|
|
uint32_t * ibuf;
|
|
|
|
uint32_t * obuf;
|
2003-01-09 20:47:14 +00:00
|
|
|
|
|
|
|
va_start( ap,fmt );
|
|
|
|
vsnprintf( p,512,fmt,ap );
|
|
|
|
va_end( ap );
|
|
|
|
|
2003-01-11 01:30:07 +00:00
|
|
|
iw=item->width;
|
|
|
|
id=item->fontid;
|
|
|
|
|
2003-01-09 20:47:14 +00:00
|
|
|
if ( ( !item )||
|
2003-01-11 01:30:07 +00:00
|
|
|
( !Fonts[id] )||
|
2003-01-09 20:47:14 +00:00
|
|
|
( !p[0] )||
|
2003-01-11 01:30:07 +00:00
|
|
|
( !fntTextWidth( id,p ) ) ) return NULL;
|
2003-01-09 20:47:14 +00:00
|
|
|
|
2003-01-11 01:30:07 +00:00
|
|
|
tw=fntTextWidth( id,p );
|
|
|
|
fbw=Fonts[id]->Bitmap.Width;
|
2003-01-09 20:47:14 +00:00
|
|
|
|
|
|
|
if ( item->Bitmap.Image == NULL )
|
|
|
|
{
|
2003-01-11 01:30:07 +00:00
|
|
|
item->Bitmap.Height=item->height=fntTextHeight( id,p );
|
2003-03-20 12:42:16 +00:00
|
|
|
item->Bitmap.Width=item->width=iw;
|
2003-01-11 01:30:07 +00:00
|
|
|
item->Bitmap.ImageSize=item->height * iw * 4;
|
2003-01-18 00:43:01 +00:00
|
|
|
if ( !item->Bitmap.ImageSize ) return NULL;
|
2003-01-09 20:47:14 +00:00
|
|
|
item->Bitmap.BPP=32;
|
|
|
|
item->Bitmap.Image=malloc( item->Bitmap.ImageSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
obuf=(uint32_t *)item->Bitmap.Image;
|
2003-01-11 01:30:07 +00:00
|
|
|
ibuf=(uint32_t *)Fonts[id]->Bitmap.Image;
|
2003-01-09 20:47:14 +00:00
|
|
|
|
|
|
|
for ( i=0;i < item->Bitmap.ImageSize / 4;i++ ) obuf[i]=0xff00ff;
|
|
|
|
|
2003-01-11 01:30:07 +00:00
|
|
|
if ( tw <= iw )
|
2003-01-09 20:47:14 +00:00
|
|
|
{
|
|
|
|
switch ( item->align )
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case fntAlignLeft: dx=0; break;
|
2003-01-11 01:30:07 +00:00
|
|
|
case fntAlignCenter: dx=( iw - fntTextWidth( id,p ) ) / 2; break;
|
|
|
|
case fntAlignRight: dx=iw - fntTextWidth( id,p ); break;
|
2003-01-09 20:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else dx+=px;
|
|
|
|
|
2003-01-11 01:30:07 +00:00
|
|
|
ofs=dx;
|
2003-01-09 20:47:14 +00:00
|
|
|
|
2002-02-23 15:12:55 +00:00
|
|
|
for ( i=0;i < (int)strlen( p );i++ )
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
2003-01-11 01:30:07 +00:00
|
|
|
c=(unsigned int)p[i];
|
|
|
|
fw=Fonts[id]->Fnt[c].sx;
|
2003-01-09 20:47:14 +00:00
|
|
|
|
2003-01-11 01:30:07 +00:00
|
|
|
if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
|
2003-01-09 20:47:14 +00:00
|
|
|
|
2003-01-11 01:30:07 +00:00
|
|
|
fh=Fonts[id]->Fnt[c].sy;
|
|
|
|
fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x;
|
|
|
|
yc=dx;
|
|
|
|
|
|
|
|
if ( dx >= 0 )
|
|
|
|
for ( y=0;y < fh;y++ )
|
|
|
|
{
|
|
|
|
for ( x=0; x < fw;x++ )
|
|
|
|
if ( dx + x >= 0 && dx + x < iw ) obuf[yc + x]=ibuf[ fyc + x ];
|
|
|
|
fyc+=fbw;
|
|
|
|
yc+=iw;
|
|
|
|
}
|
|
|
|
dx+=fw;
|
2001-08-25 21:04:29 +00:00
|
|
|
}
|
|
|
|
|
2003-01-11 01:30:07 +00:00
|
|
|
if ( ofs > 0 && tw > item->width )
|
2001-08-25 21:04:29 +00:00
|
|
|
{
|
2003-01-11 01:30:07 +00:00
|
|
|
dx=ofs;
|
|
|
|
for ( i=(int)strlen( p );i > 0;i-- )
|
|
|
|
{
|
|
|
|
c=(unsigned int)p[i];
|
|
|
|
fw=Fonts[id]->Fnt[c].sx;
|
|
|
|
|
|
|
|
if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
|
2001-08-25 21:04:29 +00:00
|
|
|
|
2003-01-11 01:30:07 +00:00
|
|
|
fh=Fonts[id]->Fnt[c].sy;
|
|
|
|
fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x;
|
2001-08-25 21:04:29 +00:00
|
|
|
|
2003-01-11 01:30:07 +00:00
|
|
|
dx-=fw; yc=dx;
|
|
|
|
if ( dx >= 0 )
|
|
|
|
for ( y=0;y < fh;y++ )
|
|
|
|
{
|
|
|
|
for ( x=fw - 1;x >= 0;x-- )
|
|
|
|
if ( dx + x >= 0 && dx + x < iw ) obuf[yc + x]=ibuf[fyc + x];
|
|
|
|
fyc+=fbw;
|
|
|
|
yc+=iw;
|
|
|
|
}
|
2001-08-25 21:04:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-11 01:30:07 +00:00
|
|
|
return &item->Bitmap;
|
2001-08-25 21:04:29 +00:00
|
|
|
}
|