0d059eb43f3194744bef1404d13a9edc4bb04dbb
[dcpomatic.git] / src / lib / render_subtitles.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "render_subtitles.h"
21 #include "types.h"
22 #include "image.h"
23 #include <cairomm/cairomm.h>
24 #include <pangomm.h>
25 #include <boost/foreach.hpp>
26
27 using std::list;
28 using std::cout;
29 using std::string;
30 using std::min;
31 using std::max;
32 using std::pair;
33 using boost::shared_ptr;
34 using boost::optional;
35
36 static PositionImage
37 render_subtitle (dcp::SubtitleString const & subtitle, dcp::Size target)
38 {
39         /* Calculate x and y scale factors.  These are only used to stretch
40            the font away from its normal aspect ratio.
41         */
42         float xscale = 1;
43         float yscale = 1;
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 ());
47                         yscale = 1;
48                 } else {
49                         xscale = 1;
50                         yscale = 1 / min (4.0f, subtitle.aspect_adjust ());
51                 }
52         }
53
54         /* Make an empty bitmap as wide as target and at
55            least tall enough for this subtitle.
56         */
57
58         /* Basic guess on height... */
59         int height = subtitle.size() * target.height / (11 * 72);
60         /* ...scaled... */
61         height *= yscale;
62         /* ...and add a bit more for luck */
63         height += target.height / 11;
64
65         shared_ptr<Image> image (new Image (PIX_FMT_RGBA, dcp::Size (target.width, height), false));
66         image->make_black ();
67
68         Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
69                 image->data()[0],
70                 Cairo::FORMAT_ARGB32,
71                 image->size().width,
72                 image->size().height,
73                 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
74                 );
75         
76         Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface);
77         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
78
79         layout->set_alignment (Pango::ALIGN_LEFT);
80
81         context->set_line_width (1);
82
83         /* Render the subtitle at the top left-hand corner of image */
84
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);
89         }
90         layout->set_font_description (font);
91         layout->set_text (subtitle.text ());
92         
93         /* Compute fade factor */
94         /* XXX */
95         float fade_factor = 1;
96
97         layout->update_from_cairo_context (context);
98                 
99         context->scale (xscale, yscale);
100
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);
107                 context->fill ();
108         }
109
110         /* The actual subtitle */
111
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);
116         context->fill ();
117
118         if (subtitle.effect() == dcp::BORDER) {
119                 /* Border effect */
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);
124                 context->stroke ();
125         }
126         
127         int layout_width;
128         int layout_height;
129         layout->get_size (layout_width, layout_height);
130
131         int x = 0;
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;
136                 break;
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);
140                 break;
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;
144                 break;
145         }
146
147         int y = 0;
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;
152                 break;
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);
156                 break;
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;
160                 break;
161         }
162
163         return PositionImage (image, Position<int> (x, y));
164 }
165
166 list<PositionImage>
167 render_subtitles (list<dcp::SubtitleString> subtitles, dcp::Size target)
168 {
169         list<PositionImage> images;
170         BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) {
171                 images.push_back (render_subtitle (i, target));
172         }
173         return images;
174 }