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