[paint] Add hb_paint_push_group_for() API

Add a new push_group variant that receives the compositing mode
upfront. This lets backends like PDF emit blend-mode state before
painting the group content, avoiding the need to buffer and
retroactively wrap.

The default (nil) implementation calls hb_paint_push_group(), so
existing backends are unaffected.

Callers updated:
- COLR PaintComposite now calls push_group_for() for both groups
- Rust/fontations push_layer passes the mode through
- hb-raster SVG context accepts mode parameter (default SRC_OVER)

All 233 tests pass.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-07 20:12:59 -06:00
1 parent 39f622285e
commit 1141e97ef4
7 files changed
+86 -6

No files matched your search

+3
View File
@@ -291,6 +291,8 @@ hb_paint_funcs_set_sweep_gradient_func
hb_paint_composite_mode_t
hb_paint_push_group_func_t
hb_paint_funcs_set_push_group_func
hb_paint_push_group_for_func_t
hb_paint_funcs_set_push_group_for_func
hb_paint_pop_group_func_t
hb_paint_funcs_set_pop_group_func
hb_paint_custom_palette_color_func_t
@@ -310,6 +312,7 @@ hb_paint_linear_gradient
hb_paint_radial_gradient
hb_paint_sweep_gradient
hb_paint_push_group
hb_paint_push_group_for
hb_paint_pop_group
hb_paint_custom_palette_color
</SECTION>
+2 -2
View File
@@ -1526,9 +1526,9 @@ struct PaintComposite
void paint_glyph (hb_paint_context_t *c) const
{
TRACE_PAINT (this);
c->funcs->push_group (c->data);
c->funcs->push_group_for (c->data, HB_PAINT_COMPOSITE_MODE_SRC_OVER);
c->recurse (this+backdrop);
c->funcs->push_group (c->data);
c->funcs->push_group_for (c->data, (hb_paint_composite_mode_t) (int) mode);
c->recurse (this+src);
c->funcs->pop_group (c->data, (hb_paint_composite_mode_t) (int) mode);
c->funcs->pop_group (c->data, HB_PAINT_COMPOSITE_MODE_SRC_OVER);
+27
View File
@@ -118,6 +118,14 @@ static void
hb_paint_push_group_nil (hb_paint_funcs_t *funcs, void *paint_data,
void *user_data) {}
static void
hb_paint_push_group_for_nil (hb_paint_funcs_t *funcs, void *paint_data,
hb_paint_composite_mode_t mode,
void *user_data)
{
hb_paint_push_group (funcs, paint_data);
}
static void
hb_paint_pop_group_nil (hb_paint_funcs_t *funcs, void *paint_data,
hb_paint_composite_mode_t mode,
@@ -723,6 +731,25 @@ hb_paint_push_group (hb_paint_funcs_t *funcs, void *paint_data)
funcs->push_group (paint_data);
}
/**
* hb_paint_push_group_for:
* @funcs: paint functions
* @paint_data: associated data passed by the caller
* @mode: the compositing mode that will be used when the group is popped
*
* Perform a "push-group" paint operation, with the compositing
* mode known in advance. By default, this calls
* hb_paint_push_group().
*
* XSince: REPLACEME
*/
void
hb_paint_push_group_for (hb_paint_funcs_t *funcs, void *paint_data,
hb_paint_composite_mode_t mode)
{
funcs->push_group_for (paint_data, mode);
}
/**
* hb_paint_pop_group:
* @funcs: paint functions
+43
View File
@@ -660,6 +660,28 @@ typedef void (*hb_paint_push_group_func_t) (hb_paint_funcs_t *funcs,
void *paint_data,
void *user_data);
/**
* hb_paint_push_group_for_func_t:
* @funcs: paint functions object
* @paint_data: The data accompanying the paint functions in hb_font_paint_glyph()
* @mode: the compositing mode that will be used when the group is popped
* @user_data: User data pointer passed to hb_paint_funcs_set_push_group_for_func()
*
* A virtual method for the #hb_paint_funcs_t to use
* an intermediate surface for subsequent paint calls,
* with the compositing mode known in advance.
*
* This is like #hb_paint_push_group_func_t, but the
* compositing mode is provided at push time. By default
* this calls #hb_paint_push_group_func_t.
*
* XSince: REPLACEME
*/
typedef void (*hb_paint_push_group_for_func_t) (hb_paint_funcs_t *funcs,
void *paint_data,
hb_paint_composite_mode_t mode,
void *user_data);
/**
* hb_paint_pop_group_func_t:
* @funcs: paint functions object
@@ -916,6 +938,23 @@ hb_paint_funcs_set_push_group_func (hb_paint_funcs_t *funcs,
void *user_data,
hb_destroy_func_t destroy);
/**
* hb_paint_funcs_set_push_group_for_func:
* @funcs: A paint functions struct
* @func: (closure user_data) (destroy destroy) (scope notified): The push-group-for callback
* @user_data: Data to pass to @func
* @destroy: (nullable): Function to call when @user_data is no longer needed
*
* Sets the push-group-for callback on the paint functions struct.
*
* XSince: REPLACEME
*/
HB_EXTERN void
hb_paint_funcs_set_push_group_for_func (hb_paint_funcs_t *funcs,
hb_paint_push_group_for_func_t func,
void *user_data,
hb_destroy_func_t destroy);
/**
* hb_paint_funcs_set_pop_group_func:
* @funcs: A paint functions struct
@@ -1026,6 +1065,10 @@ hb_paint_sweep_gradient (hb_paint_funcs_t *funcs, void *paint_data,
HB_EXTERN void
hb_paint_push_group (hb_paint_funcs_t *funcs, void *paint_data);
HB_EXTERN void
hb_paint_push_group_for (hb_paint_funcs_t *funcs, void *paint_data,
hb_paint_composite_mode_t mode);
HB_EXTERN void
hb_paint_pop_group (hb_paint_funcs_t *funcs, void *paint_data,
hb_paint_composite_mode_t mode);
+6
View File
@@ -43,6 +43,7 @@
HB_PAINT_FUNC_IMPLEMENT (radial_gradient) \
HB_PAINT_FUNC_IMPLEMENT (sweep_gradient) \
HB_PAINT_FUNC_IMPLEMENT (push_group) \
HB_PAINT_FUNC_IMPLEMENT (push_group_for) \
HB_PAINT_FUNC_IMPLEMENT (pop_group) \
HB_PAINT_FUNC_IMPLEMENT (custom_palette_color) \
/* ^--- Add new callbacks here */
@@ -146,6 +147,11 @@ struct hb_paint_funcs_t
void push_group (void *paint_data)
{ func.push_group (this, paint_data,
!user_data ? nullptr : user_data->push_group); }
void push_group_for (void *paint_data,
hb_paint_composite_mode_t mode)
{ func.push_group_for (this, paint_data,
mode,
!user_data ? nullptr : user_data->push_group_for); }
void pop_group (void *paint_data,
hb_paint_composite_mode_t mode)
{ func.pop_group (this, paint_data,
+2 -2
View File
@@ -85,10 +85,10 @@ struct hb_svg_render_context_t
{
hb_paint_pop_transform (pfuncs, paint);
}
bool push_group ()
bool push_group (hb_paint_composite_mode_t mode = HB_PAINT_COMPOSITE_MODE_SRC_OVER)
{
unsigned depth = paint->surface_stack.length;
hb_paint_push_group (pfuncs, paint);
hb_paint_push_group_for (pfuncs, paint, mode);
return paint->surface_stack.length > depth;
}
void pop_group (hb_paint_composite_mode_t mode)
+3 -2
View File
@@ -879,9 +879,10 @@ impl ColorPainter for HbColorPainter<'_> {
}
}
}
fn push_layer(&mut self, _mode: CompositeMode) {
fn push_layer(&mut self, mode: CompositeMode) {
let mode = mode as hb_paint_composite_mode_t;
unsafe {
hb_paint_push_group(self.paint_funcs, self.paint_data);
hb_paint_push_group_for(self.paint_funcs, self.paint_data, mode);
}
}
fn pop_layer_with_mode(&mut self, mode: CompositeMode) {