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