ttml: correctly implement lineHeight behaviour

The specified behaviour in TTML when lineHeight is "normal" is different
from the behaviour when a percentage is given. In the former case, the
line height is a percentage (the TTML spec recommends 125%) of the largest
font size that is applied to the spans within the block; in the latter
case, the line height is the given percentage of the font size that is
applied to the block itself.

The code doesn't correctly implement this behaviour; this patch fixes
that.

https://bugzilla.gnome.org/show_bug.cgi?id=780402
This commit is contained in:
Chris Bass 2017-03-22 10:21:28 +00:00 committed by Sebastian Dröge
parent f28df30a8b
commit 0288ee24e9
3 changed files with 42 additions and 11 deletions

View file

@ -2194,14 +2194,47 @@ static BlockMetrics
gst_ttml_render_get_block_metrics (GstTtmlRender * render, UnifiedBlock * block)
{
BlockMetrics ret;
guint descender = gst_ttml_render_get_most_frequent_descender (render, block);
guint font_size = (guint) ceil (block->style_set->font_size * render->height);
ret.line_height = (guint) ceil (font_size * block->style_set->line_height);
ret.baseline_offset = (guint) ((font_size + ret.line_height) / 2.0)
- descender;
GST_CAT_DEBUG (ttmlrender_debug,
"Got most frequent descender value of %u pixels.", descender);
/*
* The specified behaviour in TTML when lineHeight is "normal" is different
* from the behaviour when a percentage is given. In the former case, the
* line height is a percentage (the TTML spec recommends 125%) of the largest
* font size that is applied to the spans within the block; in the latter
* case, the line height is the given percentage of the font size that is
* applied to the block itself.
*/
if (block->style_set->line_height < 0) { /* lineHeight="normal" case */
guint max_text_height = 0;
guint descender = 0;
guint i;
for (i = 0; i < gst_ttml_render_unified_block_element_count (block); ++i) {
UnifiedElement *ue = gst_ttml_render_unified_block_get_element (block, i);
if (ue->pango_font_metrics.height > max_text_height) {
max_text_height = ue->pango_font_metrics.height;
descender =
ue->pango_font_metrics.height - ue->pango_font_metrics.baseline;
}
}
GST_CAT_LOG (ttmlrender_debug, "Max descender: %u Max text height: %u",
descender, max_text_height);
ret.line_height = (guint) ceil (max_text_height * 1.25);
ret.baseline_offset = (guint) ((max_text_height + ret.line_height) / 2.0)
- descender;
} else {
guint descender;
guint font_size;
descender = gst_ttml_render_get_most_frequent_descender (render, block);
GST_CAT_LOG (ttmlrender_debug,
"Got most frequent descender value of %u pixels.", descender);
font_size = (guint) ceil (block->style_set->font_size * render->height);
ret.line_height = (guint) ceil (font_size * block->style_set->line_height);
ret.baseline_offset = (guint) ((font_size + ret.line_height) / 2.0)
- descender;
}
return ret;
}

View file

@ -68,7 +68,7 @@ gst_subtitle_style_set_new (void)
ret->font_family = g_strdup ("default");
ret->font_size = 1.0;
ret->line_height = 1.25;
ret->line_height = -1;
ret->color = white;
ret->background_color = transparent;
ret->line_padding = 0.0;

View file

@ -463,10 +463,8 @@ ttml_update_style_set (GstSubtitleStyleSet * style_set, TtmlStyleSet * tss,
style_set->font_size *= (1.0 / cellres_y);
if ((attr = ttml_style_set_get_attr (tss, "lineHeight"))) {
/* The TTML spec (section 8.2.12) recommends using a line height of 125%
* when "normal" is specified. */
if (g_strcmp0 (attr, "normal") == 0)
style_set->line_height = 1.25;
style_set->line_height = -1;
else
style_set->line_height = g_ascii_strtod (attr, NULL) / 100.0;
}