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 "subtitle_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 SubtitleContentProperty::X_OFFSET = 500;
44 int const SubtitleContentProperty::Y_OFFSET = 501;
45 int const SubtitleContentProperty::X_SCALE = 502;
46 int const SubtitleContentProperty::Y_SCALE = 503;
47 int const SubtitleContentProperty::USE = 504;
48 int const SubtitleContentProperty::BURN = 505;
49 int const SubtitleContentProperty::LANGUAGE = 506;
50 int const SubtitleContentProperty::FONTS = 507;
51 int const SubtitleContentProperty::COLOUR = 508;
52 int const SubtitleContentProperty::EFFECT = 509;
53 int const SubtitleContentProperty::EFFECT_COLOUR = 510;
54 int const SubtitleContentProperty::LINE_SPACING = 511;
55 int const SubtitleContentProperty::FADE_IN = 512;
56 int const SubtitleContentProperty::FADE_OUT = 513;
57 int const SubtitleContentProperty::OUTLINE_WIDTH = 514;
59 SubtitleContent::SubtitleContent (Content* parent)
60 : ContentPart (parent)
73 shared_ptr<SubtitleContent>
74 SubtitleContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
77 /* With old metadata FFmpeg content has the subtitle-related tags even with no
78 subtitle streams, so check for that.
80 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
81 return shared_ptr<SubtitleContent> ();
84 /* Otherwise we can drop through to the newer logic */
87 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
88 return shared_ptr<SubtitleContent> ();
91 return shared_ptr<SubtitleContent> (new SubtitleContent (parent, node, version));
94 SubtitleContent::SubtitleContent (Content* parent, cxml::ConstNodePtr node, int version)
95 : ContentPart (parent)
102 , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
103 , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (2))
106 _use = node->bool_child ("UseSubtitles");
107 _burn = node->bool_child ("BurnSubtitles");
111 _x_offset = node->number_child<double> ("SubtitleXOffset");
112 _y_offset = node->number_child<double> ("SubtitleYOffset");
114 _y_offset = node->number_child<double> ("SubtitleOffset");
117 if (node->optional_bool_child("Outline").get_value_or(false)) {
118 _effect = dcp::BORDER;
119 } else if (node->optional_bool_child("Shadow").get_value_or(false)) {
120 _effect = dcp::SHADOW;
125 optional<string> effect = node->optional_string_child("Effect");
127 if (*effect == "none") {
129 } else if (*effect == "outline") {
130 _effect = dcp::BORDER;
131 } else if (*effect == "shadow") {
132 _effect = dcp::SHADOW;
137 _x_scale = node->number_child<double> ("SubtitleXScale");
138 _y_scale = node->number_child<double> ("SubtitleYScale");
140 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
143 optional<int> r = node->optional_number_child<int>("Red");
144 optional<int> g = node->optional_number_child<int>("Green");
145 optional<int> b = node->optional_number_child<int>("Blue");
147 _colour = dcp::Colour (*r, *g, *b);
151 optional<int> er = node->optional_number_child<int>("EffectRed");
152 optional<int> eg = node->optional_number_child<int>("EffectGreen");
153 optional<int> eb = node->optional_number_child<int>("EffectBlue");
154 if (er && eg && eb) {
155 _effect_colour = dcp::Colour (*er, *eg, *eb);
158 _effect_colour = dcp::Colour (
159 node->optional_number_child<int>("OutlineRed").get_value_or(255),
160 node->optional_number_child<int>("OutlineGreen").get_value_or(255),
161 node->optional_number_child<int>("OutlineBlue").get_value_or(255)
165 optional<Frame> fi = node->optional_number_child<Frame>("SubtitleFadeIn");
167 _fade_in = ContentTime (*fi);
169 optional<Frame> fo = node->optional_number_child<Frame>("SubtitleFadeOut");
171 _fade_out = ContentTime (*fo);
174 _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
176 list<cxml::NodePtr> fonts = node->node_children ("Font");
177 for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
178 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
184 SubtitleContent::SubtitleContent (Content* parent, vector<shared_ptr<Content> > c)
185 : ContentPart (parent)
187 shared_ptr<SubtitleContent> ref = c[0]->subtitle;
188 DCPOMATIC_ASSERT (ref);
189 list<shared_ptr<Font> > ref_fonts = ref->fonts ();
191 for (size_t i = 1; i < c.size(); ++i) {
193 if (c[i]->subtitle->use() != ref->use()) {
194 throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
197 if (c[i]->subtitle->burn() != ref->burn()) {
198 throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
201 if (c[i]->subtitle->x_offset() != ref->x_offset()) {
202 throw JoinError (_("Content to be joined must have the same subtitle X offset."));
205 if (c[i]->subtitle->y_offset() != ref->y_offset()) {
206 throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
209 if (c[i]->subtitle->x_scale() != ref->x_scale()) {
210 throw JoinError (_("Content to be joined must have the same subtitle X scale."));
213 if (c[i]->subtitle->y_scale() != ref->y_scale()) {
214 throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
217 if (c[i]->subtitle->line_spacing() != ref->line_spacing()) {
218 throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
221 if ((c[i]->subtitle->fade_in() != ref->fade_in()) || (c[i]->subtitle->fade_out() != ref->fade_out())) {
222 throw JoinError (_("Content to be joined must have the same subtitle fades."));
225 if ((c[i]->subtitle->outline_width() != ref->outline_width())) {
226 throw JoinError (_("Content to be joined must have the same outline width."));
229 list<shared_ptr<Font> > fonts = c[i]->subtitle->fonts ();
230 if (fonts.size() != ref_fonts.size()) {
231 throw JoinError (_("Content to be joined must use the same fonts."));
234 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
235 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
237 while (j != ref_fonts.end ()) {
239 throw JoinError (_("Content to be joined must use the same fonts."));
247 _burn = ref->burn ();
248 _x_offset = ref->x_offset ();
249 _y_offset = ref->y_offset ();
250 _x_scale = ref->x_scale ();
251 _y_scale = ref->y_scale ();
252 _language = ref->language ();
254 _line_spacing = ref->line_spacing ();
255 _fade_in = ref->fade_in ();
256 _fade_out = ref->fade_out ();
257 _outline_width = ref->outline_width ();
262 /** _mutex must not be held on entry */
264 SubtitleContent::as_xml (xmlpp::Node* root) const
266 boost::mutex::scoped_lock lm (_mutex);
268 root->add_child("UseSubtitles")->add_child_text (_use ? "1" : "0");
269 root->add_child("BurnSubtitles")->add_child_text (_burn ? "1" : "0");
270 root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
271 root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
272 root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
273 root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
274 root->add_child("SubtitleLanguage")->add_child_text (_language);
276 root->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
277 root->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
278 root->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
283 root->add_child("none");
286 root->add_child("outline");
289 root->add_child("shadow");
293 if (_effect_colour) {
294 root->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
295 root->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
296 root->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
298 root->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
300 root->add_child("SubtitleFadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
303 root->add_child("SubtitleFadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
305 root->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
307 for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
308 (*i)->as_xml (root->add_child("Font"));
313 SubtitleContent::identifier () const
315 string s = raw_convert<string> (x_scale())
316 + "_" + raw_convert<string> (y_scale())
317 + "_" + raw_convert<string> (x_offset())
318 + "_" + raw_convert<string> (y_offset())
319 + "_" + raw_convert<string> (line_spacing())
320 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
321 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
322 + "_" + raw_convert<string> (outline_width());
324 /* XXX: I suppose really _fonts shouldn't be in here, since not all
325 types of subtitle content involve fonts.
327 BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
328 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
329 s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
333 /* The language is for metadata only, and doesn't affect
334 how this content looks.
341 SubtitleContent::add_font (shared_ptr<Font> font)
343 _fonts.push_back (font);
348 SubtitleContent::connect_to_fonts ()
350 BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
354 _font_connections.clear ();
356 BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
357 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
362 SubtitleContent::font_changed ()
364 _parent->signal_changed (SubtitleContentProperty::FONTS);
368 SubtitleContent::set_colour (dcp::Colour colour)
370 maybe_set (_colour, colour, SubtitleContentProperty::COLOUR);
374 SubtitleContent::unset_colour ()
376 maybe_set (_colour, optional<dcp::Colour>(), SubtitleContentProperty::COLOUR);
380 SubtitleContent::set_effect (dcp::Effect e)
382 maybe_set (_effect, e, SubtitleContentProperty::EFFECT);
386 SubtitleContent::unset_effect ()
388 maybe_set (_effect, optional<dcp::Effect>(), SubtitleContentProperty::EFFECT);
392 SubtitleContent::set_effect_colour (dcp::Colour colour)
394 maybe_set (_effect_colour, colour, SubtitleContentProperty::EFFECT_COLOUR);
398 SubtitleContent::unset_effect_colour ()
400 maybe_set (_effect_colour, optional<dcp::Colour>(), SubtitleContentProperty::EFFECT_COLOUR);
404 SubtitleContent::set_use (bool u)
406 maybe_set (_use, u, SubtitleContentProperty::USE);
410 SubtitleContent::set_burn (bool b)
412 maybe_set (_burn, b, SubtitleContentProperty::BURN);
416 SubtitleContent::set_x_offset (double o)
418 maybe_set (_x_offset, o, SubtitleContentProperty::X_OFFSET);
422 SubtitleContent::set_y_offset (double o)
424 maybe_set (_y_offset, o, SubtitleContentProperty::Y_OFFSET);
428 SubtitleContent::set_x_scale (double s)
430 maybe_set (_x_scale, s, SubtitleContentProperty::X_SCALE);
434 SubtitleContent::set_y_scale (double s)
436 maybe_set (_y_scale, s, SubtitleContentProperty::Y_SCALE);
440 SubtitleContent::set_language (string language)
442 maybe_set (_language, language, SubtitleContentProperty::LANGUAGE);
446 SubtitleContent::set_line_spacing (double s)
448 maybe_set (_line_spacing, s, SubtitleContentProperty::LINE_SPACING);
452 SubtitleContent::set_fade_in (ContentTime t)
454 maybe_set (_fade_in, t, SubtitleContentProperty::FADE_IN);
458 SubtitleContent::unset_fade_in ()
460 maybe_set (_fade_in, optional<ContentTime>(), SubtitleContentProperty::FADE_IN);
464 SubtitleContent::set_fade_out (ContentTime t)
466 maybe_set (_fade_out, t, SubtitleContentProperty::FADE_OUT);
470 SubtitleContent::unset_fade_out ()
472 maybe_set (_fade_out, optional<ContentTime>(), SubtitleContentProperty::FADE_OUT);
476 SubtitleContent::set_outline_width (int w)
478 maybe_set (_outline_width, w, SubtitleContentProperty::OUTLINE_WIDTH);
482 SubtitleContent::take_settings_from (shared_ptr<const SubtitleContent> c)
486 set_x_offset (c->_x_offset);
487 set_y_offset (c->_y_offset);
488 set_x_scale (c->_x_scale);
489 set_y_scale (c->_y_scale);
490 maybe_set (_fonts, c->_fonts, SubtitleContentProperty::FONTS);
492 set_colour (*c->_colour);
497 set_effect (*c->_effect);
499 if (c->_effect_colour) {
500 set_effect_colour (*c->_effect_colour);
502 unset_effect_colour ();
504 set_line_spacing (c->_line_spacing);
506 set_fade_in (*c->_fade_in);
509 set_fade_out (*c->_fade_out);
511 set_outline_width (c->_outline_width);