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"));
189 CaptionContent::CaptionContent (Content* parent, vector<shared_ptr<Content> > c)
190 : ContentPart (parent)
192 shared_ptr<CaptionContent> ref = c[0]->caption;
193 DCPOMATIC_ASSERT (ref);
194 list<shared_ptr<Font> > ref_fonts = ref->fonts ();
196 for (size_t i = 1; i < c.size(); ++i) {
198 if (c[i]->caption->use() != ref->use()) {
199 throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
202 if (c[i]->caption->burn() != ref->burn()) {
203 throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
206 if (c[i]->caption->x_offset() != ref->x_offset()) {
207 throw JoinError (_("Content to be joined must have the same subtitle X offset."));
210 if (c[i]->caption->y_offset() != ref->y_offset()) {
211 throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
214 if (c[i]->caption->x_scale() != ref->x_scale()) {
215 throw JoinError (_("Content to be joined must have the same subtitle X scale."));
218 if (c[i]->caption->y_scale() != ref->y_scale()) {
219 throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
222 if (c[i]->caption->line_spacing() != ref->line_spacing()) {
223 throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
226 if ((c[i]->caption->fade_in() != ref->fade_in()) || (c[i]->caption->fade_out() != ref->fade_out())) {
227 throw JoinError (_("Content to be joined must have the same subtitle fades."));
230 if ((c[i]->caption->outline_width() != ref->outline_width())) {
231 throw JoinError (_("Content to be joined must have the same outline width."));
234 list<shared_ptr<Font> > fonts = c[i]->caption->fonts ();
235 if (fonts.size() != ref_fonts.size()) {
236 throw JoinError (_("Content to be joined must use the same fonts."));
239 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
240 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
242 while (j != ref_fonts.end ()) {
244 throw JoinError (_("Content to be joined must use the same fonts."));
252 _burn = ref->burn ();
253 _x_offset = ref->x_offset ();
254 _y_offset = ref->y_offset ();
255 _x_scale = ref->x_scale ();
256 _y_scale = ref->y_scale ();
257 _language = ref->language ();
259 _line_spacing = ref->line_spacing ();
260 _fade_in = ref->fade_in ();
261 _fade_out = ref->fade_out ();
262 _outline_width = ref->outline_width ();
267 /** _mutex must not be held on entry */
269 CaptionContent::as_xml (xmlpp::Node* root) const
271 boost::mutex::scoped_lock lm (_mutex);
273 root->add_child("UseSubtitles")->add_child_text (_use ? "1" : "0");
274 root->add_child("BurnSubtitles")->add_child_text (_burn ? "1" : "0");
275 root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
276 root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
277 root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
278 root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
279 root->add_child("SubtitleLanguage")->add_child_text (_language);
281 root->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
282 root->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
283 root->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
288 root->add_child("Effect")->add_child_text("none");
291 root->add_child("Effect")->add_child_text("outline");
294 root->add_child("Effect")->add_child_text("shadow");
298 if (_effect_colour) {
299 root->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
300 root->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
301 root->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
303 root->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
305 root->add_child("SubtitleFadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
308 root->add_child("SubtitleFadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
310 root->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
312 for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
313 (*i)->as_xml (root->add_child("Font"));
316 root->add_child("CaptionType")->add_child_text (caption_type_to_string(_type));
320 CaptionContent::identifier () const
322 string s = raw_convert<string> (x_scale())
323 + "_" + raw_convert<string> (y_scale())
324 + "_" + raw_convert<string> (x_offset())
325 + "_" + raw_convert<string> (y_offset())
326 + "_" + raw_convert<string> (line_spacing())
327 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
328 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
329 + "_" + raw_convert<string> (outline_width())
330 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
331 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::NONE)))
332 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string());
334 /* XXX: I suppose really _fonts shouldn't be in here, since not all
335 types of subtitle content involve fonts.
337 BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
338 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
339 s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
343 /* The language is for metadata only, and doesn't affect
344 how this content looks.
351 CaptionContent::add_font (shared_ptr<Font> font)
353 _fonts.push_back (font);
358 CaptionContent::connect_to_fonts ()
360 BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
364 _font_connections.clear ();
366 BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
367 _font_connections.push_back (i->Changed.connect (boost::bind (&CaptionContent::font_changed, this)));
372 CaptionContent::font_changed ()
374 _parent->signal_changed (CaptionContentProperty::FONTS);
378 CaptionContent::set_colour (dcp::Colour colour)
380 maybe_set (_colour, colour, CaptionContentProperty::COLOUR);
384 CaptionContent::unset_colour ()
386 maybe_set (_colour, optional<dcp::Colour>(), CaptionContentProperty::COLOUR);
390 CaptionContent::set_effect (dcp::Effect e)
392 maybe_set (_effect, e, CaptionContentProperty::EFFECT);
396 CaptionContent::unset_effect ()
398 maybe_set (_effect, optional<dcp::Effect>(), CaptionContentProperty::EFFECT);
402 CaptionContent::set_effect_colour (dcp::Colour colour)
404 maybe_set (_effect_colour, colour, CaptionContentProperty::EFFECT_COLOUR);
408 CaptionContent::unset_effect_colour ()
410 maybe_set (_effect_colour, optional<dcp::Colour>(), CaptionContentProperty::EFFECT_COLOUR);
414 CaptionContent::set_use (bool u)
416 maybe_set (_use, u, CaptionContentProperty::USE);
420 CaptionContent::set_burn (bool b)
422 maybe_set (_burn, b, CaptionContentProperty::BURN);
426 CaptionContent::set_x_offset (double o)
428 maybe_set (_x_offset, o, CaptionContentProperty::X_OFFSET);
432 CaptionContent::set_y_offset (double o)
434 maybe_set (_y_offset, o, CaptionContentProperty::Y_OFFSET);
438 CaptionContent::set_x_scale (double s)
440 maybe_set (_x_scale, s, CaptionContentProperty::X_SCALE);
444 CaptionContent::set_y_scale (double s)
446 maybe_set (_y_scale, s, CaptionContentProperty::Y_SCALE);
450 CaptionContent::set_language (string language)
452 maybe_set (_language, language, CaptionContentProperty::LANGUAGE);
456 CaptionContent::set_line_spacing (double s)
458 maybe_set (_line_spacing, s, CaptionContentProperty::LINE_SPACING);
462 CaptionContent::set_fade_in (ContentTime t)
464 maybe_set (_fade_in, t, CaptionContentProperty::FADE_IN);
468 CaptionContent::unset_fade_in ()
470 maybe_set (_fade_in, optional<ContentTime>(), CaptionContentProperty::FADE_IN);
474 CaptionContent::set_fade_out (ContentTime t)
476 maybe_set (_fade_out, t, CaptionContentProperty::FADE_OUT);
480 CaptionContent::unset_fade_out ()
482 maybe_set (_fade_out, optional<ContentTime>(), CaptionContentProperty::FADE_OUT);
486 CaptionContent::set_type (CaptionType type)
488 maybe_set (_type, type, CaptionContentProperty::TYPE);
492 CaptionContent::set_outline_width (int w)
494 maybe_set (_outline_width, w, CaptionContentProperty::OUTLINE_WIDTH);
498 CaptionContent::take_settings_from (shared_ptr<const CaptionContent> c)
502 set_x_offset (c->_x_offset);
503 set_y_offset (c->_y_offset);
504 set_x_scale (c->_x_scale);
505 set_y_scale (c->_y_scale);
506 maybe_set (_fonts, c->_fonts, CaptionContentProperty::FONTS);
508 set_colour (*c->_colour);
513 set_effect (*c->_effect);
515 if (c->_effect_colour) {
516 set_effect_colour (*c->_effect_colour);
518 unset_effect_colour ();
520 set_line_spacing (c->_line_spacing);
522 set_fade_in (*c->_fade_in);
525 set_fade_out (*c->_fade_out);
527 set_outline_width (c->_outline_width);