From de004ef24e078906f656cbf4cc790bbfe11ea69c Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 8 Nov 2016 10:59:22 +0000 Subject: Various fixes to subtitle rendering. Use to mark text up for Pango as this can set up everything, and is easier than using .. etc. Fix the estimation of subtitle area size to cope with different subtitle sizes. Use show_in_cairo_context rather than add_to_cairo_context so that colours in the markup are respected. --- ChangeLog | 6 +++++ src/lib/render_subtitles.cc | 62 ++++++++++++++----------------------------- src/lib/render_subtitles.h | 2 +- test/render_subtitles_test.cc | 12 ++++----- 4 files changed, 33 insertions(+), 49 deletions(-) diff --git a/ChangeLog b/ChangeLog index 80164b197..2ea8ceaa5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2016-11-08 c.hetherington + + * Fix subtitle rendering when size and colour changes + word-by-word or character-by-character. Fix some cut-off + subtitles. + 2016-11-07 Carl Hetherington * Updated da_DK translation from Anders Uhl Pedersen. diff --git a/src/lib/render_subtitles.cc b/src/lib/render_subtitles.cc index 45c964e05..aa03aefe1 100644 --- a/src/lib/render_subtitles.cc +++ b/src/lib/render_subtitles.cc @@ -24,6 +24,7 @@ #include "cross.h" #include "font.h" #include "dcpomatic_assert.h" +#include #include #include #include @@ -45,48 +46,25 @@ static FcConfig* fc_config = 0; static list > fc_config_fonts; string -marked_up (list subtitles) +marked_up (list subtitles, int target_height) { string out; - bool italic = false; - bool bold = false; - bool underline = false; - BOOST_FOREACH (SubtitleString const & i, subtitles) { - if (i.italic() && !italic) { - out += ""; - } - if (i.bold() && !bold) { - out += ""; - } - if (i.underline() && !underline) { - out += ""; - } - if (!i.underline() && underline) { - out += ""; + BOOST_FOREACH (SubtitleString const & i, subtitles) { + out += "(i.size_in_pixels(target_height) * 72 * 1024 / 96) + "\" "; + out += "color=\"#" + i.colour().to_rgb_string() + "\">"; out += i.text (); - } - - if (underline) { - out += ""; - } - if (bold) { - out += ""; - } - if (italic) { - out += ""; + out += ""; } return out; @@ -123,8 +101,12 @@ render_line (list subtitles, list > fonts, dcp: 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 = subtitles.front().size() * target.height / (11 * 72); + int height = largest * target.height / (11 * 72); /* ...scaled... */ height *= yscale; /* ...and add a bit more for luck */ @@ -247,9 +229,8 @@ render_line (list subtitles, list > fonts, dcp: /* Render the subtitle at the top left-hand corner of image */ Pango::FontDescription font (font_name); - font.set_absolute_size (subtitles.front().size_in_pixels (target.height) * PANGO_SCALE); layout->set_font_description (font); - layout->set_markup (marked_up (subtitles)); + layout->set_markup (marked_up (subtitles, target.height)); /* Compute fade factor */ float fade_factor = 1; @@ -296,12 +277,9 @@ render_line (list subtitles, list > fonts, dcp: /* The actual subtitle */ - dcp::Colour const c = subtitles.front().colour (); - context->set_source_rgba (float(c.r) / 255, float(c.g) / 255, float(c.b) / 255, fade_factor); context->set_line_width (0); context->move_to (x_offset, 0); - layout->add_to_cairo_context (context); - context->fill (); + layout->show_in_cairo_context (context); int layout_width; int layout_height; diff --git a/src/lib/render_subtitles.h b/src/lib/render_subtitles.h index f81685a28..f539896ce 100644 --- a/src/lib/render_subtitles.h +++ b/src/lib/render_subtitles.h @@ -25,7 +25,7 @@ class Font; -std::string marked_up (std::list subtitles); +std::string marked_up (std::list subtitles, int target_height); std::list render_subtitles ( std::list, std::list > fonts, dcp::Size, DCPTime ); diff --git a/test/render_subtitles_test.cc b/test/render_subtitles_test.cc index 91aafb072..68b819689 100644 --- a/test/render_subtitles_test.cc +++ b/test/render_subtitles_test.cc @@ -57,7 +57,7 @@ BOOST_AUTO_TEST_CASE (render_markup_test1) { std::list s; add (s, "Hello", false, false, false); - BOOST_CHECK_EQUAL (marked_up (s), "Hello"); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "Hello"); } /** Test marked_up() in render_subtitles.cc */ @@ -65,7 +65,7 @@ BOOST_AUTO_TEST_CASE (render_markup_test2) { std::list s; add (s, "Hello", false, true, false); - BOOST_CHECK_EQUAL (marked_up (s), "Hello"); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "Hello"); } @@ -74,7 +74,7 @@ BOOST_AUTO_TEST_CASE (render_markup_test3) { std::list s; add (s, "Hello", true, true, false); - BOOST_CHECK_EQUAL (marked_up (s), "Hello"); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "Hello"); } /** Test marked_up() in render_subtitles.cc */ @@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE (render_markup_test4) { std::list s; add (s, "Hello", true, true, true); - BOOST_CHECK_EQUAL (marked_up (s), "Hello"); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "Hello"); } /** Test marked_up() in render_subtitles.cc */ @@ -91,7 +91,7 @@ BOOST_AUTO_TEST_CASE (render_markup_test5) std::list s; add (s, "Hello", false, true, false); add (s, " world.", false, false, false); - BOOST_CHECK_EQUAL (marked_up (s), "Hello world."); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "Hello 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), "Hello world we are bold."); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "Hello world we are bold."); } -- cgit v1.2.3