Fix missing HAlign/VAlign attributes in subtitles when their value is negative.
[libdcp.git] / src / subtitle_asset_internal.cc
1 /*
2     Copyright (C) 2012-2018 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 "subtitle_asset_internal.h"
35 #include "subtitle_string.h"
36 #include <cmath>
37
38 using std::string;
39 using std::map;
40 using namespace dcp;
41
42 string
43 order::Context::xmlns () const
44 {
45         return standard == SMPTE ? "dcst" : "";
46 }
47
48 order::Font::Font (SubtitleString const & s, Standard standard)
49 {
50         if (s.font()) {
51                 if (standard == SMPTE) {
52                         _values["ID"] = s.font().get ();
53                 } else {
54                         _values["Id"] = s.font().get ();
55                 }
56         }
57         _values["Italic"] = s.italic() ? "yes" : "no";
58         _values["Color"] = s.colour().to_argb_string();
59         _values["Size"] = raw_convert<string> (s.size());
60         _values["AspectAdjust"] = raw_convert<string>(s.aspect_adjust(), 1, true);
61         _values["Effect"] = effect_to_string (s.effect());
62         _values["EffectColor"] = s.effect_colour().to_argb_string();
63         _values["Script"] = "normal";
64         if (standard == SMPTE) {
65                 _values["Underline"] = s.underline() ? "yes" : "no";
66         } else {
67                 _values["Underlined"] = s.underline() ? "yes" : "no";
68         }
69         _values["Weight"] = s.bold() ? "bold" : "normal";
70 }
71
72 xmlpp::Element*
73 order::Font::as_xml (xmlpp::Element* parent, Context& context) const
74 {
75         xmlpp::Element* e = parent->add_child ("Font", context.xmlns());
76         for (map<string, string>::const_iterator i = _values.begin(); i != _values.end(); ++i) {
77                 e->set_attribute (i->first, i->second);
78         }
79         return e;
80 }
81
82 /** Modify our values so that they contain only those that are common to us and
83  *  other.
84  */
85 void
86 order::Font::take_intersection (Font other)
87 {
88         map<string, string> inter;
89
90         for (map<string, string>::const_iterator i = other._values.begin(); i != other._values.end(); ++i) {
91                 map<string, string>::iterator t = _values.find (i->first);
92                 if (t != _values.end() && t->second == i->second) {
93                         inter.insert (*i);
94                 }
95         }
96
97         _values = inter;
98 }
99
100 /** Modify our values so that it contains only those keys that are not in other */
101 void
102 order::Font::take_difference (Font other)
103 {
104         map<string, string> diff;
105         for (map<string, string>::const_iterator i = _values.begin(); i != _values.end(); ++i) {
106                 if (other._values.find (i->first) == other._values.end ()) {
107                         diff.insert (*i);
108                 }
109         }
110
111         _values = diff;
112 }
113
114 bool
115 order::Font::empty () const
116 {
117         return _values.empty ();
118 }
119
120 xmlpp::Element*
121 order::Part::as_xml (xmlpp::Element* parent, Context &) const
122 {
123         return parent;
124 }
125
126 xmlpp::Element*
127 order::String::as_xml (xmlpp::Element* parent, Context &) const
128 {
129         parent->add_child_text (text);
130         return 0;
131 }
132
133 void
134 order::Part::write_xml (xmlpp::Element* parent, order::Context& context) const
135 {
136         if (!font.empty ()) {
137                 parent = font.as_xml (parent, context);
138         }
139
140         parent = as_xml (parent, context);
141
142         BOOST_FOREACH (boost::shared_ptr<order::Part> i, children) {
143                 i->write_xml (parent, context);
144         }
145 }
146
147 xmlpp::Element*
148 order::Text::as_xml (xmlpp::Element* parent, Context& context) const
149 {
150         xmlpp::Element* e = parent->add_child ("Text", context.xmlns());
151
152         if (_h_align != HALIGN_CENTER) {
153                 if (context.standard == SMPTE) {
154                         e->set_attribute ("Halign", halign_to_string (_h_align));
155                 } else {
156                         e->set_attribute ("HAlign", halign_to_string (_h_align));
157                 }
158         }
159
160         if (fabs(_h_position) > ALIGN_EPSILON) {
161                 if (context.standard == SMPTE) {
162                         e->set_attribute ("Hposition", raw_convert<string> (_h_position * 100, 6));
163                 } else {
164                         e->set_attribute ("HPosition", raw_convert<string> (_h_position * 100, 6));
165                 }
166         }
167
168         if (context.standard == SMPTE) {
169                 e->set_attribute ("Valign", valign_to_string (_v_align));
170         } else {
171                 e->set_attribute ("VAlign", valign_to_string (_v_align));
172         }
173
174         if (fabs(_v_position) > ALIGN_EPSILON) {
175                 if (context.standard == SMPTE) {
176                         e->set_attribute ("Vposition", raw_convert<string> (_v_position * 100, 6));
177                 } else {
178                         e->set_attribute ("VPosition", raw_convert<string> (_v_position * 100, 6));
179                 }
180         } else {
181                 if (context.standard == SMPTE) {
182                         e->set_attribute ("Vposition", "0");
183                 } else {
184                         e->set_attribute ("VPosition", "0");
185                 }
186         }
187
188         /* Interop only supports "horizontal" or "vertical" for direction, so only write this
189            for SMPTE.
190         */
191         if (_direction != DIRECTION_LTR && context.standard == SMPTE) {
192                 e->set_attribute ("Direction", direction_to_string (_direction));
193         }
194
195         return e;
196 }
197
198 xmlpp::Element*
199 order::Subtitle::as_xml (xmlpp::Element* parent, Context& context) const
200 {
201         xmlpp::Element* e = parent->add_child ("Subtitle", context.xmlns());
202         e->set_attribute ("SpotNumber", raw_convert<string> (context.spot_number++));
203         e->set_attribute ("TimeIn", _in.rebase(context.time_code_rate).as_string(context.standard));
204         e->set_attribute ("TimeOut", _out.rebase(context.time_code_rate).as_string(context.standard));
205         if (context.standard == SMPTE) {
206                 e->set_attribute ("FadeUpTime", _fade_up.rebase(context.time_code_rate).as_string(context.standard));
207                 e->set_attribute ("FadeDownTime", _fade_down.rebase(context.time_code_rate).as_string(context.standard));
208         } else {
209                 e->set_attribute ("FadeUpTime", raw_convert<string> (_fade_up.as_editable_units(context.time_code_rate)));
210                 e->set_attribute ("FadeDownTime", raw_convert<string> (_fade_down.as_editable_units(context.time_code_rate)));
211         }
212         return e;
213 }
214
215 bool
216 order::Font::operator== (Font const & other) const
217 {
218         return _values == other._values;
219 }
220
221 void
222 order::Font::clear ()
223 {
224         _values.clear ();
225 }