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