1 /*
2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2012,2013 Google, Inc.
4 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
27 */
28
29 #ifndef HB_OT_LAYOUT_GSUB_TABLE_HH
30 #define HB_OT_LAYOUT_GSUB_TABLE_HH
31
32 #include "hb-ot-layout-gsubgpos.hh"
33
34
35 namespace OT {
36
37 typedef hb_pair_t<hb_codepoint_t, hb_codepoint_t> hb_codepoint_pair_t;
38
39 template<typename Iterator>
40 static void SingleSubst_serialize (hb_serialize_context_t *c,
41 Iterator it);
42
43
44 struct SingleSubstFormat1
45 {
intersectsOT::SingleSubstFormat146 bool intersects (const hb_set_t *glyphs) const
47 { return (this+coverage).intersects (glyphs); }
48
may_have_non_1to1OT::SingleSubstFormat149 bool may_have_non_1to1 () const
50 { return false; }
51
closureOT::SingleSubstFormat152 void closure (hb_closure_context_t *c) const
53 {
54 unsigned d = deltaGlyphID;
55
56 + hb_iter (this+coverage)
57 | hb_filter (c->parent_active_glyphs ())
58 | hb_map ([d] (hb_codepoint_t g) { return (g + d) & 0xFFFFu; })
59 | hb_sink (c->output)
60 ;
61
62 }
63
closure_lookupsOT::SingleSubstFormat164 void closure_lookups (hb_closure_lookups_context_t *c) const {}
65
collect_glyphsOT::SingleSubstFormat166 void collect_glyphs (hb_collect_glyphs_context_t *c) const
67 {
68 if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
69 unsigned d = deltaGlyphID;
70 + hb_iter (this+coverage)
71 | hb_map ([d] (hb_codepoint_t g) { return (g + d) & 0xFFFFu; })
72 | hb_sink (c->output)
73 ;
74 }
75
get_coverageOT::SingleSubstFormat176 const Coverage &get_coverage () const { return this+coverage; }
77
would_applyOT::SingleSubstFormat178 bool would_apply (hb_would_apply_context_t *c) const
79 { return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; }
80
applyOT::SingleSubstFormat181 bool apply (hb_ot_apply_context_t *c) const
82 {
83 TRACE_APPLY (this);
84 hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
85 unsigned int index = (this+coverage).get_coverage (glyph_id);
86 if (likely (index == NOT_COVERED)) return_trace (false);
87
88 /* According to the Adobe Annotated OpenType Suite, result is always
89 * limited to 16bit. */
90 glyph_id = (glyph_id + deltaGlyphID) & 0xFFFFu;
91 c->replace_glyph (glyph_id);
92
93 return_trace (true);
94 }
95
96 template<typename Iterator,
97 hb_requires (hb_is_sorted_source_of (Iterator, hb_codepoint_t))>
serializeOT::SingleSubstFormat198 bool serialize (hb_serialize_context_t *c,
99 Iterator glyphs,
100 unsigned delta)
101 {
102 TRACE_SERIALIZE (this);
103 if (unlikely (!c->extend_min (*this))) return_trace (false);
104 if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs))) return_trace (false);
105 c->check_assign (deltaGlyphID, delta, HB_SERIALIZE_ERROR_INT_OVERFLOW);
106 return_trace (true);
107 }
108
subsetOT::SingleSubstFormat1109 bool subset (hb_subset_context_t *c) const
110 {
111 TRACE_SUBSET (this);
112 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
113 const hb_map_t &glyph_map = *c->plan->glyph_map;
114
115 hb_codepoint_t delta = deltaGlyphID;
116
117 auto it =
118 + hb_iter (this+coverage)
119 | hb_filter (glyphset)
120 | hb_map_retains_sorting ([&] (hb_codepoint_t g) {
121 return hb_codepoint_pair_t (g,
122 (g + delta) & 0xFFFF); })
123 | hb_filter (glyphset, hb_second)
124 | hb_map_retains_sorting ([&] (hb_codepoint_pair_t p) -> hb_codepoint_pair_t
125 { return hb_pair (glyph_map[p.first], glyph_map[p.second]); })
126 ;
127
128 bool ret = bool (it);
129 SingleSubst_serialize (c->serializer, it);
130 return_trace (ret);
131 }
132
sanitizeOT::SingleSubstFormat1133 bool sanitize (hb_sanitize_context_t *c) const
134 {
135 TRACE_SANITIZE (this);
136 return_trace (coverage.sanitize (c, this) && deltaGlyphID.sanitize (c));
137 }
138
139 protected:
140 HBUINT16 format; /* Format identifier--format = 1 */
141 Offset16To<Coverage>
142 coverage; /* Offset to Coverage table--from
143 * beginning of Substitution table */
144 HBUINT16 deltaGlyphID; /* Add to original GlyphID to get
145 * substitute GlyphID, modulo 0x10000 */
146 public:
147 DEFINE_SIZE_STATIC (6);
148 };
149
150 struct SingleSubstFormat2
151 {
intersectsOT::SingleSubstFormat2152 bool intersects (const hb_set_t *glyphs) const
153 { return (this+coverage).intersects (glyphs); }
154
may_have_non_1to1OT::SingleSubstFormat2155 bool may_have_non_1to1 () const
156 { return false; }
157
closureOT::SingleSubstFormat2158 void closure (hb_closure_context_t *c) const
159 {
160 + hb_zip (this+coverage, substitute)
161 | hb_filter (c->parent_active_glyphs (), hb_first)
162 | hb_map (hb_second)
163 | hb_sink (c->output)
164 ;
165
166 }
167
closure_lookupsOT::SingleSubstFormat2168 void closure_lookups (hb_closure_lookups_context_t *c) const {}
169
collect_glyphsOT::SingleSubstFormat2170 void collect_glyphs (hb_collect_glyphs_context_t *c) const
171 {
172 if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
173 + hb_zip (this+coverage, substitute)
174 | hb_map (hb_second)
175 | hb_sink (c->output)
176 ;
177 }
178
get_coverageOT::SingleSubstFormat2179 const Coverage &get_coverage () const { return this+coverage; }
180
would_applyOT::SingleSubstFormat2181 bool would_apply (hb_would_apply_context_t *c) const
182 { return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; }
183
applyOT::SingleSubstFormat2184 bool apply (hb_ot_apply_context_t *c) const
185 {
186 TRACE_APPLY (this);
187 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
188 if (likely (index == NOT_COVERED)) return_trace (false);
189
190 if (unlikely (index >= substitute.len)) return_trace (false);
191
192 c->replace_glyph (substitute[index]);
193
194 return_trace (true);
195 }
196
197 template<typename Iterator,
198 hb_requires (hb_is_sorted_source_of (Iterator,
199 hb_codepoint_pair_t))>
serializeOT::SingleSubstFormat2200 bool serialize (hb_serialize_context_t *c,
201 Iterator it)
202 {
203 TRACE_SERIALIZE (this);
204 auto substitutes =
205 + it
206 | hb_map (hb_second)
207 ;
208 auto glyphs =
209 + it
210 | hb_map_retains_sorting (hb_first)
211 ;
212 if (unlikely (!c->extend_min (*this))) return_trace (false);
213 if (unlikely (!substitute.serialize (c, substitutes))) return_trace (false);
214 if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs))) return_trace (false);
215 return_trace (true);
216 }
217
subsetOT::SingleSubstFormat2218 bool subset (hb_subset_context_t *c) const
219 {
220 TRACE_SUBSET (this);
221 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
222 const hb_map_t &glyph_map = *c->plan->glyph_map;
223
224 auto it =
225 + hb_zip (this+coverage, substitute)
226 | hb_filter (glyphset, hb_first)
227 | hb_filter (glyphset, hb_second)
228 | hb_map_retains_sorting ([&] (hb_pair_t<hb_codepoint_t, const HBGlyphID &> p) -> hb_codepoint_pair_t
229 { return hb_pair (glyph_map[p.first], glyph_map[p.second]); })
230 ;
231
232 bool ret = bool (it);
233 SingleSubst_serialize (c->serializer, it);
234 return_trace (ret);
235 }
236
sanitizeOT::SingleSubstFormat2237 bool sanitize (hb_sanitize_context_t *c) const
238 {
239 TRACE_SANITIZE (this);
240 return_trace (coverage.sanitize (c, this) && substitute.sanitize (c));
241 }
242
243 protected:
244 HBUINT16 format; /* Format identifier--format = 2 */
245 Offset16To<Coverage>
246 coverage; /* Offset to Coverage table--from
247 * beginning of Substitution table */
248 Array16Of<HBGlyphID>
249 substitute; /* Array of substitute
250 * GlyphIDs--ordered by Coverage Index */
251 public:
252 DEFINE_SIZE_ARRAY (6, substitute);
253 };
254
255 struct SingleSubst
256 {
257
258 template<typename Iterator,
259 hb_requires (hb_is_sorted_source_of (Iterator,
260 const hb_codepoint_pair_t))>
serializeOT::SingleSubst261 bool serialize (hb_serialize_context_t *c,
262 Iterator glyphs)
263 {
264 TRACE_SERIALIZE (this);
265 if (unlikely (!c->extend_min (u.format))) return_trace (false);
266 unsigned format = 2;
267 unsigned delta = 0;
268 if (glyphs)
269 {
270 format = 1;
271 auto get_delta = [=] (hb_codepoint_pair_t _)
272 { return (unsigned) (_.second - _.first) & 0xFFFF; };
273 delta = get_delta (*glyphs);
274 if (!hb_all (++(+glyphs), delta, get_delta)) format = 2;
275 }
276 u.format = format;
277 switch (u.format) {
278 case 1: return_trace (u.format1.serialize (c,
279 + glyphs
280 | hb_map_retains_sorting (hb_first),
281 delta));
282 case 2: return_trace (u.format2.serialize (c, glyphs));
283 default:return_trace (false);
284 }
285 }
286
287 template <typename context_t, typename ...Ts>
dispatchOT::SingleSubst288 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
289 {
290 TRACE_DISPATCH (this, u.format);
291 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
292 switch (u.format) {
293 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
294 case 2: return_trace (c->dispatch (u.format2, hb_forward<Ts> (ds)...));
295 default:return_trace (c->default_return_value ());
296 }
297 }
298
299 protected:
300 union {
301 HBUINT16 format; /* Format identifier */
302 SingleSubstFormat1 format1;
303 SingleSubstFormat2 format2;
304 } u;
305 };
306
307 template<typename Iterator>
308 static void
SingleSubst_serialize(hb_serialize_context_t * c,Iterator it)309 SingleSubst_serialize (hb_serialize_context_t *c,
310 Iterator it)
311 { c->start_embed<SingleSubst> ()->serialize (c, it); }
312
313 struct Sequence
314 {
intersectsOT::Sequence315 bool intersects (const hb_set_t *glyphs) const
316 { return hb_all (substitute, glyphs); }
317
closureOT::Sequence318 void closure (hb_closure_context_t *c) const
319 { c->output->add_array (substitute.arrayZ, substitute.len); }
320
collect_glyphsOT::Sequence321 void collect_glyphs (hb_collect_glyphs_context_t *c) const
322 { c->output->add_array (substitute.arrayZ, substitute.len); }
323
applyOT::Sequence324 bool apply (hb_ot_apply_context_t *c) const
325 {
326 TRACE_APPLY (this);
327 unsigned int count = substitute.len;
328
329 /* Special-case to make it in-place and not consider this
330 * as a "multiplied" substitution. */
331 if (unlikely (count == 1))
332 {
333 c->replace_glyph (substitute.arrayZ[0]);
334 return_trace (true);
335 }
336 /* Spec disallows this, but Uniscribe allows it.
337 * https://github.com/harfbuzz/harfbuzz/issues/253 */
338 else if (unlikely (count == 0))
339 {
340 c->buffer->delete_glyph ();
341 return_trace (true);
342 }
343
344 unsigned int klass = _hb_glyph_info_is_ligature (&c->buffer->cur()) ?
345 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH : 0;
346
347 for (unsigned int i = 0; i < count; i++) {
348 _hb_glyph_info_set_lig_props_for_component (&c->buffer->cur(), i);
349 c->output_glyph_for_component (substitute.arrayZ[i], klass);
350 }
351 c->buffer->skip_glyph ();
352
353 return_trace (true);
354 }
355
356 template <typename Iterator,
357 hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))>
serializeOT::Sequence358 bool serialize (hb_serialize_context_t *c,
359 Iterator subst)
360 {
361 TRACE_SERIALIZE (this);
362 return_trace (substitute.serialize (c, subst));
363 }
364
subsetOT::Sequence365 bool subset (hb_subset_context_t *c) const
366 {
367 TRACE_SUBSET (this);
368 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
369 const hb_map_t &glyph_map = *c->plan->glyph_map;
370
371 if (!intersects (&glyphset)) return_trace (false);
372
373 auto it =
374 + hb_iter (substitute)
375 | hb_map (glyph_map)
376 ;
377
378 auto *out = c->serializer->start_embed (*this);
379 return_trace (out->serialize (c->serializer, it));
380 }
381
sanitizeOT::Sequence382 bool sanitize (hb_sanitize_context_t *c) const
383 {
384 TRACE_SANITIZE (this);
385 return_trace (substitute.sanitize (c));
386 }
387
388 protected:
389 Array16Of<HBGlyphID>
390 substitute; /* String of GlyphIDs to substitute */
391 public:
392 DEFINE_SIZE_ARRAY (2, substitute);
393 };
394
395 struct MultipleSubstFormat1
396 {
intersectsOT::MultipleSubstFormat1397 bool intersects (const hb_set_t *glyphs) const
398 { return (this+coverage).intersects (glyphs); }
399
may_have_non_1to1OT::MultipleSubstFormat1400 bool may_have_non_1to1 () const
401 { return true; }
402
closureOT::MultipleSubstFormat1403 void closure (hb_closure_context_t *c) const
404 {
405 + hb_zip (this+coverage, sequence)
406 | hb_filter (c->parent_active_glyphs (), hb_first)
407 | hb_map (hb_second)
408 | hb_map (hb_add (this))
409 | hb_apply ([c] (const Sequence &_) { _.closure (c); })
410 ;
411
412 }
413
closure_lookupsOT::MultipleSubstFormat1414 void closure_lookups (hb_closure_lookups_context_t *c) const {}
415
collect_glyphsOT::MultipleSubstFormat1416 void collect_glyphs (hb_collect_glyphs_context_t *c) const
417 {
418 if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
419 + hb_zip (this+coverage, sequence)
420 | hb_map (hb_second)
421 | hb_map (hb_add (this))
422 | hb_apply ([c] (const Sequence &_) { _.collect_glyphs (c); })
423 ;
424 }
425
get_coverageOT::MultipleSubstFormat1426 const Coverage &get_coverage () const { return this+coverage; }
427
would_applyOT::MultipleSubstFormat1428 bool would_apply (hb_would_apply_context_t *c) const
429 { return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; }
430
applyOT::MultipleSubstFormat1431 bool apply (hb_ot_apply_context_t *c) const
432 {
433 TRACE_APPLY (this);
434
435 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
436 if (likely (index == NOT_COVERED)) return_trace (false);
437
438 return_trace ((this+sequence[index]).apply (c));
439 }
440
serializeOT::MultipleSubstFormat1441 bool serialize (hb_serialize_context_t *c,
442 hb_sorted_array_t<const HBGlyphID> glyphs,
443 hb_array_t<const unsigned int> substitute_len_list,
444 hb_array_t<const HBGlyphID> substitute_glyphs_list)
445 {
446 TRACE_SERIALIZE (this);
447 if (unlikely (!c->extend_min (*this))) return_trace (false);
448 if (unlikely (!sequence.serialize (c, glyphs.length))) return_trace (false);
449 for (unsigned int i = 0; i < glyphs.length; i++)
450 {
451 unsigned int substitute_len = substitute_len_list[i];
452 if (unlikely (!sequence[i].serialize (c, this)
453 .serialize (c, substitute_glyphs_list.sub_array (0, substitute_len))))
454 return_trace (false);
455 substitute_glyphs_list += substitute_len;
456 }
457 return_trace (coverage.serialize (c, this).serialize (c, glyphs));
458 }
459
subsetOT::MultipleSubstFormat1460 bool subset (hb_subset_context_t *c) const
461 {
462 TRACE_SUBSET (this);
463 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
464 const hb_map_t &glyph_map = *c->plan->glyph_map;
465
466 auto *out = c->serializer->start_embed (*this);
467 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
468 out->format = format;
469
470 hb_sorted_vector_t<hb_codepoint_t> new_coverage;
471 + hb_zip (this+coverage, sequence)
472 | hb_filter (glyphset, hb_first)
473 | hb_filter (subset_offset_array (c, out->sequence, this), hb_second)
474 | hb_map (hb_first)
475 | hb_map (glyph_map)
476 | hb_sink (new_coverage)
477 ;
478 out->coverage.serialize (c->serializer, out)
479 .serialize (c->serializer, new_coverage.iter ());
480 return_trace (bool (new_coverage));
481 }
482
sanitizeOT::MultipleSubstFormat1483 bool sanitize (hb_sanitize_context_t *c) const
484 {
485 TRACE_SANITIZE (this);
486 return_trace (coverage.sanitize (c, this) && sequence.sanitize (c, this));
487 }
488
489 protected:
490 HBUINT16 format; /* Format identifier--format = 1 */
491 Offset16To<Coverage>
492 coverage; /* Offset to Coverage table--from
493 * beginning of Substitution table */
494 Array16OfOffset16To<Sequence>
495 sequence; /* Array of Sequence tables
496 * ordered by Coverage Index */
497 public:
498 DEFINE_SIZE_ARRAY (6, sequence);
499 };
500
501 struct MultipleSubst
502 {
serializeOT::MultipleSubst503 bool serialize (hb_serialize_context_t *c,
504 hb_sorted_array_t<const HBGlyphID> glyphs,
505 hb_array_t<const unsigned int> substitute_len_list,
506 hb_array_t<const HBGlyphID> substitute_glyphs_list)
507 {
508 TRACE_SERIALIZE (this);
509 if (unlikely (!c->extend_min (u.format))) return_trace (false);
510 unsigned int format = 1;
511 u.format = format;
512 switch (u.format) {
513 case 1: return_trace (u.format1.serialize (c, glyphs, substitute_len_list, substitute_glyphs_list));
514 default:return_trace (false);
515 }
516 }
517
518 template <typename context_t, typename ...Ts>
dispatchOT::MultipleSubst519 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
520 {
521 TRACE_DISPATCH (this, u.format);
522 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
523 switch (u.format) {
524 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
525 default:return_trace (c->default_return_value ());
526 }
527 }
528
529 protected:
530 union {
531 HBUINT16 format; /* Format identifier */
532 MultipleSubstFormat1 format1;
533 } u;
534 };
535
536 struct AlternateSet
537 {
intersectsOT::AlternateSet538 bool intersects (const hb_set_t *glyphs) const
539 { return hb_any (alternates, glyphs); }
540
closureOT::AlternateSet541 void closure (hb_closure_context_t *c) const
542 { c->output->add_array (alternates.arrayZ, alternates.len); }
543
collect_glyphsOT::AlternateSet544 void collect_glyphs (hb_collect_glyphs_context_t *c) const
545 { c->output->add_array (alternates.arrayZ, alternates.len); }
546
applyOT::AlternateSet547 bool apply (hb_ot_apply_context_t *c) const
548 {
549 TRACE_APPLY (this);
550 unsigned int count = alternates.len;
551
552 if (unlikely (!count)) return_trace (false);
553
554 hb_mask_t glyph_mask = c->buffer->cur().mask;
555 hb_mask_t lookup_mask = c->lookup_mask;
556
557 /* Note: This breaks badly if two features enabled this lookup together. */
558 unsigned int shift = hb_ctz (lookup_mask);
559 unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
560
561 /* If alt_index is MAX_VALUE, randomize feature if it is the rand feature. */
562 if (alt_index == HB_OT_MAP_MAX_VALUE && c->random)
563 alt_index = c->random_number () % count + 1;
564
565 if (unlikely (alt_index > count || alt_index == 0)) return_trace (false);
566
567 c->replace_glyph (alternates[alt_index - 1]);
568
569 return_trace (true);
570 }
571
572 unsigned
get_alternatesOT::AlternateSet573 get_alternates (unsigned start_offset,
574 unsigned *alternate_count /* IN/OUT. May be NULL. */,
575 hb_codepoint_t *alternate_glyphs /* OUT. May be NULL. */) const
576 {
577 if (alternates.len && alternate_count)
578 {
579 + alternates.sub_array (start_offset, alternate_count)
580 | hb_sink (hb_array (alternate_glyphs, *alternate_count))
581 ;
582 }
583 return alternates.len;
584 }
585
586 template <typename Iterator,
587 hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))>
serializeOT::AlternateSet588 bool serialize (hb_serialize_context_t *c,
589 Iterator alts)
590 {
591 TRACE_SERIALIZE (this);
592 return_trace (alternates.serialize (c, alts));
593 }
594
subsetOT::AlternateSet595 bool subset (hb_subset_context_t *c) const
596 {
597 TRACE_SUBSET (this);
598 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
599 const hb_map_t &glyph_map = *c->plan->glyph_map;
600
601 auto it =
602 + hb_iter (alternates)
603 | hb_filter (glyphset)
604 | hb_map (glyph_map)
605 ;
606
607 auto *out = c->serializer->start_embed (*this);
608 return_trace (out->serialize (c->serializer, it) &&
609 out->alternates);
610 }
611
sanitizeOT::AlternateSet612 bool sanitize (hb_sanitize_context_t *c) const
613 {
614 TRACE_SANITIZE (this);
615 return_trace (alternates.sanitize (c));
616 }
617
618 protected:
619 Array16Of<HBGlyphID>
620 alternates; /* Array of alternate GlyphIDs--in
621 * arbitrary order */
622 public:
623 DEFINE_SIZE_ARRAY (2, alternates);
624 };
625
626 struct AlternateSubstFormat1
627 {
intersectsOT::AlternateSubstFormat1628 bool intersects (const hb_set_t *glyphs) const
629 { return (this+coverage).intersects (glyphs); }
630
may_have_non_1to1OT::AlternateSubstFormat1631 bool may_have_non_1to1 () const
632 { return false; }
633
closureOT::AlternateSubstFormat1634 void closure (hb_closure_context_t *c) const
635 {
636 + hb_zip (this+coverage, alternateSet)
637 | hb_filter (c->parent_active_glyphs (), hb_first)
638 | hb_map (hb_second)
639 | hb_map (hb_add (this))
640 | hb_apply ([c] (const AlternateSet &_) { _.closure (c); })
641 ;
642
643 }
644
closure_lookupsOT::AlternateSubstFormat1645 void closure_lookups (hb_closure_lookups_context_t *c) const {}
646
collect_glyphsOT::AlternateSubstFormat1647 void collect_glyphs (hb_collect_glyphs_context_t *c) const
648 {
649 if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
650 + hb_zip (this+coverage, alternateSet)
651 | hb_map (hb_second)
652 | hb_map (hb_add (this))
653 | hb_apply ([c] (const AlternateSet &_) { _.collect_glyphs (c); })
654 ;
655 }
656
get_coverageOT::AlternateSubstFormat1657 const Coverage &get_coverage () const { return this+coverage; }
658
would_applyOT::AlternateSubstFormat1659 bool would_apply (hb_would_apply_context_t *c) const
660 { return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; }
661
662 unsigned
get_glyph_alternatesOT::AlternateSubstFormat1663 get_glyph_alternates (hb_codepoint_t gid,
664 unsigned start_offset,
665 unsigned *alternate_count /* IN/OUT. May be NULL. */,
666 hb_codepoint_t *alternate_glyphs /* OUT. May be NULL. */) const
667 { return (this+alternateSet[(this+coverage).get_coverage (gid)])
668 .get_alternates (start_offset, alternate_count, alternate_glyphs); }
669
applyOT::AlternateSubstFormat1670 bool apply (hb_ot_apply_context_t *c) const
671 {
672 TRACE_APPLY (this);
673
674 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
675 if (likely (index == NOT_COVERED)) return_trace (false);
676
677 return_trace ((this+alternateSet[index]).apply (c));
678 }
679
serializeOT::AlternateSubstFormat1680 bool serialize (hb_serialize_context_t *c,
681 hb_sorted_array_t<const HBGlyphID> glyphs,
682 hb_array_t<const unsigned int> alternate_len_list,
683 hb_array_t<const HBGlyphID> alternate_glyphs_list)
684 {
685 TRACE_SERIALIZE (this);
686 if (unlikely (!c->extend_min (*this))) return_trace (false);
687 if (unlikely (!alternateSet.serialize (c, glyphs.length))) return_trace (false);
688 for (unsigned int i = 0; i < glyphs.length; i++)
689 {
690 unsigned int alternate_len = alternate_len_list[i];
691 if (unlikely (!alternateSet[i].serialize (c, this)
692 .serialize (c, alternate_glyphs_list.sub_array (0, alternate_len))))
693 return_trace (false);
694 alternate_glyphs_list += alternate_len;
695 }
696 return_trace (coverage.serialize (c, this).serialize (c, glyphs));
697 }
698
subsetOT::AlternateSubstFormat1699 bool subset (hb_subset_context_t *c) const
700 {
701 TRACE_SUBSET (this);
702 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
703 const hb_map_t &glyph_map = *c->plan->glyph_map;
704
705 auto *out = c->serializer->start_embed (*this);
706 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
707 out->format = format;
708
709 hb_sorted_vector_t<hb_codepoint_t> new_coverage;
710 + hb_zip (this+coverage, alternateSet)
711 | hb_filter (glyphset, hb_first)
712 | hb_filter (subset_offset_array (c, out->alternateSet, this), hb_second)
713 | hb_map (hb_first)
714 | hb_map (glyph_map)
715 | hb_sink (new_coverage)
716 ;
717 out->coverage.serialize (c->serializer, out)
718 .serialize (c->serializer, new_coverage.iter ());
719 return_trace (bool (new_coverage));
720 }
721
sanitizeOT::AlternateSubstFormat1722 bool sanitize (hb_sanitize_context_t *c) const
723 {
724 TRACE_SANITIZE (this);
725 return_trace (coverage.sanitize (c, this) && alternateSet.sanitize (c, this));
726 }
727
728 protected:
729 HBUINT16 format; /* Format identifier--format = 1 */
730 Offset16To<Coverage>
731 coverage; /* Offset to Coverage table--from
732 * beginning of Substitution table */
733 Array16OfOffset16To<AlternateSet>
734 alternateSet; /* Array of AlternateSet tables
735 * ordered by Coverage Index */
736 public:
737 DEFINE_SIZE_ARRAY (6, alternateSet);
738 };
739
740 struct AlternateSubst
741 {
serializeOT::AlternateSubst742 bool serialize (hb_serialize_context_t *c,
743 hb_sorted_array_t<const HBGlyphID> glyphs,
744 hb_array_t<const unsigned int> alternate_len_list,
745 hb_array_t<const HBGlyphID> alternate_glyphs_list)
746 {
747 TRACE_SERIALIZE (this);
748 if (unlikely (!c->extend_min (u.format))) return_trace (false);
749 unsigned int format = 1;
750 u.format = format;
751 switch (u.format) {
752 case 1: return_trace (u.format1.serialize (c, glyphs, alternate_len_list, alternate_glyphs_list));
753 default:return_trace (false);
754 }
755 }
756
757 template <typename context_t, typename ...Ts>
dispatchOT::AlternateSubst758 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
759 {
760 TRACE_DISPATCH (this, u.format);
761 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
762 switch (u.format) {
763 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
764 default:return_trace (c->default_return_value ());
765 }
766 }
767
768 protected:
769 union {
770 HBUINT16 format; /* Format identifier */
771 AlternateSubstFormat1 format1;
772 } u;
773 };
774
775
776 struct Ligature
777 {
intersectsOT::Ligature778 bool intersects (const hb_set_t *glyphs) const
779 { return hb_all (component, glyphs); }
780
closureOT::Ligature781 void closure (hb_closure_context_t *c) const
782 {
783 if (!intersects (c->glyphs)) return;
784 c->output->add (ligGlyph);
785 }
786
collect_glyphsOT::Ligature787 void collect_glyphs (hb_collect_glyphs_context_t *c) const
788 {
789 c->input->add_array (component.arrayZ, component.get_length ());
790 c->output->add (ligGlyph);
791 }
792
would_applyOT::Ligature793 bool would_apply (hb_would_apply_context_t *c) const
794 {
795 if (c->len != component.lenP1)
796 return false;
797
798 for (unsigned int i = 1; i < c->len; i++)
799 if (likely (c->glyphs[i] != component[i]))
800 return false;
801
802 return true;
803 }
804
applyOT::Ligature805 bool apply (hb_ot_apply_context_t *c) const
806 {
807 TRACE_APPLY (this);
808 unsigned int count = component.lenP1;
809
810 if (unlikely (!count)) return_trace (false);
811
812 /* Special-case to make it in-place and not consider this
813 * as a "ligated" substitution. */
814 if (unlikely (count == 1))
815 {
816 c->replace_glyph (ligGlyph);
817 return_trace (true);
818 }
819
820 unsigned int total_component_count = 0;
821
822 unsigned int match_length = 0;
823 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
824
825 if (likely (!match_input (c, count,
826 &component[1],
827 match_glyph,
828 nullptr,
829 &match_length,
830 match_positions,
831 &total_component_count)))
832 return_trace (false);
833
834 ligate_input (c,
835 count,
836 match_positions,
837 match_length,
838 ligGlyph,
839 total_component_count);
840
841 return_trace (true);
842 }
843
844 template <typename Iterator,
845 hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))>
serializeOT::Ligature846 bool serialize (hb_serialize_context_t *c,
847 hb_codepoint_t ligature,
848 Iterator components /* Starting from second */)
849 {
850 TRACE_SERIALIZE (this);
851 if (unlikely (!c->extend_min (*this))) return_trace (false);
852 ligGlyph = ligature;
853 if (unlikely (!component.serialize (c, components))) return_trace (false);
854 return_trace (true);
855 }
856
subsetOT::Ligature857 bool subset (hb_subset_context_t *c) const
858 {
859 TRACE_SUBSET (this);
860 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
861 const hb_map_t &glyph_map = *c->plan->glyph_map;
862
863 if (!intersects (&glyphset) || !glyphset.has (ligGlyph)) return_trace (false);
864
865 auto it =
866 + hb_iter (component)
867 | hb_map (glyph_map)
868 ;
869
870 auto *out = c->serializer->start_embed (*this);
871 return_trace (out->serialize (c->serializer,
872 glyph_map[ligGlyph],
873 it));
874 }
875
876 public:
sanitizeOT::Ligature877 bool sanitize (hb_sanitize_context_t *c) const
878 {
879 TRACE_SANITIZE (this);
880 return_trace (ligGlyph.sanitize (c) && component.sanitize (c));
881 }
882
883 protected:
884 HBGlyphID ligGlyph; /* GlyphID of ligature to substitute */
885 HeadlessArrayOf<HBGlyphID>
886 component; /* Array of component GlyphIDs--start
887 * with the second component--ordered
888 * in writing direction */
889 public:
890 DEFINE_SIZE_ARRAY (4, component);
891 };
892
893 struct LigatureSet
894 {
intersectsOT::LigatureSet895 bool intersects (const hb_set_t *glyphs) const
896 {
897 return
898 + hb_iter (ligature)
899 | hb_map (hb_add (this))
900 | hb_map ([glyphs] (const Ligature &_) { return _.intersects (glyphs); })
901 | hb_any
902 ;
903 }
904
closureOT::LigatureSet905 void closure (hb_closure_context_t *c) const
906 {
907 + hb_iter (ligature)
908 | hb_map (hb_add (this))
909 | hb_apply ([c] (const Ligature &_) { _.closure (c); })
910 ;
911 }
912
collect_glyphsOT::LigatureSet913 void collect_glyphs (hb_collect_glyphs_context_t *c) const
914 {
915 + hb_iter (ligature)
916 | hb_map (hb_add (this))
917 | hb_apply ([c] (const Ligature &_) { _.collect_glyphs (c); })
918 ;
919 }
920
would_applyOT::LigatureSet921 bool would_apply (hb_would_apply_context_t *c) const
922 {
923 return
924 + hb_iter (ligature)
925 | hb_map (hb_add (this))
926 | hb_map ([c] (const Ligature &_) { return _.would_apply (c); })
927 | hb_any
928 ;
929 }
930
applyOT::LigatureSet931 bool apply (hb_ot_apply_context_t *c) const
932 {
933 TRACE_APPLY (this);
934 unsigned int num_ligs = ligature.len;
935 for (unsigned int i = 0; i < num_ligs; i++)
936 {
937 const Ligature &lig = this+ligature[i];
938 if (lig.apply (c)) return_trace (true);
939 }
940
941 return_trace (false);
942 }
943
serializeOT::LigatureSet944 bool serialize (hb_serialize_context_t *c,
945 hb_array_t<const HBGlyphID> ligatures,
946 hb_array_t<const unsigned int> component_count_list,
947 hb_array_t<const HBGlyphID> &component_list /* Starting from second for each ligature */)
948 {
949 TRACE_SERIALIZE (this);
950 if (unlikely (!c->extend_min (*this))) return_trace (false);
951 if (unlikely (!ligature.serialize (c, ligatures.length))) return_trace (false);
952 for (unsigned int i = 0; i < ligatures.length; i++)
953 {
954 unsigned int component_count = (unsigned) hb_max ((int) component_count_list[i] - 1, 0);
955 if (unlikely (!ligature[i].serialize (c, this)
956 .serialize (c,
957 ligatures[i],
958 component_list.sub_array (0, component_count))))
959 return_trace (false);
960 component_list += component_count;
961 }
962 return_trace (true);
963 }
964
subsetOT::LigatureSet965 bool subset (hb_subset_context_t *c) const
966 {
967 TRACE_SUBSET (this);
968 auto *out = c->serializer->start_embed (*this);
969 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
970
971 + hb_iter (ligature)
972 | hb_filter (subset_offset_array (c, out->ligature, this))
973 | hb_drain
974 ;
975 return_trace (bool (out->ligature));
976 }
977
sanitizeOT::LigatureSet978 bool sanitize (hb_sanitize_context_t *c) const
979 {
980 TRACE_SANITIZE (this);
981 return_trace (ligature.sanitize (c, this));
982 }
983
984 protected:
985 Array16OfOffset16To<Ligature>
986 ligature; /* Array LigatureSet tables
987 * ordered by preference */
988 public:
989 DEFINE_SIZE_ARRAY (2, ligature);
990 };
991
992 struct LigatureSubstFormat1
993 {
intersectsOT::LigatureSubstFormat1994 bool intersects (const hb_set_t *glyphs) const
995 {
996 return
997 + hb_zip (this+coverage, ligatureSet)
998 | hb_filter (*glyphs, hb_first)
999 | hb_map (hb_second)
1000 | hb_map ([this, glyphs] (const Offset16To<LigatureSet> &_)
1001 { return (this+_).intersects (glyphs); })
1002 | hb_any
1003 ;
1004 }
1005
may_have_non_1to1OT::LigatureSubstFormat11006 bool may_have_non_1to1 () const
1007 { return true; }
1008
closureOT::LigatureSubstFormat11009 void closure (hb_closure_context_t *c) const
1010 {
1011 + hb_zip (this+coverage, ligatureSet)
1012 | hb_filter (c->parent_active_glyphs (), hb_first)
1013 | hb_map (hb_second)
1014 | hb_map (hb_add (this))
1015 | hb_apply ([c] (const LigatureSet &_) { _.closure (c); })
1016 ;
1017
1018 }
1019
closure_lookupsOT::LigatureSubstFormat11020 void closure_lookups (hb_closure_lookups_context_t *c) const {}
1021
collect_glyphsOT::LigatureSubstFormat11022 void collect_glyphs (hb_collect_glyphs_context_t *c) const
1023 {
1024 if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
1025
1026 + hb_zip (this+coverage, ligatureSet)
1027 | hb_map (hb_second)
1028 | hb_map (hb_add (this))
1029 | hb_apply ([c] (const LigatureSet &_) { _.collect_glyphs (c); })
1030 ;
1031 }
1032
get_coverageOT::LigatureSubstFormat11033 const Coverage &get_coverage () const { return this+coverage; }
1034
would_applyOT::LigatureSubstFormat11035 bool would_apply (hb_would_apply_context_t *c) const
1036 {
1037 unsigned int index = (this+coverage).get_coverage (c->glyphs[0]);
1038 if (likely (index == NOT_COVERED)) return false;
1039
1040 const LigatureSet &lig_set = this+ligatureSet[index];
1041 return lig_set.would_apply (c);
1042 }
1043
applyOT::LigatureSubstFormat11044 bool apply (hb_ot_apply_context_t *c) const
1045 {
1046 TRACE_APPLY (this);
1047
1048 unsigned int index = (this+coverage).get_coverage (c->buffer->cur ().codepoint);
1049 if (likely (index == NOT_COVERED)) return_trace (false);
1050
1051 const LigatureSet &lig_set = this+ligatureSet[index];
1052 return_trace (lig_set.apply (c));
1053 }
1054
serializeOT::LigatureSubstFormat11055 bool serialize (hb_serialize_context_t *c,
1056 hb_sorted_array_t<const HBGlyphID> first_glyphs,
1057 hb_array_t<const unsigned int> ligature_per_first_glyph_count_list,
1058 hb_array_t<const HBGlyphID> ligatures_list,
1059 hb_array_t<const unsigned int> component_count_list,
1060 hb_array_t<const HBGlyphID> component_list /* Starting from second for each ligature */)
1061 {
1062 TRACE_SERIALIZE (this);
1063 if (unlikely (!c->extend_min (*this))) return_trace (false);
1064 if (unlikely (!ligatureSet.serialize (c, first_glyphs.length))) return_trace (false);
1065 for (unsigned int i = 0; i < first_glyphs.length; i++)
1066 {
1067 unsigned int ligature_count = ligature_per_first_glyph_count_list[i];
1068 if (unlikely (!ligatureSet[i].serialize (c, this)
1069 .serialize (c,
1070 ligatures_list.sub_array (0, ligature_count),
1071 component_count_list.sub_array (0, ligature_count),
1072 component_list))) return_trace (false);
1073 ligatures_list += ligature_count;
1074 component_count_list += ligature_count;
1075 }
1076 return_trace (coverage.serialize (c, this).serialize (c, first_glyphs));
1077 }
1078
subsetOT::LigatureSubstFormat11079 bool subset (hb_subset_context_t *c) const
1080 {
1081 TRACE_SUBSET (this);
1082 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
1083 const hb_map_t &glyph_map = *c->plan->glyph_map;
1084
1085 auto *out = c->serializer->start_embed (*this);
1086 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
1087 out->format = format;
1088
1089 hb_sorted_vector_t<hb_codepoint_t> new_coverage;
1090 + hb_zip (this+coverage, ligatureSet)
1091 | hb_filter (glyphset, hb_first)
1092 | hb_filter (subset_offset_array (c, out->ligatureSet, this), hb_second)
1093 | hb_map (hb_first)
1094 | hb_map (glyph_map)
1095 | hb_sink (new_coverage)
1096 ;
1097 out->coverage.serialize (c->serializer, out)
1098 .serialize (c->serializer, new_coverage.iter ());
1099 return_trace (bool (new_coverage));
1100 }
1101
sanitizeOT::LigatureSubstFormat11102 bool sanitize (hb_sanitize_context_t *c) const
1103 {
1104 TRACE_SANITIZE (this);
1105 return_trace (coverage.sanitize (c, this) && ligatureSet.sanitize (c, this));
1106 }
1107
1108 protected:
1109 HBUINT16 format; /* Format identifier--format = 1 */
1110 Offset16To<Coverage>
1111 coverage; /* Offset to Coverage table--from
1112 * beginning of Substitution table */
1113 Array16OfOffset16To<LigatureSet>
1114 ligatureSet; /* Array LigatureSet tables
1115 * ordered by Coverage Index */
1116 public:
1117 DEFINE_SIZE_ARRAY (6, ligatureSet);
1118 };
1119
1120 struct LigatureSubst
1121 {
serializeOT::LigatureSubst1122 bool serialize (hb_serialize_context_t *c,
1123 hb_sorted_array_t<const HBGlyphID> first_glyphs,
1124 hb_array_t<const unsigned int> ligature_per_first_glyph_count_list,
1125 hb_array_t<const HBGlyphID> ligatures_list,
1126 hb_array_t<const unsigned int> component_count_list,
1127 hb_array_t<const HBGlyphID> component_list /* Starting from second for each ligature */)
1128 {
1129 TRACE_SERIALIZE (this);
1130 if (unlikely (!c->extend_min (u.format))) return_trace (false);
1131 unsigned int format = 1;
1132 u.format = format;
1133 switch (u.format) {
1134 case 1: return_trace (u.format1.serialize (c,
1135 first_glyphs,
1136 ligature_per_first_glyph_count_list,
1137 ligatures_list,
1138 component_count_list,
1139 component_list));
1140 default:return_trace (false);
1141 }
1142 }
1143
1144 template <typename context_t, typename ...Ts>
dispatchOT::LigatureSubst1145 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
1146 {
1147 TRACE_DISPATCH (this, u.format);
1148 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1149 switch (u.format) {
1150 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
1151 default:return_trace (c->default_return_value ());
1152 }
1153 }
1154
1155 protected:
1156 union {
1157 HBUINT16 format; /* Format identifier */
1158 LigatureSubstFormat1 format1;
1159 } u;
1160 };
1161
1162
1163 struct ContextSubst : Context {};
1164
1165 struct ChainContextSubst : ChainContext {};
1166
1167 struct ExtensionSubst : Extension<ExtensionSubst>
1168 {
1169 typedef struct SubstLookupSubTable SubTable;
1170 bool is_reverse () const;
1171 };
1172
1173
1174 struct ReverseChainSingleSubstFormat1
1175 {
intersectsOT::ReverseChainSingleSubstFormat11176 bool intersects (const hb_set_t *glyphs) const
1177 {
1178 if (!(this+coverage).intersects (glyphs))
1179 return false;
1180
1181 const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (backtrack);
1182
1183 unsigned int count;
1184
1185 count = backtrack.len;
1186 for (unsigned int i = 0; i < count; i++)
1187 if (!(this+backtrack[i]).intersects (glyphs))
1188 return false;
1189
1190 count = lookahead.len;
1191 for (unsigned int i = 0; i < count; i++)
1192 if (!(this+lookahead[i]).intersects (glyphs))
1193 return false;
1194
1195 return true;
1196 }
1197
may_have_non_1to1OT::ReverseChainSingleSubstFormat11198 bool may_have_non_1to1 () const
1199 { return false; }
1200
closureOT::ReverseChainSingleSubstFormat11201 void closure (hb_closure_context_t *c) const
1202 {
1203 if (!intersects (c->glyphs)) return;
1204
1205 const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (backtrack);
1206 const Array16Of<HBGlyphID> &substitute = StructAfter<Array16Of<HBGlyphID>> (lookahead);
1207
1208 + hb_zip (this+coverage, substitute)
1209 | hb_filter (c->parent_active_glyphs (), hb_first)
1210 | hb_map (hb_second)
1211 | hb_sink (c->output)
1212 ;
1213 }
1214
closure_lookupsOT::ReverseChainSingleSubstFormat11215 void closure_lookups (hb_closure_lookups_context_t *c) const {}
1216
collect_glyphsOT::ReverseChainSingleSubstFormat11217 void collect_glyphs (hb_collect_glyphs_context_t *c) const
1218 {
1219 if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
1220
1221 unsigned int count;
1222
1223 count = backtrack.len;
1224 for (unsigned int i = 0; i < count; i++)
1225 if (unlikely (!(this+backtrack[i]).collect_coverage (c->before))) return;
1226
1227 const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (backtrack);
1228 count = lookahead.len;
1229 for (unsigned int i = 0; i < count; i++)
1230 if (unlikely (!(this+lookahead[i]).collect_coverage (c->after))) return;
1231
1232 const Array16Of<HBGlyphID> &substitute = StructAfter<Array16Of<HBGlyphID>> (lookahead);
1233 count = substitute.len;
1234 c->output->add_array (substitute.arrayZ, substitute.len);
1235 }
1236
get_coverageOT::ReverseChainSingleSubstFormat11237 const Coverage &get_coverage () const { return this+coverage; }
1238
would_applyOT::ReverseChainSingleSubstFormat11239 bool would_apply (hb_would_apply_context_t *c) const
1240 { return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; }
1241
applyOT::ReverseChainSingleSubstFormat11242 bool apply (hb_ot_apply_context_t *c) const
1243 {
1244 TRACE_APPLY (this);
1245 if (unlikely (c->nesting_level_left != HB_MAX_NESTING_LEVEL))
1246 return_trace (false); /* No chaining to this type */
1247
1248 unsigned int index = (this+coverage).get_coverage (c->buffer->cur ().codepoint);
1249 if (likely (index == NOT_COVERED)) return_trace (false);
1250
1251 const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (backtrack);
1252 const Array16Of<HBGlyphID> &substitute = StructAfter<Array16Of<HBGlyphID>> (lookahead);
1253
1254 if (unlikely (index >= substitute.len)) return_trace (false);
1255
1256 unsigned int start_index = 0, end_index = 0;
1257 if (match_backtrack (c,
1258 backtrack.len, (HBUINT16 *) backtrack.arrayZ,
1259 match_coverage, this,
1260 &start_index) &&
1261 match_lookahead (c,
1262 lookahead.len, (HBUINT16 *) lookahead.arrayZ,
1263 match_coverage, this,
1264 1, &end_index))
1265 {
1266 c->buffer->unsafe_to_break_from_outbuffer (start_index, end_index);
1267 c->replace_glyph_inplace (substitute[index]);
1268 /* Note: We DON'T decrease buffer->idx. The main loop does it
1269 * for us. This is useful for preventing surprises if someone
1270 * calls us through a Context lookup. */
1271 return_trace (true);
1272 }
1273
1274 return_trace (false);
1275 }
1276
1277 template<typename Iterator,
1278 hb_requires (hb_is_iterator (Iterator))>
serialize_coverage_offset_arrayOT::ReverseChainSingleSubstFormat11279 bool serialize_coverage_offset_array (hb_subset_context_t *c, Iterator it) const
1280 {
1281 TRACE_SERIALIZE (this);
1282 auto *out = c->serializer->start_embed<Array16OfOffset16To<Coverage>> ();
1283
1284 if (unlikely (!c->serializer->allocate_size<HBUINT16> (HBUINT16::static_size)))
1285 return_trace (false);
1286
1287 for (auto& offset : it) {
1288 auto *o = out->serialize_append (c->serializer);
1289 if (unlikely (!o) || !o->serialize_subset (c, offset, this))
1290 return_trace (false);
1291 }
1292
1293 return_trace (true);
1294 }
1295
1296 template<typename Iterator, typename BacktrackIterator, typename LookaheadIterator,
1297 hb_requires (hb_is_sorted_source_of (Iterator, hb_codepoint_pair_t)),
1298 hb_requires (hb_is_iterator (BacktrackIterator)),
1299 hb_requires (hb_is_iterator (LookaheadIterator))>
serializeOT::ReverseChainSingleSubstFormat11300 bool serialize (hb_subset_context_t *c,
1301 Iterator coverage_subst_iter,
1302 BacktrackIterator backtrack_iter,
1303 LookaheadIterator lookahead_iter) const
1304 {
1305 TRACE_SERIALIZE (this);
1306
1307 auto *out = c->serializer->start_embed (this);
1308 if (unlikely (!c->serializer->check_success (out))) return_trace (false);
1309 if (unlikely (!c->serializer->embed (this->format))) return_trace (false);
1310 if (unlikely (!c->serializer->embed (this->coverage))) return_trace (false);
1311
1312 if (!serialize_coverage_offset_array (c, backtrack_iter)) return_trace (false);
1313 if (!serialize_coverage_offset_array (c, lookahead_iter)) return_trace (false);
1314
1315 auto *substitute_out = c->serializer->start_embed<Array16Of<HBGlyphID>> ();
1316 auto substitutes =
1317 + coverage_subst_iter
1318 | hb_map (hb_second)
1319 ;
1320
1321 auto glyphs =
1322 + coverage_subst_iter
1323 | hb_map_retains_sorting (hb_first)
1324 ;
1325 if (unlikely (! c->serializer->check_success (substitute_out->serialize (c->serializer, substitutes))))
1326 return_trace (false);
1327
1328 if (unlikely (!out->coverage.serialize (c->serializer, out).serialize (c->serializer, glyphs)))
1329 return_trace (false);
1330 return_trace (true);
1331 }
1332
subsetOT::ReverseChainSingleSubstFormat11333 bool subset (hb_subset_context_t *c) const
1334 {
1335 TRACE_SUBSET (this);
1336 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
1337 const hb_map_t &glyph_map = *c->plan->glyph_map;
1338
1339 const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (backtrack);
1340 const Array16Of<HBGlyphID> &substitute = StructAfter<Array16Of<HBGlyphID>> (lookahead);
1341
1342 auto it =
1343 + hb_zip (this+coverage, substitute)
1344 | hb_filter (glyphset, hb_first)
1345 | hb_filter (glyphset, hb_second)
1346 | hb_map_retains_sorting ([&] (hb_pair_t<hb_codepoint_t, const HBGlyphID &> p) -> hb_codepoint_pair_t
1347 { return hb_pair (glyph_map[p.first], glyph_map[p.second]); })
1348 ;
1349
1350 return_trace (bool (it) && serialize (c, it, backtrack.iter (), lookahead.iter ()));
1351 }
1352
sanitizeOT::ReverseChainSingleSubstFormat11353 bool sanitize (hb_sanitize_context_t *c) const
1354 {
1355 TRACE_SANITIZE (this);
1356 if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this)))
1357 return_trace (false);
1358 const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (backtrack);
1359 if (!lookahead.sanitize (c, this))
1360 return_trace (false);
1361 const Array16Of<HBGlyphID> &substitute = StructAfter<Array16Of<HBGlyphID>> (lookahead);
1362 return_trace (substitute.sanitize (c));
1363 }
1364
1365 protected:
1366 HBUINT16 format; /* Format identifier--format = 1 */
1367 Offset16To<Coverage>
1368 coverage; /* Offset to Coverage table--from
1369 * beginning of table */
1370 Array16OfOffset16To<Coverage>
1371 backtrack; /* Array of coverage tables
1372 * in backtracking sequence, in glyph
1373 * sequence order */
1374 Array16OfOffset16To<Coverage>
1375 lookaheadX; /* Array of coverage tables
1376 * in lookahead sequence, in glyph
1377 * sequence order */
1378 Array16Of<HBGlyphID>
1379 substituteX; /* Array of substitute
1380 * GlyphIDs--ordered by Coverage Index */
1381 public:
1382 DEFINE_SIZE_MIN (10);
1383 };
1384
1385 struct ReverseChainSingleSubst
1386 {
1387 template <typename context_t, typename ...Ts>
dispatchOT::ReverseChainSingleSubst1388 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
1389 {
1390 TRACE_DISPATCH (this, u.format);
1391 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1392 switch (u.format) {
1393 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
1394 default:return_trace (c->default_return_value ());
1395 }
1396 }
1397
1398 protected:
1399 union {
1400 HBUINT16 format; /* Format identifier */
1401 ReverseChainSingleSubstFormat1 format1;
1402 } u;
1403 };
1404
1405
1406
1407 /*
1408 * SubstLookup
1409 */
1410
1411 struct SubstLookupSubTable
1412 {
1413 friend struct Lookup;
1414 friend struct SubstLookup;
1415
1416 enum Type {
1417 Single = 1,
1418 Multiple = 2,
1419 Alternate = 3,
1420 Ligature = 4,
1421 Context = 5,
1422 ChainContext = 6,
1423 Extension = 7,
1424 ReverseChainSingle = 8
1425 };
1426
1427 template <typename context_t, typename ...Ts>
dispatchOT::SubstLookupSubTable1428 typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type, Ts&&... ds) const
1429 {
1430 TRACE_DISPATCH (this, lookup_type);
1431 switch (lookup_type) {
1432 case Single: return_trace (u.single.dispatch (c, hb_forward<Ts> (ds)...));
1433 case Multiple: return_trace (u.multiple.dispatch (c, hb_forward<Ts> (ds)...));
1434 case Alternate: return_trace (u.alternate.dispatch (c, hb_forward<Ts> (ds)...));
1435 case Ligature: return_trace (u.ligature.dispatch (c, hb_forward<Ts> (ds)...));
1436 case Context: return_trace (u.context.dispatch (c, hb_forward<Ts> (ds)...));
1437 case ChainContext: return_trace (u.chainContext.dispatch (c, hb_forward<Ts> (ds)...));
1438 case Extension: return_trace (u.extension.dispatch (c, hb_forward<Ts> (ds)...));
1439 case ReverseChainSingle: return_trace (u.reverseChainContextSingle.dispatch (c, hb_forward<Ts> (ds)...));
1440 default: return_trace (c->default_return_value ());
1441 }
1442 }
1443
intersectsOT::SubstLookupSubTable1444 bool intersects (const hb_set_t *glyphs, unsigned int lookup_type) const
1445 {
1446 hb_intersects_context_t c (glyphs);
1447 return dispatch (&c, lookup_type);
1448 }
1449
1450 protected:
1451 union {
1452 SingleSubst single;
1453 MultipleSubst multiple;
1454 AlternateSubst alternate;
1455 LigatureSubst ligature;
1456 ContextSubst context;
1457 ChainContextSubst chainContext;
1458 ExtensionSubst extension;
1459 ReverseChainSingleSubst reverseChainContextSingle;
1460 } u;
1461 public:
1462 DEFINE_SIZE_MIN (0);
1463 };
1464
1465
1466 struct SubstLookup : Lookup
1467 {
1468 typedef SubstLookupSubTable SubTable;
1469
get_subtableOT::SubstLookup1470 const SubTable& get_subtable (unsigned int i) const
1471 { return Lookup::get_subtable<SubTable> (i); }
1472
lookup_type_is_reverseOT::SubstLookup1473 static inline bool lookup_type_is_reverse (unsigned int lookup_type)
1474 { return lookup_type == SubTable::ReverseChainSingle; }
1475
is_reverseOT::SubstLookup1476 bool is_reverse () const
1477 {
1478 unsigned int type = get_type ();
1479 if (unlikely (type == SubTable::Extension))
1480 return reinterpret_cast<const ExtensionSubst &> (get_subtable (0)).is_reverse ();
1481 return lookup_type_is_reverse (type);
1482 }
1483
may_have_non_1to1OT::SubstLookup1484 bool may_have_non_1to1 () const
1485 {
1486 hb_have_non_1to1_context_t c;
1487 return dispatch (&c);
1488 }
1489
applyOT::SubstLookup1490 bool apply (hb_ot_apply_context_t *c) const
1491 {
1492 TRACE_APPLY (this);
1493 return_trace (dispatch (c));
1494 }
1495
intersectsOT::SubstLookup1496 bool intersects (const hb_set_t *glyphs) const
1497 {
1498 hb_intersects_context_t c (glyphs);
1499 return dispatch (&c);
1500 }
1501
closureOT::SubstLookup1502 hb_closure_context_t::return_t closure (hb_closure_context_t *c, unsigned int this_index) const
1503 {
1504 if (!c->should_visit_lookup (this_index))
1505 return hb_closure_context_t::default_return_value ();
1506
1507 c->set_recurse_func (dispatch_closure_recurse_func);
1508
1509 hb_closure_context_t::return_t ret = dispatch (c);
1510
1511 c->flush ();
1512
1513 return ret;
1514 }
1515
closure_lookupsOT::SubstLookup1516 hb_closure_lookups_context_t::return_t closure_lookups (hb_closure_lookups_context_t *c, unsigned this_index) const
1517 {
1518 if (c->is_lookup_visited (this_index))
1519 return hb_closure_lookups_context_t::default_return_value ();
1520
1521 c->set_lookup_visited (this_index);
1522 if (!intersects (c->glyphs))
1523 {
1524 c->set_lookup_inactive (this_index);
1525 return hb_closure_lookups_context_t::default_return_value ();
1526 }
1527
1528 c->set_recurse_func (dispatch_closure_lookups_recurse_func);
1529
1530 hb_closure_lookups_context_t::return_t ret = dispatch (c);
1531 return ret;
1532 }
1533
collect_glyphsOT::SubstLookup1534 hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
1535 {
1536 c->set_recurse_func (dispatch_recurse_func<hb_collect_glyphs_context_t>);
1537 return dispatch (c);
1538 }
1539
1540 template <typename set_t>
collect_coverageOT::SubstLookup1541 void collect_coverage (set_t *glyphs) const
1542 {
1543 hb_collect_coverage_context_t<set_t> c (glyphs);
1544 dispatch (&c);
1545 }
1546
would_applyOT::SubstLookup1547 bool would_apply (hb_would_apply_context_t *c,
1548 const hb_ot_layout_lookup_accelerator_t *accel) const
1549 {
1550 if (unlikely (!c->len)) return false;
1551 if (!accel->may_have (c->glyphs[0])) return false;
1552 return dispatch (c);
1553 }
1554
1555 static inline bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
1556
serialize_subtableOT::SubstLookup1557 SubTable& serialize_subtable (hb_serialize_context_t *c,
1558 unsigned int i)
1559 { return get_subtables<SubTable> ()[i].serialize (c, this); }
1560
serialize_singleOT::SubstLookup1561 bool serialize_single (hb_serialize_context_t *c,
1562 uint32_t lookup_props,
1563 hb_sorted_array_t<const HBGlyphID> glyphs,
1564 hb_array_t<const HBGlyphID> substitutes)
1565 {
1566 TRACE_SERIALIZE (this);
1567 if (unlikely (!Lookup::serialize (c, SubTable::Single, lookup_props, 1))) return_trace (false);
1568 return_trace (serialize_subtable (c, 0).u.single.
1569 serialize (c, hb_zip (glyphs, substitutes)));
1570 }
1571
serialize_multipleOT::SubstLookup1572 bool serialize_multiple (hb_serialize_context_t *c,
1573 uint32_t lookup_props,
1574 hb_sorted_array_t<const HBGlyphID> glyphs,
1575 hb_array_t<const unsigned int> substitute_len_list,
1576 hb_array_t<const HBGlyphID> substitute_glyphs_list)
1577 {
1578 TRACE_SERIALIZE (this);
1579 if (unlikely (!Lookup::serialize (c, SubTable::Multiple, lookup_props, 1))) return_trace (false);
1580 return_trace (serialize_subtable (c, 0).u.multiple.
1581 serialize (c,
1582 glyphs,
1583 substitute_len_list,
1584 substitute_glyphs_list));
1585 }
1586
serialize_alternateOT::SubstLookup1587 bool serialize_alternate (hb_serialize_context_t *c,
1588 uint32_t lookup_props,
1589 hb_sorted_array_t<const HBGlyphID> glyphs,
1590 hb_array_t<const unsigned int> alternate_len_list,
1591 hb_array_t<const HBGlyphID> alternate_glyphs_list)
1592 {
1593 TRACE_SERIALIZE (this);
1594 if (unlikely (!Lookup::serialize (c, SubTable::Alternate, lookup_props, 1))) return_trace (false);
1595 return_trace (serialize_subtable (c, 0).u.alternate.
1596 serialize (c,
1597 glyphs,
1598 alternate_len_list,
1599 alternate_glyphs_list));
1600 }
1601
serialize_ligatureOT::SubstLookup1602 bool serialize_ligature (hb_serialize_context_t *c,
1603 uint32_t lookup_props,
1604 hb_sorted_array_t<const HBGlyphID> first_glyphs,
1605 hb_array_t<const unsigned int> ligature_per_first_glyph_count_list,
1606 hb_array_t<const HBGlyphID> ligatures_list,
1607 hb_array_t<const unsigned int> component_count_list,
1608 hb_array_t<const HBGlyphID> component_list /* Starting from second for each ligature */)
1609 {
1610 TRACE_SERIALIZE (this);
1611 if (unlikely (!Lookup::serialize (c, SubTable::Ligature, lookup_props, 1))) return_trace (false);
1612 return_trace (serialize_subtable (c, 0).u.ligature.
1613 serialize (c,
1614 first_glyphs,
1615 ligature_per_first_glyph_count_list,
1616 ligatures_list,
1617 component_count_list,
1618 component_list));
1619 }
1620
1621 template <typename context_t>
1622 static inline typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
1623
1624 static inline typename hb_closure_context_t::return_t closure_glyphs_recurse_func (hb_closure_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indices, unsigned seq_index, unsigned end_index);
1625
dispatch_closure_recurse_funcOT::SubstLookup1626 static inline hb_closure_context_t::return_t dispatch_closure_recurse_func (hb_closure_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indices, unsigned seq_index, unsigned end_index)
1627 {
1628 if (!c->should_visit_lookup (lookup_index))
1629 return hb_empty_t ();
1630
1631 hb_closure_context_t::return_t ret = closure_glyphs_recurse_func (c, lookup_index, covered_seq_indices, seq_index, end_index);
1632
1633 /* While in theory we should flush here, it will cause timeouts because a recursive
1634 * lookup can keep growing the glyph set. Skip, and outer loop will retry up to
1635 * HB_CLOSURE_MAX_STAGES time, which should be enough for every realistic font. */
1636 //c->flush ();
1637
1638 return ret;
1639 }
1640
1641 HB_INTERNAL static hb_closure_lookups_context_t::return_t dispatch_closure_lookups_recurse_func (hb_closure_lookups_context_t *c, unsigned lookup_index);
1642
1643 template <typename context_t, typename ...Ts>
dispatchOT::SubstLookup1644 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
1645 { return Lookup::dispatch<SubTable> (c, hb_forward<Ts> (ds)...); }
1646
subsetOT::SubstLookup1647 bool subset (hb_subset_context_t *c) const
1648 { return Lookup::subset<SubTable> (c); }
1649
sanitizeOT::SubstLookup1650 bool sanitize (hb_sanitize_context_t *c) const
1651 { return Lookup::sanitize<SubTable> (c); }
1652 };
1653
1654 /*
1655 * GSUB -- Glyph Substitution
1656 * https://docs.microsoft.com/en-us/typography/opentype/spec/gsub
1657 */
1658
1659 struct GSUB : GSUBGPOS
1660 {
1661 static constexpr hb_tag_t tableTag = HB_OT_TAG_GSUB;
1662
get_lookupOT::GSUB1663 const SubstLookup& get_lookup (unsigned int i) const
1664 { return static_cast<const SubstLookup &> (GSUBGPOS::get_lookup (i)); }
1665
subsetOT::GSUB1666 bool subset (hb_subset_context_t *c) const
1667 {
1668 hb_subset_layout_context_t l (c, tableTag, c->plan->gsub_lookups, c->plan->gsub_langsys, c->plan->gsub_features);
1669 return GSUBGPOS::subset<SubstLookup> (&l);
1670 }
1671
sanitizeOT::GSUB1672 bool sanitize (hb_sanitize_context_t *c) const
1673 { return GSUBGPOS::sanitize<SubstLookup> (c); }
1674
1675 HB_INTERNAL bool is_blocklisted (hb_blob_t *blob,
1676 hb_face_t *face) const;
1677
closure_lookupsOT::GSUB1678 void closure_lookups (hb_face_t *face,
1679 const hb_set_t *glyphs,
1680 hb_set_t *lookup_indexes /* IN/OUT */) const
1681 { GSUBGPOS::closure_lookups<SubstLookup> (face, glyphs, lookup_indexes); }
1682
1683 typedef GSUBGPOS::accelerator_t<GSUB> accelerator_t;
1684 };
1685
1686
1687 struct GSUB_accelerator_t : GSUB::accelerator_t {};
1688
1689
1690 /* Out-of-class implementation for methods recursing */
1691
1692 #ifndef HB_NO_OT_LAYOUT
is_reverse() const1693 /*static*/ inline bool ExtensionSubst::is_reverse () const
1694 {
1695 return SubstLookup::lookup_type_is_reverse (get_type ());
1696 }
1697 template <typename context_t>
dispatch_recurse_func(context_t * c,unsigned int lookup_index)1698 /*static*/ typename context_t::return_t SubstLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
1699 {
1700 const SubstLookup &l = c->face->table.GSUB.get_relaxed ()->table->get_lookup (lookup_index);
1701 return l.dispatch (c);
1702 }
1703
closure_glyphs_recurse_func(hb_closure_context_t * c,unsigned lookup_index,hb_set_t * covered_seq_indices,unsigned seq_index,unsigned end_index)1704 /*static*/ typename hb_closure_context_t::return_t SubstLookup::closure_glyphs_recurse_func (hb_closure_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indices, unsigned seq_index, unsigned end_index)
1705 {
1706 const SubstLookup &l = c->face->table.GSUB.get_relaxed ()->table->get_lookup (lookup_index);
1707 if (l.may_have_non_1to1 ())
1708 hb_set_add_range (covered_seq_indices, seq_index, end_index);
1709 return l.dispatch (c);
1710 }
1711
dispatch_closure_lookups_recurse_func(hb_closure_lookups_context_t * c,unsigned this_index)1712 /*static*/ inline hb_closure_lookups_context_t::return_t SubstLookup::dispatch_closure_lookups_recurse_func (hb_closure_lookups_context_t *c, unsigned this_index)
1713 {
1714 const SubstLookup &l = c->face->table.GSUB.get_relaxed ()->table->get_lookup (this_index);
1715 return l.closure_lookups (c, this_index);
1716 }
1717
apply_recurse_func(hb_ot_apply_context_t * c,unsigned int lookup_index)1718 /*static*/ bool SubstLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
1719 {
1720 const SubstLookup &l = c->face->table.GSUB.get_relaxed ()->table->get_lookup (lookup_index);
1721 unsigned int saved_lookup_props = c->lookup_props;
1722 unsigned int saved_lookup_index = c->lookup_index;
1723 c->set_lookup_index (lookup_index);
1724 c->set_lookup_props (l.get_props ());
1725 bool ret = l.dispatch (c);
1726 c->set_lookup_index (saved_lookup_index);
1727 c->set_lookup_props (saved_lookup_props);
1728 return ret;
1729 }
1730 #endif
1731
1732
1733 } /* namespace OT */
1734
1735
1736 #endif /* HB_OT_LAYOUT_GSUB_TABLE_HH */
1737