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