diff options
| author | Carl Hetherington <cth@carlh.net> | 2020-04-23 21:38:48 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2020-04-24 00:46:31 +0200 |
| commit | 5e6c80eeda317edb2d782b44d5c6a2258ba3a14a (patch) | |
| tree | 8a2361fd6e15871f5ff8de1707f62c0196398e27 | |
| parent | dfc1003dfddd3f5e10204ec9b7c2b76cf0436325 (diff) | |
Extract some bits of code into their own methods.
| -rw-r--r-- | src/lib/render_text.cc | 110 |
1 files changed, 66 insertions, 44 deletions
diff --git a/src/lib/render_text.cc b/src/lib/render_text.cc index 8b9d93423..3fb67f100 100644 --- a/src/lib/render_text.cc +++ b/src/lib/render_text.cc @@ -88,52 +88,20 @@ set_source_rgba (Cairo::RefPtr<Cairo::Context> context, dcp::Colour colour, floa context->set_source_rgba (float(colour.r) / 255, float(colour.g) / 255, float(colour.b) / 255, fade_factor); } -/** @param subtitles A list of subtitles that are all on the same line, - * at the same time and with the same fade in/out. - */ -static PositionImage -render_line (list<StringText> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target, DCPTime time, int frame_rate) -{ - /* XXX: this method can only handle italic / bold changes mid-line, - nothing else yet. - */ - - DCPOMATIC_ASSERT (!subtitles.empty ()); - - /* Calculate x and y scale factors. These are only used to stretch - the font away from its normal aspect ratio. - */ - float xscale = 1; - float yscale = 1; - if (fabs (subtitles.front().aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) { - if (subtitles.front().aspect_adjust() < 1) { - xscale = max (0.25f, subtitles.front().aspect_adjust ()); - yscale = 1; - } else { - xscale = 1; - yscale = 1 / min (4.0f, subtitles.front().aspect_adjust ()); - } - } - - /* Make an empty bitmap as wide as target and at - least tall enough for this subtitle. - */ - - int largest = 0; - BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) { - largest = max (largest, i.size()); - } - /* Basic guess on height... */ - int height = largest * target.height / (11 * 72); - /* ...scaled... */ - height *= yscale; - /* ...and add a bit more for luck */ - height += target.height / 11; +static shared_ptr<Image> +create_image (int width, int height) +{ /* FFmpeg BGRA means first byte blue, second byte green, third byte red, fourth byte alpha */ - shared_ptr<Image> image (new Image (AV_PIX_FMT_BGRA, dcp::Size (target.width, height), false)); + shared_ptr<Image> image (new Image (AV_PIX_FMT_BGRA, dcp::Size (width, height), false)); image->make_black (); + return image; +} + +static Cairo::RefPtr<Cairo::ImageSurface> +create_surface (shared_ptr<Image> image) +{ #ifdef DCPOMATIC_HAVE_FORMAT_STRIDE_FOR_WIDTH Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create ( image->data()[0], @@ -156,8 +124,13 @@ render_line (list<StringText> subtitles, list<shared_ptr<Font> > fonts, dcp::Siz ); #endif - Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface); + return surface; +} + +static string +setup_font (StringText const& subtitle, list<shared_ptr<Font> > const& fonts) +{ if (!fc_config) { fc_config = FcInitLoadConfig (); } @@ -177,7 +150,7 @@ render_line (list<StringText> subtitles, list<shared_ptr<Font> > fonts, dcp::Siz } BOOST_FOREACH (shared_ptr<Font> i, fonts) { - if (i->id() == subtitles.front().font() && i->file()) { + if (i->id() == subtitle.font() && i->file()) { font_file = i->file (); } } @@ -223,7 +196,56 @@ render_line (list<StringText> subtitles, list<shared_ptr<Font> > fonts, dcp::Siz } FcConfigSetCurrent (fc_config); + return font_name; +} + + +/** @param subtitles A list of subtitles that are all on the same line, + * at the same time and with the same fade in/out. + */ +static PositionImage +render_line (list<StringText> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target, DCPTime time, int frame_rate) +{ + /* XXX: this method can only handle italic / bold changes mid-line, + nothing else yet. + */ + + DCPOMATIC_ASSERT (!subtitles.empty ()); + /* Calculate x and y scale factors. These are only used to stretch + the font away from its normal aspect ratio. + */ + float xscale = 1; + float yscale = 1; + if (fabs (subtitles.front().aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) { + if (subtitles.front().aspect_adjust() < 1) { + xscale = max (0.25f, subtitles.front().aspect_adjust ()); + yscale = 1; + } else { + xscale = 1; + yscale = 1 / min (4.0f, subtitles.front().aspect_adjust ()); + } + } + + /* Make an empty bitmap as wide as target and at + least tall enough for this subtitle. + */ + + int largest = 0; + BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) { + largest = max (largest, i.size()); + } + /* Basic guess on height... */ + int height = largest * target.height / (11 * 72); + /* ...scaled... */ + height *= yscale; + /* ...and add a bit more for luck */ + height += target.height / 11; + + shared_ptr<Image> image = create_image (target.width, height); + Cairo::RefPtr<Cairo::Surface> surface = create_surface (image); + Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface); + string const font_name = setup_font (subtitles.front(), fonts); Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context); layout->set_alignment (Pango::ALIGN_LEFT); |
