-
Notifications
You must be signed in to change notification settings - Fork 660
/
Copy pathhb-ot-layout-gsubgpos.hh
4387 lines (3785 loc) · 137 KB
/
hb-ot-layout-gsubgpos.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2007,2008,2009,2010 Red Hat, Inc.
* Copyright © 2010,2012 Google, Inc.
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Red Hat Author(s): Behdad Esfahbod
* Google Author(s): Behdad Esfahbod
*/
#ifndef HB_OT_LAYOUT_GSUBGPOS_HH
#define HB_OT_LAYOUT_GSUBGPOS_HH
#include "hb.hh"
#include "hb-buffer.hh"
#include "hb-map.hh"
#include "hb-set.hh"
#include "hb-ot-map.hh"
#include "hb-ot-layout-common.hh"
#include "hb-ot-layout-gdef-table.hh"
namespace OT {
struct hb_intersects_context_t :
hb_dispatch_context_t<hb_intersects_context_t, bool>
{
template <typename T>
return_t dispatch (const T &obj) { return obj.intersects (this->glyphs); }
static return_t default_return_value () { return false; }
bool stop_sublookup_iteration (return_t r) const { return r; }
const hb_set_t *glyphs;
hb_intersects_context_t (const hb_set_t *glyphs_) :
glyphs (glyphs_) {}
};
struct hb_have_non_1to1_context_t :
hb_dispatch_context_t<hb_have_non_1to1_context_t, bool>
{
template <typename T>
return_t dispatch (const T &obj) { return obj.may_have_non_1to1 (); }
static return_t default_return_value () { return false; }
bool stop_sublookup_iteration (return_t r) const { return r; }
};
struct hb_closure_context_t :
hb_dispatch_context_t<hb_closure_context_t>
{
typedef return_t (*recurse_func_t) (hb_closure_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indicies, unsigned seq_index, unsigned end_index);
template <typename T>
return_t dispatch (const T &obj) { obj.closure (this); return hb_empty_t (); }
static return_t default_return_value () { return hb_empty_t (); }
void recurse (unsigned lookup_index, hb_set_t *covered_seq_indicies, unsigned seq_index, unsigned end_index)
{
if (unlikely (nesting_level_left == 0 || !recurse_func))
return;
nesting_level_left--;
recurse_func (this, lookup_index, covered_seq_indicies, seq_index, end_index);
nesting_level_left++;
}
void reset_lookup_visit_count ()
{ lookup_count = 0; }
bool lookup_limit_exceeded ()
{ return lookup_count > HB_MAX_LOOKUP_VISIT_COUNT; }
bool should_visit_lookup (unsigned int lookup_index)
{
if (lookup_count++ > HB_MAX_LOOKUP_VISIT_COUNT)
return false;
if (is_lookup_done (lookup_index))
return false;
return true;
}
bool is_lookup_done (unsigned int lookup_index)
{
if (done_lookups_glyph_count->in_error () ||
done_lookups_glyph_set->in_error ())
return true;
/* Have we visited this lookup with the current set of glyphs? */
if (done_lookups_glyph_count->get (lookup_index) != glyphs->get_population ())
{
done_lookups_glyph_count->set (lookup_index, glyphs->get_population ());
if (!done_lookups_glyph_set->has (lookup_index))
{
if (unlikely (!done_lookups_glyph_set->set (lookup_index, hb::unique_ptr<hb_set_t> {hb_set_create ()})))
return true;
}
hb_set_clear (done_lookups_glyph_set->get (lookup_index));
}
hb_set_t *covered_glyph_set = done_lookups_glyph_set->get (lookup_index);
if (unlikely (covered_glyph_set->in_error ()))
return true;
if (parent_active_glyphs ().is_subset (*covered_glyph_set))
return true;
covered_glyph_set->union_ (parent_active_glyphs ());
return false;
}
const hb_set_t& previous_parent_active_glyphs () {
if (active_glyphs_stack.length <= 1)
return *glyphs;
return active_glyphs_stack[active_glyphs_stack.length - 2];
}
const hb_set_t& parent_active_glyphs ()
{
if (!active_glyphs_stack)
return *glyphs;
return active_glyphs_stack.tail ();
}
hb_set_t& push_cur_active_glyphs ()
{
return *active_glyphs_stack.push ();
}
bool pop_cur_done_glyphs ()
{
if (!active_glyphs_stack)
return false;
active_glyphs_stack.pop ();
return true;
}
hb_face_t *face;
hb_set_t *glyphs;
hb_set_t output[1];
hb_vector_t<hb_set_t> active_glyphs_stack;
recurse_func_t recurse_func = nullptr;
unsigned int nesting_level_left;
hb_closure_context_t (hb_face_t *face_,
hb_set_t *glyphs_,
hb_map_t *done_lookups_glyph_count_,
hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> *done_lookups_glyph_set_,
unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
face (face_),
glyphs (glyphs_),
nesting_level_left (nesting_level_left_),
done_lookups_glyph_count (done_lookups_glyph_count_),
done_lookups_glyph_set (done_lookups_glyph_set_)
{}
~hb_closure_context_t () { flush (); }
void set_recurse_func (recurse_func_t func) { recurse_func = func; }
void flush ()
{
output->del_range (face->get_num_glyphs (), HB_SET_VALUE_INVALID); /* Remove invalid glyphs. */
glyphs->union_ (*output);
output->clear ();
active_glyphs_stack.pop ();
active_glyphs_stack.reset ();
}
private:
hb_map_t *done_lookups_glyph_count;
hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> *done_lookups_glyph_set;
unsigned int lookup_count = 0;
};
struct hb_closure_lookups_context_t :
hb_dispatch_context_t<hb_closure_lookups_context_t>
{
typedef return_t (*recurse_func_t) (hb_closure_lookups_context_t *c, unsigned lookup_index);
template <typename T>
return_t dispatch (const T &obj) { obj.closure_lookups (this); return hb_empty_t (); }
static return_t default_return_value () { return hb_empty_t (); }
void recurse (unsigned lookup_index)
{
if (unlikely (nesting_level_left == 0 || !recurse_func))
return;
/* Return if new lookup was recursed to before. */
if (lookup_limit_exceeded ()
|| visited_lookups->in_error ()
|| visited_lookups->has (lookup_index))
// Don't increment lookup count here, that will be done in the call to closure_lookups()
// made by recurse_func.
return;
nesting_level_left--;
recurse_func (this, lookup_index);
nesting_level_left++;
}
void set_lookup_visited (unsigned lookup_index)
{ visited_lookups->add (lookup_index); }
void set_lookup_inactive (unsigned lookup_index)
{ inactive_lookups->add (lookup_index); }
bool lookup_limit_exceeded ()
{
bool ret = lookup_count > HB_MAX_LOOKUP_VISIT_COUNT;
if (ret)
DEBUG_MSG (SUBSET, nullptr, "lookup visit count limit exceeded in lookup closure!");
return ret; }
bool is_lookup_visited (unsigned lookup_index)
{
if (unlikely (lookup_count++ > HB_MAX_LOOKUP_VISIT_COUNT))
{
DEBUG_MSG (SUBSET, nullptr, "total visited lookup count %u exceeds max limit, lookup %u is dropped.",
lookup_count, lookup_index);
return true;
}
if (unlikely (visited_lookups->in_error ()))
return true;
return visited_lookups->has (lookup_index);
}
hb_face_t *face;
const hb_set_t *glyphs;
recurse_func_t recurse_func;
unsigned int nesting_level_left;
hb_closure_lookups_context_t (hb_face_t *face_,
const hb_set_t *glyphs_,
hb_set_t *visited_lookups_,
hb_set_t *inactive_lookups_,
unsigned nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
face (face_),
glyphs (glyphs_),
recurse_func (nullptr),
nesting_level_left (nesting_level_left_),
visited_lookups (visited_lookups_),
inactive_lookups (inactive_lookups_),
lookup_count (0) {}
void set_recurse_func (recurse_func_t func) { recurse_func = func; }
private:
hb_set_t *visited_lookups;
hb_set_t *inactive_lookups;
unsigned int lookup_count;
};
struct hb_would_apply_context_t :
hb_dispatch_context_t<hb_would_apply_context_t, bool>
{
template <typename T>
return_t dispatch (const T &obj) { return obj.would_apply (this); }
static return_t default_return_value () { return false; }
bool stop_sublookup_iteration (return_t r) const { return r; }
hb_face_t *face;
const hb_codepoint_t *glyphs;
unsigned int len;
bool zero_context;
hb_would_apply_context_t (hb_face_t *face_,
const hb_codepoint_t *glyphs_,
unsigned int len_,
bool zero_context_) :
face (face_),
glyphs (glyphs_),
len (len_),
zero_context (zero_context_) {}
};
struct hb_collect_glyphs_context_t :
hb_dispatch_context_t<hb_collect_glyphs_context_t>
{
typedef return_t (*recurse_func_t) (hb_collect_glyphs_context_t *c, unsigned int lookup_index);
template <typename T>
return_t dispatch (const T &obj) { obj.collect_glyphs (this); return hb_empty_t (); }
static return_t default_return_value () { return hb_empty_t (); }
void recurse (unsigned int lookup_index)
{
if (unlikely (nesting_level_left == 0 || !recurse_func))
return;
/* Note that GPOS sets recurse_func to nullptr already, so it doesn't get
* past the previous check. For GSUB, we only want to collect the output
* glyphs in the recursion. If output is not requested, we can go home now.
*
* Note further, that the above is not exactly correct. A recursed lookup
* is allowed to match input that is not matched in the context, but that's
* not how most fonts are built. It's possible to relax that and recurse
* with all sets here if it proves to be an issue.
*/
if (output == hb_set_get_empty ())
return;
/* Return if new lookup was recursed to before. */
if (recursed_lookups->has (lookup_index))
return;
hb_set_t *old_before = before;
hb_set_t *old_input = input;
hb_set_t *old_after = after;
before = input = after = hb_set_get_empty ();
nesting_level_left--;
recurse_func (this, lookup_index);
nesting_level_left++;
before = old_before;
input = old_input;
after = old_after;
recursed_lookups->add (lookup_index);
}
hb_face_t *face;
hb_set_t *before;
hb_set_t *input;
hb_set_t *after;
hb_set_t *output;
recurse_func_t recurse_func;
hb_set_t *recursed_lookups;
unsigned int nesting_level_left;
hb_collect_glyphs_context_t (hb_face_t *face_,
hb_set_t *glyphs_before, /* OUT. May be NULL */
hb_set_t *glyphs_input, /* OUT. May be NULL */
hb_set_t *glyphs_after, /* OUT. May be NULL */
hb_set_t *glyphs_output, /* OUT. May be NULL */
unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
face (face_),
before (glyphs_before ? glyphs_before : hb_set_get_empty ()),
input (glyphs_input ? glyphs_input : hb_set_get_empty ()),
after (glyphs_after ? glyphs_after : hb_set_get_empty ()),
output (glyphs_output ? glyphs_output : hb_set_get_empty ()),
recurse_func (nullptr),
recursed_lookups (hb_set_create ()),
nesting_level_left (nesting_level_left_) {}
~hb_collect_glyphs_context_t () { hb_set_destroy (recursed_lookups); }
void set_recurse_func (recurse_func_t func) { recurse_func = func; }
};
template <typename set_t>
struct hb_collect_coverage_context_t :
hb_dispatch_context_t<hb_collect_coverage_context_t<set_t>, const Coverage &>
{
typedef const Coverage &return_t; // Stoopid that we have to dupe this here.
template <typename T>
return_t dispatch (const T &obj) { return obj.get_coverage (); }
static return_t default_return_value () { return Null (Coverage); }
bool stop_sublookup_iteration (return_t r) const
{
r.collect_coverage (set);
return false;
}
hb_collect_coverage_context_t (set_t *set_) :
set (set_) {}
set_t *set;
};
struct hb_ot_apply_context_t :
hb_dispatch_context_t<hb_ot_apply_context_t, bool, HB_DEBUG_APPLY>
{
struct matcher_t
{
matcher_t () :
lookup_props (0),
mask (-1),
ignore_zwnj (false),
ignore_zwj (false),
per_syllable (false),
syllable {0},
match_func (nullptr),
match_data (nullptr) {}
typedef bool (*match_func_t) (hb_glyph_info_t &info, unsigned value, const void *data);
void set_ignore_zwnj (bool ignore_zwnj_) { ignore_zwnj = ignore_zwnj_; }
void set_ignore_zwj (bool ignore_zwj_) { ignore_zwj = ignore_zwj_; }
void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
void set_mask (hb_mask_t mask_) { mask = mask_; }
void set_per_syllable (bool per_syllable_) { per_syllable = per_syllable_; }
void set_syllable (uint8_t syllable_) { syllable = per_syllable ? syllable_ : 0; }
void set_match_func (match_func_t match_func_,
const void *match_data_)
{ match_func = match_func_; match_data = match_data_; }
enum may_match_t {
MATCH_NO,
MATCH_YES,
MATCH_MAYBE
};
may_match_t may_match (hb_glyph_info_t &info,
hb_codepoint_t glyph_data) const
{
if (!(info.mask & mask) ||
(syllable && syllable != info.syllable ()))
return MATCH_NO;
if (match_func)
return match_func (info, glyph_data, match_data) ? MATCH_YES : MATCH_NO;
return MATCH_MAYBE;
}
enum may_skip_t {
SKIP_NO,
SKIP_YES,
SKIP_MAYBE
};
may_skip_t may_skip (const hb_ot_apply_context_t *c,
const hb_glyph_info_t &info) const
{
if (!c->check_glyph_property (&info, lookup_props))
return SKIP_YES;
if (unlikely (_hb_glyph_info_is_default_ignorable_and_not_hidden (&info) &&
(ignore_zwnj || !_hb_glyph_info_is_zwnj (&info)) &&
(ignore_zwj || !_hb_glyph_info_is_zwj (&info))))
return SKIP_MAYBE;
return SKIP_NO;
}
protected:
unsigned int lookup_props;
hb_mask_t mask;
bool ignore_zwnj;
bool ignore_zwj;
bool per_syllable;
uint8_t syllable;
match_func_t match_func;
const void *match_data;
};
struct skipping_iterator_t
{
void init (hb_ot_apply_context_t *c_, bool context_match = false)
{
c = c_;
match_glyph_data16 = nullptr;
#ifndef HB_NO_BEYOND_64K
match_glyph_data24 = nullptr;
#endif
matcher.set_match_func (nullptr, nullptr);
matcher.set_lookup_props (c->lookup_props);
/* Ignore ZWNJ if we are matching GPOS, or matching GSUB context and asked to. */
matcher.set_ignore_zwnj (c->table_index == 1 || (context_match && c->auto_zwnj));
/* Ignore ZWJ if we are matching context, or asked to. */
matcher.set_ignore_zwj (context_match || c->auto_zwj);
matcher.set_mask (context_match ? -1 : c->lookup_mask);
matcher.set_per_syllable (c->per_syllable);
}
void set_lookup_props (unsigned int lookup_props)
{
matcher.set_lookup_props (lookup_props);
}
void set_match_func (matcher_t::match_func_t match_func_,
const void *match_data_)
{
matcher.set_match_func (match_func_, match_data_);
}
void set_glyph_data (const HBUINT16 glyph_data[])
{
match_glyph_data16 = glyph_data;
#ifndef HB_NO_BEYOND_64K
match_glyph_data24 = nullptr;
#endif
}
#ifndef HB_NO_BEYOND_64K
void set_glyph_data (const HBUINT24 glyph_data[])
{
match_glyph_data16 = nullptr;
match_glyph_data24 = glyph_data;
}
#endif
void reset (unsigned int start_index_,
unsigned int num_items_)
{
idx = start_index_;
num_items = num_items_;
end = c->buffer->len;
matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0);
}
void reject ()
{
num_items++;
backup_glyph_data ();
}
matcher_t::may_skip_t
may_skip (const hb_glyph_info_t &info) const
{ return matcher.may_skip (c, info); }
bool next (unsigned *unsafe_to = nullptr)
{
assert (num_items > 0);
while (idx + num_items < end)
{
idx++;
hb_glyph_info_t &info = c->buffer->info[idx];
matcher_t::may_skip_t skip = matcher.may_skip (c, info);
if (unlikely (skip == matcher_t::SKIP_YES))
continue;
matcher_t::may_match_t match = matcher.may_match (info, get_glyph_data ());
if (match == matcher_t::MATCH_YES ||
(match == matcher_t::MATCH_MAYBE &&
skip == matcher_t::SKIP_NO))
{
num_items--;
advance_glyph_data ();
return true;
}
if (skip == matcher_t::SKIP_NO)
{
if (unsafe_to)
*unsafe_to = idx + 1;
return false;
}
}
if (unsafe_to)
*unsafe_to = end;
return false;
}
bool prev (unsigned *unsafe_from = nullptr)
{
assert (num_items > 0);
while (idx > num_items - 1)
{
idx--;
hb_glyph_info_t &info = c->buffer->out_info[idx];
matcher_t::may_skip_t skip = matcher.may_skip (c, info);
if (unlikely (skip == matcher_t::SKIP_YES))
continue;
matcher_t::may_match_t match = matcher.may_match (info, get_glyph_data ());
if (match == matcher_t::MATCH_YES ||
(match == matcher_t::MATCH_MAYBE &&
skip == matcher_t::SKIP_NO))
{
num_items--;
advance_glyph_data ();
return true;
}
if (skip == matcher_t::SKIP_NO)
{
if (unsafe_from)
*unsafe_from = hb_max (1u, idx) - 1u;
return false;
}
}
if (unsafe_from)
*unsafe_from = 0;
return false;
}
hb_codepoint_t
get_glyph_data ()
{
if (match_glyph_data16) return *match_glyph_data16;
#ifndef HB_NO_BEYOND_64K
else
if (match_glyph_data24) return *match_glyph_data24;
#endif
return 0;
}
void
advance_glyph_data ()
{
if (match_glyph_data16) match_glyph_data16++;
#ifndef HB_NO_BEYOND_64K
else
if (match_glyph_data24) match_glyph_data24++;
#endif
}
void
backup_glyph_data ()
{
if (match_glyph_data16) match_glyph_data16--;
#ifndef HB_NO_BEYOND_64K
else
if (match_glyph_data24) match_glyph_data24--;
#endif
}
unsigned int idx;
protected:
hb_ot_apply_context_t *c;
matcher_t matcher;
const HBUINT16 *match_glyph_data16;
#ifndef HB_NO_BEYOND_64K
const HBUINT24 *match_glyph_data24;
#endif
unsigned int num_items;
unsigned int end;
};
const char *get_name () { return "APPLY"; }
typedef return_t (*recurse_func_t) (hb_ot_apply_context_t *c, unsigned int lookup_index);
template <typename T>
return_t dispatch (const T &obj) { return obj.apply (this); }
static return_t default_return_value () { return false; }
bool stop_sublookup_iteration (return_t r) const { return r; }
return_t recurse (unsigned int sub_lookup_index)
{
if (unlikely (nesting_level_left == 0 || !recurse_func || buffer->max_ops-- <= 0))
{
buffer->shaping_failed = true;
return default_return_value ();
}
nesting_level_left--;
bool ret = recurse_func (this, sub_lookup_index);
nesting_level_left++;
return ret;
}
skipping_iterator_t iter_input, iter_context;
unsigned int table_index; /* GSUB/GPOS */
hb_font_t *font;
hb_face_t *face;
hb_buffer_t *buffer;
recurse_func_t recurse_func = nullptr;
const GDEF &gdef;
const VariationStore &var_store;
VariationStore::cache_t *var_store_cache;
hb_direction_t direction;
hb_mask_t lookup_mask = 1;
unsigned int lookup_index = (unsigned) -1;
unsigned int lookup_props = 0;
unsigned int nesting_level_left = HB_MAX_NESTING_LEVEL;
bool has_glyph_classes;
bool auto_zwnj = true;
bool auto_zwj = true;
bool per_syllable = false;
bool random = false;
uint32_t random_state = 1;
unsigned new_syllables = (unsigned) -1;
hb_ot_apply_context_t (unsigned int table_index_,
hb_font_t *font_,
hb_buffer_t *buffer_) :
table_index (table_index_),
font (font_), face (font->face), buffer (buffer_),
gdef (
#ifndef HB_NO_OT_LAYOUT
*face->table.GDEF->table
#else
Null (GDEF)
#endif
),
var_store (gdef.get_var_store ()),
var_store_cache (
#ifndef HB_NO_VAR
table_index == 1 && font->num_coords ? var_store.create_cache () : nullptr
#else
nullptr
#endif
),
direction (buffer_->props.direction),
has_glyph_classes (gdef.has_glyph_classes ())
{ init_iters (); }
~hb_ot_apply_context_t ()
{
#ifndef HB_NO_VAR
VariationStore::destroy_cache (var_store_cache);
#endif
}
void init_iters ()
{
iter_input.init (this, false);
iter_context.init (this, true);
}
void set_lookup_mask (hb_mask_t mask) { lookup_mask = mask; init_iters (); }
void set_auto_zwj (bool auto_zwj_) { auto_zwj = auto_zwj_; init_iters (); }
void set_auto_zwnj (bool auto_zwnj_) { auto_zwnj = auto_zwnj_; init_iters (); }
void set_per_syllable (bool per_syllable_) { per_syllable = per_syllable_; init_iters (); }
void set_random (bool random_) { random = random_; }
void set_recurse_func (recurse_func_t func) { recurse_func = func; }
void set_lookup_index (unsigned int lookup_index_) { lookup_index = lookup_index_; }
void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; init_iters (); }
uint32_t random_number ()
{
/* http://www.cplusplus.com/reference/random/minstd_rand/ */
random_state = random_state * 48271 % 2147483647;
return random_state;
}
bool match_properties_mark (hb_codepoint_t glyph,
unsigned int glyph_props,
unsigned int match_props) const
{
/* If using mark filtering sets, the high short of
* match_props has the set index.
*/
if (match_props & LookupFlag::UseMarkFilteringSet)
return gdef.mark_set_covers (match_props >> 16, glyph);
/* The second byte of match_props has the meaning
* "ignore marks of attachment type different than
* the attachment type specified."
*/
if (match_props & LookupFlag::MarkAttachmentType)
return (match_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
return true;
}
bool check_glyph_property (const hb_glyph_info_t *info,
unsigned int match_props) const
{
hb_codepoint_t glyph = info->codepoint;
unsigned int glyph_props = _hb_glyph_info_get_glyph_props (info);
/* Not covered, if, for example, glyph class is ligature and
* match_props includes LookupFlags::IgnoreLigatures
*/
if (glyph_props & match_props & LookupFlag::IgnoreFlags)
return false;
if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_PROPS_MARK))
return match_properties_mark (glyph, glyph_props, match_props);
return true;
}
void _set_glyph_class (hb_codepoint_t glyph_index,
unsigned int class_guess = 0,
bool ligature = false,
bool component = false) const
{
if (new_syllables != (unsigned) -1)
buffer->cur().syllable() = new_syllables;
unsigned int props = _hb_glyph_info_get_glyph_props (&buffer->cur());
props |= HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED;
if (ligature)
{
props |= HB_OT_LAYOUT_GLYPH_PROPS_LIGATED;
/* In the only place that the MULTIPLIED bit is used, Uniscribe
* seems to only care about the "last" transformation between
* Ligature and Multiple substitutions. Ie. if you ligate, expand,
* and ligate again, it forgives the multiplication and acts as
* if only ligation happened. As such, clear MULTIPLIED bit.
*/
props &= ~HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
}
if (component)
props |= HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
if (likely (has_glyph_classes))
{
props &= HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE;
_hb_glyph_info_set_glyph_props (&buffer->cur(), props | gdef.get_glyph_props (glyph_index));
}
else if (class_guess)
{
props &= HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE;
_hb_glyph_info_set_glyph_props (&buffer->cur(), props | class_guess);
}
else
_hb_glyph_info_set_glyph_props (&buffer->cur(), props);
}
void replace_glyph (hb_codepoint_t glyph_index) const
{
_set_glyph_class (glyph_index);
(void) buffer->replace_glyph (glyph_index);
}
void replace_glyph_inplace (hb_codepoint_t glyph_index) const
{
_set_glyph_class (glyph_index);
buffer->cur().codepoint = glyph_index;
}
void replace_glyph_with_ligature (hb_codepoint_t glyph_index,
unsigned int class_guess) const
{
_set_glyph_class (glyph_index, class_guess, true);
(void) buffer->replace_glyph (glyph_index);
}
void output_glyph_for_component (hb_codepoint_t glyph_index,
unsigned int class_guess) const
{
_set_glyph_class (glyph_index, class_guess, false, true);
(void) buffer->output_glyph (glyph_index);
}
};
struct hb_accelerate_subtables_context_t :
hb_dispatch_context_t<hb_accelerate_subtables_context_t>
{
template <typename Type>
static inline bool apply_to (const void *obj, OT::hb_ot_apply_context_t *c)
{
const Type *typed_obj = (const Type *) obj;
return typed_obj->apply (c);
}
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
template <typename T>
static inline auto apply_cached_ (const T *obj, OT::hb_ot_apply_context_t *c, hb_priority<1>) HB_RETURN (bool, obj->apply (c, true) )
template <typename T>
static inline auto apply_cached_ (const T *obj, OT::hb_ot_apply_context_t *c, hb_priority<0>) HB_RETURN (bool, obj->apply (c) )
template <typename Type>
static inline bool apply_cached_to (const void *obj, OT::hb_ot_apply_context_t *c)
{
const Type *typed_obj = (const Type *) obj;
return apply_cached_ (typed_obj, c, hb_prioritize);
}
template <typename T>
static inline auto cache_func_ (const T *obj, OT::hb_ot_apply_context_t *c, bool enter, hb_priority<1>) HB_RETURN (bool, obj->cache_func (c, enter) )
template <typename T>
static inline bool cache_func_ (const T *obj, OT::hb_ot_apply_context_t *c, bool enter, hb_priority<0>) { return false; }
template <typename Type>
static inline bool cache_func_to (const void *obj, OT::hb_ot_apply_context_t *c, bool enter)
{
const Type *typed_obj = (const Type *) obj;
return cache_func_ (typed_obj, c, enter, hb_prioritize);
}
#endif
typedef bool (*hb_apply_func_t) (const void *obj, OT::hb_ot_apply_context_t *c);
typedef bool (*hb_cache_func_t) (const void *obj, OT::hb_ot_apply_context_t *c, bool enter);
struct hb_applicable_t
{
friend struct hb_accelerate_subtables_context_t;
friend struct hb_ot_layout_lookup_accelerator_t;
template <typename T>
void init (const T &obj_,
hb_apply_func_t apply_func_
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
, hb_apply_func_t apply_cached_func_
, hb_cache_func_t cache_func_
#endif
)
{
obj = &obj_;
apply_func = apply_func_;
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
apply_cached_func = apply_cached_func_;
cache_func = cache_func_;
#endif
digest.init ();
obj_.get_coverage ().collect_coverage (&digest);
}
bool apply (OT::hb_ot_apply_context_t *c) const
{
return digest.may_have (c->buffer->cur().codepoint) && apply_func (obj, c);
}
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
bool apply_cached (OT::hb_ot_apply_context_t *c) const
{
return digest.may_have (c->buffer->cur().codepoint) && apply_cached_func (obj, c);
}
bool cache_enter (OT::hb_ot_apply_context_t *c) const
{
return cache_func (obj, c, true);
}
void cache_leave (OT::hb_ot_apply_context_t *c) const
{
cache_func (obj, c, false);
}
#endif
private:
const void *obj;
hb_apply_func_t apply_func;
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
hb_apply_func_t apply_cached_func;
hb_cache_func_t cache_func;
#endif
hb_set_digest_t digest;
};
typedef hb_vector_t<hb_applicable_t> array_t;
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
template <typename T>
auto cache_cost (const T &obj, hb_priority<1>) HB_AUTO_RETURN ( obj.cache_cost () )
template <typename T>
auto cache_cost (const T &obj, hb_priority<0>) HB_AUTO_RETURN ( 0u )
#endif
/* Dispatch interface. */
template <typename T>
return_t dispatch (const T &obj)
{
hb_applicable_t entry;
entry.init (obj,
apply_to<T>
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
, apply_cached_to<T>
, cache_func_to<T>
#endif
);
array.push (entry);
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
/* Cache handling
*
* We allow one subtable from each lookup to use a cache. The assumption
* being that multiple subtables of the same lookup cannot use a cache
* because the resources they would use will collide. As such, we ask
* each subtable to tell us how much it costs (which a cache would avoid),
* and we allocate the cache opportunity to the costliest subtable.
*/
unsigned cost = cache_cost (obj, hb_prioritize);
if (cost > cache_user_cost && !array.in_error ())
{
cache_user_idx = array.length - 1;
cache_user_cost = cost;
}
#endif
return hb_empty_t ();
}
static return_t default_return_value () { return hb_empty_t (); }
hb_accelerate_subtables_context_t (array_t &array_) :
array (array_) {}
array_t &array;
#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE
unsigned cache_user_idx = (unsigned) -1;
unsigned cache_user_cost = 0;
#endif
};
typedef bool (*intersects_func_t) (const hb_set_t *glyphs, unsigned value, const void *data);
typedef void (*intersected_glyphs_func_t) (const hb_set_t *glyphs, const void *data, unsigned value, hb_set_t *intersected_glyphs);
typedef void (*collect_glyphs_func_t) (hb_set_t *glyphs, unsigned value, const void *data);
typedef bool (*match_func_t) (hb_glyph_info_t &info, unsigned value, const void *data);
struct ContextClosureFuncs
{
intersects_func_t intersects;
intersected_glyphs_func_t intersected_glyphs;
};