ee29ae8c4d28122c6dfeafc2838906a48bf4c0d8
[dcpomatic.git] / src / lib / text_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 "text_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 TextContentProperty::X_OFFSET = 500;
44 int const TextContentProperty::Y_OFFSET = 501;
45 int const TextContentProperty::X_SCALE = 502;
46 int const TextContentProperty::Y_SCALE = 503;
47 int const TextContentProperty::USE = 504;
48 int const TextContentProperty::BURN = 505;
49 int const TextContentProperty::LANGUAGE = 506;
50 int const TextContentProperty::FONTS = 507;
51 int const TextContentProperty::COLOUR = 508;
52 int const TextContentProperty::EFFECT = 509;
53 int const TextContentProperty::EFFECT_COLOUR = 510;
54 int const TextContentProperty::LINE_SPACING = 511;
55 int const TextContentProperty::FADE_IN = 512;
56 int const TextContentProperty::FADE_OUT = 513;
57 int const TextContentProperty::OUTLINE_WIDTH = 514;
58 int const TextContentProperty::TYPE = 515;
59
60 TextContent::TextContent (Content* parent, TextType type, TextType original_type)
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 (type)
71         , _original_type (original_type)
72 {
73
74 }
75
76 /** @return TextContents from node or <Text> nodes under node (according to version).
77  *  The list could be empty if no TextContents are found.
78  */
79 list<shared_ptr<TextContent> >
80 TextContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
81 {
82         if (version < 34) {
83                 /* With old metadata FFmpeg content has the subtitle-related tags even with no
84                    subtitle streams, so check for that.
85                 */
86                 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
87                         return list<shared_ptr<TextContent> >();
88                 }
89
90                 /* Otherwise we can drop through to the newer logic */
91         }
92
93         if (version < 37) {
94                 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
95                         return list<shared_ptr<TextContent> >();
96                 }
97                 list<shared_ptr<TextContent> > c;
98                 c.push_back (shared_ptr<TextContent> (new TextContent (parent, node, version)));
99                 return c;
100         }
101
102         if (!node->optional_node_child("Text")) {
103                 return list<shared_ptr<TextContent> >();
104         }
105
106         list<shared_ptr<TextContent> > c;
107         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("Text")) {
108                 c.push_back (shared_ptr<TextContent> (new TextContent (parent, i, version)));
109         }
110         return c;
111 }
112
113 TextContent::TextContent (Content* parent, cxml::ConstNodePtr node, int version)
114         : ContentPart (parent)
115         , _use (false)
116         , _burn (false)
117         , _x_offset (0)
118         , _y_offset (0)
119         , _x_scale (1)
120         , _y_scale (1)
121         , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
122         , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (2))
123         , _type (TEXT_OPEN_SUBTITLE)
124         , _original_type (TEXT_OPEN_SUBTITLE)
125 {
126         if (version >= 37) {
127                 _use = node->bool_child ("Use");
128                 _burn = node->bool_child ("Burn");
129         } else if (version >= 32) {
130                 _use = node->bool_child ("UseSubtitles");
131                 _burn = node->bool_child ("BurnSubtitles");
132         }
133
134         if (version >= 37) {
135                 _x_offset = node->number_child<double> ("XOffset");
136                 _y_offset = node->number_child<double> ("YOffset");
137         } else if (version >= 7) {
138                 _x_offset = node->number_child<double> ("SubtitleXOffset");
139                 _y_offset = node->number_child<double> ("SubtitleYOffset");
140         } else {
141                 _y_offset = node->number_child<double> ("SubtitleOffset");
142         }
143
144         if (node->optional_bool_child("Outline").get_value_or(false)) {
145                 _effect = dcp::BORDER;
146         } else if (node->optional_bool_child("Shadow").get_value_or(false)) {
147                 _effect = dcp::SHADOW;
148         } else {
149                 _effect = dcp::NONE;
150         }
151
152         optional<string> effect = node->optional_string_child("Effect");
153         if (effect) {
154                 if (*effect == "none") {
155                         _effect = dcp::NONE;
156                 } else if (*effect == "outline") {
157                         _effect = dcp::BORDER;
158                 } else if (*effect == "shadow") {
159                         _effect = dcp::SHADOW;
160                 }
161         }
162
163         if (version >= 37) {
164                 _x_scale = node->number_child<double> ("XScale");
165                 _y_scale = node->number_child<double> ("YScale");
166         } else if (version >= 10) {
167                 _x_scale = node->number_child<double> ("SubtitleXScale");
168                 _y_scale = node->number_child<double> ("SubtitleYScale");
169         } else {
170                 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
171         }
172
173         optional<int> r = node->optional_number_child<int>("Red");
174         optional<int> g = node->optional_number_child<int>("Green");
175         optional<int> b = node->optional_number_child<int>("Blue");
176         if (r && g && b) {
177                 _colour = dcp::Colour (*r, *g, *b);
178         }
179
180         if (version >= 36) {
181                 optional<int> er = node->optional_number_child<int>("EffectRed");
182                 optional<int> eg = node->optional_number_child<int>("EffectGreen");
183                 optional<int> eb = node->optional_number_child<int>("EffectBlue");
184                 if (er && eg && eb) {
185                         _effect_colour = dcp::Colour (*er, *eg, *eb);
186                 }
187         } else {
188                 _effect_colour = dcp::Colour (
189                         node->optional_number_child<int>("OutlineRed").get_value_or(255),
190                         node->optional_number_child<int>("OutlineGreen").get_value_or(255),
191                         node->optional_number_child<int>("OutlineBlue").get_value_or(255)
192                         );
193         }
194
195         optional<Frame> fi;
196         if (version >= 37) {
197                 fi = node->optional_number_child<Frame>("FadeIn");
198         } else {
199                 fi = node->optional_number_child<Frame>("SubtitleFadeIn");
200         }
201         if (fi) {
202                 _fade_in = ContentTime (*fi);
203         }
204
205         optional<Frame> fo;
206         if (version >= 37) {
207                 fo = node->optional_number_child<Frame>("FadeOut");
208         } else {
209                 fo = node->optional_number_child<Frame>("SubtitleFadeOut");
210         }
211         if (fo) {
212                 _fade_out = ContentTime (*fo);
213         }
214
215         if (version >= 37) {
216                 _language = node->optional_string_child ("Language").get_value_or ("");
217         } else {
218                 _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
219         }
220
221         list<cxml::NodePtr> fonts = node->node_children ("Font");
222         for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
223                 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
224         }
225
226         connect_to_fonts ();
227
228         if (version >= 37) {
229                 _type = string_to_text_type (node->optional_string_child("Type").get_value_or("open"));
230                 if (node->optional_string_child("OriginalType")) {
231                         _original_type = string_to_text_type (node->optional_string_child("OriginalType").get());
232                 }
233         }
234 }
235
236 TextContent::TextContent (Content* parent, vector<shared_ptr<Content> > c)
237         : ContentPart (parent)
238 {
239         /* This constructor is for join which is only supported for content types
240            that have a single text, so we can use only_text() here.
241         */
242         shared_ptr<TextContent> ref = c[0]->only_text();
243         DCPOMATIC_ASSERT (ref);
244         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
245
246         for (size_t i = 1; i < c.size(); ++i) {
247
248                 if (c[i]->only_text()->use() != ref->use()) {
249                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
250                 }
251
252                 if (c[i]->only_text()->burn() != ref->burn()) {
253                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
254                 }
255
256                 if (c[i]->only_text()->x_offset() != ref->x_offset()) {
257                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
258                 }
259
260                 if (c[i]->only_text()->y_offset() != ref->y_offset()) {
261                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
262                 }
263
264                 if (c[i]->only_text()->x_scale() != ref->x_scale()) {
265                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
266                 }
267
268                 if (c[i]->only_text()->y_scale() != ref->y_scale()) {
269                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
270                 }
271
272                 if (c[i]->only_text()->line_spacing() != ref->line_spacing()) {
273                         throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
274                 }
275
276                 if ((c[i]->only_text()->fade_in() != ref->fade_in()) || (c[i]->only_text()->fade_out() != ref->fade_out())) {
277                         throw JoinError (_("Content to be joined must have the same subtitle fades."));
278                 }
279
280                 if ((c[i]->only_text()->outline_width() != ref->outline_width())) {
281                         throw JoinError (_("Content to be joined must have the same outline width."));
282                 }
283
284                 list<shared_ptr<Font> > fonts = c[i]->only_text()->fonts ();
285                 if (fonts.size() != ref_fonts.size()) {
286                         throw JoinError (_("Content to be joined must use the same fonts."));
287                 }
288
289                 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
290                 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
291
292                 while (j != ref_fonts.end ()) {
293                         if (**j != **k) {
294                                 throw JoinError (_("Content to be joined must use the same fonts."));
295                         }
296                         ++j;
297                         ++k;
298                 }
299         }
300
301         _use = ref->use ();
302         _burn = ref->burn ();
303         _x_offset = ref->x_offset ();
304         _y_offset = ref->y_offset ();
305         _x_scale = ref->x_scale ();
306         _y_scale = ref->y_scale ();
307         _language = ref->language ();
308         _fonts = ref_fonts;
309         _line_spacing = ref->line_spacing ();
310         _fade_in = ref->fade_in ();
311         _fade_out = ref->fade_out ();
312         _outline_width = ref->outline_width ();
313         _type = ref->type ();
314         _original_type = ref->original_type ();
315
316         connect_to_fonts ();
317 }
318
319 /** _mutex must not be held on entry */
320 void
321 TextContent::as_xml (xmlpp::Node* root) const
322 {
323         boost::mutex::scoped_lock lm (_mutex);
324
325         xmlpp::Element* text = root->add_child ("Text");
326
327         text->add_child("Use")->add_child_text (_use ? "1" : "0");
328         text->add_child("Burn")->add_child_text (_burn ? "1" : "0");
329         text->add_child("XOffset")->add_child_text (raw_convert<string> (_x_offset));
330         text->add_child("YOffset")->add_child_text (raw_convert<string> (_y_offset));
331         text->add_child("XScale")->add_child_text (raw_convert<string> (_x_scale));
332         text->add_child("YScale")->add_child_text (raw_convert<string> (_y_scale));
333         text->add_child("Language")->add_child_text (_language);
334         if (_colour) {
335                 text->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
336                 text->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
337                 text->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
338         }
339         if (_effect) {
340                 switch (*_effect) {
341                 case dcp::NONE:
342                         text->add_child("Effect")->add_child_text("none");
343                         break;
344                 case dcp::BORDER:
345                         text->add_child("Effect")->add_child_text("outline");
346                         break;
347                 case dcp::SHADOW:
348                         text->add_child("Effect")->add_child_text("shadow");
349                         break;
350                 }
351         }
352         if (_effect_colour) {
353                 text->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
354                 text->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
355                 text->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
356         }
357         text->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
358         if (_fade_in) {
359                 text->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
360         }
361         if (_fade_out) {
362                 text->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
363         }
364         text->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
365
366         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
367                 (*i)->as_xml (text->add_child("Font"));
368         }
369
370         text->add_child("Type")->add_child_text (text_type_to_string(_type));
371         text->add_child("OriginalType")->add_child_text (text_type_to_string(_original_type));
372 }
373
374 string
375 TextContent::identifier () const
376 {
377         string s = raw_convert<string> (x_scale())
378                 + "_" + raw_convert<string> (y_scale())
379                 + "_" + raw_convert<string> (x_offset())
380                 + "_" + raw_convert<string> (y_offset())
381                 + "_" + raw_convert<string> (line_spacing())
382                 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
383                 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
384                 + "_" + raw_convert<string> (outline_width())
385                 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
386                 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::NONE)))
387                 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string());
388
389         /* XXX: I suppose really _fonts shouldn't be in here, since not all
390            types of subtitle content involve fonts.
391         */
392         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
393                 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
394                         s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
395                 }
396         }
397
398         /* The language is for metadata only, and doesn't affect
399            how this content looks.
400         */
401
402         return s;
403 }
404
405 void
406 TextContent::add_font (shared_ptr<Font> font)
407 {
408         _fonts.push_back (font);
409         connect_to_fonts ();
410 }
411
412 void
413 TextContent::connect_to_fonts ()
414 {
415         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
416                 i.disconnect ();
417         }
418
419         _font_connections.clear ();
420
421         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
422                 _font_connections.push_back (i->Changed.connect (boost::bind (&TextContent::font_changed, this)));
423         }
424 }
425
426 void
427 TextContent::font_changed ()
428 {
429         _parent->signal_changed (TextContentProperty::FONTS);
430 }
431
432 void
433 TextContent::set_colour (dcp::Colour colour)
434 {
435         maybe_set (_colour, colour, TextContentProperty::COLOUR);
436 }
437
438 void
439 TextContent::unset_colour ()
440 {
441         maybe_set (_colour, optional<dcp::Colour>(), TextContentProperty::COLOUR);
442 }
443
444 void
445 TextContent::set_effect (dcp::Effect e)
446 {
447         maybe_set (_effect, e, TextContentProperty::EFFECT);
448 }
449
450 void
451 TextContent::unset_effect ()
452 {
453         maybe_set (_effect, optional<dcp::Effect>(), TextContentProperty::EFFECT);
454 }
455
456 void
457 TextContent::set_effect_colour (dcp::Colour colour)
458 {
459         maybe_set (_effect_colour, colour, TextContentProperty::EFFECT_COLOUR);
460 }
461
462 void
463 TextContent::unset_effect_colour ()
464 {
465         maybe_set (_effect_colour, optional<dcp::Colour>(), TextContentProperty::EFFECT_COLOUR);
466 }
467
468 void
469 TextContent::set_use (bool u)
470 {
471         maybe_set (_use, u, TextContentProperty::USE);
472 }
473
474 void
475 TextContent::set_burn (bool b)
476 {
477         maybe_set (_burn, b, TextContentProperty::BURN);
478 }
479
480 void
481 TextContent::set_x_offset (double o)
482 {
483         maybe_set (_x_offset, o, TextContentProperty::X_OFFSET);
484 }
485
486 void
487 TextContent::set_y_offset (double o)
488 {
489         maybe_set (_y_offset, o, TextContentProperty::Y_OFFSET);
490 }
491
492 void
493 TextContent::set_x_scale (double s)
494 {
495         maybe_set (_x_scale, s, TextContentProperty::X_SCALE);
496 }
497
498 void
499 TextContent::set_y_scale (double s)
500 {
501         maybe_set (_y_scale, s, TextContentProperty::Y_SCALE);
502 }
503
504 void
505 TextContent::set_language (string language)
506 {
507         maybe_set (_language, language, TextContentProperty::LANGUAGE);
508 }
509
510 void
511 TextContent::set_line_spacing (double s)
512 {
513         maybe_set (_line_spacing, s, TextContentProperty::LINE_SPACING);
514 }
515
516 void
517 TextContent::set_fade_in (ContentTime t)
518 {
519         maybe_set (_fade_in, t, TextContentProperty::FADE_IN);
520 }
521
522 void
523 TextContent::unset_fade_in ()
524 {
525         maybe_set (_fade_in, optional<ContentTime>(), TextContentProperty::FADE_IN);
526 }
527
528 void
529 TextContent::set_fade_out (ContentTime t)
530 {
531         maybe_set (_fade_out, t, TextContentProperty::FADE_OUT);
532 }
533
534 void
535 TextContent::unset_fade_out ()
536 {
537         maybe_set (_fade_out, optional<ContentTime>(), TextContentProperty::FADE_OUT);
538 }
539
540 void
541 TextContent::set_type (TextType type)
542 {
543         maybe_set (_type, type, TextContentProperty::TYPE);
544 }
545
546 void
547 TextContent::set_outline_width (int w)
548 {
549         maybe_set (_outline_width, w, TextContentProperty::OUTLINE_WIDTH);
550 }
551
552 void
553 TextContent::take_settings_from (shared_ptr<const TextContent> c)
554 {
555         set_use (c->_use);
556         set_burn (c->_burn);
557         set_x_offset (c->_x_offset);
558         set_y_offset (c->_y_offset);
559         set_x_scale (c->_x_scale);
560         set_y_scale (c->_y_scale);
561         maybe_set (_fonts, c->_fonts, TextContentProperty::FONTS);
562         if (c->_colour) {
563                 set_colour (*c->_colour);
564         } else {
565                 unset_colour ();
566         }
567         if (c->_effect) {
568                 set_effect (*c->_effect);
569         }
570         if (c->_effect_colour) {
571                 set_effect_colour (*c->_effect_colour);
572         } else {
573                 unset_effect_colour ();
574         }
575         set_line_spacing (c->_line_spacing);
576         if (c->_fade_in) {
577                 set_fade_in (*c->_fade_in);
578         }
579         if (c->_fade_out) {
580                 set_fade_out (*c->_fade_out);
581         }
582         set_outline_width (c->_outline_width);
583 }