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