From: Carl Hetherington Date: Mon, 27 Jun 2016 23:34:24 +0000 (+0100) Subject: Fix pango markup when rendering subtitles. X-Git-Tag: v2.8.15~28 X-Git-Url: https://git.carlh.net/gitweb/?p=dcpomatic.git;a=commitdiff_plain;h=ff72bf25f215f5cb58ee165898968380f9dff73f Fix pango markup when rendering subtitles. --- diff --git a/src/lib/render_subtitles.cc b/src/lib/render_subtitles.cc index 0e985371c..fea788a5c 100644 --- a/src/lib/render_subtitles.cc +++ b/src/lib/render_subtitles.cc @@ -44,6 +44,54 @@ using boost::optional; static FcConfig* fc_config = 0; static list > fc_config_fonts; +string +marked_up (list subtitles) +{ + string out; + bool italic = false; + bool bold = false; + bool underline = false; + BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) { + if (i.italic() && !italic) { + out += ""; + } + if (i.bold() && !bold) { + out += ""; + } + if (i.underline() && !underline) { + out += ""; + } + + out += i.text (); + + if (!i.underline() && underline) { + out += ""; + } + if (!i.bold() && bold) { + out += ""; + } + if (!i.italic() && italic) { + out += ""; + } + + italic = i.italic (); + bold = i.bold (); + underline = i.underline (); + } + + if (underline) { + out += ""; + } + if (bold) { + out += ""; + } + if (italic) { + out += ""; + } + + return out; +} + /** @param subtitles A list of subtitles that are all on the same line */ static PositionImage render_line (list subtitles, list > fonts, dcp::Size target) @@ -197,55 +245,7 @@ render_line (list subtitles, list > fonts, Pango::FontDescription font (font_name); font.set_absolute_size (subtitles.front().size_in_pixels (target.height) * PANGO_SCALE); layout->set_font_description (font); - - string marked_up; - bool italic = false; - bool bold = false; - bool underline = false; - BOOST_FOREACH (dcp::SubtitleString const & i, subtitles) { - if (i.italic() != italic) { - if (i.italic()) { - marked_up += ""; - } else { - marked_up += ""; - } - italic = i.italic (); - } - - if (i.bold() != bold) { - if (i.bold()) { - marked_up += ""; - } else { - marked_up += ""; - } - bold = i.bold (); - } - - if (i.underline() != underline) { - if (i.underline()) { - marked_up += ""; - } else { - marked_up += ""; - } - underline = i.underline (); - } - - marked_up += i.text (); - } - - if (italic) { - marked_up += ""; - } - - if (bold) { - marked_up += ""; - } - - if (underline) { - marked_up += ""; - } - - layout->set_markup (marked_up); + layout->set_markup (marked_up (subtitles)); /* Compute fade factor */ /* XXX */ diff --git a/src/lib/render_subtitles.h b/src/lib/render_subtitles.h index 4fd931781..281efe97a 100644 --- a/src/lib/render_subtitles.h +++ b/src/lib/render_subtitles.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2014 Carl Hetherington + Copyright (C) 2014-2016 Carl Hetherington This file is part of DCP-o-matic. @@ -24,4 +24,5 @@ class Font; +std::string marked_up (std::list subtitles); std::list render_subtitles (std::list, std::list > fonts, dcp::Size); diff --git a/test/render_subtitles_test.cc b/test/render_subtitles_test.cc new file mode 100644 index 000000000..c56f9dfa1 --- /dev/null +++ b/test/render_subtitles_test.cc @@ -0,0 +1,84 @@ +/* + Copyright (C) 2016 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + +#include "lib/render_subtitles.h" +#include +#include + +static void +add (std::list& s, std::string text, bool italic, bool bold, bool underline) +{ + s.push_back ( + dcp::SubtitleString ( + boost::optional (), + italic, + bold, + underline, + dcp::Colour (255, 255, 255), + 42, + 1, + dcp::Time (), + dcp::Time (), + 1, + dcp::HALIGN_LEFT, + 1, + dcp::VALIGN_TOP, + dcp::DIRECTION_LTR, + text, + dcp::NONE, + dcp::Colour (0, 0, 0), + dcp::Time (), + dcp::Time () + ) + ); +} + +/** Test marked_up() in render_subtitles.cc */ +BOOST_AUTO_TEST_CASE (render_markup_test1) +{ + std::list s; + add (s, "Hello", false, false, false); + BOOST_CHECK_EQUAL (marked_up (s), "Hello"); +} + +/** Test marked_up() in render_subtitles.cc */ +BOOST_AUTO_TEST_CASE (render_markup_test2) +{ + std::list s; + add (s, "Hello", false, true, false); + BOOST_CHECK_EQUAL (marked_up (s), "Hello"); +} + + +/** Test marked_up() in render_subtitles.cc */ +BOOST_AUTO_TEST_CASE (render_markup_test3) +{ + std::list s; + add (s, "Hello", true, true, false); + BOOST_CHECK_EQUAL (marked_up (s), "Hello"); +} + +/** Test marked_up() in render_subtitles.cc */ +BOOST_AUTO_TEST_CASE (render_markup_test4) +{ + std::list s; + add (s, "Hello", true, true, true); + BOOST_CHECK_EQUAL (marked_up (s), "Hello"); +} diff --git a/test/wscript b/test/wscript index 211ead9c2..d336317e5 100644 --- a/test/wscript +++ b/test/wscript @@ -79,6 +79,7 @@ def build(bld): rect_test.cc reels_test.cc required_disk_space_test.cc + render_subtitles_test.cc resampler_test.cc scaling_test.cc seek_zero_test.cc