Files
harfbuzz/test/api/test-font.c
Wei-Lun Li 24ded4335b [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
2026-11-09 11:47:15 -06:00

1660 lines
53 KiB
C

/*
* Copyright © 2011 Google, Inc.
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Google Author(s): Behdad Esfahbod
*/
#include "hb-test.h"
#include <hb-ot.h>
#ifdef HAVE_FREETYPE
#include <hb-ft.h>
#endif
#ifdef HAVE_FONTATIONS
#include <hb-fontations.h>
#endif
/* Unit tests for hb-font.h */
static const char test_data[] = "test\0data";
#ifndef HB_NO_GETENV
#ifndef HB_NO_OT_FONT
static void
test_default_funcs_environment (void)
{
if (!g_test_subprocess ())
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
return;
}
char *saved_funcs = g_strdup (g_getenv ("HB_FONT_FUNCS"));
g_assert_true (g_setenv ("HB_FONT_FUNCS", "ot", TRUE));
hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
hb_font_t *font = hb_font_create (face);
hb_codepoint_t glyph;
g_assert_true (hb_font_set_funcs_using (font, NULL));
g_assert_true (hb_font_get_nominal_glyph (font, 'a', &glyph));
hb_position_t advance = hb_font_get_glyph_h_advance (font, glyph);
g_assert_cmpint (advance, >, 0);
#ifdef HAVE_FREETYPE
/* "ot" is the first entry of supported_font_funcs, so the same funcs
* would be selected if the environment variable were ignored entirely.
* Check that the FreeType funcs are not selected. */
g_assert_null (hb_ft_font_get_face (font));
#endif
g_assert_true (hb_font_set_funcs_using (font, ""));
g_assert_cmpint (hb_font_get_glyph_h_advance (font, glyph), ==, advance);
g_assert_true (hb_font_set_funcs_using (font, "ot"));
g_assert_cmpint (hb_font_get_glyph_h_advance (font, glyph), ==, advance);
hb_font_destroy (font);
hb_face_destroy (face);
if (saved_funcs)
g_assert_true (g_setenv ("HB_FONT_FUNCS", saved_funcs, TRUE));
else
g_unsetenv ("HB_FONT_FUNCS");
g_free (saved_funcs);
}
#endif
#if defined(HAVE_FREETYPE) && !defined(HB_NO_OT_FONT)
static void
test_default_funcs_environment_nonfirst (void)
{
if (!g_test_subprocess ())
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
return;
}
/* "ft" is not the first entry of supported_font_funcs, so selecting it
* proves that the environment default is honored rather than falling back
* to the first backend. */
char *saved_funcs = g_strdup (g_getenv ("HB_FONT_FUNCS"));
g_assert_true (g_setenv ("HB_FONT_FUNCS", "ft", TRUE));
hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
hb_font_t *font = hb_font_create (face);
g_assert_nonnull (hb_ft_font_get_face (font));
hb_codepoint_t glyph;
g_assert_true (hb_font_get_nominal_glyph (font, 'a', &glyph));
g_assert_cmpint (hb_font_get_glyph_h_advance (font, glyph), >, 0);
hb_font_destroy (font);
hb_face_destroy (face);
if (saved_funcs)
g_assert_true (g_setenv ("HB_FONT_FUNCS", saved_funcs, TRUE));
else
g_unsetenv ("HB_FONT_FUNCS");
g_free (saved_funcs);
}
#endif
#if defined(G_OS_UNIX) && defined(HAVE_FREETYPE) && !defined(HB_NO_OT_FONT)
static void
test_default_funcs_environment_snapshot (void)
{
if (!g_test_subprocess ())
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
return;
}
char *saved_funcs = g_strdup (g_getenv ("HB_FONT_FUNCS"));
char funcs_env[] = "HB_FONT_FUNCS=ft";
g_assert_cmpint (putenv (funcs_env), ==, 0);
hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
hb_font_t *font = hb_font_create (face);
g_assert_nonnull (hb_ft_font_get_face (font));
hb_font_destroy (font);
/* Keep the caller-owned storage alive and change only its valid value.
* The cached default must remain the original snapshot. */
memcpy (strchr (funcs_env, '=') + 1, "ot", 3);
font = hb_font_create (face);
g_assert_nonnull (hb_ft_font_get_face (font));
/* An explicit selection must still override the cached default. */
g_assert_true (hb_font_set_funcs_using (font, "ot"));
g_assert_null (hb_ft_font_get_face (font));
hb_codepoint_t glyph;
g_assert_true (hb_font_get_nominal_glyph (font, 'a', &glyph));
g_assert_cmpint (hb_font_get_glyph_h_advance (font, glyph), >, 0);
hb_font_destroy (font);
hb_face_destroy (face);
g_unsetenv ("HB_FONT_FUNCS");
if (saved_funcs)
g_assert_true (g_setenv ("HB_FONT_FUNCS", saved_funcs, TRUE));
g_free (saved_funcs);
}
#endif
#endif
static void
test_face_empty (void)
{
hb_face_t *created_from_empty;
hb_face_t *created_from_null;
g_assert_true (hb_face_get_empty ());
created_from_empty = hb_face_create (hb_blob_get_empty (), 0);
g_assert_true (hb_face_get_empty () != created_from_empty);
created_from_null = hb_face_create (NULL, 0);
g_assert_true (hb_face_get_empty () != created_from_null);
g_assert_true (hb_face_reference_table (hb_face_get_empty (), HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
g_assert_cmpint (hb_face_get_upem (hb_face_get_empty ()), ==, 1000);
hb_face_destroy (created_from_null);
hb_face_destroy (created_from_empty);
}
static void
test_face_create (void)
{
hb_face_t *face;
hb_blob_t *blob;
blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
g_assert_true (hb_face_reference_table (face, HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
g_assert_cmpint (hb_face_get_upem (face), ==, 1000);
hb_face_destroy (face);
}
static void
free_up (void *user_data)
{
int *freed = (int *) user_data;
g_assert_true (!*freed);
(*freed)++;
}
static hb_tag_t test_tags[] = {
HB_TAG ('a','b','c','d'),
HB_TAG ('e','f','g','h'),
};
static hb_blob_t *
get_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data HB_UNUSED)
{
for (unsigned i = 0; i < sizeof (test_tags) / sizeof (test_tags[0]); i++)
{
if (test_tags[i] == tag)
return hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
}
return hb_blob_get_empty ();
}
static void
test_face_createfortables (void)
{
hb_face_t *face;
hb_blob_t *blob;
const char *data;
unsigned int len;
int freed = 0;
face = hb_face_create_for_tables (get_table, &freed, free_up);
g_assert_true (!freed);
g_assert_true (hb_face_reference_table (face, HB_TAG ('h','e','a','d')) == hb_blob_get_empty ());
blob = hb_face_reference_table (face, HB_TAG ('a','b','c','d'));
g_assert_true (blob != hb_blob_get_empty ());
data = hb_blob_get_data (blob, &len);
g_assert_cmpint (len, ==, sizeof (test_data));
g_assert_true (0 == memcmp (data, test_data, sizeof (test_data)));
hb_blob_destroy (blob);
g_assert_cmpint (hb_face_get_upem (face), ==, 1000);
hb_face_destroy (face);
g_assert_true (freed);
}
static unsigned int
get_table_tags (const hb_face_t *face HB_UNUSED,
unsigned int start_offset,
unsigned int *table_count,
hb_tag_t *table_tags,
void *user_data HB_UNUSED)
{
unsigned count = sizeof (test_tags) / sizeof (test_tags[0]);
unsigned end_offset;
if (!table_count)
return count;
if (start_offset >= count)
{
*table_count = 0;
return count;
}
end_offset = start_offset + *table_count;
if (end_offset < start_offset)
{
*table_count = 0;
return count;
}
end_offset = end_offset < count ? end_offset : count;
*table_count = end_offset - start_offset;
for (unsigned i = start_offset; i < end_offset; i++)
table_tags[i - start_offset] = test_tags[i];
return count;
}
static void
test_face_referenceblob (void)
{
hb_blob_t *blob;
hb_face_t *face;
int freed = 0;
face = hb_face_create_for_tables (get_table, &freed, free_up);
hb_face_set_get_table_tags_func (face, get_table_tags, NULL, NULL);
blob = hb_face_reference_blob (face);
hb_face_destroy (face);
g_assert_true (blob != hb_blob_get_empty ());
face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
g_assert_true (face != hb_face_get_empty ());
g_assert_cmpuint (hb_face_get_table_tags (face, 0, NULL, NULL), ==, sizeof (test_tags) / sizeof (test_tags[0]));
for (unsigned i = 0; i < sizeof (test_tags) / sizeof (test_tags[0]); i++)
{
hb_blob_t* table = hb_face_reference_table (face, test_tags[i]);
g_assert_true (table != hb_blob_get_empty ());
hb_blob_destroy (table);
}
hb_face_destroy (face);
}
static void
_test_font_nil_funcs (hb_font_t *font)
{
hb_codepoint_t glyph;
hb_position_t x, y;
hb_glyph_extents_t extents;
unsigned int upem = hb_face_get_upem (hb_font_get_face (font));
x = y = 13;
g_assert_true (!hb_font_get_glyph_contour_point (font, 17, 2, &x, &y));
g_assert_cmpint (x, ==, 0);
g_assert_cmpint (y, ==, 0);
x = hb_font_get_glyph_h_advance (font, 17);
g_assert_cmpint (x, ==, upem);
extents.x_bearing = extents.y_bearing = 13;
extents.width = extents.height = 15;
hb_font_get_glyph_extents (font, 17, &extents);
g_assert_cmpint (extents.x_bearing, ==, 0);
g_assert_cmpint (extents.y_bearing, ==, 0);
g_assert_cmpint (extents.width, ==, 0);
g_assert_cmpint (extents.height, ==, 0);
glyph = 3;
g_assert_true (!hb_font_get_glyph (font, 17, 2, &glyph));
g_assert_cmpint (glyph, ==, 0);
}
static void
_test_fontfuncs_nil (hb_font_funcs_t *ffuncs)
{
hb_blob_t *blob;
hb_face_t *face;
hb_font_t *font;
hb_font_t *subfont;
int freed = 0;
blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
g_assert_true (!hb_face_is_immutable (face));
font = hb_font_create (face);
g_assert_true (font);
g_assert_true (hb_face_is_immutable (face));
hb_face_destroy (face);
hb_font_set_funcs (font, ffuncs, &freed, free_up);
g_assert_cmpint (freed, ==, 0);
_test_font_nil_funcs (font);
subfont = hb_font_create_sub_font (font);
g_assert_true (subfont);
g_assert_cmpint (freed, ==, 0);
hb_font_destroy (font);
g_assert_cmpint (freed, ==, 0);
_test_font_nil_funcs (subfont);
hb_font_destroy (subfont);
g_assert_cmpint (freed, ==, 1);
}
static void
test_fontfuncs_empty (void)
{
g_assert_true (hb_font_funcs_get_empty ());
g_assert_true (hb_font_funcs_is_immutable (hb_font_funcs_get_empty ()));
_test_fontfuncs_nil (hb_font_funcs_get_empty ());
}
static void
test_fontfuncs_nil (void)
{
hb_font_funcs_t *ffuncs;
ffuncs = hb_font_funcs_create ();
g_assert_true (!hb_font_funcs_is_immutable (ffuncs));
_test_fontfuncs_nil (hb_font_funcs_get_empty ());
hb_font_funcs_destroy (ffuncs);
}
static hb_bool_t
contour_point_func1 (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
hb_codepoint_t glyph, unsigned int point_index HB_UNUSED,
hb_position_t *x, hb_position_t *y,
void *user_data HB_UNUSED)
{
if (glyph == 1) {
*x = 2;
*y = 3;
return TRUE;
}
if (glyph == 2) {
*x = 4;
*y = 5;
return TRUE;
}
return FALSE;
}
static hb_bool_t
contour_point_func2 (hb_font_t *font, void *font_data HB_UNUSED,
hb_codepoint_t glyph, unsigned int point_index,
hb_position_t *x, hb_position_t *y,
void *user_data HB_UNUSED)
{
if (glyph == 1) {
*x = 6;
*y = 7;
return TRUE;
}
return hb_font_get_glyph_contour_point (hb_font_get_parent (font),
glyph, point_index, x, y);
}
static hb_position_t
glyph_h_advance_func1 (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
hb_codepoint_t glyph,
void *user_data HB_UNUSED)
{
if (glyph == 1)
return 8;
return 0;
}
static void
test_fontfuncs_subclassing (void)
{
hb_blob_t *blob;
hb_face_t *face;
hb_font_funcs_t *ffuncs1;
hb_font_funcs_t *ffuncs2;
hb_font_t *font1;
hb_font_t *font2;
hb_font_t *font3;
hb_position_t x;
hb_position_t y;
blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
font1 = hb_font_create (face);
hb_face_destroy (face);
hb_font_set_scale (font1, 10, 10);
/* setup font1 */
ffuncs1 = hb_font_funcs_create ();
hb_font_funcs_set_glyph_contour_point_func (ffuncs1, contour_point_func1, NULL, NULL);
hb_font_funcs_set_glyph_h_advance_func (ffuncs1, glyph_h_advance_func1, NULL, NULL);
hb_font_set_funcs (font1, ffuncs1, NULL, NULL);
hb_font_funcs_destroy (ffuncs1);
x = y = 1;
g_assert_true (hb_font_get_glyph_contour_point_for_origin (font1, 1, 2, HB_DIRECTION_LTR, &x, &y));
g_assert_cmpint (x, ==, 2);
g_assert_cmpint (y, ==, 3);
g_assert_true (hb_font_get_glyph_contour_point_for_origin (font1, 2, 5, HB_DIRECTION_LTR, &x, &y));
g_assert_cmpint (x, ==, 4);
g_assert_cmpint (y, ==, 5);
g_assert_true (!hb_font_get_glyph_contour_point_for_origin (font1, 3, 7, HB_DIRECTION_RTL, &x, &y));
g_assert_cmpint (x, ==, 0);
g_assert_cmpint (y, ==, 0);
x = hb_font_get_glyph_h_advance (font1, 1);
g_assert_cmpint (x, ==, 8);
x = hb_font_get_glyph_h_advance (font1, 2);
g_assert_cmpint (x, ==, 0);
/* creating sub-font doesn't make the parent font immutable;
* making a font immutable however makes it's lineage immutable.
*/
font2 = hb_font_create_sub_font (font1);
font3 = hb_font_create_sub_font (font2);
g_assert_true (!hb_font_is_immutable (font1));
g_assert_true (!hb_font_is_immutable (font2));
g_assert_true (!hb_font_is_immutable (font3));
hb_font_make_immutable (font3);
g_assert_true (hb_font_is_immutable (font1));
g_assert_true (hb_font_is_immutable (font2));
g_assert_true (hb_font_is_immutable (font3));
hb_font_destroy (font2);
hb_font_destroy (font3);
font2 = hb_font_create_sub_font (font1);
hb_font_destroy (font1);
/* setup font2 to override some funcs */
ffuncs2 = hb_font_funcs_create ();
hb_font_funcs_set_glyph_contour_point_func (ffuncs2, contour_point_func2, NULL, NULL);
hb_font_set_funcs (font2, ffuncs2, NULL, NULL);
hb_font_funcs_destroy (ffuncs2);
x = y = 1;
g_assert_true (hb_font_get_glyph_contour_point_for_origin (font2, 1, 2, HB_DIRECTION_LTR, &x, &y));
g_assert_cmpint (x, ==, 6);
g_assert_cmpint (y, ==, 7);
g_assert_true (hb_font_get_glyph_contour_point_for_origin (font2, 2, 5, HB_DIRECTION_RTL, &x, &y));
g_assert_cmpint (x, ==, 4);
g_assert_cmpint (y, ==, 5);
g_assert_true (!hb_font_get_glyph_contour_point_for_origin (font2, 3, 7, HB_DIRECTION_LTR, &x, &y));
g_assert_cmpint (x, ==, 0);
g_assert_cmpint (y, ==, 0);
x = hb_font_get_glyph_h_advance (font2, 1);
g_assert_cmpint (x, ==, 8);
x = hb_font_get_glyph_h_advance (font2, 2);
g_assert_cmpint (x, ==, 0);
/* setup font3 to override scale */
font3 = hb_font_create_sub_font (font2);
hb_font_set_scale (font3, 20, 30);
x = y = 1;
g_assert_true (hb_font_get_glyph_contour_point_for_origin (font3, 1, 2, HB_DIRECTION_RTL, &x, &y));
g_assert_cmpint (x, ==, 6*2);
g_assert_cmpint (y, ==, 7*3);
g_assert_true (hb_font_get_glyph_contour_point_for_origin (font3, 2, 5, HB_DIRECTION_LTR, &x, &y));
g_assert_cmpint (x, ==, 4*2);
g_assert_cmpint (y, ==, 5*3);
g_assert_true (!hb_font_get_glyph_contour_point_for_origin (font3, 3, 7, HB_DIRECTION_LTR, &x, &y));
g_assert_cmpint (x, ==, 0*2);
g_assert_cmpint (y, ==, 0*3);
x = hb_font_get_glyph_h_advance (font3, 1);
g_assert_cmpint (x, ==, 8*2);
x = hb_font_get_glyph_h_advance (font3, 2);
g_assert_cmpint (x, ==, 0*2);
hb_font_destroy (font3);
hb_font_destroy (font2);
}
static hb_bool_t
nominal_glyph_func (hb_font_t *font HB_UNUSED,
void *font_data HB_UNUSED,
hb_codepoint_t unicode HB_UNUSED,
hb_codepoint_t *glyph,
void *user_data HB_UNUSED)
{
*glyph = 0;
return FALSE;
}
static unsigned int
nominal_glyphs_func (hb_font_t *font HB_UNUSED,
void *font_data HB_UNUSED,
unsigned int count HB_UNUSED,
const hb_codepoint_t *first_unicode HB_UNUSED,
unsigned int unicode_stride HB_UNUSED,
hb_codepoint_t *first_glyph HB_UNUSED,
unsigned int glyph_stride HB_UNUSED,
void *user_data HB_UNUSED)
{
return 0;
}
static void
test_fontfuncs_parallels (void)
{
hb_blob_t *blob;
hb_face_t *face;
hb_font_funcs_t *ffuncs1;
hb_font_funcs_t *ffuncs2;
hb_font_t *font0;
hb_font_t *font1;
hb_font_t *font2;
hb_codepoint_t glyph;
blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
font0 = hb_font_create (face);
hb_face_destroy (face);
/* setup sub-font1 */
font1 = hb_font_create_sub_font (font0);
hb_font_destroy (font0);
ffuncs1 = hb_font_funcs_create ();
hb_font_funcs_set_nominal_glyph_func (ffuncs1, nominal_glyph_func, NULL, NULL);
hb_font_set_funcs (font1, ffuncs1, NULL, NULL);
hb_font_funcs_destroy (ffuncs1);
/* setup sub-font2 */
font2 = hb_font_create_sub_font (font1);
hb_font_destroy (font1);
ffuncs2 = hb_font_funcs_create ();
hb_font_funcs_set_nominal_glyphs_func (ffuncs1, nominal_glyphs_func, NULL, NULL);
hb_font_set_funcs (font2, ffuncs2, NULL, NULL);
hb_font_funcs_destroy (ffuncs2);
/* Just test that calling get_nominal_glyph doesn't infinite-loop. */
hb_font_get_nominal_glyph (font2, 0x0020u, &glyph);
hb_font_destroy (font2);
}
static void
test_font_empty (void)
{
hb_font_t *created_from_empty;
hb_font_t *created_from_null;
hb_font_t *created_sub_from_null;
g_assert_true (hb_font_get_empty ());
created_from_empty = hb_font_create (hb_face_get_empty ());
g_assert_true (hb_font_get_empty () != created_from_empty);
created_from_null = hb_font_create (NULL);
g_assert_true (hb_font_get_empty () != created_from_null);
created_sub_from_null = hb_font_create_sub_font (NULL);
g_assert_true (hb_font_get_empty () != created_sub_from_null);
g_assert_true (hb_font_is_immutable (hb_font_get_empty ()));
g_assert_true (hb_font_get_face (hb_font_get_empty ()) == hb_face_get_empty ());
g_assert_true (hb_font_get_parent (hb_font_get_empty ()) == NULL);
hb_font_destroy (created_sub_from_null);
hb_font_destroy (created_from_null);
hb_font_destroy (created_from_empty);
}
static void
test_font_properties (void)
{
hb_blob_t *blob;
hb_face_t *face;
hb_font_t *font;
hb_font_t *subfont;
int x_scale, y_scale;
unsigned int x_ppem, y_ppem;
unsigned int upem;
blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
font = hb_font_create (face);
hb_face_destroy (face);
g_assert_true (hb_font_get_face (font) == face);
g_assert_true (hb_font_get_parent (font) == hb_font_get_empty ());
subfont = hb_font_create_sub_font (font);
g_assert_true (hb_font_get_parent (subfont) == font);
hb_font_set_parent(subfont, NULL);
g_assert_true (hb_font_get_parent (subfont) == hb_font_get_empty());
hb_font_set_parent(subfont, font);
g_assert_true (hb_font_get_parent (subfont) == font);
hb_font_set_parent(subfont, NULL);
hb_font_make_immutable (subfont);
g_assert_true (hb_font_get_parent (subfont) == hb_font_get_empty());
hb_font_set_parent(subfont, font);
g_assert_true (hb_font_get_parent (subfont) == hb_font_get_empty());
hb_font_destroy (subfont);
/* Check scale */
upem = hb_face_get_upem (hb_font_get_face (font));
hb_font_get_scale (font, NULL, NULL);
x_scale = y_scale = 13;
hb_font_get_scale (font, &x_scale, NULL);
g_assert_cmpint (x_scale, ==, upem);
x_scale = y_scale = 13;
hb_font_get_scale (font, NULL, &y_scale);
g_assert_cmpint (y_scale, ==, upem);
x_scale = y_scale = 13;
hb_font_get_scale (font, &x_scale, &y_scale);
g_assert_cmpint (x_scale, ==, upem);
g_assert_cmpint (y_scale, ==, upem);
hb_font_set_scale (font, 17, 19);
x_scale = y_scale = 13;
hb_font_get_scale (font, &x_scale, &y_scale);
g_assert_cmpint (x_scale, ==, 17);
g_assert_cmpint (y_scale, ==, 19);
/* Check ppem */
hb_font_get_ppem (font, NULL, NULL);
x_ppem = y_ppem = 13;
hb_font_get_ppem (font, &x_ppem, NULL);
g_assert_cmpint (x_ppem, ==, 0);
x_ppem = y_ppem = 13;
hb_font_get_ppem (font, NULL, &y_ppem);
g_assert_cmpint (y_ppem, ==, 0);
x_ppem = y_ppem = 13;
hb_font_get_ppem (font, &x_ppem, &y_ppem);
g_assert_cmpint (x_ppem, ==, 0);
g_assert_cmpint (y_ppem, ==, 0);
hb_font_set_ppem (font, 17, 19);
x_ppem = y_ppem = 13;
hb_font_get_ppem (font, &x_ppem, &y_ppem);
g_assert_cmpint (x_ppem, ==, 17);
g_assert_cmpint (y_ppem, ==, 19);
/* Check ptem */
g_assert_cmpint (hb_font_get_ptem (font), ==, 0);
hb_font_set_ptem (font, 42);
g_assert_cmpint (hb_font_get_ptem (font), ==, 42);
/* Check immutable */
g_assert_true (!hb_font_is_immutable (font));
hb_font_make_immutable (font);
g_assert_true (hb_font_is_immutable (font));
hb_font_set_scale (font, 10, 12);
x_scale = y_scale = 13;
hb_font_get_scale (font, &x_scale, &y_scale);
g_assert_cmpint (x_scale, ==, 17);
g_assert_cmpint (y_scale, ==, 19);
hb_font_set_ppem (font, 10, 12);
x_ppem = y_ppem = 13;
hb_font_get_ppem (font, &x_ppem, &y_ppem);
g_assert_cmpint (x_ppem, ==, 17);
g_assert_cmpint (y_ppem, ==, 19);
/* sub_font now */
subfont = hb_font_create_sub_font (font);
hb_font_destroy (font);
g_assert_true (hb_font_get_parent (subfont) == font);
g_assert_true (hb_font_get_face (subfont) == face);
/* scale */
x_scale = y_scale = 13;
hb_font_get_scale (subfont, &x_scale, &y_scale);
g_assert_cmpint (x_scale, ==, 17);
g_assert_cmpint (y_scale, ==, 19);
hb_font_set_scale (subfont, 10, 12);
x_scale = y_scale = 13;
hb_font_get_scale (subfont, &x_scale, &y_scale);
g_assert_cmpint (x_scale, ==, 10);
g_assert_cmpint (y_scale, ==, 12);
x_scale = y_scale = 13;
hb_font_get_scale (font, &x_scale, &y_scale);
g_assert_cmpint (x_scale, ==, 17);
g_assert_cmpint (y_scale, ==, 19);
/* ppem */
x_ppem = y_ppem = 13;
hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
g_assert_cmpint (x_ppem, ==, 17);
g_assert_cmpint (y_ppem, ==, 19);
hb_font_set_ppem (subfont, 10, 12);
x_ppem = y_ppem = 13;
hb_font_get_ppem (subfont, &x_ppem, &y_ppem);
g_assert_cmpint (x_ppem, ==, 10);
g_assert_cmpint (y_ppem, ==, 12);
x_ppem = y_ppem = 13;
hb_font_get_ppem (font, &x_ppem, &y_ppem);
g_assert_cmpint (x_ppem, ==, 17);
g_assert_cmpint (y_ppem, ==, 19);
hb_font_destroy (subfont);
}
static hb_bool_t
_extreme_glyph_extents_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
hb_codepoint_t glyph HB_UNUSED,
hb_glyph_extents_t *extents,
void *user_data HB_UNUSED)
{
/* Mimic extents that the glyf/sbix producers can legitimately emit after
* clamping each field independently to the full hb_position_t range. */
extents->x_bearing = G_MAXINT;
extents->y_bearing = G_MAXINT;
extents->width = 100;
extents->height = 100;
return TRUE;
}
static void
test_synthetic_glyph_extents_overflow (void)
{
hb_font_funcs_t *ffuncs = hb_font_funcs_create ();
hb_font_funcs_set_glyph_extents_func (ffuncs, _extreme_glyph_extents_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);
/* Both synthetic transforms operate on the extreme extents above; without
* saturating arithmetic the int32 sums/differences overflow (UBSan
* signed-integer-overflow in synthetic_glyph_extents). */
hb_font_set_synthetic_slant (font, 0.25f);
hb_font_set_synthetic_bold (font, 0.05f, 0.05f, TRUE);
hb_glyph_extents_t extents;
g_assert_true (hb_font_get_glyph_extents (font, 1, &extents));
hb_font_destroy (font);
hb_face_destroy (face);
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
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning (push)
#pragma warning (disable: 4996)
#endif
static void
test_font_set_face (void)
{
hb_face_t *face1 = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
hb_face_t *face2 = hb_test_open_font_file ("fonts/OpenSans-Regular.ttf");
hb_font_t *font = hb_font_create (face1);
hb_codepoint_t glyph1, glyph2, glyph;
char name[32];
g_assert_true (hb_font_get_nominal_glyph (font, 'a', &glyph1));
g_assert_true (hb_font_get_glyph_h_advance (font, glyph1));
/* The font holds the last reference to face1, so setting a new face
* frees it; the font funcs must use the new face from then on. */
hb_face_destroy (face1);
hb_font_set_face (font, face2);
g_assert_true (hb_font_get_nominal_glyph (font, 'a', &glyph));
hb_font_t *font2 = hb_font_create (face2);
g_assert_true (hb_font_get_nominal_glyph (font2, 'a', &glyph2));
g_assert_cmpuint (glyph, ==, glyph2);
g_assert_cmpint (hb_font_get_glyph_h_advance (font, glyph), ==,
hb_font_get_glyph_h_advance (font2, glyph2));
g_assert_true (hb_font_get_glyph_name (font, glyph, name, sizeof (name)));
g_assert_cmpstr (name, ==, "a");
hb_font_destroy (font2);
hb_font_destroy (font);
hb_face_destroy (face2);
}
static hb_bool_t
_set_face_nominal_glyph_func (hb_font_t *font HB_UNUSED,
void *font_data HB_UNUSED,
hb_codepoint_t unicode HB_UNUSED,
hb_codepoint_t *glyph,
void *user_data HB_UNUSED)
{
*glyph = 1234;
return TRUE;
}
static void
_set_face_destroy (void *data)
{
/* Font data may point into the old face without holding a reference;
* the face must still be alive when the data is released. */
hb_face_t *face = (hb_face_t *) data;
g_assert_cmpuint (hb_face_get_glyph_count (face), >, 0);
}
static void
test_font_set_face_custom_funcs (void)
{
hb_face_t *face1 = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
hb_face_t *face2 = hb_test_open_font_file ("fonts/OpenSans-Regular.ttf");
hb_font_t *font = hb_font_create (face1);
hb_font_funcs_t *ffuncs = hb_font_funcs_create ();
hb_codepoint_t glyph;
hb_font_funcs_set_nominal_glyph_func (ffuncs, _set_face_nominal_glyph_func, NULL, NULL);
hb_font_set_funcs (font, ffuncs, face1, _set_face_destroy);
hb_font_funcs_destroy (ffuncs);
g_assert_true (hb_font_get_nominal_glyph (font, 'a', &glyph));
g_assert_cmpuint (glyph, ==, 1234);
/* Custom font funcs belong to the old face; setting a new face
* releases them and installs the default ones for the new face. */
hb_face_destroy (face1);
hb_font_set_face (font, face2);
g_assert_true (hb_font_get_nominal_glyph (font, 'a', &glyph));
g_assert_cmpuint (glyph, !=, 1234);
hb_font_destroy (font);
hb_face_destroy (face2);
}
static void
test_font_set_face_variation (void)
{
hb_face_t *face1 = hb_test_open_font_file ("fonts/Mada-VF.ttf");
hb_face_t *face2 = hb_test_open_font_file ("fonts/Roboto-Variable.abc.ttf");
hb_font_t *font = hb_font_create (face1);
const float *coords;
unsigned length;
/* The font's design coords are sized for face1's single axis. */
hb_font_set_variation (font, HB_TAG ('w','g','h','t'), 400);
hb_font_set_face (font, face2);
/* face2 has two axes; the old coords must not be read as face2's. */
hb_font_set_variation (font, HB_TAG ('w','g','h','t'), 500);
coords = hb_font_get_var_coords_design (font, &length);
g_assert_cmpuint (length, ==, 2);
g_assert_cmpfloat (coords[0], ==, 500);
g_assert_cmpfloat (coords[1], ==, 100);
hb_font_destroy (font);
hb_face_destroy (face1);
hb_face_destroy (face2);
}
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
#pragma warning (pop)
#endif
#endif
#ifdef HAVE_FONTATIONS
typedef struct {
hb_font_t *font;
GMutex mutex;
GCond cond;
unsigned waiting;
hb_bool_t start;
char name[128];
} fontations_name_test_t;
static gpointer
fontations_name_thread (gpointer user_data)
{
fontations_name_test_t *data = user_data;
g_mutex_lock (&data->mutex);
data->waiting++;
g_cond_broadcast (&data->cond);
while (!data->start)
g_cond_wait (&data->cond, &data->mutex);
g_mutex_unlock (&data->mutex);
for (unsigned i = 0; i < 100; i++)
{
hb_codepoint_t glyph = HB_CODEPOINT_INVALID;
g_assert_true (hb_font_get_glyph_from_name (data->font, data->name, -1, &glyph));
g_assert_cmpuint (glyph, ==, 1);
g_assert_false (hb_font_get_glyph_from_name (data->font, "nonexistent-glyph", -1, &glyph));
}
return NULL;
}
static void
test_fontations_glyph_from_name_threads (void)
{
for (unsigned run = 0; run < 8; run++)
{
hb_face_t *face = hb_test_open_font_file ("fonts/NotoSans-Bold.ttf");
fontations_name_test_t data = {0};
data.font = hb_font_create (face);
hb_fontations_font_set_funcs (data.font);
hb_font_make_immutable (data.font);
g_assert_true (hb_font_get_glyph_name (data.font, 1, data.name, sizeof (data.name)));
g_mutex_init (&data.mutex);
g_cond_init (&data.cond);
GThread *threads[8];
for (unsigned i = 0; i < G_N_ELEMENTS (threads); i++)
threads[i] = g_thread_new ("fontations-names", fontations_name_thread, &data);
g_mutex_lock (&data.mutex);
while (data.waiting != G_N_ELEMENTS (threads))
g_cond_wait (&data.cond, &data.mutex);
data.start = true;
g_cond_broadcast (&data.cond);
g_mutex_unlock (&data.mutex);
for (unsigned i = 0; i < G_N_ELEMENTS (threads); i++)
g_thread_join (threads[i]);
g_cond_clear (&data.cond);
g_mutex_clear (&data.mutex);
hb_font_destroy (data.font);
hb_face_destroy (face);
}
}
static void
test_fontations_scale_changes (void)
{
const char *fonts[] = {
"fonts/Roboto-Variable.abc.ttf",
"fonts/SourceSerifVariable-Roman-VVAR.abc.ttf",
"fonts/Roboto-Regular.abc.ttf",
};
const struct {
int values[3];
unsigned count;
} locations[] = {
{{0, 0, 0}, 0}, {{0, 0, 0}, 3},
{{9830, 0, 0}, 1}, {{9830, 0, 0}, 3},
{{9830, 16384, 0}, 2}, {{9830, 16384, 16384}, 3},
{{0, 0, 16384}, 3}, {{0, 0, 0}, 0},
};
const int scales[][2] = {{2048, 4096}, {-1234, -5678}, {0, 0}, {1234, 5678}};
for (unsigned f = 0; f < G_N_ELEMENTS (fonts); f++)
{
hb_face_t *face = hb_test_open_font_file (fonts[f]);
hb_font_t *font = hb_font_create (face);
hb_font_t *reference = hb_font_create (face);
hb_fontations_font_set_funcs (font);
for (unsigned c = 0; c < G_N_ELEMENTS (locations); c++)
{
hb_font_set_var_coords_normalized (font, locations[c].values, locations[c].count);
hb_font_set_var_coords_normalized (reference, locations[c].values, locations[c].count);
for (unsigned s = 0; s < G_N_ELEMENTS (scales); s++)
{
hb_font_set_scale (font, scales[s][0], scales[s][1]);
hb_font_set_scale (reference, scales[s][0], scales[s][1]);
/* Compare the reused instance against a freshly initialized one. */
hb_fontations_font_set_funcs (reference);
for (hb_codepoint_t glyph = 0; glyph < hb_face_get_glyph_count (face); glyph++)
{
g_assert_cmpint (hb_font_get_glyph_h_advance (font, glyph), ==,
hb_font_get_glyph_h_advance (reference, glyph));
g_assert_cmpint (hb_font_get_glyph_v_advance (font, glyph), ==,
hb_font_get_glyph_v_advance (reference, glyph));
}
}
}
hb_font_destroy (reference);
hb_font_destroy (font);
hb_face_destroy (face);
}
}
static void
test_fontations_strides (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
hb_font_t *font = hb_font_create (face);
hb_fontations_font_set_funcs (font);
const hb_codepoint_t unicodes[] = {'a', 'b', 'c'};
hb_codepoint_t ids[3];
uint8_t unicode_bytes[16], glyph_bytes[16], advance_bytes[16];
memset (unicode_bytes, 0xA5, sizeof (unicode_bytes));
memset (glyph_bytes, 0xA5, sizeof (glyph_bytes));
for (unsigned i = 0; i < G_N_ELEMENTS (unicodes); i++)
{
memcpy (unicode_bytes + 1 + 5 * i, &unicodes[i], sizeof (unicodes[i]));
g_assert_true (hb_font_get_nominal_glyph (font, unicodes[i], &ids[i]));
}
g_assert_cmpuint (hb_font_get_nominal_glyphs (font, 3,
(const hb_codepoint_t *) (unicode_bytes + 1), 5,
(hb_codepoint_t *) (glyph_bytes + 1), 5), ==, 3);
for (unsigned i = 0; i < 3; i++)
{
hb_codepoint_t glyph;
memcpy (&glyph, glyph_bytes + 1 + 5 * i, sizeof (glyph));
g_assert_cmpuint (glyph, ==, ids[i]);
g_assert_cmpuint (glyph_bytes[5 * i + 5], ==, 0xA5);
}
g_assert_cmpuint (glyph_bytes[0], ==, 0xA5);
/* hb-ot-normalize maps codepoint to var1.u32 in the same glyph-info array. */
hb_glyph_info_t infos[3] = {0};
for (unsigned i = 0; i < G_N_ELEMENTS (infos); i++)
{
infos[i].codepoint = unicodes[i];
infos[i].mask = 123456;
infos[i].cluster = i;
infos[i].var1.u32 = HB_CODEPOINT_INVALID;
infos[i].var2.u32 = 123456;
}
g_assert_cmpuint (hb_font_get_nominal_glyphs (font, G_N_ELEMENTS (infos),
&infos[0].codepoint, sizeof (infos[0]),
&infos[0].var1.u32, sizeof (infos[0])), ==, G_N_ELEMENTS (infos));
for (unsigned i = 0; i < G_N_ELEMENTS (infos); i++)
{
g_assert_cmpuint (infos[i].codepoint, ==, unicodes[i]);
g_assert_cmpuint (infos[i].var1.u32, ==, ids[i]);
g_assert_cmpuint (infos[i].mask, ==, 123456);
g_assert_cmpuint (infos[i].cluster, ==, i);
g_assert_cmpuint (infos[i].var2.u32, ==, 123456);
}
void (*get_advances[])(hb_font_t *, unsigned, const hb_codepoint_t *, unsigned,
hb_position_t *, unsigned) = {
hb_font_get_glyph_h_advances, hb_font_get_glyph_v_advances,
};
for (unsigned axis = 0; axis < G_N_ELEMENTS (get_advances); axis++)
{
hb_position_t expected[3];
get_advances[axis] (font, 3, ids, sizeof (ids[0]), expected, sizeof (expected[0]));
memset (advance_bytes, 0xA5, sizeof (advance_bytes));
get_advances[axis] (font, 3, (const hb_codepoint_t *) (glyph_bytes + 1), 5,
(hb_position_t *) (advance_bytes + 1), 5);
for (unsigned i = 0; i < 3; i++)
{
hb_position_t advance;
memcpy (&advance, advance_bytes + 1 + 5 * i, sizeof (advance));
g_assert_cmpint (advance, ==, expected[i]);
g_assert_cmpuint (advance_bytes[5 * i + 5], ==, 0xA5);
}
g_assert_cmpuint (advance_bytes[0], ==, 0xA5);
hb_position_t last = 0;
get_advances[axis] (font, 3, ids, sizeof (ids[0]), &last, 0);
g_assert_cmpint (last, ==, expected[2]);
hb_codepoint_t in_place[3];
memcpy (in_place, ids, sizeof (ids));
get_advances[axis] (font, 3, in_place, sizeof (in_place[0]),
(hb_position_t *) in_place, sizeof (in_place[0]));
for (unsigned i = 0; i < 3; i++)
g_assert_cmpint ((hb_position_t) in_place[i], ==, expected[i]);
}
struct {
hb_codepoint_t glyph;
hb_position_t h_advance, v_advance, x, y, padding;
} records[3];
for (unsigned i = 0; i < G_N_ELEMENTS (records); i++)
{
records[i].glyph = ids[i];
records[i].h_advance = records[i].v_advance = records[i].x = records[i].y = 0;
records[i].padding = 123456;
}
hb_font_get_glyph_h_advances (font, G_N_ELEMENTS (records),
&records[0].glyph, sizeof (records[0]),
&records[0].h_advance, sizeof (records[0]));
hb_font_get_glyph_v_advances (font, G_N_ELEMENTS (records),
&records[0].glyph, sizeof (records[0]),
&records[0].v_advance, sizeof (records[0]));
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (records),
&records[0].glyph, sizeof (records[0]),
&records[0].x, sizeof (records[0]),
&records[0].y, sizeof (records[0])));
for (unsigned i = 0; i < G_N_ELEMENTS (records); i++)
{
hb_position_t x, y;
g_assert_cmpuint (records[i].glyph, ==, ids[i]);
g_assert_cmpint (records[i].h_advance, ==, hb_font_get_glyph_h_advance (font, ids[i]));
g_assert_cmpint (records[i].v_advance, ==, hb_font_get_glyph_v_advance (font, ids[i]));
g_assert_true (hb_font_get_glyph_v_origin (font, ids[i], &x, &y));
g_assert_cmpint (records[i].x, ==, x);
g_assert_cmpint (records[i].y, ==, y);
g_assert_cmpint (records[i].padding, ==, 123456);
}
hb_font_destroy (font);
hb_face_destroy (face);
}
static void
test_fontations_v_origins (void)
{
const char *fonts[] = {
"fonts/Roboto-Regular.abc.ttf",
"fonts/Roboto-Variable.abc.ttf",
"fonts/SourceSerifVariable-Roman-VVAR.abc.ttf",
"fonts/AdobeVFPrototype.abc.otf",
"../shape/data/in-house/fonts/NotoSansCJK-VF.abc.ttf",
"../shape/data/in-house/fonts/NotoSansCJK-VF.abc.otf",
};
const int coords[] = {0, 16384, -16384, 0};
const hb_codepoint_t glyphs[] = {0, 1, 2, 3, HB_CODEPOINT_INVALID};
for (unsigned f = 0; f < G_N_ELEMENTS (fonts); f++)
{
hb_face_t *face = hb_test_open_font_file (fonts[f]);
hb_font_t *font = hb_font_create (face);
hb_fontations_font_set_funcs (font);
int upem = hb_face_get_upem (face);
for (int sign = 1; sign >= -1; sign -= 2)
{
hb_font_set_scale (font, sign * upem * 2, sign * upem * 3);
for (unsigned c = 0; c < G_N_ELEMENTS (coords); c++)
{
hb_font_set_var_coords_normalized (font, &coords[c], 1);
hb_position_t expected_x[G_N_ELEMENTS (glyphs)], expected_y[G_N_ELEMENTS (glyphs)];
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
g_assert_true (hb_font_get_glyph_v_origin (font, glyphs[i], &expected_x[i], &expected_y[i]));
hb_position_t origins[4 * G_N_ELEMENTS (glyphs)];
for (unsigned i = 0; i < G_N_ELEMENTS (origins); i++)
origins[i] = 123456;
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
glyphs, sizeof (glyphs[0]),
origins, 4 * sizeof (origins[0]),
origins + 1, 4 * sizeof (origins[0])));
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
{
g_assert_cmpint (origins[4 * i], ==, expected_x[i]);
g_assert_cmpint (origins[4 * i + 1], ==, expected_y[i]);
g_assert_cmpint (origins[4 * i + 2], ==, 123456);
g_assert_cmpint (origins[4 * i + 3], ==, 123456);
}
hb_position_t last_x = 0, last_y = 0;
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
glyphs, sizeof (glyphs[0]), &last_x, 0, &last_y, 0));
g_assert_cmpint (last_x, ==, expected_x[G_N_ELEMENTS (glyphs) - 1]);
g_assert_cmpint (last_y, ==, expected_y[G_N_ELEMENTS (glyphs) - 1]);
hb_codepoint_t in_place[G_N_ELEMENTS (glyphs)];
memcpy (in_place, glyphs, sizeof (glyphs));
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
in_place, sizeof (in_place[0]),
(hb_position_t *) in_place, sizeof (in_place[0]),
origins, sizeof (origins[0])));
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
{
g_assert_cmpint ((hb_position_t) in_place[i], ==, expected_x[i]);
g_assert_cmpint (origins[i], ==, expected_y[i]);
}
uint8_t x_bytes[1 + 5 * G_N_ELEMENTS (glyphs)], y_bytes[sizeof (x_bytes)];
memset (x_bytes, 0xA5, sizeof (x_bytes));
memset (y_bytes, 0xA5, sizeof (y_bytes));
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
glyphs, sizeof (glyphs[0]),
(hb_position_t *) (x_bytes + 1), 5,
(hb_position_t *) (y_bytes + 1), 5));
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
{
hb_position_t x, y;
memcpy (&x, x_bytes + 1 + 5 * i, sizeof (x));
memcpy (&y, y_bytes + 1 + 5 * i, sizeof (y));
g_assert_cmpint (x, ==, expected_x[i]);
g_assert_cmpint (y, ==, expected_y[i]);
g_assert_cmpuint (x_bytes[5 * i + 5], ==, 0xA5);
g_assert_cmpuint (y_bytes[5 * i + 5], ==, 0xA5);
}
g_assert_true (hb_font_get_glyph_v_origins (font, 0, NULL, 0, NULL, 0, NULL, 0));
}
}
hb_font_destroy (font);
hb_face_destroy (face);
}
}
static void
test_fontations_advances (void)
{
const char *fonts[] = {
"fonts/Roboto-Variable.abc.ttf",
"fonts/SourceSerifVariable-Roman-VVAR.abc.ttf",
"fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf",
};
const int coords[] = {0, 16384, -16384, 0};
const struct {
hb_codepoint_t glyph;
hb_codepoint_t padding[2];
} glyphs[] = {{1, {0}}, {2, {0}}, {3, {0}}, {HB_CODEPOINT_INVALID, {0}}};
for (unsigned f = 0; f < G_N_ELEMENTS (fonts); f++)
{
hb_face_t *face = hb_test_open_font_file (fonts[f]);
hb_font_t *font = hb_font_create (face);
hb_font_t *reference = hb_font_create (face);
int upem = hb_face_get_upem (face);
hb_fontations_font_set_funcs (font);
for (int sign = 1; sign >= -1; sign -= 2)
{
hb_font_set_scale (font, sign * upem * 2, sign * upem * 3);
hb_font_set_scale (reference, sign * upem * 2, sign * upem * 3);
for (unsigned c = 0; c < G_N_ELEMENTS (coords); c++)
{
hb_font_set_var_coords_normalized (font, &coords[c], 1);
hb_font_set_var_coords_normalized (reference, &coords[c], 1);
hb_position_t h_advances[2 * G_N_ELEMENTS (glyphs)];
hb_position_t v_advances[2 * G_N_ELEMENTS (glyphs)];
for (unsigned i = 0; i < G_N_ELEMENTS (h_advances); i++)
h_advances[i] = v_advances[i] = 123456;
hb_font_get_glyph_h_advances (font, G_N_ELEMENTS (glyphs),
&glyphs[0].glyph, sizeof (glyphs[0]),
h_advances, 2 * sizeof (h_advances[0]));
hb_font_get_glyph_v_advances (font, G_N_ELEMENTS (glyphs),
&glyphs[0].glyph, sizeof (glyphs[0]),
v_advances, 2 * sizeof (v_advances[0]));
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
{
hb_codepoint_t glyph = glyphs[i].glyph;
g_assert_cmpint (h_advances[2 * i], ==, hb_font_get_glyph_h_advance (font, glyph));
g_assert_cmpint (v_advances[2 * i], ==, hb_font_get_glyph_v_advance (font, glyph));
/* OT can apply variation deltas even to invalid glyph IDs. */
if (glyph < hb_face_get_glyph_count (face))
{
g_assert_cmpint (h_advances[2 * i], ==, hb_font_get_glyph_h_advance (reference, glyph));
g_assert_cmpint (v_advances[2 * i], ==, hb_font_get_glyph_v_advance (reference, glyph));
}
g_assert_cmpint (h_advances[2 * i + 1], ==, 123456);
g_assert_cmpint (v_advances[2 * i + 1], ==, 123456);
}
hb_font_get_glyph_h_advances (font, 0, NULL, 0, NULL, 0);
hb_font_get_glyph_v_advances (font, 0, NULL, 0, NULL, 0);
}
}
hb_font_destroy (reference);
hb_font_destroy (font);
hb_face_destroy (face);
}
}
#endif
int
main (int argc, char **argv)
{
hb_test_init (&argc, &argv);
#ifndef HB_NO_GETENV
#ifndef HB_NO_OT_FONT
hb_test_add (test_default_funcs_environment);
#endif
#if defined(HAVE_FREETYPE) && !defined(HB_NO_OT_FONT)
hb_test_add (test_default_funcs_environment_nonfirst);
#endif
#if defined(G_OS_UNIX) && defined(HAVE_FREETYPE) && !defined(HB_NO_OT_FONT)
hb_test_add (test_default_funcs_environment_snapshot);
#endif
#endif
hb_test_add (test_face_empty);
hb_test_add (test_face_create);
hb_test_add (test_face_createfortables);
hb_test_add (test_face_referenceblob);
hb_test_add (test_fontfuncs_empty);
hb_test_add (test_fontfuncs_nil);
hb_test_add (test_fontfuncs_subclassing);
hb_test_add (test_fontfuncs_parallels);
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);
hb_test_add (test_font_set_face_variation);
#endif
#ifdef HAVE_FONTATIONS
hb_test_add (test_fontations_glyph_from_name_threads);
hb_test_add (test_fontations_scale_changes);
hb_test_add (test_fontations_strides);
hb_test_add (test_fontations_v_origins);
hb_test_add (test_fontations_advances);
#endif
return hb_test_run();
}