10 #include "font_load.h"
13 raw_file
* load_raw(char *name
,int verbose
){
15 raw_file
* raw
=malloc(sizeof(raw_file
));
16 unsigned char head
[32];
17 FILE *f
=fopen(name
,"rb");
18 if(!f
) goto err_out
; // can't open
19 if(fread(head
,32,1,f
)<1) goto err_out
; // too small
20 if(memcmp(head
,"mhwanh",6)) goto err_out
; // not raw file
21 raw
->w
=head
[8]*256+head
[9];
22 raw
->h
=head
[10]*256+head
[11];
23 raw
->c
=head
[12]*256+head
[13];
24 if(raw
->w
== 0) // 2 bytes were not enough for the width... read 4 bytes from the end of the header
25 raw
->w
= ((head
[28]*0x100 + head
[29])*0x100 + head
[30])*0x100 + head
[31];
26 if(raw
->c
>256) goto err_out
; // too many colors!?
27 mp_msg(MSGT_OSD
, MSGL_DBG2
, "RAW: %s %d x %d, %d colors\n",name
,raw
->w
,raw
->h
,raw
->c
);
29 raw
->pal
=malloc(raw
->c
*3);
30 fread(raw
->pal
,3,raw
->c
,f
);
36 raw
->bmp
=malloc(raw
->h
*raw
->w
*bpp
);
37 fread(raw
->bmp
,raw
->h
*raw
->w
*bpp
,1,f
);
48 extern int sub_unicode
;
50 font_desc_t
* read_font_desc(const char* fname
,float factor
,int verbose
){
51 unsigned char sor
[1024];
52 unsigned char sor2
[1024];
64 desc
=malloc(sizeof(font_desc_t
));if(!desc
) goto fail_out
;
65 memset(desc
,0,sizeof(font_desc_t
));
67 f
=fopen(fname
,"rt");if(!f
){ mp_msg(MSGT_OSD
, MSGL_V
, "font: can't open file: %s\n",fname
); goto fail_out
;}
69 i
= strlen (fname
) - 9;
70 if ((dn
= malloc(i
+1))){
71 strncpy (dn
, fname
, i
);
75 desc
->fpath
= dn
; // search in the same dir as fonts.desc
77 // desc->fpath=get_path("font/");
78 // if (stat(desc->fpath, &fstate)!=0) desc->fpath=DATADIR"/font";
83 // set up some defaults, and erase table
87 for(i
=0;i
<65536;i
++) desc
->start
[i
]=desc
->width
[i
]=desc
->font
[i
]=-1;
91 while(fgets(sor
,1020,f
)){
95 unsigned char *d
=sor2
;
100 /* skip files that look like: TTF (0x00, 0x01), PFM (0x00, 0x01), PFB
101 * (0x80, 0x01), PCF (0x01, 0x66), fon ("MZ"), gzipped (0x1f, 0x8b) */
104 if (!sor
[0] || sor
[1] == 1 || (sor
[0] == 'M' && sor
[1] == 'Z') || (sor
[0] == 0x1f && sor
[1] == 0x8b) || (sor
[0] == 1 && sor
[1] == 0x66)) {
105 mp_msg(MSGT_OSD
, MSGL_ERR
, "%s doesn't look like a bitmap font description, ignoring.\n", fname
);
114 if(c
==0 || c
==13 || c
==10) break;
116 if(c
==39 || c
==34){ id
=c
;continue;} // idezojel
117 if(c
==';' || c
=='#') break;
120 if(ec
==' ') continue;
127 if(id
==c
){ id
=0;continue;} // idezojel
133 if(d
==sor2
) continue; // skip empty lines
136 // printf("params=%d sor=%s\n",pdb,sor);
137 // for(i=0;i<pdb;i++) printf(" param %d = '%s'\n",i,p[i]);
139 if(pdb
==1 && p
[0][0]=='['){
140 int len
=strlen(p
[0]);
141 if(len
&& len
<63 && p
[0][len
-1]==']'){
142 strcpy(section
,p
[0]);
143 mp_msg(MSGT_OSD
, MSGL_DBG2
, "font: Reading section: %s\n",section
);
144 if(strcmp(section
,"[files]")==0){
146 if(fontdb
>=16){ mp_msg(MSGT_OSD
, MSGL_ERR
, "font: Too many bitmaps defined.\n");goto fail_out
;}
152 if(strcmp(section
,"[fpath]")==0){
155 free (desc
->fpath
); // release previously allocated memory
156 desc
->fpath
=strdup(p
[0]);
162 #define FONT_PATH_SEP ""
164 //! path seperator for font paths, may not be more than one character
165 #define FONT_PATH_SEP "/"
168 if(strcmp(section
,"[files]")==0){
169 char *default_dir
=MPLAYER_DATADIR FONT_PATH_SEP
"font";
170 if(pdb
==2 && strcmp(p
[0],"alpha")==0){
172 if (!(cp
=malloc(strlen(desc
->fpath
)+strlen(p
[1])+2))) goto fail_out
;
174 snprintf(cp
,strlen(desc
->fpath
)+strlen(p
[1])+2,"%s" FONT_PATH_SEP
"%s",
176 if(!((desc
->pic_a
[fontdb
]=load_raw(cp
,verbose
)))){
178 if (!(cp
=malloc(strlen(default_dir
)+strlen(p
[1])+2)))
180 snprintf(cp
,strlen(default_dir
)+strlen(p
[1])+2,"%s" FONT_PATH_SEP
"%s",
182 if (!((desc
->pic_a
[fontdb
]=load_raw(cp
,verbose
)))){
183 mp_msg(MSGT_OSD
, MSGL_ERR
, "Can't load font bitmap: %s\n",p
[1]);
191 if(pdb
==2 && strcmp(p
[0],"bitmap")==0){
193 if (!(cp
=malloc(strlen(desc
->fpath
)+strlen(p
[1])+2))) goto fail_out
;
195 snprintf(cp
,strlen(desc
->fpath
)+strlen(p
[1])+2,"%s" FONT_PATH_SEP
"%s",
197 if(!((desc
->pic_b
[fontdb
]=load_raw(cp
,verbose
)))){
199 if (!(cp
=malloc(strlen(default_dir
)+strlen(p
[1])+2)))
201 snprintf(cp
,strlen(default_dir
)+strlen(p
[1])+2,"%s" FONT_PATH_SEP
"%s",
203 if (!((desc
->pic_b
[fontdb
]=load_raw(cp
,verbose
)))){
204 mp_msg(MSGT_OSD
, MSGL_ERR
, "Can't load font bitmap: %s\n",p
[1]);
214 if(strcmp(section
,"[info]")==0){
215 if(pdb
==2 && strcmp(p
[0],"name")==0){
216 desc
->name
=strdup(p
[1]);
219 if(pdb
==2 && strcmp(p
[0],"descversion")==0){
223 if(pdb
==2 && strcmp(p
[0],"spacewidth")==0){
224 desc
->spacewidth
=atoi(p
[1]);
227 if(pdb
==2 && strcmp(p
[0],"charspace")==0){
228 desc
->charspace
=atoi(p
[1]);
231 if(pdb
==2 && strcmp(p
[0],"height")==0){
232 desc
->height
=atoi(p
[1]);
237 if(strcmp(section
,"[characters]")==0){
240 int start
=atoi(p
[1]);
242 if(sub_unicode
&& (chr
>=0x80)) chr
=(chr
<<8)+p
[0][1];
243 else if(strlen(p
[0])!=1) chr
=strtol(p
[0],NULL
,0);
245 mp_msg(MSGT_OSD
, MSGL_WARN
, "error in font desc: end<start for char '%c'\n",chr
);
247 desc
->start
[chr
]=start
;
248 desc
->width
[chr
]=end
-start
+1;
249 desc
->font
[chr
]=fontdb
;
250 // printf("char %d '%c' start=%d width=%d\n",chr,chr,desc->start[chr],desc->width[chr]);
256 mp_msg(MSGT_OSD
, MSGL_ERR
, "Syntax error in font desc: %s",sor
);
264 mp_msg(MSGT_OSD
, MSGL_ERR
, "%s is empty or a directory, ignoring.\n", fname
);
268 //printf("font: pos of U = %d\n",desc->start[218]);
270 for(i
=0;i
<=fontdb
;i
++){
271 if(!desc
->pic_a
[i
] || !desc
->pic_b
[i
]){
272 mp_msg(MSGT_OSD
, MSGL_ERR
, "font: Missing bitmap(s) for sub-font #%d\n",i
);
279 int size
=desc
->pic_a
[i
]->w
*desc
->pic_a
[i
]->h
;
281 mp_msg(MSGT_OSD
, MSGL_DBG2
, "font: resampling alpha by factor %5.3f (%d) ",factor
,f
);fflush(stdout
);
283 int x
=desc
->pic_a
[i
]->bmp
[j
]; // alpha
284 int y
=desc
->pic_b
[i
]->bmp
[j
]; // bitmap
290 x
=255-((x
*f
)>>8); // scale
291 //if(x<0) x=0; else if(x>255) x=255;
294 if(x
+y
>255) x
=255-y
; // to avoid overflows
297 //x=((x*f*(255-y))>>16);
298 //x=((x*f*(255-y))>>16)+y;
299 //x=(x*f)>>8;if(x<y) x=y;
305 desc
->pic_a
[i
]->bmp
[j
]=x
;
306 // desc->pic_b[i]->bmp[j]=0; // hack
308 mp_msg(MSGT_OSD
, MSGL_DBG2
, "DONE!\n");
310 if(!desc
->height
) desc
->height
=desc
->pic_a
[i
]->h
;
313 j
='_';if(desc
->font
[j
]<0) j
='?';
316 desc
->start
[i
]=desc
->start
[j
];
317 desc
->width
[i
]=desc
->width
[j
];
318 desc
->font
[i
]=desc
->font
[j
];
321 desc
->width
[' ']=desc
->spacewidth
;
323 mp_msg(MSGT_OSD
, MSGL_V
, "Bitmap font %s loaded successfully! (%d chars)\n",fname
,chardb
);
342 read_font_desc("high_arpi.desc",1);