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