2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
35 /** @file src/subtitle_asset_internal.cc
36 * @brief Internal SubtitleAsset helpers
40 #include "subtitle_asset_internal.h"
41 #include "subtitle_string.h"
42 #include "compose.hpp"
48 using std::shared_ptr;
53 order::Context::xmlns () const
55 return standard == Standard::SMPTE ? "dcst" : "";
59 order::Font::Font (shared_ptr<SubtitleString> s, Standard standard)
62 if (standard == Standard::SMPTE) {
63 _values["ID"] = s->font().get ();
65 _values["Id"] = s->font().get ();
68 _values["Italic"] = s->italic() ? "yes" : "no";
69 _values["Color"] = s->colour().to_argb_string();
70 _values["Size"] = raw_convert<string> (s->size());
71 _values["AspectAdjust"] = raw_convert<string>(s->aspect_adjust(), 1, true);
72 _values["Effect"] = effect_to_string (s->effect());
73 _values["EffectColor"] = s->effect_colour().to_argb_string();
74 _values["Script"] = "normal";
75 if (standard == Standard::SMPTE) {
76 _values["Underline"] = s->underline() ? "yes" : "no";
78 _values["Underlined"] = s->underline() ? "yes" : "no";
80 _values["Weight"] = s->bold() ? "bold" : "normal";
85 order::Font::as_xml (xmlpp::Element* parent, Context& context) const
87 xmlpp::Element* e = parent->add_child ("Font", context.xmlns());
88 for (map<string, string>::const_iterator i = _values.begin(); i != _values.end(); ++i) {
89 e->set_attribute (i->first, i->second);
95 /** Modify our values so that they contain only those that are common to us and
99 order::Font::take_intersection (Font other)
101 map<string, string> inter;
103 for (auto const& i: other._values) {
104 auto t = _values.find (i.first);
105 if (t != _values.end() && t->second == i.second) {
114 /** Modify our values so that it contains only those keys that are not in other */
116 order::Font::take_difference (Font other)
118 map<string, string> diff;
119 for (auto const& i: _values) {
120 if (other._values.find (i.first) == other._values.end()) {
130 order::Font::empty () const
132 return _values.empty ();
137 order::Part::as_xml (xmlpp::Element* parent, Context &) const
144 order::String::as_xml (xmlpp::Element* parent, Context &) const
146 parent->add_child_text (text);
152 order::Part::write_xml (xmlpp::Element* parent, order::Context& context) const
154 if (!font.empty ()) {
155 parent = font.as_xml (parent, context);
158 parent = as_xml (parent, context);
160 for (auto i: children) {
161 i->write_xml (parent, context);
167 position_align (xmlpp::Element* e, order::Context& context, HAlign h_align, float h_position, VAlign v_align, float v_position)
169 if (h_align != HAlign::CENTER) {
170 if (context.standard == Standard::SMPTE) {
171 e->set_attribute ("Halign", halign_to_string (h_align));
173 e->set_attribute ("HAlign", halign_to_string (h_align));
177 if (fabs(h_position) > ALIGN_EPSILON) {
178 if (context.standard == Standard::SMPTE) {
179 e->set_attribute ("Hposition", raw_convert<string> (h_position * 100, 6));
181 e->set_attribute ("HPosition", raw_convert<string> (h_position * 100, 6));
185 if (context.standard == Standard::SMPTE) {
186 e->set_attribute ("Valign", valign_to_string (v_align));
188 e->set_attribute ("VAlign", valign_to_string (v_align));
191 if (fabs(v_position) > ALIGN_EPSILON) {
192 if (context.standard == Standard::SMPTE) {
193 e->set_attribute ("Vposition", raw_convert<string> (v_position * 100, 6));
195 e->set_attribute ("VPosition", raw_convert<string> (v_position * 100, 6));
198 if (context.standard == Standard::SMPTE) {
199 e->set_attribute ("Vposition", "0");
201 e->set_attribute ("VPosition", "0");
208 order::Text::as_xml (xmlpp::Element* parent, Context& context) const
210 auto e = parent->add_child ("Text", context.xmlns());
212 position_align (e, context, _h_align, _h_position, _v_align, _v_position);
214 /* Interop only supports "horizontal" or "vertical" for direction, so only write this
217 if (_direction != Direction::LTR && context.standard == Standard::SMPTE) {
218 e->set_attribute ("Direction", direction_to_string (_direction));
226 order::Subtitle::as_xml (xmlpp::Element* parent, Context& context) const
228 auto e = parent->add_child ("Subtitle", context.xmlns());
229 e->set_attribute ("SpotNumber", raw_convert<string> (context.spot_number++));
230 e->set_attribute ("TimeIn", _in.rebase(context.time_code_rate).as_string(context.standard));
231 e->set_attribute ("TimeOut", _out.rebase(context.time_code_rate).as_string(context.standard));
232 if (context.standard == Standard::SMPTE) {
233 e->set_attribute ("FadeUpTime", _fade_up.rebase(context.time_code_rate).as_string(context.standard));
234 e->set_attribute ("FadeDownTime", _fade_down.rebase(context.time_code_rate).as_string(context.standard));
236 e->set_attribute ("FadeUpTime", raw_convert<string> (_fade_up.as_editable_units(context.time_code_rate)));
237 e->set_attribute ("FadeDownTime", raw_convert<string> (_fade_down.as_editable_units(context.time_code_rate)));
244 order::Font::operator== (Font const & other) const
246 return _values == other._values;
251 order::Font::clear ()
258 order::Image::as_xml (xmlpp::Element* parent, Context& context) const
260 auto e = parent->add_child ("Image", context.xmlns());
262 position_align (e, context, _h_align, _h_position, _v_align, _v_position);
263 if (context.standard == Standard::SMPTE) {
264 e->add_child_text (_id);
266 e->add_child_text (_id + ".png");