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