Add hb_ot_layout_lookup_collect_glyph_alternates() (#5367)

* [map] Massage operator << overloads

* [ot-layout] Add +hb_ot_layout_lookup_collect_glyph_alternates

To collect all glyph mapping from SingleSubst or AlternateSubst
lookups in one call.  Needed by FreeType autohinter for performance.

New API:
+hb_ot_layout_lookup_collect_glyph_alternates()

* [layout] Change hb_ot_layout_lookup_collect_glyph_alternates() API

https://github.com/harfbuzz/harfbuzz/pull/5367#discussion_r2149019638
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2025-09-30 13:31:04 -06:00
1 parent 61b6a262cc
commit 4a26e289f7
9 files changed
+139 -1

No files matched your search

+1
View File
@@ -704,6 +704,7 @@ hb_ot_layout_language_get_feature_tags
hb_ot_layout_language_get_required_feature
hb_ot_layout_lookup_collect_glyphs
hb_ot_layout_lookup_get_glyph_alternates
hb_ot_layout_lookup_collect_glyph_alternates
hb_ot_layout_lookup_get_optical_bound
hb_ot_layout_lookup_substitute_closure
hb_ot_layout_lookups_substitute_closure
+13
View File
@@ -91,6 +91,19 @@ struct AlternateSet
return alternates.len;
}
void
collect_alternates (hb_codepoint_t gid,
hb_map_t *alternate_count /* IN/OUT */,
hb_map_t *alternate_glyphs /* IN/OUT */) const
{
+ hb_enumerate (alternates)
| hb_map ([gid] (hb_pair_t<unsigned, hb_codepoint_t> _) { return hb_pair (gid + (_.first << 24), _.second); })
| hb_apply ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> &p) -> void
{ _hb_collect_glyph_alternates_add (p.first, p.second,
alternate_count, alternate_glyphs); })
;
}
template <typename Iterator,
hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))>
bool serialize (hb_serialize_context_t *c,
@@ -69,6 +69,19 @@ struct AlternateSubstFormat1_2
{ return (this+alternateSet[(this+coverage).get_coverage (gid)])
.get_alternates (start_offset, alternate_count, alternate_glyphs); }
void
collect_glyph_alternates (hb_map_t *alternate_count /* IN/OUT */,
hb_map_t *alternate_glyphs /* IN/OUT */) const
{
+ hb_iter (alternateSet)
| hb_map (hb_add (this))
| hb_zip (this+coverage)
| hb_apply ([&] (const hb_pair_t<const AlternateSet<Types> &, hb_codepoint_t> _) {
_.first.collect_alternates (_.second, alternate_count, alternate_glyphs);
})
;
}
bool apply (hb_ot_apply_context_t *c) const
{
TRACE_APPLY (this);
+15
View File
@@ -123,6 +123,21 @@ struct SingleSubstFormat1_3
return 1;
}
void
collect_glyph_alternates (hb_map_t *alternate_count /* IN/OUT */,
hb_map_t *alternate_glyphs /* IN/OUT */) const
{
hb_codepoint_t d = deltaGlyphID;
hb_codepoint_t mask = get_mask ();
+ hb_iter (this+coverage)
| hb_map ([d, mask] (hb_codepoint_t g) { return hb_pair (g, (g + d) & mask); })
| hb_apply ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> &p) -> void
{ _hb_collect_glyph_alternates_add (p.first, p.second,
alternate_count, alternate_glyphs); })
;
}
bool apply (hb_ot_apply_context_t *c) const
{
TRACE_APPLY (this);
+11
View File
@@ -100,6 +100,17 @@ struct SingleSubstFormat2_4
return 1;
}
void
collect_glyph_alternates (hb_map_t *alternate_count /* IN/OUT */,
hb_map_t *alternate_glyphs /* IN/OUT */) const
{
+ hb_zip (this+coverage, substitute)
| hb_apply ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> &p) -> void
{ _hb_collect_glyph_alternates_add (p.first, p.second,
alternate_count, alternate_glyphs); })
;
}
bool apply (hb_ot_apply_context_t *c) const
{
TRACE_APPLY (this);
+7
View File
@@ -491,10 +491,17 @@ struct hb_hashmap_t
/* Sink interface. */
hb_hashmap_t& operator << (const hb_pair_t<K, V>& v)
{ set (v.first, v.second); return *this; }
template <typename V2 = V,
hb_enable_if (!std::is_trivially_copyable<V2>::value)>
hb_hashmap_t& operator << (const hb_pair_t<K, V&&>& v)
{ set (v.first, std::move (v.second)); return *this; }
template <typename K2 = K,
hb_enable_if (!std::is_trivially_copyable<K2>::value)>
hb_hashmap_t& operator << (const hb_pair_t<K&&, V>& v)
{ set (std::move (v.first), v.second); return *this; }
template <typename K2 = K, typename V2 = V,
hb_enable_if (!std::is_trivially_copyable<K2>::value &&
!std::is_trivially_copyable<V2>::value)>
hb_hashmap_t& operator << (const hb_pair_t<K&&, V&&>& v)
{ set (std::move (v.first), std::move (v.second)); return *this; }
+59 -1
View File
@@ -2597,6 +2597,7 @@ hb_ot_layout_get_baseline_with_fallback2 (hb_font_t *font,
#endif
#ifndef HB_NO_LAYOUT_RARELY_USED
struct hb_get_glyph_alternates_dispatch_t :
hb_dispatch_context_t<hb_get_glyph_alternates_dispatch_t, unsigned>
{
@@ -2616,7 +2617,6 @@ struct hb_get_glyph_alternates_dispatch_t :
( _dispatch (obj, hb_prioritize, std::forward<Ts> (ds)...) )
};
#ifndef HB_NO_LAYOUT_RARELY_USED
/**
* hb_ot_layout_lookup_get_glyph_alternates:
* @face: a face.
@@ -2650,6 +2650,64 @@ hb_ot_layout_lookup_get_glyph_alternates (hb_face_t *face,
return ret;
}
struct hb_collect_glyph_alternates_dispatch_t :
hb_dispatch_context_t<hb_collect_glyph_alternates_dispatch_t, bool>
{
static return_t default_return_value () { return false; }
bool stop_sublookup_iteration (return_t r) const { return false; }
private:
template <typename T, typename ...Ts> auto
_dispatch (const T &obj, hb_priority<1>, Ts&&... ds) HB_AUTO_RETURN
( (obj.collect_glyph_alternates (std::forward<Ts> (ds)...), true) )
template <typename T, typename ...Ts> auto
_dispatch (const T &obj, hb_priority<0>, Ts&&... ds) HB_AUTO_RETURN
( default_return_value () )
public:
template <typename T, typename ...Ts> auto
dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN
( _dispatch (obj, hb_prioritize, std::forward<Ts> (ds)...) )
};
/**
* hb_ot_layout_lookup_collect_glyph_alternates:
* @face: a face.
* @lookup_index: index of the feature lookup to query.
* @alternate_count: (inout): mapping from glyph index to number of alternates for that glyph.
* @alternate_glyphs: (inout): mapping from encoded glyph index and alternate index, to alternate glyph ids.
*
* Collects alternates of glyphs from a given GSUB lookup index.
*
* For one-to-one GSUB glyph substitutions, this function collects the
* substituted glyph.
*
* For lookups that assign multiple alternates to a glyph, all alternate glyphs are collected.
*
* For other lookup types, nothing is performed and `false` is returned.
*
* The `alternate_count` mapping will contain the number of alternates for each glyph id.
* Upon entry, this mapping should contain the glyph ids as keys, and the number of alternates
* currently known for each glyph id as values.
*
* The `alternate_glyphs` mapping will contain the alternate glyph ids for each glyph id.
* The mapping is encoded in the following way, upon entry and after processing:
* If G is the glyph id, and A0, A1, ..., A(n-1) are the alternate glyph ids,
* the mapping will contain the following entries: (G + (i << 24)) -> A(i)
* for i = 0, 1, ..., n-1 where n is the number of alternates for G as per `alternate_count`.
*
* Return value: `true` if alternates were collected, `false` otherwise.
* XSince: REPLACEME
*/
HB_EXTERN hb_bool_t
hb_ot_layout_lookup_collect_glyph_alternates (hb_face_t *face,
unsigned lookup_index,
hb_map_t *alternate_count /* IN/OUT */,
hb_map_t *alternate_glyphs /* IN/OUT */)
{
hb_collect_glyph_alternates_dispatch_t c;
const OT::SubstLookup &lookup = face->table.GSUB->table->get_lookup (lookup_index);
return lookup.dispatch (&c, alternate_count, alternate_glyphs);
}
struct hb_position_single_dispatch_t :
hb_dispatch_context_t<hb_position_single_dispatch_t, bool>
+6
View File
@@ -383,6 +383,12 @@ hb_ot_layout_lookup_get_glyph_alternates (hb_face_t *face,
unsigned *alternate_count /* IN/OUT */,
hb_codepoint_t *alternate_glyphs /* OUT */);
HB_EXTERN hb_bool_t
hb_ot_layout_lookup_collect_glyph_alternates (hb_face_t *face,
unsigned lookup_index,
hb_map_t *alternate_count /* IN/OUT */,
hb_map_t *alternate_glyphs /* IN/OUT */);
HB_EXTERN hb_bool_t
hb_ot_layout_lookup_would_substitute (hb_face_t *face,
unsigned int lookup_index,
+14
View File
@@ -645,4 +645,18 @@ _hb_buffer_assert_gsubgpos_vars (hb_buffer_t *buffer)
#undef lig_props
#undef glyph_props
static inline void
_hb_collect_glyph_alternates_add (hb_codepoint_t from,
hb_codepoint_t to,
hb_map_t *alternate_count,
hb_map_t *alternate_glyphs)
{
hb_codepoint_t zero = 0;
hb_codepoint_t *i = &zero;
alternate_count->has (from, &i);
alternate_glyphs->set (from | (*i << 24), to);
alternate_count->set (from, *i + 1);
}
#endif /* HB_OT_LAYOUT_HH */