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.
21 #include <boost/lexical_cast.hpp>
22 #include "subtitle_asset.h"
29 using std::stringstream;
30 using boost::shared_ptr;
31 using boost::lexical_cast;
32 using namespace libdcp;
34 SubtitleAsset::SubtitleAsset (string directory, string xml_file)
35 : Asset (directory, xml_file)
37 shared_ptr<XMLFile> xml (new XMLFile (path().string(), "DCSubtitle"));
39 _uuid = xml->string_child ("SubtitleID");
40 _movie_title = xml->string_child ("MovieTitle");
41 _reel_number = xml->string_child ("ReelNumber");
42 _language = xml->string_child ("Language");
44 xml->ignore_child ("LoadFont");
46 list<shared_ptr<FontNode> > font_nodes = xml->type_children<FontNode> ("Font");
47 _load_font_nodes = xml->type_children<LoadFontNode> ("LoadFont");
49 /* Now make Subtitle objects to represent the raw XML nodes
53 ParseState parse_state;
54 examine_font_nodes (xml, font_nodes, parse_state);
57 SubtitleAsset::SubtitleAsset (string directory, string movie_title, string language)
59 , _movie_title (movie_title)
61 , _language (language)
67 SubtitleAsset::examine_font_nodes (
68 shared_ptr<XMLFile> xml,
69 list<shared_ptr<FontNode> > const & font_nodes,
70 ParseState& parse_state
73 for (list<shared_ptr<FontNode> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
75 parse_state.font_nodes.push_back (*i);
76 maybe_add_subtitle ((*i)->text, parse_state);
78 for (list<shared_ptr<SubtitleNode> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
79 parse_state.subtitle_nodes.push_back (*j);
80 examine_text_nodes (xml, (*j)->text_nodes, parse_state);
81 examine_font_nodes (xml, (*j)->font_nodes, parse_state);
82 parse_state.subtitle_nodes.pop_back ();
85 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
86 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
88 parse_state.font_nodes.pop_back ();
93 SubtitleAsset::examine_text_nodes (
94 shared_ptr<XMLFile> xml,
95 list<shared_ptr<TextNode> > const & text_nodes,
96 ParseState& parse_state
99 for (list<shared_ptr<TextNode> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
100 parse_state.text_nodes.push_back (*i);
101 maybe_add_subtitle ((*i)->text, parse_state);
102 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
103 parse_state.text_nodes.pop_back ();
108 SubtitleAsset::maybe_add_subtitle (string text, ParseState const & parse_state)
110 if (empty_or_white_space (text)) {
114 if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
118 assert (!parse_state.text_nodes.empty ());
119 assert (!parse_state.subtitle_nodes.empty ());
121 FontNode effective_font (parse_state.font_nodes);
122 TextNode effective_text (*parse_state.text_nodes.back ());
123 SubtitleNode effective_subtitle (*parse_state.subtitle_nodes.back ());
125 _subtitles.push_back (
126 shared_ptr<Subtitle> (
128 font_id_to_name (effective_font.id),
129 effective_font.italic.get(),
130 effective_font.color.get(),
132 effective_subtitle.in,
133 effective_subtitle.out,
134 effective_text.v_position,
135 effective_text.v_align,
137 effective_font.effect.get(),
138 effective_font.effect_color.get(),
139 effective_subtitle.fade_up_time,
140 effective_subtitle.fade_down_time
146 FontNode::FontNode (xmlpp::Node const * node)
151 id = optional_string_attribute ("Id");
152 size = optional_int64_attribute ("Size");
153 italic = optional_bool_attribute ("Italic");
154 color = optional_color_attribute ("Color");
155 string const e = optional_string_attribute ("Effect");
157 effect = string_to_effect (e);
159 effect_color = optional_color_attribute ("EffectColor");
160 subtitle_nodes = type_children<SubtitleNode> ("Subtitle");
161 font_nodes = type_children<FontNode> ("Font");
162 text_nodes = type_children<TextNode> ("Text");
165 FontNode::FontNode (list<shared_ptr<FontNode> > const & font_nodes)
169 , effect_color ("FFFFFFFF")
171 for (list<shared_ptr<FontNode> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
172 if (!(*i)->id.empty ()) {
175 if ((*i)->size != 0) {
179 italic = (*i)->italic.get ();
182 color = (*i)->color.get ();
185 effect = (*i)->effect.get ();
187 if ((*i)->effect_color) {
188 effect_color = (*i)->effect_color.get ();
193 LoadFontNode::LoadFontNode (xmlpp::Node const * node)
196 id = string_attribute ("Id");
197 uri = string_attribute ("URI");
201 SubtitleNode::SubtitleNode (xmlpp::Node const * node)
204 in = time_attribute ("TimeIn");
205 out = time_attribute ("TimeOut");
206 font_nodes = type_children<FontNode> ("Font");
207 text_nodes = type_children<TextNode> ("Text");
208 fade_up_time = fade_time ("FadeUpTime");
209 fade_down_time = fade_time ("FadeDownTime");
213 SubtitleNode::fade_time (string name)
215 string const u = optional_string_attribute (name);
219 t = Time (0, 0, 0, 20);
220 } else if (u.find (":") != string::npos) {
223 t = Time (0, 0, 0, lexical_cast<int> (u));
226 if (t > Time (0, 0, 8, 0)) {
227 t = Time (0, 0, 8, 0);
233 TextNode::TextNode (xmlpp::Node const * node)
238 v_position = float_attribute ("VPosition");
239 string const v = optional_string_attribute ("VAlign");
241 v_align = string_to_valign (v);
244 font_nodes = type_children<FontNode> ("Font");
247 list<shared_ptr<Subtitle> >
248 SubtitleAsset::subtitles_at (Time t) const
250 list<shared_ptr<Subtitle> > s;
251 for (list<shared_ptr<Subtitle> >::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
252 if ((*i)->in() <= t && t <= (*i)->out ()) {
261 SubtitleAsset::font_id_to_name (string id) const
263 list<shared_ptr<LoadFontNode> >::const_iterator i = _load_font_nodes.begin();
264 while (i != _load_font_nodes.end() && (*i)->id != id) {
268 if (i == _load_font_nodes.end ()) {
272 if ((*i)->uri == "arial.ttf") {
300 , _v_position (v_position)
304 , _effect_color (effect_color)
305 , _fade_up_time (fade_up_time)
306 , _fade_down_time (fade_down_time)
312 Subtitle::size_in_pixels (int screen_height) const
314 /* Size in the subtitle file is given in points as if the screen
315 height is 11 inches, so a 72pt font would be 1/11th of the screen
319 return _size * screen_height / (11 * 72);
323 libdcp::operator== (Subtitle const & a, Subtitle const & b)
326 a.font() == b.font() &&
327 a.italic() == b.italic() &&
328 a.color() == b.color() &&
329 a.size() == b.size() &&
331 a.out() == b.out() &&
332 a.v_position() == b.v_position() &&
333 a.v_align() == b.v_align() &&
334 a.text() == b.text() &&
335 a.effect() == b.effect() &&
336 a.effect_color() == b.effect_color() &&
337 a.fade_up_time() == b.fade_up_time() &&
338 a.fade_down_time() == b.fade_down_time()
343 libdcp::operator<< (ostream& s, Subtitle const & sub)
345 s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
346 << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
347 << "font " << sub.font() << ", ";
355 s << ", size " << sub.size() << ", color " << sub.color() << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align()) << ";\n"
356 << "effect " << ((int) sub.effect()) << ", effect color " << sub.effect_color();
362 SubtitleAsset::add (shared_ptr<Subtitle> s)
364 _subtitles.push_back (s);
368 SubtitleAsset::write_to_cpl (ostream& s) const
370 /* XXX: should EditRate, Duration and IntrinsicDuration be in here? */
372 s << " <MainSubtitle>\n"
373 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
374 << " <AnnotationText>" << _file_name << "</AnnotationText>\n"
375 << " <EntryPoint>0</EntryPoint>\n"
376 << " </MainSubtitle>\n";
379 struct SubtitleSorter {
380 bool operator() (shared_ptr<Subtitle> a, shared_ptr<Subtitle> b) {
381 return a->in() < b->in();
386 SubtitleAsset::write_xml ()
388 ofstream f (path().string().c_str());
390 f << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
391 << "<DCSubtitle Version=\"1.0\">\n"
392 << " <SubtitleID>" << _uuid << "</SubtitleID>\n"
393 << " <MovieTitle>" << _movie_title << "</MovieTitle>\n"
394 << " <ReelNumber>" << _reel_number << "</ReelNumber>\n"
395 << " <Language>" << _language << "</Language>\n"
396 << " <LoadFont Id=\"theFontId\" URI=\"arial.ttf\"/>";
398 _subtitles.sort (SubtitleSorter ());
400 /* XXX: multiple fonts not supported */
401 /* XXX: script, underlined, weight not supported */
407 Effect effect = NONE;
412 Time last_fade_up_time;
413 Time last_fade_down_time;
415 for (list<shared_ptr<Subtitle> >::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
418 if (first || italic != (*i)->italic()) {
419 italic = (*i)->italic ();
420 a << "Italic=\"" << (italic ? "yes" : "no") << "\" ";
423 if (first || color != (*i)->color()) {
424 color = (*i)->color ();
425 a << "Color=\"" << color.to_argb_string() << "\" ";
428 if (size || size != (*i)->size()) {
429 size = (*i)->size ();
430 a << "Size=\"" << size << "\" ";
433 if (first || effect != (*i)->effect()) {
434 effect = (*i)->effect ();
435 a << "Effect=\"" << effect_to_string(effect) << "\" ";
438 if (first || effect_color != (*i)->effect_color()) {
439 effect_color = (*i)->effect_color ();
440 a << "EffectColor=\"" << effect_color.to_argb_string() << "\" ";
444 a << "Script=\"normal\" Underlined=\"no\" Weight=\"normal\">";
447 if (!a.str().empty()) {
451 f << " <Font Id=\"theFontId\" " << a << ">\n";
456 (last_in != (*i)->in() ||
457 last_out != (*i)->out() ||
458 last_fade_up_time != (*i)->fade_up_time() ||
459 last_fade_down_time != (*i)->fade_down_time()
463 f << " </Subtitle>\n";
467 << "SpotNumber=\"" << spot_number++ << "\" "
468 << "TimeIn=" << (*i)->in().to_string() << "\" "
469 << "TimeOut=\"" << (*i)->out().to_string() << "\" "
470 << "FadeUpTime=\"" << (*i)->fade_up_time().to_ticks() << "\" "
471 << "FadeDownTime=\"" << (*i)->fade_down_time().to_ticks() << "\" "
474 last_in = (*i)->in ();
475 last_out = (*i)->out ();
476 last_fade_up_time = (*i)->fade_up_time ();
477 last_fade_down_time = (*i)->fade_down_time ();
481 << "VAlign=\"" << valign_to_string ((*i)->v_align()) << "\" "
482 << "VPosition=\"" << (*i)->v_position() << "\" "
483 << ">" << (*i)->text() << "</Text>\n";
488 f << " </Subtitle>\n";