Mercurial > mplayer.hg
annotate gui/skin/font.c @ 32707:697fd62b65f9
Allow double quote to be defined in the font description file:
""" = x, y, width, height
author | ib |
---|---|
date | Tue, 18 Jan 2011 10:57:52 +0000 |
parents | 8fa2f43cb760 |
children | 6ff3cc81d602 |
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 |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
49 for ( i=0;i<256;i++ ) |
23077 | 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 { | |
32537
8fa2f43cb760
Remove most of the NULL pointer check before free all over the code
cboesch
parents:
30405
diff
changeset
|
62 free( Fonts[i]->Bitmap.Image ); |
23077 | 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 ); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
78 |
23077 | 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 ) ); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
83 if ( ( f=fopen( tmp,"rt" ) ) == NULL ) |
23077 | 84 { free( Fonts[id] ); return -3; } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
85 |
30405
d39acea63f2e
Simplify and correct loop condition, also avoids a compiler warning for unused result.
reimar
parents:
30404
diff
changeset
|
86 while ( fgets( tmp,255,f ) ) |
23077 | 87 { |
30405
d39acea63f2e
Simplify and correct loop condition, also avoids a compiler warning for unused result.
reimar
parents:
30404
diff
changeset
|
88 linenumber++; |
23077 | 89 |
30404
5fbb30fa62cc
Fix newline removal code that might read and write out of bounds.
reimar
parents:
29263
diff
changeset
|
90 // remove any kind of newline, if any |
5fbb30fa62cc
Fix newline removal code that might read and write out of bounds.
reimar
parents:
29263
diff
changeset
|
91 tmp[strcspn(tmp, "\n\r")] = 0; |
23077 | 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 ); | |
32707
697fd62b65f9
Allow double quote to be defined in the font description file:
ib
parents:
32537
diff
changeset
|
104 if ( !command[0] ) i=(int)'"'; |
697fd62b65f9
Allow double quote to be defined in the font description file:
ib
parents:
32537
diff
changeset
|
105 else i=(int)command[0]; |
23077 | 106 cutItem( param,tmp,',',0 ); Fonts[id]->Fnt[i].x=atoi( tmp ); |
107 cutItem( param,tmp,',',1 ); Fonts[id]->Fnt[i].y=atoi( tmp ); | |
108 cutItem( param,tmp,',',2 ); Fonts[id]->Fnt[i].sx=atoi( tmp ); | |
109 cutItem( param,tmp,',',3 ); Fonts[id]->Fnt[i].sy=atoi( tmp ); | |
110 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 } | |
112 else | |
113 { | |
114 if ( !strcmp( command,"image" ) ) | |
115 { | |
23703
9fb716ab06a3
Avoid code duplication and ugly config.h hack by using av_strlcat/av_strlcpy
reimar
parents:
23077
diff
changeset
|
116 av_strlcpy( tmp,path,sizeof( tmp ) ); av_strlcat( tmp,param,sizeof( tmp ) ); |
23077 | 117 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[font] font imagefile: %s\n",tmp ); |
118 if ( skinBPRead( tmp,&Fonts[id]->Bitmap ) ) return -4; | |
119 } | |
120 } | |
121 } | |
122 | |
123 return 0; | |
124 } | |
125 | |
126 int fntFindID( char * name ) | |
127 { | |
128 int i; | |
129 for ( i=0;i < 25;i++ ) | |
130 if ( Fonts[i] ) | |
131 if ( !strcmp( name,Fonts[i]->name ) ) return i; | |
132 return -1; | |
133 } | |
134 | |
135 int fntTextWidth( int id,char * str ) | |
136 { | |
137 int size = 0; | |
138 int i; | |
139 | |
140 if ( ( !Fonts[id] )||( !str[0] ) ) return 0; | |
141 | |
142 for ( i=0;i < (int)strlen( str );i++ ) | |
143 { | |
144 unsigned char c = (unsigned char)str[i]; | |
145 if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' '; | |
146 size+= Fonts[id]->Fnt[ c ].sx; | |
147 } | |
148 return size; | |
149 } | |
150 | |
151 int fntTextHeight( int id,char * str ) | |
152 { | |
153 int max = 0,i; | |
154 | |
155 if ( ( !Fonts[id] )||( !str[0] ) ) return 0; | |
156 | |
157 for ( i=0;i < (int)strlen( str );i++ ) | |
158 { | |
159 int h; | |
160 unsigned char c = (unsigned char)str[i]; | |
161 if ( Fonts[id]->Fnt[c].sx == -1 ) c = ' '; | |
162 h = Fonts[id]->Fnt[c].sy; | |
163 if ( h > max ) max=h; | |
164 } | |
165 return max; | |
166 } | |
167 | |
168 txSample * fntRender( wItem * item,int px,const char * fmt,... ) | |
169 { | |
170 va_list ap; | |
171 unsigned char p[512]; | |
172 unsigned int c; | |
173 int i, dx = 0, tw, fbw, iw, id, ofs; | |
174 int x,y,fh,fw,fyc,yc; | |
175 uint32_t * ibuf; | |
176 uint32_t * obuf; | |
177 | |
178 va_start( ap,fmt ); | |
179 vsnprintf( p,512,fmt,ap ); | |
180 va_end( ap ); | |
181 | |
182 iw=item->width; | |
183 id=item->fontid; | |
184 | |
185 if ( ( !item )|| | |
186 ( !Fonts[id] )|| | |
187 ( !p[0] )|| | |
188 ( !fntTextWidth( id,p ) ) ) return NULL; | |
189 | |
190 tw=fntTextWidth( id,p ); | |
191 fbw=Fonts[id]->Bitmap.Width; | |
192 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
193 if ( item->Bitmap.Image == NULL ) |
23077 | 194 { |
195 item->Bitmap.Height=item->height=fntTextHeight( id,p ); | |
196 item->Bitmap.Width=item->width=iw; | |
197 item->Bitmap.ImageSize=item->height * iw * 4; | |
198 if ( !item->Bitmap.ImageSize ) return NULL; | |
199 item->Bitmap.BPP=32; | |
200 item->Bitmap.Image=malloc( item->Bitmap.ImageSize ); | |
201 } | |
202 | |
203 obuf=(uint32_t *)item->Bitmap.Image; | |
204 ibuf=(uint32_t *)Fonts[id]->Bitmap.Image; | |
205 | |
206 for ( i=0;i < item->Bitmap.ImageSize / 4;i++ ) obuf[i]=0xff00ff; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
207 |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
208 if ( tw <= iw ) |
23077 | 209 { |
210 switch ( item->align ) | |
211 { | |
212 default: | |
213 case fntAlignLeft: dx=0; break; | |
214 case fntAlignCenter: dx=( iw - fntTextWidth( id,p ) ) / 2; break; | |
215 case fntAlignRight: dx=iw - fntTextWidth( id,p ); break; | |
216 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
217 |
23077 | 218 } else dx+=px; |
219 | |
220 ofs=dx; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
221 |
23077 | 222 for ( i=0;i < (int)strlen( p );i++ ) |
223 { | |
224 c=(unsigned int)p[i]; | |
225 fw=Fonts[id]->Fnt[c].sx; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
226 |
23077 | 227 if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
228 |
23077 | 229 fh=Fonts[id]->Fnt[c].sy; |
230 fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x; | |
231 yc=dx; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
232 |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
233 if ( dx >= 0 ) |
23077 | 234 for ( y=0;y < fh;y++ ) |
235 { | |
236 for ( x=0; x < fw;x++ ) | |
237 if ( dx + x >= 0 && dx + x < iw ) obuf[yc + x]=ibuf[ fyc + x ]; | |
238 fyc+=fbw; | |
239 yc+=iw; | |
240 } | |
241 dx+=fw; | |
242 } | |
243 | |
244 if ( ofs > 0 && tw > item->width ) | |
245 { | |
246 dx=ofs; | |
247 for ( i=(int)strlen( p );i > 0;i-- ) | |
248 { | |
249 c=(unsigned int)p[i]; | |
250 fw=Fonts[id]->Fnt[c].sx; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
251 |
23077 | 252 if ( fw == -1 ) { c=32; fw=Fonts[id]->Fnt[c].sx; } |
253 | |
254 fh=Fonts[id]->Fnt[c].sy; | |
255 fyc=Fonts[id]->Fnt[c].y * fbw + Fonts[id]->Fnt[c].x; | |
256 | |
257 dx-=fw; yc=dx; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
258 if ( dx >= 0 ) |
23077 | 259 for ( y=0;y < fh;y++ ) |
260 { | |
261 for ( x=fw - 1;x >= 0;x-- ) | |
262 if ( dx + x >= 0 && dx + x < iw ) obuf[yc + x]=ibuf[fyc + x]; | |
263 fyc+=fbw; | |
264 yc+=iw; | |
265 } | |
266 } | |
267 } | |
268 | |
269 return &item->Bitmap; | |
270 } |