Various fixes to subtitle rendering.
authorCarl Hetherington <cth@carlh.net>
Tue, 8 Nov 2016 10:59:22 +0000 (10:59 +0000)
committerCarl Hetherington <cth@carlh.net>
Tue, 8 Nov 2016 10:59:22 +0000 (10:59 +0000)
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.

ChangeLog
src/lib/render_subtitles.cc
src/lib/render_subtitles.h
test/render_subtitles_test.cc

index 80164b197b142708b74302555c2a0c87fb57230a..2ea8ceaa5070bd3fb8344ebacedd4a135e6fbbf2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
index 45c964e05d6ed820d06cecf17ca254608c61bab4..aa03aefe1eba7c269de9f5471498078316be5fba 100644 (file)
@@ -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;
index f81685a2871813c0a290daa27e74a7df9de5bed0..f539896ce7d7c13ecb18cd98ae41c67a74cf8582 100644 (file)
@@ -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
        );
index 91aafb072929e9158f95c81a352cdbe4820de0b1..68b819689103e0476b6e0a0cddc0b449f7dc7e4a 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), "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>");
 }