BOOST_FOREACH.
[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/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 std::max;
35 using std::shared_ptr;
36 using boost::optional;
37 using boost::function;
38 using namespace dcpomatic;
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         for (auto& 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         for (auto i: subtitle.lines) {
107                 if (!i.vertical_position.reference || (i.vertical_position.line && !i.vertical_position.lines) || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
108                         needs_placement = true;
109                         if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
110                                 bottom_line = i.vertical_position.line.get();
111                         }
112                 }
113         }
114
115         /* Find the lowest proportional position */
116         optional<float> lowest_proportional;
117         for (auto i: subtitle.lines) {
118                 if (i.vertical_position.proportional) {
119                         if (!lowest_proportional) {
120                                 lowest_proportional = i.vertical_position.proportional;
121                         } else {
122                                 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
123                         }
124                 }
125         }
126
127         list<dcp::SubtitleString> out;
128         for (auto i: subtitle.lines) {
129                 for (auto j: i.blocks) {
130
131                         if (!j.font_size.specified()) {
132                                 /* Fallback default font size if no other has been specified */
133                                 j.font_size.set_points (48);
134                         }
135
136                         float v_position;
137                         dcp::VAlign v_align;
138                         if (needs_placement) {
139                                 DCPOMATIC_ASSERT (i.vertical_position.line);
140                                 double const multiplier = 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
141                                 switch (i.vertical_position.reference.get_value_or(sub::BOTTOM_OF_SCREEN)) {
142                                 case sub::BOTTOM_OF_SCREEN:
143                                 case sub::TOP_OF_SUBTITLE:
144                                         /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
145                                            of the screen a bit to a pleasing degree.
146                                            */
147                                         v_position = 1.015 -
148                                                 (1 + bottom_line.get() - i.vertical_position.line.get()) * multiplier;
149
150                                         v_align = dcp::VALIGN_TOP;
151                                         break;
152                                 case sub::TOP_OF_SCREEN:
153                                         /* This 0.1 is another fudge factor to bring the top line away from the top of the screen a little */
154                                         v_position = 0.12 + i.vertical_position.line.get() * multiplier;
155                                         v_align = dcp::VALIGN_TOP;
156                                         break;
157                                 case sub::VERTICAL_CENTRE_OF_SCREEN:
158                                         v_position = i.vertical_position.line.get() * multiplier;
159                                         v_align = dcp::VALIGN_CENTER;
160                                         break;
161                                 }
162                         } else {
163                                 DCPOMATIC_ASSERT (i.vertical_position.reference);
164                                 if (i.vertical_position.proportional) {
165                                         v_position = i.vertical_position.proportional.get();
166                                 } else {
167                                         DCPOMATIC_ASSERT (i.vertical_position.line);
168                                         DCPOMATIC_ASSERT (i.vertical_position.lines);
169                                         v_position = float(*i.vertical_position.line) / *i.vertical_position.lines;
170                                 }
171
172                                 if (lowest_proportional) {
173                                         /* Adjust line spacing */
174                                         v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
175                                 }
176
177                                 switch (i.vertical_position.reference.get()) {
178                                 case sub::TOP_OF_SCREEN:
179                                         v_align = dcp::VALIGN_TOP;
180                                         break;
181                                 case sub::VERTICAL_CENTRE_OF_SCREEN:
182                                         v_align = dcp::VALIGN_CENTER;
183                                         break;
184                                 case sub::BOTTOM_OF_SCREEN:
185                                         v_align = dcp::VALIGN_BOTTOM;
186                                         break;
187                                 default:
188                                         v_align = dcp::VALIGN_TOP;
189                                         break;
190                                 }
191                         }
192
193                         dcp::HAlign h_align;
194                         float h_position = i.horizontal_position.proportional;
195                         switch (i.horizontal_position.reference) {
196                         case sub::LEFT_OF_SCREEN:
197                                 h_align = dcp::HALIGN_LEFT;
198                                 h_position = max(h_position, 0.05f);
199                                 break;
200                         case sub::HORIZONTAL_CENTRE_OF_SCREEN:
201                                 h_align = dcp::HALIGN_CENTER;
202                                 break;
203                         case sub::RIGHT_OF_SCREEN:
204                                 h_align = dcp::HALIGN_RIGHT;
205                                 h_position = max(h_position, 0.05f);
206                                 break;
207                         default:
208                                 h_align = dcp::HALIGN_CENTER;
209                                 break;
210                         }
211
212                         /* The idea here (rightly or wrongly) is that we set the appearance based on the
213                            values in the libsub objects, and these are overridden with values from the
214                            content by the other emit_plain_start() above.
215                         */
216
217                         out.push_back (
218                                 dcp::SubtitleString (
219                                         string(TEXT_FONT_ID),
220                                         j.italic,
221                                         j.bold,
222                                         j.underline,
223                                         j.colour.dcp(),
224                                         j.font_size.points (72 * 11),
225                                         1.0,
226                                         dcp::Time (from.seconds(), 1000),
227                                         /* XXX: hmm; this is a bit ugly (we don't know the to time yet) */
228                                         dcp::Time (),
229                                         h_position,
230                                         h_align,
231                                         v_position,
232                                         v_align,
233                                         dcp::DIRECTION_LTR,
234                                         j.text,
235                                         dcp::NONE,
236                                         j.effect_colour.get_value_or(sub::Colour(0, 0, 0)).dcp(),
237                                         /* Hack: we should use subtitle.fade_up and subtitle.fade_down here
238                                            but the times of these often don't have a frame rate associated
239                                            with them so the sub::Time won't convert them to milliseconds without
240                                            throwing an exception.  Since only DCP subs fill those in (and we don't
241                                            use libsub for DCP subs) we can cheat by just putting 0 in here.
242                                         */
243                                         dcp::Time (),
244                                         dcp::Time ()
245                                         )
246                                 );
247                 }
248         }
249
250         emit_plain_start (from, out);
251 }
252
253 void
254 TextDecoder::emit_stop (ContentTime to)
255 {
256         Stop (to);
257 }
258
259 void
260 TextDecoder::emit_plain (ContentTimePeriod period, list<dcp::SubtitleString> s)
261 {
262         emit_plain_start (period.from, s);
263         emit_stop (period.to);
264 }
265
266 void
267 TextDecoder::emit_plain (ContentTimePeriod period, sub::Subtitle const & s)
268 {
269         emit_plain_start (period.from, s);
270         emit_stop (period.to);
271 }
272
273 /*  @param rect Area expressed as a fraction of the video frame that this subtitle
274  *  is for (e.g. a width of 0.5 means the width of the subtitle is half the width
275  *  of the video frame)
276  */
277 void
278 TextDecoder::emit_bitmap (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
279 {
280         emit_bitmap_start (period.from, image, rect);
281         emit_stop (period.to);
282 }
283
284 void
285 TextDecoder::seek ()
286 {
287         _position = ContentTime ();
288 }