Include trimming.
[dcpomatic.git] / src / lib / text_subtitle_content.cc
1 /*
2     Copyright (C) 2014-2016 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 "text_subtitle_content.h"
21 #include "util.h"
22 #include "text_subtitle.h"
23 #include "film.h"
24 #include "font.h"
25 #include "raw_convert.h"
26 #include <libxml++/libxml++.h>
27 #include <iostream>
28
29 #include "i18n.h"
30
31 using std::string;
32 using std::cout;
33 using boost::shared_ptr;
34
35 std::string const TextSubtitleContent::font_id = "font";
36
37 int const TextSubtitleContentProperty::TEXT_SUBTITLE_COLOUR = 300;
38 int const TextSubtitleContentProperty::TEXT_SUBTITLE_OUTLINE = 301;
39 int const TextSubtitleContentProperty::TEXT_SUBTITLE_OUTLINE_COLOUR = 302;
40
41 TextSubtitleContent::TextSubtitleContent (shared_ptr<const Film> film, boost::filesystem::path path)
42         : Content (film, path)
43         , SubtitleContent (film, path)
44         , _colour (255, 255, 255)
45         , _outline (false)
46         , _outline_colour (0, 0, 0)
47 {
48
49 }
50
51 TextSubtitleContent::TextSubtitleContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
52         : Content (film, node)
53         , SubtitleContent (film, node, version)
54         , _length (node->number_child<ContentTime::Type> ("Length"))
55         , _frame_rate (node->optional_number_child<double>("SubtitleFrameRate"))
56         , _colour (
57                 node->optional_number_child<int>("Red").get_value_or(255),
58                 node->optional_number_child<int>("Green").get_value_or(255),
59                 node->optional_number_child<int>("Blue").get_value_or(255)
60                 )
61         , _outline (node->optional_bool_child("Outline").get_value_or(false))
62         , _outline_colour (
63                 node->optional_number_child<int>("OutlineRed").get_value_or(255),
64                 node->optional_number_child<int>("OutlineGreen").get_value_or(255),
65                 node->optional_number_child<int>("OutlineBlue").get_value_or(255)
66                 )
67 {
68
69 }
70
71 void
72 TextSubtitleContent::examine (boost::shared_ptr<Job> job)
73 {
74         Content::examine (job);
75         TextSubtitle s (shared_from_this ());
76
77         /* Default to turning these subtitles on */
78         set_use_subtitles (true);
79
80         boost::mutex::scoped_lock lm (_mutex);
81         _length = s.length ();
82         add_font (shared_ptr<Font> (new Font (font_id)));
83 }
84
85 string
86 TextSubtitleContent::summary () const
87 {
88         return path_summary() + " " + _("[subtitles]");
89 }
90
91 string
92 TextSubtitleContent::technical_summary () const
93 {
94         return Content::technical_summary() + " - " + _("Text subtitles");
95 }
96
97 void
98 TextSubtitleContent::as_xml (xmlpp::Node* node) const
99 {
100         node->add_child("Type")->add_child_text ("TextSubtitle");
101         Content::as_xml (node);
102         SubtitleContent::as_xml (node);
103         node->add_child("Length")->add_child_text (raw_convert<string> (_length.get ()));
104         node->add_child("Red")->add_child_text (raw_convert<string> (_colour.r));
105         node->add_child("Green")->add_child_text (raw_convert<string> (_colour.g));
106         node->add_child("Blue")->add_child_text (raw_convert<string> (_colour.b));
107         node->add_child("Outline")->add_child_text (raw_convert<string> (_outline));
108         node->add_child("OutlineRed")->add_child_text (raw_convert<string> (_outline_colour.r));
109         node->add_child("OutlineGreen")->add_child_text (raw_convert<string> (_outline_colour.g));
110         node->add_child("OutlineBlue")->add_child_text (raw_convert<string> (_outline_colour.b));
111 }
112
113 DCPTime
114 TextSubtitleContent::full_length () const
115 {
116         FrameRateChange const frc (subtitle_video_frame_rate(), film()->video_frame_rate ());
117         return DCPTime (_length, frc);
118 }
119
120 void
121 TextSubtitleContent::set_subtitle_video_frame_rate (int r)
122 {
123         {
124                 boost::mutex::scoped_lock lm (_mutex);
125                 _frame_rate = r;
126         }
127
128         signal_changed (SubtitleContentProperty::SUBTITLE_VIDEO_FRAME_RATE);
129 }
130
131 double
132 TextSubtitleContent::subtitle_video_frame_rate () const
133 {
134         {
135                 boost::mutex::scoped_lock lm (_mutex);
136                 if (_frame_rate) {
137                         return _frame_rate.get ();
138                 }
139         }
140
141         /* No frame rate specified, so assume this content has been
142            prepared for any concurrent video content.
143         */
144         return film()->active_frame_rate_change(position()).source;
145 }
146
147 void
148 TextSubtitleContent::set_colour (dcp::Colour colour)
149 {
150         {
151                 boost::mutex::scoped_lock lm (_mutex);
152                 if (_colour == colour) {
153                         return;
154                 }
155
156                 _colour = colour;
157         }
158
159         signal_changed (TextSubtitleContentProperty::TEXT_SUBTITLE_COLOUR);
160 }
161
162 void
163 TextSubtitleContent::set_outline (bool o)
164 {
165         {
166                 boost::mutex::scoped_lock lm (_mutex);
167                 if (_outline == o) {
168                         return;
169                 }
170
171                 _outline = o;
172         }
173
174         signal_changed (TextSubtitleContentProperty::TEXT_SUBTITLE_OUTLINE);
175 }
176
177 void
178 TextSubtitleContent::set_outline_colour (dcp::Colour colour)
179 {
180         {
181                 boost::mutex::scoped_lock lm (_mutex);
182                 if (_outline_colour == colour) {
183                         return;
184                 }
185
186                 _outline_colour = colour;
187         }
188
189         signal_changed (TextSubtitleContentProperty::TEXT_SUBTITLE_OUTLINE_COLOUR);
190 }