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