2 Copyright (C) 2013-2017 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "subtitle_decoder.h"
22 #include "subtitle_content.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>
36 using boost::shared_ptr;
37 using boost::optional;
38 using boost::function;
40 SubtitleDecoder::SubtitleDecoder (
42 shared_ptr<const SubtitleContent> c,
45 : DecoderPart (parent, log)
51 /** Called by subclasses when an image subtitle is starting.
52 * @param from From time 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
59 SubtitleDecoder::emit_image_start (ContentTime from, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
61 ImageStart (ContentImageSubtitle (from, image, rect));
65 SubtitleDecoder::emit_text_start (ContentTime from, list<dcp::SubtitleString> s)
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.)
70 BOOST_FOREACH (dcp::SubtitleString& i, s) {
72 boost::algorithm::replace_all (t, "<", "<");
73 boost::algorithm::replace_all (t, ">", ">");
77 TextStart (ContentTextSubtitle (from, s));
82 SubtitleDecoder::emit_text_start (ContentTime from, sub::Subtitle const & subtitle)
84 /* See if our next subtitle needs to be placed on screen by us */
85 bool needs_placement = false;
86 optional<int> bottom_line;
87 BOOST_FOREACH (sub::Line i, subtitle.lines) {
88 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
89 needs_placement = true;
90 DCPOMATIC_ASSERT (i.vertical_position.line);
91 if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
92 bottom_line = i.vertical_position.line.get();
97 /* Find the lowest proportional position */
98 optional<float> lowest_proportional;
99 BOOST_FOREACH (sub::Line i, subtitle.lines) {
100 if (i.vertical_position.proportional) {
101 if (!lowest_proportional) {
102 lowest_proportional = i.vertical_position.proportional;
104 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
109 list<dcp::SubtitleString> out;
110 BOOST_FOREACH (sub::Line i, subtitle.lines) {
111 BOOST_FOREACH (sub::Block j, i.blocks) {
113 if (!j.font_size.specified()) {
114 /* Fallback default font size if no other has been specified */
115 j.font_size.set_points (48);
120 if (needs_placement) {
121 DCPOMATIC_ASSERT (i.vertical_position.line);
122 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
123 of the screen a bit to a pleasing degree.
126 (1 + bottom_line.get() - i.vertical_position.line.get())
127 * 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
129 v_align = dcp::VALIGN_TOP;
131 DCPOMATIC_ASSERT (i.vertical_position.proportional);
132 DCPOMATIC_ASSERT (i.vertical_position.reference);
133 v_position = i.vertical_position.proportional.get();
135 if (lowest_proportional) {
136 /* Adjust line spacing */
137 v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
140 switch (i.vertical_position.reference.get()) {
141 case sub::TOP_OF_SCREEN:
142 v_align = dcp::VALIGN_TOP;
144 case sub::CENTRE_OF_SCREEN:
145 v_align = dcp::VALIGN_CENTER;
147 case sub::BOTTOM_OF_SCREEN:
148 v_align = dcp::VALIGN_BOTTOM;
151 v_align = dcp::VALIGN_TOP;
156 dcp::Effect effect = dcp::NONE;
157 if (content()->outline()) {
158 effect = dcp::BORDER;
159 } else if (content()->shadow()) {
160 effect = dcp::SHADOW;
164 dcp::SubtitleString (
165 string(TEXT_FONT_ID),
169 /* force the colour to whatever is configured */
171 j.font_size.points (72 * 11),
173 dcp::Time (from.seconds(), 1000),
174 /* XXX: hmm; this is a bit ugly (we don't know the to time yet) */
183 content()->effect_colour(),
184 dcp::Time (content()->fade_in().seconds(), 1000),
185 dcp::Time (content()->fade_out().seconds(), 1000)
191 emit_text_start (from, out);
195 SubtitleDecoder::emit_stop (ContentTime to)
201 SubtitleDecoder::emit_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
203 emit_text_start (period.from, s);
204 emit_stop (period.to);
208 SubtitleDecoder::emit_text (ContentTimePeriod period, sub::Subtitle const & s)
210 emit_text_start (period.from, s);
211 emit_stop (period.to);
215 SubtitleDecoder::seek ()
217 _position = ContentTime ();