[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
This commit is contained in:
Behdad Esfahbod committed 2026-03-14 13:25:34 -03:00
1 parent 03006b06ba
commit 2b93ac44e1
1 file changed
+32
+32
View File
@@ -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 */