2 Copyright (C) 2014-2021 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/>.
23 #include "dcpomatic_assert.h"
25 #include "font_config.h"
27 #include "render_text.h"
30 #include <dcp/raw_convert.h>
31 #include <dcp/warnings.h>
32 #include <cairomm/cairomm.h>
33 LIBDCP_DISABLE_WARNINGS
35 LIBDCP_ENABLE_WARNINGS
36 #include <pango/pangocairo.h>
37 #include <boost/algorithm/string.hpp>
45 using std::make_shared;
49 using std::shared_ptr;
51 using namespace dcpomatic;
54 /** Create a Pango layout using a dummy context which we can use to calculate the size
55 * of the text we will render. Then we can transfer the layout over to the real context
56 * for the actual render.
58 static Glib::RefPtr<Pango::Layout>
61 auto c_font_map = pango_cairo_font_map_new ();
62 DCPOMATIC_ASSERT (c_font_map);
63 auto font_map = Glib::wrap (c_font_map);
64 auto c_context = pango_font_map_create_context (c_font_map);
65 DCPOMATIC_ASSERT (c_context);
66 auto context = Glib::wrap (c_context);
67 return Pango::Layout::create (context);
72 setup_layout (Glib::RefPtr<Pango::Layout> layout, string font_name, string markup)
74 layout->set_alignment (Pango::ALIGN_LEFT);
75 Pango::FontDescription font (font_name);
76 layout->set_font_description (font);
77 layout->set_markup (markup);
82 marked_up (list<StringText> subtitles, int target_height, float fade_factor, string font_name)
84 auto constexpr pixels_to_1024ths_point = 72 * 1024 / 96;
86 auto make_span = [target_height, fade_factor](StringText const& subtitle, string text, string extra_attribute) {
89 if (subtitle.italic()) {
90 span += "style=\"italic\" ";
92 if (subtitle.bold()) {
93 span += "weight=\"bold\" ";
95 if (subtitle.underline()) {
96 span += "underline=\"single\" ";
98 span += "size=\"" + dcp::raw_convert<string>(lrintf(subtitle.size_in_pixels(target_height) * pixels_to_1024ths_point)) + "\" ";
99 /* Between 1-65535 inclusive, apparently... */
100 span += "alpha=\"" + dcp::raw_convert<string>(int(floor(fade_factor * 65534)) + 1) + "\" ";
101 span += "color=\"#" + subtitle.colour().to_rgb_string() + "\"";
102 if (!extra_attribute.empty()) {
103 span += " " + extra_attribute;
112 for (auto const& i: subtitles) {
113 if (std::abs(i.space_before()) > dcp::SPACE_BEFORE_EPSILON) {
114 /* We need to insert some horizontal space into the layout. The only way I can find to do this
115 * is to write a " " with some special letter_spacing. As far as I can see, such a space will
116 * be written with letter_spacing either side. This means that to get a horizontal space x we
117 * need to write a " " with letter spacing (x - s) / 2, where s is the width of the " ".
119 auto layout = create_layout();
120 setup_layout(layout, font_name, make_span(i, " ", {}));
123 layout->get_pixel_size(space_width, dummy);
124 auto spacing = ((i.space_before() * i.size_in_pixels(target_height) - space_width) / 2) * pixels_to_1024ths_point;
125 out += make_span(i, " ", "letter_spacing=\"" + dcp::raw_convert<string>(spacing) + "\"");
128 out += make_span(i, i.text(), {});
136 set_source_rgba (Cairo::RefPtr<Cairo::Context> context, dcp::Colour colour, float fade_factor)
138 context->set_source_rgba (float(colour.r) / 255, float(colour.g) / 255, float(colour.b) / 255, fade_factor);
142 static shared_ptr<Image>
143 create_image (dcp::Size size)
145 /* FFmpeg BGRA means first byte blue, second byte green, third byte red, fourth byte alpha.
146 * This must be COMPACT as we're using it with Cairo::ImageSurface::create
148 auto image = make_shared<Image>(AV_PIX_FMT_BGRA, size, Image::Alignment::COMPACT);
149 image->make_black ();
154 static Cairo::RefPtr<Cairo::ImageSurface>
155 create_surface (shared_ptr<Image> image)
157 /* XXX: I don't think it's guaranteed that format_stride_for_width will return a stride without any padding,
158 * so it's lucky that this works.
160 DCPOMATIC_ASSERT (image->alignment() == Image::Alignment::COMPACT);
161 DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_BGRA);
162 return Cairo::ImageSurface::create (
164 Cairo::FORMAT_ARGB32,
166 image->size().height,
167 /* Cairo ARGB32 means first byte blue, second byte green, third byte red, fourth byte alpha */
168 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
174 setup_font (StringText const& subtitle, list<shared_ptr<Font>> const& fonts)
176 auto font_file = default_font_file ();
178 for (auto i: fonts) {
179 if (i->id() == subtitle.font() && i->file()) {
180 font_file = i->file().get();
184 return FontConfig::instance()->make_font_available(font_file);
189 calculate_fade_factor (StringText const& first, DCPTime time, int frame_rate)
191 float fade_factor = 1;
193 /* Round the fade start/end to the nearest frame start. Otherwise if a subtitle starts just after
194 the start of a frame it will be faded out.
196 auto const fade_in_start = DCPTime::from_seconds(first.in().as_seconds()).round(frame_rate);
197 auto const fade_in_end = fade_in_start + DCPTime::from_seconds (first.fade_up_time().as_seconds ());
199 if (fade_in_start <= time && time <= fade_in_end && fade_in_start != fade_in_end) {
200 fade_factor *= DCPTime(time - fade_in_start).seconds() / DCPTime(fade_in_end - fade_in_start).seconds();
203 if (time < fade_in_start) {
207 /* first.out() may be zero if we don't know when this subtitle will finish. We can only think about
208 * fading out if we _do_ know when it will finish.
210 if (first.out() != dcp::Time()) {
211 auto const fade_out_end = DCPTime::from_seconds (first.out().as_seconds()).round(frame_rate);
212 auto const fade_out_start = fade_out_end - DCPTime::from_seconds(first.fade_down_time().as_seconds());
214 if (fade_out_start <= time && time <= fade_out_end && fade_out_start != fade_out_end) {
215 fade_factor *= 1 - DCPTime(time - fade_out_start).seconds() / DCPTime(fade_out_end - fade_out_start).seconds();
217 if (time > fade_out_end) {
227 x_position (StringText const& first, int target_width, int layout_width)
230 switch (first.h_align()) {
231 case dcp::HAlign::LEFT:
232 /* h_position is distance between left of frame and left of subtitle */
233 x = first.h_position() * target_width;
235 case dcp::HAlign::CENTER:
236 /* h_position is distance between centre of frame and centre of subtitle */
237 x = (0.5 + first.h_position()) * target_width - layout_width / 2;
239 case dcp::HAlign::RIGHT:
240 /* h_position is distance between right of frame and right of subtitle */
241 x = (1.0 - first.h_position()) * target_width - layout_width;
250 y_position (StringText const& first, int target_height, int layout_height)
253 switch (first.v_align()) {
254 case dcp::VAlign::TOP:
255 /* SMPTE says that v_position is the distance between top
256 of frame and top of subtitle, but this doesn't always seem to be
257 the case in practice; Gunnar Ásgeirsson's Dolby server appears
258 to put VAlign::TOP subs with v_position as the distance between top
259 of frame and bottom of subtitle.
261 y = first.v_position() * target_height - layout_height;
263 case dcp::VAlign::CENTER:
264 /* v_position is distance between centre of frame and centre of subtitle */
265 y = (0.5 + first.v_position()) * target_height - layout_height / 2;
267 case dcp::VAlign::BOTTOM:
268 /* v_position is distance between bottom of frame and bottom of subtitle */
269 y = (1.0 - first.v_position()) * target_height - layout_height;
277 /** @param subtitles A list of subtitles that are all on the same line,
278 * at the same time and with the same fade in/out.
281 render_line (list<StringText> subtitles, list<shared_ptr<Font>> fonts, dcp::Size target, DCPTime time, int frame_rate)
283 /* XXX: this method can only handle italic / bold changes mid-line,
287 DCPOMATIC_ASSERT (!subtitles.empty ());
288 auto const& first = subtitles.front ();
290 auto const font_name = setup_font (first, fonts);
291 auto const fade_factor = calculate_fade_factor (first, time, frame_rate);
292 auto const markup = marked_up (subtitles, target.height, fade_factor, font_name);
293 auto layout = create_layout ();
294 setup_layout (layout, font_name, markup);
296 layout->get_pixel_size (size.width, size.height);
298 /* Calculate x and y scale factors. These are only used to stretch
299 the font away from its normal aspect ratio.
303 if (fabs (first.aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) {
304 if (first.aspect_adjust() < 1) {
305 x_scale = max (0.25f, first.aspect_adjust ());
309 y_scale = 1 / min (4.0f, first.aspect_adjust ());
313 auto const border_width = first.effect() == dcp::Effect::BORDER ? (first.outline_width * target.width / 2048.0) : 0;
314 size.width += 2 * ceil (border_width);
315 size.height += 2 * ceil (border_width);
317 size.width *= x_scale;
318 size.height *= y_scale;
320 /* Shuffle the subtitle over by the border width (if we have any) so it's not cut off */
321 int const x_offset = ceil (border_width);
322 /* Move down a bit so that accents on capital letters can be seen */
323 int const y_offset = target.height / 100.0;
325 size.width += x_offset;
326 size.height += y_offset;
328 auto image = create_image (size);
329 auto surface = create_surface (image);
330 auto context = Cairo::Context::create (surface);
332 context->set_line_width (1);
333 context->scale (x_scale, y_scale);
334 layout->update_from_cairo_context (context);
336 if (first.effect() == dcp::Effect::SHADOW) {
337 /* Drop-shadow effect */
338 set_source_rgba (context, first.effect_colour(), fade_factor);
339 context->move_to (x_offset + 4, y_offset + 4);
340 layout->add_to_cairo_context (context);
344 if (first.effect() == dcp::Effect::BORDER) {
346 set_source_rgba (context, first.effect_colour(), fade_factor);
347 context->set_line_width (border_width);
348 context->set_line_join (Cairo::LINE_JOIN_ROUND);
349 context->move_to (x_offset, y_offset);
350 layout->add_to_cairo_context (context);
354 /* The actual subtitle */
356 set_source_rgba (context, first.colour(), fade_factor);
358 context->move_to (x_offset, y_offset);
359 layout->add_to_cairo_context (context);
362 context->set_line_width (0.5);
363 context->move_to (x_offset, y_offset);
364 layout->add_to_cairo_context (context);
367 int const x = x_position (first, target.width, size.width);
368 int const y = y_position (first, target.height, size.height);
369 return PositionImage (image, Position<int>(max (0, x), max(0, y)));
373 /** @param time Time of the frame that these subtitles are going on.
374 * @param target Size of the container that this subtitle will end up in.
375 * @param frame_rate DCP frame rate.
378 render_text (list<StringText> subtitles, list<shared_ptr<Font>> fonts, dcp::Size target, DCPTime time, int frame_rate)
380 list<StringText> pending;
381 list<PositionImage> images;
383 for (auto const& i: subtitles) {
384 if (!pending.empty() && (i.v_align() != pending.back().v_align() || fabs(i.v_position() - pending.back().v_position()) > 1e-4)) {
385 images.push_back (render_line (pending, fonts, target, time, frame_rate));
388 pending.push_back (i);
391 if (!pending.empty()) {
392 images.push_back (render_line (pending, fonts, target, time, frame_rate));