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;
48 int const SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE = 508;
50 SubtitleContent::SubtitleContent (shared_ptr<const Film> film)
52 , _use_subtitles (false)
53 , _burn_subtitles (false)
54 , _subtitle_x_offset (0)
55 , _subtitle_y_offset (0)
56 , _subtitle_x_scale (1)
57 , _subtitle_y_scale (1)
62 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, boost::filesystem::path p)
64 , _use_subtitles (false)
65 , _burn_subtitles (false)
66 , _subtitle_x_offset (0)
67 , _subtitle_y_offset (0)
68 , _subtitle_x_scale (1)
69 , _subtitle_y_scale (1)
74 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
75 : Content (film, node)
76 , _use_subtitles (false)
77 , _burn_subtitles (false)
78 , _subtitle_x_offset (0)
79 , _subtitle_y_offset (0)
80 , _subtitle_x_scale (1)
81 , _subtitle_y_scale (1)
84 _use_subtitles = node->bool_child ("UseSubtitles");
85 _burn_subtitles = node->bool_child ("BurnSubtitles");
89 _subtitle_x_offset = node->number_child<double> ("SubtitleXOffset");
90 _subtitle_y_offset = node->number_child<double> ("SubtitleYOffset");
92 _subtitle_y_offset = node->number_child<double> ("SubtitleOffset");
96 _subtitle_x_scale = node->number_child<double> ("SubtitleXScale");
97 _subtitle_y_scale = node->number_child<double> ("SubtitleYScale");
99 _subtitle_x_scale = _subtitle_y_scale = node->number_child<double> ("SubtitleScale");
102 _subtitle_language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
104 list<cxml::NodePtr> fonts = node->node_children ("Font");
105 for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
106 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
112 SubtitleContent::SubtitleContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
115 shared_ptr<SubtitleContent> ref = dynamic_pointer_cast<SubtitleContent> (c[0]);
116 DCPOMATIC_ASSERT (ref);
117 list<shared_ptr<Font> > ref_fonts = ref->fonts ();
119 for (size_t i = 0; i < c.size(); ++i) {
120 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (c[i]);
122 if (sc->use_subtitles() != ref->use_subtitles()) {
123 throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
126 if (sc->burn_subtitles() != ref->burn_subtitles()) {
127 throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
130 if (sc->subtitle_x_offset() != ref->subtitle_x_offset()) {
131 throw JoinError (_("Content to be joined must have the same subtitle X offset."));
134 if (sc->subtitle_y_offset() != ref->subtitle_y_offset()) {
135 throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
138 if (sc->subtitle_x_scale() != ref->subtitle_x_scale()) {
139 throw JoinError (_("Content to be joined must have the same subtitle X scale."));
142 if (sc->subtitle_y_scale() != ref->subtitle_y_scale()) {
143 throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
146 list<shared_ptr<Font> > fonts = sc->fonts ();
147 if (fonts.size() != ref_fonts.size()) {
148 throw JoinError (_("Content to be joined must use the same fonts."));
151 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
152 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
154 while (j != ref_fonts.end ()) {
156 throw JoinError (_("Content to be joined must use the same fonts."));
163 _use_subtitles = ref->use_subtitles ();
164 _burn_subtitles = ref->burn_subtitles ();
165 _subtitle_x_offset = ref->subtitle_x_offset ();
166 _subtitle_y_offset = ref->subtitle_y_offset ();
167 _subtitle_x_scale = ref->subtitle_x_scale ();
168 _subtitle_y_scale = ref->subtitle_y_scale ();
169 _subtitle_language = ref->subtitle_language ();
175 /** _mutex must not be held on entry */
177 SubtitleContent::as_xml (xmlpp::Node* root) const
179 boost::mutex::scoped_lock lm (_mutex);
181 root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use_subtitles));
182 root->add_child("BurnSubtitles")->add_child_text (raw_convert<string> (_burn_subtitles));
183 root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_subtitle_x_offset));
184 root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_subtitle_y_offset));
185 root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_subtitle_x_scale));
186 root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_subtitle_y_scale));
187 root->add_child("SubtitleLanguage")->add_child_text (_subtitle_language);
189 for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
190 (*i)->as_xml (root->add_child("Font"));
195 SubtitleContent::set_use_subtitles (bool u)
198 boost::mutex::scoped_lock lm (_mutex);
201 signal_changed (SubtitleContentProperty::USE_SUBTITLES);
205 SubtitleContent::set_burn_subtitles (bool b)
208 boost::mutex::scoped_lock lm (_mutex);
211 signal_changed (SubtitleContentProperty::BURN_SUBTITLES);
215 SubtitleContent::set_subtitle_x_offset (double o)
218 boost::mutex::scoped_lock lm (_mutex);
219 _subtitle_x_offset = o;
221 signal_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
225 SubtitleContent::set_subtitle_y_offset (double o)
228 boost::mutex::scoped_lock lm (_mutex);
229 _subtitle_y_offset = o;
231 signal_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
235 SubtitleContent::set_subtitle_x_scale (double s)
238 boost::mutex::scoped_lock lm (_mutex);
239 _subtitle_x_scale = s;
241 signal_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
245 SubtitleContent::set_subtitle_y_scale (double s)
248 boost::mutex::scoped_lock lm (_mutex);
249 _subtitle_y_scale = s;
251 signal_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
255 SubtitleContent::set_subtitle_language (string language)
258 boost::mutex::scoped_lock lm (_mutex);
259 _subtitle_language = language;
261 signal_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
265 SubtitleContent::identifier () const
268 s << Content::identifier()
269 << "_" << raw_convert<string> (subtitle_x_scale())
270 << "_" << raw_convert<string> (subtitle_y_scale())
271 << "_" << raw_convert<string> (subtitle_x_offset())
272 << "_" << raw_convert<string> (subtitle_y_offset());
274 /* XXX: I suppose really _fonts shouldn't be in here, since not all
275 types of subtitle content involve fonts.
277 BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
278 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
279 s << "_" << f->file(static_cast<FontFiles::Variant>(i)).get_value_or ("Default");
283 /* The language is for metadata only, and doesn't affect
284 how this content looks.
291 SubtitleContent::add_font (shared_ptr<Font> font)
293 _fonts.push_back (font);
298 SubtitleContent::connect_to_fonts ()
300 BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
304 _font_connections.clear ();
306 BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
307 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
312 SubtitleContent::font_changed ()
314 signal_changed (SubtitleContentProperty::FONTS);
318 SubtitleContent::has_subtitles () const
320 return has_text_subtitles() || has_image_subtitles();