Mercurial > mplayer.hg
annotate libvo/font_load.c @ 7070:aaac9080b8a3
v4l capture update by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
some silly bugs slipped through.. here is the fix
author | arpi |
---|---|
date | Thu, 22 Aug 2002 23:05:58 +0000 |
parents | 48e91dc9534b |
children | 0dc9cb756b68 |
rev | line source |
---|---|
213 | 1 |
2 #include <stdio.h> | |
3 #include <stdlib.h> | |
4 #include <string.h> | |
1446 | 5 #include <sys/types.h> |
6 #include <sys/stat.h> | |
7 #include <unistd.h> | |
213 | 8 |
947
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
9 #include "config.h" |
213 | 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; |
213 | 57 |
58 desc=malloc(sizeof(font_desc_t));if(!desc) return NULL; | |
59 memset(desc,0,sizeof(font_desc_t)); | |
60 | |
61 f=fopen(fname,"rt");if(!f){ printf("font: can't open file: %s\n",fname); return NULL;} | |
62 | |
2222
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
63 i = strlen (fname) - 9; |
2223 | 64 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
|
65 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
|
66 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
|
67 } |
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 desc->fpath = dn; // search in the same dir as fonts.desc |
1446 | 70 |
2222
ddf897c38fb1
read font files from the same dir as font.desc or as specified in font.desc
atlka
parents:
1446
diff
changeset
|
71 // 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
|
72 // 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
|
73 |
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 |
1446 | 76 |
213 | 77 // set up some defaults, and erase table |
78 desc->charspace=2; | |
79 desc->spacewidth=12; | |
80 desc->height=0; | |
81 for(i=0;i<512;i++) desc->start[i]=desc->width[i]=desc->font[i]=-1; | |
82 | |
83 section[0]=0; | |
84 | |
85 while(fgets(sor,1020,f)){ | |
86 unsigned char* p[8]; | |
87 int pdb=0; | |
88 unsigned char *s=sor; | |
89 unsigned char *d=sor2; | |
90 int ec=' '; | |
91 int id=0; | |
92 sor[1020]=0; | |
93 p[0]=d;++pdb; | |
94 while(1){ | |
95 int c=*s++; | |
96 if(c==0 || c==13 || c==10) break; | |
97 if(!id){ | |
98 if(c==39 || c==34){ id=c;continue;} // idezojel | |
99 if(c==';' || c=='#') break; | |
100 if(c==9) c=' '; | |
101 if(c==' '){ | |
102 if(ec==' ') continue; | |
103 *d=0; ++d; | |
104 p[pdb]=d;++pdb; | |
105 if(pdb>=8) break; | |
106 continue; | |
107 } | |
108 } else { | |
109 if(id==c){ id=0;continue;} // idezojel | |
110 | |
111 } | |
112 *d=c;d++; | |
113 ec=c; | |
114 } | |
115 if(d==sor2) continue; // skip empty lines | |
116 *d=0; | |
117 | |
118 // printf("params=%d sor=%s\n",pdb,sor); | |
119 // for(i=0;i<pdb;i++) printf(" param %d = '%s'\n",i,p[i]); | |
120 | |
121 if(pdb==1 && p[0][0]=='['){ | |
122 int len=strlen(p[0]); | |
123 if(len && len<63 && p[0][len-1]==']'){ | |
124 strcpy(section,p[0]); | |
339 | 125 if(verbose) printf("font: Reading section: %s\n",section); |
213 | 126 if(strcmp(section,"[files]")==0){ |
127 ++fontdb; | |
128 if(fontdb>=16){ printf("font: Too many bitmaps defined!\n");return NULL;} | |
129 } | |
130 continue; | |
131 } | |
132 } | |
133 | |
1353 | 134 if(strcmp(section,"[fpath]")==0){ |
135 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
|
136 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
|
137 free (desc->fpath); // release previously allocated memory |
1353 | 138 desc->fpath=strdup(p[0]); |
139 continue; | |
140 } | |
141 } else | |
142 | |
213 | 143 if(strcmp(section,"[files]")==0){ |
2238
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
144 char *default_dir=DATADIR"/font"; |
213 | 145 if(pdb==2 && strcmp(p[0],"alpha")==0){ |
1353 | 146 char *cp; |
147 if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) return NULL; | |
148 | |
149 snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s/%s", | |
150 desc->fpath,p[1]); | |
151 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
|
152 free(cp); |
2238
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
153 if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2))) |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
154 return NULL; |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
155 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
|
156 default_dir,p[1]); |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
157 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
|
158 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
|
159 free(cp); |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
160 return NULL; |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
161 } |
213 | 162 } |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
163 free(cp); |
213 | 164 continue; |
165 } | |
166 if(pdb==2 && strcmp(p[0],"bitmap")==0){ | |
1353 | 167 char *cp; |
168 if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) return NULL; | |
169 | |
170 snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s/%s", | |
171 desc->fpath,p[1]); | |
172 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
|
173 free(cp); |
2238
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
174 if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2))) |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
175 return NULL; |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
176 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
|
177 default_dir,p[1]); |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
178 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
|
179 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
|
180 free(cp); |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
181 return NULL; |
be4160d7db48
if not found using fpath try to load font bitmaps from default dir
atlka
parents:
2223
diff
changeset
|
182 } |
213 | 183 } |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
184 free(cp); |
213 | 185 continue; |
186 } | |
187 } else | |
188 | |
189 if(strcmp(section,"[info]")==0){ | |
214 | 190 if(pdb==2 && strcmp(p[0],"name")==0){ |
191 desc->name=strdup(p[1]); | |
192 continue; | |
193 } | |
194 if(pdb==2 && strcmp(p[0],"descversion")==0){ | |
195 version=atoi(p[1]); | |
196 continue; | |
197 } | |
213 | 198 if(pdb==2 && strcmp(p[0],"spacewidth")==0){ |
199 desc->spacewidth=atoi(p[1]); | |
200 continue; | |
201 } | |
202 if(pdb==2 && strcmp(p[0],"charspace")==0){ | |
203 desc->charspace=atoi(p[1]); | |
204 continue; | |
205 } | |
206 if(pdb==2 && strcmp(p[0],"height")==0){ | |
207 desc->height=atoi(p[1]); | |
208 continue; | |
209 } | |
210 } else | |
214 | 211 |
213 | 212 if(strcmp(section,"[characters]")==0){ |
219 | 213 if(pdb==3){ |
213 | 214 int chr=p[0][0]; |
215 int start=atoi(p[1]); | |
216 int end=atoi(p[2]); | |
727 | 217 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
|
218 else if(strlen(p[0])!=1) chr=strtol(p[0],NULL,0); |
213 | 219 if(end<start) { |
220 printf("error in font desc: end<start for char '%c'\n",chr); | |
221 } else { | |
222 desc->start[chr]=start; | |
223 desc->width[chr]=end-start+1; | |
224 desc->font[chr]=fontdb; | |
225 // printf("char %d '%c' start=%d width=%d\n",chr,chr,desc->start[chr],desc->width[chr]); | |
226 ++chardb; | |
227 } | |
228 continue; | |
229 } | |
230 } | |
231 printf("Syntax error in font desc: %s\n",sor); | |
232 | |
233 } | |
234 fclose(f); | |
235 | |
236 //printf("font: pos of U = %d\n",desc->start[218]); | |
237 | |
238 for(i=0;i<=fontdb;i++){ | |
239 if(!desc->pic_a[i] || !desc->pic_b[i]){ | |
240 printf("font: Missing bitmap(s) for sub-font #%d\n",i); | |
241 return NULL; | |
242 } | |
249 | 243 //if(factor!=1.0f) |
244 { | |
215 | 245 // re-sample alpha |
246 int f=factor*256.0f; | |
247 int size=desc->pic_a[i]->w*desc->pic_a[i]->h; | |
248 int j; | |
339 | 249 if(verbose) printf("font: resampling alpha by factor %5.3f (%d) ",factor,f);fflush(stdout); |
215 | 250 for(j=0;j<size;j++){ |
947
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
251 int x=desc->pic_a[i]->bmp[j]; // alpha |
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
252 int y=desc->pic_b[i]->bmp[j]; // bitmap |
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
253 |
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
254 #ifdef FAST_OSD |
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
255 x=(x<(255-f))?0:1; |
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
256 #else |
249 | 257 |
250 | 258 x=255-((x*f)>>8); // scale |
259 //if(x<0) x=0; else if(x>255) x=255; | |
260 //x^=255; // invert | |
261 | |
249 | 262 if(x+y>255) x=255-y; // to avoid overflows |
263 | |
264 //x=0; | |
265 //x=((x*f*(255-y))>>16); | |
266 //x=((x*f*(255-y))>>16)+y; | |
215 | 267 //x=(x*f)>>8;if(x<y) x=y; |
250 | 268 |
249 | 269 if(x<1) x=1; else |
270 if(x>=252) x=0; | |
947
76fd9463b9d3
FAST_OSD option to disable font outline antialiasing
arpi_esp
parents:
728
diff
changeset
|
271 #endif |
250 | 272 |
215 | 273 desc->pic_a[i]->bmp[j]=x; |
250 | 274 // desc->pic_b[i]->bmp[j]=0; // hack |
215 | 275 } |
339 | 276 if(verbose) printf("DONE!\n"); |
215 | 277 } |
213 | 278 if(!desc->height) desc->height=desc->pic_a[i]->h; |
279 } | |
280 | |
281 j='_';if(desc->font[j]<0) j='?'; | |
282 for(i=0;i<512;i++) | |
283 if(desc->font[i]<0){ | |
284 desc->start[i]=desc->start[j]; | |
285 desc->width[i]=desc->width[j]; | |
286 desc->font[i]=desc->font[j]; | |
287 } | |
288 desc->font[' ']=-1; | |
289 desc->width[' ']=desc->spacewidth; | |
290 | |
340 | 291 printf("Font %s loaded successfully! (%d chars)\n",fname,chardb); |
213 | 292 |
293 return desc; | |
294 } | |
295 | |
296 #if 0 | |
297 int main(){ | |
298 | |
339 | 299 read_font_desc("high_arpi.desc",1); |
213 | 300 |
301 } | |
302 #endif |