Cleanup: pass EqualityOptions as const&
[libdcp.git] / src / subtitle_asset.cc
index 22781196ac5c485ca042ba8813895e06a85db7f1..b7e75419d1d3dd4bc7c4bbb475623f1541acff94 100644 (file)
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/shared_array.hpp>
+#include <algorithm>
 
 
-using std::dynamic_pointer_cast;
-using std::string;
-using std::cout;
 using std::cerr;
+using std::cout;
+using std::dynamic_pointer_cast;
+using std::make_shared;
 using std::map;
+using std::pair;
 using std::shared_ptr;
+using std::string;
 using std::vector;
-using std::make_shared;
-using boost::optional;
 using boost::lexical_cast;
+using boost::optional;
 using namespace dcp;
 
 
@@ -201,6 +203,10 @@ SubtitleAsset::position_align (SubtitleAsset::ParseState& ps, xmlpp::Element con
                ps.v_align = string_to_valign (va.get ());
        }
 
+       auto zp = optional_number_attribute<float>(node, "Zposition");
+       if (zp) {
+               ps.z_position = zp.get() / 100;
+       }
 }
 
 
@@ -318,7 +324,11 @@ SubtitleAsset::parse_subtitles (xmlpp::Element const * node, vector<ParseState>&
 void
 SubtitleAsset::maybe_add_subtitle (string text, vector<ParseState> const & parse_state, float space_before, Standard standard)
 {
-       if (empty_or_white_space (text)) {
+       auto wanted = [](ParseState const& ps) {
+               return ps.type && (ps.type.get() == ParseState::Type::TEXT || ps.type.get() == ParseState::Type::IMAGE);
+       };
+
+       if (find_if(parse_state.begin(), parse_state.end(), wanted) == parse_state.end()) {
                return;
        }
 
@@ -363,6 +373,9 @@ SubtitleAsset::maybe_add_subtitle (string text, vector<ParseState> const & parse
                if (i.v_align) {
                        ps.v_align = i.v_align.get();
                }
+               if (i.z_position) {
+                       ps.z_position = i.z_position.get();
+               }
                if (i.direction) {
                        ps.direction = i.direction.get();
                }
@@ -407,6 +420,7 @@ SubtitleAsset::maybe_add_subtitle (string text, vector<ParseState> const & parse
                                ps.h_align.get_value_or(HAlign::CENTER),
                                ps.v_position.get_value_or(0),
                                ps.v_align.get_value_or(VAlign::CENTER),
+                               ps.z_position.get_value_or(0),
                                ps.direction.get_value_or (Direction::LTR),
                                text,
                                ps.effect.get_value_or (Effect::NONE),
@@ -449,6 +463,7 @@ SubtitleAsset::maybe_add_subtitle (string text, vector<ParseState> const & parse
                                ps.h_align.get_value_or(HAlign::CENTER),
                                ps.v_position.get_value_or(0),
                                ps.v_align.get_value_or(VAlign::CENTER),
+                               ps.z_position.get_value_or(0),
                                ps.fade_up_time.get_value_or(Time()),
                                ps.fade_down_time.get_value_or(Time())
                                )
@@ -526,7 +541,7 @@ SubtitleAsset::latest_subtitle_out () const
 
 
 bool
-SubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
+SubtitleAsset::equals(shared_ptr<const Asset> other_asset, EqualityOptions const& options, NoteHandler note) const
 {
        if (!Asset::equals (other_asset, options, note)) {
                return false;
@@ -556,8 +571,7 @@ SubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions opti
                        return false;
                }
 
-               if (string_i && *string_i != *string_j) {
-                       note (NoteType::ERROR, String::compose("subtitles differ in text or metadata: %1 vs %2", string_i->text(), string_j->text()));
+               if (string_i && !string_i->equals(string_j, options, note)) {
                        return false;
                }
 
@@ -673,6 +687,7 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* xml_root, int time_code_rate, S
        float last_h_position;
        VAlign last_v_align;
        float last_v_position;
+       float last_z_position;
        Direction last_direction;
 
        for (auto i: sorted) {
@@ -700,15 +715,17 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* xml_root, int time_code_rate, S
                            fabs(last_h_position - is->h_position()) > ALIGN_EPSILON ||
                            last_v_align != is->v_align() ||
                            fabs(last_v_position - is->v_position()) > ALIGN_EPSILON ||
+                           fabs(last_z_position - is->z_position()) > ALIGN_EPSILON ||
                            last_direction != is->direction()
                                ) {
-                               text = make_shared<order::Text>(subtitle, is->h_align(), is->h_position(), is->v_align(), is->v_position(), is->direction());
+                               text = make_shared<order::Text>(subtitle, is->h_align(), is->h_position(), is->v_align(), is->v_position(), is->z_position(), is->direction());
                                subtitle->children.push_back (text);
 
                                last_h_align = is->h_align ();
                                last_h_position = is->h_position ();
                                last_v_align = is->v_align ();
                                last_v_position = is->v_position ();
+                               last_z_position = is->z_position();
                                last_direction = is->direction ();
                        }
 
@@ -719,7 +736,7 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* xml_root, int time_code_rate, S
                if (ii) {
                        text.reset ();
                        subtitle->children.push_back (
-                               make_shared<order::Image>(subtitle, ii->id(), ii->png_image(), ii->h_align(), ii->h_position(), ii->v_align(), ii->v_position())
+                               make_shared<order::Image>(subtitle, ii->id(), ii->png_image(), ii->h_align(), ii->h_position(), ii->v_align(), ii->v_position(), ii->z_position())
                                );
                }
        }
@@ -799,3 +816,124 @@ SubtitleAsset::fix_empty_font_ids ()
                }
        }
 }
+
+
+namespace {
+
+struct State
+{
+       int indent;
+       string xml;
+       int disable_formatting;
+};
+
+}
+
+
+static
+void
+format_xml_node (xmlpp::Node const* node, State& state)
+{
+       if (auto text_node = dynamic_cast<const xmlpp::TextNode*>(node)) {
+               string content = text_node->get_content();
+               boost::replace_all(content, "&", "&amp;");
+               boost::replace_all(content, "<", "&lt;");
+               boost::replace_all(content, ">", "&gt;");
+               state.xml += content;
+       } else if (auto element = dynamic_cast<const xmlpp::Element*>(node)) {
+               ++state.indent;
+
+               auto children = element->get_children();
+               auto const should_disable_formatting =
+                       std::any_of(
+                               children.begin(), children.end(),
+                               [](xmlpp::Node const* node) { return static_cast<bool>(dynamic_cast<const xmlpp::ContentNode*>(node)); }
+                               ) || element->get_name() == "Text";
+
+               if (!state.disable_formatting) {
+                       state.xml += "\n" + string(state.indent * 2, ' ');
+               }
+
+               state.xml += "<" + element->get_name();
+
+               for (auto attribute: element->get_attributes()) {
+                       state.xml += String::compose(" %1=\"%2\"", attribute->get_name().raw(), attribute->get_value().raw());
+               }
+
+               if (children.empty()) {
+                       state.xml += "/>";
+               } else {
+                       state.xml += ">";
+
+                       if (should_disable_formatting) {
+                               ++state.disable_formatting;
+                       }
+
+                       for (auto child: children) {
+                               format_xml_node(child, state);
+                       }
+
+                       if (!state.disable_formatting) {
+                               state.xml += "\n" + string(state.indent * 2, ' ');
+                       }
+
+                       state.xml += String::compose("</%1>", element->get_name().raw());
+
+                       if (should_disable_formatting) {
+                               --state.disable_formatting;
+                       }
+               }
+
+               --state.indent;
+       }
+}
+
+
+/** Format XML much as write_to_string_formatted() would do, except without adding any white space
+ *  to <Text> nodes.  This is an attempt to avoid changing what is actually displayed as subtitles
+ *  while also formatting the XML in such a way as to avoid DoM bug 2205.
+ *
+ *  xml_namespace is an optional namespace for the root node; it would be nicer to set this up with
+ *  set_namespace_declaration in the caller and then to extract it here but I couldn't find a way
+ *  to get all namespaces with the libxml++ API.
+ */
+string
+SubtitleAsset::format_xml(xmlpp::Document const& document, optional<pair<string, string>> xml_namespace)
+{
+       auto root = document.get_root_node();
+
+       State state = {};
+       state.xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<" + root->get_name();
+
+       if (xml_namespace) {
+               if (xml_namespace->first.empty()) {
+                       state.xml += String::compose(" xmlns=\"%1\"", xml_namespace->second);
+               } else {
+                       state.xml += String::compose(" xmlns:%1=\"%2\"", xml_namespace->first, xml_namespace->second);
+               }
+       }
+
+       for (auto attribute: root->get_attributes()) {
+               state.xml += String::compose(" %1=\"%2\"", attribute->get_name().raw(), attribute->get_value().raw());
+       }
+
+       state.xml += ">";
+
+       for (auto child: document.get_root_node()->get_children()) {
+               format_xml_node(child, state);
+       }
+
+       state.xml += String::compose("\n</%1>\n", root->get_name().raw());
+
+       return state.xml;
+}
+
+
+void
+SubtitleAsset::ensure_font(string load_id, dcp::ArrayData data)
+{
+       if (std::find_if(_fonts.begin(), _fonts.end(), [load_id](Font const& font) { return font.load_id == load_id; }) == _fonts.end()) {
+               add_font(load_id, data);
+       }
+}
+