Backport from master to fix fading of the main body of a subtitle not working at...
authorCarl Hetherington <cth@carlh.net>
Mon, 15 May 2017 15:56:14 +0000 (16:56 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 15 May 2017 15:56:14 +0000 (16:56 +0100)
ChangeLog
src/lib/render_subtitles.cc
src/lib/render_subtitles.h
test/render_subtitles_test.cc

index c5baa829955824b0e7d0075f253920922f7c7e32..3e659788a9620dd4eb47d7ef88fc352c4c48657e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2017-05-15  Carl Hetherington  <cth@carlh.net>
+
+       * Backport fixes from master which correct subtitles being
+       faded in early (before their effects).
+
 2017-04-06  Carl Hetherington  <cth@carlh.net>
 
        * Version 2.10.5 released.
index 0660d0e06b1adfa52cad3adcb4dda76608106f3e..9426427fd507385a12973e0b02823af4c15b19da 100644 (file)
@@ -49,7 +49,7 @@ static FcConfig* fc_config = 0;
 static list<pair<FontFiles, string> > fc_config_fonts;
 
 string
-marked_up (list<SubtitleString> subtitles, int target_height)
+marked_up (list<SubtitleString> subtitles, int target_height, float fade_factor)
 {
        string out;
 
@@ -65,6 +65,8 @@ marked_up (list<SubtitleString> subtitles, int target_height)
                        out += "underline=\"single\" ";
                }
                out += "size=\"" + dcp::raw_convert<string>(i.size_in_pixels(target_height) * 72 * 1024 / 96) + "\" ";
+               /* Between 1-65535 inclusive, apparently... */
+               out += "alpha=\"" + dcp::raw_convert<string>(int(floor(fade_factor * 65534)) + 1) + "\" ";
                out += "color=\"#" + i.colour().to_rgb_string() + "\">";
                out += i.text ();
                out += "</span>";
@@ -73,6 +75,12 @@ marked_up (list<SubtitleString> subtitles, int target_height)
        return out;
 }
 
