changeset 213:6ec8f6ab6cb1

subtitle+OSD font support
author arpi_esp
date Sun, 25 Mar 2001 04:28:40 +0000
parents 7034ef1f685d
children 09d0f437b817
files libvo/Makefile libvo/font_load.c libvo/font_load.h libvo/sub.c libvo/video_out.h libvo/vo_mga.c libvo/vo_x11.c libvo/vo_xmga.c
diffstat 8 files changed, 294 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/libvo/Makefile	Sun Mar 25 04:27:39 2001 +0000
+++ b/libvo/Makefile	Sun Mar 25 04:28:40 2001 +0000
@@ -3,8 +3,8 @@
 
 LIBNAME = libvo.a
 
-SRCS=rgb15to16mmx.c yuv2rgb_mmx.c yuv2rgb.c video_out.c vo_null.c vo_pgm.c vo_md5.c vo_odivx.c x11_common.c $(OPTIONAL_SRCS)
-OBJS=rgb15to16mmx.o yuv2rgb_mmx.o yuv2rgb.o video_out.o vo_null.o vo_pgm.o vo_md5.o vo_odivx.o x11_common.o $(OPTIONAL_OBJS)
+SRCS=font_load.c rgb15to16mmx.c yuv2rgb_mmx.c yuv2rgb.c video_out.c vo_null.c vo_pgm.c vo_md5.c vo_odivx.c x11_common.c $(OPTIONAL_SRCS)
+OBJS=font_load.o rgb15to16mmx.o yuv2rgb_mmx.o yuv2rgb.o video_out.o vo_null.o vo_pgm.o vo_md5.o vo_odivx.o x11_common.o $(OPTIONAL_OBJS)
 
 CFLAGS  = $(OPTFLAGS) -I. -I.. -DMPG12PLAY
 # -I/usr/X11R6/include/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libvo/font_load.c	Sun Mar 25 04:28:40 2001 +0000
@@ -0,0 +1,191 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "font_load.h"
+
+raw_file* load_raw(char *name){
+    int bpp;
+    raw_file* raw=malloc(sizeof(raw_file));
+    unsigned char head[32];
+    FILE *f=fopen(name,"rb");
+    if(!f) return NULL;                        // can't open
+    if(fread(head,32,1,f)<1) return NULL;        // too small
+    if(memcmp(head,"mhwanh",6)) return NULL;        // not raw file
+    raw->w=head[8]*256+head[9];
+    raw->h=head[10]*256+head[11];
+    raw->c=head[12]*256+head[13];
+    if(raw->c>256) return NULL;                 // too many colors!?
+    printf("RAW: %s  %d x %d, %d colors\n",name,raw->w,raw->h,raw->c);
+    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;
+}
+
+font_desc_t* read_font_desc(char* fname){
+unsigned char sor[1024];
+unsigned char sor2[1024];
+font_desc_t *desc;
+FILE *f;
+char section[64];
+int i,j;
+int chardb=0;
+int fontdb=-1;
+
+desc=malloc(sizeof(font_desc_t));if(!desc) return NULL;
+memset(desc,0,sizeof(font_desc_t));
+
+f=fopen(fname,"rt");if(!f){ printf("font: can't open file: %s\n",fname); return NULL;}
+
+// set up some defaults, and erase table
+desc->charspace=2;
+desc->spacewidth=12;
+desc->height=0;
+for(i=0;i<512;i++) desc->start[i]=desc->width[i]=desc->font[i]=-1;
+
+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;
+  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]);
+        printf("font: Reading section: %s\n",section);
+        if(strcmp(section,"[files]")==0){
+            ++fontdb;
+            if(fontdb>=16){ printf("font: Too many bitmaps defined!\n");return NULL;}
+        }
+        continue;
+      }
+  }
+  
+  if(strcmp(section,"[files]")==0){
+      if(pdb==2 && strcmp(p[0],"alpha")==0){
+          if(!((desc->pic_a[fontdb]=load_raw(p[1])))){
+                printf("Can't load font bitmap: %s\n",p[1]);
+                return NULL;
+          }
+          continue;
+      }
+      if(pdb==2 && strcmp(p[0],"bitmap")==0){
+          if(!((desc->pic_b[fontdb]=load_raw(p[1])))){
+                printf("Can't load font bitmap: %s\n",p[1]);
+                return NULL;
+          }
+          continue;
+      }
+  } else
+
+  if(strcmp(section,"[info]")==0){
+      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
+  if(strcmp(section,"[characters]")==0){
+      if(pdb==3 && strlen(p[0])==1){
+          int chr=p[0][0];
+          int start=atoi(p[1]);
+          int end=atoi(p[2]);
+          if(end<start) {
+              printf("error in font desc: end<start for char '%c'\n",chr);
+          } 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;
+      }
+  }
+  printf("Syntax error in font desc: %s\n",sor);
+
+}
+fclose(f);
+
+//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]){
+        printf("font: Missing bitmap(s) for sub-font #%d\n",i);
+        return NULL;
+    }
+    if(!desc->height) desc->height=desc->pic_a[i]->h;
+}
+
+j='_';if(desc->font[j]<0) j='?';
+for(i=0;i<512;i++)
+  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;
+
+printf("font: Font %s loaded successfully! (%d chars)\n",fname,chardb);
+
+return desc;
+}
+
+#if 0
+int main(){
+
+read_font_desc("high_arpi.desc");
+
+}
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libvo/font_load.h	Sun Mar 25 04:28:40 2001 +0000
@@ -0,0 +1,22 @@
+
+typedef struct {
+    unsigned char *bmp;
+    unsigned char *pal;
+    int w,h,c;
+} raw_file;
+
+typedef struct {
+    int spacewidth;
+    int charspace;
+    int height;
+//    char *fname_a;
+//    char *fname_b;
+    raw_file* pic_a[16];
+    raw_file* pic_b[16];
+    short font[512];
+    short start[512];
+    short width[512];
+} font_desc_t;
+
+raw_file* load_raw(char *name);
+font_desc_t* read_font_desc(char* fname);
--- a/libvo/sub.c	Sun Mar 25 04:27:39 2001 +0000
+++ b/libvo/sub.c	Sun Mar 25 04:28:40 2001 +0000
@@ -1,71 +1,82 @@
 
