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 list could be empty if no TextContents are found.
86 list<shared_ptr<TextContent>>
87 TextContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
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) };
107 list<shared_ptr<TextContent>> c;
108 for (auto i: node->node_children("Text")) {
109 c.push_back (make_shared<TextContent>(parent, i, version));
115 TextContent::TextContent (Content* parent, cxml::ConstNodePtr node, int version)
116 : ContentPart (parent)
123 , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or(1))
124 , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or(4))
125 , _type (TextType::OPEN_SUBTITLE)
126 , _original_type (TextType::OPEN_SUBTITLE)
129 _use = node->bool_child ("Use");
130 _burn = node->bool_child ("Burn");
131 } else if (version >= 32) {
132 _use = node->bool_child ("UseSubtitles");
133 _burn = node->bool_child ("BurnSubtitles");
137 _x_offset = node->number_child<double> ("XOffset");
138 _y_offset = node->number_child<double> ("YOffset");
139 } else if (version >= 7) {
140 _x_offset = node->number_child<double> ("SubtitleXOffset");
141 _y_offset = node->number_child<double> ("SubtitleYOffset");
143 _y_offset = node->number_child<double> ("SubtitleOffset");
146 if (node->optional_bool_child("Outline").get_value_or(false)) {
147 _effect = dcp::Effect::BORDER;
148 } else if (node->optional_bool_child("Shadow").get_value_or(false)) {
149 _effect = dcp::Effect::SHADOW;
151 _effect = dcp::Effect::NONE;
154 auto effect = node->optional_string_child("Effect");
156 if (*effect == "none") {
157 _effect = dcp::Effect::NONE;
158 } else if (*effect == "outline") {
159 _effect = dcp::Effect::BORDER;
160 } else if (*effect == "shadow") {
161 _effect = dcp::Effect::SHADOW;
166 _x_scale = node->number_child<double> ("XScale");
167 _y_scale = node->number_child<double> ("YScale");
168 } else if (version >= 10) {
169 _x_scale = node->number_child<double> ("SubtitleXScale");
170 _y_scale = node->number_child<double> ("SubtitleYScale");
172 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
175 auto r = node->optional_number_child<int>("Red");
176 auto g = node->optional_number_child<int>("Green");
177 auto b = node->optional_number_child<int>("Blue");
179 _colour = dcp::Colour (*r, *g, *b);
183 auto er = node->optional_number_child<int>("EffectRed");
184 auto eg = node->optional_number_child<int>("EffectGreen");
185 auto eb = node->optional_number_child<int>("EffectBlue");
186 if (er && eg && eb) {
187 _effect_colour = dcp::Colour (*er, *eg, *eb);
190 _effect_colour = dcp::Colour (
191 node->optional_number_child<int>("OutlineRed").get_value_or(255),
192 node->optional_number_child<int>("OutlineGreen").get_value_or(255),
193 node->optional_number_child<int>("OutlineBlue").get_value_or(255)
199 fi = node->optional_number_child<Frame>("FadeIn");
201 fi = node->optional_number_child<Frame>("SubtitleFadeIn");
204 _fade_in = ContentTime (*fi);
209 fo = node->optional_number_child<Frame>("FadeOut");
211 fo = node->optional_number_child<Frame>("SubtitleFadeOut");
214 _fade_out = ContentTime (*fo);
217 for (auto i: node->node_children ("Font")) {
218 _fonts.push_back (make_shared<Font>(i));
224 _type = string_to_text_type (node->optional_string_child("Type").get_value_or("open"));
225 if (node->optional_string_child("OriginalType")) {
226 _original_type = string_to_text_type (node->optional_string_child("OriginalType").get());
230 auto dt = node->optional_node_child("DCPTrack");
232 _dcp_track = DCPTextTrack (dt);
235 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;
243 TextContent::TextContent (Content* parent, vector<shared_ptr<Content>> c)
244 : ContentPart (parent)
246 /* This constructor is for join which is only supported for content types
247 that have a single text, so we can use only_text() here.
249 auto ref = c[0]->only_text();
250 DCPOMATIC_ASSERT (ref);
251 auto ref_fonts = ref->fonts ();
253 for (size_t i = 1; i < c.size(); ++i) {
255 if (c[i]->only_text()->use() != ref->use()) {
256 throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
259 if (c[i]->only_text()->burn() != ref->burn()) {
260 throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
263 if (c[i]->only_text()->x_offset() != ref->x_offset()) {
264 throw JoinError (_("Content to be joined must have the same subtitle X offset."));
267 if (c[i]->only_text()->y_offset() != ref->y_offset()) {
268 throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
271 if (c[i]->only_text()->x_scale() != ref->x_scale()) {
272 throw JoinError (_("Content to be joined must have the same subtitle X scale."));
275 if (c[i]->only_text()->y_scale() != ref->y_scale()) {
276 throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
279 if (c[i]->only_text()->line_spacing() != ref->line_spacing()) {
280 throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
283 if ((c[i]->only_text()->fade_in() != ref->fade_in()) || (c[i]->only_text()->fade_out() != ref->fade_out())) {
284 throw JoinError (_("Content to be joined must have the same subtitle fades."));
287 if ((c[i]->only_text()->outline_width() != ref->outline_width())) {
288 throw JoinError (_("Content to be joined must have the same outline width."));
291 auto fonts = c[i]->only_text()->fonts ();
292 if (fonts.size() != ref_fonts.size()) {
293 throw JoinError (_("Content to be joined must use the same fonts."));
296 if (c[i]->only_text()->dcp_track() != ref->dcp_track()) {
297 throw JoinError (_("Content to be joined must use the same DCP track."));
300 if (c[i]->only_text()->language() != ref->language()) {
301 throw JoinError (_("Content to be joined must use the same text language."));
304 if (c[i]->only_text()->language_is_additional() != ref->language_is_additional()) {
305 throw JoinError (_("Content to be joined must both be main subtitle languages or both additional."));
308 auto j = ref_fonts.begin ();
309 auto k = fonts.begin ();
311 while (j != ref_fonts.end ()) {
313 throw JoinError (_("Content to be joined must use the same fonts."));
321 _burn = ref->burn ();
322 _x_offset = ref->x_offset ();
323 _y_offset = ref->y_offset ();
324 _x_scale = ref->x_scale ();
325 _y_scale = ref->y_scale ();
327 _line_spacing = ref->line_spacing ();
328 _fade_in = ref->fade_in ();
329 _fade_out = ref->fade_out ();
330 _outline_width = ref->outline_width ();
331 _type = ref->type ();
332 _original_type = ref->original_type ();
333 _dcp_track = ref->dcp_track ();
334 _language = ref->language ();
335 _language_is_additional = ref->language_is_additional ();
340 /** _mutex must not be held on entry */
342 TextContent::as_xml (xmlpp::Node* root) const
344 boost::mutex::scoped_lock lm (_mutex);
346 auto text = root->add_child ("Text");
348 text->add_child("Use")->add_child_text (_use ? "1" : "0");
349 text->add_child("Burn")->add_child_text (_burn ? "1" : "0");
350 text->add_child("XOffset")->add_child_text (raw_convert<string> (_x_offset));
351 text->add_child("YOffset")->add_child_text (raw_convert<string> (_y_offset));
352 text->add_child("XScale")->add_child_text (raw_convert<string> (_x_scale));
353 text->add_child("YScale")->add_child_text (raw_convert<string> (_y_scale));
355 text->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
356 text->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
357 text->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
361 case dcp::Effect::NONE:
362 text->add_child("Effect")->add_child_text("none");
364 case dcp::Effect::BORDER:
365 text->add_child("Effect")->add_child_text("outline");
367 case dcp::Effect::SHADOW:
368 text->add_child("Effect")->add_child_text("shadow");
372 if (_effect_colour) {
373 text->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
374 text->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
375 text->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
377 text->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
379 text->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
382 text->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
384 text->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
386 for (auto i: _fonts) {
387 i->as_xml (text->add_child("Font"));
390 text->add_child("Type")->add_child_text (text_type_to_string(_type));
391 text->add_child("OriginalType")->add_child_text (text_type_to_string(_original_type));
393 _dcp_track->as_xml(text->add_child("DCPTrack"));
396 auto lang = text->add_child("Language");
397 lang->add_child_text (_language->to_string());
398 lang->set_attribute ("Additional", _language_is_additional ? "1" : "0");
403 TextContent::identifier () const
405 auto s = raw_convert<string> (x_scale())
406 + "_" + raw_convert<string> (y_scale())
407 + "_" + raw_convert<string> (x_offset())
408 + "_" + raw_convert<string> (y_offset())
409 + "_" + raw_convert<string> (line_spacing())
410 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
411 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
412 + "_" + raw_convert<string> (outline_width())
413 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
414 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::Effect::NONE)))
415 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string())
416 + "_" + raw_convert<string> (_parent->video_frame_rate().get_value_or(0));
418 /* XXX: I suppose really _fonts shouldn't be in here, since not all
419 types of subtitle content involve fonts.
421 for (auto f: _fonts) {
422 s += "_" + f->file().get_value_or("Default").string();
425 /* The DCP track and language are for metadata only, and don't affect how this content looks */
431 TextContent::add_font (shared_ptr<Font> font)
433 _fonts.push_back (font);
438 TextContent::connect_to_fonts ()
440 for (auto const& i: _font_connections) {
444 _font_connections.clear ();
446 for (auto i: _fonts) {
447 _font_connections.push_back (i->Changed.connect (boost::bind (&TextContent::font_changed, this)));
452 TextContent::font_changed ()
455 ContentChangeSignaller cc (_parent, TextContentProperty::FONTS);
459 TextContent::set_colour (dcp::Colour colour)
461 maybe_set (_colour, colour, TextContentProperty::COLOUR);
465 TextContent::unset_colour ()
467 maybe_set (_colour, optional<dcp::Colour>(), TextContentProperty::COLOUR);
471 TextContent::set_effect (dcp::Effect e)
473 maybe_set (_effect, e, TextContentProperty::EFFECT);
477 TextContent::unset_effect ()
479 maybe_set (_effect, optional<dcp::Effect>(), TextContentProperty::EFFECT);
483 TextContent::set_effect_colour (dcp::Colour colour)
485 maybe_set (_effect_colour, colour, TextContentProperty::EFFECT_COLOUR);
489 TextContent::unset_effect_colour ()
491 maybe_set (_effect_colour, optional<dcp::Colour>(), TextContentProperty::EFFECT_COLOUR);
495 TextContent::set_use (bool u)
497 maybe_set (_use, u, TextContentProperty::USE);
501 TextContent::set_burn (bool b)
503 maybe_set (_burn, b, TextContentProperty::BURN);
507 TextContent::set_x_offset (double o)
509 maybe_set (_x_offset, o, TextContentProperty::X_OFFSET);
513 TextContent::set_y_offset (double o)
515 maybe_set (_y_offset, o, TextContentProperty::Y_OFFSET);
519 TextContent::set_x_scale (double s)
521 maybe_set (_x_scale, s, TextContentProperty::X_SCALE);
525 TextContent::set_y_scale (double s)
527 maybe_set (_y_scale, s, TextContentProperty::Y_SCALE);
531 TextContent::set_line_spacing (double s)
533 maybe_set (_line_spacing, s, TextContentProperty::LINE_SPACING);
537 TextContent::set_fade_in (ContentTime t)
539 maybe_set (_fade_in, t, TextContentProperty::FADE_IN);
543 TextContent::unset_fade_in ()
545 maybe_set (_fade_in, optional<ContentTime>(), TextContentProperty::FADE_IN);
549 TextContent::set_fade_out (ContentTime t)
551 maybe_set (_fade_out, t, TextContentProperty::FADE_OUT);
555 TextContent::unset_fade_out ()
557 maybe_set (_fade_out, optional<ContentTime>(), TextContentProperty::FADE_OUT);
561 TextContent::set_type (TextType type)
563 maybe_set (_type, type, TextContentProperty::TYPE);
567 TextContent::set_outline_width (int w)
569 maybe_set (_outline_width, w, TextContentProperty::OUTLINE_WIDTH);
573 TextContent::set_dcp_track (DCPTextTrack t)
575 maybe_set (_dcp_track, t, TextContentProperty::DCP_TRACK);
579 TextContent::unset_dcp_track ()
581 maybe_set (_dcp_track, optional<DCPTextTrack>(), TextContentProperty::DCP_TRACK);
585 TextContent::set_language (optional<dcp::LanguageTag> language)
587 maybe_set (_language, language, TextContentProperty::LANGUAGE);
591 TextContent::set_language_is_additional (bool additional)
593 maybe_set (_language_is_additional, additional, TextContentProperty::LANGUAGE_IS_ADDITIONAL);
597 TextContent::take_settings_from (shared_ptr<const TextContent> c)
601 set_x_offset (c->_x_offset);
602 set_y_offset (c->_y_offset);
603 set_x_scale (c->_x_scale);
604 set_y_scale (c->_y_scale);
605 maybe_set (_fonts, c->_fonts, TextContentProperty::FONTS);
607 set_colour (*c->_colour);
612 set_effect (*c->_effect);
614 if (c->_effect_colour) {
615 set_effect_colour (*c->_effect_colour);
617 unset_effect_colour ();
619 set_line_spacing (c->_line_spacing);
621 set_fade_in (*c->_fade_in);
624 set_fade_out (*c->_fade_out);
626 set_outline_width (c->_outline_width);
628 set_dcp_track (c->_dcp_track.get());
632 set_language (c->_language);
633 set_language_is_additional (c->_language_is_additional);