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