Use the correct font to render subtitles in preview / burn-in (#663).
[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 "cross.h"
24 #include "font.h"
25 #include <cairomm/cairomm.h>
26 #include <pangomm.h>
27 #include <boost/foreach.hpp>
28
29 using std::list;
30 using std::cout;
31 using std::string;
32 using std::min;
33 using std::max;
34 using std::pair;
35 using std::make_pair;
36 using boost::shared_ptr;
37 using boost::optional;
38
39 static FcConfig* fc_config = 0;
40 static list<pair<boost::filesystem::path, string> > fc_config_fonts;
41
42 static PositionImage
43 render_subtitle (dcp::SubtitleString const & subtitle, list<shared_ptr<Font> > fonts, dcp::Size target)
44 {
45         /* Calculate x and y scale factors.  These are only used to stretch
46            the font away from its normal aspect ratio.
47         */
48         float xscale = 1;
49         float yscale = 1;
50         if (fabs (subtitle.aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) {
51                 if (subtitle.aspect_adjust() < 1) {
52                         xscale = max (0.25f, subtitle.aspect_adjust ());
53                         yscale = 1;
54                 } else {
55                         xscale = 1;
56                         yscale = 1 / min (4.0f, subtitle.aspect_adjust ());
57                 }
58         }
59
60         /* Make an empty bitmap as wide as target and at
61            least tall enough for this subtitle.
62         */
63
64         /* Basic guess on height... */
65         int height = subtitle.size() * target.height / (11 * 72);
66         /* ...scaled... */
67         height *= yscale;
68         /* ...and add a bit more for luck */
69         height += target.height / 11;
70
71         shared_ptr<Image> image (new Image (PIX_FMT_RGBA, dcp::Size (target.width, height), false));
72         image->make_black ();
73
74         Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (
75                 image->data()[0],
76                 Cairo::FORMAT_ARGB32,
77                 image->size().width,
78                 image->size().height,
79                 Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
80                 );
81
82         Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface);
83
84         if (!fc_config) {
85                 fc_config = FcConfigCreate ();
86         }
87
88         boost::filesystem::path font_file = shared_path () / "LiberationSans-Regular.ttf";
89         BOOST_FOREACH (shared_ptr<Font> i, fonts) {
90                 if (i->id() == subtitle.font() && i->file ()) {
91                         font_file = i->file().get ();
92                 }
93         }
94
95         list<pair<boost::filesystem::path, string> >::const_iterator existing = fc_config_fonts.begin ();
96         while (existing != fc_config_fonts.end() && existing->first != font_file) {
97                 ++existing;
98         }
99
100         string font_name;
101         if (existing != fc_config_fonts.end ()) {
102                 font_name = existing->second;
103         } else {
104                 /* Make this font available to DCP-o-matic */
105                 FcConfigAppFontAddFile (fc_config, reinterpret_cast<FcChar8 const *> (font_file.string().c_str ()));
106
107                 FcPattern* pattern = FcPatternBuild (0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *> (0));
108                 FcObjectSet* object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
109                 FcFontSet* font_set = FcFontList (fc_config, pattern, object_set);
110                 if (font_set) {
111                         for (int i = 0; i < font_set->nfont; ++i) {
112                                 FcPattern* font = font_set->fonts[i];
113                                 FcChar8* file;
114                                 FcChar8* family;
115                                 FcChar8* style;
116                                 if (
117                                         FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
118                                         FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
119                                         FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
120                                         ) {
121                                         font_name = reinterpret_cast<char const *> (family);
122                                 }
123                         }
124
125                         FcFontSetDestroy (font_set);
126                 }
127
128                 FcObjectSetDestroy (object_set);
129                 FcPatternDestroy (pattern);
130
131                 fc_config_fonts.push_back (make_pair (font_file, font_name));
132         }
133
134         FcConfigSetCurrent (fc_config);
135
136         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
137
138         layout->set_alignment (Pango::ALIGN_LEFT);
139
140         context->set_line_width (1);
141
142         /* Render the subtitle at the top left-hand corner of image */
143
144         Pango::FontDescription font (font_name);
145         font.set_absolute_size (subtitle.size_in_pixels (target.height) * PANGO_SCALE);
146         if (subtitle.italic ()) {
147                 font.set_style (Pango::STYLE_ITALIC);
148         }
149         layout->set_font_description (font);
150         layout->set_text (subtitle.text ());
151
152         /* Compute fade factor */
153         /* XXX */
154         float fade_factor = 1;
155
156         layout->update_from_cairo_context (context);
157
158         context->scale (xscale, yscale);
159
160         if (subtitle.effect() == dcp::SHADOW) {
161                 /* Drop-shadow effect */
162                 dcp::Colour const ec = subtitle.effect_colour ();
163                 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
164                 context->move_to (4, 4);
165                 layout->add_to_cairo_context (context);
166                 context->fill ();
167         }
168
169         /* The actual subtitle */
170
171         dcp::Colour const c = subtitle.colour ();
172         context->set_source_rgba (float(c.r) / 255, float(c.g) / 255, float(c.b) / 255, fade_factor);
173         context->move_to (0, 0);
174         layout->add_to_cairo_context (context);
175         context->fill ();
176
177         if (subtitle.effect() == dcp::BORDER) {
178                 /* Border effect */
179                 dcp::Colour ec = subtitle.effect_colour ();
180                 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
181                 context->move_to (0, 0);
182                 layout->add_to_cairo_context (context);
183                 context->stroke ();
184         }
185
186         int layout_width;
187         int layout_height;
188         layout->get_size (layout_width, layout_height);
189
190         int x = 0;
191         switch (subtitle.h_align ()) {
192         case dcp::HALIGN_LEFT:
193                 /* h_position is distance between left of frame and left of subtitle */
194                 x = subtitle.h_position() * target.width;
195                 break;
196         case dcp::HALIGN_CENTER:
197                 /* h_position is distance between centre of frame and centre of subtitle */
198                 x = (0.5 + subtitle.h_position()) * target.width - layout_width / (PANGO_SCALE * 2);
199                 break;
200         case dcp::HALIGN_RIGHT:
201                 /* h_position is distance between right of frame and right of subtitle */
202                 x = (1.0 - subtitle.h_position()) * target.width - layout_width / PANGO_SCALE;
203                 break;
204         }
205
206         int y = 0;
207         switch (subtitle.v_align ()) {
208         case dcp::VALIGN_TOP:
209                 /* v_position is distance between top of frame and top of subtitle */
210                 y = subtitle.v_position() * target.height;
211                 break;
212         case dcp::VALIGN_CENTER:
213                 /* v_position is distance between centre of frame and centre of subtitle */
214                 y = (0.5 + subtitle.v_position()) * target.height - layout_height / (PANGO_SCALE * 2);
215                 break;
216         case dcp::VALIGN_BOTTOM:
217                 /* v_position is distance between bottom of frame and bottom of subtitle */
218                 y = (1.0 - subtitle.v_position()) * target.height - layout_height / PANGO_SCALE;
219                 break;
220         }
221
222         return PositionImage (image, Position<int> (x, y));
223 }
224
225 list<PositionImage>
226 render_subtitles (list<dcp::SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target)
227 {
228         list<PositionImage> images;
229         BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) {
230                 images.push_back (render_subtitle (i, fonts, target));
231         }
232         return images;
233 }