Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2007,2008,2009,2010 Red Hat, Inc. |
Behdad Esfahbod | 5b93e8d | 2012-04-23 22:26:13 -0400 | [diff] [blame] | 3 | * Copyright © 2010,2012 Google, Inc. |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 4 | * |
Behdad Esfahbod | c755cb3 | 2010-04-22 00:11:43 -0400 | [diff] [blame] | 5 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 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 |
Behdad Esfahbod | 98370e8 | 2010-10-27 17:39:01 -0400 | [diff] [blame] | 26 | * Google Author(s): Behdad Esfahbod |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 27 | */ |
| 28 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 29 | #ifndef HB_OT_LAYOUT_GSUBGPOS_HH |
| 30 | #define HB_OT_LAYOUT_GSUBGPOS_HH |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 31 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 32 | #include "hb.hh" |
| 33 | #include "hb-buffer.hh" |
| 34 | #include "hb-map.hh" |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 35 | #include "hb-set.hh" |
Behdad Esfahbod | 71c9f84 | 2018-09-10 22:37:19 +0200 | [diff] [blame] | 36 | #include "hb-ot-map.hh" |
Behdad Esfahbod | b929100 | 2018-08-26 01:15:47 -0700 | [diff] [blame] | 37 | #include "hb-ot-layout-common.hh" |
| 38 | #include "hb-ot-layout-gdef-table.hh" |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 39 | |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 40 | |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 41 | namespace OT { |
| 42 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 43 | |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 44 | struct hb_intersects_context_t : |
Behdad Esfahbod | 8d0a90a | 2020-06-18 16:53:54 -0700 | [diff] [blame] | 45 | hb_dispatch_context_t<hb_intersects_context_t, bool> |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 46 | { |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 47 | template <typename T> |
Behdad Esfahbod | c14efb8 | 2019-05-05 09:54:58 -0700 | [diff] [blame] | 48 | return_t dispatch (const T &obj) { return obj.intersects (this->glyphs); } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 49 | static return_t default_return_value () { return false; } |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 50 | bool stop_sublookup_iteration (return_t r) const { return r; } |
| 51 | |
| 52 | const hb_set_t *glyphs; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 53 | |
| 54 | hb_intersects_context_t (const hb_set_t *glyphs_) : |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 55 | glyphs (glyphs_) {} |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 58 | struct hb_have_non_1to1_context_t : |
| 59 | hb_dispatch_context_t<hb_have_non_1to1_context_t, bool> |
| 60 | { |
| 61 | template <typename T> |
| 62 | return_t dispatch (const T &obj) { return obj.may_have_non_1to1 (); } |
| 63 | static return_t default_return_value () { return false; } |
| 64 | bool stop_sublookup_iteration (return_t r) const { return r; } |
| 65 | }; |
| 66 | |
Behdad Esfahbod | 77a1a2b | 2015-10-09 12:20:58 -0400 | [diff] [blame] | 67 | struct hb_closure_context_t : |
Behdad Esfahbod | 25aec02 | 2020-06-18 16:58:01 -0700 | [diff] [blame] | 68 | hb_dispatch_context_t<hb_closure_context_t> |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 69 | { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 70 | 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); |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 71 | template <typename T> |
Behdad Esfahbod | 7df3ecf | 2019-05-10 20:43:26 -0700 | [diff] [blame] | 72 | return_t dispatch (const T &obj) { obj.closure (this); return hb_empty_t (); } |
| 73 | static return_t default_return_value () { return hb_empty_t (); } |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 74 | void recurse (unsigned lookup_index, hb_set_t *covered_seq_indicies, unsigned seq_index, unsigned end_index) |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 75 | { |
Behdad Esfahbod | 9b34677 | 2012-11-23 17:55:40 -0500 | [diff] [blame] | 76 | if (unlikely (nesting_level_left == 0 || !recurse_func)) |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 77 | return; |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 78 | |
| 79 | nesting_level_left--; |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 80 | recurse_func (this, lookup_index, covered_seq_indicies, seq_index, end_index); |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 81 | nesting_level_left++; |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Garret Rieger | d670257 | 2021-12-10 13:52:05 -0800 | [diff] [blame] | 84 | void reset_lookup_visit_count () |
| 85 | { lookup_count = 0; } |
| 86 | |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 87 | bool lookup_limit_exceeded () |
Qunxin Liu | 706014f | 2021-12-01 20:20:12 -0800 | [diff] [blame] | 88 | { return lookup_count > HB_MAX_LOOKUP_VISIT_COUNT; } |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 89 | |
Behdad Esfahbod | ba0ea56 | 2018-06-11 23:24:41 -0400 | [diff] [blame] | 90 | bool should_visit_lookup (unsigned int lookup_index) |
Garret Rieger | 45186b9 | 2018-06-05 17:14:42 -0700 | [diff] [blame] | 91 | { |
Qunxin Liu | 706014f | 2021-12-01 20:20:12 -0800 | [diff] [blame] | 92 | if (lookup_count++ > HB_MAX_LOOKUP_VISIT_COUNT) |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 93 | return false; |
| 94 | |
Garret Rieger | 45186b9 | 2018-06-05 17:14:42 -0700 | [diff] [blame] | 95 | if (is_lookup_done (lookup_index)) |
| 96 | return false; |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 97 | |
Garret Rieger | 45186b9 | 2018-06-05 17:14:42 -0700 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool is_lookup_done (unsigned int lookup_index) |
| 102 | { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 103 | if (done_lookups_glyph_count->in_error () || |
| 104 | done_lookups_glyph_set->in_error ()) |
Garret Rieger | 1e4fe10 | 2020-08-12 13:06:37 -0700 | [diff] [blame] | 105 | return true; |
| 106 | |
Behdad Esfahbod | c38bd40 | 2018-07-24 09:43:27 -0700 | [diff] [blame] | 107 | /* Have we visited this lookup with the current set of glyphs? */ |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 108 | if (done_lookups_glyph_count->get (lookup_index) != glyphs->get_population ()) |
| 109 | { |
| 110 | done_lookups_glyph_count->set (lookup_index, glyphs->get_population ()); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 111 | |
Behdad Esfahbod | bca710e | 2022-06-02 12:06:25 -0600 | [diff] [blame] | 112 | if (!done_lookups_glyph_set->has (lookup_index)) |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 113 | { |
Behdad Esfahbod | 997d9cc | 2022-06-02 18:04:12 -0600 | [diff] [blame] | 114 | if (unlikely (!done_lookups_glyph_set->set (lookup_index, hb::unique_ptr<hb_set_t> {hb_set_create ()}))) |
Behdad Esfahbod | 9ed5f04 | 2021-03-29 18:14:30 -0600 | [diff] [blame] | 115 | return true; |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 116 | } |
Garret Rieger | 52df6b9 | 2021-03-29 16:17:27 -0700 | [diff] [blame] | 117 | |
Garret Rieger | 9f77a0c | 2021-03-30 14:10:45 -0700 | [diff] [blame] | 118 | hb_set_clear (done_lookups_glyph_set->get (lookup_index)); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | hb_set_t *covered_glyph_set = done_lookups_glyph_set->get (lookup_index); |
Garret Rieger | 8b686af | 2021-04-06 11:33:38 -0700 | [diff] [blame] | 122 | if (unlikely (covered_glyph_set->in_error ())) |
Garret Rieger | 64122b5 | 2021-04-05 12:53:08 -0700 | [diff] [blame] | 123 | return true; |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 124 | if (parent_active_glyphs ().is_subset (*covered_glyph_set)) |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 125 | return true; |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 126 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 127 | covered_glyph_set->union_ (parent_active_glyphs ()); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 128 | return false; |
Garret Rieger | 45186b9 | 2018-06-05 17:14:42 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 131 | const hb_set_t& previous_parent_active_glyphs () { |
| 132 | if (active_glyphs_stack.length <= 1) |
| 133 | return *glyphs; |
| 134 | |
| 135 | return active_glyphs_stack[active_glyphs_stack.length - 2]; |
| 136 | } |
| 137 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 138 | const hb_set_t& parent_active_glyphs () |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 139 | { |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 140 | if (!active_glyphs_stack) |
| 141 | return *glyphs; |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 142 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 143 | return active_glyphs_stack.tail (); |
| 144 | } |
| 145 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 146 | hb_set_t& push_cur_active_glyphs () |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 147 | { |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 148 | return *active_glyphs_stack.push (); |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | bool pop_cur_done_glyphs () |
| 152 | { |
Behdad Esfahbod | 6826b2c | 2022-07-20 13:25:34 -0600 | [diff] [blame] | 153 | if (!active_glyphs_stack) |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 154 | return false; |
| 155 | |
| 156 | active_glyphs_stack.pop (); |
| 157 | return true; |
| 158 | } |
| 159 | |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 160 | hb_face_t *face; |
Behdad Esfahbod | 6a9be5b | 2012-04-23 22:23:17 -0400 | [diff] [blame] | 161 | hb_set_t *glyphs; |
Behdad Esfahbod | ede1a71 | 2019-01-09 10:45:53 -0800 | [diff] [blame] | 162 | hb_set_t output[1]; |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 163 | hb_vector_t<hb_set_t> active_glyphs_stack; |
Behdad Esfahbod | f4a8b70 | 2022-05-30 05:30:37 -0600 | [diff] [blame] | 164 | recurse_func_t recurse_func = nullptr; |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 165 | unsigned int nesting_level_left; |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 166 | |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 167 | hb_closure_context_t (hb_face_t *face_, |
Behdad Esfahbod | 6a9be5b | 2012-04-23 22:23:17 -0400 | [diff] [blame] | 168 | hb_set_t *glyphs_, |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 169 | hb_map_t *done_lookups_glyph_count_, |
Behdad Esfahbod | 997d9cc | 2022-06-02 18:04:12 -0600 | [diff] [blame] | 170 | hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> *done_lookups_glyph_set_, |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 171 | unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) : |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 172 | face (face_), |
| 173 | glyphs (glyphs_), |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 174 | nesting_level_left (nesting_level_left_), |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 175 | done_lookups_glyph_count (done_lookups_glyph_count_), |
Behdad Esfahbod | f4a8b70 | 2022-05-30 05:30:37 -0600 | [diff] [blame] | 176 | done_lookups_glyph_set (done_lookups_glyph_set_) |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 177 | {} |
Behdad Esfahbod | 9b34677 | 2012-11-23 17:55:40 -0500 | [diff] [blame] | 178 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 179 | ~hb_closure_context_t () { flush (); } |
Behdad Esfahbod | c38bd40 | 2018-07-24 09:43:27 -0700 | [diff] [blame] | 180 | |
Behdad Esfahbod | 9b34677 | 2012-11-23 17:55:40 -0500 | [diff] [blame] | 181 | void set_recurse_func (recurse_func_t func) { recurse_func = func; } |
Garret Rieger | 45186b9 | 2018-06-05 17:14:42 -0700 | [diff] [blame] | 182 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 183 | void flush () |
Behdad Esfahbod | c38bd40 | 2018-07-24 09:43:27 -0700 | [diff] [blame] | 184 | { |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 185 | output->del_range (face->get_num_glyphs (), HB_SET_VALUE_INVALID); /* Remove invalid glyphs. */ |
| 186 | glyphs->union_ (*output); |
| 187 | output->clear (); |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 188 | active_glyphs_stack.pop (); |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 189 | active_glyphs_stack.reset (); |
Behdad Esfahbod | c38bd40 | 2018-07-24 09:43:27 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Garret Rieger | 45186b9 | 2018-06-05 17:14:42 -0700 | [diff] [blame] | 192 | private: |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 193 | hb_map_t *done_lookups_glyph_count; |
Behdad Esfahbod | 997d9cc | 2022-06-02 18:04:12 -0600 | [diff] [blame] | 194 | hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> *done_lookups_glyph_set; |
Behdad Esfahbod | f4a8b70 | 2022-05-30 05:30:37 -0600 | [diff] [blame] | 195 | unsigned int lookup_count = 0; |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 196 | }; |
| 197 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 198 | |
| 199 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 200 | struct hb_closure_lookups_context_t : |
Behdad Esfahbod | 25aec02 | 2020-06-18 16:58:01 -0700 | [diff] [blame] | 201 | hb_dispatch_context_t<hb_closure_lookups_context_t> |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 202 | { |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 203 | typedef return_t (*recurse_func_t) (hb_closure_lookups_context_t *c, unsigned lookup_index); |
| 204 | template <typename T> |
| 205 | return_t dispatch (const T &obj) { obj.closure_lookups (this); return hb_empty_t (); } |
| 206 | static return_t default_return_value () { return hb_empty_t (); } |
| 207 | void recurse (unsigned lookup_index) |
| 208 | { |
| 209 | if (unlikely (nesting_level_left == 0 || !recurse_func)) |
| 210 | return; |
| 211 | |
| 212 | /* Return if new lookup was recursed to before. */ |
Garret Rieger | 77507a1 | 2021-12-10 14:10:04 -0800 | [diff] [blame] | 213 | if (lookup_limit_exceeded () |
| 214 | || visited_lookups->in_error () |
| 215 | || visited_lookups->has (lookup_index)) |
| 216 | // Don't increment lookup count here, that will be done in the call to closure_lookups() |
| 217 | // made by recurse_func. |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 218 | return; |
| 219 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 220 | nesting_level_left--; |
| 221 | recurse_func (this, lookup_index); |
| 222 | nesting_level_left++; |
| 223 | } |
| 224 | |
| 225 | void set_lookup_visited (unsigned lookup_index) |
| 226 | { visited_lookups->add (lookup_index); } |
| 227 | |
| 228 | void set_lookup_inactive (unsigned lookup_index) |
| 229 | { inactive_lookups->add (lookup_index); } |
| 230 | |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 231 | bool lookup_limit_exceeded () |
Qunxin Liu | 706014f | 2021-12-01 20:20:12 -0800 | [diff] [blame] | 232 | { |
| 233 | bool ret = lookup_count > HB_MAX_LOOKUP_VISIT_COUNT; |
| 234 | if (ret) |
| 235 | DEBUG_MSG (SUBSET, nullptr, "lookup visit count limit exceeded in lookup closure!"); |
| 236 | return ret; } |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 237 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 238 | bool is_lookup_visited (unsigned lookup_index) |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 239 | { |
Qunxin Liu | 706014f | 2021-12-01 20:20:12 -0800 | [diff] [blame] | 240 | if (unlikely (lookup_count++ > HB_MAX_LOOKUP_VISIT_COUNT)) |
| 241 | { |
| 242 | DEBUG_MSG (SUBSET, nullptr, "total visited lookup count %u exceeds max limit, lookup %u is dropped.", |
| 243 | lookup_count, lookup_index); |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 244 | return true; |
Qunxin Liu | 706014f | 2021-12-01 20:20:12 -0800 | [diff] [blame] | 245 | } |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 246 | |
Behdad Esfahbod | d7e2a51 | 2021-02-11 10:55:03 -0700 | [diff] [blame] | 247 | if (unlikely (visited_lookups->in_error ())) |
Garret Rieger | 9562239 | 2020-08-12 13:01:22 -0700 | [diff] [blame] | 248 | return true; |
| 249 | |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 250 | return visited_lookups->has (lookup_index); |
| 251 | } |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 252 | |
| 253 | hb_face_t *face; |
| 254 | const hb_set_t *glyphs; |
| 255 | recurse_func_t recurse_func; |
| 256 | unsigned int nesting_level_left; |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 257 | |
| 258 | hb_closure_lookups_context_t (hb_face_t *face_, |
| 259 | const hb_set_t *glyphs_, |
| 260 | hb_set_t *visited_lookups_, |
| 261 | hb_set_t *inactive_lookups_, |
| 262 | unsigned nesting_level_left_ = HB_MAX_NESTING_LEVEL) : |
| 263 | face (face_), |
| 264 | glyphs (glyphs_), |
| 265 | recurse_func (nullptr), |
| 266 | nesting_level_left (nesting_level_left_), |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 267 | visited_lookups (visited_lookups_), |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 268 | inactive_lookups (inactive_lookups_), |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 269 | lookup_count (0) {} |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 270 | |
| 271 | void set_recurse_func (recurse_func_t func) { recurse_func = func; } |
| 272 | |
| 273 | private: |
| 274 | hb_set_t *visited_lookups; |
| 275 | hb_set_t *inactive_lookups; |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 276 | unsigned int lookup_count; |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 277 | }; |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 278 | |
Behdad Esfahbod | 77a1a2b | 2015-10-09 12:20:58 -0400 | [diff] [blame] | 279 | struct hb_would_apply_context_t : |
Behdad Esfahbod | 8d0a90a | 2020-06-18 16:53:54 -0700 | [diff] [blame] | 280 | hb_dispatch_context_t<hb_would_apply_context_t, bool> |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 281 | { |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 282 | template <typename T> |
Behdad Esfahbod | c14efb8 | 2019-05-05 09:54:58 -0700 | [diff] [blame] | 283 | return_t dispatch (const T &obj) { return obj.would_apply (this); } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 284 | static return_t default_return_value () { return false; } |
Behdad Esfahbod | 7b912c1 | 2013-01-04 01:25:27 -0600 | [diff] [blame] | 285 | bool stop_sublookup_iteration (return_t r) const { return r; } |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 286 | |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 287 | hb_face_t *face; |
Behdad Esfahbod | 472f229 | 2012-08-07 22:25:24 -0400 | [diff] [blame] | 288 | const hb_codepoint_t *glyphs; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 289 | unsigned int len; |
Behdad Esfahbod | d9b204d | 2012-08-23 16:22:28 -0400 | [diff] [blame] | 290 | bool zero_context; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 291 | |
| 292 | hb_would_apply_context_t (hb_face_t *face_, |
Behdad Esfahbod | 472f229 | 2012-08-07 22:25:24 -0400 | [diff] [blame] | 293 | const hb_codepoint_t *glyphs_, |
| 294 | unsigned int len_, |
Behdad Esfahbod | 2bd9fe3 | 2012-09-04 15:15:19 -0400 | [diff] [blame] | 295 | bool zero_context_) : |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 296 | face (face_), |
Behdad Esfahbod | 472f229 | 2012-08-07 22:25:24 -0400 | [diff] [blame] | 297 | glyphs (glyphs_), |
| 298 | len (len_), |
Behdad Esfahbod | 70d6696 | 2020-06-18 17:09:39 -0700 | [diff] [blame] | 299 | zero_context (zero_context_) {} |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 300 | }; |
| 301 | |
Behdad Esfahbod | 77a1a2b | 2015-10-09 12:20:58 -0400 | [diff] [blame] | 302 | struct hb_collect_glyphs_context_t : |
Behdad Esfahbod | 25aec02 | 2020-06-18 16:58:01 -0700 | [diff] [blame] | 303 | hb_dispatch_context_t<hb_collect_glyphs_context_t> |
Behdad Esfahbod | e8cfdd7 | 2012-11-16 19:07:06 -0800 | [diff] [blame] | 304 | { |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 305 | typedef return_t (*recurse_func_t) (hb_collect_glyphs_context_t *c, unsigned int lookup_index); |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 306 | template <typename T> |
Behdad Esfahbod | 7df3ecf | 2019-05-10 20:43:26 -0700 | [diff] [blame] | 307 | return_t dispatch (const T &obj) { obj.collect_glyphs (this); return hb_empty_t (); } |
| 308 | static return_t default_return_value () { return hb_empty_t (); } |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 309 | void recurse (unsigned int lookup_index) |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 310 | { |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 311 | if (unlikely (nesting_level_left == 0 || !recurse_func)) |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 312 | return; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 313 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 314 | /* Note that GPOS sets recurse_func to nullptr already, so it doesn't get |
Behdad Esfahbod | 1bcfa06 | 2012-12-04 16:58:09 -0500 | [diff] [blame] | 315 | * past the previous check. For GSUB, we only want to collect the output |
Behdad Esfahbod | 76ea563 | 2013-05-04 16:01:20 -0400 | [diff] [blame] | 316 | * glyphs in the recursion. If output is not requested, we can go home now. |
| 317 | * |
| 318 | * Note further, that the above is not exactly correct. A recursed lookup |
| 319 | * is allowed to match input that is not matched in the context, but that's |
| 320 | * not how most fonts are built. It's possible to relax that and recurse |
| 321 | * with all sets here if it proves to be an issue. |
| 322 | */ |
Behdad Esfahbod | 4a350d0 | 2012-12-04 17:13:09 -0500 | [diff] [blame] | 323 | |
| 324 | if (output == hb_set_get_empty ()) |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 325 | return; |
Behdad Esfahbod | 4a350d0 | 2012-12-04 17:13:09 -0500 | [diff] [blame] | 326 | |
Behdad Esfahbod | fde3e4a | 2014-10-29 11:23:08 -0700 | [diff] [blame] | 327 | /* Return if new lookup was recursed to before. */ |
Behdad Esfahbod | 8b9d9b7 | 2017-10-22 17:48:06 -0400 | [diff] [blame] | 328 | if (recursed_lookups->has (lookup_index)) |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 329 | return; |
Behdad Esfahbod | fde3e4a | 2014-10-29 11:23:08 -0700 | [diff] [blame] | 330 | |
Behdad Esfahbod | 4a350d0 | 2012-12-04 17:13:09 -0500 | [diff] [blame] | 331 | hb_set_t *old_before = before; |
| 332 | hb_set_t *old_input = input; |
| 333 | hb_set_t *old_after = after; |
| 334 | before = input = after = hb_set_get_empty (); |
Behdad Esfahbod | 1bcfa06 | 2012-12-04 16:58:09 -0500 | [diff] [blame] | 335 | |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 336 | nesting_level_left--; |
Behdad Esfahbod | 4a350d0 | 2012-12-04 17:13:09 -0500 | [diff] [blame] | 337 | recurse_func (this, lookup_index); |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 338 | nesting_level_left++; |
Behdad Esfahbod | 4a350d0 | 2012-12-04 17:13:09 -0500 | [diff] [blame] | 339 | |
| 340 | before = old_before; |
| 341 | input = old_input; |
| 342 | after = old_after; |
| 343 | |
Behdad Esfahbod | 8b9d9b7 | 2017-10-22 17:48:06 -0400 | [diff] [blame] | 344 | recursed_lookups->add (lookup_index); |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 345 | } |
| 346 | |
Behdad Esfahbod | e8cfdd7 | 2012-11-16 19:07:06 -0800 | [diff] [blame] | 347 | hb_face_t *face; |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 348 | hb_set_t *before; |
| 349 | hb_set_t *input; |
| 350 | hb_set_t *after; |
| 351 | hb_set_t *output; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 352 | recurse_func_t recurse_func; |
Behdad Esfahbod | 8b9d9b7 | 2017-10-22 17:48:06 -0400 | [diff] [blame] | 353 | hb_set_t *recursed_lookups; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 354 | unsigned int nesting_level_left; |
Behdad Esfahbod | e8cfdd7 | 2012-11-16 19:07:06 -0800 | [diff] [blame] | 355 | |
| 356 | hb_collect_glyphs_context_t (hb_face_t *face_, |
Ebrahim Byagowi | 6310943 | 2018-10-13 14:00:05 +0330 | [diff] [blame] | 357 | hb_set_t *glyphs_before, /* OUT. May be NULL */ |
| 358 | hb_set_t *glyphs_input, /* OUT. May be NULL */ |
| 359 | hb_set_t *glyphs_after, /* OUT. May be NULL */ |
| 360 | hb_set_t *glyphs_output, /* OUT. May be NULL */ |
Behdad Esfahbod | 5ba4504 | 2015-11-02 15:43:08 -0800 | [diff] [blame] | 361 | unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) : |
Behdad Esfahbod | e8cfdd7 | 2012-11-16 19:07:06 -0800 | [diff] [blame] | 362 | face (face_), |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 363 | before (glyphs_before ? glyphs_before : hb_set_get_empty ()), |
| 364 | input (glyphs_input ? glyphs_input : hb_set_get_empty ()), |
| 365 | after (glyphs_after ? glyphs_after : hb_set_get_empty ()), |
| 366 | output (glyphs_output ? glyphs_output : hb_set_get_empty ()), |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 367 | recurse_func (nullptr), |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 368 | recursed_lookups (hb_set_create ()), |
Behdad Esfahbod | 70d6696 | 2020-06-18 17:09:39 -0700 | [diff] [blame] | 369 | nesting_level_left (nesting_level_left_) {} |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 370 | ~hb_collect_glyphs_context_t () { hb_set_destroy (recursed_lookups); } |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 371 | |
| 372 | void set_recurse_func (recurse_func_t func) { recurse_func = func; } |
Behdad Esfahbod | e8cfdd7 | 2012-11-16 19:07:06 -0800 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | |
| 376 | |
Behdad Esfahbod | 8e36ccf | 2015-02-17 19:15:34 +0300 | [diff] [blame] | 377 | template <typename set_t> |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 378 | struct hb_collect_coverage_context_t : |
Behdad Esfahbod | 8d0a90a | 2020-06-18 16:53:54 -0700 | [diff] [blame] | 379 | hb_dispatch_context_t<hb_collect_coverage_context_t<set_t>, const Coverage &> |
Behdad Esfahbod | 2005fa5 | 2012-11-22 14:38:10 -0500 | [diff] [blame] | 380 | { |
Behdad Esfahbod | dc492d7 | 2020-06-18 17:03:05 -0700 | [diff] [blame] | 381 | typedef const Coverage &return_t; // Stoopid that we have to dupe this here. |
Behdad Esfahbod | 2005fa5 | 2012-11-22 14:38:10 -0500 | [diff] [blame] | 382 | template <typename T> |
Behdad Esfahbod | c14efb8 | 2019-05-05 09:54:58 -0700 | [diff] [blame] | 383 | return_t dispatch (const T &obj) { return obj.get_coverage (); } |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 384 | static return_t default_return_value () { return Null (Coverage); } |
Behdad Esfahbod | 8e36ccf | 2015-02-17 19:15:34 +0300 | [diff] [blame] | 385 | bool stop_sublookup_iteration (return_t r) const |
| 386 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 387 | r.collect_coverage (set); |
Behdad Esfahbod | 8e36ccf | 2015-02-17 19:15:34 +0300 | [diff] [blame] | 388 | return false; |
| 389 | } |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 390 | |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 391 | hb_collect_coverage_context_t (set_t *set_) : |
Behdad Esfahbod | 70d6696 | 2020-06-18 17:09:39 -0700 | [diff] [blame] | 392 | set (set_) {} |
Behdad Esfahbod | a1733db | 2012-11-23 16:40:04 -0500 | [diff] [blame] | 393 | |
Behdad Esfahbod | 8e36ccf | 2015-02-17 19:15:34 +0300 | [diff] [blame] | 394 | set_t *set; |
Behdad Esfahbod | 2005fa5 | 2012-11-22 14:38:10 -0500 | [diff] [blame] | 395 | }; |
| 396 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 397 | struct hb_ot_apply_context_t : |
| 398 | hb_dispatch_context_t<hb_ot_apply_context_t, bool, HB_DEBUG_APPLY> |
Behdad Esfahbod | 1376fb7 | 2010-04-29 02:19:21 -0400 | [diff] [blame] | 399 | { |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 400 | struct matcher_t |
| 401 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 402 | matcher_t () : |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 403 | lookup_props (0), |
Behdad Esfahbod | 044d7a0 | 2022-03-28 12:38:56 -0600 | [diff] [blame] | 404 | mask (-1), |
Behdad Esfahbod | cfc507c | 2013-02-14 10:40:12 -0500 | [diff] [blame] | 405 | ignore_zwnj (false), |
| 406 | ignore_zwj (false), |
Behdad Esfahbod | 044d7a0 | 2022-03-28 12:38:56 -0600 | [diff] [blame] | 407 | per_syllable (false), |
Behdad Esfahbod | e8f3397 | 2022-03-28 12:07:05 -0600 | [diff] [blame] | 408 | syllable {0}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 409 | match_func (nullptr), |
Ebrahim Byagowi | f7a08cd | 2018-10-30 11:29:09 +0330 | [diff] [blame] | 410 | match_data (nullptr) {} |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 411 | |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 412 | typedef bool (*match_func_t) (hb_glyph_info_t &info, unsigned value, const void *data); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 413 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 414 | void set_ignore_zwnj (bool ignore_zwnj_) { ignore_zwnj = ignore_zwnj_; } |
| 415 | void set_ignore_zwj (bool ignore_zwj_) { ignore_zwj = ignore_zwj_; } |
| 416 | void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; } |
| 417 | void set_mask (hb_mask_t mask_) { mask = mask_; } |
Behdad Esfahbod | 044d7a0 | 2022-03-28 12:38:56 -0600 | [diff] [blame] | 418 | void set_per_syllable (bool per_syllable_) { per_syllable = per_syllable_; } |
| 419 | void set_syllable (uint8_t syllable_) { syllable = per_syllable ? syllable_ : 0; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 420 | void set_match_func (match_func_t match_func_, |
Ebrahim Byagowi | d0e2add | 2020-07-18 22:14:52 +0430 | [diff] [blame] | 421 | const void *match_data_) |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 422 | { match_func = match_func_; match_data = match_data_; } |
| 423 | |
Behdad Esfahbod | 2b2a6e8 | 2013-02-21 15:07:03 -0500 | [diff] [blame] | 424 | enum may_match_t { |
| 425 | MATCH_NO, |
| 426 | MATCH_YES, |
| 427 | MATCH_MAYBE |
| 428 | }; |
| 429 | |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 430 | may_match_t may_match (hb_glyph_info_t &info, |
Behdad Esfahbod | 1ef67a6 | 2022-07-06 17:28:02 -0600 | [diff] [blame] | 431 | hb_codepoint_t glyph_data) const |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 432 | { |
Behdad Esfahbod | 2b2a6e8 | 2013-02-21 15:07:03 -0500 | [diff] [blame] | 433 | if (!(info.mask & mask) || |
| 434 | (syllable && syllable != info.syllable ())) |
| 435 | return MATCH_NO; |
| 436 | |
| 437 | if (match_func) |
Behdad Esfahbod | 1ef67a6 | 2022-07-06 17:28:02 -0600 | [diff] [blame] | 438 | return match_func (info, glyph_data, match_data) ? MATCH_YES : MATCH_NO; |
Behdad Esfahbod | 2b2a6e8 | 2013-02-21 15:07:03 -0500 | [diff] [blame] | 439 | |
| 440 | return MATCH_MAYBE; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | enum may_skip_t { |
| 444 | SKIP_NO, |
| 445 | SKIP_YES, |
| 446 | SKIP_MAYBE |
| 447 | }; |
| 448 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 449 | may_skip_t may_skip (const hb_ot_apply_context_t *c, |
| 450 | const hb_glyph_info_t &info) const |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 451 | { |
Behdad Esfahbod | b98c5db | 2014-07-16 13:44:01 -0400 | [diff] [blame] | 452 | if (!c->check_glyph_property (&info, lookup_props)) |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 453 | return SKIP_YES; |
| 454 | |
Khaled Hosny | 06cfe3f | 2017-05-17 21:32:47 +0300 | [diff] [blame] | 455 | if (unlikely (_hb_glyph_info_is_default_ignorable_and_not_hidden (&info) && |
Behdad Esfahbod | 0b45479 | 2013-02-14 10:46:52 -0500 | [diff] [blame] | 456 | (ignore_zwnj || !_hb_glyph_info_is_zwnj (&info)) && |
Behdad Esfahbod | 4ba796b | 2015-07-22 17:41:31 +0100 | [diff] [blame] | 457 | (ignore_zwj || !_hb_glyph_info_is_zwj (&info)))) |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 458 | return SKIP_MAYBE; |
| 459 | |
| 460 | return SKIP_NO; |
| 461 | } |
| 462 | |
| 463 | protected: |
| 464 | unsigned int lookup_props; |
Behdad Esfahbod | 044d7a0 | 2022-03-28 12:38:56 -0600 | [diff] [blame] | 465 | hb_mask_t mask; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 466 | bool ignore_zwnj; |
Behdad Esfahbod | 0b45479 | 2013-02-14 10:46:52 -0500 | [diff] [blame] | 467 | bool ignore_zwj; |
Behdad Esfahbod | 044d7a0 | 2022-03-28 12:38:56 -0600 | [diff] [blame] | 468 | bool per_syllable; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 469 | uint8_t syllable; |
| 470 | match_func_t match_func; |
| 471 | const void *match_data; |
| 472 | }; |
| 473 | |
Behdad Esfahbod | 6962669 | 2015-01-29 13:08:41 +0100 | [diff] [blame] | 474 | struct skipping_iterator_t |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 475 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 476 | void init (hb_ot_apply_context_t *c_, bool context_match = false) |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 477 | { |
Behdad Esfahbod | 514564f | 2015-01-29 13:48:48 +0100 | [diff] [blame] | 478 | c = c_; |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 479 | match_glyph_data16 = nullptr; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 480 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 481 | match_glyph_data24 = nullptr; |
| 482 | #endif |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 483 | matcher.set_match_func (nullptr, nullptr); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 484 | matcher.set_lookup_props (c->lookup_props); |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 485 | /* Ignore ZWNJ if we are matching GPOS, or matching GSUB context and asked to. */ |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 486 | matcher.set_ignore_zwnj (c->table_index == 1 || (context_match && c->auto_zwnj)); |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 487 | /* Ignore ZWJ if we are matching context, or asked to. */ |
| 488 | matcher.set_ignore_zwj (context_match || c->auto_zwj); |
Behdad Esfahbod | 514564f | 2015-01-29 13:48:48 +0100 | [diff] [blame] | 489 | matcher.set_mask (context_match ? -1 : c->lookup_mask); |
Behdad Esfahbod | 044d7a0 | 2022-03-28 12:38:56 -0600 | [diff] [blame] | 490 | matcher.set_per_syllable (c->per_syllable); |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 491 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 492 | void set_lookup_props (unsigned int lookup_props) |
Behdad Esfahbod | 514564f | 2015-01-29 13:48:48 +0100 | [diff] [blame] | 493 | { |
| 494 | matcher.set_lookup_props (lookup_props); |
| 495 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 496 | void set_match_func (matcher_t::match_func_t match_func_, |
Behdad Esfahbod | f114b18 | 2022-07-06 17:31:46 -0600 | [diff] [blame] | 497 | const void *match_data_) |
Behdad Esfahbod | c074ebc | 2013-02-13 11:22:42 -0500 | [diff] [blame] | 498 | { |
Behdad Esfahbod | 4a6b1ee | 2015-10-21 11:20:55 -0200 | [diff] [blame] | 499 | matcher.set_match_func (match_func_, match_data_); |
Behdad Esfahbod | f114b18 | 2022-07-06 17:31:46 -0600 | [diff] [blame] | 500 | } |
| 501 | void set_glyph_data (const HBUINT16 glyph_data[]) |
| 502 | { |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 503 | match_glyph_data16 = glyph_data; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 504 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 505 | match_glyph_data24 = nullptr; |
| 506 | #endif |
Behdad Esfahbod | c074ebc | 2013-02-13 11:22:42 -0500 | [diff] [blame] | 507 | } |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 508 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 509 | void set_glyph_data (const HBUINT24 glyph_data[]) |
| 510 | { |
| 511 | match_glyph_data16 = nullptr; |
| 512 | match_glyph_data24 = glyph_data; |
| 513 | } |
| 514 | #endif |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 515 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 516 | void reset (unsigned int start_index_, |
Ebrahim Byagowi | 08428a1 | 2020-04-24 23:45:17 +0430 | [diff] [blame] | 517 | unsigned int num_items_) |
Behdad Esfahbod | b051be5 | 2015-01-29 13:40:39 +0100 | [diff] [blame] | 518 | { |
| 519 | idx = start_index_; |
| 520 | num_items = num_items_; |
| 521 | end = c->buffer->len; |
| 522 | matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0); |
| 523 | } |
| 524 | |
Ebrahim Byagowi | 64a45be | 2019-11-09 12:25:33 +0330 | [diff] [blame] | 525 | void reject () |
| 526 | { |
| 527 | num_items++; |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 528 | backup_glyph_data (); |
Ebrahim Byagowi | 64a45be | 2019-11-09 12:25:33 +0330 | [diff] [blame] | 529 | } |
Behdad Esfahbod | 6962669 | 2015-01-29 13:08:41 +0100 | [diff] [blame] | 530 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 531 | matcher_t::may_skip_t |
| 532 | may_skip (const hb_glyph_info_t &info) const |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 533 | { return matcher.may_skip (c, info); } |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 534 | |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 535 | bool next (unsigned *unsafe_to = nullptr) |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 536 | { |
Behdad Esfahbod | 506ffeb | 2012-01-18 16:07:53 -0500 | [diff] [blame] | 537 | assert (num_items > 0); |
Behdad Esfahbod | 37d13ac | 2015-01-29 11:38:01 +0100 | [diff] [blame] | 538 | while (idx + num_items < end) |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 539 | { |
Behdad Esfahbod | a4a48fe | 2012-01-17 18:08:41 -0500 | [diff] [blame] | 540 | idx++; |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 541 | hb_glyph_info_t &info = c->buffer->info[idx]; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 542 | |
Behdad Esfahbod | ff93ac8 | 2013-02-21 14:51:40 -0500 | [diff] [blame] | 543 | matcher_t::may_skip_t skip = matcher.may_skip (c, info); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 544 | if (unlikely (skip == matcher_t::SKIP_YES)) |
| 545 | continue; |
| 546 | |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 547 | matcher_t::may_match_t match = matcher.may_match (info, get_glyph_data ()); |
Behdad Esfahbod | 722e8b8 | 2013-02-21 15:37:51 -0500 | [diff] [blame] | 548 | if (match == matcher_t::MATCH_YES || |
| 549 | (match == matcher_t::MATCH_MAYBE && |
| 550 | skip == matcher_t::SKIP_NO)) |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 551 | { |
| 552 | num_items--; |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 553 | advance_glyph_data (); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 554 | return true; |
| 555 | } |
| 556 | |
| 557 | if (skip == matcher_t::SKIP_NO) |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 558 | { |
| 559 | if (unsafe_to) |
| 560 | *unsafe_to = idx + 1; |
Behdad Esfahbod | 722e8b8 | 2013-02-21 15:37:51 -0500 | [diff] [blame] | 561 | return false; |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 562 | } |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 563 | } |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 564 | if (unsafe_to) |
| 565 | *unsafe_to = end; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 566 | return false; |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 567 | } |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 568 | bool prev (unsigned *unsafe_from = nullptr) |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 569 | { |
Behdad Esfahbod | 506ffeb | 2012-01-18 16:07:53 -0500 | [diff] [blame] | 570 | assert (num_items > 0); |
Behdad Esfahbod | 18a06f8 | 2018-07-05 14:03:48 +0430 | [diff] [blame] | 571 | while (idx > num_items - 1) |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 572 | { |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 573 | idx--; |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 574 | hb_glyph_info_t &info = c->buffer->out_info[idx]; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 575 | |
Behdad Esfahbod | ff93ac8 | 2013-02-21 14:51:40 -0500 | [diff] [blame] | 576 | matcher_t::may_skip_t skip = matcher.may_skip (c, info); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 577 | if (unlikely (skip == matcher_t::SKIP_YES)) |
| 578 | continue; |
| 579 | |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 580 | matcher_t::may_match_t match = matcher.may_match (info, get_glyph_data ()); |
Behdad Esfahbod | 722e8b8 | 2013-02-21 15:37:51 -0500 | [diff] [blame] | 581 | if (match == matcher_t::MATCH_YES || |
| 582 | (match == matcher_t::MATCH_MAYBE && |
| 583 | skip == matcher_t::SKIP_NO)) |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 584 | { |
| 585 | num_items--; |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 586 | advance_glyph_data (); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 587 | return true; |
| 588 | } |
| 589 | |
| 590 | if (skip == matcher_t::SKIP_NO) |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 591 | { |
| 592 | if (unsafe_from) |
| 593 | *unsafe_from = hb_max (1u, idx) - 1u; |
Behdad Esfahbod | 722e8b8 | 2013-02-21 15:37:51 -0500 | [diff] [blame] | 594 | return false; |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 595 | } |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 596 | } |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 597 | if (unsafe_from) |
| 598 | *unsafe_from = 0; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 599 | return false; |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 600 | } |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 601 | |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 602 | hb_codepoint_t |
| 603 | get_glyph_data () |
| 604 | { |
| 605 | if (match_glyph_data16) return *match_glyph_data16; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 606 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 607 | else |
| 608 | if (match_glyph_data24) return *match_glyph_data24; |
| 609 | #endif |
| 610 | return 0; |
| 611 | } |
| 612 | void |
| 613 | advance_glyph_data () |
| 614 | { |
| 615 | if (match_glyph_data16) match_glyph_data16++; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 616 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 617 | else |
| 618 | if (match_glyph_data24) match_glyph_data24++; |
| 619 | #endif |
| 620 | } |
| 621 | void |
| 622 | backup_glyph_data () |
| 623 | { |
| 624 | if (match_glyph_data16) match_glyph_data16--; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 625 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 626 | else |
| 627 | if (match_glyph_data24) match_glyph_data24--; |
| 628 | #endif |
| 629 | } |
| 630 | |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 631 | unsigned int idx; |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 632 | protected: |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 633 | hb_ot_apply_context_t *c; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 634 | matcher_t matcher; |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 635 | const HBUINT16 *match_glyph_data16; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 636 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 429b387 | 2022-07-06 17:37:11 -0600 | [diff] [blame] | 637 | const HBUINT24 *match_glyph_data24; |
| 638 | #endif |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 639 | |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 640 | unsigned int num_items; |
Behdad Esfahbod | 6962669 | 2015-01-29 13:08:41 +0100 | [diff] [blame] | 641 | unsigned int end; |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 642 | }; |
| 643 | |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 644 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 645 | const char *get_name () { return "APPLY"; } |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 646 | typedef return_t (*recurse_func_t) (hb_ot_apply_context_t *c, unsigned int lookup_index); |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 647 | template <typename T> |
Behdad Esfahbod | c14efb8 | 2019-05-05 09:54:58 -0700 | [diff] [blame] | 648 | return_t dispatch (const T &obj) { return obj.apply (this); } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 649 | static return_t default_return_value () { return false; } |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 650 | bool stop_sublookup_iteration (return_t r) const { return r; } |
Behdad Esfahbod | 12757b6 | 2018-01-26 18:14:05 -0800 | [diff] [blame] | 651 | return_t recurse (unsigned int sub_lookup_index) |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 652 | { |
Behdad Esfahbod | baf7779 | 2017-11-14 21:53:48 -0800 | [diff] [blame] | 653 | if (unlikely (nesting_level_left == 0 || !recurse_func || buffer->max_ops-- <= 0)) |
Behdad Esfahbod | 9a2a857 | 2022-05-31 04:25:20 -0600 | [diff] [blame] | 654 | { |
| 655 | buffer->shaping_failed = true; |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 656 | return default_return_value (); |
Behdad Esfahbod | 9a2a857 | 2022-05-31 04:25:20 -0600 | [diff] [blame] | 657 | } |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 658 | |
| 659 | nesting_level_left--; |
Behdad Esfahbod | 12757b6 | 2018-01-26 18:14:05 -0800 | [diff] [blame] | 660 | bool ret = recurse_func (this, sub_lookup_index); |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 661 | nesting_level_left++; |
| 662 | return ret; |
| 663 | } |
| 664 | |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 665 | skipping_iterator_t iter_input, iter_context; |
| 666 | |
Behdad Esfahbod | 880f50f | 2022-05-20 16:50:00 -0600 | [diff] [blame] | 667 | unsigned int table_index; /* GSUB/GPOS */ |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 668 | hb_font_t *font; |
| 669 | hb_face_t *face; |
| 670 | hb_buffer_t *buffer; |
Behdad Esfahbod | f4a8b70 | 2022-05-30 05:30:37 -0600 | [diff] [blame] | 671 | recurse_func_t recurse_func = nullptr; |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 672 | const GDEF &gdef; |
| 673 | const VariationStore &var_store; |
Behdad Esfahbod | 880f50f | 2022-05-20 16:50:00 -0600 | [diff] [blame] | 674 | VariationStore::cache_t *var_store_cache; |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 675 | |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 676 | hb_direction_t direction; |
Behdad Esfahbod | 97aa1ce | 2022-05-29 10:32:59 -0600 | [diff] [blame] | 677 | hb_mask_t lookup_mask = 1; |
| 678 | unsigned int lookup_index = (unsigned) -1; |
| 679 | unsigned int lookup_props = 0; |
| 680 | unsigned int nesting_level_left = HB_MAX_NESTING_LEVEL; |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 681 | |
Behdad Esfahbod | 80de4bc | 2018-09-10 16:24:52 +0200 | [diff] [blame] | 682 | bool has_glyph_classes; |
Behdad Esfahbod | 97aa1ce | 2022-05-29 10:32:59 -0600 | [diff] [blame] | 683 | bool auto_zwnj = true; |
| 684 | bool auto_zwj = true; |
| 685 | bool per_syllable = false; |
| 686 | bool random = false; |
| 687 | uint32_t random_state = 1; |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 688 | unsigned new_syllables = (unsigned) -1; |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 689 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 690 | hb_ot_apply_context_t (unsigned int table_index_, |
Ebrahim Byagowi | d0e2add | 2020-07-18 22:14:52 +0430 | [diff] [blame] | 691 | hb_font_t *font_, |
| 692 | hb_buffer_t *buffer_) : |
Behdad Esfahbod | 880f50f | 2022-05-20 16:50:00 -0600 | [diff] [blame] | 693 | table_index (table_index_), |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 694 | font (font_), face (font->face), buffer (buffer_), |
Behdad Esfahbod | 7dcf8e1 | 2019-06-26 13:44:10 -0700 | [diff] [blame] | 695 | gdef ( |
| 696 | #ifndef HB_NO_OT_LAYOUT |
| 697 | *face->table.GDEF->table |
| 698 | #else |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 699 | Null (GDEF) |
Behdad Esfahbod | 7dcf8e1 | 2019-06-26 13:44:10 -0700 | [diff] [blame] | 700 | #endif |
| 701 | ), |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 702 | var_store (gdef.get_var_store ()), |
Behdad Esfahbod | 3eb7eff | 2022-05-21 15:25:53 -0600 | [diff] [blame] | 703 | var_store_cache ( |
| 704 | #ifndef HB_NO_VAR |
| 705 | table_index == 1 && font->num_coords ? var_store.create_cache () : nullptr |
| 706 | #else |
| 707 | nullptr |
| 708 | #endif |
| 709 | ), |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 710 | direction (buffer_->props.direction), |
Behdad Esfahbod | 97aa1ce | 2022-05-29 10:32:59 -0600 | [diff] [blame] | 711 | has_glyph_classes (gdef.has_glyph_classes ()) |
Behdad Esfahbod | 880f50f | 2022-05-20 16:50:00 -0600 | [diff] [blame] | 712 | { init_iters (); } |
| 713 | |
Behdad Esfahbod | 3eb7eff | 2022-05-21 15:25:53 -0600 | [diff] [blame] | 714 | ~hb_ot_apply_context_t () |
| 715 | { |
| 716 | #ifndef HB_NO_VAR |
| 717 | VariationStore::destroy_cache (var_store_cache); |
| 718 | #endif |
| 719 | } |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 720 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 721 | void init_iters () |
Behdad Esfahbod | 365576d | 2015-01-29 13:59:42 +0100 | [diff] [blame] | 722 | { |
Behdad Esfahbod | 365576d | 2015-01-29 13:59:42 +0100 | [diff] [blame] | 723 | iter_input.init (this, false); |
| 724 | iter_context.init (this, true); |
| 725 | } |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 726 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 727 | void set_lookup_mask (hb_mask_t mask) { lookup_mask = mask; init_iters (); } |
| 728 | void set_auto_zwj (bool auto_zwj_) { auto_zwj = auto_zwj_; init_iters (); } |
| 729 | void set_auto_zwnj (bool auto_zwnj_) { auto_zwnj = auto_zwnj_; init_iters (); } |
Behdad Esfahbod | 044d7a0 | 2022-03-28 12:38:56 -0600 | [diff] [blame] | 730 | void set_per_syllable (bool per_syllable_) { per_syllable = per_syllable_; init_iters (); } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 731 | void set_random (bool random_) { random = random_; } |
| 732 | void set_recurse_func (recurse_func_t func) { recurse_func = func; } |
| 733 | void set_lookup_index (unsigned int lookup_index_) { lookup_index = lookup_index_; } |
| 734 | void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; init_iters (); } |
Behdad Esfahbod | 9516cbd | 2018-09-23 22:00:34 -0400 | [diff] [blame] | 735 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 736 | uint32_t random_number () |
Behdad Esfahbod | 08260c7 | 2018-09-11 10:51:19 +0200 | [diff] [blame] | 737 | { |
Behdad Esfahbod | cfdea88 | 2018-09-11 10:57:48 +0200 | [diff] [blame] | 738 | /* http://www.cplusplus.com/reference/random/minstd_rand/ */ |
| 739 | random_state = random_state * 48271 % 2147483647; |
| 740 | return random_state; |
Behdad Esfahbod | 08260c7 | 2018-09-11 10:51:19 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 743 | bool match_properties_mark (hb_codepoint_t glyph, |
| 744 | unsigned int glyph_props, |
| 745 | unsigned int match_props) const |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 746 | { |
| 747 | /* If using mark filtering sets, the high short of |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 748 | * match_props has the set index. |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 749 | */ |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 750 | if (match_props & LookupFlag::UseMarkFilteringSet) |
| 751 | return gdef.mark_set_covers (match_props >> 16, glyph); |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 752 | |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 753 | /* The second byte of match_props has the meaning |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 754 | * "ignore marks of attachment type different than |
| 755 | * the attachment type specified." |
| 756 | */ |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 757 | if (match_props & LookupFlag::MarkAttachmentType) |
| 758 | return (match_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType); |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 759 | |
| 760 | return true; |
| 761 | } |
| 762 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 763 | bool check_glyph_property (const hb_glyph_info_t *info, |
| 764 | unsigned int match_props) const |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 765 | { |
Behdad Esfahbod | b98c5db | 2014-07-16 13:44:01 -0400 | [diff] [blame] | 766 | hb_codepoint_t glyph = info->codepoint; |
| 767 | unsigned int glyph_props = _hb_glyph_info_get_glyph_props (info); |
| 768 | |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 769 | /* Not covered, if, for example, glyph class is ligature and |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 770 | * match_props includes LookupFlags::IgnoreLigatures |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 771 | */ |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 772 | if (glyph_props & match_props & LookupFlag::IgnoreFlags) |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 773 | return false; |
| 774 | |
Behdad Esfahbod | 5a08ecf | 2012-11-16 13:34:29 -0800 | [diff] [blame] | 775 | if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_PROPS_MARK)) |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 776 | return match_properties_mark (glyph, glyph_props, match_props); |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 777 | |
| 778 | return true; |
| 779 | } |
| 780 | |
Behdad Esfahbod | fa12f1a | 2022-01-12 15:08:34 -0700 | [diff] [blame] | 781 | void _set_glyph_class (hb_codepoint_t glyph_index, |
Ebrahim Byagowi | 9748ae7 | 2020-08-11 17:52:48 +0430 | [diff] [blame] | 782 | unsigned int class_guess = 0, |
| 783 | bool ligature = false, |
| 784 | bool component = false) const |
Behdad Esfahbod | 60da763 | 2012-07-16 16:13:32 -0400 | [diff] [blame] | 785 | { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 786 | if (new_syllables != (unsigned) -1) |
| 787 | buffer->cur().syllable() = new_syllables; |
| 788 | |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 789 | unsigned int props = _hb_glyph_info_get_glyph_props (&buffer->cur()); |
| 790 | props |= HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED; |
Behdad Esfahbod | 09675a8 | 2013-10-18 01:05:58 +0200 | [diff] [blame] | 791 | if (ligature) |
Behdad Esfahbod | 832a6f9 | 2014-06-04 16:57:42 -0400 | [diff] [blame] | 792 | { |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 793 | props |= HB_OT_LAYOUT_GLYPH_PROPS_LIGATED; |
Behdad Esfahbod | 832a6f9 | 2014-06-04 16:57:42 -0400 | [diff] [blame] | 794 | /* In the only place that the MULTIPLIED bit is used, Uniscribe |
| 795 | * seems to only care about the "last" transformation between |
Bruce Mitchener | 257d0e5 | 2018-10-19 22:49:21 +0700 | [diff] [blame] | 796 | * Ligature and Multiple substitutions. Ie. if you ligate, expand, |
Behdad Esfahbod | 832a6f9 | 2014-06-04 16:57:42 -0400 | [diff] [blame] | 797 | * and ligate again, it forgives the multiplication and acts as |
| 798 | * if only ligation happened. As such, clear MULTIPLIED bit. |
| 799 | */ |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 800 | props &= ~HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED; |
Behdad Esfahbod | 832a6f9 | 2014-06-04 16:57:42 -0400 | [diff] [blame] | 801 | } |
| 802 | if (component) |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 803 | props |= HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED; |
Behdad Esfahbod | 2fca142 | 2012-07-30 18:46:41 -0400 | [diff] [blame] | 804 | if (likely (has_glyph_classes)) |
Behdad Esfahbod | 96c969c | 2022-01-12 15:06:40 -0700 | [diff] [blame] | 805 | { |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 806 | props &= HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE; |
| 807 | _hb_glyph_info_set_glyph_props (&buffer->cur(), props | gdef.get_glyph_props (glyph_index)); |
Behdad Esfahbod | 96c969c | 2022-01-12 15:06:40 -0700 | [diff] [blame] | 808 | } |
Behdad Esfahbod | 05bd1b6 | 2012-07-30 19:30:01 -0400 | [diff] [blame] | 809 | else if (class_guess) |
Behdad Esfahbod | 96c969c | 2022-01-12 15:06:40 -0700 | [diff] [blame] | 810 | { |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 811 | props &= HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE; |
| 812 | _hb_glyph_info_set_glyph_props (&buffer->cur(), props | class_guess); |
Behdad Esfahbod | 96c969c | 2022-01-12 15:06:40 -0700 | [diff] [blame] | 813 | } |
| 814 | else |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 815 | _hb_glyph_info_set_glyph_props (&buffer->cur(), props); |
Behdad Esfahbod | 60da763 | 2012-07-16 16:13:32 -0400 | [diff] [blame] | 816 | } |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 817 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 818 | void replace_glyph (hb_codepoint_t glyph_index) const |
Behdad Esfahbod | 3ec77d6 | 2012-06-08 21:44:06 -0400 | [diff] [blame] | 819 | { |
Behdad Esfahbod | fa12f1a | 2022-01-12 15:08:34 -0700 | [diff] [blame] | 820 | _set_glyph_class (glyph_index); |
Behdad Esfahbod | 3f1998a | 2021-03-15 13:33:44 -0600 | [diff] [blame] | 821 | (void) buffer->replace_glyph (glyph_index); |
Behdad Esfahbod | 98370e8 | 2010-10-27 17:39:01 -0400 | [diff] [blame] | 822 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 823 | void replace_glyph_inplace (hb_codepoint_t glyph_index) const |
Behdad Esfahbod | 7fbbf86 | 2012-07-30 18:36:42 -0400 | [diff] [blame] | 824 | { |
Behdad Esfahbod | fa12f1a | 2022-01-12 15:08:34 -0700 | [diff] [blame] | 825 | _set_glyph_class (glyph_index); |
Behdad Esfahbod | 7fbbf86 | 2012-07-30 18:36:42 -0400 | [diff] [blame] | 826 | buffer->cur().codepoint = glyph_index; |
| 827 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 828 | void replace_glyph_with_ligature (hb_codepoint_t glyph_index, |
Behdad Esfahbod | f4cd99f | 2020-04-22 14:45:57 -0700 | [diff] [blame] | 829 | unsigned int class_guess) const |
Behdad Esfahbod | a016174 | 2013-10-18 00:06:30 +0200 | [diff] [blame] | 830 | { |
Behdad Esfahbod | fa12f1a | 2022-01-12 15:08:34 -0700 | [diff] [blame] | 831 | _set_glyph_class (glyph_index, class_guess, true); |
Behdad Esfahbod | 3f1998a | 2021-03-15 13:33:44 -0600 | [diff] [blame] | 832 | (void) buffer->replace_glyph (glyph_index); |
Behdad Esfahbod | a016174 | 2013-10-18 00:06:30 +0200 | [diff] [blame] | 833 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 834 | void output_glyph_for_component (hb_codepoint_t glyph_index, |
Behdad Esfahbod | f4cd99f | 2020-04-22 14:45:57 -0700 | [diff] [blame] | 835 | unsigned int class_guess) const |
Behdad Esfahbod | a016174 | 2013-10-18 00:06:30 +0200 | [diff] [blame] | 836 | { |
Behdad Esfahbod | fa12f1a | 2022-01-12 15:08:34 -0700 | [diff] [blame] | 837 | _set_glyph_class (glyph_index, class_guess, false, true); |
Behdad Esfahbod | 34a1204 | 2021-03-15 14:39:06 -0600 | [diff] [blame] | 838 | (void) buffer->output_glyph (glyph_index); |
Behdad Esfahbod | a016174 | 2013-10-18 00:06:30 +0200 | [diff] [blame] | 839 | } |
Behdad Esfahbod | 1376fb7 | 2010-04-29 02:19:21 -0400 | [diff] [blame] | 840 | }; |
| 841 | |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 842 | |
Behdad Esfahbod | f9b643f | 2022-06-04 07:29:40 -0600 | [diff] [blame] | 843 | struct hb_accelerate_subtables_context_t : |
| 844 | hb_dispatch_context_t<hb_accelerate_subtables_context_t> |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 845 | { |
| 846 | template <typename Type> |
Behdad Esfahbod | a061e47 | 2019-12-10 13:31:50 -0600 | [diff] [blame] | 847 | static inline bool apply_to (const void *obj, OT::hb_ot_apply_context_t *c) |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 848 | { |
| 849 | const Type *typed_obj = (const Type *) obj; |
| 850 | return typed_obj->apply (c); |
| 851 | } |
| 852 | |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 853 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 854 | template <typename T> |
Behdad Esfahbod | d4c09e9 | 2022-06-07 09:03:30 -0600 | [diff] [blame] | 855 | 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) ) |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 856 | template <typename T> |
| 857 | static inline auto apply_cached_ (const T *obj, OT::hb_ot_apply_context_t *c, hb_priority<0>) HB_RETURN (bool, obj->apply (c) ) |
| 858 | template <typename Type> |
| 859 | static inline bool apply_cached_to (const void *obj, OT::hb_ot_apply_context_t *c) |
| 860 | { |
| 861 | const Type *typed_obj = (const Type *) obj; |
| 862 | return apply_cached_ (typed_obj, c, hb_prioritize); |
| 863 | } |
| 864 | |
| 865 | template <typename T> |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 866 | 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) ) |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 867 | template <typename T> |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 868 | static inline bool cache_func_ (const T *obj, OT::hb_ot_apply_context_t *c, bool enter, hb_priority<0>) { return false; } |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 869 | template <typename Type> |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 870 | static inline bool cache_func_to (const void *obj, OT::hb_ot_apply_context_t *c, bool enter) |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 871 | { |
| 872 | const Type *typed_obj = (const Type *) obj; |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 873 | return cache_func_ (typed_obj, c, enter, hb_prioritize); |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 874 | } |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 875 | #endif |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 876 | |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 877 | typedef bool (*hb_apply_func_t) (const void *obj, OT::hb_ot_apply_context_t *c); |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 878 | typedef bool (*hb_cache_func_t) (const void *obj, OT::hb_ot_apply_context_t *c, bool enter); |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 879 | |
| 880 | struct hb_applicable_t |
| 881 | { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 882 | friend struct hb_accelerate_subtables_context_t; |
| 883 | friend struct hb_ot_layout_lookup_accelerator_t; |
| 884 | |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 885 | template <typename T> |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 886 | void init (const T &obj_, |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 887 | hb_apply_func_t apply_func_ |
| 888 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
| 889 | , hb_apply_func_t apply_cached_func_ |
| 890 | , hb_cache_func_t cache_func_ |
| 891 | #endif |
| 892 | ) |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 893 | { |
| 894 | obj = &obj_; |
| 895 | apply_func = apply_func_; |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 896 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 897 | apply_cached_func = apply_cached_func_; |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 898 | cache_func = cache_func_; |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 899 | #endif |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 900 | digest.init (); |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 901 | obj_.get_coverage ().collect_coverage (&digest); |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 902 | } |
| 903 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 904 | bool apply (OT::hb_ot_apply_context_t *c) const |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 905 | { |
| 906 | return digest.may_have (c->buffer->cur().codepoint) && apply_func (obj, c); |
| 907 | } |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 908 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 909 | bool apply_cached (OT::hb_ot_apply_context_t *c) const |
| 910 | { |
| 911 | return digest.may_have (c->buffer->cur().codepoint) && apply_cached_func (obj, c); |
| 912 | } |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 913 | bool cache_enter (OT::hb_ot_apply_context_t *c) const |
| 914 | { |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 915 | return cache_func (obj, c, true); |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 916 | } |
| 917 | void cache_leave (OT::hb_ot_apply_context_t *c) const |
| 918 | { |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 919 | cache_func (obj, c, false); |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 920 | } |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 921 | #endif |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 922 | |
| 923 | private: |
| 924 | const void *obj; |
| 925 | hb_apply_func_t apply_func; |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 926 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 927 | hb_apply_func_t apply_cached_func; |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 928 | hb_cache_func_t cache_func; |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 929 | #endif |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 930 | hb_set_digest_t digest; |
| 931 | }; |
| 932 | |
Behdad Esfahbod | fa333e3 | 2018-12-27 17:56:22 -0500 | [diff] [blame] | 933 | typedef hb_vector_t<hb_applicable_t> array_t; |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 934 | |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 935 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 936 | template <typename T> |
| 937 | auto cache_cost (const T &obj, hb_priority<1>) HB_AUTO_RETURN ( obj.cache_cost () ) |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 938 | template <typename T> |
| 939 | auto cache_cost (const T &obj, hb_priority<0>) HB_AUTO_RETURN ( 0u ) |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 940 | #endif |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 941 | |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 942 | /* Dispatch interface. */ |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 943 | template <typename T> |
Behdad Esfahbod | c14efb8 | 2019-05-05 09:54:58 -0700 | [diff] [blame] | 944 | return_t dispatch (const T &obj) |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 945 | { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 946 | hb_applicable_t entry; |
| 947 | |
| 948 | entry.init (obj, |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 949 | apply_to<T> |
| 950 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
| 951 | , apply_cached_to<T> |
| 952 | , cache_func_to<T> |
| 953 | #endif |
| 954 | ); |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 955 | |
| 956 | array.push (entry); |
| 957 | |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 958 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | c8fb048 | 2022-06-07 09:20:27 -0600 | [diff] [blame] | 959 | /* Cache handling |
| 960 | * |
| 961 | * We allow one subtable from each lookup to use a cache. The assumption |
| 962 | * being that multiple subtables of the same lookup cannot use a cache |
| 963 | * because the resources they would use will collide. As such, we ask |
| 964 | * each subtable to tell us how much it costs (which a cache would avoid), |
| 965 | * and we allocate the cache opportunity to the costliest subtable. |
| 966 | */ |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 967 | unsigned cost = cache_cost (obj, hb_prioritize); |
| 968 | if (cost > cache_user_cost && !array.in_error ()) |
| 969 | { |
| 970 | cache_user_idx = array.length - 1; |
| 971 | cache_user_cost = cost; |
| 972 | } |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 973 | #endif |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 974 | |
Behdad Esfahbod | 7df3ecf | 2019-05-10 20:43:26 -0700 | [diff] [blame] | 975 | return hb_empty_t (); |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 976 | } |
Behdad Esfahbod | 7df3ecf | 2019-05-10 20:43:26 -0700 | [diff] [blame] | 977 | static return_t default_return_value () { return hb_empty_t (); } |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 978 | |
Behdad Esfahbod | f9b643f | 2022-06-04 07:29:40 -0600 | [diff] [blame] | 979 | hb_accelerate_subtables_context_t (array_t &array_) : |
| 980 | array (array_) {} |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 981 | |
| 982 | array_t &array; |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 983 | |
| 984 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 985 | unsigned cache_user_idx = (unsigned) -1; |
| 986 | unsigned cache_user_cost = 0; |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 987 | #endif |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 988 | }; |
| 989 | |
| 990 | |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 991 | typedef bool (*intersects_func_t) (const hb_set_t *glyphs, unsigned value, const void *data); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 992 | typedef void (*intersected_glyphs_func_t) (const hb_set_t *glyphs, const void *data, unsigned value, hb_set_t *intersected_glyphs); |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 993 | typedef void (*collect_glyphs_func_t) (hb_set_t *glyphs, unsigned value, const void *data); |
| 994 | typedef bool (*match_func_t) (hb_glyph_info_t &info, unsigned value, const void *data); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 995 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 996 | struct ContextClosureFuncs |
| 997 | { |
| 998 | intersects_func_t intersects; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 999 | intersected_glyphs_func_t intersected_glyphs; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1000 | }; |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1001 | struct ContextCollectGlyphsFuncs |
| 1002 | { |
| 1003 | collect_glyphs_func_t collect; |
| 1004 | }; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1005 | struct ContextApplyFuncs |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1006 | { |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 1007 | match_func_t match; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1008 | }; |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 1009 | struct ChainContextApplyFuncs |
| 1010 | { |
| 1011 | match_func_t match[3]; |
| 1012 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1013 | |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1014 | |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1015 | static inline bool intersects_glyph (const hb_set_t *glyphs, unsigned value, const void *data HB_UNUSED) |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1016 | { |
| 1017 | return glyphs->has (value); |
| 1018 | } |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1019 | static inline bool intersects_class (const hb_set_t *glyphs, unsigned value, const void *data) |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1020 | { |
| 1021 | const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data); |
| 1022 | return class_def.intersects_class (glyphs, value); |
| 1023 | } |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1024 | static inline bool intersects_coverage (const hb_set_t *glyphs, unsigned value, const void *data) |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1025 | { |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1026 | Offset16To<Coverage> coverage; |
| 1027 | coverage = value; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1028 | return (data+coverage).intersects (glyphs); |
| 1029 | } |
| 1030 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1031 | |
| 1032 | static inline void intersected_glyph (const hb_set_t *glyphs HB_UNUSED, const void *data, unsigned value, hb_set_t *intersected_glyphs) |
| 1033 | { |
| 1034 | unsigned g = reinterpret_cast<const HBUINT16 *>(data)[value]; |
| 1035 | intersected_glyphs->add (g); |
| 1036 | } |
| 1037 | static inline void intersected_class_glyphs (const hb_set_t *glyphs, const void *data, unsigned value, hb_set_t *intersected_glyphs) |
| 1038 | { |
| 1039 | const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data); |
| 1040 | class_def.intersected_class_glyphs (glyphs, value, intersected_glyphs); |
| 1041 | } |
| 1042 | static inline void intersected_coverage_glyphs (const hb_set_t *glyphs, const void *data, unsigned value, hb_set_t *intersected_glyphs) |
| 1043 | { |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 1044 | Offset16To<Coverage> coverage; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1045 | coverage = value; |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 1046 | (data+coverage).intersect_set (*glyphs, *intersected_glyphs); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1050 | template <typename HBUINT> |
Qunxin Liu | 44d88cf | 2020-05-08 15:33:34 -0700 | [diff] [blame] | 1051 | static inline bool array_is_subset_of (const hb_set_t *glyphs, |
| 1052 | unsigned int count, |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1053 | const HBUINT values[], |
Qunxin Liu | 44d88cf | 2020-05-08 15:33:34 -0700 | [diff] [blame] | 1054 | intersects_func_t intersects_func, |
| 1055 | const void *intersects_data) |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1056 | { |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1057 | for (const auto &_ : + hb_iter (values, count)) |
Qunxin Liu | 44d88cf | 2020-05-08 15:33:34 -0700 | [diff] [blame] | 1058 | if (!intersects_func (glyphs, _, intersects_data)) return false; |
| 1059 | return true; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1060 | } |
| 1061 | |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1062 | |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1063 | static inline void collect_glyph (hb_set_t *glyphs, unsigned value, const void *data HB_UNUSED) |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1064 | { |
| 1065 | glyphs->add (value); |
| 1066 | } |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1067 | static inline void collect_class (hb_set_t *glyphs, unsigned value, const void *data) |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1068 | { |
| 1069 | const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data); |
Behdad Esfahbod | 89ad3c6 | 2020-04-23 10:57:30 -0700 | [diff] [blame] | 1070 | class_def.collect_class (glyphs, value); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1071 | } |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1072 | static inline void collect_coverage (hb_set_t *glyphs, unsigned value, const void *data) |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1073 | { |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1074 | Offset16To<Coverage> coverage; |
| 1075 | coverage = value; |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 1076 | (data+coverage).collect_coverage (glyphs); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1077 | } |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1078 | template <typename HBUINT> |
Behdad Esfahbod | 0beb66e | 2012-12-05 18:46:04 -0500 | [diff] [blame] | 1079 | static inline void collect_array (hb_collect_glyphs_context_t *c HB_UNUSED, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 1080 | hb_set_t *glyphs, |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1081 | unsigned int count, |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1082 | const HBUINT values[], |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1083 | collect_glyphs_func_t collect_func, |
| 1084 | const void *collect_data) |
| 1085 | { |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1086 | return |
| 1087 | + hb_iter (values, count) |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1088 | | hb_apply ([&] (const HBUINT &_) { collect_func (glyphs, _, collect_data); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1089 | ; |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1093 | static inline bool match_glyph (hb_glyph_info_t &info, unsigned value, const void *data HB_UNUSED) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1094 | { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 1095 | return info.codepoint == value; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 1096 | } |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1097 | static inline bool match_class (hb_glyph_info_t &info, unsigned value, const void *data) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1098 | { |
Behdad Esfahbod | 2b5a59c | 2009-08-04 11:38:50 -0400 | [diff] [blame] | 1099 | const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data); |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 1100 | return class_def.get_class (info.codepoint) == value; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 1101 | } |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1102 | static inline bool match_class_cached (hb_glyph_info_t &info, unsigned value, const void *data) |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 1103 | { |
| 1104 | unsigned klass = info.syllable(); |
| 1105 | if (klass < 255) |
| 1106 | return klass == value; |
| 1107 | const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data); |
| 1108 | klass = class_def.get_class (info.codepoint); |
| 1109 | if (likely (klass < 255)) |
| 1110 | info.syllable() = klass; |
| 1111 | return klass == value; |
| 1112 | } |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1113 | static inline bool match_coverage (hb_glyph_info_t &info, unsigned value, const void *data) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1114 | { |
Behdad Esfahbod | 8b349e1 | 2022-07-06 17:22:34 -0600 | [diff] [blame] | 1115 | Offset16To<Coverage> coverage; |
| 1116 | coverage = value; |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 1117 | return (data+coverage).get_coverage (info.codepoint) != NOT_COVERED; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 1118 | } |
| 1119 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1120 | template <typename HBUINT> |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1121 | static inline bool would_match_input (hb_would_apply_context_t *c, |
| 1122 | unsigned int count, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1123 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1124 | match_func_t match_func, |
| 1125 | const void *match_data) |
| 1126 | { |
| 1127 | if (count != c->len) |
| 1128 | return false; |
| 1129 | |
| 1130 | for (unsigned int i = 1; i < count; i++) |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 1131 | { |
| 1132 | hb_glyph_info_t info; |
| 1133 | info.codepoint = c->glyphs[i]; |
| 1134 | if (likely (!match_func (info, input[i - 1], match_data))) |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1135 | return false; |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 1136 | } |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1137 | |
| 1138 | return true; |
| 1139 | } |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1140 | template <typename HBUINT> |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1141 | static inline bool match_input (hb_ot_apply_context_t *c, |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 1142 | unsigned int count, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1143 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1144 | match_func_t match_func, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 1145 | const void *match_data, |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1146 | unsigned int *end_position, |
Behdad Esfahbod | 5ba4504 | 2015-11-02 15:43:08 -0800 | [diff] [blame] | 1147 | unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1148 | unsigned int *p_total_component_count = nullptr) |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1149 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1150 | TRACE_APPLY (nullptr); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1151 | |
Behdad Esfahbod | 5ba4504 | 2015-11-02 15:43:08 -0800 | [diff] [blame] | 1152 | if (unlikely (count > HB_MAX_CONTEXT_LENGTH)) return_trace (false); |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1153 | |
Behdad Esfahbod | 3c3df9c | 2013-10-17 13:58:31 +0200 | [diff] [blame] | 1154 | hb_buffer_t *buffer = c->buffer; |
| 1155 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1156 | hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input; |
Behdad Esfahbod | b051be5 | 2015-01-29 13:40:39 +0100 | [diff] [blame] | 1157 | skippy_iter.reset (buffer->idx, count - 1); |
Behdad Esfahbod | f114b18 | 2022-07-06 17:31:46 -0600 | [diff] [blame] | 1158 | skippy_iter.set_match_func (match_func, match_data); |
| 1159 | skippy_iter.set_glyph_data (input); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1160 | |
Behdad Esfahbod | 191fa88 | 2012-08-28 22:58:55 -0400 | [diff] [blame] | 1161 | /* |
| 1162 | * This is perhaps the trickiest part of OpenType... Remarks: |
| 1163 | * |
| 1164 | * - If all components of the ligature were marks, we call this a mark ligature. |
| 1165 | * |
| 1166 | * - If there is no GDEF, and the ligature is NOT a mark ligature, we categorize |
| 1167 | * it as a ligature glyph. |
| 1168 | * |
| 1169 | * - Ligatures cannot be formed across glyphs attached to different components |
| 1170 | * of previous ligatures. Eg. the sequence is LAM,SHADDA,LAM,FATHA,HEH, and |
| 1171 | * LAM,LAM,HEH form a ligature, leaving SHADDA,FATHA next to eachother. |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1172 | * However, it would be wrong to ligate that SHADDA,FATHA sequence. |
| 1173 | * There are a couple of exceptions to this: |
| 1174 | * |
| 1175 | * o If a ligature tries ligating with marks that belong to it itself, go ahead, |
| 1176 | * assuming that the font designer knows what they are doing (otherwise it can |
| 1177 | * break Indic stuff when a matra wants to ligate with a conjunct, |
| 1178 | * |
| 1179 | * o If two marks want to ligate and they belong to different components of the |
| 1180 | * same ligature glyph, and said ligature glyph is to be ignored according to |
| 1181 | * mark-filtering rules, then allow. |
ebraminio | 7c6937e | 2017-11-20 14:49:22 -0500 | [diff] [blame] | 1182 | * https://github.com/harfbuzz/harfbuzz/issues/545 |
Behdad Esfahbod | 191fa88 | 2012-08-28 22:58:55 -0400 | [diff] [blame] | 1183 | */ |
| 1184 | |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1185 | unsigned int total_component_count = 0; |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1186 | total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->cur()); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1187 | |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1188 | unsigned int first_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur()); |
| 1189 | unsigned int first_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->cur()); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1190 | |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1191 | enum { |
| 1192 | LIGBASE_NOT_CHECKED, |
| 1193 | LIGBASE_MAY_NOT_SKIP, |
| 1194 | LIGBASE_MAY_SKIP |
| 1195 | } ligbase = LIGBASE_NOT_CHECKED; |
| 1196 | |
Behdad Esfahbod | 3c3df9c | 2013-10-17 13:58:31 +0200 | [diff] [blame] | 1197 | match_positions[0] = buffer->idx; |
Behdad Esfahbod | 370f03e | 2012-01-16 17:03:55 -0500 | [diff] [blame] | 1198 | for (unsigned int i = 1; i < count; i++) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1199 | { |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1200 | unsigned unsafe_to; |
| 1201 | if (!skippy_iter.next (&unsafe_to)) |
| 1202 | { |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1203 | *end_position = unsafe_to; |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1204 | return_trace (false); |
| 1205 | } |
Behdad Esfahbod | 6cc136f | 2013-10-17 13:55:48 +0200 | [diff] [blame] | 1206 | |
| 1207 | match_positions[i] = skippy_iter.idx; |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1208 | |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1209 | unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]); |
| 1210 | unsigned int this_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1211 | |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1212 | if (first_lig_id && first_lig_comp) |
| 1213 | { |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1214 | /* If first component was attached to a previous ligature component, |
| 1215 | * all subsequent components should be attached to the same ligature |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1216 | * component, otherwise we shouldn't ligate them... */ |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1217 | if (first_lig_id != this_lig_id || first_lig_comp != this_lig_comp) |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1218 | { |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 1219 | /* ...unless, we are attached to a base ligature and that base |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1220 | * ligature is ignorable. */ |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 1221 | if (ligbase == LIGBASE_NOT_CHECKED) |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1222 | { |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1223 | bool found = false; |
Behdad Esfahbod | 4d67743 | 2019-05-10 16:35:31 -0700 | [diff] [blame] | 1224 | const auto *out = buffer->out_info; |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1225 | unsigned int j = buffer->out_len; |
| 1226 | while (j && _hb_glyph_info_get_lig_id (&out[j - 1]) == first_lig_id) |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1227 | { |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1228 | if (_hb_glyph_info_get_lig_comp (&out[j - 1]) == 0) |
| 1229 | { |
| 1230 | j--; |
| 1231 | found = true; |
| 1232 | break; |
| 1233 | } |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1234 | j--; |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1235 | } |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1236 | |
Behdad Esfahbod | 12757b6 | 2018-01-26 18:14:05 -0800 | [diff] [blame] | 1237 | if (found && skippy_iter.may_skip (out[j]) == hb_ot_apply_context_t::matcher_t::SKIP_YES) |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1238 | ligbase = LIGBASE_MAY_SKIP; |
| 1239 | else |
| 1240 | ligbase = LIGBASE_MAY_NOT_SKIP; |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1241 | } |
| 1242 | |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 1243 | if (ligbase == LIGBASE_MAY_NOT_SKIP) |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1244 | return_trace (false); |
| 1245 | } |
| 1246 | } |
| 1247 | else |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1248 | { |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1249 | /* If first component was NOT attached to a previous ligature component, |
| 1250 | * all subsequent components should also NOT be attached to any ligature |
| 1251 | * component, unless they are attached to the first component itself! */ |
| 1252 | if (this_lig_id && this_lig_comp && (this_lig_id != first_lig_id)) |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1253 | return_trace (false); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1254 | } |
| 1255 | |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1256 | total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->info[skippy_iter.idx]); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1257 | } |
| 1258 | |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1259 | *end_position = skippy_iter.idx + 1; |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1260 | |
Behdad Esfahbod | 191fa88 | 2012-08-28 22:58:55 -0400 | [diff] [blame] | 1261 | if (p_total_component_count) |
| 1262 | *p_total_component_count = total_component_count; |
| 1263 | |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1264 | return_trace (true); |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1265 | } |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1266 | static inline bool ligate_input (hb_ot_apply_context_t *c, |
Behdad Esfahbod | e714fe6 | 2013-10-17 13:49:51 +0200 | [diff] [blame] | 1267 | unsigned int count, /* Including the first glyph */ |
Bruce Mitchener | 5a24ea1 | 2018-10-20 08:09:52 +0700 | [diff] [blame] | 1268 | const unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */ |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1269 | unsigned int match_end, |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1270 | hb_codepoint_t lig_glyph, |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1271 | unsigned int total_component_count) |
| 1272 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1273 | TRACE_APPLY (nullptr); |
Behdad Esfahbod | e714fe6 | 2013-10-17 13:49:51 +0200 | [diff] [blame] | 1274 | |
Behdad Esfahbod | 3c3df9c | 2013-10-17 13:58:31 +0200 | [diff] [blame] | 1275 | hb_buffer_t *buffer = c->buffer; |
| 1276 | |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1277 | buffer->merge_clusters (buffer->idx, match_end); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 1278 | |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1279 | /* - If a base and one or more marks ligate, consider that as a base, NOT |
| 1280 | * ligature, such that all following marks can still attach to it. |
| 1281 | * https://github.com/harfbuzz/harfbuzz/issues/1109 |
| 1282 | * |
| 1283 | * - If all components of the ligature were marks, we call this a mark ligature. |
Behdad Esfahbod | 3cca978 | 2018-10-02 15:02:16 +0200 | [diff] [blame] | 1284 | * If it *is* a mark ligature, we don't allocate a new ligature id, and leave |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1285 | * the ligature to keep its old ligature id. This will allow it to attach to |
| 1286 | * a base ligature in GPOS. Eg. if the sequence is: LAM,LAM,SHADDA,FATHA,HEH, |
Behdad Esfahbod | 3cca978 | 2018-10-02 15:02:16 +0200 | [diff] [blame] | 1287 | * and LAM,LAM,HEH for a ligature, they will leave SHADDA and FATHA with a |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1288 | * ligature id and component value of 2. Then if SHADDA,FATHA form a ligature |
| 1289 | * later, we don't want them to lose their ligature id/component, otherwise |
| 1290 | * GPOS will fail to correctly position the mark ligature on top of the |
| 1291 | * LAM,LAM,HEH ligature. See: |
| 1292 | * https://bugzilla.gnome.org/show_bug.cgi?id=676343 |
| 1293 | * |
| 1294 | * - If a ligature is formed of components that some of which are also ligatures |
| 1295 | * themselves, and those ligature components had marks attached to *their* |
| 1296 | * components, we have to attach the marks to the new ligature component |
| 1297 | * positions! Now *that*'s tricky! And these marks may be following the |
| 1298 | * last component of the whole sequence, so we should loop forward looking |
| 1299 | * for them and update them. |
| 1300 | * |
| 1301 | * Eg. the sequence is LAM,LAM,SHADDA,FATHA,HEH, and the font first forms a |
| 1302 | * 'calt' ligature of LAM,HEH, leaving the SHADDA and FATHA with a ligature |
| 1303 | * id and component == 1. Now, during 'liga', the LAM and the LAM-HEH ligature |
| 1304 | * form a LAM-LAM-HEH ligature. We need to reassign the SHADDA and FATHA to |
| 1305 | * the new ligature with a component value of 2. |
| 1306 | * |
| 1307 | * This in fact happened to a font... See: |
| 1308 | * https://bugzilla.gnome.org/show_bug.cgi?id=437633 |
| 1309 | */ |
| 1310 | |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1311 | bool is_base_ligature = _hb_glyph_info_is_base_glyph (&buffer->info[match_positions[0]]); |
| 1312 | bool is_mark_ligature = _hb_glyph_info_is_mark (&buffer->info[match_positions[0]]); |
| 1313 | for (unsigned int i = 1; i < count; i++) |
Behdad Esfahbod | 3cca978 | 2018-10-02 15:02:16 +0200 | [diff] [blame] | 1314 | if (!_hb_glyph_info_is_mark (&buffer->info[match_positions[i]])) |
| 1315 | { |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1316 | is_base_ligature = false; |
Behdad Esfahbod | 3cca978 | 2018-10-02 15:02:16 +0200 | [diff] [blame] | 1317 | is_mark_ligature = false; |
| 1318 | break; |
| 1319 | } |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1320 | bool is_ligature = !is_base_ligature && !is_mark_ligature; |
Behdad Esfahbod | 3cca978 | 2018-10-02 15:02:16 +0200 | [diff] [blame] | 1321 | |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1322 | unsigned int klass = is_ligature ? HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE : 0; |
| 1323 | unsigned int lig_id = is_ligature ? _hb_allocate_lig_id (buffer) : 0; |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1324 | unsigned int last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur()); |
| 1325 | unsigned int last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur()); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1326 | unsigned int components_so_far = last_num_components; |
| 1327 | |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1328 | if (is_ligature) |
Behdad Esfahbod | 7e08f12 | 2013-05-27 14:48:34 -0400 | [diff] [blame] | 1329 | { |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1330 | _hb_glyph_info_set_lig_props_for_ligature (&buffer->cur(), lig_id, total_component_count); |
Behdad Esfahbod | 3c3df9c | 2013-10-17 13:58:31 +0200 | [diff] [blame] | 1331 | if (_hb_glyph_info_get_general_category (&buffer->cur()) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) |
Behdad Esfahbod | 3d436d3 | 2013-10-28 21:00:37 +0100 | [diff] [blame] | 1332 | { |
Behdad Esfahbod | 8259669 | 2015-11-02 17:44:05 -0800 | [diff] [blame] | 1333 | _hb_glyph_info_set_general_category (&buffer->cur(), HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER); |
Behdad Esfahbod | 3d436d3 | 2013-10-28 21:00:37 +0100 | [diff] [blame] | 1334 | } |
Behdad Esfahbod | 7e08f12 | 2013-05-27 14:48:34 -0400 | [diff] [blame] | 1335 | } |
Behdad Esfahbod | a016174 | 2013-10-18 00:06:30 +0200 | [diff] [blame] | 1336 | c->replace_glyph_with_ligature (lig_glyph, klass); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1337 | |
| 1338 | for (unsigned int i = 1; i < count; i++) |
| 1339 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 1340 | while (buffer->idx < match_positions[i] && buffer->successful) |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1341 | { |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1342 | if (is_ligature) |
| 1343 | { |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 1344 | unsigned int this_comp = _hb_glyph_info_get_lig_comp (&buffer->cur()); |
Behdad Esfahbod | 2f02fc7 | 2015-12-17 15:21:14 +0000 | [diff] [blame] | 1345 | if (this_comp == 0) |
Behdad Esfahbod | 100fbea | 2015-12-17 15:23:09 +0000 | [diff] [blame] | 1346 | this_comp = last_num_components; |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1347 | unsigned int new_lig_comp = components_so_far - last_num_components + |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 1348 | hb_min (this_comp, last_num_components); |
Behdad Esfahbod | 2f02fc7 | 2015-12-17 15:21:14 +0000 | [diff] [blame] | 1349 | _hb_glyph_info_set_lig_props_for_mark (&buffer->cur(), lig_id, new_lig_comp); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1350 | } |
Behdad Esfahbod | 8450f43 | 2021-03-15 15:18:06 -0600 | [diff] [blame] | 1351 | (void) buffer->next_glyph (); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1352 | } |
| 1353 | |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1354 | last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur()); |
| 1355 | last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur()); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1356 | components_so_far += last_num_components; |
| 1357 | |
| 1358 | /* Skip the base glyph */ |
Behdad Esfahbod | 3c3df9c | 2013-10-17 13:58:31 +0200 | [diff] [blame] | 1359 | buffer->idx++; |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1360 | } |
| 1361 | |
Ebrahim Byagowi | cc977b6 | 2020-03-26 11:18:02 +0430 | [diff] [blame] | 1362 | if (!is_mark_ligature && last_lig_id) |
| 1363 | { |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1364 | /* Re-adjust components for any marks following. */ |
Ebrahim Byagowi | cc977b6 | 2020-03-26 11:18:02 +0430 | [diff] [blame] | 1365 | for (unsigned i = buffer->idx; i < buffer->len; ++i) |
| 1366 | { |
| 1367 | if (last_lig_id != _hb_glyph_info_get_lig_id (&buffer->info[i])) break; |
| 1368 | |
| 1369 | unsigned this_comp = _hb_glyph_info_get_lig_comp (&buffer->info[i]); |
| 1370 | if (!this_comp) break; |
| 1371 | |
| 1372 | unsigned new_lig_comp = components_so_far - last_num_components + |
| 1373 | hb_min (this_comp, last_num_components); |
| 1374 | _hb_glyph_info_set_lig_props_for_mark (&buffer->info[i], lig_id, new_lig_comp); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1375 | } |
| 1376 | } |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1377 | return_trace (true); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1378 | } |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1379 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1380 | template <typename HBUINT> |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1381 | static inline bool match_backtrack (hb_ot_apply_context_t *c, |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1382 | unsigned int count, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1383 | const HBUINT backtrack[], |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1384 | match_func_t match_func, |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 1385 | const void *match_data, |
| 1386 | unsigned int *match_start) |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1387 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1388 | TRACE_APPLY (nullptr); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1389 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1390 | hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_context; |
Behdad Esfahbod | b051be5 | 2015-01-29 13:40:39 +0100 | [diff] [blame] | 1391 | skippy_iter.reset (c->buffer->backtrack_len (), count); |
Behdad Esfahbod | f114b18 | 2022-07-06 17:31:46 -0600 | [diff] [blame] | 1392 | skippy_iter.set_match_func (match_func, match_data); |
| 1393 | skippy_iter.set_glyph_data (backtrack); |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1394 | |
Behdad Esfahbod | 4d3aeb8 | 2012-01-16 16:43:26 -0500 | [diff] [blame] | 1395 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1396 | { |
| 1397 | unsigned unsafe_from; |
| 1398 | if (!skippy_iter.prev (&unsafe_from)) |
| 1399 | { |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1400 | *match_start = unsafe_from; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1401 | return_trace (false); |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1402 | } |
| 1403 | } |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1404 | |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 1405 | *match_start = skippy_iter.idx; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1406 | return_trace (true); |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1409 | template <typename HBUINT> |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1410 | static inline bool match_lookahead (hb_ot_apply_context_t *c, |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1411 | unsigned int count, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1412 | const HBUINT lookahead[], |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1413 | match_func_t match_func, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 1414 | const void *match_data, |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1415 | unsigned int start_index, |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 1416 | unsigned int *end_index) |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1417 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1418 | TRACE_APPLY (nullptr); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1419 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1420 | hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_context; |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1421 | skippy_iter.reset (start_index - 1, count); |
Behdad Esfahbod | f114b18 | 2022-07-06 17:31:46 -0600 | [diff] [blame] | 1422 | skippy_iter.set_match_func (match_func, match_data); |
| 1423 | skippy_iter.set_glyph_data (lookahead); |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1424 | |
Behdad Esfahbod | 370f03e | 2012-01-16 17:03:55 -0500 | [diff] [blame] | 1425 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1426 | { |
| 1427 | unsigned unsafe_to; |
| 1428 | if (!skippy_iter.next (&unsafe_to)) |
| 1429 | { |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1430 | *end_index = unsafe_to; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1431 | return_trace (false); |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1432 | } |
| 1433 | } |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1434 | |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 1435 | *end_index = skippy_iter.idx + 1; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1436 | return_trace (true); |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1437 | } |
| 1438 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 1439 | |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1440 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1441 | struct LookupRecord |
| 1442 | { |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 1443 | bool serialize (hb_serialize_context_t *c, |
| 1444 | const hb_map_t *lookup_map) const |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1445 | { |
| 1446 | TRACE_SERIALIZE (this); |
| 1447 | auto *out = c->embed (*this); |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 1448 | if (unlikely (!out)) return_trace (false); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1449 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 1450 | return_trace (c->check_assign (out->lookupListIndex, lookup_map->get (lookupListIndex), HB_SERIALIZE_ERROR_INT_OVERFLOW)); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1453 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 1454 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 1455 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1456 | return_trace (c->check_struct (this)); |
Behdad Esfahbod | cd3827e | 2009-08-04 02:09:34 -0400 | [diff] [blame] | 1457 | } |
| 1458 | |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1459 | HBUINT16 sequenceIndex; /* Index into current glyph |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1460 | * sequence--first glyph = 0 */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1461 | HBUINT16 lookupListIndex; /* Lookup to apply to that |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1462 | * position--zero--based */ |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 1463 | public: |
| 1464 | DEFINE_SIZE_STATIC (4); |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1465 | }; |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1466 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 1467 | static unsigned serialize_lookuprecord_array (hb_serialize_context_t *c, |
| 1468 | const hb_array_t<const LookupRecord> lookupRecords, |
| 1469 | const hb_map_t *lookup_map) |
| 1470 | { |
| 1471 | unsigned count = 0; |
| 1472 | for (const LookupRecord& r : lookupRecords) |
| 1473 | { |
| 1474 | if (!lookup_map->has (r.lookupListIndex)) |
| 1475 | continue; |
| 1476 | |
| 1477 | if (!r.serialize (c, lookup_map)) |
| 1478 | return 0; |
| 1479 | |
| 1480 | count++; |
| 1481 | } |
| 1482 | return count; |
| 1483 | } |
| 1484 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1485 | enum ContextFormat { SimpleContext = 1, ClassBasedContext = 2, CoverageBasedContext = 3 }; |
| 1486 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1487 | template <typename HBUINT> |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1488 | static void context_closure_recurse_lookups (hb_closure_context_t *c, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1489 | unsigned inputCount, const HBUINT input[], |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1490 | unsigned lookupCount, |
| 1491 | const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */, |
| 1492 | unsigned value, |
| 1493 | ContextFormat context_format, |
| 1494 | const void *data, |
| 1495 | intersected_glyphs_func_t intersected_glyphs_func) |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1496 | { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1497 | hb_set_t *covered_seq_indicies = hb_set_create (); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1498 | for (unsigned int i = 0; i < lookupCount; i++) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1499 | { |
| 1500 | unsigned seqIndex = lookupRecord[i].sequenceIndex; |
Garret Rieger | adca4ce | 2021-03-30 13:20:50 -0700 | [diff] [blame] | 1501 | if (seqIndex >= inputCount) continue; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1502 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1503 | bool has_pos_glyphs = false; |
| 1504 | hb_set_t pos_glyphs; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1505 | |
Behdad Esfahbod | 202e6c4 | 2022-05-18 17:12:43 -0600 | [diff] [blame] | 1506 | if (!hb_set_has (covered_seq_indicies, seqIndex)) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1507 | { |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1508 | has_pos_glyphs = true; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1509 | if (seqIndex == 0) |
| 1510 | { |
| 1511 | switch (context_format) { |
| 1512 | case ContextFormat::SimpleContext: |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1513 | pos_glyphs.add (value); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1514 | break; |
| 1515 | case ContextFormat::ClassBasedContext: |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 1516 | intersected_glyphs_func (&c->parent_active_glyphs (), data, value, &pos_glyphs); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1517 | break; |
| 1518 | case ContextFormat::CoverageBasedContext: |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 1519 | pos_glyphs.set (c->parent_active_glyphs ()); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1520 | break; |
| 1521 | } |
| 1522 | } |
| 1523 | else |
| 1524 | { |
| 1525 | const void *input_data = input; |
| 1526 | unsigned input_value = seqIndex - 1; |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1527 | if (context_format != ContextFormat::SimpleContext) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1528 | { |
| 1529 | input_data = data; |
| 1530 | input_value = input[seqIndex - 1]; |
| 1531 | } |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 1532 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1533 | intersected_glyphs_func (c->glyphs, input_data, input_value, &pos_glyphs); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1537 | covered_seq_indicies->add (seqIndex); |
| 1538 | if (has_pos_glyphs) { |
Behdad Esfahbod | 4cfc2d6 | 2022-05-18 15:32:19 -0600 | [diff] [blame] | 1539 | c->push_cur_active_glyphs () = std::move (pos_glyphs); |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1540 | } else { |
| 1541 | c->push_cur_active_glyphs ().set (*c->glyphs); |
| 1542 | } |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 1543 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1544 | unsigned endIndex = inputCount; |
| 1545 | if (context_format == ContextFormat::CoverageBasedContext) |
| 1546 | endIndex += 1; |
| 1547 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1548 | c->recurse (lookupRecord[i].lookupListIndex, covered_seq_indicies, seqIndex, endIndex); |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 1549 | |
Qunxin Liu | 706014f | 2021-12-01 20:20:12 -0800 | [diff] [blame] | 1550 | c->pop_cur_done_glyphs (); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1551 | } |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 1552 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1553 | hb_set_destroy (covered_seq_indicies); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1554 | } |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 1555 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1556 | template <typename context_t> |
| 1557 | static inline void recurse_lookups (context_t *c, |
| 1558 | unsigned int lookupCount, |
| 1559 | const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */) |
| 1560 | { |
| 1561 | for (unsigned int i = 0; i < lookupCount; i++) |
| 1562 | c->recurse (lookupRecord[i].lookupListIndex); |
| 1563 | } |
| 1564 | |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1565 | static inline void apply_lookup (hb_ot_apply_context_t *c, |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 1566 | unsigned int count, /* Including the first glyph */ |
Behdad Esfahbod | 5ba4504 | 2015-11-02 15:43:08 -0800 | [diff] [blame] | 1567 | unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */ |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 1568 | unsigned int lookupCount, |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1569 | const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */ |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1570 | unsigned int match_end) |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1571 | { |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1572 | hb_buffer_t *buffer = c->buffer; |
jfkthame | 44f7d6e | 2017-02-17 03:03:24 +0000 | [diff] [blame] | 1573 | int end; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1574 | |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1575 | /* All positions are distance from beginning of *output* buffer. |
| 1576 | * Adjust. */ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1577 | { |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1578 | unsigned int bl = buffer->backtrack_len (); |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1579 | end = bl + match_end - buffer->idx; |
Behdad Esfahbod | 8751de5 | 2013-07-18 16:29:50 -0400 | [diff] [blame] | 1580 | |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1581 | int delta = bl - buffer->idx; |
| 1582 | /* Convert positions to new indexing. */ |
| 1583 | for (unsigned int j = 0; j < count; j++) |
| 1584 | match_positions[j] += delta; |
Behdad Esfahbod | 8820bb2 | 2013-02-14 07:41:03 -0500 | [diff] [blame] | 1585 | } |
Behdad Esfahbod | e73a0c2 | 2009-05-18 04:15:25 -0400 | [diff] [blame] | 1586 | |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 1587 | for (unsigned int i = 0; i < lookupCount && buffer->successful; i++) |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1588 | { |
| 1589 | unsigned int idx = lookupRecord[i].sequenceIndex; |
| 1590 | if (idx >= count) |
| 1591 | continue; |
| 1592 | |
Behdad Esfahbod | ccd9161 | 2022-03-24 13:10:48 -0600 | [diff] [blame] | 1593 | unsigned int orig_len = buffer->backtrack_len () + buffer->lookahead_len (); |
| 1594 | |
| 1595 | /* This can happen if earlier recursed lookups deleted many entries. */ |
| 1596 | if (unlikely (match_positions[idx] >= orig_len)) |
| 1597 | continue; |
| 1598 | |
Behdad Esfahbod | e593072 | 2017-11-14 15:47:55 -0800 | [diff] [blame] | 1599 | if (unlikely (!buffer->move_to (match_positions[idx]))) |
| 1600 | break; |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1601 | |
Behdad Esfahbod | baf7779 | 2017-11-14 21:53:48 -0800 | [diff] [blame] | 1602 | if (unlikely (buffer->max_ops <= 0)) |
| 1603 | break; |
| 1604 | |
Behdad Esfahbod | bc80e4b | 2022-07-24 18:48:38 -0600 | [diff] [blame] | 1605 | if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) |
| 1606 | { |
| 1607 | if (buffer->have_output) |
| 1608 | c->buffer->sync_so_far (); |
| 1609 | c->buffer->message (c->font, |
| 1610 | "recursing to lookup %u at %d", |
| 1611 | (unsigned) lookupRecord[i].lookupListIndex, |
| 1612 | buffer->idx); |
| 1613 | } |
| 1614 | |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1615 | if (!c->recurse (lookupRecord[i].lookupListIndex)) |
| 1616 | continue; |
| 1617 | |
Behdad Esfahbod | bc80e4b | 2022-07-24 18:48:38 -0600 | [diff] [blame] | 1618 | if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ()) |
| 1619 | { |
| 1620 | if (buffer->have_output) |
| 1621 | c->buffer->sync_so_far (); |
| 1622 | c->buffer->message (c->font, |
Behdad Esfahbod | 94de328 | 2022-07-24 18:51:55 -0600 | [diff] [blame] | 1623 | "recursed to lookup %u", |
| 1624 | (unsigned) lookupRecord[i].lookupListIndex); |
Behdad Esfahbod | bc80e4b | 2022-07-24 18:48:38 -0600 | [diff] [blame] | 1625 | } |
| 1626 | |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1627 | unsigned int new_len = buffer->backtrack_len () + buffer->lookahead_len (); |
| 1628 | int delta = new_len - orig_len; |
| 1629 | |
| 1630 | if (!delta) |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 1631 | continue; |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1632 | |
Behdad Esfahbod | 9ac9af7 | 2017-03-05 13:51:01 -0800 | [diff] [blame] | 1633 | /* Recursed lookup changed buffer len. Adjust. |
| 1634 | * |
| 1635 | * TODO: |
| 1636 | * |
| 1637 | * Right now, if buffer length increased by n, we assume n new glyphs |
| 1638 | * were added right after the current position, and if buffer length |
| 1639 | * was decreased by n, we assume n match positions after the current |
| 1640 | * one where removed. The former (buffer length increased) case is |
| 1641 | * fine, but the decrease case can be improved in at least two ways, |
| 1642 | * both of which are significant: |
| 1643 | * |
| 1644 | * - If recursed-to lookup is MultipleSubst and buffer length |
| 1645 | * decreased, then it's current match position that was deleted, |
| 1646 | * NOT the one after it. |
| 1647 | * |
| 1648 | * - If buffer length was decreased by n, it does not necessarily |
Behdad Esfahbod | a85461b | 2022-04-20 12:13:16 -0600 | [diff] [blame] | 1649 | * mean that n match positions where removed, as there recursed-to |
| 1650 | * lookup might had a different LookupFlag. Here's a constructed |
| 1651 | * case of that: |
| 1652 | * https://github.com/harfbuzz/harfbuzz/discussions/3538 |
Behdad Esfahbod | 9ac9af7 | 2017-03-05 13:51:01 -0800 | [diff] [blame] | 1653 | * |
| 1654 | * It should be possible to construct tests for both of these cases. |
| 1655 | */ |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1656 | |
jfkthame | 44f7d6e | 2017-02-17 03:03:24 +0000 | [diff] [blame] | 1657 | end += delta; |
Behdad Esfahbod | ccd9161 | 2022-03-24 13:10:48 -0600 | [diff] [blame] | 1658 | if (end < int (match_positions[idx])) |
Behdad Esfahbod | 359dead | 2016-05-06 16:19:19 +0100 | [diff] [blame] | 1659 | { |
Behdad Esfahbod | 4b4a1b9 | 2016-12-21 23:10:43 -0600 | [diff] [blame] | 1660 | /* End might end up being smaller than match_positions[idx] if the recursed |
Behdad Esfahbod | ccd9161 | 2022-03-24 13:10:48 -0600 | [diff] [blame] | 1661 | * lookup ended up removing many items. |
| 1662 | * Just never rewind end beyond start of current position, since that is |
| 1663 | * not possible in the recursed lookup. Also adjust delta as such. |
| 1664 | * |
| 1665 | * https://bugs.chromium.org/p/chromium/issues/detail?id=659496 |
| 1666 | * https://github.com/harfbuzz/harfbuzz/issues/1611 |
| 1667 | */ |
| 1668 | delta += match_positions[idx] - end; |
Behdad Esfahbod | 4b4a1b9 | 2016-12-21 23:10:43 -0600 | [diff] [blame] | 1669 | end = match_positions[idx]; |
Behdad Esfahbod | 359dead | 2016-05-06 16:19:19 +0100 | [diff] [blame] | 1670 | } |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1671 | |
| 1672 | unsigned int next = idx + 1; /* next now is the position after the recursed lookup. */ |
| 1673 | |
| 1674 | if (delta > 0) |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1675 | { |
Behdad Esfahbod | 5ba4504 | 2015-11-02 15:43:08 -0800 | [diff] [blame] | 1676 | if (unlikely (delta + count > HB_MAX_CONTEXT_LENGTH)) |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1677 | break; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1678 | } |
| 1679 | else |
| 1680 | { |
Behdad Esfahbod | bf2a845 | 2022-03-24 13:09:53 -0600 | [diff] [blame] | 1681 | /* NOTE: delta is non-positive. */ |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 1682 | delta = hb_max (delta, (int) next - (int) count); |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1683 | next -= delta; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1684 | } |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1685 | |
| 1686 | /* Shift! */ |
| 1687 | memmove (match_positions + next + delta, match_positions + next, |
| 1688 | (count - next) * sizeof (match_positions[0])); |
| 1689 | next += delta; |
| 1690 | count += delta; |
| 1691 | |
| 1692 | /* Fill in new entries. */ |
| 1693 | for (unsigned int j = idx + 1; j < next; j++) |
| 1694 | match_positions[j] = match_positions[j - 1] + 1; |
| 1695 | |
| 1696 | /* And fixup the rest. */ |
| 1697 | for (; next < count; next++) |
| 1698 | match_positions[next] += delta; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1699 | } |
| 1700 | |
Behdad Esfahbod | cac6c86 | 2021-03-15 13:46:54 -0600 | [diff] [blame] | 1701 | (void) buffer->move_to (end); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1702 | } |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 1703 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 1704 | |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1705 | |
| 1706 | /* Contextual lookups */ |
| 1707 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1708 | struct ContextClosureLookupContext |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1709 | { |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1710 | ContextClosureFuncs funcs; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1711 | ContextFormat context_format; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1712 | const void *intersects_data; |
| 1713 | }; |
| 1714 | |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1715 | struct ContextCollectGlyphsLookupContext |
| 1716 | { |
| 1717 | ContextCollectGlyphsFuncs funcs; |
| 1718 | const void *collect_data; |
| 1719 | }; |
| 1720 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1721 | struct ContextApplyLookupContext |
| 1722 | { |
| 1723 | ContextApplyFuncs funcs; |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 1724 | const void *match_data; |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1725 | }; |
| 1726 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1727 | template <typename HBUINT> |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1728 | static inline bool context_intersects (const hb_set_t *glyphs, |
| 1729 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1730 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1731 | ContextClosureLookupContext &lookup_context) |
| 1732 | { |
Qunxin Liu | 44d88cf | 2020-05-08 15:33:34 -0700 | [diff] [blame] | 1733 | return array_is_subset_of (glyphs, |
| 1734 | inputCount ? inputCount - 1 : 0, input, |
| 1735 | lookup_context.funcs.intersects, lookup_context.intersects_data); |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1738 | template <typename HBUINT> |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 1739 | static inline void context_closure_lookup (hb_closure_context_t *c, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1740 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1741 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1742 | unsigned int lookupCount, |
| 1743 | const LookupRecord lookupRecord[], |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1744 | unsigned value, /* Index of first glyph in Coverage or Class value in ClassDef table */ |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1745 | ContextClosureLookupContext &lookup_context) |
| 1746 | { |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1747 | if (context_intersects (c->glyphs, |
| 1748 | inputCount, input, |
| 1749 | lookup_context)) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1750 | context_closure_recurse_lookups (c, |
| 1751 | inputCount, input, |
| 1752 | lookupCount, lookupRecord, |
| 1753 | value, |
| 1754 | lookup_context.context_format, |
| 1755 | lookup_context.intersects_data, |
| 1756 | lookup_context.funcs.intersected_glyphs); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1757 | } |
| 1758 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1759 | template <typename HBUINT> |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1760 | static inline void context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c, |
| 1761 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1762 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1763 | unsigned int lookupCount, |
| 1764 | const LookupRecord lookupRecord[], |
| 1765 | ContextCollectGlyphsLookupContext &lookup_context) |
| 1766 | { |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 1767 | collect_array (c, c->input, |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1768 | inputCount ? inputCount - 1 : 0, input, |
| 1769 | lookup_context.funcs.collect, lookup_context.collect_data); |
| 1770 | recurse_lookups (c, |
| 1771 | lookupCount, lookupRecord); |
| 1772 | } |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1773 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1774 | template <typename HBUINT> |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1775 | static inline bool context_would_apply_lookup (hb_would_apply_context_t *c, |
| 1776 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1777 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 0beb66e | 2012-12-05 18:46:04 -0500 | [diff] [blame] | 1778 | unsigned int lookupCount HB_UNUSED, |
| 1779 | const LookupRecord lookupRecord[] HB_UNUSED, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1780 | ContextApplyLookupContext &lookup_context) |
| 1781 | { |
| 1782 | return would_match_input (c, |
| 1783 | inputCount, input, |
| 1784 | lookup_context.funcs.match, lookup_context.match_data); |
| 1785 | } |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1786 | |
| 1787 | template <typename HBUINT> |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1788 | static inline bool context_apply_lookup (hb_ot_apply_context_t *c, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1789 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 1790 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1791 | unsigned int lookupCount, |
| 1792 | const LookupRecord lookupRecord[], |
| 1793 | ContextApplyLookupContext &lookup_context) |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1794 | { |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1795 | unsigned match_end = 0; |
| 1796 | unsigned match_positions[HB_MAX_CONTEXT_LENGTH]; |
| 1797 | if (match_input (c, |
| 1798 | inputCount, input, |
| 1799 | lookup_context.funcs.match, lookup_context.match_data, |
| 1800 | &match_end, match_positions)) |
| 1801 | { |
| 1802 | c->buffer->unsafe_to_break (c->buffer->idx, match_end); |
| 1803 | apply_lookup (c, |
| 1804 | inputCount, match_positions, |
| 1805 | lookupCount, lookupRecord, |
| 1806 | match_end); |
| 1807 | return true; |
| 1808 | } |
| 1809 | else |
| 1810 | { |
| 1811 | c->buffer->unsafe_to_concat (c->buffer->idx, match_end); |
| 1812 | return false; |
| 1813 | } |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1814 | } |
| 1815 | |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1816 | template <typename Types> |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1817 | struct Rule |
| 1818 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1819 | bool intersects (const hb_set_t *glyphs, ContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1820 | { |
| 1821 | return context_intersects (glyphs, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1822 | inputCount, inputZ.arrayZ, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1823 | lookup_context); |
| 1824 | } |
| 1825 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1826 | void closure (hb_closure_context_t *c, unsigned value, ContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1827 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 1828 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 014e038 | 2020-03-31 16:29:29 -0700 | [diff] [blame] | 1829 | |
Behdad Esfahbod | 5192294 | 2022-07-08 14:00:24 -0600 | [diff] [blame] | 1830 | const auto &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
| 1831 | (inputZ.as_array ((inputCount ? inputCount - 1 : 0))); |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 1832 | context_closure_lookup (c, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1833 | inputCount, inputZ.arrayZ, |
| 1834 | lookupCount, lookupRecord.arrayZ, |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1835 | value, lookup_context); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1836 | } |
| 1837 | |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 1838 | void closure_lookups (hb_closure_lookups_context_t *c, |
| 1839 | ContextClosureLookupContext &lookup_context) const |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1840 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 1841 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 1842 | if (!intersects (c->glyphs, lookup_context)) return; |
Garret Rieger | 014e038 | 2020-03-31 16:29:29 -0700 | [diff] [blame] | 1843 | |
Behdad Esfahbod | 5192294 | 2022-07-08 14:00:24 -0600 | [diff] [blame] | 1844 | const auto &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
| 1845 | (inputZ.as_array (inputCount ? inputCount - 1 : 0)); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1846 | recurse_lookups (c, lookupCount, lookupRecord.arrayZ); |
| 1847 | } |
| 1848 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1849 | void collect_glyphs (hb_collect_glyphs_context_t *c, |
| 1850 | ContextCollectGlyphsLookupContext &lookup_context) const |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1851 | { |
Behdad Esfahbod | 5192294 | 2022-07-08 14:00:24 -0600 | [diff] [blame] | 1852 | const auto &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
| 1853 | (inputZ.as_array (inputCount ? inputCount - 1 : 0)); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1854 | context_collect_glyphs_lookup (c, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1855 | inputCount, inputZ.arrayZ, |
| 1856 | lookupCount, lookupRecord.arrayZ, |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1857 | lookup_context); |
| 1858 | } |
| 1859 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1860 | bool would_apply (hb_would_apply_context_t *c, |
| 1861 | ContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1862 | { |
Behdad Esfahbod | 5192294 | 2022-07-08 14:00:24 -0600 | [diff] [blame] | 1863 | const auto &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
| 1864 | (inputZ.as_array (inputCount ? inputCount - 1 : 0)); |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 1865 | return context_would_apply_lookup (c, |
| 1866 | inputCount, inputZ.arrayZ, |
| 1867 | lookupCount, lookupRecord.arrayZ, |
| 1868 | lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1869 | } |
| 1870 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1871 | bool apply (hb_ot_apply_context_t *c, |
| 1872 | ContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1873 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 1874 | TRACE_APPLY (this); |
Behdad Esfahbod | 5192294 | 2022-07-08 14:00:24 -0600 | [diff] [blame] | 1875 | const auto &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
| 1876 | (inputZ.as_array (inputCount ? inputCount - 1 : 0)); |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1877 | return_trace (context_apply_lookup (c, inputCount, inputZ.arrayZ, lookupCount, lookupRecord.arrayZ, lookup_context)); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1878 | } |
| 1879 | |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1880 | bool serialize (hb_serialize_context_t *c, |
| 1881 | const hb_map_t *input_mapping, /* old->new glyphid or class mapping */ |
| 1882 | const hb_map_t *lookup_map) const |
| 1883 | { |
| 1884 | TRACE_SERIALIZE (this); |
| 1885 | auto *out = c->start_embed (this); |
| 1886 | if (unlikely (!c->extend_min (out))) return_trace (false); |
| 1887 | |
| 1888 | out->inputCount = inputCount; |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1889 | const auto input = inputZ.as_array (inputCount - 1); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1890 | for (const auto org : input) |
| 1891 | { |
| 1892 | HBUINT16 d; |
| 1893 | d = input_mapping->get (org); |
| 1894 | c->copy (d); |
| 1895 | } |
| 1896 | |
Behdad Esfahbod | 5192294 | 2022-07-08 14:00:24 -0600 | [diff] [blame] | 1897 | const auto &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
| 1898 | (inputZ.as_array ((inputCount ? inputCount - 1 : 0))); |
Garret Rieger | f51b48c | 2021-11-02 16:16:52 -0700 | [diff] [blame] | 1899 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 1900 | unsigned count = serialize_lookuprecord_array (c, lookupRecord.as_array (lookupCount), lookup_map); |
| 1901 | return_trace (c->check_assign (out->lookupCount, count, HB_SERIALIZE_ERROR_INT_OVERFLOW)); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | bool subset (hb_subset_context_t *c, |
| 1905 | const hb_map_t *lookup_map, |
| 1906 | const hb_map_t *klass_map = nullptr) const |
| 1907 | { |
| 1908 | TRACE_SUBSET (this); |
Qunxin Liu | e88fc41 | 2021-11-18 16:53:36 -0800 | [diff] [blame] | 1909 | if (unlikely (!inputCount)) return_trace (false); |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1910 | const auto input = inputZ.as_array (inputCount - 1); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1911 | |
| 1912 | const hb_map_t *mapping = klass_map == nullptr ? c->plan->glyph_map : klass_map; |
| 1913 | if (!hb_all (input, mapping)) return_trace (false); |
| 1914 | return_trace (serialize (c->serializer, mapping, lookup_map)); |
| 1915 | } |
| 1916 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 1917 | public: |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1918 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 1919 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 1920 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 5aad819 | 2017-11-03 17:16:26 -0400 | [diff] [blame] | 1921 | return_trace (inputCount.sanitize (c) && |
| 1922 | lookupCount.sanitize (c) && |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1923 | c->check_range (inputZ.arrayZ, |
Behdad Esfahbod | 9c6921c | 2018-11-30 15:16:57 -0500 | [diff] [blame] | 1924 | inputZ.item_size * (inputCount ? inputCount - 1 : 0) + |
Behdad Esfahbod | 6d7c6e1 | 2018-02-07 14:09:56 -0600 | [diff] [blame] | 1925 | LookupRecord::static_size * lookupCount)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 1926 | } |
| 1927 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 1928 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1929 | HBUINT16 inputCount; /* Total number of glyphs in input |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1930 | * glyph sequence--includes the first |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1931 | * glyph */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1932 | HBUINT16 lookupCount; /* Number of LookupRecords */ |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1933 | UnsizedArrayOf<typename Types::HBUINT> |
Ebrahim Byagowi | ce114d6 | 2019-12-31 15:53:02 +0330 | [diff] [blame] | 1934 | inputZ; /* Array of match inputs--start with |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1935 | * second glyph */ |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1936 | /*UnsizedArrayOf<LookupRecord> |
| 1937 | lookupRecordX;*/ /* Array of LookupRecords--in |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1938 | * design order */ |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 1939 | public: |
Behdad Esfahbod | 6d7c6e1 | 2018-02-07 14:09:56 -0600 | [diff] [blame] | 1940 | DEFINE_SIZE_ARRAY (4, inputZ); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1941 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1942 | |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1943 | template <typename Types> |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1944 | struct RuleSet |
| 1945 | { |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 1946 | using Rule = OT::Rule<Types>; |
| 1947 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1948 | bool intersects (const hb_set_t *glyphs, |
| 1949 | ContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1950 | { |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1951 | return |
| 1952 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1953 | | hb_map (hb_add (this)) |
| 1954 | | hb_map ([&] (const Rule &_) { return _.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1955 | | hb_any |
| 1956 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1957 | } |
| 1958 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1959 | void closure (hb_closure_context_t *c, unsigned value, |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1960 | ContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1961 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 1962 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 014e038 | 2020-03-31 16:29:29 -0700 | [diff] [blame] | 1963 | |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1964 | return |
| 1965 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1966 | | hb_map (hb_add (this)) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1967 | | hb_apply ([&] (const Rule &_) { _.closure (c, value, lookup_context); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1968 | ; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1969 | } |
| 1970 | |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 1971 | void closure_lookups (hb_closure_lookups_context_t *c, |
| 1972 | ContextClosureLookupContext &lookup_context) const |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1973 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 1974 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1975 | + hb_iter (rule) |
| 1976 | | hb_map (hb_add (this)) |
Garret Rieger | e31c269 | 2020-09-28 16:51:25 -0700 | [diff] [blame] | 1977 | | hb_apply ([&] (const Rule &_) { _.closure_lookups (c, lookup_context); }) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1978 | ; |
| 1979 | } |
| 1980 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1981 | void collect_glyphs (hb_collect_glyphs_context_t *c, |
| 1982 | ContextCollectGlyphsLookupContext &lookup_context) const |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1983 | { |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1984 | return |
| 1985 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1986 | | hb_map (hb_add (this)) |
| 1987 | | hb_apply ([&] (const Rule &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1988 | ; |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1989 | } |
| 1990 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1991 | bool would_apply (hb_would_apply_context_t *c, |
| 1992 | ContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1993 | { |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1994 | return |
| 1995 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1996 | | hb_map (hb_add (this)) |
| 1997 | | hb_map ([&] (const Rule &_) { return _.would_apply (c, lookup_context); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1998 | | hb_any |
| 1999 | ; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2000 | } |
| 2001 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2002 | bool apply (hb_ot_apply_context_t *c, |
| 2003 | ContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2004 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2005 | TRACE_APPLY (this); |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 2006 | return_trace ( |
| 2007 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2008 | | hb_map (hb_add (this)) |
| 2009 | | hb_map ([&] (const Rule &_) { return _.apply (c, lookup_context); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 2010 | | hb_any |
| 2011 | ) |
| 2012 | ; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2013 | } |
| 2014 | |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2015 | bool subset (hb_subset_context_t *c, |
| 2016 | const hb_map_t *lookup_map, |
| 2017 | const hb_map_t *klass_map = nullptr) const |
| 2018 | { |
| 2019 | TRACE_SUBSET (this); |
| 2020 | |
| 2021 | auto snap = c->serializer->snapshot (); |
| 2022 | auto *out = c->serializer->start_embed (*this); |
| 2023 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 2024 | |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 2025 | for (const Offset16To<Rule>& _ : rule) |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2026 | { |
| 2027 | if (!_) continue; |
Garret Rieger | c2cc566 | 2021-09-22 14:15:55 -0700 | [diff] [blame] | 2028 | auto o_snap = c->serializer->snapshot (); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2029 | auto *o = out->rule.serialize_append (c->serializer); |
| 2030 | if (unlikely (!o)) continue; |
| 2031 | |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2032 | if (!o->serialize_subset (c, _, this, lookup_map, klass_map)) |
| 2033 | { |
Ebrahim Byagowi | 5a7cc7f | 2020-07-29 08:33:32 +0430 | [diff] [blame] | 2034 | out->rule.pop (); |
| 2035 | c->serializer->revert (o_snap); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | bool ret = bool (out->rule); |
| 2040 | if (!ret) c->serializer->revert (snap); |
| 2041 | |
| 2042 | return_trace (ret); |
| 2043 | } |
| 2044 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2045 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 2046 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2047 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2048 | return_trace (rule.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2049 | } |
| 2050 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2051 | protected: |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 2052 | Array16OfOffset16To<Rule> |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 2053 | rule; /* Array of Rule tables |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2054 | * ordered by preference */ |
Behdad Esfahbod | ed07422 | 2010-05-10 18:08:46 -0400 | [diff] [blame] | 2055 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 2056 | DEFINE_SIZE_ARRAY (2, rule); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2057 | }; |
| 2058 | |
| 2059 | |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2060 | template <typename Types> |
| 2061 | struct ContextFormat1_4 |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2062 | { |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2063 | using RuleSet = OT::RuleSet<Types>; |
| 2064 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2065 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2066 | { |
| 2067 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2068 | {intersects_glyph, intersected_glyph}, |
| 2069 | ContextFormat::SimpleContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2070 | nullptr |
| 2071 | }; |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2072 | |
| 2073 | return |
| 2074 | + hb_zip (this+coverage, ruleSet) |
| 2075 | | hb_filter (*glyphs, hb_first) |
| 2076 | | hb_map (hb_second) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2077 | | hb_map (hb_add (this)) |
| 2078 | | hb_map ([&] (const RuleSet &_) { return _.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2079 | | hb_any |
| 2080 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 2083 | bool may_have_non_1to1 () const |
| 2084 | { return true; } |
| 2085 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2086 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2087 | { |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 2088 | hb_set_t& cur_active_glyphs = c->push_cur_active_glyphs (); |
| 2089 | get_coverage ().intersect_set (c->previous_parent_active_glyphs (), cur_active_glyphs); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 2090 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2091 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2092 | {intersects_glyph, intersected_glyph}, |
| 2093 | ContextFormat::SimpleContext, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 2094 | nullptr |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2095 | }; |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2096 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2097 | + hb_zip (this+coverage, hb_range ((unsigned) ruleSet.len)) |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 2098 | | hb_filter ([&] (hb_codepoint_t _) { |
| 2099 | return c->previous_parent_active_glyphs ().has (_); |
| 2100 | }, hb_first) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2101 | | hb_map ([&](const hb_pair_t<hb_codepoint_t, unsigned> _) { return hb_pair_t<unsigned, const RuleSet&> (_.first, this+ruleSet[_.second]); }) |
| 2102 | | hb_apply ([&] (const hb_pair_t<unsigned, const RuleSet&>& _) { _.second.closure (c, _.first, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2103 | ; |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 2104 | |
| 2105 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2106 | } |
| 2107 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2108 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 2109 | { |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 2110 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 2111 | {intersects_glyph, intersected_glyph}, |
| 2112 | ContextFormat::SimpleContext, |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 2113 | nullptr |
| 2114 | }; |
| 2115 | |
| 2116 | + hb_zip (this+coverage, ruleSet) |
| 2117 | | hb_filter (*c->glyphs, hb_first) |
| 2118 | | hb_map (hb_second) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2119 | | hb_map (hb_add (this)) |
Garret Rieger | e31c269 | 2020-09-28 16:51:25 -0700 | [diff] [blame] | 2120 | | hb_apply ([&] (const RuleSet &_) { _.closure_lookups (c, lookup_context); }) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2121 | ; |
| 2122 | } |
| 2123 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 2124 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 2125 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2126 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2127 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 2128 | (this+coverage).collect_coverage (c->input); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2129 | |
| 2130 | struct ContextCollectGlyphsLookupContext lookup_context = { |
| 2131 | {collect_glyph}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 2132 | nullptr |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2133 | }; |
| 2134 | |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2135 | + hb_iter (ruleSet) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2136 | | hb_map (hb_add (this)) |
| 2137 | | hb_apply ([&] (const RuleSet &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2138 | ; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2139 | } |
| 2140 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2141 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2142 | { |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 2143 | const RuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])]; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2144 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2145 | {match_glyph}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 2146 | nullptr |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2147 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 2148 | return rule_set.would_apply (c, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2149 | } |
| 2150 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 2151 | const Coverage &get_coverage () const { return this+coverage; } |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 2152 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2153 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2154 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2155 | TRACE_APPLY (this); |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 2156 | unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 2157 | if (likely (index == NOT_COVERED)) |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2158 | return_trace (false); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 2159 | |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2160 | const RuleSet &rule_set = this+ruleSet[index]; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2161 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2162 | {match_glyph}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 2163 | nullptr |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2164 | }; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2165 | return_trace (rule_set.apply (c, lookup_context)); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2166 | } |
| 2167 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2168 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2169 | { |
| 2170 | TRACE_SUBSET (this); |
Garret Rieger | e583505 | 2020-09-29 11:05:08 -0700 | [diff] [blame] | 2171 | const hb_set_t &glyphset = *c->plan->glyphset_gsub (); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2172 | const hb_map_t &glyph_map = *c->plan->glyph_map; |
| 2173 | |
| 2174 | auto *out = c->serializer->start_embed (*this); |
| 2175 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 2176 | out->format = format; |
| 2177 | |
| 2178 | const hb_map_t *lookup_map = c->table_tag == HB_OT_TAG_GSUB ? c->plan->gsub_lookups : c->plan->gpos_lookups; |
| 2179 | hb_sorted_vector_t<hb_codepoint_t> new_coverage; |
| 2180 | + hb_zip (this+coverage, ruleSet) |
| 2181 | | hb_filter (glyphset, hb_first) |
| 2182 | | hb_filter (subset_offset_array (c, out->ruleSet, this, lookup_map), hb_second) |
| 2183 | | hb_map (hb_first) |
| 2184 | | hb_map (glyph_map) |
| 2185 | | hb_sink (new_coverage) |
| 2186 | ; |
| 2187 | |
Garret Rieger | 085aa65 | 2021-06-14 16:47:45 -0700 | [diff] [blame] | 2188 | out->coverage.serialize_serialize (c->serializer, new_coverage.iter ()); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2189 | return_trace (bool (new_coverage)); |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2192 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 2193 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2194 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2195 | return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2196 | } |
| 2197 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2198 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2199 | HBUINT16 format; /* Format identifier--format = 1 */ |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2200 | typename Types::template OffsetTo<Coverage> |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2201 | coverage; /* Offset to Coverage table--from |
| 2202 | * beginning of table */ |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2203 | Array16Of<typename Types::template OffsetTo<RuleSet>> |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2204 | ruleSet; /* Array of RuleSet tables |
| 2205 | * ordered by Coverage Index */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 2206 | public: |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2207 | DEFINE_SIZE_ARRAY (2 + 2 * Types::size, ruleSet); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2208 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2209 | |
| 2210 | |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 2211 | template <typename Types> |
| 2212 | struct ContextFormat2_5 |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2213 | { |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2214 | using RuleSet = OT::RuleSet<SmallTypes>; |
| 2215 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2216 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2217 | { |
| 2218 | if (!(this+coverage).intersects (glyphs)) |
| 2219 | return false; |
| 2220 | |
| 2221 | const ClassDef &class_def = this+classDef; |
| 2222 | |
| 2223 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2224 | {intersects_class, intersected_class_glyphs}, |
| 2225 | ContextFormat::ClassBasedContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2226 | &class_def |
| 2227 | }; |
| 2228 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2229 | hb_set_t retained_coverage_glyphs; |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 2230 | (this+coverage).intersect_set (*glyphs, retained_coverage_glyphs); |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2231 | |
| 2232 | hb_set_t coverage_glyph_classes; |
| 2233 | class_def.intersected_classes (&retained_coverage_glyphs, &coverage_glyph_classes); |
| 2234 | |
| 2235 | |
Behdad Esfahbod | 668d2d5 | 2019-03-29 22:48:38 -0700 | [diff] [blame] | 2236 | return |
Behdad Esfahbod | 26111a1 | 2020-06-28 02:59:47 -0700 | [diff] [blame] | 2237 | + hb_iter (ruleSet) |
| 2238 | | hb_map (hb_add (this)) |
| 2239 | | hb_enumerate |
| 2240 | | hb_map ([&] (const hb_pair_t<unsigned, const RuleSet &> p) |
Behdad Esfahbod | e530692 | 2019-03-29 23:31:07 -0700 | [diff] [blame] | 2241 | { return class_def.intersects_class (glyphs, p.first) && |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2242 | coverage_glyph_classes.has (p.first) && |
Behdad Esfahbod | 26111a1 | 2020-06-28 02:59:47 -0700 | [diff] [blame] | 2243 | p.second.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | 668d2d5 | 2019-03-29 22:48:38 -0700 | [diff] [blame] | 2244 | | hb_any |
| 2245 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 2248 | bool may_have_non_1to1 () const |
| 2249 | { return true; } |
| 2250 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2251 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2252 | { |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2253 | if (!(this+coverage).intersects (c->glyphs)) |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2254 | return; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2255 | |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 2256 | hb_set_t& cur_active_glyphs = c->push_cur_active_glyphs (); |
| 2257 | get_coverage ().intersect_set (c->previous_parent_active_glyphs (), |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 2258 | cur_active_glyphs); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 2259 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2260 | const ClassDef &class_def = this+classDef; |
| 2261 | |
| 2262 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2263 | {intersects_class, intersected_class_glyphs}, |
| 2264 | ContextFormat::ClassBasedContext, |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 2265 | &class_def |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2266 | }; |
| 2267 | |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 2268 | + hb_enumerate (ruleSet) |
Behdad Esfahbod | 78d35f0 | 2019-05-15 18:15:05 -0700 | [diff] [blame] | 2269 | | hb_filter ([&] (unsigned _) |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 2270 | { return class_def.intersects_class (&c->parent_active_glyphs (), _); }, |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 2271 | hb_first) |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 2272 | | hb_apply ([&] (const hb_pair_t<unsigned, const typename Types::template OffsetTo<RuleSet>&> _) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2273 | { |
| 2274 | const RuleSet& rule_set = this+_.second; |
| 2275 | rule_set.closure (c, _.first, lookup_context); |
| 2276 | }) |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 2277 | ; |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 2278 | |
| 2279 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2280 | } |
| 2281 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2282 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 2283 | { |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 2284 | if (!(this+coverage).intersects (c->glyphs)) |
| 2285 | return; |
| 2286 | |
| 2287 | const ClassDef &class_def = this+classDef; |
| 2288 | |
| 2289 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 2290 | {intersects_class, intersected_class_glyphs}, |
| 2291 | ContextFormat::ClassBasedContext, |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 2292 | &class_def |
| 2293 | }; |
| 2294 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2295 | + hb_iter (ruleSet) |
| 2296 | | hb_map (hb_add (this)) |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 2297 | | hb_enumerate |
| 2298 | | hb_filter ([&] (const hb_pair_t<unsigned, const RuleSet &> p) |
| 2299 | { return class_def.intersects_class (c->glyphs, p.first); }) |
| 2300 | | hb_map (hb_second) |
| 2301 | | hb_apply ([&] (const RuleSet & _) |
| 2302 | { _.closure_lookups (c, lookup_context); }); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2303 | } |
| 2304 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 2305 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 2306 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2307 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2308 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 2309 | (this+coverage).collect_coverage (c->input); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2310 | |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 2311 | const ClassDef &class_def = this+classDef; |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2312 | struct ContextCollectGlyphsLookupContext lookup_context = { |
| 2313 | {collect_class}, |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 2314 | &class_def |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2315 | }; |
| 2316 | |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2317 | + hb_iter (ruleSet) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2318 | | hb_map (hb_add (this)) |
| 2319 | | hb_apply ([&] (const RuleSet &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2320 | ; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2321 | } |
| 2322 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2323 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2324 | { |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2325 | const ClassDef &class_def = this+classDef; |
Behdad Esfahbod | 2dc1141 | 2012-11-24 19:16:34 -0500 | [diff] [blame] | 2326 | unsigned int index = class_def.get_class (c->glyphs[0]); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2327 | const RuleSet &rule_set = this+ruleSet[index]; |
| 2328 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2329 | {match_class}, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2330 | &class_def |
| 2331 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 2332 | return rule_set.would_apply (c, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2333 | } |
| 2334 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 2335 | const Coverage &get_coverage () const { return this+coverage; } |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 2336 | |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2337 | unsigned cache_cost () const |
| 2338 | { |
| 2339 | unsigned c = (this+classDef).cost () * ruleSet.len; |
| 2340 | return c >= 4 ? c : 0; |
| 2341 | } |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 2342 | bool cache_func (hb_ot_apply_context_t *c, bool enter) const |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2343 | { |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 2344 | if (enter) |
| 2345 | { |
| 2346 | if (!HB_BUFFER_TRY_ALLOCATE_VAR (c->buffer, syllable)) |
| 2347 | return false; |
| 2348 | auto &info = c->buffer->info; |
| 2349 | unsigned count = c->buffer->len; |
| 2350 | for (unsigned i = 0; i < count; i++) |
| 2351 | info[i].syllable() = 255; |
| 2352 | c->new_syllables = 255; |
| 2353 | return true; |
| 2354 | } |
| 2355 | else |
| 2356 | { |
| 2357 | c->new_syllables = (unsigned) -1; |
| 2358 | HB_BUFFER_DEALLOCATE_VAR (c->buffer, syllable); |
| 2359 | return true; |
| 2360 | } |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2361 | } |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2362 | |
| 2363 | bool apply (hb_ot_apply_context_t *c, bool cached = false) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2364 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2365 | TRACE_APPLY (this); |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 2366 | unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2367 | if (likely (index == NOT_COVERED)) return_trace (false); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 2368 | |
| 2369 | const ClassDef &class_def = this+classDef; |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2370 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2371 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2372 | {cached ? match_class_cached : match_class}, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 2373 | &class_def |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2374 | }; |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2375 | |
| 2376 | if (cached && c->buffer->cur().syllable() < 255) |
| 2377 | index = c->buffer->cur().syllable (); |
| 2378 | else |
| 2379 | { |
| 2380 | index = class_def.get_class (c->buffer->cur().codepoint); |
| 2381 | if (cached && index < 255) |
| 2382 | c->buffer->cur().syllable() = index; |
| 2383 | } |
| 2384 | const RuleSet &rule_set = this+ruleSet[index]; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2385 | return_trace (rule_set.apply (c, lookup_context)); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2386 | } |
| 2387 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2388 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2389 | { |
| 2390 | TRACE_SUBSET (this); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2391 | auto *out = c->serializer->start_embed (*this); |
| 2392 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 2393 | out->format = format; |
| 2394 | if (unlikely (!out->coverage.serialize_subset (c, coverage, this))) |
| 2395 | return_trace (false); |
| 2396 | |
| 2397 | hb_map_t klass_map; |
| 2398 | out->classDef.serialize_subset (c, classDef, this, &klass_map); |
| 2399 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2400 | const hb_set_t* glyphset = c->plan->glyphset_gsub (); |
| 2401 | hb_set_t retained_coverage_glyphs; |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 2402 | (this+coverage).intersect_set (*glyphset, retained_coverage_glyphs); |
Garret Rieger | f51b48c | 2021-11-02 16:16:52 -0700 | [diff] [blame] | 2403 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2404 | hb_set_t coverage_glyph_classes; |
| 2405 | (this+classDef).intersected_classes (&retained_coverage_glyphs, &coverage_glyph_classes); |
| 2406 | |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2407 | const hb_map_t *lookup_map = c->table_tag == HB_OT_TAG_GSUB ? c->plan->gsub_lookups : c->plan->gpos_lookups; |
| 2408 | bool ret = true; |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2409 | int non_zero_index = -1, index = 0; |
Behdad Esfahbod | 6d94194 | 2021-02-19 17:08:10 -0700 | [diff] [blame] | 2410 | for (const auto& _ : + hb_enumerate (ruleSet) |
| 2411 | | hb_filter (klass_map, hb_first)) |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2412 | { |
| 2413 | auto *o = out->ruleSet.serialize_append (c->serializer); |
| 2414 | if (unlikely (!o)) |
| 2415 | { |
Ebrahim Byagowi | 5a7cc7f | 2020-07-29 08:33:32 +0430 | [diff] [blame] | 2416 | ret = false; |
| 2417 | break; |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2418 | } |
| 2419 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2420 | if (coverage_glyph_classes.has (_.first) && |
| 2421 | o->serialize_subset (c, _.second, this, lookup_map, &klass_map)) |
Ebrahim Byagowi | 5a7cc7f | 2020-07-29 08:33:32 +0430 | [diff] [blame] | 2422 | non_zero_index = index; |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2423 | |
| 2424 | index++; |
| 2425 | } |
| 2426 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2427 | if (!ret || non_zero_index == -1) return_trace (false); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2428 | |
| 2429 | //prune empty trailing ruleSets |
| 2430 | --index; |
| 2431 | while (index > non_zero_index) |
| 2432 | { |
| 2433 | out->ruleSet.pop (); |
| 2434 | index--; |
| 2435 | } |
| 2436 | |
| 2437 | return_trace (bool (out->ruleSet)); |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2440 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 2441 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2442 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2443 | return_trace (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2444 | } |
| 2445 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2446 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2447 | HBUINT16 format; /* Format identifier--format = 2 */ |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 2448 | typename Types::template OffsetTo<Coverage> |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2449 | coverage; /* Offset to Coverage table--from |
| 2450 | * beginning of table */ |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 2451 | typename Types::template OffsetTo<ClassDef> |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2452 | classDef; /* Offset to glyph ClassDef table--from |
| 2453 | * beginning of table */ |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 2454 | Array16Of<typename Types::template OffsetTo<RuleSet>> |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2455 | ruleSet; /* Array of RuleSet tables |
| 2456 | * ordered by class */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 2457 | public: |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 2458 | DEFINE_SIZE_ARRAY (4 + 2 * Types::size, ruleSet); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2459 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2460 | |
| 2461 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2462 | struct ContextFormat3 |
| 2463 | { |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2464 | using RuleSet = OT::RuleSet<SmallTypes>; |
| 2465 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2466 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2467 | { |
Behdad Esfahbod | fb05908 | 2018-11-30 20:45:40 -0500 | [diff] [blame] | 2468 | if (!(this+coverageZ[0]).intersects (glyphs)) |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2469 | return false; |
| 2470 | |
| 2471 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2472 | {intersects_coverage, intersected_coverage_glyphs}, |
| 2473 | ContextFormat::CoverageBasedContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2474 | this |
| 2475 | }; |
| 2476 | return context_intersects (glyphs, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2477 | glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2478 | lookup_context); |
| 2479 | } |
| 2480 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 2481 | bool may_have_non_1to1 () const |
| 2482 | { return true; } |
| 2483 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2484 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2485 | { |
Behdad Esfahbod | fb05908 | 2018-11-30 20:45:40 -0500 | [diff] [blame] | 2486 | if (!(this+coverageZ[0]).intersects (c->glyphs)) |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2487 | return; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2488 | |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 2489 | hb_set_t& cur_active_glyphs = c->push_cur_active_glyphs (); |
| 2490 | get_coverage ().intersect_set (c->previous_parent_active_glyphs (), |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 2491 | cur_active_glyphs); |
| 2492 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 2493 | |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 2494 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2495 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2496 | {intersects_coverage, intersected_coverage_glyphs}, |
| 2497 | ContextFormat::CoverageBasedContext, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2498 | this |
| 2499 | }; |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2500 | context_closure_lookup (c, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2501 | glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2502 | lookupCount, lookupRecord, |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2503 | 0, lookup_context); |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 2504 | |
| 2505 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2506 | } |
| 2507 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2508 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 2509 | { |
Garret Rieger | a5c0ec7 | 2020-09-25 14:57:20 -0700 | [diff] [blame] | 2510 | if (!intersects (c->glyphs)) |
| 2511 | return; |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2512 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
| 2513 | recurse_lookups (c, lookupCount, lookupRecord); |
| 2514 | } |
| 2515 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 2516 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 2517 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2518 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2519 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 2520 | (this+coverageZ[0]).collect_coverage (c->input); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2521 | |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 2522 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2523 | struct ContextCollectGlyphsLookupContext lookup_context = { |
| 2524 | {collect_coverage}, |
Behdad Esfahbod | e75943d | 2012-11-30 08:38:24 +0200 | [diff] [blame] | 2525 | this |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2526 | }; |
| 2527 | |
| 2528 | context_collect_glyphs_lookup (c, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2529 | glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2530 | lookupCount, lookupRecord, |
| 2531 | lookup_context); |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2532 | } |
| 2533 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2534 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2535 | { |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 2536 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2537 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2538 | {match_coverage}, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2539 | this |
| 2540 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 2541 | return context_would_apply_lookup (c, |
| 2542 | glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), |
| 2543 | lookupCount, lookupRecord, |
| 2544 | lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2545 | } |
| 2546 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 2547 | const Coverage &get_coverage () const { return this+coverageZ[0]; } |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 2548 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2549 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2550 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2551 | TRACE_APPLY (this); |
Behdad Esfahbod | fb05908 | 2018-11-30 20:45:40 -0500 | [diff] [blame] | 2552 | unsigned int index = (this+coverageZ[0]).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2553 | if (likely (index == NOT_COVERED)) return_trace (false); |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 2554 | |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 2555 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2556 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2557 | {match_coverage}, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 2558 | this |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2559 | }; |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2560 | return_trace (context_apply_lookup (c, glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), lookupCount, lookupRecord, lookup_context)); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2561 | } |
| 2562 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2563 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2564 | { |
| 2565 | TRACE_SUBSET (this); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2566 | auto *out = c->serializer->start_embed (this); |
| 2567 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 2568 | |
| 2569 | out->format = format; |
| 2570 | out->glyphCount = glyphCount; |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2571 | |
Behdad Esfahbod | 26111a1 | 2020-06-28 02:59:47 -0700 | [diff] [blame] | 2572 | auto coverages = coverageZ.as_array (glyphCount); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2573 | |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 2574 | for (const Offset16To<Coverage>& offset : coverages) |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2575 | { |
Behdad Esfahbod | 6d94194 | 2021-02-19 17:08:10 -0700 | [diff] [blame] | 2576 | /* TODO(subset) This looks like should not be necessary to write this way. */ |
Behdad Esfahbod | 9b4b584 | 2021-03-31 13:27:21 -0600 | [diff] [blame] | 2577 | auto *o = c->serializer->allocate_size<Offset16To<Coverage>> (Offset16To<Coverage>::static_size); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2578 | if (unlikely (!o)) return_trace (false); |
| 2579 | if (!o->serialize_subset (c, offset, this)) return_trace (false); |
| 2580 | } |
| 2581 | |
Behdad Esfahbod | 5192294 | 2022-07-08 14:00:24 -0600 | [diff] [blame] | 2582 | const auto& lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> (coverageZ.as_array (glyphCount)); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2583 | const hb_map_t *lookup_map = c->table_tag == HB_OT_TAG_GSUB ? c->plan->gsub_lookups : c->plan->gpos_lookups; |
Garret Rieger | f51b48c | 2021-11-02 16:16:52 -0700 | [diff] [blame] | 2584 | |
Ebrahim Byagowi | a79d0e4 | 2020-05-21 07:32:58 +0430 | [diff] [blame] | 2585 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2586 | unsigned count = serialize_lookuprecord_array (c->serializer, lookupRecord.as_array (lookupCount), lookup_map); |
| 2587 | return_trace (c->serializer->check_assign (out->lookupCount, count, HB_SERIALIZE_ERROR_INT_OVERFLOW)); |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2588 | } |
| 2589 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2590 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 2591 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2592 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2593 | if (!c->check_struct (this)) return_trace (false); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2594 | unsigned int count = glyphCount; |
Behdad Esfahbod | fb05908 | 2018-11-30 20:45:40 -0500 | [diff] [blame] | 2595 | if (!count) return_trace (false); /* We want to access coverageZ[0] freely. */ |
Behdad Esfahbod | 9507b05 | 2018-09-10 23:18:07 +0200 | [diff] [blame] | 2596 | if (!c->check_array (coverageZ.arrayZ, count)) return_trace (false); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2597 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2598 | if (!coverageZ[i].sanitize (c, this)) return_trace (false); |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 2599 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
Behdad Esfahbod | 9507b05 | 2018-09-10 23:18:07 +0200 | [diff] [blame] | 2600 | return_trace (c->check_array (lookupRecord, lookupCount)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2601 | } |
| 2602 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2603 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2604 | HBUINT16 format; /* Format identifier--format = 3 */ |
| 2605 | HBUINT16 glyphCount; /* Number of glyphs in the input glyph |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2606 | * sequence */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2607 | HBUINT16 lookupCount; /* Number of LookupRecords */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 2608 | UnsizedArrayOf<Offset16To<Coverage>> |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2609 | coverageZ; /* Array of offsets to Coverage |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 2610 | * table in glyph sequence order */ |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2611 | /*UnsizedArrayOf<LookupRecord> |
| 2612 | lookupRecordX;*/ /* Array of LookupRecords--in |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2613 | * design order */ |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 2614 | public: |
Behdad Esfahbod | 6d7c6e1 | 2018-02-07 14:09:56 -0600 | [diff] [blame] | 2615 | DEFINE_SIZE_ARRAY (6, coverageZ); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2616 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2617 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2618 | struct Context |
| 2619 | { |
Behdad Esfahbod | 36bb24f | 2019-05-05 10:14:17 -0700 | [diff] [blame] | 2620 | template <typename context_t, typename ...Ts> |
Behdad Esfahbod | 83e3eab | 2019-05-07 20:58:43 -0700 | [diff] [blame] | 2621 | typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2622 | { |
Behdad Esfahbod | 00f6a8e | 2014-12-12 20:36:49 -0800 | [diff] [blame] | 2623 | TRACE_DISPATCH (this, u.format); |
Behdad Esfahbod | f396fbb | 2015-10-09 12:25:55 -0400 | [diff] [blame] | 2624 | if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ()); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2625 | switch (u.format) { |
Behdad Esfahbod | 6d555ce | 2021-11-02 00:18:22 -0600 | [diff] [blame] | 2626 | case 1: return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...)); |
| 2627 | case 2: return_trace (c->dispatch (u.format2, std::forward<Ts> (ds)...)); |
| 2628 | case 3: return_trace (c->dispatch (u.format3, std::forward<Ts> (ds)...)); |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 2629 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 1bf8fa2 | 2022-07-08 14:22:21 -0600 | [diff] [blame] | 2630 | case 4: return_trace (c->dispatch (u.format4, std::forward<Ts> (ds)...)); |
Behdad Esfahbod | d1f58e5 | 2022-07-08 13:25:07 -0600 | [diff] [blame] | 2631 | case 5: return_trace (c->dispatch (u.format5, std::forward<Ts> (ds)...)); |
| 2632 | #endif |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2633 | default:return_trace (c->default_return_value ()); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2634 | } |
| 2635 | } |
| 2636 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2637 | protected: |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2638 | union { |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 2639 | HBUINT16 format; /* Format identifier */ |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2640 | ContextFormat1_4<SmallTypes> format1; |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 2641 | ContextFormat2_5<SmallTypes> format2; |
| 2642 | ContextFormat3 format3; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 2643 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 1bf8fa2 | 2022-07-08 14:22:21 -0600 | [diff] [blame] | 2644 | ContextFormat1_4<MediumTypes> format4; |
Behdad Esfahbod | d1f58e5 | 2022-07-08 13:25:07 -0600 | [diff] [blame] | 2645 | ContextFormat2_5<MediumTypes> format5; |
| 2646 | #endif |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2647 | } u; |
| 2648 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2649 | |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2650 | |
| 2651 | /* Chaining Contextual lookups */ |
| 2652 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2653 | struct ChainContextClosureLookupContext |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2654 | { |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2655 | ContextClosureFuncs funcs; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2656 | ContextFormat context_format; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2657 | const void *intersects_data[3]; |
| 2658 | }; |
| 2659 | |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2660 | struct ChainContextCollectGlyphsLookupContext |
| 2661 | { |
| 2662 | ContextCollectGlyphsFuncs funcs; |
| 2663 | const void *collect_data[3]; |
| 2664 | }; |
| 2665 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2666 | struct ChainContextApplyLookupContext |
| 2667 | { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2668 | ChainContextApplyFuncs funcs; |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 2669 | const void *match_data[3]; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 2670 | }; |
| 2671 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2672 | template <typename HBUINT> |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2673 | static inline bool chain_context_intersects (const hb_set_t *glyphs, |
| 2674 | unsigned int backtrackCount, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2675 | const HBUINT backtrack[], |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2676 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2677 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2678 | unsigned int lookaheadCount, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2679 | const HBUINT lookahead[], |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2680 | ChainContextClosureLookupContext &lookup_context) |
| 2681 | { |
Qunxin Liu | 44d88cf | 2020-05-08 15:33:34 -0700 | [diff] [blame] | 2682 | return array_is_subset_of (glyphs, |
| 2683 | backtrackCount, backtrack, |
| 2684 | lookup_context.funcs.intersects, lookup_context.intersects_data[0]) |
| 2685 | && array_is_subset_of (glyphs, |
| 2686 | inputCount ? inputCount - 1 : 0, input, |
| 2687 | lookup_context.funcs.intersects, lookup_context.intersects_data[1]) |
| 2688 | && array_is_subset_of (glyphs, |
| 2689 | lookaheadCount, lookahead, |
| 2690 | lookup_context.funcs.intersects, lookup_context.intersects_data[2]); |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2691 | } |
| 2692 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2693 | template <typename HBUINT> |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2694 | static inline void chain_context_closure_lookup (hb_closure_context_t *c, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2695 | unsigned int backtrackCount, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2696 | const HBUINT backtrack[], |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2697 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2698 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2699 | unsigned int lookaheadCount, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2700 | const HBUINT lookahead[], |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2701 | unsigned int lookupCount, |
| 2702 | const LookupRecord lookupRecord[], |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2703 | unsigned value, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2704 | ChainContextClosureLookupContext &lookup_context) |
| 2705 | { |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2706 | if (chain_context_intersects (c->glyphs, |
| 2707 | backtrackCount, backtrack, |
| 2708 | inputCount, input, |
| 2709 | lookaheadCount, lookahead, |
| 2710 | lookup_context)) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2711 | context_closure_recurse_lookups (c, |
| 2712 | inputCount, input, |
| 2713 | lookupCount, lookupRecord, |
| 2714 | value, |
| 2715 | lookup_context.context_format, |
| 2716 | lookup_context.intersects_data[1], |
| 2717 | lookup_context.funcs.intersected_glyphs); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2718 | } |
| 2719 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2720 | template <typename HBUINT> |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2721 | static inline void chain_context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c, |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 2722 | unsigned int backtrackCount, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2723 | const HBUINT backtrack[], |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 2724 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2725 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 2726 | unsigned int lookaheadCount, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2727 | const HBUINT lookahead[], |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 2728 | unsigned int lookupCount, |
| 2729 | const LookupRecord lookupRecord[], |
| 2730 | ChainContextCollectGlyphsLookupContext &lookup_context) |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2731 | { |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 2732 | collect_array (c, c->before, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2733 | backtrackCount, backtrack, |
| 2734 | lookup_context.funcs.collect, lookup_context.collect_data[0]); |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 2735 | collect_array (c, c->input, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2736 | inputCount ? inputCount - 1 : 0, input, |
| 2737 | lookup_context.funcs.collect, lookup_context.collect_data[1]); |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 2738 | collect_array (c, c->after, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2739 | lookaheadCount, lookahead, |
| 2740 | lookup_context.funcs.collect, lookup_context.collect_data[2]); |
| 2741 | recurse_lookups (c, |
| 2742 | lookupCount, lookupRecord); |
| 2743 | } |
| 2744 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2745 | template <typename HBUINT> |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2746 | static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c, |
| 2747 | unsigned int backtrackCount, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2748 | const HBUINT backtrack[] HB_UNUSED, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2749 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2750 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2751 | unsigned int lookaheadCount, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2752 | const HBUINT lookahead[] HB_UNUSED, |
Behdad Esfahbod | 0beb66e | 2012-12-05 18:46:04 -0500 | [diff] [blame] | 2753 | unsigned int lookupCount HB_UNUSED, |
| 2754 | const LookupRecord lookupRecord[] HB_UNUSED, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2755 | ChainContextApplyLookupContext &lookup_context) |
| 2756 | { |
Behdad Esfahbod | d9b204d | 2012-08-23 16:22:28 -0400 | [diff] [blame] | 2757 | return (c->zero_context ? !backtrackCount && !lookaheadCount : true) |
Behdad Esfahbod | 1f2bb17 | 2012-08-23 16:10:37 -0400 | [diff] [blame] | 2758 | && would_match_input (c, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2759 | inputCount, input, |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2760 | lookup_context.funcs.match[1], lookup_context.match_data[1]); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2761 | } |
| 2762 | |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2763 | template <typename HBUINT> |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 2764 | static inline bool chain_context_apply_lookup (hb_ot_apply_context_t *c, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2765 | unsigned int backtrackCount, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2766 | const HBUINT backtrack[], |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2767 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2768 | const HBUINT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2769 | unsigned int lookaheadCount, |
Behdad Esfahbod | 9d0e9fa | 2022-07-06 17:49:07 -0600 | [diff] [blame] | 2770 | const HBUINT lookahead[], |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2771 | unsigned int lookupCount, |
| 2772 | const LookupRecord lookupRecord[], |
| 2773 | ChainContextApplyLookupContext &lookup_context) |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 2774 | { |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2775 | unsigned end_index = c->buffer->idx; |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 2776 | unsigned match_end = 0; |
| 2777 | unsigned match_positions[HB_MAX_CONTEXT_LENGTH]; |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2778 | if (!(match_input (c, |
| 2779 | inputCount, input, |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2780 | lookup_context.funcs.match[1], lookup_context.match_data[1], |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2781 | &match_end, match_positions) && (end_index = match_end) |
| 2782 | && match_lookahead (c, |
| 2783 | lookaheadCount, lookahead, |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2784 | lookup_context.funcs.match[2], lookup_context.match_data[2], |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2785 | match_end, &end_index))) |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 2786 | { |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2787 | c->buffer->unsafe_to_concat (c->buffer->idx, end_index); |
| 2788 | return false; |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 2789 | } |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2790 | |
| 2791 | unsigned start_index = c->buffer->out_len; |
| 2792 | if (!match_backtrack (c, |
| 2793 | backtrackCount, backtrack, |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 2794 | lookup_context.funcs.match[0], lookup_context.match_data[0], |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2795 | &start_index)) |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 2796 | { |
| 2797 | c->buffer->unsafe_to_concat_from_outbuffer (start_index, end_index); |
| 2798 | return false; |
| 2799 | } |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2800 | |
| 2801 | c->buffer->unsafe_to_break_from_outbuffer (start_index, end_index); |
| 2802 | apply_lookup (c, |
| 2803 | inputCount, match_positions, |
| 2804 | lookupCount, lookupRecord, |
| 2805 | match_end); |
| 2806 | return true; |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 2807 | } |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 2808 | |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2809 | template <typename Types> |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2810 | struct ChainRule |
| 2811 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2812 | bool intersects (const hb_set_t *glyphs, ChainContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2813 | { |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2814 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
| 2815 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2816 | return chain_context_intersects (glyphs, |
| 2817 | backtrack.len, backtrack.arrayZ, |
Behdad Esfahbod | effc7ce | 2018-09-13 20:21:54 +0200 | [diff] [blame] | 2818 | input.lenP1, input.arrayZ, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2819 | lookahead.len, lookahead.arrayZ, |
| 2820 | lookup_context); |
| 2821 | } |
| 2822 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2823 | void closure (hb_closure_context_t *c, unsigned value, |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2824 | ChainContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2825 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 2826 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 2827 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2828 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
| 2829 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
| 2830 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2831 | chain_context_closure_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2832 | backtrack.len, backtrack.arrayZ, |
Behdad Esfahbod | effc7ce | 2018-09-13 20:21:54 +0200 | [diff] [blame] | 2833 | input.lenP1, input.arrayZ, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2834 | lookahead.len, lookahead.arrayZ, |
| 2835 | lookup.len, lookup.arrayZ, |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2836 | value, |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2837 | lookup_context); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2838 | } |
| 2839 | |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 2840 | void closure_lookups (hb_closure_lookups_context_t *c, |
| 2841 | ChainContextClosureLookupContext &lookup_context) const |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2842 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 2843 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 2844 | if (!intersects (c->glyphs, lookup_context)) return; |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 2845 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2846 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
| 2847 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
| 2848 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2849 | recurse_lookups (c, lookup.len, lookup.arrayZ); |
| 2850 | } |
| 2851 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2852 | void collect_glyphs (hb_collect_glyphs_context_t *c, |
| 2853 | ChainContextCollectGlyphsLookupContext &lookup_context) const |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2854 | { |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2855 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
| 2856 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
| 2857 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2858 | chain_context_collect_glyphs_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2859 | backtrack.len, backtrack.arrayZ, |
Behdad Esfahbod | effc7ce | 2018-09-13 20:21:54 +0200 | [diff] [blame] | 2860 | input.lenP1, input.arrayZ, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2861 | lookahead.len, lookahead.arrayZ, |
| 2862 | lookup.len, lookup.arrayZ, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2863 | lookup_context); |
| 2864 | } |
| 2865 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2866 | bool would_apply (hb_would_apply_context_t *c, |
| 2867 | ChainContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2868 | { |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2869 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
| 2870 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
| 2871 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 2872 | return chain_context_would_apply_lookup (c, |
| 2873 | backtrack.len, backtrack.arrayZ, |
| 2874 | input.lenP1, input.arrayZ, |
| 2875 | lookahead.len, lookahead.arrayZ, lookup.len, |
| 2876 | lookup.arrayZ, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2877 | } |
| 2878 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2879 | bool apply (hb_ot_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2880 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2881 | TRACE_APPLY (this); |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2882 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
| 2883 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
| 2884 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2885 | return_trace (chain_context_apply_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2886 | backtrack.len, backtrack.arrayZ, |
Behdad Esfahbod | effc7ce | 2018-09-13 20:21:54 +0200 | [diff] [blame] | 2887 | input.lenP1, input.arrayZ, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2888 | lookahead.len, lookahead.arrayZ, lookup.len, |
| 2889 | lookup.arrayZ, lookup_context)); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 2890 | } |
| 2891 | |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2892 | template<typename Iterator, |
| 2893 | hb_requires (hb_is_iterator (Iterator))> |
| 2894 | void serialize_array (hb_serialize_context_t *c, |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2895 | HBUINT16 len, |
| 2896 | Iterator it) const |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2897 | { |
| 2898 | c->copy (len); |
| 2899 | for (const auto g : it) |
Behdad Esfahbod | 83b66bf | 2021-02-23 13:04:25 -0700 | [diff] [blame] | 2900 | c->copy ((HBUINT16) g); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2901 | } |
| 2902 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2903 | bool serialize (hb_serialize_context_t *c, |
| 2904 | const hb_map_t *lookup_map, |
| 2905 | const hb_map_t *backtrack_map, |
| 2906 | const hb_map_t *input_map = nullptr, |
| 2907 | const hb_map_t *lookahead_map = nullptr) const |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2908 | { |
| 2909 | TRACE_SERIALIZE (this); |
| 2910 | auto *out = c->start_embed (this); |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2911 | if (unlikely (!out)) return_trace (false); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2912 | |
| 2913 | const hb_map_t *mapping = backtrack_map; |
| 2914 | serialize_array (c, backtrack.len, + backtrack.iter () |
| 2915 | | hb_map (mapping)); |
| 2916 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2917 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2918 | if (input_map) mapping = input_map; |
| 2919 | serialize_array (c, input.lenP1, + input.iter () |
| 2920 | | hb_map (mapping)); |
| 2921 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2922 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2923 | if (lookahead_map) mapping = lookahead_map; |
| 2924 | serialize_array (c, lookahead.len, + lookahead.iter () |
| 2925 | | hb_map (mapping)); |
| 2926 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2927 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 2928 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2929 | HBUINT16* lookupCount = c->embed (&(lookup.len)); |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2930 | if (!lookupCount) return_trace (false); |
Garret Rieger | 8f47dd5 | 2020-11-04 11:05:22 -0800 | [diff] [blame] | 2931 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2932 | unsigned count = serialize_lookuprecord_array (c, lookup.as_array (), lookup_map); |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2933 | return_trace (c->check_assign (*lookupCount, count, HB_SERIALIZE_ERROR_INT_OVERFLOW)); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | bool subset (hb_subset_context_t *c, |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 2937 | const hb_map_t *lookup_map, |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2938 | const hb_map_t *backtrack_map = nullptr, |
| 2939 | const hb_map_t *input_map = nullptr, |
| 2940 | const hb_map_t *lookahead_map = nullptr) const |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2941 | { |
| 2942 | TRACE_SUBSET (this); |
| 2943 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2944 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
| 2945 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
Qunxin Liu | b2fcca6 | 2019-10-24 15:15:26 -0700 | [diff] [blame] | 2946 | |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2947 | if (!backtrack_map) |
| 2948 | { |
Garret Rieger | e583505 | 2020-09-29 11:05:08 -0700 | [diff] [blame] | 2949 | const hb_set_t &glyphset = *c->plan->glyphset_gsub (); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2950 | if (!hb_all (backtrack, glyphset) || |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2951 | !hb_all (input, glyphset) || |
| 2952 | !hb_all (lookahead, glyphset)) |
| 2953 | return_trace (false); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2954 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2955 | serialize (c->serializer, lookup_map, c->plan->glyph_map); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2956 | } |
| 2957 | else |
| 2958 | { |
| 2959 | if (!hb_all (backtrack, backtrack_map) || |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2960 | !hb_all (input, input_map) || |
| 2961 | !hb_all (lookahead, lookahead_map)) |
| 2962 | return_trace (false); |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 2963 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2964 | serialize (c->serializer, lookup_map, backtrack_map, input_map, lookahead_map); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | return_trace (true); |
| 2968 | } |
| 2969 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2970 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 2971 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2972 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2973 | if (!backtrack.sanitize (c)) return_trace (false); |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2974 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2975 | if (!input.sanitize (c)) return_trace (false); |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2976 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2977 | if (!lookahead.sanitize (c)) return_trace (false); |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 2978 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2979 | return_trace (lookup.sanitize (c)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2980 | } |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2981 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2982 | protected: |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2983 | Array16Of<typename Types::HBUINT> |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 2984 | backtrack; /* Array of backtracking values |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2985 | * (to be matched before the input |
| 2986 | * sequence) */ |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2987 | HeadlessArrayOf<typename Types::HBUINT> |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 2988 | inputX; /* Array of input values (start with |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2989 | * second glyph) */ |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 2990 | Array16Of<typename Types::HBUINT> |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 2991 | lookaheadX; /* Array of lookahead values's (to be |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 2992 | * matched after the input sequence) */ |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2993 | Array16Of<LookupRecord> |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 2994 | lookupX; /* Array of LookupRecords--in |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2995 | * design order) */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 2996 | public: |
Behdad Esfahbod | bea34c7 | 2010-05-10 17:28:16 -0400 | [diff] [blame] | 2997 | DEFINE_SIZE_MIN (8); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2998 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2999 | |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 3000 | template <typename Types> |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3001 | struct ChainRuleSet |
| 3002 | { |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 3003 | using ChainRule = OT::ChainRule<Types>; |
| 3004 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3005 | bool intersects (const hb_set_t *glyphs, ChainContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3006 | { |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3007 | return |
| 3008 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 3009 | | hb_map (hb_add (this)) |
| 3010 | | hb_map ([&] (const ChainRule &_) { return _.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3011 | | hb_any |
| 3012 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3013 | } |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3014 | void closure (hb_closure_context_t *c, unsigned value, ChainContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3015 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 3016 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 3017 | |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3018 | return |
| 3019 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 3020 | | hb_map (hb_add (this)) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3021 | | hb_apply ([&] (const ChainRule &_) { _.closure (c, value, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3022 | ; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3023 | } |
| 3024 | |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3025 | void closure_lookups (hb_closure_lookups_context_t *c, |
| 3026 | ChainContextClosureLookupContext &lookup_context) const |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3027 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 3028 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 3029 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3030 | + hb_iter (rule) |
| 3031 | | hb_map (hb_add (this)) |
Garret Rieger | e31c269 | 2020-09-28 16:51:25 -0700 | [diff] [blame] | 3032 | | hb_apply ([&] (const ChainRule &_) { _.closure_lookups (c, lookup_context); }) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3033 | ; |
| 3034 | } |
| 3035 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3036 | void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3037 | { |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3038 | return |
| 3039 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 3040 | | hb_map (hb_add (this)) |
| 3041 | | hb_apply ([&] (const ChainRule &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3042 | ; |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3043 | } |
| 3044 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3045 | bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3046 | { |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3047 | return |
| 3048 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 3049 | | hb_map (hb_add (this)) |
| 3050 | | hb_map ([&] (const ChainRule &_) { return _.would_apply (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3051 | | hb_any |
| 3052 | ; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3053 | } |
| 3054 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3055 | bool apply (hb_ot_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3056 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3057 | TRACE_APPLY (this); |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3058 | return_trace ( |
| 3059 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 3060 | | hb_map (hb_add (this)) |
| 3061 | | hb_map ([&] (const ChainRule &_) { return _.apply (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3062 | | hb_any |
| 3063 | ) |
| 3064 | ; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 3065 | } |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3066 | |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3067 | bool subset (hb_subset_context_t *c, |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 3068 | const hb_map_t *lookup_map, |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 3069 | const hb_map_t *backtrack_klass_map = nullptr, |
| 3070 | const hb_map_t *input_klass_map = nullptr, |
| 3071 | const hb_map_t *lookahead_klass_map = nullptr) const |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3072 | { |
| 3073 | TRACE_SUBSET (this); |
| 3074 | |
| 3075 | auto snap = c->serializer->snapshot (); |
| 3076 | auto *out = c->serializer->start_embed (*this); |
| 3077 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 3078 | |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3079 | for (const Offset16To<ChainRule>& _ : rule) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3080 | { |
| 3081 | if (!_) continue; |
Garret Rieger | c2cc566 | 2021-09-22 14:15:55 -0700 | [diff] [blame] | 3082 | auto o_snap = c->serializer->snapshot (); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3083 | auto *o = out->rule.serialize_append (c->serializer); |
| 3084 | if (unlikely (!o)) continue; |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 3085 | |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3086 | if (!o->serialize_subset (c, _, this, |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 3087 | lookup_map, |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 3088 | backtrack_klass_map, |
| 3089 | input_klass_map, |
| 3090 | lookahead_klass_map)) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3091 | { |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 3092 | out->rule.pop (); |
| 3093 | c->serializer->revert (o_snap); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | bool ret = bool (out->rule); |
| 3098 | if (!ret) c->serializer->revert (snap); |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 3099 | |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3100 | return_trace (ret); |
| 3101 | } |
| 3102 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3103 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 3104 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3105 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3106 | return_trace (rule.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3107 | } |
| 3108 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3109 | protected: |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3110 | Array16OfOffset16To<ChainRule> |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 3111 | rule; /* Array of ChainRule tables |
| 3112 | * ordered by preference */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 3113 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 3114 | DEFINE_SIZE_ARRAY (2, rule); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3115 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3116 | |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 3117 | template <typename Types> |
| 3118 | struct ChainContextFormat1_4 |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3119 | { |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 3120 | using ChainRuleSet = OT::ChainRuleSet<Types>; |
| 3121 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3122 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3123 | { |
| 3124 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3125 | {intersects_glyph, intersected_glyph}, |
| 3126 | ContextFormat::SimpleContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3127 | {nullptr, nullptr, nullptr} |
| 3128 | }; |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3129 | |
| 3130 | return |
| 3131 | + hb_zip (this+coverage, ruleSet) |
| 3132 | | hb_filter (*glyphs, hb_first) |
| 3133 | | hb_map (hb_second) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 3134 | | hb_map (hb_add (this)) |
| 3135 | | hb_map ([&] (const ChainRuleSet &_) { return _.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3136 | | hb_any |
| 3137 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3138 | } |
| 3139 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 3140 | bool may_have_non_1to1 () const |
| 3141 | { return true; } |
| 3142 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3143 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3144 | { |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 3145 | hb_set_t& cur_active_glyphs = c->push_cur_active_glyphs (); |
| 3146 | get_coverage ().intersect_set (c->previous_parent_active_glyphs (), |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 3147 | cur_active_glyphs); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 3148 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3149 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3150 | {intersects_glyph, intersected_glyph}, |
| 3151 | ContextFormat::SimpleContext, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 3152 | {nullptr, nullptr, nullptr} |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3153 | }; |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3154 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3155 | + hb_zip (this+coverage, hb_range ((unsigned) ruleSet.len)) |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 3156 | | hb_filter ([&] (hb_codepoint_t _) { |
| 3157 | return c->previous_parent_active_glyphs ().has (_); |
| 3158 | }, hb_first) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3159 | | hb_map ([&](const hb_pair_t<hb_codepoint_t, unsigned> _) { return hb_pair_t<unsigned, const ChainRuleSet&> (_.first, this+ruleSet[_.second]); }) |
| 3160 | | hb_apply ([&] (const hb_pair_t<unsigned, const ChainRuleSet&>& _) { _.second.closure (c, _.first, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3161 | ; |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 3162 | |
| 3163 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3164 | } |
| 3165 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3166 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 3167 | { |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3168 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 3169 | {intersects_glyph, intersected_glyph}, |
| 3170 | ContextFormat::SimpleContext, |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3171 | {nullptr, nullptr, nullptr} |
| 3172 | }; |
| 3173 | |
| 3174 | + hb_zip (this+coverage, ruleSet) |
| 3175 | | hb_filter (*c->glyphs, hb_first) |
| 3176 | | hb_map (hb_second) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3177 | | hb_map (hb_add (this)) |
Garret Rieger | e31c269 | 2020-09-28 16:51:25 -0700 | [diff] [blame] | 3178 | | hb_apply ([&] (const ChainRuleSet &_) { _.closure_lookups (c, lookup_context); }) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3179 | ; |
| 3180 | } |
| 3181 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 3182 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 3183 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3184 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 3185 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 3186 | (this+coverage).collect_coverage (c->input); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3187 | |
| 3188 | struct ChainContextCollectGlyphsLookupContext lookup_context = { |
| 3189 | {collect_glyph}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 3190 | {nullptr, nullptr, nullptr} |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3191 | }; |
| 3192 | |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3193 | + hb_iter (ruleSet) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 3194 | | hb_map (hb_add (this)) |
| 3195 | | hb_apply ([&] (const ChainRuleSet &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3196 | ; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 3197 | } |
| 3198 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3199 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3200 | { |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 3201 | const ChainRuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])]; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3202 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3203 | {{match_glyph, match_glyph, match_glyph}}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 3204 | {nullptr, nullptr, nullptr} |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3205 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 3206 | return rule_set.would_apply (c, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3207 | } |
| 3208 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3209 | const Coverage &get_coverage () const { return this+coverage; } |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 3210 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3211 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3212 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3213 | TRACE_APPLY (this); |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 3214 | unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3215 | if (likely (index == NOT_COVERED)) return_trace (false); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3216 | |
| 3217 | const ChainRuleSet &rule_set = this+ruleSet[index]; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3218 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3219 | {{match_glyph, match_glyph, match_glyph}}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 3220 | {nullptr, nullptr, nullptr} |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3221 | }; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3222 | return_trace (rule_set.apply (c, lookup_context)); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3223 | } |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3224 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3225 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 3226 | { |
| 3227 | TRACE_SUBSET (this); |
Garret Rieger | e583505 | 2020-09-29 11:05:08 -0700 | [diff] [blame] | 3228 | const hb_set_t &glyphset = *c->plan->glyphset_gsub (); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3229 | const hb_map_t &glyph_map = *c->plan->glyph_map; |
| 3230 | |
| 3231 | auto *out = c->serializer->start_embed (*this); |
| 3232 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 3233 | out->format = format; |
| 3234 | |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 3235 | const hb_map_t *lookup_map = c->table_tag == HB_OT_TAG_GSUB ? c->plan->gsub_lookups : c->plan->gpos_lookups; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3236 | hb_sorted_vector_t<hb_codepoint_t> new_coverage; |
| 3237 | + hb_zip (this+coverage, ruleSet) |
| 3238 | | hb_filter (glyphset, hb_first) |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 3239 | | hb_filter (subset_offset_array (c, out->ruleSet, this, lookup_map), hb_second) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3240 | | hb_map (hb_first) |
| 3241 | | hb_map (glyph_map) |
| 3242 | | hb_sink (new_coverage) |
| 3243 | ; |
| 3244 | |
Garret Rieger | 085aa65 | 2021-06-14 16:47:45 -0700 | [diff] [blame] | 3245 | out->coverage.serialize_serialize (c->serializer, new_coverage.iter ()); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3246 | return_trace (bool (new_coverage)); |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 3247 | } |
| 3248 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3249 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 3250 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3251 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3252 | return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3253 | } |
| 3254 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3255 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 3256 | HBUINT16 format; /* Format identifier--format = 1 */ |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 3257 | typename Types::template OffsetTo<Coverage> |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 3258 | coverage; /* Offset to Coverage table--from |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3259 | * beginning of table */ |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 3260 | Array16Of<typename Types::template OffsetTo<ChainRuleSet>> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3261 | ruleSet; /* Array of ChainRuleSet tables |
| 3262 | * ordered by Coverage Index */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 3263 | public: |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 3264 | DEFINE_SIZE_ARRAY (2 + 2 * Types::size, ruleSet); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3265 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3266 | |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3267 | template <typename Types> |
| 3268 | struct ChainContextFormat2_5 |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3269 | { |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 3270 | using ChainRuleSet = OT::ChainRuleSet<SmallTypes>; |
| 3271 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3272 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3273 | { |
| 3274 | if (!(this+coverage).intersects (glyphs)) |
| 3275 | return false; |
| 3276 | |
| 3277 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 3278 | const ClassDef &input_class_def = this+inputClassDef; |
| 3279 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 3280 | |
| 3281 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3282 | {intersects_class, intersected_class_glyphs}, |
| 3283 | ContextFormat::ClassBasedContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3284 | {&backtrack_class_def, |
| 3285 | &input_class_def, |
| 3286 | &lookahead_class_def} |
| 3287 | }; |
| 3288 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3289 | hb_set_t retained_coverage_glyphs; |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 3290 | (this+coverage).intersect_set (*glyphs, retained_coverage_glyphs); |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3291 | |
| 3292 | hb_set_t coverage_glyph_classes; |
| 3293 | input_class_def.intersected_classes (&retained_coverage_glyphs, &coverage_glyph_classes); |
| 3294 | |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 3295 | return |
Behdad Esfahbod | 26111a1 | 2020-06-28 02:59:47 -0700 | [diff] [blame] | 3296 | + hb_iter (ruleSet) |
| 3297 | | hb_map (hb_add (this)) |
| 3298 | | hb_enumerate |
| 3299 | | hb_map ([&] (const hb_pair_t<unsigned, const ChainRuleSet &> p) |
Behdad Esfahbod | 78d35f0 | 2019-05-15 18:15:05 -0700 | [diff] [blame] | 3300 | { return input_class_def.intersects_class (glyphs, p.first) && |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3301 | coverage_glyph_classes.has (p.first) && |
Behdad Esfahbod | 26111a1 | 2020-06-28 02:59:47 -0700 | [diff] [blame] | 3302 | p.second.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 3303 | | hb_any |
| 3304 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3305 | } |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 3306 | |
| 3307 | bool may_have_non_1to1 () const |
| 3308 | { return true; } |
| 3309 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3310 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3311 | { |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3312 | if (!(this+coverage).intersects (c->glyphs)) |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 3313 | return; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3314 | |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 3315 | hb_set_t& cur_active_glyphs = c->push_cur_active_glyphs (); |
| 3316 | get_coverage ().intersect_set (c->previous_parent_active_glyphs (), |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 3317 | cur_active_glyphs); |
| 3318 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 3319 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3320 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 3321 | const ClassDef &input_class_def = this+inputClassDef; |
| 3322 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 3323 | |
| 3324 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3325 | {intersects_class, intersected_class_glyphs}, |
| 3326 | ContextFormat::ClassBasedContext, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3327 | {&backtrack_class_def, |
| 3328 | &input_class_def, |
| 3329 | &lookahead_class_def} |
| 3330 | }; |
| 3331 | |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 3332 | + hb_enumerate (ruleSet) |
Behdad Esfahbod | 78d35f0 | 2019-05-15 18:15:05 -0700 | [diff] [blame] | 3333 | | hb_filter ([&] (unsigned _) |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 3334 | { return input_class_def.intersects_class (&c->parent_active_glyphs (), _); }, |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 3335 | hb_first) |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3336 | | hb_apply ([&] (const hb_pair_t<unsigned, const typename Types::template OffsetTo<ChainRuleSet>&> _) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3337 | { |
| 3338 | const ChainRuleSet& chainrule_set = this+_.second; |
| 3339 | chainrule_set.closure (c, _.first, lookup_context); |
| 3340 | }) |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 3341 | ; |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 3342 | |
| 3343 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3344 | } |
| 3345 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3346 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 3347 | { |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3348 | if (!(this+coverage).intersects (c->glyphs)) |
| 3349 | return; |
| 3350 | |
| 3351 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 3352 | const ClassDef &input_class_def = this+inputClassDef; |
| 3353 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 3354 | |
| 3355 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 3356 | {intersects_class, intersected_class_glyphs}, |
| 3357 | ContextFormat::ClassBasedContext, |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3358 | {&backtrack_class_def, |
| 3359 | &input_class_def, |
| 3360 | &lookahead_class_def} |
| 3361 | }; |
| 3362 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3363 | + hb_iter (ruleSet) |
| 3364 | | hb_map (hb_add (this)) |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3365 | | hb_enumerate |
Behdad Esfahbod | 6e1afac | 2021-02-09 18:48:46 -0700 | [diff] [blame] | 3366 | | hb_filter([&] (unsigned klass) |
| 3367 | { return input_class_def.intersects_class (c->glyphs, klass); }, hb_first) |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3368 | | hb_map (hb_second) |
| 3369 | | hb_apply ([&] (const ChainRuleSet &_) |
| 3370 | { _.closure_lookups (c, lookup_context); }) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3371 | ; |
| 3372 | } |
| 3373 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 3374 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 3375 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3376 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 3377 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 3378 | (this+coverage).collect_coverage (c->input); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3379 | |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 3380 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 3381 | const ClassDef &input_class_def = this+inputClassDef; |
| 3382 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 3383 | |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3384 | struct ChainContextCollectGlyphsLookupContext lookup_context = { |
| 3385 | {collect_class}, |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 3386 | {&backtrack_class_def, |
| 3387 | &input_class_def, |
| 3388 | &lookahead_class_def} |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3389 | }; |
| 3390 | |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3391 | + hb_iter (ruleSet) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 3392 | | hb_map (hb_add (this)) |
| 3393 | | hb_apply ([&] (const ChainRuleSet &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3394 | ; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 3395 | } |
| 3396 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3397 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3398 | { |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 3399 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3400 | const ClassDef &input_class_def = this+inputClassDef; |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 3401 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3402 | |
Behdad Esfahbod | 2dc1141 | 2012-11-24 19:16:34 -0500 | [diff] [blame] | 3403 | unsigned int index = input_class_def.get_class (c->glyphs[0]); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3404 | const ChainRuleSet &rule_set = this+ruleSet[index]; |
| 3405 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3406 | {{match_class, match_class, match_class}}, |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 3407 | {&backtrack_class_def, |
| 3408 | &input_class_def, |
| 3409 | &lookahead_class_def} |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3410 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 3411 | return rule_set.would_apply (c, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3412 | } |
| 3413 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3414 | const Coverage &get_coverage () const { return this+coverage; } |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 3415 | |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3416 | unsigned cache_cost () const |
| 3417 | { |
Behdad Esfahbod | 21346af | 2022-06-07 14:41:39 -0600 | [diff] [blame] | 3418 | unsigned c = (this+lookaheadClassDef).cost () * ruleSet.len; |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3419 | return c >= 4 ? c : 0; |
| 3420 | } |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 3421 | bool cache_func (hb_ot_apply_context_t *c, bool enter) const |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3422 | { |
Behdad Esfahbod | 5963cf4 | 2022-06-07 09:12:45 -0600 | [diff] [blame] | 3423 | if (enter) |
| 3424 | { |
| 3425 | if (!HB_BUFFER_TRY_ALLOCATE_VAR (c->buffer, syllable)) |
| 3426 | return false; |
| 3427 | auto &info = c->buffer->info; |
| 3428 | unsigned count = c->buffer->len; |
| 3429 | for (unsigned i = 0; i < count; i++) |
| 3430 | info[i].syllable() = 255; |
| 3431 | c->new_syllables = 255; |
| 3432 | return true; |
| 3433 | } |
| 3434 | else |
| 3435 | { |
| 3436 | c->new_syllables = (unsigned) -1; |
| 3437 | HB_BUFFER_DEALLOCATE_VAR (c->buffer, syllable); |
| 3438 | return true; |
| 3439 | } |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3440 | } |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3441 | |
| 3442 | bool apply (hb_ot_apply_context_t *c, bool cached = false) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3443 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3444 | TRACE_APPLY (this); |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 3445 | unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3446 | if (likely (index == NOT_COVERED)) return_trace (false); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3447 | |
| 3448 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 3449 | const ClassDef &input_class_def = this+inputClassDef; |
| 3450 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 3451 | |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3452 | /* For ChainContextFormat2_5 we cache the LookaheadClassDef instead of InputClassDef. |
Behdad Esfahbod | bfee683 | 2022-06-07 11:41:05 -0600 | [diff] [blame] | 3453 | * The reason is that most heavy fonts want to identify a glyph in context and apply |
| 3454 | * a lookup to it. In this scenario, the length of the input sequence is one, whereas |
| 3455 | * the lookahead / backtrack are typically longer. The one glyph in input sequence is |
| 3456 | * looked-up below and no input glyph is looked up in individual rules, whereas the |
| 3457 | * lookahead and backtrack glyphs are tried. Since we match lookahead before backtrack, |
| 3458 | * we should cache lookahead. This decisions showed a 20% improvement in shaping of |
| 3459 | * the Gulzar font. |
| 3460 | */ |
| 3461 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3462 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3463 | {{cached && &backtrack_class_def == &input_class_def ? match_class_cached : match_class, |
Behdad Esfahbod | bfee683 | 2022-06-07 11:41:05 -0600 | [diff] [blame] | 3464 | cached && &input_class_def == &lookahead_class_def ? match_class_cached : match_class, |
| 3465 | cached ? match_class_cached : match_class}}, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 3466 | {&backtrack_class_def, |
| 3467 | &input_class_def, |
| 3468 | &lookahead_class_def} |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3469 | }; |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3470 | |
Behdad Esfahbod | bfee683 | 2022-06-07 11:41:05 -0600 | [diff] [blame] | 3471 | index = input_class_def.get_class (c->buffer->cur().codepoint); |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3472 | const ChainRuleSet &rule_set = this+ruleSet[index]; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3473 | return_trace (rule_set.apply (c, lookup_context)); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3474 | } |
| 3475 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3476 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 3477 | { |
| 3478 | TRACE_SUBSET (this); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3479 | auto *out = c->serializer->start_embed (*this); |
| 3480 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 3481 | out->format = format; |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3482 | out->coverage.serialize_subset (c, coverage, this); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3483 | |
| 3484 | hb_map_t backtrack_klass_map; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3485 | hb_map_t input_klass_map; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3486 | hb_map_t lookahead_klass_map; |
Behdad Esfahbod | dfa9d7a | 2021-02-11 11:08:52 -0700 | [diff] [blame] | 3487 | |
| 3488 | out->backtrackClassDef.serialize_subset (c, backtrackClassDef, this, &backtrack_klass_map); |
| 3489 | // TODO: subset inputClassDef based on glyphs survived in Coverage subsetting |
| 3490 | out->inputClassDef.serialize_subset (c, inputClassDef, this, &input_klass_map); |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3491 | out->lookaheadClassDef.serialize_subset (c, lookaheadClassDef, this, &lookahead_klass_map); |
Behdad Esfahbod | dfa9d7a | 2021-02-11 11:08:52 -0700 | [diff] [blame] | 3492 | |
| 3493 | if (unlikely (!c->serializer->propagate_error (backtrack_klass_map, |
| 3494 | input_klass_map, |
| 3495 | lookahead_klass_map))) |
Garret Rieger | 06dbb6a | 2020-07-31 15:56:14 -0700 | [diff] [blame] | 3496 | return_trace (false); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3497 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3498 | const hb_set_t* glyphset = c->plan->glyphset_gsub (); |
| 3499 | hb_set_t retained_coverage_glyphs; |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 3500 | (this+coverage).intersect_set (*glyphset, retained_coverage_glyphs); |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3501 | |
| 3502 | hb_set_t coverage_glyph_classes; |
| 3503 | (this+inputClassDef).intersected_classes (&retained_coverage_glyphs, &coverage_glyph_classes); |
| 3504 | |
Garret Rieger | dc37555 | 2020-09-25 13:08:46 -0700 | [diff] [blame] | 3505 | int non_zero_index = -1, index = 0; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3506 | bool ret = true; |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 3507 | const hb_map_t *lookup_map = c->table_tag == HB_OT_TAG_GSUB ? c->plan->gsub_lookups : c->plan->gpos_lookups; |
Garret Rieger | dc37555 | 2020-09-25 13:08:46 -0700 | [diff] [blame] | 3508 | auto last_non_zero = c->serializer->snapshot (); |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3509 | for (const auto& _ : + hb_enumerate (ruleSet) |
| 3510 | | hb_filter (input_klass_map, hb_first)) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3511 | { |
| 3512 | auto *o = out->ruleSet.serialize_append (c->serializer); |
| 3513 | if (unlikely (!o)) |
| 3514 | { |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 3515 | ret = false; |
| 3516 | break; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3517 | } |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3518 | if (coverage_glyph_classes.has (_.first) && |
| 3519 | o->serialize_subset (c, _.second, this, |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 3520 | lookup_map, |
| 3521 | &backtrack_klass_map, |
| 3522 | &input_klass_map, |
| 3523 | &lookahead_klass_map)) |
Garret Rieger | dc37555 | 2020-09-25 13:08:46 -0700 | [diff] [blame] | 3524 | { |
| 3525 | last_non_zero = c->serializer->snapshot (); |
Ebrahim Byagowi | 5a7cc7f | 2020-07-29 08:33:32 +0430 | [diff] [blame] | 3526 | non_zero_index = index; |
Garret Rieger | dc37555 | 2020-09-25 13:08:46 -0700 | [diff] [blame] | 3527 | } |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 3528 | |
| 3529 | index++; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3530 | } |
| 3531 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3532 | if (!ret || non_zero_index == -1) return_trace (false); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3533 | |
Garret Rieger | dc37555 | 2020-09-25 13:08:46 -0700 | [diff] [blame] | 3534 | // prune empty trailing ruleSets |
| 3535 | if (index > non_zero_index) { |
| 3536 | c->serializer->revert (last_non_zero); |
| 3537 | out->ruleSet.len = non_zero_index + 1; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | return_trace (bool (out->ruleSet)); |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 3541 | } |
| 3542 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3543 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 3544 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3545 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3546 | return_trace (coverage.sanitize (c, this) && |
| 3547 | backtrackClassDef.sanitize (c, this) && |
| 3548 | inputClassDef.sanitize (c, this) && |
| 3549 | lookaheadClassDef.sanitize (c, this) && |
| 3550 | ruleSet.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3551 | } |
| 3552 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3553 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 3554 | HBUINT16 format; /* Format identifier--format = 2 */ |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3555 | typename Types::template OffsetTo<Coverage> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3556 | coverage; /* Offset to Coverage table--from |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3557 | * beginning of table */ |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3558 | typename Types::template OffsetTo<ClassDef> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3559 | backtrackClassDef; /* Offset to glyph ClassDef table |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3560 | * containing backtrack sequence |
| 3561 | * data--from beginning of table */ |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3562 | typename Types::template OffsetTo<ClassDef> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3563 | inputClassDef; /* Offset to glyph ClassDef |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3564 | * table containing input sequence |
| 3565 | * data--from beginning of table */ |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3566 | typename Types::template OffsetTo<ClassDef> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3567 | lookaheadClassDef; /* Offset to glyph ClassDef table |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3568 | * containing lookahead sequence |
| 3569 | * data--from beginning of table */ |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3570 | Array16Of<typename Types::template OffsetTo<ChainRuleSet>> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3571 | ruleSet; /* Array of ChainRuleSet tables |
| 3572 | * ordered by class */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 3573 | public: |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3574 | DEFINE_SIZE_ARRAY (4 + 4 * Types::size, ruleSet); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3575 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3576 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3577 | struct ChainContextFormat3 |
| 3578 | { |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 3579 | using RuleSet = OT::RuleSet<SmallTypes>; |
| 3580 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3581 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3582 | { |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3583 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3584 | |
| 3585 | if (!(this+input[0]).intersects (glyphs)) |
| 3586 | return false; |
| 3587 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3588 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3589 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3590 | {intersects_coverage, intersected_coverage_glyphs}, |
| 3591 | ContextFormat::CoverageBasedContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3592 | {this, this, this} |
| 3593 | }; |
| 3594 | return chain_context_intersects (glyphs, |
| 3595 | backtrack.len, (const HBUINT16 *) backtrack.arrayZ, |
| 3596 | input.len, (const HBUINT16 *) input.arrayZ + 1, |
| 3597 | lookahead.len, (const HBUINT16 *) lookahead.arrayZ, |
| 3598 | lookup_context); |
| 3599 | } |
| 3600 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 3601 | bool may_have_non_1to1 () const |
| 3602 | { return true; } |
| 3603 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3604 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3605 | { |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3606 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 3607 | |
| 3608 | if (!(this+input[0]).intersects (c->glyphs)) |
| 3609 | return; |
| 3610 | |
Behdad Esfahbod | 00dfbbc | 2022-07-21 11:39:32 -0600 | [diff] [blame] | 3611 | hb_set_t& cur_active_glyphs = c->push_cur_active_glyphs (); |
| 3612 | get_coverage ().intersect_set (c->previous_parent_active_glyphs (), |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 3613 | cur_active_glyphs); |
| 3614 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 3615 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3616 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
| 3617 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 3618 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3619 | {intersects_coverage, intersected_coverage_glyphs}, |
| 3620 | ContextFormat::CoverageBasedContext, |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 3621 | {this, this, this} |
| 3622 | }; |
| 3623 | chain_context_closure_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 3624 | backtrack.len, (const HBUINT16 *) backtrack.arrayZ, |
| 3625 | input.len, (const HBUINT16 *) input.arrayZ + 1, |
| 3626 | lookahead.len, (const HBUINT16 *) lookahead.arrayZ, |
| 3627 | lookup.len, lookup.arrayZ, |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3628 | 0, lookup_context); |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 3629 | |
| 3630 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3631 | } |
| 3632 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3633 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 3634 | { |
Garret Rieger | a5c0ec7 | 2020-09-25 14:57:20 -0700 | [diff] [blame] | 3635 | if (!intersects (c->glyphs)) |
| 3636 | return; |
| 3637 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3638 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
| 3639 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
| 3640 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3641 | recurse_lookups (c, lookup.len, lookup.arrayZ); |
| 3642 | } |
| 3643 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 3644 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 3645 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3646 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 3647 | { |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3648 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3649 | |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 3650 | (this+input[0]).collect_coverage (c->input); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3651 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3652 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
| 3653 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
| 3654 | |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3655 | struct ChainContextCollectGlyphsLookupContext lookup_context = { |
| 3656 | {collect_coverage}, |
| 3657 | {this, this, this} |
| 3658 | }; |
| 3659 | chain_context_collect_glyphs_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 3660 | backtrack.len, (const HBUINT16 *) backtrack.arrayZ, |
| 3661 | input.len, (const HBUINT16 *) input.arrayZ + 1, |
| 3662 | lookahead.len, (const HBUINT16 *) lookahead.arrayZ, |
| 3663 | lookup.len, lookup.arrayZ, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3664 | lookup_context); |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 3665 | } |
| 3666 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3667 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3668 | { |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3669 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
| 3670 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
| 3671 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3672 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3673 | {{match_coverage, match_coverage, match_coverage}}, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3674 | {this, this, this} |
| 3675 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 3676 | return chain_context_would_apply_lookup (c, |
| 3677 | backtrack.len, (const HBUINT16 *) backtrack.arrayZ, |
| 3678 | input.len, (const HBUINT16 *) input.arrayZ + 1, |
| 3679 | lookahead.len, (const HBUINT16 *) lookahead.arrayZ, |
| 3680 | lookup.len, lookup.arrayZ, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3681 | } |
| 3682 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3683 | const Coverage &get_coverage () const |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 3684 | { |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3685 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 3686 | return this+input[0]; |
| 3687 | } |
| 3688 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3689 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3690 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3691 | TRACE_APPLY (this); |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3692 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 3693 | |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 3694 | unsigned int index = (this+input[0]).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3695 | if (likely (index == NOT_COVERED)) return_trace (false); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3696 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3697 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
| 3698 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3699 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3700 | {{match_coverage, match_coverage, match_coverage}}, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 3701 | {this, this, this} |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 3702 | }; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3703 | return_trace (chain_context_apply_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 3704 | backtrack.len, (const HBUINT16 *) backtrack.arrayZ, |
| 3705 | input.len, (const HBUINT16 *) input.arrayZ + 1, |
| 3706 | lookahead.len, (const HBUINT16 *) lookahead.arrayZ, |
| 3707 | lookup.len, lookup.arrayZ, lookup_context)); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3708 | } |
| 3709 | |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3710 | template<typename Iterator, |
| 3711 | hb_requires (hb_is_iterator (Iterator))> |
Ebrahim Byagowi | 07acd1a | 2020-03-08 23:39:24 +0330 | [diff] [blame] | 3712 | bool serialize_coverage_offsets (hb_subset_context_t *c, Iterator it, const void* base) const |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3713 | { |
| 3714 | TRACE_SERIALIZE (this); |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3715 | auto *out = c->serializer->start_embed<Array16OfOffset16To<Coverage>> (); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3716 | |
Garret Rieger | 90eb1a4 | 2020-09-25 12:36:32 -0700 | [diff] [blame] | 3717 | if (unlikely (!c->serializer->allocate_size<HBUINT16> (HBUINT16::static_size))) |
Garret Rieger | 940e1c6 | 2020-09-28 17:22:01 -0700 | [diff] [blame] | 3718 | return_trace (false); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3719 | |
Garret Rieger | 940e1c6 | 2020-09-28 17:22:01 -0700 | [diff] [blame] | 3720 | for (auto& offset : it) { |
| 3721 | auto *o = out->serialize_append (c->serializer); |
| 3722 | if (unlikely (!o) || !o->serialize_subset (c, offset, base)) |
| 3723 | return_trace (false); |
| 3724 | } |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3725 | |
Garret Rieger | 940e1c6 | 2020-09-28 17:22:01 -0700 | [diff] [blame] | 3726 | return_trace (true); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3727 | } |
| 3728 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3729 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 3730 | { |
| 3731 | TRACE_SUBSET (this); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3732 | |
| 3733 | auto *out = c->serializer->start_embed (this); |
| 3734 | if (unlikely (!out)) return_trace (false); |
| 3735 | if (unlikely (!c->serializer->embed (this->format))) return_trace (false); |
| 3736 | |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3737 | if (!serialize_coverage_offsets (c, backtrack.iter (), this)) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3738 | return_trace (false); |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 3739 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3740 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3741 | if (!serialize_coverage_offsets (c, input.iter (), this)) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3742 | return_trace (false); |
| 3743 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3744 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3745 | if (!serialize_coverage_offsets (c, lookahead.iter (), this)) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3746 | return_trace (false); |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 3747 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3748 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Qunxin Liu | 37379f8 | 2021-09-02 11:54:37 -0700 | [diff] [blame] | 3749 | const hb_map_t *lookup_map = c->table_tag == HB_OT_TAG_GSUB ? c->plan->gsub_lookups : c->plan->gpos_lookups; |
Garret Rieger | f51b48c | 2021-11-02 16:16:52 -0700 | [diff] [blame] | 3750 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3751 | HBUINT16 *lookupCount = c->serializer->copy<HBUINT16> (lookup.len); |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 3752 | if (!lookupCount) return_trace (false); |
Qunxin Liu | 37379f8 | 2021-09-02 11:54:37 -0700 | [diff] [blame] | 3753 | |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3754 | unsigned count = serialize_lookuprecord_array (c->serializer, lookup.as_array (), lookup_map); |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 3755 | return_trace (c->serializer->check_assign (*lookupCount, count, HB_SERIALIZE_ERROR_INT_OVERFLOW)); |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 3756 | } |
| 3757 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3758 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 3759 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3760 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3761 | if (!backtrack.sanitize (c, this)) return_trace (false); |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3762 | const auto &input = StructAfter<decltype (inputX)> (backtrack); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3763 | if (!input.sanitize (c, this)) return_trace (false); |
| 3764 | if (!input.len) return_trace (false); /* To be consistent with Context. */ |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3765 | const auto &lookahead = StructAfter<decltype (lookaheadX)> (input); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3766 | if (!lookahead.sanitize (c, this)) return_trace (false); |
Behdad Esfahbod | 6d051f4 | 2022-07-08 14:08:51 -0600 | [diff] [blame] | 3767 | const auto &lookup = StructAfter<decltype (lookupX)> (lookahead); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3768 | return_trace (lookup.sanitize (c)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3769 | } |
| 3770 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3771 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 3772 | HBUINT16 format; /* Format identifier--format = 3 */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3773 | Array16OfOffset16To<Coverage> |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 3774 | backtrack; /* Array of coverage tables |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3775 | * in backtracking sequence, in glyph |
| 3776 | * sequence order */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3777 | Array16OfOffset16To<Coverage> |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 3778 | inputX ; /* Array of coverage |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3779 | * tables in input sequence, in glyph |
| 3780 | * sequence order */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3781 | Array16OfOffset16To<Coverage> |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 3782 | lookaheadX; /* Array of coverage tables |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3783 | * in lookahead sequence, in glyph |
| 3784 | * sequence order */ |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 3785 | Array16Of<LookupRecord> |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 3786 | lookupX; /* Array of LookupRecords--in |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 3787 | * design order) */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 3788 | public: |
Behdad Esfahbod | bea34c7 | 2010-05-10 17:28:16 -0400 | [diff] [blame] | 3789 | DEFINE_SIZE_MIN (10); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3790 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3791 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3792 | struct ChainContext |
| 3793 | { |
Behdad Esfahbod | 36bb24f | 2019-05-05 10:14:17 -0700 | [diff] [blame] | 3794 | template <typename context_t, typename ...Ts> |
Behdad Esfahbod | 83e3eab | 2019-05-07 20:58:43 -0700 | [diff] [blame] | 3795 | typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3796 | { |
Behdad Esfahbod | 00f6a8e | 2014-12-12 20:36:49 -0800 | [diff] [blame] | 3797 | TRACE_DISPATCH (this, u.format); |
Behdad Esfahbod | f396fbb | 2015-10-09 12:25:55 -0400 | [diff] [blame] | 3798 | if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ()); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3799 | switch (u.format) { |
Behdad Esfahbod | 6d555ce | 2021-11-02 00:18:22 -0600 | [diff] [blame] | 3800 | case 1: return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...)); |
| 3801 | case 2: return_trace (c->dispatch (u.format2, std::forward<Ts> (ds)...)); |
| 3802 | case 3: return_trace (c->dispatch (u.format3, std::forward<Ts> (ds)...)); |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 3803 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 1bf8fa2 | 2022-07-08 14:22:21 -0600 | [diff] [blame] | 3804 | case 4: return_trace (c->dispatch (u.format4, std::forward<Ts> (ds)...)); |
Behdad Esfahbod | d1f58e5 | 2022-07-08 13:25:07 -0600 | [diff] [blame] | 3805 | case 5: return_trace (c->dispatch (u.format5, std::forward<Ts> (ds)...)); |
| 3806 | #endif |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3807 | default:return_trace (c->default_return_value ()); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3808 | } |
| 3809 | } |
| 3810 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3811 | protected: |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3812 | union { |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3813 | HBUINT16 format; /* Format identifier */ |
Behdad Esfahbod | 0f13eb1 | 2022-07-08 13:43:33 -0600 | [diff] [blame] | 3814 | ChainContextFormat1_4<SmallTypes> format1; |
Behdad Esfahbod | a90c5af | 2022-07-08 13:11:47 -0600 | [diff] [blame] | 3815 | ChainContextFormat2_5<SmallTypes> format2; |
| 3816 | ChainContextFormat3 format3; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 3817 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 1bf8fa2 | 2022-07-08 14:22:21 -0600 | [diff] [blame] | 3818 | ChainContextFormat1_4<MediumTypes> format4; |
Behdad Esfahbod | d1f58e5 | 2022-07-08 13:25:07 -0600 | [diff] [blame] | 3819 | ChainContextFormat2_5<MediumTypes> format5; |
| 3820 | #endif |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3821 | } u; |
| 3822 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3823 | |
| 3824 | |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3825 | template <typename T> |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3826 | struct ExtensionFormat1 |
| 3827 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3828 | unsigned int get_type () const { return extensionLookupType; } |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3829 | |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3830 | template <typename X> |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3831 | const X& get_subtable () const |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3832 | { return this + reinterpret_cast<const Offset32To<typename T::SubTable> &> (extensionOffset); } |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3833 | |
Behdad Esfahbod | 36bb24f | 2019-05-05 10:14:17 -0700 | [diff] [blame] | 3834 | template <typename context_t, typename ...Ts> |
Behdad Esfahbod | 83e3eab | 2019-05-07 20:58:43 -0700 | [diff] [blame] | 3835 | typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3836 | { |
| 3837 | TRACE_DISPATCH (this, format); |
Behdad Esfahbod | f396fbb | 2015-10-09 12:25:55 -0400 | [diff] [blame] | 3838 | if (unlikely (!c->may_dispatch (this, this))) return_trace (c->no_dispatch_return_value ()); |
Behdad Esfahbod | 6d555ce | 2021-11-02 00:18:22 -0600 | [diff] [blame] | 3839 | return_trace (get_subtable<typename T::SubTable> ().dispatch (c, get_type (), std::forward<Ts> (ds)...)); |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3840 | } |
| 3841 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 3842 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const |
| 3843 | { dispatch (c); } |
| 3844 | |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3845 | /* This is called from may_dispatch() above with hb_sanitize_context_t. */ |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3846 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 3847 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3848 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 949f6af | 2018-01-15 20:44:10 -0500 | [diff] [blame] | 3849 | return_trace (c->check_struct (this) && |
Behdad Esfahbod | 9c3747c | 2018-09-03 16:53:03 -0700 | [diff] [blame] | 3850 | extensionLookupType != T::SubTable::Extension); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3851 | } |
| 3852 | |
Garret Rieger | 08a4997 | 2020-10-06 13:02:12 -0700 | [diff] [blame] | 3853 | bool subset (hb_subset_context_t *c) const |
| 3854 | { |
| 3855 | TRACE_SUBSET (this); |
| 3856 | |
| 3857 | auto *out = c->serializer->start_embed (this); |
| 3858 | if (unlikely (!out || !c->serializer->extend_min (out))) return_trace (false); |
| 3859 | |
| 3860 | out->format = format; |
| 3861 | out->extensionLookupType = extensionLookupType; |
| 3862 | |
| 3863 | const auto& src_offset = |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3864 | reinterpret_cast<const Offset32To<typename T::SubTable> &> (extensionOffset); |
Garret Rieger | 08a4997 | 2020-10-06 13:02:12 -0700 | [diff] [blame] | 3865 | auto& dest_offset = |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3866 | reinterpret_cast<Offset32To<typename T::SubTable> &> (out->extensionOffset); |
Garret Rieger | 08a4997 | 2020-10-06 13:02:12 -0700 | [diff] [blame] | 3867 | |
| 3868 | return_trace (dest_offset.serialize_subset (c, src_offset, this, get_type ())); |
| 3869 | } |
| 3870 | |
Garret Rieger | 8d611a7 | 2022-07-22 22:49:40 +0000 | [diff] [blame] | 3871 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 3872 | HBUINT16 format; /* Format identifier. Set to 1. */ |
| 3873 | HBUINT16 extensionLookupType; /* Lookup type of subtable referenced |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3874 | * by ExtensionOffset (i.e. the |
| 3875 | * extension subtable). */ |
Behdad Esfahbod | cd9bc73 | 2019-05-10 13:17:41 -0700 | [diff] [blame] | 3876 | Offset32 extensionOffset; /* Offset to the extension subtable, |
Behdad Esfahbod | 81f2af4 | 2010-04-22 00:58:49 -0400 | [diff] [blame] | 3877 | * of lookup type subtable. */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 3878 | public: |
| 3879 | DEFINE_SIZE_STATIC (8); |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3880 | }; |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3881 | |
Behdad Esfahbod | 653eeb2 | 2012-11-23 16:57:36 -0500 | [diff] [blame] | 3882 | template <typename T> |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3883 | struct Extension |
| 3884 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3885 | unsigned int get_type () const |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3886 | { |
| 3887 | switch (u.format) { |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 3888 | case 1: return u.format1.get_type (); |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3889 | default:return 0; |
| 3890 | } |
| 3891 | } |
Behdad Esfahbod | 7dddd4e | 2012-11-23 17:04:55 -0500 | [diff] [blame] | 3892 | template <typename X> |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3893 | const X& get_subtable () const |
Behdad Esfahbod | 7dddd4e | 2012-11-23 17:04:55 -0500 | [diff] [blame] | 3894 | { |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3895 | switch (u.format) { |
Behdad Esfahbod | 9c3747c | 2018-09-03 16:53:03 -0700 | [diff] [blame] | 3896 | case 1: return u.format1.template get_subtable<typename T::SubTable> (); |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 3897 | default:return Null (typename T::SubTable); |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3898 | } |
Behdad Esfahbod | 7dddd4e | 2012-11-23 17:04:55 -0500 | [diff] [blame] | 3899 | } |
| 3900 | |
Garret Rieger | 08a4997 | 2020-10-06 13:02:12 -0700 | [diff] [blame] | 3901 | // Specialization of dispatch for subset. dispatch() normally just |
| 3902 | // dispatches to the sub table this points too, but for subset |
| 3903 | // we need to run subset on this subtable too. |
| 3904 | template <typename ...Ts> |
| 3905 | typename hb_subset_context_t::return_t dispatch (hb_subset_context_t *c, Ts&&... ds) const |
| 3906 | { |
| 3907 | switch (u.format) { |
| 3908 | case 1: return u.format1.subset (c); |
| 3909 | default: return c->default_return_value (); |
| 3910 | } |
| 3911 | } |
| 3912 | |
Behdad Esfahbod | 36bb24f | 2019-05-05 10:14:17 -0700 | [diff] [blame] | 3913 | template <typename context_t, typename ...Ts> |
Behdad Esfahbod | 83e3eab | 2019-05-07 20:58:43 -0700 | [diff] [blame] | 3914 | typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const |
Behdad Esfahbod | 653eeb2 | 2012-11-23 16:57:36 -0500 | [diff] [blame] | 3915 | { |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3916 | TRACE_DISPATCH (this, u.format); |
Behdad Esfahbod | f396fbb | 2015-10-09 12:25:55 -0400 | [diff] [blame] | 3917 | if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ()); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3918 | switch (u.format) { |
Behdad Esfahbod | 6d555ce | 2021-11-02 00:18:22 -0600 | [diff] [blame] | 3919 | case 1: return_trace (u.format1.dispatch (c, std::forward<Ts> (ds)...)); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3920 | default:return_trace (c->default_return_value ()); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3921 | } |
| 3922 | } |
| 3923 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3924 | protected: |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3925 | union { |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 3926 | HBUINT16 format; /* Format identifier */ |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3927 | ExtensionFormat1<T> format1; |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3928 | } u; |
| 3929 | }; |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3930 | |
| 3931 | |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 3932 | /* |
| 3933 | * GSUB/GPOS Common |
| 3934 | */ |
| 3935 | |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3936 | struct hb_ot_layout_lookup_accelerator_t |
| 3937 | { |
| 3938 | template <typename TLookup> |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3939 | void init (const TLookup &lookup) |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3940 | { |
| 3941 | digest.init (); |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 3942 | lookup.collect_coverage (&digest); |
Behdad Esfahbod | 78c09bf | 2018-10-10 11:50:46 -0400 | [diff] [blame] | 3943 | |
| 3944 | subtables.init (); |
Behdad Esfahbod | f9b643f | 2022-06-04 07:29:40 -0600 | [diff] [blame] | 3945 | OT::hb_accelerate_subtables_context_t c_accelerate_subtables (subtables); |
| 3946 | lookup.dispatch (&c_accelerate_subtables); |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 3947 | |
| 3948 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3949 | cache_user_idx = c_accelerate_subtables.cache_user_idx; |
| 3950 | for (unsigned i = 0; i < subtables.length; i++) |
| 3951 | if (i != cache_user_idx) |
| 3952 | subtables[i].apply_cached_func = subtables[i].apply_func; |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 3953 | #endif |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3954 | } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3955 | void fini () { subtables.fini (); } |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3956 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3957 | bool may_have (hb_codepoint_t g) const |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3958 | { return digest.may_have (g); } |
| 3959 | |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3960 | bool apply (hb_ot_apply_context_t *c, bool use_cache) const |
Behdad Esfahbod | e78549e | 2018-10-10 11:54:48 -0400 | [diff] [blame] | 3961 | { |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 3962 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3963 | if (use_cache) |
| 3964 | { |
| 3965 | for (unsigned int i = 0; i < subtables.length; i++) |
| 3966 | if (subtables[i].apply_cached (c)) |
| 3967 | return true; |
| 3968 | } |
| 3969 | else |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 3970 | #endif |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3971 | { |
| 3972 | for (unsigned int i = 0; i < subtables.length; i++) |
| 3973 | if (subtables[i].apply (c)) |
| 3974 | return true; |
| 3975 | } |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 3976 | return false; |
Behdad Esfahbod | e78549e | 2018-10-10 11:54:48 -0400 | [diff] [blame] | 3977 | } |
| 3978 | |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3979 | bool cache_enter (OT::hb_ot_apply_context_t *c) const |
| 3980 | { |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 3981 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3982 | return cache_user_idx != (unsigned) -1 && |
| 3983 | subtables[cache_user_idx].cache_enter (c); |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 3984 | #else |
| 3985 | return false; |
| 3986 | #endif |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3987 | } |
| 3988 | void cache_leave (OT::hb_ot_apply_context_t *c) const |
| 3989 | { |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 3990 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3991 | subtables[cache_user_idx].cache_leave (c); |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 3992 | #endif |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | |
Behdad Esfahbod | e78549e | 2018-10-10 11:54:48 -0400 | [diff] [blame] | 3996 | private: |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3997 | hb_set_digest_t digest; |
Behdad Esfahbod | f9b643f | 2022-06-04 07:29:40 -0600 | [diff] [blame] | 3998 | hb_accelerate_subtables_context_t::array_t subtables; |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 3999 | #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE |
Behdad Esfahbod | b96622d | 2022-06-05 02:45:41 -0600 | [diff] [blame] | 4000 | unsigned cache_user_idx = (unsigned) -1; |
Behdad Esfahbod | 39820af | 2022-06-07 10:18:38 -0600 | [diff] [blame] | 4001 | #endif |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 4002 | }; |
| 4003 | |
Behdad Esfahbod | 5fd0a3f | 2022-07-11 13:10:04 -0600 | [diff] [blame] | 4004 | template <typename Types> |
| 4005 | struct GSUBGPOSVersion1_2 |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 4006 | { |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4007 | friend struct GSUBGPOS; |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 4008 | |
Garret Rieger | 8d611a7 | 2022-07-22 22:49:40 +0000 | [diff] [blame] | 4009 | protected: |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4010 | FixedVersion<>version; /* Version of the GSUB/GPOS table--initially set |
| 4011 | * to 0x00010000u */ |
Behdad Esfahbod | 5fd0a3f | 2022-07-11 13:10:04 -0600 | [diff] [blame] | 4012 | typename Types:: template OffsetTo<ScriptList> |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4013 | scriptList; /* ScriptList table */ |
Behdad Esfahbod | 5fd0a3f | 2022-07-11 13:10:04 -0600 | [diff] [blame] | 4014 | typename Types::template OffsetTo<FeatureList> |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4015 | featureList; /* FeatureList table */ |
Behdad Esfahbod | 5fd0a3f | 2022-07-11 13:10:04 -0600 | [diff] [blame] | 4016 | typename Types::template OffsetTo<LookupList<Types>> |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4017 | lookupList; /* LookupList table */ |
| 4018 | Offset32To<FeatureVariations> |
| 4019 | featureVars; /* Offset to Feature Variations |
| 4020 | table--from beginning of table |
| 4021 | * (may be NULL). Introduced |
| 4022 | * in version 0x00010001. */ |
| 4023 | public: |
Behdad Esfahbod | 5fd0a3f | 2022-07-11 13:10:04 -0600 | [diff] [blame] | 4024 | DEFINE_SIZE_MIN (4 + 3 * Types::size); |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 4025 | |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4026 | unsigned int get_size () const |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 4027 | { |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4028 | return min_size + |
| 4029 | (version.to_int () >= 0x00010001u ? featureVars.static_size : 0); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 4030 | } |
| 4031 | |
Garret Rieger | 8d611a7 | 2022-07-22 22:49:40 +0000 | [diff] [blame] | 4032 | const typename Types::template OffsetTo<LookupList<Types>>* get_lookup_list_offset () const |
| 4033 | { |
| 4034 | return &lookupList; |
| 4035 | } |
| 4036 | |
Behdad Esfahbod | 9c3747c | 2018-09-03 16:53:03 -0700 | [diff] [blame] | 4037 | template <typename TLookup> |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4038 | bool sanitize (hb_sanitize_context_t *c) const |
Qunxin Liu | 973c47f | 2020-06-11 11:27:57 -0700 | [diff] [blame] | 4039 | { |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4040 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4041 | typedef List16OfOffsetTo<TLookup, typename Types::HBUINT> TLookupList; |
| 4042 | if (unlikely (!(scriptList.sanitize (c, this) && |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4043 | featureList.sanitize (c, this) && |
Behdad Esfahbod | 9ef9fc0 | 2022-07-11 13:29:23 -0600 | [diff] [blame] | 4044 | reinterpret_cast<const typename Types::template OffsetTo<TLookupList> &> (lookupList).sanitize (c, this)))) |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4045 | return_trace (false); |
Qunxin Liu | 973c47f | 2020-06-11 11:27:57 -0700 | [diff] [blame] | 4046 | |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4047 | #ifndef HB_NO_VAR |
| 4048 | if (unlikely (!(version.to_int () < 0x00010001u || featureVars.sanitize (c, this)))) |
| 4049 | return_trace (false); |
| 4050 | #endif |
Behdad Esfahbod | 4119f73 | 2022-06-08 06:34:48 -0600 | [diff] [blame] | 4051 | |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4052 | return_trace (true); |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 4053 | } |
| 4054 | |
Qunxin Liu | 973c47f | 2020-06-11 11:27:57 -0700 | [diff] [blame] | 4055 | template <typename TLookup> |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 4056 | bool subset (hb_subset_layout_context_t *c) const |
Behdad Esfahbod | bfa72a9 | 2018-09-01 18:34:50 -0700 | [diff] [blame] | 4057 | { |
| 4058 | TRACE_SUBSET (this); |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 4059 | auto *out = c->subset_context->serializer->embed (*this); |
Behdad Esfahbod | bfa72a9 | 2018-09-01 18:34:50 -0700 | [diff] [blame] | 4060 | if (unlikely (!out)) return_trace (false); |
Behdad Esfahbod | 1b6d0c4 | 2018-12-13 18:10:48 -0500 | [diff] [blame] | 4061 | |
Behdad Esfahbod | 9ef9fc0 | 2022-07-11 13:29:23 -0600 | [diff] [blame] | 4062 | typedef LookupOffsetList<TLookup, typename Types::HBUINT> TLookupList; |
| 4063 | reinterpret_cast<typename Types::template OffsetTo<TLookupList> &> (out->lookupList) |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 4064 | .serialize_subset (c->subset_context, |
Behdad Esfahbod | 9ef9fc0 | 2022-07-11 13:29:23 -0600 | [diff] [blame] | 4065 | reinterpret_cast<const typename Types::template OffsetTo<TLookupList> &> (lookupList), |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 4066 | this, |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 4067 | c); |
| 4068 | |
Behdad Esfahbod | 9ef9fc0 | 2022-07-11 13:29:23 -0600 | [diff] [blame] | 4069 | reinterpret_cast<typename Types::template OffsetTo<RecordListOfFeature> &> (out->featureList) |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 4070 | .serialize_subset (c->subset_context, |
Behdad Esfahbod | 9ef9fc0 | 2022-07-11 13:29:23 -0600 | [diff] [blame] | 4071 | reinterpret_cast<const typename Types::template OffsetTo<RecordListOfFeature> &> (featureList), |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 4072 | this, |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 4073 | c); |
| 4074 | |
| 4075 | out->scriptList.serialize_subset (c->subset_context, |
| 4076 | scriptList, |
| 4077 | this, |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 4078 | c); |
Behdad Esfahbod | 9c3747c | 2018-09-03 16:53:03 -0700 | [diff] [blame] | 4079 | |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 4080 | #ifndef HB_NO_VAR |
Behdad Esfahbod | bfa72a9 | 2018-09-01 18:34:50 -0700 | [diff] [blame] | 4081 | if (version.to_int () >= 0x00010001u) |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 4082 | { |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 4083 | bool ret = out->featureVars.serialize_subset (c->subset_context, featureVars, this, c); |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4084 | if (!ret && version.major == 1) |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 4085 | { |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 4086 | out->version.major = 1; |
| 4087 | out->version.minor = 0; |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 4088 | } |
| 4089 | } |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 4090 | #endif |
Behdad Esfahbod | 1b6d0c4 | 2018-12-13 18:10:48 -0500 | [diff] [blame] | 4091 | |
Behdad Esfahbod | bfa72a9 | 2018-09-01 18:34:50 -0700 | [diff] [blame] | 4092 | return_trace (true); |
| 4093 | } |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4094 | }; |
| 4095 | |
| 4096 | struct GSUBGPOS |
| 4097 | { |
| 4098 | unsigned int get_size () const |
| 4099 | { |
| 4100 | switch (u.version.major) { |
| 4101 | case 1: return u.version1.get_size (); |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 4102 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4103 | case 2: return u.version2.get_size (); |
| 4104 | #endif |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4105 | default: return u.version.static_size; |
| 4106 | } |
| 4107 | } |
| 4108 | |
| 4109 | template <typename TLookup> |
| 4110 | bool sanitize (hb_sanitize_context_t *c) const |
| 4111 | { |
| 4112 | TRACE_SANITIZE (this); |
| 4113 | if (unlikely (!u.version.sanitize (c))) return_trace (false); |
| 4114 | switch (u.version.major) { |
| 4115 | case 1: return_trace (u.version1.sanitize<TLookup> (c)); |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 4116 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4117 | case 2: return_trace (u.version2.sanitize<TLookup> (c)); |
| 4118 | #endif |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4119 | default: return_trace (true); |
| 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | template <typename TLookup> |
| 4124 | bool subset (hb_subset_layout_context_t *c) const |
| 4125 | { |
| 4126 | switch (u.version.major) { |
| 4127 | case 1: return u.version1.subset<TLookup> (c); |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 4128 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4129 | case 2: return u.version2.subset<TLookup> (c); |
| 4130 | #endif |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4131 | default: return false; |
| 4132 | } |
| 4133 | } |
| 4134 | |
| 4135 | const ScriptList &get_script_list () const |
| 4136 | { |
| 4137 | switch (u.version.major) { |
| 4138 | case 1: return this+u.version1.scriptList; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 4139 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4140 | case 2: return this+u.version2.scriptList; |
| 4141 | #endif |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4142 | default: return Null (ScriptList); |
| 4143 | } |
| 4144 | } |
| 4145 | const FeatureList &get_feature_list () const |
| 4146 | { |
| 4147 | switch (u.version.major) { |
| 4148 | case 1: return this+u.version1.featureList; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 4149 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4150 | case 2: return this+u.version2.featureList; |
| 4151 | #endif |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4152 | default: return Null (FeatureList); |
| 4153 | } |
| 4154 | } |
| 4155 | unsigned int get_lookup_count () const |
| 4156 | { |
| 4157 | switch (u.version.major) { |
| 4158 | case 1: return (this+u.version1.lookupList).len; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 4159 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4160 | case 2: return (this+u.version2.lookupList).len; |
| 4161 | #endif |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4162 | default: return 0; |
| 4163 | } |
| 4164 | } |
| 4165 | const Lookup& get_lookup (unsigned int i) const |
| 4166 | { |
| 4167 | switch (u.version.major) { |
| 4168 | case 1: return (this+u.version1.lookupList)[i]; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 4169 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4170 | case 2: return (this+u.version2.lookupList)[i]; |
| 4171 | #endif |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4172 | default: return Null (Lookup); |
| 4173 | } |
| 4174 | } |
| 4175 | const FeatureVariations &get_feature_variations () const |
| 4176 | { |
| 4177 | switch (u.version.major) { |
| 4178 | case 1: return (u.version.to_int () >= 0x00010001u ? this+u.version1.featureVars : Null (FeatureVariations)); |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 4179 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4180 | case 2: return this+u.version2.featureVars; |
| 4181 | #endif |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4182 | default: return Null (FeatureVariations); |
| 4183 | } |
| 4184 | } |
| 4185 | |
| 4186 | bool has_data () const { return u.version.to_int (); } |
| 4187 | unsigned int get_script_count () const |
| 4188 | { return get_script_list ().len; } |
| 4189 | const Tag& get_script_tag (unsigned int i) const |
| 4190 | { return get_script_list ().get_tag (i); } |
| 4191 | unsigned int get_script_tags (unsigned int start_offset, |
| 4192 | unsigned int *script_count /* IN/OUT */, |
| 4193 | hb_tag_t *script_tags /* OUT */) const |
| 4194 | { return get_script_list ().get_tags (start_offset, script_count, script_tags); } |
| 4195 | const Script& get_script (unsigned int i) const |
| 4196 | { return get_script_list ()[i]; } |
| 4197 | bool find_script_index (hb_tag_t tag, unsigned int *index) const |
| 4198 | { return get_script_list ().find_index (tag, index); } |
| 4199 | |
| 4200 | unsigned int get_feature_count () const |
| 4201 | { return get_feature_list ().len; } |
| 4202 | hb_tag_t get_feature_tag (unsigned int i) const |
| 4203 | { return i == Index::NOT_FOUND_INDEX ? HB_TAG_NONE : get_feature_list ().get_tag (i); } |
| 4204 | unsigned int get_feature_tags (unsigned int start_offset, |
| 4205 | unsigned int *feature_count /* IN/OUT */, |
| 4206 | hb_tag_t *feature_tags /* OUT */) const |
| 4207 | { return get_feature_list ().get_tags (start_offset, feature_count, feature_tags); } |
| 4208 | const Feature& get_feature (unsigned int i) const |
| 4209 | { return get_feature_list ()[i]; } |
| 4210 | bool find_feature_index (hb_tag_t tag, unsigned int *index) const |
| 4211 | { return get_feature_list ().find_index (tag, index); } |
| 4212 | |
| 4213 | bool find_variations_index (const int *coords, unsigned int num_coords, |
| 4214 | unsigned int *index) const |
| 4215 | { |
| 4216 | #ifdef HB_NO_VAR |
| 4217 | *index = FeatureVariations::NOT_FOUND_INDEX; |
| 4218 | return false; |
| 4219 | #endif |
| 4220 | return get_feature_variations ().find_index (coords, num_coords, index); |
| 4221 | } |
| 4222 | const Feature& get_feature_variation (unsigned int feature_index, |
| 4223 | unsigned int variations_index) const |
| 4224 | { |
| 4225 | #ifndef HB_NO_VAR |
| 4226 | if (FeatureVariations::NOT_FOUND_INDEX != variations_index && |
| 4227 | u.version.to_int () >= 0x00010001u) |
| 4228 | { |
| 4229 | const Feature *feature = get_feature_variations ().find_substitute (variations_index, |
| 4230 | feature_index); |
| 4231 | if (feature) |
| 4232 | return *feature; |
| 4233 | } |
| 4234 | #endif |
| 4235 | return get_feature (feature_index); |
| 4236 | } |
| 4237 | |
| 4238 | void feature_variation_collect_lookups (const hb_set_t *feature_indexes, |
| 4239 | hb_set_t *lookup_indexes /* OUT */) const |
| 4240 | { |
| 4241 | #ifndef HB_NO_VAR |
| 4242 | get_feature_variations ().collect_lookups (feature_indexes, lookup_indexes); |
| 4243 | #endif |
| 4244 | } |
| 4245 | |
| 4246 | template <typename TLookup> |
| 4247 | void closure_lookups (hb_face_t *face, |
| 4248 | const hb_set_t *glyphs, |
| 4249 | hb_set_t *lookup_indexes /* IN/OUT */) const |
| 4250 | { |
| 4251 | hb_set_t visited_lookups, inactive_lookups; |
| 4252 | OT::hb_closure_lookups_context_t c (face, glyphs, &visited_lookups, &inactive_lookups); |
| 4253 | |
| 4254 | c.set_recurse_func (TLookup::template dispatch_recurse_func<hb_closure_lookups_context_t>); |
| 4255 | |
| 4256 | for (unsigned lookup_index : + hb_iter (lookup_indexes)) |
| 4257 | reinterpret_cast<const TLookup &> (get_lookup (lookup_index)).closure_lookups (&c, lookup_index); |
| 4258 | |
| 4259 | hb_set_union (lookup_indexes, &visited_lookups); |
| 4260 | hb_set_subtract (lookup_indexes, &inactive_lookups); |
| 4261 | } |
| 4262 | |
| 4263 | void prune_langsys (const hb_map_t *duplicate_feature_map, |
| 4264 | hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> *script_langsys_map, |
| 4265 | hb_set_t *new_feature_indexes /* OUT */) const |
| 4266 | { |
| 4267 | hb_prune_langsys_context_t c (this, script_langsys_map, duplicate_feature_map, new_feature_indexes); |
| 4268 | |
| 4269 | unsigned count = get_script_count (); |
| 4270 | for (unsigned script_index = 0; script_index < count; script_index++) |
| 4271 | { |
| 4272 | const Script& s = get_script (script_index); |
| 4273 | s.prune_langsys (&c, script_index); |
| 4274 | } |
| 4275 | } |
| 4276 | |
Garret Rieger | 718bf5a | 2020-09-29 13:16:01 -0700 | [diff] [blame] | 4277 | void prune_features (const hb_map_t *lookup_indices, /* IN */ |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 4278 | hb_set_t *feature_indices /* IN/OUT */) const |
Qunxin Liu | 8ffc9ad | 2019-10-31 15:59:02 -0700 | [diff] [blame] | 4279 | { |
Garret Rieger | 718bf5a | 2020-09-29 13:16:01 -0700 | [diff] [blame] | 4280 | #ifndef HB_NO_VAR |
| 4281 | // This is the set of feature indices which have alternate versions defined |
| 4282 | // if the FeatureVariation's table and the alternate version(s) intersect the |
| 4283 | // set of lookup indices. |
| 4284 | hb_set_t alternate_feature_indices; |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4285 | get_feature_variations ().closure_features (lookup_indices, &alternate_feature_indices); |
Behdad Esfahbod | fad452b | 2021-08-16 20:48:24 -0600 | [diff] [blame] | 4286 | if (unlikely (alternate_feature_indices.in_error())) |
| 4287 | { |
| 4288 | feature_indices->err (); |
Garret Rieger | 718bf5a | 2020-09-29 13:16:01 -0700 | [diff] [blame] | 4289 | return; |
| 4290 | } |
| 4291 | #endif |
| 4292 | |
| 4293 | for (unsigned i : feature_indices->iter()) |
Qunxin Liu | 8ffc9ad | 2019-10-31 15:59:02 -0700 | [diff] [blame] | 4294 | { |
Qunxin Liu | d7c012a | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 4295 | const Feature& f = get_feature (i); |
Garret Rieger | c35d786 | 2021-04-01 14:32:38 -0700 | [diff] [blame] | 4296 | hb_tag_t tag = get_feature_tag (i); |
| 4297 | if (tag == HB_TAG ('p', 'r', 'e', 'f')) |
| 4298 | // Note: Never ever drop feature 'pref', even if it's empty. |
| 4299 | // HarfBuzz chooses shaper for Khmer based on presence of this |
| 4300 | // feature. See thread at: |
| 4301 | // http://lists.freedesktop.org/archives/harfbuzz/2012-November/002660.html |
| 4302 | continue; |
Garret Rieger | 718bf5a | 2020-09-29 13:16:01 -0700 | [diff] [blame] | 4303 | |
Qunxin Liu | ca418ca | 2021-11-17 16:42:08 -0800 | [diff] [blame] | 4304 | |
| 4305 | if (!f.featureParams.is_null () && |
| 4306 | tag == HB_TAG ('s', 'i', 'z', 'e')) |
| 4307 | continue; |
| 4308 | |
| 4309 | if (!f.intersects_lookup_indexes (lookup_indices) |
Behdad Esfahbod | 2902529 | 2021-03-02 15:05:22 -0700 | [diff] [blame] | 4310 | #ifndef HB_NO_VAR |
| 4311 | && !alternate_feature_indices.has (i) |
| 4312 | #endif |
| 4313 | ) |
| 4314 | feature_indices->del (i); |
Qunxin Liu | 8ffc9ad | 2019-10-31 15:59:02 -0700 | [diff] [blame] | 4315 | } |
Qunxin Liu | 8ffc9ad | 2019-10-31 15:59:02 -0700 | [diff] [blame] | 4316 | } |
| 4317 | |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 4318 | template <typename T> |
| 4319 | struct accelerator_t |
| 4320 | { |
Behdad Esfahbod | ac1bb3e | 2022-01-20 11:47:17 -0700 | [diff] [blame] | 4321 | accelerator_t (hb_face_t *face) |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 4322 | { |
Ebrahim Byagowi | ba22df3 | 2020-03-10 10:42:20 +0330 | [diff] [blame] | 4323 | this->table = hb_sanitize_context_t ().reference_table<T> (face); |
Behdad Esfahbod | 5671947 | 2020-06-05 12:57:23 -0700 | [diff] [blame] | 4324 | if (unlikely (this->table->is_blocklisted (this->table.get_blob (), face))) |
Behdad Esfahbod | 574d888 | 2018-11-25 16:51:22 -0500 | [diff] [blame] | 4325 | { |
| 4326 | hb_blob_destroy (this->table.get_blob ()); |
| 4327 | this->table = hb_blob_get_empty (); |
| 4328 | } |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 4329 | |
Behdad Esfahbod | b929100 | 2018-08-26 01:15:47 -0700 | [diff] [blame] | 4330 | this->lookup_count = table->get_lookup_count (); |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 4331 | |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 4332 | this->accels = (hb_ot_layout_lookup_accelerator_t *) hb_calloc (this->lookup_count, sizeof (hb_ot_layout_lookup_accelerator_t)); |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 4333 | if (unlikely (!this->accels)) |
Dominik Röttsches | a5f6f86 | 2020-10-23 14:25:05 +0300 | [diff] [blame] | 4334 | { |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 4335 | this->lookup_count = 0; |
Behdad Esfahbod | 53806e5 | 2020-11-25 11:51:37 -0700 | [diff] [blame] | 4336 | this->table.destroy (); |
Dominik Röttsches | a5f6f86 | 2020-10-23 14:25:05 +0300 | [diff] [blame] | 4337 | this->table = hb_blob_get_empty (); |
| 4338 | } |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 4339 | |
| 4340 | for (unsigned int i = 0; i < this->lookup_count; i++) |
Behdad Esfahbod | b929100 | 2018-08-26 01:15:47 -0700 | [diff] [blame] | 4341 | this->accels[i].init (table->get_lookup (i)); |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 4342 | } |
Behdad Esfahbod | ac1bb3e | 2022-01-20 11:47:17 -0700 | [diff] [blame] | 4343 | ~accelerator_t () |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 4344 | { |
Behdad Esfahbod | b929100 | 2018-08-26 01:15:47 -0700 | [diff] [blame] | 4345 | for (unsigned int i = 0; i < this->lookup_count; i++) |
| 4346 | this->accels[i].fini (); |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 4347 | hb_free (this->accels); |
Behdad Esfahbod | da6aa3b | 2018-11-11 11:40:57 -0500 | [diff] [blame] | 4348 | this->table.destroy (); |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 4349 | } |
| 4350 | |
Behdad Esfahbod | 5d0078a | 2018-11-10 23:52:15 -0500 | [diff] [blame] | 4351 | hb_blob_ptr_t<T> table; |
Behdad Esfahbod | b929100 | 2018-08-26 01:15:47 -0700 | [diff] [blame] | 4352 | unsigned int lookup_count; |
| 4353 | hb_ot_layout_lookup_accelerator_t *accels; |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 4354 | }; |
| 4355 | |
Behdad Esfahbod | 212aba6 | 2009-05-24 00:50:27 -0400 | [diff] [blame] | 4356 | protected: |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4357 | union { |
Behdad Esfahbod | 5fd0a3f | 2022-07-11 13:10:04 -0600 | [diff] [blame] | 4358 | FixedVersion<> version; /* Version identifier */ |
| 4359 | GSUBGPOSVersion1_2<SmallTypes> version1; |
Behdad Esfahbod | 5a9c793 | 2022-07-22 21:33:15 -0600 | [diff] [blame] | 4360 | #ifndef HB_NO_BEYOND_64K |
Behdad Esfahbod | 99f017f | 2022-07-11 13:13:19 -0600 | [diff] [blame] | 4361 | GSUBGPOSVersion1_2<MediumTypes> version2; |
| 4362 | #endif |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4363 | } u; |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 4364 | public: |
Behdad Esfahbod | 6d0e3e6 | 2022-07-11 12:28:53 -0600 | [diff] [blame] | 4365 | DEFINE_SIZE_MIN (4); |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 4366 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 4367 | |
Behdad Esfahbod | 6f20f72 | 2009-05-17 20:28:01 -0400 | [diff] [blame] | 4368 | |
Behdad Esfahbod | 7d52e66 | 2012-11-16 18:49:54 -0800 | [diff] [blame] | 4369 | } /* namespace OT */ |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 4370 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 4371 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 4372 | #endif /* HB_OT_LAYOUT_GSUBGPOS_HH */ |