[font] Saturate synthetic bold and glyph origin arithmetic (#6585)

* [font] Saturate synthetic bold and glyph origin arithmetic

#6154 made hb_position_t arithmetic saturate and #6296 did the same
for synthetic_glyph_extents(), but the rest of hb_font_t still used
plain signed arithmetic:

- changed() computed the embolden strength with abs (x_scale), which
  overflows for INT32_MIN, and converted the float product to int
  without a range check. A strength of INT32_MIN could also not be
  negated later on, for negative scales.
- get_font_[hv]_extents(), the advance, origin and contour point
  getters added the strength with += and could overflow.
- add_offset(), subtract_offset() and apply_offset() overflow when
  they apply a large origin, and so do the origin fallbacks in
  apply_glyph_[hv]_origins_with_fallback().
- em_multf() converted its float result to hb_position_t without a
  range check, which is undefined for a value such as an ascender
  above the em at a scale close to INT32_MAX.

Use hb_saturate_add(), hb_saturate_sub() and hb_clamp_to() in these
places, and keep the strength away from INT32_MIN so that negating it
stays defined. Results that fit do not change.

The shift variables that were int are now hb_position_t, since the
saturating helpers deduce one type and int32_t is not int everywhere.

Tested with test-font under UBSan: the new tests fail on the base
commit at hb-font.hh:365, :848, :1154 and :1167, and pass here.

Assisted-by: Claude

* [test] Cover plural origins and synthetic slant saturation

Review feedback: the plural origin getters and the slant
clamp in get_glyph_contour_point were not covered by the
tests, so removing their saturation would still pass.

test_synthetic_bold_saturates now also calls
hb_font_get_glyph_h_origins and hb_font_get_glyph_v_origins
once each and checks both coordinates for INT32_MAX. A new
test_synthetic_slant_saturates sets a slant of 1 on a contour
point one short of INT32_MAX in both axes and checks that x
saturates to INT32_MAX.

Verified by mutation: replacing the plural hb_saturate_add
calls with plain addition makes the first test fail (the
value wraps), and dropping the hb_clamp_to in the slant path
makes the second fail (x comes out as -2). Full
meson test -C build: 270 ok, 0 fail, 7 skipped
(subset+repack, missing repacker dependency; same as the
baseline run for this PR).

Assisted-by: Claude

* [font] Apply the synthetic slant shift before clamping

get_glyph_contour_point clamped the slant shift to
hb_position_t before adding it to x. When x and the shift
have opposite signs and partly cancel, clamping first
changes the result: a contour point at x = -2000000000,
y = 2000000000 with a slant of 2 should land at
x = 2000000000, but came out as 147483647, because the
shift was clamped to INT32_MAX before the addition.

Add the shift first and clamp the sum instead. The
existing saturation case is unchanged: hb_clamp_to
widens to double to do the clamp, so an out-of-range sum
still saturates instead of hitting an undefined
float-to-int conversion.

The new test_synthetic_slant_cancelling covers the
cancelling case. Verified by mutation: restoring the
clamp-first order makes it fail with 147483647, the
reported value. Full meson test -C build: 270 ok,
0 fail, 7 skipped, same as baseline.

Assisted-by: Claude
This commit is contained in:
Wei-Lun Li authored and GitHub committed 2026-11-09 11:47:15 -06:00
1 parent 4cbeba18e1
commit 24ded4335b
2 files changed
+299 -34

No files matched your search

+40 -34
View File
@@ -283,8 +283,8 @@ struct hb_font_t
if (synthetic && ret)
{
/* Embolden */
int y_shift = y_scale < 0 ? -y_strength : y_strength;
extents->ascender += y_shift;
hb_position_t y_shift = y_scale < 0 ? -y_strength : y_strength;
extents->ascender = hb_saturate_add (extents->ascender, y_shift);
}
return ret;
@@ -300,14 +300,14 @@ struct hb_font_t
if (synthetic && ret)
{
/* Embolden */
int x_shift = x_scale < 0 ? -x_strength : x_strength;
hb_position_t x_shift = x_scale < 0 ? -x_strength : x_strength;
if (embolden_in_place)
{
extents->ascender += x_shift / 2;
extents->descender -= x_shift - x_shift / 2;
extents->ascender = hb_saturate_add (extents->ascender, x_shift / 2);
extents->descender = hb_saturate_sub (extents->descender, x_shift - x_shift / 2);
}
else
extents->ascender += x_shift;
extents->ascender = hb_saturate_add (extents->ascender, x_shift);
}
return ret;
@@ -362,7 +362,7 @@ struct hb_font_t
{
/* Embolden */
hb_position_t strength = x_scale >= 0 ? x_strength : -x_strength;
advance += advance ? strength : 0;
advance = hb_saturate_add (advance, advance ? strength : 0);
}
return advance;
@@ -379,7 +379,7 @@ struct hb_font_t
{
/* Embolden */
hb_position_t strength = y_scale >= 0 ? y_strength : -y_strength;
advance += advance ? strength : 0;
advance = hb_saturate_add (advance, advance ? strength : 0);
}
return advance;
@@ -404,7 +404,7 @@ struct hb_font_t
hb_position_t strength = x_scale >= 0 ? x_strength : -x_strength;
for (unsigned int i = 0; i < count; i++)
{
*first_advance += *first_advance ? strength : 0;
*first_advance = hb_saturate_add (*first_advance, *first_advance ? strength : 0);
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
}
}
@@ -429,7 +429,7 @@ struct hb_font_t
hb_position_t strength = y_scale >= 0 ? y_strength : -y_strength;
for (unsigned int i = 0; i < count; i++)
{
*first_advance += *first_advance ? strength : 0;
*first_advance = hb_saturate_add (*first_advance, *first_advance ? strength : 0);
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
}
}
@@ -451,8 +451,8 @@ struct hb_font_t
/* Embolden */
if (!embolden_in_place)
{
*x += x_scale < 0 ? -x_strength : x_strength;
*y += y_scale < 0 ? -y_strength : y_strength;
*x = hb_saturate_add (*x, x_scale < 0 ? -x_strength : x_strength);
*y = hb_saturate_add (*y, y_scale < 0 ? -y_strength : y_strength);
}
}
@@ -475,8 +475,8 @@ struct hb_font_t
/* Embolden */
if (!embolden_in_place)
{
*x += x_scale < 0 ? -x_strength : x_strength;
*y += y_scale < 0 ? -y_strength : y_strength;
*x = hb_saturate_add (*x, x_scale < 0 ? -x_strength : x_strength);
*y = hb_saturate_add (*y, y_scale < 0 ? -y_strength : y_strength);
}
}
@@ -510,8 +510,8 @@ struct hb_font_t
/* Embolden */
if (!embolden_in_place)
{
*first_x += x_shift;
*first_y += y_shift;
*first_x = hb_saturate_add (*first_x, x_shift);
*first_y = hb_saturate_add (*first_y, y_shift);
}
first_x = &StructAtOffsetUnaligned<hb_position_t> (first_x, x_stride);
first_y = &StructAtOffsetUnaligned<hb_position_t> (first_y, y_stride);
@@ -548,8 +548,8 @@ struct hb_font_t
/* Embolden */
if (!embolden_in_place)
{
*first_x += x_shift;
*first_y += y_shift;
*first_x = hb_saturate_add (*first_x, x_shift);
*first_y = hb_saturate_add (*first_y, y_shift);
}
first_x = &StructAtOffsetUnaligned<hb_position_t> (first_x, x_stride);
first_y = &StructAtOffsetUnaligned<hb_position_t> (first_y, y_stride);
@@ -653,13 +653,13 @@ struct hb_font_t
{
/* Slant */
if (slant_xy)
*x += roundf (*y * slant_xy);
*x = hb_clamp_to<hb_position_t> (*x + roundf (*y * slant_xy));
/* Embolden */
if (!embolden_in_place)
{
int x_shift = x_scale < 0 ? -x_strength : x_strength;
*x += x_shift;
hb_position_t x_shift = x_scale < 0 ? -x_strength : x_strength;
*x = hb_saturate_add (*x, x_shift);
}
}
@@ -839,20 +839,22 @@ struct hb_font_t
{
assert (mult == -1 || mult == +1);
*x += dx * mult;
*y += dy * mult;
if (mult > 0)
add_offset (x, y, dx, dy);
else
subtract_offset (x, y, dx, dy);
}
void add_offset (hb_position_t *x, hb_position_t *y,
hb_position_t dx, hb_position_t dy)
{
*x += dx;
*y += dy;
*x = hb_saturate_add (*x, dx);
*y = hb_saturate_add (*y, dy);
}
void subtract_offset (hb_position_t *x, hb_position_t *y,
hb_position_t dx, hb_position_t dy)
{
*x -= dx;
*y -= dy;
*x = hb_saturate_sub (*x, dx);
*y = hb_saturate_sub (*y, dy);
}
void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
@@ -899,8 +901,8 @@ struct hb_font_t
for (unsigned j = 0; j < n; j++)
{
hb_codepoint_t glyph = buf->info[offset + j].codepoint;
origins[j].x -= get_glyph_h_advance (glyph) / 2;
origins[j].y -= ascender;
origins[j].x = hb_saturate_sub (origins[j].x, get_glyph_h_advance (glyph) / 2);
origins[j].y = hb_saturate_sub (origins[j].y, ascender);
}
}
else
@@ -963,8 +965,8 @@ struct hb_font_t
for (unsigned j = 0; j < n; j++)
{
hb_codepoint_t glyph = buf->info[offset + j].codepoint;
origins[j].x += get_glyph_h_advance (glyph) / 2;
origins[j].y += ascender;
origins[j].x = hb_saturate_add (origins[j].x, get_glyph_h_advance (glyph) / 2);
origins[j].y = hb_saturate_add (origins[j].y, ascender);
}
}
else
@@ -1151,8 +1153,8 @@ struct hb_font_t
is_synthetic = x_embolden || y_embolden || slant;
x_strength = roundf (abs (x_scale) * x_embolden);
y_strength = roundf (abs (y_scale) * y_embolden);
x_strength = embolden_strength (x_scale, x_embolden);
y_strength = embolden_strength (y_scale, y_embolden);
slant_xy = y_scale ? slant * x_scale / y_scale : 0.f;
@@ -1161,10 +1163,14 @@ struct hb_font_t
serial++;
}
/* The strength is negated for negative scales, so keep it away from INT32_MIN. */
static int32_t embolden_strength (int32_t scale, float embolden)
{ return hb_max (hb_clamp_to<int32_t> (roundf (fabsf ((float) scale) * embolden)), -INT32_MAX); }
hb_position_t em_mult (int32_t v, int64_t mult)
{ return (hb_position_t) ((v * mult + 32768) >> 16); }
hb_position_t em_multf (float v, float mult)
{ return (hb_position_t) roundf (em_fmultf (v, mult)); }
{ return hb_clamp_to<hb_position_t> (roundf (em_fmultf (v, mult))); }
float em_fmultf (float v, float mult)
{ return v * mult; }
float em_fmult (int32_t v, float mult)
+259
View File
@@ -26,6 +26,8 @@
#include "hb-test.h"
#include <hb-ot.h>
#ifdef HAVE_FREETYPE
#include <hb-ft.h>
#endif
@@ -842,6 +844,257 @@ test_synthetic_glyph_extents_overflow (void)
hb_font_funcs_destroy (ffuncs);
}
static hb_position_t
_near_max_advance_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
hb_codepoint_t glyph HB_UNUSED,
void *user_data HB_UNUSED)
{
return INT32_MAX - 1;
}
static hb_bool_t
_near_max_origin_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
hb_codepoint_t glyph HB_UNUSED,
hb_position_t *x, hb_position_t *y,
void *user_data HB_UNUSED)
{
*x = *y = INT32_MAX - 1;
return TRUE;
}
static hb_bool_t
_near_max_contour_point_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
hb_codepoint_t glyph HB_UNUSED,
unsigned int point_index HB_UNUSED,
hb_position_t *x, hb_position_t *y,
void *user_data HB_UNUSED)
{
*x = *y = INT32_MAX - 1;
return TRUE;
}
static hb_bool_t
_near_max_font_extents_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
hb_font_extents_t *extents,
void *user_data HB_UNUSED)
{
extents->ascender = INT32_MAX - 1;
extents->descender = INT32_MIN + 1;
extents->line_gap = 0;
return TRUE;
}
static hb_font_funcs_t *
_near_max_font_funcs (void)
{
hb_font_funcs_t *ffuncs = hb_font_funcs_create ();
hb_font_funcs_set_glyph_h_advance_func (ffuncs, _near_max_advance_func, NULL, NULL);
hb_font_funcs_set_glyph_v_advance_func (ffuncs, _near_max_advance_func, NULL, NULL);
hb_font_funcs_set_glyph_h_origin_func (ffuncs, _near_max_origin_func, NULL, NULL);
hb_font_funcs_set_glyph_v_origin_func (ffuncs, _near_max_origin_func, NULL, NULL);
hb_font_funcs_set_glyph_contour_point_func (ffuncs, _near_max_contour_point_func, NULL, NULL);
hb_font_funcs_set_font_h_extents_func (ffuncs, _near_max_font_extents_func, NULL, NULL);
hb_font_funcs_set_font_v_extents_func (ffuncs, _near_max_font_extents_func, NULL, NULL);
return ffuncs;
}
static void
test_synthetic_bold_saturates (void)
{
hb_font_funcs_t *ffuncs = _near_max_font_funcs ();
hb_face_t *face = hb_face_create (hb_blob_get_empty (), 0);
hb_font_t *font = hb_font_create (face);
hb_font_set_funcs (font, ffuncs, NULL, NULL);
hb_font_set_scale (font, 2048, 2048);
hb_font_set_synthetic_bold (font, 0.05f, 0.05f, FALSE);
/* Every value is one short of INT32_MAX, and bold adds about 100 to it. */
g_assert_cmpint (hb_font_get_glyph_h_advance (font, 1), ==, INT32_MAX);
g_assert_cmpint (hb_font_get_glyph_v_advance (font, 1), ==, INT32_MAX);
hb_codepoint_t glyph = 1;
hb_position_t a = 0;
hb_font_get_glyph_h_advances (font, 1, &glyph, 0, &a, 0);
g_assert_cmpint (a, ==, INT32_MAX);
hb_font_get_glyph_v_advances (font, 1, &glyph, 0, &a, 0);
g_assert_cmpint (a, ==, INT32_MAX);
hb_position_t x = 0, y = 0;
g_assert_true (hb_font_get_glyph_h_origin (font, 1, &x, &y));
g_assert_cmpint (x, ==, INT32_MAX);
g_assert_cmpint (y, ==, INT32_MAX);
g_assert_true (hb_font_get_glyph_v_origin (font, 1, &x, &y));
g_assert_cmpint (x, ==, INT32_MAX);
g_assert_cmpint (y, ==, INT32_MAX);
hb_position_t xs = 0, ys = 0;
g_assert_true (hb_font_get_glyph_h_origins (font, 1, &glyph, 0, &xs, 0, &ys, 0));
g_assert_cmpint (xs, ==, INT32_MAX);
g_assert_cmpint (ys, ==, INT32_MAX);
g_assert_true (hb_font_get_glyph_v_origins (font, 1, &glyph, 0, &xs, 0, &ys, 0));
g_assert_cmpint (xs, ==, INT32_MAX);
g_assert_cmpint (ys, ==, INT32_MAX);
g_assert_true (hb_font_get_glyph_contour_point (font, 1, 0, &x, &y));
g_assert_cmpint (x, ==, INT32_MAX);
hb_font_extents_t extents;
g_assert_true (hb_font_get_h_extents (font, &extents));
g_assert_cmpint (extents.ascender, ==, INT32_MAX);
g_assert_true (hb_font_get_v_extents (font, &extents));
g_assert_cmpint (extents.ascender, ==, INT32_MAX);
hb_font_set_synthetic_bold (font, 0.05f, 0.05f, TRUE);
g_assert_true (hb_font_get_v_extents (font, &extents));
g_assert_cmpint (extents.ascender, ==, INT32_MAX);
g_assert_cmpint (extents.descender, ==, INT32_MIN);
hb_font_destroy (font);
hb_face_destroy (face);
hb_font_funcs_destroy (ffuncs);
}
static void
test_synthetic_slant_saturates (void)
{
hb_font_funcs_t *ffuncs = _near_max_font_funcs ();
hb_face_t *face = hb_face_create (hb_blob_get_empty (), 0);
hb_font_t *font = hb_font_create (face);
hb_font_set_funcs (font, ffuncs, NULL, NULL);
hb_font_set_scale (font, 2048, 2048);
/* The contour point is one short of INT32_MAX in both axes. A slant of 1
* shifts x by the full y value. The shift is added first and the sum is
* clamped afterwards: hb_clamp_to widens to double, so the out-of-range
* sum saturates instead of hitting an undefined float-to-int
* conversion. */
hb_font_set_synthetic_slant (font, 1.f);
hb_position_t x = 0, y = 0;
g_assert_true (hb_font_get_glyph_contour_point (font, 1, 0, &x, &y));
g_assert_cmpint (x, ==, INT32_MAX);
g_assert_cmpint (y, ==, INT32_MAX - 1);
hb_font_destroy (font);
hb_face_destroy (face);
hb_font_funcs_destroy (ffuncs);
}
static hb_bool_t
_cancelling_contour_point_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
hb_codepoint_t glyph HB_UNUSED,
unsigned int point_index HB_UNUSED,
hb_position_t *x, hb_position_t *y,
void *user_data HB_UNUSED)
{
*x = -2000000000;
*y = 2000000000;
return TRUE;
}
static void
test_synthetic_slant_cancelling (void)
{
hb_font_funcs_t *ffuncs = hb_font_funcs_create ();
hb_font_funcs_set_glyph_contour_point_func (ffuncs, _cancelling_contour_point_func, NULL, NULL);
hb_face_t *face = hb_face_create (hb_blob_get_empty (), 0);
hb_font_t *font = hb_font_create (face);
hb_font_set_funcs (font, ffuncs, NULL, NULL);
hb_font_set_scale (font, 2048, 2048);
/* A slant of 2 shifts x by twice y: -2000000000 + 2 * 2000000000 is
* 2000000000, which fits. Clamping the shift to INT32_MAX before
* adding it would give 147483647 instead, so the sum has to be
* clamped, not the shift. */
hb_font_set_synthetic_slant (font, 2.f);
hb_position_t x = 0, y = 0;
g_assert_true (hb_font_get_glyph_contour_point (font, 1, 0, &x, &y));
g_assert_cmpint (x, ==, 2000000000);
g_assert_cmpint (y, ==, 2000000000);
hb_font_destroy (font);
hb_face_destroy (face);
hb_font_funcs_destroy (ffuncs);
}
static void
test_glyph_origin_offsets_saturate (void)
{
hb_font_funcs_t *ffuncs = _near_max_font_funcs ();
hb_face_t *face = hb_face_create (hb_blob_get_empty (), 0);
hb_font_t *font = hb_font_create (face);
hb_font_set_funcs (font, ffuncs, NULL, NULL);
hb_position_t x = 100, y = 100;
hb_font_add_glyph_origin_for_direction (font, 1, HB_DIRECTION_LTR, &x, &y);
g_assert_cmpint (x, ==, INT32_MAX);
g_assert_cmpint (y, ==, INT32_MAX);
x = y = -100;
hb_font_subtract_glyph_origin_for_direction (font, 1, HB_DIRECTION_LTR, &x, &y);
g_assert_cmpint (x, ==, INT32_MIN);
g_assert_cmpint (y, ==, INT32_MIN);
hb_font_destroy (font);
hb_face_destroy (face);
hb_font_funcs_destroy (ffuncs);
}
static hb_position_t
_small_advance_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
hb_codepoint_t glyph HB_UNUSED,
void *user_data HB_UNUSED)
{
return 100;
}
static void
test_synthetic_bold_extreme_strength (void)
{
hb_font_funcs_t *ffuncs = hb_font_funcs_create ();
hb_font_funcs_set_glyph_h_advance_func (ffuncs, _small_advance_func, NULL, NULL);
hb_face_t *face = hb_face_create (hb_blob_get_empty (), 0);
hb_font_t *font = hb_font_create (face);
hb_font_set_funcs (font, ffuncs, NULL, NULL);
/* The strength does not fit in 32 bits, in either direction. */
hb_font_set_scale (font, 2048, 2048);
hb_font_set_synthetic_bold (font, 1e30f, 1e30f, FALSE);
g_assert_cmpint (hb_font_get_glyph_h_advance (font, 1), ==, INT32_MAX);
hb_font_set_synthetic_bold (font, -1e30f, -1e30f, FALSE);
g_assert_cmpint (hb_font_get_glyph_h_advance (font, 1), ==, 100 - INT32_MAX);
/* The magnitude of INT32_MIN does not fit either. */
hb_font_set_scale (font, INT32_MIN, INT32_MIN);
hb_font_set_synthetic_bold (font, 1.f, 1.f, FALSE);
g_assert_cmpint (hb_font_get_glyph_h_advance (font, 1), ==, 100 - INT32_MAX);
hb_font_destroy (font);
hb_face_destroy (face);
hb_font_funcs_destroy (ffuncs);
}
static void
test_font_float_scale_saturates (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/Mplus1p-Regular.660E.ttf");
hb_font_t *font = hb_font_create (face);
/* The ascender of this font is larger than its em, so at these scales the
* scaled value does not fit in hb_position_t. */
hb_position_t ascender = 0;
hb_font_set_scale (font, INT32_MAX, INT32_MAX);
g_assert_true (hb_ot_metrics_get_position (font, HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER, &ascender));
g_assert_cmpint (ascender, ==, INT32_MAX);
hb_font_set_scale (font, INT32_MIN, INT32_MIN);
g_assert_true (hb_ot_metrics_get_position (font, HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER, &ascender));
g_assert_cmpint (ascender, ==, INT32_MIN);
hb_font_destroy (font);
hb_face_destroy (face);
}
#ifndef HB_DISABLE_DEPRECATED
#if defined(__GNUC__)
#pragma GCC diagnostic push
@@ -1383,6 +1636,12 @@ main (int argc, char **argv)
hb_test_add (test_font_empty);
hb_test_add (test_font_properties);
hb_test_add (test_synthetic_glyph_extents_overflow);
hb_test_add (test_synthetic_bold_saturates);
hb_test_add (test_synthetic_slant_saturates);
hb_test_add (test_synthetic_slant_cancelling);
hb_test_add (test_glyph_origin_offsets_saturate);
hb_test_add (test_synthetic_bold_extreme_strength);
hb_test_add (test_font_float_scale_saturates);
#ifndef HB_DISABLE_DEPRECATED
hb_test_add (test_font_set_face);
hb_test_add (test_font_set_face_custom_funcs);