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>
37 using boost::shared_ptr;
38 using boost::optional;
42 static FcConfig* fc_config = 0;
43 static list<pair<boost::filesystem::path, string> > fc_config_fonts;
46 render_subtitle (dcp::SubtitleString const & subtitle, list<shared_ptr<Font> > fonts, dcp::Size target)
48 /* Calculate x and y scale factors. These are only used to stretch
49 the font away from its normal aspect ratio.
53 if (fabs (subtitle.aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) {
54 if (subtitle.aspect_adjust() < 1) {
55 xscale = max (0.25f, subtitle.aspect_adjust ());
59 yscale = 1 / min (4.0f, subtitle.aspect_adjust ());
63 /* Make an empty bitmap as wide as target and at
64 least tall enough for this subtitle.
67 /* Basic guess on height... */
68 int height = subtitle.size() * target.height / (11 * 72);
71 /* ...and add a bit more for luck */
72 height += target.height / 11;
74 shared_ptr<Image> image (new Image (PIX_FMT_RGBA, dcp::Size (target.width, height), false));
77 Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
82 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
85 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface);
88 fc_config = FcConfigCreate ();
91 boost::filesystem::path font_file = shared_path () / "LiberationSans-Regular.ttf";
92 BOOST_FOREACH (shared_ptr<Font> i, fonts) {
93 if (i->id() == subtitle.font() && i->file ()) {
94 font_file = i->file().get ();
98 list<pair<boost::filesystem::path, string> >::const_iterator existing = fc_config_fonts.begin ();
99 while (existing != fc_config_fonts.end() && existing->first != font_file) {
104 if (existing != fc_config_fonts.end ()) {
105 font_name = existing->second;
107 /* Make this font available to DCP-o-matic */
108 if (FcConfigAppFontAddFile (fc_config, reinterpret_cast<FcChar8 const *> (font_file.string().c_str ())) == FcFalse) {
109 #if defined(DEBUG_FONTS)
110 cerr << "FcConfigAppFontAddFile failed to load " << font_file << "\n";
114 FcPattern* pattern = FcPatternBuild (0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *> (0));
115 FcObjectSet* object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
116 FcFontSet* font_set = FcFontList (fc_config, pattern, object_set);
118 for (int i = 0; i < font_set->nfont; ++i) {
119 FcPattern* font = font_set->fonts[i];
124 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
125 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
126 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
128 font_name = reinterpret_cast<char const *> (family);
132 FcFontSetDestroy (font_set);
135 FcObjectSetDestroy (object_set);
136 FcPatternDestroy (pattern);
138 fc_config_fonts.push_back (make_pair (font_file, font_name));
141 FcConfigSetCurrent (fc_config);
143 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
145 layout->set_alignment (Pango::ALIGN_LEFT);
147 context->set_line_width (1);
149 /* Render the subtitle at the top left-hand corner of image */
151 #if defined(DEBUG_FONTS)
152 cerr << "Font name is " << font_name << "\n";
154 Pango::FontDescription font (font_name);
155 font.set_absolute_size (subtitle.size_in_pixels (target.height) * PANGO_SCALE);
156 if (subtitle.italic ()) {
157 font.set_style (Pango::STYLE_ITALIC);
159 layout->set_font_description (font);
160 layout->set_text (subtitle.text ());
162 /* Compute fade factor */
164 float fade_factor = 1;
166 layout->update_from_cairo_context (context);
168 context->scale (xscale, yscale);
170 if (subtitle.effect() == dcp::SHADOW) {
171 /* Drop-shadow effect */
172 dcp::Colour const ec = subtitle.effect_colour ();
173 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
174 context->move_to (4, 4);
175 layout->add_to_cairo_context (context);
179 /* The actual subtitle */
181 dcp::Colour const c = subtitle.colour ();
182 context->set_source_rgba (float(c.r) / 255, float(c.g) / 255, float(c.b) / 255, fade_factor);
183 context->move_to (0, 0);
184 layout->add_to_cairo_context (context);
187 if (subtitle.effect() == dcp::BORDER) {
189 dcp::Colour ec = subtitle.effect_colour ();
190 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
191 context->move_to (0, 0);
192 layout->add_to_cairo_context (context);
198 layout->get_size (layout_width, layout_height);
201 switch (subtitle.h_align ()) {
202 case dcp::HALIGN_LEFT:
203 /* h_position is distance between left of frame and left of subtitle */
204 x = subtitle.h_position() * target.width;
206 case dcp::HALIGN_CENTER:
207 /* h_position is distance between centre of frame and centre of subtitle */
208 x = (0.5 + subtitle.h_position()) * target.width - layout_width / (PANGO_SCALE * 2);
210 case dcp::HALIGN_RIGHT:
211 /* h_position is distance between right of frame and right of subtitle */
212 x = (1.0 - subtitle.h_position()) * target.width - layout_width / PANGO_SCALE;
217 switch (subtitle.v_align ()) {
218 case dcp::VALIGN_TOP:
219 /* v_position is distance between top of frame and top of subtitle */
220 y = subtitle.v_position() * target.height;
222 case dcp::VALIGN_CENTER:
223 /* v_position is distance between centre of frame and centre of subtitle */
224 y = (0.5 + subtitle.v_position()) * target.height - layout_height / (PANGO_SCALE * 2);
226 case dcp::VALIGN_BOTTOM:
227 /* v_position is distance between bottom of frame and bottom of subtitle */
228 y = (1.0 - subtitle.v_position()) * target.height - layout_height / PANGO_SCALE;
232 return PositionImage (image, Position<int> (x, y));
236 render_subtitles (list<dcp::SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target)
238 list<PositionImage> images;
239 BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) {
240 images.push_back (render_subtitle (i, fonts, target));