Pass optimisation setting through to subtitle XML writers.
[libdcp.git] / src / interop_subtitle_asset.cc
index 15694205baed53ab56c0c56c32b74fa220f13d42..0c35abbf3aeb946130d265df4d3dc7620ed06c91 100644 (file)
@@ -39,6 +39,8 @@
 
 #include "compose.hpp"
 #include "dcp_assert.h"
+#include "equality_options.h"
+#include "filesystem.h"
 #include "font_asset.h"
 #include "file.h"
 #include "interop_load_font_node.h"
@@ -74,7 +76,7 @@ InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
        _raw_xml = dcp::file_to_string (file);
 
        auto xml = make_shared<cxml::Document>("DCSubtitle");
-       xml->read_file (file);
+       xml->read_file(dcp::filesystem::fix_long_path(file));
        _id = xml->string_child ("SubtitleID");
        _reel_number = xml->string_child ("ReelNumber");
        _language = xml->string_child ("Language");
@@ -107,7 +109,7 @@ InteropSubtitleAsset::InteropSubtitleAsset ()
 
 
 string
-InteropSubtitleAsset::xml_as_string () const
+InteropSubtitleAsset::xml_as_string(SubtitleOptimisation optimisation) const
 {
        xmlpp::Document doc;
        auto root = doc.create_root_node ("DCSubtitle");
@@ -124,7 +126,7 @@ InteropSubtitleAsset::xml_as_string () const
                load_font->set_attribute ("URI", i->uri);
        }
 
-       subtitles_as_xml (root, 250, Standard::INTEROP);
+       subtitles_as_xml(root, 250, Standard::INTEROP, optimisation);
 
        return format_xml(doc, {});
 }
@@ -140,7 +142,7 @@ InteropSubtitleAsset::add_font (string load_id, dcp::ArrayData data)
 
 
 bool
-InteropSubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
+InteropSubtitleAsset::equals(shared_ptr<const Asset> other_asset, EqualityOptions const& options, NoteHandler note) const
 {
        if (!SubtitleAsset::equals (other_asset, options, note)) {
                return false;
@@ -190,37 +192,34 @@ InteropSubtitleAsset::load_font_nodes () const
 
 
 void
-InteropSubtitleAsset::write (boost::filesystem::path p) const
+InteropSubtitleAsset::write(boost::filesystem::path path, SubtitleOptimisation optimisation) const
 {
-       File f(p, "wb");
-       if (!f) {
-               throw FileError ("Could not open file for writing", p, -1);
+       File file(path, "wb");
+       if (!file) {
+               throw FileError("Could not open file for writing", path, -1);
        }
 
-       _raw_xml = xml_as_string ();
+       _raw_xml = xml_as_string(optimisation);
        /* length() here gives bytes not characters */
-       f.write(_raw_xml->c_str(), 1, _raw_xml->length());
+       file.write(_raw_xml->c_str(), 1, _raw_xml->length());
 
-       _file = p;
+       _file = path;
 
        /* Image subtitles */
        for (auto i: _subtitles) {
                auto im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
                if (im) {
-                       im->write_png_file(p.parent_path() / String::compose("%1.png", im->id()));
+                       im->write_png_file(path.parent_path() / String::compose("%1.png", im->id()));
                }
        }
 
        /* Fonts */
        for (auto i: _load_font_nodes) {
-               auto file = p.parent_path() / i->uri;
-               auto j = _fonts.begin();
-               while (j != _fonts.end() && j->load_id != i->id) {
-                       ++j;
-               }
-               if (j != _fonts.end ()) {
-                       j->data.write (file);
-                       j->file = file;
+               auto file = path.parent_path() / i->uri;
+               auto font_with_id = std::find_if(_fonts.begin(), _fonts.end(), [i](Font const& font) { return font.load_id == i->id; });
+               if (font_with_id != _fonts.end()) {
+                       font_with_id->data.write(file);
+                       font_with_id->file = file;
                }
        }
 }
@@ -239,30 +238,44 @@ InteropSubtitleAsset::resolve_fonts (vector<shared_ptr<Asset>> assets)
                        continue;
                }
 
+               DCP_ASSERT(_file);
+
                for (auto load_font_node: _load_font_nodes) {
-                       bool got = false;
-                       for (auto const& font: _fonts) {
-                               if (font.load_id == load_font_node->id) {
-                                       got = true;
-                                       break;
+                       auto const path_in_load_font_node = _file->parent_path() / load_font_node->uri;
+                       if (font->file() && path_in_load_font_node == *font->file()) {
+                               auto existing = std::find_if(_fonts.begin(), _fonts.end(), [load_font_node](Font const& font) { return font.load_id == load_font_node->id; });
+                               if (existing != _fonts.end()) {
+                                       *existing = Font(load_font_node->id, asset->id(), font->file().get());
+                               } else {
+                                       _fonts.push_back(Font(load_font_node->id, asset->id(), font->file().get()));
                                }
                        }
-
-                       if (!got && font->file() && load_font_node->uri == font->file()->leaf().string()) {
-                               _fonts.push_back(Font(load_font_node->id, asset->id(), font->file().get()));
-                       }
                }
        }
 }
 
 
-void
-InteropSubtitleAsset::add_font_assets (vector<shared_ptr<Asset>>& assets)
+vector<shared_ptr<Asset>>
+InteropSubtitleAsset::font_assets()
 {
+       vector<shared_ptr<Asset>> assets;
        for (auto const& i: _fonts) {
                DCP_ASSERT (i.file);
-               assets.push_back (make_shared<FontAsset>(i.uuid, i.file.get()));
+               assets.push_back(make_shared<FontAsset>(i.uuid, i.file.get()));
        }
+       return assets;
+}
+
+
+vector<shared_ptr<const Asset>>
+InteropSubtitleAsset::font_assets() const
+{
+       vector<shared_ptr<const Asset>> assets;
+       for (auto const& i: _fonts) {
+               DCP_ASSERT (i.file);
+               assets.push_back(make_shared<const FontAsset>(i.uuid, i.file.get()));
+       }
+       return assets;
 }
 
 
@@ -312,3 +325,16 @@ InteropSubtitleAsset::set_font_file (string load_id, boost::filesystem::path fil
        }
 }
 
+
+vector<string>
+InteropSubtitleAsset::unresolved_fonts() const
+{
+       vector<string> unresolved;
+       for (auto load_font_node: _load_font_nodes) {
+               if (std::find_if(_fonts.begin(), _fonts.end(), [load_font_node](Font const& font) { return font.load_id == load_font_node->id; }) == _fonts.end()) {
+                       unresolved.push_back(load_font_node->id);
+               }
+       }
+       return unresolved;
+}
+