Clean up after previous commit.
[dcpomatic.git] / src / lib / caption_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 "caption_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 CaptionContentProperty::X_OFFSET = 500;
44 int const CaptionContentProperty::Y_OFFSET = 501;
45 int const CaptionContentProperty::X_SCALE = 502;
46 int const CaptionContentProperty::Y_SCALE = 503;
47 int const CaptionContentProperty::USE = 504;
48 int const CaptionContentProperty::BURN = 505;
49 int const CaptionContentProperty::LANGUAGE = 506;
50 int const CaptionContentProperty::FONTS = 507;
51 int const CaptionContentProperty::COLOUR = 508;
52 int const CaptionContentProperty::EFFECT = 509;
53 int const CaptionContentProperty::EFFECT_COLOUR = 510;
54 int const CaptionContentProperty::LINE_SPACING = 511;
55 int const CaptionContentProperty::FADE_IN = 512;
56 int const CaptionContentProperty::FADE_OUT = 513;
57 int const CaptionContentProperty::OUTLINE_WIDTH = 514;
58 int const CaptionContentProperty::TYPE = 515;
59
60 CaptionContent::CaptionContent (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         , _line_spacing (1)
69         , _outline_width (2)
70         , _type (CAPTION_OPEN)
71 {
72
73 }
74
75 shared_ptr<CaptionContent>
76 CaptionContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
77 {
78         if (version < 34) {
79                 /* With old metadata FFmpeg content has the subtitle-related tags even with no
80                    subtitle streams, so check for that.
81                 */
82                 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
83                         return shared_ptr<CaptionContent> ();
84                 }
85
86                 /* Otherwise we can drop through to the newer logic */
87         }
88
89         if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
90                 return shared_ptr<CaptionContent> ();
91         }
92
93         return shared_ptr<CaptionContent> (new CaptionContent (parent, node, version));
94 }
95
96 CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int version)
97         : ContentPart (parent)
98         , _use (false)
99         , _burn (false)
100         , _x_offset (0)
101         , _y_offset (0)
102         , _x_scale (1)
103         , _y_scale (1)
104         , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
105         , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (2))
106         , _type (CAPTION_OPEN)
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         optional<Frame> fi = node->optional_number_child<Frame>("SubtitleFadeIn");
169         if (fi) {
170                 _fade_in = ContentTime (*fi);
171         }
172         optional<Frame> fo = node->optional_number_child<Frame>("SubtitleFadeOut");
173         if (fo) {
174                 _fade_out = ContentTime (*fo);
175         }
176
177         _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
178
179         list<cxml::NodePtr> fonts = node->node_children ("Font");
180         for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
181                 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
182         }
183
184         connect_to_fonts ();
185
186         _type = string_to_caption_type (node->optional_string_child("CaptionType").get_value_or("open"));
187 }
188
189 CaptionContent::CaptionContent (Content* parent, vector<shared_ptr<Content> > c)
190         : ContentPart (parent)
191 {
192         shared_ptr<CaptionContent> ref = c[0]->caption;
193         DCPOMATIC_ASSERT (ref);
194         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
195
196         for (size_t i = 1; i < c.size(); ++i) {
197
198                 if (c[i]->caption->use() != ref->use()) {
199                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
200                 }
201
202                 if (c[i]->caption->burn() != ref->burn()) {
203                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
204                 }
205
206                 if (c[i]->caption->x_offset() != ref->x_offset()) {
207                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
208                 }
209
210                 if (c[i]->caption->y_offset() != ref->y_offset()) {
211                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
212                 }
213
214                 if (c[i]->caption->x_scale() != ref->x_scale()) {
215                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
216                 }
217
218                 if (c[i]->caption->y_scale() != ref->y_scale()) {
219                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
220                 }
221
222                 if (c[i]->caption->line_spacing() != ref->line_spacing()) {
223                         throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
224                 }
225
226                 if ((c[i]->caption->fade_in() != ref->fade_in()) || (c[i]->caption->fade_out() != ref->fade_out())) {
227                         throw JoinError (_("Content to be joined must have the same subtitle fades."));
228                 }
229
230                 if ((c[i]->caption->outline_width() != ref->outline_width())) {
231                         throw JoinError (_("Content to be joined must have the same outline width."));
232                 }
233
234                 list<shared_ptr<Font> > fonts = c[i]->caption->fonts ();
235                 if (fonts.size() != ref_fonts.size()) {
236                         throw JoinError (_("Content to be joined must use the same fonts."));
237                 }
238
239                 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
240                 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
241
242                 while (j != ref_fonts.end ()) {
243                         if (**j != **k) {
244                                 throw JoinError (_("Content to be joined must use the same fonts."));
245                         }
246                         ++j;
247                         ++k;
248                 }
249         }
250
251         _use = ref->use ();
252         _burn = ref->burn ();
253         _x_offset = ref->x_offset ();
254         _y_offset = ref->y_offset ();
255         _x_scale = ref->x_scale ();
256         _y_scale = ref->y_scale ();
257         _language = ref->language ();
258         _fonts = ref_fonts;
259         _line_spacing = ref->line_spacing ();
260         _fade_in = ref->fade_in ();
261         _fade_out = ref->fade_out ();
262         _outline_width = ref->outline_width ();
263
264         connect_to_fonts ();
265 }
266
267 /** _mutex must not be held on entry */
268 void
269 CaptionContent::as_xml (xmlpp::Node* root) const
270 {
271         boost::mutex::scoped_lock lm (_mutex);
272
273         root->add_child("UseSubtitles")->add_child_text (_use ? "1" : "0");
274         root->add_child("BurnSubtitles")->add_child_text (_burn ? "1" : "0");
275         root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
276         root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
277         root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
278         root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
279         root->add_child("SubtitleLanguage")->add_child_text (_language);
280         if (_colour) {
281                 root->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
282                 root->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
283                 root->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
284         }
285         if (_effect) {
286                 switch (*_effect) {
287                 case dcp::NONE:
288                         root->add_child("Effect")->add_child_text("none");
289                         break;
290                 case dcp::BORDER:
291                         root->add_child("Effect")->add_child_text("outline");
292                         break;
293                 case dcp::SHADOW:
294                         root->add_child("Effect")->add_child_text("shadow");
295                         break;
296                 }
297         }
298         if (_effect_colour) {
299                 root->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
300                 root->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
301                 root->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
302         }
303         root->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
304         if (_fade_in) {
305                 root->add_child("SubtitleFadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
306         }
307         if (_fade_out) {
308                 root->add_child("SubtitleFadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
309         }
310         root->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
311
312         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
313                 (*i)->as_xml (root->add_child("Font"));
314         }
315
316         root->add_child("CaptionType")->add_child_text (caption_type_to_string(_type));
317 }
318
319 string
320 CaptionContent::identifier () const
321 {
322         string s = raw_convert<string> (x_scale())
323                 + "_" + raw_convert<string> (y_scale())
324                 + "_" + raw_convert<string> (x_offset())
325                 + "_" + raw_convert<string> (y_offset())
326                 + "_" + raw_convert<string> (line_spacing())
327                 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
328                 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
329                 + "_" + raw_convert<string> (outline_width())
330                 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
331                 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::NONE)))
332                 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string());
333
334         /* XXX: I suppose really _fonts shouldn't be in here, since not all
335            types of subtitle content involve fonts.
336         */
337         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
338                 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
339                         s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
340                 }
341         }
342
343         /* The language is for metadata only, and doesn't affect
344            how this content looks.
345         */
346
347         return s;
348 }
349
350 void
351 CaptionContent::add_font (shared_ptr<Font> font)
352 {
353         _fonts.push_back (font);
354         connect_to_fonts ();
355 }
356
357 void
358 CaptionContent::connect_to_fonts ()
359 {
360         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
361                 i.disconnect ();
362         }
363
364         _font_connections.clear ();
365
366         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
367                 _font_connections.push_back (i->Changed.connect (boost::bind (&CaptionContent::font_changed, this)));
368         }
369 }
370
371 void
372 CaptionContent::font_changed ()
373 {
374         _parent->signal_changed (CaptionContentProperty::FONTS);
375 }
376
377 void
378 CaptionContent::set_colour (dcp::Colour colour)
379 {
380         maybe_set (_colour, colour, CaptionContentProperty::COLOUR);
381 }
382
383 void
384 CaptionContent::unset_colour ()
385 {
386         maybe_set (_colour, optional<dcp::Colour>(), CaptionContentProperty::COLOUR);
387 }
388
389 void
390 CaptionContent::set_effect (dcp::Effect e)
391 {
392         maybe_set (_effect, e, CaptionContentProperty::EFFECT);
393 }
394
395 void
396 CaptionContent::unset_effect ()
397 {
398         maybe_set (_effect, optional<dcp::Effect>(), CaptionContentProperty::EFFECT);
399 }
400
401 void
402 CaptionContent::set_effect_colour (dcp::Colour colour)
403 {
404         maybe_set (_effect_colour, colour, CaptionContentProperty::EFFECT_COLOUR);
405 }
406
407 void
408 CaptionContent::unset_effect_colour ()
409 {
410         maybe_set (_effect_colour, optional<dcp::Colour>(), CaptionContentProperty::EFFECT_COLOUR);
411 }
412
413 void
414 CaptionContent::set_use (bool u)
415 {
416         maybe_set (_use, u, CaptionContentProperty::USE);
417 }
418
419 void
420 CaptionContent::set_burn (bool b)
421 {
422         maybe_set (_burn, b, CaptionContentProperty::BURN);
423 }
424
425 void
426 CaptionContent::set_x_offset (double o)
427 {
428         maybe_set (_x_offset, o, CaptionContentProperty::X_OFFSET);
429 }
430
431 void
432 CaptionContent::set_y_offset (double o)
433 {
434         maybe_set (_y_offset, o, CaptionContentProperty::Y_OFFSET);
435 }
436
437 void
438 CaptionContent::set_x_scale (double s)
439 {
440         maybe_set (_x_scale, s, CaptionContentProperty::X_SCALE);
441 }
442
443 void
444 CaptionContent::set_y_scale (double s)
445 {
446         maybe_set (_y_scale, s, CaptionContentProperty::Y_SCALE);
447 }
448
449 void
450 CaptionContent::set_language (string language)
451 {
452         maybe_set (_language, language, CaptionContentProperty::LANGUAGE);
453 }
454
455 void
456 CaptionContent::set_line_spacing (double s)
457 {
458         maybe_set (_line_spacing, s, CaptionContentProperty::LINE_SPACING);
459 }
460
461 void
462 CaptionContent::set_fade_in (ContentTime t)
463 {
464         maybe_set (_fade_in, t, CaptionContentProperty::FADE_IN);
465 }
466
467 void
468 CaptionContent::unset_fade_in ()
469 {
470         maybe_set (_fade_in, optional<ContentTime>(), CaptionContentProperty::FADE_IN);
471 }
472
473 void
474 CaptionContent::set_fade_out (ContentTime t)
475 {
476         maybe_set (_fade_out, t, CaptionContentProperty::FADE_OUT);
477 }
478
479 void
480 CaptionContent::unset_fade_out ()
481 {
482         maybe_set (_fade_out, optional<ContentTime>(), CaptionContentProperty::FADE_OUT);
483 }
484
485 void
486 CaptionContent::set_type (CaptionType type)
487 {
488         maybe_set (_type, type, CaptionContentProperty::TYPE);
489 }
490
491 void
492 CaptionContent::set_outline_width (int w)
493 {
494         maybe_set (_outline_width, w, CaptionContentProperty::OUTLINE_WIDTH);
495 }
496
497 void
498 CaptionContent::take_settings_from (shared_ptr<const CaptionContent> c)
499 {
500         set_use (c->_use);
501         set_burn (c->_burn);
502         set_x_offset (c->_x_offset);
503         set_y_offset (c->_y_offset);
504         set_x_scale (c->_x_scale);
505         set_y_scale (c->_y_scale);
506         maybe_set (_fonts, c->_fonts, CaptionContentProperty::FONTS);
507         if (c->_colour) {
508                 set_colour (*c->_colour);
509         } else {
510                 unset_colour ();
511         }
512         if (c->_effect) {
513                 set_effect (*c->_effect);
514         }
515         if (c->_effect_colour) {
516                 set_effect_colour (*c->_effect_colour);
517         } else {
518                 unset_effect_colour ();
519         }
520         set_line_spacing (c->_line_spacing);
521         if (c->_fade_in) {
522                 set_fade_in (*c->_fade_in);
523         }
524         if (c->_fade_out) {
525                 set_fade_out (*c->_fade_out);
526         }
527         set_outline_width (c->_outline_width);
528 }