2 Copyright (C) 2013-2020 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/>.
21 #include "text_content.h"
23 #include "exceptions.h"
26 #include <dcp/raw_convert.h>
27 #include <libcxml/cxml.h>
28 #include <libxml++/libxml++.h>
37 using std::shared_ptr;
38 using std::dynamic_pointer_cast;
39 using boost::optional;
40 using dcp::raw_convert;
41 using namespace dcpomatic;
43 int const TextContentProperty::X_OFFSET = 500;
44 int const TextContentProperty::Y_OFFSET = 501;
45 int const TextContentProperty::X_SCALE = 502;
46 int const TextContentProperty::Y_SCALE = 503;
47 int const TextContentProperty::USE = 504;
48 int const TextContentProperty::BURN = 505;
49 int const TextContentProperty::FONTS = 506;
50 int const TextContentProperty::COLOUR = 507;
51 int const TextContentProperty::EFFECT = 508;
52 int const TextContentProperty::EFFECT_COLOUR = 509;
53 int const TextContentProperty::LINE_SPACING = 510;
54 int const TextContentProperty::FADE_IN = 511;
55 int const TextContentProperty::FADE_OUT = 512;
56 int const TextContentProperty::OUTLINE_WIDTH = 513;
57 int const TextContentProperty::TYPE = 514;
58 int const TextContentProperty::DCP_TRACK = 515;
60 TextContent::TextContent (Content* parent, TextType type, TextType original_type)
61 : ContentPart (parent)
71 , _original_type (original_type)
76 /** @return TextContents from node or <Text> nodes under node (according to version).
77 * The list could be empty if no TextContents are found.
79 list<shared_ptr<TextContent> >
80 TextContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
83 /* With old metadata FFmpeg content has the subtitle-related tags even with no
84 subtitle streams, so check for that.
86 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
87 return list<shared_ptr<TextContent> >();
90 /* Otherwise we can drop through to the newer logic */
94 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
95 return list<shared_ptr<TextContent> >();
97 list<shared_ptr<TextContent> > c;
98 c.push_back (shared_ptr<TextContent> (new TextContent (parent, node, version)));
102 if (!node->optional_node_child("Text")) {
103 return list<shared_ptr<TextContent> >();
106 list<shared_ptr<TextContent> > c;
107 for (auto i: node->node_children("Text")) {
108 c.push_back (shared_ptr<TextContent> (new TextContent (parent, i, version)));
114 TextContent::TextContent (Content* parent, cxml::ConstNodePtr node, int version)
115 : ContentPart (parent)
122 , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
123 , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (4))
124 , _type (TEXT_OPEN_SUBTITLE)
125 , _original_type (TEXT_OPEN_SUBTITLE)
128 _use = node->bool_child ("Use");
129 _burn = node->bool_child ("Burn");
130 } else if (version >= 32) {
131 _use = node->bool_child ("UseSubtitles");
132 _burn = node->bool_child ("BurnSubtitles");
136 _x_offset = node->number_child<double> ("XOffset");
137 _y_offset = node->number_child<double> ("YOffset");
138 } else if (version >= 7) {
139 _x_offset = node->number_child<double> ("SubtitleXOffset");
140 _y_offset = node->number_child<double> ("SubtitleYOffset");
142 _y_offset = node->number_child<double> ("SubtitleOffset");
145 if (node->optional_bool_child("Outline").get_value_or(false)) {
146 _effect = dcp::BORDER;
147 } else if (node->optional_bool_child("Shadow").get_value_or(false)) {
148 _effect = dcp::SHADOW;
153 optional<string> effect = node->optional_string_child("Effect");
155 if (*effect == "none") {
157 } else if (*effect == "outline") {
158 _effect = dcp::BORDER;
159 } else if (*effect == "shadow") {
160 _effect = dcp::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 optional<int> r = node->optional_number_child<int>("Red");
175 optional<int> g = node->optional_number_child<int>("Green");
176 optional<int> b = node->optional_number_child<int>("Blue");
178 _colour = dcp::Colour (*r, *g, *b);
182 optional<int> er = node->optional_number_child<int>("EffectRed");
183 optional<int> eg = node->optional_number_child<int>("EffectGreen");
184 optional<int> 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 list<cxml::NodePtr> fonts = node->node_children ("Font");
217 for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
218 _fonts.push_back (shared_ptr<Font> (new 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 cxml::ConstNodePtr dt = node->optional_node_child("DCPTrack");
232 _dcp_track = DCPTextTrack (dt);
236 TextContent::TextContent (Content* parent, vector<shared_ptr<Content> > c)
237 : ContentPart (parent)
239 /* This constructor is for join which is only supported for content types
240 that have a single text, so we can use only_text() here.
242 shared_ptr<TextContent> ref = c[0]->only_text();
243 DCPOMATIC_ASSERT (ref);
244 list<shared_ptr<Font> > ref_fonts = ref->fonts ();
246 for (size_t i = 1; i < c.size(); ++i) {
248 if (c[i]->only_text()->use() != ref->use()) {
249 throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
252 if (c[i]->only_text()->burn() != ref->burn()) {
253 throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
256 if (c[i]->only_text()->x_offset() != ref->x_offset()) {
257 throw JoinError (_("Content to be joined must have the same subtitle X offset."));
260 if (c[i]->only_text()->y_offset() != ref->y_offset()) {
261 throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
264 if (c[i]->only_text()->x_scale() != ref->x_scale()) {
265 throw JoinError (_("Content to be joined must have the same subtitle X scale."));
268 if (c[i]->only_text()->y_scale() != ref->y_scale()) {
269 throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
272 if (c[i]->only_text()->line_spacing() != ref->line_spacing()) {
273 throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
276 if ((c[i]->only_text()->fade_in() != ref->fade_in()) || (c[i]->only_text()->fade_out() != ref->fade_out())) {
277 throw JoinError (_("Content to be joined must have the same subtitle fades."));
280 if ((c[i]->only_text()->outline_width() != ref->outline_width())) {
281 throw JoinError (_("Content to be joined must have the same outline width."));
284 list<shared_ptr<Font> > fonts = c[i]->only_text()->fonts ();
285 if (fonts.size() != ref_fonts.size()) {
286 throw JoinError (_("Content to be joined must use the same fonts."));
289 if (c[i]->only_text()->dcp_track() != ref->dcp_track()) {
290 throw JoinError (_("Content to be joined must use the same DCP track."));
293 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
294 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
296 while (j != ref_fonts.end ()) {
298 throw JoinError (_("Content to be joined must use the same fonts."));
306 _burn = ref->burn ();
307 _x_offset = ref->x_offset ();
308 _y_offset = ref->y_offset ();
309 _x_scale = ref->x_scale ();
310 _y_scale = ref->y_scale ();
312 _line_spacing = ref->line_spacing ();
313 _fade_in = ref->fade_in ();
314 _fade_out = ref->fade_out ();
315 _outline_width = ref->outline_width ();
316 _type = ref->type ();
317 _original_type = ref->original_type ();
318 _dcp_track = ref->dcp_track ();
323 /** _mutex must not be held on entry */
325 TextContent::as_xml (xmlpp::Node* root) const
327 boost::mutex::scoped_lock lm (_mutex);
329 xmlpp::Element* text = root->add_child ("Text");
331 text->add_child("Use")->add_child_text (_use ? "1" : "0");
332 text->add_child("Burn")->add_child_text (_burn ? "1" : "0");
333 text->add_child("XOffset")->add_child_text (raw_convert<string> (_x_offset));
334 text->add_child("YOffset")->add_child_text (raw_convert<string> (_y_offset));
335 text->add_child("XScale")->add_child_text (raw_convert<string> (_x_scale));
336 text->add_child("YScale")->add_child_text (raw_convert<string> (_y_scale));
338 text->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
339 text->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
340 text->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
345 text->add_child("Effect")->add_child_text("none");
348 text->add_child("Effect")->add_child_text("outline");
351 text->add_child("Effect")->add_child_text("shadow");
355 if (_effect_colour) {
356 text->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
357 text->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
358 text->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
360 text->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
362 text->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
365 text->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
367 text->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
369 for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
370 (*i)->as_xml (text->add_child("Font"));
373 text->add_child("Type")->add_child_text (text_type_to_string(_type));
374 text->add_child("OriginalType")->add_child_text (text_type_to_string(_original_type));
376 _dcp_track->as_xml(text->add_child("DCPTrack"));
381 TextContent::identifier () const
383 string s = raw_convert<string> (x_scale())
384 + "_" + raw_convert<string> (y_scale())
385 + "_" + raw_convert<string> (x_offset())
386 + "_" + raw_convert<string> (y_offset())
387 + "_" + raw_convert<string> (line_spacing())
388 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
389 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
390 + "_" + raw_convert<string> (outline_width())
391 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
392 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::NONE)))
393 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string())
394 + "_" + raw_convert<string> (_parent->video_frame_rate().get_value_or(0));
396 /* XXX: I suppose really _fonts shouldn't be in here, since not all
397 types of subtitle content involve fonts.
399 for (auto f: _fonts) {
400 s += "_" + f->file().get_value_or("Default").string();
403 /* The DCP track is for metadata only, and doesn't affect how this content looks */
409 TextContent::add_font (shared_ptr<Font> font)
411 _fonts.push_back (font);
416 TextContent::connect_to_fonts ()
418 for (auto const& i: _font_connections) {
422 _font_connections.clear ();
424 for (auto i: _fonts) {
425 _font_connections.push_back (i->Changed.connect (boost::bind (&TextContent::font_changed, this)));
430 TextContent::font_changed ()
433 ChangeSignaller<Content> cc (_parent, TextContentProperty::FONTS);
437 TextContent::set_colour (dcp::Colour colour)
439 maybe_set (_colour, colour, TextContentProperty::COLOUR);
443 TextContent::unset_colour ()
445 maybe_set (_colour, optional<dcp::Colour>(), TextContentProperty::COLOUR);
449 TextContent::set_effect (dcp::Effect e)
451 maybe_set (_effect, e, TextContentProperty::EFFECT);
455 TextContent::unset_effect ()
457 maybe_set (_effect, optional<dcp::Effect>(), TextContentProperty::EFFECT);
461 TextContent::set_effect_colour (dcp::Colour colour)
463 maybe_set (_effect_colour, colour, TextContentProperty::EFFECT_COLOUR);
467 TextContent::unset_effect_colour ()
469 maybe_set (_effect_colour, optional<dcp::Colour>(), TextContentProperty::EFFECT_COLOUR);
473 TextContent::set_use (bool u)
475 maybe_set (_use, u, TextContentProperty::USE);
479 TextContent::set_burn (bool b)
481 maybe_set (_burn, b, TextContentProperty::BURN);
485 TextContent::set_x_offset (double o)
487 maybe_set (_x_offset, o, TextContentProperty::X_OFFSET);
491 TextContent::set_y_offset (double o)
493 maybe_set (_y_offset, o, TextContentProperty::Y_OFFSET);
497 TextContent::set_x_scale (double s)
499 maybe_set (_x_scale, s, TextContentProperty::X_SCALE);
503 TextContent::set_y_scale (double s)
505 maybe_set (_y_scale, s, TextContentProperty::Y_SCALE);
509 TextContent::set_line_spacing (double s)
511 maybe_set (_line_spacing, s, TextContentProperty::LINE_SPACING);
515 TextContent::set_fade_in (ContentTime t)
517 maybe_set (_fade_in, t, TextContentProperty::FADE_IN);
521 TextContent::unset_fade_in ()
523 maybe_set (_fade_in, optional<ContentTime>(), TextContentProperty::FADE_IN);
527 TextContent::set_fade_out (ContentTime t)
529 maybe_set (_fade_out, t, TextContentProperty::FADE_OUT);
533 TextContent::unset_fade_out ()
535 maybe_set (_fade_out, optional<ContentTime>(), TextContentProperty::FADE_OUT);
539 TextContent::set_type (TextType type)
541 maybe_set (_type, type, TextContentProperty::TYPE);
545 TextContent::set_outline_width (int w)
547 maybe_set (_outline_width, w, TextContentProperty::OUTLINE_WIDTH);
551 TextContent::set_dcp_track (DCPTextTrack t)
553 maybe_set (_dcp_track, t, TextContentProperty::DCP_TRACK);
557 TextContent::unset_dcp_track ()
559 maybe_set (_dcp_track, optional<DCPTextTrack>(), TextContentProperty::DCP_TRACK);
563 TextContent::take_settings_from (shared_ptr<const TextContent> c)
567 set_x_offset (c->_x_offset);
568 set_y_offset (c->_y_offset);
569 set_x_scale (c->_x_scale);
570 set_y_scale (c->_y_scale);
571 maybe_set (_fonts, c->_fonts, TextContentProperty::FONTS);
573 set_colour (*c->_colour);
578 set_effect (*c->_effect);
580 if (c->_effect_colour) {
581 set_effect_colour (*c->_effect_colour);
583 unset_effect_colour ();
585 set_line_spacing (c->_line_spacing);
587 set_fade_in (*c->_fade_in);
590 set_fade_out (*c->_fade_out);
592 set_outline_width (c->_outline_width);
594 set_dcp_track (c->_dcp_track.get());