diff options
| author | Carl Hetherington <cth@carlh.net> | 2016-11-08 10:59:22 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2016-11-08 10:59:22 +0000 |
| commit | de004ef24e078906f656cbf4cc790bbfe11ea69c (patch) | |
| tree | f5b86576430275975ad9d8223159e09aeecb6c38 | |
| parent | e3e94b79925f085c7176ea85a396c0a56714f56d (diff) | |
Various fixes to subtitle rendering.
Use <span> to mark text up for Pango as this can set up everything,
and is easier than using <b>..</b> 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.
| -rw-r--r-- | ChangeLog | 6 | ||||
| -rw-r--r-- | src/lib/render_subtitles.cc | 62 | ||||
| -rw-r--r-- | src/lib/render_subtitles.h | 2 | ||||
| -rw-r--r-- | test/render_subtitles_test.cc | 12 |
4 files changed, 33 insertions, 49 deletions
@@ -1,3 +1,9 @@ +2016-11-08 c.hetherington <cth@carlh.net> + + * 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 <cth@carlh.net> * 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 <dcp/raw_convert.h> #include <fontconfig/fontconfig.h> #include <cairomm/cairomm.h> #include <pangomm.h> @@ -45,48 +46,25 @@ static FcConfig* fc_config = 0; static list<pair<FontFiles, string> > fc_config_fonts; string -marked_up (list<SubtitleString> subtitles) +marked_up (list<SubtitleString> 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 += "<i>"; - } - if (i.bold() && !bold) { - out += "<b>"; - } - if (i.underline() && !underline) { - out += "<u>"; - } - if (!i.underline() && underline) { - out += "</u>"; + BOOST_FOREACH (SubtitleString const & i, subtitles) { + out += "<span "; + if (i.italic()) { + out += "style=\"italic\" "; } - if (!i.bold() && bold) { - out += "</b>"; + if (i.bold()) { + out += "weight=\"bold\" "; } - if (!i.italic() && italic) { - out += "</i>"; + if (i.underline()) { + out += "underline=\"single\" "; } - - italic = i.italic (); - bold = i.bold (); - underline = i.underline (); - + out += "size=\"" + dcp::raw_convert<string>(i.size_in_pixels(target_height) * 72 * 1024 / 96) + "\" "; + out += "color=\"#" + i.colour().to_rgb_string() + "\">"; out += i.text (); - } - - if (underline) { - out += "</u>"; - } - if (bold) { - out += "</b>"; - } - if (italic) { - out += "</i>"; + out += "</span>"; } return out; @@ -123,8 +101,12 @@ render_line (list<SubtitleString> subtitles, list<shared_ptr<Font> > 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<SubtitleString> subtitles, list<shared_ptr<Font> > 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<SubtitleString> subtitles, list<shared_ptr<Font> > 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<SubtitleString> subtitles); +std::string marked_up (std::list<SubtitleString> subtitles, int target_height); std::list<PositionImage> render_subtitles ( std::list<SubtitleString>, std::list<boost::shared_ptr<Font> > 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<SubtitleString> 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<SubtitleString> s; add (s, "Hello", false, true, false); - BOOST_CHECK_EQUAL (marked_up (s), "<b>Hello</b>"); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "<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), "<i><b>Hello</b></i>"); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "<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), "<i><b><u>Hello</u></b></i>"); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "<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), "<b>Hello</b> world."); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "<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), "<i>Hello</i> world <b>we are bold.</b>"); + BOOST_CHECK_EQUAL (marked_up (s, 1024), "<i>Hello</i> world <b>we are bold.</b>"); } |
