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