Mercurial > mplayer.hg
annotate libvo/font_load.c @ 580:dd0a2985304f
little fix, just to be precise
author | arpi_esp |
---|---|
date | Sun, 22 Apr 2001 17:07:57 +0000 |
parents | 1f7c824033fb |
children | 8a7666a78f83 |
rev | line source |
---|---|
213 | 1 |
2 #include <stdio.h> | |
3 #include <stdlib.h> | |
4 #include <string.h> | |
5 | |
6 #include "font_load.h" | |
7 | |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
8 char *get_path ( char * ); |
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
9 |
339 | 10 raw_file* load_raw(char *name,int verbose){ |
213 | 11 int bpp; |
12 raw_file* raw=malloc(sizeof(raw_file)); | |
13 unsigned char head[32]; | |
14 FILE *f=fopen(name,"rb"); | |
15 if(!f) return NULL; // can't open | |
16 if(fread(head,32,1,f)<1) return NULL; // too small | |
17 if(memcmp(head,"mhwanh",6)) return NULL; // not raw file | |
18 raw->w=head[8]*256+head[9]; | |
19 raw->h=head[10]*256+head[11]; | |
20 raw->c=head[12]*256+head[13]; | |
21 if(raw->c>256) return NULL; // too many colors!? | |
339 | 22 if(verbose) printf("RAW: %s %d x %d, %d colors\n",name,raw->w,raw->h,raw->c); |
213 | 23 if(raw->c){ |
24 raw->pal=malloc(raw->c*3); | |
25 fread(raw->pal,3,raw->c,f); | |
26 bpp=1; | |
27 } else { | |
28 raw->pal=NULL; | |
29 bpp=3; | |
30 } | |
31 raw->bmp=malloc(raw->h*raw->w*bpp); | |
32 fread(raw->bmp,raw->h*raw->w*bpp,1,f); | |
33 fclose(f); | |
34 return raw; | |
35 } | |
36 | |
339 | 37 font_desc_t* read_font_desc(char* fname,float factor,int verbose){ |
213 | 38 unsigned char sor[1024]; |
39 unsigned char sor2[1024]; | |
40 font_desc_t *desc; | |
41 FILE *f; | |
42 char section[64]; | |
43 int i,j; | |
44 int chardb=0; | |
45 int fontdb=-1; | |
214 | 46 int version=0; |
213 | 47 |
48 desc=malloc(sizeof(font_desc_t));if(!desc) return NULL; | |
49 memset(desc,0,sizeof(font_desc_t)); | |
50 | |
51 f=fopen(fname,"rt");if(!f){ printf("font: can't open file: %s\n",fname); return NULL;} | |
52 | |
53 // set up some defaults, and erase table | |
54 desc->charspace=2; | |
55 desc->spacewidth=12; | |
56 desc->height=0; | |
57 for(i=0;i<512;i++) desc->start[i]=desc->width[i]=desc->font[i]=-1; | |
58 | |
59 section[0]=0; | |
60 | |
61 while(fgets(sor,1020,f)){ | |
62 unsigned char* p[8]; | |
63 int pdb=0; | |
64 unsigned char *s=sor; | |
65 unsigned char *d=sor2; | |
66 int ec=' '; | |
67 int id=0; | |
68 sor[1020]=0; | |
69 p[0]=d;++pdb; | |
70 while(1){ | |
71 int c=*s++; | |
72 if(c==0 || c==13 || c==10) break; | |
73 if(!id){ | |
74 if(c==39 || c==34){ id=c;continue;} // idezojel | |
75 if(c==';' || c=='#') break; | |
76 if(c==9) c=' '; | |
77 if(c==' '){ | |
78 if(ec==' ') continue; | |
79 *d=0; ++d; | |
80 p[pdb]=d;++pdb; | |
81 if(pdb>=8) break; | |
82 continue; | |
83 } | |
84 } else { | |
85 if(id==c){ id=0;continue;} // idezojel | |
86 | |
87 } | |
88 *d=c;d++; | |
89 ec=c; | |
90 } | |
91 if(d==sor2) continue; // skip empty lines | |
92 *d=0; | |
93 | |
94 // printf("params=%d sor=%s\n",pdb,sor); | |
95 // for(i=0;i<pdb;i++) printf(" param %d = '%s'\n",i,p[i]); | |
96 | |
97 if(pdb==1 && p[0][0]=='['){ | |
98 int len=strlen(p[0]); | |
99 if(len && len<63 && p[0][len-1]==']'){ | |
100 strcpy(section,p[0]); | |
339 | 101 if(verbose) printf("font: Reading section: %s\n",section); |
213 | 102 if(strcmp(section,"[files]")==0){ |
103 ++fontdb; | |
104 if(fontdb>=16){ printf("font: Too many bitmaps defined!\n");return NULL;} | |
105 } | |
106 continue; | |
107 } | |
108 } | |
109 | |
110 if(strcmp(section,"[files]")==0){ | |
111 if(pdb==2 && strcmp(p[0],"alpha")==0){ | |
217
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
112 char *cp,*cp2; |
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
113 if (!(cp=get_path("font/"))) return NULL; |
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
114 if (!(cp2=malloc(strlen(cp)+strlen(p[1])+1))) { |
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
115 free(cp); |
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
116 return NULL; |
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
117 } |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
118 sprintf(cp2,"%s%s",cp,p[1]); |
339 | 119 if(!((desc->pic_a[fontdb]=load_raw(cp2,verbose)))){ |
213 | 120 printf("Can't load font bitmap: %s\n",p[1]); |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
121 free(cp); |
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
122 free(cp2); |
213 | 123 return NULL; |
124 } | |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
125 free(cp); |
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
126 free(cp2); |
213 | 127 continue; |
128 } | |
129 if(pdb==2 && strcmp(p[0],"bitmap")==0){ | |
217
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
130 char *cp,*cp2; |
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
131 if (!(cp=get_path("font/"))) return NULL; |
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
132 if (!(cp2=malloc(strlen(cp)+strlen(p[1])+1))) { |
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
133 free(cp); |
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
134 return NULL; |
cb3b0d65dddd
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
216
diff
changeset
|
135 } |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
136 sprintf(cp2,"%s%s",cp,p[1]); |
339 | 137 if(!((desc->pic_b[fontdb]=load_raw(cp2,verbose)))){ |
213 | 138 printf("Can't load font bitmap: %s\n",p[1]); |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
139 free(cp); |
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
140 free(cp2); |
213 | 141 return NULL; |
142 } | |
216
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
143 free(cp); |
338b5664ea13
Search font files in ~/.mplayer/font/ instead of current dir
lgb
parents:
215
diff
changeset
|
144 free(cp2); |
213 | 145 continue; |
146 } | |
147 } else | |
148 | |
149 if(strcmp(section,"[info]")==0){ | |
214 | 150 if(pdb==2 && strcmp(p[0],"name")==0){ |
151 desc->name=strdup(p[1]); | |
152 continue; | |
153 } | |
154 if(pdb==2 && strcmp(p[0],"descversion")==0){ | |
155 version=atoi(p[1]); | |
156 continue; | |
157 } | |
213 | 158 if(pdb==2 && strcmp(p[0],"spacewidth")==0){ |
159 desc->spacewidth=atoi(p[1]); | |
160 continue; | |
161 } | |
162 if(pdb==2 && strcmp(p[0],"charspace")==0){ | |
163 desc->charspace=atoi(p[1]); | |
164 continue; | |
165 } | |
166 if(pdb==2 && strcmp(p[0],"height")==0){ | |
167 desc->height=atoi(p[1]); | |
168 continue; | |
169 } | |
170 } else | |
214 | 171 |
213 | 172 if(strcmp(section,"[characters]")==0){ |
219 | 173 if(pdb==3){ |
213 | 174 int chr=p[0][0]; |
175 int start=atoi(p[1]); | |
176 int end=atoi(p[2]); | |
219 | 177 if(strlen(p[0])!=1) chr=strtol(p[0],NULL,0); |
213 | 178 if(end<start) { |
179 printf("error in font desc: end<start for char '%c'\n",chr); | |
180 } else { | |
181 desc->start[chr]=start; | |
182 desc->width[chr]=end-start+1; | |
183 desc->font[chr]=fontdb; | |
184 // printf("char %d '%c' start=%d width=%d\n",chr,chr,desc->start[chr],desc->width[chr]); | |
185 ++chardb; | |
186 } | |
187 continue; | |
188 } | |
189 } | |
190 printf("Syntax error in font desc: %s\n",sor); | |
191 | |
192 } | |
193 fclose(f); | |
194 | |
195 //printf("font: pos of U = %d\n",desc->start[218]); | |
196 | |
197 for(i=0;i<=fontdb;i++){ | |
198 if(!desc->pic_a[i] || !desc->pic_b[i]){ | |
199 printf("font: Missing bitmap(s) for sub-font #%d\n",i); | |
200 return NULL; | |
201 } | |
249 | 202 //if(factor!=1.0f) |
203 { | |
215 | 204 // re-sample alpha |
205 int f=factor*256.0f; | |
206 int size=desc->pic_a[i]->w*desc->pic_a[i]->h; | |
207 int j; | |
339 | 208 if(verbose) printf("font: resampling alpha by factor %5.3f (%d) ",factor,f);fflush(stdout); |
215 | 209 for(j=0;j<size;j++){ |
210 int x=desc->pic_a[i]->bmp[j]; | |
211 int y=desc->pic_b[i]->bmp[j]; | |
249 | 212 |
250 | 213 x=255-((x*f)>>8); // scale |
214 //if(x<0) x=0; else if(x>255) x=255; | |
215 //x^=255; // invert | |
216 | |
249 | 217 if(x+y>255) x=255-y; // to avoid overflows |
218 | |
219 //x=0; | |
220 //x=((x*f*(255-y))>>16); | |
221 //x=((x*f*(255-y))>>16)+y; | |
215 | 222 //x=(x*f)>>8;if(x<y) x=y; |
250 | 223 |
249 | 224 if(x<1) x=1; else |
225 if(x>=252) x=0; | |
250 | 226 |
215 | 227 desc->pic_a[i]->bmp[j]=x; |
250 | 228 // desc->pic_b[i]->bmp[j]=0; // hack |
215 | 229 } |
339 | 230 if(verbose) printf("DONE!\n"); |
215 | 231 } |
213 | 232 if(!desc->height) desc->height=desc->pic_a[i]->h; |
233 } | |
234 | |
235 j='_';if(desc->font[j]<0) j='?'; | |
236 for(i=0;i<512;i++) | |
237 if(desc->font[i]<0){ | |
238 desc->start[i]=desc->start[j]; | |
239 desc->width[i]=desc->width[j]; | |
240 desc->font[i]=desc->font[j]; | |
241 } | |
242 desc->font[' ']=-1; | |
243 desc->width[' ']=desc->spacewidth; | |
244 | |
340 | 245 printf("Font %s loaded successfully! (%d chars)\n",fname,chardb); |
213 | 246 |
247 return desc; | |
248 } | |
249 | |
250 #if 0 | |
251 int main(){ | |
252 | |
339 | 253 read_font_desc("high_arpi.desc",1); |
213 | 254 |
255 } | |
256 #endif |