VP6F has to be flipped for binary decoder.
[mplayer/glamo.git] / gui / skin / font.c
blobf56528992533f26032c71c955fca4ed30754a53e
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <inttypes.h>
25 #include "gui/app.h"
26 #include "skin.h"
27 #include "font.h"
28 #include "cut.h"
29 #include "mp_msg.h"
30 #include "libavutil/avstring.h"
32 int items;
34 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 };
36 int fntAddNewFont( char * name )
38 int id;
39 int i;
41 for( id=0;id<26;id++ )
42 if ( !Fonts[id] ) break;
44 if ( id == 25 ) return -2;
46 if ( ( Fonts[id]=calloc( 1,sizeof( bmpFont ) ) ) == NULL ) return -1;
48 av_strlcpy( Fonts[id]->name,name,128 ); // FIXME: as defined in font.h
49 for ( i=0;i<256;i++ )
50 Fonts[id]->Fnt[i].x=Fonts[id]->Fnt[i].y=Fonts[id]->Fnt[i].sx=Fonts[id]->Fnt[i].sy=-1;
52 return id;
55 void fntFreeFont( void )
57 int i;
58 for( i=0;i < 25;i++ )
60 if ( Fonts[i] )
62 if ( Fonts[i]->Bitmap.Image ) free( Fonts[i]->Bitmap.Image );
63 free( Fonts[i] );
64 Fonts[i]=NULL;
69 int fntRead( char * path,char * fname )
71 FILE * f;
72 unsigned char tmp[512];
73 unsigned char * ptmp;
74 unsigned char command[32];
75 unsigned char param[256];
76 int c,linenumber = 0;
77 int id = fntAddNewFont( fname );
79 if ( id < 0 ) return id;
81 av_strlcpy( tmp,path,sizeof( tmp ) );
82 av_strlcat( tmp,fname,sizeof( tmp ) ); av_strlcat( tmp,".fnt",sizeof( tmp ) );
83 if ( ( f=fopen( tmp,"rt" ) ) == NULL )
84 { free( Fonts[id] ); return -3; }
86 while ( !feof( f ) )
88 fgets( tmp,255,f ); linenumber++;
90 c=tmp[ strlen( tmp ) - 1 ]; if ( ( c == '\n' )||( c == '\r' ) ) tmp[ strlen( tmp ) - 1 ]=0;
91 c=tmp[ strlen( tmp ) - 1 ]; if ( ( c == '\n' )||( c == '\r' ) ) tmp[ strlen( tmp ) - 1 ]=0;
92 for ( c=0;c < (int)strlen( tmp );c++ )
93 if ( tmp[c] == ';' ) { tmp[c]=0; break; }
94 if ( !tmp[0] ) continue;
95 ptmp=trimleft( tmp );
96 if ( !tmp[0] ) continue;
97 ptmp=strswap( ptmp,'\t',' ' );
98 ptmp=trim( ptmp );
99 cutItem( ptmp,command,'=',0 ); cutItem( ptmp,param,'=',1 );
100 if ( command[0] == '"' )
102 int i;
103 cutItem( command,command,'"',1 );
104 i=(int)command[0];
105 cutItem( param,tmp,',',0 ); Fonts[id]->Fnt[i].x=atoi( tmp );
106 cutItem( param,tmp,',',1 ); Fonts[id]->Fnt[i].y=atoi( tmp );
107 cutItem( param,tmp,',',2 ); Fonts[id]->Fnt[i].sx=atoi( tmp );
108 cutItem( param,tmp,',',3 ); Fonts[id]->Fnt[i].sy=atoi( tmp );
109 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 );
111 else
113 if ( !strcmp( command,"image" ) )
115 av_strlcpy( tmp,path,sizeof( tmp ) ); av_strlcat( tmp,param,sizeof( tmp ) );
116 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[font] font imagefile: %s\n",tmp );
117 if ( skinBPRead( tmp,&Fonts[id]->Bitmap ) ) return -4;
122 return 0;
125 int fntFindID( char * name )
127 int i;
128 for ( i=0;i < 25;i++ )
129 if ( Fonts[i] )
130 if ( !strcmp( name,Fonts[i]->name ) ) return i;
131 return -1;
134 int fntTextWidth( int id,char * str )
136 int size = 0;
137 int i;
139 if ( ( !Fonts[id] )||( !str[0] ) ) return 0;
141 for ( i=0;i < (int)strlen( str );i++ )
143 unsigned char c = (unsigned char)str[i];
144 if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
145 size+= Fonts[id]->Fnt[ c ].sx;
147 return size;
150 int fntTextHeight( int id,char * str )
152 int max = 0,i;
154 if ( ( !Fonts[id] )||( !str[0] ) ) return 0;
156 for ( i=0;i < (int)strlen( str );i++ )
158 int h;
159 unsigned char c = (unsigned char)str[i];
160 if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
161 h = Fonts[id]->Fnt[c].sy;
162 if ( h > max ) max=h;
164 return max;
167 txSample * fntRender( wItem * item,int px,const char * fmt,... )
169 va_list ap;
170 unsigned char p[512];
171 unsigned int c;
172 int i, dx = 0, tw, fbw, iw, id, ofs;
173 int x,y,fh,fw,fyc,yc;
174 uint32_t * ibuf;
175 uint32_t * obuf;
177 va_start( ap,fmt );
178 vsnprintf( p,512,fmt,ap );
179 va_end( ap );
181 iw=item->width;
182 id=item->fontid;
184 if ( ( !item )||
185 ( !Fonts[id] )||
186 ( !p[0] )||
187 ( !fntTextWidth( id,p ) ) ) return NULL;
189 tw=fntTextWidth( id,p );
190 fbw=Fonts[id]->Bitmap.Width;
192 if ( item->Bitmap.Image == NULL )
194 item->Bitmap.Height=item->height=fntTextHeight( id,p );
195 item->Bitmap.Width=item->width=iw;
196 item->Bitmap.ImageSize=item->height * iw * 4;
197 if ( !item->Bitmap.ImageSize ) return NULL;
198 item->Bitmap.BPP=32;
199 item->Bitmap.Image=malloc( item->Bitmap.ImageSize );
202 obuf=(uint32_t *)item->Bitmap.Image;
203 ibuf=(uint32_t *)Fonts[id]->Bitmap.Image;
205 for ( i=0;i < item->Bitmap.ImageSize / 4;i++ ) obuf[i]=0xff00ff;
207 if ( tw <= iw )
209 switch ( item->align )
211 default:
212 case fntAlignLeft: dx=0; break;
213 case fntAlignCenter: dx=( iw - fntTextWidth( id,p ) ) / 2; break;
214 case fntAlignRight: dx=iw - fntTextWidth( id,p ); break;
217 } else dx+=px;
219 ofs=dx;
221 for ( i=0;i < (int)strlen( p );i++ )
223 c=(unsigned int)p[i];
224 fw=Fonts[id]->Fnt[c].sx;
226 if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
228 fh=Fonts[id]->Fnt[c].sy;
229 fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x;
230 yc=dx;
232 if ( dx >= 0 )
233 for ( y=0;y < fh;y++ )
235 for ( x=0; x < fw;x++ )
236 if ( dx + x >= 0 && dx + x < iw ) obuf[yc + x]=ibuf[ fyc + x ];
237 fyc+=fbw;
238 yc+=iw;
240 dx+=fw;
243 if ( ofs > 0 && tw > item->width )
245 dx=ofs;
246 for ( i=(int)strlen( p );i > 0;i-- )
248 c=(unsigned int)p[i];
249 fw=Fonts[id]->Fnt[c].sx;
251 if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
253 fh=Fonts[id]->Fnt[c].sy;
254 fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x;
256 dx-=fw; yc=dx;
257 if ( dx >= 0 )
258 for ( y=0;y < fh;y++ )
260 for ( x=fw - 1;x >= 0;x-- )
261 if ( dx + x >= 0 && dx + x < iw ) obuf[yc + x]=ibuf[fyc + x];
262 fyc+=fbw;
263 yc+=iw;
268 return &item->Bitmap;