Mercurial > mplayer.hg
annotate libvo/font_load.c @ 13701:ce163851db17
sync by Carl F«ärstenberg <azatoth at gmail dot com>
fixes by Jan Knutar <jknutar at nic dot fi>
author | diego |
---|---|
date | Wed, 20 Oct 2004 09:40:48 +0000 |
parents | a22f1b86ec0f |
children | 41a4fc8421e9 |
rev | line source |
---|---|
7122
0dc9cb756b68
freetype 2.0/2.1+ support - disabled by default until bugs fixed
arpi
parents:
5928
diff
changeset
|
1 #include "config.h" |
0dc9cb756b68
freetype 2.0/2.1+ support - disabled by default until bugs fixed
arpi
parents:
5928
diff
changeset
|
2 |
213 | 3 #include <stdio.h> |
4 #include <stdlib.h> | |
5 #include <string.h> | |
1446 | 6 #include <sys/types.h> |
7 #include <sys/stat.h> | |
8 #include <unistd.h> | |
213 | 9 |
10 #include "font_load.h" | |
11 | |
2476 | 12 extern char *get_path ( char * ); |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
13 |
339 | 14 raw_file* load_raw(char *name,int verbose){ |
213 | 15 int bpp; |
16 raw_file* raw=malloc(sizeof(raw_file)); | |
17 unsigned char head[32]; | |
18 FILE *f=fopen(name,"rb"); | |
19 if(!f) return NULL; // can't open | |
20 if(fread(head,32,1,f)<1) return NULL; // too small | |
21 if(memcmp(head,"mhwanh",6)) return NULL; // not raw file | |
22 raw->w=head[8]*256+head[9]; | |
23 raw->h=head[10]*256+head[11]; | |
24 raw->c=head[12]*256+head[13]; | |
5928
48e91dc9534b
.raw width>=65536 support by Georgi Georgiev <chutz@chubaka.homeip.net>
arpi
parents:
2476
diff
changeset
|
25 if(raw->w == 0) // 2 bytes were not enough for the width... read 4 bytes from the end of the header |
48e91dc9534b
.raw width>=65536 support by Georgi Georgiev <chutz@chubaka.homeip.net>
arpi
parents:
2476
diff
changeset
|
26 raw->w = ((head[28]*0x100 + head[29])*0x100 + head[30])*0x100 + head[31]; |
213 | 27 if(raw->c>256) return NULL; // too many colors!? |
339 | 28 if(verbose) printf("RAW: %s %d x %d, %d colors\n",name,raw->w,raw->h,raw->c); |
213 | 29 if(raw->c){ |
30 raw->pal=malloc(raw->c*3); | |
31 fread(raw->pal,3,raw->c,f); | |
32 bpp=1; | |
33 } else { | |
34 raw->pal=NULL; | |
35 bpp=3; | |
36 } | |
37 raw->bmp=malloc(raw->h*raw->w*bpp); | |
38 fread(raw->bmp,raw->h*raw->w*bpp,1,f); | |
39 fclose(f); | |
40 return raw; | |
41 } | |
42 | |
728 | 43 extern int sub_unicode; |
44 | |
339 | 45 font_desc_t* read_font_desc(char* fname,float factor,int verbose){ |
213 | 46 unsigned char sor[1024]; |
47 unsigned char sor2[1024]; | |
48 font_desc_t *desc; | |
49 FILE *f; | |
2222
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
50 char *dn; |
1446 | 51 struct stat fstate; |
213 | 52 char section[64]; |
53 int i,j; | |
54 int chardb=0; | |
55 int fontdb=-1; | |
214 | 56 int version=0; |
8635
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
57 int first=1; |
213 | 58 |
13641 | 59 desc=malloc(sizeof(font_desc_t));if(!desc) goto fail_out; |
213 | 60 memset(desc,0,sizeof(font_desc_t)); |
61 | |
13641 | 62 f=fopen(fname,"rt");if(!f){ printf("font: can't open file: %s\n",fname); goto fail_out;} |
213 | 63 |
2222
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
64 i = strlen (fname) - 9; |
2223 | 65 if ((dn = malloc(i+1))){ |
2222
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
66 strncpy (dn, fname, i); |
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
67 dn[i]='\0'; |
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
68 } |
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
69 |
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
70 desc->fpath = dn; // search in the same dir as fonts.desc |
1446 | 71 |
2222
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
72 // desc->fpath=get_path("font/"); |
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
73 // if (stat(desc->fpath, &fstate)!=0) desc->fpath=DATADIR"/font"; |
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
74 |
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
75 |
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
76 |
1446 | 77 |
213 | 78 // set up some defaults, and erase table |
79 desc->charspace=2; | |
80 desc->spacewidth=12; | |
81 desc->height=0; | |
12609
4be019266884
array initialization fix by SungKwanKang <ksquarekr at yahoo.com>
faust3
parents:
10272
diff
changeset
|
82 for(i=0;i<65536;i++) desc->start[i]=desc->width[i]=desc->font[i]=-1; |
213 | 83 |
84 section[0]=0; | |
85 | |
86 while(fgets(sor,1020,f)){ | |
87 unsigned char* p[8]; | |
88 int pdb=0; | |
89 unsigned char *s=sor; | |
90 unsigned char *d=sor2; | |
91 int ec=' '; | |
92 int id=0; | |
93 sor[1020]=0; | |
8635
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
94 |
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
95 /* skip files that look like: TTF (0x00, 0x01), PFM (0x00, 0x01), PFB |
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
96 * (0x80, 0x01), PCF (0x01, 0x66), fon ("MZ"), gzipped (0x1f, 0x8b) */ |
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
97 |
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
98 if (first) { |
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
99 if (!sor[0] || sor[1] == 1 || (sor[0] == 'M' && sor[1] == 'Z') || (sor[0] == 0x1f && sor[1] == 0x8b) || (sor[0] == 1 && sor[1] == 0x66)) { |
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
100 printf("%s doesn't look like a font description, ignoring\n", fname); |
13641 | 101 goto fail_out; |
8635
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
102 } |
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
103 first = 0; |
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
104 } |
81dbd28ef7c0
these patches let ,,oldstyle'' and freetype subtitle renderers live
arpi
parents:
7122
diff
changeset
|
105 |
213 | 106 p[0]=d;++pdb; |
107 while(1){ | |
108 int c=*s++; | |
109 if(c==0 || c==13 || c==10) break; | |
110 if(!id){ | |
111 if(c==39 || c==34){ id=c;continue;} // idezojel | |
112 if(c==';' || c=='#') break; | |
113 if(c==9) c=' '; | |
114 if(c==' '){ | |
115 if(ec==' ') continue; | |
116 *d=0; ++d; | |
117 p[pdb]=d;++pdb; | |
118 if(pdb>=8) break; | |
119 continue; | |
120 } | |
121 } else { | |
122 if(id==c){ id=0;continue;} // idezojel | |
123 | |
124 } | |
125 *d=c;d++; | |
126 ec=c; | |
127 } | |
128 if(d==sor2) continue; // skip empty lines | |
129 *d=0; | |
130 | |
131 // printf("params=%d sor=%s\n",pdb,sor); | |
132 // for(i=0;i<pdb;i++) printf(" param %d = '%s'\n",i,p[i]); | |
133 | |
134 if(pdb==1 && p[0][0]=='['){ | |
135 int len=strlen(p[0]); | |
136 if(len && len<63 && p[0][len-1]==']'){ | |
137 strcpy(section,p[0]); | |
339 | 138 if(verbose) printf("font: Reading section: %s\n",section); |
213 | 139 if(strcmp(section,"[files]")==0){ |
140 ++fontdb; | |
13641 | 141 if(fontdb>=16){ printf("font: Too many bitmaps defined!\n");goto fail_out;} |
213 | 142 } |
143 continue; | |
144 } | |
145 } | |
146 | |
1353 | 147 if(strcmp(section,"[fpath]")==0){ |
148 if(pdb==1){ | |
2222
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
149 if (desc->fpath) |
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
150 free (desc->fpath); // release previously allocated memory |
1353 | 151 desc->fpath=strdup(p[0]); |
152 continue; | |
153 } | |
154 } else | |
155 | |
213 | 156 if(strcmp(section,"[files]")==0){ |
10272
7b0bc557987b
renames: DATADIR->MPLAYER_DATADIR, CONFDIR->MPLAYER_CONFDIR, LIBDIR->MPLAYER_LIBDIR
arpi
parents:
8635
diff
changeset
|
157 char *default_dir=MPLAYER_DATADIR "/font"; |
213 | 158 if(pdb==2 && strcmp(p[0],"alpha")==0){ |
1353 | 159 char *cp; |
13641 | 160 if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) goto fail_out; |
1353 | 161 |
162 snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s/%s", | |
163 desc->fpath,p[1]); | |
164 if(!((desc->pic_a[fontdb]=load_raw(cp,verbose)))){ | |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
165 free(cp); |
2238
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
166 if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2))) |
13641 | 167 goto fail_out; |
2238
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
168 snprintf(cp,strlen(default_dir)+strlen(p[1])+2,"%s/%s", |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
169 default_dir,p[1]); |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
170 if (!((desc->pic_a[fontdb]=load_raw(cp,verbose)))){ |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
171 printf("Can't load font bitmap: %s\n",p[1]); |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
172 free(cp); |
13641 | 173 goto fail_out; |
2238
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
174 } |
213 | 175 } |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
176 free(cp); |
213 | 177 continue; |
178 } | |
179 if(pdb==2 && strcmp(p[0],"bitmap")==0){ | |
1353 | 180 char *cp; |
13641 | 181 if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) goto fail_out; |
1353 | 182 |
183 snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s/%s", | |
184 desc->fpath,p[1]); | |
185 if(!((desc->pic_b[fontdb]=load_raw(cp,verbose)))){ | |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
186 free(cp); |
2238
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
187 if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2))) |
13641 | 188 goto fail_out; |
2238
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
189 snprintf(cp,strlen(default_dir)+strlen(p[1])+2,"%s/%s", |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
190 default_dir,p[1]); |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
191 if (!((desc->pic_b[fontdb]=load_raw(cp,verbose)))){ |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
192 printf("Can't load font bitmap: %s\n",p[1]); |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
193 free(cp); |
13641 | 194 goto fail_out; |
2238
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
195 } |
213 | 196 } |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
197 free(cp); |
213 | 198 continue; |
199 } | |
200 } else | |
201 | |
202 if(strcmp(section,"[info]")==0){ | |
214 | 203 if(pdb==2 && strcmp(p[0],"name")==0){ |
204 desc->name=strdup(p[1]); | |
205 continue; | |
206 } | |
207 if(pdb==2 && strcmp(p[0],"descversion")==0){ | |
208 version=atoi(p[1]); | |
209 continue; | |
210 } | |
213 | 211 if(pdb==2 && strcmp(p[0],"spacewidth")==0){ |
212 desc->spacewidth=atoi(p[1]); | |
213 continue; | |
214 } | |
215 if(pdb==2 && strcmp(p[0],"charspace")==0){ | |
216 desc->charspace=atoi(p[1]); | |
217 continue; | |
218 } | |
219 if(pdb==2 && strcmp(p[0],"height")==0){ | |
220 desc->height=atoi(p[1]); | |
221 continue; | |
222 } | |
223 } else | |
214 | 224 |
213 | 225 if(strcmp(section,"[characters]")==0){ |
219 | 226 if(pdb==3){ |
213 | 227 int chr=p[0][0]; |
228 int start=atoi(p[1]); | |
229 int end=atoi(p[2]); | |
727 | 230 if(sub_unicode && (chr>=0x80)) chr=(chr<<8)+p[0][1]; |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
340
diff
changeset
|
231 else if(strlen(p[0])!=1) chr=strtol(p[0],NULL,0); |
213 | 232 if(end<start) { |
233 printf("error in font desc: end<start for char '%c'\n",chr); | |
234 } else { | |
235 desc->start[chr]=start; | |
236 desc->width[chr]=end-start+1; | |
237 desc->font[chr]=fontdb; | |
238 // printf("char %d '%c' start=%d width=%d\n",chr,chr,desc->start[chr],desc->width[chr]); | |
239 ++chardb; | |
240 } | |
241 continue; | |
242 } | |
243 } | |
244 printf("Syntax error in font desc: %s\n",sor); | |
13641 | 245 goto fail_out; |
213 | 246 |
247 } | |
248 fclose(f); | |
13641 | 249 f = NULL; |
213 | 250 |
12793
a9429d90157a
avoid using corrupted font descriptions patch by Daniel von Dincklage <danielvd+mpl@cs.colorado.edu>
faust3
parents:
12609
diff
changeset
|
251 if (first == 1) { |
a9429d90157a
avoid using corrupted font descriptions patch by Daniel von Dincklage <danielvd+mpl@cs.colorado.edu>
faust3
parents:
12609
diff
changeset
|
252 printf("%s is empty or a directory, ignoring\n", fname); |
13641 | 253 goto fail_out; |
12793
a9429d90157a
avoid using corrupted font descriptions patch by Daniel von Dincklage <danielvd+mpl@cs.colorado.edu>
faust3
parents:
12609
diff
changeset
|
254 } |
a9429d90157a
avoid using corrupted font descriptions patch by Daniel von Dincklage <danielvd+mpl@cs.colorado.edu>
faust3
parents:
12609
diff
changeset
|
255 |
213 | 256 //printf("font: pos of U = %d\n",desc->start[218]); |
257 | |
258 for(i=0;i<=fontdb;i++){ | |
259 if(!desc->pic_a[i] || !desc->pic_b[i]){ | |
260 printf("font: Missing bitmap(s) for sub-font #%d\n",i); | |
13641 | 261 goto fail_out; |
213 | 262 } |
249 | 263 //if(factor!=1.0f) |
264 { | |
215 | 265 // re-sample alpha |
266 int f=factor*256.0f; | |
267 int size=desc->pic_a[i]->w*desc->pic_a[i]->h; | |
268 int j; | |
339 | 269 if(verbose) printf("font: resampling alpha by factor %5.3f (%d) ",factor,f);fflush(stdout); |
215 | 270 for(j=0;j<size;j++){ |
947
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
271 int x=desc->pic_a[i]->bmp[j]; // alpha |
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
272 int y=desc->pic_b[i]->bmp[j]; // bitmap |
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
273 |
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
274 #ifdef FAST_OSD |
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
275 x=(x<(255-f))?0:1; |
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
276 #else |
249 | 277 |
250 | 278 x=255-((x*f)>>8); // scale |
279 //if(x<0) x=0; else if(x>255) x=255; | |
280 //x^=255; // invert | |
281 | |
249 | 282 if(x+y>255) x=255-y; // to avoid overflows |
283 | |
284 //x=0; | |
285 //x=((x*f*(255-y))>>16); | |
286 //x=((x*f*(255-y))>>16)+y; | |
215 | 287 //x=(x*f)>>8;if(x<y) x=y; |
250 | 288 |
249 | 289 if(x<1) x=1; else |
290 if(x>=252) x=0; | |
947
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
291 #endif |
250 | 292 |
215 | 293 desc->pic_a[i]->bmp[j]=x; |
250 | 294 // desc->pic_b[i]->bmp[j]=0; // hack |
215 | 295 } |
339 | 296 if(verbose) printf("DONE!\n"); |
215 | 297 } |
213 | 298 if(!desc->height) desc->height=desc->pic_a[i]->h; |
299 } | |
300 | |
301 j='_';if(desc->font[j]<0) j='?'; | |
12609
4be019266884
array initialization fix by SungKwanKang <ksquarekr at yahoo.com>
faust3
parents:
10272
diff
changeset
|
302 for(i=0;i<65536;i++) |
213 | 303 if(desc->font[i]<0){ |
304 desc->start[i]=desc->start[j]; | |
305 desc->width[i]=desc->width[j]; | |
306 desc->font[i]=desc->font[j]; | |
307 } | |
308 desc->font[' ']=-1; | |
309 desc->width[' ']=desc->spacewidth; | |
310 | |
340 | 311 printf("Font %s loaded successfully! (%d chars)\n",fname,chardb); |
213 | 312 |
313 return desc; | |
13641 | 314 |
315 fail_out: | |
316 if (f) | |
317 fclose(f); | |
318 if (desc->fpath) | |
319 free(desc->fpath); | |
320 if (desc->name) | |
321 free(desc->name); | |
322 if (desc) | |
323 free(desc); | |
324 return NULL; | |
213 | 325 } |
326 | |
327 #if 0 | |
328 int main(){ | |
329 | |
339 | 330 read_font_desc("high_arpi.desc",1); |
213 | 331 |
332 } | |
333 #endif | |
7122
0dc9cb756b68
freetype 2.0/2.1+ support - disabled by default until bugs fixed
arpi
parents:
5928
diff
changeset
|
334 |