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