Split up into RawSubtitle and Subtitle, with collect(). Hopefully cleaner.
[libsub.git] / src / dcp_reader.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "dcp_reader.h"
21 #include "vertical_reference.h"
22 #include "xml.h"
23 #include <libcxml/cxml.h>
24 #include <boost/algorithm/string.hpp>
25 #include <boost/lexical_cast.hpp>
26
27 using std::string;
28 using std::list;
29 using std::vector;
30 using std::istream;
31 using std::cout;
32 using boost::shared_ptr;
33 using boost::optional;
34 using boost::lexical_cast;
35 using boost::is_any_of;
36 using namespace sub;
37
38 namespace sub {
39
40 class DCPFont;
41
42 class DCPText
43 {
44 public:
45         DCPText ()
46                 : v_position (0)
47                 , v_align (TOP)
48         {}
49         
50         DCPText (shared_ptr<const cxml::Node> node)
51                 : v_align (CENTRE)
52         {
53                 text = node->content ();
54                 v_position = node->number_attribute<float> ("VPosition");
55                 optional<string> v = node->optional_string_attribute ("VAlign");
56                 if (v) {
57                         v_align = string_to_vertical_reference (v.get ());
58                 }
59                 
60                 font_nodes = type_children<DCPFont> (node, "Font");
61         }
62
63         float v_position;
64         VerticalReference v_align;
65         string text;
66         shared_ptr<DCPFont> foo;
67         list<shared_ptr<DCPFont> > font_nodes;
68 };
69
70 class DCPSubtitle 
71 {
72 public:
73         DCPSubtitle () {}
74         DCPSubtitle (shared_ptr<const cxml::Node> node)
75         {
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");
82         }
83
84         MetricTime in;
85         MetricTime out;
86         MetricTime fade_up_time;
87         MetricTime fade_down_time;
88         list<shared_ptr<DCPFont> > font_nodes;
89         list<shared_ptr<DCPText> > text_nodes;
90
91 private:
92         static MetricTime time (std::string time)
93         {
94                 vector<string> b;
95                 split (b, time, is_any_of (":"));
96                 if (b.size() != 4) {
97                         boost::throw_exception (XMLError ("unrecognised time specification"));
98                 }
99
100                 return MetricTime (lexical_cast<int>(b[0]), lexical_cast<int> (b[1]), lexical_cast<int> (b[2]), lexical_cast<int> (b[3]) * 4);
101         }
102         
103         MetricTime fade_time (shared_ptr<const cxml::Node> node, string name)
104         {
105                 string const u = node->optional_string_attribute (name).get_value_or ("");
106                 MetricTime t;
107                 
108                 if (u.empty ()) {
109                         t = MetricTime (0, 0, 0, 80);
110                 } else if (u.find (":") != string::npos) {
111                         t = time (u);
112                 } else {
113                         t = MetricTime (0, 0, 0, lexical_cast<int>(u) * 4);
114                 }
115                 
116                 if (t > MetricTime (0, 0, 8, 0)) {
117                         t = MetricTime (0, 0, 8, 0);
118                 }
119                 
120                 return t;
121         }
122 };
123
124 class DCPFont 
125 {
126 public:
127         DCPFont ()
128                 : size (0)
129         {}
130         
131         DCPFont (shared_ptr<const cxml::Node> node)
132         {
133                 text = node->content ();
134                 
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");
139                 if (c) {
140                         colour = Colour (c.get ());
141                 }
142                 optional<string> const e = node->optional_string_attribute ("Effect");
143                 if (e) {
144                         effect = string_to_effect (e.get ());
145                 }
146                 c = node->optional_string_attribute ( "EffectColor");
147                 if (c) {
148                         effect_colour = Colour (c.get ());
149                 }
150                 subtitle_nodes = type_children<DCPSubtitle> (node, "Subtitle");
151                 font_nodes = type_children<DCPFont> (node, "Font");
152                 text_nodes = type_children<DCPText> (node, "Text");
153         }
154         
155         DCPFont (list<shared_ptr<DCPFont> > const & font_nodes)
156                 : size (0)
157                 , italic (false)
158                 , colour ("FFFFFFFF")
159                 , effect_colour ("FFFFFFFF")
160         {
161                 for (list<shared_ptr<DCPFont> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
162                         if (!(*i)->id.empty ()) {
163                                 id = (*i)->id;
164                         }
165                         if ((*i)->size != 0) {
166                                 size = (*i)->size;
167                         }
168                         if ((*i)->italic) {
169                                 italic = (*i)->italic.get ();
170                         }
171                         if ((*i)->colour) {
172                                 colour = (*i)->colour.get ();
173                         }
174                         if ((*i)->effect) {
175                                 effect = (*i)->effect.get ();
176                         }
177                         if ((*i)->effect_colour) {
178                                 effect_colour = (*i)->effect_colour.get ();
179                         }
180                 }
181         }
182
183         string text;
184         string id;
185         int size;
186         optional<bool> italic;
187         optional<Colour> colour;
188         optional<Effect> effect;
189         optional<Colour> effect_colour;
190         
191         list<shared_ptr<DCPSubtitle> > subtitle_nodes;
192         list<shared_ptr<DCPFont> > font_nodes;
193         list<shared_ptr<DCPText> > text_nodes;
194 };
195
196 class DCPLoadFont 
197 {
198 public:
199         DCPLoadFont () {}
200         DCPLoadFont (shared_ptr<const cxml::Node> node)
201         {
202                 id = node->string_attribute ("Id");
203                 uri = node->string_attribute ("URI");
204         }
205
206         string id;
207         string uri;
208 };
209
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;
214 };
215
216 }
217
218 /** @param s A string.
219  *  @return true if the string contains only space, newline or tab characters, or is empty.
220  */
221 static bool
222 empty_or_white_space (string s)
223 {
224         for (size_t i = 0; i < s.length(); ++i) {
225                 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
226                         return false;
227                 }
228         }
229
230         return true;
231 }
232
233 string
234 DCPReader::font_id_to_name (string id) const
235 {
236         list<shared_ptr<DCPLoadFont> >::const_iterator i = _load_font_nodes.begin();
237         while (i != _load_font_nodes.end() && (*i)->id != id) {
238                 ++i;
239         }
240
241         if (i == _load_font_nodes.end ()) {
242                 return "";
243         }
244
245         if ((*i)->uri == "arial.ttf" || (*i)->uri == "Arial.ttf") {
246                 return "Arial";
247         }
248
249         return (*i)->uri;
250 }
251
252 DCPReader::DCPReader (istream& in)
253 {
254         shared_ptr<cxml::Document> xml (new cxml::Document ("DCSubtitle"));
255         xml->read_stream (in);
256
257         xml->ignore_child ("SubtitleID");
258         xml->ignore_child ("MovieTitle");
259         xml->ignore_child ("ReelNumber");
260         xml->ignore_child ("Language");
261
262         list<shared_ptr<DCPFont> > font_nodes = type_children<DCPFont> (xml, "Font");
263         _load_font_nodes = type_children<DCPLoadFont> (xml, "LoadFont");
264         
265         /* Now make Subtitle objects to represent the raw XML nodes
266            in a sane way.
267         */
268
269         ParseState parse_state;
270         examine_font_nodes (xml, font_nodes, parse_state);
271 }
272
273 void
274 DCPReader::examine_font_nodes (
275         shared_ptr<const cxml::Node> xml,
276         list<shared_ptr<DCPFont> > const & font_nodes,
277         ParseState& parse_state
278         )
279 {
280         for (list<shared_ptr<DCPFont> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
281
282                 parse_state.font_nodes.push_back (*i);
283                 maybe_add_subtitle ((*i)->text, parse_state);
284
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 ();
290                 }
291         
292                 examine_font_nodes (xml, (*i)->font_nodes, parse_state);
293                 examine_text_nodes (xml, (*i)->text_nodes, parse_state);
294                 
295                 parse_state.font_nodes.pop_back ();
296         }
297 }
298
299 void
300 DCPReader::examine_text_nodes (
301         shared_ptr<const cxml::Node> xml,
302         list<shared_ptr<DCPText> > const & text_nodes,
303         ParseState& parse_state
304         )
305 {
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 ();
311         }
312 }
313
314 void
315 DCPReader::maybe_add_subtitle (string text, ParseState& parse_state)
316 {
317         if (empty_or_white_space (text)) {
318                 return;
319         }
320         
321         if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
322                 return;
323         }
324
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 ());
328
329         RawSubtitle sub;
330
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;
337                 
338         sub.text = text;
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 ();
345
346         _subs.push_back (sub);
347 }