2 Copyright (C) 2014 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 <cairomm/cairomm.h>
22 #include "render_subtitles.h"
32 using boost::shared_ptr;
33 using boost::optional;
36 calculate_position (dcp::VAlign v_align, double v_position, int target_height, int offset)
40 return v_position * target_height - offset;
42 return (0.5 + v_position) * target_height - offset;
44 return (1.0 - v_position) * target_height - offset;
51 render_subtitles (list<dcp::SubtitleString> subtitles, dcp::Size target)
53 /* Estimate height that the subtitle image needs to be */
56 for (list<dcp::SubtitleString>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
57 int const b = calculate_position (i->v_align(), i->v_position(), target.height, 0);
58 int const t = b - i->size() * target.height / (11 * 72);
60 top = min (top.get_value_or (t), t);
61 bottom = max (bottom.get_value_or (b), b);
65 bottom = bottom.get() + 32;
67 shared_ptr<Image> image (new Image (PIX_FMT_RGBA, dcp::Size (target.width, bottom.get() - top.get ()), false));
70 Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
75 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
78 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface);
79 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
81 layout->set_width (image->size().width * PANGO_SCALE);
82 layout->set_alignment (Pango::ALIGN_CENTER);
84 context->set_line_width (1);
86 for (list<dcp::SubtitleString>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
87 Pango::FontDescription font (i->font().get_value_or ("Arial"));
88 font.set_absolute_size (i->size_in_pixels (target.height) * PANGO_SCALE);
90 font.set_style (Pango::STYLE_ITALIC);
92 layout->set_font_description (font);
93 layout->set_text (i->text ());
95 /* Compute fade factor */
97 float fade_factor = 1;
99 layout->update_from_cairo_context (context);
101 /* Work out position */
104 int const y = calculate_position (i->v_align (), i->v_position (), target.height, (layout->get_baseline() / PANGO_SCALE) + top.get ());
106 if (i->effect() == dcp::SHADOW) {
107 /* Drop-shadow effect */
108 dcp::Colour const ec = i->effect_colour ();
109 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
110 context->move_to (x + 4, y + 4);
111 layout->add_to_cairo_context (context);
115 /* The actual subtitle */
116 context->move_to (x, y);
117 dcp::Colour const c = i->colour ();
118 context->set_source_rgba (float(c.r) / 255, float(c.g) / 255, float(c.b) / 255, fade_factor);
119 layout->add_to_cairo_context (context);
122 if (i->effect() == dcp::BORDER) {
124 context->move_to (x, y);
125 dcp::Colour ec = i->effect_colour ();
126 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
127 layout->add_to_cairo_context (context);
132 return PositionImage (image, Position<int> (0, top.get ()));