From 2b93ac44e1a7bdb13f3e7a790ed74ffbb1554e7b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 14 Mar 2026 13:25:34 -0300 Subject: [PATCH] [raster] Fast-path axis-aligned clip rectangles on masks hb_raster_paint_push_clip_rectangle() already has an O(1) fast path for axis-aligned rectangle clips when the existing clip is also a rectangle. If the existing clip is an alpha mask, the code fell back to the generic quad rasterization loop even though the new clip was still axis-aligned. That makes malformed COLR inputs spend large amounts of time in the per- pixel inside-quad test. Intersect axis-aligned rectangle clips with an existing alpha mask by copying only the intersecting row spans. Tested: - meson compile -C build hb-raster-fuzzer Assisted-by: OpenAI Codex --- src/hb-raster-paint.cc | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index a3e508e00..c053ff632 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -432,6 +432,38 @@ hb_raster_paint_push_clip_rectangle (hb_paint_funcs_t *pfuncs HB_UNUSED, new_clip.rect_y1 = hb_min (py1, old_clip.rect_y1); new_clip.update_bounds_from_rect (); } + else if (is_axis_aligned) + { + /* Fast path: intersect an axis-aligned rect with an existing alpha clip. */ + new_clip.is_rect = false; + size_t clip_size = (size_t) new_clip.stride * h; + if (unlikely (clip_size > HB_RASTER_MAX_BUFFER_SIZE || + !new_clip.alpha.resize ((unsigned) clip_size))) + { + hb_raster_paint_push_empty_clip (c, w, h); + return; + } + hb_memset (new_clip.alpha.arrayZ, 0, (unsigned) clip_size); + + unsigned iy0 = (unsigned) hb_max (py0, (int) old_clip.min_y); + unsigned iy1 = (unsigned) hb_min (py1, (int) old_clip.max_y); + unsigned ix0 = (unsigned) hb_max (px0, (int) old_clip.min_x); + unsigned ix1 = (unsigned) hb_min (px1, (int) old_clip.max_x); + + if (ix0 < ix1 && iy0 < iy1) + { + for (unsigned y = iy0; y < iy1; y++) + { + const uint8_t *old_row = old_clip.alpha.arrayZ + y * old_clip.stride; + uint8_t *new_row = new_clip.alpha.arrayZ + y * new_clip.stride; + hb_memcpy (new_row + ix0, old_row + ix0, ix1 - ix0); + } + new_clip.min_x = ix0; + new_clip.min_y = iy0; + new_clip.max_x = ix1; + new_clip.max_y = iy1; + } + } else { /* General case: rasterize transformed quad as alpha mask */