Small tidy-ups and comments.
[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 <libxml++/libxml++.h>
27 #include <boost/foreach.hpp>
28 #include <cmath>
29 #include <cstdio>
30
31 using std::list;
32 using std::string;
33 using std::cout;
34 using boost::shared_ptr;
35 using boost::optional;
36 using boost::dynamic_pointer_cast;
37 using namespace dcp;
38
39 InteropSubtitleAsset::InteropSubtitleAsset (boost::filesystem::path file)
40         : SubtitleAsset (file)
41 {
42         shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
43         xml->read_file (file);
44         _id = xml->string_child ("SubtitleID");
45         _reel_number = xml->string_child ("ReelNumber");
46         _language = xml->string_child ("Language");
47         _movie_title = xml->string_child ("MovieTitle");
48         _load_font_nodes = type_children<dcp::InteropLoadFontNode> (xml, "LoadFont");
49
50         list<cxml::NodePtr> f = xml->node_children ("Font");
51         list<shared_ptr<dcp::FontNode> > font_nodes;
52         BOOST_FOREACH (cxml::NodePtr& i, f) {
53                 font_nodes.push_back (shared_ptr<FontNode> (new FontNode (i, 250)));
54         }
55
56         parse_subtitles (xml, font_nodes);
57 }
58
59 InteropSubtitleAsset::InteropSubtitleAsset (string movie_title, string language)
60         : _movie_title (movie_title)
61 {
62         _language = language;
63 }
64
65 Glib::ustring
66 InteropSubtitleAsset::xml_as_string () const
67 {
68         xmlpp::Document doc;
69         xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
70         root->set_attribute ("Version", "1.0");
71
72         root->add_child("SubtitleID")->add_child_text (_id);
73         root->add_child("MovieTitle")->add_child_text (_movie_title);
74         root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
75         root->add_child("Language")->add_child_text (_language);
76
77         for (list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin(); i != _load_font_nodes.end(); ++i) {
78                 xmlpp::Element* load_font = root->add_child("LoadFont");
79                 load_font->set_attribute ("Id", (*i)->id);
80                 load_font->set_attribute ("URI", (*i)->uri);
81         }
82
83         subtitles_as_xml (root, 250, "");
84
85         return doc.write_to_string_formatted ("UTF-8");
86 }
87
88 void
89 InteropSubtitleAsset::add_font (string id, string uri)
90 {
91         _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode> (new InteropLoadFontNode (id, uri)));
92 }
93
94 bool
95 InteropSubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
96 {
97         if (!SubtitleAsset::equals (other_asset, options, note)) {
98                 return false;
99         }
100         
101         shared_ptr<const InteropSubtitleAsset> other = dynamic_pointer_cast<const InteropSubtitleAsset> (other_asset);
102         if (!other) {
103                 return false;
104         }
105
106         list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin ();
107         list<shared_ptr<InteropLoadFontNode> >::const_iterator j = other->_load_font_nodes.begin ();
108
109         while (i != _load_font_nodes.end ()) {
110                 if (j == other->_load_font_nodes.end ()) {
111                         note (DCP_ERROR, "<LoadFont> nodes differ");
112                         return false;
113                 }
114
115                 if (**i != **j) {
116                         note (DCP_ERROR, "<LoadFont> nodes differ");
117                         return false;
118                 }
119
120                 ++i;
121                 ++j;
122         }
123
124         if (_movie_title != other->_movie_title) {
125                 note (DCP_ERROR, "Subtitle movie titles differ");
126                 return false;
127         }
128
129         return true;
130 }
131
132 list<shared_ptr<LoadFontNode> >
133 InteropSubtitleAsset::load_font_nodes () const
134 {
135         list<shared_ptr<LoadFontNode> > lf;
136         copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
137         return lf;
138 }
139
140 /** Write this content to an XML file */
141 void
142 InteropSubtitleAsset::write (boost::filesystem::path p) const
143 {
144         FILE* f = fopen_boost (p, "w");
145         if (!f) {
146                 throw FileError ("Could not open file for writing", p, -1);
147         }
148         
149         Glib::ustring const s = xml_as_string ();
150         fwrite (s.c_str(), 1, s.bytes(), f);
151         fclose (f);
152
153         _file = p;
154 }