summaryrefslogtreecommitdiff
path: root/src/lib/render_subtitles.cc
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2016-11-08 10:59:22 +0000
committerCarl Hetherington <cth@carlh.net>2016-11-08 10:59:22 +0000
commitde004ef24e078906f656cbf4cc790bbfe11ea69c (patch)
treef5b86576430275975ad9d8223159e09aeecb6c38 /src/lib/render_subtitles.cc
parente3e94b79925f085c7176ea85a396c0a56714f56d (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.
Diffstat (limited to 'src/lib/render_subtitles.cc')
-rw-r--r--src/lib/render_subtitles.cc62
1 files changed, 20 insertions, 42 deletions
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;