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"
26 #include "render_text.h"
30 #include <dcp/raw_convert.h>
31 #include <fontconfig/fontconfig.h>
32 #include <cairomm/cairomm.h>
33 DCPOMATIC_DISABLE_WARNINGS
35 DCPOMATIC_ENABLE_WARNINGS
36 #include <pango/pangocairo.h>
37 #ifndef DCPOMATIC_HAVE_SHOW_IN_CAIRO_CONTEXT
38 #include <pango/pangocairo.h>
40 #include <boost/algorithm/string.hpp>
48 using std::make_shared;
52 using std::shared_ptr;
54 using boost::optional;
55 using boost::algorithm::replace_all;
56 using namespace dcpomatic;
59 static FcConfig* fc_config = nullptr;
60 static list<pair<boost::filesystem::path, string>> fc_config_fonts;
64 marked_up (list<StringText> subtitles, int target_height, float fade_factor)
68 for (auto const& i: subtitles) {
71 out += "style=\"italic\" ";
74 out += "weight=\"bold\" ";
77 out += "underline=\"single\" ";
79 out += "size=\"" + dcp::raw_convert<string>(i.size_in_pixels(target_height) * 72 * 1024 / 96) + "\" ";
80 /* Between 1-65535 inclusive, apparently... */
81 out += "alpha=\"" + dcp::raw_convert<string>(int(floor(fade_factor * 65534)) + 1) + "\" ";
82 out += "color=\"#" + i.colour().to_rgb_string() + "\">";
85 replace_all(t, "&", "&");
96 set_source_rgba (Cairo::RefPtr<Cairo::Context> context, dcp::Colour colour, float fade_factor)
98 context->set_source_rgba (float(colour.r) / 255, float(colour.g) / 255, float(colour.b) / 255, fade_factor);
102 static shared_ptr<Image>
103 create_image (dcp::Size size)
105 /* FFmpeg BGRA means first byte blue, second byte green, third byte red, fourth byte alpha */
106 auto image = make_shared<Image>(AV_PIX_FMT_BGRA, size, false);
107 image->make_black ();
112 static Cairo::RefPtr<Cairo::ImageSurface>
113 create_surface (shared_ptr<Image> image)
115 #ifdef DCPOMATIC_HAVE_FORMAT_STRIDE_FOR_WIDTH
116 auto surface = Cairo::ImageSurface::create (
118 Cairo::FORMAT_ARGB32,
120 image->size().height,
121 /* Cairo ARGB32 means first byte blue, second byte green, third byte red, fourth byte alpha */
122 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
125 /* Centos 5 does not have Cairo::ImageSurface::format_stride_for_width, so just use width * 4
126 which I hope is safe (if slow)
128 auto surface = Cairo::ImageSurface::create (
130 Cairo::FORMAT_ARGB32,
132 image->size().height,
133 image->size().width * 4
142 setup_font (StringText const& subtitle, list<shared_ptr<Font>> const& fonts)
145 fc_config = FcInitLoadConfig ();
148 auto font_file = default_font_file ();
150 for (auto i: fonts) {
151 if (i->id() == subtitle.font() && i->file()) {
152 font_file = i->file().get();
156 auto existing = fc_config_fonts.cbegin ();
157 while (existing != fc_config_fonts.end() && existing->first != font_file) {
162 if (existing != fc_config_fonts.end ()) {
163 font_name = existing->second;
165 /* Make this font available to DCP-o-matic */
166 FcConfigAppFontAddFile (fc_config, reinterpret_cast<FcChar8 const *>(font_file.string().c_str()));
167 auto pattern = FcPatternBuild (
168 0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *>(0)
170 auto object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
171 auto font_set = FcFontList (fc_config, pattern, object_set);
173 for (int i = 0; i < font_set->nfont; ++i) {
174 FcPattern* font = font_set->fonts[i];
179 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
180 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
181 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
183 font_name = reinterpret_cast<char const *> (family);
187 FcFontSetDestroy (font_set);
190 FcObjectSetDestroy (object_set);
191 FcPatternDestroy (pattern);
193 fc_config_fonts.push_back (make_pair(font_file, font_name));
196 FcConfigSetCurrent (fc_config);
202 calculate_fade_factor (StringText const& first, DCPTime time, int frame_rate)
204 float fade_factor = 1;
206 /* Round the fade start/end to the nearest frame start. Otherwise if a subtitle starts just after
207 the start of a frame it will be faded out.
209 auto const fade_in_start = DCPTime::from_seconds(first.in().as_seconds()).round(frame_rate);
210 auto const fade_in_end = fade_in_start + DCPTime::from_seconds (first.fade_up_time().as_seconds ());
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_in_start <= time && time <= fade_in_end && fade_in_start != fade_in_end) {
215 fade_factor *= DCPTime(time - fade_in_start).seconds() / DCPTime(fade_in_end - fade_in_start).seconds();
217 if (fade_out_start <= time && time <= fade_out_end && fade_out_start != fade_out_end) {
218 fade_factor *= 1 - DCPTime(time - fade_out_start).seconds() / DCPTime(fade_out_end - fade_out_start).seconds();
220 if (time < fade_in_start || time > fade_out_end) {
229 x_position (StringText const& first, int target_width, int layout_width)
232 switch (first.h_align()) {
233 case dcp::HAlign::LEFT:
234 /* h_position is distance between left of frame and left of subtitle */
235 x = first.h_position() * target_width;
237 case dcp::HAlign::CENTER:
238 /* h_position is distance between centre of frame and centre of subtitle */
239 x = (0.5 + first.h_position()) * target_width - layout_width / 2;
241 case dcp::HAlign::RIGHT:
242 /* h_position is distance between right of frame and right of subtitle */
243 x = (1.0 - first.h_position()) * target_width - layout_width;
252 y_position (StringText const& first, int target_height, int layout_height)
255 switch (first.v_align()) {
256 case dcp::VAlign::TOP:
257 /* SMPTE says that v_position is the distance between top
258 of frame and top of subtitle, but this doesn't always seem to be
259 the case in practice; Gunnar Ásgeirsson's Dolby server appears
260 to put VAlign::TOP subs with v_position as the distance between top
261 of frame and bottom of subtitle.
263 y = first.v_position() * target_height - layout_height;
265 case dcp::VAlign::CENTER:
266 /* v_position is distance between centre of frame and centre of subtitle */
267 y = (0.5 + first.v_position()) * target_height - layout_height / 2;
269 case dcp::VAlign::BOTTOM:
270 /* v_position is distance between bottom of frame and bottom of subtitle */
271 y = (1.0 - first.v_position()) * target_height - layout_height;
280 setup_layout (Glib::RefPtr<Pango::Layout> layout, string font_name, string markup)
282 layout->set_alignment (Pango::ALIGN_LEFT);
283 Pango::FontDescription font (font_name);
284 layout->set_font_description (font);
285 layout->set_markup (markup);
289 /** Create a Pango layout using a dummy context which we can use to calculate the size
290 * of the text we will render. Then we can transfer the layout over to the real context
291 * for the actual render.
293 static Glib::RefPtr<Pango::Layout>
296 auto c_font_map = pango_cairo_font_map_new ();
297 DCPOMATIC_ASSERT (c_font_map);
298 auto font_map = Glib::wrap (c_font_map);
299 auto c_context = pango_font_map_create_context (c_font_map);
300 DCPOMATIC_ASSERT (c_context);
301 auto context = Glib::wrap (c_context);
302 return Pango::Layout::create (context);
306 /** @param subtitles A list of subtitles that are all on the same line,
307 * at the same time and with the same fade in/out.
310 render_line (list<StringText> subtitles, list<shared_ptr<Font>> fonts, dcp::Size target, DCPTime time, int frame_rate)
312 /* XXX: this method can only handle italic / bold changes mid-line,
316 DCPOMATIC_ASSERT (!subtitles.empty ());
317 auto const& first = subtitles.front ();
319 auto const font_name = setup_font (first, fonts);
320 auto const fade_factor = calculate_fade_factor (first, time, frame_rate);
321 auto const markup = marked_up (subtitles, target.height, fade_factor);
322 auto layout = create_layout ();
323 setup_layout (layout, font_name, markup);
325 layout->get_pixel_size (size.width, size.height);
327 /* Calculate x and y scale factors. These are only used to stretch
328 the font away from its normal aspect ratio.
332 if (fabs (first.aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) {
333 if (first.aspect_adjust() < 1) {
334 x_scale = max (0.25f, first.aspect_adjust ());
338 y_scale = 1 / min (4.0f, first.aspect_adjust ());
342 auto const border_width = first.effect() == dcp::Effect::BORDER ? (first.outline_width * target.width / 2048.0) : 0;
343 size.width += 2 * ceil (border_width);
344 size.height += 2 * ceil (border_width);
346 size.width *= x_scale;
347 size.height *= y_scale;
349 /* Shuffle the subtitle over by the border width (if we have any) so it's not cut off */
350 int const x_offset = ceil (border_width);
351 /* Move down a bit so that accents on capital letters can be seen */
352 int const y_offset = target.height / 100.0;
354 size.width += x_offset;
355 size.height += y_offset;
357 auto image = create_image (size);
358 auto surface = create_surface (image);
359 auto context = Cairo::Context::create (surface);
361 context->set_line_width (1);
362 context->scale (x_scale, y_scale);
363 layout->update_from_cairo_context (context);
365 if (first.effect() == dcp::Effect::SHADOW) {
366 /* Drop-shadow effect */
367 set_source_rgba (context, first.effect_colour(), fade_factor);
368 context->move_to (x_offset + 4, y_offset + 4);
369 layout->add_to_cairo_context (context);
373 if (first.effect() == dcp::Effect::BORDER) {
375 set_source_rgba (context, first.effect_colour(), fade_factor);
376 context->set_line_width (border_width);
377 context->set_line_join (Cairo::LINE_JOIN_ROUND);
378 context->move_to (x_offset, y_offset);
379 layout->add_to_cairo_context (context);
383 /* The actual subtitle */
385 set_source_rgba (context, first.colour(), fade_factor);
387 context->move_to (x_offset, y_offset);
388 layout->add_to_cairo_context (context);
391 context->set_line_width (0.5);
392 context->move_to (x_offset, y_offset);
393 layout->add_to_cairo_context (context);
396 int const x = x_position (first, target.width, size.width);
397 int const y = y_position (first, target.height, size.height);
398 return PositionImage (image, Position<int>(max (0, x), max(0, y)));
402 /** @param time Time of the frame that these subtitles are going on.
403 * @param target Size of the container that this subtitle will end up in.
404 * @param frame_rate DCP frame rate.
407 render_text (list<StringText> subtitles, list<shared_ptr<Font>> fonts, dcp::Size target, DCPTime time, int frame_rate)
409 list<StringText> pending;
410 list<PositionImage> images;
412 for (auto const& i: subtitles) {
413 if (!pending.empty() && (i.v_align() != pending.back().v_align() || fabs(i.v_position() - pending.back().v_position()) > 1e-4)) {
414 images.push_back (render_line (pending, fonts, target, time, frame_rate));
417 pending.push_back (i);
420 if (!pending.empty()) {
421 images.push_back (render_line (pending, fonts, target, time, frame_rate));