Remove join function; the code is long and I don't think anybody
[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
190 /** _mutex must not be held on entry */
191 void
192 CaptionContent::as_xml (xmlpp::Node* root) const
193 {
194         boost::mutex::scoped_lock lm (_mutex);
195
196         root->add_child("UseSubtitles")->add_child_text (_use ? "1" : "0");
197         root->add_child("BurnSubtitles")->add_child_text (_burn ? "1" : "0");
198         root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
199         root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
200         root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
201         root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
202         root->add_child("SubtitleLanguage")->add_child_text (_language);
203         if (_colour) {
204                 root->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
205                 root->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
206                 root->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
207         }
208         if (_effect) {
209                 switch (*_effect) {
210                 case dcp::NONE:
211                         root->add_child("Effect")->add_child_text("none");
212                         break;
213                 case dcp::BORDER:
214                         root->add_child("Effect")->add_child_text("outline");
215                         break;
216                 case dcp::SHADOW:
217                         root->add_child("Effect")->add_child_text("shadow");
218                         break;
219                 }
220         }
221         if (_effect_colour) {
222                 root->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
223                 root->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
224                 root->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
225         }
226         root->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
227         if (_fade_in) {
228                 root->add_child("SubtitleFadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
229         }
230         if (_fade_out) {
231                 root->add_child("SubtitleFadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
232         }
233         root->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
234
235         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
236                 (*i)->as_xml (root->add_child("Font"));
237         }
238
239         root->add_child("CaptionType")->add_child_text (caption_type_to_string(_type));
240 }
241
242 string
243 CaptionContent::identifier () const
244 {
245         string s = raw_convert<string> (x_scale())
246                 + "_" + raw_convert<string> (y_scale())
247                 + "_" + raw_convert<string> (x_offset())
248                 + "_" + raw_convert<string> (y_offset())
249                 + "_" + raw_convert<string> (line_spacing())
250                 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
251                 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
252                 + "_" + raw_convert<string> (outline_width())
253                 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
254                 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::NONE)))
255                 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string());
256
257         /* XXX: I suppose really _fonts shouldn't be in here, since not all
258            types of subtitle content involve fonts.
259         */
260         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
261                 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
262                         s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
263                 }
264         }
265
266         /* The language is for metadata only, and doesn't affect
267            how this content looks.
268         */
269
270         return s;
271 }
272
273 void
274 CaptionContent::add_font (shared_ptr<Font> font)
275 {
276         _fonts.push_back (font);
277         connect_to_fonts ();
278 }
279
280 void
281 CaptionContent::connect_to_fonts ()
282 {
283         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
284                 i.disconnect ();
285         }
286
287         _font_connections.clear ();
288
289         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
290                 _font_connections.push_back (i->Changed.connect (boost::bind (&CaptionContent::font_changed, this)));
291         }
292 }
293
294 void
295 CaptionContent::font_changed ()
296 {
297         _parent->signal_changed (CaptionContentProperty::FONTS);
298 }
299
300 void
301 CaptionContent::set_colour (dcp::Colour colour)
302 {
303         maybe_set (_colour, colour, CaptionContentProperty::COLOUR);
304 }
305
306 void
307 CaptionContent::unset_colour ()
308 {
309         maybe_set (_colour, optional<dcp::Colour>(), CaptionContentProperty::COLOUR);
310 }
311
312 void
313 CaptionContent::set_effect (dcp::Effect e)
314 {
315         maybe_set (_effect, e, CaptionContentProperty::EFFECT);
316 }
317
318 void
319 CaptionContent::unset_effect ()
320 {
321         maybe_set (_effect, optional<dcp::Effect>(), CaptionContentProperty::EFFECT);
322 }
323
324 void
325 CaptionContent::set_effect_colour (dcp::Colour colour)
326 {
327         maybe_set (_effect_colour, colour, CaptionContentProperty::EFFECT_COLOUR);
328 }
329
330 void
331 CaptionContent::unset_effect_colour ()
332 {
333         maybe_set (_effect_colour, optional<dcp::Colour>(), CaptionContentProperty::EFFECT_COLOUR);
334 }
335
336 void
337 CaptionContent::set_use (bool u)
338 {
339         maybe_set (_use, u, CaptionContentProperty::USE);
340 }
341
342 void
343 CaptionContent::set_burn (bool b)
344 {
345         maybe_set (_burn, b, CaptionContentProperty::BURN);
346 }
347
348 void
349 CaptionContent::set_x_offset (double o)
350 {
351         maybe_set (_x_offset, o, CaptionContentProperty::X_OFFSET);
352 }
353
354 void
355 CaptionContent::set_y_offset (double o)
356 {
357         maybe_set (_y_offset, o, CaptionContentProperty::Y_OFFSET);
358 }
359
360 void
361 CaptionContent::set_x_scale (double s)
362 {
363         maybe_set (_x_scale, s, CaptionContentProperty::X_SCALE);
364 }
365
366 void
367 CaptionContent::set_y_scale (double s)
368 {
369         maybe_set (_y_scale, s, CaptionContentProperty::Y_SCALE);
370 }
371
372 void
373 CaptionContent::set_language (string language)
374 {
375         maybe_set (_language, language, CaptionContentProperty::LANGUAGE);
376 }
377
378 void
379 CaptionContent::set_line_spacing (double s)
380 {
381         maybe_set (_line_spacing, s, CaptionContentProperty::LINE_SPACING);
382 }
383
384 void
385 CaptionContent::set_fade_in (ContentTime t)
386 {
387         maybe_set (_fade_in, t, CaptionContentProperty::FADE_IN);
388 }
389
390 void
391 CaptionContent::unset_fade_in ()
392 {
393         maybe_set (_fade_in, optional<ContentTime>(), CaptionContentProperty::FADE_IN);
394 }
395
396 void
397 CaptionContent::set_fade_out (ContentTime t)
398 {
399         maybe_set (_fade_out, t, CaptionContentProperty::FADE_OUT);
400 }
401
402 void
403 CaptionContent::unset_fade_out ()
404 {
405         maybe_set (_fade_out, optional<ContentTime>(), CaptionContentProperty::FADE_OUT);
406 }
407
408 void
409 CaptionContent::set_type (CaptionType type)
410 {
411         maybe_set (_type, type, CaptionContentProperty::TYPE);
412 }
413
414 void
415 CaptionContent::set_outline_width (int w)
416 {
417         maybe_set (_outline_width, w, CaptionContentProperty::OUTLINE_WIDTH);
418 }
419
420 void
421 CaptionContent::take_settings_from (shared_ptr<const CaptionContent> c)
422 {
423         set_use (c->_use);
424         set_burn (c->_burn);
425         set_x_offset (c->_x_offset);
426         set_y_offset (c->_y_offset);
427         set_x_scale (c->_x_scale);
428         set_y_scale (c->_y_scale);
429         maybe_set (_fonts, c->_fonts, CaptionContentProperty::FONTS);
430         if (c->_colour) {
431                 set_colour (*c->_colour);
432         } else {
433                 unset_colour ();
434         }
435         if (c->_effect) {
436                 set_effect (*c->_effect);
437         }
438         if (c->_effect_colour) {
439                 set_effect_colour (*c->_effect_colour);
440         } else {
441                 unset_effect_colour ();
442         }
443         set_line_spacing (c->_line_spacing);
444         if (c->_fade_in) {
445                 set_fade_in (*c->_fade_in);
446         }
447         if (c->_fade_out) {
448                 set_fade_out (*c->_fade_out);
449         }
450         set_outline_width (c->_outline_width);
451 }