X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Frender_text.cc;h=3fb67f100786e2bdae0cfcba50d62e549269a114;hb=5e6c80eeda317edb2d782b44d5c6a2258ba3a14a;hp=8b9d93423d390062e53b710fba9feaf06b098a92;hpb=dfc1003dfddd3f5e10204ec9b7c2b76cf0436325;p=dcpomatic.git 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 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 subtitles, list > 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 +create_image (int width, int height) +{ /* FFmpeg BGRA means first byte blue, second byte green, third byte red, fourth byte alpha */ - shared_ptr image (new Image (AV_PIX_FMT_BGRA, dcp::Size (target.width, height), false)); + shared_ptr image (new Image (AV_PIX_FMT_BGRA, dcp::Size (width, height), false)); image->make_black (); + return image; +} + +static Cairo::RefPtr +create_surface (shared_ptr image) +{ #ifdef DCPOMATIC_HAVE_FORMAT_STRIDE_FOR_WIDTH Cairo::RefPtr surface = Cairo::ImageSurface::create ( image->data()[0], @@ -156,8 +124,13 @@ render_line (list subtitles, list > fonts, dcp::Siz ); #endif - Cairo::RefPtr context = Cairo::Context::create (surface); + return surface; +} + +static string +setup_font (StringText const& subtitle, list > const& fonts) +{ if (!fc_config) { fc_config = FcInitLoadConfig (); } @@ -177,7 +150,7 @@ render_line (list subtitles, list > fonts, dcp::Siz } BOOST_FOREACH (shared_ptr 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 subtitles, list > 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 subtitles, list > 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 = create_image (target.width, height); + Cairo::RefPtr surface = create_surface (image); + Cairo::RefPtr context = Cairo::Context::create (surface); + string const font_name = setup_font (subtitles.front(), fonts); Glib::RefPtr layout = Pango::Layout::create (context); layout->set_alignment (Pango::ALIGN_LEFT);