No-op; Fix GPL address and mention libdcp by name.
[libdcp.git] / src / subtitle_asset.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "raw_convert.h"
22 #include "subtitle_asset.h"
23 #include "util.h"
24 #include "xml.h"
25 #include "font_node.h"
26 #include "text_node.h"
27 #include "subtitle_string.h"
28 #include "dcp_assert.h"
29 #include "AS_DCP.h"
30 #include "KM_util.h"
31 #include <libxml++/nodes/element.h>
32 #include <boost/algorithm/string.hpp>
33 #include <boost/shared_array.hpp>
34 #include <boost/foreach.hpp>
35 #include <fstream>
36
37 using std::string;
38 using std::list;
39 using std::ostream;
40 using std::ofstream;
41 using std::stringstream;
42 using std::cout;
43 using std::cerr;
44 using std::map;
45 using boost::shared_ptr;
46 using boost::shared_array;
47 using boost::optional;
48 using boost::dynamic_pointer_cast;
49 using namespace dcp;
50
51 SubtitleAsset::SubtitleAsset ()
52 {
53
54 }
55
56 SubtitleAsset::SubtitleAsset (boost::filesystem::path file)
57         : Asset (file)
58 {
59
60 }
61
62 void
63 SubtitleAsset::parse_subtitles (
64         shared_ptr<cxml::Document> xml,
65         list<shared_ptr<dcp::FontNode> > font_nodes,
66         list<shared_ptr<dcp::SubtitleNode> > subtitle_nodes
67         )
68 {
69         /* Make Subtitle objects to represent the raw XML nodes in a sane way */
70         ParseState parse_state;
71         examine_nodes (xml, font_nodes, parse_state);
72         examine_nodes (xml, subtitle_nodes, parse_state);
73 }
74
75 void
76 SubtitleAsset::examine_nodes (
77         shared_ptr<const cxml::Node> xml,
78         list<shared_ptr<dcp::SubtitleNode> > const & subtitle_nodes,
79         ParseState& parse_state
80         )
81 {
82         BOOST_FOREACH (shared_ptr<dcp::SubtitleNode> i, subtitle_nodes) {
83                 parse_state.subtitle_nodes.push_back (i);
84                 examine_nodes (xml, i->text_nodes, parse_state);
85                 examine_nodes (xml, i->font_nodes, parse_state);
86                 parse_state.subtitle_nodes.pop_back ();
87         }
88 }
89
90 void
91 SubtitleAsset::examine_nodes (
92         shared_ptr<const cxml::Node> xml,
93         list<shared_ptr<dcp::FontNode> > const & font_nodes,
94         ParseState& parse_state
95         )
96 {
97         BOOST_FOREACH (shared_ptr<dcp::FontNode> i, font_nodes) {
98
99                 parse_state.font_nodes.push_back (i);
100                 maybe_add_subtitle (i->text, parse_state);
101
102                 examine_nodes (xml, i->subtitle_nodes, parse_state);
103                 examine_nodes (xml, i->font_nodes, parse_state);
104                 examine_nodes (xml, i->text_nodes, parse_state);
105
106                 parse_state.font_nodes.pop_back ();
107         }
108 }
109
110 void
111 SubtitleAsset::examine_nodes (
112         shared_ptr<const cxml::Node> xml,
113         list<shared_ptr<dcp::TextNode> > const & text_nodes,
114         ParseState& parse_state
115         )
116 {
117         BOOST_FOREACH (shared_ptr<dcp::TextNode> i, text_nodes) {
118                 parse_state.text_nodes.push_back (i);
119                 maybe_add_subtitle (i->text, parse_state);
120                 examine_nodes (xml, i->font_nodes, parse_state);
121                 parse_state.text_nodes.pop_back ();
122         }
123 }
124
125 void
126 SubtitleAsset::maybe_add_subtitle (string text, ParseState const & parse_state)
127 {
128         if (empty_or_white_space (text)) {
129                 return;
130         }
131
132         if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
133                 return;
134         }
135
136         DCP_ASSERT (!parse_state.text_nodes.empty ());
137         DCP_ASSERT (!parse_state.subtitle_nodes.empty ());
138
139         dcp::FontNode effective_font (parse_state.font_nodes);
140         dcp::TextNode effective_text (*parse_state.text_nodes.back ());
141         dcp::SubtitleNode effective_subtitle (*parse_state.subtitle_nodes.back ());
142
143         _subtitles.push_back (
144                 SubtitleString (
145                         effective_font.id,
146                         effective_font.italic.get_value_or (false),
147                         effective_font.bold.get_value_or (false),
148                         effective_font.colour.get_value_or (dcp::Colour (255, 255, 255)),
149                         effective_font.size,
150                         effective_font.aspect_adjust.get_value_or (1.0),
151                         effective_subtitle.in,
152                         effective_subtitle.out,
153                         effective_text.h_position,
154                         effective_text.h_align,
155                         effective_text.v_position,
156                         effective_text.v_align,
157                         effective_text.direction,
158                         text,
159                         effective_font.effect.get_value_or (NONE),
160                         effective_font.effect_colour.get_value_or (dcp::Colour (0, 0, 0)),
161                         effective_subtitle.fade_up_time,
162                         effective_subtitle.fade_down_time
163                         )
164                 );
165 }
166
167 list<SubtitleString>
168 SubtitleAsset::subtitles_during (Time from, Time to, bool starting) const
169 {
170         list<SubtitleString> s;
171         BOOST_FOREACH (SubtitleString const & i, _subtitles) {
172                 if ((starting && from <= i.in() && i.in() < to) || (!starting && i.out() >= from && i.in() <= to)) {
173                         s.push_back (i);
174                 }
175         }
176
177         return s;
178 }
179
180 void
181 SubtitleAsset::add (SubtitleString s)
182 {
183         _subtitles.push_back (s);
184 }
185
186 Time
187 SubtitleAsset::latest_subtitle_out () const
188 {
189         Time t;
190         BOOST_FOREACH (SubtitleString const & i, _subtitles) {
191                 if (i.out() > t) {
192                         t = i.out ();
193                 }
194         }
195
196         return t;
197 }
198
199 bool
200 SubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
201 {
202         if (!Asset::equals (other_asset, options, note)) {
203                 return false;
204         }
205
206         shared_ptr<const SubtitleAsset> other = dynamic_pointer_cast<const SubtitleAsset> (other_asset);
207         if (!other) {
208                 return false;
209         }
210
211         if (_subtitles != other->_subtitles) {
212                 note (DCP_ERROR, "subtitles differ");
213                 return false;
214         }
215
216         return true;
217 }
218
219 struct SubtitleSorter {
220         bool operator() (SubtitleString const & a, SubtitleString const & b) {
221                 if (a.in() != b.in()) {
222                         return a.in() < b.in();
223                 }
224                 return a.v_position() < b.v_position();
225         }
226 };
227
228 /** @param standard Standard (INTEROP or SMPTE); this is used rather than putting things in the child
229  *  class because the differences between the two are fairly subtle.
230  */
231 void
232 SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const
233 {
234         list<SubtitleString> sorted = _subtitles;
235         sorted.sort (SubtitleSorter ());
236
237         string const xmlns = standard == SMPTE ? "dcst" : "";
238
239         /* XXX: script, underlined not supported */
240
241         optional<string> font;
242         bool italic = false;
243         bool bold = false;
244         Colour colour;
245         int size = 0;
246         float aspect_adjust = 1.0;
247         Effect effect = NONE;
248         Colour effect_colour;
249         int spot_number = 1;
250         Time last_in;
251         Time last_out;
252         Time last_fade_up_time;
253         Time last_fade_down_time;
254
255         xmlpp::Element* font_element = 0;
256         xmlpp::Element* subtitle_element = 0;
257
258         BOOST_FOREACH (SubtitleString const & i, sorted) {
259
260                 /* We will start a new <Font>...</Font> whenever some font property changes.
261                    I suppose we should really make an optimal hierarchy of <Font> tags, but
262                    that seems hard.
263                 */
264
265                 bool const font_changed =
266                         font          != i.font()          ||
267                         italic        != i.italic()        ||
268                         bold          != i.bold()          ||
269                         colour        != i.colour()        ||
270                         size          != i.size()          ||
271                         fabs (aspect_adjust - i.aspect_adjust()) > ASPECT_ADJUST_EPSILON ||
272                         effect        != i.effect()        ||
273                         effect_colour != i.effect_colour();
274
275                 if (font_changed) {
276                         font = i.font ();
277                         italic = i.italic ();
278                         bold = i.bold ();
279                         colour = i.colour ();
280                         size = i.size ();
281                         aspect_adjust = i.aspect_adjust ();
282                         effect = i.effect ();
283                         effect_colour = i.effect_colour ();
284                 }
285
286                 if (!font_element || font_changed) {
287                         font_element = root->add_child ("Font", xmlns);
288                         if (font) {
289                                 if (standard == SMPTE) {
290                                         font_element->set_attribute ("ID", font.get ());
291                                 } else {
292                                         font_element->set_attribute ("Id", font.get ());
293                                 }
294                         }
295                         font_element->set_attribute ("Italic", italic ? "yes" : "no");
296                         font_element->set_attribute ("Color", colour.to_argb_string());
297                         font_element->set_attribute ("Size", raw_convert<string> (size));
298                         if (fabs (aspect_adjust - 1.0) > ASPECT_ADJUST_EPSILON) {
299                                 font_element->set_attribute ("AspectAdjust", raw_convert<string> (aspect_adjust));
300                         }
301                         font_element->set_attribute ("Effect", effect_to_string (effect));
302                         font_element->set_attribute ("EffectColor", effect_colour.to_argb_string());
303                         font_element->set_attribute ("Script", "normal");
304                         if (standard == SMPTE) {
305                                 font_element->set_attribute ("Underline", "no");
306                         } else {
307                                 font_element->set_attribute ("Underlined", "no");
308                         }
309                         font_element->set_attribute ("Weight", bold ? "bold" : "normal");
310                 }
311
312                 if (!subtitle_element || font_changed ||
313                     (last_in != i.in() ||
314                      last_out != i.out() ||
315                      last_fade_up_time != i.fade_up_time() ||
316                      last_fade_down_time != i.fade_down_time()
317                             )) {
318
319                         subtitle_element = font_element->add_child ("Subtitle", xmlns);
320                         subtitle_element->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
321                         subtitle_element->set_attribute ("TimeIn", i.in().rebase(time_code_rate).as_string(standard));
322                         subtitle_element->set_attribute ("TimeOut", i.out().rebase(time_code_rate).as_string(standard));
323                         if (standard == SMPTE) {
324                                 subtitle_element->set_attribute ("FadeUpTime", i.fade_up_time().rebase(time_code_rate).as_string(standard));
325                                 subtitle_element->set_attribute ("FadeDownTime", i.fade_down_time().rebase(time_code_rate).as_string(standard));
326                         } else {
327                                 subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i.fade_up_time().as_editable_units(time_code_rate)));
328                                 subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i.fade_down_time().as_editable_units(time_code_rate)));
329                         }
330
331                         last_in = i.in ();
332                         last_out = i.out ();
333                         last_fade_up_time = i.fade_up_time ();
334                         last_fade_down_time = i.fade_down_time ();
335                 }
336
337                 xmlpp::Element* text = subtitle_element->add_child ("Text", xmlns);
338
339                 if (i.h_align() != HALIGN_CENTER) {
340                         if (standard == SMPTE) {
341                                 text->set_attribute ("Halign", halign_to_string (i.h_align ()));
342                         } else {
343                                 text->set_attribute ("HAlign", halign_to_string (i.h_align ()));
344                         }
345                 }
346
347                 if (i.h_position() > ALIGN_EPSILON) {
348                         if (standard == SMPTE) {
349                                 text->set_attribute ("Hposition", raw_convert<string> (i.h_position() * 100, 6));
350                         } else {
351                                 text->set_attribute ("HPosition", raw_convert<string> (i.h_position() * 100, 6));
352                         }
353                 }
354
355                 if (standard == SMPTE) {
356                         text->set_attribute ("Valign", valign_to_string (i.v_align()));
357                 } else {
358                         text->set_attribute ("VAlign", valign_to_string (i.v_align()));
359                 }
360
361                 if (i.v_position() > ALIGN_EPSILON) {
362                         if (standard == SMPTE) {
363                                 text->set_attribute ("Vposition", raw_convert<string> (i.v_position() * 100, 6));
364                         } else {
365                                 text->set_attribute ("VPosition", raw_convert<string> (i.v_position() * 100, 6));
366                         }
367                 } else {
368                         if (standard == SMPTE) {
369                                 text->set_attribute ("Vposition", "0");
370                         } else {
371                                 text->set_attribute ("VPosition", "0");
372                         }
373                 }
374
375                 /* Interop only supports "horizontal" or "vertical" for direction, so only write this
376                    for SMPTE.
377                 */
378                 if (i.direction() != DIRECTION_LTR && standard == SMPTE) {
379                         text->set_attribute ("Direction", direction_to_string (i.direction ()));
380                 }
381
382                 text->add_child_text (i.text());
383         }
384 }
385
386 map<string, Data>
387 SubtitleAsset::fonts_with_load_ids () const
388 {
389         map<string, Data> out;
390         BOOST_FOREACH (Font const & i, _fonts) {
391                 out[i.load_id] = i.data;
392         }
393         return out;
394 }