/* * CINELERRA * Copyright (C) 1997-2014 Adam Williams * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ // Originally developed by Heroine Virtual Ltd. // Support for multiple encodings, outline (stroke) by // Andraz Tori // Additional support for UTF-8 by // Paolo Rampino aka Akirad #include "asset.h" #include "bccmodels.h" #include "bcsignals.h" #include "cache.h" #include "clip.h" #include "cstrdup.h" #include "edl.h" #include "edlsession.h" #include "file.h" #include "filexml.h" #include "filesystem.h" #include "indexable.h" #include "ft2build.h" #include FT_GLYPH_H #include FT_BBOX_H #include FT_OUTLINE_H #include FT_STROKER_H #include "language.h" #include "mwindow.inc" #include "overlayframe.h" #include "renderengine.h" #include "titler.h" #include "titlerwindow.h" #include "transportque.h" #include "vrender.h" #include #include #include #include #include #include #include #include #include #include #define FIXED_FONT "Bitstream Vera Sans Mono (Bits)" #define SMALL (1.0 / 64.0) #define MAX_FLT 3.40282347e+38 #define MIN_FLT -3.40282347e+38 REGISTER_PLUGIN(TitleMain) #ifdef X_HAVE_UTF8_STRING #define DEFAULT_ENCODING "UTF-8" #else #define DEFAULT_ENCODING "ISO8859-1" #endif #define DEFAULT_TIMECODEFORMAT TIME_HMS TitleConfig::TitleConfig() { style = 0; color = BLACK; alpha = 0xff; outline_alpha = 0xff; size = 24; motion_strategy = NO_MOTION; loop = 0; line_pitch = 0; hjustification = JUSTIFY_CENTER; vjustification = JUSTIFY_MID; fade_in = 0.0; fade_out = 0.0; title_x = title_y = 0.0; title_w = title_h = 0; dropshadow = 2; strcpy(font, "fixed"); strcpy(encoding, DEFAULT_ENCODING); timecode_format = DEFAULT_TIMECODEFORMAT; pixels_per_second = 1.0; timecode = 0; stroke_width = 1.0; wtext[0] = 0; wlen = 0; color_stroke = 0xff0000; outline_color = WHITE; background = 0; strcpy(background_path, ""); outline_size = 0; window_w = 800; window_h = 460; next_keyframe_position = 0; prev_keyframe_position = 0; drag = 0; loop_playback = 0; } TitleConfig::~TitleConfig() { } int TitleConfig::equivalent(TitleConfig &that) { return !strcasecmp(font, that.font) && style == that.style && size == that.size && color == that.color && color_stroke == that.color_stroke && stroke_width == that.stroke_width && outline_color == that.outline_color && alpha == that.alpha && outline_alpha == that.outline_alpha && EQUIV(pixels_per_second, that.pixels_per_second) && motion_strategy == that.motion_strategy && loop == that.loop && line_pitch == that.line_pitch && hjustification == that.hjustification && vjustification == that.vjustification && fade_in == that.fade_in && fade_out == that.fade_out && title_x == that.title_x && title_y == that.title_y && title_w == that.title_w && title_h == that.title_h && dropshadow == that.dropshadow && timecode == that.timecode && timecode_format == that.timecode_format && outline_size == that.outline_size && !strcasecmp(encoding, that.encoding) && wlen == that.wlen && !memcmp(wtext, that.wtext, wlen * sizeof(wchar_t)); } void TitleConfig::copy_from(TitleConfig &that) { strcpy(font, that.font); style = that.style; size = that.size; color = that.color; color_stroke = that.color_stroke; stroke_width = that.stroke_width; outline_color = that.outline_color; alpha = that.alpha; outline_alpha = that.outline_alpha; pixels_per_second = that.pixels_per_second; motion_strategy = that.motion_strategy; loop = that.loop; line_pitch = that.line_pitch; hjustification = that.hjustification; vjustification = that.vjustification; fade_in = that.fade_in; fade_out = that.fade_out; title_x = that.title_x; title_y = that.title_y; title_w = that.title_w; title_h = that.title_h; dropshadow = that.dropshadow; timecode = that.timecode; timecode_format = that.timecode_format; outline_size = that.outline_size; strcpy(encoding, that.encoding); memcpy(wtext, that.wtext, that.wlen * sizeof(wchar_t)); wlen = that.wlen; window_w = that.window_w; window_h = that.window_h; } void TitleConfig::interpolate(TitleConfig &prev, TitleConfig &next, int64_t prev_frame, int64_t next_frame, int64_t current_frame) { strcpy(font, prev.font); strcpy(encoding, prev.encoding); style = prev.style; size = prev.size; color = prev.color; color_stroke = prev.color_stroke; stroke_width = prev.stroke_width; outline_color = prev.outline_color; alpha = prev.alpha; outline_alpha = prev.outline_alpha; motion_strategy = prev.motion_strategy; loop = prev.loop; line_pitch = prev.line_pitch; hjustification = prev.hjustification; vjustification = prev.vjustification; fade_in = prev.fade_in; fade_out = prev.fade_out; outline_size = prev.outline_size; pixels_per_second = prev.pixels_per_second; memcpy(wtext, prev.wtext, prev.wlen * sizeof(wchar_t)); wlen = prev.wlen; wtext[wlen] = 0; double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame); double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame); this->title_x = prev.title_x * prev_scale + next.title_x * next_scale; this->title_y = prev.title_y * prev_scale + next.title_y * next_scale; // this->title_x = prev.title_x; // this->title_y = prev.title_y; timecode = prev.timecode; timecode_format = prev.timecode_format; this->dropshadow = prev.dropshadow * prev_scale + next.dropshadow * next_scale; // this->dropshadow = prev.dropshadow; } void TitleConfig::to_wtext(const char *from_enc, const char *text, int tlen) { wlen = BC_Resources::encode(from_enc, BC_Resources::wide_encoding, (char*)text,tlen, (char *)wtext,sizeof(wtext)) / sizeof(wchar_t); while( wlen > 0 && !wtext[wlen-1] ) --wlen; } TitleGlyph::TitleGlyph() { char_code = 0; size = 0; data = 0; data_stroke = 0; freetype_index = 0; width = 0; height = 0; style = 0; pitch = 0; left = 0; top = 0; bottom = 0; right = 0; advance_x = 0; } TitleGlyph::~TitleGlyph() { //printf("TitleGlyph::~TitleGlyph 1\n"); if( data ) delete data; if( data_stroke ) delete data_stroke; } GlyphPackage::GlyphPackage() : LoadPackage() { glyph = 0; } GlyphUnit::GlyphUnit(TitleMain *plugin, GlyphEngine *server) : LoadClient(server) { this->plugin = plugin; freetype_library = 0; freetype_face = 0; } GlyphUnit::~GlyphUnit() { if( freetype_library ) FT_Done_FreeType(freetype_library); } void GlyphUnit::process_package(LoadPackage *package) { GlyphPackage *pkg = (GlyphPackage*)package; TitleGlyph *glyph = pkg->glyph; int result = 0; char new_path[BCTEXTLEN]; if( plugin->load_freetype_face(freetype_library, freetype_face, glyph->font->path) ) { printf(_("GlyphUnit::process_package FT_New_Face failed, path=%s\n"), glyph->font->path); result = 1; } if( !result ) { int gindex = FT_Get_Char_Index(freetype_face, glyph->char_code); //printf("GlyphUnit::process_package 1 %c\n", glyph->char_code); // Char not found if( gindex == 0 ) { BC_Resources *resources = BC_WindowBase::get_resources(); // Search replacement font if( resources->find_font_by_char(glyph->char_code, new_path, freetype_face) ) { plugin->load_freetype_face(freetype_library, freetype_face, new_path); gindex = FT_Get_Char_Index(freetype_face, glyph->char_code); } } FT_Set_Pixel_Sizes(freetype_face, glyph->size, 0); if( gindex == 0 ) { // carrige return if( glyph->char_code != 10 ) printf(_("GlyphUnit::process_package FT_Load_Char failed - char: %li.\n"), glyph->char_code); // Prevent a crash here glyph->width = 8; glyph->height = 8; glyph->pitch = 8; glyph->advance_x = 8; glyph->left = 9; glyph->top = 9; glyph->right = 0; glyph->bottom = 0; glyph->freetype_index = 0; glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch); glyph->data->clear_frame(); glyph->data_stroke = 0; // create outline glyph if( plugin->config.stroke_width >= SMALL && (plugin->config.style & BC_FONT_OUTLINE) ) { glyph->data_stroke = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch); glyph->data_stroke->clear_frame(); } } // char found and no outline desired else if( plugin->config.stroke_width < SMALL || !(plugin->config.style & BC_FONT_OUTLINE) ) { FT_Glyph glyph_image; FT_BBox bbox; FT_Bitmap bm; FT_Load_Glyph(freetype_face, gindex, FT_LOAD_DEFAULT); FT_Get_Glyph(freetype_face->glyph, &glyph_image); FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph_image)->outline, &bbox); // printf("Stroke: Xmin: %ld, Xmax: %ld, Ymin: %ld, yMax: %ld\n", // bbox.xMin,bbox.xMax, bbox.yMin, bbox.yMax); glyph->width = bm.width = ((bbox.xMax - bbox.xMin + 63) >> 6); glyph->height = bm.rows = ((bbox.yMax - bbox.yMin + 63) >> 6); glyph->pitch = bm.pitch = bm.width; bm.pixel_mode = FT_PIXEL_MODE_GRAY; bm.num_grays = 256; glyph->left = (bbox.xMin + 31) >> 6; glyph->top = (bbox.yMax + 31) >> 6; glyph->right = (bbox.xMax + 31) >> 6; glyph->bottom = (bbox.yMin + 31) >> 6; glyph->freetype_index = gindex; glyph->advance_x = ((freetype_face->glyph->advance.x + 31) >> 6); //printf("GlyphUnit::process_package 1 width=%d height=%d pitch=%d left=%d top=%d advance_x=%d freetype_index=%d\n", //glyph->width, glyph->height, glyph->pitch, glyph->left, glyph->top, glyph->advance_x, glyph->freetype_index); glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch); glyph->data->clear_frame(); bm.buffer = glyph->data->get_data(); FT_Outline_Translate(&((FT_OutlineGlyph) glyph_image)->outline, - bbox.xMin, - bbox.yMin); FT_Outline_Get_Bitmap( freetype_library, &((FT_OutlineGlyph) glyph_image)->outline, &bm); FT_Done_Glyph(glyph_image); } else { // Outline desired and glyph found FT_Glyph glyph_image; FT_Stroker stroker; FT_Outline outline; FT_Bitmap bm; FT_BBox bbox; FT_UInt npoints, ncontours; FT_Load_Glyph(freetype_face, gindex, FT_LOAD_DEFAULT); FT_Get_Glyph(freetype_face->glyph, &glyph_image); // check if the outline is ok (non-empty); FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph_image)->outline, &bbox); if( bbox.xMin == 0 && bbox.xMax == 0 && bbox.yMin == 0 && bbox.yMax == 0 ) { FT_Done_Glyph(glyph_image); glyph->width = 0; glyph->height = 0; glyph->left = 0; glyph->top = 0; glyph->right = 0; glyph->bottom = 0; glyph->pitch = 0; glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch); glyph->data_stroke = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch); glyph->advance_x =((int)(freetype_face->glyph->advance.x + plugin->config.stroke_width * 64)) >> 6; return; } #if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 2) FT_Stroker_New(freetype_library, &stroker); #else FT_Stroker_New(((FT_LibraryRec *)freetype_library)->memory, &stroker); #endif FT_Stroker_Set(stroker, (int)(plugin->config.stroke_width * 64), FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0); FT_Stroker_ParseOutline(stroker, &((FT_OutlineGlyph) glyph_image)->outline,1); FT_Stroker_GetCounts(stroker,&npoints, &ncontours); if( npoints == 0 && ncontours == 0 ) { // this never happens, but FreeType has a bug regarding Linotype's Palatino font FT_Stroker_Done(stroker); FT_Done_Glyph(glyph_image); glyph->width = 0; glyph->height = 0; glyph->left = 0; glyph->top = 0; glyph->right = 0; glyph->bottom = 0; glyph->pitch = 0; glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch); glyph->data_stroke = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch); glyph->advance_x =((int)(freetype_face->glyph->advance.x + plugin->config.stroke_width * 64)) >> 6; return; }; FT_Outline_New(freetype_library, npoints, ncontours, &outline); outline.n_points=0; outline.n_contours=0; FT_Stroker_Export (stroker, &outline); FT_Outline_Get_BBox(&outline, &bbox); FT_Outline_Translate(&outline, - bbox.xMin, - bbox.yMin); FT_Outline_Translate(&((FT_OutlineGlyph) glyph_image)->outline, - bbox.xMin, - bbox.yMin + (int)(plugin->config.stroke_width*32)); // printf("Stroke: Xmin: %ld, Xmax: %ld, Ymin: %ld, yMax: %ld\n" // "Fill Xmin: %ld, Xmax: %ld, Ymin: %ld, yMax: %ld\n", // bbox.xMin,bbox.xMax, bbox.yMin, bbox.yMax, // bbox_fill.xMin,bbox_fill.xMax, bbox_fill.yMin, bbox_fill.yMax); glyph->width = bm.width = ((bbox.xMax - bbox.xMin) >> 6)+1; glyph->height = bm.rows = ((bbox.yMax - bbox.yMin) >> 6) +1; glyph->pitch = bm.pitch = bm.width; bm.pixel_mode = FT_PIXEL_MODE_GRAY; bm.num_grays = 256; glyph->left = (bbox.xMin + 31) >> 6; glyph->top = (bbox.yMax + 31) >> 6; glyph->right = (bbox.xMax + 31) >> 6; glyph->bottom = (bbox.yMin + 31) >> 6; glyph->freetype_index = gindex; int real_advance = ((int)ceil((float)freetype_face->glyph->advance.x + plugin->config.stroke_width * 64) >> 6); glyph->advance_x = glyph->width + glyph->left; if( real_advance > glyph->advance_x ) glyph->advance_x = real_advance; //printf("GlyphUnit::process_package 1 width=%d height=%d " // "pitch=%d left=%d top=%d advance_x=%d freetype_index=%d\n", // glyph->width, glyph->height, glyph->pitch, glyph->left, // glyph->top, glyph->advance_x, glyph->freetype_index); //printf("GlyphUnit::process_package 1\n"); glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch); glyph->data->clear_frame(); glyph->data_stroke = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch); glyph->data_stroke->clear_frame(); // for debugging memset( glyph->data_stroke->get_data(), 60, glyph->pitch * glyph->height); bm.buffer=glyph->data->get_data(); FT_Outline_Get_Bitmap( freetype_library, &((FT_OutlineGlyph) glyph_image)->outline, &bm); bm.buffer=glyph->data_stroke->get_data(); FT_Outline_Get_Bitmap( freetype_library, &outline, &bm); FT_Outline_Done(freetype_library,&outline); FT_Stroker_Done(stroker); FT_Done_Glyph(glyph_image); //printf("GlyphUnit::process_package 2\n"); } } } GlyphEngine::GlyphEngine(TitleMain *plugin, int cpus) : LoadServer(cpus, cpus) { this->plugin = plugin; } void GlyphEngine::init_packages() { int current_package = 0; for( int i=0; ititle_glyphs.count(); ++i ) { if( !plugin->title_glyphs[i]->data ) { GlyphPackage *pkg = (GlyphPackage*)get_package(current_package++); pkg->glyph = plugin->title_glyphs[i]; } } } LoadClient* GlyphEngine::new_client() { return new GlyphUnit(plugin, this); } LoadPackage* GlyphEngine::new_package() { return new GlyphPackage; } // Copy a single character to the text mask TitlePackage::TitlePackage() : LoadPackage() { x = y = 0; chr = 0; } TitleUnit::TitleUnit(TitleMain *plugin, TitleEngine *server) : LoadClient(server) { this->plugin = plugin; this->engine = server; this->chr = 0; } static void get_mask_colors(int rgb, int color_model, int &rr, int &gg, int &bb) { int r = 0xff & (rgb>>16), g = 0xff & (rgb>>8), b = 0xff & (rgb>>0); if( BC_CModels::is_yuv(color_model) ) bc_rgb2yuv(r,g,b, r,g,b); rr = r; gg = g; bb = b; } void TitleUnit::draw_frame(int mode, VFrame *dst, VFrame *src, int x, int y) { int inp_w = src->get_w(), inp_h = src->get_h(); int out_w = dst->get_w(), out_h = dst->get_h(); unsigned char **inp_rows = src->get_rows(); unsigned char **out_rows = dst->get_rows(); int x_inp = 0, y_inp = 0; int x_out = x; if( x_out < 0 ) { x_inp = -x_out; x_out = 0; } if( x_out+inp_w > out_w ) inp_w = out_w-x_out; if( x_inp >= inp_w || x_out >= out_w ) return; int y_out = y; if( y_out < 0 ) { y_inp = -y_out; y_out = 0; } if( y_out+inp_h > out_h ) inp_h = out_h-y_out; if( y_inp >= inp_h || y_out >= out_h ) return; int color = chr->color, max = 0xff; int alpha = chr->alpha * chr->fade; int ofs = BC_CModels::is_yuv(dst->get_color_model()) ? 0x80 : 0x00; switch( mode ) { case DRAW_ALPHA: { while( y_inp < inp_h && y_out < out_h ) { uint8_t *inp = inp_rows[y_inp], *out = out_rows[y_out]; for( int xin=x_inp,xout=x_out*4+3; xintext_model, r, g, b); while( y_inp < inp_h && y_out < out_h ) { uint8_t *inp = inp_rows[y_inp], *out = out_rows[y_out]; for( int xin=x_inp,xout=x_out*4+0; xinx, y = pkg->y; chr = pkg->chr; switch( chr->typ ) { case CHAR_IMAGE: { VFrame *vframe = (VFrame *)chr->vp; if( !vframe ) return; draw_frame(DRAW_IMAGE, plugin->text_mask, vframe, x, y); break; } case CHAR_GLYPH: { if( chr->wch == 0 || chr->wch == '\n') return; TitleGlyph *glyph = (TitleGlyph *)chr->vp; if( !glyph ) return; if( engine->do_dropshadow ) { x += plugin->config.dropshadow; y += plugin->config.dropshadow; } else if( plugin->config.dropshadow < 0 ) { x -= plugin->config.dropshadow; y -= plugin->config.dropshadow; } int mode = engine->do_dropshadow ? DRAW_ALPHA : DRAW_COLOR; draw_frame(mode, plugin->text_mask, glyph->data, x, y); if( plugin->config.stroke_width >= SMALL && (plugin->config.style & BC_FONT_OUTLINE) ) draw_frame(mode, plugin->stroke_mask, glyph->data_stroke, x, y); break; } } } TitleEngine::TitleEngine(TitleMain *plugin, int cpus) : LoadServer(cpus, cpus) { this->plugin = plugin; } void TitleEngine::init_packages() { int current_package = 0; for( int i=plugin->visible_char1; ivisible_char2; ++i ) { TitlePackage *pkg = (TitlePackage*)get_package(current_package++); TitleChar *chr = plugin->title_chars[i]; TitleRow *row = plugin->title_rows[chr->row]; pkg->chr = chr; pkg->x = row->x0 + chr->x - plugin->mask_x1; pkg->y = row->y0 - chr->y - plugin->mask_y1; //printf("draw '%c' at %d,%d\n",(int)pkg->chr->wch, pkg->x, pkg->y); } } LoadClient* TitleEngine::new_client() { return new TitleUnit(plugin, this); } LoadPackage* TitleEngine::new_package() { return new TitlePackage; } void TitleTranslateUnit::translation_array_f(transfer_table_f* &table, float in_x1, float in_x2, int in_total, float out_x1, float out_x2, int out_total, int &x1_out, int &x2_out) { int out_w; //float offset = out_x1 - in_x1; x1_out = (int)out_x1; x2_out = MIN((int)ceil(out_x2), out_total); if( x1_out >= x2_out ) return; out_w = x2_out - x1_out; table = new transfer_table_f[out_w]; bzero(table, sizeof(transfer_table_f) * out_w); float in_x = in_x1; for( int out_x=x1_out; out_xin_x1 = (int)in_x; entry->in_x2 = (int)in_x + 1; // Get fraction of output pixel to fill entry->output_fraction = 1; if( out_x1 > out_x ) { entry->output_fraction -= out_x1 - out_x; } if( out_x2 < out_x + 1 ) { entry->output_fraction = (out_x2 - out_x); } // Advance in_x until out_x_fraction is filled float out_x_fraction = entry->output_fraction; float in_x_fraction = floor(in_x + 1) - in_x; if( out_x_fraction <= in_x_fraction ) { entry->in_fraction1 = out_x_fraction; entry->in_fraction2 = 0.0; in_x += out_x_fraction; } else { entry->in_fraction1 = in_x_fraction; in_x += out_x_fraction; entry->in_fraction2 = in_x - floor(in_x); } // Clip in_x and zero out fraction. This doesn't work for YUV. if( entry->in_x2 >= in_total ) { entry->in_x2 = in_total - 1; entry->in_fraction2 = 0.0; } if( entry->in_x1 >= in_total ) { entry->in_x1 = in_total - 1; entry->in_fraction1 = 0.0; } } } // Copy a single character to the text mask TitleOutlinePackage::TitleOutlinePackage() : LoadPackage() { } TitleOutlineUnit::TitleOutlineUnit(TitleMain *plugin, TitleOutlineEngine *server) : LoadClient(server) { this->plugin = plugin; this->engine = server; } void TitleOutlineUnit::process_package(LoadPackage *package) { TitleOutlinePackage *pkg = (TitleOutlinePackage*)package; int r = 0, g = 0, b = 0, outline_a = plugin->config.outline_alpha; get_mask_colors(plugin->config.outline_color, plugin->mask_model, r, g, b); unsigned char **outline_rows = plugin->outline_mask->get_rows(); unsigned char **text_rows = plugin->text_mask->get_rows(); int mask_w1 = plugin->text_mask->get_w()-1; int mask_h1 = plugin->text_mask->get_h()-1; int ofs = plugin->config.outline_size; if( engine->pass == 0 ) { // get max alpha under outline size macropixel for( int y=pkg->y1, my=pkg->y2; ytext_mask->get_w(); x max_a ) max_a = pixel[3]; } } unsigned char *out = out_row + x*4; out[0] = r; out[1] = g; out[2] = b; out[3] = (max_a * outline_a) / 0xff; } } return; } else { // Overlay text mask on top of outline mask int ofs = BC_CModels::is_yuv(plugin->output->get_color_model()) ? 0x80 : 0; for( int y=pkg->y1, my=pkg->y2; ytext_mask->get_w(); xplugin = plugin; } void TitleOutlineEngine::init_packages() { int mask_h = plugin->text_mask->get_h(); if( !mask_h ) return; int py1 = 0, py2 = 0; int pkgs = get_total_packages(); for( int i=0; iy1 = py1; pkg->y2 = py2; } } void TitleOutlineEngine::do_outline() { pass = 0; process_packages(); pass = 1; process_packages(); } LoadClient* TitleOutlineEngine::new_client() { return new TitleOutlineUnit(plugin, this); } LoadPackage* TitleOutlineEngine::new_package() { return new TitleOutlinePackage; } TitleTranslatePackage::TitleTranslatePackage() : LoadPackage() { y1 = y2 = 0; } TitleTranslateUnit::TitleTranslateUnit(TitleMain *plugin, TitleTranslate *server) : LoadClient(server) { this->plugin = plugin; } void TitleTranslate::run_packages() { // Generate scaling tables delete [] x_table; x_table = 0; delete [] y_table; y_table = 0; float in_w = xlat_mask->get_w(); float in_h = xlat_mask->get_h(); float ix1 = 0, ix2 = ix1 + in_w; float iy1 = 0, iy2 = iy1 + in_h; float out_w = plugin->output->get_w(); float out_h = plugin->output->get_h(); float x1 = plugin->title_x, x2 = x1 + plugin->title_w; float y1 = plugin->title_y, y2 = y1 + plugin->title_h; bclamp(x1, 0, out_w); bclamp(y1, 0, out_h); bclamp(x2, 0, out_w); bclamp(y2, 0, out_h); float ox1 = plugin->title_x + plugin->text_x - plugin->text_x1 + plugin->mask_x1, ox2 = ox1 + in_w; float oy1 = plugin->title_y + plugin->text_y - plugin->text_y1 + plugin->mask_y1, oy2 = oy1 + in_h; if( ox1 < x1 ) { ix1 -= (ox1-x1); ox1 = x1; } if( oy1 < y1 ) { iy1 -= (oy1-y1); oy1 = y1; } if( ox2 > x2 ) { ix2 -= (ox2-x2); ox2 = x2; } if( oy2 > y2 ) { iy2 -= (oy2-x2); oy2 = y2; } #if 0 printf("TitleTranslate text txy=%7.2f,%-7.2f\n" " mxy1=%7d,%-7d mxy2=%7d,%-7d\n" " xy1=%7.2f,%-7.2f xy2=%7.2f,%-7.2f\n" " ixy1=%7.2f,%-7.2f ixy2=%7.2f,%-7.2f\n" " oxy1=%7.2f,%-7.2f oxy2=%7.2f,%-7.2f\n", plugin->text_x, plugin->text_y, plugin->mask_x1, plugin->mask_y1, plugin->mask_x2, plugin->mask_y2, x1,y1, x2,y2, ix1,iy1, ix2,iy2, ox1,oy1, ox2,oy2); #endif out_x1 = out_x2 = out_y1 = out_y2 = 0; TitleTranslateUnit::translation_array_f(x_table, ix1, ix2, in_w, ox1, ox2, out_w, out_x1, out_x2); TitleTranslateUnit::translation_array_f(y_table, iy1, iy2, in_h, oy1, oy2, out_h, out_y1, out_y2); process_packages(); } #define TRANSLATE(type, max, components, ofs) { \ unsigned char **in_rows = server->xlat_mask->get_rows(); \ type **out_rows = (type**)plugin->output->get_rows(); \ \ for( int y=pkg->y1; yy2; ++y ) { \ int in_y1 = server->y_table[y].in_x1; \ int in_y2 = server->y_table[y].in_x2; \ float y_f1 = server->y_table[y].in_fraction1; \ float y_f2 = server->y_table[y].in_fraction2; \ unsigned char *in_row1 = in_rows[in_y1]; \ unsigned char *in_row2 = in_rows[in_y2]; \ type *out_row = out_rows[y + server->out_y1]; \ for( int i=0,x=server->out_x1; xout_x2; ++i,++x ) { \ int in_x1 = 4*server->x_table[i].in_x1; \ int in_x2 = 4*server->x_table[i].in_x2; \ float x_f1 = server->x_table[i].in_fraction1; \ float x_f2 = server->x_table[i].in_fraction2; \ float f11 = x_f1 * y_f1 / (256.f-max); \ float f12 = x_f2 * y_f1 / (256.f-max); \ float f21 = x_f1 * y_f2 / (256.f-max); \ float f22 = x_f2 * y_f2 / (256.f-max); \ type input_r = (type)( \ in_row1[in_x1 + 0] * f11 + in_row1[in_x2 + 0] * f12 + \ in_row2[in_x1 + 0] * f21 + in_row2[in_x2 + 0] * f22 ); \ type input_g = (type)( \ in_row1[in_x1 + 1] * f11 + in_row1[in_x2 + 1] * f12 + \ in_row2[in_x1 + 1] * f21 + in_row2[in_x2 + 1] * f22 ); \ type input_b = (type)( \ in_row1[in_x1 + 2] * f11 + in_row1[in_x2 + 2] * f12 + \ in_row2[in_x1 + 2] * f21 + in_row2[in_x2 + 2] * f22 ); \ type input_a = (type)( \ in_row1[in_x1 + 3] * f11 + in_row1[in_x2 + 3] * f12 + \ in_row2[in_x1 + 3] * f21 + in_row2[in_x2 + 3] * f22 ); \ input_a = input_a * plugin->fade; \ if( components == 4 ) { \ type transparency = out_row[x * components + 3] * (max - input_a) / max; \ out_row[x * components + 0] = (input_r * input_a + \ out_row[x * components + 0] * transparency) / max; \ out_row[x * components + 1] = ((input_g-ofs) * input_a + \ (out_row[x * components + 1]-ofs) * transparency) / max + ofs; \ out_row[x * components + 2] = ((input_b-ofs) * input_a + \ (out_row[x * components + 2]-ofs) * transparency) / max + ofs; \ out_row[x * components + 3] = MAX(input_a, out_row[x * components + 3]); \ } \ else { \ type transparency = max - input_a; \ out_row[x * components + 0] = (input_r * input_a + \ out_row[x * components + 0] * transparency) / max; \ out_row[x * components + 1] = ((input_g-ofs) * input_a + \ (out_row[x * components + 1]-ofs) * transparency) / max + ofs; \ out_row[x * components + 2] = ((input_b-ofs) * input_a + \ (out_row[x * components + 2]-ofs) * transparency) / max + ofs; \ } \ } \ } \ } void TitleTranslateUnit::process_package(LoadPackage *package) { TitleTranslatePackage *pkg = (TitleTranslatePackage*)package; TitleTranslate *server = (TitleTranslate*)this->server; switch( plugin->output->get_color_model() ) { case BC_RGB888: TRANSLATE(unsigned char, 0xff, 3, 0); break; case BC_RGB_FLOAT: TRANSLATE(float, 1.0, 3, 0); break; case BC_YUV888: TRANSLATE(unsigned char, 0xff, 3, 0x80); break; case BC_RGBA_FLOAT: TRANSLATE(float, 1.0, 4, 0); break; case BC_RGBA8888: TRANSLATE(unsigned char, 0xff, 4, 0); break; case BC_YUVA8888: TRANSLATE(unsigned char, 0xff, 4, 0x80); break; } //printf("TitleTranslateUnit::process_package 5\n"); } TitleTranslate::TitleTranslate(TitleMain *plugin, int cpus) : LoadServer(cpus, cpus) { this->plugin = plugin; x_table = 0; y_table = 0; out_x1 = out_x2 = 0; out_y1 = out_y2 = 0; } TitleTranslate::~TitleTranslate() { delete [] x_table; delete [] y_table; } void TitleTranslate::init_packages() { int out_h = out_y2 - out_y1; int py1 = 0, py2 = 0; int pkgs = get_total_packages(); for( int i=0; iy1 = py1; pkg->y2 = py2; } //printf("TitleTranslate::init_packages 2\n"); } LoadClient* TitleTranslate::new_client() { return new TitleTranslateUnit(plugin, this); } LoadPackage* TitleTranslate::new_package() { return new TitleTranslatePackage; } TitleCurNudge::TitleCurNudge(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, 0) { } TitleCurColor::TitleCurColor(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, plugin->config.color) { } TitleCurAlpha::TitleCurAlpha(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, plugin->config.alpha) { } TitleCurSize::TitleCurSize(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, plugin->config.size) { } TitleCurBold::TitleCurBold(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, (plugin->config.style & BC_FONT_BOLD) ? 1 : 0) { } TitleCurItalic::TitleCurItalic(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, (plugin->config.style & BC_FONT_ITALIC) ? 1 : 0) { } TitleCurFont::TitleCurFont(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, plugin->config_font()) { } TitleCurCaps::TitleCurCaps(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, 0) { } TitleCurUnder::TitleCurUnder(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, 0) { } TitleCurBlink::TitleCurBlink(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, 0) { } TitleCurFixed::TitleCurFixed(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, 0) { } TitleCurSuper::TitleCurSuper(TitleParser *parser, TitleMain *plugin) : TitleStack(parser, 0) { } TitleParser::TitleParser(TitleMain *plugin) : plugin(plugin), cur_nudge(this, plugin), cur_color(this, plugin), cur_alpha(this, plugin), cur_size(this, plugin), cur_bold(this, plugin), cur_italic(this, plugin), cur_font(this, plugin), cur_caps(this, plugin), cur_under(this, plugin), cur_blink(this, plugin), cur_fixed(this, plugin), cur_super(this, plugin) { bfr = out = plugin->config.wtext; lmt = bfr + plugin->config.wlen; } TitleMain::TitleMain(PluginServer *server) : PluginVClient(server) { text_mask = 0; stroke_mask = 0; outline_mask = 0; glyph_engine = 0; title_engine = 0; translate = 0; outline_engine = 0; freetype_library = 0; freetype_face = 0; text_font[0] = 0; window_w = window_h = 0; title_x = title_y = 0; title_w = title_h = 0; text_w = text_h = 0; text_x = text_y = 0; mask_w = mask_h = 0; mask_x1 = mask_y1 = mask_x2 = mask_y2 = 0; text_rows = 0; visible_row1 = visible_char1 = 0; visible_row2 = visible_char2 = 0; fade = 1; input = 0; output = 0; output_model = BC_RGBA8888; mask_model = BC_RGBA8888; background = 0; bg_file = 0; bg_frame = 0; render_engine = 0; video_cache = 0; overlay_frame = 0; cpus = PluginClient::smp + 1; if( cpus > 8 ) cpus = 8; last_position = -1; need_reconfigure = 1; } TitleMain::~TitleMain() { if( background ) { background->Garbage::remove_user(); background = 0; } delete render_engine; delete video_cache; delete overlay_frame; delete bg_file; delete bg_frame; delete text_mask; delete outline_mask; delete stroke_mask; delete glyph_engine; delete title_engine; if( freetype_face ) FT_Done_Face(freetype_face); if( freetype_library ) FT_Done_FreeType(freetype_library); delete translate; delete outline_engine; } const char* TitleMain::plugin_title() { return _("Title"); } int TitleMain::is_realtime() { return 1; } int TitleMain::is_synthesis() { return 1; } NEW_WINDOW_MACRO(TitleMain, TitleWindow); void TitleMain::build_previews(TitleWindow *gui) { ArrayList&fonts = *gui->get_resources()->fontlist; for( int font_number=0; font_numberimage ) return; } // create example bitmaps FT_Library freetype_library = 0; // Freetype library FT_Face freetype_face = 0; const char *test_string = "Aa"; char new_path[BCTEXTLEN]; int text_height = gui->get_text_height(LARGEFONT); int max_height = 3*text_height/2, max_width = 2 * max_height; int text_color = BC_WindowBase::get_resources()->default_text_color; int r = (text_color >> 16) & 0xff; int g = (text_color >> 8) & 0xff; int b = text_color & 0xff; // dimensions for each line int height[fonts.size()]; int ascent[fonts.size()]; // pass 1 gets the extents for all the fonts // pass 2 draws the image int total_w = 0; int total_h = 0; for( int pass=0; pass<2; ++pass ) { //printf("TitleMain::build_previews %d %d %d\n", //__LINE__, text_height, total_h); for( int font_number=0; font_numberdisplayname, font->displayname) ) { if( pass == 1 ) { font->image = fonts[i]->image; } skip = 1; break; } } if( skip ) continue; if( pass > 0 ) { font->image = new VFrame; font->image->set_use_shm(0); font->image->reallocate(0, -1, 0, 0, 0, total_w, total_h, BC_RGBA8888, -1); font->image->clear_frame(); } int current_w = 1, current_h = 1; int current_x = 0, current_ascent = 0; int len = strlen(test_string); for( int j=0; jpath, // c, // (char *)new_path); strcpy(new_path, font->path); if( load_freetype_face(freetype_library, freetype_face, new_path)) continue; FT_Set_Pixel_Sizes(freetype_face, text_height, 0); if( FT_Load_Char(freetype_face, c, FT_LOAD_RENDER) ) continue; int glyph_w = freetype_face->glyph->bitmap.width; int glyph_h = freetype_face->glyph->bitmap.rows; if( glyph_h > max_height ) glyph_h = max_height; int glyph_a = freetype_face->glyph->advance.x >> 6; int glyph_t = freetype_face->glyph->bitmap_top; if( pass == 0 ) { current_w = current_x + glyph_w; if( current_w > max_width ) current_w = max_width; if( total_w < current_w ) total_w = current_w; if( current_ascent < glyph_t ) current_ascent = glyph_t; if( current_h < glyph_h ) current_h = glyph_h; if( total_h < glyph_h ) total_h = glyph_h; } else { // copy 1 row at a time, center vertically int out_y = (total_h-height[font_number])/2 + ascent[font_number]-glyph_t; if( out_y < 0 ) out_y = 0; for( int in_y = 0; in_y < glyph_h && out_y < total_h; ++in_y, ++out_y ) { unsigned char *in_row = freetype_face->glyph->bitmap.buffer + freetype_face->glyph->bitmap.pitch * in_y; int out_x = current_x; unsigned char *out_row = font->image->get_rows()[out_y] + out_x * 4; for( int in_x = 0; in_x < glyph_w && out_x < total_w; ++in_x, ++out_x ) { *out_row = (*in_row * r + (0xff - *in_row) * *out_row) / 0xff; ++out_row; *out_row = (*in_row * g + (0xff - *in_row) * *out_row) / 0xff; ++out_row; *out_row = (*in_row * b + (0xff - *in_row) * *out_row) / 0xff; ++out_row; *out_row = MAX(*in_row, *out_row); ++out_row; in_row++; } } } current_x += glyph_a; } height[font_number] = current_h; ascent[font_number] = current_ascent; } } if( freetype_library ) FT_Done_FreeType(freetype_library); } //This checks if char_code is on the selected font, else it changes font to the first compatible //Akirad int TitleMain::check_char_code_path(FT_Library &freetype_library, char *path_old, FT_ULong &char_code, char *path_new) { FT_Face temp_freetype_face; FcPattern *pat; FcFontSet *fs; FcObjectSet *os; FcChar8 *file, *format; FcConfig *config; FcPattern *font; FcInit(); config = FcConfigGetCurrent(); FcConfigSetRescanInterval(config, 0); pat = FcPatternCreate(); os = FcObjectSetBuild ( FC_FILE, FC_FONTFORMAT, (char *) 0); fs = FcFontList(config, pat, os); int found = 0; char tmpstring[BCTEXTLEN]; int limit_to_truetype = 0; //if you want to limit search to truetype put 1 if( !freetype_library ) FT_Init_FreeType(&freetype_library); if( !FT_New_Face(freetype_library, path_old, 0, &temp_freetype_face) ) { FT_Set_Pixel_Sizes(temp_freetype_face, 128, 0); int gindex = FT_Get_Char_Index(temp_freetype_face, char_code); if( gindex != 0 && char_code == 10 ) { strcpy(path_new, path_old); found = 1; } } if( !found ) { for( int i=0; fs && infont; ++i ) { font = fs->fonts[i]; FcPatternGetString(font, FC_FONTFORMAT, 0, &format); if( strcmp((char *)format, "TrueType") && !limit_to_truetype ) continue; if( FcPatternGetString(font, FC_FILE, 0, &file) != FcResultMatch ) continue; sprintf(tmpstring, "%s", file); if( !FT_New_Face(freetype_library, tmpstring, 0, &temp_freetype_face) ) continue; FT_Set_Pixel_Sizes(temp_freetype_face, 128, 0); int gindex = FT_Get_Char_Index(temp_freetype_face, char_code); if( gindex != 0 && char_code == 10 ) { sprintf(path_new, "%s", tmpstring); found = 1; goto done; } } } done: if( fs ) FcFontSetDestroy(fs); if( temp_freetype_face ) FT_Done_Face(temp_freetype_face); temp_freetype_face = 0; if( !found ) { strcpy(path_new, path_old); return 1; } return 0; } int TitleMain::load_freetype_face(FT_Library &freetype_library, FT_Face &freetype_face, const char *path) { //printf("TitleMain::load_freetype_face 1\n"); if( !freetype_library ) FT_Init_FreeType(&freetype_library); if( freetype_face ) FT_Done_Face(freetype_face); freetype_face = 0; //printf("TitleMain::load_freetype_face 2\n"); // Use freetype's internal function for loading font if( FT_New_Face(freetype_library, path, 0, &freetype_face) ) { fprintf(stderr, _("TitleMain::load_freetype_face %s failed.\n"), path); freetype_face = 0; freetype_library = 0; return 1; } return 0; } int TitleMain::load_font(BC_FontEntry *font) { if( load_freetype_face(freetype_library,freetype_face, font->path) ) return 1; strcpy(text_font, font->displayname); return 0; } Indexable *TitleMain::open_background(const char *filename) { delete render_engine; render_engine = 0; delete video_cache; video_cache = 0; delete bg_file; bg_file = new File; Asset *asset = new Asset(filename); int result = bg_file->open_file(server->preferences, asset, 1, 0); if( result == FILE_OK ) return (Indexable *)asset; // not asset asset->Garbage::remove_user(); delete bg_file; bg_file = 0; if( result != FILE_IS_XML ) return 0; // nested edl FileXML xml_file; if( xml_file.read_from_file(filename) ) return 0; EDL *nested_edl = new EDL; nested_edl->create_objects(); nested_edl->set_path(filename); nested_edl->load_xml(&xml_file, LOAD_ALL); TransportCommand command; //command.command = audio_tracks ? NORMAL_FWD : CURRENT_FRAME; command.command = CURRENT_FRAME; command.get_edl()->copy_all(nested_edl); command.change_type = CHANGE_ALL; command.realtime = 0; render_engine = new RenderEngine(0, server->preferences, 0, 0); render_engine->set_vcache(video_cache = new CICache(server->preferences)); render_engine->arm_command(&command); return (Indexable *)nested_edl; } int TitleMain::read_background(VFrame *frame, int64_t position, int color_model) { int result = 1; VFrame *iframe = frame; int bw = background->get_w(), bh = background->get_h(); if( background->is_asset ) { Asset *asset = (Asset *)background; if( bw != asset->width || bh != asset->height ) iframe = new_temp(asset->width, asset->height, color_model); int64_t source_position = (int64_t)(position * asset->frame_rate / project_frame_rate); if( config.loop_playback ) { int64_t loop_size = asset->get_video_frames(); source_position -= (int64_t)(source_position / loop_size) * loop_size; } if( bg_file ) { bg_file->set_video_position(source_position, 0); result = bg_file->read_frame(iframe); } } else { EDL *nested_edl = (EDL *)background; if( color_model != nested_edl->session->color_model || bw != nested_edl->session->output_w || bh != nested_edl->session->output_h ) iframe = new_temp( nested_edl->session->output_w, nested_edl->session->output_h, nested_edl->session->color_model); int64_t source_position = (int64_t)(position * nested_edl->session->frame_rate / project_frame_rate); if( config.loop_playback ) { int64_t loop_size = nested_edl->get_video_frames(); source_position -= (int64_t)(source_position / loop_size) * loop_size; } if( render_engine->vrender ) result = render_engine->vrender->process_buffer(iframe, source_position, 0); } if( !result && iframe != frame ) frame->transfer_from(iframe); return result; } void TitleMain::draw_background() { if( background ) { if( strcmp(background->path, config.background_path) ) { if( background ) { background->Garbage::remove_user(); background = 0; } if( bg_file ) { delete bg_file; bg_file = 0; } } } if( !background && config.background_path[0] && !access(config.background_path,R_OK) ) background = open_background(config.background_path); if( background ) { int bw = background->get_w(), bh = background->get_h(); if( bg_frame && (bg_frame->get_w() != bw || bg_frame->get_h() != bh || bg_frame->get_color_model() != output_model) ) { delete bg_frame; bg_frame = 0; } if( !bg_frame ) bg_frame = new VFrame(0, -1, bw, bh, output_model, -1); int64_t position = get_source_position() - get_source_start(); if( !read_background(bg_frame, position, output_model) ) { if( !overlay_frame ) overlay_frame = new OverlayFrame(cpus); float in_x1 = 0, in_x2 = bg_frame->get_w(); float in_y1 = 0, in_y2 = bg_frame->get_h(); float out_x1 = config.title_x, out_x2 = out_x1 + title_w; float out_y1 = config.title_y, out_y2 = out_y1 + title_h; overlay_frame->overlay(output, bg_frame, in_x1,in_y1, in_x2,in_y2, out_x1,out_y1, out_x2,out_y2, 1, TRANSFER_NORMAL, CUBIC_LINEAR); } } } BC_FontEntry* TitleMain::get_font(const char *font_name, int style) { if( !strcmp("fixed", font_name) ) font_name = FIXED_FONT; int flavor = ((style & BC_FONT_ITALIC) != 0 ? FL_SLANT_ITALIC | FL_SLANT_OBLIQUE : FL_SLANT_ROMAN) | ((style & BC_FONT_BOLD) != 0 ? FL_WEIGHT_BOLD | FL_WEIGHT_DEMIBOLD | FL_WEIGHT_EXTRABOLD| FL_WEIGHT_BLACK | FL_WEIGHT_EXTRABLACK : FL_WEIGHT_BOOK | FL_WEIGHT_NORMAL | FL_WEIGHT_MEDIUM | FL_WEIGHT_LIGHT | FL_WEIGHT_EXTRALIGHT | FL_WEIGHT_THIN); int mask = FL_WEIGHT_MASK | FL_SLANT_MASK; BC_Resources *resources = BC_WindowBase::get_resources(); return resources->find_fontentry(font_name, flavor, mask, style); } BC_FontEntry* TitleMain::config_font() { BC_FontEntry *font = get_font(config.font, config.style); if( font && load_font(font) ) font = get_font(FIXED_FONT,0); return font; } static inline bool is_ltr(wchar_t wch) { return (wch>='a' && wch<='z') || (wch>='A' && wch<='Z'); } static inline bool is_nbr(wchar_t wch) { return (wch>='0' && wch<='9'); } static inline bool is_ws(wchar_t wch) { return wch==' ' || wch=='\t'; } static inline bool is_idch(wchar_t wch) { return is_ltr(wch) || is_nbr(wch) || wch=='_'; } // return eof=-1, chr=0, opener=1, closer=2 int TitleParser::wget(wchar_t &wch) { char *ip = id, *tp = text; *ip = 0; *tp = 0; int ilen = sizeof(id), tlen = sizeof(text), ich; while( (ich=wnext()) >= 0 ) { if( ich == '\\' ) { if( (ich=wnext()) == '\n' ) continue; wch = ich; return 0; } if( ich == '<' ) break; if( ich != '#' ) { wch = ich; return 0; } while( (ich=wnext()) >= 0 && ich != '\n' ); if( ich < 0 ) break; } if( ich < 0 ) return -1; int ret = 1; long pos = tell(); if( (ich=wnext()) == '/' ) { ret = 2; ich = wnext(); } if( is_ltr(ich) ) { *ip++ = ich; while( is_idch(ich=wnext()) ) if( --ilen > 0 ) *ip++ = ich; } *ip = 0; while( is_ws(ich) ) ich = wnext(); while( ich >= 0 && ich != '>' ) { if( ich == '\n' || ich == '<' ) { ich = -1; break; } if( ich == '\\' && (ich=wnext()) < 0 ) break; if( --tlen > 0 ) *tp++ = ich; ich = wnext(); } *tp = 0; if( ich < 0 ) { ich = '<'; seek(pos); ret = 0; } wch = ich; return ret; } TitleGlyph *TitleMain::get_glyph(FT_ULong char_code, BC_FontEntry *font, int size, int style) { for( int i=0, n=title_glyphs.count(); ichar_code == char_code && glyph->font == font && glyph->size == size && glyph->style == style ) return glyph; } return 0; } int TitleMain::get_width(TitleGlyph *cur, TitleGlyph *nxt) { if( !cur || cur->char_code == '\n' ) return 0; int result = cur->advance_x; if( !nxt ) return result; FT_Vector kerning; if( !FT_Get_Kerning(freetype_face, cur->freetype_index, nxt->freetype_index, ft_kerning_default, &kerning) ) result += (kerning.x >> 6); return result; } VFrame *TitleMain::get_image(const char *path) { for( int i=0; ipath) ) return title_images[i]->vframe; } return 0; } VFrame *TitleMain::add_image(const char *path) { VFrame *vframe = get_image(path); if( !vframe && (vframe=VFramePng::vframe_png(path)) != 0 ) { if( vframe->get_color_model() != text_model ) { VFrame *frame = new VFrame(vframe->get_w(), vframe->get_h(), text_model); frame->transfer_from(vframe); delete vframe; vframe = frame; } title_images.append(new TitleImage(path, vframe)); } return vframe; } int TitleCurColor::set(const char *txt) { #define BCOLOR(NM) { #NM, NM } static const struct { const char *name; int color; } colors[] = { BCOLOR(MNBLUE), BCOLOR(ORANGE), BCOLOR(BLOND), BCOLOR(MNGREY), BCOLOR(FGGREY), BCOLOR(FTGREY), BCOLOR(DKGREY), BCOLOR(LTGREY), BCOLOR(MEGREY), BCOLOR(DMGREY), BCOLOR(MDGREY), BCOLOR(LTPURPLE),BCOLOR(MEPURPLE),BCOLOR(MDPURPLE), BCOLOR(DKPURPLE), BCOLOR(LTCYAN), BCOLOR(MECYAN), BCOLOR(MDCYAN), BCOLOR(DKCYAN), BCOLOR(YELLOW), BCOLOR(LTYELLOW),BCOLOR(MEYELLOW), BCOLOR(MDYELLOW), BCOLOR(LTGREEN), BCOLOR(DKGREEN), BCOLOR(DKYELLOW), BCOLOR(LTPINK), BCOLOR(PINK), BCOLOR(LTBLUE), BCOLOR(DKBLUE), BCOLOR(RED), BCOLOR(GREEN), BCOLOR(BLUE), BCOLOR(BLACK), BCOLOR(WHITE), }; int color; if( *txt ) { if( txt[0] == '#' ) { if( sscanf(&txt[1], "%x", &color) != 1 ) return 1; } else { int i = sizeof(colors)/sizeof(colors[0]); while( --i >= 0 && strcasecmp(txt, colors[i].name) ); if( i < 0 ) return 1; color = colors[i].color; } } else color = parser->plugin->config.color; push(color); return 0; } int TitleCurAlpha::set(const char *txt) { float a = !*txt ? parser->plugin->config.alpha : strtof(txt,(char**)&txt) * 255; int alpha = a; if( *txt || alpha < 0 || alpha > 255 ) return 1; push(alpha); return 0; } int TitleCurSize::set(const char *txt) { float size = 0; if( !*txt ) { size = parser->plugin->config.size; } else if( *txt == '+' || *txt == '-' ) { size = *this; for( int ch; (ch=*txt)!=0; ++txt ) { if( ch == '+' ) { size *= 5./4.; continue; } if( ch == '-' ) { size *= 4./5.; continue; } return 1; } } else { size = strtof(txt,(char**)&txt); } if( *txt || size <= 0 || size > 2048 ) return 1; int style = parser->cur_font.style(); if( !parser->cur_font.set(0,style) ) return 1; push(size); return 0; } int TitleCurSize::unset(const char *txt) { if( pop() ) return 1; int style = parser->cur_font.style(); parser->cur_font.set(0,style); return 0; } int TitleCurBold::set(const char *txt) { int bold = !*txt ? 1 : strtol(txt,(char**)&txt,0); if( *txt || bold < 0 || bold > 1 ) return 1; int style = parser->cur_font.style(); if( bold ) style |= BC_FONT_BOLD; else style &= ~BC_FONT_BOLD; if( !parser->cur_font.set(0,style) ) return 1; push(bold); return 0; } int TitleCurBold::unset(const char *txt) { if( pop() ) return 1; int style = parser->cur_font.style(); parser->cur_font.set(0,style); return 0; } int TitleCurItalic::set(const char *txt) { int italic = !*txt ? 1 : strtol(txt,(char**)&txt,0); if( *txt || italic < 0 || italic > 1 ) return 1; int style = parser->cur_font.style(); if( italic ) style |= BC_FONT_ITALIC; else style &= ~BC_FONT_ITALIC; if( !parser->cur_font.set(0,style) ) return 1; push(italic); return 0; } int TitleCurItalic::unset(const char *txt) { if( pop() ) return 1; int style = parser->cur_font.style(); parser->cur_font.set(0,style); return 0; } int TitleCurFont::style() { int style = 0; if( parser->cur_bold ) style |= BC_FONT_BOLD; if( parser->cur_italic ) style |= BC_FONT_ITALIC; return style; } BC_FontEntry* TitleCurFont::get(const char *txt, int style) { if( !txt ) txt = parser->plugin->text_font; else if( !*txt ) txt = parser->plugin->config.font; return parser->plugin->get_font(txt, style); } BC_FontEntry *TitleCurFont::set(const char *txt, int style) { BC_FontEntry *font = get(txt, style); if( !font || parser->plugin->load_font(font) ) return 0; if( !txt ) (BC_FontEntry*&)*this = font; return font; } int TitleCurFont::set(const char *txt) { BC_FontEntry *font = set(txt, style()); if( !font ) return 1; push(font); return 0; } int TitleCurFont::unset(const char *txt) { if( *txt || pop() ) return 1; BC_FontEntry *font = *this; if( !font ) return 1; font = get(font->displayname, style()); if( !font ) return 1; (BC_FontEntry*&)*this = font; return 0; } int TitleCurCaps::set(const char *txt) { int caps = !*txt ? 1 : strtol(txt,(char **)&txt,0); if( *txt || caps < -1 || caps > 1 ) return 1; push(caps); return 0; } int TitleCurBlink::set(const char *txt) { float blink = !*txt ? 1 : strtof(txt,(char **)&txt); if( *txt ) return 1; push(blink); return 0; } int TitleCurFixed::set(const char *txt) { int fixed = !*txt ? parser->cur_size*3/4 : strtol(txt,(char **)&txt,0); if( *txt || fixed < 0 ) return 1; push(fixed); return 0; } int TitleCurSuper::set(const char *txt) { int super = !*txt ? 1 : strtol(txt,(char **)&txt,0); if( *txt || super < -1 || super > 1 ) return 1; push(super); return 0; } int TitleCurNudge::set(const char *txt) { if( !*txt ) return 1; short nx = strtol(txt,(char **)&txt,0); if( *txt++ != ',' ) return 1; short ny = strtol(txt,(char **)&txt,0); if( *txt ) return 1; int nudge = (nx << 16) | (ny & 0xffff); push(nudge); return 0; } int TitleParser::set_attributes(int ret) { if( !strcmp(id,_("nudge")) ) return ret>1 ? cur_nudge.unset(text) : cur_nudge.set(text); if( !strcmp(id,_("color")) ) return ret>1 ? cur_color.unset(text) : cur_color.set(text); if( !strcmp(id,_("alpha")) ) return ret>1 ? cur_alpha.unset(text) : cur_alpha.set(text); if( !strcmp(id,_("font")) ) return ret>1 ? cur_font.unset(text) : cur_font.set(text); if( !strcmp(id,_("size")) ) return ret>1 ? cur_size.unset(text) : cur_size.set(text); if( !strcmp(id,_("bold")) ) return ret>1 ? cur_bold.unset(text) : cur_bold.set(text); if( !strcmp(id,_("italic")) ) return ret>1 ? cur_italic.unset(text) : cur_italic.set(text); if( !strcmp(id,_("caps")) ) return ret>1 ? cur_caps.unset(text) : cur_caps.set(text); if( !strcmp(id,_("ul")) ) return ret>1 ? cur_under.unset(text) : cur_under.set(text); if( !strcmp(id,_("blink")) ) return ret>1 ? cur_blink.unset(text) : cur_blink.set(text); if( !strcmp(id,_("fixed")) ) return ret>1 ? cur_fixed.unset(text) : cur_fixed.set(text); if( !strcmp(id,_("sup")) ) return ret>1 ? cur_super.unset(text) : cur_super.set(text); return 1; } void TitleMain::load_glyphs() { // Build table of all glyphs needed TitleParser wchrs(this); int total_packages = 0; while( !wchrs.eof() ) { wchar_t wch1 = wchrs.wcur(), wch; long ipos = wchrs.tell(); int ret = wchrs.wget(wch); if( ret > 0 ) { if( !wchrs.set_attributes(ret) ) continue; if( !strcmp(wchrs.id,"png") && add_image(wchrs.text) ) continue; wch = wch1; wchrs.seek(ipos+1); ret = 0; } if( !ret ) { int cur_caps = wchrs.cur_caps; if( cur_caps > 0 ) wch = towupper(wch); else if( cur_caps < 0 ) wch = towlower(wch); BC_FontEntry *cur_font = wchrs.cur_font; int cur_size = wchrs.cur_size; int cur_style = 0; int cur_bold = wchrs.cur_bold; if( cur_bold ) cur_style |= BC_FONT_BOLD; int cur_italic = wchrs.cur_italic; if( cur_italic ) cur_style |= BC_FONT_ITALIC; int cur_super = wchrs.cur_super; if( cur_super ) cur_size /= 2; int exists = 0; for( int j=0; jchar_code == (FT_ULong)wch && glyph->font == cur_font && glyph->size == cur_size && glyph->style == cur_style ) { exists = 1; break; } } if( !exists && cur_font ) { total_packages++; TitleGlyph *glyph = new TitleGlyph; glyph->char_code = (FT_ULong)wch; glyph->font = cur_font; glyph->size = cur_size; glyph->style = cur_style; title_glyphs.append(glyph); } } } if( !glyph_engine ) glyph_engine = new GlyphEngine(this, cpus); glyph_engine->set_package_count(total_packages); glyph_engine->process_packages(); } TitleImage::TitleImage(const char *path, VFrame *vfrm) { this->path = cstrdup(path); this->vframe = vfrm; } TitleImage::~TitleImage() { delete [] path; delete vframe; } TitleChar *TitleChar::init(int typ, void *vp) { wch = 0; flags = 0; this->typ = typ; this->vp = vp; x = y = 0; row = dx = 0; blink = 0.; size = 0.; color = BLACK; alpha = 0xff; fade = 1; return this; } TitleRow *TitleRow::init() { x0 = y0 = 0; x1 = y2 = 0; //MAX_FLT; y1 = x2 = 0; //MIN_FLT; return this; } int TitleMain::get_text() { // Determine extents of total text title_chars.reset(); title_rows.reset(); int pitch = config.line_pitch; float font_h = config.size; int descent = 0; TitleParser wchrs(this); TitleRow *row = 0; float row_w = 0, row_h = 0; float row_x = 0, row_y = 0; text_rows = 0; for(;;) { if( !row ) row = title_rows.add(); TitleChar *chr = 0; long ipos = wchrs.tell(); wchar_t wch1 = wchrs.wcur(), wch; int ret = wchrs.wget(wch); if( ret < 0 || wch == '\n' ) { if( row->x1 > row->x2 ) row->x1 = row->x2 = 0; if( row->y2 > row->y1 ) row->y1 = row->y2 = 0; int dy = row->y1 - descent; row_y += pitch ? pitch : dy > font_h ? dy : font_h; row->y0 = row_y; descent = row->y2; if( row_x > row_w ) row_w = row_x; if( row_y > row_h ) row_h = row_y; text_rows = title_rows.count(); row_x = 0; row = 0; if( ret < 0 ) break; continue; } BC_FontEntry *cur_font = wchrs.cur_font; int cur_color = wchrs.cur_color; int cur_alpha = wchrs.cur_alpha; float cur_size = wchrs.cur_size; int cur_caps = wchrs.cur_caps; int cur_under = wchrs.cur_under; float cur_blink = wchrs.cur_blink; int cur_fixed = wchrs.cur_fixed; int cur_super = wchrs.cur_super; int cur_nudge = wchrs.cur_nudge; int cur_style = 0; int cur_bold = wchrs.cur_bold; if( cur_bold ) cur_style |= BC_FONT_BOLD; int cur_italic = wchrs.cur_italic; if( cur_italic ) cur_style |= BC_FONT_ITALIC; short nx = cur_nudge >> 16, ny = cur_nudge; int cx = nx, cy = ny, cw = 0, ch = 0, dx = 0; if( ret > 0 ) { if( !wchrs.set_attributes(ret) ) continue; ret = -1; if( !strcmp(wchrs.id,"png") ) { VFrame *png_image = get_image(wchrs.text); if( png_image ) { chr = title_chars.add(CHAR_IMAGE, png_image); cy += ch = png_image->get_h(); cw = dx = png_image->get_w(); wch = 0; ret = 0; } } if( ret < 0 ) { wch = wch1; wchrs.seek(ipos+1); } } if( wch ) { if( cur_caps > 0 ) wch = towupper(wch); else if( cur_caps < 0 ) wch = towlower(wch); int size = !cur_super ? cur_size : cur_size/2; TitleGlyph *gp = get_glyph(wch, cur_font, size, cur_style); if( !gp ) continue; if( cur_super > 0 ) cy += cur_size-4*size/3; else if( cur_super < 0 ) cy -= size/3; dx = gp->advance_x; if( cur_fixed ) dx = cur_fixed; else if( !wchrs.eof() ) { TitleGlyph *np = get_glyph(wchrs.wcur(), cur_font, cur_size, cur_style); dx = get_width(gp, np); } chr = title_chars.add(CHAR_GLYPH, gp); cx += gp->left; cy += gp->top; cw = gp->right - gp->left; ch = gp->top - gp->bottom; } if( !chr ) continue; chr->wch = wch; chr->size = cur_size; chr->blink = cur_blink; chr->color = cur_color; chr->alpha = cur_alpha; chr->fade = 1; if( cur_fixed ) chr->flags |= FLAG_FIXED; if( cur_super > 0 ) chr->flags |= FLAG_SUPER; if( cur_super < 0 ) chr->flags |= FLAG_SUBER; if( cur_under ) chr->flags |= FLAG_UNDER; if( cur_blink ) chr->flags |= FLAG_BLINK; chr->x = (cx += row_x); chr->y = cy; chr->dx = dx; row->bound(cx,cy, cx+cw,cy-ch); chr->row = text_rows; row_x += chr->dx; } if( !row_w || !row_h ) return 1; // rows boundary, note: row->xy is y up (FT), row_xy is y down (X) text_x1 = text_y1 = MAX_FLT; text_x2 = text_y2 = MIN_FLT; for( int i=0; ix0 = 0; break; case JUSTIFY_CENTER: row->x0 = (row_w - (row->x2-row->x1)) / 2; break; case JUSTIFY_RIGHT: row->x0 = row_w - (row->x2-row->x1); break; } float v; if( text_x1 > (v=row->x0+row->x1) ) text_x1 = v; if( text_y1 > (v=row->y0-row->y1) ) text_y1 = v; if( text_x2 < (v=row->x0+row->x2) ) text_x2 = v; if( text_y2 < (v=row->y0-row->y2) ) text_y2 = v; } if( text_x1 > text_x2 || text_y1 > text_y2 ) return 1; text_x1 += fuzz1; text_y1 += fuzz1; text_x2 += fuzz2; text_y2 += fuzz2; text_w = text_x2 - text_x1; text_h = text_y2 - text_y1; return 0; } int TitleMain::get_visible_text() { // Determine y of visible text switch( config.motion_strategy ) { case BOTTOM_TO_TOP: case TOP_TO_BOTTOM: { float magnitude = config.pixels_per_second * (get_source_position() - config.prev_keyframe_position) / PluginVClient::project_frame_rate; if( config.loop ) { int loop_size = text_h + title_h; magnitude -= (int)(magnitude / loop_size) * loop_size; } text_y = config.motion_strategy == BOTTOM_TO_TOP ? title_h - magnitude : magnitude - text_h; break; } default: switch( config.vjustification ) { case JUSTIFY_TOP: text_y = 0; break; case JUSTIFY_MID: text_y = (title_h - text_h) / 2; break; case JUSTIFY_BOTTOM: text_y = title_h - text_h; break; default: break; } } // Determine x of visible text switch( config.motion_strategy ) { case RIGHT_TO_LEFT: case LEFT_TO_RIGHT: { float magnitude = config.pixels_per_second * (get_source_position() - config.prev_keyframe_position) / PluginVClient::project_frame_rate; if( config.loop ) { int loop_size = text_w + title_w; magnitude -= (int)(magnitude / loop_size) * loop_size; } text_x = config.motion_strategy == RIGHT_TO_LEFT ? title_w - magnitude : magnitude - text_w; break; } default: switch ( config.hjustification ) { case JUSTIFY_LEFT: text_x = 0; break; case JUSTIFY_CENTER: text_x = (title_w - text_w) / 2; break; case JUSTIFY_RIGHT: text_x = title_w - text_w; break; default: break; } } // until bottom of this row is visible int row1 = 0; int y0 = text_y - text_y1; while( row1 < text_rows ) { TitleRow *row = title_rows[row1]; if( y0 + row->y0-row->y2 > 0 ) break; ++row1; } if( row1 >= text_rows ) return 0; // until top of next row is not visible y0 -= title_h; int row2 = row1; while( row2 < text_rows ) { TitleRow *row = title_rows[row2]; if( y0 + row->y0-row->y1 >= 0 ) break; ++row2; } if( row1 >= row2 ) return 0; //printf("get_visible_rows: visible_row1/2 = %d to %d\n",visible_row1,visible_row2); // Only draw visible chars double frame_rate = PluginVClient::get_project_framerate(); int64_t units = get_source_position() - get_source_start(); double position = units / frame_rate; int blinking = 0; int char1 = -1, char2 = -1, nchars = title_chars.count(); for( int i=0; irow < row1 ) continue; if( chr->row >= row2 ) break; if( char1 < 0 ) char1 = i; char2 = i; if( (chr->flags & FLAG_BLINK) != 0 ) { blinking = 1; double rate = 1 / fabs(chr->blink), rate2 = 2 * rate; double pos = position - ((int64_t)(position / rate2)) * rate2; chr->fade = chr->blink > 0 ? (pos > rate ? 0 : 1) : fabs(rate+pos) / rate; } } if( char1 < 0 ) char1 = 0; ++char2; if( !blinking && visible_row1 == row1 && visible_row2 == row2 ) return 0; // need redraw visible_row1 = row1; visible_row2 = row2; visible_char1 = char1; visible_char2 = char2; return 1; } int TitleMain::draw_text(int need_redraw) { // until top of next row is not visible mask_x1 = mask_y1 = INT_MAX; mask_x2 = mask_y2 = INT_MIN; for( int i=visible_row1; i (v=row->x0+row->x1) ) mask_x1 = v; if( mask_y1 > (v=row->y0-row->y1) ) mask_y1 = v; if( mask_x2 < (v=row->x0+row->x2) ) mask_x2 = v; if( mask_y2 < (v=row->y0-row->y2) ) mask_y2 = v; } if( mask_x1 >= mask_x2 || mask_y1 >= mask_y2 ) return 1; mask_x1 += fuzz1; mask_y1 += fuzz1; mask_x2 += fuzz2; mask_y2 += fuzz2; mask_w = mask_x2 - mask_x1; mask_h = mask_y2 - mask_y1; //printf("TitleMain::draw_text %d-%d frame %dx%d\n", // visible_row1, visible_row2, mask_w,mask_h); if( text_mask && (text_mask->get_color_model() != text_model || text_mask->get_w() != mask_w || text_mask->get_h() != mask_h) ) { delete text_mask; text_mask = 0; delete stroke_mask; stroke_mask = 0; } if( !text_mask ) { // Always use 8 bit because the glyphs are 8 bit // Need to set YUV to get clear_frame to set the right chroma. mask_model = text_model; text_mask = new VFrame; text_mask->set_use_shm(0); text_mask->reallocate(0, -1, 0, 0, 0, mask_w, mask_h, mask_model, -1); int drop = abs(config.dropshadow); int drop_w = mask_w + drop; int drop_h = mask_h + drop; stroke_mask = new VFrame; stroke_mask->set_use_shm(0); stroke_mask->reallocate(0, -1, 0, 0, 0, drop_w, drop_h, mask_model, -1); need_redraw = 1; } // Draw on text mask if it has changed if( need_redraw ) { //printf("redraw %d to %d %d,%d %d,%d - %d,%d\n", visible_char1,visible_char2, // ext_x0, ext_y0, ext_x1,ext_y1, ext_x2,ext_y2); text_mask->clear_frame(); stroke_mask->clear_frame(); #if 0 unsigned char *data = text_mask->get_data(); // draw bbox on text for( int x=0; xdo_dropshadow = 1; title_engine->set_package_count(visible_char2 - visible_char1); title_engine->process_packages(); } // Then draw foreground title_engine->do_dropshadow = 0; title_engine->set_package_count(visible_char2 - visible_char1); title_engine->process_packages(); draw_underline(text_mask, config.alpha); // Convert to text outlines if( config.outline_size > 0 ) { if( outline_mask && (text_mask->get_w() != outline_mask->get_w() || text_mask->get_h() != outline_mask->get_h()) ) { delete outline_mask; outline_mask = 0; } if( !outline_mask ) { outline_mask = new VFrame; outline_mask->set_use_shm(0); outline_mask->reallocate(0, -1, 0, 0, 0, text_mask->get_w(), text_mask->get_h(), text_mask->get_color_model(), -1); } if( !outline_engine ) outline_engine = new TitleOutlineEngine(this, cpus); outline_engine->do_outline(); } } return 0; } int TitleMain::draw_underline(VFrame *mask, int alpha) { int row = -1, sz = -1, color = -1, rgb = -1; int bpp = mask->get_bytes_per_pixel(), bpp1 = bpp-1; int x1 = 0, x2 = 0, y0 = 0; for( int i=visible_char1; iflags & FLAG_UNDER) && row < 0 ) { sz = chr->size; rgb = chr->color; int rr, gg, bb; get_mask_colors(rgb, mask_model, rr, gg, bb); color = (rr<<16) | (gg<<8) | (bb<<0); row = chr->row; TitleRow *rp = title_rows[row]; x1 = rp->x0 + chr->x; y0 = rp->y0; } if( (chr->flags & FLAG_UNDER) && row == chr->row && chr->color == rgb ) { TitleRow *rp = title_rows[row]; x2 = rp->x0 + chr->x + chr->dx; if( sz < chr->size ) sz = chr->size; if( ++i < visible_char2 ) continue; } if( row < 0 ) { ++i; continue; } x1 -= mask_x1; x2 -= mask_x1; y0 -= mask_y1; if( x1 < 0 ) x1 = 0; if( x2 > mask_w ) x2 = mask_w; int sz1 = sz / 32 + 1, sz2 = sz1/2; int y1 = y0 - sz2, y2 = y1 + sz1; if( y1 < 0 ) y1 = 0; if( y2 > mask_h ) y2 = mask_h; unsigned char **rows = mask->get_rows(); for( int y=y1; y=0; ) *bp++ = color>>(8*i); *bp = alpha; } } row = -1; } return 0; } void TitleMain::draw_overlay() { //printf("TitleMain::draw_overlay 1\n"); fade = 1; if( !EQUIV(config.fade_in, 0) ) { int fade_len = lroundf(config.fade_in * PluginVClient::project_frame_rate); int fade_position = get_source_position() - config.prev_keyframe_position; if( fade_position >= 0 && fade_position < fade_len ) { fade = (float)fade_position / fade_len; } } if( !EQUIV(config.fade_out, 0) ) { int fade_len = lroundf(config.fade_out * PluginVClient::project_frame_rate); int fade_position = config.next_keyframe_position - get_source_position(); if( fade_position >= 0 && fade_position < fade_len ) { fade = (float)fade_position / fade_len; } } if( !translate ) translate = new TitleTranslate(this, cpus); if( text_x+mask_w > 0 && text_x < title_w ) { translate->xlat_mask = text_mask; translate->run_packages(); if( config.stroke_width >= SMALL && (config.style & BC_FONT_OUTLINE) ) { translate->xlat_mask = stroke_mask; translate->run_packages(); } } } const char* TitleMain::motion_to_text(int motion) { switch( motion ) { case NO_MOTION: return _("No motion"); case BOTTOM_TO_TOP: return _("Bottom to top"); case TOP_TO_BOTTOM: return _("Top to bottom"); case RIGHT_TO_LEFT: return _("Right to left"); case LEFT_TO_RIGHT: return _("Left to right"); } return _("Unknown"); } int TitleMain::text_to_motion(const char *text) { for( int i=0; iget_w(), ih = output->get_h(); int mr = MIN(iw, ih)/200 + 2, rr = 2*mr; int x = title_x, y = title_y, w = title_w, h = title_h; int r2 = (rr+1)/2; int x0 = x-r2, x1 = x+(w+1)/2, x2 = x+w+r2; int y0 = y-r2, y1 = y+(h+1)/2, y2 = y+h+r2; int st = 1; for( int r=2; r=0; ) { // bbox int lft = x+r, rgt = x+w-1-r; int top = y+r, bot = y+h-1-r; out.draw_line(lft,top, rgt,top); out.draw_line(rgt,top, rgt,bot); out.draw_line(rgt,bot, lft,bot); out.draw_line(lft,bot, lft,top); } for( int r=mr; r=0; ) { // edge arrows out.draw_line(x1-r,y0+r, x1+r,y0+r); out.draw_line(x2-r,y1-r, x2-r,y1+r); out.draw_line(x1-r,y2-r, x1+r,y2-r); out.draw_line(x0+r,y1+r, x0+r,y1-r); } x0 += r2; y0 += r2; x2 -= r2; y2 -= r2; for( int r=2*mr; --r>=0; ) { // corner arrows out.draw_line(x0,y0+r, x0+r,y0); out.draw_line(x2,y0+r, x2-r,y0); out.draw_line(x2,y2-r, x2-r,y2); out.draw_line(x0,y2-r, x0+r,y2); } } int TitleMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr) { int result = 0; input = input_ptr; output = output_ptr; output_model = output->get_color_model(); text_model = BC_CModels::is_yuv(output_model) ? BC_YUVA8888 : BC_RGBA8888; if( text_model != mask_model ) need_reconfigure = 1; need_reconfigure |= load_configuration(); int64_t cur_position = get_source_position(); if( last_position < 0 || last_position+1 != cur_position ) need_reconfigure = 1; last_position = cur_position; title_x = config.title_x; title_y = config.title_y; title_w = config.title_w ? config.title_w : input->get_w(); title_h = config.title_h ? config.title_h : input->get_h(); fuzz1 = -config.outline_size; fuzz2 = config.outline_size; if( config.dropshadow < 0 ) fuzz1 += config.dropshadow; else fuzz2 += config.dropshadow; fuzz = fuzz2 - fuzz1; // Check boundaries if( config.size <= 0 || config.size >= 2048 ) config.size = 72; if( config.stroke_width < 0 || config.stroke_width >= 512 ) config.stroke_width = 0.0; if( !config.wlen && !config.timecode ) return 0; if( !strlen(config.encoding) ) strcpy(config.encoding, DEFAULT_ENCODING); // Always synthesize text and redraw it for timecode if( config.timecode ) { int64_t rendered_frame = get_source_position(); if( get_direction() == PLAY_REVERSE ) rendered_frame -= 1; char text[BCTEXTLEN]; Units::totext(text, (double)rendered_frame / PluginVClient::project_frame_rate, config.timecode_format, PluginVClient::get_project_samplerate(), PluginVClient::get_project_framerate(), 16); config.to_wtext(config.encoding, text, strlen(text)+1); need_reconfigure = 1; } if( config.background ) draw_background(); // Handle reconfiguration if( need_reconfigure ) { need_reconfigure = 0; reset_render(); result = init_freetype(); if( !result ) { load_glyphs(); result = get_text(); } } if( !result ) result = draw_text(get_visible_text()); // Overlay mask on output if( !result ) draw_overlay(); if( config.drag ) draw_boundry(); return 0; } void TitleMain::update_gui() { if( thread ) { int reconfigure = load_configuration(); if( reconfigure ) { TitleWindow *window = (TitleWindow*)thread->window; window->lock_window("TitleMain::update_gui"); window->update(); window->unlock_window(); window->color_thread->update_gui(config.color, 0); } } } int TitleMain::load_configuration() { KeyFrame *prev_keyframe, *next_keyframe; prev_keyframe = get_prev_keyframe(get_source_position()); next_keyframe = get_next_keyframe(get_source_position()); TitleConfig old_config, prev_config, next_config; old_config.copy_from(config); read_data(prev_keyframe); prev_config.copy_from(config); read_data(next_keyframe); next_config.copy_from(config); config.prev_keyframe_position = prev_keyframe->position; config.next_keyframe_position = next_keyframe->position; // if no previous keyframe exists, it should be start of the plugin, not start of the track if( config.next_keyframe_position == config.prev_keyframe_position ) config.next_keyframe_position = get_source_start() + get_total_len(); if( config.prev_keyframe_position == 0 ) config.prev_keyframe_position = get_source_start(); // printf("TitleMain::load_configuration 10 %d %d\n", // config.prev_keyframe_position, // config.next_keyframe_position); config.interpolate(prev_config, next_config, (next_keyframe->position == prev_keyframe->position) ? get_source_position() : prev_keyframe->position, (next_keyframe->position == prev_keyframe->position) ? get_source_position() + 1 : next_keyframe->position, get_source_position()); if( !config.equivalent(old_config) ) return 1; return 0; } void TitleMain::save_data(KeyFrame *keyframe) { FileXML output; output.set_shared_output(keyframe->get_data(), MESSAGESIZE); output.tag.set_title("TITLE"); output.tag.set_property("FONT", config.font); output.tag.set_property("ENCODING", config.encoding); output.tag.set_property("STYLE", (int64_t)config.style); output.tag.set_property("SIZE", config.size); output.tag.set_property("COLOR", config.color); output.tag.set_property("ALPHA", config.alpha); output.tag.set_property("OUTLINE_SIZE", config.outline_size); output.tag.set_property("OUTLINE_COLOR", config.outline_color); output.tag.set_property("OUTLINE_ALPHA", config.outline_alpha); output.tag.set_property("COLOR_STROKE", config.color_stroke); output.tag.set_property("STROKE_WIDTH", config.stroke_width); output.tag.set_property("MOTION_STRATEGY", config.motion_strategy); output.tag.set_property("LOOP", config.loop); output.tag.set_property("LINE_PITCH", config.line_pitch); output.tag.set_property("PIXELS_PER_SECOND", config.pixels_per_second); output.tag.set_property("HJUSTIFICATION", config.hjustification); output.tag.set_property("VJUSTIFICATION", config.vjustification); output.tag.set_property("FADE_IN", config.fade_in); output.tag.set_property("FADE_OUT", config.fade_out); output.tag.set_property("TITLE_X", config.title_x); output.tag.set_property("TITLE_Y", config.title_y); output.tag.set_property("TITLE_W", config.title_w); output.tag.set_property("TITLE_H", config.title_h); output.tag.set_property("DROPSHADOW", config.dropshadow); output.tag.set_property("TIMECODE", config.timecode); output.tag.set_property("TIMECODEFORMAT", config.timecode_format); output.tag.set_property("WINDOW_W", config.window_w); output.tag.set_property("WINDOW_H", config.window_h); output.tag.set_property("DRAG", config.drag); output.tag.set_property("BACKGROUND", config.background); output.tag.set_property("BACKGROUND_PATH", config.background_path); output.tag.set_property("LOOP_PLAYBACK", config.loop_playback); output.append_tag(); output.append_newline(); char text[BCTEXTLEN]; int text_len = BC_Resources::encode( BC_Resources::wide_encoding, DEFAULT_ENCODING, (char*)config.wtext, config.wlen*sizeof(wchar_t), text, sizeof(text)); output.append_text(text, text_len); output.tag.set_title("/TITLE"); output.append_tag(); output.append_newline(); output.terminate_string(); //printf("TitleMain::save_data 1\n%s\n", output.string); //printf("TitleMain::save_data 2\n%s\n", config.text); } void TitleMain::read_data(KeyFrame *keyframe) { FileXML input; input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data())); int result = 0; config.prev_keyframe_position = keyframe->position; while(!result) { result = input.read_tag(); if( result ) break; if( input.tag.title_is("TITLE") ) { input.tag.get_property("FONT", config.font); input.tag.get_property("ENCODING", config.encoding); config.style = input.tag.get_property("STYLE", (int64_t)config.style); config.size = input.tag.get_property("SIZE", config.size); config.color = input.tag.get_property("COLOR", config.color); config.alpha = input.tag.get_property("ALPHA", config.alpha); config.outline_size = input.tag.get_property("OUTLINE_SIZE", config.outline_size); config.outline_color = input.tag.get_property("OUTLINE_COLOR", config.outline_color); config.outline_alpha = input.tag.get_property("OUTLINE_ALPHA", config.outline_alpha); config.color_stroke = input.tag.get_property("COLOR_STROKE", config.color_stroke); config.stroke_width = input.tag.get_property("STROKE_WIDTH", config.stroke_width); config.motion_strategy = input.tag.get_property("MOTION_STRATEGY", config.motion_strategy); config.loop = input.tag.get_property("LOOP", config.loop); config.line_pitch = input.tag.get_property("LINE_PITCH", config.line_pitch); config.pixels_per_second = input.tag.get_property("PIXELS_PER_SECOND", config.pixels_per_second); config.hjustification = input.tag.get_property("HJUSTIFICATION", config.hjustification); config.vjustification = input.tag.get_property("VJUSTIFICATION", config.vjustification); config.fade_in = input.tag.get_property("FADE_IN", config.fade_in); config.fade_out = input.tag.get_property("FADE_OUT", config.fade_out); config.title_x = input.tag.get_property("TITLE_X", config.title_x); config.title_y = input.tag.get_property("TITLE_Y", config.title_y); config.title_w = input.tag.get_property("TITLE_W", config.title_w); config.title_h = input.tag.get_property("TITLE_H", config.title_h); config.dropshadow = input.tag.get_property("DROPSHADOW", config.dropshadow); config.timecode = input.tag.get_property("TIMECODE", config.timecode); config.timecode_format = input.tag.get_property("TIMECODEFORMAT", config.timecode_format); config.window_w = input.tag.get_property("WINDOW_W", config.window_w); config.window_h = input.tag.get_property("WINDOW_H", config.window_h); config.drag = input.tag.get_property("DRAG", config.drag); config.background = input.tag.get_property("BACKGROUND", config.background); input.tag.get_property("BACKGROUND_PATH", config.background_path); config.loop_playback = input.tag.get_property("LOOP_PLAYBACK", config.loop_playback); const char *text = input.read_text(); config.to_wtext(config.encoding, text, strlen(text)+1); } else if( input.tag.title_is("/TITLE") ) { result = 1; } } } void TitleMain::insert_text(const char *txt, int pos) { int ilen = strlen(txt); 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+ilen-1; i>=pos; --i,--j ) { if( j >= wsize ) continue; wtext[j] = wtext[i]; } for( int i=pos, j=0; j= wsize ) break; wtext[i] = txt[j]; } if( (wlen+=ilen) > wsize ) wlen = wsize; wtext[wlen] = 0; config.wlen = wlen; }