[MPlayer-dev-eng] [PATCH] Subtitles alignment

Salvatore Falco sfalco at studenti.ing.uniroma1.it
Fri Sep 5 14:37:08 CEST 2003


	Hi all. This patch allows to left-/center-/right- align
subtitles. This alignment is derived from the subtitle file. Currently
I only implemented JACOsub support, but I plan to add other formats, if
this patch will be accepted.

-- 
	Saluti, Salvatore Falco
-------------- next part --------------
diff -Nur MPlayer-20030904.orig/libvo/sub.c MPlayer-20030904/libvo/sub.c
--- MPlayer-20030904.orig/libvo/sub.c	2003-06-09 00:24:49.000000000 +0000
+++ MPlayer-20030904/libvo/sub.c	2003-09-05 15:03:31.000000000 +0000
@@ -308,7 +331,7 @@
 
 inline static void vo_update_text_sub(mp_osd_obj_t* obj,int dxs,int dys){
    unsigned char *t;
-   int c,i,j,l,x,y,font,prevc;
+   int c,i,j,l,x,y,font,prevc, counter;
    int len;
    int k,lastk;
    int lastStripPosition;
@@ -461,10 +654,36 @@
 
     y = obj->y;
     
+    switch(vo_sub->alignment) {
+	case SUB_ALIGNMENT_HLEFT:
+	    obj->alignment |= 0x1;
+	    break;
+	case SUB_ALIGNMENT_HCENTER:
+	    obj->alignment |= 0x0;
+	    break;
+	case SUB_ALIGNMENT_HRIGHT:
+	default:
+	    obj->alignment |= 0x2;
+    }
+
     i=j=0;
-    if ((l=obj->params.subtitle.lines)) for (;;) {
- 	 x=obj->params.subtitle.xtbl[i++]; 
-	 if (sub_justify) x = 10;
+    if ((l = obj->params.subtitle.lines)) {
+	for(counter = dxs; i < l; ++i)
+	    if (obj->params.subtitle.xtbl[i] < counter) counter = obj->params.subtitle.xtbl[i];
+	for (i = 0; i < l; ++i) {
+	    switch (obj->alignment&0x3) {
+		case 1:
+		    // left
+		    x = counter;
+		    break;
+		case 2:
+		    // right
+		    x = 2 * obj->params.subtitle.xtbl[i] - counter - ((obj->params.subtitle.xtbl[i] == counter) ? 0 : 1);
+		    break;
+		default:
+		    //center
+		    x = obj->params.subtitle.xtbl[i];
+	    }
 	 prevc = -1;
 	 while ((c=obj->params.subtitle.utbl[j++])){
 	       x += kerning(vo_font,prevc,c);
@@ -477,9 +696,9 @@
 			     vo_font->pic_a[font]->w);
 	       x+=vo_font->width[c]+vo_font->charspace;
                prevc = c;
-	 }
-         if (!--l) break;
+	    }
          y+=vo_font->height;
+	}
     }
     
 }
diff -Nur MPlayer-20030904.orig/subreader.c MPlayer-20030904/subreader.c
--- MPlayer-20030904.orig/subreader.c	2003-09-02 17:26:33.000000000 +0000
+++ MPlayer-20030904/subreader.c	2003-09-05 14:11:50.000000000 +0000
@@ -811,11 +811,17 @@
 		|| (strstr(directive, "RLG") != NULL)) {
 		continue;
 	    }
+	    if (strstr(directive, "JL") != NULL) {
+		current->alignment = SUB_ALIGNMENT_HLEFT;
+	    } else if (strstr(directive, "JR") != NULL) {
+		current->alignment = SUB_ALIGNMENT_HRIGHT;
+	    } else {
+		current->alignment = SUB_ALIGNMENT_HCENTER;
+	    }
 	    strcpy(line2, line1);
 	    p = line2;
 	}
-	for (q = line1; (!eol(*p)) && (current->lines + 1 < SUB_MAX_TEXT);
-	     ++p) {
+	for (q = line1; (!eol(*p)) && (current->lines < SUB_MAX_TEXT); ++p) {
 	    switch (*p) {
 	    case '{':
 		comment++;
@@ -1266,6 +1272,7 @@
 	    first[sub_num].start = sub->start;
   	    first[sub_num].end   = sub->end;
 	    first[sub_num].lines = sub->lines;
+	    first[sub_num].alignment = sub->alignment;
   	    for(i = 0; i < sub->lines; ++i){
 		first[sub_num].text[i] = sub->text[i];
   	    }
@@ -1278,6 +1285,7 @@
     		first[j + 1].start = first[j].start;
     		first[j + 1].end   = first[j].end;
 		first[j + 1].lines = first[j].lines;
+		first[j + 1].alignment = first[j].alignment;
     		for(i = 0; i < first[j].lines; ++i){
       		    first[j + 1].text[i] = first[j].text[i];
 		}
@@ -1285,6 +1293,7 @@
 	    	    first[j].start = sub->start;
 	    	    first[j].end   = sub->end;
 	    	    first[j].lines = sub->lines;
+	    	    first[j].alignment = sub->alignment;
 	    	    for(i = 0; i < SUB_MAX_TEXT; ++i){
 			first[j].text[i] = sub->text[i];
 		    }
@@ -1471,6 +1480,7 @@
 		    second[sub_num + j].start = first[sub_first + j].start;
 		    second[sub_num + j].end   = first[sub_first + j].end;
 		    second[sub_num + j].lines = first[sub_first + j].lines;
+		    second[sub_num + j].alignment = first[sub_first + j].alignment;
 		    for (ls = 0; ls < second[sub_num + j].lines; ls++) {
 			second[sub_num + j].text[ls] = strdup(first[sub_first + j].text[ls]);
 		    }
@@ -1487,6 +1497,7 @@
 	    memset(&second[sub_num], '\0', sizeof(subtitle));
 	    second[sub_num].start = local_start;
 	    second[sub_num].end   = local_end;
+	    second[sub_num].alignment = SUB_ALIGNMENT_HCENTER;
 	    n_max = (lines_to_add < SUB_MAX_TEXT) ? lines_to_add : SUB_MAX_TEXT;
 	    for (i = 0, j = 0; j < n_max; ++j) {
 		if (placeholder[counter][j] != -1) {
diff -Nur MPlayer-20030904.orig/subreader.h MPlayer-20030904/subreader.h
--- MPlayer-20030904.orig/subreader.h	2003-04-09 18:04:04.000000000 +0000
+++ MPlayer-20030904/subreader.h	2003-09-05 14:11:50.000000000 +0000
@@ -27,6 +27,9 @@
 #define MAX_SUBTITLE_FILES 128
 
 #define SUB_MAX_TEXT 10
+#define SUB_ALIGNMENT_HLEFT	1
+#define SUB_ALIGNMENT_HCENTER	0
+#define SUB_ALIGNMENT_HRIGHT	2
 
 typedef struct {
 
@@ -36,6 +39,7 @@
     unsigned long end;
     
     char *text[SUB_MAX_TEXT];
+    unsigned char alignment;
 } subtitle;
 
 typedef struct {


More information about the MPlayer-dev-eng mailing list