From 8f735c05cd0338ffebdd8ca40453cbaedc5fe9ec Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 16 Apr 2026 03:30:18 -0600 Subject: [PATCH] [draw] Normalize w/h in hb_draw_rect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Negative w/h are documented as supported but the stroked variant's math (outer grows by stroke_width, inner shrinks by stroke_width) silently inverts — outer ends up smaller than inner, the ring becomes a filled outer rect, and arbitrary-path clip users (who feed raw font glyph extents with y_bearing-style negative height) see the clip fill everything instead of just the stroke ring. Normalize w/h to non-negative at the top of hb_draw_rect so the rest of the stroke math produces the expected outer-contains-inner ring. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/hb-draw.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/hb-draw.cc b/src/hb-draw.cc index 1dc14ba90..c17398e70 100644 --- a/src/hb-draw.cc +++ b/src/hb-draw.cc @@ -596,6 +596,12 @@ hb_draw_rect (hb_draw_funcs_t *dfuncs, void *draw_data, if (stroke_width <= 0.f || !std::isfinite (stroke_width)) return; + + /* Normalize to non-negative width/height so the stroke math + * below (outer grows by sw, inner shrinks by sw) produces the + * expected outer-contains-inner ring regardless of w/h signs. */ + if (w < 0.f) { x += w; w = -w; } + if (h < 0.f) { y += h; h = -h; } /* w or h == 0 is still meaningful when stroking: a stroked * zero-height rect is a horizontal line of length w; zero * width is a vertical line. Both degenerate to a single