2 Copyright (C) 2012 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)
97 shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
98 xml->read_file (xml_file);
103 SubtitleAsset::read_xml (shared_ptr<cxml::Document> xml)
105 /* XXX: hacks aplenty in here; need separate parsers for DCSubtitle and SubtitleReel */
108 optional<string> x = xml->optional_string_child ("SubtitleID");
111 x = xml->optional_string_child ("Id");
113 _uuid = x.get_value_or ("");
115 _movie_title = xml->optional_string_child ("MovieTitle");
116 _reel_number = xml->string_child ("ReelNumber");
117 _language = xml->string_child ("Language");
119 xml->ignore_child ("LoadFont");
121 list<shared_ptr<libdcp::parse::Font> > font_nodes = type_children<libdcp::parse::Font> (xml, "Font");
122 _load_font_nodes = type_children<libdcp::parse::LoadFont> (xml, "LoadFont");
124 /* Now make Subtitle objects to represent the raw XML nodes
128 shared_ptr<cxml::Node> subtitle_list = xml->optional_node_child ("SubtitleList");
130 list<shared_ptr<libdcp::parse::Font> > font = type_children<libdcp::parse::Font> (subtitle_list, "Font");
131 copy (font.begin(), font.end(), back_inserter (font_nodes));
134 ParseState parse_state;
135 examine_font_nodes (xml, font_nodes, parse_state);
139 SubtitleAsset::examine_font_nodes (
140 shared_ptr<const cxml::Node> xml,
141 list<shared_ptr<libdcp::parse::Font> > const & font_nodes,
142 ParseState& parse_state
145 for (list<shared_ptr<libdcp::parse::Font> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
147 parse_state.font_nodes.push_back (*i);
148 maybe_add_subtitle ((*i)->text, parse_state);
150 for (list<shared_ptr<libdcp::parse::Subtitle> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
151 parse_state.subtitle_nodes.push_back (*j);
152 examine_text_nodes (xml, (*j)->text_nodes, parse_state);
153 examine_font_nodes (xml, (*j)->font_nodes, parse_state);
154 parse_state.subtitle_nodes.pop_back ();
157 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
158 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
160 parse_state.font_nodes.pop_back ();
165 SubtitleAsset::examine_text_nodes (
166 shared_ptr<const cxml::Node> xml,
167 list<shared_ptr<libdcp::parse::Text> > const & text_nodes,
168 ParseState& parse_state
171 for (list<shared_ptr<libdcp::parse::Text> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
172 parse_state.text_nodes.push_back (*i);
173 maybe_add_subtitle ((*i)->text, parse_state);
174 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
175 parse_state.text_nodes.pop_back ();
180 SubtitleAsset::maybe_add_subtitle (string text, ParseState const & parse_state)
182 if (empty_or_white_space (text)) {
186 if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
190 assert (!parse_state.text_nodes.empty ());
191 assert (!parse_state.subtitle_nodes.empty ());
193 libdcp::parse::Font effective_font (parse_state.font_nodes);
194 libdcp::parse::Text effective_text (*parse_state.text_nodes.back ());
195 libdcp::parse::Subtitle effective_subtitle (*parse_state.subtitle_nodes.back ());
197 _subtitles.push_back (
198 shared_ptr<Subtitle> (
200 font_id_to_name (effective_font.id),
201 effective_font.italic.get(),
202 effective_font.color.get(),
204 effective_subtitle.in,
205 effective_subtitle.out,
206 effective_text.v_position,
207 effective_text.v_align,
209 effective_font.effect ? effective_font.effect.get() : NONE,
210 effective_font.effect_color.get(),
211 effective_subtitle.fade_up_time,
212 effective_subtitle.fade_down_time
218 list<shared_ptr<Subtitle> >
219 SubtitleAsset::subtitles_at (Time t) const
221 list<shared_ptr<Subtitle> > s;
222 for (list<shared_ptr<Subtitle> >::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
223 if ((*i)->in() <= t && t <= (*i)->out ()) {
232 SubtitleAsset::font_id_to_name (string id) const
234 list<shared_ptr<libdcp::parse::LoadFont> >::const_iterator i = _load_font_nodes.begin();
235 while (i != _load_font_nodes.end() && (*i)->id != id) {
239 if (i == _load_font_nodes.end ()) {
243 if ((*i)->uri && (*i)->uri.get() == "arial.ttf") {
271 , _v_position (v_position)
275 , _effect_color (effect_color)
276 , _fade_up_time (fade_up_time)
277 , _fade_down_time (fade_down_time)
283 Subtitle::size_in_pixels (int screen_height) const
285 /* Size in the subtitle file is given in points as if the screen
286 height is 11 inches, so a 72pt font would be 1/11th of the screen
290 return _size * screen_height / (11 * 72);
294 libdcp::operator== (Subtitle const & a, Subtitle const & b)
297 a.font() == b.font() &&
298 a.italic() == b.italic() &&
299 a.color() == b.color() &&
300 a.size() == b.size() &&
302 a.out() == b.out() &&
303 a.v_position() == b.v_position() &&
304 a.v_align() == b.v_align() &&
305 a.text() == b.text() &&
306 a.effect() == b.effect() &&
307 a.effect_color() == b.effect_color() &&
308 a.fade_up_time() == b.fade_up_time() &&
309 a.fade_down_time() == b.fade_down_time()
314 libdcp::operator<< (ostream& s, Subtitle const & sub)
316 s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
317 << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
318 << "font " << sub.font() << ", ";
326 s << ", size " << sub.size() << ", color " << sub.color() << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align()) << ";\n"
327 << "effect " << ((int) sub.effect()) << ", effect color " << sub.effect_color();
333 SubtitleAsset::add (shared_ptr<Subtitle> s)
335 _subtitles.push_back (s);
340 SubtitleAsset::write_to_cpl (xmlpp::Element* node) const
342 /* XXX: should EditRate, Duration and IntrinsicDuration be in here? */
344 xmlpp::Node* ms = node->add_child ("MainSubtitle");
345 ms->add_child("Id")->add_child_text("urn:uuid:" + _uuid);
346 ms->add_child("AnnotationText")->add_child_text (_file_name.string ());
348 ms->add_child("EntryPoint")->add_child_text ("0");
351 struct SubtitleSorter {
352 bool operator() (shared_ptr<Subtitle> a, shared_ptr<Subtitle> b) {
353 if (a->in() != b->in()) {
354 return a->in() < b->in();
356 return a->v_position() < b->v_position();
361 SubtitleAsset::write_xml () const
363 FILE* f = fopen_boost (path (), "r");
364 Glib::ustring const s = xml_as_string ();
365 fwrite (s.c_str(), 1, s.length(), f);
370 SubtitleAsset::xml_as_string () const
373 xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
374 root->set_attribute ("Version", "1.0");
376 root->add_child("SubtitleID")->add_child_text (_uuid);
378 root->add_child("MovieTitle")->add_child_text (_movie_title.get ());
380 root->add_child("ReelNumber")->add_child_text (raw_convert<string> (_reel_number));
381 root->add_child("Language")->add_child_text (_language);
383 if (_load_font_nodes.size() > 1) {
384 boost::throw_exception (MiscError ("multiple LoadFont nodes not supported"));
387 if (!_load_font_nodes.empty ()) {
388 xmlpp::Element* load_font = root->add_child("LoadFont");
389 load_font->set_attribute("Id", _load_font_nodes.front()->id);
390 if (_load_font_nodes.front()->uri) {
391 load_font->set_attribute("URI", _load_font_nodes.front()->uri.get ());
395 list<shared_ptr<Subtitle> > sorted = _subtitles;
397 sorted.sort (SubtitleSorter ());
400 /* XXX: multiple fonts not supported */
401 /* XXX: script, underlined, weight not supported */
406 Effect effect = NONE;
411 Time last_fade_up_time;
412 Time last_fade_down_time;
414 xmlpp::Element* font = 0;
415 xmlpp::Element* subtitle = 0;
417 for (list<shared_ptr<Subtitle> >::iterator i = sorted.begin(); i != sorted.end(); ++i) {
419 /* We will start a new <Font>...</Font> whenever some font property changes.
420 I suppose we should really make an optimal hierarchy of <Font> tags, but
424 bool const font_changed =
425 italic != (*i)->italic() ||
426 color != (*i)->color() ||
427 size != (*i)->size() ||
428 effect != (*i)->effect() ||
429 effect_color != (*i)->effect_color();
432 italic = (*i)->italic ();
433 color = (*i)->color ();
434 size = (*i)->size ();
435 effect = (*i)->effect ();
436 effect_color = (*i)->effect_color ();
439 if (!font || font_changed) {
440 font = root->add_child ("Font");
441 string id = "theFontId";
442 if (!_load_font_nodes.empty()) {
443 id = _load_font_nodes.front()->id;
445 font->set_attribute ("Id", id);
446 font->set_attribute ("Italic", italic ? "yes" : "no");
447 font->set_attribute ("Color", color.to_argb_string());
448 font->set_attribute ("Size", raw_convert<string> (size));
449 font->set_attribute ("Effect", effect_to_string (effect));
450 font->set_attribute ("EffectColor", effect_color.to_argb_string());
451 font->set_attribute ("Script", "normal");
452 font->set_attribute ("Underlined", "no");
453 font->set_attribute ("Weight", "normal");
456 if (!subtitle || font_changed ||
457 (last_in != (*i)->in() ||
458 last_out != (*i)->out() ||
459 last_fade_up_time != (*i)->fade_up_time() ||
460 last_fade_down_time != (*i)->fade_down_time()
463 subtitle = font->add_child ("Subtitle");
464 subtitle->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
465 subtitle->set_attribute ("TimeIn", (*i)->in().to_string());
466 subtitle->set_attribute ("TimeOut", (*i)->out().to_string());
467 subtitle->set_attribute ("FadeUpTime", raw_convert<string> ((*i)->fade_up_time().to_ticks()));
468 subtitle->set_attribute ("FadeDownTime", raw_convert<string> ((*i)->fade_down_time().to_ticks()));
470 last_in = (*i)->in ();
471 last_out = (*i)->out ();
472 last_fade_up_time = (*i)->fade_up_time ();
473 last_fade_down_time = (*i)->fade_down_time ();
476 xmlpp::Element* text = subtitle->add_child ("Text");
477 text->set_attribute ("VAlign", valign_to_string ((*i)->v_align()));
478 text->set_attribute ("VPosition", raw_convert<string> ((*i)->v_position()));
479 text->add_child_text ((*i)->text());
482 return doc.write_to_string_formatted ("UTF-8");