Don't overlap simultaneous video content in the timeline. Fix keep-aligned for separ...
[dcpomatic.git] / src / lib / subtitle_content.cc
1 /*
2     Copyright (C) 2013 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 "subtitle_content.h"
22 #include "util.h"
23 #include "exceptions.h"
24
25 #include "i18n.h"
26
27 using std::string;
28 using std::vector;
29 using boost::shared_ptr;
30 using boost::lexical_cast;
31 using boost::dynamic_pointer_cast;
32
33 int const SubtitleContentProperty::SUBTITLE_X_OFFSET = 500;
34 int const SubtitleContentProperty::SUBTITLE_Y_OFFSET = 501;
35 int const SubtitleContentProperty::SUBTITLE_SCALE = 502;
36
37 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, boost::filesystem::path p)
38         : Content (f, p)
39         , _subtitle_x_offset (0)
40         , _subtitle_y_offset (0)
41         , _subtitle_scale (1)
42 {
43
44 }
45
46 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
47         : Content (f, node)
48         , _subtitle_x_offset (0)
49         , _subtitle_y_offset (0)
50         , _subtitle_scale (1)
51 {
52         LocaleGuard lg;
53
54         if (version >= 7) {
55                 _subtitle_x_offset = node->number_child<float> ("SubtitleXOffset");
56                 _subtitle_y_offset = node->number_child<float> ("SubtitleYOffset");
57         } else {
58                 _subtitle_y_offset = node->number_child<float> ("SubtitleOffset");
59         }
60         
61         _subtitle_scale = node->number_child<float> ("SubtitleScale");
62 }
63
64 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
65         : Content (f, c)
66 {
67         shared_ptr<SubtitleContent> ref = dynamic_pointer_cast<SubtitleContent> (c[0]);
68         assert (ref);
69         
70         for (size_t i = 0; i < c.size(); ++i) {
71                 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (c[i]);
72
73                 if (sc->subtitle_x_offset() != ref->subtitle_x_offset()) {
74                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
75                 }
76                 
77                 if (sc->subtitle_y_offset() != ref->subtitle_y_offset()) {
78                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
79                 }
80
81                 if (sc->subtitle_scale() != ref->subtitle_scale()) {
82                         throw JoinError (_("Content to be joined must have the same subtitle scale."));
83                 }
84         }
85
86         _subtitle_x_offset = ref->subtitle_x_offset ();
87         _subtitle_y_offset = ref->subtitle_y_offset ();
88         _subtitle_scale = ref->subtitle_scale ();
89 }
90
91 void
92 SubtitleContent::as_xml (xmlpp::Node* root) const
93 {
94         LocaleGuard lg;
95         
96         root->add_child("SubtitleXOffset")->add_child_text (lexical_cast<string> (_subtitle_x_offset));
97         root->add_child("SubtitleYOffset")->add_child_text (lexical_cast<string> (_subtitle_y_offset));
98         root->add_child("SubtitleScale")->add_child_text (lexical_cast<string> (_subtitle_scale));
99 }
100
101 void
102 SubtitleContent::set_subtitle_x_offset (double o)
103 {
104         {
105                 boost::mutex::scoped_lock lm (_mutex);
106                 _subtitle_x_offset = o;
107         }
108         signal_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
109 }
110
111 void
112 SubtitleContent::set_subtitle_y_offset (double o)
113 {
114         {
115                 boost::mutex::scoped_lock lm (_mutex);
116                 _subtitle_y_offset = o;
117         }
118         signal_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
119 }
120
121 void
122 SubtitleContent::set_subtitle_scale (double s)
123 {
124         {
125                 boost::mutex::scoped_lock lm (_mutex);
126                 _subtitle_scale = s;
127         }
128         signal_changed (SubtitleContentProperty::SUBTITLE_SCALE);
129 }