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