ec7d12948e5a483692774a53a8ae58b57741c319
[libdcp.git] / src / text_node.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/text.cc
22  *  @brief TextNode class for parsing subtitle XML.
23  */
24
25 #include "text_node.h"
26 #include "xml.h"
27 #include "font_node.h"
28 #include <libcxml/cxml.h>
29 #include <boost/foreach.hpp>
30
31 using std::string;
32 using std::list;
33 using boost::shared_ptr;
34 using boost::optional;
35 using namespace dcp;
36
37 /** Read a &lt;Text&gt; node from a subtitle XML file, noting its contents
38  *  in this object's member variables.
39  *  @param node Node to read.
40  */
41 TextNode::TextNode (boost::shared_ptr<const cxml::Node> node, int tcr, string font_id_attribute)
42         : h_position (0)
43         , h_align (HALIGN_CENTER)
44         , v_position (0)
45         , v_align (VALIGN_CENTER)
46         , direction (DIRECTION_LTR)
47 {
48         text = node->content ();
49
50         optional<float> hp = node->optional_number_attribute<float> ("HPosition");
51         if (!hp) {
52                 hp = node->optional_number_attribute<float> ("Hposition");
53         }
54         if (hp) {
55                 h_position = hp.get () / 100;
56         }
57
58         optional<string> ha = node->optional_string_attribute ("HAlign");
59         if (!ha) {
60                 ha = node->optional_string_attribute ("Halign");
61         }
62         if (ha) {
63                 h_align = string_to_halign (ha.get ());
64         }
65
66         optional<float> vp = node->optional_number_attribute<float> ("VPosition");
67         if (!vp) {
68                 vp = node->optional_number_attribute<float> ("Vposition");
69         }
70         if (vp) {
71                 v_position = vp.get () / 100;
72         }
73
74         optional<string> va = node->optional_string_attribute ("VAlign");
75         if (!va) {
76                 va = node->optional_string_attribute ("Valign");
77         }
78         if (va) {
79                 v_align = string_to_valign (va.get ());
80         }
81
82         optional<string> d = node->optional_string_attribute ("Direction");
83         if (d) {
84                 direction = string_to_direction (d.get ());
85         }
86
87         list<cxml::NodePtr> f = node->node_children ("Font");
88         BOOST_FOREACH (cxml::NodePtr& i, f) {
89                 font_nodes.push_back (shared_ptr<FontNode> (new FontNode (i, tcr, font_id_attribute)));
90         }
91 }