Extract examine_subtitle_nodes into its own method.
[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_subtitle_nodes (
71         shared_ptr<const cxml::Node> xml,
72         list<shared_ptr<dcp::SubtitleNode> > const & subtitle_nodes,
73         ParseState& parse_state
74         )
75 {
76         for (list<shared_ptr<dcp::SubtitleNode> >::const_iterator j = subtitle_nodes.begin(); j != subtitle_nodes.end(); ++j) {
77                 parse_state.subtitle_nodes.push_back (*j);
78                 examine_text_nodes (xml, (*j)->text_nodes, parse_state);
79                 examine_font_nodes (xml, (*j)->font_nodes, parse_state);
80                 parse_state.subtitle_nodes.pop_back ();
81         }
82 }
83
84 void
85 SubtitleAsset::examine_font_nodes (
86         shared_ptr<const cxml::Node> xml,
87         list<shared_ptr<dcp::FontNode> > const & font_nodes,
88         ParseState& parse_state
89         )
90 {
91         for (list<shared_ptr<dcp::FontNode> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
92
93                 parse_state.font_nodes.push_back (*i);
94                 maybe_add_subtitle ((*i)->text, parse_state);
95
96                 examine_subtitle_nodes (xml, (*i)->subtitle_nodes, parse_state);
97
98                 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
99                 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
100
101                 parse_state.font_nodes.pop_back ();
102         }
103 }
104
105 void
106 SubtitleAsset::examine_text_nodes (
107         shared_ptr<const cxml::Node> xml,
108         list<shared_ptr<dcp::TextNode> > const & text_nodes,
109         ParseState& parse_state
110         )
111 {
112         for (list<shared_ptr<dcp::TextNode> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
113                 parse_state.text_nodes.push_back (*i);
114                 maybe_add_subtitle ((*i)->text, parse_state);
115                 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
116                 parse_state.text_nodes.pop_back ();
117         }
118 }
119
120 void
121 SubtitleAsset::maybe_add_subtitle (string text, ParseState const & parse_state)
122 {
123         if (empty_or_white_space (text)) {
124                 return;
125         }
126
127         if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
128                 return;
129         }
130
131         DCP_ASSERT (!parse_state.text_nodes.empty ());
132         DCP_ASSERT (!parse_state.subtitle_nodes.empty ());
133
134         dcp::FontNode effective_font (parse_state.font_nodes);
135         dcp::TextNode effective_text (*parse_state.text_nodes.back ());
136         dcp::SubtitleNode effective_subtitle (*parse_state.subtitle_nodes.back ());
137
138         _subtitles.push_back (
139                 SubtitleString (
140                         effective_font.id,
141                         effective_font.italic.get_value_or (false),
142                         effective_font.colour.get_value_or (dcp::Colour (255, 255, 255)),
143                         effective_font.size,
144                         effective_font.aspect_adjust.get_value_or (1.0),
145                         effective_subtitle.in,
146                         effective_subtitle.out,
147                         effective_text.h_position,
148                         effective_text.h_align,
149                         effective_text.v_position,
150                         effective_text.v_align,
151                         text,
152                         effective_font.effect.get_value_or (NONE),
153                         effective_font.effect_colour.get_value_or (dcp::Colour (0, 0, 0)),
154                         effective_subtitle.fade_up_time,
155                         effective_subtitle.fade_down_time
156                         )
157                 );
158 }
159
160 list<SubtitleString>
161 SubtitleAsset::subtitles_during (Time from, Time to, bool starting) const
162 {
163         list<SubtitleString> s;
164         for (list<SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
165                 if ((starting && from <= i->in() && i->in() < to) || (!starting && i->out() >= from && i->in() <= to)) {
166                         s.push_back (*i);
167                 }
168         }
169
170         return s;
171 }
172
173 void
174 SubtitleAsset::add (SubtitleString s)
175 {
176         _subtitles.push_back (s);
177 }
178
179 Time
180 SubtitleAsset::latest_subtitle_out () const
181 {
182         Time t;
183         for (list<SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
184                 if (i->out() > t) {
185                         t = i->out ();
186                 }
187         }
188
189         return t;
190 }
191
192 bool
193 SubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
194 {
195         if (!Asset::equals (other_asset, options, note)) {
196                 return false;
197         }
198
199         shared_ptr<const SubtitleAsset> other = dynamic_pointer_cast<const SubtitleAsset> (other_asset);
200         if (!other) {
201                 return false;
202         }
203
204         if (_subtitles != other->_subtitles) {
205                 note (DCP_ERROR, "subtitles differ");
206                 return false;
207         }
208
209         return true;
210 }
211
212 struct SubtitleSorter {
213         bool operator() (SubtitleString const & a, SubtitleString const & b) {
214                 if (a.in() != b.in()) {
215                         return a.in() < b.in();
216                 }
217                 return a.v_position() < b.v_position();
218         }
219 };
220
221 /** @param standard Standard (INTEROP or SMPTE); this is used rather than putting things in the child
222  *  class because the differences between the two are fairly subtle.
223  */
224 void
225 SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const
226 {
227         list<SubtitleString> sorted = _subtitles;
228         sorted.sort (SubtitleSorter ());
229
230         string const xmlns = standard == SMPTE ? "dcst" : "";
231
232         /* XXX: script, underlined, weight not supported */
233
234         optional<string> font;
235         bool italic = false;
236         Colour colour;
237         int size = 0;
238         float aspect_adjust = 1.0;
239         Effect effect = NONE;
240         Colour effect_colour;
241         int spot_number = 1;
242         Time last_in;
243         Time last_out;
244         Time last_fade_up_time;
245         Time last_fade_down_time;
246
247         xmlpp::Element* font_element = 0;
248         xmlpp::Element* subtitle_element = 0;
249
250         for (list<SubtitleString>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
251
252                 /* We will start a new <Font>...</Font> whenever some font property changes.
253                    I suppose we should really make an optimal hierarchy of <Font> tags, but
254                    that seems hard.
255                 */
256
257                 bool const font_changed =
258                         font          != i->font()          ||
259                         italic        != i->italic()        ||
260                         colour        != i->colour()        ||
261                         size          != i->size()          ||
262                         fabs (aspect_adjust - i->aspect_adjust()) > ASPECT_ADJUST_EPSILON ||
263                         effect        != i->effect()        ||
264                         effect_colour != i->effect_colour();
265
266                 if (font_changed) {
267                         font = i->font ();
268                         italic = i->italic ();
269                         colour = i->colour ();
270                         size = i->size ();
271                         aspect_adjust = i->aspect_adjust ();
272                         effect = i->effect ();
273                         effect_colour = i->effect_colour ();
274                 }
275
276                 if (!font_element || font_changed) {
277                         font_element = root->add_child ("Font", xmlns);
278                         if (font) {
279                                 if (standard == SMPTE) {
280                                         font_element->set_attribute ("ID", font.get ());
281                                 } else {
282                                         font_element->set_attribute ("Id", font.get ());
283                                 }
284                         }
285                         font_element->set_attribute ("Italic", italic ? "yes" : "no");
286                         font_element->set_attribute ("Color", colour.to_argb_string());
287                         font_element->set_attribute ("Size", raw_convert<string> (size));
288                         if (fabs (aspect_adjust - 1.0) > ASPECT_ADJUST_EPSILON) {
289                                 font_element->set_attribute ("AspectAdjust", raw_convert<string> (aspect_adjust));
290                         }
291                         font_element->set_attribute ("Effect", effect_to_string (effect));
292                         font_element->set_attribute ("EffectColor", effect_colour.to_argb_string());
293                         font_element->set_attribute ("Script", "normal");
294                         if (standard == SMPTE) {
295                                 font_element->set_attribute ("Underline", "no");
296                         } else {
297                                 font_element->set_attribute ("Underlined", "no");
298                         }
299                         font_element->set_attribute ("Weight", "normal");
300                 }
301
302                 if (!subtitle_element || font_changed ||
303                     (last_in != i->in() ||
304                      last_out != i->out() ||
305                      last_fade_up_time != i->fade_up_time() ||
306                      last_fade_down_time != i->fade_down_time()
307                             )) {
308
309                         subtitle_element = font_element->add_child ("Subtitle", xmlns);
310                         subtitle_element->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
311                         subtitle_element->set_attribute ("TimeIn", i->in().rebase(time_code_rate).as_string(standard));
312                         subtitle_element->set_attribute ("TimeOut", i->out().rebase(time_code_rate).as_string(standard));
313                         if (standard == SMPTE) {
314                                 subtitle_element->set_attribute ("FadeUpTime", i->fade_up_time().rebase(time_code_rate).as_string(standard));
315                                 subtitle_element->set_attribute ("FadeDownTime", i->fade_down_time().rebase(time_code_rate).as_string(standard));
316                         } else {
317                                 subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i->fade_up_time().as_editable_units(time_code_rate)));
318                                 subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i->fade_down_time().as_editable_units(time_code_rate)));
319                         }
320
321                         last_in = i->in ();
322                         last_out = i->out ();
323                         last_fade_up_time = i->fade_up_time ();
324                         last_fade_down_time = i->fade_down_time ();
325                 }
326
327                 xmlpp::Element* text = subtitle_element->add_child ("Text", xmlns);
328                 if (i->h_align() != HALIGN_CENTER) {
329                         if (standard == SMPTE) {
330                                 text->set_attribute ("Halign", halign_to_string (i->h_align ()));
331                         } else {
332                                 text->set_attribute ("HAlign", halign_to_string (i->h_align ()));
333                         }
334                 }
335                 if (i->h_position() > ALIGN_EPSILON) {
336                         if (standard == SMPTE) {
337                                 text->set_attribute ("Hposition", raw_convert<string> (i->h_position() * 100, 6));
338                         } else {
339                                 text->set_attribute ("HPosition", raw_convert<string> (i->h_position() * 100, 6));
340                         }
341                 }
342                 if (standard == SMPTE) {
343                         text->set_attribute ("Valign", valign_to_string (i->v_align()));
344                 } else {
345                         text->set_attribute ("VAlign", valign_to_string (i->v_align()));
346                 }
347                 if (i->v_position() > ALIGN_EPSILON) {
348                         if (standard == SMPTE) {
349                                 text->set_attribute ("Vposition", raw_convert<string> (i->v_position() * 100, 6));
350                         } else {
351                                 text->set_attribute ("VPosition", raw_convert<string> (i->v_position() * 100, 6));
352                         }
353                 } else {
354                         if (standard == SMPTE) {
355                                 text->set_attribute ("Vposition", "0");
356                         } else {
357                                 text->set_attribute ("VPosition", "0");
358                         }
359                 }
360                 text->add_child_text (i->text());
361         }
362 }
363
364 map<string, Data>
365 SubtitleAsset::fonts_with_load_ids () const
366 {
367         map<string, Data> out;
368         BOOST_FOREACH (Font const & i, _fonts) {
369                 out[i.load_id] = i.data;
370         }
371         return out;
372 }