sync to last commit on google_code
[goodguy/history.git] / cinelerra-5.0 / plugins / titler / title.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
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>
27
28
29
30
31 #include "bcsignals.h"
32 #include "clip.h"
33 #include "colormodels.h"
34 #include "filexml.h"
35 #include "filesystem.h"
36 #include "transportque.inc"
37 #include "ft2build.h"
38 #include FT_GLYPH_H
39 #include FT_BBOX_H
40 #include FT_OUTLINE_H
41 #include FT_STROKER_H
42 #include "language.h"
43 #include "mwindow.inc"
44 #include "cicolors.h"
45 #include "title.h"
46 #include "titlewindow.h"
47 #include "transportque.inc"
48
49
50 #include <errno.h>
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <endian.h>
55 #include <byteswap.h>
56 #include <iconv.h>
57 #include <sys/stat.h>
58 #include <fontconfig/fontconfig.h>
59
60 #define ZERO (1.0 / 64.0)
61
62 REGISTER_PLUGIN(TitleMain)
63
64 #ifdef X_HAVE_UTF8_STRING
65 #define DEFAULT_ENCODING "UTF-8"
66 #else
67 #define DEFAULT_ENCODING "ISO8859-1"
68 #endif
69 #define DEFAULT_TIMECODEFORMAT TIME_HMS
70
71 static YUV yuv;
72
73 TitleConfig::TitleConfig()
74 {
75         style = 0;
76         color = BLACK;
77         alpha = 0xff;
78         outline_alpha = 0xff;
79         size = 24;
80         motion_strategy = NO_MOTION;
81         loop = 0;
82         line_pitch = 0;
83         hjustification = JUSTIFY_CENTER;
84         vjustification = JUSTIFY_MID;
85         fade_in = 0.0;
86         fade_out = 0.0;
87         x = 0.0;
88         y = 0.0;
89         dropshadow = 2;
90         sprintf(font, "fixed");
91         sprintf(encoding, DEFAULT_ENCODING);
92         timecode_format = DEFAULT_TIMECODEFORMAT;
93         pixels_per_second = 1.0;
94         timecode = 0;
95         stroke_width = 1.0;
96         wtext[0] = 0;  wlen = 0;
97         color_stroke = 0xff0000;
98         outline_color = WHITE;
99
100         outline_size = 0;
101         window_w = 640;
102         window_h = 480;
103         next_keyframe_position = 0;
104         prev_keyframe_position = 0;
105 }
106
107 TitleConfig::~TitleConfig()
108 {
109 }
110
111 // Does not test equivalency but determines if redrawing text is necessary.
112 int TitleConfig::equivalent(TitleConfig &that)
113 {
114         return dropshadow == that.dropshadow &&
115                 style == that.style &&
116                 size == that.size &&
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) &&
132                 wlen == that.wlen &&
133                 !memcmp(wtext, that.wtext, wlen * sizeof(wchar_t));
134 }
135
136 void TitleConfig::copy_from(TitleConfig &that)
137 {
138         strcpy(font, that.font);
139         style = that.style;
140         size = that.size;
141         color = that.color;
142         color_stroke = that.color_stroke;
143         stroke_width = that.stroke_width;
144         outline_color = that.outline_color;
145         alpha = that.alpha;
146         outline_alpha = that.outline_alpha;
147         pixels_per_second = that.pixels_per_second;
148         motion_strategy = that.motion_strategy;
149         loop = that.loop;
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;
155         x = that.x;
156         y = that.y;
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));
163         wlen = that.wlen;
164         window_w = that.window_w;
165         window_h = that.window_h;
166
167         limits();
168 }
169
170 void TitleConfig::interpolate(TitleConfig &prev,
171         TitleConfig &next,
172         int64_t prev_frame,
173         int64_t next_frame,
174         int64_t current_frame)
175 {
176         strcpy(font, prev.font);
177         strcpy(encoding, prev.encoding);
178         style = prev.style;
179         size = prev.size;
180         color = prev.color;
181         color_stroke = prev.color_stroke;
182         stroke_width = prev.stroke_width;
183         outline_color = prev.outline_color;
184         alpha = prev.alpha;
185         outline_alpha = prev.outline_alpha;
186         motion_strategy = prev.motion_strategy;
187         loop = prev.loop;
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));
196         wlen = prev.wlen;
197         wtext[wlen] = 0;
198
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;
203 //      this->x = prev.x;
204 //      this->y = prev.y;
205         timecode = prev.timecode;
206         timecode_format = prev.timecode_format;
207         this->dropshadow = prev.dropshadow * prev_scale + next.dropshadow * next_scale;
208 //      this->dropshadow = prev.dropshadow;
209 }
210
211 void TitleConfig::limits()
212 {
213         if(window_w < 100) window_w = 100;
214         if(window_h < 100) window_h = 100;
215 }
216
217
218
219
220 void TitleConfig::to_wtext(const char *from_enc, const char *text, int tlen)
221 {
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;
225 }
226
227
228 TitleGlyph::TitleGlyph()
229 {
230         char_code = 0;
231         data = 0;
232         data_stroke = 0;
233         freetype_index = 0;
234         width = 0;
235         height = 0;
236         pitch = 0;
237         left = 0;
238         top = 0;
239         bottom = 0;
240         right = 0;
241         advance_x = 0;
242 }
243
244
245 TitleGlyph::~TitleGlyph()
246 {
247 //printf("TitleGlyph::~TitleGlyph 1\n");
248         if(data) delete data;
249         if(data_stroke) delete data_stroke;
250 }
251
252
253
254
255
256
257
258
259
260
261
262 GlyphPackage::GlyphPackage() : LoadPackage()
263 {
264         glyph = 0;
265 }
266
267 GlyphUnit::GlyphUnit(TitleMain *plugin, GlyphEngine *server)
268  : LoadClient(server)
269 {
270         this->plugin = plugin;
271         current_font = 0;
272         freetype_library = 0;
273         freetype_face = 0;
274 }
275
276 GlyphUnit::~GlyphUnit()
277 {
278         if(freetype_library)
279                 FT_Done_FreeType(freetype_library);
280 }
281
282 void GlyphUnit::process_package(LoadPackage *package)
283 {
284         GlyphPackage *pkg = (GlyphPackage*)package;
285         TitleGlyph *glyph = pkg->glyph;
286         int result = 0;
287         char new_path[BCTEXTLEN];
288
289         current_font = plugin->get_font();
290
291         if(plugin->load_freetype_face(freetype_library, freetype_face,
292                         current_font->path)) {
293                 printf(_("GlyphUnit::process_package FT_New_Face failed.\n"));
294                 result = 1;
295         }
296
297         if(!result) {
298                 int gindex = FT_Get_Char_Index(freetype_face, glyph->char_code);
299
300 //printf("GlyphUnit::process_package 1 %c\n", glyph->char_code);
301 // Char not found
302                 if(gindex == 0) {
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))
306                         {
307                                 plugin->load_freetype_face(freetype_library,
308                                         freetype_face, new_path);
309                                 gindex = FT_Get_Char_Index(freetype_face, glyph->char_code);
310                         }
311                 }
312                 FT_Set_Pixel_Sizes(freetype_face, plugin->config.size, 0);
313
314                 if (gindex == 0) {
315 // carrige return
316                         if (glyph->char_code != 10)  
317                                 printf(_("GlyphUnit::process_package FT_Load_Char failed - char: %li.\n"),
318                                         glyph->char_code);
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;
328
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();
334                         }
335                 }
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;
340                         FT_BBox bbox;
341                         FT_Bitmap bm;
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);
347
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;
352                         bm.num_grays = 256;
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);
361         
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,
369                                 &bm);
370                         FT_Done_Glyph(glyph_image);
371                 }
372                 else {
373 // Outline desired and glyph found
374                         FT_Glyph glyph_image;
375                         FT_Stroker stroker;
376                         FT_Outline outline;
377                         FT_Bitmap bm;
378                         FT_BBox bbox;
379                         FT_UInt npoints, ncontours;     
380
381                         FT_Load_Glyph(freetype_face, gindex, FT_LOAD_DEFAULT);
382                         FT_Get_Glyph(freetype_face->glyph, &glyph_image);
383
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;
392                                 glyph->pitch = 0;
393                                 glyph->data =
394                                         new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
395                                 glyph->data_stroke =
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;
399                                 return;
400                         }
401 #if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 2)
402                         FT_Stroker_New(freetype_library, &stroker);
403 #else
404                         FT_Stroker_New(((FT_LibraryRec *)freetype_library)->memory, &stroker);
405 #endif
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;
417                                 glyph->pitch = 0;
418                                 glyph->data =
419                                         new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
420                                 glyph->data_stroke =
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;
424                                 return;
425                         };
426
427                         FT_Outline_New(freetype_library, npoints, ncontours, &outline);
428                         outline.n_points=0;
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,
434                                         - bbox.xMin,
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);
440         
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;
445                         bm.num_grays = 256;
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);
460
461
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,
471                                 &bm);   
472                         bm.buffer=glyph->data_stroke->get_data();
473                         FT_Outline_Get_Bitmap( freetype_library,
474                         &outline,
475                                 &bm);
476                         FT_Outline_Done(freetype_library,&outline);
477                         FT_Stroker_Done(stroker);
478                         FT_Done_Glyph(glyph_image);
479
480 //printf("GlyphUnit::process_package 2\n");
481                 }
482         }
483 }
484
485
486
487 GlyphEngine::GlyphEngine(TitleMain *plugin, int cpus)
488  : LoadServer(cpus, cpus)
489 {
490         this->plugin = plugin;
491 }
492
493 void GlyphEngine::init_packages()
494 {
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];
500                 }
501         }
502 }
503
504 LoadClient* GlyphEngine::new_client()
505 {
506         return new GlyphUnit(plugin, this);
507 }
508
509 LoadPackage* GlyphEngine::new_package()
510 {
511         return new GlyphPackage;
512 }
513
514
515
516
517
518 // Copy a single character to the text mask
519 TitlePackage::TitlePackage()
520  : LoadPackage()
521 {
522         x = y = 0;
523         char_code = 0;
524 }
525
526
527 TitleUnit::TitleUnit(TitleMain *plugin, TitleEngine *server)
528  : LoadClient(server)
529 {
530         this->plugin = plugin;
531         this->engine = server;
532 }
533
534 void TitleUnit::draw_glyph(VFrame *output, VFrame *data, TitleGlyph *glyph, int x, int y)
535 {
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();
540
541         int baseline = plugin->get_char_height();
542         if( engine->do_dropshadow ) {
543                 x += plugin->config.dropshadow;
544                 y += plugin->config.dropshadow;
545         }
546         else if( plugin->config.dropshadow < 0 ) {
547                 x -= plugin->config.dropshadow;
548                 y -= plugin->config.dropshadow;
549         }
550
551         int x_in = 0, y_in = 0;
552         int x_out = x + glyph->left;
553         if( x_out < 0 ) { x_in = -x_out;  x_out = 0; }
554         if( x_out+glyph_w > output_w ) glyph_w = output_w-x_out;
555         if( x_in >= glyph_w || x_out >= output_w ) return;
556         int y_out = y + baseline - glyph->top;
557         if( y_out < 0 ) { y_in = -y_out;  y_out = 0; }
558         if( y_out+glyph_h > output_h ) glyph_h = output_h-y_out;
559         if( y_in >= glyph_h || y_out >= output_h ) return;
560
561         if(engine->do_dropshadow) {
562                 while( y_in < glyph_h && y_out < output_h ) {
563                         unsigned char *in_row = in_rows[y_in];
564                         unsigned char *out_row = out_rows[y_out];
565                         for( int xin=x_in,xout=x_out*4+3; xin<glyph_w; ++xin,xout+=4 ) {
566                                 out_row[xout] = in_row[xin];
567                         }
568                         ++y_in;  ++y_out;
569                 }
570                 return;
571         }
572
573         int r, g, b, a;
574         plugin->get_color_components(&r, &g, &b, &a, 0);
575         //int outline = plugin->config.outline_size;
576         //if(outline) a = 0xff;
577
578         while( y_in < glyph_h && y_out < output_h ) {
579                 unsigned char *in_row = in_rows[y_in];
580                 unsigned char *out_row = out_rows[y_out];
581                 for( int xin=x_in,xout=x_out*4+0; xin<glyph_w; ++xin,xout+=4 ) {
582                         int in_a = in_row[xin], out_a = out_row[xout+3];
583                         if( in_a + out_a == 0 ) continue;  // alpha blundering
584                         int opacity = in_a * a, transp = out_a * (0xff - opacity/0xff);
585                         out_row[xout+0] = (opacity * r + transp * out_row[xout+0]) / (0xff*0xff);
586                         out_row[xout+1] = (opacity * g + transp * out_row[xout+1]) / (0xff*0xff);
587                         out_row[xout+2] = (opacity * b + transp * out_row[xout+2]) / (0xff*0xff);
588                         out_row[xout+3] = (opacity + transp) / 0xff;
589                 }
590                 ++y_in;  ++y_out;
591         }
592 }
593
594
595 void TitleUnit::process_package(LoadPackage *package)
596 {
597         TitlePackage *pkg = (TitlePackage*)package;
598
599         if( pkg->char_code == 0 || pkg->char_code == '\n') return;
600         TitleGlyph *glyph = plugin->get_glyph(pkg->char_code);
601         if( !glyph ) return;
602         draw_glyph(plugin->text_mask, glyph->data, glyph, pkg->x, pkg->y);
603         if(plugin->config.stroke_width >= ZERO && (plugin->config.style & BC_FONT_OUTLINE))
604                 draw_glyph(plugin->text_mask_stroke, glyph->data_stroke, glyph, pkg->x, pkg->y);
605 }
606
607 TitleEngine::TitleEngine(TitleMain *plugin, int cpus)
608  : LoadServer(cpus, cpus)
609 {
610         this->plugin = plugin;
611 }
612
613 void TitleEngine::init_packages()
614 {
615         int current_package = 0;
616         int dx = plugin->config.outline_size - plugin->extent.x1;
617         int dy = plugin->config.outline_size - plugin->extent.y1;
618         for(int i = plugin->visible_char1; i < plugin->visible_char2; i++) {
619                 TitlePackage *pkg = (TitlePackage*)get_package(current_package++);
620                 char_pos_t *pos = plugin->char_pos + i;
621                 pkg->x = pos->x + dx;
622                 pkg->y = pos->y + dy;
623                 pkg->char_code = plugin->config.wtext[i];
624 //printf("draw '%c' at %d,%d\n",(int)pkg->char_code, pkg->x, pkg->y);
625         }
626 }
627
628 LoadClient* TitleEngine::new_client()
629 {
630         return new TitleUnit(plugin, this);
631 }
632
633 LoadPackage* TitleEngine::new_package()
634 {
635         return new TitlePackage;
636 }
637
638 void TitleTranslateUnit::translation_array_f(transfer_table_f* &table,
639         float out_x1,
640         float out_x2,
641         float in_x1,
642         float in_x2,
643         int in_total,
644         int out_total,
645         int &out_x1_int,
646         int &out_x2_int)
647 {
648         int out_w_int;
649         //float offset = out_x1 - in_x1;
650
651         out_x1_int = (int)out_x1;
652         out_x2_int = MIN((int)ceil(out_x2), out_total);
653         out_w_int = out_x2_int - out_x1_int;
654
655         table = new transfer_table_f[out_w_int];
656         bzero(table, sizeof(transfer_table_f) * out_w_int);
657
658         float in_x = in_x1;
659         for(int out_x = out_x1_int; out_x < out_x2_int; out_x++)
660         {
661                 transfer_table_f *entry = &table[out_x - out_x1_int];
662
663                 entry->in_x1 = (int)in_x;
664                 entry->in_x2 = (int)in_x + 1;
665
666 // Get fraction of output pixel to fill
667                 entry->output_fraction = 1;
668
669                 if(out_x1 > out_x)
670                 {
671                         entry->output_fraction -= out_x1 - out_x;
672                 }
673
674                 if(out_x2 < out_x + 1)
675                 {
676                         entry->output_fraction = (out_x2 - out_x);
677                 }
678
679 // Advance in_x until out_x_fraction is filled
680                 float out_x_fraction = entry->output_fraction;
681                 float in_x_fraction = floor(in_x + 1) - in_x;
682
683                 if(out_x_fraction <= in_x_fraction)
684                 {
685                         entry->in_fraction1 = out_x_fraction;
686                         entry->in_fraction2 = 0.0;
687                         in_x += out_x_fraction;
688                 }
689                 else
690                 {
691                         entry->in_fraction1 = in_x_fraction;
692                         in_x += out_x_fraction;
693                         entry->in_fraction2 = in_x - floor(in_x);
694                 }
695
696 // Clip in_x and zero out fraction.  This doesn't work for YUV.
697                 if(entry->in_x2 >= in_total)
698                 {
699                         entry->in_x2 = in_total - 1;
700                         entry->in_fraction2 = 0.0;
701                 }
702
703                 if(entry->in_x1 >= in_total)
704                 {
705                         entry->in_x1 = in_total - 1;
706                         entry->in_fraction1 = 0.0;
707                 }
708         }
709 }
710
711
712
713 // Copy a single character to the text mask
714 TitleOutlinePackage::TitleOutlinePackage()
715  : LoadPackage()
716 {
717 }
718
719
720 TitleOutlineUnit::TitleOutlineUnit(TitleMain *plugin, TitleOutlineEngine *server)
721  : LoadClient(server)
722 {
723         this->plugin = plugin;
724         this->engine = server;
725 }
726
727 void TitleOutlineUnit::process_package(LoadPackage *package)
728 {
729         TitleOutlinePackage *pkg = (TitleOutlinePackage*)package;
730         int r, g, b, outline_a;
731         plugin->get_color_components(&r, &g, &b, &outline_a, 1);
732         unsigned char **outline_rows = plugin->outline_mask->get_rows();
733         unsigned char **text_rows = plugin->text_mask->get_rows();
734         int mask_w1 = plugin->text_mask->get_w()-1;
735         int mask_h1 = plugin->text_mask->get_h()-1;
736         int ofs = plugin->config.outline_size;
737
738         if(engine->pass == 0) {
739 // get max alpha under outline size macropixel
740                 for(int y = pkg->y1; y < pkg->y2; y++) {
741                         unsigned char *out_row = outline_rows[y];
742                         int y1 = y - ofs, y2 = y + ofs;
743                         CLAMP(y1, 0, mask_h1);  CLAMP(y2, 0, mask_h1);
744                         for(int x = 0; x < plugin->text_mask->get_w(); x++) {
745                                 int x1 = x - ofs, x2 = x + ofs;
746                                 CLAMP(x1, 0, mask_w1);  CLAMP(x2, 0, mask_w1);
747
748                                 int max_a = 0;
749                                 for(int yy = y1; yy <= y2; yy++) {
750                                         unsigned char *text_row = text_rows[yy];
751                                         for(int xx = x1; xx <= x2; ++xx) {
752                                                 unsigned char *pixel = text_row + xx*4;
753                                                 if(pixel[3] > max_a) max_a = pixel[3];
754                                         }
755                                 }
756
757                                 unsigned char *out = out_row + x*4;
758                                 out[0] = r;  out[1] = g;  out[2] = b;
759                                 out[3] = (max_a * outline_a) / 0xff;
760                         }
761                 }
762                 return;
763         }
764         else {
765 // Overlay text mask on top of outline mask
766                 int ofs = BC_CModels::is_yuv(plugin->output->get_color_model()) ? 0x80 : 0;
767                 for(int y = pkg->y1; y < pkg->y2; y++) {
768                         unsigned char *outline_row = outline_rows[y];
769                         unsigned char *text_row = text_rows[y];
770                         for(int x = 0; x < plugin->text_mask->get_w(); x++) {
771                                 unsigned char *out = text_row + x * 4;
772                                 unsigned char *inp = outline_row + x * 4;
773                                 int out_a = out[3], in_a = inp[3];
774                                 int transparency = in_a * (0xff - out_a) / 0xff;
775                                 out[0] = (out[0] * out_a + inp[0] * transparency) / 0xff;
776                                 out[1] = ((out[1]-ofs) * out_a + (inp[1]-ofs) * transparency) / 0xff + ofs;
777                                 out[2] = ((out[2]-ofs) * out_a + (inp[2]-ofs) * transparency) / 0xff + ofs;
778                                 out[3] = in_a + out_a - in_a*out_a / 0xff;
779                         }
780                 }
781         }
782 }
783
784 TitleOutlineEngine::TitleOutlineEngine(TitleMain *plugin, int cpus)
785  : LoadServer(cpus, cpus)
786 {
787         this->plugin = plugin;
788 }
789
790 void TitleOutlineEngine::init_packages()
791 {
792         int mask_h = plugin->text_mask->get_h();
793         if( !mask_h ) return;   
794         int py1 = 0, py2 = 0;
795         int pkgs = get_total_packages();
796         for( int i=0; i<pkgs; py1=py2 ) {
797                 TitleOutlinePackage *pkg = (TitleOutlinePackage*)get_package(i);
798                 py2 = (++i * mask_h)/ pkgs;
799                 pkg->y1 = py1;  pkg->y2 = py2;
800         }
801 }
802                 
803 void TitleOutlineEngine::do_outline()
804 {
805         pass = 0;  process_packages();
806         pass = 1;  process_packages();
807 }
808
809 LoadClient* TitleOutlineEngine::new_client()
810 {
811         return new TitleOutlineUnit(plugin, this);
812 }
813
814 LoadPackage* TitleOutlineEngine::new_package()
815 {
816         return new TitleOutlinePackage;
817 }
818
819
820
821
822 TitleTranslatePackage::TitleTranslatePackage()
823  : LoadPackage()
824 {
825         y1 = y2 = 0;
826 }
827
828
829 TitleTranslateUnit::TitleTranslateUnit(TitleMain *plugin, TitleTranslate *server)
830  : LoadClient(server)
831 {
832         this->plugin = plugin;
833 }
834
835 void TitleTranslate::run_packages()
836 {
837         output_w = plugin->output->get_w();
838         output_h = plugin->output->get_h();
839
840         float x1 = plugin->text_x1 + plugin->extent.x1;
841         float x2 = plugin->text_x1 + plugin->extent.x2;
842         if (x2 <= 0 || x1 >= x2 || x1 >= output_w) return;
843
844         float y1 = plugin->text_y1 + plugin->extent.y1;
845         float y2 = plugin->text_y1 + plugin->extent.y2;
846         if (y2 <= 0 || y1 >= y2 || y1 >= output_h) return;
847
848         process_packages();
849 }
850
851
852
853
854 #define TRANSLATE(type, max, components, ofs) \
855 { \
856         unsigned char **in_rows = plugin->text_mask->get_rows(); \
857         type **out_rows = (type**)plugin->output->get_rows(); \
858  \
859         for(int i = pkg->y1; i < pkg->y2; i++) \
860         { \
861                 if(i + server->out_y1_int >= 0 && \
862                         i + server->out_y1_int < server->output_h) \
863                 { \
864                         int in_y1, in_y2; \
865                         float y_fraction1, y_fraction2; \
866                         in_y1 = server->y_table[i].in_x1; \
867                         in_y2 = server->y_table[i].in_x2; \
868                         y_fraction1 = server->y_table[i].in_fraction1; \
869                         y_fraction2 = server->y_table[i].in_fraction2; \
870                         unsigned char *in_row1 = in_rows[in_y1]; \
871                         unsigned char *in_row2 = in_rows[in_y2]; \
872                         type *out_row = out_rows[i + server->out_y1_int]; \
873  \
874                         for(int j = server->out_x1_int; j < server->out_x2_int; j++) \
875                         { \
876                                 if(j >= 0 && j < server->output_w) \
877                                 { \
878                                         int in_x1; \
879                                         int in_x2; \
880                                         float x_fraction1; \
881                                         float x_fraction2; \
882                                         in_x1 =  \
883                                                 server->x_table[j - server->out_x1_int].in_x1; \
884                                         in_x2 =  \
885                                                 server->x_table[j - server->out_x1_int].in_x2; \
886                                         x_fraction1 =  \
887                                                 server->x_table[j - server->out_x1_int].in_fraction1; \
888                                         x_fraction2 =  \
889                                                 server->x_table[j - server->out_x1_int].in_fraction2; \
890  \
891                                         float fraction1 = x_fraction1 * y_fraction1 / (256.f-max); \
892                                         float fraction2 = x_fraction2 * y_fraction1 / (256.f-max); \
893                                         float fraction3 = x_fraction1 * y_fraction2 / (256.f-max); \
894                                         float fraction4 = x_fraction2 * y_fraction2 / (256.f-max); \
895                                         type input_r = (type)(in_row1[in_x1 * 4 + 0] * fraction1 +  \
896                                                                 in_row1[in_x2 * 4 + 0] * fraction2 +  \
897                                                                 in_row2[in_x1 * 4 + 0] * fraction3 +  \
898                                                                 in_row2[in_x2 * 4 + 0] * fraction4); \
899                                         type input_g = (type)(in_row1[in_x1 * 4 + 1] * fraction1 +  \
900                                                                 in_row1[in_x2 * 4 + 1] * fraction2 +  \
901                                                                 in_row2[in_x1 * 4 + 1] * fraction3 +  \
902                                                                 in_row2[in_x2 * 4 + 1] * fraction4); \
903                                         type input_b = (type)(in_row1[in_x1 * 4 + 2] * fraction1 +  \
904                                                                 in_row1[in_x2 * 4 + 2] * fraction2 +  \
905                                                                 in_row2[in_x1 * 4 + 2] * fraction3 +  \
906                                                                 in_row2[in_x2 * 4 + 2] * fraction4); \
907                                         type input_a = (type)(in_row1[in_x1 * 4 + 3] * fraction1 +  \
908                                                                 in_row1[in_x2 * 4 + 3] * fraction2 +  \
909                                                                 in_row2[in_x1 * 4 + 3] * fraction3 +  \
910                                                                 in_row2[in_x2 * 4 + 3] * fraction4); \
911 /* Plugin alpha is actually 0 - 0x100 */ \
912                                         input_a = input_a * plugin->alpha / 0x100; \
913                                         type transparency; \
914  \
915  \
916                                         if(components == 4) \
917                                         { \
918                                                 transparency = out_row[j * components + 3] * (max - input_a) / max; \
919                                                 out_row[j * components + 0] =  \
920                                                         (input_r * input_a + out_row[j * components + 0] * transparency) / max; \
921                                                 out_row[j * components + 1] =  \
922                                                         ((input_g-ofs) * input_a + (out_row[j * components + 1]-ofs) * transparency) / max + ofs; \
923                                                 out_row[j * components + 2] =  \
924                                                         ((input_b-ofs) * input_a + (out_row[j * components + 2]-ofs) * transparency) / max + ofs; \
925                                                 out_row[j * components + 3] =  \
926                                                         MAX(input_a, out_row[j * components + 3]); \
927                                         } \
928                                         else \
929                                         { \
930                                                 transparency = max - input_a; \
931                                                 out_row[j * components + 0] =  \
932                                                         (input_r * input_a + out_row[j * components + 0] * transparency) / max; \
933                                                 out_row[j * components + 1] =  \
934                                                         ((input_g-ofs) * input_a + (out_row[j * components + 1]-ofs) * transparency) / max + ofs; \
935                                                 out_row[j * components + 2] =  \
936                                                         ((input_b-ofs) * input_a + (out_row[j * components + 2]-ofs) * transparency) / max + ofs; \
937                                         } \
938                                 } \
939                         } \
940                 } \
941         } \
942 }
943
944 #define TRANSLATEA(type, max, components, r, g, b) \
945 { \
946         unsigned char **in_rows = plugin->text_mask->get_rows(); \
947         type **out_rows = (type**)plugin->output->get_rows(); \
948  \
949         for(int i = pkg->y1; i < pkg->y2; i++) \
950         { \
951                 if(i + server->out_y1_int >= 0 && \
952                         i + server->out_y1_int < server->output_h) \
953                 { \
954                         unsigned char *in_row = in_rows[i]; \
955                         type *out_row = out_rows[i + server->out_y1_int]; \
956  \
957                         for(int j = server->out_x1; j < server->out_x2_int; j++) \
958                         { \
959                                 if(j  >= 0 && \
960                                         j < server->output_w) \
961                                 { \
962                                         int input = (int)(in_row[j - server->out_x1]);  \
963  \
964                                         input *= plugin->alpha; \
965 /* Alpha is 0 - 256 */ \
966                                         input >>= 8; \
967  \
968                                         int anti_input = 0xff - input; \
969                                         if(components == 4) \
970                                         { \
971                                                 out_row[j * components + 0] =  \
972                                                         (r * input + out_row[j * components + 0] * anti_input) / 0xff; \
973                                                 out_row[j * components + 1] =  \
974                                                         (g * input + out_row[j * components + 1] * anti_input) / 0xff; \
975                                                 out_row[j * components + 2] =  \
976                                                         (b * input + out_row[j * components + 2] * anti_input) / 0xff; \
977                                                 if(max == 0xffff) \
978                                                         out_row[j * components + 3] =  \
979                                                                 MAX((input << 8) | input, out_row[j * components + 3]); \
980                                                 else \
981                                                         out_row[j * components + 3] =  \
982                                                                 MAX(input, out_row[j * components + 3]); \
983                                         } \
984                                         else \
985                                         { \
986                                                 out_row[j * components + 0] =  \
987                                                         (r * input + out_row[j * components + 0] * anti_input) / 0xff; \
988                                                 out_row[j * components + 1] =  \
989                                                         (g * input + out_row[j * components + 1] * anti_input) / 0xff; \
990                                                 out_row[j * components + 2] =  \
991                                                         (b * input + out_row[j * components + 2] * anti_input) / 0xff; \
992                                         } \
993                                 } \
994                         } \
995                 } \
996         } \
997 }
998
999 void TitleTranslateUnit::process_package(LoadPackage *package)
1000 {
1001         TitleTranslatePackage *pkg = (TitleTranslatePackage*)package;
1002         TitleTranslate *server = (TitleTranslate*)this->server;
1003
1004         switch(plugin->output->get_color_model()) {
1005         case BC_RGB888:     TRANSLATE(unsigned char, 0xff, 3, 0);    break;
1006         case BC_RGB_FLOAT:  TRANSLATE(float, 1.0, 3, 0);             break;
1007         case BC_YUV888:     TRANSLATE(unsigned char, 0xff, 3, 0x80); break;
1008         case BC_RGBA_FLOAT: TRANSLATE(float, 1.0, 4, 0);             break;
1009         case BC_RGBA8888:   TRANSLATE(unsigned char, 0xff, 4, 0);    break;
1010         case BC_YUVA8888:   TRANSLATE(unsigned char, 0xff, 4, 0x80); break;
1011         }
1012 //printf("TitleTranslateUnit::process_package 5\n");
1013 }
1014
1015
1016
1017
1018 TitleTranslate::TitleTranslate(TitleMain *plugin, int cpus)
1019  : LoadServer(cpus, cpus)
1020 {
1021         this->plugin = plugin;
1022         x_table = 0;
1023         y_table = 0;
1024         out_x1 = out_x2 = 0;
1025         out_y1 = out_y2 = 0;
1026         out_x1_int = out_x2_int = 0;
1027         out_y1_int = out_y2_int = 0;
1028         output_w = output_h = 0;
1029 }
1030
1031 TitleTranslate::~TitleTranslate()
1032 {
1033         delete [] x_table;
1034         delete [] y_table;
1035 }
1036
1037 void TitleTranslate::init_packages()
1038 {
1039 // Generate scaling tables
1040         delete [] x_table;  x_table = 0;
1041         delete [] y_table;  y_table = 0;
1042
1043         output_w = plugin->output->get_w();
1044         output_h = plugin->output->get_h();
1045
1046         float x1 = plugin->text_x1 + plugin->extent.x1;
1047         float x2 = plugin->text_x1 + plugin->extent.x2;
1048         TitleTranslateUnit::translation_array_f(x_table, x1, x2, 0,
1049                 plugin->mask_w, plugin->mask_w,
1050                 output_w, out_x1_int, out_x2_int);
1051
1052         float y1 = plugin->text_y1 + plugin->extent.y1;
1053         float y2 = plugin->text_y1 + plugin->extent.y2;
1054         TitleTranslateUnit::translation_array_f(y_table, y1, y2, 0,
1055                 plugin->mask_h, plugin->mask_h,
1056                 output_h, out_y1_int, out_y2_int);
1057
1058 //printf("TitleTranslate::init_packages 1\n");
1059         out_x1 = out_x1_int;  out_x2 = out_x2_int;
1060         out_y1 = out_y1_int;  out_y2 = out_y2_int;
1061
1062         int out_h = out_y2 - out_y1;
1063         int py1 = 0, py2 = 0;
1064         int pkgs = get_total_packages();
1065         for( int i=0; i<pkgs; py1=py2 ) {
1066                 TitleTranslatePackage *pkg = (TitleTranslatePackage*)get_package(i);
1067                 py2 = (++i*out_h) / pkgs;
1068                 pkg->y1 = py1;  pkg->y2 = py2;
1069         }
1070 //printf("TitleTranslate::init_packages 2\n");
1071 }
1072
1073 LoadClient* TitleTranslate::new_client()
1074 {
1075         return new TitleTranslateUnit(plugin, this);
1076 }
1077
1078 LoadPackage* TitleTranslate::new_package()
1079 {
1080         return new TitleTranslatePackage;
1081 }
1082
1083
1084
1085 TitleMain::TitleMain(PluginServer *server)
1086  : PluginVClient(server)
1087 {
1088         text_mask = 0;
1089         outline_mask = 0;
1090         text_mask_stroke = 0;
1091         glyph_engine = 0;
1092         title_engine = 0;
1093         freetype_face = 0;
1094         freetype_library = 0;
1095         char_pos = 0;
1096         row_geom = 0;
1097         row_geom_size = 0;
1098         translate = 0;
1099         outline_engine = 0;
1100         visible_row1 = 0;       visible_row2 = 0;
1101         visible_char1 = 0;      visible_char2 = 0;
1102         text_y1 = text_y2 = text_x1 = 0;
1103         alpha = 0x100;
1104         text_rows = 0;
1105         text_w = 0; text_h = 0;
1106         input = 0;  output = 0;
1107         cpus = PluginClient::smp + 1;
1108         if( cpus > 8 ) cpus = 8;
1109         need_reconfigure = 1;
1110 }
1111
1112 TitleMain::~TitleMain()
1113 {
1114         delete text_mask;
1115         delete outline_mask;
1116         delete text_mask_stroke;
1117         delete [] char_pos;
1118         delete [] row_geom;
1119         clear_glyphs();
1120         delete glyph_engine;
1121         delete title_engine;
1122         if( freetype_face )
1123                 FT_Done_Face(freetype_face);
1124         if( freetype_library )
1125                 FT_Done_FreeType(freetype_library);
1126         delete translate;
1127         delete outline_engine;
1128 }
1129
1130 const char* TitleMain::plugin_title() { return _("Title"); }
1131 int TitleMain::is_realtime() { return 1; }
1132 int TitleMain::is_synthesis() { return 1; }
1133
1134 NEW_WINDOW_MACRO(TitleMain, TitleWindow);
1135
1136
1137 void TitleMain::build_previews(TitleWindow *gui)
1138 {
1139         ArrayList<BC_FontEntry*>*fonts = gui->get_resources()->fontlist;
1140
1141         for(int font_number = 0; font_number < fonts->size(); font_number++)
1142         {
1143                 BC_FontEntry *font_entry = fonts->get(font_number);
1144 // already have examples
1145                 if(font_entry->image) return;
1146         }
1147
1148 // create example bitmaps
1149         FT_Library freetype_library = 0;        // Freetype library
1150         FT_Face freetype_face = 0;
1151         const char *test_string = "Aa";
1152         char new_path[BCTEXTLEN];
1153         int text_height = gui->get_text_height(LARGEFONT);
1154         int text_color = BC_WindowBase::get_resources()->default_text_color;
1155         int r = (text_color >> 16) & 0xff;
1156         int g = (text_color >> 8) & 0xff;
1157         int b = text_color & 0xff;
1158 // dimensions for each line
1159         int height[fonts->size()];
1160         int ascent[fonts->size()];
1161
1162 // pass 1 gets the extents for all the fonts
1163 // pass 2 draws the image
1164         int total_w = 0;
1165         int total_h = 0;
1166         for(int pass = 0; pass < 2; pass++)
1167         {
1168 //printf("TitleMain::build_previews %d %d %d\n",
1169 //__LINE__,
1170 //text_height,
1171 //total_h);
1172                 for(int font_number = 0; font_number < fonts->size(); font_number++)
1173                 {
1174                         BC_FontEntry *font_entry = fonts->get(font_number);
1175
1176 // test if font of same name has been processed
1177                         int skip = 0;
1178                         for(int i = 0; i < font_number; i++) {
1179                                 if(!strcasecmp(fonts->get(i)->displayname, font_entry->displayname)) {
1180                                         if(pass == 1) {
1181                                                 font_entry->image = fonts->get(i)->image;
1182                                         }
1183                                         skip = 1;
1184                                         break;
1185                                 }
1186                         }
1187
1188                         if(skip) continue;
1189
1190                         int current_x = 0;
1191                         int current_w = 0;
1192                         int current_ascent = 0;
1193                         int current_h = 0;
1194                         if(pass == 1) {
1195                                 font_entry->image = new VFrame;
1196                                 font_entry->image->set_use_shm(0);
1197                                 font_entry->image->reallocate(0, -1, 0, 0, 0,
1198                                         total_w, total_h, BC_RGBA8888, -1);
1199                                 font_entry->image->clear_frame();
1200                         }
1201
1202                         current_x = 0;
1203                         current_w = 0;
1204                         int len = strlen(test_string);
1205                         for(int j = 0; j < len; j++)
1206                         {
1207                                 FT_ULong c = test_string[j];
1208                                 check_char_code_path(freetype_library,
1209                                         font_entry->path,
1210                                         c,
1211                                         (char *)new_path);
1212                                 if( !load_freetype_face(freetype_library,
1213                                         freetype_face, new_path)) {
1214                                         FT_Set_Pixel_Sizes(freetype_face, text_height, 0);
1215
1216                                         if(!FT_Load_Char(freetype_face, c, FT_LOAD_RENDER)) {
1217                                                 if(pass == 0) {
1218                                                         current_w = current_x + freetype_face->glyph->bitmap.width;
1219                                                         if(freetype_face->glyph->bitmap_top > current_ascent)
1220                                                                 current_ascent = freetype_face->glyph->bitmap_top;
1221                                                         if(freetype_face->glyph->bitmap.rows > total_h)
1222                                                                 total_h = freetype_face->glyph->bitmap.rows;
1223                                                         if(freetype_face->glyph->bitmap.rows > current_h)
1224                                                                 current_h = freetype_face->glyph->bitmap.rows;
1225                                                 }
1226                                                 else {
1227 // copy 1 row at a time
1228 // center vertically
1229                                                         int out_y = (total_h - height[font_number]) / 2 +
1230                                                                 ascent[font_number] - freetype_face->glyph->bitmap_top;
1231                                                         for(int in_y = 0;
1232                                                                 in_y < freetype_face->glyph->bitmap.rows &&
1233                                                                         out_y < total_h;
1234                                                                 in_y++, out_y++) {
1235                                                                 unsigned char *out_row = font_entry->image->get_rows()[out_y] +
1236                                                                         current_x * 4;
1237                                                                 unsigned char *in_row = freetype_face->glyph->bitmap.buffer +
1238                                                                         freetype_face->glyph->bitmap.pitch * in_y;
1239
1240                                                                 for(int out_x = 0; out_x < freetype_face->glyph->bitmap.width &&
1241                                                                         out_x < total_w;
1242                                                                         out_x++) {
1243                                                                         *out_row = (*in_row * r +
1244                                                                                 (0xff - *in_row) * *out_row) / 0xff; ++out_row;
1245                                                                         *out_row = (*in_row * g +
1246                                                                                 (0xff - *in_row) * *out_row) / 0xff; ++out_row;
1247                                                                         *out_row = (*in_row * b +
1248                                                                                 (0xff - *in_row) * *out_row) / 0xff; ++out_row;
1249                                                                         *out_row = MAX(*in_row, *out_row);  ++out_row;
1250                                                                         in_row++;
1251                                                                 }
1252                                                         }
1253                                                 }
1254
1255
1256                                                 current_x += freetype_face->glyph->advance.x >> 6;
1257                                         }
1258                                 }
1259                         }
1260
1261                         height[font_number] = current_h;
1262                         ascent[font_number] = current_ascent;
1263                         if(pass == 0 && current_w > total_w) total_w = current_w;
1264
1265                 }
1266         }
1267
1268         if(freetype_library) FT_Done_FreeType(freetype_library);
1269 }
1270
1271
1272
1273 //This checks if char_code is on the selected font, else it changes font to the first compatible //Akirad
1274 int TitleMain::check_char_code_path(FT_Library &freetype_library,
1275         char *path_old,
1276         FT_ULong &char_code,
1277         char *path_new)
1278 {
1279         FT_Face temp_freetype_face;
1280         FcPattern *pat;
1281         FcFontSet *fs;
1282         FcObjectSet *os;
1283         FcChar8 *file, *format;
1284         FcConfig *config;
1285         int i;
1286
1287         FcInit();
1288         config = FcConfigGetCurrent();
1289         FcConfigSetRescanInterval(config, 0);
1290
1291         pat = FcPatternCreate();
1292         os = FcObjectSetBuild ( FC_FILE, FC_FONTFORMAT, (char *) 0);
1293         fs = FcFontList(config, pat, os);
1294         FcPattern *font;
1295         int notfindit = 1;
1296         char tmpstring[BCTEXTLEN];
1297         int limit_to_truetype = 0; //if you want to limit search to truetype put 1
1298         if(!freetype_library) FT_Init_FreeType(&freetype_library);
1299         if(!FT_New_Face(freetype_library,
1300                                         path_old,
1301                                         0,
1302                                         &temp_freetype_face))
1303         {
1304                 FT_Set_Pixel_Sizes(temp_freetype_face, 128, 0);
1305                 int gindex = FT_Get_Char_Index(temp_freetype_face, char_code);
1306                 if((!gindex == 0) && (!char_code != 10))
1307                 {
1308                         strcpy(path_new, path_old);
1309                         notfindit = 0;
1310                 }
1311         }
1312
1313         if(notfindit)
1314         {
1315                 for (i=0; fs && i < fs->nfont; i++)
1316                 {
1317                         font = fs->fonts[i];
1318                         FcPatternGetString(font, FC_FONTFORMAT, 0, &format);
1319                         if((!strcmp((char *)format, "TrueType")) || limit_to_truetype)
1320                         {
1321                                 if(FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch)
1322                                 {
1323
1324                                         sprintf(tmpstring, "%s", file);
1325                                         if(!FT_New_Face(freetype_library,
1326                                                                 tmpstring,
1327                                                                 0,
1328                                                                 &temp_freetype_face))
1329                                         {
1330                                                 FT_Set_Pixel_Sizes(temp_freetype_face, 128, 0);
1331                                                 int gindex = FT_Get_Char_Index(temp_freetype_face, char_code);
1332                                                 if((!gindex == 0) && (!char_code != 10))
1333                                                 {
1334                                                         sprintf(path_new, "%s", tmpstring);
1335                                                         notfindit = 0;
1336                                                         goto done;
1337
1338                                                 }
1339                                         }
1340                                 }
1341                         }
1342                 }
1343         }
1344
1345 done:
1346         if(fs) FcFontSetDestroy(fs);
1347         if(temp_freetype_face) FT_Done_Face(temp_freetype_face);
1348         temp_freetype_face = 0;
1349
1350         if(notfindit)
1351         {
1352                 strcpy(path_new, path_old);
1353                 return 1;
1354         }
1355
1356         return 0;
1357 }
1358
1359
1360
1361
1362 int TitleMain::load_freetype_face(FT_Library &freetype_library,
1363         FT_Face &freetype_face,
1364         const char *path)
1365 {
1366 //printf("TitleMain::load_freetype_face 1\n");
1367         if(!freetype_library)
1368                 FT_Init_FreeType(&freetype_library);
1369         if(freetype_face)
1370                 FT_Done_Face(freetype_face);
1371         freetype_face = 0;
1372 //printf("TitleMain::load_freetype_face 2\n");
1373
1374 // Use freetype's internal function for loading font
1375         if(FT_New_Face(freetype_library, path, 0, &freetype_face)) {
1376                 fprintf(stderr, _("TitleMain::load_freetype_face %s failed.\n"), path);
1377                 freetype_face = 0;
1378                 freetype_library = 0;
1379                 return 1;
1380         }
1381         return 0;
1382 }
1383
1384 BC_FontEntry* TitleMain::get_font()
1385 {
1386         int style = 0;
1387         int mask;
1388
1389         style |= (config.style & BC_FONT_ITALIC) ? FL_SLANT_ITALIC : FL_SLANT_ROMAN;
1390         style |= (config.style & BC_FONT_BOLD) ? FL_WEIGHT_BOLD : FL_WEIGHT_NORMAL;
1391
1392         mask = FL_WEIGHT_MASK | FL_SLANT_MASK;
1393
1394         BC_Resources *resources =  BC_WindowBase::get_resources();
1395         return resources->find_fontentry(config.font, style, mask);
1396 }
1397
1398 int TitleMain::get_char_height()
1399 {
1400 // this is baseline to the baseline distance
1401         int result = config.size;
1402         if((config.style & BC_FONT_OUTLINE)) result += (int)ceil(config.stroke_width * 2);
1403         return result;
1404 }
1405
1406 TitleGlyph *TitleMain::get_glyph(FT_ULong char_code)
1407 {
1408         for(int i = 0; i < glyphs.size(); i++) {
1409                 if(glyphs.get(i)->char_code == char_code)
1410                         return glyphs.get(i);
1411         }
1412         return 0;
1413 }
1414
1415 int TitleMain::get_char_width(FT_ULong char_code)
1416 {
1417         if(char_code == '\n') return 0;
1418         TitleGlyph *glyph = get_glyph(char_code);
1419         return !glyph ? 0 : glyph->width;
1420 }
1421
1422 int TitleMain::get_char_advance(FT_ULong current, FT_ULong next)
1423 {
1424         FT_Vector kerning;
1425
1426         if(current == '\n') return 0;
1427         TitleGlyph *current_glyph = get_glyph(current);
1428         int result = !current_glyph ? 0 : current_glyph->advance_x;
1429         TitleGlyph *next_glyph = !next ? 0 : get_glyph(next);
1430         if(next_glyph)
1431                 FT_Get_Kerning(freetype_face,
1432                                 current_glyph->freetype_index,
1433                                 next_glyph->freetype_index,
1434                                 ft_kerning_default,
1435                                 &kerning);
1436         else
1437                 kerning.x = 0;
1438         return result + (kerning.x >> 6);
1439 }
1440
1441 void TitleMain::load_glyphs()
1442 {
1443 // Build table of all glyphs needed
1444         int total_packages = 0;
1445
1446         for(int i = 0; i < config.wlen; i++)
1447         {
1448                 int exists = 0;
1449                 FT_ULong char_code = config.wtext[i];
1450
1451                 for(int j = 0; j < glyphs.total; j++) {
1452                         if(glyphs.values[j]->char_code == char_code) {
1453                                 exists = 1;
1454                                 break;
1455                         }
1456                 }
1457
1458                 if(!exists && char_code != 0) {
1459                         total_packages++;
1460                         TitleGlyph *glyph = new TitleGlyph;
1461                         glyphs.append(glyph);
1462                         glyph->char_code = char_code;
1463                 }
1464         }
1465
1466         if(!glyph_engine)
1467                 glyph_engine = new GlyphEngine(this, cpus);
1468
1469         glyph_engine->set_package_count(total_packages);
1470         glyph_engine->process_packages();
1471 }
1472
1473
1474 void TitleMain::get_total_extents()
1475 {
1476 // Determine extents of total text
1477         int wlen = config.wlen;
1478         char_pos = new char_pos_t[wlen+1];
1479
1480         int pitch = config.line_pitch;
1481         int font_h = get_char_height();
1482         int row = 0;
1483         int row_w = 0, row_h = 0;
1484         int max_char_w = 0, max_char_h = 0;
1485         text_h = text_w = 0;
1486
1487         // unjustified positions, initial bbox
1488         for(int i = 0; i < wlen; i++) {
1489                 char_pos[i].x = row_w;
1490                 char_pos[i].y = text_h;
1491                 char_pos[i].row = row;
1492                 wchar_t wchar = config.wtext[i];
1493                 if( wchar == '\n' ) {
1494                         text_h += pitch ? pitch : row_h > font_h ? row_h : font_h;
1495                         if(row_w > text_w) text_w = row_w;
1496                         row_w = row_h = 0;  ++row;
1497                         continue;
1498                 }
1499                 TitleGlyph *glyph = get_glyph(wchar);
1500                 int char_w = i+1>=wlen ? glyph->width :
1501                         get_char_advance(wchar, config.wtext[i+1]);
1502                 char_pos[i].w = char_w;
1503                 row_w += char_w;
1504                 if( char_w > max_char_w ) max_char_w = char_w;
1505                 int char_h = glyph->top - glyph->bottom;
1506                 if( char_h > max_char_h ) max_char_h = char_h;
1507                 if( char_h > row_h ) row_h = char_h;
1508 //printf("charcode '%c'  %d,%d  glyph bbox %d,%d %d,%d\n",
1509 //  (int)wchar, char_pos[i].x, char_pos[i].y,
1510 //  glyph->left, glyph->top, glyph->right, glyph->bottom);
1511         }
1512         if( wlen > 0 && config.wtext[wlen-1] != '\n' ) {
1513                 text_h += pitch ? pitch : row_h > font_h ? row_h : font_h;
1514                 if(row_w > text_w) text_w = row_w;
1515                 ++row;
1516         }
1517         char_pos[wlen].x = row_w;
1518         char_pos[wlen].y = text_h;
1519         char_pos[wlen].row = row;
1520         char_pos[wlen].w = 0;
1521         text_rows = row;
1522
1523         // justify positions
1524         int row_start = 0, pos = 0;
1525         switch(config.hjustification) {
1526         case JUSTIFY_MID:
1527                 while( row_start < wlen ) {
1528                         while( pos<wlen && config.wtext[pos]!='\n' ) ++pos;
1529                         int ofs = (text_w - char_pos[pos].x) / 2;
1530 //printf("justify_mid ofs=%d\n", ofs);
1531                         while( row_start < pos )
1532                                 char_pos[row_start++].x += ofs;
1533                         ++pos;
1534                 }
1535                 break;
1536         case JUSTIFY_RIGHT:
1537                 while( row_start < wlen ) {
1538                         while( pos<wlen && config.wtext[pos]!='\n' ) ++pos;
1539                         int ofs = text_w - char_pos[pos].x;
1540 //printf("justify_right ofs=%d\n", ofs);
1541                         while( row_start < pos )
1542                                 char_pos[row_start++].x += ofs;
1543                         ++pos;
1544                 }
1545                 break;
1546         case JUSTIFY_LEFT:
1547         default:
1548                 break;
1549         }
1550
1551         if( row_geom_size < text_rows+1 ) {
1552                 delete [] row_geom;   row_geom = 0;
1553         }
1554         if (!row_geom) {
1555                 row_geom_size = text_rows+1;
1556                 row_geom = new RowGeom[row_geom_size];
1557         }
1558
1559         // Determine row extents
1560         struct RowGeom *geom = &row_geom[0];
1561         TitleGlyph *glyph = 0;
1562         geom->x0 = geom->y0 = 0;
1563         geom->x1 = geom->y1 = 0;
1564         geom->x2 = geom->y2 = 0;
1565
1566         // x0,y0 row origin in without kern
1567         // x1,y1 - x2,y2 left baseline relative bbox with kerns
1568         for(int i = 0; i < wlen; i++) {
1569                 int x = char_pos[i].x;
1570                 int y = char_pos[i].y;
1571                 wchar_t wchar = config.wtext[i];
1572                 if( wchar == '\n' ) {
1573                         glyph = 0;  ++geom;
1574                         geom->x0 = geom->y0 = 0;
1575                         geom->x1 = geom->y1 = 0;
1576                         geom->x2 = geom->y2 = 0;
1577                         continue;
1578                 }
1579                 TitleGlyph *gp = get_glyph(wchar);
1580                 if( !gp ) continue;
1581                 if( !glyph ) {
1582                         geom->x0 = x;          geom->y0 = y;
1583                         int dx = x-geom->x0, dy = y-geom->y0 + config.size;
1584                         geom->x1 = dx + gp->left;   geom->y1 = dy - gp->top;
1585                         geom->x2 = dx + gp->right;  geom->y2 = dy - gp->bottom;
1586                 }
1587                 glyph = gp;
1588                 int dx = x-geom->x0, dy = y-geom->y0 + config.size;
1589                 int dx1 = dx + glyph->left;
1590                 int dx2 = dx + glyph->right;
1591                 if( dx1 < geom->x1 ) geom->x1 = dx1;
1592                 if( dx2 > geom->x2 ) geom->x2 = dx2;
1593                 int dy1 = dy - glyph->top;
1594                 int dy2 = dy - glyph->bottom;
1595                 if( dy1 < geom->y1 ) geom->y1 = dy1;
1596                 if( dy2 > geom->y2 ) geom->y2 = dy2;
1597 //printf("charcode '%c'  %d,%d row bbox %d,%d %d,%d %d,%d\n",
1598 //  (int)wchar, x,y, geom->x0,geom->y0, geom->x1,geom->y1, geom->x2,geom->y2);
1599         }
1600         if( wlen > 0 && config.wtext[wlen-1] != '\n' ) {
1601                 ++geom;
1602                 geom->x0 = 0;
1603                 geom->y0 = text_h;
1604                 geom->x1 = geom->y1 = 0;
1605                 geom->x2 = geom->y2 = 0;
1606         }
1607 }
1608
1609 int TitleMain::draw_mask()
1610 {
1611         int old_visible_row1 = visible_row1;
1612         int old_visible_row2 = visible_row2;
1613
1614 // Determine y of visible text
1615         if(config.motion_strategy == BOTTOM_TO_TOP) {
1616 // printf("TitleMain::draw_mask 1 %d %lld %lld %lld\n",
1617 //      config.motion_strategy,
1618 //      get_source_position(),
1619 //      get_source_start(),
1620 //      config.prev_keyframe_position);
1621                 float magnitude = config.pixels_per_second *
1622                         (get_source_position() - config.prev_keyframe_position) /
1623                         PluginVClient::project_frame_rate;
1624                 if(config.loop) {
1625                         int loop_size = text_h + input->get_h();
1626                         magnitude -= (int)(magnitude / loop_size) * loop_size;
1627                 }
1628                 text_y1 = config.y + input->get_h() - magnitude;
1629         }
1630         else
1631         if(config.motion_strategy == TOP_TO_BOTTOM) {
1632                 float magnitude = config.pixels_per_second *
1633                         (get_source_position() - config.prev_keyframe_position) /
1634                         PluginVClient::project_frame_rate;
1635                 if(config.loop)
1636                 {
1637                         int loop_size = text_h + input->get_h();
1638                         magnitude -= (int)(magnitude / loop_size) * loop_size;
1639                 }
1640                 text_y1 = config.y + magnitude;
1641                 text_y1 -= text_h;
1642         }
1643         else if(config.vjustification == JUSTIFY_TOP) {
1644                 text_y1 = config.y;
1645         }
1646         else if(config.vjustification == JUSTIFY_MID) {
1647                 text_y1 = config.y + input->get_h() / 2 - text_h / 2;
1648         }
1649         else if(config.vjustification == JUSTIFY_BOTTOM) {
1650                 text_y1 = config.y + input->get_h() - text_h;
1651         }
1652
1653         text_y2 = text_y1 + text_h + 0.5;
1654
1655 // Determine x of visible text
1656         if(config.motion_strategy == RIGHT_TO_LEFT) {
1657                 float magnitude = config.pixels_per_second *
1658                         (get_source_position() - config.prev_keyframe_position) /
1659                         PluginVClient::project_frame_rate;
1660                 if(config.loop) {
1661                         int loop_size = text_w + input->get_w();
1662                         magnitude -= (int)(magnitude / loop_size) * loop_size;
1663                 }
1664                 text_x1 = config.x + (float)input->get_w() - magnitude;
1665         }
1666         else if(config.motion_strategy == LEFT_TO_RIGHT) {
1667                 float magnitude = config.pixels_per_second *
1668                         (get_source_position() - config.prev_keyframe_position) /
1669                         PluginVClient::project_frame_rate;
1670                 if(config.loop) {
1671                         int loop_size = text_w + input->get_w();
1672                         magnitude -= (int)(magnitude / loop_size) * loop_size;
1673                 }
1674                 text_x1 = config.x + -(float)text_w + magnitude;
1675         }
1676         else if(config.hjustification == JUSTIFY_LEFT) {
1677                 text_x1 = config.x;
1678         }
1679         else if(config.hjustification == JUSTIFY_MID) {
1680                 text_x1 = config.x + input->get_w() / 2 - text_w / 2;
1681         }
1682         else if(config.hjustification == JUSTIFY_RIGHT) {
1683                 text_x1 = config.x + input->get_w() - text_w;
1684         }
1685         text_x2 = text_x1 + text_w;
1686
1687         // bottom of this row is visible
1688         visible_row1 = 0;
1689         RowGeom *geom = 0;
1690         while( visible_row1 < text_rows ) {
1691                 geom = &row_geom[visible_row1];
1692                 int y0 = text_y1 + geom->bottom();
1693                 if( y0 > 0 ) break;
1694                 ++visible_row1;
1695         }
1696
1697         // top of next row is not visible
1698         visible_row2 = visible_row1;
1699         while( visible_row2 < text_rows ) {
1700                 geom = &row_geom[visible_row2];
1701                 int y0 = text_y1 + geom->top();
1702                 if( y0 >= input->get_h() ) break;
1703                 ++visible_row2;
1704         }
1705
1706         if( visible_row1 == visible_row2 ) return 1;
1707
1708 //printf("visible rows %d to %d\n", visible_row1, visible_row2);
1709         // Only use visible rows, get bbox including kerns
1710         // text origin, no kerning
1711         geom = &row_geom[visible_row1];
1712         extent.x0 = geom->x0;       extent.y0 = geom->y0;
1713         extent.x1 = geom->left();   extent.y1 = geom->top();
1714         extent.x2 = geom->right();  extent.y2 = geom->bottom();
1715
1716         for( int i=visible_row1; i<visible_row2; ++i ) {
1717                 geom = &row_geom[i];  int v;
1718                 if( (v=geom->left())   < extent.x1 ) extent.x1 = v;
1719                 if( (v=geom->top())    < extent.y1 ) extent.y1 = v;
1720                 if( (v=geom->right())  > extent.x2 ) extent.x2 = v;
1721                 if( (v=geom->bottom()) > extent.y2 ) extent.y2 = v;
1722         }
1723
1724 //printf("exts x0,y0=%d,%d  x1,y1=%d,%d  x2,y2=%d,%d text %f,%f %dx%d\n",
1725 // extent.x0,extent.y0, extent.x1,extent.y1, extent.x2,extent.y2,
1726 // text_x1,text_y1, text_w,text_h);
1727         // Only draw visible chars
1728         visible_char1 = visible_char2 = -1;
1729         int wlen = config.wlen;
1730         for(int i = 0; i < wlen; i++) {
1731                 char_pos_t *pos = char_pos + i;
1732                 if( pos->row < visible_row1 ) continue;
1733                 if( pos->row >= visible_row2 ) continue;
1734                 if(visible_char1 < 0) visible_char1 = i;
1735                 visible_char2 = i;
1736         }
1737         visible_char2++;
1738
1739         extent.x1 -= config.outline_size*2;
1740         extent.y1 -= config.outline_size*2;
1741         extent.x2 += abs(config.dropshadow) + config.outline_size*2;
1742         extent.y2 += abs(config.dropshadow) + config.outline_size*2;
1743
1744         // Determine mask geometry
1745         mask_w = extent.x2 - extent.x1;
1746         if( mask_w <= 0 ) return 1;
1747         mask_h = extent.y2 - extent.y1;
1748         if( mask_h <= 0 ) return 1;
1749
1750 //printf("TitleMain::draw_mask %d-%d frame %dx%d\n",
1751 //  visible_row1, visible_row2, mask_w,mask_h)
1752         int need_redraw = 0;
1753         if(text_mask && (text_mask->get_w() != mask_w || text_mask->get_h() != mask_h)) {
1754                 delete text_mask;         text_mask = 0;
1755                 delete text_mask_stroke;  text_mask_stroke = 0;
1756         }
1757
1758         if(!text_mask) {
1759 // Always use 8 bit because the glyphs are 8 bit
1760 // Need to set YUV to get clear_frame to set the right chroma.
1761                 int output_model = get_output()->get_color_model();
1762                 int color_model = BC_CModels::is_yuv(output_model) ? BC_YUVA8888 : BC_RGBA8888;
1763                 text_mask = new VFrame;
1764                 text_mask->set_use_shm(0);
1765                 text_mask->reallocate(0, -1, 0, 0, 0, mask_w, mask_h, color_model, -1);
1766                 float drop = abs(config.dropshadow);
1767                 int drop_w = mask_w + drop;
1768                 int drop_h = mask_h + drop;
1769                 text_mask_stroke = new VFrame;
1770                 text_mask_stroke->set_use_shm(0);
1771                 text_mask_stroke->reallocate(0, -1, 0, 0, 0, drop_w, drop_h, color_model, -1);
1772                 need_redraw = 1;
1773         }
1774
1775 // Draw on text mask if it has changed
1776         if( need_redraw ||
1777             old_visible_row1 != visible_row1 ||
1778             old_visible_row2 != visible_row2 ) {
1779 //printf("redraw %d to %d   %d,%d  %d,%d - %d,%d\n", visible_char1,visible_char2,
1780 //  extent.x0, extent.y0, extent.x1,extent.y1, extent.x2,extent.y2);
1781
1782                 text_mask->clear_frame();
1783                 text_mask_stroke->clear_frame();
1784 #if 0
1785                 unsigned char *data = text_mask->get_data(); // draw bbox on text_mask
1786                 for( int x=0; x<mask_w; ++x ) data[4*x+3] = 0xff;
1787                 for( int x=0; x<mask_w; ++x ) data[4*((mask_h-1)*mask_w+x)+3] = 0xff;
1788                 for( int y=0; y<mask_h; ++y ) data[4*mask_w*y+3] = 0xff;
1789                 for( int y=0; y<mask_h; ++y ) data[4*(mask_w*y + mask_w-1)+3] = 0xff;
1790 #endif
1791                 if(!title_engine)
1792                         title_engine = new TitleEngine(this, cpus);
1793
1794 // Draw dropshadow first
1795                 if(config.dropshadow) {
1796                         title_engine->do_dropshadow = 1;
1797                         title_engine->set_package_count(visible_char2 - visible_char1);
1798                         title_engine->process_packages();
1799                 }
1800
1801 // Then draw foreground
1802                 title_engine->do_dropshadow = 0;
1803                 title_engine->set_package_count(visible_char2 - visible_char1);
1804                 title_engine->process_packages();
1805
1806 // Convert to text outlines
1807                 if(config.outline_size > 0) {
1808                         if(outline_mask &&
1809                             (text_mask->get_w() != outline_mask->get_w() ||
1810                              text_mask->get_h() != outline_mask->get_h())) {
1811                                 delete outline_mask;  outline_mask = 0;
1812                         }
1813
1814                         if(!outline_mask) {
1815                                 outline_mask = new VFrame;
1816                                 outline_mask->set_use_shm(0);
1817                                 outline_mask->reallocate(0, -1, 0, 0, 0,
1818                                         text_mask->get_w(), text_mask->get_h(),
1819                                         text_mask->get_color_model(), -1);
1820                         }
1821                         if(!outline_engine) outline_engine =
1822                                 new TitleOutlineEngine(this, cpus);
1823                         outline_engine->do_outline();
1824                 }
1825         }
1826
1827         return 0;
1828 }
1829
1830 void TitleMain::overlay_mask()
1831 {
1832
1833 //printf("TitleMain::overlay_mask 1\n");
1834         alpha = 0x100;
1835         if(!EQUIV(config.fade_in, 0))
1836         {
1837                 int fade_len = lroundf(config.fade_in * PluginVClient::project_frame_rate);
1838                 int fade_position = get_source_position() - config.prev_keyframe_position;
1839
1840                 if(fade_position >= 0 && fade_position < fade_len)
1841                 {
1842                         alpha = lroundf(256.0f * fade_position / fade_len);
1843                 }
1844         }
1845         if(!EQUIV(config.fade_out, 0))
1846         {
1847                 int fade_len = lroundf(config.fade_out * PluginVClient::project_frame_rate);
1848                 int fade_position = config.next_keyframe_position - get_source_position();
1849
1850
1851                 if(fade_position >= 0 && fade_position < fade_len)
1852                 {
1853                         alpha = lroundf(256.0f * fade_position / fade_len);
1854                 }
1855         }
1856
1857         if(!translate)
1858                 translate = new TitleTranslate(this, cpus);
1859
1860         if(config.dropshadow)
1861         {
1862                 text_x1 += config.dropshadow;
1863                 if(text_x1 < input->get_w() && text_x1 + text_w > 0)
1864                 {
1865 // Do 2 passes if dropshadow.
1866                         int temp_color = config.color;
1867                         config.color = 0x0;
1868                         translate->run_packages();
1869                         config.color = temp_color;
1870                 }
1871                 text_x1 -= config.dropshadow;
1872         }
1873 //printf("TitleMain::overlay_mask 1\n");
1874
1875         if(text_x1 < input->get_w() && text_x1 + text_w > 0) {
1876                 translate->run_packages();
1877                 if (config.stroke_width >= ZERO && (config.style & BC_FONT_OUTLINE)) {
1878                         int temp_color = config.color;
1879                         VFrame *tmp_text_mask = this->text_mask;
1880                         config.color = config.color_stroke;
1881                         this->text_mask = this->text_mask_stroke;
1882
1883                         translate->run_packages();
1884                         config.color = temp_color;
1885                         this->text_mask = tmp_text_mask;
1886                 }
1887         }
1888 //printf("TitleMain::overlay_mask 200\n");
1889 }
1890
1891 void TitleMain::get_color_components(int *r, int *g, int *b, int *a, int is_outline)
1892 {
1893         int color = is_outline ? config.outline_color : config.color;
1894         unsigned char r_in = color >> 16;
1895         unsigned char g_in = color >> 8;
1896         unsigned char b_in = color;
1897         *a = is_outline ? config.outline_alpha : config.alpha;
1898
1899         switch(output->get_color_model()) {
1900                 case BC_YUV888:
1901                         yuv.rgb_to_yuv_8(r_in, g_in, b_in, *r, *g, *b);
1902                         break;
1903                 case BC_YUVA8888:
1904                         yuv.rgb_to_yuv_8(r_in, g_in, b_in, *r, *g, *b);
1905                         break;
1906                 default:
1907                         *r = r_in;  *g = g_in;  *b = b_in;
1908                         break;
1909         }
1910 }
1911
1912 void TitleMain::clear_glyphs()
1913 {
1914 //printf("TitleMain::clear_glyphs 1\n");
1915         glyphs.remove_all_objects();
1916 }
1917
1918 const char* TitleMain::motion_to_text(int motion)
1919 {
1920         switch(motion)
1921         {
1922                 case NO_MOTION: return _("No motion"); break;
1923                 case BOTTOM_TO_TOP: return _("Bottom to top"); break;
1924                 case TOP_TO_BOTTOM: return _("Top to bottom"); break;
1925                 case RIGHT_TO_LEFT: return _("Right to left"); break;
1926                 case LEFT_TO_RIGHT: return _("Left to right"); break;
1927         }
1928         return "";
1929 }
1930
1931 int TitleMain::text_to_motion(const char *text)
1932 {
1933         for(int i = 0; i < TOTAL_PATHS; i++)
1934         {
1935                 if(!strcasecmp(motion_to_text(i), text)) return i;
1936         }
1937         return 0;
1938 }
1939
1940 void TitleMain::reset_render()
1941 {
1942         delete text_mask;          text_mask = 0;
1943         delete glyph_engine;       glyph_engine = 0;
1944         delete [] char_pos;        char_pos = 0;
1945         delete text_mask_stroke;   text_mask_stroke = 0;
1946         delete [] row_geom;        row_geom = 0;
1947         row_geom_size = 0;
1948         visible_row1 = 0;          visible_row2 = 0;
1949         visible_char1 = 0;         visible_char2 = 0;
1950         text_rows = 0;
1951         clear_glyphs();
1952         if(freetype_face) {
1953                 FT_Done_Face(freetype_face);
1954                 freetype_face = 0;
1955         }
1956 }
1957
1958 int TitleMain::init_freetype()
1959 {
1960         if(!freetype_library)
1961                 FT_Init_FreeType(&freetype_library);
1962         if(!freetype_face) {
1963                 BC_FontEntry *font = get_font();
1964                 if(load_freetype_face(freetype_library, freetype_face, font->path)) {
1965                         printf("TitleMain::process_realtime %s: FT_New_Face failed.\n",
1966                                 font->displayname);
1967                         return 1;
1968                 }
1969                 FT_Set_Pixel_Sizes(freetype_face, config.size, 0);
1970         }
1971         return 0;
1972 }
1973
1974 int TitleMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
1975 {
1976         int result = 0;
1977         input = input_ptr;
1978         output = output_ptr;
1979
1980         need_reconfigure |= load_configuration();
1981
1982 // Check boundaries
1983         if(config.size <= 0 || config.size >= 2048)
1984                 config.size = 72;
1985         if(config.stroke_width < 0 || config.stroke_width >= 512)
1986                 config.stroke_width = 0.0;
1987         if(!config.wlen)
1988                 return 0;
1989         if(!strlen(config.encoding))
1990                 strcpy(config.encoding, DEFAULT_ENCODING);
1991
1992 // Always synthesize text and redraw it for timecode
1993         if(config.timecode)
1994         {
1995                 int64_t rendered_frame = get_source_position();
1996                 if (get_direction() == PLAY_REVERSE)
1997                         rendered_frame -= 1;
1998
1999                 char text[BCTEXTLEN];
2000                 Units::totext(text,
2001                                 (double)rendered_frame / PluginVClient::project_frame_rate,
2002                                 config.timecode_format,
2003                                 PluginVClient::get_project_samplerate(),
2004                                 PluginVClient::get_project_framerate(),
2005                                 16);
2006                 config.to_wtext(config.encoding, text, strlen(text)+1);
2007                 need_reconfigure = 1;
2008         }
2009
2010 // printf("TitleMain::process_realtime %d need_reconfigure=%d\n",
2011 // __LINE__,
2012 // need_reconfigure);
2013
2014 // Handle reconfiguration
2015         if(need_reconfigure) {
2016                 reset_render();
2017                 result = init_freetype();
2018                 if(!result) {
2019 //PRINT_TRACE
2020                         load_glyphs();
2021                         get_total_extents();
2022                         need_reconfigure = 0;
2023                 }
2024         }
2025
2026         if(!result) {
2027 //PRINT_TRACE
2028 // Determine region of text visible on the output and draw mask
2029                 result = draw_mask();
2030         }
2031
2032
2033 // Overlay mask on output
2034         if(!result) {
2035 //PRINT_TRACE
2036                 overlay_mask();
2037         }
2038
2039         return 0;
2040 }
2041
2042 void TitleMain::update_gui()
2043 {
2044         if(thread)
2045         {
2046                 int reconfigure = load_configuration();
2047                 if(reconfigure)
2048                 {
2049                         TitleWindow *window = (TitleWindow*)thread->window;
2050                         window->lock_window("TitleMain::update_gui");
2051                         window->update();
2052                         window->unlock_window();
2053                         window->color_thread->update_gui(config.color, 0);
2054                         window->unlock_window();
2055                 }
2056         }
2057 }
2058
2059 int TitleMain::load_configuration()
2060 {
2061         KeyFrame *prev_keyframe, *next_keyframe;
2062         prev_keyframe = get_prev_keyframe(get_source_position());
2063         next_keyframe = get_next_keyframe(get_source_position());
2064
2065         TitleConfig old_config, prev_config, next_config;
2066         old_config.copy_from(config);
2067         read_data(prev_keyframe);
2068         prev_config.copy_from(config);
2069         read_data(next_keyframe);
2070         next_config.copy_from(config);
2071
2072         config.prev_keyframe_position = prev_keyframe->position;
2073         config.next_keyframe_position = next_keyframe->position;
2074
2075         // if no previous keyframe exists, it should be start of the plugin, not start of the track
2076         if(config.next_keyframe_position == config.prev_keyframe_position)
2077                 config.next_keyframe_position = get_source_start() + get_total_len();
2078         if (config.prev_keyframe_position == 0) 
2079                 config.prev_keyframe_position = get_source_start();
2080 // printf("TitleMain::load_configuration 10 %d %d\n", 
2081 // config.prev_keyframe_position,
2082 // config.next_keyframe_position);
2083
2084         config.interpolate(prev_config, 
2085                 next_config, 
2086                 (next_keyframe->position == prev_keyframe->position) ?
2087                         get_source_position() :
2088                         prev_keyframe->position,
2089                 (next_keyframe->position == prev_keyframe->position) ?
2090                         get_source_position() + 1 :
2091                         next_keyframe->position,
2092                 get_source_position());
2093
2094         if(!config.equivalent(old_config))
2095                 return 1;
2096         return 0;
2097 }
2098
2099
2100 void TitleMain::save_data(KeyFrame *keyframe)
2101 {
2102         FileXML output;
2103
2104         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
2105         output.tag.set_title("TITLE");
2106         output.tag.set_property("FONT", config.font);
2107         output.tag.set_property("ENCODING", config.encoding);
2108         output.tag.set_property("STYLE", (int64_t)config.style);
2109         output.tag.set_property("SIZE", config.size);
2110         output.tag.set_property("COLOR", config.color);
2111         output.tag.set_property("COLOR_STROKE", config.color_stroke);
2112         output.tag.set_property("STROKE_WIDTH", config.stroke_width);
2113         output.tag.set_property("OUTLINE_COLOR", config.outline_color);
2114         output.tag.set_property("ALPHA", config.alpha);
2115         output.tag.set_property("OUTLINE_ALPHA", config.outline_alpha);
2116         output.tag.set_property("MOTION_STRATEGY", config.motion_strategy);
2117         output.tag.set_property("LOOP", config.loop);
2118         output.tag.set_property("LINE_PITCH", config.line_pitch);
2119         output.tag.set_property("PIXELS_PER_SECOND", config.pixels_per_second);
2120         output.tag.set_property("HJUSTIFICATION", config.hjustification);
2121         output.tag.set_property("VJUSTIFICATION", config.vjustification);
2122         output.tag.set_property("FADE_IN", config.fade_in);
2123         output.tag.set_property("FADE_OUT", config.fade_out);
2124         output.tag.set_property("TITLE_X", config.x);
2125         output.tag.set_property("TITLE_Y", config.y);
2126         output.tag.set_property("DROPSHADOW", config.dropshadow);
2127         output.tag.set_property("OUTLINE_SIZE", config.outline_size);
2128         output.tag.set_property("TIMECODE", config.timecode);
2129         output.tag.set_property("TIMECODEFORMAT", config.timecode_format);
2130         output.tag.set_property("WINDOW_W", config.window_w);
2131         output.tag.set_property("WINDOW_H", config.window_h);
2132         output.append_tag();
2133         output.append_newline();
2134         char text[BCTEXTLEN];
2135         int text_len = BC_Resources::encode(
2136                 BC_Resources::wide_encoding, DEFAULT_ENCODING,
2137                 (char*)config.wtext, config.wlen*sizeof(wchar_t),
2138                 text, sizeof(text));
2139         output.append_text(text, text_len);
2140         output.tag.set_title("/TITLE");
2141         output.append_tag();
2142         output.append_newline();
2143         output.terminate_string();
2144 //printf("TitleMain::save_data 1\n%s\n", output.string);
2145 //printf("TitleMain::save_data 2\n%s\n", config.text);
2146 }
2147
2148 void TitleMain::read_data(KeyFrame *keyframe)
2149 {
2150         FileXML input;
2151
2152         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
2153
2154         int result = 0;
2155
2156         config.prev_keyframe_position = keyframe->position;
2157         while(!result)
2158         {
2159                 result = input.read_tag();
2160                 if( result ) break;
2161
2162                 if(input.tag.title_is("TITLE")) {
2163                         input.tag.get_property("FONT", config.font);
2164                         input.tag.get_property("ENCODING", config.encoding);
2165                         config.style = input.tag.get_property("STYLE", (int64_t)config.style);
2166                         config.size = input.tag.get_property("SIZE", config.size);
2167                         config.color = input.tag.get_property("COLOR", config.color);
2168                         config.color_stroke = input.tag.get_property("COLOR_STROKE", config.color_stroke);
2169                         config.stroke_width = input.tag.get_property("STROKE_WIDTH", config.stroke_width);
2170                         config.outline_color = input.tag.get_property("OUTLINE_COLOR", config.outline_color);
2171                         config.alpha = input.tag.get_property("ALPHA", config.alpha);
2172                         config.outline_alpha = input.tag.get_property("OUTLINE_ALPHA", config.outline_alpha);
2173                         config.motion_strategy = input.tag.get_property("MOTION_STRATEGY", config.motion_strategy);
2174                         config.loop = input.tag.get_property("LOOP", config.loop);
2175                         config.line_pitch = input.tag.get_property("LINE_PITCH", config.line_pitch);
2176                         config.pixels_per_second = input.tag.get_property("PIXELS_PER_SECOND", config.pixels_per_second);
2177                         config.hjustification = input.tag.get_property("HJUSTIFICATION", config.hjustification);
2178                         config.vjustification = input.tag.get_property("VJUSTIFICATION", config.vjustification);
2179                         config.fade_in = input.tag.get_property("FADE_IN", config.fade_in);
2180                         config.fade_out = input.tag.get_property("FADE_OUT", config.fade_out);
2181                         config.x = input.tag.get_property("TITLE_X", config.x);
2182                         config.y = input.tag.get_property("TITLE_Y", config.y);
2183                         config.dropshadow = input.tag.get_property("DROPSHADOW", config.dropshadow);
2184                         config.outline_size = input.tag.get_property("OUTLINE_SIZE", config.outline_size);
2185                         config.timecode = input.tag.get_property("TIMECODE", config.timecode);
2186                         input.tag.get_property("TIMECODEFORMAT", config.timecode_format);
2187                         config.window_w = input.tag.get_property("WINDOW_W", config.window_w);
2188                         config.window_h = input.tag.get_property("WINDOW_H", config.window_h);
2189                         const char *text = input.read_text();
2190                         config.to_wtext(config.encoding, text, strlen(text)+1);
2191                 }
2192                 else if(input.tag.title_is("/TITLE")) {
2193                         result = 1;
2194                 }
2195         }
2196 }