Arch build fix; whitespace.
[libsub.git] / src / dcp_reader.cc
1 /*
2     Copyright (C) 2014-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 "dcp_reader.h"
21 #include "vertical_reference.h"
22 #include "xml.h"
23 #include "util.h"
24 #include "dcp/font.h"
25 #include "dcp/text.h"
26 #include "dcp/subtitle.h"
27 #include <libcxml/cxml.h>
28 #include <libxml++/libxml++.h>
29 #include <iostream>
30
31 using std::list;
32 using std::cout;
33 using std::string;
34 using boost::shared_ptr;
35 using boost::optional;
36 using namespace sub;
37
38 void
39 DCPReader::parse_common (cxml::NodePtr root, optional<int> tcr)
40 {
41         _reel_number = root->string_child ("ReelNumber");
42         _language = root->string_child ("Language");
43
44         ParseState parse_state;
45         parse_node (root->node(), parse_state, tcr);
46 }
47
48 void
49 DCPReader::parse_node (xmlpp::Node const * node, ParseState& parse_state, optional<int> tcr)
50 {
51         xmlpp::Node::NodeList children = node->get_children ();
52         for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
53                 xmlpp::ContentNode const * c = dynamic_cast<xmlpp::ContentNode const *> (*i);
54                 if (c) {
55                         maybe_add_subtitle (c->get_content (), parse_state);
56                 }
57
58                 xmlpp::Element* e = dynamic_cast<xmlpp::Element *> (*i);
59                 if (e) {
60                         cxml::NodePtr n (new cxml::Node (e));
61                         if (n->name() == "Font") {
62                                 parse_state.font_nodes.push_back (shared_ptr<dcp::Font> (new dcp::Font (n)));
63                                 parse_node (e, parse_state, tcr);
64                                 parse_state.font_nodes.pop_back ();
65                         } else if (n->name() == "Text") {
66                                 parse_state.text_nodes.push_back (shared_ptr<dcp::Text> (new dcp::Text (n)));
67                                 parse_node (e, parse_state, tcr);
68                                 parse_state.text_nodes.pop_back ();
69                         } else if (n->name() == "Subtitle") {
70                                 parse_state.subtitle_nodes.push_back (shared_ptr<dcp::Subtitle> (new dcp::Subtitle (n, tcr)));
71                                 parse_node (e, parse_state, tcr);
72                                 parse_state.subtitle_nodes.pop_back ();
73                         } else if (n->name() == "SubtitleList") {
74                                 parse_node (e, parse_state, tcr);
75                         }
76                 }
77         }
78 }
79
80 void
81 DCPReader::maybe_add_subtitle (string text, ParseState const & parse_state)
82 {
83         if (empty_or_white_space (text)) {
84                 return;
85         }
86
87         if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
88                 return;
89         }
90
91         dcp::Font effective_font (parse_state.font_nodes);
92         dcp::Text effective_text (*parse_state.text_nodes.back ());
93         dcp::Subtitle effective_subtitle (*parse_state.subtitle_nodes.back ());
94
95         RawSubtitle rs;
96
97         rs.text = text;
98         rs.font = effective_font.id;
99         rs.font_size.set_proportional (float (effective_font.size) / (72 * 11));
100         rs.effect = effective_font.effect;
101         rs.effect_colour = effective_font.effect_colour;
102         rs.colour = effective_font.colour.get();
103         rs.bold = false;
104         rs.italic = effective_font.italic.get();
105         rs.underline = false;
106         rs.vertical_position.proportional = float (effective_text.v_position) / 100;
107         rs.vertical_position.reference = effective_text.v_align;
108         rs.from = effective_subtitle.in;
109         rs.to = effective_subtitle.out;
110         rs.fade_up = effective_subtitle.fade_up_time;
111         rs.fade_down = effective_subtitle.fade_down_time;
112
113         _subs.push_back (rs);
114 }