typo fixes
[mplayer/greg.git] / libvo / font_load.c
blobbe445de86a9bcf9c13cff41d1e5cc01fe9fb3b96
1 #include "config.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
10 #include "font_load.h"
11 #include "mp_msg.h"
13 extern char *get_path ( char * );
15 raw_file* load_raw(char *name,int verbose){
16 int bpp;
17 raw_file* raw=malloc(sizeof(raw_file));
18 unsigned char head[32];
19 FILE *f=fopen(name,"rb");
20 if(!f) goto err_out; // can't open
21 if(fread(head,32,1,f)<1) goto err_out; // too small
22 if(memcmp(head,"mhwanh",6)) goto err_out; // not raw file
23 raw->w=head[8]*256+head[9];
24 raw->h=head[10]*256+head[11];
25 raw->c=head[12]*256+head[13];
26 if(raw->w == 0) // 2 bytes were not enough for the width... read 4 bytes from the end of the header
27 raw->w = ((head[28]*0x100 + head[29])*0x100 + head[30])*0x100 + head[31];
28 if(raw->c>256) goto err_out; // too many colors!?
29 mp_msg(MSGT_OSD, MSGL_DBG2, "RAW: %s %d x %d, %d colors\n",name,raw->w,raw->h,raw->c);
30 if(raw->c){
31 raw->pal=malloc(raw->c*3);
32 fread(raw->pal,3,raw->c,f);
33 bpp=1;
34 } else {
35 raw->pal=NULL;
36 bpp=3;
38 raw->bmp=malloc(raw->h*raw->w*bpp);
39 fread(raw->bmp,raw->h*raw->w*bpp,1,f);
40 fclose(f);
41 return raw;
43 err_out:
44 if (f)
45 fclose(f);
46 free(raw);
47 return NULL;
50 extern int sub_unicode;
52 font_desc_t* read_font_desc(char* fname,float factor,int verbose){
53 unsigned char sor[1024];
54 unsigned char sor2[1024];
55 font_desc_t *desc;
56 FILE *f = NULL;
57 char *dn;
58 struct stat fstate;
59 char section[64];
60 int i,j;
61 int chardb=0;
62 int fontdb=-1;
63 int version=0;
64 int first=1;
66 desc=malloc(sizeof(font_desc_t));if(!desc) goto fail_out;
67 memset(desc,0,sizeof(font_desc_t));
69 f=fopen(fname,"rt");if(!f){ mp_msg(MSGT_OSD, MSGL_V, "font: can't open file: %s\n",fname); goto fail_out;}
71 i = strlen (fname) - 9;
72 if ((dn = malloc(i+1))){
73 strncpy (dn, fname, i);
74 dn[i]='\0';
77 desc->fpath = dn; // search in the same dir as fonts.desc
79 // desc->fpath=get_path("font/");
80 // if (stat(desc->fpath, &fstate)!=0) desc->fpath=DATADIR"/font";
85 // set up some defaults, and erase table
86 desc->charspace=2;
87 desc->spacewidth=12;
88 desc->height=0;
89 for(i=0;i<65536;i++) desc->start[i]=desc->width[i]=desc->font[i]=-1;
91 section[0]=0;
93 while(fgets(sor,1020,f)){
94 unsigned char* p[8];
95 int pdb=0;
96 unsigned char *s=sor;
97 unsigned char *d=sor2;
98 int ec=' ';
99 int id=0;
100 sor[1020]=0;
102 /* skip files that look like: TTF (0x00, 0x01), PFM (0x00, 0x01), PFB
103 * (0x80, 0x01), PCF (0x01, 0x66), fon ("MZ"), gzipped (0x1f, 0x8b) */
105 if (first) {
106 if (!sor[0] || sor[1] == 1 || (sor[0] == 'M' && sor[1] == 'Z') || (sor[0] == 0x1f && sor[1] == 0x8b) || (sor[0] == 1 && sor[1] == 0x66)) {
107 mp_msg(MSGT_OSD, MSGL_ERR, "%s doesn't look like a font description, ignoring.\n", fname);
108 goto fail_out;
110 first = 0;
113 p[0]=d;++pdb;
114 while(1){
115 int c=*s++;
116 if(c==0 || c==13 || c==10) break;
117 if(!id){
118 if(c==39 || c==34){ id=c;continue;} // idezojel
119 if(c==';' || c=='#') break;
120 if(c==9) c=' ';
121 if(c==' '){
122 if(ec==' ') continue;
123 *d=0; ++d;
124 p[pdb]=d;++pdb;
125 if(pdb>=8) break;
126 continue;
128 } else {
129 if(id==c){ id=0;continue;} // idezojel
132 *d=c;d++;
133 ec=c;
135 if(d==sor2) continue; // skip empty lines
136 *d=0;
138 // printf("params=%d sor=%s\n",pdb,sor);
139 // for(i=0;i<pdb;i++) printf(" param %d = '%s'\n",i,p[i]);
141 if(pdb==1 && p[0][0]=='['){
142 int len=strlen(p[0]);
143 if(len && len<63 && p[0][len-1]==']'){
144 strcpy(section,p[0]);
145 mp_msg(MSGT_OSD, MSGL_DBG2, "font: Reading section: %s\n",section);
146 if(strcmp(section,"[files]")==0){
147 ++fontdb;
148 if(fontdb>=16){ mp_msg(MSGT_OSD, MSGL_ERR, "font: Too many bitmaps defined.\n");goto fail_out;}
150 continue;
154 if(strcmp(section,"[fpath]")==0){
155 if(pdb==1){
156 if (desc->fpath)
157 free (desc->fpath); // release previously allocated memory
158 desc->fpath=strdup(p[0]);
159 continue;
161 } else
163 if(strcmp(section,"[files]")==0){
164 char *default_dir=MPLAYER_DATADIR "/font";
165 if(pdb==2 && strcmp(p[0],"alpha")==0){
166 char *cp;
167 if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) goto fail_out;
169 snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s/%s",
170 desc->fpath,p[1]);
171 if(!((desc->pic_a[fontdb]=load_raw(cp,verbose)))){
172 free(cp);
173 if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2)))
174 goto fail_out;
175 snprintf(cp,strlen(default_dir)+strlen(p[1])+2,"%s/%s",
176 default_dir,p[1]);
177 if (!((desc->pic_a[fontdb]=load_raw(cp,verbose)))){
178 mp_msg(MSGT_OSD, MSGL_ERR, "Can't load font bitmap: %s\n",p[1]);
179 free(cp);
180 goto fail_out;
183 free(cp);
184 continue;
186 if(pdb==2 && strcmp(p[0],"bitmap")==0){
187 char *cp;
188 if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) goto fail_out;
190 snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s/%s",
191 desc->fpath,p[1]);
192 if(!((desc->pic_b[fontdb]=load_raw(cp,verbose)))){
193 free(cp);
194 if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2)))
195 goto fail_out;
196 snprintf(cp,strlen(default_dir)+strlen(p[1])+2,"%s/%s",
197 default_dir,p[1]);
198 if (!((desc->pic_b[fontdb]=load_raw(cp,verbose)))){
199 mp_msg(MSGT_OSD, MSGL_ERR, "Can't load font bitmap: %s\n",p[1]);
200 free(cp);
201 goto fail_out;
204 free(cp);
205 continue;
207 } else
209 if(strcmp(section,"[info]")==0){
210 if(pdb==2 && strcmp(p[0],"name")==0){
211 desc->name=strdup(p[1]);
212 continue;
214 if(pdb==2 && strcmp(p[0],"descversion")==0){
215 version=atoi(p[1]);
216 continue;
218 if(pdb==2 && strcmp(p[0],"spacewidth")==0){
219 desc->spacewidth=atoi(p[1]);
220 continue;
222 if(pdb==2 && strcmp(p[0],"charspace")==0){
223 desc->charspace=atoi(p[1]);
224 continue;
226 if(pdb==2 && strcmp(p[0],"height")==0){
227 desc->height=atoi(p[1]);
228 continue;
230 } else
232 if(strcmp(section,"[characters]")==0){
233 if(pdb==3){
234 int chr=p[0][0];
235 int start=atoi(p[1]);
236 int end=atoi(p[2]);
237 if(sub_unicode && (chr>=0x80)) chr=(chr<<8)+p[0][1];
238 else if(strlen(p[0])!=1) chr=strtol(p[0],NULL,0);
239 if(end<start) {
240 mp_msg(MSGT_OSD, MSGL_WARN, "error in font desc: end<start for char '%c'\n",chr);
241 } else {
242 desc->start[chr]=start;
243 desc->width[chr]=end-start+1;
244 desc->font[chr]=fontdb;
245 // printf("char %d '%c' start=%d width=%d\n",chr,chr,desc->start[chr],desc->width[chr]);
246 ++chardb;
248 continue;
251 mp_msg(MSGT_OSD, MSGL_ERR, "Syntax error in font desc: %s",sor);
252 goto fail_out;
255 fclose(f);
256 f = NULL;
258 if (first == 1) {
259 mp_msg(MSGT_OSD, MSGL_ERR, "%s is empty or a directory, ignoring.\n", fname);
260 goto fail_out;
263 //printf("font: pos of U = %d\n",desc->start[218]);
265 for(i=0;i<=fontdb;i++){
266 if(!desc->pic_a[i] || !desc->pic_b[i]){
267 mp_msg(MSGT_OSD, MSGL_ERR, "font: Missing bitmap(s) for sub-font #%d\n",i);
268 goto fail_out;
270 //if(factor!=1.0f)
272 // re-sample alpha
273 int f=factor*256.0f;
274 int size=desc->pic_a[i]->w*desc->pic_a[i]->h;
275 int j;
276 mp_msg(MSGT_OSD, MSGL_DBG2, "font: resampling alpha by factor %5.3f (%d) ",factor,f);fflush(stdout);
277 for(j=0;j<size;j++){
278 int x=desc->pic_a[i]->bmp[j]; // alpha
279 int y=desc->pic_b[i]->bmp[j]; // bitmap
281 #ifdef FAST_OSD
282 x=(x<(255-f))?0:1;
283 #else
285 x=255-((x*f)>>8); // scale
286 //if(x<0) x=0; else if(x>255) x=255;
287 //x^=255; // invert
289 if(x+y>255) x=255-y; // to avoid overflows
291 //x=0;
292 //x=((x*f*(255-y))>>16);
293 //x=((x*f*(255-y))>>16)+y;
294 //x=(x*f)>>8;if(x<y) x=y;
296 if(x<1) x=1; else
297 if(x>=252) x=0;
298 #endif
300 desc->pic_a[i]->bmp[j]=x;
301 // desc->pic_b[i]->bmp[j]=0; // hack
303 mp_msg(MSGT_OSD, MSGL_DBG2, "DONE!\n");
305 if(!desc->height) desc->height=desc->pic_a[i]->h;
308 j='_';if(desc->font[j]<0) j='?';
309 for(i=0;i<65536;i++)
310 if(desc->font[i]<0){
311 desc->start[i]=desc->start[j];
312 desc->width[i]=desc->width[j];
313 desc->font[i]=desc->font[j];
315 desc->font[' ']=-1;
316 desc->width[' ']=desc->spacewidth;
318 mp_msg(MSGT_OSD, MSGL_V, "Font %s loaded successfully! (%d chars)\n",fname,chardb);
320 return desc;
322 fail_out:
323 if (f)
324 fclose(f);
325 if (desc->fpath)
326 free(desc->fpath);
327 if (desc->name)
328 free(desc->name);
329 if (desc)
330 free(desc);
331 return NULL;
334 #if 0
335 int main(){
337 read_font_desc("high_arpi.desc",1);
340 #endif