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