2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "render_subtitles.h"
25 #include <cairomm/cairomm.h>
27 #include <boost/foreach.hpp>
38 using boost::shared_ptr;
39 using boost::optional;
41 static FcConfig* fc_config = 0;
42 static list<pair<boost::filesystem::path, string> > fc_config_fonts;
45 render_subtitle (dcp::SubtitleString const & subtitle, list<shared_ptr<Font> > fonts, dcp::Size target)
47 /* Calculate x and y scale factors. These are only used to stretch
48 the font away from its normal aspect ratio.
52 if (fabs (subtitle.aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) {
53 if (subtitle.aspect_adjust() < 1) {
54 xscale = max (0.25f, subtitle.aspect_adjust ());
58 yscale = 1 / min (4.0f, subtitle.aspect_adjust ());
62 /* Make an empty bitmap as wide as target and at
63 least tall enough for this subtitle.
66 /* Basic guess on height... */
67 int height = subtitle.size() * target.height / (11 * 72);
70 /* ...and add a bit more for luck */
71 height += target.height / 11;
73 shared_ptr<Image> image (new Image (AV_PIX_FMT_RGBA, dcp::Size (target.width, height), false));
76 Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
81 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
84 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface);
87 fc_config = FcConfigCreate ();
90 boost::filesystem::path font_file;
92 font_file = shared_path () / "LiberationSans-Regular.ttf";
93 } catch (boost::filesystem::filesystem_error& e) {
94 /* Hack: try the debian/ubuntu location if getting the shared path failed */
95 font_file = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf";
98 BOOST_FOREACH (shared_ptr<Font> i, fonts) {
99 if (i->id() == subtitle.font() && i->file ()) {
100 font_file = i->file().get ();
104 list<pair<boost::filesystem::path, string> >::const_iterator existing = fc_config_fonts.begin ();
105 while (existing != fc_config_fonts.end() && existing->first != font_file) {
110 if (existing != fc_config_fonts.end ()) {
111 font_name = existing->second;
113 /* Make this font available to DCP-o-matic */
114 FcConfigAppFontAddFile (fc_config, reinterpret_cast<FcChar8 const *> (font_file.string().c_str ()));
116 FcPattern* pattern = FcPatternBuild (0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *> (0));
117 FcObjectSet* object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
118 FcFontSet* font_set = FcFontList (fc_config, pattern, object_set);
120 for (int i = 0; i < font_set->nfont; ++i) {
121 FcPattern* font = font_set->fonts[i];
126 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
127 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
128 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
130 font_name = reinterpret_cast<char const *> (family);
134 FcFontSetDestroy (font_set);
137 FcObjectSetDestroy (object_set);
138 FcPatternDestroy (pattern);
140 fc_config_fonts.push_back (make_pair (font_file, font_name));
143 FcConfigSetCurrent (fc_config);
145 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
147 layout->set_alignment (Pango::ALIGN_LEFT);
149 context->set_line_width (1);
151 /* Render the subtitle at the top left-hand corner of image */
153 Pango::FontDescription font (font_name);
154 font.set_absolute_size (subtitle.size_in_pixels (target.height) * PANGO_SCALE);
155 if (subtitle.italic ()) {
156 font.set_style (Pango::STYLE_ITALIC);
158 layout->set_font_description (font);
159 layout->set_text (subtitle.text ());
161 /* Compute fade factor */
163 float fade_factor = 1;
165 layout->update_from_cairo_context (context);
167 context->scale (xscale, yscale);
169 if (subtitle.effect() == dcp::SHADOW) {
170 /* Drop-shadow effect */
171 dcp::Colour const ec = subtitle.effect_colour ();
172 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
173 context->move_to (4, 4);
174 layout->add_to_cairo_context (context);
178 /* The actual subtitle */
180 dcp::Colour const c = subtitle.colour ();
181 context->set_source_rgba (float(c.r) / 255, float(c.g) / 255, float(c.b) / 255, fade_factor);
182 context->move_to (0, 0);
183 layout->add_to_cairo_context (context);
186 if (subtitle.effect() == dcp::BORDER) {
188 dcp::Colour ec = subtitle.effect_colour ();
189 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
190 context->move_to (0, 0);
191 layout->add_to_cairo_context (context);
197 layout->get_size (layout_width, layout_height);
200 switch (subtitle.h_align ()) {
201 case dcp::HALIGN_LEFT:
202 /* h_position is distance between left of frame and left of subtitle */
203 x = subtitle.h_position() * target.width;
205 case dcp::HALIGN_CENTER:
206 /* h_position is distance between centre of frame and centre of subtitle */
207 x = (0.5 + subtitle.h_position()) * target.width - layout_width / (PANGO_SCALE * 2);
209 case dcp::HALIGN_RIGHT:
210 /* h_position is distance between right of frame and right of subtitle */
211 x = (1.0 - subtitle.h_position()) * target.width - layout_width / PANGO_SCALE;
216 switch (subtitle.v_align ()) {
217 case dcp::VALIGN_TOP:
218 /* v_position is distance between top of frame and top of subtitle */
219 y = subtitle.v_position() * target.height;
221 case dcp::VALIGN_CENTER:
222 /* v_position is distance between centre of frame and centre of subtitle */
223 y = (0.5 + subtitle.v_position()) * target.height - layout_height / (PANGO_SCALE * 2);
225 case dcp::VALIGN_BOTTOM:
226 /* v_position is distance between bottom of frame and bottom of subtitle */
227 y = (1.0 - subtitle.v_position()) * target.height - layout_height / PANGO_SCALE;
231 return PositionImage (image, Position<int> (x, y));
235 render_subtitles (list<dcp::SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target)
237 list<PositionImage> images;
238 BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) {
239 images.push_back (render_subtitle (i, fonts, target));