VO, AO: remove obsolete/problematic VO/AO drivers
[mplayer.git] / sub / font_load.c
blobd8f9eafe34c045b330e4486ecdac101dff7b1141
1 /*
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.
19 #include "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "osdep/io.h"
27 #include "font_load.h"
28 #include "mp_msg.h"
29 #include "libavutil/attributes.h"
31 raw_file* load_raw(char *name,int verbose){
32 int bpp;
33 raw_file* raw=malloc(sizeof(raw_file));
34 unsigned char head[32];
35 FILE *f=fopen(name,"rb");
36 if(!f) goto err_out; // can't open
37 if(fread(head,32,1,f)<1) goto err_out; // too small
38 if(memcmp(head,"mhwanh",6)) goto err_out; // not raw file
39 raw->w=head[8]*256+head[9];
40 raw->h=head[10]*256+head[11];
41 raw->c=head[12]*256+head[13];
42 if(raw->w == 0) // 2 bytes were not enough for the width... read 4 bytes from the end of the header
43 raw->w = ((head[28]*0x100 + head[29])*0x100 + head[30])*0x100 + head[31];
44 if(raw->c>256) goto err_out; // too many colors!?
45 mp_msg(MSGT_OSD, MSGL_DBG2, "RAW: %s %d x %d, %d colors\n",name,raw->w,raw->h,raw->c);
46 if(raw->c){
47 raw->pal=malloc(raw->c*3);
48 fread(raw->pal,3,raw->c,f);
49 bpp=1;
50 } else {
51 raw->pal=NULL;
52 bpp=3;
54 raw->bmp=malloc(raw->h*raw->w*bpp);
55 fread(raw->bmp,raw->h*raw->w*bpp,1,f);
56 fclose(f);
57 return raw;
59 err_out:
60 if (f)
61 fclose(f);
62 free(raw);
63 return NULL;
66 extern int sub_unicode;
68 font_desc_t* read_font_desc(const char* fname,float factor,int verbose){
69 unsigned char sor[1024];
70 unsigned char sor2[1024];
71 font_desc_t *desc;
72 FILE *f = NULL;
73 char *dn;
74 char section[64];
75 int i,j;
76 int chardb=0;
77 int fontdb=-1;
78 int version av_unused;
79 int first=1;
81 desc=malloc(sizeof(font_desc_t));if(!desc) goto fail_out;
82 memset(desc,0,sizeof(font_desc_t));
84 f=fopen(fname,"rt");if(!f){ mp_msg(MSGT_OSD, MSGL_V, "font: can't open file: %s\n",fname); goto fail_out;}
86 i = strlen (fname) - 9;
87 if ((dn = malloc(i+1))){
88 strncpy (dn, fname, i);
89 dn[i]='\0';
92 desc->fpath = dn; // search in the same dir as fonts.desc
96 // set up some defaults, and erase table
97 desc->charspace=2;
98 desc->spacewidth=12;
99 desc->height=0;
100 for(i=0;i<65536;i++) desc->start[i]=desc->width[i]=desc->font[i]=-1;
102 section[0]=0;
104 while(fgets(sor,1020,f)){
105 unsigned char* p[8];
106 int pdb=0;
107 unsigned char *s=sor;
108 unsigned char *d=sor2;
109 int ec=' ';
110 int id=0;
111 sor[1020]=0;
113 /* skip files that look like: TTF (0x00, 0x01), PFM (0x00, 0x01), PFB
114 * (0x80, 0x01), PCF (0x01, 0x66), fon ("MZ"), gzipped (0x1f, 0x8b) */
116 if (first) {
117 if (!sor[0] || sor[1] == 1 || (sor[0] == 'M' && sor[1] == 'Z') || (sor[0] == 0x1f && sor[1] == 0x8b) || (sor[0] == 1 && sor[1] == 0x66)) {
118 mp_msg(MSGT_OSD, MSGL_ERR, "%s doesn't look like a bitmap font description, ignoring.\n", fname);
119 goto fail_out;
121 first = 0;
124 p[0]=d;++pdb;
125 while(1){
126 int c=*s++;
127 if(c==0 || c==13 || c==10) break;
128 if(!id){
129 if(c==39 || c==34){ id=c;continue;} // idezojel
130 if(c==';' || c=='#') break;
131 if(c==9) c=' ';
132 if(c==' '){
133 if(ec==' ') continue;
134 *d=0; ++d;
135 p[pdb]=d;++pdb;
136 if(pdb>=8) break;
137 continue;
139 } else {
140 if(id==c){ id=0;continue;} // idezojel
143 *d=c;d++;
144 ec=c;
146 if(d==sor2) continue; // skip empty lines
147 *d=0;
149 // printf("params=%d sor=%s\n",pdb,sor);
150 // for(i=0;i<pdb;i++) printf(" param %d = '%s'\n",i,p[i]);
152 if(pdb==1 && p[0][0]=='['){
153 int len=strlen(p[0]);
154 if(len && len<63 && p[0][len-1]==']'){
155 strcpy(section,p[0]);
156 mp_msg(MSGT_OSD, MSGL_DBG2, "font: Reading section: %s\n",section);
157 if(strcmp(section,"[files]")==0){
158 ++fontdb;
159 if(fontdb>=16){ mp_msg(MSGT_OSD, MSGL_ERR, "font: Too many bitmaps defined.\n");goto fail_out;}
161 continue;
165 if(strcmp(section,"[fpath]")==0){
166 if(pdb==1){
167 free (desc->fpath); // release previously allocated memory
168 desc->fpath=strdup(p[0]);
169 continue;
171 } else
173 #ifdef __AMIGAOS4__
174 #define FONT_PATH_SEP ""
175 #else
176 //! path seperator for font paths, may not be more than one character
177 #define FONT_PATH_SEP "/"
178 #endif
180 if(strcmp(section,"[files]")==0){
181 char *default_dir=MPLAYER_DATADIR FONT_PATH_SEP "font";
182 if(pdb==2 && strcmp(p[0],"alpha")==0){
183 char *cp;
184 if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) goto fail_out;
186 snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
187 desc->fpath,p[1]);
188 if(!((desc->pic_a[fontdb]=load_raw(cp,verbose)))){
189 free(cp);
190 if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2)))
191 goto fail_out;
192 snprintf(cp,strlen(default_dir)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
193 default_dir,p[1]);
194 if (!((desc->pic_a[fontdb]=load_raw(cp,verbose)))){
195 mp_msg(MSGT_OSD, MSGL_ERR, "Can't load font bitmap: %s\n",p[1]);
196 free(cp);
197 goto fail_out;
200 free(cp);
201 continue;
203 if(pdb==2 && strcmp(p[0],"bitmap")==0){
204 char *cp;
205 if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) goto fail_out;
207 snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
208 desc->fpath,p[1]);
209 if(!((desc->pic_b[fontdb]=load_raw(cp,verbose)))){
210 free(cp);
211 if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2)))
212 goto fail_out;
213 snprintf(cp,strlen(default_dir)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
214 default_dir,p[1]);
215 if (!((desc->pic_b[fontdb]=load_raw(cp,verbose)))){
216 mp_msg(MSGT_OSD, MSGL_ERR, "Can't load font bitmap: %s\n",p[1]);
217 free(cp);
218 goto fail_out;
221 free(cp);
222 continue;
224 } else
226 if(strcmp(section,"[info]")==0){
227 if(pdb==2 && strcmp(p[0],"name")==0){
228 desc->name=strdup(p[1]);
229 continue;
231 if(pdb==2 && strcmp(p[0],"descversion")==0){
232 version=atoi(p[1]);
233 continue;
235 if(pdb==2 && strcmp(p[0],"spacewidth")==0){
236 desc->spacewidth=atoi(p[1]);
237 continue;
239 if(pdb==2 && strcmp(p[0],"charspace")==0){
240 desc->charspace=atoi(p[1]);
241 continue;
243 if(pdb==2 && strcmp(p[0],"height")==0){
244 desc->height=atoi(p[1]);
245 continue;
247 } else
249 if(strcmp(section,"[characters]")==0){
250 if(pdb==3){
251 int chr=p[0][0];
252 int start=atoi(p[1]);
253 int end=atoi(p[2]);
254 if(sub_unicode && (chr>=0x80)) chr=(chr<<8)+p[0][1];
255 else if(strlen(p[0])!=1) chr=strtol(p[0],NULL,0);
256 if(end<start) {
257 mp_msg(MSGT_OSD, MSGL_WARN, "error in font desc: end<start for char '%c'\n",chr);
258 } else {
259 desc->start[chr]=start;
260 desc->width[chr]=end-start+1;
261 desc->font[chr]=fontdb;
262 // printf("char %d '%c' start=%d width=%d\n",chr,chr,desc->start[chr],desc->width[chr]);
263 ++chardb;
265 continue;
268 mp_msg(MSGT_OSD, MSGL_ERR, "Syntax error in font desc: %s",sor);
269 goto fail_out;
272 fclose(f);
273 f = NULL;
275 if (first == 1) {
276 mp_msg(MSGT_OSD, MSGL_ERR, "%s is empty or a directory, ignoring.\n", fname);
277 goto fail_out;
280 //printf("font: pos of U = %d\n",desc->start[218]);
282 for(i=0;i<=fontdb;i++){
283 if(!desc->pic_a[i] || !desc->pic_b[i]){
284 mp_msg(MSGT_OSD, MSGL_ERR, "font: Missing bitmap(s) for sub-font #%d\n",i);
285 goto fail_out;
287 //if(factor!=1.0f)
289 // re-sample alpha
290 int f=factor*256.0f;
291 int size=desc->pic_a[i]->w*desc->pic_a[i]->h;
292 int j;
293 mp_msg(MSGT_OSD, MSGL_DBG2, "font: resampling alpha by factor %5.3f (%d) ",factor,f);fflush(stdout);
294 for(j=0;j<size;j++){
295 int x=desc->pic_a[i]->bmp[j]; // alpha
296 int y=desc->pic_b[i]->bmp[j]; // bitmap
298 #ifdef FAST_OSD
299 x=(x<(255-f))?0:1;
300 #else
302 x=255-((x*f)>>8); // scale
303 //if(x<0) x=0; else if(x>255) x=255;
304 //x^=255; // invert
306 if(x+y>255) x=255-y; // to avoid overflows
308 //x=0;
309 //x=((x*f*(255-y))>>16);
310 //x=((x*f*(255-y))>>16)+y;
311 //x=(x*f)>>8;if(x<y) x=y;
313 if(x<1) x=1; else
314 if(x>=252) x=0;
315 #endif
317 desc->pic_a[i]->bmp[j]=x;
318 // desc->pic_b[i]->bmp[j]=0; // hack
320 mp_msg(MSGT_OSD, MSGL_DBG2, "DONE!\n");
322 if(!desc->height) desc->height=desc->pic_a[i]->h;
325 j='_';if(desc->font[j]<0) j='?';
326 for(i=0;i<65536;i++)
327 if(desc->font[i]<0){
328 desc->start[i]=desc->start[j];
329 desc->width[i]=desc->width[j];
330 desc->font[i]=desc->font[j];
332 desc->font[' ']=-1;
333 desc->width[' ']=desc->spacewidth;
335 mp_msg(MSGT_OSD, MSGL_V, "Bitmap font %s loaded successfully! (%d chars)\n",fname,chardb);
337 return desc;
339 fail_out:
340 if (f)
341 fclose(f);
342 free(desc->fpath);
343 free(desc->name);
344 free(desc);
345 return NULL;
348 #ifndef CONFIG_FREETYPE
349 void render_one_glyph(font_desc_t *desc, int c) {}
350 int kerning(font_desc_t *desc, int prevc, int c) { return 0; }
351 #endif