From 6e67c5feed7b37fc7a46b0918b4e5eef478a8a13 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Thu, 30 Jul 2026 14:19:22 +0300 Subject: [PATCH] [ot] Add hb_ot_fetch_bits() For fetching various bit fields scattered around OpenType tables. Many of these are legacy or unreliable, but applications might need them for various legacy reasons. Fixes https://github.com/harfbuzz/harfbuzz/issues/1920 --- docs/harfbuzz-docs.xml | 1 + docs/harfbuzz-sections.txt | 6 ++ src/harfbuzz-subset.cc | 1 + src/harfbuzz-world.cc | 1 + src/harfbuzz.cc | 1 + src/hb-config.hh | 1 + src/hb-ot-fetch.cc | 79 +++++++++++++++++ src/hb-ot-fetch.h | 76 +++++++++++++++++ src/hb-ot-head-table.hh | 2 + src/hb-ot.h | 1 + src/meson.build | 2 + test/api/meson.build | 1 + test/api/test-ot-fetch.c | 171 +++++++++++++++++++++++++++++++++++++ 13 files changed, 343 insertions(+) create mode 100644 src/hb-ot-fetch.cc create mode 100644 src/hb-ot-fetch.h create mode 100644 test/api/test-ot-fetch.c diff --git a/docs/harfbuzz-docs.xml b/docs/harfbuzz-docs.xml index 464353dfb..0ae264713 100644 --- a/docs/harfbuzz-docs.xml +++ b/docs/harfbuzz-docs.xml @@ -85,6 +85,7 @@ + diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 02683add9..fa15cc5ba 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -834,6 +834,12 @@ hb_ot_var_axis_flags_t hb_ot_var_axis_info_t +
+hb-ot-fetch +hb_ot_bits_tag_t +hb_ot_fetch_bits +
+
hb-set hb_set_create diff --git a/src/harfbuzz-subset.cc b/src/harfbuzz-subset.cc index fb517fe01..f6fd80c9f 100644 --- a/src/harfbuzz-subset.cc +++ b/src/harfbuzz-subset.cc @@ -19,6 +19,7 @@ #include "hb-ot-cff2-table.cc" #include "hb-ot-color.cc" #include "hb-ot-face.cc" +#include "hb-ot-fetch.cc" #include "hb-ot-font.cc" #include "hb-ot-layout.cc" #include "hb-ot-map.cc" diff --git a/src/harfbuzz-world.cc b/src/harfbuzz-world.cc index 7bae7399f..2e5bb127c 100644 --- a/src/harfbuzz-world.cc +++ b/src/harfbuzz-world.cc @@ -49,6 +49,7 @@ #include "hb-ot-cff2-table.cc" #include "hb-ot-color.cc" #include "hb-ot-face.cc" +#include "hb-ot-fetch.cc" #include "hb-ot-font.cc" #include "hb-ot-layout.cc" #include "hb-ot-map.cc" diff --git a/src/harfbuzz.cc b/src/harfbuzz.cc index 471942c55..0ceaeff40 100644 --- a/src/harfbuzz.cc +++ b/src/harfbuzz.cc @@ -27,6 +27,7 @@ #include "hb-ot-cff2-table.cc" #include "hb-ot-color.cc" #include "hb-ot-face.cc" +#include "hb-ot-fetch.cc" #include "hb-ot-font.cc" #include "hb-ot-layout.cc" #include "hb-ot-map.cc" diff --git a/src/hb-config.hh b/src/hb-config.hh index dca05b728..161766fd0 100644 --- a/src/hb-config.hh +++ b/src/hb-config.hh @@ -76,6 +76,7 @@ #define HB_NO_MMAP #define HB_NO_NAME #define HB_NO_OPEN +#define HB_NO_OT_FETCH #define HB_NO_OT_FONT_GLYPH_NAMES #define HB_NO_SETLOCALE #define HB_NO_STYLE diff --git a/src/hb-ot-fetch.cc b/src/hb-ot-fetch.cc new file mode 100644 index 000000000..0052b5f9c --- /dev/null +++ b/src/hb-ot-fetch.cc @@ -0,0 +1,79 @@ +/* + * Copyright © 2026 Khaled Hosny + * + * 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. + */ + +#include "hb.hh" + +#ifndef HB_NO_OT_FETCH + +#include "hb-ot.h" + +#include "hb-ot-head-table.hh" +#include "hb-ot-os2-table.hh" +#include "hb-ot-post-table.hh" + +/** + * SECTION:hb-ot-fetch + * @title: hb-ot-fetch + * @short_description: OpenType bit fields + * @include: hb-ot.h + * + * Functions for fetching various bit fields 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. + **/ + + +/** + * hb_ot_fetch_bits: + * @face: #hb_face_t to work upon + * @tag: tag of the bit field to fetch + * + * Fetches a bit field of @face. + * + * Return value: the bit field, or zero if the font does not have it. + * + * XSince: REPLACEME + **/ +uint32_t +hb_ot_fetch_bits (hb_face_t *face, + hb_ot_bits_tag_t tag) +{ + switch ((unsigned) tag) + { + case HB_OT_BITS_TAG_FS_TYPE: return face->table.OS2->fsType; + case HB_OT_BITS_TAG_FS_SELECTION: return face->table.OS2->fsSelection; + case HB_OT_BITS_TAG_MAC_STYLE: return face->table.head->get_mac_style (); + case HB_OT_BITS_TAG_IS_FIXED_PITCH: return face->table.post->table->isFixedPitch; + case HB_OT_BITS_TAG_UNICODE_RANGE_1: return face->table.OS2->ulUnicodeRange[0]; + case HB_OT_BITS_TAG_UNICODE_RANGE_2: return face->table.OS2->ulUnicodeRange[1]; + case HB_OT_BITS_TAG_UNICODE_RANGE_3: return face->table.OS2->ulUnicodeRange[2]; + case HB_OT_BITS_TAG_UNICODE_RANGE_4: return face->table.OS2->ulUnicodeRange[3]; + case HB_OT_BITS_TAG_CODE_PAGE_RANGE_1: return face->table.OS2->v1 ().ulCodePageRange1; + case HB_OT_BITS_TAG_CODE_PAGE_RANGE_2: return face->table.OS2->v1 ().ulCodePageRange2; + default: return 0; + } +} + +#endif diff --git a/src/hb-ot-fetch.h b/src/hb-ot-fetch.h new file mode 100644 index 000000000..acf05dd28 --- /dev/null +++ b/src/hb-ot-fetch.h @@ -0,0 +1,76 @@ +/* + * Copyright © 2026 Khaled Hosny + * + * 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. + */ + +#if !defined(HB_OT_H_IN) && !defined(HB_NO_SINGLE_HEADER_ERROR) +#error "Include instead." +#endif + +#ifndef HB_OT_FETCH_H +#define HB_OT_FETCH_H + +#include "hb.h" + +HB_BEGIN_DECLS + +/** + * hb_ot_bits_tag_t: + * @HB_OT_BITS_TAG_FS_TYPE: `fsType` of the `OS/2` table. + * @HB_OT_BITS_TAG_FS_SELECTION: `fsSelection` of the `OS/2` table. + * @HB_OT_BITS_TAG_MAC_STYLE: `macStyle` of the `head` table. + * @HB_OT_BITS_TAG_IS_FIXED_PITCH: `isFixedPitch` of the `post` table. + * @HB_OT_BITS_TAG_UNICODE_RANGE_1: `ulUnicodeRange1` of the `OS/2` table. + * @HB_OT_BITS_TAG_UNICODE_RANGE_2: `ulUnicodeRange2` of the `OS/2` table. + * @HB_OT_BITS_TAG_UNICODE_RANGE_3: `ulUnicodeRange3` of the `OS/2` table. + * @HB_OT_BITS_TAG_UNICODE_RANGE_4: `ulUnicodeRange4` of the `OS/2` table. + * @HB_OT_BITS_TAG_CODE_PAGE_RANGE_1: `ulCodePageRange1` of the `OS/2` table. + * @HB_OT_BITS_TAG_CODE_PAGE_RANGE_2: `ulCodePageRange2` of the `OS/2` table. + * + * Bit fields that can be fetched with hb_ot_fetch_bits(). + * + * XSince: REPLACEME + **/ +typedef enum { + HB_OT_BITS_TAG_FS_TYPE = HB_TAG ('f','s','t','p'), + HB_OT_BITS_TAG_FS_SELECTION = HB_TAG ('f','s','s','l'), + HB_OT_BITS_TAG_MAC_STYLE = HB_TAG ('m','c','s','t'), + HB_OT_BITS_TAG_IS_FIXED_PITCH = HB_TAG ('f','x','p','t'), + HB_OT_BITS_TAG_UNICODE_RANGE_1 = HB_TAG ('u','r','n','1'), + HB_OT_BITS_TAG_UNICODE_RANGE_2 = HB_TAG ('u','r','n','2'), + HB_OT_BITS_TAG_UNICODE_RANGE_3 = HB_TAG ('u','r','n','3'), + HB_OT_BITS_TAG_UNICODE_RANGE_4 = HB_TAG ('u','r','n','4'), + HB_OT_BITS_TAG_CODE_PAGE_RANGE_1 = HB_TAG ('c','p','r','1'), + HB_OT_BITS_TAG_CODE_PAGE_RANGE_2 = HB_TAG ('c','p','r','2'), + + /*< private >*/ + _HB_OT_BITS_TAG_MAX_VALUE = HB_TAG_MAX_SIGNED /*< skip >*/ +} hb_ot_bits_tag_t; + + +HB_EXTERN uint32_t +hb_ot_fetch_bits (hb_face_t *face, + hb_ot_bits_tag_t tag); + +HB_END_DECLS + +#endif /* HB_OT_FETCH_H */ diff --git a/src/hb-ot-head-table.hh b/src/hb-ot-head-table.hh index 4cb6c15c6..bb1482fda 100644 --- a/src/hb-ot-head-table.hh +++ b/src/hb-ot-head-table.hh @@ -99,6 +99,8 @@ struct head bool is_condensed () const { return macStyle & CONDENSED; } bool is_expanded () const { return macStyle & EXPANDED; } + unsigned get_mac_style () const { return macStyle; } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); diff --git a/src/hb-ot.h b/src/hb-ot.h index f2dbaa1b3..f0d3521a8 100644 --- a/src/hb-ot.h +++ b/src/hb-ot.h @@ -30,6 +30,7 @@ #include "hb.h" +#include "hb-ot-fetch.h" #include "hb-ot-color.h" #include "hb-ot-deprecated.h" #include "hb-ot-font.h" diff --git a/src/meson.build b/src/meson.build index 299ecd60b..035bed87b 100644 --- a/src/meson.build +++ b/src/meson.build @@ -92,6 +92,7 @@ hb_base_sources = files( 'hb-ot-face-table-list.hh', 'hb-ot-face.cc', 'hb-ot-face.hh', + 'hb-ot-fetch.cc', 'hb-ot-font.cc', 'hb-ot-gasp-table.hh', 'hb-ot-glyf-table.hh', @@ -313,6 +314,7 @@ hb_base_headers = files( 'hb-map.h', 'hb-ot-color.h', 'hb-ot-deprecated.h', + 'hb-ot-fetch.h', 'hb-ot-font.h', 'hb-ot-layout.h', 'hb-ot-math.h', diff --git a/test/api/meson.build b/test/api/meson.build index eba987764..d161456eb 100644 --- a/test/api/meson.build +++ b/test/api/meson.build @@ -30,6 +30,7 @@ tests = [ 'test-ot-collect-glyphs.c', 'test-ot-color.c', 'test-ot-face.c', + 'test-ot-fetch.c', 'test-ot-glyphname.c', 'test-ot-layout.c', 'test-ot-ligature-carets.c', diff --git a/test/api/test-ot-fetch.c b/test/api/test-ot-fetch.c new file mode 100644 index 000000000..ae832ecb1 --- /dev/null +++ b/test/api/test-ot-fetch.c @@ -0,0 +1,171 @@ +/* + * Copyright © 2026 Khaled Hosny + * + * 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. + */ + +#include "hb-test.h" + +#include + +/* Unit tests for hb-ot-fetch.h */ + +static void +test_ot_bits_fs_type (void) +{ + hb_face_t *adwaita = hb_test_open_font_file ("fonts/adwaita.ttf"); + hb_face_t *zycon = hb_test_open_font_file ("fonts/Zycon.ttf"); + hb_face_t *abc = hb_test_open_font_file ("fonts/SourceSansPro-Regular.abc.otf"); + + g_assert_cmpuint (hb_ot_fetch_bits (adwaita, HB_OT_BITS_TAG_FS_TYPE), ==, + 0x0004u); + g_assert_cmpuint (hb_ot_fetch_bits (zycon, HB_OT_BITS_TAG_FS_TYPE), ==, 1); + g_assert_cmpuint (hb_ot_fetch_bits (abc, HB_OT_BITS_TAG_FS_TYPE), ==, + 0x0000u); + + hb_face_destroy (abc); + hb_face_destroy (zycon); + hb_face_destroy (adwaita); +} + +static void +test_ot_bits_fs_selection (void) +{ + hb_face_t *adwaita = hb_test_open_font_file ("fonts/adwaita.ttf"); + hb_face_t *bold = hb_test_open_font_file ("fonts/NotoSans-Bold.ttf"); + hb_face_t *italic = hb_test_open_font_file ("fonts/notosansitalic.ttf"); + hb_face_t *abc = hb_test_open_font_file ("fonts/SourceSansPro-Regular.abc.otf"); + + g_assert_cmpuint (hb_ot_fetch_bits (adwaita, HB_OT_BITS_TAG_FS_SELECTION), ==, + 0x0040u | 0x0080u); + g_assert_cmpuint (hb_ot_fetch_bits (bold, HB_OT_BITS_TAG_FS_SELECTION), ==, + 0x0020u | 0x0080u); + g_assert_cmpuint (hb_ot_fetch_bits (italic, HB_OT_BITS_TAG_FS_SELECTION), ==, + 0x0001u | 0x0100u); + + /* USE_TYPO_METRICS */ + g_assert_true (hb_ot_fetch_bits (adwaita, HB_OT_BITS_TAG_FS_SELECTION) + & 0x0080u); + g_assert_false (hb_ot_fetch_bits (abc, HB_OT_BITS_TAG_FS_SELECTION) + & 0x0080u); + + hb_face_destroy (abc); + hb_face_destroy (italic); + hb_face_destroy (bold); + hb_face_destroy (adwaita); +} + +static void +test_ot_bits_mac_style (void) +{ + hb_face_t *bold = hb_test_open_font_file ("fonts/NotoSans-Bold.ttf"); + hb_face_t *italic = hb_test_open_font_file ("fonts/notosansitalic.ttf"); + hb_face_t *adwaita = hb_test_open_font_file ("fonts/adwaita.ttf"); + + g_assert_cmpuint (hb_ot_fetch_bits (bold, HB_OT_BITS_TAG_MAC_STYLE), ==, + 0x0001u); + g_assert_cmpuint (hb_ot_fetch_bits (italic, HB_OT_BITS_TAG_MAC_STYLE), ==, + 0x0002u); + g_assert_cmpuint (hb_ot_fetch_bits (adwaita, HB_OT_BITS_TAG_MAC_STYLE), ==, 0); + + hb_face_destroy (adwaita); + hb_face_destroy (italic); + hb_face_destroy (bold); +} + +static void +test_ot_bits_is_fixed_pitch (void) +{ + hb_face_t *mono = hb_test_open_font_file ("fonts/Inconsolata-Regular.abc.ttf"); + hb_face_t *adwaita = hb_test_open_font_file ("fonts/adwaita.ttf"); + + g_assert_cmpuint (hb_ot_fetch_bits (mono, HB_OT_BITS_TAG_IS_FIXED_PITCH), !=, 0); + g_assert_cmpuint (hb_ot_fetch_bits (adwaita, HB_OT_BITS_TAG_IS_FIXED_PITCH), ==, 0); + + hb_face_destroy (adwaita); + hb_face_destroy (mono); +} + +static void +test_ot_bits_ranges (void) +{ + hb_face_t *bold = hb_test_open_font_file ("fonts/NotoSans-Bold.ttf"); + hb_face_t *mplus = hb_test_open_font_file ("fonts/Mplus1p-Regular.ttf"); + hb_face_t *zycon = hb_test_open_font_file ("fonts/Zycon.ttf"); + + g_assert_cmpuint (hb_ot_fetch_bits (bold, HB_OT_BITS_TAG_UNICODE_RANGE_1), ==, 0xE00002FFu); + g_assert_cmpuint (hb_ot_fetch_bits (bold, HB_OT_BITS_TAG_UNICODE_RANGE_2), ==, 0x4000201Fu); + g_assert_cmpuint (hb_ot_fetch_bits (bold, HB_OT_BITS_TAG_UNICODE_RANGE_3), ==, 0x08000029u); + g_assert_cmpuint (hb_ot_fetch_bits (bold, HB_OT_BITS_TAG_UNICODE_RANGE_4), ==, 0x00100000u); + + g_assert_cmpuint (hb_ot_fetch_bits (mplus, HB_OT_BITS_TAG_CODE_PAGE_RANGE_1), ==, 0x601201BFu); + g_assert_cmpuint (hb_ot_fetch_bits (mplus, HB_OT_BITS_TAG_CODE_PAGE_RANGE_2), ==, 0xDFF70000u); + + /* OS/2 v0 does not have code page ranges */ + g_assert_cmpuint (hb_ot_fetch_bits (zycon, HB_OT_BITS_TAG_CODE_PAGE_RANGE_1), ==, 0); + g_assert_cmpuint (hb_ot_fetch_bits (zycon, HB_OT_BITS_TAG_CODE_PAGE_RANGE_2), ==, 0); + + hb_face_destroy (zycon); + hb_face_destroy (mplus); + hb_face_destroy (bold); +} + +static void +test_ot_bits_missing_tables (void) +{ + /* No OS/2 or post tables */ + hb_face_t *face = hb_test_open_font_file ("fonts/meta.ttf"); + + g_assert_cmpuint (hb_ot_fetch_bits (face, HB_OT_BITS_TAG_FS_TYPE), ==, 0); + g_assert_cmpuint (hb_ot_fetch_bits (face, HB_OT_BITS_TAG_FS_SELECTION), ==, 0); + g_assert_cmpuint (hb_ot_fetch_bits (face, HB_OT_BITS_TAG_IS_FIXED_PITCH), ==, 0); + g_assert_cmpuint (hb_ot_fetch_bits (face, HB_OT_BITS_TAG_UNICODE_RANGE_1), ==, 0); + g_assert_cmpuint (hb_ot_fetch_bits (face, HB_OT_BITS_TAG_CODE_PAGE_RANGE_1), ==, 0); + + hb_face_destroy (face); +} + +static void +test_ot_bits_invalid (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/adwaita.ttf"); + + g_assert_cmpuint (hb_ot_fetch_bits (face, (hb_ot_bits_tag_t) HB_TAG ('x','x','x','x')), ==, 0); + g_assert_cmpuint (hb_ot_fetch_bits (hb_face_get_empty (), HB_OT_BITS_TAG_FS_TYPE), ==, 0); + + hb_face_destroy (face); +} + +int +main (int argc, char **argv) +{ + hb_test_init (&argc, &argv); + + hb_test_add (test_ot_bits_fs_type); + hb_test_add (test_ot_bits_fs_selection); + hb_test_add (test_ot_bits_mac_style); + hb_test_add (test_ot_bits_is_fixed_pitch); + hb_test_add (test_ot_bits_ranges); + hb_test_add (test_ot_bits_missing_tables); + hb_test_add (test_ot_bits_invalid); + + return hb_test_run (); +}