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