12 #include "../mp_msg.h"
13 #include "../libavutil/avstring.h"
17 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
};
19 int fntAddNewFont( char * name
)
24 for( id
=0;id
<26;id
++ )
25 if ( !Fonts
[id
] ) break;
27 if ( id
== 25 ) return -2;
29 if ( ( Fonts
[id
]=calloc( 1,sizeof( bmpFont
) ) ) == NULL
) return -1;
31 av_strlcpy( Fonts
[id
]->name
,name
,128 ); // FIXME: as defined in font.h
33 Fonts
[id
]->Fnt
[i
].x
=Fonts
[id
]->Fnt
[i
].y
=Fonts
[id
]->Fnt
[i
].sx
=Fonts
[id
]->Fnt
[i
].sy
=-1;
38 void fntFreeFont( void )
45 if ( Fonts
[i
]->Bitmap
.Image
) free( Fonts
[i
]->Bitmap
.Image
);
52 int fntRead( char * path
,char * fname
)
55 unsigned char tmp
[512];
57 unsigned char command
[32];
58 unsigned char param
[256];
60 int id
= fntAddNewFont( fname
);
62 if ( id
< 0 ) return id
;
64 av_strlcpy( tmp
,path
,sizeof( tmp
) );
65 av_strlcat( tmp
,fname
,sizeof( tmp
) ); av_strlcat( tmp
,".fnt",sizeof( tmp
) );
66 if ( ( f
=fopen( tmp
,"rt" ) ) == NULL
)
67 { free( Fonts
[id
] ); return -3; }
71 fgets( tmp
,255,f
); linenumber
++;
73 c
=tmp
[ strlen( tmp
) - 1 ]; if ( ( c
== '\n' )||( c
== '\r' ) ) tmp
[ strlen( tmp
) - 1 ]=0;
74 c
=tmp
[ strlen( tmp
) - 1 ]; if ( ( c
== '\n' )||( c
== '\r' ) ) tmp
[ strlen( tmp
) - 1 ]=0;
75 for ( c
=0;c
< (int)strlen( tmp
);c
++ )
76 if ( tmp
[c
] == ';' ) { tmp
[c
]=0; break; }
77 if ( !tmp
[0] ) continue;
79 if ( !tmp
[0] ) continue;
80 ptmp
=strswap( ptmp
,'\t',' ' );
82 cutItem( ptmp
,command
,'=',0 ); cutItem( ptmp
,param
,'=',1 );
83 if ( command
[0] == '"' )
86 cutItem( command
,command
,'"',1 );
88 cutItem( param
,tmp
,',',0 ); Fonts
[id
]->Fnt
[i
].x
=atoi( tmp
);
89 cutItem( param
,tmp
,',',1 ); Fonts
[id
]->Fnt
[i
].y
=atoi( tmp
);
90 cutItem( param
,tmp
,',',2 ); Fonts
[id
]->Fnt
[i
].sx
=atoi( tmp
);
91 cutItem( param
,tmp
,',',3 ); Fonts
[id
]->Fnt
[i
].sy
=atoi( tmp
);
92 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
);
96 if ( !strcmp( command
,"image" ) )
98 av_strlcpy( tmp
,path
,sizeof( tmp
) ); av_strlcat( tmp
,param
,sizeof( tmp
) );
99 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[font] font imagefile: %s\n",tmp
);
100 if ( skinBPRead( tmp
,&Fonts
[id
]->Bitmap
) ) return -4;
108 int fntFindID( char * name
)
111 for ( i
=0;i
< 25;i
++ )
113 if ( !strcmp( name
,Fonts
[i
]->name
) ) return i
;
117 int fntTextWidth( int id
,char * str
)
122 if ( ( !Fonts
[id
] )||( !str
[0] ) ) return 0;
124 for ( i
=0;i
< (int)strlen( str
);i
++ )
126 unsigned char c
= (unsigned char)str
[i
];
127 if ( Fonts
[id
]->Fnt
[c
].sx
== -1 ) c
= ' ';
128 size
+= Fonts
[id
]->Fnt
[ c
].sx
;
133 int fntTextHeight( int id
,char * str
)
137 if ( ( !Fonts
[id
] )||( !str
[0] ) ) return 0;
139 for ( i
=0;i
< (int)strlen( str
);i
++ )
142 unsigned char c
= (unsigned char)str
[i
];
143 if ( Fonts
[id
]->Fnt
[c
].sx
== -1 ) c
= ' ';
144 h
= Fonts
[id
]->Fnt
[c
].sy
;
145 if ( h
> max
) max
=h
;
150 txSample
* fntRender( wItem
* item
,int px
,const char * fmt
,... )
153 unsigned char p
[512];
155 int i
, dx
= 0, tw
, fbw
, iw
, id
, ofs
;
156 int x
,y
,fh
,fw
,fyc
,yc
;
161 vsnprintf( p
,512,fmt
,ap
);
170 ( !fntTextWidth( id
,p
) ) ) return NULL
;
172 tw
=fntTextWidth( id
,p
);
173 fbw
=Fonts
[id
]->Bitmap
.Width
;
175 if ( item
->Bitmap
.Image
== NULL
)
177 item
->Bitmap
.Height
=item
->height
=fntTextHeight( id
,p
);
178 item
->Bitmap
.Width
=item
->width
=iw
;
179 item
->Bitmap
.ImageSize
=item
->height
* iw
* 4;
180 if ( !item
->Bitmap
.ImageSize
) return NULL
;
182 item
->Bitmap
.Image
=malloc( item
->Bitmap
.ImageSize
);
185 obuf
=(uint32_t *)item
->Bitmap
.Image
;
186 ibuf
=(uint32_t *)Fonts
[id
]->Bitmap
.Image
;
188 for ( i
=0;i
< item
->Bitmap
.ImageSize
/ 4;i
++ ) obuf
[i
]=0xff00ff;
192 switch ( item
->align
)
195 case fntAlignLeft
: dx
=0; break;
196 case fntAlignCenter
: dx
=( iw
- fntTextWidth( id
,p
) ) / 2; break;
197 case fntAlignRight
: dx
=iw
- fntTextWidth( id
,p
); break;
204 for ( i
=0;i
< (int)strlen( p
);i
++ )
206 c
=(unsigned int)p
[i
];
207 fw
=Fonts
[id
]->Fnt
[c
].sx
;
209 if ( fw
== -1 ) { c
=32; fw
=Fonts
[id
]->Fnt
[c
].sx
; }
211 fh
=Fonts
[id
]->Fnt
[c
].sy
;
212 fyc
=Fonts
[id
]->Fnt
[c
].y
* fbw
+ Fonts
[id
]->Fnt
[c
].x
;
216 for ( y
=0;y
< fh
;y
++ )
218 for ( x
=0; x
< fw
;x
++ )
219 if ( dx
+ x
>= 0 && dx
+ x
< iw
) obuf
[yc
+ x
]=ibuf
[ fyc
+ x
];
226 if ( ofs
> 0 && tw
> item
->width
)
229 for ( i
=(int)strlen( p
);i
> 0;i
-- )
231 c
=(unsigned int)p
[i
];
232 fw
=Fonts
[id
]->Fnt
[c
].sx
;
234 if ( fw
== -1 ) { c
=32; fw
=Fonts
[id
]->Fnt
[c
].sx
; }
236 fh
=Fonts
[id
]->Fnt
[c
].sy
;
237 fyc
=Fonts
[id
]->Fnt
[c
].y
* fbw
+ Fonts
[id
]->Fnt
[c
].x
;
241 for ( y
=0;y
< fh
;y
++ )
243 for ( x
=fw
- 1;x
>= 0;x
-- )
244 if ( dx
+ x
>= 0 && dx
+ x
< iw
) obuf
[yc
+ x
]=ibuf
[fyc
+ x
];
251 return &item
->Bitmap
;