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