Cleanup: pass EqualityOptions as const&
[libdcp.git] / src / interop_subtitle_asset.cc
index 453cad8be7ce0aff053c2cad014b3a5817a74f4e..e7c1432b75ee56b6972020a85c4c93175aaa275e 100644 (file)
@@ -40,6 +40,7 @@
 #include "compose.hpp"
 #include "dcp_assert.h"
 #include "font_asset.h"
+#include "file.h"
 #include "interop_load_font_node.h"
 #include "interop_subtitle_asset.h"
 #include "raw_convert.h"
@@ -56,16 +57,13 @@ LIBDCP_ENABLE_WARNINGS
 #include <cstdio>
 
 
-using std::list;
-using std::string;
-using std::cout;
 using std::cerr;
-using std::map;
-using std::shared_ptr;
+using std::cout;
 using std::dynamic_pointer_cast;
-using std::vector;
 using std::make_shared;
-using boost::shared_array;
+using std::shared_ptr;
+using std::string;
+using std::vector;
 using boost::optional;
 using namespace dcp;
 
@@ -121,14 +119,14 @@ InteropSubtitleAsset::xml_as_string () const
        root->add_child("Language")->add_child_text (_language);
 
        for (auto i: _load_font_nodes) {
-               xmlpp::Element* load_font = root->add_child("LoadFont");
+               auto load_font = root->add_child("LoadFont");
                load_font->set_attribute ("Id", i->id);
                load_font->set_attribute ("URI", i->uri);
        }
 
        subtitles_as_xml (root, 250, Standard::INTEROP);
 
-       return doc.write_to_string ("UTF-8");
+       return format_xml(doc, {});
 }
 
 
@@ -137,12 +135,12 @@ InteropSubtitleAsset::add_font (string load_id, dcp::ArrayData data)
 {
        _fonts.push_back (Font(load_id, make_uuid(), data));
        auto const uri = String::compose("font_%1.ttf", _load_font_nodes.size());
-       _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode>(new InteropLoadFontNode(load_id, uri)));
+       _load_font_nodes.push_back (make_shared<InteropLoadFontNode>(load_id, uri));
 }
 
 
 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;
@@ -194,15 +192,14 @@ InteropSubtitleAsset::load_font_nodes () const
 void
 InteropSubtitleAsset::write (boost::filesystem::path p) const
 {
-       auto f = fopen_boost (p, "w");
+       File f(p, "wb");
        if (!f) {
                throw FileError ("Could not open file for writing", p, -1);
        }
 
-       auto const s = xml_as_string ();
+       _raw_xml = xml_as_string ();
        /* length() here gives bytes not characters */
-       fwrite (s.c_str(), 1, s.length(), f);
-       fclose (f);
+       f.write(_raw_xml->c_str(), 1, _raw_xml->length());
 
        _file = p;
 
@@ -217,13 +214,10 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const
        /* 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 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;
                }
        }
 }
@@ -236,49 +230,63 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const
 void
 InteropSubtitleAsset::resolve_fonts (vector<shared_ptr<Asset>> assets)
 {
-       for (auto i: assets) {
-               auto font = dynamic_pointer_cast<FontAsset> (i);
+       for (auto asset: assets) {
+               auto font = dynamic_pointer_cast<FontAsset>(asset);
                if (!font) {
                        continue;
                }
 
-               for (auto j: _load_font_nodes) {
-                       bool got = false;
-                       for (auto const& k: _fonts) {
-                               if (k.load_id == j->id) {
-                                       got = true;
-                                       break;
-                               }
-                       }
+               DCP_ASSERT(_file);
 
-                       if (!got && font->file() && j->uri == font->file()->leaf().string()) {
-                               _fonts.push_back (Font (j->id, i->id(), font->file().get()));
+               for (auto load_font_node: _load_font_nodes) {
+                       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()));
+                               }
                        }
                }
        }
 }
 
 
-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;
 }
 
 
 void
-InteropSubtitleAsset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
+InteropSubtitleAsset::add_to_assetmap (AssetMap& asset_map, boost::filesystem::path root) const
 {
-       Asset::write_to_assetmap (node, root);
+       Asset::add_to_assetmap(asset_map, root);
 
        for (auto i: _subtitles) {
-               auto im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
+               auto im = dynamic_pointer_cast<dcp::SubtitleImage>(i);
                if (im) {
-                       DCP_ASSERT (im->file());
-                       write_file_to_assetmap (node, root, im->file().get(), im->id());
+                       DCP_ASSERT(im->file());
+                       add_file_to_assetmap(asset_map, root, im->file().get(), im->id());
                }
        }
 }
@@ -293,7 +301,7 @@ InteropSubtitleAsset::add_to_pkl (shared_ptr<PKL> pkl, boost::filesystem::path r
                auto im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
                if (im) {
                        auto png_image = im->png_image ();
-                       pkl->add_asset (im->id(), optional<string>(), make_digest(png_image), png_image.size(), "image/png");
+                       pkl->add_asset(im->id(), optional<string>(), make_digest(png_image), png_image.size(), "image/png", root.filename().string());
                }
        }
 }
@@ -315,3 +323,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;
+}
+