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