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