Revert "Remove join function; the code is long and I don't think anybody"
[dcpomatic.git] / src / lib / caption_content.cc
index 801ccc0d0f2bb11f05769591748e292f764d53f6..0d13568e20ee3347999c64497e93b2539f958d0b 100644 (file)
@@ -68,11 +68,15 @@ CaptionContent::CaptionContent (Content* parent)
        , _line_spacing (1)
        , _outline_width (2)
        , _type (CAPTION_OPEN)
+       , _original_type (CAPTION_OPEN)
 {
 
 }
 
-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,7 +84,7 @@ 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 */
@@ -88,16 +92,22 @@ CaptionContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
 
        if (version < 37) {
                if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
-                       return shared_ptr<CaptionContent> ();
+                       return list<shared_ptr<CaptionContent> >();
                }
-               shared_ptr<CaptionContent> (new CaptionContent (parent, node, version));
+               list<shared_ptr<CaptionContent> > c;
+               c.push_back (shared_ptr<CaptionContent> (new CaptionContent (parent, node, version)));
+               return c;
        }
 
        if (!node->node_child("Caption")) {
-               return shared_ptr<CaptionContent> ();
+               return list<shared_ptr<CaptionContent> >();
        }
 
-       return shared_ptr<CaptionContent> (new CaptionContent (parent, node->node_child("Caption"), 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)
@@ -215,8 +225,86 @@ CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int ve
        connect_to_fonts ();
 
        _type = string_to_caption_type (node->optional_string_child("Type").get_value_or("open"));
+       _original_type = string_to_caption_type (node->optional_string_child("Type").get_value_or("open"));
 }
 
+CaptionContent::CaptionContent (Content* parent, vector<shared_ptr<Content> > c)
+       : ContentPart (parent)
+{
+       shared_ptr<CaptionContent> ref = c[0]->caption;
+       DCPOMATIC_ASSERT (ref);
+       list<shared_ptr<Font> > ref_fonts = ref->fonts ();
+
+       for (size_t i = 1; i < c.size(); ++i) {
+
+               if (c[i]->caption->use() != ref->use()) {
+                       throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
+               }
+
+               if (c[i]->caption->burn() != ref->burn()) {
+                       throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
+               }
+
+               if (c[i]->caption->x_offset() != ref->x_offset()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle X offset."));
+               }
+
+               if (c[i]->caption->y_offset() != ref->y_offset()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
+               }
+
+               if (c[i]->caption->x_scale() != ref->x_scale()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle X scale."));
+               }
+
+               if (c[i]->caption->y_scale() != ref->y_scale()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
+               }
+
+               if (c[i]->caption->line_spacing() != ref->line_spacing()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
+               }
+
+               if ((c[i]->caption->fade_in() != ref->fade_in()) || (c[i]->caption->fade_out() != ref->fade_out())) {
+                       throw JoinError (_("Content to be joined must have the same subtitle fades."));
+               }
+
+               if ((c[i]->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]->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 ();
+
+       connect_to_fonts ();
+}
 
 /** _mutex must not be held on entry */
 void
@@ -270,6 +358,7 @@ CaptionContent::as_xml (xmlpp::Node* root) const
        }
 
        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