+static void
+set_source_rgba (Cairo::RefPtr<Cairo::Context> context, dcp::Colour colour, float fade_factor)
+{
+       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.
  */
@@ -229,12 +237,6 @@ render_line (list<SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp:
 
        context->set_line_width (1);
 
-       /* Render the subtitle at the top left-hand corner of image */
-
-       Pango::FontDescription font (font_name);
-       layout->set_font_description (font);
-       layout->set_markup (marked_up (subtitles, target.height));
-
        /* Compute fade factor */
        float fade_factor = 1;
 
@@ -250,6 +252,12 @@ render_line (list<SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp:
                fade_factor = 0;
        }
 
+       /* Render the subtitle at the top left-hand corner of image */
+
+       Pango::FontDescription font (font_name);
+       layout->set_font_description (font);
+       layout->set_markup (marked_up (subtitles, target.height, fade_factor));
+
        context->scale (xscale, yscale);
        layout->update_from_cairo_context (context);
 
@@ -262,8 +270,7 @@ render_line (list<SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp:
 
        if (subtitles.front().effect() == dcp::SHADOW) {
                /* Drop-shadow effect */
-               dcp::Colour const ec = subtitles.front().effect_colour ();
-               context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
+               set_source_rgba (context, subtitles.front().effect_colour(), fade_factor);
                context->move_to (x_offset + 4, y_offset + 4);
                layout->add_to_cairo_context (context);
                context->fill ();
@@ -271,8 +278,7 @@ render_line (list<SubtitleString> subtitles, list<shared_ptr<Font> > fonts, dcp:
 
        if (subtitles.front().effect() == dcp::BORDER) {
                /* Border effect; stroke the subtitle with a large (arbitrarily chosen) line width */
-               dcp::Colour ec = subtitles.front().effect_colour ();
-               context->set_source_rgba (float(ec.r) / 255, float(ec.g) / 255, float(ec.b) / 255, fade_factor);
+               set_source_rgba (context, subtitles.front().effect_colour(), fade_factor);
                context->set_line_width (subtitles.front().outline_width * target.width / 2048.0);
                context->set_line_join (Cairo::LINE_JOIN_ROUND);
                context->move_to (x_offset, y_offset);
index f539896ce7d7c13ecb18cd98ae41c67a74cf8582..02bf3ec656c45b6bb4409ac21a00fa4150321f3b 100644 (file)
@@ -25,7 +25,7 @@
 
 class Font;
 
-std::string marked_up (std::list<SubtitleString> subtitles, int target_height);
+std::string marked_up (std::list<SubtitleString> subtitles, int target_height, float fade_factor);
 std::list<PositionImage> render_subtitles (
        std::list<SubtitleString>, std::list<boost::shared_ptr<Font> > fonts, dcp::Size, DCPTime
        );
index 68b819689103e0476b6e0a0cddc0b449f7dc7e4a..ff0df6047758bc216ef1a6adf864bec1238e3362 100644 (file)
@@ -57,7 +57,7 @@ BOOST_AUTO_TEST_CASE (render_markup_test1)
 {
        std::list<SubtitleString> s;
        add (s, "Hello", false, false, false);
-       BOOST_CHECK_EQUAL (marked_up (s, 1024), "Hello");
+       BOOST_CHECK_EQUAL (marked_up (s, 1024, 1), "Hello");
 }
 
 /** Test marked_up() in render_subtitles.cc */
@@ -65,7 +65,7 @@ BOOST_AUTO_TEST_CASE (render_markup_test2)
 {
        std::list<SubtitleString> s;
        add (s, "Hello", false, true, false);
-       BOOST_CHECK_EQUAL (marked_up (s, 1024), "<b>Hello</b>");
+       BOOST_CHECK_EQUAL (marked_up (s, 1024, 1), "<b>Hello</b>");
 }
 
 
@@ -74,7 +74,7 @@ BOOST_AUTO_TEST_CASE (render_markup_test3)
 {
        std::list<SubtitleString> s;
        add (s, "Hello", true, true, false);
-       BOOST_CHECK_EQUAL (marked_up (s, 1024), "<i><b>Hello</b></i>");
+       BOOST_CHECK_EQUAL (marked_up (s, 1024, 1), "<i><b>Hello</b></i>");
 }
 
 /** Test marked_up() in render_subtitles.cc */
@@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE (render_markup_test4)
 {
        std::list<SubtitleString> s;
        add (s, "Hello", true, true, true);
-       BOOST_CHECK_EQUAL (marked_up (s, 1024), "<i><b><u>Hello</u></b></i>");
+       BOOST_CHECK_EQUAL (marked_up (s, 1024, 1), "<i><b><u>Hello</u></b></i>");
 }
 
 /** Test marked_up() in render_subtitles.cc */
@@ -91,7 +91,7 @@ BOOST_AUTO_TEST_CASE (render_markup_test5)
        std::list<SubtitleString> s;
        add (s, "Hello", false, true, false);
        add (s, " world.", false, false, false);
-       BOOST_CHECK_EQUAL (marked_up (s, 1024), "<b>Hello</b> world.");
+       BOOST_CHECK_EQUAL (marked_up (s, 1024, 1), "<b>Hello</b> world.");
 }
 
 /** Test marked_up() in render_subtitles.cc */
@@ -101,5 +101,5 @@ BOOST_AUTO_TEST_CASE (render_markup_test6)
        add (s, "Hello", true, false, false);
        add (s, " world ", false, false, false);
        add (s, "we are bold.", false, true, false);
-       BOOST_CHECK_EQUAL (marked_up (s, 1024), "<i>Hello</i> world <b>we are bold.</b>");
+       BOOST_CHECK_EQUAL (marked_up (s, 1024, 1), "<i>Hello</i> world <b>we are bold.</b>");
 }