In-line run of subs_in_out so that it gets the environment more easily.
[libdcp.git] / src / interop_subtitle_asset.cc
index f93e7cf42ec6252961aca47e31974f66c8567c15..32c3f66a4268649985fb0c031313b54c5b9123cc 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
 
     This file is part of libdcp.
 
     files in the program, then also delete it here.
 */
 
-#include "interop_subtitle_asset.h"
+
+/** @file  src/interop_subtitle_asset.cc
+ *  @brief InteropSubtitleAsset class
+ */
+
+
+#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"
-#include "subtitle_asset_internal.h"
-#include "xml.h"
+#include "interop_subtitle_asset.h"
 #include "raw_convert.h"
-#include "util.h"
-#include "font_asset.h"
-#include "dcp_assert.h"
-#include "compose.hpp"
+#include "subtitle_asset_internal.h"
 #include "subtitle_image.h"
+#include "util.h"
+#include "warnings.h"
+#include "xml.h"
+LIBDCP_DISABLE_WARNINGS
 #include <libxml++/libxml++.h>
-#include <boost/foreach.hpp>
+LIBDCP_ENABLE_WARNINGS
 #include <boost/weak_ptr.hpp>
 #include <cmath>
 #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::make_shared;
+using std::shared_ptr;
+using std::string;
 using std::vector;
-using boost::shared_array;
 using boost::optional;
 using namespace dcp;
 
+
 InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
        : SubtitleAsset (file)
 {
        _raw_xml = dcp::file_to_string (file);
 
-       shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
-       xml->read_file (file);
+       auto xml = make_shared<cxml::Document>("DCSubtitle");
+       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");
@@ -75,32 +86,33 @@ InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
        /* Now we need to drop down to xmlpp */
 
        vector<ParseState> ps;
-       xmlpp::Node::NodeList c = xml->node()->get_children ();
-       for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
-               xmlpp::Element const * e = dynamic_cast<xmlpp::Element const *> (*i);
+       for (auto i: xml->node()->get_children()) {
+               auto e = dynamic_cast<xmlpp::Element const *>(i);
                if (e && (e->get_name() == "Font" || e->get_name() == "Subtitle")) {
                        parse_subtitles (e, ps, optional<int>(), Standard::INTEROP);
                }
        }
 
-       BOOST_FOREACH (shared_ptr<Subtitle> i, _subtitles) {
-               shared_ptr<SubtitleImage> si = dynamic_pointer_cast<SubtitleImage>(i);
+       for (auto i: _subtitles) {
+               auto si = dynamic_pointer_cast<SubtitleImage>(i);
                if (si) {
                        si->read_png_file (file.parent_path() / String::compose("%1.png", si->id()));
                }
        }
 }
 
+
 InteropSubtitleAsset::InteropSubtitleAsset ()
 {
 
 }
 
+
 string
 InteropSubtitleAsset::xml_as_string () const
 {
        xmlpp::Document doc;
-       xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
+       auto root = doc.create_root_node ("DCSubtitle");
        root->set_attribute ("Version", "1.0");
 
        root->add_child("SubtitleID")->add_child_text (_id);
@@ -109,32 +121,34 @@ 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, {});
 }
 
+
 void
 InteropSubtitleAsset::add_font (string load_id, dcp::ArrayData data)
 {
        _fonts.push_back (Font(load_id, make_uuid(), data));
-       string const uri = String::compose("font_%1.ttf", _load_font_nodes.size());
-       _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode>(new InteropLoadFontNode(load_id, uri)));
+       auto const uri = String::compose("font_%1.ttf", _load_font_nodes.size());
+       _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;
        }
 
-       shared_ptr<const InteropSubtitleAsset> other = dynamic_pointer_cast<const InteropSubtitleAsset> (other_asset);
+       auto other = dynamic_pointer_cast<const InteropSubtitleAsset> (other_asset);
        if (!other) {
                return false;
        }
@@ -167,6 +181,7 @@ InteropSubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptio
        return true;
 }
 
