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