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