Hand-apply d849d411cff28ef5453085791d0b4d7cd73bd070 from master; replace all assert...
[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 <libcxml/cxml.h>
21 #include <dcp/raw_convert.h>
22 #include "subtitle_content.h"
23 #include "util.h"
24 #include "exceptions.h"
25 #include "safe_stringstream.h"
26 #include "font.h"
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::vector;
32 using std::cout;
33 using std::list;
34 using boost::shared_ptr;
35 using boost::dynamic_pointer_cast;
36 using dcp::raw_convert;
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> f)
47         : Content (f)
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> f, boost::filesystem::path p)
58         : Content (f, 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> f, cxml::ConstNodePtr node, int version)
69         : Content (f, 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
104 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
105         : Content (f, c)
106 {
107         shared_ptr<SubtitleContent> ref = dynamic_pointer_cast<SubtitleContent> (c[0]);
108         DCPOMATIC_ASSERT (ref);
109         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
110         
111         for (size_t i = 0; i < c.size(); ++i) {
112                 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (c[i]);
113
114                 if (sc->use_subtitles() != ref->use_subtitles()) {
115                         throw JoinError (_("Content to be joined must have the same 'use 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         _subtitle_x_offset = ref->subtitle_x_offset ();
153         _subtitle_y_offset = ref->subtitle_y_offset ();
154         _subtitle_x_scale = ref->subtitle_x_scale ();
155         _subtitle_y_scale = ref->subtitle_y_scale ();
156         _subtitle_language = ref->subtitle_language ();
157         _fonts = ref_fonts;
158 }
159
160 /** _mutex must not be held on entry */
161 void
162 SubtitleContent::as_xml (xmlpp::Node* root) const
163 {
164         boost::mutex::scoped_lock lm (_mutex);
165         
166         root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use_subtitles));
167         root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_subtitle_x_offset));
168         root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_subtitle_y_offset));
169         root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_subtitle_x_scale));
170         root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_subtitle_y_scale));
171         root->add_child("SubtitleLanguage")->add_child_text (_subtitle_language);
172
173         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
174                 (*i)->as_xml (root->add_child("Font"));
175         }
176 }
177
178 void
179 SubtitleContent::set_use_subtitles (bool u)
180 {
181         {
182                 boost::mutex::scoped_lock lm (_mutex);
183                 _use_subtitles = u;
184         }
185         signal_changed (SubtitleContentProperty::USE_SUBTITLES);
186 }
187         
188 void
189 SubtitleContent::set_subtitle_x_offset (double o)
190 {
191         {
192                 boost::mutex::scoped_lock lm (_mutex);
193                 _subtitle_x_offset = o;
194         }
195         signal_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
196 }
197
198 void
199 SubtitleContent::set_subtitle_y_offset (double o)
200 {
201         {
202                 boost::mutex::scoped_lock lm (_mutex);
203                 _subtitle_y_offset = o;
204         }
205         signal_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
206 }
207
208 void
209 SubtitleContent::set_subtitle_x_scale (double s)
210 {
211         {
212                 boost::mutex::scoped_lock lm (_mutex);
213                 _subtitle_x_scale = s;
214         }
215         signal_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
216 }
217
218 void
219 SubtitleContent::set_subtitle_y_scale (double s)
220 {
221         {
222                 boost::mutex::scoped_lock lm (_mutex);
223                 _subtitle_y_scale = s;
224         }
225         signal_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
226 }
227
228 void
229 SubtitleContent::set_subtitle_language (string language)
230 {
231         {
232                 boost::mutex::scoped_lock lm (_mutex);
233                 _subtitle_language = language;
234         }
235         signal_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
236 }
237
238 string
239 SubtitleContent::identifier () const
240 {
241         SafeStringStream s;
242         s << Content::identifier()
243           << "_" << raw_convert<string> (subtitle_x_scale())
244           << "_" << raw_convert<string> (subtitle_y_scale())
245           << "_" << raw_convert<string> (subtitle_x_offset())
246           << "_" << raw_convert<string> (subtitle_y_offset());
247
248         /* The language is for metadata only, and doesn't affect
249            how this content looks.
250         */
251
252         return s.str ();
253 }