xref: /aosp_15_r20/external/harfbuzz_ng/src/OT/Layout/GSUB/AlternateSet.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
1 #ifndef OT_LAYOUT_GSUB_ALTERNATESET_HH
2 #define OT_LAYOUT_GSUB_ALTERNATESET_HH
3 
4 #include "Common.hh"
5 
6 namespace OT {
7 namespace Layout {
8 namespace GSUB_impl {
9 
10 template <typename Types>
11 struct AlternateSet
12 {
13   protected:
14   Array16Of<typename Types::HBGlyphID>
15                 alternates;             /* Array of alternate GlyphIDs--in
16                                          * arbitrary order */
17   public:
18   DEFINE_SIZE_ARRAY (2, alternates);
19 
sanitizeOT::Layout::GSUB_impl::AlternateSet20   bool sanitize (hb_sanitize_context_t *c) const
21   {
22     TRACE_SANITIZE (this);
23     return_trace (alternates.sanitize (c));
24   }
25 
intersectsOT::Layout::GSUB_impl::AlternateSet26   bool intersects (const hb_set_t *glyphs) const
27   { return hb_any (alternates, glyphs); }
28 
closureOT::Layout::GSUB_impl::AlternateSet29   void closure (hb_closure_context_t *c) const
30   { c->output->add_array (alternates.arrayZ, alternates.len); }
31 
collect_glyphsOT::Layout::GSUB_impl::AlternateSet32   void collect_glyphs (hb_collect_glyphs_context_t *c) const
33   { c->output->add_array (alternates.arrayZ, alternates.len); }
34 
applyOT::Layout::GSUB_impl::AlternateSet35   bool apply (hb_ot_apply_context_t *c) const
36   {
37     TRACE_APPLY (this);
38     unsigned int count = alternates.len;
39 
40     if (unlikely (!count)) return_trace (false);
41 
42     hb_mask_t glyph_mask = c->buffer->cur().mask;
43     hb_mask_t lookup_mask = c->lookup_mask;
44 
45     /* Note: This breaks badly if two features enabled this lookup together. */
46     unsigned int shift = hb_ctz (lookup_mask);
47     unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
48 
49     /* If alt_index is MAX_VALUE, randomize feature if it is the rand feature. */
50     if (alt_index == HB_OT_MAP_MAX_VALUE && c->random)
51     {
52       /* Maybe we can do better than unsafe-to-break all; but since we are
53        * changing random state, it would be hard to track that.  Good 'nough. */
54       c->buffer->unsafe_to_break (0, c->buffer->len);
55       alt_index = c->random_number () % count + 1;
56     }
57 
58     if (unlikely (alt_index > count || alt_index == 0)) return_trace (false);
59 
60     if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
61     {
62       c->buffer->sync_so_far ();
63       c->buffer->message (c->font,
64 			  "replacing glyph at %u (alternate substitution)",
65 			  c->buffer->idx);
66     }
67 
68     c->replace_glyph (alternates[alt_index - 1]);
69 
70     if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
71     {
72       c->buffer->message (c->font,
73 			  "replaced glyph at %u (alternate substitution)",
74 			  c->buffer->idx - 1u);
75     }
76 
77     return_trace (true);
78   }
79 
80   unsigned
get_alternatesOT::Layout::GSUB_impl::AlternateSet81   get_alternates (unsigned        start_offset,
82                   unsigned       *alternate_count  /* IN/OUT.  May be NULL. */,
83                   hb_codepoint_t *alternate_glyphs /* OUT.     May be NULL. */) const
84   {
85     if (alternates.len && alternate_count)
86     {
87       + alternates.as_array ().sub_array (start_offset, alternate_count)
88       | hb_sink (hb_array (alternate_glyphs, *alternate_count))
89       ;
90     }
91     return alternates.len;
92   }
93 
94   template <typename Iterator,
95             hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))>
serializeOT::Layout::GSUB_impl::AlternateSet96   bool serialize (hb_serialize_context_t *c,
97                   Iterator alts)
98   {
99     TRACE_SERIALIZE (this);
100     return_trace (alternates.serialize (c, alts));
101   }
102 
subsetOT::Layout::GSUB_impl::AlternateSet103   bool subset (hb_subset_context_t *c) const
104   {
105     TRACE_SUBSET (this);
106     const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
107     const hb_map_t &glyph_map = *c->plan->glyph_map;
108 
109     auto it =
110       + hb_iter (alternates)
111       | hb_filter (glyphset)
112       | hb_map (glyph_map)
113       ;
114 
115     auto *out = c->serializer->start_embed (*this);
116     return_trace (out->serialize (c->serializer, it) &&
117                   out->alternates);
118   }
119 };
120 
121 }
122 }
123 }
124 
125 
126 #endif /* OT_LAYOUT_GSUB_ALTERNATESET_HH */
127