c047f990542ef95747f3f621f665a4cf9ce9eec2
[goodguy/cinelerra.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  N_("nudge")
27 #define KW_COLOR  N_("color")
28 #define KW_ALPHA  N_("alpha")
29 #define KW_FONT   N_("font")
30 #define KW_SIZE   N_("size")
31 #define KW_BOLD   N_("bold")
32 #define KW_ITALIC N_("italic")
33 #define KW_CAPS   N_("caps")
34 #define KW_UL     N_("ul")
35 #define KW_BLINK  N_("blink")
36 #define KW_FIXED  N_("fixed")
37 #define KW_ALIAS  N_("smooth")
38 #define KW_SUP    N_("sup")
39 #define KW_PNG    N_("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         wchr_t *wtext;
165         long wsize, 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         int demand(long sz);
182
183 // Time Code Format
184         int timecode_format;
185 // drag enable
186         int drag;
187 // loop background playback
188         int loop_playback;
189 };
190
191 class TitleGlyph
192 {
193 public:
194         TitleGlyph();
195         ~TitleGlyph();
196
197         FT_ULong char_code;
198         int freetype_index;
199         BC_FontEntry *font;
200         int width, height, style;
201         int size, pitch;
202         int advance_x;
203         int left, top, right, bottom;
204         VFrame *data, *data_stroke;
205 };
206 class TitleGlyphs : public ArrayList<TitleGlyph *> {
207 public:
208         void clear() { remove_all_objects(); }
209         int count() { return size(); }
210
211         TitleGlyphs() {}
212         ~TitleGlyphs() { clear(); }
213 };
214
215 class TitleImage {
216 public:
217         char *path;
218         VFrame *vframe;
219
220         TitleImage(const char *path, VFrame *vfrm);
221         ~TitleImage();
222 };
223 class TitleImages : public ArrayList<TitleImage *> {
224 public:
225         void clear() { remove_all_objects(); }
226         int count() { return size(); }
227
228         TitleImages() {}
229         ~TitleImages() { clear(); }
230 };
231
232
233 // Position of each image box in a row
234 class TitleChar {
235 public:
236         wchr_t wch;
237         int typ, flags;
238         void *vp;
239         int x, y;
240         int row, dx;
241         int color, alpha;
242         float fade;
243         float blink, size;
244
245         TitleChar *init(int typ, void *vp);
246 };
247 class TitleChars : public ArrayList<TitleChar *> {
248         int next;
249 public:
250         void reset() { next = 0; }
251         void clear() { remove_all_objects(); reset(); }
252         int count() { return next; }
253         TitleChar *add(int typ, void *vp) {
254                 TitleChar *ret = next < size() ? get(next++) : 0;
255                 if( !ret ) { append(ret = new TitleChar());  next = size(); }
256                 return ret->init(typ, vp);
257         }
258         TitleChars() { reset(); }
259         ~TitleChars() { clear(); }
260 };
261
262 class TitleRow {
263 public:
264         float x0, y0, x1, y1, x2, y2;
265         TitleRow *init();
266         void bound(float lt, float tp, float rt, float bt) {
267                 if( x1 > lt ) x1 = lt;
268                 if( y1 < tp ) y1 = tp;
269                 if( x2 < rt ) x2 = rt;
270                 if( y2 > bt ) y2 = bt;
271         }
272 };
273 class TitleRows : public ArrayList<TitleRow *> {
274         int next;
275 public:
276         void reset() { next = 0; }
277         void clear() { remove_all_objects(); reset(); }
278         int count() { return next; }
279         TitleRow *add() {
280                 TitleRow *ret = next < size() ? get(next++) : 0;
281                 if( !ret ) { append(ret = new TitleRow());  next = size(); }
282                 return ret->init();
283         }
284         TitleRows() { reset(); }
285         ~TitleRows() { clear(); }
286 };
287
288 // Draw a single character into the glyph cache
289 //
290 class GlyphPackage : public LoadPackage
291 {
292 public:
293         GlyphPackage();
294         TitleGlyph *glyph;
295 };
296
297
298 class GlyphUnit : public LoadClient
299 {
300 public:
301         GlyphUnit(TitleMain *plugin, GlyphEngine *server);
302         ~GlyphUnit();
303         void process_package(LoadPackage *package);
304
305         TitleMain *plugin;
306         FT_Library freetype_library;            // Freetype library
307         FT_Face freetype_face;
308 };
309
310 class GlyphEngine : public LoadServer
311 {
312 public:
313         GlyphEngine(TitleMain *plugin, int cpus);
314         void init_packages();
315         LoadClient* new_client();
316         LoadPackage* new_package();
317         TitleMain *plugin;
318 };
319
320
321 // Copy a single character to the text mask
322 class TitlePackage : public LoadPackage
323 {
324 public:
325         TitlePackage();
326         int x, y;
327         TitleChar *chr;
328 };
329
330 // overlay modes
331 #define DRAW_ALPHA 1
332 #define DRAW_COLOR 2
333 #define DRAW_IMAGE 3
334
335 class TitleUnit : public LoadClient
336 {
337 public:
338         TitleUnit(TitleMain *plugin, TitleEngine *server);
339         void process_package(LoadPackage *package);
340         void draw_frame(int mode, VFrame *dst, VFrame *src, int x, int y);
341
342         TitleMain *plugin;
343         TitleEngine *engine;
344         TitleChar *chr;
345 };
346
347 class TitleEngine : public LoadServer
348 {
349 public:
350         TitleEngine(TitleMain *plugin, int cpus);
351         void init_packages();
352         LoadClient* new_client();
353         LoadPackage* new_package();
354         TitleMain *plugin;
355         int do_dropshadow;
356 };
357
358
359 // Create outline
360 class TitleOutlinePackage : public LoadPackage
361 {
362 public:
363         TitleOutlinePackage();
364         int y1, y2;
365 };
366
367
368 class TitleOutlineUnit : public LoadClient
369 {
370 public:
371         TitleOutlineUnit(TitleMain *plugin, TitleOutlineEngine *server);
372         void process_package(LoadPackage *package);
373         TitleMain *plugin;
374         TitleOutlineEngine *engine;
375 };
376
377 class TitleOutlineEngine : public LoadServer
378 {
379 public:
380         TitleOutlineEngine(TitleMain *plugin, int cpus);
381         void init_packages();
382         void do_outline();
383         LoadClient* new_client();
384         LoadPackage* new_package();
385         TitleMain *plugin;
386         int pass;
387 };
388
389 template<class typ> class TitleStack : public ArrayList<typ>
390 {
391         typ &last() { return ArrayList<typ>::last(); }
392         int size() { return ArrayList<typ>::size(); }
393         typ &append(typ &v) { return ArrayList<typ>::append(v); }
394         void remove() { return ArrayList<typ>::remove(); }
395 public:
396         TitleParser *parser;
397         TitleStack(TitleParser *p, typ v) : parser(p) { append(v); }
398         operator typ&() { return last(); }
399         typ &push(typ &v) { return append(v); }
400         int pop() { return size()>1 ? (remove(),0) : 1; }
401         int set(const char *txt);
402         int unset(const char *txt);
403 };
404
405 template<class typ> int TitleStack<typ>::set(const char *txt)
406 {
407         typ v = !*txt ? 1 : strtol(txt,(char **)&txt,0);
408         if( *txt || v < 0 || v > 1 ) return 1;
409         push(v);
410         return 0;
411 }
412 template<class typ> int TitleStack<typ>::unset(const char *txt)
413 {
414         return pop();
415 }
416
417 class TitleCurNudge : public TitleStack<int> {
418 public:
419         TitleCurNudge(TitleParser *parser, TitleMain *plugin);
420         int set(const char *txt);
421 };
422
423 class TitleCurColor : public TitleStack<int> {
424 public:
425         TitleCurColor(TitleParser *parser, TitleMain *plugin);
426         int set(const char *txt);
427 };
428
429 class TitleCurAlpha : public TitleStack<int> {
430 public:
431         TitleCurAlpha(TitleParser *parser, TitleMain *plugin);
432         int set(const char *txt);
433 };
434
435 class TitleCurSize : public TitleStack<float> {
436 public:
437         TitleCurSize(TitleParser *parser, TitleMain *plugin);
438         int set(const char *txt);
439         int unset(const char *txt);
440 };
441
442 class TitleCurBold : public TitleStack<int> {
443 public:
444         TitleCurBold(TitleParser *parser, TitleMain *plugin);
445         int set(const char *txt);
446         int unset(const char *txt);
447 };
448
449 class TitleCurItalic : public TitleStack<int> {
450 public:
451         TitleCurItalic(TitleParser *parser, TitleMain *plugin);
452         int set(const char *txt);
453         int unset(const char *txt);
454 };
455
456 class TitleCurFont : public TitleStack<BC_FontEntry*>
457 {
458 public:
459         BC_FontEntry *get(const char *txt, int style);
460         BC_FontEntry *set(const char *txt, int style);
461         int style();
462         virtual int set(const char *txt=0);
463         virtual int unset(const char *txt);
464         TitleCurFont(TitleParser *parser, TitleMain *plugin);
465 };
466
467 class TitleCurCaps : public TitleStack<int> {
468 public:
469         TitleCurCaps(TitleParser *parser, TitleMain *plugin);
470         int set(const char *txt);
471 };
472
473 class TitleCurUnder : public TitleStack<int> {
474 public:
475         TitleCurUnder(TitleParser *parser, TitleMain *plugin);
476 };
477
478 class TitleCurBlink : public TitleStack<float> {
479 public:
480         TitleCurBlink(TitleParser *parser, TitleMain *plugin);
481         int set(const char *txt);
482 };
483
484 class TitleCurFixed : public TitleStack<int> {
485 public:
486         TitleCurFixed(TitleParser *parser, TitleMain *plugin);
487         int set(const char *txt);
488 };
489
490 class TitleCurAlias : public TitleStack<int> {
491 public:
492         TitleCurAlias(TitleParser *parser, TitleMain *plugin);
493         int set(const char *txt);
494 };
495
496 class TitleCurSuper : public TitleStack<int> {
497 public:
498         TitleCurSuper(TitleParser *parser, TitleMain *plugin);
499         int set(const char *txt);
500 };
501
502
503 class TitleParser
504 {
505         const wchr_t *bfr, *out, *lmt;
506 public:
507         TitleMain *plugin;
508
509         long tell() { return out - bfr; }
510         void seek(long pos) { out = bfr + pos; }
511         bool eof() { return out >= lmt; }
512         int wcur() { return eof() ? -1 : *out; }
513         int wnext() { return eof() ? -1 : *out++; }
514         int wget(wchr_t &wch);
515         int tget(wchr_t &wch);
516         wchr_t wid[BCSTRLEN], wtxt[BCTEXTLEN];
517         char id[BCSTRLEN], text[BCTEXTLEN];
518         int set_attributes(int ret);
519
520         TitleCurNudge  cur_nudge;
521         TitleCurColor  cur_color;
522         TitleCurAlpha  cur_alpha;
523         TitleCurSize   cur_size;
524         TitleCurBold   cur_bold;
525         TitleCurItalic cur_italic;
526         TitleCurFont   cur_font;
527         TitleCurCaps   cur_caps;
528         TitleCurUnder  cur_under;
529         TitleCurBlink  cur_blink;
530         TitleCurFixed  cur_fixed;
531         TitleCurAlias  cur_alias;
532         TitleCurSuper  cur_super;
533
534         TitleParser(TitleMain *main);
535 };
536
537
538 // Overlay text mask with fractional translation
539 // We don't use OverlayFrame to enable alpha blending on non alpha
540 // output.
541 class TitleTranslatePackage : public LoadPackage
542 {
543 public:
544         TitleTranslatePackage();
545         int y1, y2;
546 };
547
548 class TitleTranslateUnit : public LoadClient
549 {
550 public:
551         TitleTranslateUnit(TitleMain *plugin, TitleTranslate *server);
552
553         void process_package(LoadPackage *package);
554         TitleMain *plugin;
555 };
556
557 class TitleTranslate : public LoadServer
558 {
559 public:
560         TitleTranslate(TitleMain *plugin, int cpus);
561         ~TitleTranslate();
562
563         TitleMain *plugin;
564         VFrame *input;
565         float in_w, in_h, out_w, out_h;
566         float ix1, iy1, ix2, iy2;
567         float ox1, oy1, ox2, oy2;
568
569         void copy(VFrame *input);
570         LoadClient* new_client();
571         LoadPackage* new_package();
572         void init_packages();
573 };
574
575
576 class TitleMain : public PluginVClient
577 {
578 public:
579         TitleMain(PluginServer *server);
580         ~TitleMain();
581
582 // required for all realtime plugins
583         PLUGIN_CLASS_MEMBERS(TitleConfig)
584         int process_realtime(VFrame *input_ptr, VFrame *output_ptr);
585         int is_realtime();
586         int is_synthesis();
587         void update_gui();
588         void save_data(KeyFrame *keyframe);
589         void read_data(KeyFrame *keyframe);
590         void insert_text(const wchr_t *wtxt, int pos);
591
592         void build_previews(TitleWindow *gui);
593         void reset_render();
594         int init_freetype();
595         int set_font(BC_FontEntry*&font, const char *txt);
596         int set_font(BC_FontEntry*&font, const char *txt, int style);
597         int set_size(int &size, const char *txt);
598         int set_color(int &color, const char *txt);
599         int set_bold(int &bold, const char *txt);
600         int set_italic(int &italic, const char *txt);
601         int set_caps(int &caps, const char *txt);
602         int set_under(int &under, const char *txt);
603         void load_glyphs();
604         int draw_text(int need_redraw);
605         int draw_underline(VFrame *mask, int alpha);
606         void draw_overlay();
607         void draw_boundry();
608         int get_text();
609         int get_visible_text();
610         int check_char_code_path(FT_Library &freetype_library,
611                 char *path_old, FT_ULong &char_code, char *path_new);
612         int load_freetype_face(FT_Library &freetype_library,
613                 FT_Face &freetype_face, const char *path);
614         int load_font(BC_FontEntry *font);
615         Indexable *open_background(const char *filename);
616         int read_background(VFrame *frame, int64_t position, int color_model);
617         void draw_background();
618         static BC_FontEntry* get_font(const char *font_name, int style);
619         BC_FontEntry* config_font();
620         TitleGlyph *get_glyph(FT_ULong char_code, BC_FontEntry *font, int size, int style);
621         int get_width(TitleGlyph *cur, TitleGlyph *nxt);
622
623         VFrame *add_image(const char *path);
624         VFrame *get_image(const char *path);
625
626 // backward compatibility
627         void convert_encoding();
628         static const char* motion_to_text(int motion);
629         static int text_to_motion(const char *text);
630
631         VFrame *text_mask;
632         VFrame *stroke_mask;
633         GlyphEngine *glyph_engine;
634         TitleEngine *title_engine;
635         VFrame *outline_mask;
636         TitleOutlineEngine *outline_engine;
637         TitleTranslate *translate;
638
639         TitleChars title_chars;
640         TitleRows title_rows;
641         TitleGlyphs title_glyphs;
642         TitleImages title_images;
643
644         FT_Library freetype_library;
645         FT_Face freetype_face;
646         char text_font[BCTEXTLEN];
647
648         int window_w, window_h;
649         int fuzz, fuzz1, fuzz2;
650         float title_x, title_y;
651         int title_w, title_h;
652
653         float text_x, text_y, text_w, text_h;
654         float text_x1, text_y1, text_x2, text_y2;
655
656         int mask_x, mask_y; int mask_w, mask_h;
657         int mask_x1, mask_y1, mask_x2, mask_y2;
658
659         int text_rows;
660         int visible_row1, visible_char1;
661         int visible_row2, visible_char2;
662         float fade;
663
664         VFrame *input, *output;
665         int output_model, text_model, mask_model;
666
667         Indexable *background;
668         File *bg_file;
669         VFrame *bg_frame;
670         RenderEngine *render_engine;
671         CICache *video_cache;
672         OverlayFrame *overlay_frame;
673
674         int64_t last_position;
675         int need_reconfigure;
676         int cpus;
677 };
678
679
680 #endif