8badf5183892a19107678d7c4b532250dfd5a426
[dcpomatic.git] / src / lib / subtitle_decoder.cc
1 /*
2     Copyright (C) 2013-2017 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "subtitle_decoder.h"
22 #include "subtitle_content.h"
23 #include "util.h"
24 #include "log.h"
25 #include "compose.hpp"
26 #include <sub/subtitle.h>
27 #include <boost/shared_ptr.hpp>
28 #include <boost/foreach.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include <iostream>
31
32 using std::list;
33 using std::cout;
34 using std::string;
35 using std::min;
36 using boost::shared_ptr;
37 using boost::optional;
38 using boost::function;
39
40 SubtitleDecoder::SubtitleDecoder (
41         Decoder* parent,
42         shared_ptr<const SubtitleContent> c,
43         shared_ptr<Log> log,
44         ContentTime first
45         )
46         : DecoderPart (parent, log)
47         , _content (c)
48         , _position (first)
49 {
50
51 }
52
53 /** Called by subclasses when an image subtitle is starting.
54  *  @param from From time of the subtitle.
55  *  @param image Subtitle image.
56  *  @param rect Area expressed as a fraction of the video frame that this subtitle
57  *  is for (e.g. a width of 0.5 means the width of the subtitle is half the width
58  *  of the video frame)
59  */
60 void
61 SubtitleDecoder::emit_image_start (ContentTime from, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
62 {
63         ImageStart (ContentImageSubtitle (from, image, rect));
64 }
65
66 void
67 SubtitleDecoder::emit_text_start (ContentTime from, list<dcp::SubtitleString> s)
68 {
69         BOOST_FOREACH (dcp::SubtitleString& i, s) {
70                 /* We must escape < and > in strings, otherwise they might confuse our subtitle
71                    renderer (which uses some HTML-esque markup to do bold/italic etc.)
72                 */
73                 string t = i.text ();
74                 boost::algorithm::replace_all (t, "<", "&lt;");
75                 boost::algorithm::replace_all (t, ">", "&gt;");
76                 i.set_text (t);
77
78                 /* Set any forced appearance */
79                 if (content()->colour()) {
80                         i.set_colour (*content()->colour());
81                 }
82                 if (content()->effect_colour()) {
83                         i.set_effect_colour (*content()->effect_colour());
84                 }
85                 if (content()->outline()) {
86                         i.set_effect (dcp::BORDER);
87                 } else if (content()->shadow()) {
88                         i.set_effect (dcp::SHADOW);
89                 }
90                 i.set_fade_up_time (dcp::Time(content()->fade_in().seconds(), 1000));
91                 i.set_fade_down_time (dcp::Time(content()->fade_out().seconds(), 1000));
92         }
93
94         TextStart (ContentTextSubtitle (from, s));
95         _position = from;
96 }
97
98 void
99 SubtitleDecoder::emit_text_start (ContentTime from, sub::Subtitle const & subtitle)
100 {
101         /* See if our next subtitle needs to be vertically placed on screen by us */
102         bool needs_placement = false;
103         optional<int> bottom_line;
104         BOOST_FOREACH (sub::Line i, subtitle.lines) {
105                 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
106                         needs_placement = true;
107                         DCPOMATIC_ASSERT (i.vertical_position.line);
108                         if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
109                                 bottom_line = i.vertical_position.line.get();
110                         }
111                 }
112         }
113
114         /* Find the lowest proportional position */
115         optional<float> lowest_proportional;
116         BOOST_FOREACH (sub::Line i, subtitle.lines) {
117                 if (i.vertical_position.proportional) {
118                         if (!lowest_proportional) {
119                                 lowest_proportional = i.vertical_position.proportional;
120                         } else {
121                                 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
122                         }
123                 }
124         }
125
126         list<dcp::SubtitleString> out;
127         BOOST_FOREACH (sub::Line i, subtitle.lines) {
128                 BOOST_FOREACH (sub::Block j, i.blocks) {
129
130                         if (!j.font_size.specified()) {
131                                 /* Fallback default font size if no other has been specified */
132                                 j.font_size.set_points (48);
133                         }
134
135                         float v_position;
136                         dcp::VAlign v_align;
137                         if (needs_placement) {
138                                 DCPOMATIC_ASSERT (i.vertical_position.line);
139                                 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
140                                    of the screen a bit to a pleasing degree.
141                                 */
142                                 v_position = 1.015 -
143                                         (1 + bottom_line.get() - i.vertical_position.line.get())
144                                         * 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
145
146                                 v_align = dcp::VALIGN_TOP;
147                         } else {
148                                 DCPOMATIC_ASSERT (i.vertical_position.proportional);
149                                 DCPOMATIC_ASSERT (i.vertical_position.reference);
150                                 v_position = i.vertical_position.proportional.get();
151
152                                 if (lowest_proportional) {
153                                         /* Adjust line spacing */
154                                         v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
155                                 }
156
157                                 switch (i.vertical_position.reference.get()) {
158                                 case sub::TOP_OF_SCREEN:
159                                         v_align = dcp::VALIGN_TOP;
160                                         break;
161                                 case sub::VERTICAL_CENTRE_OF_SCREEN:
162                                         v_align = dcp::VALIGN_CENTER;
163                                         break;
164                                 case sub::BOTTOM_OF_SCREEN:
165                                         v_align = dcp::VALIGN_BOTTOM;
166                                         break;
167                                 default:
168                                         v_align = dcp::VALIGN_TOP;
169                                         break;
170                                 }
171                         }
172
173                         dcp::HAlign h_align;
174                         switch (i.horizontal_position.reference) {
175                         case sub::LEFT_OF_SCREEN:
176                                 h_align = dcp::HALIGN_LEFT;
177                                 break;
178                         case sub::HORIZONTAL_CENTRE_OF_SCREEN:
179                                 h_align = dcp::HALIGN_CENTER;
180                                 break;
181                         case sub::RIGHT_OF_SCREEN:
182                                 h_align = dcp::HALIGN_RIGHT;
183                                 break;
184                         default:
185                                 h_align = dcp::HALIGN_CENTER;
186                                 break;
187                         }
188
189                         /* The idea here (rightly or wrongly) is that we set the appearance based on the
190                            values in the libsub objects, and these are overridden with values from the
191                            content by the other emit_text_start() above.
192                         */
193
194                         out.push_back (
195                                 dcp::SubtitleString (
196                                         string(TEXT_FONT_ID),
197                                         j.italic,
198                                         j.bold,
199                                         j.underline,
200                                         j.colour.dcp(),
201                                         j.font_size.points (72 * 11),
202                                         1.0,
203                                         dcp::Time (from.seconds(), 1000),
204                                         /* XXX: hmm; this is a bit ugly (we don't know the to time yet) */
205                                         dcp::Time (),
206                                         i.horizontal_position.proportional,
207                                         h_align,
208                                         v_position,
209                                         v_align,
210                                         dcp::DIRECTION_LTR,
211                                         j.text,
212                                         dcp::NONE,
213                                         j.effect_colour.get_value_or(sub::Colour(0, 0, 0)).dcp(),
214                                         /* Hack: we should use subtitle.fade_up and subtitle.fade_down here
215                                            but the times of these often don't have a frame rate associated
216                                            with them so the sub::Time won't convert them to milliseconds without
217                                            throwing an exception.  Since only DCP subs fill those in (and we don't
218                                            use libsub for DCP subs) we can cheat by just putting 0 in here.
219                                         */
220                                         dcp::Time (),
221                                         dcp::Time ()
222                                         )
223                                 );
224                 }
225         }
226
227         emit_text_start (from, out);
228 }
229
230 void
231 SubtitleDecoder::emit_stop (ContentTime to)
232 {
233         Stop (to);
234 }
235
236 void
237 SubtitleDecoder::emit_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
238 {
239         emit_text_start (period.from, s);
240         emit_stop (period.to);
241 }
242
243 void
244 SubtitleDecoder::emit_text (ContentTimePeriod period, sub::Subtitle const & s)
245 {
246         emit_text_start (period.from, s);
247         emit_stop (period.to);
248 }
249
250 void
251 SubtitleDecoder::seek ()
252 {
253         _position = ContentTime ();
254 }