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;
47 /** @param subtitles A list of subtitles that are all on the same line */
49 render_line (list<dcp::SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target)
51 /* XXX: this method can only handle italic / bold changes mid-line,
55 DCPOMATIC_ASSERT (!subtitles.empty ());
57 /* Calculate x and y scale factors. These are only used to stretch
58 the font away from its normal aspect ratio.
62 if (fabs (subtitles.front().aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) {
63 if (subtitles.front().aspect_adjust() < 1) {
64 xscale = max (0.25f, subtitles.front().aspect_adjust ());
68 yscale = 1 / min (4.0f, subtitles.front().aspect_adjust ());
72 /* Make an empty bitmap as wide as target and at
73 least tall enough for this subtitle.
76 /* Basic guess on height... */
77 int height = subtitles.front().size() * target.height / (11 * 72);
80 /* ...and add a bit more for luck */
81 height += target.height / 11;
83 shared_ptr<Image> image (new Image (AV_PIX_FMT_RGBA, dcp::Size (target.width, height), false));
86 #ifdef DCPOMATIC_HAVE_FORMAT_STRIDE_FOR_WIDTH
87 Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
92 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
95 /* Centos 5 does not have Cairo::ImageSurface::format_stride_for_width, so just use width * 4
96 which I hope is safe (if slow)
98 Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
100 Cairo::FORMAT_ARGB32,
102 image->size().height,
103 image->size().width * 4
107 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface);
110 fc_config = FcConfigCreate ();
113 FontFiles font_files;
116 font_files.set (FontFiles::NORMAL, shared_path () / "LiberationSans-Regular.ttf");
117 font_files.set (FontFiles::ITALIC, shared_path () / "LiberationSans-Italic.ttf");
118 font_files.set (FontFiles::BOLD, shared_path () / "LiberationSans-Bold.ttf");
119 } catch (boost::filesystem::filesystem_error& e) {
123 /* Hack: try the debian/ubuntu locations if getting the shared path failed */
125 if (!font_files.get(FontFiles::NORMAL) || !boost::filesystem::exists(font_files.get(FontFiles::NORMAL).get())) {
126 font_files.set (FontFiles::NORMAL, "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf");
128 if (!font_files.get(FontFiles::ITALIC) || !boost::filesystem::exists(font_files.get(FontFiles::ITALIC).get())) {
129 font_files.set (FontFiles::ITALIC, "/usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf");
131 if (!font_files.get(FontFiles::BOLD) || !boost::filesystem::exists(font_files.get(FontFiles::BOLD).get())) {
132 font_files.set (FontFiles::BOLD, "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf");
135 BOOST_FOREACH (shared_ptr<Font> i, fonts) {
136 if (i->id() == subtitles.front().font() && i->file(FontFiles::NORMAL)) {
137 font_files = i->files ();
141 list<pair<FontFiles, string> >::const_iterator existing = fc_config_fonts.begin ();
142 while (existing != fc_config_fonts.end() && existing->first != font_files) {
147 if (existing != fc_config_fonts.end ()) {
148 font_name = existing->second;
150 /* Make this font available to DCP-o-matic */
151 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
152 if (font_files.get(static_cast<FontFiles::Variant>(i))) {
153 FcConfigAppFontAddFile (
155 reinterpret_cast<FcChar8 const *> (font_files.get(static_cast<FontFiles::Variant>(i)).get().string().c_str())
160 FcPattern* pattern = FcPatternBuild (0, FC_FILE, FcTypeString, font_files.get(FontFiles::NORMAL).get().string().c_str(), static_cast<char *> (0));
161 FcObjectSet* object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
162 FcFontSet* font_set = FcFontList (fc_config, pattern, object_set);
164 for (int i = 0; i < font_set->nfont; ++i) {
165 FcPattern* font = font_set->fonts[i];
170 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
171 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
172 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
174 font_name = reinterpret_cast<char const *> (family);
178 FcFontSetDestroy (font_set);
181 FcObjectSetDestroy (object_set);
182 FcPatternDestroy (pattern);
184 fc_config_fonts.push_back (make_pair (font_files, font_name));
187 FcConfigSetCurrent (fc_config);
189 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
191 layout->set_alignment (Pango::ALIGN_LEFT);
193 context->set_line_width (1);
195 /* Render the subtitle at the top left-hand corner of image */
197 Pango::FontDescription font (font_name);
198 font.set_absolute_size (subtitles.front().size_in_pixels (target.height) * PANGO_SCALE);
199 layout->set_font_description (font);
204 BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) {
205 if (i.italic() != italic) {
211 italic = i.italic ();
214 if (i.bold() != bold) {
223 marked_up += i.text ();
234 layout->set_markup (marked_up);
236 /* Compute fade factor */
238 float fade_factor = 1;
240 context->scale (xscale, yscale);
241 layout->update_from_cairo_context (context);
243 if (subtitles.front().effect() == dcp::SHADOW) {
244 /* Drop-shadow effect */
245 dcp::Colour const ec = subtitles.front().effect_colour ();
246 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
247 context->move_to (4, 4);
248 layout->add_to_cairo_context (context);
252 /* The actual subtitle */
254 dcp::Colour const c = subtitles.front().colour ();
255 context->set_source_rgba (float(c.r) / 255, float(c.g) / 255, float(c.b) / 255, fade_factor);
256 context->move_to (0, 0);
257 layout->add_to_cairo_context (context);
260 if (subtitles.front().effect() == dcp::BORDER) {
262 dcp::Colour ec = subtitles.front().effect_colour ();
263 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
264 context->move_to (0, 0);
265 layout->add_to_cairo_context (context);
271 layout->get_pixel_size (layout_width, layout_height);
272 layout_width *= xscale;
273 layout_height *= yscale;
276 switch (subtitles.front().h_align ()) {
277 case dcp::HALIGN_LEFT:
278 /* h_position is distance between left of frame and left of subtitle */
279 x = subtitles.front().h_position() * target.width;
281 case dcp::HALIGN_CENTER:
282 /* h_position is distance between centre of frame and centre of subtitle */
283 x = (0.5 + subtitles.front().h_position()) * target.width - layout_width / 2;
285 case dcp::HALIGN_RIGHT:
286 /* h_position is distance between right of frame and right of subtitle */
287 x = (1.0 - subtitles.front().h_position()) * target.width - layout_width;
292 switch (subtitles.front().v_align ()) {
293 case dcp::VALIGN_TOP:
294 /* SMPTE says that v_position is the distance between top
295 of frame and top of subtitle, but this doesn't always seem to be
296 the case in practice; Gunnar Ásgeirsson's Dolby server appears
297 to put VALIGN_TOP subs with v_position as the distance between top
298 of frame and bottom of subtitle.
300 y = subtitles.front().v_position() * target.height - layout_height;
302 case dcp::VALIGN_CENTER:
303 /* v_position is distance between centre of frame and centre of subtitle */
304 y = (0.5 + subtitles.front().v_position()) * target.height - layout_height / 2;
306 case dcp::VALIGN_BOTTOM:
307 /* v_position is distance between bottom of frame and bottom of subtitle */
308 y = (1.0 - subtitles.front().v_position()) * target.height - layout_height;
312 return PositionImage (image, Position<int> (max (0, x), max (0, y)));
316 render_subtitles (list<dcp::SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target)
318 list<dcp::SubtitleString> pending;
319 list<PositionImage> images;
321 BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) {
322 if (!pending.empty() && fabs (i.v_position() - pending.back().v_position()) > 1e-4) {
323 images.push_back (render_line (pending, fonts, target));
326 pending.push_back (i);
329 if (!pending.empty ()) {
330 images.push_back (render_line (pending, fonts, target));