[ft] Allow font queries from color paint callbacks (#6490)

Release the face lock around solid, gradient, and custom palette
callbacks. Keep lazy FreeType color-stop reads locked and give each
getter call its own iterator, so callbacks can query the same font
without deadlocking or disturbing another stop query.

Snapshot the selected palette once per COLRv1 traversal, shared by
the bounds and paint passes. Nested paints can select a different
palette on the face; they must not change the outer paint's colors.
Return failure if the palette copy cannot be allocated. Stops remain
lazy and no cache policy or limits change.

Add a bounded callback regression with the existing COLRv1 fixture.
It queries font extents, performs nested paints with another palette,
and checks repeated and paged stop reads against the OT backend.
The original code times out; unlocking without copying the palette
fails the color comparison. The full Meson suite passes: 274 tests, no failures.

Reported by Anthropic's OSS Scanner as ANT-2026-YPLDSFHA.

Assisted-by: OpenAI Codex
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-10-30 14:52:35 -06:00
1 parent f9fcba7272
commit c34cf564b1
2 files changed
+233 -67

No files matched your search

+61 -67
View File
@@ -152,6 +152,45 @@ struct hb_ft_paint_context_t
int edge_count = HB_MAX_GRAPH_EDGE_COUNT;
};
static void
_hb_ft_get_solid_color (hb_ft_paint_context_t *c,
const FT_ColorIndex &color_index,
hb_bool_t *is_foreground,
hb_color_t *color)
{
*is_foreground = color_index.palette_index == 0xFFFF;
if (*is_foreground)
{
*color = HB_COLOR (hb_color_get_blue (c->foreground),
hb_color_get_green (c->foreground),
hb_color_get_red (c->foreground),
_hb_ft_color_alpha (hb_color_get_alpha (c->foreground),
color_index.alpha));
return;
}
c->ft_font->lock.unlock ();
bool custom = c->funcs->custom_palette_color (c->data, color_index.palette_index, color);
c->ft_font->lock.lock ();
if (custom)
{
*color = HB_COLOR (hb_color_get_blue (*color),
hb_color_get_green (*color),
hb_color_get_red (*color),
_hb_ft_color_alpha (hb_color_get_alpha (*color),
color_index.alpha));
return;
}
FT_Color ft_color = c->palette ? c->palette[color_index.palette_index] : FT_Color {};
*color = HB_COLOR (ft_color.blue,
ft_color.green,
ft_color.red,
_hb_ft_color_alpha (ft_color.alpha,
color_index.alpha));
}
static unsigned
_hb_ft_color_line_get_color_stops (hb_color_line_t *color_line,
void *color_line_data,
@@ -160,11 +199,15 @@ _hb_ft_color_line_get_color_stops (hb_color_line_t *color_line,
hb_color_stop_t *color_stops,
void *user_data)
{
FT_ColorLine *cl = (FT_ColorLine *) color_line_data;
/* Keep iteration local: a palette callback can query this color line again. */
FT_ColorLine line = *(FT_ColorLine *) color_line_data;
FT_ColorLine *cl = &line;
hb_ft_paint_context_t *c = (hb_ft_paint_context_t *) user_data;
if (count && color_stops)
{
/* Gradient callbacks run without the face lock. */
hb_lock_t lock (c->ft_font->lock);
FT_ColorStop stop;
unsigned wrote = 0;
FT_ColorStopIterator iter = cl->color_stop_iterator;
@@ -202,36 +245,8 @@ _hb_ft_color_line_get_color_stops (hb_color_line_t *color_line,
else
color_stops->offset = stop.stop_offset / 65536.f;
color_stops->is_foreground = stop.color.palette_index == 0xFFFF;
if (color_stops->is_foreground)
color_stops->color = HB_COLOR (hb_color_get_blue (c->foreground),
hb_color_get_green (c->foreground),
hb_color_get_red (c->foreground),
_hb_ft_color_alpha (hb_color_get_alpha (c->foreground),
stop.color.alpha));
else
{
hb_color_t color;
if (c->funcs->custom_palette_color (c->data, stop.color.palette_index, &color))
{
color_stops->color = HB_COLOR (hb_color_get_blue (color),
hb_color_get_green (color),
hb_color_get_red (color),
_hb_ft_color_alpha (hb_color_get_alpha (color),
stop.color.alpha));
}
else if (c->palette)
{
FT_Color ft_color = c->palette[stop.color.palette_index];
color_stops->color = HB_COLOR (ft_color.blue,
ft_color.green,
ft_color.red,
_hb_ft_color_alpha (ft_color.alpha,
stop.color.alpha));
}
else
color_stops->color = HB_COLOR (0, 0, 0, 0);
}
_hb_ft_get_solid_color (c, stop.color,
&color_stops->is_foreground, &color_stops->color);
color_stops++;
wrote++;
@@ -260,42 +275,6 @@ _hb_ft_color_line_get_extend (hb_color_line_t *color_line,
}
}
static void
_hb_ft_get_solid_color (hb_ft_paint_context_t *c,
const FT_ColorIndex &color_index,
hb_bool_t *is_foreground,
hb_color_t *color)
{
*is_foreground = color_index.palette_index == 0xFFFF;
if (*is_foreground)
{
*color = HB_COLOR (hb_color_get_blue (c->foreground),
hb_color_get_green (c->foreground),
hb_color_get_red (c->foreground),
_hb_ft_color_alpha (hb_color_get_alpha (c->foreground),
color_index.alpha));
return;
}
if (c->funcs->custom_palette_color (c->data, color_index.palette_index, color))
{
*color = HB_COLOR (hb_color_get_blue (*color),
hb_color_get_green (*color),
hb_color_get_red (*color),
_hb_ft_color_alpha (hb_color_get_alpha (*color),
color_index.alpha));
return;
}
FT_Color ft_color = c->palette[color_index.palette_index];
*color = HB_COLOR (ft_color.blue,
ft_color.green,
ft_color.red,
_hb_ft_color_alpha (ft_color.alpha,
color_index.alpha));
}
void
_hb_ft_paint (hb_ft_paint_context_t *c,
FT_OpaquePaint opaque_paint)
@@ -329,7 +308,9 @@ _hb_ft_paint (hb_ft_paint_context_t *c,
hb_bool_t is_foreground;
hb_color_t color;
_hb_ft_get_solid_color (c, paint.u.solid.color, &is_foreground, &color);
c->ft_font->lock.unlock ();
c->funcs->color (c->data, is_foreground, color);
c->ft_font->lock.lock ();
}
break;
case FT_COLR_PAINTFORMAT_LINEAR_GRADIENT:
@@ -340,6 +321,7 @@ _hb_ft_paint (hb_ft_paint_context_t *c,
_hb_ft_color_line_get_extend, nullptr
};
c->ft_font->lock.unlock ();
c->funcs->linear_gradient (c->data, &cl,
paint.u.linear_gradient.p0.x / 65536.f,
paint.u.linear_gradient.p0.y / 65536.f,
@@ -347,6 +329,7 @@ _hb_ft_paint (hb_ft_paint_context_t *c,
paint.u.linear_gradient.p1.y / 65536.f,
paint.u.linear_gradient.p2.x / 65536.f,
paint.u.linear_gradient.p2.y / 65536.f);
c->ft_font->lock.lock ();
}
break;
case FT_COLR_PAINTFORMAT_RADIAL_GRADIENT:
@@ -357,6 +340,7 @@ _hb_ft_paint (hb_ft_paint_context_t *c,
_hb_ft_color_line_get_extend, nullptr
};
c->ft_font->lock.unlock ();
c->funcs->radial_gradient (c->data, &cl,
paint.u.radial_gradient.c0.x / 65536.f,
paint.u.radial_gradient.c0.y / 65536.f,
@@ -364,6 +348,7 @@ _hb_ft_paint (hb_ft_paint_context_t *c,
paint.u.radial_gradient.c1.x / 65536.f,
paint.u.radial_gradient.c1.y / 65536.f,
paint.u.radial_gradient.r1 / 65536.f);
c->ft_font->lock.lock ();
}
break;
case FT_COLR_PAINTFORMAT_SWEEP_GRADIENT:
@@ -374,11 +359,13 @@ _hb_ft_paint (hb_ft_paint_context_t *c,
_hb_ft_color_line_get_extend, nullptr
};
c->ft_font->lock.unlock ();
c->funcs->sweep_gradient (c->data, &cl,
paint.u.sweep_gradient.center.x / 65536.f,
paint.u.sweep_gradient.center.y / 65536.f,
(paint.u.sweep_gradient.start_angle / 65536.f + 1) * HB_PI,
(paint.u.sweep_gradient.end_angle / 65536.f + 1) * HB_PI);
c->ft_font->lock.lock ();
}
break;
case FT_COLR_PAINTFORMAT_GLYPH:
@@ -572,6 +559,13 @@ hb_ft_paint_glyph_colr (hb_font_t *font,
FT_COLOR_NO_ROOT_TRANSFORM,
&paint))
{
/* Callbacks may select another palette on the same face. */
hb_vector_t<FT_Color> palette_copy;
palette_copy.extend (palette_array);
if (unlikely (palette_copy.in_error ()))
return false;
palette_array = palette_copy.as_array ();
hb_ft_paint_context_t c (ft_font, font,
paint_funcs, paint_data,
palette_array, palette_index, foreground);
+172
View File
@@ -691,6 +691,177 @@ test_color_stops_ft (void)
}
typedef struct
{
hb_font_t *font;
hb_paint_funcs_t *empty_funcs;
GString *colors;
unsigned int gradients[3];
unsigned int palette_calls;
hb_bool_t reenter;
} reentrant_paint_t;
static void
reentrant_query_font (reentrant_paint_t *data)
{
if (data->reenter)
{
hb_glyph_extents_t extents;
hb_font_get_glyph_extents (data->font, 8, &extents);
}
}
static hb_bool_t
reentrant_palette (hb_paint_funcs_t *funcs HB_UNUSED,
void *paint_data,
unsigned int color_index HB_UNUSED,
hb_color_t *color HB_UNUSED,
void *user_data HB_UNUSED)
{
reentrant_paint_t *data = paint_data;
reentrant_query_font (data);
if (data->reenter)
/* A nested paint selects a different FreeType palette. */
hb_font_paint_glyph (data->font, 8, data->empty_funcs, NULL, 1,
HB_COLOR (0, 0, 0, 255));
data->palette_calls++;
return FALSE;
}
static void
reentrant_color (hb_paint_funcs_t *funcs HB_UNUSED,
void *paint_data,
hb_bool_t is_foreground,
hb_color_t color,
void *user_data HB_UNUSED)
{
reentrant_paint_t *data = paint_data;
reentrant_query_font (data);
g_string_append_printf (data->colors, "c %u %u;", is_foreground, color);
}
static void
reentrant_gradient (reentrant_paint_t *data, hb_color_line_t *line, unsigned int kind)
{
unsigned int total = hb_color_line_get_color_stops (line, 0, NULL, NULL);
hb_color_stop_t *stops = g_new0 (hb_color_stop_t, total);
hb_color_stop_t *again = g_new0 (hb_color_stop_t, total);
unsigned int count = total;
reentrant_query_font (data);
hb_color_line_get_color_stops (line, 0, &count, stops);
g_assert_cmpuint (count, ==, total);
reentrant_query_font (data);
count = total;
hb_color_line_get_color_stops (line, 0, &count, again);
g_assert_cmpuint (count, ==, total);
for (unsigned int i = 0; i < total; i++)
{
g_assert_cmpfloat (stops[i].offset, ==, again[i].offset);
g_assert_cmpuint (stops[i].color, ==, again[i].color);
g_assert_cmpuint (stops[i].is_foreground, ==, again[i].is_foreground);
count = 1;
hb_color_line_get_color_stops (line, i, &count, again);
g_assert_cmpuint (count, ==, 1);
g_assert_cmpuint (stops[i].color, ==, again[0].color);
g_string_append_printf (data->colors, "g%u %u %u;", kind,
stops[i].is_foreground, stops[i].color);
}
data->gradients[kind]++;
g_free (again);
g_free (stops);
}
static void
reentrant_linear (hb_paint_funcs_t *funcs HB_UNUSED, void *paint_data,
hb_color_line_t *line,
float x0 HB_UNUSED, float y0 HB_UNUSED,
float x1 HB_UNUSED, float y1 HB_UNUSED,
float x2 HB_UNUSED, float y2 HB_UNUSED,
void *user_data HB_UNUSED)
{
reentrant_gradient (paint_data, line, 0);
}
static void
reentrant_radial (hb_paint_funcs_t *funcs HB_UNUSED, void *paint_data,
hb_color_line_t *line,
float x0 HB_UNUSED, float y0 HB_UNUSED, float r0 HB_UNUSED,
float x1 HB_UNUSED, float y1 HB_UNUSED, float r1 HB_UNUSED,
void *user_data HB_UNUSED)
{
reentrant_gradient (paint_data, line, 1);
}
static void
reentrant_sweep (hb_paint_funcs_t *funcs HB_UNUSED, void *paint_data,
hb_color_line_t *line,
float x0 HB_UNUSED, float y0 HB_UNUSED,
float start HB_UNUSED, float end HB_UNUSED,
void *user_data HB_UNUSED)
{
reentrant_gradient (paint_data, line, 2);
}
static void
test_ft_reentrant_paint (void)
{
if (!have_ft_colrv1 ())
{
g_test_skip ("FreeType COLRv1 support not present");
return;
}
#if GLIB_CHECK_VERSION(2,38,0)
if (!g_test_subprocess ())
{
g_test_trap_subprocess (NULL, 5 * G_USEC_PER_SEC, 0);
g_test_trap_assert_passed ();
return;
}
#endif
static const hb_codepoint_t glyphs[] = {8, 12, 94, 116, 177};
hb_face_t *face = hb_test_open_font_file (TEST_GLYPHS);
hb_font_t *font = hb_font_create (face);
hb_paint_funcs_t *funcs = hb_paint_funcs_create ();
hb_paint_funcs_t *empty_funcs = hb_paint_funcs_create ();
reentrant_paint_t data = {font, empty_funcs, g_string_new (""), {0, 0, 0}, 0, FALSE};
hb_paint_funcs_set_color_func (funcs, reentrant_color, NULL, NULL);
hb_paint_funcs_set_custom_palette_color_func (funcs, reentrant_palette, NULL, NULL);
hb_paint_funcs_set_linear_gradient_func (funcs, reentrant_linear, NULL, NULL);
hb_paint_funcs_set_radial_gradient_func (funcs, reentrant_radial, NULL, NULL);
hb_paint_funcs_set_sweep_gradient_func (funcs, reentrant_sweep, NULL, NULL);
/* Get reference colors before enabling font reentry and nested paints. */
for (unsigned int i = 0; i < G_N_ELEMENTS (glyphs); i++)
hb_font_paint_glyph (font, glyphs[i], funcs, &data, 0, HB_COLOR (0, 0, 0, 255));
char *expected = g_strdup (data.colors->str);
g_string_truncate (data.colors, 0);
data.reenter = TRUE;
data.palette_calls = 0;
#ifdef HB_HAS_FREETYPE
hb_ft_font_set_funcs (font);
#endif
for (unsigned int i = 0; i < G_N_ELEMENTS (glyphs); i++)
hb_font_paint_glyph (font, glyphs[i], funcs, &data, 0, HB_COLOR (0, 0, 0, 255));
g_assert_cmpstr (data.colors->str, ==, expected);
g_assert_cmpuint (data.palette_calls, >, 0);
for (unsigned int i = 0; i < 3; i++)
g_assert_cmpuint (data.gradients[i], >, 0);
g_free (expected);
g_string_free (data.colors, TRUE);
hb_paint_funcs_destroy (empty_funcs);
hb_paint_funcs_destroy (funcs);
hb_font_destroy (font);
hb_face_destroy (face);
}
/* ── Arbitrary-path clip API (push_clip_path_start/_end) ─────────── */
static void
@@ -1021,6 +1192,7 @@ main (int argc, char **argv)
hb_test_add (test_color_stops_ot);
hb_test_add (test_color_stops_ft);
hb_test_add (test_ft_reentrant_paint);
status = hb_test_run();