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