2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
21 #include "raw_convert.h"
22 #include "subtitle_asset.h"
25 #include "font_node.h"
26 #include "text_node.h"
27 #include "subtitle_string.h"
28 #include "dcp_assert.h"
31 #include <libxml++/nodes/element.h>
32 #include <boost/algorithm/string.hpp>
33 #include <boost/shared_array.hpp>
34 #include <boost/foreach.hpp>
41 using std::stringstream;
45 using boost::shared_ptr;
46 using boost::shared_array;
47 using boost::optional;
48 using boost::dynamic_pointer_cast;
51 SubtitleAsset::SubtitleAsset ()
56 SubtitleAsset::SubtitleAsset (boost::filesystem::path file)
63 SubtitleAsset::parse_subtitles (
64 shared_ptr<cxml::Document> xml,
65 list<shared_ptr<dcp::FontNode> > font_nodes,
66 list<shared_ptr<dcp::SubtitleNode> > subtitle_nodes
69 /* Make Subtitle objects to represent the raw XML nodes in a sane way */
70 ParseState parse_state;
71 examine_nodes (xml, font_nodes, parse_state);
72 examine_nodes (xml, subtitle_nodes, parse_state);
76 SubtitleAsset::examine_nodes (
77 shared_ptr<const cxml::Node> xml,
78 list<shared_ptr<dcp::SubtitleNode> > const & subtitle_nodes,
79 ParseState& parse_state
82 BOOST_FOREACH (shared_ptr<dcp::SubtitleNode> i, subtitle_nodes) {
83 parse_state.subtitle_nodes.push_back (i);
84 examine_nodes (xml, i->text_nodes, parse_state);
85 examine_nodes (xml, i->font_nodes, parse_state);
86 parse_state.subtitle_nodes.pop_back ();
91 SubtitleAsset::examine_nodes (
92 shared_ptr<const cxml::Node> xml,
93 list<shared_ptr<dcp::FontNode> > const & font_nodes,
94 ParseState& parse_state
97 BOOST_FOREACH (shared_ptr<dcp::FontNode> i, font_nodes) {
99 parse_state.font_nodes.push_back (i);
100 maybe_add_subtitle (i->text, parse_state);
102 examine_nodes (xml, i->subtitle_nodes, parse_state);
103 examine_nodes (xml, i->font_nodes, parse_state);
104 examine_nodes (xml, i->text_nodes, parse_state);
106 parse_state.font_nodes.pop_back ();
111 SubtitleAsset::examine_nodes (
112 shared_ptr<const cxml::Node> xml,
113 list<shared_ptr<dcp::TextNode> > const & text_nodes,
114 ParseState& parse_state
117 BOOST_FOREACH (shared_ptr<dcp::TextNode> i, text_nodes) {
118 parse_state.text_nodes.push_back (i);
119 maybe_add_subtitle (i->text, parse_state);
120 examine_nodes (xml, i->font_nodes, parse_state);
121 parse_state.text_nodes.pop_back ();
126 SubtitleAsset::maybe_add_subtitle (string text, ParseState const & parse_state)
128 if (empty_or_white_space (text)) {
132 if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
136 DCP_ASSERT (!parse_state.text_nodes.empty ());
137 DCP_ASSERT (!parse_state.subtitle_nodes.empty ());
139 dcp::FontNode effective_font (parse_state.font_nodes);
140 dcp::TextNode effective_text (*parse_state.text_nodes.back ());
141 dcp::SubtitleNode effective_subtitle (*parse_state.subtitle_nodes.back ());
143 _subtitles.push_back (
146 effective_font.italic.get_value_or (false),
147 effective_font.bold.get_value_or (false),
148 effective_font.colour.get_value_or (dcp::Colour (255, 255, 255)),
150 effective_font.aspect_adjust.get_value_or (1.0),
151 effective_subtitle.in,
152 effective_subtitle.out,
153 effective_text.h_position,
154 effective_text.h_align,
155 effective_text.v_position,
156 effective_text.v_align,
157 effective_text.direction,
159 effective_font.effect.get_value_or (NONE),
160 effective_font.effect_colour.get_value_or (dcp::Colour (0, 0, 0)),
161 effective_subtitle.fade_up_time,
162 effective_subtitle.fade_down_time
168 SubtitleAsset::subtitles_during (Time from, Time to, bool starting) const
170 list<SubtitleString> s;
171 BOOST_FOREACH (SubtitleString const & i, _subtitles) {
172 if ((starting && from <= i.in() && i.in() < to) || (!starting && i.out() >= from && i.in() <= to)) {
181 SubtitleAsset::add (SubtitleString s)
183 _subtitles.push_back (s);
187 SubtitleAsset::latest_subtitle_out () const
190 BOOST_FOREACH (SubtitleString const & i, _subtitles) {
200 SubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
202 if (!Asset::equals (other_asset, options, note)) {
206 shared_ptr<const SubtitleAsset> other = dynamic_pointer_cast<const SubtitleAsset> (other_asset);
211 if (_subtitles != other->_subtitles) {
212 note (DCP_ERROR, "subtitles differ");
219 struct SubtitleSorter {
220 bool operator() (SubtitleString const & a, SubtitleString const & b) {
221 if (a.in() != b.in()) {
222 return a.in() < b.in();
224 return a.v_position() < b.v_position();
228 /** @param standard Standard (INTEROP or SMPTE); this is used rather than putting things in the child
229 * class because the differences between the two are fairly subtle.
232 SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const
234 list<SubtitleString> sorted = _subtitles;
235 sorted.sort (SubtitleSorter ());
237 string const xmlns = standard == SMPTE ? "dcst" : "";
239 /* XXX: script, underlined not supported */
241 optional<string> font;
246 float aspect_adjust = 1.0;
247 Effect effect = NONE;
248 Colour effect_colour;
252 Time last_fade_up_time;
253 Time last_fade_down_time;
255 xmlpp::Element* font_element = 0;
256 xmlpp::Element* subtitle_element = 0;
258 BOOST_FOREACH (SubtitleString const & i, sorted) {
260 /* We will start a new <Font>...</Font> whenever some font property changes.
261 I suppose we should really make an optimal hierarchy of <Font> tags, but
265 bool const font_changed =
267 italic != i.italic() ||
269 colour != i.colour() ||
271 fabs (aspect_adjust - i.aspect_adjust()) > ASPECT_ADJUST_EPSILON ||
272 effect != i.effect() ||
273 effect_colour != i.effect_colour();
277 italic = i.italic ();
279 colour = i.colour ();
281 aspect_adjust = i.aspect_adjust ();
282 effect = i.effect ();
283 effect_colour = i.effect_colour ();
286 if (!font_element || font_changed) {
287 font_element = root->add_child ("Font", xmlns);
289 if (standard == SMPTE) {
290 font_element->set_attribute ("ID", font.get ());
292 font_element->set_attribute ("Id", font.get ());
295 font_element->set_attribute ("Italic", italic ? "yes" : "no");
296 font_element->set_attribute ("Color", colour.to_argb_string());
297 font_element->set_attribute ("Size", raw_convert<string> (size));
298 if (fabs (aspect_adjust - 1.0) > ASPECT_ADJUST_EPSILON) {
299 font_element->set_attribute ("AspectAdjust", raw_convert<string> (aspect_adjust));
301 font_element->set_attribute ("Effect", effect_to_string (effect));
302 font_element->set_attribute ("EffectColor", effect_colour.to_argb_string());
303 font_element->set_attribute ("Script", "normal");
304 if (standard == SMPTE) {
305 font_element->set_attribute ("Underline", "no");
307 font_element->set_attribute ("Underlined", "no");
309 font_element->set_attribute ("Weight", bold ? "bold" : "normal");
312 if (!subtitle_element || font_changed ||
313 (last_in != i.in() ||
314 last_out != i.out() ||
315 last_fade_up_time != i.fade_up_time() ||
316 last_fade_down_time != i.fade_down_time()
319 subtitle_element = font_element->add_child ("Subtitle", xmlns);
320 subtitle_element->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
321 subtitle_element->set_attribute ("TimeIn", i.in().rebase(time_code_rate).as_string(standard));
322 subtitle_element->set_attribute ("TimeOut", i.out().rebase(time_code_rate).as_string(standard));
323 if (standard == SMPTE) {
324 subtitle_element->set_attribute ("FadeUpTime", i.fade_up_time().rebase(time_code_rate).as_string(standard));
325 subtitle_element->set_attribute ("FadeDownTime", i.fade_down_time().rebase(time_code_rate).as_string(standard));
327 subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i.fade_up_time().as_editable_units(time_code_rate)));
328 subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i.fade_down_time().as_editable_units(time_code_rate)));
333 last_fade_up_time = i.fade_up_time ();
334 last_fade_down_time = i.fade_down_time ();
337 xmlpp::Element* text = subtitle_element->add_child ("Text", xmlns);
339 if (i.h_align() != HALIGN_CENTER) {
340 if (standard == SMPTE) {
341 text->set_attribute ("Halign", halign_to_string (i.h_align ()));
343 text->set_attribute ("HAlign", halign_to_string (i.h_align ()));
347 if (i.h_position() > ALIGN_EPSILON) {
348 if (standard == SMPTE) {
349 text->set_attribute ("Hposition", raw_convert<string> (i.h_position() * 100, 6));
351 text->set_attribute ("HPosition", raw_convert<string> (i.h_position() * 100, 6));
355 if (standard == SMPTE) {
356 text->set_attribute ("Valign", valign_to_string (i.v_align()));
358 text->set_attribute ("VAlign", valign_to_string (i.v_align()));
361 if (i.v_position() > ALIGN_EPSILON) {
362 if (standard == SMPTE) {
363 text->set_attribute ("Vposition", raw_convert<string> (i.v_position() * 100, 6));
365 text->set_attribute ("VPosition", raw_convert<string> (i.v_position() * 100, 6));
368 if (standard == SMPTE) {
369 text->set_attribute ("Vposition", "0");
371 text->set_attribute ("VPosition", "0");
375 /* Interop only supports "horizontal" or "vertical" for direction, so only write this
378 if (i.direction() != DIRECTION_LTR && standard == SMPTE) {
379 text->set_attribute ("Direction", direction_to_string (i.direction ()));
382 text->add_child_text (i.text());
387 SubtitleAsset::fonts_with_load_ids () const
389 map<string, Data> out;
390 BOOST_FOREACH (Font const & i, _fonts) {
391 out[i.load_id] = i.data;