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