/src/harfbuzz/src/OT/Layout/GSUB/AlternateSet.hh
Line | Count | Source |
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 | | |
20 | | bool sanitize (hb_sanitize_context_t *c) const |
21 | 2.74M | { |
22 | 2.74M | TRACE_SANITIZE (this); |
23 | 2.74M | return_trace (alternates.sanitize (c)); |
24 | 2.74M | } |
25 | | |
26 | | bool intersects (const hb_set_t *glyphs) const |
27 | | { return hb_any (alternates, glyphs); } |
28 | | |
29 | | void closure (hb_closure_context_t *c) const |
30 | 0 | { c->output->add_array (alternates.arrayZ, alternates.len); } |
31 | | |
32 | | void depend (hb_depend_context_t *c, hb_codepoint_t source) const |
33 | 0 | { |
34 | 0 | + hb_iter (alternates) |
35 | 0 | | hb_apply ([&] (const hb_codepoint_t &target) { c->depend_data->add_gsub_lookup (source, c->lookup_index, target); }) |
36 | 0 | ; |
37 | 0 | } |
38 | | |
39 | | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
40 | 0 | { c->output->add_array (alternates.arrayZ, alternates.len); } |
41 | | |
42 | | bool apply (hb_ot_apply_context_t *c) const |
43 | 0 | { |
44 | 0 | TRACE_APPLY (this); |
45 | 0 | unsigned int count = alternates.len; |
46 | |
|
47 | 0 | if (unlikely (!count)) return_trace (false); |
48 | | |
49 | 0 | hb_mask_t glyph_mask = c->buffer->cur().mask; |
50 | 0 | hb_mask_t lookup_mask = c->lookup_mask; |
51 | | |
52 | | /* Note: This breaks badly if two features enabled this lookup together. */ |
53 | 0 | unsigned int shift = hb_ctz (lookup_mask); |
54 | 0 | unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift); |
55 | | |
56 | | /* If alt_index is MAX_VALUE, randomize feature if it is the rand feature. */ |
57 | 0 | if (alt_index == HB_OT_MAP_MAX_VALUE && c->random) |
58 | 0 | { |
59 | | /* Maybe we can do better than unsafe-to-break all; but since we are |
60 | | * changing random state, it would be hard to track that. Good 'nough. */ |
61 | 0 | c->buffer->unsafe_to_break (0, c->buffer->len); |
62 | 0 | alt_index = c->random_number () % count + 1; |
63 | 0 | } |
64 | |
|
65 | 0 | if (unlikely (alt_index > count || alt_index == 0)) return_trace (false); |
66 | | |
67 | 0 | if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) |
68 | 0 | { |
69 | 0 | c->buffer->sync_so_far (); |
70 | 0 | c->buffer->message (c->font, |
71 | 0 | "replacing glyph at %u (alternate substitution)", |
72 | 0 | c->buffer->idx); |
73 | 0 | } |
74 | |
|
75 | 0 | c->replace_glyph (alternates[alt_index - 1]); |
76 | |
|
77 | 0 | if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) |
78 | 0 | { |
79 | 0 | c->buffer->message (c->font, |
80 | 0 | "replaced glyph at %u (alternate substitution)", |
81 | 0 | c->buffer->idx - 1u); |
82 | 0 | } |
83 | |
|
84 | 0 | return_trace (true); |
85 | 0 | } |
86 | | |
87 | | unsigned |
88 | | get_alternates (unsigned start_offset, |
89 | | unsigned *alternate_count /* IN/OUT. May be NULL. */, |
90 | | hb_codepoint_t *alternate_glyphs /* OUT. May be NULL. */) const |
91 | 0 | { |
92 | 0 | if (alternates.len && alternate_count && alternate_glyphs) |
93 | 0 | { |
94 | 0 | + alternates.as_array ().sub_array (start_offset, alternate_count) |
95 | 0 | | hb_sink (hb_array (alternate_glyphs, *alternate_count)) |
96 | 0 | ; |
97 | 0 | } |
98 | 0 | return alternates.len; |
99 | 0 | } |
100 | | |
101 | | void |
102 | | collect_alternates (hb_codepoint_t gid, |
103 | | hb_map_t *alternate_count /* IN/OUT */, |
104 | | hb_map_t *alternate_glyphs /* IN/OUT */) const |
105 | 0 | { |
106 | 0 | + hb_enumerate (alternates) |
107 | 0 | | hb_map ([gid] (hb_pair_t<unsigned, hb_codepoint_t> _) { return hb_pair (gid + (_.first << 24), _.second); }) |
108 | 0 | | hb_apply ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> &p) -> void |
109 | 0 | { _hb_collect_glyph_alternates_add (p.first, p.second, |
110 | 0 | alternate_count, alternate_glyphs); }) |
111 | 0 | ; |
112 | 0 | } |
113 | | |
114 | | template <typename Iterator, |
115 | | hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))> |
116 | | bool serialize (hb_serialize_context_t *c, |
117 | | Iterator alts) |
118 | 0 | { |
119 | 0 | TRACE_SERIALIZE (this); |
120 | 0 | return_trace (alternates.serialize (c, alts)); |
121 | 0 | } Unexecuted instantiation: _ZN2OT6Layout9GSUB_impl12AlternateSetINS0_10SmallTypesEE9serializeI10hb_array_tIKNS_11HBGlyphID16EETnPN12hb_enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELPv0EEEbP22hb_serialize_context_tSB_ Unexecuted instantiation: hb-ot-face.cc:_ZN2OT6Layout9GSUB_impl12AlternateSetINS0_10SmallTypesEE9serializeI13hb_map_iter_tI16hb_filter_iter_tI10hb_array_tIKNS_11HBGlyphID16EERK8hb_set_tRK4$_19LPv0EERK8hb_map_tL24hb_function_sortedness_t0ELSI_0EETnPN12hb_enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELSI_0EEEbP22hb_serialize_context_tSQ_ Unexecuted instantiation: hb-aat-layout.cc:_ZN2OT6Layout9GSUB_impl12AlternateSetINS0_10SmallTypesEE9serializeI13hb_map_iter_tI16hb_filter_iter_tI10hb_array_tIKNS_11HBGlyphID16EERK8hb_set_tRK4$_19LPv0EERK8hb_map_tL24hb_function_sortedness_t0ELSI_0EETnPN12hb_enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELSI_0EEEbP22hb_serialize_context_tSQ_ Unexecuted instantiation: hb-ot-layout.cc:_ZN2OT6Layout9GSUB_impl12AlternateSetINS0_10SmallTypesEE9serializeI13hb_map_iter_tI16hb_filter_iter_tI10hb_array_tIKNS_11HBGlyphID16EERK8hb_set_tRK4$_19LPv0EERK8hb_map_tL24hb_function_sortedness_t0ELSI_0EETnPN12hb_enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELSI_0EEEbP22hb_serialize_context_tSQ_ Unexecuted instantiation: hb-ot-shaper-arabic.cc:_ZN2OT6Layout9GSUB_impl12AlternateSetINS0_10SmallTypesEE9serializeI13hb_map_iter_tI16hb_filter_iter_tI10hb_array_tIKNS_11HBGlyphID16EERK8hb_set_tRK4$_19LPv0EERK8hb_map_tL24hb_function_sortedness_t0ELSI_0EETnPN12hb_enable_ifIXsr15hb_is_source_ofIT_jEE5valueEvE4typeELSI_0EEEbP22hb_serialize_context_tSQ_ |
122 | | |
123 | | bool subset (hb_subset_context_t *c) const |
124 | 0 | { |
125 | 0 | TRACE_SUBSET (this); |
126 | 0 | const hb_set_t &glyphset = *c->plan->glyphset_gsub (); |
127 | 0 | const hb_map_t &glyph_map = *c->plan->glyph_map; |
128 | 0 |
|
129 | 0 | auto it = |
130 | 0 | + hb_iter (alternates) |
131 | 0 | | hb_filter (glyphset) |
132 | 0 | | hb_map (glyph_map) |
133 | 0 | ; |
134 | 0 |
|
135 | 0 | auto *out = c->serializer->start_embed (*this); |
136 | 0 | return_trace (out->serialize (c->serializer, it) && |
137 | 0 | out->alternates); |
138 | 0 | } |
139 | | }; |
140 | | |
141 | | } |
142 | | } |
143 | | } |
144 | | |
145 | | |
146 | | #endif /* OT_LAYOUT_GSUB_ALTERNATESET_HH */ |