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 "text_decoder.h"
22 #include "text_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;
39 using namespace dcpomatic;
41 TextDecoder::TextDecoder (
43 shared_ptr<const TextContent> c,
46 : DecoderPart (parent)
53 /** Called by subclasses when an image subtitle is starting.
54 * @param from From time of the subtitle.
55 * @param image Subtitle image.
56 * @param rect Area expressed as a fraction of the video frame that this subtitle
57 * is for (e.g. a width of 0.5 means the width of the subtitle is half the width
61 TextDecoder::emit_bitmap_start (ContentTime from, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
63 BitmapStart (ContentBitmapText (from, image, rect));
68 TextDecoder::emit_plain_start (ContentTime from, list<dcp::SubtitleString> s)
70 BOOST_FOREACH (dcp::SubtitleString& i, s) {
71 /* We must escape < and > in strings, otherwise they might confuse our subtitle
72 renderer (which uses some HTML-esque markup to do bold/italic etc.)
75 boost::algorithm::replace_all (t, "<", "<");
76 boost::algorithm::replace_all (t, ">", ">");
79 /* Set any forced appearance */
80 if (content()->colour()) {
81 i.set_colour (*content()->colour());
83 if (content()->effect_colour()) {
84 i.set_effect_colour (*content()->effect_colour());
86 if (content()->effect()) {
87 i.set_effect (*content()->effect());
89 if (content()->fade_in()) {
90 i.set_fade_up_time (dcp::Time(content()->fade_in()->seconds(), 1000));
92 if (content()->fade_out()) {
93 i.set_fade_down_time (dcp::Time(content()->fade_out()->seconds(), 1000));
97 PlainStart (ContentStringText (from, s));
102 TextDecoder::emit_plain_start (ContentTime from, sub::Subtitle const & subtitle)
104 /* See if our next subtitle needs to be vertically placed on screen by us */
105 bool needs_placement = false;
106 optional<int> bottom_line;
107 BOOST_FOREACH (sub::Line i, subtitle.lines) {
108 if (!i.vertical_position.reference || i.vertical_position.reference.get() == sub::TOP_OF_SUBTITLE) {
109 needs_placement = true;
110 DCPOMATIC_ASSERT (i.vertical_position.line);
111 if (!bottom_line || bottom_line.get() < i.vertical_position.line.get()) {
112 bottom_line = i.vertical_position.line.get();
117 /* Find the lowest proportional position */
118 optional<float> lowest_proportional;
119 BOOST_FOREACH (sub::Line i, subtitle.lines) {
120 if (i.vertical_position.proportional) {
121 if (!lowest_proportional) {
122 lowest_proportional = i.vertical_position.proportional;
124 lowest_proportional = min (lowest_proportional.get(), i.vertical_position.proportional.get());
129 list<dcp::SubtitleString> out;
130 BOOST_FOREACH (sub::Line i, subtitle.lines) {
131 BOOST_FOREACH (sub::Block j, i.blocks) {
133 if (!j.font_size.specified()) {
134 /* Fallback default font size if no other has been specified */
135 j.font_size.set_points (48);
140 if (needs_placement) {
141 DCPOMATIC_ASSERT (i.vertical_position.line);
142 /* This 1.015 is an arbitrary value to lift the bottom sub off the bottom
143 of the screen a bit to a pleasing degree.
146 (1 + bottom_line.get() - i.vertical_position.line.get())
147 * 1.2 * content()->line_spacing() * content()->y_scale() * j.font_size.proportional (72 * 11);
149 v_align = dcp::VALIGN_TOP;
151 DCPOMATIC_ASSERT (i.vertical_position.proportional);
152 DCPOMATIC_ASSERT (i.vertical_position.reference);
153 v_position = i.vertical_position.proportional.get();
155 if (lowest_proportional) {
156 /* Adjust line spacing */
157 v_position = ((v_position - lowest_proportional.get()) * content()->line_spacing()) + lowest_proportional.get();
160 switch (i.vertical_position.reference.get()) {
161 case sub::TOP_OF_SCREEN:
162 v_align = dcp::VALIGN_TOP;
164 case sub::VERTICAL_CENTRE_OF_SCREEN:
165 v_align = dcp::VALIGN_CENTER;
167 case sub::BOTTOM_OF_SCREEN:
168 v_align = dcp::VALIGN_BOTTOM;
171 v_align = dcp::VALIGN_TOP;
177 switch (i.horizontal_position.reference) {
178 case sub::LEFT_OF_SCREEN:
179 h_align = dcp::HALIGN_LEFT;
181 case sub::HORIZONTAL_CENTRE_OF_SCREEN:
182 h_align = dcp::HALIGN_CENTER;
184 case sub::RIGHT_OF_SCREEN:
185 h_align = dcp::HALIGN_RIGHT;
188 h_align = dcp::HALIGN_CENTER;
192 /* The idea here (rightly or wrongly) is that we set the appearance based on the
193 values in the libsub objects, and these are overridden with values from the
194 content by the other emit_plain_start() above.
198 dcp::SubtitleString (
199 string(TEXT_FONT_ID),
204 j.font_size.points (72 * 11),
206 dcp::Time (from.seconds(), 1000),
207 /* XXX: hmm; this is a bit ugly (we don't know the to time yet) */
209 i.horizontal_position.proportional,
216 j.effect_colour.get_value_or(sub::Colour(0, 0, 0)).dcp(),
217 /* Hack: we should use subtitle.fade_up and subtitle.fade_down here
218 but the times of these often don't have a frame rate associated
219 with them so the sub::Time won't convert them to milliseconds without
220 throwing an exception. Since only DCP subs fill those in (and we don't
221 use libsub for DCP subs) we can cheat by just putting 0 in here.
230 emit_plain_start (from, out);
234 TextDecoder::emit_stop (ContentTime to)
240 TextDecoder::emit_plain (ContentTimePeriod period, list<dcp::SubtitleString> s)
242 emit_plain_start (period.from, s);
243 emit_stop (period.to);
247 TextDecoder::emit_plain (ContentTimePeriod period, sub::Subtitle const & s)
249 emit_plain_start (period.from, s);
250 emit_stop (period.to);
253 /* @param rect Area expressed as a fraction of the video frame that this subtitle
254 * is for (e.g. a width of 0.5 means the width of the subtitle is half the width
255 * of the video frame)
258 TextDecoder::emit_bitmap (ContentTimePeriod period, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
260 emit_bitmap_start (period.from, image, rect);
261 emit_stop (period.to);
267 _position = ContentTime ();