2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
34 #include "interop_subtitle_asset.h"
35 #include "interop_load_font_node.h"
37 #include "raw_convert.h"
38 #include "font_node.h"
40 #include "font_asset.h"
41 #include "dcp_assert.h"
42 #include <libxml++/libxml++.h>
43 #include <boost/foreach.hpp>
52 using boost::shared_ptr;
53 using boost::shared_array;
54 using boost::optional;
55 using boost::dynamic_pointer_cast;
58 InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
59 : SubtitleAsset (file)
61 shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
62 xml->read_file (file);
63 _id = xml->string_child ("SubtitleID");
64 _reel_number = xml->string_child ("ReelNumber");
65 _language = xml->string_child ("Language");
66 _movie_title = xml->string_child ("MovieTitle");
67 _load_font_nodes = type_children<dcp::InteropLoadFontNode> (xml, "LoadFont");
69 list<shared_ptr<dcp::FontNode> > font_nodes;
70 BOOST_FOREACH (cxml::NodePtr const & i, xml->node_children ("Font")) {
71 font_nodes.push_back (shared_ptr<FontNode> (new FontNode (i, 250, "Id")));
74 list<shared_ptr<dcp::SubtitleNode> > subtitle_nodes;
75 BOOST_FOREACH (cxml::NodePtr const & i, xml->node_children ("Subtitle")) {
76 subtitle_nodes.push_back (shared_ptr<SubtitleNode> (new SubtitleNode (i, 250, "Id")));
79 parse_subtitles (xml, font_nodes, subtitle_nodes);
82 InteropSubtitleAsset::InteropSubtitleAsset ()
88 InteropSubtitleAsset::xml_as_string () const
91 xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
92 root->set_attribute ("Version", "1.0");
94 root->add_child("SubtitleID")->add_child_text (_id);
95 root->add_child("MovieTitle")->add_child_text (_movie_title);
96 root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
97 root->add_child("Language")->add_child_text (_language);
99 for (list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin(); i != _load_font_nodes.end(); ++i) {
100 xmlpp::Element* load_font = root->add_child("LoadFont");
101 load_font->set_attribute ("Id", (*i)->id);
102 load_font->set_attribute ("URI", (*i)->uri);
105 subtitles_as_xml (root, 250, INTEROP);
107 return doc.write_to_string_formatted ("UTF-8");
111 InteropSubtitleAsset::add_font (string load_id, boost::filesystem::path file)
113 _fonts.push_back (Font (load_id, make_uuid(), file));
114 _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode> (new InteropLoadFontNode (load_id, file.leaf().string ())));
118 InteropSubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
120 if (!SubtitleAsset::equals (other_asset, options, note)) {
124 shared_ptr<const InteropSubtitleAsset> other = dynamic_pointer_cast<const InteropSubtitleAsset> (other_asset);
129 list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin ();
130 list<shared_ptr<InteropLoadFontNode> >::const_iterator j = other->_load_font_nodes.begin ();
132 while (i != _load_font_nodes.end ()) {
133 if (j == other->_load_font_nodes.end ()) {
134 note (DCP_ERROR, "<LoadFont> nodes differ");
139 note (DCP_ERROR, "<LoadFont> nodes differ");
147 if (_movie_title != other->_movie_title) {
148 note (DCP_ERROR, "Subtitle movie titles differ");
155 list<shared_ptr<LoadFontNode> >
156 InteropSubtitleAsset::load_font_nodes () const
158 list<shared_ptr<LoadFontNode> > lf;
159 copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
163 /** Write this content to an XML file with its fonts alongside */
165 InteropSubtitleAsset::write (boost::filesystem::path p) const
167 FILE* f = fopen_boost (p, "w");
169 throw FileError ("Could not open file for writing", p, -1);
172 string const s = xml_as_string ();
173 /* length() here gives bytes not characters */
174 fwrite (s.c_str(), 1, s.length(), f);
179 BOOST_FOREACH (shared_ptr<InteropLoadFontNode> i, _load_font_nodes) {
180 boost::filesystem::path file = p.parent_path() / i->uri;
181 FILE* f = fopen_boost (file, "wb");
183 throw FileError ("could not open font file for writing", file, errno);
185 list<Font>::const_iterator j = _fonts.begin ();
186 while (j != _fonts.end() && j->load_id != i->id) {
189 if (j != _fonts.end ()) {
190 fwrite (j->data.data().get(), 1, j->data.size(), f);
198 InteropSubtitleAsset::resolve_fonts (list<shared_ptr<Asset> > assets)
200 BOOST_FOREACH (shared_ptr<Asset> i, assets) {
201 shared_ptr<FontAsset> font = dynamic_pointer_cast<FontAsset> (i);
206 BOOST_FOREACH (shared_ptr<InteropLoadFontNode> j, _load_font_nodes) {
207 if (j->uri == font->file().leaf().string ()) {
208 _fonts.push_back (Font (j->id, i->id(), font->file ()));
215 InteropSubtitleAsset::add_font_assets (list<shared_ptr<Asset> >& assets)
217 BOOST_FOREACH (Font const & i, _fonts) {
219 assets.push_back (shared_ptr<FontAsset> (new FontAsset (i.uuid, i.file.get ())));