Fix handling of timing in SMPTE subtitles.
[libdcp.git] / src / subtitle.cc
1 /*
2     Copyright (C) 2012-2015 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 "subtitle.h"
21 #include "xml.h"
22 #include "font.h"
23 #include "text.h"
24 #include <libcxml/cxml.h>
25 #include <boost/lexical_cast.hpp>
26
27 using std::string;
28 using std::list;
29 using boost::optional;
30 using boost::shared_ptr;
31 using boost::lexical_cast;
32 using namespace dcp;
33
34 Subtitle::Subtitle (boost::shared_ptr<const cxml::Node> node, int tcr)
35 {
36         in = Time (node->string_attribute ("TimeIn"), tcr);
37         out = Time (node->string_attribute ("TimeOut"), tcr);
38
39         list<cxml::NodePtr> f = node->node_children ("Font");
40         for (list<cxml::NodePtr>::iterator i = f.begin(); i != f.end(); ++i) {
41                 font_nodes.push_back (shared_ptr<Font> (new Font (*i, tcr)));
42         }
43
44         list<cxml::NodePtr> t = node->node_children ("Text");
45         for (list<cxml::NodePtr>::iterator i = t.begin(); i != t.end(); ++i) {
46                 text_nodes.push_back (shared_ptr<Text> (new Text (*i, tcr)));
47         }
48         
49         fade_up_time = fade_time (node, "FadeUpTime", tcr);
50         fade_down_time = fade_time (node, "FadeDownTime", tcr);
51 }
52
53 Time
54 Subtitle::fade_time (shared_ptr<const cxml::Node> node, string name, int tcr)
55 {
56         string const u = node->optional_string_attribute (name).get_value_or ("");
57         Time t;
58         
59         if (u.empty ()) {
60                 t = Time (0, 0, 0, 20, 250);
61         } else if (u.find (":") != string::npos) {
62                 t = Time (u, tcr);
63         } else {
64                 t = Time (0, 0, 0, lexical_cast<int> (u), tcr);
65         }
66
67         if (t > Time (0, 0, 8, 0, 250)) {
68                 t = Time (0, 0, 8, 0, 250);
69         }
70
71         return t;
72 }