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