Basic grunt-work, untested and unfinished, but it compiles.
[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 "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         )
45         : DecoderPart (parent, log)
46         , _content (c)
47 {
48
49 }
50
51 /** Called by subclasses when an image subtitle is ready.
52  *  @param period Period of the subtitle.
53  *  @param image Subtitle image.
54  *  @param rect Area expressed as a fraction of the video frame that this subtitle
55  *  is for (e.g. a width of 0.5 means the width of the subtitle is half the width
56  *  of the video frame)
57  */
58 void
59 SubtitleDecoder::emit_image (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
60 {
61         ImageData (ContentImageSubtitle (period, image, rect));
62 }
63
64 void
65 SubtitleDecoder::emit_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
66 {
67         /* We must escape < and > in strings, otherwise they might confuse our subtitle
68            renderer (which uses some HTML-esque markup to do bold/italic etc.)
69         */
70         BOOST_FOREACH (dcp::SubtitleString& i, s) {
71                 string t = i.text ();
72                 boost::algorithm::replace_all (t, "<", "&lt;");
73                 boost::algorithm::replace_all (t, ">", "&gt;");
74                 i.set_text (t);
75         }
76
77         TextData (ContentTextSubtitle (period, s));
78 }
79
80 void
81 SubtitleDecoder::emit_text (ContentTimePeriod period, sub::Subtitle const & subtitle)
82 {
83         /* See if our next subtitle needs to be placed on screen by us */
84         bool needs_placement = false;
85         optional<int> bottom_line;
86         BOOST_FOREACH (sub::Line i, subtitle.lines) {
87                 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
88                         needs_placement = true;
89                         DCPOMATIC_ASSERT (i.vertical_position.line);
90                         if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
91                                 bottom_line = i.vertical_position.line.get();
92                         }
93                 }
94         }
95
96         /* Find the lowest proportional position */
97         optional<float> lowest_proportional;
98         BOOST_FOREACH (sub::Line i, subtitle.lines) {
99                 if (i.vertical_position.proportional) {
100                         if (!lowest_proportional) {
101                                 lowest_proportional = i.vertical_position.proportional;
102                         } else {
103                                 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
104                         }
105                 }
106         }
107
108         list<dcp::SubtitleString> out;
109         BOOST_FOREACH (sub::Line i, subtitle.lines) {
110                 BOOST_FOREACH (sub::Block j, i.blocks) {
111
112                         if (!j.font_size.specified()) {
113                                 /* Fallback default font size if no other has been specified */
114                                 j.font_size.set_points (48);
115                         }
116
117                         float v_position;
118                         dcp::VAlign v_align;
119                         if (needs_placement) {
120                                 DCPOMATIC_ASSERT (i.vertical_position.line);
121                                 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
122                                    of the screen a bit to a pleasing degree.
123                                 */
124                                 v_position = 1.015 -
125                                         (1 + bottom_line.get() - i.vertical_position.line.get())
126                                         * 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
127
128                                 v_align = dcp::VALIGN_TOP;
129                         } else {
130                                 DCPOMATIC_ASSERT (i.vertical_position.proportional);
131                                 DCPOMATIC_ASSERT (i.vertical_position.reference);
132                                 v_position = i.vertical_position.proportional.get();
133
134                                 if (lowest_proportional) {
135                                         /* Adjust line spacing */
136                                         v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
137                                 }
138
139                                 switch (i.vertical_position.reference.get()) {
140                                 case sub::TOP_OF_SCREEN:
141                                         v_align = dcp::VALIGN_TOP;
142                                         break;
143                                 case sub::CENTRE_OF_SCREEN:
144                                         v_align = dcp::VALIGN_CENTER;
145                                         break;
146                                 case sub::BOTTOM_OF_SCREEN:
147                                         v_align = dcp::VALIGN_BOTTOM;
148                                         break;
149                                 default:
150                                         v_align = dcp::VALIGN_TOP;
151                                         break;
152                                 }
153                         }
154
155                         dcp::Effect effect = dcp::NONE;
156                         if (content()->outline()) {
157                                 effect = dcp::BORDER;
158                         } else if (content()->shadow()) {
159                                 effect = dcp::SHADOW;
160                         }
161
162                         out.push_back (
163                                 dcp::SubtitleString (
164                                         string(TEXT_FONT_ID),
165                                         j.italic,
166                                         j.bold,
167                                         j.underline,
168                                         /* force the colour to whatever is configured */
169                                         content()->colour(),
170                                         j.font_size.points (72 * 11),
171                                         1.0,
172                                         dcp::Time (period.from.seconds(), 1000),
173                                         dcp::Time (period.to.seconds(), 1000),
174                                         0,
175                                         dcp::HALIGN_CENTER,
176                                         v_position,
177                                         v_align,
178                                         dcp::DIRECTION_LTR,
179                                         j.text,
180                                         effect,
181                                         content()->effect_colour(),
182                                         dcp::Time (content()->fade_in().seconds(), 1000),
183                                         dcp::Time (content()->fade_out().seconds(), 1000)
184                                         )
185                                 );
186                 }
187         }
188
189         emit_text (period, out);
190 }