Remove unnecessary Film variable in ContentPart.
[dcpomatic.git] / src / lib / subtitle_content.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "subtitle_content.h"
21 #include "util.h"
22 #include "exceptions.h"
23 #include "safe_stringstream.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::OUTLINE_COLOUR = 510;
52
53 SubtitleContent::SubtitleContent (Content* parent)
54         : ContentPart (parent)
55         , _use (false)
56         , _burn (false)
57         , _x_offset (0)
58         , _y_offset (0)
59         , _x_scale (1)
60         , _y_scale (1)
61         , _colour (255, 255, 255)
62         , _outline (false)
63         , _outline_colour (0, 0, 0)
64 {
65
66 }
67
68 shared_ptr<SubtitleContent>
69 SubtitleContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
70 {
71         if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
72                 return shared_ptr<SubtitleContent> ();
73         }
74
75         return shared_ptr<SubtitleContent> (new SubtitleContent (parent, node, version));
76 }
77
78 SubtitleContent::SubtitleContent (Content* parent, cxml::ConstNodePtr node, int version)
79         : ContentPart (parent)
80         , _use (false)
81         , _burn (false)
82         , _x_offset (0)
83         , _y_offset (0)
84         , _x_scale (1)
85         , _y_scale (1)
86         , _colour (
87                 node->optional_number_child<int>("Red").get_value_or(255),
88                 node->optional_number_child<int>("Green").get_value_or(255),
89                 node->optional_number_child<int>("Blue").get_value_or(255)
90                 )
91         , _outline (node->optional_bool_child("Outline").get_value_or(false))
92         , _outline_colour (
93                 node->optional_number_child<int>("OutlineRed").get_value_or(255),
94                 node->optional_number_child<int>("OutlineGreen").get_value_or(255),
95                 node->optional_number_child<int>("OutlineBlue").get_value_or(255)
96                 )
97 {
98         if (version >= 32) {
99                 _use = node->bool_child ("UseSubtitles");
100                 _burn = node->bool_child ("BurnSubtitles");
101         }
102
103         if (version >= 7) {
104                 _x_offset = node->number_child<double> ("SubtitleXOffset");
105                 _y_offset = node->number_child<double> ("SubtitleYOffset");
106         } else {
107                 _y_offset = node->number_child<double> ("SubtitleOffset");
108         }
109
110         if (version >= 10) {
111                 _x_scale = node->number_child<double> ("SubtitleXScale");
112                 _y_scale = node->number_child<double> ("SubtitleYScale");
113         } else {
114                 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
115         }
116
117         _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
118
119         list<cxml::NodePtr> fonts = node->node_children ("Font");
120         for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
121                 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
122         }
123
124         connect_to_fonts ();
125 }
126
127 SubtitleContent::SubtitleContent (Content* parent, vector<shared_ptr<Content> > c)
128         : ContentPart (parent)
129 {
130         shared_ptr<SubtitleContent> ref = c[0]->subtitle;
131         DCPOMATIC_ASSERT (ref);
132         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
133
134         for (size_t i = 1; i < c.size(); ++i) {
135
136                 if (c[i]->subtitle->use() != ref->use()) {
137                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
138                 }
139
140                 if (c[i]->subtitle->burn() != ref->burn()) {
141                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
142                 }
143
144                 if (c[i]->subtitle->x_offset() != ref->x_offset()) {
145                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
146                 }
147
148                 if (c[i]->subtitle->y_offset() != ref->y_offset()) {
149                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
150                 }
151
152                 if (c[i]->subtitle->x_scale() != ref->x_scale()) {
153                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
154                 }
155
156                 if (c[i]->subtitle->y_scale() != ref->y_scale()) {
157                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
158                 }
159
160                 list<shared_ptr<Font> > fonts = c[i]->subtitle->fonts ();
161                 if (fonts.size() != ref_fonts.size()) {
162                         throw JoinError (_("Content to be joined must use the same fonts."));
163                 }
164
165                 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
166                 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
167
168                 while (j != ref_fonts.end ()) {
169                         if (**j != **k) {
170                                 throw JoinError (_("Content to be joined must use the same fonts."));
171                         }
172                         ++j;
173                         ++k;
174                 }
175         }
176
177         _use = ref->use ();
178         _burn = ref->burn ();
179         _x_offset = ref->x_offset ();
180         _y_offset = ref->y_offset ();
181         _x_scale = ref->x_scale ();
182         _y_scale = ref->y_scale ();
183         _language = ref->language ();
184         _fonts = ref_fonts;
185
186         connect_to_fonts ();
187 }
188
189 /** _mutex must not be held on entry */
190 void
191 SubtitleContent::as_xml (xmlpp::Node* root) const
192 {
193         boost::mutex::scoped_lock lm (_mutex);
194
195         root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use));
196         root->add_child("BurnSubtitles")->add_child_text (raw_convert<string> (_burn));
197         root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
198         root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
199         root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
200         root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
201         root->add_child("SubtitleLanguage")->add_child_text (_language);
202         root->add_child("Red")->add_child_text (raw_convert<string> (_colour.r));
203         root->add_child("Green")->add_child_text (raw_convert<string> (_colour.g));
204         root->add_child("Blue")->add_child_text (raw_convert<string> (_colour.b));
205         root->add_child("Outline")->add_child_text (raw_convert<string> (_outline));
206         root->add_child("OutlineRed")->add_child_text (raw_convert<string> (_outline_colour.r));
207         root->add_child("OutlineGreen")->add_child_text (raw_convert<string> (_outline_colour.g));
208         root->add_child("OutlineBlue")->add_child_text (raw_convert<string> (_outline_colour.b));
209
210         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
211                 (*i)->as_xml (root->add_child("Font"));
212         }
213 }
214
215 string
216 SubtitleContent::identifier () const
217 {
218         SafeStringStream s;
219         s << raw_convert<string> (x_scale())
220           << "_" << raw_convert<string> (y_scale())
221           << "_" << raw_convert<string> (x_offset())
222           << "_" << raw_convert<string> (y_offset());
223
224         /* XXX: I suppose really _fonts shouldn't be in here, since not all
225            types of subtitle content involve fonts.
226         */
227         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
228                 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
229                         s << "_" << f->file(static_cast<FontFiles::Variant>(i)).get_value_or ("Default");
230                 }
231         }
232
233         /* The language is for metadata only, and doesn't affect
234            how this content looks.
235         */
236
237         return s.str ();
238 }
239
240 void
241 SubtitleContent::add_font (shared_ptr<Font> font)
242 {
243         _fonts.push_back (font);
244         connect_to_fonts ();
245 }
246
247 void
248 SubtitleContent::connect_to_fonts ()
249 {
250         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
251                 i.disconnect ();
252         }
253
254         _font_connections.clear ();
255
256         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
257                 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
258         }
259 }
260
261 void
262 SubtitleContent::font_changed ()
263 {
264         _parent->signal_changed (SubtitleContentProperty::FONTS);
265 }
266
267 void
268 SubtitleContent::set_colour (dcp::Colour colour)
269 {
270         maybe_set (_colour, colour, SubtitleContentProperty::COLOUR);
271 }
272
273 void
274 SubtitleContent::set_outline (bool o)
275 {
276         maybe_set (_outline, o, SubtitleContentProperty::OUTLINE);
277 }
278
279 void
280 SubtitleContent::set_outline_colour (dcp::Colour colour)
281 {
282         maybe_set (_outline_colour, colour, SubtitleContentProperty::OUTLINE_COLOUR);
283 }
284
285 void
286 SubtitleContent::set_use (bool u)
287 {
288         maybe_set (_use, u, SubtitleContentProperty::USE);
289 }
290
291 void
292 SubtitleContent::set_burn (bool b)
293 {
294         maybe_set (_burn, b, SubtitleContentProperty::BURN);
295 }
296
297 void
298 SubtitleContent::set_x_offset (double o)
299 {
300         maybe_set (_x_offset, o, SubtitleContentProperty::X_OFFSET);
301 }
302
303 void
304 SubtitleContent::set_y_offset (double o)
305 {
306         maybe_set (_y_offset, o, SubtitleContentProperty::Y_OFFSET);
307 }
308
309 void
310 SubtitleContent::set_x_scale (double s)
311 {
312         maybe_set (_x_scale, s, SubtitleContentProperty::X_SCALE);
313 }
314
315 void
316 SubtitleContent::set_y_scale (double s)
317 {
318         maybe_set (_y_scale, s, SubtitleContentProperty::Y_SCALE);
319 }
320
321 void
322 SubtitleContent::set_language (string language)
323 {
324         maybe_set (_language, language, SubtitleContentProperty::LANGUAGE);
325 }