Still more licence fixups.
[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
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<shared_ptr<dcp::FontNode> > font_nodes;
56         BOOST_FOREACH (cxml::NodePtr const & i, xml->node_children ("Font")) {
57                 font_nodes.push_back (shared_ptr<FontNode> (new FontNode (i, 250, "Id")));
58         }
59
60         list<shared_ptr<dcp::SubtitleNode> > subtitle_nodes;
61         BOOST_FOREACH (cxml::NodePtr const & i, xml->node_children ("Subtitle")) {
62                 subtitle_nodes.push_back (shared_ptr<SubtitleNode> (new SubtitleNode (i, 250, "Id")));
63         }
64
65         parse_subtitles (xml, font_nodes, subtitle_nodes);
66 }
67
68 InteropSubtitleAsset::InteropSubtitleAsset ()
69 {
70
71 }
72
73 string
74 InteropSubtitleAsset::xml_as_string () const
75 {
76         xmlpp::Document doc;
77         xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
78         root->set_attribute ("Version", "1.0");
79
80         root->add_child("SubtitleID")->add_child_text (_id);
81         root->add_child("MovieTitle")->add_child_text (_movie_title);
82         root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
83         root->add_child("Language")->add_child_text (_language);
84
85         for (list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin(); i != _load_font_nodes.end(); ++i) {
86                 xmlpp::Element* load_font = root->add_child("LoadFont");
87                 load_font->set_attribute ("Id", (*i)->id);
88                 load_font->set_attribute ("URI", (*i)->uri);
89         }
90
91         subtitles_as_xml (root, 250, INTEROP);
92
93         return doc.write_to_string_formatted ("UTF-8");
94 }
95
96 void
97 InteropSubtitleAsset::add_font (string load_id, boost::filesystem::path file)
98 {
99         _fonts.push_back (Font (load_id, make_uuid(), file));
100         _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode> (new InteropLoadFontNode (load_id, file.leaf().string ())));
101 }
102
103 bool
104 InteropSubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
105 {
106         if (!SubtitleAsset::equals (other_asset, options, note)) {
107                 return false;
108         }
109
110         shared_ptr<const InteropSubtitleAsset> other = dynamic_pointer_cast<const InteropSubtitleAsset> (other_asset);
111         if (!other) {
112                 return false;
113         }
114
115         list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin ();
116         list<shared_ptr<InteropLoadFontNode> >::const_iterator j = other->_load_font_nodes.begin ();
117
118         while (i != _load_font_nodes.end ()) {
119                 if (j == other->_load_font_nodes.end ()) {
120                         note (DCP_ERROR, "<LoadFont> nodes differ");
121                         return false;
122                 }
123
124                 if (**i != **j) {
125                         note (DCP_ERROR, "<LoadFont> nodes differ");
126                         return false;
127                 }
128
129                 ++i;
130                 ++j;
131         }
132
133         if (_movie_title != other->_movie_title) {
134                 note (DCP_ERROR, "Subtitle movie titles differ");
135                 return false;
136         }
137
138         return true;
139 }
140
141 list<shared_ptr<LoadFontNode> >
142 InteropSubtitleAsset::load_font_nodes () const
143 {
144         list<shared_ptr<LoadFontNode> > lf;
145         copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
146         return lf;
147 }
148
149 /** Write this content to an XML file with its fonts alongside */
150 void
151 InteropSubtitleAsset::write (boost::filesystem::path p) const
152 {
153         FILE* f = fopen_boost (p, "w");
154         if (!f) {
155                 throw FileError ("Could not open file for writing", p, -1);
156         }
157
158         string const s = xml_as_string ();
159         /* length() here gives bytes not characters */
160         fwrite (s.c_str(), 1, s.length(), f);
161         fclose (f);
162
163         _file = p;
164
165         BOOST_FOREACH (shared_ptr<InteropLoadFontNode> i, _load_font_nodes) {
166                 boost::filesystem::path file = p.parent_path() / i->uri;
167                 FILE* f = fopen_boost (file, "wb");
168                 if (!f) {
169                         throw FileError ("could not open font file for writing", file, errno);
170                 }
171                 list<Font>::const_iterator j = _fonts.begin ();
172                 while (j != _fonts.end() && j->load_id != i->id) {
173                         ++j;
174                 }
175                 if (j != _fonts.end ()) {
176                         fwrite (j->data.data().get(), 1, j->data.size(), f);
177                         j->file = file;
178                 }
179                 fclose (f);
180         }
181 }
182
183 void
184 InteropSubtitleAsset::resolve_fonts (list<shared_ptr<Asset> > assets)
185 {
186         BOOST_FOREACH (shared_ptr<Asset> i, assets) {
187                 shared_ptr<FontAsset> font = dynamic_pointer_cast<FontAsset> (i);
188                 if (!font) {
189                         continue;
190                 }
191
192                 BOOST_FOREACH (shared_ptr<InteropLoadFontNode> j, _load_font_nodes) {
193                         if (j->uri == font->file().leaf().string ()) {
194                                 _fonts.push_back (Font (j->id, i->id(), font->file ()));
195                         }
196                 }
197         }
198 }
199
200 void
201 InteropSubtitleAsset::add_font_assets (list<shared_ptr<Asset> >& assets)
202 {
203         BOOST_FOREACH (Font const & i, _fonts) {
204                 DCP_ASSERT (i.file);
205                 assets.push_back (shared_ptr<FontAsset> (new FontAsset (i.uuid, i.file.get ())));
206         }
207 }