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