Index: sub.c =================================================================== RCS file: /cvsroot/mplayer/main/libvo/sub.c,v retrieving revision 1.69 diff -u -b -B -r1.69 sub.c --- sub.c 19 Jan 2003 20:13:49 -0000 1.69 +++ sub.c 25 Mar 2003 18:35:22 -0000 @@ -305,6 +305,97 @@ // vo_draw_text_sub(int dxs,int dys,void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride)) +static int splitpos[100]; +static int splitcnt; + +static void split(int *dest, char *src, int off, int *minw, int *maxw, int dxs) +{ + unsigned char *t; + int c,i,j,l,x,y,font,prevc; + int len; + int k,lastk; + int lastStripPosition; + int xsize,lastxsize; + int xmin=dxs,xmax=0; + int h,lasth; + int min_linecnt; + int spaces[100], xsizes[100], spacecnt; + + t = src; + +// fprintf(stderr,"str = '%s'\n", t); + + spacecnt = 0; + + xsize=-vo_font->charspace; + prevc = -1; + + len=strlen(t)-1; + + for (j=0;j<=len;j++){ + if ((c=t[j])>=0x80){ + if (sub_utf8){ + if ((c & 0xe0) == 0xc0) /* 2 bytes U+00080..U+0007FF*/ + c = (c & 0x1f)<<6 | (t[++j] & 0x3f); + else if((c & 0xf0) == 0xe0){ /* 3 bytes U+00800..U+00FFFF*/ + c = (((c & 0x0f)<<6) | (t[++j] & 0x3f))<<6; + c |= (t[++j] & 0x3f); + } + } else if (sub_unicode) + c = (c<<8) + t[++j]; + } + if (k==MAX_UCS){ + len=j; // end here + mp_msg(MSGT_OSD,MSGL_WARN,"\nMAX_UCS exceeded!\n"); + } + if (!c) c++; // avoid UCS 0 + + if (c == ' ') { +// fprintf(stderr, "space = %d\n", j); + spaces[spacecnt] = j; + xsizes[spacecnt] = xsize; + spacecnt++; + } + + render_one_glyph(vo_font, c); + xsize+=vo_font->width[c]+vo_font->charspace+kerning(vo_font,prevc,c); + prevc = c; + + if (xsize > dxs*sub_width_p/100) break; + } + + if (xsize > dxs*sub_width_p/100 && spacecnt) { + int minw2 = 65535, maxw2 = 0, minpenalty = 65535, minsplit = -1; + for (i = 0; i < spacecnt; i++) { + int tmpminw2, tmpmaxw2, tmpxsize; + t = src + spaces[i] + 1; + tmpxsize = xsizes[i]; + split(dest+1, t, off + spaces[i] + 1, &tmpminw2, &tmpmaxw2, dxs); +// fprintf(stderr, "off = %d, split = %d, t = %s, xs = %d, xmin = %d, xmax = %d\n", off, off + spaces[i], t, tmpxsize, tmpminw2, tmpmaxw2); + + if (abs(tmpminw2-xsizes[i])+abs(tmpmaxw2-xsizes[i]) < minpenalty) { + minsplit = i; + minpenalty = abs(tmpminw2-xsizes[i])+abs(tmpmaxw2-xsizes[i]); + minw2 = tmpminw2; + maxw2 = tmpmaxw2; + xsize = tmpxsize; + } + } +// fprintf(stderr, "min_idx = %d, split = %d\n", minsplit, off + spaces[minsplit]); + dest[0] = off + spaces[minsplit]; + t = src + spaces[minsplit] + 1; + split(dest+1, t, off + spaces[minsplit] + 1, &minw2, &maxw2, dxs); + if (minw) *minw = minw2 < xsize ? minw2 : xsize; + if (maxw) *maxw = maxw2 > xsize ? maxw2 : xsize; + } else { + if (minw) *minw = xsize; + if (maxw) *maxw = xsize; + dest[0] = -1; + } + +} + + 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; @@ -314,6 +405,8 @@ int xsize,lastxsize; int xmin=dxs,xmax=0; int h,lasth; + int min_linecnt, maxw; + int splitcnt; obj->flags|=OSDFLAG_CHANGED|OSDFLAG_VISIBLE; @@ -337,12 +430,13 @@ t=vo_sub->text[i++]; len=strlen(t)-1; -// printf("sub(%d) '%s'\n",len,t); -// if(len<0) memy -=h; // according to max of vo_font->pic_a[font]->h -// else + split(splitpos, t, 0, NULL, NULL, dxs); + xsize=-vo_font->charspace; prevc = -1; + splitcnt = 0; + for (j=0;j<=len;j++){ if ((c=t[j])>=0x80){ if (sub_utf8){ @@ -385,18 +479,7 @@ } obj->params.subtitle.utbl[k++]=c; xsize+=vo_font->width[c]+vo_font->charspace+kerning(vo_font,prevc,c); - if (dxs*sub_width_p/1000){ - j=lastStripPosition; - xsize=lastxsize; - k=lastk; - } else { - xsize -=vo_font->width[c]+vo_font->charspace+kerning(vo_font,prevc,c);; // go back - k--; // cut line here - while (t[j] && t[j]!=' ') j++; // jump to the nearest space - } - } else if (jparams.subtitle.lines==MAX_UCSLINES||k>MAX_UCS){ l=0; len=j; // end parsing - } else if(l || jcharspace; lasth=h; h=vo_font->height; } -// printf("h: %d -> %d \n",vo_font->height,h); obj->y -=h; // according to max of vo_font->pic_a[font]->h prevc = -1; +// printf("h: %d -> %d \n",vo_font->height,h); } }