2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "subtitle_content.h"
22 #include "exceptions.h"
23 #include "safe_stringstream.h"
25 #include "raw_convert.h"
26 #include <libcxml/cxml.h>
27 #include <libxml++/libxml++.h>
28 #include <boost/foreach.hpp>
37 using boost::shared_ptr;
38 using boost::dynamic_pointer_cast;
40 int const SubtitleContentProperty::SUBTITLE_X_OFFSET = 500;
41 int const SubtitleContentProperty::SUBTITLE_Y_OFFSET = 501;
42 int const SubtitleContentProperty::SUBTITLE_X_SCALE = 502;
43 int const SubtitleContentProperty::SUBTITLE_Y_SCALE = 503;
44 int const SubtitleContentProperty::USE_SUBTITLES = 504;
45 int const SubtitleContentProperty::BURN_SUBTITLES = 505;
46 int const SubtitleContentProperty::SUBTITLE_LANGUAGE = 506;
47 int const SubtitleContentProperty::FONTS = 507;
49 SubtitleContent::SubtitleContent (shared_ptr<const Film> film)
51 , _use_subtitles (false)
52 , _burn_subtitles (false)
53 , _subtitle_x_offset (0)
54 , _subtitle_y_offset (0)
55 , _subtitle_x_scale (1)
56 , _subtitle_y_scale (1)
61 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, boost::filesystem::path p)
63 , _use_subtitles (false)
64 , _burn_subtitles (false)
65 , _subtitle_x_offset (0)
66 , _subtitle_y_offset (0)
67 , _subtitle_x_scale (1)
68 , _subtitle_y_scale (1)
73 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
74 : Content (film, node)
75 , _use_subtitles (false)
76 , _burn_subtitles (false)
77 , _subtitle_x_offset (0)
78 , _subtitle_y_offset (0)
79 , _subtitle_x_scale (1)
80 , _subtitle_y_scale (1)
83 _use_subtitles = node->bool_child ("UseSubtitles");
84 _burn_subtitles = node->bool_child ("BurnSubtitles");
88 _subtitle_x_offset = node->number_child<float> ("SubtitleXOffset");
89 _subtitle_y_offset = node->number_child<float> ("SubtitleYOffset");
91 _subtitle_y_offset = node->number_child<float> ("SubtitleOffset");
95 _subtitle_x_scale = node->number_child<float> ("SubtitleXScale");
96 _subtitle_y_scale = node->number_child<float> ("SubtitleYScale");
98 _subtitle_x_scale = _subtitle_y_scale = node->number_child<float> ("SubtitleScale");
101 _subtitle_language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
103 list<cxml::NodePtr> fonts = node->node_children ("Font");
104 for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
105 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
111 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
114 shared_ptr<SubtitleContent> ref = dynamic_pointer_cast<SubtitleContent> (c[0]);
115 DCPOMATIC_ASSERT (ref);
116 list<shared_ptr<Font> > ref_fonts = ref->fonts ();
118 for (size_t i = 0; i < c.size(); ++i) {
119 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (c[i]);
121 if (sc->use_subtitles() != ref->use_subtitles()) {
122 throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
125 if (sc->burn_subtitles() != ref->burn_subtitles()) {
126 throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
129 if (sc->subtitle_x_offset() != ref->subtitle_x_offset()) {
130 throw JoinError (_("Content to be joined must have the same subtitle X offset."));
133 if (sc->subtitle_y_offset() != ref->subtitle_y_offset()) {
134 throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
137 if (sc->subtitle_x_scale() != ref->subtitle_x_scale()) {
138 throw JoinError (_("Content to be joined must have the same subtitle X scale."));
141 if (sc->subtitle_y_scale() != ref->subtitle_y_scale()) {
142 throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
145 list<shared_ptr<Font> > fonts = sc->fonts ();
146 if (fonts.size() != ref_fonts.size()) {
147 throw JoinError (_("Content to be joined must use the same fonts."));
150 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
151 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
153 while (j != ref_fonts.end ()) {
155 throw JoinError (_("Content to be joined must use the same fonts."));
162 _use_subtitles = ref->use_subtitles ();
163 _burn_subtitles = ref->burn_subtitles ();
164 _subtitle_x_offset = ref->subtitle_x_offset ();
165 _subtitle_y_offset = ref->subtitle_y_offset ();
166 _subtitle_x_scale = ref->subtitle_x_scale ();
167 _subtitle_y_scale = ref->subtitle_y_scale ();
168 _subtitle_language = ref->subtitle_language ();
174 /** _mutex must not be held on entry */
176 SubtitleContent::as_xml (xmlpp::Node* root) const
178 boost::mutex::scoped_lock lm (_mutex);
180 root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use_subtitles));
181 root->add_child("BurnSubtitles")->add_child_text (raw_convert<string> (_burn_subtitles));
182 root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_subtitle_x_offset));
183 root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_subtitle_y_offset));
184 root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_subtitle_x_scale));
185 root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_subtitle_y_scale));
186 root->add_child("SubtitleLanguage")->add_child_text (_subtitle_language);
188 for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
189 (*i)->as_xml (root->add_child("Font"));
194 SubtitleContent::set_use_subtitles (bool u)
197 boost::mutex::scoped_lock lm (_mutex);
200 signal_changed (SubtitleContentProperty::USE_SUBTITLES);
204 SubtitleContent::set_burn_subtitles (bool b)
207 boost::mutex::scoped_lock lm (_mutex);
210 signal_changed (SubtitleContentProperty::BURN_SUBTITLES);
214 SubtitleContent::set_subtitle_x_offset (double o)
217 boost::mutex::scoped_lock lm (_mutex);
218 _subtitle_x_offset = o;
220 signal_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
224 SubtitleContent::set_subtitle_y_offset (double o)
227 boost::mutex::scoped_lock lm (_mutex);
228 _subtitle_y_offset = o;
230 signal_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
234 SubtitleContent::set_subtitle_x_scale (double s)
237 boost::mutex::scoped_lock lm (_mutex);
238 _subtitle_x_scale = s;
240 signal_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
244 SubtitleContent::set_subtitle_y_scale (double s)
247 boost::mutex::scoped_lock lm (_mutex);
248 _subtitle_y_scale = s;
250 signal_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
254 SubtitleContent::set_subtitle_language (string language)
257 boost::mutex::scoped_lock lm (_mutex);
258 _subtitle_language = language;
260 signal_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
264 SubtitleContent::identifier () const
267 s << Content::identifier()
268 << "_" << raw_convert<string> (subtitle_x_scale())
269 << "_" << raw_convert<string> (subtitle_y_scale())
270 << "_" << raw_convert<string> (subtitle_x_offset())
271 << "_" << raw_convert<string> (subtitle_y_offset());
273 /* XXX: I suppose really _fonts shouldn't be in here, since not all
274 types of subtitle content involve fonts.
276 BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
277 s << f->file().get_value_or ("Default");
280 /* The language is for metadata only, and doesn't affect
281 how this content looks.
288 SubtitleContent::add_font (shared_ptr<Font> font)
290 _fonts.push_back (font);
295 SubtitleContent::connect_to_fonts ()
297 BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
301 _font_connections.clear ();
303 BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
304 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
309 SubtitleContent::font_changed ()
311 signal_changed (SubtitleContentProperty::FONTS);
315 SubtitleContent::has_subtitles () const
317 return has_text_subtitles() || has_image_subtitles();