Remove unnecessary Content class.
[libdcp.git] / src / interop_subtitle_content.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_content.h"
21 #include "interop_load_font_node.h"
22 #include "xml.h"
23 #include "raw_convert.h"
24 #include "font_node.h"
25 #include <libxml++/libxml++.h>
26 #include <boost/foreach.hpp>
27 #include <cmath>
28
29 using std::list;
30 using std::string;
31 using std::cout;
32 using boost::shared_ptr;
33 using boost::optional;
34 using boost::dynamic_pointer_cast;
35 using namespace dcp;
36
37 InteropSubtitleContent::InteropSubtitleContent (boost::filesystem::path file)
38         : SubtitleContent (file)
39 {
40         shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
41         xml->read_file (file);
42         _id = xml->string_child ("SubtitleID");
43
44         _movie_title = xml->string_child ("MovieTitle");
45         _load_font_nodes = type_children<dcp::InteropLoadFontNode> (xml, "LoadFont");
46
47         list<cxml::NodePtr> f = xml->node_children ("Font");
48         list<shared_ptr<dcp::FontNode> > font_nodes;
49         BOOST_FOREACH (cxml::NodePtr& i, f) {
50                 font_nodes.push_back (shared_ptr<FontNode> (new FontNode (i, 250)));
51         }
52
53         parse_common (xml, font_nodes);
54 }
55
56 InteropSubtitleContent::InteropSubtitleContent (string movie_title, string language)
57         : _movie_title (movie_title)
58 {
59         _language = language;
60 }
61
62 struct SubtitleSorter {
63         bool operator() (SubtitleString const & a, SubtitleString const & b) {
64                 if (a.in() != b.in()) {
65                         return a.in() < b.in();
66                 }
67                 return a.v_position() < b.v_position();
68         }
69 };
70
71 Glib::ustring
72 InteropSubtitleContent::xml_as_string () const
73 {
74         xmlpp::Document doc;
75         xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
76         root->set_attribute ("Version", "1.0");
77
78         root->add_child("SubtitleID")->add_child_text (_id);
79         root->add_child("MovieTitle")->add_child_text (_movie_title);
80         root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
81         root->add_child("Language")->add_child_text (_language);
82
83         for (list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin(); i != _load_font_nodes.end(); ++i) {
84                 xmlpp::Element* load_font = root->add_child("LoadFont");
85                 load_font->set_attribute ("Id", (*i)->id);
86                 load_font->set_attribute ("URI", (*i)->uri);
87         }
88
89         list<SubtitleString> sorted = _subtitles;
90         sorted.sort (SubtitleSorter ());
91
92         /* XXX: script, underlined, weight not supported */
93
94         optional<string> font;
95         bool italic = false;
96         Colour colour;
97         int size = 0;
98         float aspect_adjust = 1.0;
99         Effect effect = NONE;
100         Colour effect_colour;
101         int spot_number = 1;
102         Time last_in;
103         Time last_out;
104         Time last_fade_up_time;
105         Time last_fade_down_time;
106
107         xmlpp::Element* font_element = 0;
108         xmlpp::Element* subtitle_element = 0;
109
110         for (list<SubtitleString>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
111
112                 /* We will start a new <Font>...</Font> whenever some font property changes.
113                    I suppose we should really make an optimal hierarchy of <Font> tags, but
114                    that seems hard.
115                 */
116
117                 bool const font_changed =
118                         font          != i->font()          ||
119                         italic        != i->italic()        ||
120                         colour        != i->colour()        ||
121                         size          != i->size()          ||
122                         fabs (aspect_adjust - i->aspect_adjust()) > ASPECT_ADJUST_EPSILON ||
123                         effect        != i->effect()        ||
124                         effect_colour != i->effect_colour();
125
126                 if (font_changed) {
127                         font = i->font ();
128                         italic = i->italic ();
129                         colour = i->colour ();
130                         size = i->size ();
131                         aspect_adjust = i->aspect_adjust ();
132                         effect = i->effect ();
133                         effect_colour = i->effect_colour ();
134                 }
135
136                 if (!font_element || font_changed) {
137                         font_element = root->add_child ("Font");
138                         if (font) {
139                                 font_element->set_attribute ("Id", font.get ());
140                         }
141                         font_element->set_attribute ("Italic", italic ? "yes" : "no");
142                         font_element->set_attribute ("Color", colour.to_argb_string());
143                         font_element->set_attribute ("Size", raw_convert<string> (size));
144                         if (fabs (aspect_adjust - 1.0) > ASPECT_ADJUST_EPSILON) {
145                                 font_element->set_attribute ("AspectAdjust", raw_convert<string> (aspect_adjust));
146                         }
147                         font_element->set_attribute ("Effect", effect_to_string (effect));
148                         font_element->set_attribute ("EffectColor", effect_colour.to_argb_string());
149                         font_element->set_attribute ("Script", "normal");
150                         font_element->set_attribute ("Underlined", "no");
151                         font_element->set_attribute ("Weight", "normal");
152                 }
153
154                 if (!subtitle_element || font_changed ||
155                     (last_in != i->in() ||
156                      last_out != i->out() ||
157                      last_fade_up_time != i->fade_up_time() ||
158                      last_fade_down_time != i->fade_down_time()
159                             )) {
160
161                         subtitle_element = font_element->add_child ("Subtitle");
162                         subtitle_element->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
163                         subtitle_element->set_attribute ("TimeIn", i->in().as_string());
164                         subtitle_element->set_attribute ("TimeOut", i->out().as_string());
165                         subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i->fade_up_time().as_editable_units(250)));
166                         subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i->fade_down_time().as_editable_units(250)));
167
168                         last_in = i->in ();
169                         last_out = i->out ();
170                         last_fade_up_time = i->fade_up_time ();
171                         last_fade_down_time = i->fade_down_time ();
172                 }
173
174                 xmlpp::Element* text = subtitle_element->add_child ("Text");
175                 if (i->h_align() != HALIGN_CENTER) {
176                         text->set_attribute ("HAlign", halign_to_string (i->h_align ()));
177                 }
178                 if (i->h_position() > ALIGN_EPSILON) {
179                         text->set_attribute ("HPosition", raw_convert<string> (i->h_position() * 100, 6));
180                 }
181                 text->set_attribute ("VAlign", valign_to_string (i->v_align()));                
182                 text->set_attribute ("VPosition", raw_convert<string> (i->v_position() * 100, 6));
183                 text->add_child_text (i->text());
184         }
185
186         return doc.write_to_string_formatted ("UTF-8");
187 }
188
189 void
190 InteropSubtitleContent::add_font (string id, string uri)
191 {
192         _load_font_nodes.push_back (shared_ptr<InteropLoadFontNode> (new InteropLoadFontNode (id, uri)));
193 }
194
195 bool
196 InteropSubtitleContent::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
197 {
198         if (!SubtitleContent::equals (other_asset, options, note)) {
199                 return false;
200         }
201         
202         shared_ptr<const InteropSubtitleContent> other = dynamic_pointer_cast<const InteropSubtitleContent> (other_asset);
203         if (!other) {
204                 return false;
205         }
206
207         list<shared_ptr<InteropLoadFontNode> >::const_iterator i = _load_font_nodes.begin ();
208         list<shared_ptr<InteropLoadFontNode> >::const_iterator j = other->_load_font_nodes.begin ();
209
210         while (i != _load_font_nodes.end ()) {
211                 if (j == other->_load_font_nodes.end ()) {
212                         note (DCP_ERROR, "<LoadFont> nodes differ");
213                         return false;
214                 }
215
216                 if (**i != **j) {
217                         note (DCP_ERROR, "<LoadFont> nodes differ");
218                         return false;
219                 }
220
221                 ++i;
222                 ++j;
223         }
224
225         if (_movie_title != other->_movie_title) {
226                 note (DCP_ERROR, "Subtitle movie titles differ");
227                 return false;
228         }
229
230         return true;
231 }
232
233 list<shared_ptr<LoadFontNode> >
234 InteropSubtitleContent::load_font_nodes () const
235 {
236         list<shared_ptr<LoadFontNode> > lf;
237         copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
238         return lf;
239 }