Use an enum class for Marker.
[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 "compose.hpp"
37 #include <cmath>
38
39 using std::string;
40 using std::map;
41 using std::shared_ptr;
42 using namespace dcp;
43
44 string
45 order::Context::xmlns () const
46 {
47         return standard == SMPTE ? "dcst" : "";
48 }
49
50 order::Font::Font (shared_ptr<SubtitleString> s, Standard standard)
51 {
52         if (s->font()) {
53                 if (standard == SMPTE) {
54                         _values["ID"] = s->font().get ();
55                 } else {
56                         _values["Id"] = s->font().get ();
57                 }
58         }
59         _values["Italic"] = s->italic() ? "yes" : "no";
60         _values["Color"] = s->colour().to_argb_string();
61         _values["Size"] = raw_convert<string> (s->size());
62         _values["AspectAdjust"] = raw_convert<string>(s->aspect_adjust(), 1, true);
63         _values["Effect"] = effect_to_string (s->effect());
64         _values["EffectColor"] = s->effect_colour().to_argb_string();
65         _values["Script"] = "normal";
66         if (standard == SMPTE) {
67                 _values["Underline"] = s->underline() ? "yes" : "no";
68         } else {
69                 _values["Underlined"] = s->underline() ? "yes" : "no";
70         }
71         _values["Weight"] = s->bold() ? "bold" : "normal";
72 }
73
74 xmlpp::Element*
75 order::Font::as_xml (xmlpp::Element* parent, Context& context) const
76 {
77         xmlpp::Element* e = parent->add_child ("Font", context.xmlns());
78         for (map<string, string>::const_iterator i = _values.begin(); i != _values.end(); ++i) {
79                 e->set_attribute (i->first, i->second);
80         }
81         return e;
82 }
83
84 /** Modify our values so that they contain only those that are common to us and
85  *  other.
86  */
87 void
88 order::Font::take_intersection (Font other)
89 {
90         map<string, string> inter;
91
92         for (map<string, string>::const_iterator i = other._values.begin(); i != other._values.end(); ++i) {
93                 map<string, string>::iterator t = _values.find (i->first);
94                 if (t != _values.end() && t->second == i->second) {
95                         inter.insert (*i);
96                 }
97         }
98
99         _values = inter;
100 }
101
102 /** Modify our values so that it contains only those keys that are not in other */
103 void
104 order::Font::take_difference (Font other)
105 {
106         map<string, string> diff;
107         for (map<string, string>::const_iterator i = _values.begin(); i != _values.end(); ++i) {
108                 if (other._values.find (i->first) == other._values.end ()) {
109                         diff.insert (*i);
110                 }
111         }
112
113         _values = diff;
114 }
115
116 bool
117 order::Font::empty () const
118 {
119         return _values.empty ();
120 }
121
122 xmlpp::Element*
123 order::Part::as_xml (xmlpp::Element* parent, Context &) const
124 {
125         return parent;
126 }
127
128 xmlpp::Element*
129 order::String::as_xml (xmlpp::Element* parent, Context &) const
130 {
131         parent->add_child_text (text);
132         return 0;
133 }
134
135 void
136 order::Part::write_xml (xmlpp::Element* parent, order::Context& context) const
137 {
138         if (!font.empty ()) {
139                 parent = font.as_xml (parent, context);
140         }
141
142         parent = as_xml (parent, context);
143
144         BOOST_FOREACH (std::shared_ptr<order::Part> i, children) {
145                 i->write_xml (parent, context);
146         }
147 }
148
149 static void
150 position_align (xmlpp::Element* e, order::Context& context, HAlign h_align, float h_position, VAlign v_align, float v_position)
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
189 xmlpp::Element*
190 order::Text::as_xml (xmlpp::Element* parent, Context& context) const
191 {
192         xmlpp::Element* e = parent->add_child ("Text", context.xmlns());
193
194         position_align (e, context, _h_align, _h_position, _v_align, _v_position);
195
196         /* Interop only supports "horizontal" or "vertical" for direction, so only write this
197            for SMPTE.
198         */
199         if (_direction != DIRECTION_LTR && context.standard == SMPTE) {
200                 e->set_attribute ("Direction", direction_to_string (_direction));
201         }
202
203         return e;
204 }
205
206 xmlpp::Element*
207 order::Subtitle::as_xml (xmlpp::Element* parent, Context& context) const
208 {
209         xmlpp::Element* e = parent->add_child ("Subtitle", context.xmlns());
210         e->set_attribute ("SpotNumber", raw_convert<string> (context.spot_number++));
211         e->set_attribute ("TimeIn", _in.rebase(context.time_code_rate).as_string(context.standard));
212         e->set_attribute ("TimeOut", _out.rebase(context.time_code_rate).as_string(context.standard));
213         if (context.standard == SMPTE) {
214                 e->set_attribute ("FadeUpTime", _fade_up.rebase(context.time_code_rate).as_string(context.standard));
215                 e->set_attribute ("FadeDownTime", _fade_down.rebase(context.time_code_rate).as_string(context.standard));
216         } else {
217                 e->set_attribute ("FadeUpTime", raw_convert<string> (_fade_up.as_editable_units(context.time_code_rate)));
218                 e->set_attribute ("FadeDownTime", raw_convert<string> (_fade_down.as_editable_units(context.time_code_rate)));
219         }
220         return e;
221 }
222
223 bool
224 order::Font::operator== (Font const & other) const
225 {
226         return _values == other._values;
227 }
228
229 void
230 order::Font::clear ()
231 {
232         _values.clear ();
233 }
234
235 xmlpp::Element *
236 order::Image::as_xml (xmlpp::Element* parent, Context& context) const
237 {
238         xmlpp::Element* e = parent->add_child ("Image", context.xmlns());
239
240         position_align (e, context, _h_align, _h_position, _v_align, _v_position);
241         if (context.standard == SMPTE) {
242                 e->add_child_text (_id);
243         } else {
244                 e->add_child_text (_id + ".png");
245         }
246
247         return e;
248 }