2 Copyright (C) 2013-2016 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"
24 #include "safe_stringstream.h"
26 #include "raw_convert.h"
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
30 #include <boost/foreach.hpp>
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
42 int const SubtitleContentProperty::X_OFFSET = 500;
43 int const SubtitleContentProperty::Y_OFFSET = 501;
44 int const SubtitleContentProperty::X_SCALE = 502;
45 int const SubtitleContentProperty::Y_SCALE = 503;
46 int const SubtitleContentProperty::USE = 504;
47 int const SubtitleContentProperty::BURN = 505;
48 int const SubtitleContentProperty::LANGUAGE = 506;
49 int const SubtitleContentProperty::FONTS = 507;
50 int const SubtitleContentProperty::COLOUR = 508;
51 int const SubtitleContentProperty::OUTLINE = 509;
52 int const SubtitleContentProperty::OUTLINE_COLOUR = 510;
54 SubtitleContent::SubtitleContent (Content* parent)
55 : ContentPart (parent)
62 , _colour (255, 255, 255)
64 , _outline_colour (0, 0, 0)
69 shared_ptr<SubtitleContent>
70 SubtitleContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
73 /* With old metadata FFmpeg content has the subtitle-related tags even with no
74 subtitle streams, so check for that.
76 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
77 return shared_ptr<SubtitleContent> ();
80 /* Otherwise we can drop through to the newer logic */
83 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
84 return shared_ptr<SubtitleContent> ();
87 return shared_ptr<SubtitleContent> (new SubtitleContent (parent, node, version));
90 SubtitleContent::SubtitleContent (Content* parent, cxml::ConstNodePtr node, int version)
91 : ContentPart (parent)
99 node->optional_number_child<int>("Red").get_value_or(255),
100 node->optional_number_child<int>("Green").get_value_or(255),
101 node->optional_number_child<int>("Blue").get_value_or(255)
103 , _outline (node->optional_bool_child("Outline").get_value_or(false))
105 node->optional_number_child<int>("OutlineRed").get_value_or(255),
106 node->optional_number_child<int>("OutlineGreen").get_value_or(255),
107 node->optional_number_child<int>("OutlineBlue").get_value_or(255)
111 _use = node->bool_child ("UseSubtitles");
112 _burn = node->bool_child ("BurnSubtitles");
116 _x_offset = node->number_child<double> ("SubtitleXOffset");
117 _y_offset = node->number_child<double> ("SubtitleYOffset");
119 _y_offset = node->number_child<double> ("SubtitleOffset");
123 _x_scale = node->number_child<double> ("SubtitleXScale");
124 _y_scale = node->number_child<double> ("SubtitleYScale");
126 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
129 _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
131 list<cxml::NodePtr> fonts = node->node_children ("Font");
132 for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
133 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
139 SubtitleContent::SubtitleContent (Content* parent, vector<shared_ptr<Content> > c)
140 : ContentPart (parent)
142 shared_ptr<SubtitleContent> ref = c[0]->subtitle;
143 DCPOMATIC_ASSERT (ref);
144 list<shared_ptr<Font> > ref_fonts = ref->fonts ();
146 for (size_t i = 1; i < c.size(); ++i) {
148 if (c[i]->subtitle->use() != ref->use()) {
149 throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
152 if (c[i]->subtitle->burn() != ref->burn()) {
153 throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
156 if (c[i]->subtitle->x_offset() != ref->x_offset()) {
157 throw JoinError (_("Content to be joined must have the same subtitle X offset."));
160 if (c[i]->subtitle->y_offset() != ref->y_offset()) {
161 throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
164 if (c[i]->subtitle->x_scale() != ref->x_scale()) {
165 throw JoinError (_("Content to be joined must have the same subtitle X scale."));
168 if (c[i]->subtitle->y_scale() != ref->y_scale()) {
169 throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
172 list<shared_ptr<Font> > fonts = c[i]->subtitle->fonts ();
173 if (fonts.size() != ref_fonts.size()) {
174 throw JoinError (_("Content to be joined must use the same fonts."));
177 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
178 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
180 while (j != ref_fonts.end ()) {
182 throw JoinError (_("Content to be joined must use the same fonts."));
190 _burn = ref->burn ();
191 _x_offset = ref->x_offset ();
192 _y_offset = ref->y_offset ();
193 _x_scale = ref->x_scale ();
194 _y_scale = ref->y_scale ();
195 _language = ref->language ();
201 /** _mutex must not be held on entry */
203 SubtitleContent::as_xml (xmlpp::Node* root) const
205 boost::mutex::scoped_lock lm (_mutex);
207 root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use));
208 root->add_child("BurnSubtitles")->add_child_text (raw_convert<string> (_burn));
209 root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
210 root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
211 root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
212 root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
213 root->add_child("SubtitleLanguage")->add_child_text (_language);
214 root->add_child("Red")->add_child_text (raw_convert<string> (_colour.r));
215 root->add_child("Green")->add_child_text (raw_convert<string> (_colour.g));
216 root->add_child("Blue")->add_child_text (raw_convert<string> (_colour.b));
217 root->add_child("Outline")->add_child_text (raw_convert<string> (_outline));
218 root->add_child("OutlineRed")->add_child_text (raw_convert<string> (_outline_colour.r));
219 root->add_child("OutlineGreen")->add_child_text (raw_convert<string> (_outline_colour.g));
220 root->add_child("OutlineBlue")->add_child_text (raw_convert<string> (_outline_colour.b));
222 for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
223 (*i)->as_xml (root->add_child("Font"));
228 SubtitleContent::identifier () const
231 s << raw_convert<string> (x_scale())
232 << "_" << raw_convert<string> (y_scale())
233 << "_" << raw_convert<string> (x_offset())
234 << "_" << raw_convert<string> (y_offset());
236 /* XXX: I suppose really _fonts shouldn't be in here, since not all
237 types of subtitle content involve fonts.
239 BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
240 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
241 s << "_" << f->file(static_cast<FontFiles::Variant>(i)).get_value_or ("Default");
245 /* The language is for metadata only, and doesn't affect
246 how this content looks.
253 SubtitleContent::add_font (shared_ptr<Font> font)
255 _fonts.push_back (font);
260 SubtitleContent::connect_to_fonts ()
262 BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
266 _font_connections.clear ();
268 BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
269 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
274 SubtitleContent::font_changed ()
276 _parent->signal_changed (SubtitleContentProperty::FONTS);
280 SubtitleContent::set_colour (dcp::Colour colour)
282 maybe_set (_colour, colour, SubtitleContentProperty::COLOUR);
286 SubtitleContent::set_outline (bool o)
288 maybe_set (_outline, o, SubtitleContentProperty::OUTLINE);
292 SubtitleContent::set_outline_colour (dcp::Colour colour)
294 maybe_set (_outline_colour, colour, SubtitleContentProperty::OUTLINE_COLOUR);
298 SubtitleContent::set_use (bool u)
300 maybe_set (_use, u, SubtitleContentProperty::USE);
304 SubtitleContent::set_burn (bool b)
306 maybe_set (_burn, b, SubtitleContentProperty::BURN);
310 SubtitleContent::set_x_offset (double o)
312 maybe_set (_x_offset, o, SubtitleContentProperty::X_OFFSET);
316 SubtitleContent::set_y_offset (double o)
318 maybe_set (_y_offset, o, SubtitleContentProperty::Y_OFFSET);
322 SubtitleContent::set_x_scale (double s)
324 maybe_set (_x_scale, s, SubtitleContentProperty::X_SCALE);
328 SubtitleContent::set_y_scale (double s)
330 maybe_set (_y_scale, s, SubtitleContentProperty::Y_SCALE);
334 SubtitleContent::set_language (string language)
336 maybe_set (_language, language, SubtitleContentProperty::LANGUAGE);