From 7612ec44dc5bc319e211aee1ec93741ca5f19a42 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 23 Mar 2025 13:10:12 -0600 Subject: [PATCH] [util] Show glyph extents only if available --- src/hb-buffer-serialize.cc | 16 +++++++++------- util/view-cairo.hh | 14 ++++++++------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/hb-buffer-serialize.cc b/src/hb-buffer-serialize.cc index de2c99d98..b71aa7219 100644 --- a/src/hb-buffer-serialize.cc +++ b/src/hb-buffer-serialize.cc @@ -169,11 +169,13 @@ _hb_buffer_serialize_glyphs_json (hb_buffer_t *buffer, if (flags & HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS) { hb_glyph_extents_t extents; - hb_font_get_glyph_extents(font, info[i].codepoint, &extents); - p += hb_max (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"xb\":%d,\"yb\":%d", - extents.x_bearing, extents.y_bearing)); - p += hb_max (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"w\":%d,\"h\":%d", - extents.width, extents.height)); + if (hb_font_get_glyph_extents(font, info[i].codepoint, &extents)) + { + p += hb_max (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"xb\":%d,\"yb\":%d", + extents.x_bearing, extents.y_bearing)); + p += hb_max (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"w\":%d,\"h\":%d", + extents.width, extents.height)); + } } *p++ = '}'; @@ -318,8 +320,8 @@ _hb_buffer_serialize_glyphs_text (hb_buffer_t *buffer, if (flags & HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS) { hb_glyph_extents_t extents; - hb_font_get_glyph_extents(font, info[i].codepoint, &extents); - p += hb_max (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "<%d,%d,%d,%d>", extents.x_bearing, extents.y_bearing, extents.width, extents.height)); + if (hb_font_get_glyph_extents(font, info[i].codepoint, &extents)) + p += hb_max (0, snprintf (p, ARRAY_LENGTH (b) - (p - b), "<%d,%d,%d,%d>", extents.x_bearing, extents.y_bearing, extents.width, extents.height)); } if (i == end-1) { diff --git a/util/view-cairo.hh b/util/view-cairo.hh index c96003c1f..b8f034a48 100644 --- a/util/view-cairo.hh +++ b/util/view-cairo.hh @@ -189,13 +189,15 @@ view_cairo_t::render (const font_options_t *font_opts) for (unsigned i = 0; i < l.num_glyphs; i++) { hb_glyph_extents_t hb_extents; - hb_font_get_glyph_extents (font, l.glyphs[i].index, &hb_extents); - double x1 = scalbn ((double) hb_extents.x_bearing, - (int) subpixel_bits); - double y1 = -scalbn ((double) hb_extents.y_bearing, - (int) subpixel_bits); - double width = scalbn ((double) hb_extents.width, - (int) subpixel_bits); - double height = -scalbn ((double) hb_extents.height, - (int) subpixel_bits); + if (hb_font_get_glyph_extents (font, l.glyphs[i].index, &hb_extents)) + { + double x1 = scalbn ((double) hb_extents.x_bearing, - (int) subpixel_bits); + double y1 = -scalbn ((double) hb_extents.y_bearing, - (int) subpixel_bits); + double width = scalbn ((double) hb_extents.width, - (int) subpixel_bits); + double height = -scalbn ((double) hb_extents.height, - (int) subpixel_bits); - cairo_rectangle (cr, l.glyphs[i].x + x1, l.glyphs[i].y + y1, width, height); + cairo_rectangle (cr, l.glyphs[i].x + x1, l.glyphs[i].y + y1, width, height); + } } cairo_stroke (cr);