1 #ifndef OT_LAYOUT_GSUB_REVERSECHAINSINGLESUBSTFORMAT1_HH
2 #define OT_LAYOUT_GSUB_REVERSECHAINSINGLESUBSTFORMAT1_HH
10 struct ReverseChainSingleSubstFormat1
13 HBUINT16 format; /* Format identifier--format = 1 */
15 coverage; /* Offset to Coverage table--from
16 * beginning of table */
17 Array16OfOffset16To<Coverage>
18 backtrack; /* Array of coverage tables
19 * in backtracking sequence, in glyph
21 Array16OfOffset16To<Coverage>
22 lookaheadX; /* Array of coverage tables
23 * in lookahead sequence, in glyph
25 Array16Of<HBGlyphID16>
26 substituteX; /* Array of substitute
27 * GlyphIDs--ordered by Coverage Index */
31 bool sanitize (hb_sanitize_context_t *c) const
33 TRACE_SANITIZE (this);
34 if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this)))
36 const auto &lookahead = StructAfter<decltype (lookaheadX)> (backtrack);
37 if (!lookahead.sanitize (c, this))
39 const auto &substitute = StructAfter<decltype (substituteX)> (lookahead);
40 return_trace (substitute.sanitize (c));
43 bool intersects (const hb_set_t *glyphs) const
45 if (!(this+coverage).intersects (glyphs))
48 const auto &lookahead = StructAfter<decltype (lookaheadX)> (backtrack);
52 count = backtrack.len;
53 for (unsigned int i = 0; i < count; i++)
54 if (!(this+backtrack[i]).intersects (glyphs))
57 count = lookahead.len;
58 for (unsigned int i = 0; i < count; i++)
59 if (!(this+lookahead[i]).intersects (glyphs))
65 bool may_have_non_1to1 () const
68 void closure (hb_closure_context_t *c) const
70 if (!intersects (c->glyphs)) return;
72 const auto &lookahead = StructAfter<decltype (lookaheadX)> (backtrack);
73 const auto &substitute = StructAfter<decltype (substituteX)> (lookahead);
75 + hb_zip (this+coverage, substitute)
76 | hb_filter (c->parent_active_glyphs (), hb_first)
82 void closure_lookups (hb_closure_lookups_context_t *c) const {}
84 void collect_glyphs (hb_collect_glyphs_context_t *c) const
86 if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
90 count = backtrack.len;
91 for (unsigned int i = 0; i < count; i++)
92 if (unlikely (!(this+backtrack[i]).collect_coverage (c->before))) return;
94 const auto &lookahead = StructAfter<decltype (lookaheadX)> (backtrack);
95 count = lookahead.len;
96 for (unsigned int i = 0; i < count; i++)
97 if (unlikely (!(this+lookahead[i]).collect_coverage (c->after))) return;
99 const auto &substitute = StructAfter<decltype (substituteX)> (lookahead);
100 count = substitute.len;
101 c->output->add_array (substitute.arrayZ, substitute.len);
104 const Coverage &get_coverage () const { return this+coverage; }
106 bool would_apply (hb_would_apply_context_t *c) const
107 { return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; }
109 bool apply (hb_ot_apply_context_t *c) const
112 if (unlikely (c->nesting_level_left != HB_MAX_NESTING_LEVEL))
113 return_trace (false); /* No chaining to this type */
115 unsigned int index = (this+coverage).get_coverage (c->buffer->cur ().codepoint);
116 if (likely (index == NOT_COVERED)) return_trace (false);
118 const auto &lookahead = StructAfter<decltype (lookaheadX)> (backtrack);
119 const auto &substitute = StructAfter<decltype (substituteX)> (lookahead);
121 if (unlikely (index >= substitute.len)) return_trace (false);
123 unsigned int start_index = 0, end_index = 0;
124 if (match_backtrack (c,
125 backtrack.len, (HBUINT16 *) backtrack.arrayZ,
126 match_coverage, this,
129 lookahead.len, (HBUINT16 *) lookahead.arrayZ,
130 match_coverage, this,
131 c->buffer->idx + 1, &end_index))
133 c->buffer->unsafe_to_break_from_outbuffer (start_index, end_index);
135 if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
137 c->buffer->message (c->font,
138 "replacing glyph at %u (reverse chaining substitution)",
142 c->replace_glyph_inplace (substitute[index]);
144 if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
146 c->buffer->message (c->font,
147 "replaced glyph at %u (reverse chaining substitution)",
151 /* Note: We DON'T decrease buffer->idx. The main loop does it
152 * for us. This is useful for preventing surprises if someone
153 * calls us through a Context lookup. */
158 c->buffer->unsafe_to_concat_from_outbuffer (start_index, end_index);
159 return_trace (false);
163 template<typename Iterator,
164 hb_requires (hb_is_iterator (Iterator))>
165 bool serialize_coverage_offset_array (hb_subset_context_t *c, Iterator it) const
167 TRACE_SERIALIZE (this);
168 auto *out = c->serializer->start_embed<Array16OfOffset16To<Coverage>> ();
170 if (unlikely (!c->serializer->allocate_size<HBUINT16> (HBUINT16::static_size)))
171 return_trace (false);
173 for (auto& offset : it) {
174 auto *o = out->serialize_append (c->serializer);
175 if (unlikely (!o) || !o->serialize_subset (c, offset, this))
176 return_trace (false);
182 template<typename Iterator, typename BacktrackIterator, typename LookaheadIterator,
183 hb_requires (hb_is_sorted_source_of (Iterator, hb_codepoint_pair_t)),
184 hb_requires (hb_is_iterator (BacktrackIterator)),
185 hb_requires (hb_is_iterator (LookaheadIterator))>
186 bool serialize (hb_subset_context_t *c,
187 Iterator coverage_subst_iter,
188 BacktrackIterator backtrack_iter,
189 LookaheadIterator lookahead_iter) const
191 TRACE_SERIALIZE (this);
193 auto *out = c->serializer->start_embed (this);
194 if (unlikely (!c->serializer->embed (this->format))) return_trace (false);
195 if (unlikely (!c->serializer->embed (this->coverage))) return_trace (false);
197 if (!serialize_coverage_offset_array (c, backtrack_iter)) return_trace (false);
198 if (!serialize_coverage_offset_array (c, lookahead_iter)) return_trace (false);
200 auto *substitute_out = c->serializer->start_embed<Array16Of<HBGlyphID16>> ();
202 + coverage_subst_iter
207 + coverage_subst_iter
208 | hb_map_retains_sorting (hb_first)
210 if (unlikely (! c->serializer->check_success (substitute_out->serialize (c->serializer, substitutes))))
211 return_trace (false);
213 if (unlikely (!out->coverage.serialize_serialize (c->serializer, glyphs)))
214 return_trace (false);
218 bool subset (hb_subset_context_t *c) const
221 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
222 const hb_map_t &glyph_map = *c->plan->glyph_map;
224 const auto &lookahead = StructAfter<decltype (lookaheadX)> (backtrack);
225 const auto &substitute = StructAfter<decltype (substituteX)> (lookahead);
228 + hb_zip (this+coverage, substitute)
229 | hb_filter (glyphset, hb_first)
230 | hb_filter (glyphset, hb_second)
231 | hb_map_retains_sorting ([&] (hb_pair_t<hb_codepoint_t, const HBGlyphID16 &> p) -> hb_codepoint_pair_t
232 { return hb_pair (glyph_map[p.first], glyph_map[p.second]); })
235 return_trace (bool (it) && serialize (c, it, backtrack.iter (), lookahead.iter ()));
243 #endif /* HB_OT_LAYOUT_GSUB_REVERSECHAINSINGLESUBSTFORMAT1_HH */