-typedef struct {
-    unsigned char *bmp;
-    unsigned char *pal;
-    int w,h,c;
-} raw_file;
-
-raw_file* load_raw(char *name){
-    int bpp;
-    raw_file* raw=malloc(sizeof(raw_file));
-    unsigned char head[32];
-    FILE *f=fopen(name,"rb");
-    if(!f) return NULL;                        // can't open
-    if(fread(head,32,1,f)<1) return NULL;        // too small
-    if(memcmp(head,"mhwanh",6)) return NULL;        // not raw file
-    raw->w=head[8]*256+head[9];
-    raw->h=head[10]*256+head[11];
-    raw->c=head[12]*256+head[13];
-    if(raw->c>256) return NULL;                 // too many colors!?
-    printf("RAW: %d x %d, %d colors\n",raw->w,raw->h,raw->c);
-    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;
-}
-
-static int vo_font_loaded=-1;
-static raw_file* vo_font_bmp=NULL;
-static raw_file* vo_font_alpha=NULL;
-
-void vo_load_font(char *bmpname,char *alphaname){
-    vo_font_loaded=0;
-    if(!(vo_font_bmp=load_raw(bmpname)))
-        printf("vo: Can't load font BMP\n"); else
-    if(!(vo_font_alpha=load_raw(alphaname)))
-        printf("vo: Can't load font Alpha\n"); else
-    vo_font_loaded=1;
-}
+//static int vo_font_loaded=-1;
+font_desc_t* vo_font=NULL;
 
 int vo_sub_lines=2;
