rework intl strategy, titler upgrades, fonts fixes, igor ru.po
[goodguy/history.git] / cinelerra-5.1 / plugins / titler / titler.h
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 #ifndef TITLE_H
23 #define TITLE_H
24
25 #define KW_NUDGE  _("nudge")
26 #define KW_COLOR  _("color")
27 #define KW_ALPHA  _("alpha")
28 #define KW_FONT   _("font")
29 #define KW_SIZE   _("size")
30 #define KW_BOLD   _("bold")
31 #define KW_ITALIC _("italic")
32 #define KW_CAPS   _("caps")
33 #define KW_UL     _("ul")
34 #define KW_BLINK  _("blink")
35 #define KW_FIXED  _("fixed")
36 #define KW_SUP    _("sup")
37 #define KW_PNG    _("png")
38
39 class TitleConfig;
40 class TitleGlyph;
41 class TitleGlyphs;
42 class TitleImage;
43 class TitleImages;
44 class TitleChar;
45 class TitleChars;
46 class TitleRow;
47 class TitleRows;
48 class GlyphPackage;
49 class GlyphUnit;
50 class GlyphEngine;
51 class TitlePackage;
52 class TitleUnit;
53 class TitleEngine;
54 class TitleOutlinePackage;
55 class TitleOutlineUnit;
56 class TitleOutlineEngine;
57 class TitleTranslatePackage;
58 class TitleTranslateUnit;
59 class TitleTranslate;
60 class TitleCurFont;
61 class TitleCurSize;
62 class TitleCurColor;
63 class TitleCurAlpha;
64 class TitleCurBold;
65 class TitleCurItalic;
66 class TitleCurCaps;
67 class TitleCurUnder;
68 class TitleCurBlink;
69 class TitleCurFixed;
70 class TitleCurSuper;
71 class TitleCurNudge;
72 class TitleParser;
73 class TitleMain;
74
75 #include "bchash.h"
76 #include "bcfontentry.h"
77 #include "file.inc"
78 #include "indexable.inc"
79 #include "loadbalance.h"
80 #include "mutex.h"
81 #include "overlayframe.inc"
82 #include "pluginvclient.h"
83 #include "renderengine.inc"
84 #include "titlerwindow.h"
85
86 #include <ft2build.h>
87 #include FT_FREETYPE_H
88 #include <sys/types.h>
89 #include <wchar.h>
90 #include <wctype.h>
91
92 // Motion strategy
93 #define TOTAL_PATHS 5
94 #define NO_MOTION     0x0
95 #define BOTTOM_TO_TOP 0x1
96 #define TOP_TO_BOTTOM 0x2
97 #define RIGHT_TO_LEFT 0x3
98 #define LEFT_TO_RIGHT 0x4
99
100 // Horizontal justification
101 #define JUSTIFY_LEFT   0x0
102 #define JUSTIFY_CENTER 0x1
103 #define JUSTIFY_RIGHT  0x2
104
105 // Vertical justification
106 #define JUSTIFY_TOP     0x0
107 #define JUSTIFY_MID     0x1
108 #define JUSTIFY_BOTTOM  0x2
109
110 // char types
111 #define CHAR_GLYPH  0
112 #define CHAR_IMAGE  1
113 // flags
114 #define FLAG_UNDER  0x0001
115 #define FLAG_FIXED  0x0002
116 #define FLAG_SUPER  0x0004
117 #define FLAG_SUBER  0x0008
118 #define FLAG_BLINK  0x0010
119
120 class TitleConfig
121 {
122 public:
123         TitleConfig();
124         ~TitleConfig();
125
126         void to_wtext(const char *from_enc, const char *text, int tlen);
127 // Only used to clear glyphs
128         int equivalent(TitleConfig &that);
129         void copy_from(TitleConfig &that);
130         void interpolate(TitleConfig &prev,
131                 TitleConfig &next,
132                 int64_t prev_frame,
133                 int64_t next_frame,
134                 int64_t current_frame);
135
136 // Font information
137         char font[BCTEXTLEN];
138 // Encoding to convert from
139         char encoding[BCTEXTLEN];
140         int style;
141         float size;
142         int color;
143         int alpha;
144         float outline_size;
145         int outline_color;
146         int outline_alpha;
147         int color_stroke;
148         float stroke_width;
149         int motion_strategy;     // Motion of title across frame
150         int line_pitch;
151         int loop;                // Loop motion path
152         int hjustification;
153         int vjustification;
154 // Number of seconds the fade in and fade out of the title take
155         double fade_in, fade_out;
156         float pixels_per_second; // Speed of motion
157 // Text to display
158         wchar_t wtext[BCTEXTLEN];
159         int wlen;
160 // Position in frame relative to top left
161         float title_x, title_y;
162         int title_w, title_h;
163 // Size of window
164         int window_w, window_h;
165 // Calculated during every frame for motion strategy
166         int64_t prev_keyframe_position;
167         int64_t next_keyframe_position;
168 // Stamp timecode
169         int timecode;
170         int dropshadow;
171         int background;
172         char background_path[BCTEXTLEN];
173
174         void convert_text();
175
176 // Time Code Format
177         int timecode_format;
178 // drag enable
179         int drag;
180 // loop background playback
181         int loop_playback;
182 };
183
184 class TitleGlyph
185 {
186 public:
187         TitleGlyph();
188         ~TitleGlyph();
189
190         FT_ULong char_code;
191         int freetype_index;
192         BC_FontEntry *font;
193         int width, height, style;
194         int size, pitch;
195         int advance_x;
196         int left, top, right, bottom;
197         VFrame *data, *data_stroke;
198 };
199 class TitleGlyphs : public ArrayList<TitleGlyph *> {
200 public:
201         void clear() { remove_all_objects(); }
202         int count() { return size(); }
203
204         TitleGlyphs() {}
205         ~TitleGlyphs() { clear(); }
206 };
207
208 class TitleImage {
209 public:
210         char *path;
211         VFrame *vframe;
212
213         TitleImage(const char *path, VFrame *vfrm);
214         ~TitleImage();
215 };
216 class TitleImages : public ArrayList<TitleImage *> {
217 public:
218         void clear() { remove_all_objects(); }
219         int count() { return size(); }
220
221         TitleImages() {}
222         ~TitleImages() { clear(); }
223 };
224
225
226 // Position of each image box in a row
227 class TitleChar {
228 public:
229         wchar_t wch;
230         int typ, flags;
231         void *vp;
232         int x, y;
233         int row, dx;
234         int color, alpha;
235         float fade;
236         float blink, size;
237
238         TitleChar *init(int typ, void *vp);
239 };
240 class TitleChars : public ArrayList<TitleChar *> {
241         int next;
242 public:
243         void reset() { next = 0; }
244         void clear() { remove_all_objects(); reset(); }
245         int count() { return next; }
246         TitleChar *add(int typ, void *vp) {
247                 TitleChar *ret = next < size() ? get(next++) : 0;
248                 if( !ret ) { append(ret = new TitleChar());  next = size(); }
249                 return ret->init(typ, vp);
250         }
251         TitleChars() { reset(); }
252         ~TitleChars() { clear(); }
253 };
254
255 class TitleRow {
256 public:
257         float x0, y0, x1, y1, x2, y2;
258         TitleRow *init();
259         void bound(float lt, float tp, float rt, float bt) {
260                 if( x1 > lt ) x1 = lt;
261                 if( y1 < tp ) y1 = tp;
262                 if( x2 < rt ) x2 = rt;
263                 if( y2 > bt ) y2 = bt;
264         }
265 };
266 class TitleRows : public ArrayList<TitleRow *> {
267         int next;
268 public:
269         void reset() { next = 0; }
270         void clear() { remove_all_objects(); reset(); }
271         int count() { return next; }
272         TitleRow *add() {
273                 TitleRow *ret = next < size() ? get(next++) : 0;
274                 if( !ret ) { append(ret = new TitleRow());  next = size(); }
275                 return ret->init();
276         }
277         TitleRows() { reset(); }
278         ~TitleRows() { clear(); }
279 };
280
281 // Draw a single character into the glyph cache
282 //
283 class GlyphPackage : public LoadPackage
284 {
285 public:
286         GlyphPackage();
287         TitleGlyph *glyph;
288 };
289
290
291 class GlyphUnit : public LoadClient
292 {
293 public:
294         GlyphUnit(TitleMain *plugin, GlyphEngine *server);
295         ~GlyphUnit();
296         void process_package(LoadPackage *package);
297
298         TitleMain *plugin;
299         FT_Library freetype_library;            // Freetype library
300         FT_Face freetype_face;
301 };
302
303 class GlyphEngine : public LoadServer
304 {
305 public:
306         GlyphEngine(TitleMain *plugin, int cpus);
307         void init_packages();
308         LoadClient* new_client();
309         LoadPackage* new_package();
310         TitleMain *plugin;
311 };
312
313
314 // Copy a single character to the text mask
315 class TitlePackage : public LoadPackage
316 {
317 public:
318         TitlePackage();
319         int x, y;
320         TitleChar *chr;
321 };
322
323 // overlay modes
324 #define DRAW_ALPHA 1
325 #define DRAW_COLOR 2
326 #define DRAW_IMAGE 3
327
328 class TitleUnit : public LoadClient
329 {
330 public:
331         TitleUnit(TitleMain *plugin, TitleEngine *server);
332         void process_package(LoadPackage *package);
333         void draw_frame(int mode, VFrame *dst, VFrame *src, int x, int y);
334
335         TitleMain *plugin;
336         TitleEngine *engine;
337         TitleChar *chr;
338 };
339
340 class TitleEngine : public LoadServer
341 {
342 public:
343         TitleEngine(TitleMain *plugin, int cpus);
344         void init_packages();
345         LoadClient* new_client();
346         LoadPackage* new_package();
347         TitleMain *plugin;
348         int do_dropshadow;
349 };
350
351
352 // Create outline
353 class TitleOutlinePackage : public LoadPackage
354 {
355 public:
356         TitleOutlinePackage();
357         int y1, y2;
358 };
359
360
361 class TitleOutlineUnit : public LoadClient
362 {
363 public:
364         TitleOutlineUnit(TitleMain *plugin, TitleOutlineEngine *server);
365         void process_package(LoadPackage *package);
366         TitleMain *plugin;
367         TitleOutlineEngine *engine;
368 };
369
370 class TitleOutlineEngine : public LoadServer
371 {
372 public:
373         TitleOutlineEngine(TitleMain *plugin, int cpus);
374         void init_packages();
375         void do_outline();
376         LoadClient* new_client();
377         LoadPackage* new_package();
378         TitleMain *plugin;
379         int pass;
380 };
381
382
383 // Overlay text mask with fractional translation
384 // We don't use OverlayFrame to enable alpha blending on non alpha
385 // output.
386 class TitleTranslatePackage : public LoadPackage
387 {
388 public:
389         TitleTranslatePackage();
390         int y1, y2;
391 };
392
393 typedef struct {
394         int in_x1;
395         float in_fraction1;
396         int in_x2;       // Might be same as in_x1 for boundary
397         float in_fraction2;
398         float output_fraction;
399 } transfer_table_f;
400
401 class TitleTranslateUnit : public LoadClient
402 {
403 public:
404         TitleTranslateUnit(TitleMain *plugin, TitleTranslate *server);
405
406         static void translation_array_f(transfer_table_f* &table,
407                 float out_x1, float out_x2, int out_total,
408                 float in_x1, float in_x2, int in_total,
409                 int &x1_out, int &x2_out);
410         void process_package(LoadPackage *package);
411         TitleMain *plugin;
412 };
413
414 class TitleTranslate : public LoadServer
415 {
416 public:
417         TitleTranslate(TitleMain *plugin, int cpus);
418         ~TitleTranslate();
419         void init_packages();
420         void run_packages();
421         LoadClient* new_client();
422         LoadPackage* new_package();
423         TitleMain *plugin;
424         transfer_table_f *y_table;
425         transfer_table_f *x_table;
426         VFrame *xlat_mask;
427         int out_x1, out_x2;
428         int out_y1, out_y2;
429 };
430
431 template<class typ> class TitleStack : public ArrayList<typ>
432 {
433         typ &last() { return ArrayList<typ>::last(); }
434         int size() { return ArrayList<typ>::size(); }
435         typ &append(typ &v) { return ArrayList<typ>::append(v); }
436         void remove() { return ArrayList<typ>::remove(); }
437 public:
438         TitleParser *parser;
439         TitleStack(TitleParser *p, typ v) : parser(p) { append(v); }
440         operator typ&() { return last(); }
441         typ &push(typ &v) { return append(v); }
442         int pop() { return size()>1 ? (remove(),0) : 1; }
443         int set(const char *txt);
444         int unset(const char *txt);
445 };
446
447 template<class typ> int TitleStack<typ>::set(const char *txt)
448 {
449         typ v = !*txt ? 1 : strtol(txt,(char **)&txt,0);
450         if( *txt || v < 0 || v > 1 ) return 1;
451         push(v);
452         return 0;
453 }
454 template<class typ> int TitleStack<typ>::unset(const char *txt)
455 {
456         return pop();
457 }
458
459 class TitleCurNudge : public TitleStack<int> {
460 public:
461         TitleCurNudge(TitleParser *parser, TitleMain *plugin);
462         int set(const char *txt);
463 };
464
465 class TitleCurColor : public TitleStack<int> {
466 public:
467         TitleCurColor(TitleParser *parser, TitleMain *plugin);
468         int set(const char *txt);
469 };
470
471 class TitleCurAlpha : public TitleStack<int> {
472 public:
473         TitleCurAlpha(TitleParser *parser, TitleMain *plugin);
474         int set(const char *txt);
475 };
476
477 class TitleCurSize : public TitleStack<float> {
478 public:
479         TitleCurSize(TitleParser *parser, TitleMain *plugin);
480         int set(const char *txt);
481         int unset(const char *txt);
482 };
483
484 class TitleCurBold : public TitleStack<int> {
485 public:
486         TitleCurBold(TitleParser *parser, TitleMain *plugin);
487         int set(const char *txt);
488         int unset(const char *txt);
489 };
490
491 class TitleCurItalic : public TitleStack<int> {
492 public:
493         TitleCurItalic(TitleParser *parser, TitleMain *plugin);
494         int set(const char *txt);
495         int unset(const char *txt);
496 };
497
498 class TitleCurFont : public TitleStack<BC_FontEntry*>
499 {
500 public:
501         BC_FontEntry *get(const char *txt, int style);
502         BC_FontEntry *set(const char *txt, int style);
503         int style();
504         virtual int set(const char *txt=0);
505         virtual int unset(const char *txt);
506         TitleCurFont(TitleParser *parser, TitleMain *plugin);
507 };
508
509 class TitleCurCaps : public TitleStack<int> {
510 public:
511         TitleCurCaps(TitleParser *parser, TitleMain *plugin);
512         int set(const char *txt);
513 };
514
515 class TitleCurUnder : public TitleStack<int> {
516 public:
517         TitleCurUnder(TitleParser *parser, TitleMain *plugin);
518 };
519
520 class TitleCurBlink : public TitleStack<float> {
521 public:
522         TitleCurBlink(TitleParser *parser, TitleMain *plugin);
523         int set(const char *txt);
524 };
525
526 class TitleCurFixed : public TitleStack<int> {
527 public:
528         TitleCurFixed(TitleParser *parser, TitleMain *plugin);
529         int set(const char *txt);
530 };
531
532 class TitleCurSuper : public TitleStack<int> {
533 public:
534         TitleCurSuper(TitleParser *parser, TitleMain *plugin);
535         int set(const char *txt);
536 };
537
538
539 class TitleParser
540 {
541         const wchar_t *bfr, *out, *lmt;
542 public:
543         TitleMain *plugin;
544
545         long tell() { return out - bfr; }
546         void seek(long pos) { out = bfr + pos; }
547         bool eof() { return out >= lmt; }
548         int wcur() { return eof() ? -1 : *out; }
549         int wnext() { return eof() ? -1 : *out++; }
550         int wget(wchar_t &wch);
551         int tget(wchar_t &wch);
552         wchar_t wid[BCSTRLEN], wtxt[BCTEXTLEN];
553         char id[BCSTRLEN], text[BCTEXTLEN];
554         int set_attributes(int ret);
555
556         TitleCurNudge  cur_nudge;
557         TitleCurColor  cur_color;
558         TitleCurAlpha  cur_alpha;
559         TitleCurSize   cur_size;
560         TitleCurBold   cur_bold;
561         TitleCurItalic cur_italic;
562         TitleCurFont   cur_font;
563         TitleCurCaps   cur_caps;
564         TitleCurUnder  cur_under;
565         TitleCurBlink  cur_blink;
566         TitleCurFixed  cur_fixed;
567         TitleCurSuper  cur_super;
568
569         TitleParser(TitleMain *main);
570 };
571
572
573 class TitleMain : public PluginVClient
574 {
575 public:
576         TitleMain(PluginServer *server);
577         ~TitleMain();
578
579 // required for all realtime plugins
580         PLUGIN_CLASS_MEMBERS(TitleConfig)
581         int process_realtime(VFrame *input_ptr, VFrame *output_ptr);
582         int is_realtime();
583         int is_synthesis();
584         void update_gui();
585         void save_data(KeyFrame *keyframe);
586         void read_data(KeyFrame *keyframe);
587         void insert_text(const wchar_t *wtxt, int pos);
588
589         void build_previews(TitleWindow *gui);
590         void reset_render();
591         int init_freetype();
592         int set_font(BC_FontEntry*&font, const char *txt);
593         int set_font(BC_FontEntry*&font, const char *txt, int style);
594         int set_size(int &size, const char *txt);
595         int set_color(int &color, const char *txt);
596         int set_bold(int &bold, const char *txt);
597         int set_italic(int &italic, const char *txt);
598         int set_caps(int &caps, const char *txt);
599         int set_under(int &under, const char *txt);
600         void load_glyphs();
601         int draw_text(int need_redraw);
602         int draw_underline(VFrame *mask, int alpha);
603         void draw_overlay();
604         void draw_boundry();
605         int get_text();
606         int get_visible_text();
607         int check_char_code_path(FT_Library &freetype_library,
608                 char *path_old, FT_ULong &char_code, char *path_new);
609         int load_freetype_face(FT_Library &freetype_library,
610                 FT_Face &freetype_face, const char *path);
611         int load_font(BC_FontEntry *font);
612         Indexable *open_background(const char *filename);
613         int read_background(VFrame *frame, int64_t position, int color_model);
614         void draw_background();
615         static BC_FontEntry* get_font(const char *font_name, int style);
616         BC_FontEntry* config_font();
617         TitleGlyph *get_glyph(FT_ULong char_code, BC_FontEntry *font, int size, int style);
618         int get_width(TitleGlyph *cur, TitleGlyph *nxt);
619
620         VFrame *add_image(const char *path);
621         VFrame *get_image(const char *path);
622
623 // backward compatibility
624         void convert_encoding();
625         static const char* motion_to_text(int motion);
626         static int text_to_motion(const char *text);
627
628         VFrame *text_mask;
629         VFrame *stroke_mask;
630         GlyphEngine *glyph_engine;
631         TitleEngine *title_engine;
632         VFrame *outline_mask;
633         TitleOutlineEngine *outline_engine;
634         TitleTranslate *translate;
635
636         TitleChars title_chars;
637         TitleRows title_rows;
638         TitleGlyphs title_glyphs;
639         TitleImages title_images;
640
641         FT_Library freetype_library;
642         FT_Face freetype_face;
643         char text_font[BCTEXTLEN];
644
645         int window_w, window_h;
646         int fuzz, fuzz1, fuzz2;
647         int title_x, title_y, title_w, title_h;
648
649         float text_x, text_y, text_w, text_h;
650         float text_x1, text_y1, text_x2, text_y2;
651
652         int mask_x, mask_y; int mask_w, mask_h;
653         int mask_x1, mask_y1, mask_x2, mask_y2;
654
655         int text_rows;
656         int visible_row1, visible_char1;
657         int visible_row2, visible_char2;
658         float fade;
659
660         VFrame *input, *output;
661         int output_model, text_model, mask_model;
662
663         Indexable *background;
664         File *bg_file;
665         VFrame *bg_frame;
666         RenderEngine *render_engine;
667         CICache *video_cache;
668         OverlayFrame *overlay_frame;
669
670         int64_t last_position;
671         int need_reconfigure;
672         int cpus;
673 };
674
675
676 #endif