2 Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic 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 DCP-o-matic 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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "caption_content.h"
23 #include "exceptions.h"
26 #include <dcp/raw_convert.h>
27 #include <libcxml/cxml.h>
28 #include <libxml++/libxml++.h>
29 #include <boost/foreach.hpp>
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40 using boost::optional;
41 using dcp::raw_convert;
43 int const CaptionContentProperty::X_OFFSET = 500;
44 int const CaptionContentProperty::Y_OFFSET = 501;
45 int const CaptionContentProperty::X_SCALE = 502;
46 int const CaptionContentProperty::Y_SCALE = 503;
47 int const CaptionContentProperty::USE = 504;
48 int const CaptionContentProperty::BURN = 505;
49 int const CaptionContentProperty::LANGUAGE = 506;
50 int const CaptionContentProperty::FONTS = 507;
51 int const CaptionContentProperty::COLOUR = 508;
52 int const CaptionContentProperty::EFFECT = 509;
53 int const CaptionContentProperty::EFFECT_COLOUR = 510;
54 int const CaptionContentProperty::LINE_SPACING = 511;
55 int const CaptionContentProperty::FADE_IN = 512;
56 int const CaptionContentProperty::FADE_OUT = 513;
57 int const CaptionContentProperty::OUTLINE_WIDTH = 514;
58 int const CaptionContentProperty::TYPE = 515;
60 CaptionContent::CaptionContent (Content* parent)
61 : ContentPart (parent)
70 , _type (CAPTION_OPEN)
75 shared_ptr<CaptionContent>
76 CaptionContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
79 /* With old metadata FFmpeg content has the subtitle-related tags even with no
80 subtitle streams, so check for that.
82 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
83 return shared_ptr<CaptionContent> ();
86 /* Otherwise we can drop through to the newer logic */
89 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
90 return shared_ptr<CaptionContent> ();
93 return shared_ptr<CaptionContent> (new CaptionContent (parent, node, version));
96 CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int version)
97 : ContentPart (parent)
104 , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
105 , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (2))
106 , _type (CAPTION_OPEN)
109 _use = node->bool_child ("UseSubtitles");
110 _burn = node->bool_child ("BurnSubtitles");
114 _x_offset = node->number_child<double> ("SubtitleXOffset");
115 _y_offset = node->number_child<double> ("SubtitleYOffset");
117 _y_offset = node->number_child<double> ("SubtitleOffset");
120 if (node->optional_bool_child("Outline").get_value_or(false)) {
121 _effect = dcp::BORDER;
122 } else if (node->optional_bool_child("Shadow").get_value_or(false)) {
123 _effect = dcp::SHADOW;
128 optional<string> effect = node->optional_string_child("Effect");
130 if (*effect == "none") {
132 } else if (*effect == "outline") {
133 _effect = dcp::BORDER;
134 } else if (*effect == "shadow") {
135 _effect = dcp::SHADOW;
140 _x_scale = node->number_child<double> ("SubtitleXScale");
141 _y_scale = node->number_child<double> ("SubtitleYScale");
143 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
146 optional<int> r = node->optional_number_child<int>("Red");
147 optional<int> g = node->optional_number_child<int>("Green");
148 optional<int> b = node->optional_number_child<int>("Blue");
150 _colour = dcp::Colour (*r, *g, *b);
154 optional<int> er = node->optional_number_child<int>("EffectRed");
155 optional<int> eg = node->optional_number_child<int>("EffectGreen");
156 optional<int> eb = node->optional_number_child<int>("EffectBlue");
157 if (er && eg && eb) {
158 _effect_colour = dcp::Colour (*er, *eg, *eb);
161 _effect_colour = dcp::Colour (
162 node->optional_number_child<int>("OutlineRed").get_value_or(255),
163 node->optional_number_child<int>("OutlineGreen").get_value_or(255),
164 node->optional_number_child<int>("OutlineBlue").get_value_or(255)
168 optional<Frame> fi = node->optional_number_child<Frame>("SubtitleFadeIn");
170 _fade_in = ContentTime (*fi);
172 optional<Frame> fo = node->optional_number_child<Frame>("SubtitleFadeOut");
174 _fade_out = ContentTime (*fo);
177 _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
179 list<cxml::NodePtr> fonts = node->node_children ("Font");
180 for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
181 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
186 _type = string_to_caption_type (node->optional_string_child("CaptionType").get_value_or("open"));
190 /** _mutex must not be held on entry */
192 CaptionContent::as_xml (xmlpp::Node* root) const
194 boost::mutex::scoped_lock lm (_mutex);
196 root->add_child("UseSubtitles")->add_child_text (_use ? "1" : "0");
197 root->add_child("BurnSubtitles")->add_child_text (_burn ? "1" : "0");
198 root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
199 root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
200 root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
201 root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
202 root->add_child("SubtitleLanguage")->add_child_text (_language);
204 root->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
205 root->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
206 root->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
211 root->add_child("Effect")->add_child_text("none");
214 root->add_child("Effect")->add_child_text("outline");
217 root->add_child("Effect")->add_child_text("shadow");
221 if (_effect_colour) {
222 root->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
223 root->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
224 root->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
226 root->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
228 root->add_child("SubtitleFadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
231 root->add_child("SubtitleFadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
233 root->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
235 for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
236 (*i)->as_xml (root->add_child("Font"));
239 root->add_child("CaptionType")->add_child_text (caption_type_to_string(_type));
243 CaptionContent::identifier () const
245 string s = raw_convert<string> (x_scale())
246 + "_" + raw_convert<string> (y_scale())
247 + "_" + raw_convert<string> (x_offset())
248 + "_" + raw_convert<string> (y_offset())
249 + "_" + raw_convert<string> (line_spacing())
250 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
251 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
252 + "_" + raw_convert<string> (outline_width())
253 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
254 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::NONE)))
255 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string());
257 /* XXX: I suppose really _fonts shouldn't be in here, since not all
258 types of subtitle content involve fonts.
260 BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
261 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
262 s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
266 /* The language is for metadata only, and doesn't affect
267 how this content looks.
274 CaptionContent::add_font (shared_ptr<Font> font)
276 _fonts.push_back (font);
281 CaptionContent::connect_to_fonts ()
283 BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
287 _font_connections.clear ();
289 BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
290 _font_connections.push_back (i->Changed.connect (boost::bind (&CaptionContent::font_changed, this)));
295 CaptionContent::font_changed ()
297 _parent->signal_changed (CaptionContentProperty::FONTS);
301 CaptionContent::set_colour (dcp::Colour colour)
303 maybe_set (_colour, colour, CaptionContentProperty::COLOUR);
307 CaptionContent::unset_colour ()
309 maybe_set (_colour, optional<dcp::Colour>(), CaptionContentProperty::COLOUR);
313 CaptionContent::set_effect (dcp::Effect e)
315 maybe_set (_effect, e, CaptionContentProperty::EFFECT);
319 CaptionContent::unset_effect ()
321 maybe_set (_effect, optional<dcp::Effect>(), CaptionContentProperty::EFFECT);
325 CaptionContent::set_effect_colour (dcp::Colour colour)
327 maybe_set (_effect_colour, colour, CaptionContentProperty::EFFECT_COLOUR);
331 CaptionContent::unset_effect_colour ()
333 maybe_set (_effect_colour, optional<dcp::Colour>(), CaptionContentProperty::EFFECT_COLOUR);
337 CaptionContent::set_use (bool u)
339 maybe_set (_use, u, CaptionContentProperty::USE);
343 CaptionContent::set_burn (bool b)
345 maybe_set (_burn, b, CaptionContentProperty::BURN);
349 CaptionContent::set_x_offset (double o)
351 maybe_set (_x_offset, o, CaptionContentProperty::X_OFFSET);
355 CaptionContent::set_y_offset (double o)
357 maybe_set (_y_offset, o, CaptionContentProperty::Y_OFFSET);
361 CaptionContent::set_x_scale (double s)
363 maybe_set (_x_scale, s, CaptionContentProperty::X_SCALE);
367 CaptionContent::set_y_scale (double s)
369 maybe_set (_y_scale, s, CaptionContentProperty::Y_SCALE);
373 CaptionContent::set_language (string language)
375 maybe_set (_language, language, CaptionContentProperty::LANGUAGE);
379 CaptionContent::set_line_spacing (double s)
381 maybe_set (_line_spacing, s, CaptionContentProperty::LINE_SPACING);
385 CaptionContent::set_fade_in (ContentTime t)
387 maybe_set (_fade_in, t, CaptionContentProperty::FADE_IN);
391 CaptionContent::unset_fade_in ()
393 maybe_set (_fade_in, optional<ContentTime>(), CaptionContentProperty::FADE_IN);
397 CaptionContent::set_fade_out (ContentTime t)
399 maybe_set (_fade_out, t, CaptionContentProperty::FADE_OUT);
403 CaptionContent::unset_fade_out ()
405 maybe_set (_fade_out, optional<ContentTime>(), CaptionContentProperty::FADE_OUT);
409 CaptionContent::set_type (CaptionType type)
411 maybe_set (_type, type, CaptionContentProperty::TYPE);
415 CaptionContent::set_outline_width (int w)
417 maybe_set (_outline_width, w, CaptionContentProperty::OUTLINE_WIDTH);
421 CaptionContent::take_settings_from (shared_ptr<const CaptionContent> c)
425 set_x_offset (c->_x_offset);
426 set_y_offset (c->_y_offset);
427 set_x_scale (c->_x_scale);
428 set_y_scale (c->_y_scale);
429 maybe_set (_fonts, c->_fonts, CaptionContentProperty::FONTS);
431 set_colour (*c->_colour);
436 set_effect (*c->_effect);
438 if (c->_effect_colour) {
439 set_effect_colour (*c->_effect_colour);
441 unset_effect_colour ();
443 set_line_spacing (c->_line_spacing);
445 set_fade_in (*c->_fade_in);
448 set_fade_out (*c->_fade_out);
450 set_outline_width (c->_outline_width);