Merge pull request #6660 from harfbuzz/codex/monster-font-budget-headroom

[raster] Crop paint masks to their visible destination
diff --git a/src/hb-raster-draw.cc b/src/hb-raster-draw.cc
index 2687855..38d4ff9 100644
--- a/src/hb-raster-draw.cc
+++ b/src/hb-raster-draw.cc
@@ -540,6 +540,11 @@
     max_rows = draw->fixed_extents.height;
     max_cols = draw->fixed_extents.width;
   }
+  else if (draw->has_clip_box)
+  {
+    max_rows = hb_max ((int64_t) 0, hb_clamp_to<int64_t> (ceil ((double) draw->clip_y1) - floor ((double) draw->clip_y0)));
+    max_cols = hb_max ((int64_t) 0, hb_clamp_to<int64_t> (ceil ((double) draw->clip_x1) - floor ((double) draw->clip_x0)));
+  }
 
   int64_t work = 0;
   for (const auto &edge : draw->edges)
@@ -1666,10 +1671,21 @@
       int x1 = (int) (((int64_t) xmax + HB_RASTER_PIXEL_MASK) >> HB_RASTER_PIXEL_BITS);
       int y1 = (int) (((int64_t) ymax + HB_RASTER_PIXEL_MASK) >> HB_RASTER_PIXEL_BITS);
 
+      /* Paint masks only need the part visible on their destination.
+       * Clip before the dimension cap, so a large off-surface outline
+       * neither allocates a huge mask nor loses its visible portion. */
+      if (draw->has_clip_box)
+      {
+	x0 = hb_max (x0, hb_clamp_to<int> (floorf (draw->clip_x0)));
+	y0 = hb_max (y0, hb_clamp_to<int> (floorf (draw->clip_y0)));
+	x1 = hb_min (x1, hb_clamp_to<int> (ceilf (draw->clip_x1)));
+	y1 = hb_min (y1, hb_clamp_to<int> (ceilf (draw->clip_y1)));
+      }
+
       ext.x_origin = x0;
       ext.y_origin = y0;
-      ext.width    = (unsigned) hb_min (hb_max (0, x1 - x0), HB_RASTER_MAX_AUTO_DIMENSION);
-      ext.height   = (unsigned) hb_min (hb_max (0, y1 - y0), HB_RASTER_MAX_AUTO_DIMENSION);
+      ext.width    = (unsigned) hb_min (hb_max ((int64_t) 0, (int64_t) x1 - x0), (int64_t) HB_RASTER_MAX_AUTO_DIMENSION);
+      ext.height   = (unsigned) hb_min (hb_max ((int64_t) 0, (int64_t) y1 - y0), (int64_t) HB_RASTER_MAX_AUTO_DIMENSION);
       ext.stride   = 0; /* filled below */
     }
   }
diff --git a/src/test-raster-paint.cc b/src/test-raster-paint.cc
index 7634642..fff8266 100644
--- a/src/test-raster-paint.cc
+++ b/src/test-raster-paint.cc
@@ -56,6 +56,35 @@
   hb_always_assert (hb_paint_set_budget (funcs, paint, HB_BUDGET_DEFAULT));
   hb_always_assert (paint->pixel_remaining == initial_budget);
 
+  /* An outline can surround the small visible surface while extending far
+   * past the automatic dimension cap.  Rasterize its visible part: a mask
+   * starting at -10000 and truncated to 4096 pixels misses the surface. */
+  hb_raster_paint_clear (paint);
+  hb_raster_paint_set_extents (paint, &root);
+  void *draw_data = nullptr;
+  hb_draw_funcs_t *draw_funcs = hb_paint_push_clip_path_start (funcs, paint, &draw_data);
+  hb_always_assert (draw_funcs);
+  hb_draw_state_t state = HB_DRAW_STATE_DEFAULT;
+  hb_draw_move_to (draw_funcs, draw_data, &state, -10000.f, -10000.f);
+  hb_draw_line_to (draw_funcs, draw_data, &state, 10000.f, -10000.f);
+  hb_draw_line_to (draw_funcs, draw_data, &state, 10000.f, 10000.f);
+  hb_draw_line_to (draw_funcs, draw_data, &state, -10000.f, 10000.f);
+  hb_draw_close_path (draw_funcs, draw_data, &state);
+  hb_paint_push_clip_path_end (funcs, paint);
+  hb_paint_color (funcs, paint, false, HB_COLOR (255, 255, 255, 255));
+  hb_paint_pop_clip (funcs, paint);
+  hb_raster_image_t *image = hb_raster_paint_render (paint);
+  hb_always_assert (image);
+  hb_raster_extents_t extents;
+  hb_raster_image_get_extents (image, &extents);
+  hb_always_assert (extents.width == root.width && extents.height == root.height);
+  const uint8_t *pixels = hb_raster_image_get_buffer (image);
+  for (unsigned y = 0; y < root.height; y++)
+    for (unsigned x = 0; x < root.width * 4; x++)
+      hb_always_assert (pixels[y * extents.stride + x] == 255);
+  hb_always_assert (paint->pixel_remaining >= 0);
+  hb_raster_image_destroy (image);
+
   hb_raster_paint_destroy (paint);
   return 0;
 }
diff --git a/test/api/test-raster.cc b/test/api/test-raster.cc
index 4606836..3e3ac2a 100644
--- a/test/api/test-raster.cc
+++ b/test/api/test-raster.cc
@@ -804,7 +804,8 @@
   hb_paint_pop_clip (funcs, paint);
   img = hb_raster_paint_render (paint);
   g_assert_nonnull (img);
-  g_assert_cmpuint (hb_raster_image_get_buffer (img)[3], ==, 0);
+  /* Paint masks crop the same edges to the visible one-pixel surface. */
+  g_assert_cmpuint (hb_raster_image_get_buffer (img)[3], ==, 255);
   hb_raster_image_destroy (img);
   hb_raster_paint_destroy (paint);
 }