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 <boost/foreach.hpp>
35 using boost::shared_ptr;
36 using boost::dynamic_pointer_cast;
38 int const SubtitleContentProperty::SUBTITLE_X_OFFSET = 500;
39 int const SubtitleContentProperty::SUBTITLE_Y_OFFSET = 501;
40 int const SubtitleContentProperty::SUBTITLE_X_SCALE = 502;
41 int const SubtitleContentProperty::SUBTITLE_Y_SCALE = 503;
42 int const SubtitleContentProperty::USE_SUBTITLES = 504;
43 int const SubtitleContentProperty::BURN_SUBTITLES = 505;
44 int const SubtitleContentProperty::SUBTITLE_LANGUAGE = 506;
45 int const SubtitleContentProperty::FONTS = 507;
47 SubtitleContent::SubtitleContent (shared_ptr<const Film> film)
49 , _use_subtitles (false)
50 , _burn_subtitles (false)
51 , _subtitle_x_offset (0)
52 , _subtitle_y_offset (0)
53 , _subtitle_x_scale (1)
54 , _subtitle_y_scale (1)
59 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, boost::filesystem::path p)
61 , _use_subtitles (false)
62 , _burn_subtitles (false)
63 , _subtitle_x_offset (0)
64 , _subtitle_y_offset (0)
65 , _subtitle_x_scale (1)
66 , _subtitle_y_scale (1)
71 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
72 : Content (film, node)
73 , _use_subtitles (false)
74 , _burn_subtitles (false)
75 , _subtitle_x_offset (0)
76 , _subtitle_y_offset (0)
77 , _subtitle_x_scale (1)
78 , _subtitle_y_scale (1)
81 _use_subtitles = node->bool_child ("UseSubtitles");
82 _burn_subtitles = node->bool_child ("BurnSubtitles");
86 _subtitle_x_offset = node->number_child<float> ("SubtitleXOffset");
87 _subtitle_y_offset = node->number_child<float> ("SubtitleYOffset");
89 _subtitle_y_offset = node->number_child<float> ("SubtitleOffset");
93 _subtitle_x_scale = node->number_child<float> ("SubtitleXScale");
94 _subtitle_y_scale = node->number_child<float> ("SubtitleYScale");
96 _subtitle_x_scale = _subtitle_y_scale = node->number_child<float> ("SubtitleScale");
99 _subtitle_language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
101 list<cxml::NodePtr> fonts = node->node_children ("Font");
102 for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
103 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
109 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
112 shared_ptr<SubtitleContent> ref = dynamic_pointer_cast<SubtitleContent> (c[0]);
113 DCPOMATIC_ASSERT (ref);
114 list<shared_ptr<Font> > ref_fonts = ref->fonts ();
116 for (size_t i = 0; i < c.size(); ++i) {
117 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (c[i]);
119 if (sc->use_subtitles() != ref->use_subtitles()) {
120 throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
123 if (sc->burn_subtitles() != ref->burn_subtitles()) {
124 throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
127 if (sc->subtitle_x_offset() != ref->subtitle_x_offset()) {
128 throw JoinError (_("Content to be joined must have the same subtitle X offset."));
131 if (sc->subtitle_y_offset() != ref->subtitle_y_offset()) {
132 throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
135 if (sc->subtitle_x_scale() != ref->subtitle_x_scale()) {
136 throw JoinError (_("Content to be joined must have the same subtitle X scale."));
139 if (sc->subtitle_y_scale() != ref->subtitle_y_scale()) {
140 throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
143 list<shared_ptr<Font> > fonts = sc->fonts ();
144 if (fonts.size() != ref_fonts.size()) {
145 throw JoinError (_("Content to be joined must use the same fonts."));
148 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
149 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
151 while (j != ref_fonts.end ()) {
153 throw JoinError (_("Content to be joined must use the same fonts."));
160 _use_subtitles = ref->use_subtitles ();
161 _burn_subtitles = ref->burn_subtitles ();
162 _subtitle_x_offset = ref->subtitle_x_offset ();
163 _subtitle_y_offset = ref->subtitle_y_offset ();
164 _subtitle_x_scale = ref->subtitle_x_scale ();
165 _subtitle_y_scale = ref->subtitle_y_scale ();
166 _subtitle_language = ref->subtitle_language ();
172 /** _mutex must not be held on entry */
174 SubtitleContent::as_xml (xmlpp::Node* root) const
176 boost::mutex::scoped_lock lm (_mutex);
178 root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use_subtitles));
179 root->add_child("BurnSubtitles")->add_child_text (raw_convert<string> (_burn_subtitles));
180 root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_subtitle_x_offset));
181 root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_subtitle_y_offset));
182 root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_subtitle_x_scale));
183 root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_subtitle_y_scale));
184 root->add_child("SubtitleLanguage")->add_child_text (_subtitle_language);
186 for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
187 (*i)->as_xml (root->add_child("Font"));
192 SubtitleContent::set_use_subtitles (bool u)
195 boost::mutex::scoped_lock lm (_mutex);
198 signal_changed (SubtitleContentProperty::USE_SUBTITLES);
202 SubtitleContent::set_burn_subtitles (bool b)
205 boost::mutex::scoped_lock lm (_mutex);
208 signal_changed (SubtitleContentProperty::BURN_SUBTITLES);
212 SubtitleContent::set_subtitle_x_offset (double o)
215 boost::mutex::scoped_lock lm (_mutex);
216 _subtitle_x_offset = o;
218 signal_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
222 SubtitleContent::set_subtitle_y_offset (double o)
225 boost::mutex::scoped_lock lm (_mutex);
226 _subtitle_y_offset = o;
228 signal_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
232 SubtitleContent::set_subtitle_x_scale (double s)
235 boost::mutex::scoped_lock lm (_mutex);
236 _subtitle_x_scale = s;
238 signal_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
242 SubtitleContent::set_subtitle_y_scale (double s)
245 boost::mutex::scoped_lock lm (_mutex);
246 _subtitle_y_scale = s;
248 signal_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
252 SubtitleContent::set_subtitle_language (string language)
255 boost::mutex::scoped_lock lm (_mutex);
256 _subtitle_language = language;
258 signal_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
262 SubtitleContent::identifier () const
265 s << Content::identifier()
266 << "_" << raw_convert<string> (subtitle_x_scale())
267 << "_" << raw_convert<string> (subtitle_y_scale())
268 << "_" << raw_convert<string> (subtitle_x_offset())
269 << "_" << raw_convert<string> (subtitle_y_offset());
271 /* XXX: I suppose really _fonts shouldn't be in here, since not all
272 types of subtitle content involve fonts.
274 BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
275 s << f->file().get_value_or ("Default");
278 /* The language is for metadata only, and doesn't affect
279 how this content looks.
286 SubtitleContent::add_font (shared_ptr<Font> font)
288 _fonts.push_back (font);
293 SubtitleContent::connect_to_fonts ()
295 BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
299 _font_connections.clear ();
301 BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
302 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
307 SubtitleContent::font_changed ()
309 signal_changed (SubtitleContentProperty::FONTS);
313 SubtitleContent::has_subtitles () const
315 return has_text_subtitles() || has_image_subtitles();