consistency cosmetics
[mplayer/greg.git] / gui / skin / font.c
blob90210ced4a666f2a315fb46bc6ec331ce66c3083
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdarg.h>
5 #include <string.h>
6 #include <inttypes.h>
8 #include "app.h"
9 #include "skin.h"
10 #include "font.h"
11 #include "cut.h"
12 #include "../mp_msg.h"
13 #include "../libavutil/avstring.h"
15 int items;
17 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 };
19 int fntAddNewFont( char * name )
21 int id;
22 int i;
24 for( id=0;id<26;id++ )
25 if ( !Fonts[id] ) break;
27 if ( id == 25 ) return -2;
29 if ( ( Fonts[id]=calloc( 1,sizeof( bmpFont ) ) ) == NULL ) return -1;
31 av_strlcpy( Fonts[id]->name,name,128 ); // FIXME: as defined in font.h
32 for ( i=0;i<256;i++ )
33 Fonts[id]->Fnt[i].x=Fonts[id]->Fnt[i].y=Fonts[id]->Fnt[i].sx=Fonts[id]->Fnt[i].sy=-1;
35 return id;
38 void fntFreeFont( void )
40 int i;
41 for( i=0;i < 25;i++ )
43 if ( Fonts[i] )
45 if ( Fonts[i]->Bitmap.Image ) free( Fonts[i]->Bitmap.Image );
46 free( Fonts[i] );
47 Fonts[i]=NULL;
52 int fntRead( char * path,char * fname )
54 FILE * f;
55 unsigned char tmp[512];
56 unsigned char * ptmp;
57 unsigned char command[32];
58 unsigned char param[256];
59 int c,linenumber = 0;
60 int id = fntAddNewFont( fname );
62 if ( id < 0 ) return id;
64 av_strlcpy( tmp,path,sizeof( tmp ) );
65 av_strlcat( tmp,fname,sizeof( tmp ) ); av_strlcat( tmp,".fnt",sizeof( tmp ) );
66 if ( ( f=fopen( tmp,"rt" ) ) == NULL )
67 { free( Fonts[id] ); return -3; }
69 while ( !feof( f ) )
71 fgets( tmp,255,f ); linenumber++;
73 c=tmp[ strlen( tmp ) - 1 ]; if ( ( c == '\n' )||( c == '\r' ) ) tmp[ strlen( tmp ) - 1 ]=0;
74 c=tmp[ strlen( tmp ) - 1 ]; if ( ( c == '\n' )||( c == '\r' ) ) tmp[ strlen( tmp ) - 1 ]=0;
75 for ( c=0;c < (int)strlen( tmp );c++ )
76 if ( tmp[c] == ';' ) { tmp[c]=0; break; }
77 if ( !tmp[0] ) continue;
78 ptmp=trimleft( tmp );
79 if ( !tmp[0] ) continue;
80 ptmp=strswap( ptmp,'\t',' ' );
81 ptmp=trim( ptmp );
82 cutItem( ptmp,command,'=',0 ); cutItem( ptmp,param,'=',1 );
83 if ( command[0] == '"' )
85 int i;
86 cutItem( command,command,'"',1 );
87 i=(int)command[0];
88 cutItem( param,tmp,',',0 ); Fonts[id]->Fnt[i].x=atoi( tmp );
89 cutItem( param,tmp,',',1 ); Fonts[id]->Fnt[i].y=atoi( tmp );
90 cutItem( param,tmp,',',2 ); Fonts[id]->Fnt[i].sx=atoi( tmp );
91 cutItem( param,tmp,',',3 ); Fonts[id]->Fnt[i].sy=atoi( tmp );
92 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 );
94 else
96 if ( !strcmp( command,"image" ) )
98 av_strlcpy( tmp,path,sizeof( tmp ) ); av_strlcat( tmp,param,sizeof( tmp ) );
99 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[font] font imagefile: %s\n",tmp );
100 if ( skinBPRead( tmp,&Fonts[id]->Bitmap ) ) return -4;
105 return 0;
108 int fntFindID( char * name )
110 int i;
111 for ( i=0;i < 25;i++ )
112 if ( Fonts[i] )
113 if ( !strcmp( name,Fonts[i]->name ) ) return i;
114 return -1;
117 int fntTextWidth( int id,char * str )
119 int size = 0;
120 int i;
122 if ( ( !Fonts[id] )||( !str[0] ) ) return 0;
124 for ( i=0;i < (int)strlen( str );i++ )
126 unsigned char c = (unsigned char)str[i];
127 if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
128 size+= Fonts[id]->Fnt[ c ].sx;
130 return size;
133 int fntTextHeight( int id,char * str )
135 int max = 0,i;
137 if ( ( !Fonts[id] )||( !str[0] ) ) return 0;
139 for ( i=0;i < (int)strlen( str );i++ )
141 int h;
142 unsigned char c = (unsigned char)str[i];
143 if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
144 h = Fonts[id]->Fnt[c].sy;
145 if ( h > max ) max=h;
147 return max;
150 txSample * fntRender( wItem * item,int px,const char * fmt,... )
152 va_list ap;
153 unsigned char p[512];
154 unsigned int c;
155 int i, dx = 0, tw, fbw, iw, id, ofs;
156 int x,y,fh,fw,fyc,yc;
157 uint32_t * ibuf;
158 uint32_t * obuf;
160 va_start( ap,fmt );
161 vsnprintf( p,512,fmt,ap );
162 va_end( ap );
164 iw=item->width;
165 id=item->fontid;
167 if ( ( !item )||
168 ( !Fonts[id] )||
169 ( !p[0] )||
170 ( !fntTextWidth( id,p ) ) ) return NULL;
172 tw=fntTextWidth( id,p );
173 fbw=Fonts[id]->Bitmap.Width;
175 if ( item->Bitmap.Image == NULL )
177 item->Bitmap.Height=item->height=fntTextHeight( id,p );
178 item->Bitmap.Width=item->width=iw;
179 item->Bitmap.ImageSize=item->height * iw * 4;
180 if ( !item->Bitmap.ImageSize ) return NULL;
181 item->Bitmap.BPP=32;
182 item->Bitmap.Image=malloc( item->Bitmap.ImageSize );
185 obuf=(uint32_t *)item->Bitmap.Image;
186 ibuf=(uint32_t *)Fonts[id]->Bitmap.Image;
188 for ( i=0;i < item->Bitmap.ImageSize / 4;i++ ) obuf[i]=0xff00ff;
190 if ( tw <= iw )
192 switch ( item->align )
194 default:
195 case fntAlignLeft: dx=0; break;
196 case fntAlignCenter: dx=( iw - fntTextWidth( id,p ) ) / 2; break;
197 case fntAlignRight: dx=iw - fntTextWidth( id,p ); break;
200 } else dx+=px;
202 ofs=dx;
204 for ( i=0;i < (int)strlen( p );i++ )
206 c=(unsigned int)p[i];
207 fw=Fonts[id]->Fnt[c].sx;
209 if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
211 fh=Fonts[id]->Fnt[c].sy;
212 fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x;
213 yc=dx;
215 if ( dx >= 0 )
216 for ( y=0;y < fh;y++ )
218 for ( x=0; x < fw;x++ )
219 if ( dx + x >= 0 && dx + x < iw ) obuf[yc + x]=ibuf[ fyc + x ];
220 fyc+=fbw;
221 yc+=iw;
223 dx+=fw;
226 if ( ofs > 0 && tw > item->width )
228 dx=ofs;
229 for ( i=(int)strlen( p );i > 0;i-- )
231 c=(unsigned int)p[i];
232 fw=Fonts[id]->Fnt[c].sx;
234 if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
236 fh=Fonts[id]->Fnt[c].sy;
237 fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x;
239 dx-=fw; yc=dx;
240 if ( dx >= 0 )
241 for ( y=0;y < fh;y++ )
243 for ( x=fw - 1;x >= 0;x-- )
244 if ( dx + x >= 0 && dx + x < iw ) obuf[yc + x]=ibuf[fyc + x];
245 fyc+=fbw;
246 yc+=iw;
251 return &item->Bitmap;