2002-08-28 20:52:02 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-03-25 04:28:40 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-08-06 00:22:13 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2001-03-25 04:28:40 +00:00
|
|
|
|
|
|
|
#include "font_load.h"
|
2004-11-08 00:06:49 +00:00
|
|
|
#include "mp_msg.h"
|
2001-03-25 04:28:40 +00:00
|
|
|
|
2001-10-25 23:34:14 +00:00
|
|
|
extern char *get_path ( char * );
|
2001-03-26 17:16:27 +00:00
|
|
|
|
2001-04-11 00:47:05 +00:00
|
|
|
raw_file* load_raw(char *name,int verbose){
|
2001-03-25 04:28:40 +00:00
|
|
|
int bpp;
|
|
|
|
raw_file* raw=malloc(sizeof(raw_file));
|
|
|
|
unsigned char head[32];
|
|
|
|
FILE *f=fopen(name,"rb");
|
2006-03-09 22:20:20 +00:00
|
|
|
if(!f) goto err_out; // can't open
|
|
|
|
if(fread(head,32,1,f)<1) goto err_out; // too small
|
|
|
|
if(memcmp(head,"mhwanh",6)) goto err_out; // not raw file
|
2001-03-25 04:28:40 +00:00
|
|
|
raw->w=head[8]*256+head[9];
|
|
|
|
raw->h=head[10]*256+head[11];
|
|
|
|
raw->c=head[12]*256+head[13];
|
2002-05-02 01:56:28 +00:00
|
|
|
if(raw->w == 0) // 2 bytes were not enough for the width... read 4 bytes from the end of the header
|
|
|
|
raw->w = ((head[28]*0x100 + head[29])*0x100 + head[30])*0x100 + head[31];
|
2006-03-09 22:20:20 +00:00
|
|
|
if(raw->c>256) goto err_out; // too many colors!?
|
2004-11-08 00:06:49 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_DBG2, "RAW: %s %d x %d, %d colors\n",name,raw->w,raw->h,raw->c);
|
2001-03-25 04:28:40 +00:00
|
|
|
if(raw->c){
|
|
|
|
raw->pal=malloc(raw->c*3);
|
|
|
|
fread(raw->pal,3,raw->c,f);
|
|
|
|
bpp=1;
|
|
|
|
} else {
|
|
|
|
raw->pal=NULL;
|
|
|
|
bpp=3;
|
|
|
|
}
|
|
|
|
raw->bmp=malloc(raw->h*raw->w*bpp);
|
|
|
|
fread(raw->bmp,raw->h*raw->w*bpp,1,f);
|
|
|
|
fclose(f);
|
|
|
|
return raw;
|
2006-03-09 22:20:20 +00:00
|
|
|
|
|
|
|
err_out:
|
|
|
|
if (f)
|
|
|
|
fclose(f);
|
|
|
|
free(raw);
|
|
|
|
return NULL;
|
2001-03-25 04:28:40 +00:00
|
|
|
}
|
|
|
|
|
2001-05-08 20:09:39 +00:00
|
|
|
extern int sub_unicode;
|
|
|
|
|
2006-07-09 17:45:36 +00:00
|
|
|
font_desc_t* read_font_desc(const char* fname,float factor,int verbose){
|
2001-03-25 04:28:40 +00:00
|
|
|
unsigned char sor[1024];
|
|
|
|
unsigned char sor2[1024];
|
|
|
|
font_desc_t *desc;
|
2006-03-08 10:04:05 +00:00
|
|
|
FILE *f = NULL;
|
2001-10-16 10:14:04 +00:00
|
|
|
char *dn;
|
2001-08-06 00:22:13 +00:00
|
|
|
struct stat fstate;
|
2001-03-25 04:28:40 +00:00
|
|
|
char section[64];
|
|
|
|
int i,j;
|
|
|
|
int chardb=0;
|
|
|
|
int fontdb=-1;
|
2001-03-25 19:10:52 +00:00
|
|
|
int version=0;
|
2002-12-29 14:59:23 +00:00
|
|
|
int first=1;
|
2001-03-25 04:28:40 +00:00
|
|
|
|
2004-10-14 15:09:35 +00:00
|
|
|
desc=malloc(sizeof(font_desc_t));if(!desc) goto fail_out;
|
2001-03-25 04:28:40 +00:00
|
|
|
memset(desc,0,sizeof(font_desc_t));
|
|
|
|
|
2004-11-30 02:36:05 +00:00
|
|
|
f=fopen(fname,"rt");if(!f){ mp_msg(MSGT_OSD, MSGL_V, "font: can't open file: %s\n",fname); goto fail_out;}
|
2001-03-25 04:28:40 +00:00
|
|
|
|
2001-10-16 10:14:04 +00:00
|
|
|
i = strlen (fname) - 9;
|
2001-10-16 12:50:59 +00:00
|
|
|
if ((dn = malloc(i+1))){
|
2001-10-16 10:14:04 +00:00
|
|
|
strncpy (dn, fname, i);
|
|
|
|
dn[i]='\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
desc->fpath = dn; // search in the same dir as fonts.desc
|
|
|
|
|
|
|
|
// desc->fpath=get_path("font/");
|
|
|
|
// if (stat(desc->fpath, &fstate)!=0) desc->fpath=DATADIR"/font";
|
|
|
|
|
|
|
|
|
2001-08-06 00:22:13 +00:00
|
|
|
|
|
|
|
|
2001-03-25 04:28:40 +00:00
|
|
|
// set up some defaults, and erase table
|
|
|
|
desc->charspace=2;
|
|
|
|
desc->spacewidth=12;
|
|
|
|
desc->height=0;
|
2004-06-18 13:51:45 +00:00
|
|
|
for(i=0;i<65536;i++) desc->start[i]=desc->width[i]=desc->font[i]=-1;
|
2001-03-25 04:28:40 +00:00
|
|
|
|
|
|
|
section[0]=0;
|
|
|
|
|
|
|
|
while(fgets(sor,1020,f)){
|
|
|
|
unsigned char* p[8];
|
|
|
|
int pdb=0;
|
|
|
|
unsigned char *s=sor;
|
|
|
|
unsigned char *d=sor2;
|
|
|
|
int ec=' ';
|
|
|
|
int id=0;
|
|
|
|
sor[1020]=0;
|
2002-12-29 14:59:23 +00:00
|
|
|
|
|
|
|
/* skip files that look like: TTF (0x00, 0x01), PFM (0x00, 0x01), PFB
|
|
|
|
* (0x80, 0x01), PCF (0x01, 0x66), fon ("MZ"), gzipped (0x1f, 0x8b) */
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
if (!sor[0] || sor[1] == 1 || (sor[0] == 'M' && sor[1] == 'Z') || (sor[0] == 0x1f && sor[1] == 0x8b) || (sor[0] == 1 && sor[1] == 0x66)) {
|
2006-08-27 15:33:11 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_ERR, "%s doesn't look like a bitmap font description, ignoring.\n", fname);
|
2004-10-14 15:09:35 +00:00
|
|
|
goto fail_out;
|
2002-12-29 14:59:23 +00:00
|
|
|
}
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
|
2001-03-25 04:28:40 +00:00
|
|
|
p[0]=d;++pdb;
|
|
|
|
while(1){
|
|
|
|
int c=*s++;
|
|
|
|
if(c==0 || c==13 || c==10) break;
|
|
|
|
if(!id){
|
|
|
|
if(c==39 || c==34){ id=c;continue;} // idezojel
|
|
|
|
if(c==';' || c=='#') break;
|
|
|
|
if(c==9) c=' ';
|
|
|
|
if(c==' '){
|
|
|
|
if(ec==' ') continue;
|
|
|
|
*d=0; ++d;
|
|
|
|
p[pdb]=d;++pdb;
|
|
|
|
if(pdb>=8) break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(id==c){ id=0;continue;} // idezojel
|
|
|
|
|
|
|
|
}
|
|
|
|
*d=c;d++;
|
|
|
|
ec=c;
|
|
|
|
}
|
|
|
|
if(d==sor2) continue; // skip empty lines
|
|
|
|
*d=0;
|
|
|
|
|
|
|
|
// printf("params=%d sor=%s\n",pdb,sor);
|
|
|
|
// for(i=0;i<pdb;i++) printf(" param %d = '%s'\n",i,p[i]);
|
|
|
|
|
|
|
|
if(pdb==1 && p[0][0]=='['){
|
|
|
|
int len=strlen(p[0]);
|
|
|
|
if(len && len<63 && p[0][len-1]==']'){
|
|
|
|
strcpy(section,p[0]);
|
2004-11-08 00:06:49 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_DBG2, "font: Reading section: %s\n",section);
|
2001-03-25 04:28:40 +00:00
|
|
|
if(strcmp(section,"[files]")==0){
|
|
|
|
++fontdb;
|
2004-11-08 00:06:49 +00:00
|
|
|
if(fontdb>=16){ mp_msg(MSGT_OSD, MSGL_ERR, "font: Too many bitmaps defined.\n");goto fail_out;}
|
2001-03-25 04:28:40 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-20 00:01:09 +00:00
|
|
|
if(strcmp(section,"[fpath]")==0){
|
|
|
|
if(pdb==1){
|
2001-10-16 10:14:04 +00:00
|
|
|
if (desc->fpath)
|
|
|
|
free (desc->fpath); // release previously allocated memory
|
2001-07-20 00:01:09 +00:00
|
|
|
desc->fpath=strdup(p[0]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
|
2006-08-14 14:13:53 +00:00
|
|
|
#ifdef SYS_AMIGAOS4
|
|
|
|
#define FONT_PATH_SEP ""
|
|
|
|
#else
|
|
|
|
//! path seperator for font paths, may not be more than one character
|
|
|
|
#define FONT_PATH_SEP "/"
|
|
|
|
#endif
|
|
|
|
|
2001-03-25 04:28:40 +00:00
|
|
|
if(strcmp(section,"[files]")==0){
|
2006-08-14 14:13:53 +00:00
|
|
|
char *default_dir=MPLAYER_DATADIR FONT_PATH_SEP "font";
|
2001-03-25 04:28:40 +00:00
|
|
|
if(pdb==2 && strcmp(p[0],"alpha")==0){
|
2001-07-20 00:01:09 +00:00
|
|
|
char *cp;
|
2004-10-14 15:09:35 +00:00
|
|
|
if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) goto fail_out;
|
2001-07-20 00:01:09 +00:00
|
|
|
|
2006-08-14 14:13:53 +00:00
|
|
|
snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
|
2001-07-20 00:01:09 +00:00
|
|
|
desc->fpath,p[1]);
|
|
|
|
if(!((desc->pic_a[fontdb]=load_raw(cp,verbose)))){
|
2001-03-26 17:16:27 +00:00
|
|
|
free(cp);
|
2001-10-17 15:15:30 +00:00
|
|
|
if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2)))
|
2004-10-14 15:09:35 +00:00
|
|
|
goto fail_out;
|
2006-08-14 14:13:53 +00:00
|
|
|
snprintf(cp,strlen(default_dir)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
|
2001-10-17 15:15:30 +00:00
|
|
|
default_dir,p[1]);
|
|
|
|
if (!((desc->pic_a[fontdb]=load_raw(cp,verbose)))){
|
2004-11-08 00:06:49 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_ERR, "Can't load font bitmap: %s\n",p[1]);
|
2001-10-17 15:15:30 +00:00
|
|
|
free(cp);
|
2004-10-14 15:09:35 +00:00
|
|
|
goto fail_out;
|
2001-10-17 15:15:30 +00:00
|
|
|
}
|
2001-03-25 04:28:40 +00:00
|
|
|
}
|
2001-03-26 17:16:27 +00:00
|
|
|
free(cp);
|
2001-03-25 04:28:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(pdb==2 && strcmp(p[0],"bitmap")==0){
|
2001-07-20 00:01:09 +00:00
|
|
|
char *cp;
|
2004-10-14 15:09:35 +00:00
|
|
|
if (!(cp=malloc(strlen(desc->fpath)+strlen(p[1])+2))) goto fail_out;
|
2001-07-20 00:01:09 +00:00
|
|
|
|
2006-08-14 14:13:53 +00:00
|
|
|
snprintf(cp,strlen(desc->fpath)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
|
2001-07-20 00:01:09 +00:00
|
|
|
desc->fpath,p[1]);
|
|
|
|
if(!((desc->pic_b[fontdb]=load_raw(cp,verbose)))){
|
2001-03-26 17:16:27 +00:00
|
|
|
free(cp);
|
2001-10-17 15:15:30 +00:00
|
|
|
if (!(cp=malloc(strlen(default_dir)+strlen(p[1])+2)))
|
2004-10-14 15:09:35 +00:00
|
|
|
goto fail_out;
|
2006-08-14 14:13:53 +00:00
|
|
|
snprintf(cp,strlen(default_dir)+strlen(p[1])+2,"%s" FONT_PATH_SEP "%s",
|
2001-10-17 15:15:30 +00:00
|
|
|
default_dir,p[1]);
|
|
|
|
if (!((desc->pic_b[fontdb]=load_raw(cp,verbose)))){
|
2004-11-08 00:06:49 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_ERR, "Can't load font bitmap: %s\n",p[1]);
|
2001-10-17 15:15:30 +00:00
|
|
|
free(cp);
|
2004-10-14 15:09:35 +00:00
|
|
|
goto fail_out;
|
2001-10-17 15:15:30 +00:00
|
|
|
}
|
2001-03-25 04:28:40 +00:00
|
|
|
}
|
2001-03-26 17:16:27 +00:00
|
|
|
free(cp);
|
2001-03-25 04:28:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
|
|
|
|
if(strcmp(section,"[info]")==0){
|
2001-03-25 19:10:52 +00:00
|
|
|
if(pdb==2 && strcmp(p[0],"name")==0){
|
|
|
|
desc->name=strdup(p[1]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(pdb==2 && strcmp(p[0],"descversion")==0){
|
|
|
|
version=atoi(p[1]);
|
|
|
|
continue;
|
|
|
|
}
|
2001-03-25 04:28:40 +00:00
|
|
|
if(pdb==2 && strcmp(p[0],"spacewidth")==0){
|
|
|
|
desc->spacewidth=atoi(p[1]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(pdb==2 && strcmp(p[0],"charspace")==0){
|
|
|
|
desc->charspace=atoi(p[1]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(pdb==2 && strcmp(p[0],"height")==0){
|
|
|
|
desc->height=atoi(p[1]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
2001-03-25 19:10:52 +00:00
|
|
|
|
2001-03-25 04:28:40 +00:00
|
|
|
if(strcmp(section,"[characters]")==0){
|
2001-03-27 00:38:09 +00:00
|
|
|
if(pdb==3){
|
2001-03-25 04:28:40 +00:00
|
|
|
int chr=p[0][0];
|
|
|
|
int start=atoi(p[1]);
|
|
|
|
int end=atoi(p[2]);
|
2001-05-08 19:50:22 +00:00
|
|
|
if(sub_unicode && (chr>=0x80)) chr=(chr<<8)+p[0][1];
|
2001-05-06 02:26:17 +00:00
|
|
|
else if(strlen(p[0])!=1) chr=strtol(p[0],NULL,0);
|
2001-03-25 04:28:40 +00:00
|
|
|
if(end<start) {
|
2004-11-08 00:06:49 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_WARN, "error in font desc: end<start for char '%c'\n",chr);
|
2001-03-25 04:28:40 +00:00
|
|
|
} else {
|
|
|
|
desc->start[chr]=start;
|
|
|
|
desc->width[chr]=end-start+1;
|
|
|
|
desc->font[chr]=fontdb;
|
|
|
|
// printf("char %d '%c' start=%d width=%d\n",chr,chr,desc->start[chr],desc->width[chr]);
|
|
|
|
++chardb;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2004-11-08 00:06:49 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_ERR, "Syntax error in font desc: %s",sor);
|
2004-10-14 15:09:35 +00:00
|
|
|
goto fail_out;
|
2001-03-25 04:28:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
fclose(f);
|
2004-10-14 15:09:35 +00:00
|
|
|
f = NULL;
|
2001-03-25 04:28:40 +00:00
|
|
|
|
2004-07-11 12:42:19 +00:00
|
|
|
if (first == 1) {
|
2004-11-08 00:06:49 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_ERR, "%s is empty or a directory, ignoring.\n", fname);
|
2004-10-14 15:09:35 +00:00
|
|
|
goto fail_out;
|
2004-07-11 12:42:19 +00:00
|
|
|
}
|
|
|
|
|
2001-03-25 04:28:40 +00:00
|
|
|
//printf("font: pos of U = %d\n",desc->start[218]);
|
|
|
|
|
|
|
|
for(i=0;i<=fontdb;i++){
|
|
|
|
if(!desc->pic_a[i] || !desc->pic_b[i]){
|
2004-11-08 00:06:49 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_ERR, "font: Missing bitmap(s) for sub-font #%d\n",i);
|
2004-10-14 15:09:35 +00:00
|
|
|
goto fail_out;
|
2001-03-25 04:28:40 +00:00
|
|
|
}
|
2001-03-29 16:06:36 +00:00
|
|
|
//if(factor!=1.0f)
|
|
|
|
{
|
2001-03-25 20:38:14 +00:00
|
|
|
// re-sample alpha
|
|
|
|
int f=factor*256.0f;
|
|
|
|
int size=desc->pic_a[i]->w*desc->pic_a[i]->h;
|
|
|
|
int j;
|
2004-11-08 00:06:49 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_DBG2, "font: resampling alpha by factor %5.3f (%d) ",factor,f);fflush(stdout);
|
2001-03-25 20:38:14 +00:00
|
|
|
for(j=0;j<size;j++){
|
2001-06-02 16:02:38 +00:00
|
|
|
int x=desc->pic_a[i]->bmp[j]; // alpha
|
|
|
|
int y=desc->pic_b[i]->bmp[j]; // bitmap
|
|
|
|
|
|
|
|
#ifdef FAST_OSD
|
|
|
|
x=(x<(255-f))?0:1;
|
|
|
|
#else
|
2001-03-29 16:06:36 +00:00
|
|
|
|
2001-03-29 16:39:37 +00:00
|
|
|
x=255-((x*f)>>8); // scale
|
|
|
|
//if(x<0) x=0; else if(x>255) x=255;
|
|
|
|
//x^=255; // invert
|
|
|
|
|
2001-03-29 16:06:36 +00:00
|
|
|
if(x+y>255) x=255-y; // to avoid overflows
|
|
|
|
|
|
|
|
//x=0;
|
|
|
|
//x=((x*f*(255-y))>>16);
|
|
|
|
//x=((x*f*(255-y))>>16)+y;
|
2001-03-25 20:38:14 +00:00
|
|
|
//x=(x*f)>>8;if(x<y) x=y;
|
2001-03-29 16:39:37 +00:00
|
|
|
|
2001-03-29 16:06:36 +00:00
|
|
|
if(x<1) x=1; else
|
|
|
|
if(x>=252) x=0;
|
2001-06-02 16:02:38 +00:00
|
|
|
#endif
|
2001-03-29 16:39:37 +00:00
|
|
|
|
2001-03-25 20:38:14 +00:00
|
|
|
desc->pic_a[i]->bmp[j]=x;
|
2001-03-29 16:39:37 +00:00
|
|
|
// desc->pic_b[i]->bmp[j]=0; // hack
|
2001-03-25 20:38:14 +00:00
|
|
|
}
|
2004-11-08 00:06:49 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_DBG2, "DONE!\n");
|
2001-03-25 20:38:14 +00:00
|
|
|
}
|
2001-03-25 04:28:40 +00:00
|
|
|
if(!desc->height) desc->height=desc->pic_a[i]->h;
|
|
|
|
}
|
|
|
|
|
|
|
|
j='_';if(desc->font[j]<0) j='?';
|
2004-06-18 13:51:45 +00:00
|
|
|
for(i=0;i<65536;i++)
|
2001-03-25 04:28:40 +00:00
|
|
|
if(desc->font[i]<0){
|
|
|
|
desc->start[i]=desc->start[j];
|
|
|
|
desc->width[i]=desc->width[j];
|
|
|
|
desc->font[i]=desc->font[j];
|
|
|
|
}
|
|
|
|
desc->font[' ']=-1;
|
|
|
|
desc->width[' ']=desc->spacewidth;
|
|
|
|
|
2006-08-27 15:33:11 +00:00
|
|
|
mp_msg(MSGT_OSD, MSGL_V, "Bitmap font %s loaded successfully! (%d chars)\n",fname,chardb);
|
2001-03-25 04:28:40 +00:00
|
|
|
|
|
|
|
return desc;
|
2004-10-14 15:09:35 +00:00
|
|
|
|
|
|
|
fail_out:
|
|
|
|
if (f)
|
|
|
|
fclose(f);
|
|
|
|
if (desc->fpath)
|
|
|
|
free(desc->fpath);
|
|
|
|
if (desc->name)
|
|
|
|
free(desc->name);
|
|
|
|
if (desc)
|
|
|
|
free(desc);
|
|
|
|
return NULL;
|
2001-03-25 04:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
int main(){
|
|
|
|
|
2001-04-11 00:47:05 +00:00
|
|
|
read_font_desc("high_arpi.desc",1);
|
2001-03-25 04:28:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
2002-08-28 20:52:02 +00:00
|
|
|
|