Update test data.
[libdcp.git] / src / parse / subtitle.cc
1 /*
2     Copyright (C) 2012-2013 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 <boost/shared_ptr.hpp>
21 #include <boost/optional.hpp>
22 #include "subtitle.h"
23 #include "../types.h"
24
25 using std::string;
26 using std::list;
27 using boost::shared_ptr;
28 using boost::optional;
29 using boost::lexical_cast;
30 using namespace libdcp;
31 using namespace libdcp::parse;
32
33 Font::Font (shared_ptr<const cxml::Node> node)
34 {
35         text = node->content ();
36         
37         id = node->optional_string_attribute ("Id").get_value_or ("");
38         size = node->optional_number_attribute<int64_t> ("Size").get_value_or (0);
39         italic = node->optional_bool_attribute ("Italic");
40         optional<string> c = node->optional_string_attribute ("Color");
41         if (c) {
42                 color = Color (c.get ());
43         }
44         optional<string> const e = node->optional_string_attribute ("Effect");
45         if (e) {
46                 effect = string_to_effect (e.get ());
47         }
48         c = node->optional_string_attribute ( "EffectColor");
49         if (c) {
50                 effect_color = Color (c.get ());
51         }
52         subtitle_nodes = type_children<Subtitle> (node, "Subtitle");
53         font_nodes = type_children<Font> (node, "Font");
54         text_nodes = type_children<Text> (node, "Text");
55 }
56
57 Font::Font (list<shared_ptr<Font> > const & font_nodes)
58         : size (0)
59         , italic (false)
60         , color ("FFFFFFFF")
61         , effect_color ("FFFFFFFF")
62 {
63         for (list<shared_ptr<Font> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
64                 if (!(*i)->id.empty ()) {
65                         id = (*i)->id;
66                 }
67                 if ((*i)->size != 0) {
68                         size = (*i)->size;
69                 }
70                 if ((*i)->italic) {
71                         italic = (*i)->italic.get ();
72                 }
73                 if ((*i)->color) {
74                         color = (*i)->color.get ();
75                 }
76                 if ((*i)->effect) {
77                         effect = (*i)->effect.get ();
78                 }
79                 if ((*i)->effect_color) {
80                         effect_color = (*i)->effect_color.get ();
81                 }
82         }
83 }
84
85 LoadFont::LoadFont (shared_ptr<const cxml::Node> node)
86 {
87         optional<string> x = node->optional_string_attribute ("Id");
88         if (!x) {
89                 x = node->optional_string_attribute ("ID");
90         }
91         id = x.get_value_or ("");
92                 
93         uri = node->optional_string_attribute ("URI");
94 }
95
96 Subtitle::Subtitle (shared_ptr<const cxml::Node> node)
97 {
98         in = Time (node->string_attribute ("TimeIn"));
99         out = Time (node->string_attribute ("TimeOut"));
100         font_nodes = type_children<Font> (node, "Font");
101         text_nodes = type_children<Text> (node, "Text");
102         fade_up_time = fade_time (node, "FadeUpTime");
103         fade_down_time = fade_time (node, "FadeDownTime");
104 }
105
106 Time
107 Subtitle::fade_time (shared_ptr<const cxml::Node> node, string name)
108 {
109         string const u = node->optional_string_attribute (name).get_value_or ("");
110         Time t;
111         
112         if (u.empty ()) {
113                 t = Time (0, 0, 0, 20);
114         } else if (u.find (":") != string::npos) {
115                 t = Time (u);
116         } else {
117                 t = Time (0, 0, 0, lexical_cast<int> (u));
118         }
119
120         if (t > Time (0, 0, 8, 0)) {
121                 t = Time (0, 0, 8, 0);
122         }
123
124         return t;
125 }
126
127 Text::Text (shared_ptr<const cxml::Node> node)
128         : v_align (CENTER)
129 {
130         /* Vertical position */
131         text = node->content ();
132         optional<float> x = node->optional_number_attribute<float> ("VPosition");
133         if (!x) {
134                 x = node->number_attribute<float> ("Vposition");
135         }
136         v_position = x.get ();
137
138         /* Vertical alignment */
139         optional<string> v = node->optional_string_attribute ("VAlign");
140         if (!v) {
141                 v = node->optional_string_attribute ("Valign");
142         }
143         if (v) {
144                 v_align = string_to_valign (v.get ());
145         }
146
147         font_nodes = type_children<Font> (node, "Font");
148 }
149