-char* vo_sub_text[8];
+unsigned char* vo_sub_text[8];
+
+unsigned char* vo_osd_text="00:00:00";
+
+void vo_draw_text(int dxs,int dys,void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride)){
+    int i;
+    int y;
 
+    if(!vo_font) return; // no font
+
+    if(vo_osd_text){
+        int len=strlen(vo_osd_text);
+        int j;
+        int y=10;
+        int x=20;
 
-void vo_draw_text(void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride)){
-    int i;
-    int y=100;
+        for(j=0;j<len;j++){
+          int c=vo_osd_text[j];
+          int font=vo_font->font[c];
+          if(font>=0)
+            draw_alpha(x,y,
+              vo_font->width[c],
+              vo_font->pic_a[font]->h,
+              vo_font->pic_b[font]->bmp+vo_font->start[c],
+              vo_font->pic_a[font]->bmp+vo_font->start[c],
+              vo_font->pic_a[font]->w);
+          x+=vo_font->width[c]+vo_font->charspace;
+        }
+        
+    }
+
+#if 1
 
     if(vo_sub_lines<=0) return; // no text
-    
-    if(vo_font_loaded==-1) vo_load_font("font_b.raw","font_a.raw");
-    if(!vo_font_loaded) return; // no font
-    
-//    if(!vo_font_bmp) vo_load_font("mplayer_font_lowres_bitmap.raw","mplayer_font_lowres_alpha.raw");
+    y=dys-(1+vo_sub_lines)*vo_font->height;
     
     for(i=0;i<vo_sub_lines;i++){
-        char* text="Hello World!"; //vo_sub_text[i];
-        draw_alpha(100,y,50,vo_font_bmp->h,vo_font_bmp->bmp,vo_font_alpha->bmp,vo_font_bmp->w);
-//        x11_draw_alpha(100,y,50,vo_font_bmp->h,vo_font_bmp->bmp,vo_font_alpha->bmp,vo_font_bmp->w);
-        y+=50;
+        unsigned char* text="Hello World! HÛDEJÓ!"; //vo_sub_text[i];
+        int len=strlen(text);
+        int j;
+        int xsize=-vo_font->charspace;
+        int x=0;
+
+        for(j=0;j<len;j++){
+          int w=vo_font->width[text[j]];
+          if(w>100) printf("gazvan: %d (%d=%c)\n",w,text[j],text[j]);
+          xsize+=w+vo_font->charspace;
+        }
+        //printf("text width = %d\n",xsize);
+        
+        if(xsize>dxs) printf("Warning! SUB too wide!!! (%d>%d)\n",xsize,dxs);
+        
+        x=dxs/2-xsize/2;
+        
+        for(j=0;j<len;j++){
+          int c=text[j];
+          int font=vo_font->font[c];
+          if(x>=0 && x+vo_font->width[c]<dxs)
+          if(font>=0)
+            draw_alpha(x,y,
+              vo_font->width[c],
+              vo_font->pic_a[font]->h,
+              vo_font->pic_b[font]->bmp+vo_font->start[c],
+              vo_font->pic_a[font]->bmp+vo_font->start[c],
+              vo_font->pic_a[font]->w);
+          x+=vo_font->width[c]+vo_font->charspace;
+        }
+
+        y+=vo_font->height;
     }
 
+#endif
+
 }
 
+
+
--- a/libvo/video_out.h	Sun Mar 25 04:27:39 2001 +0000
+++ b/libvo/video_out.h	Sun Mar 25 04:28:40 2001 +0000
@@ -8,6 +8,8 @@
 
 #include <inttypes.h>
 
+#include "font_load.h"
+
 #define IMGFMT_YV12 0x32315659
 //#define IMGFMT_YUY2 (('Y'<<24)|('U'<<16)|('Y'<<8)|'2')
 #define IMGFMT_YUY2 (('2'<<24)|('Y'<<16)|('U'<<8)|'Y')
@@ -103,3 +105,8 @@
 // NULL terminated array of all drivers
 extern vo_functions_t* video_out_drivers[];
 
+extern int vo_sub_lines;
+extern unsigned char* vo_sub_text[8];
+extern unsigned char* vo_osd_text;
+extern font_desc_t* vo_font;
+
--- a/libvo/vo_mga.c	Sun Mar 25 04:27:39 2001 +0000
+++ b/libvo/vo_mga.c	Sun Mar 25 04:28:40 2001 +0000
@@ -105,7 +105,7 @@
 
 static void flip_page(void)
 {
-    vo_draw_text(draw_alpha);
+    vo_draw_text(mga_vid_config.src_width,mga_vid_config.src_height,draw_alpha);
     vo_mga_flip_page();
 }
 
--- a/libvo/vo_x11.c	Sun Mar 25 04:27:39 2001 +0000
+++ b/libvo/vo_x11.c	Sun Mar 25 04:28:40 2001 +0000
@@ -406,7 +406,7 @@
 
 
 static void flip_page( void ){
-    vo_draw_text(draw_alpha);
+    vo_draw_text(image_width,image_height,draw_alpha);
     check_events();
     Display_Image( myximage,ImageData );
 }
--- a/libvo/vo_xmga.c	Sun Mar 25 04:27:39 2001 +0000
+++ b/libvo/vo_xmga.c	Sun Mar 25 04:28:40 2001 +0000
@@ -151,7 +151,7 @@
     timer=t;
 #endif
 
-    vo_draw_text(draw_alpha);
+    vo_draw_text(mga_vid_config.src_width,mga_vid_config.src_height,draw_alpha);
 
     check_events();
     vo_mga_flip_page();