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