Handle subtitles which contain Pango markup.
[dcpomatic.git] / src / lib / subtitle_decoder.cc
1 /*
2     Copyright (C) 2013-2016 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 <sub/subtitle.h>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/foreach.hpp>
27 #include <boost/algorithm/string.hpp>
28 #include <iostream>
29
30 using std::list;
31 using std::cout;
32 using std::string;
33 using std::min;
34 using boost::shared_ptr;
35 using boost::optional;
36 using boost::function;
37
38 SubtitleDecoder::SubtitleDecoder (
39         Decoder* parent,
40         shared_ptr<const SubtitleContent> c,
41         function<list<ContentTimePeriod> (ContentTimePeriod, bool)> image_during,
42         function<list<ContentTimePeriod> (ContentTimePeriod, bool)> text_during
43         )
44         : _parent (parent)
45         , _content (c)
46         , _image_during (image_during)
47         , _text_during (text_during)
48 {
49
50 }
51
52 /** Called by subclasses when an image subtitle is ready.
53  *  @param period Period of the subtitle.
54  *  @param image Subtitle image.
55  *  @param rect Area expressed as a fraction of the video frame that this subtitle
56  *  is for (e.g. a width of 0.5 means the width of the subtitle is half the width
57  *  of the video frame)
58  */
59 void
60 SubtitleDecoder::give_image (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
61 {
62         _decoded_image.push_back (ContentImageSubtitle (period, image, rect));
63 }
64
65 void
66 SubtitleDecoder::give_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
67 {
68         /* We must escape < and > in strings, otherwise they might confuse our subtitle
69            renderer (which uses some HTML-esque markup to do bold/italic etc.)
70         */
71         BOOST_FOREACH (dcp::SubtitleString& i, s) {
72                 string t = i.text ();
73                 boost::algorithm::replace_all (t, "<", "&lt;");
74                 boost::algorithm::replace_all (t, ">", "&gt;");
75                 i.set_text (t);
76         }
77
78         _decoded_text.push_back (ContentTextSubtitle (period, s));
79 }
80
81 /** @param sp Full periods of subtitles that are showing or starting during the specified period */
82 template <class T>
83 list<T>
84 SubtitleDecoder::get (list<T> const & subs, list<ContentTimePeriod> const & sp, ContentTimePeriod period, bool starting, bool accurate)
85 {
86         if (sp.empty ()) {
87                 /* Nothing in this period */
88                 return list<T> ();
89         }
90
91         /* Seek if what we want is before what we have, or a more than a little bit after */
92         if (subs.empty() || sp.back().to < subs.front().period().from || sp.front().from > (subs.back().period().to + ContentTime::from_seconds (1))) {
93                 _parent->seek (sp.front().from, true);
94         }
95
96         /* Now enough pass() calls will either:
97          *  (a) give us what we want, or
98          *  (b) hit the end of the decoder.
99          */
100         while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {}
101
102         /* Now look for what we wanted in the data we have collected */
103         /* XXX: inefficient */
104
105         list<T> out;
106         for (typename list<T>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
107                 if ((starting && period.contains(i->period().from)) || (!starting && period.overlap(i->period()))) {
108                         out.push_back (*i);
109                 }
110         }
111
112         /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */
113
114         list<ContentImageSubtitle>::iterator i = _decoded_image.begin();
115         while (i != _decoded_image.end()) {
116                 list<ContentImageSubtitle>::iterator tmp = i;
117                 ++tmp;
118
119                 if (
120                         i->period().to < (period.from - ContentTime::from_seconds (5)) ||
121                         i->period().from > (period.to + ContentTime::from_seconds (5))
122                         ) {
123                         _decoded_image.erase (i);
124                 }
125
126                 i = tmp;
127         }
128
129         return out;
130 }
131
132 list<ContentTextSubtitle>
133 SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
134 {
135         return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, starting, accurate);
136 }
137
138 list<ContentImageSubtitle>
139 SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
140 {
141         return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, starting, accurate);
142 }
143
144 void
145 SubtitleDecoder::seek (ContentTime, bool)
146 {
147         reset ();
148 }
149
150 void
151 SubtitleDecoder::reset ()
152 {
153         _decoded_text.clear ();
154         _decoded_image.clear ();
155 }
156
157 void
158 SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
159 {
160         /* See if our next subtitle needs to be placed on screen by us */
161         bool needs_placement = false;
162         optional<int> bottom_line;
163         BOOST_FOREACH (sub::Line i, subtitle.lines) {
164                 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
165                         needs_placement = true;
166                         DCPOMATIC_ASSERT (i.vertical_position.line);
167                         if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
168                                 bottom_line = i.vertical_position.line.get();
169                         }
170                 }
171         }
172
173         /* Find the lowest proportional postion */
174         optional<float> lowest_proportional;
175         BOOST_FOREACH (sub::Line i, subtitle.lines) {
176                 if (i.vertical_position.proportional) {
177                         if (!lowest_proportional) {
178                                 lowest_proportional = i.vertical_position.proportional;
179                         } else {
180                                 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
181                         }
182                 }
183         }
184
185         list<dcp::SubtitleString> out;
186         BOOST_FOREACH (sub::Line i, subtitle.lines) {
187                 BOOST_FOREACH (sub::Block j, i.blocks) {
188
189                         if (!j.font_size.specified()) {
190                                 /* Fallback default font size if none other has been specified */
191                                 j.font_size.set_points (48);
192                         }
193
194                         float v_position;
195                         dcp::VAlign v_align;
196                         if (needs_placement) {
197                                 DCPOMATIC_ASSERT (i.vertical_position.line);
198                                 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
199                                    of the screen a bit to a pleasing degree.
200                                 */
201                                 v_position = 1.015 -
202                                         (1 + bottom_line.get() - i.vertical_position.line.get())
203                                         * 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
204
205                                 v_align = dcp::VALIGN_TOP;
206                         } else {
207                                 DCPOMATIC_ASSERT (i.vertical_position.proportional);
208                                 DCPOMATIC_ASSERT (i.vertical_position.reference);
209                                 v_position = i.vertical_position.proportional.get();
210
211                                 if (lowest_proportional) {
212                                         /* Adjust line spacing */
213                                         v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
214                                 }
215
216                                 switch (i.vertical_position.reference.get()) {
217                                 case sub::TOP_OF_SCREEN:
218                                         v_align = dcp::VALIGN_TOP;
219                                         break;
220                                 case sub::CENTRE_OF_SCREEN:
221                                         v_align = dcp::VALIGN_CENTER;
222                                         break;
223                                 case sub::BOTTOM_OF_SCREEN:
224                                         v_align = dcp::VALIGN_BOTTOM;
225                                         break;
226                                 default:
227                                         v_align = dcp::VALIGN_TOP;
228                                         break;
229                                 }
230                         }
231
232                         dcp::Effect effect = dcp::NONE;
233                         if (content()->outline()) {
234                                 effect = dcp::BORDER;
235                         } else if (content()->shadow()) {
236                                 effect = dcp::SHADOW;
237                         }
238
239                         out.push_back (
240                                 dcp::SubtitleString (
241                                         string(TEXT_FONT_ID),
242                                         j.italic,
243                                         j.bold,
244                                         j.underline,
245                                         /* force the colour to whatever is configured */
246                                         content()->colour(),
247                                         j.font_size.points (72 * 11),
248                                         1.0,
249                                         dcp::Time (period.from.seconds(), 1000),
250                                         dcp::Time (period.to.seconds(), 1000),
251                                         0,
252                                         dcp::HALIGN_CENTER,
253                                         v_position,
254                                         v_align,
255                                         dcp::DIRECTION_LTR,
256                                         j.text,
257                                         effect,
258                                         content()->effect_colour(),
259                                         dcp::Time (content()->fade_in().seconds(), 1000),
260                                         dcp::Time (content()->fade_out().seconds(), 1000)
261                                         )
262                                 );
263                 }
264         }
265
266         give_text (period, out);
267 }