dynamic keyframes, textbox rework, andrea ffmpeg.opts, perpetual chkpt undo, lv2...
[goodguy/history.git] / cinelerra-5.1 / plugins / titler / titler.C
index 5086fea00d029d6101dae87de2b60ae5229449ed..d0f4f681ca08fddd629e7cffa36f392c6aa8317f 100644 (file)
@@ -102,7 +102,7 @@ TitleConfig::TitleConfig()
        vjustification = JUSTIFY_MID;
        fade_in = 0.0;  fade_out = 0.0;
        pixels_per_second = 100.0;
-       wtext[0] = 0;  wlen = 0;
+       wtext = 0;  wsize = 0;  wlen = 0;
        title_x = title_y = 0.0;
        title_w = title_h = 0;
        window_w = 860;
@@ -120,6 +120,7 @@ TitleConfig::TitleConfig()
 
 TitleConfig::~TitleConfig()
 {
+       delete [] wtext;
 }
 
 int TitleConfig::equivalent(TitleConfig &that)
@@ -178,7 +179,7 @@ void TitleConfig::copy_from(TitleConfig &that)
        fade_in = that.fade_in;
        fade_out = that.fade_out;
        pixels_per_second = that.pixels_per_second;
-       wlen = that.wlen;
+       demand(wlen = that.wlen);
        memcpy(wtext, that.wtext, that.wlen * sizeof(wchar_t));
        title_x = that.title_x;  title_y = that.title_y;
        title_w = that.title_w;  title_h = that.title_h;
@@ -216,7 +217,7 @@ void TitleConfig::interpolate(TitleConfig &prev, TitleConfig &next,
        fade_in = prev.fade_in;
        fade_out = prev.fade_out;
        pixels_per_second = prev.pixels_per_second;
-       wlen = prev.wlen;
+       demand(wlen = prev.wlen);
        memcpy(wtext, prev.wtext, prev.wlen * sizeof(wchar_t));
        wtext[wlen] = 0;
        this->title_x = prev.title_x == next.title_x ? prev.title_x :
@@ -239,10 +240,21 @@ void TitleConfig::interpolate(TitleConfig &prev, TitleConfig &next,
        loop_playback = prev.loop_playback;
 }
 
+int TitleConfig::demand(long sz)
+{
+       if( wtext && wsize >= sz ) return 0;
+       delete [] wtext;
+       wsize = sz + wlen/2 + 0x1000;
+       wtext = new wchar_t[wsize+1];
+       wtext[wsize] = 0;
+       return 1;
+}
+
 void TitleConfig::to_wtext(const char *from_enc, const char *text, int tlen)
 {
+       demand(tlen);
        wlen = BC_Resources::encode(from_enc, BC_Resources::wide_encoding,
-               (char*)text,tlen, (char *)wtext,sizeof(wtext)) / sizeof(wchar_t);
+               (char*)text,tlen, (char *)wtext,sizeof(*wtext)*wsize) / sizeof(wchar_t);
        while( wlen > 0 && !wtext[wlen-1] ) --wlen;
 }
 
@@ -2503,7 +2515,7 @@ void TitleMain::save_data(KeyFrame *keyframe)
 {
        FileXML output;
 
-       output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
+       output.set_shared_output(keyframe->xbuf);
        output.tag.set_title("TITLE");
        output.tag.set_property("FONT", config.font);
        output.tag.set_property("ENCODING", config.encoding);
@@ -2539,19 +2551,12 @@ void TitleMain::save_data(KeyFrame *keyframe)
        output.tag.set_property("LOOP_PLAYBACK", config.loop_playback);
        output.append_tag();
        output.append_newline();
-       char text[2*sizeof(config.wtext)];
+       long tsz = 2*config.wsize + 0x1000;
+       char text[tsz];
        int text_len = BC_Resources::encode(
                BC_Resources::wide_encoding, DEFAULT_ENCODING,
                (char*)config.wtext, config.wlen*sizeof(wchar_t),
-               text, sizeof(text));
-       int len = output.length(), avail = MESSAGESIZE-16 - len;
-       if( text_len >= avail ) { // back off last utf8 char
-               text_len = avail;
-               while( text_len > 0 && (text[text_len-1] & 0xc0) == 0x80 )
-                       text[--text_len] = 0;
-               if( text_len > 0 )
-                       text[--text_len] = 0;
-       }
+               text, tsz);
        output.append_text(text, text_len);
        output.tag.set_title("/TITLE");
        output.append_tag();
@@ -2565,7 +2570,7 @@ void TitleMain::read_data(KeyFrame *keyframe)
 {
        FileXML input;
 
-       input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
+       input.set_shared_input(keyframe->xbuf);
 
        int result = 0;
 
@@ -2620,22 +2625,22 @@ void TitleMain::read_data(KeyFrame *keyframe)
 void TitleMain::insert_text(const wchar_t *wtxt, int pos)
 {
        int len = wcslen(wtxt);
-       wchar_t *wtext = config.wtext;
-       int wsize = sizeof(config.wtext)-1;
        int wlen = config.wlen;
        if( pos < 0 ) pos = 0;
        if( pos > wlen ) pos = wlen;
-
-       for( int i=wlen-1, j=wlen+len-1; i>=pos; --i,--j ) {
-               if( j >= wsize ) continue;
+       config.demand(wlen + len);
+       int wsize1 = config.wsize-1;
+       wchar_t *wtext = config.wtext;
+       for( int i=wlen, j=wlen+len; --i>=pos; ) {
+               if( --j >= wsize1 ) continue;
                wtext[j] = wtext[i];
        }
        for( int i=pos, j=0; j<len; ++i,++j ) {
-               if( i >= wsize ) break;
+               if( i >= wsize1 ) break;
                wtext[i] = wtxt[j];
        }
 
-       if( (wlen+=len) > wsize ) wlen = wsize;
+       if( (wlen+=len) > wsize1 ) wlen = wsize1;
        wtext[wlen] = 0;
        config.wlen = wlen;
 }