Accessor for ClosedCaptionsDialog.
[dcpomatic.git] / src / lib / text_content.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "text_content.h"
22 #include "util.h"
23 #include "exceptions.h"
24 #include "font.h"
25 #include "content.h"
26 #include <dcp/raw_convert.h>
27 #include <libcxml/cxml.h>
28 #include <libxml++/libxml++.h>
29 #include <boost/foreach.hpp>
30 #include <iostream>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::vector;
36 using std::cout;
37 using std::list;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40 using boost::optional;
41 using dcp::raw_convert;
42 using namespace dcpomatic;
43
44 int const TextContentProperty::X_OFFSET = 500;
45 int const TextContentProperty::Y_OFFSET = 501;
46 int const TextContentProperty::X_SCALE = 502;
47 int const TextContentProperty::Y_SCALE = 503;
48 int const TextContentProperty::USE = 504;
49 int const TextContentProperty::BURN = 505;
50 int const TextContentProperty::LANGUAGE = 506;
51 int const TextContentProperty::FONTS = 507;
52 int const TextContentProperty::COLOUR = 508;
53 int const TextContentProperty::EFFECT = 509;
54 int const TextContentProperty::EFFECT_COLOUR = 510;
55 int const TextContentProperty::LINE_SPACING = 511;
56 int const TextContentProperty::FADE_IN = 512;
57 int const TextContentProperty::FADE_OUT = 513;
58 int const TextContentProperty::OUTLINE_WIDTH = 514;
59 int const TextContentProperty::TYPE = 515;
60 int const TextContentProperty::DCP_TRACK = 516;
61
62 TextContent::TextContent (Content* parent, TextType type, TextType original_type)
63         : ContentPart (parent)
64         , _use (false)
65         , _burn (false)
66         , _x_offset (0)
67         , _y_offset (0)
68         , _x_scale (1)
69         , _y_scale (1)
70         , _line_spacing (1)
71         , _outline_width (4)
72         , _type (type)
73         , _original_type (original_type)
74 {
75
76 }
77
78 /** @return TextContents from node or <Text> nodes under node (according to version).
79  *  The list could be empty if no TextContents are found.
80  */
81 list<shared_ptr<TextContent> >
82 TextContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
83 {
84         if (version < 34) {
85                 /* With old metadata FFmpeg content has the subtitle-related tags even with no
86                    subtitle streams, so check for that.
87                 */
88                 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
89                         return list<shared_ptr<TextContent> >();
90                 }
91
92                 /* Otherwise we can drop through to the newer logic */
93         }
94
95         if (version < 37) {
96                 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
97                         return list<shared_ptr<TextContent> >();
98                 }
99                 list<shared_ptr<TextContent> > c;
100                 c.push_back (shared_ptr<TextContent> (new TextContent (parent, node, version)));
101                 return c;
102         }
103
104         if (!node->optional_node_child("Text")) {
105                 return list<shared_ptr<TextContent> >();
106         }
107
108         list<shared_ptr<TextContent> > c;
109         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("Text")) {
110                 c.push_back (shared_ptr<TextContent> (new TextContent (parent, i, version)));
111         }
112
113         return c;
114 }
115
116 TextContent::TextContent (Content* parent, cxml::ConstNodePtr node, int version)
117         : ContentPart (parent)
118         , _use (false)
119         , _burn (false)
120         , _x_offset (0)
121         , _y_offset (0)
122         , _x_scale (1)
123         , _y_scale (1)
124         , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
125         , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (4))
126         , _type (TEXT_OPEN_SUBTITLE)
127         , _original_type (TEXT_OPEN_SUBTITLE)
128 {
129         if (version >= 37) {
130                 _use = node->bool_child ("Use");
131                 _burn = node->bool_child ("Burn");
132         } else if (version >= 32) {
133                 _use = node->bool_child ("UseSubtitles");
134                 _burn = node->bool_child ("BurnSubtitles");
135         }
136
137         if (version >= 37) {
138                 _x_offset = node->number_child<double> ("XOffset");
139                 _y_offset = node->number_child<double> ("YOffset");
140         } else if (version >= 7) {
141                 _x_offset = node->number_child<double> ("SubtitleXOffset");
142                 _y_offset = node->number_child<double> ("SubtitleYOffset");
143         } else {
144                 _y_offset = node->number_child<double> ("SubtitleOffset");
145         }
146
147         if (node->optional_bool_child("Outline").get_value_or(false)) {
148                 _effect = dcp::BORDER;
149         } else if (node->optional_bool_child("Shadow").get_value_or(false)) {
150                 _effect = dcp::SHADOW;
151         } else {
152                 _effect = dcp::NONE;
153         }
154
155         optional<string> effect = node->optional_string_child("Effect");
156         if (effect) {
157                 if (*effect == "none") {
158                         _effect = dcp::NONE;
159                 } else if (*effect == "outline") {
160                         _effect = dcp::BORDER;
161                 } else if (*effect == "shadow") {
162                         _effect = dcp::SHADOW;
163                 }
164         }
165
166         if (version >= 37) {
167                 _x_scale = node->number_child<double> ("XScale");
168                 _y_scale = node->number_child<double> ("YScale");
169         } else if (version >= 10) {
170                 _x_scale = node->number_child<double> ("SubtitleXScale");
171                 _y_scale = node->number_child<double> ("SubtitleYScale");
172         } else {
173                 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
174         }
175
176         optional<int> r = node->optional_number_child<int>("Red");
177         optional<int> g = node->optional_number_child<int>("Green");
178         optional<int> b = node->optional_number_child<int>("Blue");
179         if (r && g && b) {
180                 _colour = dcp::Colour (*r, *g, *b);
181         }
182
183         if (version >= 36) {
184                 optional<int> er = node->optional_number_child<int>("EffectRed");
185                 optional<int> eg = node->optional_number_child<int>("EffectGreen");
186                 optional<int> eb = node->optional_number_child<int>("EffectBlue");
187                 if (er && eg && eb) {
188                         _effect_colour = dcp::Colour (*er, *eg, *eb);
189                 }
190         } else {
191                 _effect_colour = dcp::Colour (
192                         node->optional_number_child<int>("OutlineRed").get_value_or(255),
193                         node->optional_number_child<int>("OutlineGreen").get_value_or(255),
194                         node->optional_number_child<int>("OutlineBlue").get_value_or(255)
195                         );
196         }
197
198         optional<Frame> fi;
199         if (version >= 37) {
200                 fi = node->optional_number_child<Frame>("FadeIn");
201         } else {
202                 fi = node->optional_number_child<Frame>("SubtitleFadeIn");
203         }
204         if (fi) {
205                 _fade_in = ContentTime (*fi);
206         }
207
208         optional<Frame> fo;
209         if (version >= 37) {
210                 fo = node->optional_number_child<Frame>("FadeOut");
211         } else {
212                 fo = node->optional_number_child<Frame>("SubtitleFadeOut");
213         }
214         if (fo) {
215                 _fade_out = ContentTime (*fo);
216         }
217
218         if (version >= 37) {
219                 _language = node->optional_string_child ("Language").get_value_or ("");
220         } else {
221                 _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
222         }
223
224         list<cxml::NodePtr> fonts = node->node_children ("Font");
225         for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
226                 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
227         }
228
229         connect_to_fonts ();
230
231         if (version >= 37) {
232                 _type = string_to_text_type (node->optional_string_child("Type").get_value_or("open"));
233                 if (node->optional_string_child("OriginalType")) {
234                         _original_type = string_to_text_type (node->optional_string_child("OriginalType").get());
235                 }
236         }
237
238         cxml::ConstNodePtr dt = node->optional_node_child("DCPTrack");
239         if (dt) {
240                 _dcp_track = DCPTextTrack (dt);
241         }
242 }
243
244 TextContent::TextContent (Content* parent, vector<shared_ptr<Content> > c)
245         : ContentPart (parent)
246 {
247         /* This constructor is for join which is only supported for content types
248            that have a single text, so we can use only_text() here.
249         */
250         shared_ptr<TextContent> ref = c[0]->only_text();
251         DCPOMATIC_ASSERT (ref);
252         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
253
254         for (size_t i = 1; i < c.size(); ++i) {
255
256                 if (c[i]->only_text()->use() != ref->use()) {
257                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
258                 }
259
260                 if (c[i]->only_text()->burn() != ref->burn()) {
261                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
262                 }
263
264                 if (c[i]->only_text()->x_offset() != ref->x_offset()) {
265                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
266                 }
267
268                 if (c[i]->only_text()->y_offset() != ref->y_offset()) {
269                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
270                 }
271
272                 if (c[i]->only_text()->x_scale() != ref->x_scale()) {
273                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
274                 }
275
276                 if (c[i]->only_text()->y_scale() != ref->y_scale()) {
277                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
278                 }
279
280                 if (c[i]->only_text()->line_spacing() != ref->line_spacing()) {
281                         throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
282                 }
283
284                 if ((c[i]->only_text()->fade_in() != ref->fade_in()) || (c[i]->only_text()->fade_out() != ref->fade_out())) {
285                         throw JoinError (_("Content to be joined must have the same subtitle fades."));
286                 }
287
288                 if ((c[i]->only_text()->outline_width() != ref->outline_width())) {
289                         throw JoinError (_("Content to be joined must have the same outline width."));
290                 }
291
292                 list<shared_ptr<Font> > fonts = c[i]->only_text()->fonts ();
293                 if (fonts.size() != ref_fonts.size()) {
294                         throw JoinError (_("Content to be joined must use the same fonts."));
295                 }
296
297                 if (c[i]->only_text()->dcp_track() != ref->dcp_track()) {
298                         throw JoinError (_("Content to be joined must use the same DCP track."));
299                 }
300
301                 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
302                 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
303
304                 while (j != ref_fonts.end ()) {
305                         if (**j != **k) {
306                                 throw JoinError (_("Content to be joined must use the same fonts."));
307                         }
308                         ++j;
309                         ++k;
310                 }
311         }
312
313         _use = ref->use ();
314         _burn = ref->burn ();
315         _x_offset = ref->x_offset ();
316         _y_offset = ref->y_offset ();
317         _x_scale = ref->x_scale ();
318         _y_scale = ref->y_scale ();
319         _language = ref->language ();
320         _fonts = ref_fonts;
321         _line_spacing = ref->line_spacing ();
322         _fade_in = ref->fade_in ();
323         _fade_out = ref->fade_out ();
324         _outline_width = ref->outline_width ();
325         _type = ref->type ();
326         _original_type = ref->original_type ();
327         _dcp_track = ref->dcp_track ();
328
329         connect_to_fonts ();
330 }
331
332 /** _mutex must not be held on entry */
333 void
334 TextContent::as_xml (xmlpp::Node* root) const
335 {
336         boost::mutex::scoped_lock lm (_mutex);
337
338         xmlpp::Element* text = root->add_child ("Text");
339
340         text->add_child("Use")->add_child_text (_use ? "1" : "0");
341         text->add_child("Burn")->add_child_text (_burn ? "1" : "0");
342         text->add_child("XOffset")->add_child_text (raw_convert<string> (_x_offset));
343         text->add_child("YOffset")->add_child_text (raw_convert<string> (_y_offset));
344         text->add_child("XScale")->add_child_text (raw_convert<string> (_x_scale));
345         text->add_child("YScale")->add_child_text (raw_convert<string> (_y_scale));
346         text->add_child("Language")->add_child_text (_language);
347         if (_colour) {
348                 text->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
349                 text->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
350                 text->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
351         }
352         if (_effect) {
353                 switch (*_effect) {
354                 case dcp::NONE:
355                         text->add_child("Effect")->add_child_text("none");
356                         break;
357                 case dcp::BORDER:
358                         text->add_child("Effect")->add_child_text("outline");
359                         break;
360                 case dcp::SHADOW:
361                         text->add_child("Effect")->add_child_text("shadow");
362                         break;
363                 }
364         }
365         if (_effect_colour) {
366                 text->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
367                 text->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
368                 text->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
369         }
370         text->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
371         if (_fade_in) {
372                 text->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
373         }
374         if (_fade_out) {
375                 text->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
376         }
377         text->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
378
379         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
380                 (*i)->as_xml (text->add_child("Font"));
381         }
382
383         text->add_child("Type")->add_child_text (text_type_to_string(_type));
384         text->add_child("OriginalType")->add_child_text (text_type_to_string(_original_type));
385         if (_dcp_track) {
386                 _dcp_track->as_xml(text->add_child("DCPTrack"));
387         }
388 }
389
390 string
391 TextContent::identifier () const
392 {
393         string s = raw_convert<string> (x_scale())
394                 + "_" + raw_convert<string> (y_scale())
395                 + "_" + raw_convert<string> (x_offset())
396                 + "_" + raw_convert<string> (y_offset())
397                 + "_" + raw_convert<string> (line_spacing())
398                 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
399                 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
400                 + "_" + raw_convert<string> (outline_width())
401                 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
402                 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::NONE)))
403                 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string())
404                 + "_" + raw_convert<string> (_parent->video_frame_rate().get_value_or(0));
405
406         /* XXX: I suppose really _fonts shouldn't be in here, since not all
407            types of subtitle content involve fonts.
408         */
409         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
410                 s += "_" + f->file().get_value_or("Default").string();
411         }
412
413         /* The DCP track and language are for metadata only, and don't affect
414            how this content looks.
415         */
416
417         return s;
418 }
419
420 void
421 TextContent::add_font (shared_ptr<Font> font)
422 {
423         _fonts.push_back (font);
424         connect_to_fonts ();
425 }
426
427 void
428 TextContent::connect_to_fonts ()
429 {
430         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
431                 i.disconnect ();
432         }
433
434         _font_connections.clear ();
435
436         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
437                 _font_connections.push_back (i->Changed.connect (boost::bind (&TextContent::font_changed, this)));
438         }
439 }
440
441 void
442 TextContent::font_changed ()
443 {
444         /* XXX: too late */
445         ChangeSignaller<Content> cc (_parent, TextContentProperty::FONTS);
446 }
447
448 void
449 TextContent::set_colour (dcp::Colour colour)
450 {
451         maybe_set (_colour, colour, TextContentProperty::COLOUR);
452 }
453
454 void
455 TextContent::unset_colour ()
456 {
457         maybe_set (_colour, optional<dcp::Colour>(), TextContentProperty::COLOUR);
458 }
459
460 void
461 TextContent::set_effect (dcp::Effect e)
462 {
463         maybe_set (_effect, e, TextContentProperty::EFFECT);
464 }
465
466 void
467 TextContent::unset_effect ()
468 {
469         maybe_set (_effect, optional<dcp::Effect>(), TextContentProperty::EFFECT);
470 }
471
472 void
473 TextContent::set_effect_colour (dcp::Colour colour)
474 {
475         maybe_set (_effect_colour, colour, TextContentProperty::EFFECT_COLOUR);
476 }
477
478 void
479 TextContent::unset_effect_colour ()
480 {
481         maybe_set (_effect_colour, optional<dcp::Colour>(), TextContentProperty::EFFECT_COLOUR);
482 }
483
484 void
485 TextContent::set_use (bool u)
486 {
487         maybe_set (_use, u, TextContentProperty::USE);
488 }
489
490 void
491 TextContent::set_burn (bool b)
492 {
493         maybe_set (_burn, b, TextContentProperty::BURN);
494 }
495
496 void
497 TextContent::set_x_offset (double o)
498 {
499         maybe_set (_x_offset, o, TextContentProperty::X_OFFSET);
500 }
501
502 void
503 TextContent::set_y_offset (double o)
504 {
505         maybe_set (_y_offset, o, TextContentProperty::Y_OFFSET);
506 }
507
508 void
509 TextContent::set_x_scale (double s)
510 {
511         maybe_set (_x_scale, s, TextContentProperty::X_SCALE);
512 }
513
514 void
515 TextContent::set_y_scale (double s)
516 {
517         maybe_set (_y_scale, s, TextContentProperty::Y_SCALE);
518 }
519
520 void
521 TextContent::set_language (string language)
522 {
523         maybe_set (_language, language, TextContentProperty::LANGUAGE);
524 }
525
526 void
527 TextContent::set_line_spacing (double s)
528 {
529         maybe_set (_line_spacing, s, TextContentProperty::LINE_SPACING);
530 }
531
532 void
533 TextContent::set_fade_in (ContentTime t)
534 {
535         maybe_set (_fade_in, t, TextContentProperty::FADE_IN);
536 }
537
538 void
539 TextContent::unset_fade_in ()
540 {
541         maybe_set (_fade_in, optional<ContentTime>(), TextContentProperty::FADE_IN);
542 }
543
544 void
545 TextContent::set_fade_out (ContentTime t)
546 {
547         maybe_set (_fade_out, t, TextContentProperty::FADE_OUT);
548 }
549
550 void
551 TextContent::unset_fade_out ()
552 {
553         maybe_set (_fade_out, optional<ContentTime>(), TextContentProperty::FADE_OUT);
554 }
555
556 void
557 TextContent::set_type (TextType type)
558 {
559         maybe_set (_type, type, TextContentProperty::TYPE);
560 }
561
562 void
563 TextContent::set_outline_width (int w)
564 {
565         maybe_set (_outline_width, w, TextContentProperty::OUTLINE_WIDTH);
566 }
567
568 void
569 TextContent::set_dcp_track (DCPTextTrack t)
570 {
571         maybe_set (_dcp_track, t, TextContentProperty::DCP_TRACK);
572 }
573
574 void
575 TextContent::unset_dcp_track ()
576 {
577         maybe_set (_dcp_track, optional<DCPTextTrack>(), TextContentProperty::DCP_TRACK);
578 }
579
580 void
581 TextContent::take_settings_from (shared_ptr<const TextContent> c)
582 {
583         set_use (c->_use);
584         set_burn (c->_burn);
585         set_x_offset (c->_x_offset);
586         set_y_offset (c->_y_offset);
587         set_x_scale (c->_x_scale);
588         set_y_scale (c->_y_scale);
589         maybe_set (_fonts, c->_fonts, TextContentProperty::FONTS);
590         if (c->_colour) {
591                 set_colour (*c->_colour);
592         } else {
593                 unset_colour ();
594         }
595         if (c->_effect) {
596                 set_effect (*c->_effect);
597         }
598         if (c->_effect_colour) {
599                 set_effect_colour (*c->_effect_colour);
600         } else {
601                 unset_effect_colour ();
602         }
603         set_line_spacing (c->_line_spacing);
604         if (c->_fade_in) {
605                 set_fade_in (*c->_fade_in);
606         }
607         if (c->_fade_out) {
608                 set_fade_out (*c->_fade_out);
609         }
610         set_outline_width (c->_outline_width);
611         if (c->_dcp_track) {
612                 set_dcp_track (c->_dcp_track.get());
613         } else {
614                 unset_dcp_track ();
615         }
616 }