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