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