Merge remote-tracking branch 'origin/master' into 2.0
[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
42 SubtitleContent::SubtitleContent (shared_ptr<const Film> f)
43         : Content (f)
44         , _use_subtitles (false)
45         , _subtitle_x_offset (0)
46         , _subtitle_y_offset (0)
47         , _subtitle_x_scale (1)
48         , _subtitle_y_scale (1)
49 {
50
51 }
52
53 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, boost::filesystem::path p)
54         : Content (f, p)
55         , _use_subtitles (false)
56         , _subtitle_x_offset (0)
57         , _subtitle_y_offset (0)
58         , _subtitle_x_scale (1)
59         , _subtitle_y_scale (1)
60 {
61
62 }
63
64 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
65         : Content (f, node)
66         , _use_subtitles (false)
67         , _subtitle_x_offset (0)
68         , _subtitle_y_offset (0)
69         , _subtitle_x_scale (1)
70         , _subtitle_y_scale (1)
71 {
72         if (version >= 32) {
73                 _use_subtitles = node->bool_child ("UseSubtitles");
74         } else {
75                 _use_subtitles = false;
76         }
77         
78         if (version >= 7) {
79                 _subtitle_x_offset = node->number_child<float> ("SubtitleXOffset");
80                 _subtitle_y_offset = node->number_child<float> ("SubtitleYOffset");
81         } else {
82                 _subtitle_y_offset = node->number_child<float> ("SubtitleOffset");
83         }
84
85         if (version >= 10) {
86                 _subtitle_x_scale = node->number_child<float> ("SubtitleXScale");
87                 _subtitle_y_scale = node->number_child<float> ("SubtitleYScale");
88         } else {
89                 _subtitle_x_scale = _subtitle_y_scale = node->number_child<float> ("SubtitleScale");
90         }
91 }
92
93 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
94         : Content (f, c)
95 {
96         shared_ptr<SubtitleContent> ref = dynamic_pointer_cast<SubtitleContent> (c[0]);
97         assert (ref);
98         
99         for (size_t i = 0; i < c.size(); ++i) {
100                 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (c[i]);
101
102                 if (sc->use_subtitles() != ref->use_subtitles()) {
103                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
104                 }
105
106                 if (sc->subtitle_x_offset() != ref->subtitle_x_offset()) {
107                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
108                 }
109                 
110                 if (sc->subtitle_y_offset() != ref->subtitle_y_offset()) {
111                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
112                 }
113
114                 if (sc->subtitle_x_scale() != ref->subtitle_x_scale()) {
115                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
116                 }
117
118                 if (sc->subtitle_y_scale() != ref->subtitle_y_scale()) {
119                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
120                 }
121         }
122
123         _use_subtitles = ref->use_subtitles ();
124         _subtitle_x_offset = ref->subtitle_x_offset ();
125         _subtitle_y_offset = ref->subtitle_y_offset ();
126         _subtitle_x_scale = ref->subtitle_x_scale ();
127         _subtitle_y_scale = ref->subtitle_y_scale ();
128 }
129
130 void
131 SubtitleContent::as_xml (xmlpp::Node* root) const
132 {
133         root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use_subtitles));
134         root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_subtitle_x_offset));
135         root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_subtitle_y_offset));
136         root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_subtitle_x_scale));
137         root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_subtitle_y_scale));
138 }
139
140 void
141 SubtitleContent::set_use_subtitles (bool u)
142 {
143         {
144                 boost::mutex::scoped_lock lm (_mutex);
145                 _use_subtitles = u;
146         }
147         signal_changed (SubtitleContentProperty::USE_SUBTITLES);
148 }
149         
150 void
151 SubtitleContent::set_subtitle_x_offset (double o)
152 {
153         {
154                 boost::mutex::scoped_lock lm (_mutex);
155                 _subtitle_x_offset = o;
156         }
157         signal_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
158 }
159
160 void
161 SubtitleContent::set_subtitle_y_offset (double o)
162 {
163         {
164                 boost::mutex::scoped_lock lm (_mutex);
165                 _subtitle_y_offset = o;
166         }
167         signal_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
168 }
169
170 void
171 SubtitleContent::set_subtitle_x_scale (double s)
172 {
173         {
174                 boost::mutex::scoped_lock lm (_mutex);
175                 _subtitle_x_scale = s;
176         }
177         signal_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
178 }
179
180 void
181 SubtitleContent::set_subtitle_y_scale (double s)
182 {
183         {
184                 boost::mutex::scoped_lock lm (_mutex);
185                 _subtitle_y_scale = s;
186         }
187         signal_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
188 }
189
190 string
191 SubtitleContent::identifier () const
192 {
193         SafeStringStream s;
194         s << Content::identifier()
195           << "_" << raw_convert<string> (subtitle_x_scale())
196           << "_" << raw_convert<string> (subtitle_y_scale())
197           << "_" << raw_convert<string> (subtitle_x_offset())
198           << "_" << raw_convert<string> (subtitle_y_offset());
199
200         return s.str ();
201 }