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;
50 DCPText (shared_ptr<const cxml::Node> node)
53 text = node->content ();
54 v_position = node->number_attribute<float> ("VPosition");
55 optional<string> v = node->optional_string_attribute ("VAlign");
57 v_align = string_to_vertical_reference (v.get ());
60 font_nodes = type_children<DCPFont> (node, "Font");
64 VerticalReference v_align;
66 shared_ptr<DCPFont> foo;
67 list<shared_ptr<DCPFont> > font_nodes;
74 DCPSubtitle (shared_ptr<const cxml::Node> node)
76 in = MetricTime (time (node->string_attribute ("TimeIn")));
77 out = MetricTime (time (node->string_attribute ("TimeOut")));
78 font_nodes = type_children<DCPFont> (node, "Font");
79 text_nodes = type_children<DCPText> (node, "Text");
80 fade_up_time = fade_time (node, "FadeUpTime");
81 fade_down_time = fade_time (node, "FadeDownTime");
86 MetricTime fade_up_time;
87 MetricTime fade_down_time;
88 list<shared_ptr<DCPFont> > font_nodes;
89 list<shared_ptr<DCPText> > text_nodes;
92 static MetricTime time (std::string time)
95 split (b, time, is_any_of (":"));
97 boost::throw_exception (XMLError ("unrecognised time specification"));
100 return MetricTime (lexical_cast<int>(b[0]), lexical_cast<int> (b[1]), lexical_cast<int> (b[2]), lexical_cast<int> (b[3]) * 4);
103 MetricTime fade_time (shared_ptr<const cxml::Node> node, string name)
105 string const u = node->optional_string_attribute (name).get_value_or ("");
109 t = MetricTime (0, 0, 0, 80);
110 } else if (u.find (":") != string::npos) {
113 t = MetricTime (0, 0, 0, lexical_cast<int>(u) * 4);
116 if (t > MetricTime (0, 0, 8, 0)) {
117 t = MetricTime (0, 0, 8, 0);
131 DCPFont (shared_ptr<const cxml::Node> node)
133 text = node->content ();
135 id = node->optional_string_attribute ("Id").get_value_or ("");
136 size = node->optional_number_attribute<int64_t> ("Size").get_value_or (0);
137 italic = node->optional_bool_attribute ("Italic");
138 optional<string> c = node->optional_string_attribute ("Color");
140 colour = Colour (c.get ());
142 optional<string> const e = node->optional_string_attribute ("Effect");
144 effect = string_to_effect (e.get ());
146 c = node->optional_string_attribute ( "EffectColor");
148 effect_colour = Colour (c.get ());
150 subtitle_nodes = type_children<DCPSubtitle> (node, "Subtitle");
151 font_nodes = type_children<DCPFont> (node, "Font");
152 text_nodes = type_children<DCPText> (node, "Text");
155 DCPFont (list<shared_ptr<DCPFont> > const & font_nodes)
158 , colour ("FFFFFFFF")
159 , effect_colour ("FFFFFFFF")
161 for (list<shared_ptr<DCPFont> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
162 if (!(*i)->id.empty ()) {
165 if ((*i)->size != 0) {
169 italic = (*i)->italic.get ();
172 colour = (*i)->colour.get ();
175 effect = (*i)->effect.get ();
177 if ((*i)->effect_colour) {
178 effect_colour = (*i)->effect_colour.get ();
186 optional<bool> italic;
187 optional<Colour> colour;
188 optional<Effect> effect;
189 optional<Colour> effect_colour;
191 list<shared_ptr<DCPSubtitle> > subtitle_nodes;
192 list<shared_ptr<DCPFont> > font_nodes;
193 list<shared_ptr<DCPText> > text_nodes;
200 DCPLoadFont (shared_ptr<const cxml::Node> node)
202 id = node->string_attribute ("Id");
203 uri = node->string_attribute ("URI");
210 struct DCPReader::ParseState {
211 list<shared_ptr<DCPFont> > font_nodes;
212 list<shared_ptr<DCPText> > text_nodes;
213 list<shared_ptr<DCPSubtitle> > subtitle_nodes;
218 /** @param s A string.
219 * @return true if the string contains only space, newline or tab characters, or is empty.
222 empty_or_white_space (string s)
224 for (size_t i = 0; i < s.length(); ++i) {
225 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
234 DCPReader::font_id_to_name (string id) const
236 list<shared_ptr<DCPLoadFont> >::const_iterator i = _load_font_nodes.begin();
237 while (i != _load_font_nodes.end() && (*i)->id != id) {
241 if (i == _load_font_nodes.end ()) {
245 if ((*i)->uri == "arial.ttf" || (*i)->uri == "Arial.ttf") {
252 DCPReader::DCPReader (istream& in)
254 shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
255 xml->read_stream (in);
257 xml->ignore_child ("SubtitleID");
258 xml->ignore_child ("MovieTitle");
259 xml->ignore_child ("ReelNumber");
260 xml->ignore_child ("Language");
262 list<shared_ptr<DCPFont> > font_nodes = type_children<DCPFont> (xml, "Font");
263 _load_font_nodes = type_children<DCPLoadFont> (xml, "LoadFont");
265 /* Now make Subtitle objects to represent the raw XML nodes
269 ParseState parse_state;
270 examine_font_nodes (xml, font_nodes, parse_state);
274 DCPReader::examine_font_nodes (
275 shared_ptr<const cxml::Node> xml,
276 list<shared_ptr<DCPFont> > const & font_nodes,
277 ParseState& parse_state
280 for (list<shared_ptr<DCPFont> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
282 parse_state.font_nodes.push_back (*i);
283 maybe_add_subtitle ((*i)->text, parse_state);
285 for (list<shared_ptr<DCPSubtitle> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
286 parse_state.subtitle_nodes.push_back (*j);
287 examine_text_nodes (xml, (*j)->text_nodes, parse_state);
288 examine_font_nodes (xml, (*j)->font_nodes, parse_state);
289 parse_state.subtitle_nodes.pop_back ();
292 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
293 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
295 parse_state.font_nodes.pop_back ();
300 DCPReader::examine_text_nodes (
301 shared_ptr<const cxml::Node> xml,
302 list<shared_ptr<DCPText> > const & text_nodes,
303 ParseState& parse_state
306 for (list<shared_ptr<DCPText> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
307 parse_state.text_nodes.push_back (*i);
308 maybe_add_subtitle ((*i)->text, parse_state);
309 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
310 parse_state.text_nodes.pop_back ();
315 DCPReader::maybe_add_subtitle (string text, ParseState& parse_state)
317 if (empty_or_white_space (text)) {
321 if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
325 DCPFont effective_font (parse_state.font_nodes);
326 DCPText effective_text (*parse_state.text_nodes.back ());
327 DCPSubtitle effective_subtitle (*parse_state.subtitle_nodes.back ());
331 sub.vertical_position.proportional = float (effective_text.v_position) / 100;
332 sub.vertical_position.reference = effective_text.v_align;
333 sub.from.set_metric (effective_subtitle.in);
334 sub.to.set_metric (effective_subtitle.out);
335 sub.fade_up = effective_subtitle.fade_up_time;
336 sub.fade_down = effective_subtitle.fade_down_time;
339 sub.font = font_id_to_name (effective_font.id);
340 sub.font_size.set_proportional (float (effective_font.size) / (72 * 11));
341 sub.effect = effective_font.effect;
342 sub.effect_colour = effective_font.effect_colour;
343 sub.colour = effective_font.colour.get ();
344 sub.italic = effective_font.italic.get ();
346 _subs.push_back (sub);