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