1 #ifndef OT_LAYOUT_GSUB_ALTERNATESET_HH
2 #define OT_LAYOUT_GSUB_ALTERNATESET_HH
10 template <typename Types>
14 Array16Of<typename Types::HBGlyphID>
15 alternates; /* Array of alternate GlyphIDs--in
18 DEFINE_SIZE_ARRAY (2, alternates);
20 bool sanitize (hb_sanitize_context_t *c) const
22 TRACE_SANITIZE (this);
23 return_trace (alternates.sanitize (c));
26 bool intersects (const hb_set_t *glyphs) const
27 { return hb_any (alternates, glyphs); }
29 void closure (hb_closure_context_t *c) const
30 { c->output->add_array (alternates.arrayZ, alternates.len); }
32 void collect_glyphs (hb_collect_glyphs_context_t *c) const
33 { c->output->add_array (alternates.arrayZ, alternates.len); }
35 bool apply (hb_ot_apply_context_t *c) const
38 unsigned int count = alternates.len;
40 if (unlikely (!count)) return_trace (false);
42 hb_mask_t glyph_mask = c->buffer->cur().mask;
43 hb_mask_t lookup_mask = c->lookup_mask;
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);
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)
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;
58 if (unlikely (alt_index > count || alt_index == 0)) return_trace (false);
60 if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
62 c->buffer->sync_so_far ();
63 c->buffer->message (c->font,
64 "replacing glyph at %u (alternate substitution)",
68 c->replace_glyph (alternates[alt_index - 1]);
70 if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
72 c->buffer->message (c->font,
73 "replaced glyph at %u (alternate substitution)",
81 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
85 if (alternates.len && alternate_count)
87 + alternates.as_array ().sub_array (start_offset, alternate_count)
88 | hb_sink (hb_array (alternate_glyphs, *alternate_count))
91 return alternates.len;
94 template <typename Iterator,
95 hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))>
96 bool serialize (hb_serialize_context_t *c,
99 TRACE_SERIALIZE (this);
100 return_trace (alternates.serialize (c, alts));
103 bool subset (hb_subset_context_t *c) const
106 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
107 const hb_map_t &glyph_map = *c->plan->glyph_map;
110 + hb_iter (alternates)
111 | hb_filter (glyphset)
115 auto *out = c->serializer->start_embed (*this);
116 return_trace (out->serialize (c->serializer, it) &&
126 #endif /* OT_LAYOUT_GSUB_ALTERNATESET_HH */