Switch to UUIDs for Interop image subtitle identification (rather than indices)....
[libdcp.git] / src / interop_subtitle_asset.cc
index 620fed7362d6d1bc206d20326d7de8ab27219506..2ef82c901e64b0b9ba9ea014d8a2fb312fd70a6d 100644 (file)
@@ -1,32 +1,49 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
 
-    This program is free software; you can redistribute it and/or modify
+    This file is part of libdcp.
+
+    libdcp is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    This program is distributed in the hope that it will be useful,
+    libdcp is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
+    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
+
+    In addition, as a special exception, the copyright holders give
+    permission to link the code of portions of this program with the
+    OpenSSL library under certain conditions as described in each
+    individual source file, and distribute linked combinations
+    including the two.
+
+    You must obey the GNU General Public License in all respects
+    for all of the code used other than OpenSSL.  If you modify
+    file(s) with this exception, you may extend this exception to your
+    version of the file(s), but you are not obligated to do so.  If you
+    do not wish to do so, delete this exception statement from your
+    version.  If you delete this exception statement from all source
+    files in the program, then also delete it here.
 */
 
 #include "interop_subtitle_asset.h"
 #include "interop_load_font_node.h"
+#include "subtitle_asset_internal.h"
 #include "xml.h"
 #include "raw_convert.h"
-#include "font_node.h"
 #include "util.h"
 #include "font_asset.h"
 #include "dcp_assert.h"
+#include "compose.hpp"
+#include "subtitle_image.h"
 #include <libxml++/libxml++.h>
 #include <boost/foreach.hpp>
+#include <boost/weak_ptr.hpp>
 #include <cmath>
 #include <cstdio>
 
@@ -50,15 +67,18 @@ InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
        _reel_number = xml->string_child ("ReelNumber");
        _language = xml->string_child ("Language");
        _movie_title = xml->string_child ("MovieTitle");
-       _load_font_nodes = type_children<dcp::InteropLoadFontNode> (xml, "LoadFont");
+       _load_font_nodes = type_children<InteropLoadFontNode> (xml, "LoadFont");
 
-       list<cxml::NodePtr> f = xml->node_children ("Font");
-       list<shared_ptr<dcp::FontNode> > font_nodes;
-       BOOST_FOREACH (cxml::NodePtr& i, f) {
-               font_nodes.push_back (shared_ptr<FontNode> (new FontNode (i, 250)));
-       }
+       /* Now we need to drop down to xmlpp */
 
-       parse_subtitles (xml, font_nodes);
+       list<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);
+               if (e && (e->get_name() == "Font" || e->get_name() == "Subtitle")) {
+                       parse_subtitles (e, ps, optional<int>(), INTEROP);
+               }
+       }
 }
 
 InteropSubtitleAsset::InteropSubtitleAsset ()
@@ -66,7 +86,7 @@ InteropSubtitleAsset::InteropSubtitleAsset ()
 
 }
 
-Glib::ustring
+string
 InteropSubtitleAsset::xml_as_string () const
 {
        xmlpp::Document doc;
@@ -84,9 +104,9 @@ InteropSubtitleAsset::xml_as_string () const
                load_font->set_attribute ("URI", (*i)->uri);
        }
 
-       subtitles_as_xml (root, 250, "");
+       subtitles_as_xml (root, 250, INTEROP);
 
-       return doc.write_to_string_formatted ("UTF-8");
+       return doc.write_to_string ("UTF-8");
 }
 
 void
@@ -151,15 +171,27 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const
                throw FileError ("Could not open file for writing", p, -1);
        }
 
-       Glib::ustring const s = xml_as_string ();
-       fwrite (s.c_str(), 1, s.bytes(), f);
+       string const s = xml_as_string ();
+       /* length() here gives bytes not characters */
+       fwrite (s.c_str(), 1, s.length(), f);
        fclose (f);
 
        _file = p;
 
+       /* Image subtitles */
+       BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, _subtitles) {
+               shared_ptr<dcp::SubtitleImage> im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
+               if (im) {
+                       ImageUUIDMap::const_iterator uuid = _image_subtitle_uuid.find(im);
+                       DCP_ASSERT (uuid != _image_subtitle_uuid.end());
+                       im->png_image().write (p.parent_path() / String::compose("%1.png", uuid->second));
+               }
+       }
+
+       /* Fonts */
        BOOST_FOREACH (shared_ptr<InteropLoadFontNode> i, _load_font_nodes) {
                boost::filesystem::path file = p.parent_path() / i->uri;
-               FILE* f = fopen_boost (file, "w");
+               FILE* f = fopen_boost (file, "wb");
                if (!f) {
                        throw FileError ("could not open font file for writing", file, errno);
                }
@@ -168,25 +200,37 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const
                        ++j;
                }
                if (j != _fonts.end ()) {
-                       fwrite (j->data.data.get(), 1, j->data.size, f);
+                       fwrite (j->data.data().get(), 1, j->data.size(), f);
                        j->file = file;
                }
                fclose (f);
        }
 }
 
+/** 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.
+ */
 void
-InteropSubtitleAsset::resolve_fonts (list<shared_ptr<Object> > objects)
+InteropSubtitleAsset::resolve_fonts (list<shared_ptr<Asset> > assets)
 {
-       BOOST_FOREACH (shared_ptr<Object> i, objects) {
+       BOOST_FOREACH (shared_ptr<Asset> i, assets) {
                shared_ptr<FontAsset> font = dynamic_pointer_cast<FontAsset> (i);
                if (!font) {
                        continue;
                }
 
                BOOST_FOREACH (shared_ptr<InteropLoadFontNode> j, _load_font_nodes) {
-                       if (j->uri == font->file().leaf().string ()) {
-                               _fonts.push_back (Font (j->id, i->id(), font->file ()));
+                       bool got = false;
+                       BOOST_FOREACH (Font const & k, _fonts) {
+                               if (k.load_id == j->id) {
+                                       got = true;
+                                       break;
+                               }
+                       }
+
+                       if (!got && font->file() && j->uri == font->file()->leaf().string()) {
+                               _fonts.push_back (Font (j->id, i->id(), font->file().get()));
                        }
                }
        }