Merge.
[libdcp.git] / src / 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 "raw_convert.h"
21 #include "subtitle_asset.h"
22 #include "util.h"
23 #include "xml.h"
24 #include "font_node.h"
25 #include "text_node.h"
26 #include "subtitle_string.h"
27 #include "dcp_assert.h"
28 #include "AS_DCP.h"
29 #include "KM_util.h"
30 #include <libxml++/nodes/element.h>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/shared_array.hpp>
33 #include <boost/foreach.hpp>
34 #include <fstream>
35
36 using std::string;
37 using std::list;
38 using std::ostream;
39 using std::ofstream;
40 using std::stringstream;
41 using std::cout;
42 using std::cerr;
43 using std::map;
44 using boost::shared_ptr;
45 using boost::shared_array;
46 using boost::optional;
47 using boost::dynamic_pointer_cast;
48 using namespace dcp;
49
50 SubtitleAsset::SubtitleAsset ()
51 {
52
53 }
54
55 SubtitleAsset::SubtitleAsset (boost::filesystem::path file)
56         : Asset (file)
57 {
58
59 }
60
61 void
62 SubtitleAsset::parse_subtitles (shared_ptr<cxml::Document> xml, list<shared_ptr<dcp::FontNode> > font_nodes)
63 {
64         /* Make Subtitle objects to represent the raw XML nodes in a sane way */
65         ParseState parse_state;
66         examine_font_nodes (xml, font_nodes, parse_state);
67 }
68
69 void
70 SubtitleAsset::examine_font_nodes (
71         shared_ptr<const cxml::Node> xml,
72         list<shared_ptr<dcp::FontNode> > const & font_nodes,
73         ParseState& parse_state
74         )
75 {
76         for (list<shared_ptr<dcp::FontNode> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
77
78                 parse_state.font_nodes.push_back (*i);
79                 maybe_add_subtitle ((*i)->text, parse_state);
80
81                 for (list<shared_ptr<dcp::SubtitleNode> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
82                         parse_state.subtitle_nodes.push_back (*j);
83                         examine_text_nodes (xml, (*j)->text_nodes, parse_state);
84                         examine_font_nodes (xml, (*j)->font_nodes, parse_state);
85                         parse_state.subtitle_nodes.pop_back ();
86                 }
87
88                 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
89                 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
90
91                 parse_state.font_nodes.pop_back ();
92         }
93 }
94
95 void
96 SubtitleAsset::examine_text_nodes (
97         shared_ptr<const cxml::Node> xml,
98         list<shared_ptr<dcp::TextNode> > const & text_nodes,
99         ParseState& parse_state
100         )
101 {
102         for (list<shared_ptr<dcp::TextNode> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
103                 parse_state.text_nodes.push_back (*i);
104                 maybe_add_subtitle ((*i)->text, parse_state);
105                 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
106                 parse_state.text_nodes.pop_back ();
107         }
108 }
109
110 void
111 SubtitleAsset::maybe_add_subtitle (string text, ParseState const & parse_state)
112 {
113         if (empty_or_white_space (text)) {
114                 return;
115         }
116
117         if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
118                 return;
119         }
120
121         DCP_ASSERT (!parse_state.text_nodes.empty ());
122         DCP_ASSERT (!parse_state.subtitle_nodes.empty ());
123
124         dcp::FontNode effective_font (parse_state.font_nodes);
125         dcp::TextNode effective_text (*parse_state.text_nodes.back ());
126         dcp::SubtitleNode effective_subtitle (*parse_state.subtitle_nodes.back ());
127
128         _subtitles.push_back (
129                 SubtitleString (
130                         effective_font.id,
131                         effective_font.italic.get_value_or (false),
132                         effective_font.colour.get_value_or (dcp::Colour (255, 255, 255)),
133                         effective_font.size,
134                         effective_font.aspect_adjust.get_value_or (1.0),
135                         effective_subtitle.in,
136                         effective_subtitle.out,
137                         effective_text.h_position,
138                         effective_text.h_align,
139                         effective_text.v_position,
140                         effective_text.v_align,
141                         text,
142                         effective_font.effect.get_value_or (NONE),
143                         effective_font.effect_colour.get_value_or (dcp::Colour (0, 0, 0)),
144                         effective_subtitle.fade_up_time,
145                         effective_subtitle.fade_down_time
146                         )
147                 );
148 }
149
150 list<SubtitleString>
151 SubtitleAsset::subtitles_during (Time from, Time to) const
152 {
153         list<SubtitleString> s;
154         for (list<SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
155                 if (i->out() >= from && i->in() <= to) {
156                         s.push_back (*i);
157                 }
158         }
159
160         return s;
161 }
162
163 void
164 SubtitleAsset::add (SubtitleString s)
165 {
166         _subtitles.push_back (s);
167 }
168
169 Time
170 SubtitleAsset::latest_subtitle_out () const
171 {
172         Time t;
173         for (list<SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
174                 if (i->out() > t) {
175                         t = i->out ();
176                 }
177         }
178
179         return t;
180 }
181
182 bool
183 SubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
184 {
185         if (!Asset::equals (other_asset, options, note)) {
186                 return false;
187         }
188
189         shared_ptr<const SubtitleAsset> other = dynamic_pointer_cast<const SubtitleAsset> (other_asset);
190         if (!other) {
191                 return false;
192         }
193
194         if (_subtitles != other->_subtitles) {
195                 note (DCP_ERROR, "subtitles differ");
196                 return false;
197         }
198
199         return true;
200 }
201
202 struct SubtitleSorter {
203         bool operator() (SubtitleString const & a, SubtitleString const & b) {
204                 if (a.in() != b.in()) {
205                         return a.in() < b.in();
206                 }
207                 return a.v_position() < b.v_position();
208         }
209 };
210
211 void
212 SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, string xmlns) const
213 {
214         list<SubtitleString> sorted = _subtitles;
215         sorted.sort (SubtitleSorter ());
216
217         /* XXX: script, underlined, weight not supported */
218
219         optional<string> font;
220         bool italic = false;
221         Colour colour;
222         int size = 0;
223         float aspect_adjust = 1.0;
224         Effect effect = NONE;
225         Colour effect_colour;
226         int spot_number = 1;
227         Time last_in;
228         Time last_out;
229         Time last_fade_up_time;
230         Time last_fade_down_time;
231
232         xmlpp::Element* font_element = 0;
233         xmlpp::Element* subtitle_element = 0;
234
235         for (list<SubtitleString>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
236
237                 /* We will start a new <Font>...</Font> whenever some font property changes.
238                    I suppose we should really make an optimal hierarchy of <Font> tags, but
239                    that seems hard.
240                 */
241
242                 bool const font_changed =
243                         font          != i->font()          ||
244                         italic        != i->italic()        ||
245                         colour        != i->colour()        ||
246                         size          != i->size()          ||
247                         fabs (aspect_adjust - i->aspect_adjust()) > ASPECT_ADJUST_EPSILON ||
248                         effect        != i->effect()        ||
249                         effect_colour != i->effect_colour();
250
251                 if (font_changed) {
252                         font = i->font ();
253                         italic = i->italic ();
254                         colour = i->colour ();
255                         size = i->size ();
256                         aspect_adjust = i->aspect_adjust ();
257                         effect = i->effect ();
258                         effect_colour = i->effect_colour ();
259                 }
260
261                 if (!font_element || font_changed) {
262                         font_element = root->add_child ("Font", xmlns);
263                         if (font) {
264                                 font_element->set_attribute ("Id", font.get ());
265                         }
266                         font_element->set_attribute ("Italic", italic ? "yes" : "no");
267                         font_element->set_attribute ("Color", colour.to_argb_string());
268                         font_element->set_attribute ("Size", raw_convert<string> (size));
269                         if (fabs (aspect_adjust - 1.0) > ASPECT_ADJUST_EPSILON) {
270                                 font_element->set_attribute ("AspectAdjust", raw_convert<string> (aspect_adjust));
271                         }
272                         font_element->set_attribute ("Effect", effect_to_string (effect));
273                         font_element->set_attribute ("EffectColor", effect_colour.to_argb_string());
274                         font_element->set_attribute ("Script", "normal");
275                         font_element->set_attribute ("Underlined", "no");
276                         font_element->set_attribute ("Weight", "normal");
277                 }
278
279                 if (!subtitle_element || font_changed ||
280                     (last_in != i->in() ||
281                      last_out != i->out() ||
282                      last_fade_up_time != i->fade_up_time() ||
283                      last_fade_down_time != i->fade_down_time()
284                             )) {
285
286                         subtitle_element = font_element->add_child ("Subtitle", xmlns);
287                         subtitle_element->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
288                         subtitle_element->set_attribute ("TimeIn", i->in().rebase(time_code_rate).as_string());
289                         subtitle_element->set_attribute ("TimeOut", i->out().rebase(time_code_rate).as_string());
290                         subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i->fade_up_time().as_editable_units(time_code_rate)));
291                         subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i->fade_down_time().as_editable_units(time_code_rate)));
292
293                         last_in = i->in ();
294                         last_out = i->out ();
295                         last_fade_up_time = i->fade_up_time ();
296                         last_fade_down_time = i->fade_down_time ();
297                 }
298
299                 xmlpp::Element* text = subtitle_element->add_child ("Text", xmlns);
300                 if (i->h_align() != HALIGN_CENTER) {
301                         text->set_attribute ("HAlign", halign_to_string (i->h_align ()));
302                 }
303                 if (i->h_position() > ALIGN_EPSILON) {
304                         text->set_attribute ("HPosition", raw_convert<string> (i->h_position() * 100, 6));
305                 }
306                 text->set_attribute ("VAlign", valign_to_string (i->v_align()));
307                 text->set_attribute ("VPosition", raw_convert<string> (i->v_position() * 100, 6));
308                 text->add_child_text (i->text());
309         }
310 }
311
312 map<string, Data>
313 SubtitleAsset::fonts_with_load_ids () const
314 {
315         map<string, Data> out;
316         BOOST_FOREACH (Font const & i, _fonts) {
317                 out[i.load_id] = i.data;
318         }
319         return out;
320 }