Merge branch '1.0' of git.carlh.net:git/libdcp into 1.0
[libdcp.git] / src / font_node.cc
1 /*
2     Copyright (C) 2012-2016 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     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #include "types.h"
35 #include "raw_convert.h"
36 #include "font_node.h"
37 #include "xml.h"
38 #include "text_node.h"
39 #include <libcxml/cxml.h>
40 #include <boost/foreach.hpp>
41
42 using std::string;
43 using std::list;
44 using boost::shared_ptr;
45 using boost::optional;
46 using namespace dcp;
47
48 FontNode::FontNode (cxml::ConstNodePtr node, optional<int> tcr, Standard standard)
49 {
50         text = node->content ();
51
52         if (standard == INTEROP) {
53                 id = node->optional_string_attribute ("Id");
54         } else {
55                 id = node->optional_string_attribute ("ID");
56         }
57         size = node->optional_number_attribute<int64_t> ("Size").get_value_or (0);
58         aspect_adjust = node->optional_number_attribute<float> ("AspectAdjust");
59         italic = node->optional_bool_attribute ("Italic");
60         bold = node->optional_string_attribute("Weight").get_value_or("normal") == "bold";
61         if (standard == INTEROP) {
62                 underline = node->optional_bool_attribute ("Underlined");
63         } else {
64                 underline = node->optional_bool_attribute ("Underline");
65         }
66         optional<string> c = node->optional_string_attribute ("Color");
67         if (c) {
68                 colour = Colour (c.get ());
69         }
70         optional<string> const e = node->optional_string_attribute ("Effect");
71         if (e) {
72                 effect = string_to_effect (e.get ());
73         }
74         c = node->optional_string_attribute ("EffectColor");
75         if (c) {
76                 effect_colour = Colour (c.get ());
77         }
78
79         list<cxml::NodePtr> s = node->node_children ("Subtitle");
80         BOOST_FOREACH (cxml::NodePtr& i, s) {
81                 subtitle_nodes.push_back (shared_ptr<SubtitleNode> (new SubtitleNode (i, tcr, standard)));
82         }
83
84         list<cxml::NodePtr> f = node->node_children ("Font");
85         BOOST_FOREACH (cxml::NodePtr& i, f) {
86                 font_nodes.push_back (shared_ptr<FontNode> (new FontNode (i, tcr, standard)));
87         }
88
89         list<cxml::NodePtr> t = node->node_children ("Text");
90         BOOST_FOREACH (cxml::NodePtr& i, t) {
91                 text_nodes.push_back (shared_ptr<TextNode> (new TextNode (i, tcr, standard)));
92         }
93 }
94
95 FontNode::FontNode (std::list<boost::shared_ptr<FontNode> > const & font_nodes)
96         : size (0)
97         , italic (false)
98         , bold (false)
99         , underline (false)
100         , colour ("FFFFFFFF")
101         , effect_colour ("FFFFFFFF")
102 {
103         for (list<shared_ptr<FontNode> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
104                 if ((*i)->id) {
105                         id = (*i)->id;
106                 }
107                 if ((*i)->size != 0) {
108                         size = (*i)->size;
109                 }
110                 if ((*i)->aspect_adjust) {
111                         aspect_adjust = (*i)->aspect_adjust.get ();
112                 }
113                 if ((*i)->italic) {
114                         italic = (*i)->italic.get ();
115                 }
116                 if ((*i)->bold) {
117                         bold = (*i)->bold.get ();
118                 }
119                 if ((*i)->underline) {
120                         underline = (*i)->underline.get ();
121                 }
122                 if ((*i)->colour) {
123                         colour = (*i)->colour.get ();
124                 }
125                 if ((*i)->effect) {
126                         effect = (*i)->effect.get ();
127                 }
128                 if ((*i)->effect_colour) {
129                         effect_colour = (*i)->effect_colour.get ();
130                 }
131         }
132 }