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;
40 static FcConfig* fc_config = 0;
41 static list<pair<boost::filesystem::path, string> > fc_config_fonts;
44 render_subtitle (dcp::SubtitleString const & subtitle, list<shared_ptr<Font> > fonts, dcp::Size target)
46 /* Calculate x and y scale factors. These are only used to stretch
47 the font away from its normal aspect ratio.
51 if (fabs (subtitle.aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) {
52 if (subtitle.aspect_adjust() < 1) {
53 xscale = max (0.25f, subtitle.aspect_adjust ());
57 yscale = 1 / min (4.0f, subtitle.aspect_adjust ());
61 /* Make an empty bitmap as wide as target and at
62 least tall enough for this subtitle.
65 /* Basic guess on height... */
66 int height = subtitle.size() * target.height / (11 * 72);
69 /* ...and add a bit more for luck */
70 height += target.height / 11;
72 shared_ptr<Image> image (new Image (PIX_FMT_RGBA, dcp::Size (target.width, height), false));
75 Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
80 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
83 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface);
86 fc_config = FcConfigCreate ();
89 boost::filesystem::path font_file = shared_path () / "LiberationSans-Regular.ttf";
90 BOOST_FOREACH (shared_ptr<Font> i, fonts) {
91 if (i->id() == subtitle.font() && i->file ()) {
92 font_file = i->file().get ();
96 list<pair<boost::filesystem::path, string> >::const_iterator existing = fc_config_fonts.begin ();
97 while (existing != fc_config_fonts.end() && existing->first != font_file) {
102 if (existing != fc_config_fonts.end ()) {
103 font_name = existing->second;
105 /* Make this font available to DCP-o-matic */
106 FcConfigAppFontAddFile (fc_config, reinterpret_cast<FcChar8 const *> (font_file.string().c_str ()));
108 FcPattern* pattern = FcPatternBuild (0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *> (0));
109 FcObjectSet* object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
110 FcFontSet* font_set = FcFontList (fc_config, pattern, object_set);
112 for (int i = 0; i < font_set->nfont; ++i) {
113 FcPattern* font = font_set->fonts[i];
118 FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
119 FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
120 FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
122 font_name = reinterpret_cast<char const *> (family);
126 FcFontSetDestroy (font_set);
129 FcObjectSetDestroy (object_set);
130 FcPatternDestroy (pattern);
132 fc_config_fonts.push_back (make_pair (font_file, font_name));
135 FcConfigSetCurrent (fc_config);
137 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
139 layout->set_alignment (Pango::ALIGN_LEFT);
141 context->set_line_width (1);
143 /* Render the subtitle at the top left-hand corner of image */
145 Pango::FontDescription font (font_name);
146 font.set_absolute_size (subtitle.size_in_pixels (target.height) * PANGO_SCALE);
147 if (subtitle.italic ()) {
148 font.set_style (Pango::STYLE_ITALIC);
150 layout->set_font_description (font);
151 layout->set_text (subtitle.text ());
153 /* Compute fade factor */
155 float fade_factor = 1;
157 layout->update_from_cairo_context (context);
159 context->scale (xscale, yscale);
161 if (subtitle.effect() == dcp::SHADOW) {
162 /* Drop-shadow effect */
163 dcp::Colour const ec = subtitle.effect_colour ();
164 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
165 context->move_to (4, 4);
166 layout->add_to_cairo_context (context);
170 /* The actual subtitle */
172 dcp::Colour const c = subtitle.colour ();
173 context->set_source_rgba (float(c.r) / 255, float(c.g) / 255, float(c.b) / 255, fade_factor);
174 context->move_to (0, 0);
175 layout->add_to_cairo_context (context);
178 if (subtitle.effect() == dcp::BORDER) {
180 dcp::Colour ec = subtitle.effect_colour ();
181 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
182 context->move_to (0, 0);
183 layout->add_to_cairo_context (context);
189 layout->get_size (layout_width, layout_height);
192 switch (subtitle.h_align ()) {
193 case dcp::HALIGN_LEFT:
194 /* h_position is distance between left of frame and left of subtitle */
195 x = subtitle.h_position() * target.width;
197 case dcp::HALIGN_CENTER:
198 /* h_position is distance between centre of frame and centre of subtitle */
199 x = (0.5 + subtitle.h_position()) * target.width - layout_width / (PANGO_SCALE * 2);
201 case dcp::HALIGN_RIGHT:
202 /* h_position is distance between right of frame and right of subtitle */
203 x = (1.0 - subtitle.h_position()) * target.width - layout_width / PANGO_SCALE;
208 switch (subtitle.v_align ()) {
209 case dcp::VALIGN_TOP:
210 /* v_position is distance between top of frame and top of subtitle */
211 y = subtitle.v_position() * target.height;
213 case dcp::VALIGN_CENTER:
214 /* v_position is distance between centre of frame and centre of subtitle */
215 y = (0.5 + subtitle.v_position()) * target.height - layout_height / (PANGO_SCALE * 2);
217 case dcp::VALIGN_BOTTOM:
218 /* v_position is distance between bottom of frame and bottom of subtitle */
219 y = (1.0 - subtitle.v_position()) * target.height - layout_height / PANGO_SCALE;
223 return PositionImage (image, Position<int> (x, y));
227 render_subtitles (list<dcp::SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target)
229 list<PositionImage> images;
230 BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) {
231 images.push_back (render_subtitle (i, fonts, target));