From 86e7473f0dcc22efa2f28facff84eb0a8427bdc0 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Fri, 31 Jul 2026 00:14:21 +0300 Subject: [PATCH] [ot] Add hb_ot_fetch_number() Similar to hb_ot_fetch_bits() but for signed numbers. The only tags so far are for font bounding box from head table. Fixes https://github.com/harfbuzz/harfbuzz/issues/6110 --- docs/harfbuzz-sections.txt | 2 ++ src/hb-ot-fetch.cc | 30 ++++++++++++++++++++++++++++-- src/hb-ot-fetch.h | 25 +++++++++++++++++++++++++ test/api/test-ot-fetch.c | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index fa15cc5ba..1aad5394b 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -838,6 +838,8 @@ hb_ot_var_axis_info_t hb-ot-fetch hb_ot_bits_tag_t hb_ot_fetch_bits +hb_ot_number_tag_t +hb_ot_fetch_number
diff --git a/src/hb-ot-fetch.cc b/src/hb-ot-fetch.cc index 0052b5f9c..9e5a2de62 100644 --- a/src/hb-ot-fetch.cc +++ b/src/hb-ot-fetch.cc @@ -35,10 +35,11 @@ /** * SECTION:hb-ot-fetch * @title: hb-ot-fetch - * @short_description: OpenType bit fields + * @short_description: OpenType bit fields and numbers * @include: hb-ot.h * - * Functions for fetching various bit fields scattered around OpenType tables. + * Functions for fetching various bit fields and numbers scattered around + * OpenType tables. * * These are raw table values, and many of them are legacy or unreliable, but * applications might need them for various legacy reasons. @@ -76,4 +77,29 @@ hb_ot_fetch_bits (hb_face_t *face, } } +/** + * hb_ot_fetch_number: + * @face: #hb_face_t to work upon + * @tag: tag of the number to fetch + * + * Fetches a number of @face in font units. + * + * Return value: the number, or zero if the font does not have it. + * + * XSince: REPLACEME + **/ +int32_t +hb_ot_fetch_number (hb_face_t *face, + hb_ot_number_tag_t tag) +{ + switch ((unsigned) tag) + { + case HB_OT_NUMBER_TAG_FONT_X_MIN: return face->table.head->xMin; + case HB_OT_NUMBER_TAG_FONT_Y_MIN: return face->table.head->yMin; + case HB_OT_NUMBER_TAG_FONT_X_MAX: return face->table.head->xMax; + case HB_OT_NUMBER_TAG_FONT_Y_MAX: return face->table.head->yMax; + default: return 0; + } +} + #endif diff --git a/src/hb-ot-fetch.h b/src/hb-ot-fetch.h index acf05dd28..95adadc81 100644 --- a/src/hb-ot-fetch.h +++ b/src/hb-ot-fetch.h @@ -71,6 +71,31 @@ HB_EXTERN uint32_t hb_ot_fetch_bits (hb_face_t *face, hb_ot_bits_tag_t tag); +/** + * hb_ot_number_tag_t: + * @HB_OT_NUMBER_TAG_FONT_X_MIN: `xMin` of the `head` table. + * @HB_OT_NUMBER_TAG_FONT_Y_MIN: `yMin` of the `head` table. + * @HB_OT_NUMBER_TAG_FONT_X_MAX: `xMax` of the `head` table. + * @HB_OT_NUMBER_TAG_FONT_Y_MAX: `yMax` of the `head` table. + * + * Numbers that can be fetched with hb_ot_fetch_number(). + * + * XSince: REPLACEME + **/ +typedef enum { + HB_OT_NUMBER_TAG_FONT_X_MIN = HB_TAG ('x','m','i','n'), + HB_OT_NUMBER_TAG_FONT_Y_MIN = HB_TAG ('y','m','i','n'), + HB_OT_NUMBER_TAG_FONT_X_MAX = HB_TAG ('x','m','a','x'), + HB_OT_NUMBER_TAG_FONT_Y_MAX = HB_TAG ('y','m','a','x'), + + /*< private >*/ + _HB_OT_NUMBER_TAG_MAX_VALUE = HB_TAG_MAX_SIGNED /*< skip >*/ +} hb_ot_number_tag_t; + +HB_EXTERN int32_t +hb_ot_fetch_number (hb_face_t *face, + hb_ot_number_tag_t tag); + HB_END_DECLS #endif /* HB_OT_FETCH_H */ diff --git a/test/api/test-ot-fetch.c b/test/api/test-ot-fetch.c index ae832ecb1..ae28aa698 100644 --- a/test/api/test-ot-fetch.c +++ b/test/api/test-ot-fetch.c @@ -154,6 +154,37 @@ test_ot_bits_invalid (void) hb_face_destroy (face); } +static void +test_ot_number_bounding_box (void) +{ + hb_face_t *adwaita = hb_test_open_font_file ("fonts/adwaita.ttf"); + hb_face_t *abc = hb_test_open_font_file ("fonts/SourceSansPro-Regular.abc.otf"); + + g_assert_cmpint (hb_ot_fetch_number (adwaita, HB_OT_NUMBER_TAG_FONT_X_MIN), ==, 51); + g_assert_cmpint (hb_ot_fetch_number (adwaita, HB_OT_NUMBER_TAG_FONT_Y_MIN), ==, -250); + g_assert_cmpint (hb_ot_fetch_number (adwaita, HB_OT_NUMBER_TAG_FONT_X_MAX), ==, 1238); + g_assert_cmpint (hb_ot_fetch_number (adwaita, HB_OT_NUMBER_TAG_FONT_Y_MAX), ==, 950); + + g_assert_cmpint (hb_ot_fetch_number (abc, HB_OT_NUMBER_TAG_FONT_X_MIN), ==, -454); + g_assert_cmpint (hb_ot_fetch_number (abc, HB_OT_NUMBER_TAG_FONT_Y_MIN), ==, -293); + g_assert_cmpint (hb_ot_fetch_number (abc, HB_OT_NUMBER_TAG_FONT_X_MAX), ==, 2159); + g_assert_cmpint (hb_ot_fetch_number (abc, HB_OT_NUMBER_TAG_FONT_Y_MAX), ==, 968); + + hb_face_destroy (adwaita); + hb_face_destroy (abc); +} + +static void +test_ot_number_invalid (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/adwaita.ttf"); + + g_assert_cmpint (hb_ot_fetch_number (face, (hb_ot_number_tag_t) HB_TAG ('x','x','x','x')), ==, 0); + g_assert_cmpint (hb_ot_fetch_number (hb_face_get_empty (), HB_OT_NUMBER_TAG_FONT_X_MIN), ==, 0); + + hb_face_destroy (face); +} + int main (int argc, char **argv) { @@ -166,6 +197,8 @@ main (int argc, char **argv) hb_test_add (test_ot_bits_ranges); hb_test_add (test_ot_bits_missing_tables); hb_test_add (test_ot_bits_invalid); + hb_test_add (test_ot_number_bounding_box); + hb_test_add (test_ot_number_invalid); return hb_test_run (); }