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