From f178bb42d3622b3144bc30d5c5d99c884eee0455 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 30 Sep 2021 17:08:29 -0400 Subject: pangocairo: Round font metrics when appropriate When metrics hinting is on, we used to provide rounded font metrics. We should keep doing that since otherwise we end up with logical rects sometimes being rounded and sometimes not, leading to jumping empty lines. Test included. Fixes: #421 --- pango/pangocairo-fcfont.c | 14 +++++++++++++- tests/testmisc.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/pango/pangocairo-fcfont.c b/pango/pangocairo-fcfont.c index 0019f83a..85cc45c8 100644 --- a/pango/pangocairo-fcfont.c +++ b/pango/pangocairo-fcfont.c @@ -76,8 +76,20 @@ pango_cairo_fc_font_create_base_metrics_for_context (PangoCairoFont *cfont, PangoContext *context) { PangoFcFont *fcfont = (PangoFcFont *) (cfont); + PangoFontMetrics *metrics; + const cairo_font_options_t *options; - return pango_fc_font_create_base_metrics_for_context (fcfont, context); + metrics = pango_fc_font_create_base_metrics_for_context (fcfont, context); + + options = pango_cairo_context_get_font_options (context); + if (cairo_font_options_get_hint_metrics (options) == CAIRO_HINT_METRICS_ON) + { + metrics->ascent = PANGO_PIXELS_CEIL (metrics->ascent) * PANGO_SCALE; + metrics->descent = PANGO_PIXELS_CEIL (metrics->descent) * PANGO_SCALE; + metrics->height = PANGO_PIXELS_CEIL (metrics->height) * PANGO_SCALE; + } + + return metrics; } static void diff --git a/tests/testmisc.c b/tests/testmisc.c index 60694134..cf64e00f 100644 --- a/tests/testmisc.c +++ b/tests/testmisc.c @@ -489,6 +489,46 @@ test_extents (void) g_object_unref (context); } +static void +test_empty_line_height (void) +{ + PangoContext *context; + PangoLayout *layout; + PangoRectangle ext1, ext2, ext3; + cairo_font_options_t *options; + int hint; + + context = pango_font_map_create_context (pango_cairo_font_map_get_default ()); + + for (hint = CAIRO_HINT_METRICS_OFF; hint <= CAIRO_HINT_METRICS_ON; hint++) + { + options = cairo_font_options_create (); + cairo_font_options_set_hint_metrics (options, hint); + pango_cairo_context_set_font_options (context, options); + cairo_font_options_destroy (options); + + layout = pango_layout_new (context); + + pango_layout_get_extents (layout, NULL, &ext1); + + pango_layout_set_text (layout, "a", 1); + + pango_layout_get_extents (layout, NULL, &ext2); + + g_assert_cmpint (ext1.height, ==, ext2.height); + + pango_layout_set_text (layout, "Pg", 1); + + pango_layout_get_extents (layout, NULL, &ext3); + + g_assert_cmpint (ext2.height, ==, ext3.height); + + g_object_unref (layout); + } + + g_object_unref (context); +} + int main (int argc, char *argv[]) { @@ -514,6 +554,7 @@ main (int argc, char *argv[]) g_test_add_func ("/bidi/get-cursor", test_get_cursor); g_test_add_func ("/layout/index-to-x", test_index_to_x); g_test_add_func ("/layout/extents", test_extents); + g_test_add_func ("/layout/empty-line-height", test_empty_line_height); return g_test_run (); } -- cgit v1.2.1