Mercurial > mplayer.hg
annotate gui/skin/font.c @ 28215:8c9029514722
Simplify phony target declaration.
author | diego |
---|---|
date | Sun, 04 Jan 2009 23:27:03 +0000 |
parents | b0a7b35b78d2 |
children | 0f1b5b68af32 |
rev | line source |
---|---|
26458 | 1 /* |
2 * This file is part of MPlayer. | |
3 * | |
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. | |
8 * | |
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. | |
13 * | |
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. | |
17 */ | |
23077 | 18 |
19 #include <stdlib.h> | |
20 #include <stdio.h> | |
21 #include <stdarg.h> | |
22 #include <string.h> | |
23 #include <inttypes.h> | |
24 | |
26365
10dfbc523184
Add gui/ prefix to some #include paths so that compilation from the
diego
parents:
23703
diff
changeset
|
25 #include "gui/app.h" |
23077 | 26 #include "skin.h" |
27 #include "font.h" | |
28 #include "cut.h" | |
26382
b2f4abcf20ed
Make include paths consistent; do not use ../ in them.
diego
parents:
26365
diff
changeset
|
29 #include "mp_msg.h" |
b2f4abcf20ed
Make include paths consistent; do not use ../ in them.
diego
parents:
26365
diff
changeset
|
30 #include "libavutil/avstring.h" |
23077 | 31 |
32 int items; | |
33 | |
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 }; | |
35 | |
36 int fntAddNewFont( char * name ) | |
37 { | |
38 int id; | |
39 int i; | |
40 | |
41 for( id=0;id<26;id++ ) | |
42 if ( !Fonts[id] ) break; | |
43 | |
44 if ( id == 25 ) return -2; | |
45 | |
46 if ( ( Fonts[id]=calloc( 1,sizeof( bmpFont ) ) ) == NULL ) return -1; | |
47 | |
23703
9fb716ab06a3
Avoid code duplication and ugly config.h hack by using av_strlcat/av_strlcpy
reimar
parents:
23077
diff
changeset
|
48 av_strlcpy( Fonts[id]->name,name,128 ); // FIXME: as defined in font.h |
23077 | 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; | |
51 | |
52 return id; | |
53 } | |
54 | |
55 void fntFreeFont( void ) | |
56 { | |
57 int i; | |
58 for( i=0;i < 25;i++ ) | |
59 { | |
60 if ( Fonts[i] ) | |
61 { | |
62 if ( Fonts[i]->Bitmap.Image ) free( Fonts[i]->Bitmap.Image ); | |
63 free( Fonts[i] ); | |
64 Fonts[i]=NULL; | |
65 } | |
66 } | |
67 } | |
68 | |
69 int fntRead( char * path,char * fname ) | |
70 { | |
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 ); | |
78 | |
79 if ( id < 0 ) return id; | |
80 | |
23703
9fb716ab06a3
Avoid code duplication and ugly config.h hack by using av_strlcat/av_strlcpy
reimar
parents:
23077
diff
changeset
|
81 av_strlcpy( tmp,path,sizeof( tmp ) ); |
9fb716ab06a3
Avoid code duplication and ugly config.h hack by using av_strlcat/av_strlcpy
reimar
parents:
23077
diff
changeset
|
82 av_strlcat( tmp,fname,sizeof( tmp ) ); av_strlcat( tmp,".fnt",sizeof( tmp ) ); |
23077 | 83 if ( ( f=fopen( tmp,"rt" ) ) == NULL ) |
84 { free( Fonts[id] ); return -3; } | |
85 | |
86 while ( !feof( f ) ) | |
87 { | |
88 fgets( tmp,255,f ); linenumber++; | |
89 | |
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] == '"' ) | |
101 { | |
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 ); | |
110 } | |
111 else | |
112 { | |
113 if ( !strcmp( command,"image" ) ) | |
114 { | |
23703
9fb716ab06a3
Avoid code duplication and ugly config.h hack by using av_strlcat/av_strlcpy
reimar
parents:
23077
diff
changeset
|
115 av_strlcpy( tmp,path,sizeof( tmp ) ); av_strlcat( tmp,param,sizeof( tmp ) ); |
23077 | 116 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[font] font imagefile: %s\n",tmp ); |
117 if ( skinBPRead( tmp,&Fonts[id]->Bitmap ) ) return -4; | |
118 } | |
119 } | |
120 } | |
121 | |
122 return 0; | |
123 } | |
124 | |
125 int fntFindID( char * name ) | |
126 { | |
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; | |
132 } | |
133 | |
134 int fntTextWidth( int id,char * str ) | |
135 { | |
136 int size = 0; | |
137 int i; | |
138 | |
139 if ( ( !Fonts[id] )||( !str[0] ) ) return 0; | |
140 | |
141 for ( i=0;i < (int)strlen( str );i++ ) | |
142 { | |
143 unsigned char c = (unsigned char)str[i]; | |
144 if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' '; | |
145 size+= Fonts[id]->Fnt[ c ].sx; | |
146 } | |
147 return size; | |
148 } | |
149 | |
150 int fntTextHeight( int id,char * str ) | |
151 { | |
152 int max = 0,i; | |
153 | |
154 if ( ( !Fonts[id] )||( !str[0] ) ) return 0; | |
155 | |
156 for ( i=0;i < (int)strlen( str );i++ ) | |
157 { | |
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; | |
163 } | |
164 return max; | |
165 } | |
166 | |
167 txSample * fntRender( wItem * item,int px,const char * fmt,... ) | |
168 { | |
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; | |
176 | |
177 va_start( ap,fmt ); | |
178 vsnprintf( p,512,fmt,ap ); | |
179 va_end( ap ); | |
180 | |
181 iw=item->width; | |
182 id=item->fontid; | |
183 | |
184 if ( ( !item )|| | |
185 ( !Fonts[id] )|| | |
186 ( !p[0] )|| | |
187 ( !fntTextWidth( id,p ) ) ) return NULL; | |
188 | |
189 tw=fntTextWidth( id,p ); | |
190 fbw=Fonts[id]->Bitmap.Width; | |
191 | |
192 if ( item->Bitmap.Image == NULL ) | |
193 { | |
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 ); | |
200 } | |
201 | |
202 obuf=(uint32_t *)item->Bitmap.Image; | |
203 ibuf=(uint32_t *)Fonts[id]->Bitmap.Image; | |
204 | |
205 for ( i=0;i < item->Bitmap.ImageSize / 4;i++ ) obuf[i]=0xff00ff; | |
206 | |
207 if ( tw <= iw ) | |
208 { | |
209 switch ( item->align ) | |
210 { | |
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; | |
215 } | |
216 | |
217 } else dx+=px; | |
218 | |
219 ofs=dx; | |
220 | |
221 for ( i=0;i < (int)strlen( p );i++ ) | |
222 { | |
223 c=(unsigned int)p[i]; | |
224 fw=Fonts[id]->Fnt[c].sx; | |
225 | |
226 if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; } | |
227 | |
228 fh=Fonts[id]->Fnt[c].sy; | |
229 fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x; | |
230 yc=dx; | |
231 | |
232 if ( dx >= 0 ) | |
233 for ( y=0;y < fh;y++ ) | |
234 { | |
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; | |
239 } | |
240 dx+=fw; | |
241 } | |
242 | |
243 if ( ofs > 0 && tw > item->width ) | |
244 { | |
245 dx=ofs; | |
246 for ( i=(int)strlen( p );i > 0;i-- ) | |
247 { | |
248 c=(unsigned int)p[i]; | |
249 fw=Fonts[id]->Fnt[c].sx; | |
250 | |
251 if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; } | |
252 | |
253 fh=Fonts[id]->Fnt[c].sy; | |
254 fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x; | |
255 | |
256 dx-=fw; yc=dx; | |
257 if ( dx >= 0 ) | |
258 for ( y=0;y < fh;y++ ) | |
259 { | |
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; | |
264 } | |
265 } | |
266 } | |
267 | |
268 return &item->Bitmap; | |
269 } |