Rename Font -> FontAsset; add dcpdumpsub tool.
[libdcp.git] / src / interop_subtitle_asset.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "interop_subtitle_asset.h"
21 #include "interop_load_font_node.h"
22 #include "xml.h"
23 #include "raw_convert.h"
24 #include "font_node.h"
25 #include "util.h"
26 #include "font_asset.h"
27 #include "dcp_assert.h"
28 #include <libxml++/libxml++.h>
29 #include <boost/foreach.hpp>
30 #include <cmath>
31 #include <cstdio>
32
33 using std::list;
34 using std::string;
35 using std::cout;
36 using std::cerr;
37 using std::map;
38 using boost::shared_ptr;
39 using boost::shared_array;
40 using boost::optional;
41 using boost::dynamic_pointer_cast;
42 using namespace dcp;
43
44 InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
45         : SubtitleAsset (file)
46 {
47         shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
48         xml->read_file (file);
49         _id = xml->string_child ("SubtitleID");
50         _reel_number = xml->string_child ("ReelNumber");
51         _language = xml->string_child ("Language");
52         _movie_title = xml->string_child ("MovieTitle");
53         _load_font_nodes = type_children<dcp::InteropLoadFontNode> (xml, "LoadFont");
54
55         list<cxml::NodePtr> f = xml->node_children ("Font");
56         list<shared_ptr<dcp::FontNode> > font_nodes;
57         BOOST_FOREACH (cxml::NodePtr& i, f) {
58                 font_nodes.push_back (shared_ptr<FontNode> (new FontNode (i, 250)));
59         }
60
61         parse_subtitles (xml, font_nodes);
62 }
63
64 InteropSubtitleAsset::InteropSubtitleAsset ()
65 {
66         
67 }
68
69 Glib::ustring
70 InteropSubtitleAsset::xml_as_string () const
71 {
72         xmlpp::Document doc;
73         xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
74         root->set_attribute ("Version", "1.0");
75
76         root->add_child("SubtitleID")->add_child_text (_id);
77         root->add_child("MovieTitle")->add_child_text (_movie_title);
78         root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
79         root->add_child("Language")->add_child_text (_language);
80
81         for (list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin(); i != _load_font_nodes.end(); ++i) {
82                 xmlpp::Element* load_font = root->add_child("LoadFont");
83                 load_font->set_attribute ("Id", (*i)->id);
84                 load_font->set_attribute ("URI", (*i)->uri);
85         }
86
87         subtitles_as_xml (root, 250, "");
88
89         return doc.write_to_string_formatted ("UTF-8");
90 }
91
92 void
93 InteropSubtitleAsset::add_font (string id, boost::filesystem::path file)
94 {
95         add_font_data (id, file);
96         _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode> (new InteropLoadFontNode (id, file.leaf().string ())));
97 }
98
99 bool
100 InteropSubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
101 {
102         if (!SubtitleAsset::equals (other_asset, options, note)) {
103                 return false;
104         }
105         
106         shared_ptr<const InteropSubtitleAsset> other = dynamic_pointer_cast<const InteropSubtitleAsset> (other_asset);
107         if (!other) {
108                 return false;
109         }
110
111         list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin ();
112         list<shared_ptr<InteropLoadFontNode> >::const_iterator j = other->_load_font_nodes.begin ();
113
114         while (i != _load_font_nodes.end ()) {
115                 if (j == other->_load_font_nodes.end ()) {
116                         note (DCP_ERROR, "<LoadFont> nodes differ");
117                         return false;
118                 }
119
120                 if (**i != **j) {
121                         note (DCP_ERROR, "<LoadFont> nodes differ");
122                         return false;
123                 }
124
125                 ++i;
126                 ++j;
127         }
128
129         if (_movie_title != other->_movie_title) {
130                 note (DCP_ERROR, "Subtitle movie titles differ");
131                 return false;
132         }
133
134         return true;
135 }
136
137 list<shared_ptr<LoadFontNode> >
138 InteropSubtitleAsset::load_font_nodes () const
139 {
140         list<shared_ptr<LoadFontNode> > lf;
141         copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
142         return lf;
143 }
144
145 /** Write this content to an XML file with its fonts alongside */
146 void
147 InteropSubtitleAsset::write (boost::filesystem::path p) const
148 {
149         FILE* f = fopen_boost (p, "w");
150         if (!f) {
151                 throw FileError ("Could not open file for writing", p, -1);
152         }
153         
154         Glib::ustring const s = xml_as_string ();
155         fwrite (s.c_str(), 1, s.bytes(), f);
156         fclose (f);
157
158         _file = p;
159
160         BOOST_FOREACH (shared_ptr<InteropLoadFontNode> i, _load_font_nodes) {
161                 boost::filesystem::path file = p.parent_path() / i->uri;
162                 FILE* f = fopen_boost (file, "w");
163                 if (!f) {
164                         throw FileError ("could not open font file for writing", file, errno);
165                 }
166                 map<string, FileData>::const_iterator j = _fonts.find (i->id);
167                 if (j != _fonts.end ()) {
168                         fwrite (j->second.data.get(), 1, j->second.size, f);
169                         j->second.file = file;
170                 }
171                 fclose (f);
172         }
173 }
174
175 void
176 InteropSubtitleAsset::resolve_fonts (list<shared_ptr<Object> > objects)
177 {
178         BOOST_FOREACH (shared_ptr<Object> i, objects) {
179                 shared_ptr<FontAsset> font = dynamic_pointer_cast<FontAsset> (i);
180                 if (!font) {
181                         continue;
182                 }
183
184                 BOOST_FOREACH (shared_ptr<InteropLoadFontNode> j, _load_font_nodes) {
185                         if (j->uri == font->file().leaf().string ()) {
186                                 add_font_data (j->id, font->file ());
187                         }
188                 }
189         }
190 }
191
192 void
193 InteropSubtitleAsset::add_font_assets (list<shared_ptr<Asset> >& assets)
194 {
195         for (map<string, FileData>::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
196                 DCP_ASSERT (i->second.file);
197                 assets.push_back (shared_ptr<FontAsset> (new FontAsset (i->second.file.get ())));
198         }
199 }