+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.
#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>
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;
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 */
/* 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;
/* 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;
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
);
{
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 */
{
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>");
}
{
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 */
{
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 */
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 */
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>");
}