Update to test/data.
[dcpomatic.git] / src / lib / caption_content.cc
index e3f6551fdfeb7e60405d047dbe99a2f7b83a8f1b..4d0795190dc9f61261ffb3c282070bf8cff955a8 100644 (file)
@@ -57,7 +57,7 @@ int const CaptionContentProperty::FADE_OUT = 513;
 int const CaptionContentProperty::OUTLINE_WIDTH = 514;
 int const CaptionContentProperty::TYPE = 515;
 
-CaptionContent::CaptionContent (Content* parent)
+CaptionContent::CaptionContent (Content* parent, CaptionType original_type)
        : ContentPart (parent)
        , _use (false)
        , _burn (false)
@@ -67,12 +67,16 @@ CaptionContent::CaptionContent (Content* parent)
        , _y_scale (1)
        , _line_spacing (1)
        , _outline_width (2)
-       , _type (CAPTION_OPEN)
+       , _type (original_type)
+       , _original_type (original_type)
 {
 
 }
 
-shared_ptr<CaptionContent>
+/** @return CaptionContents from node or <Caption> nodes under node (according to version).
+ *  The list could be empty if no CaptionContents are found.
+ */
+list<shared_ptr<CaptionContent> >
 CaptionContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
 {
        if (version < 34) {
@@ -80,17 +84,30 @@ CaptionContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
                   subtitle streams, so check for that.
                */
                if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
-                       return shared_ptr<CaptionContent> ();
+                       return list<shared_ptr<CaptionContent> >();
                }
 
                /* Otherwise we can drop through to the newer logic */
        }
 
-       if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
-               return shared_ptr<CaptionContent> ();
+       if (version < 37) {
+               if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
+                       return list<shared_ptr<CaptionContent> >();
+               }
+               list<shared_ptr<CaptionContent> > c;
+               c.push_back (shared_ptr<CaptionContent> (new CaptionContent (parent, node, version)));
+               return c;
+       }
+
+       if (!node->optional_node_child("Caption")) {
+               return list<shared_ptr<CaptionContent> >();
        }
 
-       return shared_ptr<CaptionContent> (new CaptionContent (parent, node, version));
+       list<shared_ptr<CaptionContent> > c;
+       BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("Caption")) {
+               c.push_back (shared_ptr<CaptionContent> (new CaptionContent (parent, i, version)));
+       }
+       return c;
 }
 
 CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int version)
@@ -104,13 +121,20 @@ CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int ve
        , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
        , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (2))
        , _type (CAPTION_OPEN)
