Move video frame rate ('prepared-for') into Content.
[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 "film.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::OUTLINE_COLOUR = 510;
53
54 SubtitleContent::SubtitleContent (Content* parent, shared_ptr<const Film> film)
55         : ContentPart (parent, film)
56         , _use (false)
57         , _burn (false)
58         , _x_offset (0)
59         , _y_offset (0)
60         , _x_scale (1)
61         , _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 (false)
72         , _burn (false)
73         , _x_offset (0)
74         , _y_offset (0)
75         , _x_scale (1)
76         , _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 = node->bool_child ("UseSubtitles");
91                 _burn = node->bool_child ("BurnSubtitles");
92         }
93
94         if (version >= 7) {
95                 _x_offset = node->number_child<double> ("SubtitleXOffset");
96                 _y_offset = node->number_child<double> ("SubtitleYOffset");
97         } else {
98                 _y_offset = node->number_child<double> ("SubtitleOffset");
99         }
100
101         if (version >= 10) {
102                 _x_scale = node->number_child<double> ("SubtitleXScale");
103                 _y_scale = node->number_child<double> ("SubtitleYScale");
104         } else {
105                 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
106         }
107
108         _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 = c[0]->subtitle;
122         DCPOMATIC_ASSERT (ref);
123         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
124
125         for (size_t i = 1; i < c.size(); ++i) {
126
127                 if (c[i]->subtitle->use() != ref->use()) {
128                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
129                 }
130
131                 if (c[i]->subtitle->burn() != ref->burn()) {
132                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
133                 }
134
135                 if (c[i]->subtitle->x_offset() != ref->x_offset()) {
136                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
137                 }
138
139                 if (c[i]->subtitle->y_offset() != ref->y_offset()) {
140                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
141                 }
142
143                 if (c[i]->subtitle->x_scale() != ref->x_scale()) {
144                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
145                 }
146
147                 if (c[i]->subtitle->y_scale() != ref->y_scale()) {
148                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
149                 }
150
151                 list<shared_ptr<Font> > fonts = c[i]->subtitle->fonts ();
152                 if (fonts.size() != ref_fonts.size()) {
153                         throw JoinError (_("Content to be joined must use the same fonts."));
154                 }
155
156                 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
157                 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
158
159                 while (j != ref_fonts.end ()) {
160                         if (**j != **k) {
161                                 throw JoinError (_("Content to be joined must use the same fonts."));
162                         }
163                         ++j;
164                         ++k;
165                 }
166         }
167
168         _use = ref->use ();
169         _burn = ref->burn ();
170         _x_offset = ref->x_offset ();
171         _y_offset = ref->y_offset ();
172         _x_scale = ref->x_scale ();
173         _y_scale = ref->y_scale ();
174         _language = ref->language ();
175         _fonts = ref_fonts;
176
177         connect_to_fonts ();
178 }
179
180 /** _mutex must not be held on entry */
181 void
182 SubtitleContent::as_xml (xmlpp::Node* root) const
183 {
184         boost::mutex::scoped_lock lm (_mutex);
185
186         root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use));
187         root->add_child("BurnSubtitles")->add_child_text (raw_convert<string> (_burn));
188         root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
189         root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
190         root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
191         root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
192         root->add_child("SubtitleLanguage")->add_child_text (_language);
193         root->add_child("Red")->add_child_text (raw_convert<string> (_colour.r));
194         root->add_child("Green")->add_child_text (raw_convert<string> (_colour.g));
195         root->add_child("Blue")->add_child_text (raw_convert<string> (_colour.b));
196         root->add_child("Outline")->add_child_text (raw_convert<string> (_outline));
197         root->add_child("OutlineRed")->add_child_text (raw_convert<string> (_outline_colour.r));
198         root->add_child("OutlineGreen")->add_child_text (raw_convert<string> (_outline_colour.g));
199         root->add_child("OutlineBlue")->add_child_text (raw_convert<string> (_outline_colour.b));
200
201         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
202                 (*i)->as_xml (root->add_child("Font"));
203         }
204 }
205
206 string
207 SubtitleContent::identifier () const
208 {
209         SafeStringStream s;
210         s << raw_convert<string> (x_scale())
211           << "_" << raw_convert<string> (y_scale())
212           << "_" << raw_convert<string> (x_offset())
213           << "_" << raw_convert<string> (y_offset());
214
215         /* XXX: I suppose really _fonts shouldn't be in here, since not all
216            types of subtitle content involve fonts.
217         */
218         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
219                 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
220                         s << "_" << f->file(static_cast<FontFiles::Variant>(i)).get_value_or ("Default");
221                 }
222         }
223
224         /* The language is for metadata only, and doesn't affect
225            how this content looks.
226         */
227
228         return s.str ();
229 }
230
231 void
232 SubtitleContent::add_font (shared_ptr<Font> font)
233 {
234         _fonts.push_back (font);
235         connect_to_fonts ();
236 }
237
238 void
239 SubtitleContent::connect_to_fonts ()
240 {
241         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
242                 i.disconnect ();
243         }
244
245         _font_connections.clear ();
246
247         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
248                 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
249         }
250 }
251
252 void
253 SubtitleContent::font_changed ()
254 {
255         _parent->signal_changed (SubtitleContentProperty::FONTS);
256 }
257
258 void
259 SubtitleContent::set_colour (dcp::Colour colour)
260 {
261         maybe_set (_colour, colour, SubtitleContentProperty::COLOUR);
262 }
263
264 void
265 SubtitleContent::set_outline (bool o)
266 {
267         maybe_set (_outline, o, SubtitleContentProperty::OUTLINE);
268 }
269
270 void
271 SubtitleContent::set_outline_colour (dcp::Colour colour)
272 {
273         maybe_set (_outline_colour, colour, SubtitleContentProperty::OUTLINE_COLOUR);
274 }
275
276 void
277 SubtitleContent::set_use (bool u)
278 {
279         maybe_set (_use, u, SubtitleContentProperty::USE);
280 }
281
282 void
283 SubtitleContent::set_burn (bool b)
284 {
285         maybe_set (_burn, b, SubtitleContentProperty::BURN);
286 }
287
288 void
289 SubtitleContent::set_x_offset (double o)
290 {
291         maybe_set (_x_offset, o, SubtitleContentProperty::X_OFFSET);
292 }
293
294 void
295 SubtitleContent::set_y_offset (double o)
296 {
297         maybe_set (_y_offset, o, SubtitleContentProperty::Y_OFFSET);
298 }
299
300 void
301 SubtitleContent::set_x_scale (double s)
302 {
303         maybe_set (_x_scale, s, SubtitleContentProperty::X_SCALE);
304 }
305
306 void
307 SubtitleContent::set_y_scale (double s)
308 {
309         maybe_set (_y_scale, s, SubtitleContentProperty::Y_SCALE);
310 }
311
312 void
313 SubtitleContent::set_language (string language)
314 {
315         maybe_set (_language, language, SubtitleContentProperty::LANGUAGE);
316 }