s/use_template/take_settings_from/g
[dcpomatic.git] / src / lib / subtitle_content.cc
1 /*
2     Copyright (C) 2013-2016 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 #include "subtitle_content.h"
22 #include "util.h"
23 #include "exceptions.h"
24 #include "font.h"
25 #include "content.h"
26 #include <dcp/raw_convert.h>
27 #include <libcxml/cxml.h>
28 #include <libxml++/libxml++.h>
29 #include <boost/foreach.hpp>
30 #include <iostream>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::vector;
36 using std::cout;
37 using std::list;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40 using dcp::raw_convert;
41
42 int const SubtitleContentProperty::X_OFFSET = 500;
43 int const SubtitleContentProperty::Y_OFFSET = 501;
44 int const SubtitleContentProperty::X_SCALE = 502;
45 int const SubtitleContentProperty::Y_SCALE = 503;
46 int const SubtitleContentProperty::USE = 504;
47 int const SubtitleContentProperty::BURN = 505;
48 int const SubtitleContentProperty::LANGUAGE = 506;
49 int const SubtitleContentProperty::FONTS = 507;
50 int const SubtitleContentProperty::COLOUR = 508;
51 int const SubtitleContentProperty::OUTLINE = 509;
52 int const SubtitleContentProperty::SHADOW = 510;
53 int const SubtitleContentProperty::EFFECT_COLOUR = 511;
54 int const SubtitleContentProperty::LINE_SPACING = 512;
55 int const SubtitleContentProperty::FADE_IN = 513;
56 int const SubtitleContentProperty::FADE_OUT = 514;
57 int const SubtitleContentProperty::OUTLINE_WIDTH = 515;
58
59 SubtitleContent::SubtitleContent (Content* parent)
60         : ContentPart (parent)
61         , _use (false)
62         , _burn (false)
63         , _x_offset (0)
64         , _y_offset (0)
65         , _x_scale (1)
66         , _y_scale (1)
67         , _colour (255, 255, 255)
68         , _outline (false)
69         , _shadow (false)
70         , _effect_colour (0, 0, 0)
71         , _line_spacing (1)
72         , _outline_width (2)
73 {
74
75 }
76
77 shared_ptr<SubtitleContent>
78 SubtitleContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
79 {
80         if (version < 34) {
81                 /* With old metadata FFmpeg content has the subtitle-related tags even with no
82                    subtitle streams, so check for that.
83                 */
84                 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
85                         return shared_ptr<SubtitleContent> ();
86                 }
87
88                 /* Otherwise we can drop through to the newer logic */
89         }
90
91         if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
92                 return shared_ptr<SubtitleContent> ();
93         }
94
95         return shared_ptr<SubtitleContent> (new SubtitleContent (parent, node, version));
96 }
97
98 SubtitleContent::SubtitleContent (Content* parent, cxml::ConstNodePtr node, int version)
99         : ContentPart (parent)
100         , _use (false)
101         , _burn (false)
102         , _x_offset (0)
103         , _y_offset (0)
104         , _x_scale (1)
105         , _y_scale (1)
106         , _colour (
107                 node->optional_number_child<int>("Red").get_value_or(255),
108                 node->optional_number_child<int>("Green").get_value_or(255),
109                 node->optional_number_child<int>("Blue").get_value_or(255)
110                 )
111         , _outline (node->optional_bool_child("Outline").get_value_or(false))
112         , _shadow (node->optional_bool_child("Shadow").get_value_or(false))
113         , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
114         , _fade_in (node->optional_number_child<Frame>("SubtitleFadeIn").get_value_or (0))
115         , _fade_out (node->optional_number_child<Frame>("SubtitleFadeOut").get_value_or (0))
116         , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (2))
117 {
118         if (version >= 32) {
119                 _use = node->bool_child ("UseSubtitles");
120                 _burn = node->bool_child ("BurnSubtitles");
121         }
122
123         if (version >= 7) {
124                 _x_offset = node->number_child<double> ("SubtitleXOffset");
125                 _y_offset = node->number_child<double> ("SubtitleYOffset");
126         } else {
127                 _y_offset = node->number_child<double> ("SubtitleOffset");
128         }
129
130         if (version >= 10) {
131                 _x_scale = node->number_child<double> ("SubtitleXScale");
132                 _y_scale = node->number_child<double> ("SubtitleYScale");
133         } else {
134                 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
135         }
136
137         if (version >= 36) {
138                 _effect_colour = dcp::Colour (
139                         node->optional_number_child<int>("EffectRed").get_value_or(255),
140                         node->optional_number_child<int>("EffectGreen").get_value_or(255),
141                         node->optional_number_child<int>("EffectBlue").get_value_or(255)
142                         );
143         } else {
144                 _effect_colour = dcp::Colour (
145                         node->optional_number_child<int>("OutlineRed").get_value_or(255),
146                         node->optional_number_child<int>("OutlineGreen").get_value_or(255),
147                         node->optional_number_child<int>("OutlineBlue").get_value_or(255)
148                         );
149         }
150
151         _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
152
153         list<cxml::NodePtr> fonts = node->node_children ("Font");
154         for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
155                 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
156         }
157
158         connect_to_fonts ();
159 }
160
161 SubtitleContent::SubtitleContent (Content* parent, vector<shared_ptr<Content> > c)
162         : ContentPart (parent)
163 {
164         shared_ptr<SubtitleContent> ref = c[0]->subtitle;
165         DCPOMATIC_ASSERT (ref);
166         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
167
168         for (size_t i = 1; i < c.size(); ++i) {
169
170                 if (c[i]->subtitle->use() != ref->use()) {
171                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
172                 }
173
174                 if (c[i]->subtitle->burn() != ref->burn()) {
175                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
176                 }
177
178                 if (c[i]->subtitle->x_offset() != ref->x_offset()) {
179                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
180                 }
181
182                 if (c[i]->subtitle->y_offset() != ref->y_offset()) {
183                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
184                 }
185
186                 if (c[i]->subtitle->x_scale() != ref->x_scale()) {
187                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
188                 }
189
190                 if (c[i]->subtitle->y_scale() != ref->y_scale()) {
191                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
192                 }
193
194                 if (c[i]->subtitle->line_spacing() != ref->line_spacing()) {
195                         throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
196                 }
197
198                 if ((c[i]->subtitle->fade_in() != ref->fade_in()) || (c[i]->subtitle->fade_out() != ref->fade_out())) {
199                         throw JoinError (_("Content to be joined must have the same subtitle fades."));
200                 }
201
202                 if ((c[i]->subtitle->outline_width() != ref->outline_width())) {
203                         throw JoinError (_("Content to be joined must have the same outline width."));
204                 }
205
206                 list<shared_ptr<Font> > fonts = c[i]->subtitle->fonts ();
207                 if (fonts.size() != ref_fonts.size()) {
208                         throw JoinError (_("Content to be joined must use the same fonts."));
209                 }
210
211                 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
212                 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
213
214                 while (j != ref_fonts.end ()) {
215                         if (**j != **k) {
216                                 throw JoinError (_("Content to be joined must use the same fonts."));
217                         }
218                         ++j;
219                         ++k;
220                 }
221         }
222
223         _use = ref->use ();
224         _burn = ref->burn ();
225         _x_offset = ref->x_offset ();
226         _y_offset = ref->y_offset ();
227         _x_scale = ref->x_scale ();
228         _y_scale = ref->y_scale ();
229         _language = ref->language ();
230         _fonts = ref_fonts;
231         _line_spacing = ref->line_spacing ();
232         _fade_in = ref->fade_in ();
233         _fade_out = ref->fade_out ();
234         _outline_width = ref->outline_width ();
235
236         connect_to_fonts ();
237 }
238
239 /** _mutex must not be held on entry */
240 void
241 SubtitleContent::as_xml (xmlpp::Node* root) const
242 {
243         boost::mutex::scoped_lock lm (_mutex);
244
245         root->add_child("UseSubtitles")->add_child_text (_use ? "1" : "0");
246         root->add_child("BurnSubtitles")->add_child_text (_burn ? "1" : "0");
247         root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
248         root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
249         root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
250         root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
251         root->add_child("SubtitleLanguage")->add_child_text (_language);
252         root->add_child("Red")->add_child_text (raw_convert<string> (_colour.r));
253         root->add_child("Green")->add_child_text (raw_convert<string> (_colour.g));
254         root->add_child("Blue")->add_child_text (raw_convert<string> (_colour.b));
255         root->add_child("Outline")->add_child_text (_outline ? "1" : "0");
256         root->add_child("Shadow")->add_child_text (_shadow ? "1" : "0");
257         root->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour.r));
258         root->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour.g));
259         root->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour.b));
260         root->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
261         root->add_child("SubtitleFadeIn")->add_child_text (raw_convert<string> (_fade_in.get()));
262         root->add_child("SubtitleFadeOut")->add_child_text (raw_convert<string> (_fade_out.get()));
263         root->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
264
265         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
266                 (*i)->as_xml (root->add_child("Font"));
267         }
268 }
269
270 string
271 SubtitleContent::identifier () const
272 {
273         string s = raw_convert<string> (x_scale())
274                 + "_" + raw_convert<string> (y_scale())
275                 + "_" + raw_convert<string> (x_offset())
276                 + "_" + raw_convert<string> (y_offset())
277                 + "_" + raw_convert<string> (line_spacing())
278                 + "_" + raw_convert<string> (fade_in().get())
279                 + "_" + raw_convert<string> (fade_out().get())
280                 + "_" + raw_convert<string> (outline_width());
281
282         /* XXX: I suppose really _fonts shouldn't be in here, since not all
283            types of subtitle content involve fonts.
284         */
285         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
286                 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
287                         s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
288                 }
289         }
290
291         /* The language is for metadata only, and doesn't affect
292            how this content looks.
293         */
294
295         return s;
296 }
297
298 void
299 SubtitleContent::add_font (shared_ptr<Font> font)
300 {
301         _fonts.push_back (font);
302         connect_to_fonts ();
303 }
304
305 void
306 SubtitleContent::connect_to_fonts ()
307 {
308         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
309                 i.disconnect ();
310         }
311
312         _font_connections.clear ();
313
314         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
315                 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
316         }
317 }
318
319 void
320 SubtitleContent::font_changed ()
321 {
322         _parent->signal_changed (SubtitleContentProperty::FONTS);
323 }
324
325 void
326 SubtitleContent::set_colour (dcp::Colour colour)
327 {
328         maybe_set (_colour, colour, SubtitleContentProperty::COLOUR);
329 }
330
331 void
332 SubtitleContent::set_outline (bool o)
333 {
334         maybe_set (_outline, o, SubtitleContentProperty::OUTLINE);
335 }
336
337 void
338 SubtitleContent::set_shadow (bool s)
339 {
340         maybe_set (_shadow, s, SubtitleContentProperty::SHADOW);
341 }
342
343 void
344 SubtitleContent::set_effect_colour (dcp::Colour colour)
345 {
346         maybe_set (_effect_colour, colour, SubtitleContentProperty::EFFECT_COLOUR);
347 }
348
349 void
350 SubtitleContent::set_use (bool u)
351 {
352         maybe_set (_use, u, SubtitleContentProperty::USE);
353 }
354
355 void
356 SubtitleContent::set_burn (bool b)
357 {
358         maybe_set (_burn, b, SubtitleContentProperty::BURN);
359 }
360
361 void
362 SubtitleContent::set_x_offset (double o)
363 {
364         maybe_set (_x_offset, o, SubtitleContentProperty::X_OFFSET);
365 }
366
367 void
368 SubtitleContent::set_y_offset (double o)
369 {
370         maybe_set (_y_offset, o, SubtitleContentProperty::Y_OFFSET);
371 }
372
373 void
374 SubtitleContent::set_x_scale (double s)
375 {
376         maybe_set (_x_scale, s, SubtitleContentProperty::X_SCALE);
377 }
378
379 void
380 SubtitleContent::set_y_scale (double s)
381 {
382         maybe_set (_y_scale, s, SubtitleContentProperty::Y_SCALE);
383 }
384
385 void
386 SubtitleContent::set_language (string language)
387 {
388         maybe_set (_language, language, SubtitleContentProperty::LANGUAGE);
389 }
390
391 void
392 SubtitleContent::set_line_spacing (double s)
393 {
394         maybe_set (_line_spacing, s, SubtitleContentProperty::LINE_SPACING);
395 }
396
397 void
398 SubtitleContent::set_fade_in (ContentTime t)
399 {
400         maybe_set (_fade_in, t, SubtitleContentProperty::FADE_IN);
401 }
402
403 void
404 SubtitleContent::set_fade_out (ContentTime t)
405 {
406         maybe_set (_fade_out, t, SubtitleContentProperty::FADE_OUT);
407 }
408
409 void
410 SubtitleContent::set_outline_width (int w)
411 {
412         maybe_set (_outline_width, w, SubtitleContentProperty::OUTLINE_WIDTH);
413 }
414
415 void
416 SubtitleContent::take_settings_from (shared_ptr<const SubtitleContent> c)
417 {
418         _use = c->_use;
419         _burn = c->_burn;
420         _x_offset = c->_x_offset;
421         _y_offset = c->_y_offset;
422         _x_scale = c->_x_scale;
423         _y_scale = c->_y_scale;
424         _fonts = c->_fonts;
425         _colour = c->_colour;
426         _outline = c->_outline;
427         _shadow = c->_shadow;
428         _effect_colour = c->_effect_colour;
429         _line_spacing = c->_line_spacing;
430         _fade_in = c->_fade_in;
431         _fade_out = c->_fade_out;
432         _outline_width = c->_outline_width;
433 }