cfdaabcbc0bdf24e3bf133052d842563c4837e40
[dcpomatic.git] / src / lib / text_content.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "text_content.h"
23 #include "util.h"
24 #include "exceptions.h"
25 #include "font.h"
26 #include "content.h"
27 #include <dcp/raw_convert.h>
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
30 #include <iostream>
31
32 #include "i18n.h"
33
34
35 using std::string;
36 using std::vector;
37 using std::cout;
38 using std::list;
39 using std::shared_ptr;
40 using std::make_shared;
41 using std::dynamic_pointer_cast;
42 using boost::optional;
43 using dcp::raw_convert;
44 using namespace dcpomatic;
45
46
47 int const TextContentProperty::X_OFFSET = 500;
48 int const TextContentProperty::Y_OFFSET = 501;
49 int const TextContentProperty::X_SCALE = 502;
50 int const TextContentProperty::Y_SCALE = 503;
51 int const TextContentProperty::USE = 504;
52 int const TextContentProperty::BURN = 505;
53 int const TextContentProperty::FONTS = 506;
54 int const TextContentProperty::COLOUR = 507;
55 int const TextContentProperty::EFFECT = 508;
56 int const TextContentProperty::EFFECT_COLOUR = 509;
57 int const TextContentProperty::LINE_SPACING = 510;
58 int const TextContentProperty::FADE_IN = 511;
59 int const TextContentProperty::FADE_OUT = 512;
60 int const TextContentProperty::OUTLINE_WIDTH = 513;
61 int const TextContentProperty::TYPE = 514;
62 int const TextContentProperty::DCP_TRACK = 515;
63 int const TextContentProperty::LANGUAGE = 516;
64 int const TextContentProperty::LANGUAGE_IS_ADDITIONAL = 517;
65
66
67 TextContent::TextContent (Content* parent, TextType type, TextType original_type)
68         : ContentPart (parent)
69         , _use (false)
70         , _burn (false)
71         , _x_offset (0)
72         , _y_offset (0)
73         , _x_scale (1)
74         , _y_scale (1)
75         , _line_spacing (1)
76         , _outline_width (4)
77         , _type (type)
78         , _original_type (original_type)
79 {
80
81 }
82
83 /** @return TextContents from node or <Text> nodes under node (according to version).
84  *  The vector could be empty if no TextContents are found.
85  */
86 vector<shared_ptr<TextContent>>
87 TextContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version, list<string>& notes)
88 {
89         if (version < 34) {
90                 /* With old metadata FFmpeg content has the subtitle-related tags even with no
91                    subtitle streams, so check for that.
92                 */
93                 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
94                         return {};
95                 }
96
97                 /* Otherwise we can drop through to the newer logic */
98         }
99
100         if (version < 37) {
101                 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
102                         return {};
103                 }
104                 return { make_shared<TextContent>(parent, node, version, notes) };
105         }
106
107         vector<shared_ptr<TextContent>> content;
108         for (auto i: node->node_children("Text")) {
109                 content.push_back(make_shared<TextContent>(parent, i, version, notes));
110         }
111
112         return content;
113 }
114
115
116 TextContent::TextContent (Content* parent, cxml::ConstNodePtr node, int version, list<string>& notes)
117         : ContentPart (parent)
118         , _use (false)
119         , _burn (false)
120         , _x_offset (0)
121         , _y_offset (0)
122         , _x_scale (1)
123         , _y_scale (1)
124         , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or(1))
125         , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or(4))
126         , _type (TextType::OPEN_SUBTITLE)
127         , _original_type (TextType::OPEN_SUBTITLE)
128 {
129         if (version >= 37) {
130                 _use = node->bool_child ("Use");
131                 _burn = node->bool_child ("Burn");
132         } else if (version >= 32) {
133                 _use = node->bool_child ("UseSubtitles");
134                 _burn = node->bool_child ("BurnSubtitles");
135         }
136
137         if (version >= 37) {
138                 _x_offset = node->number_child<double> ("XOffset");
139                 _y_offset = node->number_child<double> ("YOffset");
140         } else if (version >= 7) {
141                 _x_offset = node->number_child<double> ("SubtitleXOffset");
142                 _y_offset = node->number_child<double> ("SubtitleYOffset");
143         } else {
144                 _y_offset = node->number_child<double> ("SubtitleOffset");
145         }
146
147         if (node->optional_bool_child("Outline").get_value_or(false)) {
148                 _effect = dcp::Effect::BORDER;
149         } else if (node->optional_bool_child("Shadow").get_value_or(false)) {
150                 _effect = dcp::Effect::SHADOW;
151         }
152
153         auto effect = node->optional_string_child("Effect");
154         if (effect) {
155                 if (*effect == "none") {
156                         _effect = dcp::Effect::NONE;
157                 } else if (*effect == "outline") {
158                         _effect = dcp::Effect::BORDER;
159                 } else if (*effect == "shadow") {
160                         _effect = dcp::Effect::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         auto r = node->optional_number_child<int>("Red");
175         auto g = node->optional_number_child<int>("Green");
176         auto 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                 auto er = node->optional_number_child<int>("EffectRed");
183                 auto eg = node->optional_number_child<int>("EffectGreen");
184                 auto 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         for (auto i: node->node_children ("Font")) {
217                 _fonts.push_back (make_shared<Font>(i));
218         }
219
220         connect_to_fonts ();
221
222         if (version >= 37) {
223                 _type = string_to_text_type (node->optional_string_child("Type").get_value_or("open"));
224                 if (node->optional_string_child("OriginalType")) {
225                         _original_type = string_to_text_type (node->optional_string_child("OriginalType").get());
226                 }
227         }
228
229         auto dt = node->optional_node_child("DCPTrack");
230         if (dt) {
231                 _dcp_track = DCPTextTrack (dt);
232         }
233
234         auto lang = node->optional_node_child("Language");
235         if (lang) {
236                 try {
237                         _language = dcp::LanguageTag(lang->content());
238                         auto additional = lang->optional_bool_attribute("Additional");
239                         if (!additional) {
240                                 additional = lang->optional_bool_attribute("additional");
241                         }
242                         _language_is_additional = additional.get_value_or(false);
243                 } catch (dcp::LanguageTagError&) {
244                         /* The language tag can be empty or invalid if it was loaded from a
245                          * 2.14.x metadata file; we'll just ignore it in that case.
246                          */
247                         if (version <= 37) {
248                                 if (!lang->content().empty()) {
249                                         notes.push_back (String::compose(
250                                                 _("A subtitle or closed caption file in this project is marked with the language '%1', "
251                                                   "which DCP-o-matic does not recognise.  The file's language has been cleared."), lang->content()));
252                                 }
253                         } else {
254                                 throw;
255                         }
256                 }
257         }
258 }
259
260 TextContent::TextContent (Content* parent, vector<shared_ptr<Content>> c)
261         : ContentPart (parent)
262 {
263         /* This constructor is for join which is only supported for content types
264            that have a single text, so we can use only_text() here.
265         */
266         auto ref = c[0]->only_text();
267         DCPOMATIC_ASSERT (ref);
268         auto ref_fonts = ref->fonts ();
269
270         for (size_t i = 1; i < c.size(); ++i) {
271
272                 if (c[i]->only_text()->use() != ref->use()) {
273                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
274                 }
275
276                 if (c[i]->only_text()->burn() != ref->burn()) {
277                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
278                 }
279
280                 if (c[i]->only_text()->x_offset() != ref->x_offset()) {
281                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
282                 }
283
284                 if (c[i]->only_text()->y_offset() != ref->y_offset()) {
285                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
286                 }
287
288                 if (c[i]->only_text()->x_scale() != ref->x_scale()) {
289                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
290                 }
291
292                 if (c[i]->only_text()->y_scale() != ref->y_scale()) {
293                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
294                 }
295
296                 if (c[i]->only_text()->line_spacing() != ref->line_spacing()) {
297                         throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
298                 }
299
300                 if ((c[i]->only_text()->fade_in() != ref->fade_in()) || (c[i]->only_text()->fade_out() != ref->fade_out())) {
301                         throw JoinError (_("Content to be joined must have the same subtitle fades."));
302                 }
303
304                 if ((c[i]->only_text()->outline_width() != ref->outline_width())) {
305                         throw JoinError (_("Content to be joined must have the same outline width."));
306                 }
307
308                 auto fonts = c[i]->only_text()->fonts ();
309                 if (fonts.size() != ref_fonts.size()) {
310                         throw JoinError (_("Content to be joined must use the same fonts."));
311                 }
312
313                 if (c[i]->only_text()->dcp_track() != ref->dcp_track()) {
314                         throw JoinError (_("Content to be joined must use the same DCP track."));
315                 }
316
317                 if (c[i]->only_text()->language() != ref->language()) {
318                         throw JoinError (_("Content to be joined must use the same text language."));
319                 }
320
321                 if (c[i]->only_text()->language_is_additional() != ref->language_is_additional()) {
322                         throw JoinError (_("Content to be joined must both be main subtitle languages or both additional."));
323                 }
324
325                 auto j = ref_fonts.begin ();
326                 auto k = fonts.begin ();
327
328                 while (j != ref_fonts.end ()) {
329                         if (**j != **k) {
330                                 throw JoinError (_("Content to be joined must use the same fonts."));
331                         }
332                         ++j;
333                         ++k;
334                 }
335         }
336
337         _use = ref->use ();
338         _burn = ref->burn ();
339         _x_offset = ref->x_offset ();
340         _y_offset = ref->y_offset ();
341         _x_scale = ref->x_scale ();
342         _y_scale = ref->y_scale ();
343         _fonts = ref_fonts;
344         _line_spacing = ref->line_spacing ();
345         _fade_in = ref->fade_in ();
346         _fade_out = ref->fade_out ();
347         _outline_width = ref->outline_width ();
348         _type = ref->type ();
349         _original_type = ref->original_type ();
350         _dcp_track = ref->dcp_track ();
351         _language = ref->language ();
352         _language_is_additional = ref->language_is_additional ();
353
354         connect_to_fonts ();
355 }
356
357 /** _mutex must not be held on entry */
358 void
359 TextContent::as_xml(xmlpp::Element* root) const
360 {
361         boost::mutex::scoped_lock lm (_mutex);
362
363         auto text = cxml::add_child(root, "Text");
364
365         cxml::add_text_child(text, "Use", _use ? "1" : "0");
366         cxml::add_text_child(text, "Burn", _burn ? "1" : "0");
367         cxml::add_text_child(text, "XOffset", raw_convert<string>(_x_offset));
368         cxml::add_text_child(text, "YOffset", raw_convert<string>(_y_offset));
369         cxml::add_text_child(text, "XScale", raw_convert<string>(_x_scale));
370         cxml::add_text_child(text, "YScale", raw_convert<string>(_y_scale));
371         if (_colour) {
372                 cxml::add_text_child(text, "Red", raw_convert<string>(_colour->r));
373                 cxml::add_text_child(text, "Green", raw_convert<string>(_colour->g));
374                 cxml::add_text_child(text, "Blue", raw_convert<string>(_colour->b));
375         }
376         if (_effect) {
377                 switch (*_effect) {
378                 case dcp::Effect::NONE:
379                         cxml::add_text_child(text, "Effect", "none");
380                         break;
381                 case dcp::Effect::BORDER:
382                         cxml::add_text_child(text, "Effect", "outline");
383                         break;
384                 case dcp::Effect::SHADOW:
385                         cxml::add_text_child(text, "Effect", "shadow");
386                         break;
387                 }
388         }
389         if (_effect_colour) {
390                 cxml::add_text_child(text, "EffectRed", raw_convert<string>(_effect_colour->r));
391                 cxml::add_text_child(text, "EffectGreen", raw_convert<string>(_effect_colour->g));
392                 cxml::add_text_child(text, "EffectBlue", raw_convert<string>(_effect_colour->b));
393         }
394         cxml::add_text_child(text, "LineSpacing", raw_convert<string>(_line_spacing));
395         if (_fade_in) {
396                 cxml::add_text_child(text, "FadeIn", raw_convert<string>(_fade_in->get()));
397         }
398         if (_fade_out) {
399                 cxml::add_text_child(text, "FadeOut", raw_convert<string>(_fade_out->get()));
400         }
401         cxml::add_text_child(text, "OutlineWidth", raw_convert<string>(_outline_width));
402
403         for (auto i: _fonts) {
404                 i->as_xml(cxml::add_child(text, "Font"));
405         }
406
407         cxml::add_text_child(text, "Type", text_type_to_string(_type));
408         cxml::add_text_child(text, "OriginalType", text_type_to_string(_original_type));
409         if (_dcp_track) {
410                 _dcp_track->as_xml(cxml::add_child(text, "DCPTrack"));
411         }
412         if (_language) {
413                 auto lang = cxml::add_child(text, "Language");
414                 lang->add_child_text (_language->to_string());
415                 lang->set_attribute("additional", _language_is_additional ? "1" : "0");
416         }
417 }
418
419 string
420 TextContent::identifier () const
421 {
422         auto s = raw_convert<string> (x_scale())
423                 + "_" + raw_convert<string> (y_scale())
424                 + "_" + raw_convert<string> (x_offset())
425                 + "_" + raw_convert<string> (y_offset())
426                 + "_" + raw_convert<string> (line_spacing())
427                 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
428                 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
429                 + "_" + raw_convert<string> (outline_width())
430                 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
431                 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::Effect::NONE)))
432                 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string())
433                 + "_" + raw_convert<string> (_parent->video_frame_rate().get_value_or(0));
434
435         /* XXX: I suppose really _fonts shouldn't be in here, since not all
436            types of subtitle content involve fonts.
437         */
438         for (auto f: _fonts) {
439                 s += "_" + f->file().get_value_or("Default").string();
440         }
441
442         /* The DCP track and language are for metadata only, and don't affect how this content looks */
443
444         return s;
445 }
446
447 void
448 TextContent::add_font (shared_ptr<Font> font)
449 {
450         boost::mutex::scoped_lock lm(_mutex);
451
452         DCPOMATIC_ASSERT(!get_font_unlocked(font->id()));
453         _fonts.push_back (font);
454         connect_to_fonts ();
455 }
456
457 void
458 TextContent::connect_to_fonts ()
459 {
460         for (auto const& i: _font_connections) {
461                 i.disconnect ();
462         }
463
464         _font_connections.clear ();
465
466         for (auto i: _fonts) {
467                 _font_connections.push_back (i->Changed.connect (boost::bind (&TextContent::font_changed, this)));
468         }
469 }
470
471 void
472 TextContent::font_changed ()
473 {
474         /* XXX: too late */
475         ContentChangeSignaller cc (_parent, TextContentProperty::FONTS);
476 }
477
478 void
479 TextContent::set_colour (dcp::Colour colour)
480 {
481         maybe_set (_colour, colour, TextContentProperty::COLOUR);
482 }
483
484 void
485 TextContent::unset_colour ()
486 {
487         maybe_set (_colour, optional<dcp::Colour>(), TextContentProperty::COLOUR);
488 }
489
490 void
491 TextContent::set_effect (dcp::Effect e)
492 {
493         maybe_set (_effect, e, TextContentProperty::EFFECT);
494 }
495
496 void
497 TextContent::unset_effect ()
498 {
499         maybe_set (_effect, optional<dcp::Effect>(), TextContentProperty::EFFECT);
500 }
501
502 void
503 TextContent::set_effect_colour (dcp::Colour colour)
504 {
505         maybe_set (_effect_colour, colour, TextContentProperty::EFFECT_COLOUR);
506 }
507
508 void
509 TextContent::unset_effect_colour ()
510 {
511         maybe_set (_effect_colour, optional<dcp::Colour>(), TextContentProperty::EFFECT_COLOUR);
512 }
513
514 void
515 TextContent::set_use (bool u)
516 {
517         maybe_set (_use, u, TextContentProperty::USE);
518 }
519
520 void
521 TextContent::set_burn (bool b)
522 {
523         maybe_set (_burn, b, TextContentProperty::BURN);
524 }
525
526 void
527 TextContent::set_x_offset (double o)
528 {
529         maybe_set (_x_offset, o, TextContentProperty::X_OFFSET);
530 }
531
532 void
533 TextContent::set_y_offset (double o)
534 {
535         maybe_set (_y_offset, o, TextContentProperty::Y_OFFSET);
536 }
537
538 void
539 TextContent::set_x_scale (double s)
540 {
541         maybe_set (_x_scale, s, TextContentProperty::X_SCALE);
542 }
543
544 void
545 TextContent::set_y_scale (double s)
546 {
547         maybe_set (_y_scale, s, TextContentProperty::Y_SCALE);
548 }
549
550 void
551 TextContent::set_line_spacing (double s)
552 {
553         maybe_set (_line_spacing, s, TextContentProperty::LINE_SPACING);
554 }
555
556 void
557 TextContent::set_fade_in (ContentTime t)
558 {
559         maybe_set (_fade_in, t, TextContentProperty::FADE_IN);
560 }
561
562 void
563 TextContent::unset_fade_in ()
564 {
565         maybe_set (_fade_in, optional<ContentTime>(), TextContentProperty::FADE_IN);
566 }
567
568 void
569 TextContent::set_fade_out (ContentTime t)
570 {
571         maybe_set (_fade_out, t, TextContentProperty::FADE_OUT);
572 }
573
574 void
575 TextContent::unset_fade_out ()
576 {
577         maybe_set (_fade_out, optional<ContentTime>(), TextContentProperty::FADE_OUT);
578 }
579
580 void
581 TextContent::set_type (TextType type)
582 {
583         maybe_set (_type, type, TextContentProperty::TYPE);
584 }
585
586 void
587 TextContent::set_outline_width (int w)
588 {
589         maybe_set (_outline_width, w, TextContentProperty::OUTLINE_WIDTH);
590 }
591
592 void
593 TextContent::set_dcp_track (DCPTextTrack t)
594 {
595         maybe_set (_dcp_track, t, TextContentProperty::DCP_TRACK);
596 }
597
598 void
599 TextContent::unset_dcp_track ()
600 {
601         maybe_set (_dcp_track, optional<DCPTextTrack>(), TextContentProperty::DCP_TRACK);
602 }
603
604 void
605 TextContent::set_language (optional<dcp::LanguageTag> language)
606 {
607         maybe_set (_language, language, TextContentProperty::LANGUAGE);
608 }
609
610 void
611 TextContent::set_language_is_additional (bool additional)
612 {
613         maybe_set (_language_is_additional, additional, TextContentProperty::LANGUAGE_IS_ADDITIONAL);
614 }
615
616 void
617 TextContent::take_settings_from (shared_ptr<const TextContent> c)
618 {
619         set_use (c->_use);
620         set_burn (c->_burn);
621         set_x_offset (c->_x_offset);
622         set_y_offset (c->_y_offset);
623         set_x_scale (c->_x_scale);
624         set_y_scale (c->_y_scale);
625         maybe_set (_fonts, c->_fonts, TextContentProperty::FONTS);
626         if (c->_colour) {
627                 set_colour (*c->_colour);
628         } else {
629                 unset_colour ();
630         }
631         if (c->_effect) {
632                 set_effect (*c->_effect);
633         }
634         if (c->_effect_colour) {
635                 set_effect_colour (*c->_effect_colour);
636         } else {
637                 unset_effect_colour ();
638         }
639         set_line_spacing (c->_line_spacing);
640         if (c->_fade_in) {
641                 set_fade_in (*c->_fade_in);
642         }
643         if (c->_fade_out) {
644                 set_fade_out (*c->_fade_out);
645         }
646         set_outline_width (c->_outline_width);
647         if (c->_dcp_track) {
648                 set_dcp_track (c->_dcp_track.get());
649         } else {
650                 unset_dcp_track ();
651         }
652         set_language (c->_language);
653         set_language_is_additional (c->_language_is_additional);
654 }
655
656
657 shared_ptr<dcpomatic::Font>
658 TextContent::get_font(string id) const
659 {
660         boost::mutex::scoped_lock lm(_mutex);
661         return get_font_unlocked(id);
662 }
663
664
665 shared_ptr<dcpomatic::Font>
666 TextContent::get_font_unlocked(string id) const
667 {
668         auto iter = std::find_if(_fonts.begin(), _fonts.end(), [&id](shared_ptr<dcpomatic::Font> font) {
669                 return font->id() == id;
670         });
671
672         if (iter == _fonts.end()) {
673                 return {};
674         }
675
676         return *iter;
677 }
678
679
680 void
681 TextContent::clear_fonts()
682 {
683         boost::mutex::scoped_lock lm(_mutex);
684
685         _fonts.clear();
686 }
687