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