+
 vector<shared_ptr<LoadFontNode>>
 InteropSubtitleAsset::load_font_nodes () const
 {
@@ -175,44 +190,41 @@ InteropSubtitleAsset::load_font_nodes () const
        return lf;
 }
 
-/** Write this content to an XML file with its fonts alongside */
+
 void
 InteropSubtitleAsset::write (boost::filesystem::path p) const
 {
-       FILE* f = fopen_boost (p, "w");
+       File f(p, "wb");
        if (!f) {
                throw FileError ("Could not open file for writing", p, -1);
        }
 
-       string 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;
 
        /* Image subtitles */
-       BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, _subtitles) {
-               shared_ptr<dcp::SubtitleImage> im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
+       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()));
                }
        }
 
        /* Fonts */
-       BOOST_FOREACH (shared_ptr<InteropLoadFontNode> i, _load_font_nodes) {
-               boost::filesystem::path 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;
+       for (auto i: _load_font_nodes) {
+               auto file = p.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;
                }
        }
 }
 
+
 /** Look at a supplied list of assets and find the fonts.  Then match these
  *  fonts up with anything requested by a <LoadFont> so that _fonts contains
  *  a list of font ID, load ID and data.
@@ -220,61 +232,78 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const
 void
 InteropSubtitleAsset::resolve_fonts (vector<shared_ptr<Asset>> assets)
 {
-       BOOST_FOREACH (shared_ptr<Asset> i, assets) {
-               shared_ptr<FontAsset> font = dynamic_pointer_cast<FontAsset> (i);
+       for (auto asset: assets) {
+               auto font = dynamic_pointer_cast<FontAsset>(asset);
                if (!font) {
                        continue;
                }
 
-               BOOST_FOREACH (shared_ptr<InteropLoadFontNode> j, _load_font_nodes) {
-                       bool got = false;
-                       BOOST_FOREACH (Font 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()));
+       }
+       return assets;
+}
+
+
+vector<shared_ptr<const Asset>>
+InteropSubtitleAsset::font_assets() const
 {
-       BOOST_FOREACH (Font const & i, _fonts) {
+       vector<shared_ptr<const Asset>> assets;
+       for (auto const& i: _fonts) {
                DCP_ASSERT (i.file);
-               assets.push_back (shared_ptr<FontAsset> (new FontAsset (i.uuid, i.file.get ())));
+               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);
 
-       BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, _subtitles) {
-               shared_ptr<dcp::SubtitleImage> im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
+       for (auto i: _subtitles) {
+               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());
                }
        }
 }
 
+
 void
 InteropSubtitleAsset::add_to_pkl (shared_ptr<PKL> pkl, boost::filesystem::path root) const
 {
        Asset::add_to_pkl (pkl, root);
 
-       BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, _subtitles) {
-               shared_ptr<dcp::SubtitleImage> im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
+       for (auto i: _subtitles) {
+               auto im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
                if (im) {
-                       ArrayData png_image = im->png_image ();
-                       pkl->add_asset (im->id(), optional<string>(), make_digest(png_image), png_image.size(), "image/png");
+                       auto png_image = im->png_image ();
+                       pkl->add_asset(im->id(), optional<string>(), make_digest(png_image), png_image.size(), "image/png", root.filename().string());
                }
        }
 }
@@ -283,16 +312,29 @@ InteropSubtitleAsset::add_to_pkl (shared_ptr<PKL> pkl, boost::filesystem::path r
 void
 InteropSubtitleAsset::set_font_file (string load_id, boost::filesystem::path file)
 {
-       BOOST_FOREACH (Font& i, _fonts) {
+       for (auto& i: _fonts) {
                if (i.load_id == load_id) {
                        i.file = file;
                }
        }
 
-       BOOST_FOREACH (shared_ptr<InteropLoadFontNode> i, _load_font_nodes) {
+       for (auto i: _load_font_nodes) {
                if (i->id == load_id) {
                        i->uri = file.filename().string();
                }
        }
 }
 
+
+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;
+}
+