cache2.c: Avoid warnings about discarding volatile
[mplayer.git] / sub / font_load.c
blob7bf777550156790d32d542b20aa1c18f2bcbaeba
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
28 #include "font_load.h"
29 #include "mp_msg.h"
30 #include "libavutil/attributes.h"
32 raw_file* load_raw(char *name,int verbose){
33 int bpp;
34 raw_file* raw=malloc(sizeof(raw_file));
35 unsigned char head[32];
36 FILE *f=fopen(name,"rb");
37 if(!f) goto err_out; // can't open
38 if(fread(head,32,1,f)<1) goto err_out; // too small
39 if(memcmp(head,"mhwanh",6)) goto err_out; // not raw file
40 raw->w=head[8]*256+head[9];
41 raw->h=head[10]*256+head[11];
42 raw->c=head[12]*256+head[13];
43 if(raw->w == 0) // 2 bytes were not enough for the width... read 4 bytes from the end of the header
44 raw->w = ((head[28]*0x100 + head[29])*0x100 + head[30])*0x100 + head[31];
45 if(raw->c>256) goto err_out; // too many colors!?
46 mp_msg(MSGT_OSD, MSGL_DBG2, "RAW: %s %d x %d, %d colors\n",name,raw->w,raw->h,raw->c);
47 if(raw->c){
48 raw->pal=malloc(raw->c*3);
49 fread(raw->pal,3,raw->c,f);
50 bpp=1;
51 } else {
52 raw->pal=NULL;
53 bpp=3;
55 raw->bmp=malloc(raw->h*raw->w*bpp);
56 fread(raw->bmp,raw->h*raw->w*bpp,1,f);
57 fclose(f);
58 return raw;
60 err_out:
61 if (f)
62 fclose(f);
63 free(raw);
64 return NULL;
67 extern int sub_unicode;
69 font_desc_t* read_font_desc(const char* fname,float factor,int verbose){
70 unsigned char sor[1024];
71 unsigned char sor2[1024];
72 font_desc_t *desc;
73 FILE *f = NULL;
74 char *dn;
75 //struct stat fstate;
76 char section[64];
77 int i,j;
78 int chardb=0;
79 int fontdb=-1;
80 int version av_unused;
81 int first=1;
83 desc=malloc(sizeof(font_desc_t));if(!desc) goto fail_out;
84 memset(desc,0,sizeof(font_desc_t));
86 f=fopen(fname,"rt");if(!f){ mp_msg(MSGT_OSD, MSGL_V, "font: can't open file: %s\n",fname); goto fail_out;}
88 i = strlen (fname) - 9;
89 if ((dn = malloc(i+1))){
90 strncpy (dn, fname, i);
91 dn[i]='\0';
94 desc->fpath = dn; // search in the same dir as fonts.desc
96 // desc->fpath=get_path("font/");
97 // if (stat(desc->fpath, &fstate)!=0) desc->fpath=DATADIR"/font";
102 // set up some defaults, and erase table
103 desc->charspace=2;
104 desc->spacewidth=12;
105 desc->height=0;
106 for(i=0;i<65536;i++) desc->start[i]=desc->width[i]=desc->font[i]=-1;
108 section[0]=0;
110 while(fgets(sor,1020,f)){
111 unsigned char* p[8];
112 int pdb=0;
113 unsigned char *s=sor;
114 unsigned char *d=sor2;
115 int ec=' ';
116 int id=0;
117 sor[1020]=0;
119 /* skip files that look like: TTF (0x00, 0x01), PFM (0x00, 0x01), PFB
120 * (0x80, 0x01), PCF (0x01, 0x66), fon ("MZ"), gzipped (0x1f, 0x8b) */
122 if (first) {
123 if (!sor[0] || sor[1] == 1 || (sor[0] == 'M' && sor[1] == 'Z') || (sor[0] == 0x1f && sor[1] == 0x8b) || (sor[0] == 1 && sor[1] == 0x66)) {
124 mp_msg(MSGT_OSD, MSGL_ERR, "%s doesn't look like a bitmap font description, ignoring.\n", fname);
125 goto fail_out;
127 first = 0;
130 p[0]=d;++pdb;
131 while(1){
132 int c=*s++;
133 if(c==0 || c==13 || c==10) break;
134 if(!id){
135 if(c==39 || c==34){ id=c;continue;} // idezojel
136 if(c==';' || c=='#') break;
137 if(c==9) c=' ';
138 if(c==' '){
139 if(ec==' ') continue;
140 *d=0; ++d;
141 p[pdb]=d;++pdb;
142 if(pdb>=8) break;
143 continue;
145 } else {
146 if(id==c){ id=0;continue;} // idezojel
149 *d=c;d++;
150 ec=c;
152 if(d==sor2) continue; // skip empty lines
153 *d=0;
155 // printf("params=%d sor=%s\n",pdb,sor);
156 // for(i=0;i<pdb;i++) printf(" param %d = '%s'\n",i,p[i]);
158 if(pdb==1 && p[0][0]=='['){
159 int len=strlen(p[0]);
160 if(len && len<63 && p[0][len-1]==']'){
161 strcpy(section,p[0]);
162 mp_msg(MSGT_OSD, MSGL_DBG2, "font: Reading section: %s\n",section);
163 if(strcmp(section,"[files]")==0){
164 ++fontdb;
165 if(fontdb>=16){ mp_msg(MSGT_OSD, MSGL_ERR, "font: Too many bitmaps defined.\n");goto fail_out;}
167 continue;
171 if(strcmp(section,"[fpath]")==0){
172 if(pdb==1){
173 free (desc->fpath); // release previously allocated memory
174 desc->fpath=strdup(p[0]);
175 continue;
177 } else
179 #ifdef __AMIGAOS4__
180 #define FONT_PATH_SEP ""
181 #else
182 //! path seperator for font paths, may not be more than one character
183 #define FONT_PATH_SEP "/"
184 #endif
186 if(strcmp(section,"[files]")==0){
187 char *default_dir=MPLAYER_DATADIR FONT_PATH_SEP "font";
188 if(pdb==2 && strcmp(p[0],"alpha")==0){
189 char *cp;
190 if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) goto fail_out;
192 snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
193 desc->fpath,p[1]);
194 if(!((desc->pic_a[fontdb]=load_raw(cp,verbose)))){
195 free(cp);
196 if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2)))
197 goto fail_out;
198 snprintf(cp,strlen(default_dir)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
199 default_dir,p[1]);
200 if (!((desc->pic_a[fontdb]=load_raw(cp,verbose)))){
201 mp_msg(MSGT_OSD, MSGL_ERR, "Can't load font bitmap: %s\n",p[1]);
202 free(cp);
203 goto fail_out;
206 free(cp);
207 continue;
209 if(pdb==2 && strcmp(p[0],"bitmap")==0){
210 char *cp;
211 if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) goto fail_out;
213 snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
214 desc->fpath,p[1]);
215 if(!((desc->pic_b[fontdb]=load_raw(cp,verbose)))){
216 free(cp);
217 if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2)))
218 goto fail_out;
219 snprintf(cp,strlen(default_dir)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
220 default_dir,p[1]);
221 if (!((desc->pic_b[fontdb]=load_raw(cp,verbose)))){
222 mp_msg(MSGT_OSD, MSGL_ERR, "Can't load font bitmap: %s\n",p[1]);
223 free(cp);
224 goto fail_out;
227 free(cp);
228 continue;
230 } else
232 if(strcmp(section,"[info]")==0){
233 if(pdb==2 && strcmp(p[0],"name")==0){
234 desc->name=strdup(p[1]);
235 continue;
237 if(pdb==2 && strcmp(p[0],"descversion")==0){
238 version=atoi(p[1]);
239 continue;
241 if(pdb==2 && strcmp(p[0],"spacewidth")==0){
242 desc->spacewidth=atoi(p[1]);
243 continue;
245 if(pdb==2 && strcmp(p[0],"charspace")==0){
246 desc->charspace=atoi(p[1]);
247 continue;
249 if(pdb==2 && strcmp(p[0],"height")==0){
250 desc->height=atoi(p[1]);
251 continue;
253 } else
255 if(strcmp(section,"[characters]")==0){
256 if(pdb==3){
257 int chr=p[0][0];
258 int start=atoi(p[1]);
259 int end=atoi(p[2]);
260 if(sub_unicode && (chr>=0x80)) chr=(chr<<8)+p[0][1];
261 else if(strlen(p[0])!=1) chr=strtol(p[0],NULL,0);
262 if(end<start) {
263 mp_msg(MSGT_OSD, MSGL_WARN, "error in font desc: end<start for char '%c'\n",chr);
264 } else {
265 desc->start[chr]=start;
266 desc->width[chr]=end-start+1;
267 desc->font[chr]=fontdb;
268 // printf("char %d '%c' start=%d width=%d\n",chr,chr,desc->start[chr],desc->width[chr]);
269 ++chardb;
271 continue;
274 mp_msg(MSGT_OSD, MSGL_ERR, "Syntax error in font desc: %s",sor);
275 goto fail_out;
278 fclose(f);
279 f = NULL;
281 if (first == 1) {
282 mp_msg(MSGT_OSD, MSGL_ERR, "%s is empty or a directory, ignoring.\n", fname);
283 goto fail_out;
286 //printf("font: pos of U = %d\n",desc->start[218]);
288 for(i=0;i<=fontdb;i++){
289 if(!desc->pic_a[i] || !desc->pic_b[i]){
290 mp_msg(MSGT_OSD, MSGL_ERR, "font: Missing bitmap(s) for sub-font #%d\n",i);
291 goto fail_out;
293 //if(factor!=1.0f)
295 // re-sample alpha
296 int f=factor*256.0f;
297 int size=desc->pic_a[i]->w*desc->pic_a[i]->h;
298 int j;
299 mp_msg(MSGT_OSD, MSGL_DBG2, "font: resampling alpha by factor %5.3f (%d) ",factor,f);fflush(stdout);
300 for(j=0;j<size;j++){
301 int x=desc->pic_a[i]->bmp[j]; // alpha
302 int y=desc->pic_b[i]->bmp[j]; // bitmap
304 #ifdef FAST_OSD
305 x=(x<(255-f))?0:1;
306 #else
308 x=255-((x*f)>>8); // scale
309 //if(x<0) x=0; else if(x>255) x=255;
310 //x^=255; // invert
312 if(x+y>255) x=255-y; // to avoid overflows
314 //x=0;
315 //x=((x*f*(255-y))>>16);
316 //x=((x*f*(255-y))>>16)+y;
317 //x=(x*f)>>8;if(x<y) x=y;
319 if(x<1) x=1; else
320 if(x>=252) x=0;
321 #endif
323 desc->pic_a[i]->bmp[j]=x;
324 // desc->pic_b[i]->bmp[j]=0; // hack
326 mp_msg(MSGT_OSD, MSGL_DBG2, "DONE!\n");
328 if(!desc->height) desc->height=desc->pic_a[i]->h;
331 j='_';if(desc->font[j]<0) j='?';
332 for(i=0;i<65536;i++)
333 if(desc->font[i]<0){
334 desc->start[i]=desc->start[j];
335 desc->width[i]=desc->width[j];
336 desc->font[i]=desc->font[j];
338 desc->font[' ']=-1;
339 desc->width[' ']=desc->spacewidth;
341 mp_msg(MSGT_OSD, MSGL_V, "Bitmap font %s loaded successfully! (%d chars)\n",fname,chardb);
343 return desc;
345 fail_out:
346 if (f)
347 fclose(f);
348 free(desc->fpath);
349 free(desc->name);
350 free(desc);
351 return NULL;
354 #ifndef CONFIG_FREETYPE
355 void render_one_glyph(font_desc_t *desc, int c) {}
356 int kerning(font_desc_t *desc, int prevc, int c) { return 0; }
357 #endif