23077
|
1
|
|
2 #include <stdlib.h>
|
|
3 #include <stdio.h>
|
|
4 #include <stdarg.h>
|
|
5 #include <string.h>
|
|
6 #include <inttypes.h>
|
|
7
|
|
8 #include "app.h"
|
|
9 #include "skin.h"
|
|
10 #include "font.h"
|
|
11 #include "cut.h"
|
|
12 #include "../mp_msg.h"
|
|
13
|
|
14 int items;
|
|
15
|
|
16 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 };
|
|
17
|
|
18 int fntAddNewFont( char * name )
|
|
19 {
|
|
20 int id;
|
|
21 int i;
|
|
22
|
|
23 for( id=0;id<26;id++ )
|
|
24 if ( !Fonts[id] ) break;
|
|
25
|
|
26 if ( id == 25 ) return -2;
|
|
27
|
|
28 if ( ( Fonts[id]=calloc( 1,sizeof( bmpFont ) ) ) == NULL ) return -1;
|
|
29
|
|
30 strlcpy( Fonts[id]->name,name,128 ); // FIXME: as defined in font.h
|
|
31 for ( i=0;i<256;i++ )
|
|
32 Fonts[id]->Fnt[i].x=Fonts[id]->Fnt[i].y=Fonts[id]->Fnt[i].sx=Fonts[id]->Fnt[i].sy=-1;
|
|
33
|
|
34 return id;
|
|
35 }
|
|
36
|
|
37 void fntFreeFont( void )
|
|
38 {
|
|
39 int i;
|
|
40 for( i=0;i < 25;i++ )
|
|
41 {
|
|
42 if ( Fonts[i] )
|
|
43 {
|
|
44 if ( Fonts[i]->Bitmap.Image ) free( Fonts[i]->Bitmap.Image );
|
|
45 free( Fonts[i] );
|
|
46 Fonts[i]=NULL;
|
|
47 }
|
|
48 }
|
|
49 }
|
|
50
|
|
51 int fntRead( char * path,char * fname )
|
|
52 {
|
|
53 FILE * f;
|
|
54 unsigned char tmp[512];
|
|
55 unsigned char * ptmp;
|
|
56 unsigned char command[32];
|
|
57 unsigned char param[256];
|
|
58 int c,linenumber = 0;
|
|
59 int id = fntAddNewFont( fname );
|
|
60
|
|
61 if ( id < 0 ) return id;
|
|
62
|
|
63 strlcpy( tmp,path,sizeof( tmp ) );
|
|
64 strlcat( tmp,fname,sizeof( tmp ) ); strlcat( tmp,".fnt",sizeof( tmp ) );
|
|
65 if ( ( f=fopen( tmp,"rt" ) ) == NULL )
|
|
66 { free( Fonts[id] ); return -3; }
|
|
67
|
|
68 while ( !feof( f ) )
|
|
69 {
|
|
70 fgets( tmp,255,f ); linenumber++;
|
|
71
|
|
72 c=tmp[ strlen( tmp ) - 1 ]; if ( ( c == '\n' )||( c == '\r' ) ) tmp[ strlen( tmp ) - 1 ]=0;
|
|
73 c=tmp[ strlen( tmp ) - 1 ]; if ( ( c == '\n' )||( c == '\r' ) ) tmp[ strlen( tmp ) - 1 ]=0;
|
|
74 for ( c=0;c < (int)strlen( tmp );c++ )
|
|
75 if ( tmp[c] == ';' ) { tmp[c]=0; break; }
|
|
76 if ( !tmp[0] ) continue;
|
|
77 ptmp=trimleft( tmp );
|
|
78 if ( !tmp[0] ) continue;
|
|
79 ptmp=strswap( ptmp,'\t',' ' );
|
|
80 ptmp=trim( ptmp );
|
|
81 cutItem( ptmp,command,'=',0 ); cutItem( ptmp,param,'=',1 );
|
|
82 if ( command[0] == '"' )
|
|
83 {
|
|
84 int i;
|
|
85 cutItem( command,command,'"',1 );
|
|
86 i=(int)command[0];
|
|
87 cutItem( param,tmp,',',0 ); Fonts[id]->Fnt[i].x=atoi( tmp );
|
|
88 cutItem( param,tmp,',',1 ); Fonts[id]->Fnt[i].y=atoi( tmp );
|
|
89 cutItem( param,tmp,',',2 ); Fonts[id]->Fnt[i].sx=atoi( tmp );
|
|
90 cutItem( param,tmp,',',3 ); Fonts[id]->Fnt[i].sy=atoi( tmp );
|
|
91 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 );
|
|
92 }
|
|
93 else
|
|
94 {
|
|
95 if ( !strcmp( command,"image" ) )
|
|
96 {
|
|
97 strlcpy( tmp,path,sizeof( tmp ) ); strlcat( tmp,param,sizeof( tmp ) );
|
|
98 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[font] font imagefile: %s\n",tmp );
|
|
99 if ( skinBPRead( tmp,&Fonts[id]->Bitmap ) ) return -4;
|
|
100 }
|
|
101 }
|
|
102 }
|
|
103
|
|
104 return 0;
|
|
105 }
|
|
106
|
|
107 int fntFindID( char * name )
|
|
108 {
|
|
109 int i;
|
|
110 for ( i=0;i < 25;i++ )
|
|
111 if ( Fonts[i] )
|
|
112 if ( !strcmp( name,Fonts[i]->name ) ) return i;
|
|
113 return -1;
|
|
114 }
|
|
115
|
|
116 int fntTextWidth( int id,char * str )
|
|
117 {
|
|
118 int size = 0;
|
|
119 int i;
|
|
120
|
|
121 if ( ( !Fonts[id] )||( !str[0] ) ) return 0;
|
|
122
|
|
123 for ( i=0;i < (int)strlen( str );i++ )
|
|
124 {
|
|
125 unsigned char c = (unsigned char)str[i];
|
|
126 if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
|
|
127 size+= Fonts[id]->Fnt[ c ].sx;
|
|
128 }
|
|
129 return size;
|
|
130 }
|
|
131
|
|
132 int fntTextHeight( int id,char * str )
|
|
133 {
|
|
134 int max = 0,i;
|
|
135
|
|
136 if ( ( !Fonts[id] )||( !str[0] ) ) return 0;
|
|
137
|
|
138 for ( i=0;i < (int)strlen( str );i++ )
|
|
139 {
|
|
140 int h;
|
|
141 unsigned char c = (unsigned char)str[i];
|
|
142 if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' ';
|
|
143 h = Fonts[id]->Fnt[c].sy;
|
|
144 if ( h > max ) max=h;
|
|
145 }
|
|
146 return max;
|
|
147 }
|
|
148
|
|
149 txSample * fntRender( wItem * item,int px,const char * fmt,... )
|
|
150 {
|
|
151 va_list ap;
|
|
152 unsigned char p[512];
|
|
153 unsigned int c;
|
|
154 int i, dx = 0, tw, fbw, iw, id, ofs;
|
|
155 int x,y,fh,fw,fyc,yc;
|
|
156 uint32_t * ibuf;
|
|
157 uint32_t * obuf;
|
|
158
|
|
159 va_start( ap,fmt );
|
|
160 vsnprintf( p,512,fmt,ap );
|
|
161 va_end( ap );
|
|
162
|
|
163 iw=item->width;
|
|
164 id=item->fontid;
|
|
165
|
|
166 if ( ( !item )||
|
|
167 ( !Fonts[id] )||
|
|
168 ( !p[0] )||
|
|
169 ( !fntTextWidth( id,p ) ) ) return NULL;
|
|
170
|
|
171 tw=fntTextWidth( id,p );
|
|
172 fbw=Fonts[id]->Bitmap.Width;
|
|
173
|
|
174 if ( item->Bitmap.Image == NULL )
|
|
175 {
|
|
176 item->Bitmap.Height=item->height=fntTextHeight( id,p );
|
|
177 item->Bitmap.Width=item->width=iw;
|
|
178 item->Bitmap.ImageSize=item->height * iw * 4;
|
|
179 if ( !item->Bitmap.ImageSize ) return NULL;
|
|
180 item->Bitmap.BPP=32;
|
|
181 item->Bitmap.Image=malloc( item->Bitmap.ImageSize );
|
|
182 }
|
|
183
|
|
184 obuf=(uint32_t *)item->Bitmap.Image;
|
|
185 ibuf=(uint32_t *)Fonts[id]->Bitmap.Image;
|
|
186
|
|
187 for ( i=0;i < item->Bitmap.ImageSize / 4;i++ ) obuf[i]=0xff00ff;
|
|
188
|
|
189 if ( tw <= iw )
|
|
190 {
|
|
191 switch ( item->align )
|
|
192 {
|
|
193 default:
|
|
194 case fntAlignLeft: dx=0; break;
|
|
195 case fntAlignCenter: dx=( iw - fntTextWidth( id,p ) ) / 2; break;
|
|
196 case fntAlignRight: dx=iw - fntTextWidth( id,p ); break;
|
|
197 }
|
|
198
|
|
199 } else dx+=px;
|
|
200
|
|
201 ofs=dx;
|
|
202
|
|
203 for ( i=0;i < (int)strlen( p );i++ )
|
|
204 {
|
|
205 c=(unsigned int)p[i];
|
|
206 fw=Fonts[id]->Fnt[c].sx;
|
|
207
|
|
208 if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
|
|
209
|
|
210 fh=Fonts[id]->Fnt[c].sy;
|
|
211 fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x;
|
|
212 yc=dx;
|
|
213
|
|
214 if ( dx >= 0 )
|
|
215 for ( y=0;y < fh;y++ )
|
|
216 {
|
|
217 for ( x=0; x < fw;x++ )
|
|
218 if ( dx + x >= 0 && dx + x < iw ) obuf[yc + x]=ibuf[ fyc + x ];
|
|
219 fyc+=fbw;
|
|
220 yc+=iw;
|
|
221 }
|
|
222 dx+=fw;
|
|
223 }
|
|
224
|
|
225 if ( ofs > 0 && tw > item->width )
|
|
226 {
|
|
227 dx=ofs;
|
|
228 for ( i=(int)strlen( p );i > 0;i-- )
|
|
229 {
|
|
230 c=(unsigned int)p[i];
|
|
231 fw=Fonts[id]->Fnt[c].sx;
|
|
232
|
|
233 if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; }
|
|
234
|
|
235 fh=Fonts[id]->Fnt[c].sy;
|
|
236 fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x;
|
|
237
|
|
238 dx-=fw; yc=dx;
|
|
239 if ( dx >= 0 )
|
|
240 for ( y=0;y < fh;y++ )
|
|
241 {
|
|
242 for ( x=fw - 1;x >= 0;x-- )
|
|
243 if ( dx + x >= 0 && dx + x < iw ) obuf[yc + x]=ibuf[fyc + x];
|
|
244 fyc+=fbw;
|
|
245 yc+=iw;
|
|
246 }
|
|
247 }
|
|
248 }
|
|
249
|
|
250 return &item->Bitmap;
|
|
251 }
|