4 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 // Originally developed by Heroine Virtual Ltd.
23 // Support for multiple encodings, outline (stroke) by
24 // Andraz Tori <Andraz.tori1@guest.arnes.si>
25 // Additional support for UTF-8 by
26 // Paolo Rampino aka Akirad <info at tuttoainternet.it>
31 #include "bcsignals.h"
33 #include "colormodels.h"
35 #include "filesystem.h"
36 #include "transportque.inc"
43 #include "mwindow.inc"
46 #include "titlewindow.h"
47 #include "transportque.inc"
58 #include <fontconfig/fontconfig.h>
60 #define ZERO (1.0 / 64.0)
62 REGISTER_PLUGIN(TitleMain)
64 #ifdef X_HAVE_UTF8_STRING
65 #define DEFAULT_ENCODING "UTF-8"
67 #define DEFAULT_ENCODING "ISO8859-1"
69 #define DEFAULT_TIMECODEFORMAT TIME_HMS
73 TitleConfig::TitleConfig()
80 motion_strategy = NO_MOTION;
83 hjustification = JUSTIFY_CENTER;
84 vjustification = JUSTIFY_MID;
90 sprintf(font, "fixed");
91 sprintf(encoding, DEFAULT_ENCODING);
92 timecode_format = DEFAULT_TIMECODEFORMAT;
93 pixels_per_second = 1.0;
96 wtext[0] = 0; wlen = 0;
97 color_stroke = 0xff0000;
98 outline_color = WHITE;
103 next_keyframe_position = 0;
104 prev_keyframe_position = 0;
107 TitleConfig::~TitleConfig()
111 // Does not test equivalency but determines if redrawing text is necessary.
112 int TitleConfig::equivalent(TitleConfig &that)
114 return dropshadow == that.dropshadow &&
115 style == that.style &&
117 color == that.color &&
118 color_stroke == that.color_stroke &&
119 stroke_width == that.stroke_width &&
120 outline_color == that.outline_color &&
121 alpha == that.alpha &&
122 outline_alpha == that.outline_alpha &&
123 timecode == that.timecode &&
124 timecode_format == that.timecode_format &&
125 line_pitch == that.line_pitch &&
126 outline_size == that.outline_size &&
127 hjustification == that.hjustification &&
128 vjustification == that.vjustification &&
129 EQUIV(pixels_per_second, that.pixels_per_second) &&
130 !strcasecmp(font, that.font) &&
131 !strcasecmp(encoding, that.encoding) &&
133 !memcmp(wtext, that.wtext, wlen * sizeof(wchar_t));
136 void TitleConfig::copy_from(TitleConfig &that)
138 strcpy(font, that.font);
142 color_stroke = that.color_stroke;
143 stroke_width = that.stroke_width;
144 outline_color = that.outline_color;
146 outline_alpha = that.outline_alpha;
147 pixels_per_second = that.pixels_per_second;
148 motion_strategy = that.motion_strategy;
150 line_pitch = that.line_pitch;
151 hjustification = that.hjustification;
152 vjustification = that.vjustification;
153 fade_in = that.fade_in;
154 fade_out = that.fade_out;
157 dropshadow = that.dropshadow;
158 timecode = that.timecode;
159 timecode_format = that.timecode_format;
160 outline_size = that.outline_size;
161 strcpy(encoding, that.encoding);
162 memcpy(wtext, that.wtext, that.wlen * sizeof(wchar_t));
164 window_w = that.window_w;
165 window_h = that.window_h;
170 void TitleConfig::interpolate(TitleConfig &prev,
174 int64_t current_frame)
176 strcpy(font, prev.font);
177 strcpy(encoding, prev.encoding);
181 color_stroke = prev.color_stroke;
182 stroke_width = prev.stroke_width;
183 outline_color = prev.outline_color;
185 outline_alpha = prev.outline_alpha;
186 motion_strategy = prev.motion_strategy;
188 line_pitch = prev.line_pitch;
189 hjustification = prev.hjustification;
190 vjustification = prev.vjustification;
191 fade_in = prev.fade_in;
192 fade_out = prev.fade_out;
193 outline_size = prev.outline_size;
194 pixels_per_second = prev.pixels_per_second;
195 memcpy(wtext, prev.wtext, prev.wlen * sizeof(wchar_t));
199 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
200 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
201 this->x = prev.x * prev_scale + next.x * next_scale;
202 this->y = prev.y * prev_scale + next.y * next_scale;
205 timecode = prev.timecode;
206 timecode_format = prev.timecode_format;
207 // this->dropshadow = (int)(prev.dropshadow * prev_scale + next.dropshadow * next_scale);
208 this->dropshadow = prev.dropshadow;
211 void TitleConfig::limits()
213 if(window_w < 100) window_w = 100;
214 if(window_h < 100) window_h = 100;
220 void TitleConfig::to_wtext(const char *from_enc, const char *text, int tlen)
222 wlen = BC_Resources::encode(from_enc, BC_Resources::wide_encoding,
223 (char*)text,tlen, (char *)wtext,sizeof(wtext)) / sizeof(wchar_t);
224 while( wlen > 0 && !wtext[wlen-1] ) --wlen;
228 TitleGlyph::TitleGlyph()
245 TitleGlyph::~TitleGlyph()
247 //printf("TitleGlyph::~TitleGlyph 1\n");
248 if(data) delete data;
249 if(data_stroke) delete data_stroke;
262 GlyphPackage::GlyphPackage() : LoadPackage()
267 GlyphUnit::GlyphUnit(TitleMain *plugin, GlyphEngine *server)
270 this->plugin = plugin;
272 freetype_library = 0;
276 GlyphUnit::~GlyphUnit()
279 FT_Done_FreeType(freetype_library);
282 void GlyphUnit::process_package(LoadPackage *package)
284 GlyphPackage *pkg = (GlyphPackage*)package;
285 TitleGlyph *glyph = pkg->glyph;
287 char new_path[BCTEXTLEN];
289 current_font = plugin->get_font();
291 if(plugin->load_freetype_face(freetype_library, freetype_face,
292 current_font->path)) {
293 printf(_("GlyphUnit::process_package FT_New_Face failed.\n"));
298 int gindex = FT_Get_Char_Index(freetype_face, glyph->char_code);
300 //printf("GlyphUnit::process_package 1 %c\n", glyph->char_code);
303 BC_Resources *resources = BC_WindowBase::get_resources();
304 // Search replacement font
305 if(resources->find_font_by_char(glyph->char_code, new_path, freetype_face))
307 plugin->load_freetype_face(freetype_library,
308 freetype_face, new_path);
309 gindex = FT_Get_Char_Index(freetype_face, glyph->char_code);
312 FT_Set_Pixel_Sizes(freetype_face, plugin->config.size, 0);
316 if (glyph->char_code != 10)
317 printf(_("GlyphUnit::process_package FT_Load_Char failed - char: %li.\n"),
319 // Prevent a crash here
320 glyph->width = 8; glyph->height = 8;
321 glyph->pitch = 8; glyph->advance_x = 8;
322 glyph->left = 9; glyph->top = 9;
323 glyph->right = 0; glyph->bottom = 0;
324 glyph->freetype_index = 0;
325 glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
326 glyph->data->clear_frame();
327 glyph->data_stroke = 0;
329 // create outline glyph
330 if (plugin->config.stroke_width >= ZERO &&
331 (plugin->config.style & BC_FONT_OUTLINE)) {
332 glyph->data_stroke = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
333 glyph->data_stroke->clear_frame();
336 // char found and no outline desired
337 else if (plugin->config.stroke_width < ZERO ||
338 !(plugin->config.style & BC_FONT_OUTLINE)) {
339 FT_Glyph glyph_image;
342 FT_Load_Glyph(freetype_face, gindex, FT_LOAD_DEFAULT);
343 FT_Get_Glyph(freetype_face->glyph, &glyph_image);
344 FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph_image)->outline, &bbox);
345 // printf("Stroke: Xmin: %ld, Xmax: %ld, Ymin: %ld, yMax: %ld\n",
346 // bbox.xMin,bbox.xMax, bbox.yMin, bbox.yMax);
348 glyph->width = bm.width = ((bbox.xMax - bbox.xMin + 63) >> 6);
349 glyph->height = bm.rows = ((bbox.yMax - bbox.yMin + 63) >> 6);
350 glyph->pitch = bm.pitch = bm.width;
351 bm.pixel_mode = FT_PIXEL_MODE_GRAY;
353 glyph->left = (bbox.xMin + 31) >> 6;
354 glyph->top = (bbox.yMax + 31) >> 6;
355 glyph->right = (bbox.xMax + 31) >> 6;
356 glyph->bottom = (bbox.yMin + 31) >> 6;
357 glyph->freetype_index = gindex;
358 glyph->advance_x = ((freetype_face->glyph->advance.x + 31) >> 6);
359 //printf("GlyphUnit::process_package 1 width=%d height=%d pitch=%d left=%d top=%d advance_x=%d freetype_index=%d\n",
360 //glyph->width, glyph->height, glyph->pitch, glyph->left, glyph->top, glyph->advance_x, glyph->freetype_index);
362 glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
363 glyph->data->clear_frame();
364 bm.buffer = glyph->data->get_data();
365 FT_Outline_Translate(&((FT_OutlineGlyph) glyph_image)->outline,
366 - bbox.xMin, - bbox.yMin);
367 FT_Outline_Get_Bitmap( freetype_library,
368 &((FT_OutlineGlyph) glyph_image)->outline,
370 FT_Done_Glyph(glyph_image);
373 // Outline desired and glyph found
374 FT_Glyph glyph_image;
379 FT_UInt npoints, ncontours;
381 FT_Load_Glyph(freetype_face, gindex, FT_LOAD_DEFAULT);
382 FT_Get_Glyph(freetype_face->glyph, &glyph_image);
384 // check if the outline is ok (non-empty);
385 FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph_image)->outline, &bbox);
386 if( bbox.xMin == 0 && bbox.xMax == 0 &&
387 bbox.yMin == 0 && bbox.yMax == 0 ) {
388 FT_Done_Glyph(glyph_image);
389 glyph->width = 0; glyph->height = 0;
390 glyph->left = 0; glyph->top = 0;
391 glyph->right = 0; glyph->bottom = 0;
394 new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
396 new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
397 glyph->advance_x =((int)(freetype_face->glyph->advance.x +
398 plugin->config.stroke_width * 64)) >> 6;
401 #if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 2)
402 FT_Stroker_New(freetype_library, &stroker);
404 FT_Stroker_New(((FT_LibraryRec *)freetype_library)->memory, &stroker);
406 FT_Stroker_Set(stroker, (int)(plugin->config.stroke_width * 64),
407 FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
408 FT_Stroker_ParseOutline(stroker, &((FT_OutlineGlyph) glyph_image)->outline,1);
409 FT_Stroker_GetCounts(stroker,&npoints, &ncontours);
410 if (npoints ==0 && ncontours == 0) {
411 // this never happens, but FreeType has a bug regarding Linotype's Palatino font
412 FT_Stroker_Done(stroker);
413 FT_Done_Glyph(glyph_image);
414 glyph->width = 0; glyph->height = 0;
415 glyph->left = 0; glyph->top = 0;
416 glyph->right = 0; glyph->bottom = 0;
419 new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
421 new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
422 glyph->advance_x =((int)(freetype_face->glyph->advance.x +
423 plugin->config.stroke_width * 64)) >> 6;
427 FT_Outline_New(freetype_library, npoints, ncontours, &outline);
429 outline.n_contours=0;
430 FT_Stroker_Export (stroker, &outline);
431 FT_Outline_Get_BBox(&outline, &bbox);
432 FT_Outline_Translate(&outline, - bbox.xMin, - bbox.yMin);
433 FT_Outline_Translate(&((FT_OutlineGlyph) glyph_image)->outline,
435 - bbox.yMin + (int)(plugin->config.stroke_width*32));
436 // printf("Stroke: Xmin: %ld, Xmax: %ld, Ymin: %ld, yMax: %ld\n"
437 // "Fill Xmin: %ld, Xmax: %ld, Ymin: %ld, yMax: %ld\n",
438 // bbox.xMin,bbox.xMax, bbox.yMin, bbox.yMax,
439 // bbox_fill.xMin,bbox_fill.xMax, bbox_fill.yMin, bbox_fill.yMax);
441 glyph->width = bm.width = ((bbox.xMax - bbox.xMin) >> 6)+1;
442 glyph->height = bm.rows = ((bbox.yMax - bbox.yMin) >> 6) +1;
443 glyph->pitch = bm.pitch = bm.width;
444 bm.pixel_mode = FT_PIXEL_MODE_GRAY;
446 glyph->left = (bbox.xMin + 31) >> 6;
447 glyph->top = (bbox.yMax + 31) >> 6;
448 glyph->right = (bbox.xMax + 31) >> 6;
449 glyph->bottom = (bbox.yMin + 31) >> 6;
450 glyph->freetype_index = gindex;
451 int real_advance = ((int)ceil((float)freetype_face->glyph->advance.x +
452 plugin->config.stroke_width * 64) >> 6);
453 glyph->advance_x = glyph->width + glyph->left;
454 if (real_advance > glyph->advance_x)
455 glyph->advance_x = real_advance;
456 //printf("GlyphUnit::process_package 1 width=%d height=%d "
457 // "pitch=%d left=%d top=%d advance_x=%d freetype_index=%d\n",
458 // glyph->width, glyph->height, glyph->pitch, glyph->left,
459 // glyph->top, glyph->advance_x, glyph->freetype_index);
462 //printf("GlyphUnit::process_package 1\n");
463 glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
464 glyph->data->clear_frame();
465 glyph->data_stroke = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
466 glyph->data_stroke->clear_frame();
467 // for debugging memset( glyph->data_stroke->get_data(), 60, glyph->pitch * glyph->height);
468 bm.buffer=glyph->data->get_data();
469 FT_Outline_Get_Bitmap( freetype_library,
470 &((FT_OutlineGlyph) glyph_image)->outline,
472 bm.buffer=glyph->data_stroke->get_data();
473 FT_Outline_Get_Bitmap( freetype_library,
476 FT_Outline_Done(freetype_library,&outline);
477 FT_Stroker_Done(stroker);
478 FT_Done_Glyph(glyph_image);
480 //printf("GlyphUnit::process_package 2\n");
487 GlyphEngine::GlyphEngine(TitleMain *plugin, int cpus)
488 : LoadServer(cpus, cpus)
490 this->plugin = plugin;
493 void GlyphEngine::init_packages()
495 int current_package = 0;
496 for(int i = 0; i < plugin->glyphs.total; i++) {
497 if(!plugin->glyphs.values[i]->data) {
498 GlyphPackage *pkg = (GlyphPackage*)get_package(current_package++);
499 pkg->glyph = plugin->glyphs.values[i];
504 LoadClient* GlyphEngine::new_client()
506 return new GlyphUnit(plugin, this);
509 LoadPackage* GlyphEngine::new_package()
511 return new GlyphPackage;
518 // Copy a single character to the text mask
519 TitlePackage::TitlePackage()
527 TitleUnit::TitleUnit(TitleMain *plugin, TitleEngine *server)
530 this->plugin = plugin;
531 this->engine = server;
534 void TitleUnit::draw_glyph(VFrame *output, VFrame *data, TitleGlyph *glyph, int x, int y)
536 int glyph_w = data->get_w(), glyph_h = data->get_h();
537 int output_w = output->get_w(), output_h = output->get_h();
538 unsigned char **in_rows = data->get_rows();
539 unsigned char **out_rows = output->get_rows();
541 int baseline = plugin->get_char_height();
542 if(engine->do_dropshadow) {
543 x += plugin->config.dropshadow;
544 y += plugin->config.dropshadow;
547 int x_in = 0, y_in = 0;
548 int x_out = x + glyph->left;
549 if( x_out < 0 ) { x_in = -x_out; x_out = 0; }
550 if( x_out+glyph_w > output_w ) glyph_w = output_w-x_out;
551 if( x_in >= glyph_w || x_out >= output_w ) return;
552 int y_out = y + baseline - glyph->top;
553 if( y_out < 0 ) { y_in = -y_out; y_out = 0; }
554 if( y_out+glyph_h > output_h ) glyph_h = output_h-y_out;
555 if( y_in >= glyph_h || y_out >= output_h ) return;
557 if(engine->do_dropshadow) {
558 while( y_in < glyph_h && y_out < output_h ) {
559 unsigned char *in_row = in_rows[y_in];
560 unsigned char *out_row = out_rows[y_out];
561 for( int xin=x_in,xout=x_out*4+3; xin<glyph_w; ++xin,xout+=4 ) {
562 out_row[xout] = in_row[xin];
570 plugin->get_color_components(&r, &g, &b, &a, 0);
571 //int outline = plugin->config.outline_size;
572 //if(outline) a = 0xff;
574 while( y_in < glyph_h && y_out < output_h ) {
575 unsigned char *in_row = in_rows[y_in];
576 unsigned char *out_row = out_rows[y_out];
577 for( int xin=x_in,xout=x_out*4+0; xin<glyph_w; ++xin,xout+=4 ) {
578 int in_a = in_row[xin], out_a = out_row[xout+3];
579 if( in_a + out_a == 0 ) continue; // alpha blundering
580 int opacity = in_a * a, transp = out_a * (0xff - opacity/0xff);
581 out_row[xout+0] = (opacity * r + transp * out_row[xout+0]) / (0xff*0xff);
582 out_row[xout+1] = (opacity * g + transp * out_row[xout+1]) / (0xff*0xff);
583 out_row[xout+2] = (opacity * b + transp * out_row[xout+2]) / (0xff*0xff);
584 out_row[xout+3] = (opacity + transp) / 0xff;
591 void TitleUnit::process_package(LoadPackage *package)
593 TitlePackage *pkg = (TitlePackage*)package;
595 if( pkg->char_code == 0 || pkg->char_code == '\n') return;
596 TitleGlyph *glyph = plugin->get_glyph(pkg->char_code);
598 draw_glyph(plugin->text_mask, glyph->data, glyph, pkg->x, pkg->y);
599 if(plugin->config.stroke_width >= ZERO && (plugin->config.style & BC_FONT_OUTLINE))
600 draw_glyph(plugin->text_mask_stroke, glyph->data_stroke, glyph, pkg->x, pkg->y);
603 TitleEngine::TitleEngine(TitleMain *plugin, int cpus)
604 : LoadServer(cpus, cpus)
606 this->plugin = plugin;
609 void TitleEngine::init_packages()
611 int current_package = 0;
612 int dx = plugin->config.outline_size - plugin->extent.x1;
613 int dy = plugin->config.outline_size - plugin->extent.y1;
614 for(int i = plugin->visible_char1; i < plugin->visible_char2; i++) {
615 TitlePackage *pkg = (TitlePackage*)get_package(current_package++);
616 char_pos_t *pos = plugin->char_pos + i;
617 pkg->x = pos->x + dx;
618 pkg->y = pos->y + dy;
619 pkg->char_code = plugin->config.wtext[i];
620 //printf("draw '%c' at %d,%d\n",(int)pkg->char_code, pkg->x, pkg->y);
624 LoadClient* TitleEngine::new_client()
626 return new TitleUnit(plugin, this);
629 LoadPackage* TitleEngine::new_package()
631 return new TitlePackage;
634 void TitleTranslateUnit::translation_array_f(transfer_table_f* &table,
645 //float offset = out_x1 - in_x1;
647 out_x1_int = (int)out_x1;
648 out_x2_int = MIN((int)ceil(out_x2), out_total);
649 out_w_int = out_x2_int - out_x1_int;
651 table = new transfer_table_f[out_w_int];
652 bzero(table, sizeof(transfer_table_f) * out_w_int);
655 for(int out_x = out_x1_int; out_x < out_x2_int; out_x++)
657 transfer_table_f *entry = &table[out_x - out_x1_int];
659 entry->in_x1 = (int)in_x;
660 entry->in_x2 = (int)in_x + 1;
662 // Get fraction of output pixel to fill
663 entry->output_fraction = 1;
667 entry->output_fraction -= out_x1 - out_x;
670 if(out_x2 < out_x + 1)
672 entry->output_fraction = (out_x2 - out_x);
675 // Advance in_x until out_x_fraction is filled
676 float out_x_fraction = entry->output_fraction;
677 float in_x_fraction = floor(in_x + 1) - in_x;
679 if(out_x_fraction <= in_x_fraction)
681 entry->in_fraction1 = out_x_fraction;
682 entry->in_fraction2 = 0.0;
683 in_x += out_x_fraction;
687 entry->in_fraction1 = in_x_fraction;
688 in_x += out_x_fraction;
689 entry->in_fraction2 = in_x - floor(in_x);
692 // Clip in_x and zero out fraction. This doesn't work for YUV.
693 if(entry->in_x2 >= in_total)
695 entry->in_x2 = in_total - 1;
696 entry->in_fraction2 = 0.0;
699 if(entry->in_x1 >= in_total)
701 entry->in_x1 = in_total - 1;
702 entry->in_fraction1 = 0.0;
709 // Copy a single character to the text mask
710 TitleOutlinePackage::TitleOutlinePackage()
716 TitleOutlineUnit::TitleOutlineUnit(TitleMain *plugin, TitleOutlineEngine *server)
719 this->plugin = plugin;
720 this->engine = server;
723 void TitleOutlineUnit::process_package(LoadPackage *package)
725 TitleOutlinePackage *pkg = (TitleOutlinePackage*)package;
726 int r, g, b, outline_a;
727 plugin->get_color_components(&r, &g, &b, &outline_a, 1);
728 unsigned char **outline_rows = plugin->outline_mask->get_rows();
729 unsigned char **text_rows = plugin->text_mask->get_rows();
730 int mask_w1 = plugin->text_mask->get_w()-1;
731 int mask_h1 = plugin->text_mask->get_h()-1;
732 int ofs = plugin->config.outline_size;
734 if(engine->pass == 0) {
735 // get max alpha under outline size macropixel
736 for(int y = pkg->y1; y < pkg->y2; y++) {
737 unsigned char *out_row = outline_rows[y];
738 int y1 = y - ofs, y2 = y + ofs;
739 CLAMP(y1, 0, mask_h1); CLAMP(y2, 0, mask_h1);
740 for(int x = 0; x < plugin->text_mask->get_w(); x++) {
741 int x1 = x - ofs, x2 = x + ofs;
742 CLAMP(x1, 0, mask_w1); CLAMP(x2, 0, mask_w1);
745 for(int yy = y1; yy <= y2; yy++) {
746 unsigned char *text_row = text_rows[yy];
747 for(int xx = x1; xx <= x2; ++xx) {
748 unsigned char *pixel = text_row + xx*4;
749 if(pixel[3] > max_a) max_a = pixel[3];
753 unsigned char *out = out_row + x*4;
754 out[0] = r; out[1] = g; out[2] = b;
755 out[3] = (max_a * outline_a) / 0xff;
761 // Overlay text mask on top of outline mask
762 int ofs = BC_CModels::is_yuv(plugin->output->get_color_model()) ? 0x80 : 0;
763 for(int y = pkg->y1; y < pkg->y2; y++) {
764 unsigned char *outline_row = outline_rows[y];
765 unsigned char *text_row = text_rows[y];
766 for(int x = 0; x < plugin->text_mask->get_w(); x++) {
767 unsigned char *out = text_row + x * 4;
768 unsigned char *inp = outline_row + x * 4;
769 int out_a = out[3], in_a = inp[3];
770 int transparency = in_a * (0xff - out_a) / 0xff;
771 out[0] = (out[0] * out_a + inp[0] * transparency) / 0xff;
772 out[1] = ((out[1]-ofs) * out_a + (inp[1]-ofs) * transparency) / 0xff + ofs;
773 out[2] = ((out[2]-ofs) * out_a + (inp[2]-ofs) * transparency) / 0xff + ofs;
774 out[3] = in_a + out_a - in_a*out_a / 0xff;
780 TitleOutlineEngine::TitleOutlineEngine(TitleMain *plugin, int cpus)
781 : LoadServer(cpus, cpus)
783 this->plugin = plugin;
786 void TitleOutlineEngine::init_packages()
788 int mask_h = plugin->text_mask->get_h();
789 if( !mask_h ) return;
790 int py1 = 0, py2 = 0;
791 int pkgs = get_total_packages();
792 for( int i=0; i<pkgs; py1=py2 ) {
793 TitleOutlinePackage *pkg = (TitleOutlinePackage*)get_package(i);
794 py2 = (++i * mask_h)/ pkgs;
795 pkg->y1 = py1; pkg->y2 = py2;
799 void TitleOutlineEngine::do_outline()
801 pass = 0; process_packages();
802 pass = 1; process_packages();
805 LoadClient* TitleOutlineEngine::new_client()
807 return new TitleOutlineUnit(plugin, this);
810 LoadPackage* TitleOutlineEngine::new_package()
812 return new TitleOutlinePackage;
818 TitleTranslatePackage::TitleTranslatePackage()
825 TitleTranslateUnit::TitleTranslateUnit(TitleMain *plugin, TitleTranslate *server)
828 this->plugin = plugin;
831 void TitleTranslate::run_packages()
833 output_w = plugin->output->get_w();
834 output_h = plugin->output->get_h();
836 float x1 = plugin->text_x1 + plugin->extent.x1;
837 float x2 = plugin->text_x1 + plugin->extent.x2;
838 if (x2 <= 0 || x1 >= x2 || x1 >= output_w) return;
840 float y1 = plugin->text_y1 + plugin->extent.y1;
841 float y2 = plugin->text_y1 + plugin->extent.y2;
842 if (y2 <= 0 || y1 >= y2 || y1 >= output_h) return;
850 #define TRANSLATE(type, max, components, ofs) \
852 unsigned char **in_rows = plugin->text_mask->get_rows(); \
853 type **out_rows = (type**)plugin->output->get_rows(); \
855 for(int i = pkg->y1; i < pkg->y2; i++) \
857 if(i + server->out_y1_int >= 0 && \
858 i + server->out_y1_int < server->output_h) \
861 float y_fraction1, y_fraction2; \
862 in_y1 = server->y_table[i].in_x1; \
863 in_y2 = server->y_table[i].in_x2; \
864 y_fraction1 = server->y_table[i].in_fraction1; \
865 y_fraction2 = server->y_table[i].in_fraction2; \
866 unsigned char *in_row1 = in_rows[in_y1]; \
867 unsigned char *in_row2 = in_rows[in_y2]; \
868 type *out_row = out_rows[i + server->out_y1_int]; \
870 for(int j = server->out_x1_int; j < server->out_x2_int; j++) \
872 if(j >= 0 && j < server->output_w) \
879 server->x_table[j - server->out_x1_int].in_x1; \
881 server->x_table[j - server->out_x1_int].in_x2; \
883 server->x_table[j - server->out_x1_int].in_fraction1; \
885 server->x_table[j - server->out_x1_int].in_fraction2; \
887 float fraction1 = x_fraction1 * y_fraction1 / (256.f-max); \
888 float fraction2 = x_fraction2 * y_fraction1 / (256.f-max); \
889 float fraction3 = x_fraction1 * y_fraction2 / (256.f-max); \
890 float fraction4 = x_fraction2 * y_fraction2 / (256.f-max); \
891 type input_r = (type)(in_row1[in_x1 * 4 + 0] * fraction1 + \
892 in_row1[in_x2 * 4 + 0] * fraction2 + \
893 in_row2[in_x1 * 4 + 0] * fraction3 + \
894 in_row2[in_x2 * 4 + 0] * fraction4); \
895 type input_g = (type)(in_row1[in_x1 * 4 + 1] * fraction1 + \
896 in_row1[in_x2 * 4 + 1] * fraction2 + \
897 in_row2[in_x1 * 4 + 1] * fraction3 + \
898 in_row2[in_x2 * 4 + 1] * fraction4); \
899 type input_b = (type)(in_row1[in_x1 * 4 + 2] * fraction1 + \
900 in_row1[in_x2 * 4 + 2] * fraction2 + \
901 in_row2[in_x1 * 4 + 2] * fraction3 + \
902 in_row2[in_x2 * 4 + 2] * fraction4); \
903 type input_a = (type)(in_row1[in_x1 * 4 + 3] * fraction1 + \
904 in_row1[in_x2 * 4 + 3] * fraction2 + \
905 in_row2[in_x1 * 4 + 3] * fraction3 + \
906 in_row2[in_x2 * 4 + 3] * fraction4); \
907 /* Plugin alpha is actually 0 - 0x100 */ \
908 input_a = input_a * plugin->alpha / 0x100; \
912 if(components == 4) \
914 transparency = out_row[j * components + 3] * (max - input_a) / max; \
915 out_row[j * components + 0] = \
916 (input_r * input_a + out_row[j * components + 0] * transparency) / max; \
917 out_row[j * components + 1] = \
918 ((input_g-ofs) * input_a + (out_row[j * components + 1]-ofs) * transparency) / max + ofs; \
919 out_row[j * components + 2] = \
920 ((input_b-ofs) * input_a + (out_row[j * components + 2]-ofs) * transparency) / max + ofs; \
921 out_row[j * components + 3] = \
922 MAX(input_a, out_row[j * components + 3]); \
926 transparency = max - input_a; \
927 out_row[j * components + 0] = \
928 (input_r * input_a + out_row[j * components + 0] * transparency) / max; \
929 out_row[j * components + 1] = \
930 ((input_g-ofs) * input_a + (out_row[j * components + 1]-ofs) * transparency) / max + ofs; \
931 out_row[j * components + 2] = \
932 ((input_b-ofs) * input_a + (out_row[j * components + 2]-ofs) * transparency) / max + ofs; \
940 #define TRANSLATEA(type, max, components, r, g, b) \
942 unsigned char **in_rows = plugin->text_mask->get_rows(); \
943 type **out_rows = (type**)plugin->output->get_rows(); \
945 for(int i = pkg->y1; i < pkg->y2; i++) \
947 if(i + server->out_y1_int >= 0 && \
948 i + server->out_y1_int < server->output_h) \
950 unsigned char *in_row = in_rows[i]; \
951 type *out_row = out_rows[i + server->out_y1_int]; \
953 for(int j = server->out_x1; j < server->out_x2_int; j++) \
956 j < server->output_w) \
958 int input = (int)(in_row[j - server->out_x1]); \
960 input *= plugin->alpha; \
961 /* Alpha is 0 - 256 */ \
964 int anti_input = 0xff - input; \
965 if(components == 4) \
967 out_row[j * components + 0] = \
968 (r * input + out_row[j * components + 0] * anti_input) / 0xff; \
969 out_row[j * components + 1] = \
970 (g * input + out_row[j * components + 1] * anti_input) / 0xff; \
971 out_row[j * components + 2] = \
972 (b * input + out_row[j * components + 2] * anti_input) / 0xff; \
974 out_row[j * components + 3] = \
975 MAX((input << 8) | input, out_row[j * components + 3]); \
977 out_row[j * components + 3] = \
978 MAX(input, out_row[j * components + 3]); \
982 out_row[j * components + 0] = \
983 (r * input + out_row[j * components + 0] * anti_input) / 0xff; \
984 out_row[j * components + 1] = \
985 (g * input + out_row[j * components + 1] * anti_input) / 0xff; \
986 out_row[j * components + 2] = \
987 (b * input + out_row[j * components + 2] * anti_input) / 0xff; \
995 void TitleTranslateUnit::process_package(LoadPackage *package)
997 TitleTranslatePackage *pkg = (TitleTranslatePackage*)package;
998 TitleTranslate *server = (TitleTranslate*)this->server;
1000 switch(plugin->output->get_color_model()) {
1001 case BC_RGB888: TRANSLATE(unsigned char, 0xff, 3, 0); break;
1002 case BC_RGB_FLOAT: TRANSLATE(float, 1.0, 3, 0); break;
1003 case BC_YUV888: TRANSLATE(unsigned char, 0xff, 3, 0x80); break;
1004 case BC_RGBA_FLOAT: TRANSLATE(float, 1.0, 4, 0); break;
1005 case BC_RGBA8888: TRANSLATE(unsigned char, 0xff, 4, 0); break;
1006 case BC_YUVA8888: TRANSLATE(unsigned char, 0xff, 4, 0x80); break;
1008 //printf("TitleTranslateUnit::process_package 5\n");
1014 TitleTranslate::TitleTranslate(TitleMain *plugin, int cpus)
1015 : LoadServer(cpus, cpus)
1017 this->plugin = plugin;
1020 out_x1 = out_x2 = 0;
1021 out_y1 = out_y2 = 0;
1022 out_x1_int = out_x2_int = 0;
1023 out_y1_int = out_y2_int = 0;
1024 output_w = output_h = 0;
1027 TitleTranslate::~TitleTranslate()
1033 void TitleTranslate::init_packages()
1035 // Generate scaling tables
1036 delete [] x_table; x_table = 0;
1037 delete [] y_table; y_table = 0;
1039 output_w = plugin->output->get_w();
1040 output_h = plugin->output->get_h();
1042 float x1 = plugin->text_x1 + plugin->extent.x1;
1043 float x2 = plugin->text_x1 + plugin->extent.x2;
1044 TitleTranslateUnit::translation_array_f(x_table, x1, x2, 0,
1045 plugin->mask_w, plugin->mask_w,
1046 output_w, out_x1_int, out_x2_int);
1048 float y1 = plugin->text_y1 + plugin->extent.y1;
1049 float y2 = plugin->text_y1 + plugin->extent.y2;
1050 TitleTranslateUnit::translation_array_f(y_table, y1, y2, 0,
1051 plugin->mask_h, plugin->mask_h,
1052 output_h, out_y1_int, out_y2_int);
1054 //printf("TitleTranslate::init_packages 1\n");
1055 out_x1 = out_x1_int; out_x2 = out_x2_int;
1056 out_y1 = out_y1_int; out_y2 = out_y2_int;
1058 int out_h = out_y2 - out_y1;
1059 int py1 = 0, py2 = 0;
1060 int pkgs = get_total_packages();
1061 for( int i=0; i<pkgs; py1=py2 ) {
1062 TitleTranslatePackage *pkg = (TitleTranslatePackage*)get_package(i);
1063 py2 = (++i*out_h) / pkgs;
1064 pkg->y1 = py1; pkg->y2 = py2;
1066 //printf("TitleTranslate::init_packages 2\n");
1069 LoadClient* TitleTranslate::new_client()
1071 return new TitleTranslateUnit(plugin, this);
1074 LoadPackage* TitleTranslate::new_package()
1076 return new TitleTranslatePackage;
1081 TitleMain::TitleMain(PluginServer *server)
1082 : PluginVClient(server)
1086 text_mask_stroke = 0;
1090 freetype_library = 0;
1096 visible_row1 = 0; visible_row2 = 0;
1097 visible_char1 = 0; visible_char2 = 0;
1098 text_y1 = text_y2 = text_x1 = 0;
1101 text_w = 0; text_h = 0;
1102 input = 0; output = 0;
1103 cpus = PluginClient::smp + 1;
1104 if( cpus > 8 ) cpus = 8;
1105 need_reconfigure = 1;
1108 TitleMain::~TitleMain()
1111 delete outline_mask;
1112 delete text_mask_stroke;
1116 delete glyph_engine;
1117 delete title_engine;
1119 FT_Done_Face(freetype_face);
1120 if( freetype_library )
1121 FT_Done_FreeType(freetype_library);
1123 delete outline_engine;
1126 const char* TitleMain::plugin_title() { return _("Title"); }
1127 int TitleMain::is_realtime() { return 1; }
1128 int TitleMain::is_synthesis() { return 1; }
1130 NEW_WINDOW_MACRO(TitleMain, TitleWindow);
1133 void TitleMain::build_previews(TitleWindow *gui)
1135 ArrayList<BC_FontEntry*>*fonts = gui->get_resources()->fontlist;
1137 for(int font_number = 0; font_number < fonts->size(); font_number++)
1139 BC_FontEntry *font_entry = fonts->get(font_number);
1140 // already have examples
1141 if(font_entry->image) return;
1144 // create example bitmaps
1145 FT_Library freetype_library = 0; // Freetype library
1146 FT_Face freetype_face = 0;
1147 const char *test_string = "Aa";
1148 char new_path[BCTEXTLEN];
1149 int text_height = gui->get_text_height(LARGEFONT);
1150 int text_color = BC_WindowBase::get_resources()->default_text_color;
1151 int r = (text_color >> 16) & 0xff;
1152 int g = (text_color >> 8) & 0xff;
1153 int b = text_color & 0xff;
1154 // dimensions for each line
1155 int height[fonts->size()];
1156 int ascent[fonts->size()];
1158 // pass 1 gets the extents for all the fonts
1159 // pass 2 draws the image
1162 for(int pass = 0; pass < 2; pass++)
1164 //printf("TitleMain::build_previews %d %d %d\n",
1168 for(int font_number = 0; font_number < fonts->size(); font_number++)
1170 BC_FontEntry *font_entry = fonts->get(font_number);
1172 // test if font of same name has been processed
1174 for(int i = 0; i < font_number; i++) {
1175 if(!strcasecmp(fonts->get(i)->displayname, font_entry->displayname)) {
1177 font_entry->image = fonts->get(i)->image;
1188 int current_ascent = 0;
1191 font_entry->image = new VFrame;
1192 font_entry->image->set_use_shm(0);
1193 font_entry->image->reallocate(0, -1, 0, 0, 0,
1194 total_w, total_h, BC_RGBA8888, -1);
1195 font_entry->image->clear_frame();
1200 int len = strlen(test_string);
1201 for(int j = 0; j < len; j++)
1203 FT_ULong c = test_string[j];
1204 check_char_code_path(freetype_library,
1208 if( !load_freetype_face(freetype_library,
1209 freetype_face, new_path)) {
1210 FT_Set_Pixel_Sizes(freetype_face, text_height, 0);
1212 if(!FT_Load_Char(freetype_face, c, FT_LOAD_RENDER)) {
1214 current_w = current_x + freetype_face->glyph->bitmap.width;
1215 if(freetype_face->glyph->bitmap_top > current_ascent)
1216 current_ascent = freetype_face->glyph->bitmap_top;
1217 if(freetype_face->glyph->bitmap.rows > total_h)
1218 total_h = freetype_face->glyph->bitmap.rows;
1219 if(freetype_face->glyph->bitmap.rows > current_h)
1220 current_h = freetype_face->glyph->bitmap.rows;
1223 // copy 1 row at a time
1224 // center vertically
1225 int out_y = (total_h - height[font_number]) / 2 +
1226 ascent[font_number] - freetype_face->glyph->bitmap_top;
1228 in_y < freetype_face->glyph->bitmap.rows &&
1231 unsigned char *out_row = font_entry->image->get_rows()[out_y] +
1233 unsigned char *in_row = freetype_face->glyph->bitmap.buffer +
1234 freetype_face->glyph->bitmap.pitch * in_y;
1236 for(int out_x = 0; out_x < freetype_face->glyph->bitmap.width &&
1239 *out_row = (*in_row * r +
1240 (0xff - *in_row) * *out_row) / 0xff; ++out_row;
1241 *out_row = (*in_row * g +
1242 (0xff - *in_row) * *out_row) / 0xff; ++out_row;
1243 *out_row = (*in_row * b +
1244 (0xff - *in_row) * *out_row) / 0xff; ++out_row;
1245 *out_row = MAX(*in_row, *out_row); ++out_row;
1252 current_x += freetype_face->glyph->advance.x >> 6;
1257 height[font_number] = current_h;
1258 ascent[font_number] = current_ascent;
1259 if(pass == 0 && current_w > total_w) total_w = current_w;
1264 if(freetype_library) FT_Done_FreeType(freetype_library);
1269 //This checks if char_code is on the selected font, else it changes font to the first compatible //Akirad
1270 int TitleMain::check_char_code_path(FT_Library &freetype_library,
1272 FT_ULong &char_code,
1275 FT_Face temp_freetype_face;
1279 FcChar8 *file, *format;
1284 config = FcConfigGetCurrent();
1285 FcConfigSetRescanInterval(config, 0);
1287 pat = FcPatternCreate();
1288 os = FcObjectSetBuild ( FC_FILE, FC_FONTFORMAT, (char *) 0);
1289 fs = FcFontList(config, pat, os);
1292 char tmpstring[BCTEXTLEN];
1293 int limit_to_truetype = 0; //if you want to limit search to truetype put 1
1294 if(!freetype_library) FT_Init_FreeType(&freetype_library);
1295 if(!FT_New_Face(freetype_library,
1298 &temp_freetype_face))
1300 FT_Set_Pixel_Sizes(temp_freetype_face, 128, 0);
1301 int gindex = FT_Get_Char_Index(temp_freetype_face, char_code);
1302 if((!gindex == 0) && (!char_code != 10))
1304 strcpy(path_new, path_old);
1311 for (i=0; fs && i < fs->nfont; i++)
1313 font = fs->fonts[i];
1314 FcPatternGetString(font, FC_FONTFORMAT, 0, &format);
1315 if((!strcmp((char *)format, "TrueType")) || limit_to_truetype)
1317 if(FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch)
1320 sprintf(tmpstring, "%s", file);
1321 if(!FT_New_Face(freetype_library,
1324 &temp_freetype_face))
1326 FT_Set_Pixel_Sizes(temp_freetype_face, 128, 0);
1327 int gindex = FT_Get_Char_Index(temp_freetype_face, char_code);
1328 if((!gindex == 0) && (!char_code != 10))
1330 sprintf(path_new, "%s", tmpstring);
1342 if(fs) FcFontSetDestroy(fs);
1343 if(temp_freetype_face) FT_Done_Face(temp_freetype_face);
1344 temp_freetype_face = 0;
1348 strcpy(path_new, path_old);
1358 int TitleMain::load_freetype_face(FT_Library &freetype_library,
1359 FT_Face &freetype_face,
1362 //printf("TitleMain::load_freetype_face 1\n");
1363 if(!freetype_library)
1364 FT_Init_FreeType(&freetype_library);
1366 FT_Done_Face(freetype_face);
1368 //printf("TitleMain::load_freetype_face 2\n");
1370 // Use freetype's internal function for loading font
1371 if(FT_New_Face(freetype_library, path, 0, &freetype_face)) {
1372 fprintf(stderr, _("TitleMain::load_freetype_face %s failed.\n"), path);
1374 freetype_library = 0;
1380 BC_FontEntry* TitleMain::get_font()
1385 style |= (config.style & BC_FONT_ITALIC) ? FL_SLANT_ITALIC : FL_SLANT_ROMAN;
1386 style |= (config.style & BC_FONT_BOLD) ? FL_WEIGHT_BOLD : FL_WEIGHT_NORMAL;
1388 mask = FL_WEIGHT_MASK | FL_SLANT_MASK;
1390 BC_Resources *resources = BC_WindowBase::get_resources();
1391 return resources->find_fontentry(config.font, style, mask);
1394 int TitleMain::get_char_height()
1396 // this is baseline to the baseline distance
1397 int result = config.size;
1398 if((config.style & BC_FONT_OUTLINE)) result += (int)ceil(config.stroke_width * 2);
1402 TitleGlyph *TitleMain::get_glyph(FT_ULong char_code)
1404 for(int i = 0; i < glyphs.size(); i++) {
1405 if(glyphs.get(i)->char_code == char_code)
1406 return glyphs.get(i);
1411 int TitleMain::get_char_width(FT_ULong char_code)
1413 if(char_code == '\n') return 0;
1414 TitleGlyph *glyph = get_glyph(char_code);
1415 return !glyph ? 0 : glyph->width;
1418 int TitleMain::get_char_advance(FT_ULong current, FT_ULong next)
1422 if(current == '\n') return 0;
1423 TitleGlyph *current_glyph = get_glyph(current);
1424 int result = !current_glyph ? 0 : current_glyph->advance_x;
1425 TitleGlyph *next_glyph = !next ? 0 : get_glyph(next);
1427 FT_Get_Kerning(freetype_face,
1428 current_glyph->freetype_index,
1429 next_glyph->freetype_index,
1434 return result + (kerning.x >> 6);
1437 void TitleMain::load_glyphs()
1439 // Build table of all glyphs needed
1440 int total_packages = 0;
1442 for(int i = 0; i < config.wlen; i++)
1445 FT_ULong char_code = config.wtext[i];
1447 for(int j = 0; j < glyphs.total; j++) {
1448 if(glyphs.values[j]->char_code == char_code) {
1454 if(!exists && char_code != 0) {
1456 TitleGlyph *glyph = new TitleGlyph;
1457 glyphs.append(glyph);
1458 glyph->char_code = char_code;
1463 glyph_engine = new GlyphEngine(this, cpus);
1465 glyph_engine->set_package_count(total_packages);
1466 glyph_engine->process_packages();
1470 void TitleMain::get_total_extents()
1472 // Determine extents of total text
1473 int wlen = config.wlen;
1474 char_pos = new char_pos_t[wlen+1];
1476 int pitch = config.line_pitch;
1477 int font_h = get_char_height();
1479 int row_w = 0, row_h = 0;
1480 int max_char_w = 0, max_char_h = 0;
1481 text_h = text_w = 0;
1483 // unjustified positions, initial bbox
1484 for(int i = 0; i < wlen; i++) {
1485 char_pos[i].x = row_w;
1486 char_pos[i].y = text_h;
1487 char_pos[i].row = row;
1488 wchar_t wchar = config.wtext[i];
1489 if( wchar == '\n' ) {
1490 text_h += pitch ? pitch : row_h > font_h ? row_h : font_h;
1491 if(row_w > text_w) text_w = row_w;
1492 row_w = row_h = 0; ++row;
1495 TitleGlyph *glyph = get_glyph(wchar);
1496 int char_w = i+1>=wlen ? glyph->width :
1497 get_char_advance(wchar, config.wtext[i+1]);
1498 char_pos[i].w = char_w;
1500 if( char_w > max_char_w ) max_char_w = char_w;
1501 int char_h = glyph->top - glyph->bottom;
1502 if( char_h > max_char_h ) max_char_h = char_h;
1503 if( char_h > row_h ) row_h = char_h;
1504 //printf("charcode '%c' %d,%d glyph bbox %d,%d %d,%d\n",
1505 // (int)wchar, char_pos[i].x, char_pos[i].y,
1506 // glyph->left, glyph->top, glyph->right, glyph->bottom);
1508 if( wlen > 0 && config.wtext[wlen-1] != '\n' ) {
1509 text_h += pitch ? pitch : row_h > font_h ? row_h : font_h;
1510 if(row_w > text_w) text_w = row_w;
1513 char_pos[wlen].x = row_w;
1514 char_pos[wlen].y = text_h;
1515 char_pos[wlen].row = row;
1516 char_pos[wlen].w = 0;
1519 // justify positions
1520 int row_start = 0, pos = 0;
1521 switch(config.hjustification) {
1523 while( row_start < wlen ) {
1524 while( pos<wlen && config.wtext[pos]!='\n' ) ++pos;
1525 int ofs = (text_w - char_pos[pos].x) / 2;
1526 //printf("justify_mid ofs=%d\n", ofs);
1527 while( row_start < pos )
1528 char_pos[row_start++].x += ofs;
1533 while( row_start < wlen ) {
1534 while( pos<wlen && config.wtext[pos]!='\n' ) ++pos;
1535 int ofs = text_w - char_pos[pos].x;
1536 //printf("justify_right ofs=%d\n", ofs);
1537 while( row_start < pos )
1538 char_pos[row_start++].x += ofs;
1547 if( row_geom_size < text_rows+1 ) {
1548 delete [] row_geom; row_geom = 0;
1551 row_geom_size = text_rows+1;
1552 row_geom = new RowGeom[row_geom_size];
1555 // Determine row extents
1556 struct RowGeom *geom = &row_geom[0];
1557 TitleGlyph *glyph = 0;
1558 geom->x0 = geom->y0 = 0;
1559 geom->x1 = geom->y1 = 0;
1560 geom->x2 = geom->y2 = 0;
1562 // x0,y0 row origin in without kern
1563 // x1,y1 - x2,y2 left baseline relative bbox with kerns
1564 for(int i = 0; i < wlen; i++) {
1565 int x = char_pos[i].x;
1566 int y = char_pos[i].y;
1567 wchar_t wchar = config.wtext[i];
1568 if( wchar == '\n' ) {
1570 geom->x0 = geom->y0 = 0;
1571 geom->x1 = geom->y1 = 0;
1572 geom->x2 = geom->y2 = 0;
1575 TitleGlyph *gp = get_glyph(wchar);
1578 geom->x0 = x; geom->y0 = y;
1579 int dx = x-geom->x0, dy = y-geom->y0 + config.size;
1580 geom->x1 = dx + gp->left; geom->y1 = dy - gp->top;
1581 geom->x2 = dx + gp->right; geom->y2 = dy - gp->bottom;
1584 int dx = x-geom->x0, dy = y-geom->y0 + config.size;
1585 int dx1 = dx + glyph->left;
1586 int dx2 = dx + glyph->right;
1587 if( dx1 < geom->x1 ) geom->x1 = dx1;
1588 if( dx2 > geom->x2 ) geom->x2 = dx2;
1589 int dy1 = dy - glyph->top;
1590 int dy2 = dy - glyph->bottom;
1591 if( dy1 < geom->y1 ) geom->y1 = dy1;
1592 if( dy2 > geom->y2 ) geom->y2 = dy2;
1593 //printf("charcode '%c' %d,%d row bbox %d,%d %d,%d %d,%d\n",
1594 // (int)wchar, x,y, geom->x0,geom->y0, geom->x1,geom->y1, geom->x2,geom->y2);
1596 if( wlen > 0 && config.wtext[wlen-1] != '\n' ) {
1600 geom->x1 = geom->y1 = 0;
1601 geom->x2 = geom->y2 = 0;
1605 int TitleMain::draw_mask()
1607 int old_visible_row1 = visible_row1;
1608 int old_visible_row2 = visible_row2;
1610 // Determine y of visible text
1611 if(config.motion_strategy == BOTTOM_TO_TOP) {
1612 // printf("TitleMain::draw_mask 1 %d %lld %lld %lld\n",
1613 // config.motion_strategy,
1614 // get_source_position(),
1615 // get_source_start(),
1616 // config.prev_keyframe_position);
1617 float magnitude = config.pixels_per_second *
1618 (get_source_position() - config.prev_keyframe_position) /
1619 PluginVClient::project_frame_rate;
1621 int loop_size = text_h + input->get_h();
1622 magnitude -= (int)(magnitude / loop_size) * loop_size;
1624 text_y1 = config.y + input->get_h() - magnitude;
1627 if(config.motion_strategy == TOP_TO_BOTTOM) {
1628 float magnitude = config.pixels_per_second *
1629 (get_source_position() - config.prev_keyframe_position) /
1630 PluginVClient::project_frame_rate;
1633 int loop_size = text_h + input->get_h();
1634 magnitude -= (int)(magnitude / loop_size) * loop_size;
1636 text_y1 = config.y + magnitude;
1639 else if(config.vjustification == JUSTIFY_TOP) {
1642 else if(config.vjustification == JUSTIFY_MID) {
1643 text_y1 = config.y + input->get_h() / 2 - text_h / 2;
1645 else if(config.vjustification == JUSTIFY_BOTTOM) {
1646 text_y1 = config.y + input->get_h() - text_h;
1649 text_y2 = text_y1 + text_h + 0.5;
1651 // Determine x of visible text
1652 if(config.motion_strategy == RIGHT_TO_LEFT) {
1653 float magnitude = config.pixels_per_second *
1654 (get_source_position() - config.prev_keyframe_position) /
1655 PluginVClient::project_frame_rate;
1657 int loop_size = text_w + input->get_w();
1658 magnitude -= (int)(magnitude / loop_size) * loop_size;
1660 text_x1 = config.x + (float)input->get_w() - magnitude;
1662 else if(config.motion_strategy == LEFT_TO_RIGHT) {
1663 float magnitude = config.pixels_per_second *
1664 (get_source_position() - config.prev_keyframe_position) /
1665 PluginVClient::project_frame_rate;
1667 int loop_size = text_w + input->get_w();
1668 magnitude -= (int)(magnitude / loop_size) * loop_size;
1670 text_x1 = config.x + -(float)text_w + magnitude;
1672 else if(config.hjustification == JUSTIFY_LEFT) {
1675 else if(config.hjustification == JUSTIFY_MID) {
1676 text_x1 = config.x + input->get_w() / 2 - text_w / 2;
1678 else if(config.hjustification == JUSTIFY_RIGHT) {
1679 text_x1 = config.x + input->get_w() - text_w;
1681 text_x2 = text_x1 + text_w;
1683 // bottom of this row is visible
1686 while( visible_row1 < text_rows ) {
1687 geom = &row_geom[visible_row1];
1688 int y0 = text_y1 + geom->bottom();
1693 // top of next row is not visible
1694 visible_row2 = visible_row1;
1695 while( visible_row2 < text_rows ) {
1696 geom = &row_geom[visible_row2];
1697 int y0 = text_y1 + geom->top();
1698 if( y0 >= input->get_h() ) break;
1702 if( visible_row1 == visible_row2 ) return 1;
1704 //printf("visible rows %d to %d\n", visible_row1, visible_row2);
1705 // Only use visible rows, get bbox including kerns
1706 // text origin, no kerning
1707 geom = &row_geom[visible_row1];
1708 extent.x0 = geom->x0; extent.y0 = geom->y0;
1709 extent.x1 = geom->left(); extent.y1 = geom->top();
1710 extent.x2 = geom->right(); extent.y2 = geom->bottom();
1712 for( int i=visible_row1; i<visible_row2; ++i ) {
1713 geom = &row_geom[i]; int v;
1714 if( (v=geom->left()) < extent.x1 ) extent.x1 = v;
1715 if( (v=geom->top()) < extent.y1 ) extent.y1 = v;
1716 if( (v=geom->right()) > extent.x2 ) extent.x2 = v;
1717 if( (v=geom->bottom()) > extent.y2 ) extent.y2 = v;
1720 //printf("exts x0,y0=%d,%d x1,y1=%d,%d x2,y2=%d,%d text %f,%f %dx%d\n",
1721 // extent.x0,extent.y0, extent.x1,extent.y1, extent.x2,extent.y2,
1722 // text_x1,text_y1, text_w,text_h);
1723 // Only draw visible chars
1724 visible_char1 = visible_char2 = -1;
1725 int wlen = config.wlen;
1726 for(int i = 0; i < wlen; i++) {
1727 char_pos_t *pos = char_pos + i;
1728 if( pos->row < visible_row1 ) continue;
1729 if( pos->row >= visible_row2 ) continue;
1730 if(visible_char1 < 0) visible_char1 = i;
1735 extent.x1 -= config.outline_size*2;
1736 extent.y1 -= config.outline_size*2;
1737 extent.x2 += config.dropshadow + config.outline_size*2;
1738 extent.y2 += config.dropshadow + config.outline_size*2;
1740 // Determine mask geometry
1741 mask_w = extent.x2 - extent.x1;
1742 if( mask_w <= 0 ) return 1;
1743 mask_h = extent.y2 - extent.y1;
1744 if( mask_h <= 0 ) return 1;
1746 //printf("TitleMain::draw_mask %d-%d frame %dx%d\n",
1747 // visible_row1, visible_row2, mask_w,mask_h)
1748 int need_redraw = 0;
1749 if(text_mask && (text_mask->get_w() != mask_w || text_mask->get_h() != mask_h)) {
1750 delete text_mask; text_mask = 0;
1751 delete text_mask_stroke; text_mask_stroke = 0;
1755 // Always use 8 bit because the glyphs are 8 bit
1756 // Need to set YUV to get clear_frame to set the right chroma.
1757 int output_model = get_output()->get_color_model();
1758 int color_model = BC_CModels::is_yuv(output_model) ? BC_YUVA8888 : BC_RGBA8888;
1759 text_mask = new VFrame;
1760 text_mask->set_use_shm(0);
1761 text_mask->reallocate(0, -1, 0, 0, 0, mask_w, mask_h, color_model, -1);
1762 int drop = !config.dropshadow ? 0 : config.dropshadow;
1763 int drop_w = mask_w + drop;
1764 int drop_h = mask_h + drop;
1765 text_mask_stroke = new VFrame;
1766 text_mask_stroke->set_use_shm(0);
1767 text_mask_stroke->reallocate(0, -1, 0, 0, 0, drop_w, drop_h, color_model, -1);
1771 // Draw on text mask if it has changed
1773 old_visible_row1 != visible_row1 ||
1774 old_visible_row2 != visible_row2 ) {
1775 //printf("redraw %d to %d %d,%d %d,%d - %d,%d\n", visible_char1,visible_char2,
1776 // extent.x0, extent.y0, extent.x1,extent.y1, extent.x2,extent.y2);
1778 text_mask->clear_frame();
1779 text_mask_stroke->clear_frame();
1781 unsigned char *data = text_mask->get_data(); // draw bbox on text_mask
1782 for( int x=0; x<mask_w; ++x ) data[4*x+3] = 0xff;
1783 for( int x=0; x<mask_w; ++x ) data[4*((mask_h-1)*mask_w+x)+3] = 0xff;
1784 for( int y=0; y<mask_h; ++y ) data[4*mask_w*y+3] = 0xff;
1785 for( int y=0; y<mask_h; ++y ) data[4*(mask_w*y + mask_w-1)+3] = 0xff;
1788 title_engine = new TitleEngine(this, cpus);
1790 // Draw dropshadow first
1791 if(config.dropshadow) {
1792 title_engine->do_dropshadow = 1;
1793 title_engine->set_package_count(visible_char2 - visible_char1);
1794 title_engine->process_packages();
1797 // Then draw foreground
1798 title_engine->do_dropshadow = 0;
1799 title_engine->set_package_count(visible_char2 - visible_char1);
1800 title_engine->process_packages();
1802 // Convert to text outlines
1803 if(config.outline_size > 0) {
1805 (text_mask->get_w() != outline_mask->get_w() ||
1806 text_mask->get_h() != outline_mask->get_h())) {
1807 delete outline_mask; outline_mask = 0;
1811 outline_mask = new VFrame;
1812 outline_mask->set_use_shm(0);
1813 outline_mask->reallocate(0, -1, 0, 0, 0,
1814 text_mask->get_w(), text_mask->get_h(),
1815 text_mask->get_color_model(), -1);
1817 if(!outline_engine) outline_engine =
1818 new TitleOutlineEngine(this, cpus);
1819 outline_engine->do_outline();
1826 void TitleMain::overlay_mask()
1829 //printf("TitleMain::overlay_mask 1\n");
1831 if(!EQUIV(config.fade_in, 0))
1833 int fade_len = lroundf(config.fade_in * PluginVClient::project_frame_rate);
1834 int fade_position = get_source_position() - config.prev_keyframe_position;
1836 if(fade_position >= 0 && fade_position < fade_len)
1838 alpha = lroundf(256.0f * fade_position / fade_len);
1841 if(!EQUIV(config.fade_out, 0))
1843 int fade_len = lroundf(config.fade_out * PluginVClient::project_frame_rate);
1844 int fade_position = config.next_keyframe_position - get_source_position();
1847 if(fade_position >= 0 && fade_position < fade_len)
1849 alpha = lroundf(256.0f * fade_position / fade_len);
1854 translate = new TitleTranslate(this, cpus);
1856 if(config.dropshadow)
1858 text_x1 += config.dropshadow;
1859 if(text_x1 < input->get_w() && text_x1 + text_w > 0)
1861 // Do 2 passes if dropshadow.
1862 int temp_color = config.color;
1864 translate->run_packages();
1865 config.color = temp_color;
1867 text_x1 -= config.dropshadow;
1869 //printf("TitleMain::overlay_mask 1\n");
1871 if(text_x1 < input->get_w() && text_x1 + text_w > 0) {
1872 translate->run_packages();
1873 if (config.stroke_width >= ZERO && (config.style & BC_FONT_OUTLINE)) {
1874 int temp_color = config.color;
1875 VFrame *tmp_text_mask = this->text_mask;
1876 config.color = config.color_stroke;
1877 this->text_mask = this->text_mask_stroke;
1879 translate->run_packages();
1880 config.color = temp_color;
1881 this->text_mask = tmp_text_mask;
1884 //printf("TitleMain::overlay_mask 200\n");
1887 void TitleMain::get_color_components(int *r, int *g, int *b, int *a, int is_outline)
1889 int color = is_outline ? config.outline_color : config.color;
1890 unsigned char r_in = color >> 16;
1891 unsigned char g_in = color >> 8;
1892 unsigned char b_in = color;
1893 *a = is_outline ? config.outline_alpha : config.alpha;
1895 switch(output->get_color_model()) {
1897 yuv.rgb_to_yuv_8(r_in, g_in, b_in, *r, *g, *b);
1900 yuv.rgb_to_yuv_8(r_in, g_in, b_in, *r, *g, *b);
1903 *r = r_in; *g = g_in; *b = b_in;
1908 void TitleMain::clear_glyphs()
1910 //printf("TitleMain::clear_glyphs 1\n");
1911 glyphs.remove_all_objects();
1914 const char* TitleMain::motion_to_text(int motion)
1918 case NO_MOTION: return _("No motion"); break;
1919 case BOTTOM_TO_TOP: return _("Bottom to top"); break;
1920 case TOP_TO_BOTTOM: return _("Top to bottom"); break;
1921 case RIGHT_TO_LEFT: return _("Right to left"); break;
1922 case LEFT_TO_RIGHT: return _("Left to right"); break;
1927 int TitleMain::text_to_motion(const char *text)
1929 for(int i = 0; i < TOTAL_PATHS; i++)
1931 if(!strcasecmp(motion_to_text(i), text)) return i;
1936 void TitleMain::reset_render()
1938 delete text_mask; text_mask = 0;
1939 delete glyph_engine; glyph_engine = 0;
1940 delete [] char_pos; char_pos = 0;
1941 delete text_mask_stroke; text_mask_stroke = 0;
1942 delete [] row_geom; row_geom = 0;
1944 visible_row1 = 0; visible_row2 = 0;
1945 visible_char1 = 0; visible_char2 = 0;
1949 FT_Done_Face(freetype_face);
1954 int TitleMain::init_freetype()
1956 if(!freetype_library)
1957 FT_Init_FreeType(&freetype_library);
1958 if(!freetype_face) {
1959 BC_FontEntry *font = get_font();
1960 if(load_freetype_face(freetype_library, freetype_face, font->path)) {
1961 printf("TitleMain::process_realtime %s: FT_New_Face failed.\n",
1965 FT_Set_Pixel_Sizes(freetype_face, config.size, 0);
1970 int TitleMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
1974 output = output_ptr;
1976 need_reconfigure |= load_configuration();
1979 if(config.size <= 0 || config.size >= 2048)
1981 if(config.stroke_width < 0 || config.stroke_width >= 512)
1982 config.stroke_width = 0.0;
1985 if(!strlen(config.encoding))
1986 strcpy(config.encoding, DEFAULT_ENCODING);
1988 // Always synthesize text and redraw it for timecode
1991 int64_t rendered_frame = get_source_position();
1992 if (get_direction() == PLAY_REVERSE)
1993 rendered_frame -= 1;
1995 char text[BCTEXTLEN];
1997 (double)rendered_frame / PluginVClient::project_frame_rate,
1998 config.timecode_format,
1999 PluginVClient::get_project_samplerate(),
2000 PluginVClient::get_project_framerate(),
2002 config.to_wtext(config.encoding, text, strlen(text)+1);
2003 need_reconfigure = 1;
2006 // printf("TitleMain::process_realtime %d need_reconfigure=%d\n",
2008 // need_reconfigure);
2010 // Handle reconfiguration
2011 if(need_reconfigure) {
2013 result = init_freetype();
2017 get_total_extents();
2018 need_reconfigure = 0;
2024 // Determine region of text visible on the output and draw mask
2025 result = draw_mask();
2029 // Overlay mask on output
2038 void TitleMain::update_gui()
2042 int reconfigure = load_configuration();
2045 TitleWindow *window = (TitleWindow*)thread->window;
2046 window->lock_window("TitleMain::update_gui");
2048 window->unlock_window();
2049 window->color_thread->update_gui(config.color, 0);
2050 window->unlock_window();
2055 int TitleMain::load_configuration()
2057 KeyFrame *prev_keyframe, *next_keyframe;
2058 prev_keyframe = get_prev_keyframe(get_source_position());
2059 next_keyframe = get_next_keyframe(get_source_position());
2061 TitleConfig old_config, prev_config, next_config;
2062 old_config.copy_from(config);
2063 read_data(prev_keyframe);
2064 prev_config.copy_from(config);
2065 read_data(next_keyframe);
2066 next_config.copy_from(config);
2068 config.prev_keyframe_position = prev_keyframe->position;
2069 config.next_keyframe_position = next_keyframe->position;
2071 // if no previous keyframe exists, it should be start of the plugin, not start of the track
2072 if(config.next_keyframe_position == config.prev_keyframe_position)
2073 config.next_keyframe_position = get_source_start() + get_total_len();
2074 if (config.prev_keyframe_position == 0)
2075 config.prev_keyframe_position = get_source_start();
2076 // printf("TitleMain::load_configuration 10 %d %d\n",
2077 // config.prev_keyframe_position,
2078 // config.next_keyframe_position);
2080 config.interpolate(prev_config,
2082 (next_keyframe->position == prev_keyframe->position) ?
2083 get_source_position() :
2084 prev_keyframe->position,
2085 (next_keyframe->position == prev_keyframe->position) ?
2086 get_source_position() + 1 :
2087 next_keyframe->position,
2088 get_source_position());
2090 if(!config.equivalent(old_config))
2096 void TitleMain::save_data(KeyFrame *keyframe)
2100 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
2101 output.tag.set_title("TITLE");
2102 output.tag.set_property("FONT", config.font);
2103 output.tag.set_property("ENCODING", config.encoding);
2104 output.tag.set_property("STYLE", (int64_t)config.style);
2105 output.tag.set_property("SIZE", config.size);
2106 output.tag.set_property("COLOR", config.color);
2107 output.tag.set_property("COLOR_STROKE", config.color_stroke);
2108 output.tag.set_property("STROKE_WIDTH", config.stroke_width);
2109 output.tag.set_property("OUTLINE_COLOR", config.outline_color);
2110 output.tag.set_property("ALPHA", config.alpha);
2111 output.tag.set_property("OUTLINE_ALPHA", config.outline_alpha);
2112 output.tag.set_property("MOTION_STRATEGY", config.motion_strategy);
2113 output.tag.set_property("LOOP", config.loop);
2114 output.tag.set_property("LINE_PITCH", config.line_pitch);
2115 output.tag.set_property("PIXELS_PER_SECOND", config.pixels_per_second);
2116 output.tag.set_property("HJUSTIFICATION", config.hjustification);
2117 output.tag.set_property("VJUSTIFICATION", config.vjustification);
2118 output.tag.set_property("FADE_IN", config.fade_in);
2119 output.tag.set_property("FADE_OUT", config.fade_out);
2120 output.tag.set_property("TITLE_X", config.x);
2121 output.tag.set_property("TITLE_Y", config.y);
2122 output.tag.set_property("DROPSHADOW", config.dropshadow);
2123 output.tag.set_property("OUTLINE_SIZE", config.outline_size);
2124 output.tag.set_property("TIMECODE", config.timecode);
2125 output.tag.set_property("TIMECODEFORMAT", config.timecode_format);
2126 output.tag.set_property("WINDOW_W", config.window_w);
2127 output.tag.set_property("WINDOW_H", config.window_h);
2128 output.append_tag();
2129 output.append_newline();
2130 char text[BCTEXTLEN];
2131 int text_len = BC_Resources::encode(
2132 BC_Resources::wide_encoding, DEFAULT_ENCODING,
2133 (char*)config.wtext, config.wlen*sizeof(wchar_t),
2134 text, sizeof(text));
2135 output.append_text(text, text_len);
2136 output.tag.set_title("/TITLE");
2137 output.append_tag();
2138 output.append_newline();
2139 output.terminate_string();
2140 //printf("TitleMain::save_data 1\n%s\n", output.string);
2141 //printf("TitleMain::save_data 2\n%s\n", config.text);
2144 void TitleMain::read_data(KeyFrame *keyframe)
2148 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
2152 config.prev_keyframe_position = keyframe->position;
2155 result = input.read_tag();
2158 if(input.tag.title_is("TITLE")) {
2159 input.tag.get_property("FONT", config.font);
2160 input.tag.get_property("ENCODING", config.encoding);
2161 config.style = input.tag.get_property("STYLE", (int64_t)config.style);
2162 config.size = input.tag.get_property("SIZE", config.size);
2163 config.color = input.tag.get_property("COLOR", config.color);
2164 config.color_stroke = input.tag.get_property("COLOR_STROKE", config.color_stroke);
2165 config.stroke_width = input.tag.get_property("STROKE_WIDTH", config.stroke_width);
2166 config.outline_color = input.tag.get_property("OUTLINE_COLOR", config.outline_color);
2167 config.alpha = input.tag.get_property("ALPHA", config.alpha);
2168 config.outline_alpha = input.tag.get_property("OUTLINE_ALPHA", config.outline_alpha);
2169 config.motion_strategy = input.tag.get_property("MOTION_STRATEGY", config.motion_strategy);
2170 config.loop = input.tag.get_property("LOOP", config.loop);
2171 config.line_pitch = input.tag.get_property("LINE_PITCH", config.line_pitch);
2172 config.pixels_per_second = input.tag.get_property("PIXELS_PER_SECOND", config.pixels_per_second);
2173 config.hjustification = input.tag.get_property("HJUSTIFICATION", config.hjustification);
2174 config.vjustification = input.tag.get_property("VJUSTIFICATION", config.vjustification);
2175 config.fade_in = input.tag.get_property("FADE_IN", config.fade_in);
2176 config.fade_out = input.tag.get_property("FADE_OUT", config.fade_out);
2177 config.x = input.tag.get_property("TITLE_X", config.x);
2178 config.y = input.tag.get_property("TITLE_Y", config.y);
2179 config.dropshadow = input.tag.get_property("DROPSHADOW", config.dropshadow);
2180 config.outline_size = input.tag.get_property("OUTLINE_SIZE", config.outline_size);
2181 config.timecode = input.tag.get_property("TIMECODE", config.timecode);
2182 input.tag.get_property("TIMECODEFORMAT", config.timecode_format);
2183 config.window_w = input.tag.get_property("WINDOW_W", config.window_w);
2184 config.window_h = input.tag.get_property("WINDOW_H", config.window_h);
2185 const char *text = input.read_text();
2186 config.to_wtext(config.encoding, text, strlen(text)+1);
2188 else if(input.tag.title_is("/TITLE")) {