2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
22 #include <boost/algorithm/string.hpp>
23 #include <libxml++/nodes/element.h>
26 #include "subtitle_asset.h"
27 #include "parse/subtitle.h"
30 #include "raw_convert.h"
36 using std::stringstream;
38 using boost::shared_ptr;
39 using boost::optional;
40 using namespace libdcp;
42 SubtitleAsset::SubtitleAsset (string directory, string file)
43 : Asset (directory, file)
46 /* Grotesque hack: we should look in the PKL to see what type this file is;
47 instead we'll look at the first character to decide what to do.
48 I think this is easily fixable (properly) in 1.0.
51 FILE* f = fopen_boost (path(), "r");
53 throw FileError ("Could not open file for reading", file, errno);
55 unsigned char test[1];
56 fread (test, 1, 1, f);
59 if (test[0] == '<' || test[0] == 0xef) {
60 read_xml (path().string());
62 read_mxf (path().string());
66 SubtitleAsset::SubtitleAsset (string directory, string movie_title, string language)
68 , _movie_title (movie_title)
70 , _language (language)
77 SubtitleAsset::read_mxf (string mxf_file)
79 ASDCP::TimedText::MXFReader reader;
80 Kumu::Result_t r = reader.OpenRead (mxf_file.c_str ());
81 if (ASDCP_FAILURE (r)) {
82 boost::throw_exception (MXFFileError ("could not open MXF file for reading", mxf_file, r));
86 reader.ReadTimedTextResource (s, 0, 0);
87 shared_ptr<cxml::Document> xml (new cxml::Document ("SubtitleReel"));
95 SubtitleAsset::read_xml (string xml_file)
98 shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
99 xml->read_file (xml_file);
100 read_xml (xml, false);
101 } catch (cxml::Error& e) {
102 shared_ptr<cxml::Document> xml (new cxml::Document ("SubtitleReel"));
103 xml->read_file (xml_file);
104 read_xml (xml, true);
109 SubtitleAsset::read_xml (shared_ptr<cxml::Document> xml, bool smpte)
111 /* XXX: hacks aplenty in here; need separate parsers for DCSubtitle (Interop) and SubtitleReel (SMPTE) */
114 optional<string> x = xml->optional_string_child ("SubtitleID");
117 x = xml->optional_string_child ("Id");
119 _uuid = x.get_value_or ("");
121 _movie_title = xml->optional_string_child ("MovieTitle");
122 _reel_number = xml->string_child ("ReelNumber");
123 _language = xml->string_child ("Language");
127 tcr = xml->optional_number_child<int> ("TimeCodeRate");
130 _load_font_nodes = type_children<libdcp::parse::LoadFont> (xml, "LoadFont");
132 /* Now make Subtitle objects to represent the raw XML nodes
136 ParseState parse_state;
137 parse_node (xml->node(), parse_state, tcr);
141 SubtitleAsset::parse_node (xmlpp::Node* node, ParseState& parse_state, optional<int> tcr)
143 xmlpp::Node::NodeList children = node->get_children ();
144 for (xmlpp::Node::NodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
145 xmlpp::ContentNode* c = dynamic_cast<xmlpp::ContentNode *> (*i);
147 maybe_add_subtitle (c->get_content (), parse_state);
150 xmlpp::Element* e = dynamic_cast<xmlpp::Element *> (*i);
152 cxml::ConstNodePtr n (new cxml::Node (e));
153 if (n->name() == "Font") {
154 parse_state.font_nodes.push_back (shared_ptr<parse::Font> (new parse::Font (n, tcr)));
155 parse_node (e, parse_state, tcr);
156 parse_state.font_nodes.pop_back ();
157 } else if (n->name() == "Text") {
158 parse_state.text_nodes.push_back (shared_ptr<parse::Text> (new parse::Text (n, tcr)));
159 parse_node (e, parse_state, tcr);
160 parse_state.text_nodes.pop_back ();
161 } else if (n->name() == "Subtitle") {
162 parse_state.subtitle_nodes.push_back (shared_ptr<parse::Subtitle> (new parse::Subtitle (n, tcr)));
163 parse_node (e, parse_state, tcr);
164 parse_state.subtitle_nodes.pop_back ();
165 } else if (n->name() == "SubtitleList") {
166 parse_node (e, parse_state, tcr);
173 SubtitleAsset::maybe_add_subtitle (string text, ParseState& parse_state)
175 if (empty_or_white_space (text)) {
179 if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
183 assert (!parse_state.text_nodes.empty ());
184 assert (!parse_state.subtitle_nodes.empty ());
186 libdcp::parse::Font effective_font (parse_state.font_nodes);
187 libdcp::parse::Text effective_text (*parse_state.text_nodes.back ());
188 libdcp::parse::Subtitle effective_subtitle (*parse_state.subtitle_nodes.back ());
190 shared_ptr<Subtitle> c = parse_state.current;
192 effective_subtitle.in != c->in() ||
193 effective_subtitle.out != c->out() ||
194 effective_text.v_position != c->v_position() ||
195 effective_text.v_align != c->v_align()) {
197 parse_state.current.reset (
199 font_id_to_name (effective_font.id),
200 effective_font.italic.get(),
201 effective_font.color.get(),
203 effective_subtitle.in,
204 effective_subtitle.out,
205 effective_text.v_position,
206 effective_text.v_align,
208 effective_font.effect ? effective_font.effect.get() : NONE,
209 effective_font.effect_color.get(),
210 effective_subtitle.fade_up_time,
211 effective_subtitle.fade_down_time
215 _subtitles.push_back (parse_state.current);
218 if (effective_font.italic.get()) {
219 parse_state.current->set_text (parse_state.current->text() + "<i>" + text + "</i>");
221 parse_state.current->set_text (parse_state.current->text() + text);
225 list<shared_ptr<Subtitle> >
226 SubtitleAsset::subtitles_during (Time from, Time to) const
228 list<shared_ptr<Subtitle> > s;
229 for (list<shared_ptr<Subtitle> >::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
230 if ((*i)->out() >= from && (*i)->in() <= to) {
239 SubtitleAsset::font_id_to_name (string id) const
241 list<shared_ptr<libdcp::parse::LoadFont> >::const_iterator i = _load_font_nodes.begin();
242 while (i != _load_font_nodes.end() && (*i)->id != id) {
246 if (i == _load_font_nodes.end ()) {
250 if ((*i)->uri && (*i)->uri.get() == "arial.ttf") {
278 , _v_position (v_position)
282 , _effect_color (effect_color)
283 , _fade_up_time (fade_up_time)
284 , _fade_down_time (fade_down_time)
290 Subtitle::size_in_pixels (int screen_height) const
292 /* Size in the subtitle file is given in points as if the screen
293 height is 11 inches, so a 72pt font would be 1/11th of the screen
297 return _size * screen_height / (11 * 72);
301 libdcp::operator== (Subtitle const & a, Subtitle const & b)
304 a.font() == b.font() &&
305 a.italic() == b.italic() &&
306 a.color() == b.color() &&
307 a.size() == b.size() &&
309 a.out() == b.out() &&
310 a.v_position() == b.v_position() &&
311 a.v_align() == b.v_align() &&
312 a.text() == b.text() &&
313 a.effect() == b.effect() &&
314 a.effect_color() == b.effect_color() &&
315 a.fade_up_time() == b.fade_up_time() &&
316 a.fade_down_time() == b.fade_down_time()
321 libdcp::operator<< (ostream& s, Subtitle const & sub)
323 s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
324 << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
325 << "font " << sub.font() << ", ";
333 s << ", size " << sub.size() << ", color " << sub.color() << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align()) << ";\n"
334 << "effect " << ((int) sub.effect()) << ", effect color " << sub.effect_color();
340 SubtitleAsset::add (shared_ptr<Subtitle> s)
342 _subtitles.push_back (s);
347 SubtitleAsset::write_to_cpl (xmlpp::Element* node) const
349 /* XXX: should EditRate, Duration and IntrinsicDuration be in here? */
351 xmlpp::Node* ms = node->add_child ("MainSubtitle");
352 ms->add_child("Id")->add_child_text("urn:uuid:" + _uuid);
353 ms->add_child("AnnotationText")->add_child_text (_file_name.string ());
355 ms->add_child("EntryPoint")->add_child_text ("0");
358 struct SubtitleSorter {
359 bool operator() (shared_ptr<Subtitle> a, shared_ptr<Subtitle> b) {
360 if (a->in() != b->in()) {
361 return a->in() < b->in();
363 return a->v_position() < b->v_position();
368 SubtitleAsset::write_xml () const
370 FILE* f = fopen_boost (path (), "r");
371 Glib::ustring const s = xml_as_string ();
372 fwrite (s.c_str(), 1, s.length(), f);
377 SubtitleAsset::xml_as_string () const
380 xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
381 root->set_attribute ("Version", "1.0");
383 root->add_child("SubtitleID")->add_child_text (_uuid);
385 root->add_child("MovieTitle")->add_child_text (_movie_title.get ());
387 root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
388 root->add_child("Language")->add_child_text (_language);
390 if (_load_font_nodes.size() > 1) {
391 boost::throw_exception (MiscError ("multiple LoadFont nodes not supported"));
394 if (!_load_font_nodes.empty ()) {
395 xmlpp::Element* load_font = root->add_child("LoadFont");
396 load_font->set_attribute("Id", _load_font_nodes.front()->id);
397 if (_load_font_nodes.front()->uri) {
398 load_font->set_attribute("URI", _load_font_nodes.front()->uri.get ());
402 list<shared_ptr<Subtitle> > sorted = _subtitles;
404 sorted.sort (SubtitleSorter ());
407 /* XXX: multiple fonts not supported */
408 /* XXX: script, underlined, weight not supported */
413 Effect effect = NONE;
418 Time last_fade_up_time;
419 Time last_fade_down_time;
421 xmlpp::Element* font = 0;
422 xmlpp::Element* subtitle = 0;
424 for (list<shared_ptr<Subtitle> >::iterator i = sorted.begin(); i != sorted.end(); ++i) {
426 /* We will start a new <Font>...</Font> whenever some font property changes.
427 I suppose we should really make an optimal hierarchy of <Font> tags, but
431 bool const font_changed =
432 italic != (*i)->italic() ||
433 color != (*i)->color() ||
434 size != (*i)->size() ||
435 effect != (*i)->effect() ||
436 effect_color != (*i)->effect_color();
439 italic = (*i)->italic ();
440 color = (*i)->color ();
441 size = (*i)->size ();
442 effect = (*i)->effect ();
443 effect_color = (*i)->effect_color ();
446 if (!font || font_changed) {
447 font = root->add_child ("Font");
448 string id = "theFontId";
449 if (!_load_font_nodes.empty()) {
450 id = _load_font_nodes.front()->id;
452 font->set_attribute ("Id", id);
453 font->set_attribute ("Italic", italic ? "yes" : "no");
454 font->set_attribute ("Color", color.to_argb_string());
455 font->set_attribute ("Size", raw_convert<string> (size));
456 font->set_attribute ("Effect", effect_to_string (effect));
457 font->set_attribute ("EffectColor", effect_color.to_argb_string());
458 font->set_attribute ("Script", "normal");
459 font->set_attribute ("Underlined", "no");
460 font->set_attribute ("Weight", "normal");
463 if (!subtitle || font_changed ||
464 (last_in != (*i)->in() ||
465 last_out != (*i)->out() ||
466 last_fade_up_time != (*i)->fade_up_time() ||
467 last_fade_down_time != (*i)->fade_down_time()
470 subtitle = font->add_child ("Subtitle");
471 subtitle->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
472 subtitle->set_attribute ("TimeIn", (*i)->in().to_string());
473 subtitle->set_attribute ("TimeOut", (*i)->out().to_string());
474 subtitle->set_attribute ("FadeUpTime", raw_convert<string> ((*i)->fade_up_time().to_editable_units(250)));
475 subtitle->set_attribute ("FadeDownTime", raw_convert<string> ((*i)->fade_down_time().to_editable_units(250)));
477 last_in = (*i)->in ();
478 last_out = (*i)->out ();
479 last_fade_up_time = (*i)->fade_up_time ();
480 last_fade_down_time = (*i)->fade_down_time ();
483 xmlpp::Element* text = subtitle->add_child ("Text");
484 text->set_attribute ("VAlign", valign_to_string ((*i)->v_align()));
485 text->set_attribute ("VPosition", raw_convert<string> ((*i)->v_position()));
486 text->add_child_text ((*i)->text());
489 return doc.write_to_string_formatted ("UTF-8");