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