2 Copyright (C) 2014 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 "dcp_reader.h"
21 #include "vertical_reference.h"
23 #include <libcxml/cxml.h>
24 #include <boost/algorithm/string.hpp>
25 #include <boost/lexical_cast.hpp>
32 using boost::shared_ptr;
33 using boost::optional;
34 using boost::lexical_cast;
35 using boost::is_any_of;
43 * @brief A DCP subtitle <Text> node.
50 , v_align (TOP_OF_SCREEN)
53 DCPText (shared_ptr<const cxml::Node> node)
54 : v_align (CENTRE_OF_SCREEN)
56 text = node->content ();
57 v_position = node->number_attribute<float> ("VPosition");
58 optional<string> v = node->optional_string_attribute ("VAlign");
60 v_align = string_to_vertical_reference (v.get ());
63 font_nodes = type_children<DCPFont> (node, "Font");
67 VerticalReference v_align;
69 shared_ptr<DCPFont> foo;
70 list<shared_ptr<DCPFont> > font_nodes;
73 /** @class DCPSubtitle
74 * @brief A DCP subtitle <Subtitle> node.
80 DCPSubtitle (shared_ptr<const cxml::Node> node)
82 in = MetricTime (time (node->string_attribute ("TimeIn")));
83 out = MetricTime (time (node->string_attribute ("TimeOut")));
84 font_nodes = type_children<DCPFont> (node, "Font");
85 text_nodes = type_children<DCPText> (node, "Text");
86 fade_up_time = fade_time (node, "FadeUpTime");
87 fade_down_time = fade_time (node, "FadeDownTime");
92 MetricTime fade_up_time;
93 MetricTime fade_down_time;
94 list<shared_ptr<DCPFont> > font_nodes;
95 list<shared_ptr<DCPText> > text_nodes;
98 static MetricTime time (std::string time)
101 split (b, time, is_any_of (":"));
103 boost::throw_exception (XMLError ("unrecognised time specification"));
106 return MetricTime (lexical_cast<int>(b[0]), lexical_cast<int> (b[1]), lexical_cast<int> (b[2]), lexical_cast<int> (b[3]) * 4);
109 MetricTime fade_time (shared_ptr<const cxml::Node> node, string name)
111 string const u = node->optional_string_attribute (name).get_value_or ("");
115 t = MetricTime (0, 0, 0, 80);
116 } else if (u.find (":") != string::npos) {
119 t = MetricTime (0, 0, 0, lexical_cast<int>(u) * 4);
122 if (t > MetricTime (0, 0, 8, 0)) {
123 t = MetricTime (0, 0, 8, 0);
131 * @brief A DCP subtitle <Font> node.
140 DCPFont (shared_ptr<const cxml::Node> node)
142 text = node->content ();
144 id = node->optional_string_attribute ("Id").get_value_or ("");
145 size = node->optional_number_attribute<int64_t> ("Size").get_value_or (0);
146 italic = node->optional_bool_attribute ("Italic");
147 optional<string> c = node->optional_string_attribute ("Color");
149 colour = Colour (c.get ());
151 optional<string> const e = node->optional_string_attribute ("Effect");
153 effect = string_to_effect (e.get ());
155 c = node->optional_string_attribute ( "EffectColor");
157 effect_colour = Colour (c.get ());
159 subtitle_nodes = type_children<DCPSubtitle> (node, "Subtitle");
160 font_nodes = type_children<DCPFont> (node, "Font");
161 text_nodes = type_children<DCPText> (node, "Text");
164 DCPFont (list<shared_ptr<DCPFont> > const & font_nodes)
167 , colour ("FFFFFFFF")
168 , effect_colour ("FFFFFFFF")
170 for (list<shared_ptr<DCPFont> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
171 if (!(*i)->id.empty ()) {
174 if ((*i)->size != 0) {
178 italic = (*i)->italic.get ();
181 colour = (*i)->colour.get ();
184 effect = (*i)->effect.get ();
186 if ((*i)->effect_colour) {
187 effect_colour = (*i)->effect_colour.get ();
195 optional<bool> italic;
196 optional<Colour> colour;
197 optional<Effect> effect;
198 optional<Colour> effect_colour;
200 list<shared_ptr<DCPSubtitle> > subtitle_nodes;
201 list<shared_ptr<DCPFont> > font_nodes;
202 list<shared_ptr<DCPText> > text_nodes;
205 /** @class DCPLoadFont
206 * @brief A DCP subtitle <LoadFont> node.
212 DCPLoadFont (shared_ptr<const cxml::Node> node)
214 id = node->string_attribute ("Id");
215 uri = node->string_attribute ("URI");
222 /** @class DCPReader::ParseState
223 * @brief Holder of state for use while reading DCP subtitles.
225 struct DCPReader::ParseState {
226 list<shared_ptr<DCPFont> > font_nodes;
227 list<shared_ptr<DCPText> > text_nodes;
228 list<shared_ptr<DCPSubtitle> > subtitle_nodes;
233 /** @param s A string.
234 * @return true if the string contains only space, newline or tab characters, or is empty.
237 empty_or_white_space (string s)
239 for (size_t i = 0; i < s.length(); ++i) {
240 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
249 DCPReader::font_id_to_name (string id) const
251 list<shared_ptr<DCPLoadFont> >::const_iterator i = _load_font_nodes.begin();
252 while (i != _load_font_nodes.end() && (*i)->id != id) {
256 if (i == _load_font_nodes.end ()) {
260 if ((*i)->uri == "arial.ttf" || (*i)->uri == "Arial.ttf") {
268 * @brief A class to read DCP subtitles.
270 DCPReader::DCPReader (istream& in)
272 shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
273 xml->read_stream (in);
275 xml->ignore_child ("SubtitleID");
276 xml->ignore_child ("MovieTitle");
277 xml->ignore_child ("ReelNumber");
278 xml->ignore_child ("Language");
280 list<shared_ptr<DCPFont> > font_nodes = type_children<DCPFont> (xml, "Font");
281 _load_font_nodes = type_children<DCPLoadFont> (xml, "LoadFont");
283 /* Now make Subtitle objects to represent the raw XML nodes
287 ParseState parse_state;
288 examine_font_nodes (xml, font_nodes, parse_state);
292 DCPReader::examine_font_nodes (
293 shared_ptr<const cxml::Node> xml,
294 list<shared_ptr<DCPFont> > const & font_nodes,
295 ParseState& parse_state
298 for (list<shared_ptr<DCPFont> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
300 parse_state.font_nodes.push_back (*i);
301 maybe_add_subtitle ((*i)->text, parse_state);
303 for (list<shared_ptr<DCPSubtitle> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
304 parse_state.subtitle_nodes.push_back (*j);
305 examine_text_nodes (xml, (*j)->text_nodes, parse_state);
306 examine_font_nodes (xml, (*j)->font_nodes, parse_state);
307 parse_state.subtitle_nodes.pop_back ();
310 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
311 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
313 parse_state.font_nodes.pop_back ();
318 DCPReader::examine_text_nodes (
319 shared_ptr<const cxml::Node> xml,
320 list<shared_ptr<DCPText> > const & text_nodes,
321 ParseState& parse_state
324 for (list<shared_ptr<DCPText> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
325 parse_state.text_nodes.push_back (*i);
326 maybe_add_subtitle ((*i)->text, parse_state);
327 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
328 parse_state.text_nodes.pop_back ();
333 DCPReader::maybe_add_subtitle (string text, ParseState& parse_state)
335 if (empty_or_white_space (text)) {
339 if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
343 DCPFont effective_font (parse_state.font_nodes);
344 DCPText effective_text (*parse_state.text_nodes.back ());
345 DCPSubtitle effective_subtitle (*parse_state.subtitle_nodes.back ());
349 sub.vertical_position.proportional = float (effective_text.v_position) / 100;
350 sub.vertical_position.reference = effective_text.v_align;
351 sub.from.set_metric (effective_subtitle.in);
352 sub.to.set_metric (effective_subtitle.out);
353 sub.fade_up = effective_subtitle.fade_up_time;
354 sub.fade_down = effective_subtitle.fade_down_time;
357 sub.font = font_id_to_name (effective_font.id);
358 sub.font_size.set_proportional (float (effective_font.size) / (72 * 11));
359 sub.effect = effective_font.effect;
360 sub.effect_colour = effective_font.effect_colour;
361 sub.colour = effective_font.colour.get ();
362 sub.italic = effective_font.italic.get ();
364 _subs.push_back (sub);