+       , _original_type (CAPTION_OPEN)
 {
-       if (version >= 32) {
+       if (version >= 37) {
+               _use = node->bool_child ("Use");
+               _burn = node->bool_child ("Burn");
+       } else if (version >= 32) {
                _use = node->bool_child ("UseSubtitles");
                _burn = node->bool_child ("BurnSubtitles");
        }
 
-       if (version >= 7) {
+       if (version >= 37) {
+               _x_offset = node->number_child<double> ("XOffset");
+               _y_offset = node->number_child<double> ("YOffset");
+       } else if (version >= 7) {
                _x_offset = node->number_child<double> ("SubtitleXOffset");
                _y_offset = node->number_child<double> ("SubtitleYOffset");
        } else {
@@ -136,7 +160,10 @@ CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int ve
                }
        }
 
-       if (version >= 10) {
+       if (version >= 37) {
+               _x_scale = node->number_child<double> ("XScale");
+               _y_scale = node->number_child<double> ("YScale");
+       } else if (version >= 10) {
                _x_scale = node->number_child<double> ("SubtitleXScale");
                _y_scale = node->number_child<double> ("SubtitleYScale");
        } else {
@@ -165,16 +192,31 @@ CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int ve
                        );
        }
 
-       optional<Frame> fi = node->optional_number_child<Frame>("SubtitleFadeIn");
+       optional<Frame> fi;
+       if (version >= 37) {
+               fi = node->optional_number_child<Frame>("FadeIn");
+       } else {
+               fi = node->optional_number_child<Frame>("SubtitleFadeIn");
+       }
        if (fi) {
                _fade_in = ContentTime (*fi);
        }
-       optional<Frame> fo = node->optional_number_child<Frame>("SubtitleFadeOut");
+
+       optional<Frame> fo;
+       if (version >= 37) {
+               fo = node->optional_number_child<Frame>("FadeOut");
+       } else {
+               fo = node->optional_number_child<Frame>("SubtitleFadeOut");
+       }
        if (fo) {
                _fade_out = ContentTime (*fo);
        }
 
-       _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
+       if (version >= 37) {
+               _language = node->optional_string_child ("Language").get_value_or ("");
+       } else {
+               _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
+       }
 
        list<cxml::NodePtr> fonts = node->node_children ("Font");
        for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
@@ -183,9 +225,92 @@ CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int ve
 
        connect_to_fonts ();
 
-       _type = string_to_caption_type (node->optional_string_child("CaptionType").get_value_or("open"));
+       _type = string_to_caption_type (node->optional_string_child("Type").get_value_or("open"));
+       _original_type = string_to_caption_type (node->optional_string_child("OriginalType").get_value_or("open"));
 }
 
+CaptionContent::CaptionContent (Content* parent, vector<shared_ptr<Content> > c)
+       : ContentPart (parent)
+{
+       /* This constructor is for join which is only supported for content types
+          that have a single caption, so we can use only_caption() here.
+       */
+       shared_ptr<CaptionContent> ref = c[0]->only_caption();
+       DCPOMATIC_ASSERT (ref);
+       list<shared_ptr<Font> > ref_fonts = ref->fonts ();
+
+       for (size_t i = 1; i < c.size(); ++i) {
+
+               if (c[i]->only_caption()->use() != ref->use()) {
+                       throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
+               }
+
+               if (c[i]->only_caption()->burn() != ref->burn()) {
+                       throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
+               }
+
+               if (c[i]->only_caption()->x_offset() != ref->x_offset()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle X offset."));
+               }
+
+               if (c[i]->only_caption()->y_offset() != ref->y_offset()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
+               }
+
+               if (c[i]->only_caption()->x_scale() != ref->x_scale()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle X scale."));
+               }
+
+               if (c[i]->only_caption()->y_scale() != ref->y_scale()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
+               }
+
+               if (c[i]->only_caption()->line_spacing() != ref->line_spacing()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
+               }
+
+               if ((c[i]->only_caption()->fade_in() != ref->fade_in()) || (c[i]->only_caption()->fade_out() != ref->fade_out())) {
+                       throw JoinError (_("Content to be joined must have the same subtitle fades."));
+               }
+
+               if ((c[i]->only_caption()->outline_width() != ref->outline_width())) {
+                       throw JoinError (_("Content to be joined must have the same outline width."));
+               }
+
+               list<shared_ptr<Font> > fonts = c[i]->only_caption()->fonts ();
+               if (fonts.size() != ref_fonts.size()) {
+                       throw JoinError (_("Content to be joined must use the same fonts."));
+               }
+
+               list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
+               list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
+
+               while (j != ref_fonts.end ()) {
+                       if (**j != **k) {
+                               throw JoinError (_("Content to be joined must use the same fonts."));
+                       }
+                       ++j;
+                       ++k;
+               }
+       }
+
+       _use = ref->use ();
+       _burn = ref->burn ();
+       _x_offset = ref->x_offset ();
+       _y_offset = ref->y_offset ();
+       _x_scale = ref->x_scale ();
+       _y_scale = ref->y_scale ();
+       _language = ref->language ();
+       _fonts = ref_fonts;
+       _line_spacing = ref->line_spacing ();
+       _fade_in = ref->fade_in ();
+       _fade_out = ref->fade_out ();
+       _outline_width = ref->outline_width ();
+       _type = ref->type ();
+       _original_type = ref->original_type ();
+
+       connect_to_fonts ();
+}
 
 /** _mutex must not be held on entry */
 void
@@ -193,50 +318,53 @@ CaptionContent::as_xml (xmlpp::Node* root) const
 {
        boost::mutex::scoped_lock lm (_mutex);
 
-       root->add_child("UseSubtitles")->add_child_text (_use ? "1" : "0");
-       root->add_child("BurnSubtitles")->add_child_text (_burn ? "1" : "0");
-       root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
-       root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
-       root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
-       root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
-       root->add_child("SubtitleLanguage")->add_child_text (_language);
+       xmlpp::Element* caption = root->add_child ("Caption");
+
+       caption->add_child("Use")->add_child_text (_use ? "1" : "0");
+       caption->add_child("Burn")->add_child_text (_burn ? "1" : "0");
+       caption->add_child("XOffset")->add_child_text (raw_convert<string> (_x_offset));
+       caption->add_child("YOffset")->add_child_text (raw_convert<string> (_y_offset));
+       caption->add_child("XScale")->add_child_text (raw_convert<string> (_x_scale));
+       caption->add_child("YScale")->add_child_text (raw_convert<string> (_y_scale));
+       caption->add_child("Language")->add_child_text (_language);
        if (_colour) {
-               root->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
-               root->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
-               root->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
+               caption->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
+               caption->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
+               caption->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
        }
        if (_effect) {
                switch (*_effect) {
                case dcp::NONE:
-                       root->add_child("Effect")->add_child_text("none");
+                       caption->add_child("Effect")->add_child_text("none");
                        break;
                case dcp::BORDER:
-                       root->add_child("Effect")->add_child_text("outline");
+                       caption->add_child("Effect")->add_child_text("outline");
                        break;
                case dcp::SHADOW:
-                       root->add_child("Effect")->add_child_text("shadow");
+                       caption->add_child("Effect")->add_child_text("shadow");
                        break;
                }
        }
        if (_effect_colour) {
-               root->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
-               root->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
-               root->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
+               caption->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
+               caption->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
+               caption->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
        }
-       root->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
+       caption->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
        if (_fade_in) {
-               root->add_child("SubtitleFadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
+               caption->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
        }
        if (_fade_out) {
-               root->add_child("SubtitleFadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
+               caption->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
        }
-       root->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
+       caption->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
 
        for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
-               (*i)->as_xml (root->add_child("Font"));
+               (*i)->as_xml (caption->add_child("Font"));
        }
 
-       root->add_child("CaptionType")->add_child_text (caption_type_to_string(_type));
+       caption->add_child("Type")->add_child_text (caption_type_to_string(_type));
+       caption->add_child("OriginalType")->add_child_text (caption_type_to_string(_original_type));
 }
 
 string