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