Remove debug code.
[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 = shared_path () / "LiberationSans-Regular.ttf";
90         BOOST_FOREACH (shared_ptr<Font> i, fonts) {
91                 if (i->id() == subtitle.font() && i->file ()) {
92                         font_file = i->file().get ();
93                 }
94         }
95
96         list<pair<boost::filesystem::path, string> >::const_iterator existing = fc_config_fonts.begin ();
97         while (existing != fc_config_fonts.end() && existing->first != font_file) {
98                 ++existing;
99         }
100
101         string font_name;
102         if (existing != fc_config_fonts.end ()) {
103                 font_name = existing->second;
104         } else {
105                 /* Make this font available to DCP-o-matic */
106                 FcConfigAppFontAddFile (fc_config, reinterpret_cast<FcChar8 const *> (font_file.string().c_str ()));
107
108                 FcPattern* pattern = FcPatternBuild (0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *> (0));
109                 FcObjectSet* object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
110                 FcFontSet* font_set = FcFontList (fc_config, pattern, object_set);
111                 if (font_set) {
112                         for (int i = 0; i < font_set->nfont; ++i) {
113                                 FcPattern* font = font_set->fonts[i];
114                                 FcChar8* file;
115                                 FcChar8* family;
116                                 FcChar8* style;
117                                 if (
118                                         FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
119                                         FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
120                                         FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
121                                         ) {
122                                         font_name = reinterpret_cast<char const *> (family);
123                                 }
124                         }
125
126                         FcFontSetDestroy (font_set);
127                 }
128
129                 FcObjectSetDestroy (object_set);
130                 FcPatternDestroy (pattern);
131
132                 fc_config_fonts.push_back (make_pair (font_file, font_name));
133         }
134
135         FcConfigSetCurrent (fc_config);
136
137         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
138
139         layout->set_alignment (Pango::ALIGN_LEFT);
140
141         context->set_line_width (1);
142
143         /* Render the subtitle at the top left-hand corner of image */
144
145         Pango::FontDescription font (font_name);
146         font.set_absolute_size (subtitle.size_in_pixels (target.height) * PANGO_SCALE);
147         if (subtitle.italic ()) {
148                 font.set_style (Pango::STYLE_ITALIC);
149         }
150         layout->set_font_description (font);
151         layout->set_text (subtitle.text ());
152
153         /* Compute fade factor */
154         /* XXX */
155         float fade_factor = 1;
156
157         layout->update_from_cairo_context (context);
158
159         context->scale (xscale, yscale);
160
161         if (subtitle.effect() == dcp::SHADOW) {
162                 /* Drop-shadow effect */
163                 dcp::Colour const ec = subtitle.effect_colour ();
164                 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
165                 context->move_to (4, 4);
166                 layout->add_to_cairo_context (context);
167                 context->fill ();
168         }
169
170         /* The actual subtitle */
171
172         dcp::Colour const c = subtitle.colour ();
173         context->set_source_rgba (float(c.r) / 255, float(c.g) / 255, float(c.b) / 255, fade_factor);
174         context->move_to (0, 0);
175         layout->add_to_cairo_context (context);
176         context->fill ();
177
178         if (subtitle.effect() == dcp::BORDER) {
179                 /* Border effect */
180                 dcp::Colour ec = subtitle.effect_colour ();
181                 context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
182                 context->move_to (0, 0);
183                 layout->add_to_cairo_context (context);
184                 context->stroke ();
185         }
186
187         int layout_width;
188         int layout_height;
189         layout->get_size (layout_width, layout_height);
190
191         int x = 0;
192         switch (subtitle.h_align ()) {
193         case dcp::HALIGN_LEFT:
194                 /* h_position is distance between left of frame and left of subtitle */
195                 x = subtitle.h_position() * target.width;
196                 break;
197         case dcp::HALIGN_CENTER:
198                 /* h_position is distance between centre of frame and centre of subtitle */
199                 x = (0.5 + subtitle.h_position()) * target.width - layout_width / (PANGO_SCALE * 2);
200                 break;
201         case dcp::HALIGN_RIGHT:
202                 /* h_position is distance between right of frame and right of subtitle */
203                 x = (1.0 - subtitle.h_position()) * target.width - layout_width / PANGO_SCALE;
204                 break;
205         }
206
207         int y = 0;
208         switch (subtitle.v_align ()) {
209         case dcp::VALIGN_TOP:
210                 /* v_position is distance between top of frame and top of subtitle */
211                 y = subtitle.v_position() * target.height;
212                 break;
213         case dcp::VALIGN_CENTER:
214                 /* v_position is distance between centre of frame and centre of subtitle */
215                 y = (0.5 + subtitle.v_position()) * target.height - layout_height / (PANGO_SCALE * 2);
216                 break;
217         case dcp::VALIGN_BOTTOM:
218                 /* v_position is distance between bottom of frame and bottom of subtitle */
219                 y = (1.0 - subtitle.v_position()) * target.height - layout_height / PANGO_SCALE;
220                 break;
221         }
222
223         return PositionImage (image, Position<int> (x, y));
224 }
225
226 list<PositionImage>
227 render_subtitles (list<dcp::SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target)
228 {
229         list<PositionImage> images;
230         BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) {
231                 images.push_back (render_subtitle (i, fonts, target));
232         }
233         return images;
234 }