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.
20 #include "raw_convert.h"
21 #include "subtitle_asset.h"
24 #include "font_node.h"
25 #include "text_node.h"
26 #include "subtitle_string.h"
27 #include "dcp_assert.h"
30 #include <libxml++/nodes/element.h>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/shared_array.hpp>
33 #include <boost/foreach.hpp>
40 using std::stringstream;
44 using boost::shared_ptr;
45 using boost::shared_array;
46 using boost::optional;
47 using boost::dynamic_pointer_cast;
50 SubtitleAsset::SubtitleAsset ()
55 SubtitleAsset::SubtitleAsset (boost::filesystem::path file)
62 SubtitleAsset::parse_subtitles (
63 shared_ptr<cxml::Document> xml,
64 list<shared_ptr<dcp::FontNode> > font_nodes,
65 list<shared_ptr<dcp::SubtitleNode> > subtitle_nodes
68 /* Make Subtitle objects to represent the raw XML nodes in a sane way */
69 ParseState parse_state;
70 examine_nodes (xml, font_nodes, parse_state);
71 examine_nodes (xml, subtitle_nodes, parse_state);
75 SubtitleAsset::examine_nodes (
76 shared_ptr<const cxml::Node> xml,
77 list<shared_ptr<dcp::SubtitleNode> > const & subtitle_nodes,
78 ParseState& parse_state
81 BOOST_FOREACH (shared_ptr<dcp::SubtitleNode> i, subtitle_nodes) {
82 parse_state.subtitle_nodes.push_back (i);
83 examine_nodes (xml, i->text_nodes, parse_state);
84 examine_nodes (xml, i->font_nodes, parse_state);
85 parse_state.subtitle_nodes.pop_back ();
90 SubtitleAsset::examine_nodes (
91 shared_ptr<const cxml::Node> xml,
92 list<shared_ptr<dcp::FontNode> > const & font_nodes,
93 ParseState& parse_state
96 BOOST_FOREACH (shared_ptr<dcp::FontNode> i, font_nodes) {
98 parse_state.font_nodes.push_back (i);
99 maybe_add_subtitle (i->text, parse_state);
101 examine_nodes (xml, i->subtitle_nodes, parse_state);
102 examine_nodes (xml, i->font_nodes, parse_state);
103 examine_nodes (xml, i->text_nodes, parse_state);
105 parse_state.font_nodes.pop_back ();
110 SubtitleAsset::examine_nodes (
111 shared_ptr<const cxml::Node> xml,
112 list<shared_ptr<dcp::TextNode> > const & text_nodes,
113 ParseState& parse_state
116 BOOST_FOREACH (shared_ptr<dcp::TextNode> i, text_nodes) {
117 parse_state.text_nodes.push_back (i);
118 maybe_add_subtitle (i->text, parse_state);
119 examine_nodes (xml, i->font_nodes, parse_state);
120 parse_state.text_nodes.pop_back ();
125 SubtitleAsset::maybe_add_subtitle (string text, ParseState const & parse_state)
127 if (empty_or_white_space (text)) {
131 if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
135 DCP_ASSERT (!parse_state.text_nodes.empty ());
136 DCP_ASSERT (!parse_state.subtitle_nodes.empty ());
138 dcp::FontNode effective_font (parse_state.font_nodes);
139 dcp::TextNode effective_text (*parse_state.text_nodes.back ());
140 dcp::SubtitleNode effective_subtitle (*parse_state.subtitle_nodes.back ());
142 _subtitles.push_back (
145 effective_font.italic.get_value_or (false),
146 effective_font.bold.get_value_or (false),
147 effective_font.colour.get_value_or (dcp::Colour (255, 255, 255)),
149 effective_font.aspect_adjust.get_value_or (1.0),
150 effective_subtitle.in,
151 effective_subtitle.out,
152 effective_text.h_position,
153 effective_text.h_align,
154 effective_text.v_position,
155 effective_text.v_align,
156 effective_text.direction,
158 effective_font.effect.get_value_or (NONE),
159 effective_font.effect_colour.get_value_or (dcp::Colour (0, 0, 0)),
160 effective_subtitle.fade_up_time,
161 effective_subtitle.fade_down_time
167 SubtitleAsset::subtitles_during (Time from, Time to, bool starting) const
169 list<SubtitleString> s;
170 BOOST_FOREACH (SubtitleString const & i, _subtitles) {
171 if ((starting && from <= i.in() && i.in() < to) || (!starting && i.out() >= from && i.in() <= to)) {
180 SubtitleAsset::add (SubtitleString s)
182 _subtitles.push_back (s);
186 SubtitleAsset::latest_subtitle_out () const
189 BOOST_FOREACH (SubtitleString const & i, _subtitles) {
199 SubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
201 if (!Asset::equals (other_asset, options, note)) {
205 shared_ptr<const SubtitleAsset> other = dynamic_pointer_cast<const SubtitleAsset> (other_asset);
210 if (_subtitles != other->_subtitles) {
211 note (DCP_ERROR, "subtitles differ");
218 struct SubtitleSorter {
219 bool operator() (SubtitleString const & a, SubtitleString const & b) {
220 if (a.in() != b.in()) {
221 return a.in() < b.in();
223 return a.v_position() < b.v_position();
227 /** @param standard Standard (INTEROP or SMPTE); this is used rather than putting things in the child
228 * class because the differences between the two are fairly subtle.
231 SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const
233 list<SubtitleString> sorted = _subtitles;
234 sorted.sort (SubtitleSorter ());
236 string const xmlns = standard == SMPTE ? "dcst" : "";
238 /* XXX: script, underlined not supported */
240 optional<string> font;
245 float aspect_adjust = 1.0;
246 Effect effect = NONE;
247 Colour effect_colour;
251 Time last_fade_up_time;
252 Time last_fade_down_time;
254 xmlpp::Element* font_element = 0;
255 xmlpp::Element* subtitle_element = 0;
257 BOOST_FOREACH (SubtitleString const & i, sorted) {
259 /* We will start a new <Font>...</Font> whenever some font property changes.
260 I suppose we should really make an optimal hierarchy of <Font> tags, but
264 bool const font_changed =
266 italic != i.italic() ||
268 colour != i.colour() ||
270 fabs (aspect_adjust - i.aspect_adjust()) > ASPECT_ADJUST_EPSILON ||
271 effect != i.effect() ||
272 effect_colour != i.effect_colour();
276 italic = i.italic ();
278 colour = i.colour ();
280 aspect_adjust = i.aspect_adjust ();
281 effect = i.effect ();
282 effect_colour = i.effect_colour ();
285 if (!font_element || font_changed) {
286 font_element = root->add_child ("Font", xmlns);
288 if (standard == SMPTE) {
289 font_element->set_attribute ("ID", font.get ());
291 font_element->set_attribute ("Id", font.get ());
294 font_element->set_attribute ("Italic", italic ? "yes" : "no");
295 font_element->set_attribute ("Color", colour.to_argb_string());
296 font_element->set_attribute ("Size", raw_convert<string> (size));
297 if (fabs (aspect_adjust - 1.0) > ASPECT_ADJUST_EPSILON) {
298 font_element->set_attribute ("AspectAdjust", raw_convert<string> (aspect_adjust));
300 font_element->set_attribute ("Effect", effect_to_string (effect));
301 font_element->set_attribute ("EffectColor", effect_colour.to_argb_string());
302 font_element->set_attribute ("Script", "normal");
303 if (standard == SMPTE) {
304 font_element->set_attribute ("Underline", "no");
306 font_element->set_attribute ("Underlined", "no");
308 font_element->set_attribute ("Weight", bold ? "bold" : "normal");
311 if (!subtitle_element || font_changed ||
312 (last_in != i.in() ||
313 last_out != i.out() ||
314 last_fade_up_time != i.fade_up_time() ||
315 last_fade_down_time != i.fade_down_time()
318 subtitle_element = font_element->add_child ("Subtitle", xmlns);
319 subtitle_element->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
320 subtitle_element->set_attribute ("TimeIn", i.in().rebase(time_code_rate).as_string(standard));
321 subtitle_element->set_attribute ("TimeOut", i.out().rebase(time_code_rate).as_string(standard));
322 if (standard == SMPTE) {
323 subtitle_element->set_attribute ("FadeUpTime", i.fade_up_time().rebase(time_code_rate).as_string(standard));
324 subtitle_element->set_attribute ("FadeDownTime", i.fade_down_time().rebase(time_code_rate).as_string(standard));
326 subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i.fade_up_time().as_editable_units(time_code_rate)));
327 subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i.fade_down_time().as_editable_units(time_code_rate)));
332 last_fade_up_time = i.fade_up_time ();
333 last_fade_down_time = i.fade_down_time ();
336 xmlpp::Element* text = subtitle_element->add_child ("Text", xmlns);
338 if (i.h_align() != HALIGN_CENTER) {
339 if (standard == SMPTE) {
340 text->set_attribute ("Halign", halign_to_string (i.h_align ()));
342 text->set_attribute ("HAlign", halign_to_string (i.h_align ()));
346 if (i.h_position() > ALIGN_EPSILON) {
347 if (standard == SMPTE) {
348 text->set_attribute ("Hposition", raw_convert<string> (i.h_position() * 100, 6));
350 text->set_attribute ("HPosition", raw_convert<string> (i.h_position() * 100, 6));
354 if (standard == SMPTE) {
355 text->set_attribute ("Valign", valign_to_string (i.v_align()));
357 text->set_attribute ("VAlign", valign_to_string (i.v_align()));
360 if (i.v_position() > ALIGN_EPSILON) {
361 if (standard == SMPTE) {
362 text->set_attribute ("Vposition", raw_convert<string> (i.v_position() * 100, 6));
364 text->set_attribute ("VPosition", raw_convert<string> (i.v_position() * 100, 6));
367 if (standard == SMPTE) {
368 text->set_attribute ("Vposition", "0");
370 text->set_attribute ("VPosition", "0");
374 /* Interop only supports "horizontal" or "vertical" for direction, so only write this
377 if (i.direction() != DIRECTION_LTR && standard == SMPTE) {
378 text->set_attribute ("Direction", direction_to_string (i.direction ()));
381 text->add_child_text (i.text());
386 SubtitleAsset::fonts_with_load_ids () const
388 map<string, Data> out;
389 BOOST_FOREACH (Font const & i, _fonts) {
390 out[i.load_id] = i.data;