Include tidying src/lib/a-j*.h
[dcpomatic.git] / src / lib / subtitle_content.cc
1 /*
2     Copyright (C) 2013-2014 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 <libcxml/cxml.h>
27 #include <libxml++/libxml++.h>
28 #include <boost/foreach.hpp>
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::vector;
34 using std::cout;
35 using std::list;
36 using boost::shared_ptr;
37 using boost::dynamic_pointer_cast;
38
39 int const SubtitleContentProperty::SUBTITLE_X_OFFSET = 500;
40 int const SubtitleContentProperty::SUBTITLE_Y_OFFSET = 501;
41 int const SubtitleContentProperty::SUBTITLE_X_SCALE = 502;
42 int const SubtitleContentProperty::SUBTITLE_Y_SCALE = 503;
43 int const SubtitleContentProperty::USE_SUBTITLES = 504;
44 int const SubtitleContentProperty::BURN_SUBTITLES = 505;
45 int const SubtitleContentProperty::SUBTITLE_LANGUAGE = 506;
46 int const SubtitleContentProperty::FONTS = 507;
47
48 SubtitleContent::SubtitleContent (shared_ptr<const Film> film)
49         : Content (film)
50         , _use_subtitles (false)
51         , _burn_subtitles (false)
52         , _subtitle_x_offset (0)
53         , _subtitle_y_offset (0)
54         , _subtitle_x_scale (1)
55         , _subtitle_y_scale (1)
56 {
57
58 }
59
60 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, boost::filesystem::path p)
61         : Content (film, p)
62         , _use_subtitles (false)
63         , _burn_subtitles (false)
64         , _subtitle_x_offset (0)
65         , _subtitle_y_offset (0)
66         , _subtitle_x_scale (1)
67         , _subtitle_y_scale (1)
68 {
69
70 }
71
72 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
73         : Content (film, node)
74         , _use_subtitles (false)
75         , _burn_subtitles (false)
76         , _subtitle_x_offset (0)
77         , _subtitle_y_offset (0)
78         , _subtitle_x_scale (1)
79         , _subtitle_y_scale (1)
80 {
81         if (version >= 32) {
82                 _use_subtitles = node->bool_child ("UseSubtitles");
83                 _burn_subtitles = node->bool_child ("BurnSubtitles");
84         }
85
86         if (version >= 7) {
87                 _subtitle_x_offset = node->number_child<float> ("SubtitleXOffset");
88                 _subtitle_y_offset = node->number_child<float> ("SubtitleYOffset");
89         } else {
90                 _subtitle_y_offset = node->number_child<float> ("SubtitleOffset");
91         }
92
93         if (version >= 10) {
94                 _subtitle_x_scale = node->number_child<float> ("SubtitleXScale");
95                 _subtitle_y_scale = node->number_child<float> ("SubtitleYScale");
96         } else {
97                 _subtitle_x_scale = _subtitle_y_scale = node->number_child<float> ("SubtitleScale");
98         }
99
100         _subtitle_language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
101
102         list<cxml::NodePtr> fonts = node->node_children ("Font");
103         for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
104                 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
105         }
106
107         connect_to_fonts ();
108 }
109
110 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
111         : Content (film, c)
112 {
113         shared_ptr<SubtitleContent> ref = dynamic_pointer_cast<SubtitleContent> (c[0]);
114         DCPOMATIC_ASSERT (ref);
115         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
116
117         for (size_t i = 0; i < c.size(); ++i) {
118                 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (c[i]);
119
120                 if (sc->use_subtitles() != ref->use_subtitles()) {
121                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
122                 }
123
124                 if (sc->burn_subtitles() != ref->burn_subtitles()) {
125                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
126                 }
127
128                 if (sc->subtitle_x_offset() != ref->subtitle_x_offset()) {
129                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
130                 }
131
132                 if (sc->subtitle_y_offset() != ref->subtitle_y_offset()) {
133                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
134                 }
135
136                 if (sc->subtitle_x_scale() != ref->subtitle_x_scale()) {
137                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
138                 }
139
140                 if (sc->subtitle_y_scale() != ref->subtitle_y_scale()) {
141                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
142                 }
143
144                 list<shared_ptr<Font> > fonts = sc->fonts ();
145                 if (fonts.size() != ref_fonts.size()) {
146                         throw JoinError (_("Content to be joined must use the same fonts."));
147                 }
148
149                 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
150                 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
151
152                 while (j != ref_fonts.end ()) {
153                         if (**j != **k) {
154                                 throw JoinError (_("Content to be joined must use the same fonts."));
155                         }
156                         ++j;
157                         ++k;
158                 }
159         }
160
161         _use_subtitles = ref->use_subtitles ();
162         _burn_subtitles = ref->burn_subtitles ();
163         _subtitle_x_offset = ref->subtitle_x_offset ();
164         _subtitle_y_offset = ref->subtitle_y_offset ();
165         _subtitle_x_scale = ref->subtitle_x_scale ();
166         _subtitle_y_scale = ref->subtitle_y_scale ();
167         _subtitle_language = ref->subtitle_language ();
168         _fonts = ref_fonts;
169
170         connect_to_fonts ();
171 }
172
173 /** _mutex must not be held on entry */
174 void
175 SubtitleContent::as_xml (xmlpp::Node* root) const
176 {
177         boost::mutex::scoped_lock lm (_mutex);
178
179         root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use_subtitles));
180         root->add_child("BurnSubtitles")->add_child_text (raw_convert<string> (_burn_subtitles));
181         root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_subtitle_x_offset));
182         root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_subtitle_y_offset));
183         root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_subtitle_x_scale));
184         root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_subtitle_y_scale));
185         root->add_child("SubtitleLanguage")->add_child_text (_subtitle_language);
186
187         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
188                 (*i)->as_xml (root->add_child("Font"));
189         }
190 }
191
192 void
193 SubtitleContent::set_use_subtitles (bool u)
194 {
195         {
196                 boost::mutex::scoped_lock lm (_mutex);
197                 _use_subtitles = u;
198         }
199         signal_changed (SubtitleContentProperty::USE_SUBTITLES);
200 }
201
202 void
203 SubtitleContent::set_burn_subtitles (bool b)
204 {
205         {
206                 boost::mutex::scoped_lock lm (_mutex);
207                 _burn_subtitles = b;
208         }
209         signal_changed (SubtitleContentProperty::BURN_SUBTITLES);
210 }
211
212 void
213 SubtitleContent::set_subtitle_x_offset (double o)
214 {
215         {
216                 boost::mutex::scoped_lock lm (_mutex);
217                 _subtitle_x_offset = o;
218         }
219         signal_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
220 }
221
222 void
223 SubtitleContent::set_subtitle_y_offset (double o)
224 {
225         {
226                 boost::mutex::scoped_lock lm (_mutex);
227                 _subtitle_y_offset = o;
228         }
229         signal_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
230 }
231
232 void
233 SubtitleContent::set_subtitle_x_scale (double s)
234 {
235         {
236                 boost::mutex::scoped_lock lm (_mutex);
237                 _subtitle_x_scale = s;
238         }
239         signal_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
240 }
241
242 void
243 SubtitleContent::set_subtitle_y_scale (double s)
244 {
245         {
246                 boost::mutex::scoped_lock lm (_mutex);
247                 _subtitle_y_scale = s;
248         }
249         signal_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
250 }
251
252 void
253 SubtitleContent::set_subtitle_language (string language)
254 {
255         {
256                 boost::mutex::scoped_lock lm (_mutex);
257                 _subtitle_language = language;
258         }
259         signal_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
260 }
261
262 string
263 SubtitleContent::identifier () const
264 {
265         SafeStringStream s;
266         s << Content::identifier()
267           << "_" << raw_convert<string> (subtitle_x_scale())
268           << "_" << raw_convert<string> (subtitle_y_scale())
269           << "_" << raw_convert<string> (subtitle_x_offset())
270           << "_" << raw_convert<string> (subtitle_y_offset());
271
272         /* XXX: I suppose really _fonts shouldn't be in here, since not all
273            types of subtitle content involve fonts.
274         */
275         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
276                 s << f->file().get_value_or ("Default");
277         }
278
279         /* The language is for metadata only, and doesn't affect
280            how this content looks.
281         */
282
283         return s.str ();
284 }
285
286 void
287 SubtitleContent::add_font (shared_ptr<Font> font)
288 {
289         _fonts.push_back (font);
290         connect_to_fonts ();
291 }
292
293 void
294 SubtitleContent::connect_to_fonts ()
295 {
296         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
297                 i.disconnect ();
298         }
299
300         _font_connections.clear ();
301
302         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
303                 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
304         }
305 }
306
307 void
308 SubtitleContent::font_changed ()
309 {
310         signal_changed (SubtitleContentProperty::FONTS);
311 }
312
313 bool
314 SubtitleContent::has_subtitles () const
315 {
316         return has_text_subtitles() || has_image_subtitles();
317 }