Store image subtitle ID in the object, rather than a separate map. Start of reading...
[libdcp.git] / src / interop_subtitle_asset.cc
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #include "interop_subtitle_asset.h"
35 #include "interop_load_font_node.h"
36 #include "subtitle_asset_internal.h"
37 #include "xml.h"
38 #include "raw_convert.h"
39 #include "util.h"
40 #include "font_asset.h"
41 #include "dcp_assert.h"
42 #include "compose.hpp"
43 #include "subtitle_image.h"
44 #include <libxml++/libxml++.h>
45 #include <boost/foreach.hpp>
46 #include <boost/weak_ptr.hpp>
47 #include <cmath>
48 #include <cstdio>
49
50 using std::list;
51 using std::string;
52 using std::cout;
53 using std::cerr;
54 using std::map;
55 using boost::shared_ptr;
56 using boost::shared_array;
57 using boost::optional;
58 using boost::dynamic_pointer_cast;
59 using namespace dcp;
60
61 InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
62         : SubtitleAsset (file)
63 {
64         shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
65         xml->read_file (file);
66         _id = xml->string_child ("SubtitleID");
67         _reel_number = xml->string_child ("ReelNumber");
68         _language = xml->string_child ("Language");
69         _movie_title = xml->string_child ("MovieTitle");
70         _load_font_nodes = type_children<InteropLoadFontNode> (xml, "LoadFont");
71
72         /* Now we need to drop down to xmlpp */
73
74         list<ParseState> ps;
75         xmlpp::Node::NodeList c = xml->node()->get_children ();
76         for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
77                 xmlpp::Element const * e = dynamic_cast<xmlpp::Element const *> (*i);
78                 if (e && (e->get_name() == "Font" || e->get_name() == "Subtitle")) {
79                         parse_subtitles (e, ps, optional<int>(), INTEROP);
80                 }
81         }
82
83         /* XXX: now find SubtitleImages in _subtitles and load their PNG */
84 }
85
86 InteropSubtitleAsset::InteropSubtitleAsset ()
87 {
88
89 }
90
91 string
92 InteropSubtitleAsset::xml_as_string () const
93 {
94         xmlpp::Document doc;
95         xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
96         root->set_attribute ("Version", "1.0");
97
98         root->add_child("SubtitleID")->add_child_text (_id);
99         root->add_child("MovieTitle")->add_child_text (_movie_title);
100         root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
101         root->add_child("Language")->add_child_text (_language);
102
103         for (list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin(); i != _load_font_nodes.end(); ++i) {
104                 xmlpp::Element* load_font = root->add_child("LoadFont");
105                 load_font->set_attribute ("Id", (*i)->id);
106                 load_font->set_attribute ("URI", (*i)->uri);
107         }
108
109         subtitles_as_xml (root, 250, INTEROP);
110
111         return doc.write_to_string ("UTF-8");
112 }
113
114 void
115 InteropSubtitleAsset::add_font (string load_id, boost::filesystem::path file)
116 {
117         _fonts.push_back (Font (load_id, make_uuid(), file));
118         _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode> (new InteropLoadFontNode (load_id, file.leaf().string ())));
119 }
120
121 bool
122 InteropSubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
123 {
124         if (!SubtitleAsset::equals (other_asset, options, note)) {
125                 return false;
126         }
127
128         shared_ptr<const InteropSubtitleAsset> other = dynamic_pointer_cast<const InteropSubtitleAsset> (other_asset);
129         if (!other) {
130                 return false;
131         }
132
133         list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin ();
134         list<shared_ptr<InteropLoadFontNode> >::const_iterator j = other->_load_font_nodes.begin ();
135
136         while (i != _load_font_nodes.end ()) {
137                 if (j == other->_load_font_nodes.end ()) {
138                         note (DCP_ERROR, "<LoadFont> nodes differ");
139                         return false;
140                 }
141
142                 if (**i != **j) {
143                         note (DCP_ERROR, "<LoadFont> nodes differ");
144                         return false;
145                 }
146
147                 ++i;
148                 ++j;
149         }
150
151         if (_movie_title != other->_movie_title) {
152                 note (DCP_ERROR, "Subtitle movie titles differ");
153                 return false;
154         }
155
156         return true;
157 }
158
159 list<shared_ptr<LoadFontNode> >
160 InteropSubtitleAsset::load_font_nodes () const
161 {
162         list<shared_ptr<LoadFontNode> > lf;
163         copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
164         return lf;
165 }
166
167 /** Write this content to an XML file with its fonts alongside */
168 void
169 InteropSubtitleAsset::write (boost::filesystem::path p) const
170 {
171         FILE* f = fopen_boost (p, "w");
172         if (!f) {
173                 throw FileError ("Could not open file for writing", p, -1);
174         }
175
176         string const s = xml_as_string ();
177         /* length() here gives bytes not characters */
178         fwrite (s.c_str(), 1, s.length(), f);
179         fclose (f);
180
181         _file = p;
182
183         /* Image subtitles */
184         BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, _subtitles) {
185                 shared_ptr<dcp::SubtitleImage> im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
186                 if (im) {
187                         im->png_image().write (p.parent_path() / String::compose("%1.png", im->id()));
188                 }
189         }
190
191         /* Fonts */
192         BOOST_FOREACH (shared_ptr<InteropLoadFontNode> i, _load_font_nodes) {
193                 boost::filesystem::path file = p.parent_path() / i->uri;
194                 FILE* f = fopen_boost (file, "wb");
195                 if (!f) {
196                         throw FileError ("could not open font file for writing", file, errno);
197                 }
198                 list<Font>::const_iterator j = _fonts.begin ();
199                 while (j != _fonts.end() && j->load_id != i->id) {
200                         ++j;
201                 }
202                 if (j != _fonts.end ()) {
203                         fwrite (j->data.data().get(), 1, j->data.size(), f);
204                         j->file = file;
205                 }
206                 fclose (f);
207         }
208 }
209
210 /** Look at a supplied list of assets and find the fonts.  Then match these
211  *  fonts up with anything requested by a <LoadFont> so that _fonts contains
212  *  a list of font ID, load ID and data.
213  */
214 void
215 InteropSubtitleAsset::resolve_fonts (list<shared_ptr<Asset> > assets)
216 {
217         BOOST_FOREACH (shared_ptr<Asset> i, assets) {
218                 shared_ptr<FontAsset> font = dynamic_pointer_cast<FontAsset> (i);
219                 if (!font) {
220                         continue;
221                 }
222
223                 BOOST_FOREACH (shared_ptr<InteropLoadFontNode> j, _load_font_nodes) {
224                         bool got = false;
225                         BOOST_FOREACH (Font const & k, _fonts) {
226                                 if (k.load_id == j->id) {
227                                         got = true;
228                                         break;
229                                 }
230                         }
231
232                         if (!got && font->file() && j->uri == font->file()->leaf().string()) {
233                                 _fonts.push_back (Font (j->id, i->id(), font->file().get()));
234                         }
235                 }
236         }
237 }
238
239 void
240 InteropSubtitleAsset::add_font_assets (list<shared_ptr<Asset> >& assets)
241 {
242         BOOST_FOREACH (Font const & i, _fonts) {
243                 DCP_ASSERT (i.file);
244                 assets.push_back (shared_ptr<FontAsset> (new FontAsset (i.uuid, i.file.get ())));
245         }
246 }