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");
212 /** @param s A string.
213 * @return true if the string contains only space, newline or tab characters, or is empty.
216 empty_or_white_space (string s)
218 for (size_t i = 0; i < s.length(); ++i) {
219 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
228 DCPReader::font_id_to_name (string id) const
230 list<shared_ptr<DCPLoadFont> >::const_iterator i = _load_font_nodes.begin();
231 while (i != _load_font_nodes.end() && (*i)->id != id) {
235 if (i == _load_font_nodes.end ()) {
239 if ((*i)->uri == "arial.ttf") {
246 DCPReader::DCPReader (istream& in)
248 shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
249 xml->read_stream (in);
251 xml->ignore_child ("SubtitleID");
252 xml->ignore_child ("MovieTitle");
253 xml->ignore_child ("ReelNumber");
254 xml->ignore_child ("Language");
256 list<shared_ptr<DCPFont> > font_nodes = type_children<DCPFont> (xml, "Font");
257 _load_font_nodes = type_children<DCPLoadFont> (xml, "LoadFont");
259 /* Now make Subtitle objects to represent the raw XML nodes
263 ParseState parse_state;
264 examine_font_nodes (xml, font_nodes, parse_state);
270 DCPReader::examine_font_nodes (
271 shared_ptr<const cxml::Node> xml,
272 list<shared_ptr<DCPFont> > const & font_nodes,
273 ParseState& parse_state
276 for (list<shared_ptr<DCPFont> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
278 parse_state.font_nodes.push_back (*i);
279 maybe_add_subtitle ((*i)->text, parse_state);
281 for (list<shared_ptr<DCPSubtitle> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
282 parse_state.subtitle_nodes.push_back (*j);
283 examine_text_nodes (xml, (*j)->text_nodes, parse_state);
284 examine_font_nodes (xml, (*j)->font_nodes, parse_state);
285 parse_state.subtitle_nodes.pop_back ();
288 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
289 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
291 parse_state.font_nodes.pop_back ();
296 DCPReader::examine_text_nodes (
297 shared_ptr<const cxml::Node> xml,
298 list<shared_ptr<DCPText> > const & text_nodes,
299 ParseState& parse_state
302 for (list<shared_ptr<DCPText> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
303 parse_state.text_nodes.push_back (*i);
304 maybe_add_subtitle ((*i)->text, parse_state);
305 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
306 parse_state.text_nodes.pop_back ();
311 DCPReader::maybe_add_subtitle (string text, ParseState const & parse_state)
313 if (empty_or_white_space (text)) {
317 if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
321 assert (!parse_state.text_nodes.empty ());
322 assert (!parse_state.subtitle_nodes.empty ());
324 DCPFont effective_font (parse_state.font_nodes);
325 DCPText effective_text (*parse_state.text_nodes.back ());
326 DCPSubtitle effective_subtitle (*parse_state.subtitle_nodes.back ());
330 s.font = font_id_to_name (effective_font.id);
331 s.font_size.proportional = float (effective_font.size) / (72 * 11);
332 s.vertical_position.proportional = float (effective_text.v_position) / 100;
333 s.vertical_position.reference = effective_text.v_align;
334 s.effect = effective_font.effect;
335 s.effect_colour = effective_font.effect_colour;
336 s.colour = effective_font.colour.get ();
337 s.italic = effective_font.italic.get ();
338 s.from.metric = effective_subtitle.in;
339 s.to.metric = effective_subtitle.out;
340 s.fade_up = effective_subtitle.fade_up_time;
341 s.fade_down = effective_subtitle.fade_down_time;