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