From c7ef6a2ed58ae8ec108ee0962bef46f42c73a60c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Sep 2024 19:42:40 -0600 Subject: [PATCH] Remove the hack re variation-selectors Instead of abusing an unused Gen_Cat value, use existing facilities to remember variation selectors. Addresses https://github.com/RazrFalcon/rustybuzz/pull/134#issuecomment-2374894164 --- src/hb-ot-layout.hh | 35 +++++++++++++++++++++++++++++++---- src/hb-ot-shape-normalize.cc | 3 +-- src/hb-ot-shape.cc | 5 ++--- src/hb-unicode.hh | 3 --- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/hb-ot-layout.hh b/src/hb-ot-layout.hh index 3a8d36ac4..f26bf51ab 100644 --- a/src/hb-ot-layout.hh +++ b/src/hb-ot-layout.hh @@ -173,7 +173,7 @@ _hb_next_syllable (hb_buffer_t *buffer, unsigned int start) /* Design: * unicode_props() is a two-byte number. The low byte includes: - * - Extended General_Category: 5 bits. + * - Modified General_Category: 5 bits. * - A bit each for: * * Is it Default_Ignorable(); we have a modified Default_Ignorable(). * * Whether it's one of the four Mongolian Free Variation Selectors, @@ -186,17 +186,23 @@ _hb_next_syllable (hb_buffer_t *buffer, unsigned int start) * - For Cf: whether it's ZWJ, ZWNJ, or something else. * - For Ws: index of which space character this is, if space fallback * is needed, ie. we don't set this by default, only if asked to. + * + * Above I said "modified" General_Category. This is because we need to + * remember Variation Selectors, and we don't have bits left. So we + * change their Gen_Cat from Mn to Cf, and use a bit of the high byte to + * remember them. */ enum hb_unicode_props_flags_t { UPROPS_MASK_GEN_CAT = 0x001Fu, UPROPS_MASK_IGNORABLE = 0x0020u, - UPROPS_MASK_HIDDEN = 0x0040u, /* MONGOLIAN FREE VARIATION SELECTOR 1..4, or TAG characters */ + UPROPS_MASK_HIDDEN = 0x0040u, /* MONGOLIAN FREE VARIATION SELECTOR 1..4, or TAG characters, or CGJ sometimes */ UPROPS_MASK_CONTINUATION=0x0080u, /* If GEN_CAT=FORMAT, top byte masks: */ UPROPS_MASK_Cf_ZWJ = 0x0100u, - UPROPS_MASK_Cf_ZWNJ = 0x0200u + UPROPS_MASK_Cf_ZWNJ = 0x0200u, + UPROPS_MASK_Cf_VS = 0x0400u }; HB_MARK_AS_FLAG_T (hb_unicode_props_flags_t); @@ -229,7 +235,7 @@ _hb_glyph_info_set_unicode_props (hb_glyph_info_t *info, hb_buffer_t *buffer) /* TAG characters need similar treatment. Fixes: * https://github.com/harfbuzz/harfbuzz/issues/463 */ else if (unlikely (hb_in_range (u, 0xE0020u, 0xE007Fu))) props |= UPROPS_MASK_HIDDEN; - /* COMBINING GRAPHEME JOINER should not be skipped; at least some times. + /* COMBINING GRAPHEME JOINER should not be skipped during GSUB either. * https://github.com/harfbuzz/harfbuzz/issues/554 */ else if (unlikely (u == 0x034Fu)) { @@ -302,6 +308,27 @@ _hb_glyph_info_get_unicode_space_fallback_type (const hb_glyph_info_t *info) (hb_unicode_funcs_t::space_t) (info->unicode_props()>>8) : hb_unicode_funcs_t::NOT_SPACE; } +static inline bool +_hb_glyph_info_is_variation_selector (const hb_glyph_info_t *info) +{ + return _hb_glyph_info_get_general_category (info) == + HB_UNICODE_GENERAL_CATEGORY_FORMAT && + (info->unicode_props() & UPROPS_MASK_Cf_VS); +} +static inline void +_hb_glyph_info_set_variation_selector (hb_glyph_info_t *info, bool customize) +{ + if (customize) + { + _hb_glyph_info_set_general_category (info, HB_UNICODE_GENERAL_CATEGORY_FORMAT); + info->unicode_props() |= UPROPS_MASK_Cf_VS; + } + else + { + // Reset to their original condition + _hb_glyph_info_set_general_category (info, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK); + } +} static inline bool _hb_glyph_info_substituted (const hb_glyph_info_t *info); diff --git a/src/hb-ot-shape-normalize.cc b/src/hb-ot-shape-normalize.cc index b86649d77..2d093f78b 100644 --- a/src/hb-ot-shape-normalize.cc +++ b/src/hb-ot-shape-normalize.cc @@ -222,8 +222,7 @@ handle_variation_selector_cluster (const hb_ot_shape_normalize_context_t *c, (void) buffer->next_glyph (); buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_VARIATION_SELECTOR_FALLBACK; - _hb_glyph_info_set_general_category (&buffer->cur(), - _HB_UNICODE_GENERAL_CATEGORY_VARIATION_SELECTOR); + _hb_glyph_info_set_variation_selector (&buffer->cur(), true); if (buffer->not_found_variation_selector != HB_CODEPOINT_INVALID) _hb_glyph_info_clear_default_ignorable (&buffer->cur()); diff --git a/src/hb-ot-shape.cc b/src/hb-ot-shape.cc index 1aca39101..59b97a799 100644 --- a/src/hb-ot-shape.cc +++ b/src/hb-ot-shape.cc @@ -850,12 +850,11 @@ hb_ot_deal_with_variation_selectors (hb_buffer_t *buffer) for (unsigned int i = 0; i < count; i++) { - if (_hb_glyph_info_get_general_category (&info[i]) == - _HB_UNICODE_GENERAL_CATEGORY_VARIATION_SELECTOR) + if (_hb_glyph_info_is_variation_selector (&info[i])) { info[i].codepoint = buffer->not_found_variation_selector; pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0; - _hb_glyph_info_set_general_category (&info[i], HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK); + _hb_glyph_info_set_variation_selector (&info[i], false); } } } diff --git a/src/hb-unicode.hh b/src/hb-unicode.hh index 2b1921c28..39aaee5ba 100644 --- a/src/hb-unicode.hh +++ b/src/hb-unicode.hh @@ -34,9 +34,6 @@ #include "hb.hh" -// Hack. See: https://github.com/harfbuzz/harfbuzz/pull/4529#discussion_r1769638033 -#define _HB_UNICODE_GENERAL_CATEGORY_VARIATION_SELECTOR ((hb_unicode_general_category_t) 30) - extern HB_INTERNAL const uint8_t _hb_modified_combining_class[256]; /*