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