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 */
90 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
91 return shared_ptr<CaptionContent> ();
93 return shared_ptr<CaptionContent> (new CaptionContent (parent, node, version));
96 if (!node->node_child("Caption")) {
97 return shared_ptr<CaptionContent> ();
100 return shared_ptr<CaptionContent> (new CaptionContent (parent, node->node_child("Caption"), version));
103 CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int version)
104 : ContentPart (parent)
111 , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
112 , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (2))
113 , _type (CAPTION_OPEN)
116 _use = node->bool_child ("Use");
117 _burn = node->bool_child ("Burn");
118 } else if (version >= 32) {
119 _use = node->bool_child ("UseSubtitles");
120 _burn = node->bool_child ("BurnSubtitles");
124 _x_offset = node->number_child<double> ("XOffset");
125 _y_offset = node->number_child<double> ("YOffset");
126 } else if (version >= 7) {
127 _x_offset = node->number_child<double> ("SubtitleXOffset");
128 _y_offset = node->number_child<double> ("SubtitleYOffset");
130 _y_offset = node->number_child<double> ("SubtitleOffset");
133 if (node->optional_bool_child("Outline").get_value_or(false)) {
134 _effect = dcp::BORDER;
135 } else if (node->optional_bool_child("Shadow").get_value_or(false)) {
136 _effect = dcp::SHADOW;
141 optional<string> effect = node->optional_string_child("Effect");
143 if (*effect == "none") {
145 } else if (*effect == "outline") {
146 _effect = dcp::BORDER;
147 } else if (*effect == "shadow") {
148 _effect = dcp::SHADOW;
153 _x_scale = node->number_child<double> ("XScale");
154 _y_scale = node->number_child<double> ("YScale");
155 } else if (version >= 10) {
156 _x_scale = node->number_child<double> ("SubtitleXScale");
157 _y_scale = node->number_child<double> ("SubtitleYScale");
159 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
162 optional<int> r = node->optional_number_child<int>("Red");
163 optional<int> g = node->optional_number_child<int>("Green");
164 optional<int> b = node->optional_number_child<int>("Blue");
166 _colour = dcp::Colour (*r, *g, *b);
170 optional<int> er = node->optional_number_child<int>("EffectRed");
171 optional<int> eg = node->optional_number_child<int>("EffectGreen");
172 optional<int> eb = node->optional_number_child<int>("EffectBlue");
173 if (er && eg && eb) {
174 _effect_colour = dcp::Colour (*er, *eg, *eb);
177 _effect_colour = dcp::Colour (
178 node->optional_number_child<int>("OutlineRed").get_value_or(255),
179 node->optional_number_child<int>("OutlineGreen").get_value_or(255),
180 node->optional_number_child<int>("OutlineBlue").get_value_or(255)
186 fi = node->optional_number_child<Frame>("FadeIn");
188 fi = node->optional_number_child<Frame>("SubtitleFadeIn");
191 _fade_in = ContentTime (*fi);
196 fo = node->optional_number_child<Frame>("FadeOut");
198 fo = node->optional_number_child<Frame>("SubtitleFadeOut");
201 _fade_out = ContentTime (*fo);
205 _language = node->optional_string_child ("Language").get_value_or ("");
207 _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
210 list<cxml::NodePtr> fonts = node->node_children ("Font");
211 for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
212 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
217 _type = string_to_caption_type (node->optional_string_child("Type").get_value_or("open"));
221 /** _mutex must not be held on entry */
223 CaptionContent::as_xml (xmlpp::Node* root) const
225 boost::mutex::scoped_lock lm (_mutex);
227 xmlpp::Element* caption = root->add_child ("Caption");
229 caption->add_child("Use")->add_child_text (_use ? "1" : "0");
230 caption->add_child("Burn")->add_child_text (_burn ? "1" : "0");
231 caption->add_child("XOffset")->add_child_text (raw_convert<string> (_x_offset));
232 caption->add_child("YOffset")->add_child_text (raw_convert<string> (_y_offset));
233 caption->add_child("XScale")->add_child_text (raw_convert<string> (_x_scale));
234 caption->add_child("YScale")->add_child_text (raw_convert<string> (_y_scale));
235 caption->add_child("Language")->add_child_text (_language);
237 caption->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
238 caption->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
239 caption->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
244 caption->add_child("Effect")->add_child_text("none");
247 caption->add_child("Effect")->add_child_text("outline");
250 caption->add_child("Effect")->add_child_text("shadow");
254 if (_effect_colour) {
255 caption->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
256 caption->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
257 caption->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
259 caption->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
261 caption->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
264 caption->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
266 caption->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
268 for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
269 (*i)->as_xml (caption->add_child("Font"));
272 caption->add_child("Type")->add_child_text (caption_type_to_string(_type));
276 CaptionContent::identifier () const
278 string s = raw_convert<string> (x_scale())
279 + "_" + raw_convert<string> (y_scale())
280 + "_" + raw_convert<string> (x_offset())
281 + "_" + raw_convert<string> (y_offset())
282 + "_" + raw_convert<string> (line_spacing())
283 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
284 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
285 + "_" + raw_convert<string> (outline_width())
286 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
287 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::NONE)))
288 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string());
290 /* XXX: I suppose really _fonts shouldn't be in here, since not all
291 types of subtitle content involve fonts.
293 BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
294 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
295 s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
299 /* The language is for metadata only, and doesn't affect
300 how this content looks.
307 CaptionContent::add_font (shared_ptr<Font> font)
309 _fonts.push_back (font);
314 CaptionContent::connect_to_fonts ()
316 BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
320 _font_connections.clear ();
322 BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
323 _font_connections.push_back (i->Changed.connect (boost::bind (&CaptionContent::font_changed, this)));
328 CaptionContent::font_changed ()
330 _parent->signal_changed (CaptionContentProperty::FONTS);
334 CaptionContent::set_colour (dcp::Colour colour)
336 maybe_set (_colour, colour, CaptionContentProperty::COLOUR);
340 CaptionContent::unset_colour ()
342 maybe_set (_colour, optional<dcp::Colour>(), CaptionContentProperty::COLOUR);
346 CaptionContent::set_effect (dcp::Effect e)
348 maybe_set (_effect, e, CaptionContentProperty::EFFECT);
352 CaptionContent::unset_effect ()
354 maybe_set (_effect, optional<dcp::Effect>(), CaptionContentProperty::EFFECT);
358 CaptionContent::set_effect_colour (dcp::Colour colour)
360 maybe_set (_effect_colour, colour, CaptionContentProperty::EFFECT_COLOUR);
364 CaptionContent::unset_effect_colour ()
366 maybe_set (_effect_colour, optional<dcp::Colour>(), CaptionContentProperty::EFFECT_COLOUR);
370 CaptionContent::set_use (bool u)
372 maybe_set (_use, u, CaptionContentProperty::USE);
376 CaptionContent::set_burn (bool b)
378 maybe_set (_burn, b, CaptionContentProperty::BURN);
382 CaptionContent::set_x_offset (double o)
384 maybe_set (_x_offset, o, CaptionContentProperty::X_OFFSET);
388 CaptionContent::set_y_offset (double o)
390 maybe_set (_y_offset, o, CaptionContentProperty::Y_OFFSET);
394 CaptionContent::set_x_scale (double s)
396 maybe_set (_x_scale, s, CaptionContentProperty::X_SCALE);
400 CaptionContent::set_y_scale (double s)
402 maybe_set (_y_scale, s, CaptionContentProperty::Y_SCALE);
406 CaptionContent::set_language (string language)
408 maybe_set (_language, language, CaptionContentProperty::LANGUAGE);
412 CaptionContent::set_line_spacing (double s)
414 maybe_set (_line_spacing, s, CaptionContentProperty::LINE_SPACING);
418 CaptionContent::set_fade_in (ContentTime t)
420 maybe_set (_fade_in, t, CaptionContentProperty::FADE_IN);
424 CaptionContent::unset_fade_in ()
426 maybe_set (_fade_in, optional<ContentTime>(), CaptionContentProperty::FADE_IN);
430 CaptionContent::set_fade_out (ContentTime t)
432 maybe_set (_fade_out, t, CaptionContentProperty::FADE_OUT);
436 CaptionContent::unset_fade_out ()
438 maybe_set (_fade_out, optional<ContentTime>(), CaptionContentProperty::FADE_OUT);
442 CaptionContent::set_type (CaptionType type)
444 maybe_set (_type, type, CaptionContentProperty::TYPE);
448 CaptionContent::set_outline_width (int w)
450 maybe_set (_outline_width, w, CaptionContentProperty::OUTLINE_WIDTH);
454 CaptionContent::take_settings_from (shared_ptr<const CaptionContent> c)
458 set_x_offset (c->_x_offset);
459 set_y_offset (c->_y_offset);
460 set_x_scale (c->_x_scale);
461 set_y_scale (c->_y_scale);
462 maybe_set (_fonts, c->_fonts, CaptionContentProperty::FONTS);
464 set_colour (*c->_colour);
469 set_effect (*c->_effect);
471 if (c->_effect_colour) {
472 set_effect_colour (*c->_effect_colour);
474 unset_effect_colour ();
476 set_line_spacing (c->_line_spacing);
478 set_fade_in (*c->_fade_in);
481 set_fade_out (*c->_fade_out);
483 set_outline_width (c->_outline_width);