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