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