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"
23 #include <cairomm/cairomm.h>
25 #include <boost/foreach.hpp>
33 using boost::shared_ptr;
34 using boost::optional;
37 render_subtitle (dcp::SubtitleString const & subtitle, dcp::Size target)
39 /* Calculate x and y scale factors. These are only used to stretch
40 the font away from its normal aspect ratio.
44 if (fabs (subtitle.aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) {
45 if (subtitle.aspect_adjust() < 1) {
46 xscale = max (0.25f, subtitle.aspect_adjust ());
50 yscale = 1 / min (4.0f, subtitle.aspect_adjust ());
54 /* Make an empty bitmap as wide as target and at
55 least tall enough for this subtitle.
58 /* Basic guess on height... */
59 int height = subtitle.size() * target.height / (11 * 72);
62 /* ...and add a bit more for luck */
63 height += target.height / 11;
65 shared_ptr<Image> image (new Image (PIX_FMT_RGBA, dcp::Size (target.width, height), false));
68 Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
73 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
76 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface);
77 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
79 layout->set_alignment (Pango::ALIGN_LEFT);
81 context->set_line_width (1);
83 /* Render the subtitle at the top left-hand corner of image */
85 Pango::FontDescription font (subtitle.font().get_value_or ("Arial"));
86 font.set_absolute_size (subtitle.size_in_pixels (target.height) * PANGO_SCALE);
87 if (subtitle.italic ()) {
88 font.set_style (Pango::STYLE_ITALIC);
90 layout->set_font_description (font);
91 layout->set_text (subtitle.text ());
93 /* Compute fade factor */
95 float fade_factor = 1;
97 layout->update_from_cairo_context (context);
99 context->scale (xscale, yscale);
101 if (subtitle.effect() == dcp::SHADOW) {
102 /* Drop-shadow effect */
103 dcp::Colour const ec = subtitle.effect_colour ();
104 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
105 context->move_to (4, 4);
106 layout->add_to_cairo_context (context);
110 /* The actual subtitle */
112 dcp::Colour const c = subtitle.colour ();
113 context->set_source_rgba (float(c.r) / 255, float(c.g) / 255, float(c.b) / 255, fade_factor);
114 context->move_to (0, 0);
115 layout->add_to_cairo_context (context);
118 if (subtitle.effect() == dcp::BORDER) {
120 dcp::Colour ec = subtitle.effect_colour ();
121 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
122 context->move_to (0, 0);
123 layout->add_to_cairo_context (context);
129 layout->get_size (layout_width, layout_height);
132 switch (subtitle.h_align ()) {
133 case dcp::HALIGN_LEFT:
134 /* h_position is distance between left of frame and left of subtitle */
135 x = subtitle.h_position() * target.width;
137 case dcp::HALIGN_CENTER:
138 /* h_position is distance between centre of frame and centre of subtitle */
139 x = (0.5 + subtitle.h_position()) * target.width - layout_width / (PANGO_SCALE * 2);
141 case dcp::HALIGN_RIGHT:
142 /* h_position is distance between right of frame and right of subtitle */
143 x = (1.0 - subtitle.h_position()) * target.width - layout_width / PANGO_SCALE;
148 switch (subtitle.v_align ()) {
149 case dcp::VALIGN_TOP:
150 /* v_position is distance between top of frame and top of subtitle */
151 y = subtitle.v_position() * target.height;
153 case dcp::VALIGN_CENTER:
154 /* v_position is distance between centre of frame and centre of subtitle */
155 y = (0.5 + subtitle.v_position()) * target.height - layout_height / (PANGO_SCALE * 2);
157 case dcp::VALIGN_BOTTOM:
158 /* v_position is distance between bottom of frame and bottom of subtitle */
159 y = (1.0 - subtitle.v_position()) * target.height - layout_height / PANGO_SCALE;
163 return PositionImage (image, Position<int> (x, y));
167 render_subtitles (list<dcp::SubtitleString> subtitles, dcp::Size target)
169 list<PositionImage> images;
170 BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) {
171 images.push_back (render_subtitle (i, target));