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.
30 #include "libavutil/avstring.h"
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
)
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
50 Fonts
[id
]->Fnt
[i
].x
=Fonts
[id
]->Fnt
[i
].y
=Fonts
[id
]->Fnt
[i
].sx
=Fonts
[id
]->Fnt
[i
].sy
=-1;
55 void fntFreeFont( void )
62 if ( Fonts
[i
]->Bitmap
.Image
) free( Fonts
[i
]->Bitmap
.Image
);
69 int fntRead( char * path
,char * fname
)
72 unsigned char tmp
[512];
74 unsigned char command
[32];
75 unsigned char param
[256];
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; }
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;
96 if ( !tmp
[0] ) continue;
97 ptmp
=strswap( ptmp
,'\t',' ' );
99 cutItem( ptmp
,command
,'=',0 ); cutItem( ptmp
,param
,'=',1 );
100 if ( command
[0] == '"' )
103 cutItem( command
,command
,'"',1 );
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
);
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;
125 int fntFindID( char * name
)
128 for ( i
=0;i
< 25;i
++ )
130 if ( !strcmp( name
,Fonts
[i
]->name
) ) return i
;
134 int fntTextWidth( int id
,char * str
)
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
;
150 int fntTextHeight( int id
,char * str
)
154 if ( ( !Fonts
[id
] )||( !str
[0] ) ) return 0;
156 for ( i
=0;i
< (int)strlen( str
);i
++ )
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
;
167 txSample
* fntRender( wItem
* item
,int px
,const char * fmt
,... )
170 unsigned char p
[512];
172 int i
, dx
= 0, tw
, fbw
, iw
, id
, ofs
;
173 int x
,y
,fh
,fw
,fyc
,yc
;
178 vsnprintf( p
,512,fmt
,ap
);
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
;
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;
209 switch ( item
->align
)
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;
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
;
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
];
243 if ( ofs
> 0 && tw
> item
->width
)
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
;
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
];
268 return &item
->Bitmap
;