Note whether subtitle colour is forced or not.
[dcpomatic.git] / src / lib / subtitle_content.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "subtitle_content.h"
22 #include "util.h"
23 #include "exceptions.h"
24 #include "font.h"
25 #include "content.h"
26 #include <dcp/raw_convert.h>
27 #include <libcxml/cxml.h>
28 #include <libxml++/libxml++.h>
29 #include <boost/foreach.hpp>
30 #include <iostream>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::vector;
36 using std::cout;
37 using std::list;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40 using boost::optional;
41 using dcp::raw_convert;
42
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::OUTLINE = 509;
53 int const SubtitleContentProperty::SHADOW = 510;
54 int const SubtitleContentProperty::EFFECT_COLOUR = 511;
55 int const SubtitleContentProperty::LINE_SPACING = 512;
56 int const SubtitleContentProperty::FADE_IN = 513;
57 int const SubtitleContentProperty::FADE_OUT = 514;
58 int const SubtitleContentProperty::OUTLINE_WIDTH = 515;
59
60 SubtitleContent::SubtitleContent (Content* parent)
61         : ContentPart (parent)
62         , _use (false)
63         , _burn (false)
64         , _x_offset (0)
65         , _y_offset (0)
66         , _x_scale (1)
67         , _y_scale (1)
68         , _outline (false)
69         , _shadow (false)
70         , _effect_colour (0, 0, 0)
71         , _line_spacing (1)
72         , _outline_width (2)
73 {
74
75 }
76
77 shared_ptr<SubtitleContent>
78 SubtitleContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
79 {
80         if (version < 34) {
81                 /* With old metadata FFmpeg content has the subtitle-related tags even with no
82                    subtitle streams, so check for that.
83                 */
84                 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
85                         return shared_ptr<SubtitleContent> ();
86                 }
87
88                 /* Otherwise we can drop through to the newer logic */
89         }
90
91         if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
92                 return shared_ptr<SubtitleContent> ();
93         }
94
95         return shared_ptr<SubtitleContent> (new SubtitleContent (parent, node, version));
96 }
97
98 SubtitleContent::SubtitleContent (Content* parent, cxml::ConstNodePtr node, int version)
99         : ContentPart (parent)
100         , _use (false)
101         , _burn (false)
102         , _x_offset (0)
103         , _y_offset (0)
104         , _x_scale (1)
105         , _y_scale (1)
106         , _outline (node->optional_bool_child("Outline").get_value_or(false))
107         , _shadow (node->optional_bool_child("Shadow").get_value_or(false))
108         , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
109         , _fade_in (node->optional_number_child<Frame>("SubtitleFadeIn").get_value_or (0))
110         , _fade_out (node->optional_number_child<Frame>("SubtitleFadeOut").get_value_or (0))
111         , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (2))
112 {
113         if (version >= 32) {
114                 _use = node->bool_child ("UseSubtitles");
115                 _burn = node->bool_child ("BurnSubtitles");
116         }
117
118         if (version >= 7) {
119                 _x_offset = node->number_child<double> ("SubtitleXOffset");
120                 _y_offset = node->number_child<double> ("SubtitleYOffset");
121         } else {
122                 _y_offset = node->number_child<double> ("SubtitleOffset");
123         }
124
125         if (version >= 10) {
126                 _x_scale = node->number_child<double> ("SubtitleXScale");
127                 _y_scale = node->number_child<double> ("SubtitleYScale");
128         } else {
129                 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
130         }
131
132         optional<int> r = node->optional_number_child<int>("Red");
133         optional<int> g = node->optional_number_child<int>("Green");
134         optional<int> b = node->optional_number_child<int>("Blue");
135         if (r && g && b) {
136                 _colour = dcp::Colour (*r, *g, *b);
137         }
138
139         if (version >= 36) {
140                 _effect_colour = dcp::Colour (
141                         node->optional_number_child<int>("EffectRed").get_value_or(255),
142                         node->optional_number_child<int>("EffectGreen").get_value_or(255),
143                         node->optional_number_child<int>("EffectBlue").get_value_or(255)
144                         );
145         } else {
146                 _effect_colour = dcp::Colour (
147                         node->optional_number_child<int>("OutlineRed").get_value_or(255),
148                         node->optional_number_child<int>("OutlineGreen").get_value_or(255),
149                         node->optional_number_child<int>("OutlineBlue").get_value_or(255)
150                         );
151         }
152
153         _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
154
155         list<cxml::NodePtr> fonts = node->node_children ("Font");
156         for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
157                 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
158         }
159
160         connect_to_fonts ();
161 }
162
163 SubtitleContent::SubtitleContent (Content* parent, vector<shared_ptr<Content> > c)
164         : ContentPart (parent)
165 {
166         shared_ptr<SubtitleContent> ref = c[0]->subtitle;
167         DCPOMATIC_ASSERT (ref);
168         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
169
170         for (size_t i = 1; i < c.size(); ++i) {
171
172                 if (c[i]->subtitle->use() != ref->use()) {
173                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
174                 }
175
176                 if (c[i]->subtitle->burn() != ref->burn()) {
177                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
178                 }
179
180                 if (c[i]->subtitle->x_offset() != ref->x_offset()) {
181                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
182                 }
183
184                 if (c[i]->subtitle->y_offset() != ref->y_offset()) {
185                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
186                 }
187
188                 if (c[i]->subtitle->x_scale() != ref->x_scale()) {
189                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
190                 }
191
192                 if (c[i]->subtitle->y_scale() != ref->y_scale()) {
193                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
194                 }
195
196                 if (c[i]->subtitle->line_spacing() != ref->line_spacing()) {
197                         throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
198                 }
199
200                 if ((c[i]->subtitle->fade_in() != ref->fade_in()) || (c[i]->subtitle->fade_out() != ref->fade_out())) {
201                         throw JoinError (_("Content to be joined must have the same subtitle fades."));
202                 }
203
204                 if ((c[i]->subtitle->outline_width() != ref->outline_width())) {
205                         throw JoinError (_("Content to be joined must have the same outline width."));
206                 }
207
208                 list<shared_ptr<Font> > fonts = c[i]->subtitle->fonts ();
209                 if (fonts.size() != ref_fonts.size()) {
210                         throw JoinError (_("Content to be joined must use the same fonts."));
211                 }
212
213                 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
214                 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
215
216                 while (j != ref_fonts.end ()) {
217                         if (**j != **k) {
218                                 throw JoinError (_("Content to be joined must use the same fonts."));
219                         }
220                         ++j;
221                         ++k;
222                 }
223         }
224
225         _use = ref->use ();
226         _burn = ref->burn ();
227         _x_offset = ref->x_offset ();
228         _y_offset = ref->y_offset ();
229         _x_scale = ref->x_scale ();
230         _y_scale = ref->y_scale ();
231         _language = ref->language ();
232         _fonts = ref_fonts;
233         _line_spacing = ref->line_spacing ();
234         _fade_in = ref->fade_in ();
235         _fade_out = ref->fade_out ();
236         _outline_width = ref->outline_width ();
237
238         connect_to_fonts ();
239 }
240
241 /** _mutex must not be held on entry */
242 void
243 SubtitleContent::as_xml (xmlpp::Node* root) const
244 {
245         boost::mutex::scoped_lock lm (_mutex);
246
247         root->add_child("UseSubtitles")->add_child_text (_use ? "1" : "0");
248         root->add_child("BurnSubtitles")->add_child_text (_burn ? "1" : "0");
249         root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
250         root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
251         root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
252         root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
253         root->add_child("SubtitleLanguage")->add_child_text (_language);
254         if (_colour) {
255                 root->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
256                 root->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
257                 root->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
258         }
259         root->add_child("Outline")->add_child_text (_outline ? "1" : "0");
260         root->add_child("Shadow")->add_child_text (_shadow ? "1" : "0");
261         root->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour.r));
262         root->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour.g));
263         root->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour.b));
264         root->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
265         root->add_child("SubtitleFadeIn")->add_child_text (raw_convert<string> (_fade_in.get()));
266         root->add_child("SubtitleFadeOut")->add_child_text (raw_convert<string> (_fade_out.get()));
267         root->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
268
269         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
270                 (*i)->as_xml (root->add_child("Font"));
271         }
272 }
273
274 string
275 SubtitleContent::identifier () const
276 {
277         string s = raw_convert<string> (x_scale())
278                 + "_" + raw_convert<string> (y_scale())
279                 + "_" + raw_convert<string> (x_offset())
280                 + "_" + raw_convert<string> (y_offset())
281                 + "_" + raw_convert<string> (line_spacing())
282                 + "_" + raw_convert<string> (fade_in().get())
283                 + "_" + raw_convert<string> (fade_out().get())
284                 + "_" + raw_convert<string> (outline_width());
285
286         /* XXX: I suppose really _fonts shouldn't be in here, since not all
287            types of subtitle content involve fonts.
288         */
289         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
290                 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
291                         s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
292                 }
293         }
294
295         /* The language is for metadata only, and doesn't affect
296            how this content looks.
297         */
298
299         return s;
300 }
301
302 void
303 SubtitleContent::add_font (shared_ptr<Font> font)
304 {
305         _fonts.push_back (font);
306         connect_to_fonts ();
307 }
308
309 void
310 SubtitleContent::connect_to_fonts ()
311 {
312         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
313                 i.disconnect ();
314         }
315
316         _font_connections.clear ();
317
318         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
319                 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
320         }
321 }
322
323 void
324 SubtitleContent::font_changed ()
325 {
326         _parent->signal_changed (SubtitleContentProperty::FONTS);
327 }
328
329 void
330 SubtitleContent::set_colour (dcp::Colour colour)
331 {
332         maybe_set (_colour, colour, SubtitleContentProperty::COLOUR);
333 }
334
335 void
336 SubtitleContent::unset_colour ()
337 {
338         maybe_set (_colour, optional<dcp::Colour>(), SubtitleContentProperty::COLOUR);
339 }
340
341 void
342 SubtitleContent::set_outline (bool o)
343 {
344         maybe_set (_outline, o, SubtitleContentProperty::OUTLINE);
345 }
346
347 void
348 SubtitleContent::set_shadow (bool s)
349 {
350         maybe_set (_shadow, s, SubtitleContentProperty::SHADOW);
351 }
352
353 void
354 SubtitleContent::set_effect_colour (dcp::Colour colour)
355 {
356         maybe_set (_effect_colour, colour, SubtitleContentProperty::EFFECT_COLOUR);
357 }
358
359 void
360 SubtitleContent::set_use (bool u)
361 {
362         maybe_set (_use, u, SubtitleContentProperty::USE);
363 }
364
365 void
366 SubtitleContent::set_burn (bool b)
367 {
368         maybe_set (_burn, b, SubtitleContentProperty::BURN);
369 }
370
371 void
372 SubtitleContent::set_x_offset (double o)
373 {
374         maybe_set (_x_offset, o, SubtitleContentProperty::X_OFFSET);
375 }
376
377 void
378 SubtitleContent::set_y_offset (double o)
379 {
380         maybe_set (_y_offset, o, SubtitleContentProperty::Y_OFFSET);
381 }
382
383 void
384 SubtitleContent::set_x_scale (double s)
385 {
386         maybe_set (_x_scale, s, SubtitleContentProperty::X_SCALE);
387 }
388
389 void
390 SubtitleContent::set_y_scale (double s)
391 {
392         maybe_set (_y_scale, s, SubtitleContentProperty::Y_SCALE);
393 }
394
395 void
396 SubtitleContent::set_language (string language)
397 {
398         maybe_set (_language, language, SubtitleContentProperty::LANGUAGE);
399 }
400
401 void
402 SubtitleContent::set_line_spacing (double s)
403 {
404         maybe_set (_line_spacing, s, SubtitleContentProperty::LINE_SPACING);
405 }
406
407 void
408 SubtitleContent::set_fade_in (ContentTime t)
409 {
410         maybe_set (_fade_in, t, SubtitleContentProperty::FADE_IN);
411 }
412
413 void
414 SubtitleContent::set_fade_out (ContentTime t)
415 {
416         maybe_set (_fade_out, t, SubtitleContentProperty::FADE_OUT);
417 }
418
419 void
420 SubtitleContent::set_outline_width (int w)
421 {
422         maybe_set (_outline_width, w, SubtitleContentProperty::OUTLINE_WIDTH);
423 }
424
425 void
426 SubtitleContent::take_settings_from (shared_ptr<const SubtitleContent> c)
427 {
428         set_use (c->_use);
429         set_burn (c->_burn);
430         set_x_offset (c->_x_offset);
431         set_y_offset (c->_y_offset);
432         set_x_scale (c->_x_scale);
433         set_y_scale (c->_y_scale);
434         maybe_set (_fonts, c->_fonts, SubtitleContentProperty::FONTS);
435         if (c->_colour) {
436                 set_colour (*c->_colour);
437         } else {
438                 unset_colour ();
439         }
440         set_outline (c->_outline);
441         set_shadow (c->_shadow);
442         set_effect_colour (c->_effect_colour);
443         set_line_spacing (c->_line_spacing);
444         set_fade_in (c->_fade_in);
445         set_fade_out (c->_fade_out);
446         set_outline_width (c->_outline_width);
447 }