Fix various SNAFUs with Font ID handling.
[libdcp.git] / src / interop_subtitle_asset.cc
index 231dda16d4f96a718d31bf643022eef942c239d1..a3850f0408b7b02137336b61449e8d86c16fdee6 100644 (file)
@@ -23,6 +23,8 @@
 #include "raw_convert.h"
 #include "font_node.h"
 #include "util.h"
+#include "font_asset.h"
+#include "dcp_assert.h"
 #include <libxml++/libxml++.h>
 #include <boost/foreach.hpp>
 #include <cmath>
 using std::list;
 using std::string;
 using std::cout;
+using std::cerr;
+using std::map;
 using boost::shared_ptr;
+using boost::shared_array;
 using boost::optional;
 using boost::dynamic_pointer_cast;
 using namespace dcp;
@@ -58,7 +63,7 @@ InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
 
 InteropSubtitleAsset::InteropSubtitleAsset ()
 {
-       
+
 }
 
 Glib::ustring
@@ -85,9 +90,10 @@ InteropSubtitleAsset::xml_as_string () const
 }
 
 void
-InteropSubtitleAsset::add_font (string id, string uri)
+InteropSubtitleAsset::add_font (string load_id, boost::filesystem::path file)
 {
-       _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode> (new InteropLoadFontNode (id, uri)));
+       _fonts.push_back (Font (load_id, make_uuid(), file));
+       _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode> (new InteropLoadFontNode (load_id, file.leaf().string ())));
 }
 
 bool
@@ -96,7 +102,7 @@ InteropSubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptio
        if (!SubtitleAsset::equals (other_asset, options, note)) {
                return false;
        }
-       
+
        shared_ptr<const InteropSubtitleAsset> other = dynamic_pointer_cast<const InteropSubtitleAsset> (other_asset);
        if (!other) {
                return false;
@@ -136,7 +142,7 @@ InteropSubtitleAsset::load_font_nodes () const
        return lf;
 }
 
-/** Write this content to an XML file */
+/** Write this content to an XML file with its fonts alongside */
 void
 InteropSubtitleAsset::write (boost::filesystem::path p) const
 {
@@ -144,10 +150,53 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const
        if (!f) {
                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);
        fclose (f);
 
        _file = p;
+
+       BOOST_FOREACH (shared_ptr<InteropLoadFontNode> i, _load_font_nodes) {
+               boost::filesystem::path file = p.parent_path() / i->uri;
+               FILE* f = fopen_boost (file, "w");
+               if (!f) {
+                       throw FileError ("could not open font file for writing", file, errno);
+               }
+               list<Font>::const_iterator j = _fonts.begin ();
+               while (j->load_id != i->id) {
+                       ++j;
+               }
+               if (j != _fonts.end ()) {
+                       fwrite (j->data.data.get(), 1, j->data.size, f);
+                       j->file = file;
+               }
+               fclose (f);
+       }
+}
+
+void
+InteropSubtitleAsset::resolve_fonts (list<shared_ptr<Object> > objects)
+{
+       BOOST_FOREACH (shared_ptr<Object> i, objects) {
+               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 ()));
+                       }
+               }
+       }
+}
+
+void
+InteropSubtitleAsset::add_font_assets (list<shared_ptr<Asset> >& assets)
+{
+       BOOST_FOREACH (Font const & i, _fonts) {
+               DCP_ASSERT (i.file);
+               assets.push_back (shared_ptr<FontAsset> (new FontAsset (i.uuid, i.file.get ())));
+       }
 }