2 Copyright (C) 2014-2016 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 "render_subtitles.h"
26 #include "dcpomatic_assert.h"
27 #include <fontconfig/fontconfig.h>
28 #include <cairomm/cairomm.h>
30 #include <boost/foreach.hpp>
41 using boost::shared_ptr;
42 using boost::optional;
44 static FcConfig* fc_config = 0;
45 static list<pair<FontFiles, string> > fc_config_fonts;
48 marked_up (list<SubtitleString> subtitles)
53 bool underline = false;
54 BOOST_FOREACH (SubtitleString const & i, subtitles) {
56 if (i.italic() && !italic) {
59 if (i.bold() && !bold) {
62 if (i.underline() && !underline) {
65 if (!i.underline() && underline) {
68 if (!i.bold() && bold) {
71 if (!i.italic() && italic) {
77 underline = i.underline ();
95 /** @param subtitles A list of subtitles that are all on the same line,
96 * at the same time and with the same fade in/out.
99 render_line (list<SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target, DCPTime time)
101 /* XXX: this method can only handle italic / bold changes mid-line,
105 DCPOMATIC_ASSERT (!subtitles.empty ());
107 /* Calculate x and y scale factors. These are only used to stretch
108 the font away from its normal aspect ratio.
112 if (fabs (subtitles.front().aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) {
113 if (subtitles.front().aspect_adjust() < 1) {
114 xscale = max (0.25f, subtitles.front().aspect_adjust ());
118 yscale = 1 / min (4.0f, subtitles.front().aspect_adjust ());
122 /* Make an empty bitmap as wide as target and at
123 least tall enough for this subtitle.
126 /* Basic guess on height... */
127 int height = subtitles.front().size() * target.height / (11 * 72);
130 /* ...and add a bit more for luck */
131 height += target.height / 11;
133 shared_ptr<Image> image (new Image (AV_PIX_FMT_RGBA, dcp::Size (target.width, height), false));
134 image->make_black ();
136 #ifdef DCPOMATIC_HAVE_FORMAT_STRIDE_FOR_WIDTH
137 Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
139 Cairo::FORMAT_ARGB32,
141 image->size().height,
142 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
145 /* Centos 5 does not have Cairo::ImageSurface::format_stride_for_width, so just use width * 4
146 which I hope is safe (if slow)
148 Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
150 Cairo::FORMAT_ARGB32,
152 image->size().height,
153 image->size().width * 4
157 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface);
160 fc_config = FcConfigCreate ();
163 FontFiles font_files;
166 font_files.set (FontFiles::NORMAL, shared_path () / "LiberationSans-Regular.ttf");
167 font_files.set (FontFiles::ITALIC, shared_path () / "LiberationSans-Italic.ttf");
168 font_files.set (FontFiles::BOLD, shared_path () / "LiberationSans-Bold.ttf");
169 } catch (boost::filesystem::filesystem_error& e) {
173 /* Hack: try the debian/ubuntu locations if getting the shared path failed */
175 if (!font_files.get(FontFiles::NORMAL) || !boost::filesystem::exists(font_files.get(FontFiles::NORMAL).get())) {
176 font_files.set (FontFiles::NORMAL, "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf");
178 if (!font_files.get(FontFiles::ITALIC) || !boost::filesystem::exists(font_files.get(FontFiles::ITALIC).get())) {
179 font_files.set (FontFiles::ITALIC, "/usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf");
181 if (!font_files.get(FontFiles::BOLD) || !boost::filesystem::exists(font_files.get(FontFiles::BOLD).get())) {
182 font_files.set (FontFiles::BOLD, "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf");
185 BOOST_FOREACH (shared_ptr<Font> i, fonts) {
186 if (i->id() == subtitles.front().font() && i->file(FontFiles::NORMAL)) {
187 font_files = i->files ();
191 list<pair<FontFiles, string> >::const_iterator existing = fc_config_fonts.begin ();
192 while (existing != fc_config_fonts.end() && existing->first != font_files) {
197 if (existing != fc_config_fonts.end ()) {
198 font_name = existing->second;
200 /* Make this font available to DCP-o-matic */
201 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
202 if (font_files.get(static_cast<FontFiles::Variant>(i))) {
203 FcConfigAppFontAddFile (
205 reinterpret_cast<FcChar8 const *> (font_files.get(static_cast<FontFiles::Variant>(i)).get().string().c_str())
210 FcPattern* pattern = FcPatternBuild (
211 0, FC_FILE, FcTypeString, font_files.get(FontFiles::NORMAL).get().string().c_str(), static_cast<char *> (0)
213 FcObjectSet* object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
214 FcFontSet* font_set = FcFontList (fc_config, pattern, object_set);
216 for (int i = 0; i < font_set->nfont; ++i) {
217 FcPattern* font = font_set->fonts[i];
222 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
223 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
224 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
226 font_name = reinterpret_cast<char const *> (family);
230 FcFontSetDestroy (font_set);
233 FcObjectSetDestroy (object_set);
234 FcPatternDestroy (pattern);
236 fc_config_fonts.push_back (make_pair (font_files, font_name));
239 FcConfigSetCurrent (fc_config);
241 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
243 layout->set_alignment (Pango::ALIGN_LEFT);
245 context->set_line_width (1);
247 /* Render the subtitle at the top left-hand corner of image */
249 Pango::FontDescription font (font_name);
250 font.set_absolute_size (subtitles.front().size_in_pixels (target.height) * PANGO_SCALE);
251 layout->set_font_description (font);
252 layout->set_markup (marked_up (subtitles));
254 /* Compute fade factor */
255 float fade_factor = 1;
257 DCPTime const fade_in_start = DCPTime::from_seconds (subtitles.front().in().as_seconds ());
258 DCPTime const fade_in_end = fade_in_start + DCPTime::from_seconds (subtitles.front().fade_up_time().as_seconds ());
259 DCPTime const fade_out_end = DCPTime::from_seconds (subtitles.front().out().as_seconds ());
260 DCPTime const fade_out_start = fade_out_end - DCPTime::from_seconds (subtitles.front().fade_down_time().as_seconds ());
261 if (fade_in_start <= time && time <= fade_in_end && fade_in_start != fade_in_end) {
262 fade_factor = DCPTime(time - fade_in_start).seconds() / DCPTime(fade_in_end - fade_in_start).seconds();
263 } else if (fade_out_start <= time && time <= fade_out_end && fade_out_start != fade_out_end) {
264 fade_factor = 1 - DCPTime(time - fade_out_start).seconds() / DCPTime(fade_out_end - fade_out_start).seconds();
267 context->scale (xscale, yscale);
268 layout->update_from_cairo_context (context);
270 /* Shuffle the subtitle over very slightly if it has a border so that the left-hand
271 side of the first character's border is not cut off.
273 int const x_offset = subtitles.front().effect() == dcp::BORDER ? (target.width / 600.0) : 0;
275 if (subtitles.front().effect() == dcp::SHADOW) {
276 /* Drop-shadow effect */
277 dcp::Colour const ec = subtitles.front().effect_colour ();
278 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
279 context->move_to (x_offset + 4, 4);
280 layout->add_to_cairo_context (context);
284 if (subtitles.front().effect() == dcp::BORDER) {
285 /* Border effect; stroke the subtitle with a large (arbitrarily chosen) line width */
286 dcp::Colour ec = subtitles.front().effect_colour ();
287 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
288 context->set_line_width (subtitles.front().outline_width * target.width / 2048.0);
289 context->set_line_join (Cairo::LINE_JOIN_ROUND);
290 context->move_to (x_offset, 0);
291 layout->add_to_cairo_context (context);
295 /* The actual subtitle */
297 dcp::Colour const c = subtitles.front().colour ();
298 context->set_source_rgba (float(c.r) / 255, float(c.g) / 255, float(c.b) / 255, fade_factor);
299 context->set_line_width (0);
300 context->move_to (x_offset, 0);
301 layout->add_to_cairo_context (context);
306 layout->get_pixel_size (layout_width, layout_height);
307 layout_width *= xscale;
308 layout_height *= yscale;
311 switch (subtitles.front().h_align ()) {
312 case dcp::HALIGN_LEFT:
313 /* h_position is distance between left of frame and left of subtitle */
314 x = subtitles.front().h_position() * target.width;
316 case dcp::HALIGN_CENTER:
317 /* h_position is distance between centre of frame and centre of subtitle */
318 x = (0.5 + subtitles.front().h_position()) * target.width - layout_width / 2;
320 case dcp::HALIGN_RIGHT:
321 /* h_position is distance between right of frame and right of subtitle */
322 x = (1.0 - subtitles.front().h_position()) * target.width - layout_width;
327 switch (subtitles.front().v_align ()) {
328 case dcp::VALIGN_TOP:
329 /* SMPTE says that v_position is the distance between top
330 of frame and top of subtitle, but this doesn't always seem to be
331 the case in practice; Gunnar Ásgeirsson's Dolby server appears
332 to put VALIGN_TOP subs with v_position as the distance between top
333 of frame and bottom of subtitle.
335 y = subtitles.front().v_position() * target.height - layout_height;
337 case dcp::VALIGN_CENTER:
338 /* v_position is distance between centre of frame and centre of subtitle */
339 y = (0.5 + subtitles.front().v_position()) * target.height - layout_height / 2;
341 case dcp::VALIGN_BOTTOM:
342 /* v_position is distance between bottom of frame and bottom of subtitle */
343 y = (1.0 - subtitles.front().v_position()) * target.height - layout_height;
347 return PositionImage (image, Position<int> (max (0, x), max (0, y)));
350 /** @param time Time of the frame that these subtitles are going on */
352 render_subtitles (list<SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target, DCPTime time)
354 list<SubtitleString> pending;
355 list<PositionImage> images;
357 BOOST_FOREACH (SubtitleString const & i, subtitles) {
358 if (!pending.empty() && fabs (i.v_position() - pending.back().v_position()) > 1e-4) {
359 images.push_back (render_line (pending, fonts, target, time));
362 pending.push_back (i);
365 if (!pending.empty ()) {
366 images.push_back (render_line (pending, fonts, target, time));