/src/harfbuzz/src/OT/Layout/GSUB/SingleSubstFormat1.hh
Line | Count | Source |
1 | | #ifndef OT_LAYOUT_GSUB_SINGLESUBSTFORMAT1_HH |
2 | | #define OT_LAYOUT_GSUB_SINGLESUBSTFORMAT1_HH |
3 | | |
4 | | #include "Common.hh" |
5 | | |
6 | | namespace OT { |
7 | | namespace Layout { |
8 | | namespace GSUB_impl { |
9 | | |
10 | | template <typename Types> |
11 | | struct SingleSubstFormat1_3 |
12 | | { |
13 | | protected: |
14 | | HBUINT16 format; /* Format identifier--format = 1 */ |
15 | | typename Types::template OffsetTo<Coverage> |
16 | | coverage; /* Offset to Coverage table--from |
17 | | * beginning of Substitution table */ |
18 | | typename Types::HBUINT |
19 | | deltaGlyphID; /* Add to original GlyphID to get |
20 | | * substitute GlyphID, modulo 0x10000 */ |
21 | | |
22 | | public: |
23 | | DEFINE_SIZE_STATIC (2 + 2 * Types::size); |
24 | | |
25 | | bool sanitize (hb_sanitize_context_t *c) const |
26 | 145 | { |
27 | 145 | TRACE_SANITIZE (this); |
28 | 145 | return_trace (c->check_struct (this) && |
29 | 145 | coverage.sanitize (c, this) && |
30 | | /* The coverage table may use a range to represent a set |
31 | | * of glyphs, which means a small number of bytes can |
32 | | * generate a large glyph set. Manually modify the |
33 | | * sanitizer max ops to take this into account. |
34 | | * |
35 | | * Note: This check *must* be right after coverage sanitize. */ |
36 | 145 | c->check_ops ((this + coverage).get_population () >> 1)); |
37 | 145 | } |
38 | | |
39 | | hb_codepoint_t get_mask () const |
40 | 0 | { return (1 << (8 * Types::size)) - 1; } |
41 | | |
42 | | bool intersects (const hb_set_t *glyphs) const |
43 | 0 | { return (this+coverage).intersects (glyphs); } |
44 | | |
45 | | bool may_have_non_1to1 () const |
46 | 0 | { return false; } |
47 | | |
48 | | void depend (hb_depend_context_t *c) const |
49 | 0 | { |
50 | 0 | hb_codepoint_t d = deltaGlyphID; |
51 | 0 | hb_codepoint_t mask = get_mask (); |
52 | 0 | unsigned num_glyphs = c->face->get_num_glyphs (); |
53 | 0 |
|
54 | 0 | // Filter coverage by active glyphs (like closure does) |
55 | 0 | hb_set_t intersection; |
56 | 0 | (this+coverage).intersect_set (c->parent_active_glyphs (), intersection); |
57 | 0 |
|
58 | 0 | + hb_iter (intersection) |
59 | 0 | | hb_map ([&] (hb_codepoint_t _) { return hb_codepoint_pair_t (_, (_ + d) & mask); }) |
60 | 0 | | hb_filter ([num_glyphs] (const hb_codepoint_pair_t &_) { return _.second < num_glyphs; }) |
61 | 0 | | hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup(_.first, c->lookup_index, _.second); }) |
62 | 0 | ; |
63 | 0 | } |
64 | | |
65 | | void closure (hb_closure_context_t *c) const |
66 | 0 | { |
67 | 0 | hb_codepoint_t d = deltaGlyphID; |
68 | 0 | hb_codepoint_t mask = get_mask (); |
69 | | |
70 | | /* Help fuzzer avoid this function as much. */ |
71 | 0 | unsigned pop = (this+coverage).get_population (); |
72 | 0 | if (pop >= mask) |
73 | 0 | return; |
74 | | |
75 | 0 | hb_set_t intersection; |
76 | 0 | (this+coverage).intersect_set (c->parent_active_glyphs (), intersection); |
77 | | |
78 | | /* In degenerate fuzzer-found fonts, but not real fonts, |
79 | | * this table can keep adding new glyphs in each round of closure. |
80 | | * Refuse to close-over, if it maps glyph range to overlapping range. */ |
81 | 0 | hb_codepoint_t min_before = intersection.get_min (); |
82 | 0 | hb_codepoint_t max_before = intersection.get_max (); |
83 | 0 | hb_codepoint_t min_after = (min_before + d) & mask; |
84 | 0 | hb_codepoint_t max_after = (max_before + d) & mask; |
85 | 0 | if (intersection.get_population () == max_before - min_before + 1 && |
86 | 0 | ((min_before <= min_after && min_after <= max_before) || |
87 | 0 | (min_before <= max_after && max_after <= max_before))) |
88 | 0 | return; |
89 | | |
90 | 0 | + hb_iter (intersection) |
91 | 0 | | hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; }) |
92 | 0 | | hb_sink (c->output) |
93 | 0 | ; |
94 | 0 | } |
95 | | |
96 | 0 | void closure_lookups (hb_closure_lookups_context_t *c) const {} |
97 | | |
98 | | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
99 | 0 | { |
100 | 0 | if (unlikely (!(this+coverage).collect_coverage (c->input))) return; |
101 | 0 | hb_codepoint_t d = deltaGlyphID; |
102 | 0 | hb_codepoint_t mask = get_mask (); |
103 | |
|
104 | 0 | + hb_iter (this+coverage) |
105 | 0 | | hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; }) |
106 | 0 | | hb_sink (c->output) |
107 | 0 | ; |
108 | 0 | } |
109 | | |
110 | 0 | const Coverage &get_coverage () const { return this+coverage; } |
111 | | |
112 | | bool would_apply (hb_would_apply_context_t *c) const |
113 | 0 | { return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; } |
114 | | |
115 | | unsigned |
116 | | get_glyph_alternates (hb_codepoint_t glyph_id, |
117 | | unsigned start_offset, |
118 | | unsigned *alternate_count /* IN/OUT. May be NULL. */, |
119 | | hb_codepoint_t *alternate_glyphs /* OUT. May be NULL. */) const |
120 | 0 | { |
121 | 0 | unsigned int index = (this+coverage).get_coverage (glyph_id); |
122 | 0 | if (likely (index == NOT_COVERED)) |
123 | 0 | { |
124 | 0 | if (alternate_count) |
125 | 0 | *alternate_count = 0; |
126 | 0 | return 0; |
127 | 0 | } |
128 | | |
129 | 0 | if (alternate_count && *alternate_count && alternate_glyphs) |
130 | 0 | { |
131 | 0 | hb_codepoint_t d = deltaGlyphID; |
132 | 0 | hb_codepoint_t mask = get_mask (); |
133 | |
|
134 | 0 | glyph_id = (glyph_id + d) & mask; |
135 | |
|
136 | 0 | *alternate_glyphs = glyph_id; |
137 | 0 | *alternate_count = 1; |
138 | 0 | } |
139 | |
|
140 | 0 | return 1; |
141 | 0 | } |
142 | | |
143 | | void |
144 | | collect_glyph_alternates (hb_map_t *alternate_count /* IN/OUT */, |
145 | | hb_map_t *alternate_glyphs /* IN/OUT */) const |
146 | 0 | { |
147 | 0 | hb_codepoint_t d = deltaGlyphID; |
148 | 0 | hb_codepoint_t mask = get_mask (); |
149 | |
|
150 | 0 | + hb_iter (this+coverage) |
151 | 0 | | hb_map ([d, mask] (hb_codepoint_t g) { return hb_pair (g, (g + d) & mask); }) |
152 | 0 | | hb_apply ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> &p) -> void |
153 | 0 | { _hb_collect_glyph_alternates_add (p.first, p.second, |
154 | 0 | alternate_count, alternate_glyphs); }) |
155 | 0 | ; |
156 | 0 | } |
157 | | |
158 | | bool apply (hb_ot_apply_context_t *c) const |
159 | 0 | { |
160 | 0 | TRACE_APPLY (this); |
161 | 0 | hb_codepoint_t glyph_id = c->buffer->cur().codepoint; |
162 | 0 | unsigned int index = (this+coverage).get_coverage (glyph_id); |
163 | 0 | if (index == NOT_COVERED) return_trace (false); |
164 | | |
165 | 0 | hb_codepoint_t d = deltaGlyphID; |
166 | 0 | hb_codepoint_t mask = get_mask (); |
167 | |
|
168 | 0 | glyph_id = (glyph_id + d) & mask; |
169 | |
|
170 | 0 | if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) |
171 | 0 | { |
172 | 0 | c->buffer->sync_so_far (); |
173 | 0 | c->buffer->message (c->font, |
174 | 0 | "replacing glyph at %u (single substitution)", |
175 | 0 | c->buffer->idx); |
176 | 0 | } |
177 | |
|
178 | 0 | c->replace_glyph (glyph_id); |
179 | |
|
180 | 0 | if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) |
181 | 0 | { |
182 | 0 | c->buffer->message (c->font, |
183 | 0 | "replaced glyph at %u (single substitution)", |
184 | 0 | c->buffer->idx - 1u); |
185 | 0 | } |
186 | |
|
187 | 0 | return_trace (true); |
188 | 0 | } |
189 | | |
190 | | template<typename Iterator, |
191 | | hb_requires (hb_is_sorted_source_of (Iterator, hb_codepoint_t))> |
192 | | bool serialize (hb_serialize_context_t *c, |
193 | | Iterator glyphs, |
194 | | unsigned delta) |
195 | 0 | { |
196 | 0 | TRACE_SERIALIZE (this); |
197 | 0 | if (unlikely (!c->extend_min (this))) return_trace (false); |
198 | 0 | if (unlikely (!coverage.serialize_serialize (c, glyphs))) return_trace (false); |
199 | 0 | c->check_assign (deltaGlyphID, delta, HB_SERIALIZE_ERROR_INT_OVERFLOW); |
200 | 0 | return_trace (true); |
201 | 0 | } Unexecuted instantiation: hb-ot-face.cc:_ZN2OT6Layout9GSUB_impl20SingleSubstFormat1_3INS0_10SmallTypesEE9serializeI13hb_map_iter_tIS6_I16hb_filter_iter_tIS6_IN23hb_bit_set_invertible_t6iter_tEZNKS4_6subsetEP19hb_subset_context_tEUljE_L24hb_function_sortedness_t1ELPv0EERK8hb_set_tRK3$_7LSE_0EEZNKS4_6subsetESB_EUl9hb_pair_tIjjEE_LSD_1ELSE_0EERK3$_6LSD_1ELSE_0EETnPN12hb_enable_ifIXaasr15hb_is_source_ofIT_jEE5valuesrSW_18is_sorted_iteratorEvE4typeELSE_0EEEbP22hb_serialize_context_tSW_j Unexecuted instantiation: hb-ot-face.cc:_ZN2OT6Layout9GSUB_impl20SingleSubstFormat1_3INS0_10SmallTypesEE9serializeI13hb_map_iter_tIS6_I16hb_filter_iter_tIS7_I13hb_zip_iter_tINS0_6Common8Coverage6iter_tE10hb_array_tIKNS_11HBGlyphID16EEERK8hb_set_tRK3$_6LPv0EESJ_RK3$_7LSN_0EEZNKS1_20SingleSubstFormat2_4IS3_E6subsetEP19hb_subset_context_tEUl9hb_pair_tIjRSE_EE_L24hb_function_sortedness_t1ELSN_0EESM_LS11_1ELSN_0EETnPN12hb_enable_ifIXaasr15hb_is_source_ofIT_jEE5valuesrS15_18is_sorted_iteratorEvE4typeELSN_0EEEbP22hb_serialize_context_tS15_j Unexecuted instantiation: hb-aat-layout.cc:_ZN2OT6Layout9GSUB_impl20SingleSubstFormat1_3INS0_10SmallTypesEE9serializeI13hb_map_iter_tIS6_I16hb_filter_iter_tIS6_IN23hb_bit_set_invertible_t6iter_tEZNKS4_6subsetEP19hb_subset_context_tEUljE_L24hb_function_sortedness_t1ELPv0EERK8hb_set_tRK3$_7LSE_0EEZNKS4_6subsetESB_EUl9hb_pair_tIjjEE_LSD_1ELSE_0EERK3$_6LSD_1ELSE_0EETnPN12hb_enable_ifIXaasr15hb_is_source_ofIT_jEE5valuesrSW_18is_sorted_iteratorEvE4typeELSE_0EEEbP22hb_serialize_context_tSW_j Unexecuted instantiation: hb-aat-layout.cc:_ZN2OT6Layout9GSUB_impl20SingleSubstFormat1_3INS0_10SmallTypesEE9serializeI13hb_map_iter_tIS6_I16hb_filter_iter_tIS7_I13hb_zip_iter_tINS0_6Common8Coverage6iter_tE10hb_array_tIKNS_11HBGlyphID16EEERK8hb_set_tRK3$_6LPv0EESJ_RK3$_7LSN_0EEZNKS1_20SingleSubstFormat2_4IS3_E6subsetEP19hb_subset_context_tEUl9hb_pair_tIjRSE_EE_L24hb_function_sortedness_t1ELSN_0EESM_LS11_1ELSN_0EETnPN12hb_enable_ifIXaasr15hb_is_source_ofIT_jEE5valuesrS15_18is_sorted_iteratorEvE4typeELSN_0EEEbP22hb_serialize_context_tS15_j Unexecuted instantiation: hb-ot-layout.cc:_ZN2OT6Layout9GSUB_impl20SingleSubstFormat1_3INS0_10SmallTypesEE9serializeI13hb_map_iter_tIS6_I16hb_filter_iter_tIS6_IN23hb_bit_set_invertible_t6iter_tEZNKS4_6subsetEP19hb_subset_context_tEUljE_L24hb_function_sortedness_t1ELPv0EERK8hb_set_tRK3$_7LSE_0EEZNKS4_6subsetESB_EUl9hb_pair_tIjjEE_LSD_1ELSE_0EERK3$_6LSD_1ELSE_0EETnPN12hb_enable_ifIXaasr15hb_is_source_ofIT_jEE5valuesrSW_18is_sorted_iteratorEvE4typeELSE_0EEEbP22hb_serialize_context_tSW_j Unexecuted instantiation: hb-ot-layout.cc:_ZN2OT6Layout9GSUB_impl20SingleSubstFormat1_3INS0_10SmallTypesEE9serializeI13hb_map_iter_tIS6_I16hb_filter_iter_tIS7_I13hb_zip_iter_tINS0_6Common8Coverage6iter_tE10hb_array_tIKNS_11HBGlyphID16EEERK8hb_set_tRK3$_6LPv0EESJ_RK3$_7LSN_0EEZNKS1_20SingleSubstFormat2_4IS3_E6subsetEP19hb_subset_context_tEUl9hb_pair_tIjRSE_EE_L24hb_function_sortedness_t1ELSN_0EESM_LS11_1ELSN_0EETnPN12hb_enable_ifIXaasr15hb_is_source_ofIT_jEE5valuesrS15_18is_sorted_iteratorEvE4typeELSN_0EEEbP22hb_serialize_context_tS15_j Unexecuted instantiation: hb-ot-shaper-arabic.cc:_ZN2OT6Layout9GSUB_impl20SingleSubstFormat1_3INS0_10SmallTypesEE9serializeI13hb_map_iter_tI13hb_zip_iter_tI17hb_sorted_array_tINS_11HBGlyphID16EE10hb_array_tIS9_EERK3$_6L24hb_function_sortedness_t1ELPv0EETnPN12hb_enable_ifIXaasr15hb_is_source_ofIT_jEE5valuesrSL_18is_sorted_iteratorEvE4typeELSI_0EEEbP22hb_serialize_context_tSL_j Unexecuted instantiation: hb-ot-shaper-arabic.cc:_ZN2OT6Layout9GSUB_impl20SingleSubstFormat1_3INS0_10SmallTypesEE9serializeI13hb_map_iter_tIS6_I16hb_filter_iter_tIS6_IN23hb_bit_set_invertible_t6iter_tEZNKS4_6subsetEP19hb_subset_context_tEUljE_L24hb_function_sortedness_t1ELPv0EERK8hb_set_tRK3$_7LSE_0EEZNKS4_6subsetESB_EUl9hb_pair_tIjjEE_LSD_1ELSE_0EERK3$_6LSD_1ELSE_0EETnPN12hb_enable_ifIXaasr15hb_is_source_ofIT_jEE5valuesrSW_18is_sorted_iteratorEvE4typeELSE_0EEEbP22hb_serialize_context_tSW_j Unexecuted instantiation: hb-ot-shaper-arabic.cc:_ZN2OT6Layout9GSUB_impl20SingleSubstFormat1_3INS0_10SmallTypesEE9serializeI13hb_map_iter_tIS6_I16hb_filter_iter_tIS7_I13hb_zip_iter_tINS0_6Common8Coverage6iter_tE10hb_array_tIKNS_11HBGlyphID16EEERK8hb_set_tRK3$_6LPv0EESJ_RK3$_7LSN_0EEZNKS1_20SingleSubstFormat2_4IS3_E6subsetEP19hb_subset_context_tEUl9hb_pair_tIjRSE_EE_L24hb_function_sortedness_t1ELSN_0EESM_LS11_1ELSN_0EETnPN12hb_enable_ifIXaasr15hb_is_source_ofIT_jEE5valuesrS15_18is_sorted_iteratorEvE4typeELSN_0EEEbP22hb_serialize_context_tS15_j |
202 | | |
203 | | bool subset (hb_subset_context_t *c) const |
204 | 0 | { |
205 | 0 | TRACE_SUBSET (this); |
206 | 0 | const hb_set_t &glyphset = *c->plan->glyphset_gsub (); |
207 | 0 | const hb_map_t &glyph_map = *c->plan->glyph_map; |
208 | 0 |
|
209 | 0 | hb_codepoint_t d = deltaGlyphID; |
210 | 0 | hb_codepoint_t mask = get_mask (); |
211 | 0 |
|
212 | 0 | hb_set_t intersection; |
213 | 0 | (this+coverage).intersect_set (glyphset, intersection); |
214 | 0 |
|
215 | 0 | auto it = |
216 | 0 | + hb_iter (intersection) |
217 | 0 | | hb_map_retains_sorting ([d, mask] (hb_codepoint_t g) { |
218 | 0 | return hb_codepoint_pair_t (g, |
219 | 0 | (g + d) & mask); }) |
220 | 0 | | hb_filter (glyphset, hb_second) |
221 | 0 | | hb_map_retains_sorting ([&] (hb_codepoint_pair_t p) -> hb_codepoint_pair_t |
222 | 0 | { return hb_pair (glyph_map[p.first], glyph_map[p.second]); }) |
223 | 0 | ; |
224 | 0 |
|
225 | 0 | bool ret = bool (it); |
226 | 0 | SingleSubst_serialize (c->serializer, it); |
227 | 0 | return_trace (ret); |
228 | 0 | } |
229 | | }; |
230 | | |
231 | | } |
232 | | } |
233 | | } |
234 | | |
235 | | |
236 | | #endif /* OT_LAYOUT_GSUB_SINGLESUBSTFORMAT1_HH */ |