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