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