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 <libcxml/cxml.h>
21 #include <dcp/raw_convert.h>
22 #include "subtitle_content.h"
24 #include "exceptions.h"
25 #include "safe_stringstream.h"
32 using boost::shared_ptr;
33 using boost::dynamic_pointer_cast;
34 using dcp::raw_convert;
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 int const SubtitleContentProperty::SUBTITLE_LANGUAGE = 505;
42 int const SubtitleContentProperty::FONTS = 506;
44 SubtitleContent::SubtitleContent (shared_ptr<const Film> f)
46 , _use_subtitles (false)
47 , _subtitle_x_offset (0)
48 , _subtitle_y_offset (0)
49 , _subtitle_x_scale (1)
50 , _subtitle_y_scale (1)
55 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, boost::filesystem::path p)
57 , _use_subtitles (false)
58 , _subtitle_x_offset (0)
59 , _subtitle_y_offset (0)
60 , _subtitle_x_scale (1)
61 , _subtitle_y_scale (1)
66 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
68 , _use_subtitles (false)
69 , _subtitle_x_offset (0)
70 , _subtitle_y_offset (0)
71 , _subtitle_x_scale (1)
72 , _subtitle_y_scale (1)
75 _use_subtitles = node->bool_child ("UseSubtitles");
77 _use_subtitles = false;
81 _subtitle_x_offset = node->number_child<float> ("SubtitleXOffset");
82 _subtitle_y_offset = node->number_child<float> ("SubtitleYOffset");
84 _subtitle_y_offset = node->number_child<float> ("SubtitleOffset");
88 _subtitle_x_scale = node->number_child<float> ("SubtitleXScale");
89 _subtitle_y_scale = node->number_child<float> ("SubtitleYScale");
91 _subtitle_x_scale = _subtitle_y_scale = node->number_child<float> ("SubtitleScale");
94 _subtitle_language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
97 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
100 shared_ptr<SubtitleContent> ref = dynamic_pointer_cast<SubtitleContent> (c[0]);
103 for (size_t i = 0; i < c.size(); ++i) {
104 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (c[i]);
106 if (sc->use_subtitles() != ref->use_subtitles()) {
107 throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
110 if (sc->subtitle_x_offset() != ref->subtitle_x_offset()) {
111 throw JoinError (_("Content to be joined must have the same subtitle X offset."));
114 if (sc->subtitle_y_offset() != ref->subtitle_y_offset()) {
115 throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
118 if (sc->subtitle_x_scale() != ref->subtitle_x_scale()) {
119 throw JoinError (_("Content to be joined must have the same subtitle X scale."));
122 if (sc->subtitle_y_scale() != ref->subtitle_y_scale()) {
123 throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
127 _use_subtitles = ref->use_subtitles ();
128 _subtitle_x_offset = ref->subtitle_x_offset ();
129 _subtitle_y_offset = ref->subtitle_y_offset ();
130 _subtitle_x_scale = ref->subtitle_x_scale ();
131 _subtitle_y_scale = ref->subtitle_y_scale ();
132 _subtitle_language = ref->subtitle_language ();
136 SubtitleContent::as_xml (xmlpp::Node* root) const
138 root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use_subtitles));
139 root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_subtitle_x_offset));
140 root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_subtitle_y_offset));
141 root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_subtitle_x_scale));
142 root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_subtitle_y_scale));
143 root->add_child("SubtitleLanguage")->add_child_text (_subtitle_language);
147 SubtitleContent::set_use_subtitles (bool u)
150 boost::mutex::scoped_lock lm (_mutex);
153 signal_changed (SubtitleContentProperty::USE_SUBTITLES);
157 SubtitleContent::set_subtitle_x_offset (double o)
160 boost::mutex::scoped_lock lm (_mutex);
161 _subtitle_x_offset = o;
163 signal_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
167 SubtitleContent::set_subtitle_y_offset (double o)
170 boost::mutex::scoped_lock lm (_mutex);
171 _subtitle_y_offset = o;
173 signal_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
177 SubtitleContent::set_subtitle_x_scale (double s)
180 boost::mutex::scoped_lock lm (_mutex);
181 _subtitle_x_scale = s;
183 signal_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
187 SubtitleContent::set_subtitle_y_scale (double s)
190 boost::mutex::scoped_lock lm (_mutex);
191 _subtitle_y_scale = s;
193 signal_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
197 SubtitleContent::set_subtitle_language (string language)
200 boost::mutex::scoped_lock lm (_mutex);
201 _subtitle_language = language;
203 signal_changed (SubtitleContentProperty::SUBTITLE_LANGUAGE);
207 SubtitleContent::identifier () const
210 s << Content::identifier()
211 << "_" << raw_convert<string> (subtitle_x_scale())
212 << "_" << raw_convert<string> (subtitle_y_scale())
213 << "_" << raw_convert<string> (subtitle_x_offset())
214 << "_" << raw_convert<string> (subtitle_y_offset());
216 /* The language is for metadata only, and doesn't affect
217 how this content looks.