2 Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "text_content.h"
24 #include "exceptions.h"
27 #include <dcp/raw_convert.h>
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
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;
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;
67 TextContent::TextContent (Content* parent, TextType type, TextType original_type)
68 : ContentPart (parent)
78 , _original_type (original_type)
83 /** @return TextContents from node or <Text> nodes under node (according to version).
84 * The vector could be empty if no TextContents are found.
86 vector<shared_ptr<TextContent>>
87 TextContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version, list<string>& notes)
90 /* With old metadata FFmpeg content has the subtitle-related tags even with no
91 subtitle streams, so check for that.
93 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
97 /* Otherwise we can drop through to the newer logic */
101 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
104 return { make_shared<TextContent>(parent, node, version, notes) };
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));
116 TextContent::TextContent (Content* parent, cxml::ConstNodePtr node, int version, list<string>& notes)
117 : ContentPart (parent)
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)
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");
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");
144 _y_offset = node->number_child<double> ("SubtitleOffset");
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;
153 auto effect = node->optional_string_child("Effect");
155 if (*effect == "none") {
156 _effect = dcp::Effect::NONE;
157 } else if (*effect == "outline") {
158 _effect = dcp::Effect::BORDER;
159 } else if (*effect == "shadow") {
160 _effect = dcp::Effect::SHADOW;
165 _x_scale = node->number_child<double> ("XScale");
166 _y_scale = node->number_child<double> ("YScale");
167 } else if (version >= 10) {
168 _x_scale = node->number_child<double> ("SubtitleXScale");
169 _y_scale = node->number_child<double> ("SubtitleYScale");
171 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
174 auto r = node->optional_number_child<int>("Red");
175 auto g = node->optional_number_child<int>("Green");
176 auto b = node->optional_number_child<int>("Blue");
178 _colour = dcp::Colour (*r, *g, *b);
182 auto er = node->optional_number_child<int>("EffectRed");
183 auto eg = node->optional_number_child<int>("EffectGreen");
184 auto eb = node->optional_number_child<int>("EffectBlue");
185 if (er && eg && eb) {
186 _effect_colour = dcp::Colour (*er, *eg, *eb);
189 _effect_colour = dcp::Colour (
190 node->optional_number_child<int>("OutlineRed").get_value_or(255),
191 node->optional_number_child<int>("OutlineGreen").get_value_or(255),
192 node->optional_number_child<int>("OutlineBlue").get_value_or(255)
198 fi = node->optional_number_child<Frame>("FadeIn");
200 fi = node->optional_number_child<Frame>("SubtitleFadeIn");
203 _fade_in = ContentTime (*fi);
208 fo = node->optional_number_child<Frame>("FadeOut");
210 fo = node->optional_number_child<Frame>("SubtitleFadeOut");
213 _fade_out = ContentTime (*fo);
216 for (auto i: node->node_children ("Font")) {
217 _fonts.push_back (make_shared<Font>(i));
223 _type = string_to_text_type (node->optional_string_child("Type").get_value_or("open"));
224 if (node->optional_string_child("OriginalType")) {
225 _original_type = string_to_text_type (node->optional_string_child("OriginalType").get());
229 auto dt = node->optional_node_child("DCPTrack");
231 _dcp_track = DCPTextTrack (dt);
234 auto lang = node->optional_node_child("Language");
237 _language = dcp::LanguageTag(lang->content());
238 auto add = lang->optional_bool_attribute("Additional");
239 _language_is_additional = add && *add;
240 } catch (dcp::LanguageTagError&) {
241 /* The language tag can be empty or invalid if it was loaded from a
242 * 2.14.x metadata file; we'll just ignore it in that case.
245 if (!lang->content().empty()) {
246 notes.push_back (String::compose(
247 _("A subtitle or closed caption file in this project is marked with the language '%1', "
248 "which DCP-o-matic does not recognise. The file's language has been cleared."), lang->content()));
257 TextContent::TextContent (Content* parent, vector<shared_ptr<Content>> c)
258 : ContentPart (parent)
260 /* This constructor is for join which is only supported for content types
261 that have a single text, so we can use only_text() here.
263 auto ref = c[0]->only_text();
264 DCPOMATIC_ASSERT (ref);
265 auto ref_fonts = ref->fonts ();
267 for (size_t i = 1; i < c.size(); ++i) {
269 if (c[i]->only_text()->use() != ref->use()) {
270 throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
273 if (c[i]->only_text()->burn() != ref->burn()) {
274 throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
277 if (c[i]->only_text()->x_offset() != ref->x_offset()) {
278 throw JoinError (_("Content to be joined must have the same subtitle X offset."));
281 if (c[i]->only_text()->y_offset() != ref->y_offset()) {
282 throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
285 if (c[i]->only_text()->x_scale() != ref->x_scale()) {
286 throw JoinError (_("Content to be joined must have the same subtitle X scale."));
289 if (c[i]->only_text()->y_scale() != ref->y_scale()) {
290 throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
293 if (c[i]->only_text()->line_spacing() != ref->line_spacing()) {
294 throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
297 if ((c[i]->only_text()->fade_in() != ref->fade_in()) || (c[i]->only_text()->fade_out() != ref->fade_out())) {
298 throw JoinError (_("Content to be joined must have the same subtitle fades."));
301 if ((c[i]->only_text()->outline_width() != ref->outline_width())) {
302 throw JoinError (_("Content to be joined must have the same outline width."));
305 auto fonts = c[i]->only_text()->fonts ();
306 if (fonts.size() != ref_fonts.size()) {
307 throw JoinError (_("Content to be joined must use the same fonts."));
310 if (c[i]->only_text()->dcp_track() != ref->dcp_track()) {
311 throw JoinError (_("Content to be joined must use the same DCP track."));
314 if (c[i]->only_text()->language() != ref->language()) {
315 throw JoinError (_("Content to be joined must use the same text language."));
318 if (c[i]->only_text()->language_is_additional() != ref->language_is_additional()) {
319 throw JoinError (_("Content to be joined must both be main subtitle languages or both additional."));
322 auto j = ref_fonts.begin ();
323 auto k = fonts.begin ();
325 while (j != ref_fonts.end ()) {
327 throw JoinError (_("Content to be joined must use the same fonts."));
335 _burn = ref->burn ();
336 _x_offset = ref->x_offset ();
337 _y_offset = ref->y_offset ();
338 _x_scale = ref->x_scale ();
339 _y_scale = ref->y_scale ();
341 _line_spacing = ref->line_spacing ();
342 _fade_in = ref->fade_in ();
343 _fade_out = ref->fade_out ();
344 _outline_width = ref->outline_width ();
345 _type = ref->type ();
346 _original_type = ref->original_type ();
347 _dcp_track = ref->dcp_track ();
348 _language = ref->language ();
349 _language_is_additional = ref->language_is_additional ();
354 /** _mutex must not be held on entry */
356 TextContent::as_xml (xmlpp::Node* root) const
358 boost::mutex::scoped_lock lm (_mutex);
360 auto text = root->add_child ("Text");
362 text->add_child("Use")->add_child_text (_use ? "1" : "0");
363 text->add_child("Burn")->add_child_text (_burn ? "1" : "0");
364 text->add_child("XOffset")->add_child_text (raw_convert<string> (_x_offset));
365 text->add_child("YOffset")->add_child_text (raw_convert<string> (_y_offset));
366 text->add_child("XScale")->add_child_text (raw_convert<string> (_x_scale));
367 text->add_child("YScale")->add_child_text (raw_convert<string> (_y_scale));
369 text->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
370 text->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
371 text->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
375 case dcp::Effect::NONE:
376 text->add_child("Effect")->add_child_text("none");
378 case dcp::Effect::BORDER:
379 text->add_child("Effect")->add_child_text("outline");
381 case dcp::Effect::SHADOW:
382 text->add_child("Effect")->add_child_text("shadow");
386 if (_effect_colour) {
387 text->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
388 text->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
389 text->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
391 text->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
393 text->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
396 text->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
398 text->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
400 for (auto i: _fonts) {
401 i->as_xml (text->add_child("Font"));
404 text->add_child("Type")->add_child_text (text_type_to_string(_type));
405 text->add_child("OriginalType")->add_child_text (text_type_to_string(_original_type));
407 _dcp_track->as_xml(text->add_child("DCPTrack"));
410 auto lang = text->add_child("Language");
411 lang->add_child_text (_language->to_string());
412 lang->set_attribute ("Additional", _language_is_additional ? "1" : "0");
417 TextContent::identifier () const
419 auto s = raw_convert<string> (x_scale())
420 + "_" + raw_convert<string> (y_scale())
421 + "_" + raw_convert<string> (x_offset())
422 + "_" + raw_convert<string> (y_offset())
423 + "_" + raw_convert<string> (line_spacing())
424 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
425 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
426 + "_" + raw_convert<string> (outline_width())
427 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
428 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::Effect::NONE)))
429 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string())
430 + "_" + raw_convert<string> (_parent->video_frame_rate().get_value_or(0));
432 /* XXX: I suppose really _fonts shouldn't be in here, since not all
433 types of subtitle content involve fonts.
435 for (auto f: _fonts) {
436 s += "_" + f->file().get_value_or("Default").string();
439 /* The DCP track and language are for metadata only, and don't affect how this content looks */
445 TextContent::add_font (shared_ptr<Font> font)
447 boost::mutex::scoped_lock lm(_mutex);
449 DCPOMATIC_ASSERT(!get_font_unlocked(font->id()));
450 _fonts.push_back (font);
455 TextContent::connect_to_fonts ()
457 for (auto const& i: _font_connections) {
461 _font_connections.clear ();
463 for (auto i: _fonts) {
464 _font_connections.push_back (i->Changed.connect (boost::bind (&TextContent::font_changed, this)));
469 TextContent::font_changed ()
472 ContentChangeSignaller cc (_parent, TextContentProperty::FONTS);
476 TextContent::set_colour (dcp::Colour colour)
478 maybe_set (_colour, colour, TextContentProperty::COLOUR);
482 TextContent::unset_colour ()
484 maybe_set (_colour, optional<dcp::Colour>(), TextContentProperty::COLOUR);
488 TextContent::set_effect (dcp::Effect e)
490 maybe_set (_effect, e, TextContentProperty::EFFECT);
494 TextContent::unset_effect ()
496 maybe_set (_effect, optional<dcp::Effect>(), TextContentProperty::EFFECT);
500 TextContent::set_effect_colour (dcp::Colour colour)
502 maybe_set (_effect_colour, colour, TextContentProperty::EFFECT_COLOUR);
506 TextContent::unset_effect_colour ()
508 maybe_set (_effect_colour, optional<dcp::Colour>(), TextContentProperty::EFFECT_COLOUR);
512 TextContent::set_use (bool u)
514 maybe_set (_use, u, TextContentProperty::USE);
518 TextContent::set_burn (bool b)
520 maybe_set (_burn, b, TextContentProperty::BURN);
524 TextContent::set_x_offset (double o)
526 maybe_set (_x_offset, o, TextContentProperty::X_OFFSET);
530 TextContent::set_y_offset (double o)
532 maybe_set (_y_offset, o, TextContentProperty::Y_OFFSET);
536 TextContent::set_x_scale (double s)
538 maybe_set (_x_scale, s, TextContentProperty::X_SCALE);
542 TextContent::set_y_scale (double s)
544 maybe_set (_y_scale, s, TextContentProperty::Y_SCALE);
548 TextContent::set_line_spacing (double s)
550 maybe_set (_line_spacing, s, TextContentProperty::LINE_SPACING);
554 TextContent::set_fade_in (ContentTime t)
556 maybe_set (_fade_in, t, TextContentProperty::FADE_IN);
560 TextContent::unset_fade_in ()
562 maybe_set (_fade_in, optional<ContentTime>(), TextContentProperty::FADE_IN);
566 TextContent::set_fade_out (ContentTime t)
568 maybe_set (_fade_out, t, TextContentProperty::FADE_OUT);
572 TextContent::unset_fade_out ()
574 maybe_set (_fade_out, optional<ContentTime>(), TextContentProperty::FADE_OUT);
578 TextContent::set_type (TextType type)
580 maybe_set (_type, type, TextContentProperty::TYPE);
584 TextContent::set_outline_width (int w)
586 maybe_set (_outline_width, w, TextContentProperty::OUTLINE_WIDTH);
590 TextContent::set_dcp_track (DCPTextTrack t)
592 maybe_set (_dcp_track, t, TextContentProperty::DCP_TRACK);
596 TextContent::unset_dcp_track ()
598 maybe_set (_dcp_track, optional<DCPTextTrack>(), TextContentProperty::DCP_TRACK);
602 TextContent::set_language (optional<dcp::LanguageTag> language)
604 maybe_set (_language, language, TextContentProperty::LANGUAGE);
608 TextContent::set_language_is_additional (bool additional)
610 maybe_set (_language_is_additional, additional, TextContentProperty::LANGUAGE_IS_ADDITIONAL);
614 TextContent::take_settings_from (shared_ptr<const TextContent> c)
618 set_x_offset (c->_x_offset);
619 set_y_offset (c->_y_offset);
620 set_x_scale (c->_x_scale);
621 set_y_scale (c->_y_scale);
622 maybe_set (_fonts, c->_fonts, TextContentProperty::FONTS);
624 set_colour (*c->_colour);
629 set_effect (*c->_effect);
631 if (c->_effect_colour) {
632 set_effect_colour (*c->_effect_colour);
634 unset_effect_colour ();
636 set_line_spacing (c->_line_spacing);
638 set_fade_in (*c->_fade_in);
641 set_fade_out (*c->_fade_out);
643 set_outline_width (c->_outline_width);
645 set_dcp_track (c->_dcp_track.get());
649 set_language (c->_language);
650 set_language_is_additional (c->_language_is_additional);
654 shared_ptr<dcpomatic::Font>
655 TextContent::get_font(string id) const
657 boost::mutex::scoped_lock lm(_mutex);
658 return get_font_unlocked(id);
662 shared_ptr<dcpomatic::Font>
663 TextContent::get_font_unlocked(string id) const
665 auto iter = std::find_if(_fonts.begin(), _fonts.end(), [&id](shared_ptr<dcpomatic::Font> font) {
666 return font->id() == id;
669 if (iter == _fonts.end()) {
678 TextContent::clear_fonts()
680 boost::mutex::scoped_lock lm(_mutex);