Nudge L/R-aligned subs in from the edge of the screen; do our own placement of subs...
[dcpomatic.git] / src / lib / text_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 "text_decoder.h"
22 #include "text_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 std::max;
37 using boost::shared_ptr;
38 using boost::optional;
39 using boost::function;
40 using namespace dcpomatic;
41
42 TextDecoder::TextDecoder (
43         Decoder* parent,
44         shared_ptr<const TextContent> c,
45         ContentTime first
46         )
47         : DecoderPart (parent)
48         , _content (c)
49         , _position (first)
50 {
51
52 }
53
54 /** Called by subclasses when an image subtitle is starting.
55  *  @param from From time of the subtitle.
56  *  @param image Subtitle image.
57  *  @param rect Area expressed as a fraction of the video frame that this subtitle
58  *  is for (e.g. a width of 0.5 means the width of the subtitle is half the width
59  *  of the video frame)
60  */
61 void
62 TextDecoder::emit_bitmap_start (ContentTime from, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
63 {
64         BitmapStart (ContentBitmapText (from, image, rect));
65         _position = from;
66 }
67
68 void
69 TextDecoder::emit_plain_start (ContentTime from, list<dcp::SubtitleString> s)
70 {
71         BOOST_FOREACH (dcp::SubtitleString& i, s) {
72                 /* We must escape < and > in strings, otherwise they might confuse our subtitle
73                    renderer (which uses some HTML-esque markup to do bold/italic etc.)
74                 */
75                 string t = i.text ();
76                 boost::algorithm::replace_all (t, "<", "&lt;");
77                 boost::algorithm::replace_all (t, ">", "&gt;");
78                 i.set_text (t);
79
80                 /* Set any forced appearance */
81                 if (content()->colour()) {
82                         i.set_colour (*content()->colour());
83                 }
84                 if (content()->effect_colour()) {
85                         i.set_effect_colour (*content()->effect_colour());
86                 }
87                 if (content()->effect()) {
88                         i.set_effect (*content()->effect());
89                 }
90                 if (content()->fade_in()) {
91                         i.set_fade_up_time (dcp::Time(content()->fade_in()->seconds(), 1000));
92                 }
93                 if (content()->fade_out()) {
94                         i.set_fade_down_time (dcp::Time(content()->fade_out()->seconds(), 1000));
95                 }
96         }
97
98         PlainStart (ContentStringText (from, s));
99         _position = from;
100 }
101
102 void
103 TextDecoder::emit_plain_start (ContentTime from, sub::Subtitle const & subtitle)
104 {
105         /* See if our next subtitle needs to be vertically placed on screen by us */
106         bool needs_placement = false;
107         optional<int> bottom_line;
108         BOOST_FOREACH (sub::Line i, subtitle.lines) {
109                 if (!i.vertical_position.reference || !i.vertical_position.lines || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
110                         needs_placement = true;
111                         DCPOMATIC_ASSERT (i.vertical_position.line);
112                         if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
113                                 bottom_line = i.vertical_position.line.get();
114                         }
115                 }
116         }
117
118         /* Find the lowest proportional position */
119         optional<float> lowest_proportional;
120         BOOST_FOREACH (sub::Line i, subtitle.lines) {
121                 if (i.vertical_position.proportional) {
122                         if (!lowest_proportional) {
123                                 lowest_proportional = i.vertical_position.proportional;
124                         } else {
125                                 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
126                         }
127                 }
128         }
129
130         list<dcp::SubtitleString> out;
131         BOOST_FOREACH (sub::Line i, subtitle.lines) {
132                 BOOST_FOREACH (sub::Block j, i.blocks) {
133
134                         if (!j.font_size.specified()) {
135                                 /* Fallback default font size if no other has been specified */
136                                 j.font_size.set_points (48);
137                         }
138
139                         float v_position;
140                         dcp::VAlign v_align;
141                         if (needs_placement) {
142                                 DCPOMATIC_ASSERT (i.vertical_position.line);
143                                 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
144                                    of the screen a bit to a pleasing degree.
145                                 */
146                                 v_position = 1.015 -
147                                         (1 + bottom_line.get() - i.vertical_position.line.get())
148                                         * 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
149
150                                 v_align = dcp::VALIGN_TOP;
151                         } else {
152                                 DCPOMATIC_ASSERT (i.vertical_position.reference);
153                                 if (i.vertical_position.proportional) {
154                                         v_position = i.vertical_position.proportional.get();
155                                 } else {
156                                         DCPOMATIC_ASSERT (i.vertical_position.line);
157                                         DCPOMATIC_ASSERT (i.vertical_position.lines);
158                                         v_position = float(*i.vertical_position.line) / *i.vertical_position.lines;
159                                 }
160
161                                 if (lowest_proportional) {
162                                         /* Adjust line spacing */
163                                         v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
164                                 }
165
166                                 switch (i.vertical_position.reference.get()) {
167                                 case sub::TOP_OF_SCREEN:
168                                         v_align = dcp::VALIGN_TOP;
169                                         break;
170                                 case sub::VERTICAL_CENTRE_OF_SCREEN:
171                                         v_align = dcp::VALIGN_CENTER;
172                                         break;
173                                 case sub::BOTTOM_OF_SCREEN:
174                                         v_align = dcp::VALIGN_BOTTOM;
175                                         break;
176                                 default:
177                                         v_align = dcp::VALIGN_TOP;
178                                         break;
179                                 }
180                         }
181
182                         dcp::HAlign h_align;
183                         float h_position = i.horizontal_position.proportional;
184                         switch (i.horizontal_position.reference) {
185                         case sub::LEFT_OF_SCREEN:
186                                 h_align = dcp::HALIGN_LEFT;
187                                 h_position = max(h_position, 0.05f);
188                                 break;
189                         case sub::HORIZONTAL_CENTRE_OF_SCREEN:
190                                 h_align = dcp::HALIGN_CENTER;
191                                 break;
192                         case sub::RIGHT_OF_SCREEN:
193                                 h_align = dcp::HALIGN_RIGHT;
194                                 h_position = max(h_position, 0.05f);
195                                 break;
196                         default:
197                                 h_align = dcp::HALIGN_CENTER;
198                                 break;
199                         }
200
201                         /* The idea here (rightly or wrongly) is that we set the appearance based on the
202                            values in the libsub objects, and these are overridden with values from the
203                            content by the other emit_plain_start() above.
204                         */
205
206                         out.push_back (
207                                 dcp::SubtitleString (
208                                         string(TEXT_FONT_ID),
209                                         j.italic,
210                                         j.bold,
211                                         j.underline,
212                                         j.colour.dcp(),
213                                         j.font_size.points (72 * 11),
214                                         1.0,
215                                         dcp::Time (from.seconds(), 1000),
216                                         /* XXX: hmm; this is a bit ugly (we don't know the to time yet) */
217                                         dcp::Time (),
218                                         h_position,
219                                         h_align,
220                                         v_position,
221                                         v_align,
222                                         dcp::DIRECTION_LTR,
223                                         j.text,
224                                         dcp::NONE,
225                                         j.effect_colour.get_value_or(sub::Colour(0, 0, 0)).dcp(),
226                                         /* Hack: we should use subtitle.fade_up and subtitle.fade_down here
227                                            but the times of these often don't have a frame rate associated
228                                            with them so the sub::Time won't convert them to milliseconds without
229                                            throwing an exception.  Since only DCP subs fill those in (and we don't
230                                            use libsub for DCP subs) we can cheat by just putting 0 in here.
231                                         */
232                                         dcp::Time (),
233                                         dcp::Time ()
234                                         )
235                                 );
236                 }
237         }
238
239         emit_plain_start (from, out);
240 }
241
242 void
243 TextDecoder::emit_stop (ContentTime to)
244 {
245         Stop (to);
246 }
247
248 void
249 TextDecoder::emit_plain (ContentTimePeriod period, list<dcp::SubtitleString> s)
250 {
251         emit_plain_start (period.from, s);
252         emit_stop (period.to);
253 }
254
255 void
256 TextDecoder::emit_plain (ContentTimePeriod period, sub::Subtitle const & s)
257 {
258         emit_plain_start (period.from, s);
259         emit_stop (period.to);
260 }
261
262 /*  @param rect Area expressed as a fraction of the video frame that this subtitle
263  *  is for (e.g. a width of 0.5 means the width of the subtitle is half the width
264  *  of the video frame)
265  */
266 void
267 TextDecoder::emit_bitmap (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
268 {
269         emit_bitmap_start (period.from, image, rect);
270         emit_stop (period.to);
271 }
272
273 void
274 TextDecoder::seek ()
275 {
276         _position = ContentTime ();
277 }