Don't use write_to_string_formatted with subs as it introduces lots of extra spaces...
[libdcp.git] / src / interop_subtitle_asset.cc
1 /*
2     Copyright (C) 2012-2015 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 "xml.h"
37 #include "raw_convert.h"
38 #include "util.h"
39 #include "font_asset.h"
40 #include "dcp_assert.h"
41 #include <libxml++/libxml++.h>
42 #include <boost/foreach.hpp>
43 #include <cmath>
44 #include <cstdio>
45
46 using std::list;
47 using std::string;
48 using std::cout;
49 using std::cerr;
50 using std::map;
51 using boost::shared_ptr;
52 using boost::shared_array;
53 using boost::optional;
54 using boost::dynamic_pointer_cast;
55 using namespace dcp;
56
57 InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
58         : SubtitleAsset (file)
59 {
60         shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
61         xml->read_file (file);
62         _id = xml->string_child ("SubtitleID");
63         _reel_number = xml->string_child ("ReelNumber");
64         _language = xml->string_child ("Language");
65         _movie_title = xml->string_child ("MovieTitle");
66         _load_font_nodes = type_children<dcp::InteropLoadFontNode> (xml, "LoadFont");
67
68         /* Now we need to drop down to xmlpp */
69
70         list<ParseState> ps;
71         xmlpp::Node::NodeList c = xml->node()->get_children ();
72         for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
73                 xmlpp::Element const * e = dynamic_cast<xmlpp::Element const *> (*i);
74                 if (e && (e->get_name() == "Font" || e->get_name() == "Subtitle")) {
75                         parse_subtitles (e, ps, optional<int>(), INTEROP);
76                 }
77         }
78 }
79
80 InteropSubtitleAsset::InteropSubtitleAsset ()
81 {
82
83 }
84
85 string
86 InteropSubtitleAsset::xml_as_string () const
87 {
88         xmlpp::Document doc;
89         xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
90         root->set_attribute ("Version", "1.0");
91
92         root->add_child("SubtitleID")->add_child_text (_id);
93         root->add_child("MovieTitle")->add_child_text (_movie_title);
94         root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
95         root->add_child("Language")->add_child_text (_language);
96
97         for (list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin(); i != _load_font_nodes.end(); ++i) {
98                 xmlpp::Element* load_font = root->add_child("LoadFont");
99                 load_font->set_attribute ("Id", (*i)->id);
100                 load_font->set_attribute ("URI", (*i)->uri);
101         }
102
103         subtitles_as_xml (root, 250, INTEROP);
104
105         return doc.write_to_string ("UTF-8");
106 }
107
108 void
109 InteropSubtitleAsset::add_font (string load_id, boost::filesystem::path file)
110 {
111         _fonts.push_back (Font (load_id, make_uuid(), file));
112         _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode> (new InteropLoadFontNode (load_id, file.leaf().string ())));
113 }
114
115 bool
116 InteropSubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
117 {
118         if (!SubtitleAsset::equals (other_asset, options, note)) {
119                 return false;
120         }
121
122         shared_ptr<const InteropSubtitleAsset> other = dynamic_pointer_cast<const InteropSubtitleAsset> (other_asset);
123         if (!other) {
124                 return false;
125         }
126
127         list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin ();
128         list<shared_ptr<InteropLoadFontNode> >::const_iterator j = other->_load_font_nodes.begin ();
129
130         while (i != _load_font_nodes.end ()) {
131                 if (j == other->_load_font_nodes.end ()) {
132                         note (DCP_ERROR, "<LoadFont> nodes differ");
133                         return false;
134                 }
135
136                 if (**i != **j) {
137                         note (DCP_ERROR, "<LoadFont> nodes differ");
138                         return false;
139                 }
140
141                 ++i;
142                 ++j;
143         }
144
145         if (_movie_title != other->_movie_title) {
146                 note (DCP_ERROR, "Subtitle movie titles differ");
147                 return false;
148         }
149
150         return true;
151 }
152
153 list<shared_ptr<LoadFontNode> >
154 InteropSubtitleAsset::load_font_nodes () const
155 {
156         list<shared_ptr<LoadFontNode> > lf;
157         copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
158         return lf;
159 }
160
161 /** Write this content to an XML file with its fonts alongside */
162 void
163 InteropSubtitleAsset::write (boost::filesystem::path p) const
164 {
165         FILE* f = fopen_boost (p, "w");
166         if (!f) {
167                 throw FileError ("Could not open file for writing", p, -1);
168         }
169
170         string const s = xml_as_string ();
171         /* length() here gives bytes not characters */
172         fwrite (s.c_str(), 1, s.length(), f);
173         fclose (f);
174
175         _file = p;
176
177         BOOST_FOREACH (shared_ptr<InteropLoadFontNode> i, _load_font_nodes) {
178                 boost::filesystem::path file = p.parent_path() / i->uri;
179                 FILE* f = fopen_boost (file, "wb");
180                 if (!f) {
181                         throw FileError ("could not open font file for writing", file, errno);
182                 }
183                 list<Font>::const_iterator j = _fonts.begin ();
184                 while (j != _fonts.end() && j->load_id != i->id) {
185                         ++j;
186                 }
187                 if (j != _fonts.end ()) {
188                         fwrite (j->data.data().get(), 1, j->data.size(), f);
189                         j->file = file;
190                 }
191                 fclose (f);
192         }
193 }
194
195 void
196 InteropSubtitleAsset::resolve_fonts (list<shared_ptr<Asset> > assets)
197 {
198         BOOST_FOREACH (shared_ptr<Asset> i, assets) {
199                 shared_ptr<FontAsset> font = dynamic_pointer_cast<FontAsset> (i);
200                 if (!font) {
201                         continue;
202                 }
203
204                 BOOST_FOREACH (shared_ptr<InteropLoadFontNode> j, _load_font_nodes) {
205                         if (font->file() && j->uri == font->file()->leaf().string ()) {
206                                 _fonts.push_back (Font (j->id, i->id(), font->file().get()));
207                         }
208                 }
209         }
210 }
211
212 void
213 InteropSubtitleAsset::add_font_assets (list<shared_ptr<Asset> >& assets)
214 {
215         BOOST_FOREACH (Font const & i, _fonts) {
216                 DCP_ASSERT (i.file);
217                 assets.push_back (shared_ptr<FontAsset> (new FontAsset (i.uuid, i.file.get ())));
218         }
219 }