Tweaks: don't use shared_ptr for dcp::SubtitleString; SubtitleContent::write_xml...
[libdcp.git] / src / subtitle_content.cc
1 /*
2     Copyright (C) 2012-2014 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_content.h"
22 #include "util.h"
23 #include "xml.h"
24 #include "font.h"
25 #include "text.h"
26 #include "load_font.h"
27 #include "subtitle_string.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 <fstream>
33
34 using std::string;
35 using std::list;
36 using std::ostream;
37 using std::ofstream;
38 using std::stringstream;
39 using std::cout;
40 using boost::shared_ptr;
41 using boost::optional;
42 using namespace dcp;
43
44 SubtitleContent::SubtitleContent (boost::filesystem::path file, bool mxf)
45         : Content (file)
46         , _need_sort (false)
47 {
48         shared_ptr<cxml::Document> xml;
49         
50         if (mxf) {
51                 ASDCP::TimedText::MXFReader reader;
52                 Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
53                 if (ASDCP_FAILURE (r)) {
54                         boost::throw_exception (MXFFileError ("could not open MXF file for reading", file, r));
55                 }
56                 
57                 string s;
58                 reader.ReadTimedTextResource (s, 0, 0);
59                 xml.reset (new cxml::Document ("SubtitleReel"));
60                 stringstream t;
61                 t << s;
62                 xml->read_stream (t);
63
64                 ASDCP::WriterInfo info;
65                 reader.FillWriterInfo (info);
66                 
67                 char buffer[64];
68                 Kumu::bin2UUIDhex (info.AssetUUID, ASDCP::UUIDlen, buffer, sizeof (buffer));
69                 _id = buffer;
70
71         } else {
72                 xml.reset (new cxml::Document ("DCSubtitle"));
73                 xml->read_file (file);
74                 _id = xml->string_child ("SubtitleID");
75         }
76
77         /* XXX: hacks aplenty in here; probably need separate parsers for DCSubtitle and SubtitleReel */
78
79         _movie_title = xml->optional_string_child ("MovieTitle");
80         _reel_number = xml->string_child ("ReelNumber");
81         _language = xml->string_child ("Language");
82
83         xml->ignore_child ("LoadFont");
84
85         list<shared_ptr<dcp::Font> > font_nodes = type_children<dcp::Font> (xml, "Font");
86         _load_font_nodes = type_children<dcp::LoadFont> (xml, "LoadFont");
87
88         /* Now make Subtitle objects to represent the raw XML nodes
89            in a sane way.
90         */
91
92         shared_ptr<cxml::Node> subtitle_list = xml->optional_node_child ("SubtitleList");
93         if (subtitle_list) {
94                 list<shared_ptr<dcp::Font> > font = type_children<dcp::Font> (subtitle_list, "Font");
95                 copy (font.begin(), font.end(), back_inserter (font_nodes));
96         }
97         
98         ParseState parse_state;
99         examine_font_nodes (xml, font_nodes, parse_state);
100 }
101
102 SubtitleContent::SubtitleContent (Fraction edit_rate, string movie_title, string language)
103         : Content (edit_rate)
104         , _movie_title (movie_title)
105         , _reel_number ("1")
106         , _language (language)
107         , _need_sort (false)
108 {
109
110 }
111
112 void
113 SubtitleContent::examine_font_nodes (
114         shared_ptr<const cxml::Node> xml,
115         list<shared_ptr<dcp::Font> > const & font_nodes,
116         ParseState& parse_state
117         )
118 {
119         for (list<shared_ptr<dcp::Font> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
120
121                 parse_state.font_nodes.push_back (*i);
122                 maybe_add_subtitle ((*i)->text, parse_state);
123
124                 for (list<shared_ptr<dcp::Subtitle> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
125                         parse_state.subtitle_nodes.push_back (*j);
126                         examine_text_nodes (xml, (*j)->text_nodes, parse_state);
127                         examine_font_nodes (xml, (*j)->font_nodes, parse_state);
128                         parse_state.subtitle_nodes.pop_back ();
129                 }
130         
131                 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
132                 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
133                 
134                 parse_state.font_nodes.pop_back ();
135         }
136 }
137
138 void
139 SubtitleContent::examine_text_nodes (
140         shared_ptr<const cxml::Node> xml,
141         list<shared_ptr<dcp::Text> > const & text_nodes,
142         ParseState& parse_state
143         )
144 {
145         for (list<shared_ptr<dcp::Text> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
146                 parse_state.text_nodes.push_back (*i);
147                 maybe_add_subtitle ((*i)->text, parse_state);
148                 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
149                 parse_state.text_nodes.pop_back ();
150         }
151 }
152
153 void
154 SubtitleContent::maybe_add_subtitle (string text, ParseState const & parse_state)
155 {
156         if (empty_or_white_space (text)) {
157                 return;
158         }
159         
160         if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
161                 return;
162         }
163
164         assert (!parse_state.text_nodes.empty ());
165         assert (!parse_state.subtitle_nodes.empty ());
166         
167         dcp::Font effective_font (parse_state.font_nodes);
168         dcp::Text effective_text (*parse_state.text_nodes.back ());
169         dcp::Subtitle effective_subtitle (*parse_state.subtitle_nodes.back ());
170
171         _subtitles.push_back (
172                 SubtitleString (
173                         font_id_to_name (effective_font.id),
174                         effective_font.italic.get(),
175                         effective_font.color.get(),
176                         effective_font.size,
177                         effective_subtitle.in,
178                         effective_subtitle.out,
179                         effective_text.v_position,
180                         effective_text.v_align,
181                         text,
182                         effective_font.effect ? effective_font.effect.get() : NONE,
183                         effective_font.effect_color.get(),
184                         effective_subtitle.fade_up_time,
185                         effective_subtitle.fade_down_time
186                         )
187                 );
188 }
189
190 list<SubtitleString>
191 SubtitleContent::subtitles_at (Time t) const
192 {
193         list<SubtitleString> s;
194         for (list<SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
195                 if (i->in() <= t && t <= i->out ()) {
196                         s.push_back (*i);
197                 }
198         }
199
200         return s;
201 }
202
203 std::string
204 SubtitleContent::font_id_to_name (string id) const
205 {
206         list<shared_ptr<dcp::LoadFont> >::const_iterator i = _load_font_nodes.begin();
207         while (i != _load_font_nodes.end() && (*i)->id != id) {
208                 ++i;
209         }
210
211         if (i == _load_font_nodes.end ()) {
212                 return "";
213         }
214
215         if ((*i)->uri && (*i)->uri.get() == "arial.ttf") {
216                 return "Arial";
217         }
218
219         return "";
220 }
221
222 void
223 SubtitleContent::add (SubtitleString s)
224 {
225         _subtitles.push_back (s);
226         _need_sort = true;
227 }
228
229 struct SubtitleSorter {
230         bool operator() (SubtitleString const & a, SubtitleString const & b) {
231                 if (a.in() != b.in()) {
232                         return a.in() < b.in();
233                 }
234                 return a.v_position() < b.v_position();
235         }
236 };
237
238 void
239 SubtitleContent::write_xml (boost::filesystem::path p) const
240 {
241         FILE* f = fopen_boost (p, "r");
242         Glib::ustring const s = xml_as_string ();
243         fwrite (s.c_str(), 1, s.length(), f);
244         fclose (f);
245 }
246
247 Glib::ustring
248 SubtitleContent::xml_as_string () const
249 {
250         xmlpp::Document doc;
251         xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
252         root->set_attribute ("Version", "1.0");
253
254         root->add_child("SubtitleID")->add_child_text (_id);
255         if (_movie_title) {
256                 root->add_child("MovieTitle")->add_child_text (_movie_title.get ());
257         }
258         root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
259         root->add_child("Language")->add_child_text (_language);
260
261         if (_load_font_nodes.size() > 1) {
262                 boost::throw_exception (MiscError ("multiple LoadFont nodes not supported"));
263         }
264
265         if (!_load_font_nodes.empty ()) {
266                 xmlpp::Element* load_font = root->add_child("LoadFont");
267                 load_font->set_attribute ("Id", _load_font_nodes.front()->id);
268                 if (_load_font_nodes.front()->uri) {
269                         load_font->set_attribute ("URI", _load_font_nodes.front()->uri.get ());
270                 }
271         }
272
273         list<SubtitleString> sorted = _subtitles;
274         if (_need_sort) {
275                 sorted.sort (SubtitleSorter ());
276         }
277
278         /* XXX: multiple fonts not supported */
279         /* XXX: script, underlined, weight not supported */
280
281         bool italic = false;
282         Color color;
283         int size = 0;
284         Effect effect = NONE;
285         Color effect_color;
286         int spot_number = 1;
287         Time last_in;
288         Time last_out;
289         Time last_fade_up_time;
290         Time last_fade_down_time;
291
292         xmlpp::Element* font = 0;
293         xmlpp::Element* subtitle = 0;
294
295         for (list<SubtitleString>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
296
297                 /* We will start a new <Font>...</Font> whenever some font property changes.
298                    I suppose we should really make an optimal hierarchy of <Font> tags, but
299                    that seems hard.
300                 */
301
302                 bool const font_changed =
303                         italic       != i->italic()       ||
304                         color        != i->color()        ||
305                         size         != i->size()         ||
306                         effect       != i->effect()       ||
307                         effect_color != i->effect_color();
308
309                 if (font_changed) {
310                         italic = i->italic ();
311                         color = i->color ();
312                         size = i->size ();
313                         effect = i->effect ();
314                         effect_color = i->effect_color ();
315                 }
316
317                 if (!font || font_changed) {
318                         font = root->add_child ("Font");
319                         string id = "theFontId";
320                         if (!_load_font_nodes.empty()) {
321                                 id = _load_font_nodes.front()->id;
322                         }
323                         font->set_attribute ("Id", id);
324                         font->set_attribute ("Italic", italic ? "yes" : "no");
325                         font->set_attribute ("Color", color.to_argb_string());
326                         font->set_attribute ("Size", raw_convert<string> (size));
327                         font->set_attribute ("Effect", effect_to_string (effect));
328                         font->set_attribute ("EffectColor", effect_color.to_argb_string());
329                         font->set_attribute ("Script", "normal");
330                         font->set_attribute ("Underlined", "no");
331                         font->set_attribute ("Weight", "normal");
332                 }
333
334                 if (!subtitle || font_changed ||
335                     (last_in != i->in() ||
336                      last_out != i->out() ||
337                      last_fade_up_time != i->fade_up_time() ||
338                      last_fade_down_time != i->fade_down_time()
339                             )) {
340
341                         subtitle = font->add_child ("Subtitle");
342                         subtitle->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
343                         subtitle->set_attribute ("TimeIn", i->in().to_string());
344                         subtitle->set_attribute ("TimeOut", i->out().to_string());
345                         subtitle->set_attribute ("FadeUpTime", raw_convert<string> (i->fade_up_time().to_ticks()));
346                         subtitle->set_attribute ("FadeDownTime", raw_convert<string> (i->fade_down_time().to_ticks()));
347
348                         last_in = i->in ();
349                         last_out = i->out ();
350                         last_fade_up_time = i->fade_up_time ();
351                         last_fade_down_time = i->fade_down_time ();
352                 }
353
354                 xmlpp::Element* text = subtitle->add_child ("Text");
355                 text->set_attribute ("VAlign", valign_to_string (i->v_align()));                
356                 text->set_attribute ("VPosition", raw_convert<string> (i->v_position()));
357                 text->add_child_text (i->text());
358         }
359
360         return doc.write_to_string_formatted ("UTF-8");
361 }
362