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 | |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 112 | if (!done_lookups_glyph_set->get (lookup_index)) |
| 113 | { |
Behdad Esfahbod | 9ed5f04 | 2021-03-29 18:14:30 -0600 | [diff] [blame] | 114 | hb_set_t* empty_set = hb_set_create (); |
| 115 | if (unlikely (!done_lookups_glyph_set->set (lookup_index, empty_set))) |
| 116 | { |
| 117 | hb_set_destroy (empty_set); |
| 118 | return true; |
| 119 | } |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 120 | } |
Garret Rieger | 52df6b9 | 2021-03-29 16:17:27 -0700 | [diff] [blame] | 121 | |
Garret Rieger | 9f77a0c | 2021-03-30 14:10:45 -0700 | [diff] [blame] | 122 | hb_set_clear (done_lookups_glyph_set->get (lookup_index)); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | 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] | 126 | if (unlikely (covered_glyph_set->in_error ())) |
Garret Rieger | 64122b5 | 2021-04-05 12:53:08 -0700 | [diff] [blame] | 127 | return true; |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 128 | if (parent_active_glyphs ().is_subset (*covered_glyph_set)) |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 129 | return true; |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 130 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 131 | covered_glyph_set->union_ (parent_active_glyphs ()); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 132 | return false; |
Garret Rieger | 45186b9 | 2018-06-05 17:14:42 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 135 | const hb_set_t& previous_parent_active_glyphs () { |
| 136 | if (active_glyphs_stack.length <= 1) |
| 137 | return *glyphs; |
| 138 | |
| 139 | return active_glyphs_stack[active_glyphs_stack.length - 2]; |
| 140 | } |
| 141 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 142 | const hb_set_t& parent_active_glyphs () |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 143 | { |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 144 | if (!active_glyphs_stack) |
| 145 | return *glyphs; |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 146 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 147 | return active_glyphs_stack.tail (); |
| 148 | } |
| 149 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 150 | hb_set_t& push_cur_active_glyphs () |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 151 | { |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 152 | return *active_glyphs_stack.push (); |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | bool pop_cur_done_glyphs () |
| 156 | { |
| 157 | if (active_glyphs_stack.length < 1) |
| 158 | return false; |
| 159 | |
| 160 | active_glyphs_stack.pop (); |
| 161 | return true; |
| 162 | } |
| 163 | |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 164 | hb_face_t *face; |
Behdad Esfahbod | 6a9be5b | 2012-04-23 22:23:17 -0400 | [diff] [blame] | 165 | hb_set_t *glyphs; |
Behdad Esfahbod | ede1a71 | 2019-01-09 10:45:53 -0800 | [diff] [blame] | 166 | hb_set_t output[1]; |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 167 | hb_vector_t<hb_set_t> active_glyphs_stack; |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 168 | recurse_func_t recurse_func; |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 169 | unsigned int nesting_level_left; |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 170 | |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 171 | hb_closure_context_t (hb_face_t *face_, |
Behdad Esfahbod | 6a9be5b | 2012-04-23 22:23:17 -0400 | [diff] [blame] | 172 | hb_set_t *glyphs_, |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 173 | hb_map_t *done_lookups_glyph_count_, |
Behdad Esfahbod | 394f772 | 2021-11-19 11:49:23 -0700 | [diff] [blame] | 174 | hb_hashmap_t<unsigned, hb_set_t *> *done_lookups_glyph_set_, |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 175 | unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) : |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 176 | face (face_), |
| 177 | glyphs (glyphs_), |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 178 | recurse_func (nullptr), |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 179 | nesting_level_left (nesting_level_left_), |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 180 | done_lookups_glyph_count (done_lookups_glyph_count_), |
| 181 | done_lookups_glyph_set (done_lookups_glyph_set_), |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 182 | lookup_count (0) |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 183 | {} |
Behdad Esfahbod | 9b34677 | 2012-11-23 17:55:40 -0500 | [diff] [blame] | 184 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 185 | ~hb_closure_context_t () { flush (); } |
Behdad Esfahbod | c38bd40 | 2018-07-24 09:43:27 -0700 | [diff] [blame] | 186 | |
Behdad Esfahbod | 9b34677 | 2012-11-23 17:55:40 -0500 | [diff] [blame] | 187 | void set_recurse_func (recurse_func_t func) { recurse_func = func; } |
Garret Rieger | 45186b9 | 2018-06-05 17:14:42 -0700 | [diff] [blame] | 188 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 189 | void flush () |
Behdad Esfahbod | c38bd40 | 2018-07-24 09:43:27 -0700 | [diff] [blame] | 190 | { |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 191 | output->del_range (face->get_num_glyphs (), HB_SET_VALUE_INVALID); /* Remove invalid glyphs. */ |
| 192 | glyphs->union_ (*output); |
| 193 | output->clear (); |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 194 | active_glyphs_stack.pop (); |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 195 | active_glyphs_stack.reset (); |
Behdad Esfahbod | c38bd40 | 2018-07-24 09:43:27 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Garret Rieger | 45186b9 | 2018-06-05 17:14:42 -0700 | [diff] [blame] | 198 | private: |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 199 | hb_map_t *done_lookups_glyph_count; |
Behdad Esfahbod | 394f772 | 2021-11-19 11:49:23 -0700 | [diff] [blame] | 200 | hb_hashmap_t<unsigned, hb_set_t *> *done_lookups_glyph_set; |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 201 | unsigned int lookup_count; |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 202 | }; |
| 203 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 204 | |
| 205 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 206 | struct hb_closure_lookups_context_t : |
Behdad Esfahbod | 25aec02 | 2020-06-18 16:58:01 -0700 | [diff] [blame] | 207 | hb_dispatch_context_t<hb_closure_lookups_context_t> |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 208 | { |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 209 | typedef return_t (*recurse_func_t) (hb_closure_lookups_context_t *c, unsigned lookup_index); |
| 210 | template <typename T> |
| 211 | return_t dispatch (const T &obj) { obj.closure_lookups (this); return hb_empty_t (); } |
| 212 | static return_t default_return_value () { return hb_empty_t (); } |
| 213 | void recurse (unsigned lookup_index) |
| 214 | { |
| 215 | if (unlikely (nesting_level_left == 0 || !recurse_func)) |
| 216 | return; |
| 217 | |
| 218 | /* Return if new lookup was recursed to before. */ |
Garret Rieger | 77507a1 | 2021-12-10 14:10:04 -0800 | [diff] [blame] | 219 | if (lookup_limit_exceeded () |
| 220 | || visited_lookups->in_error () |
| 221 | || visited_lookups->has (lookup_index)) |
| 222 | // Don't increment lookup count here, that will be done in the call to closure_lookups() |
| 223 | // made by recurse_func. |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 224 | return; |
| 225 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 226 | nesting_level_left--; |
| 227 | recurse_func (this, lookup_index); |
| 228 | nesting_level_left++; |
| 229 | } |
| 230 | |
| 231 | void set_lookup_visited (unsigned lookup_index) |
| 232 | { visited_lookups->add (lookup_index); } |
| 233 | |
| 234 | void set_lookup_inactive (unsigned lookup_index) |
| 235 | { inactive_lookups->add (lookup_index); } |
| 236 | |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 237 | bool lookup_limit_exceeded () |
Qunxin Liu | 706014f | 2021-12-01 20:20:12 -0800 | [diff] [blame] | 238 | { |
| 239 | bool ret = lookup_count > HB_MAX_LOOKUP_VISIT_COUNT; |
| 240 | if (ret) |
| 241 | DEBUG_MSG (SUBSET, nullptr, "lookup visit count limit exceeded in lookup closure!"); |
| 242 | return ret; } |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 243 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 244 | bool is_lookup_visited (unsigned lookup_index) |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 245 | { |
Qunxin Liu | 706014f | 2021-12-01 20:20:12 -0800 | [diff] [blame] | 246 | if (unlikely (lookup_count++ > HB_MAX_LOOKUP_VISIT_COUNT)) |
| 247 | { |
| 248 | DEBUG_MSG (SUBSET, nullptr, "total visited lookup count %u exceeds max limit, lookup %u is dropped.", |
| 249 | lookup_count, lookup_index); |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 250 | return true; |
Qunxin Liu | 706014f | 2021-12-01 20:20:12 -0800 | [diff] [blame] | 251 | } |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 252 | |
Behdad Esfahbod | d7e2a51 | 2021-02-11 10:55:03 -0700 | [diff] [blame] | 253 | if (unlikely (visited_lookups->in_error ())) |
Garret Rieger | 9562239 | 2020-08-12 13:01:22 -0700 | [diff] [blame] | 254 | return true; |
| 255 | |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 256 | return visited_lookups->has (lookup_index); |
| 257 | } |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 258 | |
| 259 | hb_face_t *face; |
| 260 | const hb_set_t *glyphs; |
| 261 | recurse_func_t recurse_func; |
| 262 | unsigned int nesting_level_left; |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 263 | |
| 264 | hb_closure_lookups_context_t (hb_face_t *face_, |
| 265 | const hb_set_t *glyphs_, |
| 266 | hb_set_t *visited_lookups_, |
| 267 | hb_set_t *inactive_lookups_, |
| 268 | unsigned nesting_level_left_ = HB_MAX_NESTING_LEVEL) : |
| 269 | face (face_), |
| 270 | glyphs (glyphs_), |
| 271 | recurse_func (nullptr), |
| 272 | nesting_level_left (nesting_level_left_), |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 273 | visited_lookups (visited_lookups_), |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 274 | inactive_lookups (inactive_lookups_), |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 275 | lookup_count (0) {} |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 276 | |
| 277 | void set_recurse_func (recurse_func_t func) { recurse_func = func; } |
| 278 | |
| 279 | private: |
| 280 | hb_set_t *visited_lookups; |
| 281 | hb_set_t *inactive_lookups; |
Garret Rieger | 834a224 | 2020-03-12 03:02:36 -0700 | [diff] [blame] | 282 | unsigned int lookup_count; |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 283 | }; |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 284 | |
Behdad Esfahbod | 77a1a2b | 2015-10-09 12:20:58 -0400 | [diff] [blame] | 285 | struct hb_would_apply_context_t : |
Behdad Esfahbod | 8d0a90a | 2020-06-18 16:53:54 -0700 | [diff] [blame] | 286 | hb_dispatch_context_t<hb_would_apply_context_t, bool> |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 287 | { |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 288 | template <typename T> |
Behdad Esfahbod | c14efb8 | 2019-05-05 09:54:58 -0700 | [diff] [blame] | 289 | return_t dispatch (const T &obj) { return obj.would_apply (this); } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 290 | static return_t default_return_value () { return false; } |
Behdad Esfahbod | 7b912c1 | 2013-01-04 01:25:27 -0600 | [diff] [blame] | 291 | bool stop_sublookup_iteration (return_t r) const { return r; } |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 292 | |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 293 | hb_face_t *face; |
Behdad Esfahbod | 472f229 | 2012-08-07 22:25:24 -0400 | [diff] [blame] | 294 | const hb_codepoint_t *glyphs; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 295 | unsigned int len; |
Behdad Esfahbod | d9b204d | 2012-08-23 16:22:28 -0400 | [diff] [blame] | 296 | bool zero_context; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 297 | |
| 298 | hb_would_apply_context_t (hb_face_t *face_, |
Behdad Esfahbod | 472f229 | 2012-08-07 22:25:24 -0400 | [diff] [blame] | 299 | const hb_codepoint_t *glyphs_, |
| 300 | unsigned int len_, |
Behdad Esfahbod | 2bd9fe3 | 2012-09-04 15:15:19 -0400 | [diff] [blame] | 301 | bool zero_context_) : |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 302 | face (face_), |
Behdad Esfahbod | 472f229 | 2012-08-07 22:25:24 -0400 | [diff] [blame] | 303 | glyphs (glyphs_), |
| 304 | len (len_), |
Behdad Esfahbod | 70d6696 | 2020-06-18 17:09:39 -0700 | [diff] [blame] | 305 | zero_context (zero_context_) {} |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 306 | }; |
| 307 | |
Behdad Esfahbod | 77a1a2b | 2015-10-09 12:20:58 -0400 | [diff] [blame] | 308 | struct hb_collect_glyphs_context_t : |
Behdad Esfahbod | 25aec02 | 2020-06-18 16:58:01 -0700 | [diff] [blame] | 309 | hb_dispatch_context_t<hb_collect_glyphs_context_t> |
Behdad Esfahbod | e8cfdd7 | 2012-11-16 19:07:06 -0800 | [diff] [blame] | 310 | { |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 311 | 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] | 312 | template <typename T> |
Behdad Esfahbod | 7df3ecf | 2019-05-10 20:43:26 -0700 | [diff] [blame] | 313 | return_t dispatch (const T &obj) { obj.collect_glyphs (this); return hb_empty_t (); } |
| 314 | static return_t default_return_value () { return hb_empty_t (); } |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 315 | void recurse (unsigned int lookup_index) |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 316 | { |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 317 | if (unlikely (nesting_level_left == 0 || !recurse_func)) |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 318 | return; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 319 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 320 | /* 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] | 321 | * 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] | 322 | * glyphs in the recursion. If output is not requested, we can go home now. |
| 323 | * |
| 324 | * Note further, that the above is not exactly correct. A recursed lookup |
| 325 | * is allowed to match input that is not matched in the context, but that's |
| 326 | * not how most fonts are built. It's possible to relax that and recurse |
| 327 | * with all sets here if it proves to be an issue. |
| 328 | */ |
Behdad Esfahbod | 4a350d0 | 2012-12-04 17:13:09 -0500 | [diff] [blame] | 329 | |
| 330 | if (output == hb_set_get_empty ()) |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 331 | return; |
Behdad Esfahbod | 4a350d0 | 2012-12-04 17:13:09 -0500 | [diff] [blame] | 332 | |
Behdad Esfahbod | fde3e4a | 2014-10-29 11:23:08 -0700 | [diff] [blame] | 333 | /* Return if new lookup was recursed to before. */ |
Behdad Esfahbod | 8b9d9b7 | 2017-10-22 17:48:06 -0400 | [diff] [blame] | 334 | if (recursed_lookups->has (lookup_index)) |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 335 | return; |
Behdad Esfahbod | fde3e4a | 2014-10-29 11:23:08 -0700 | [diff] [blame] | 336 | |
Behdad Esfahbod | 4a350d0 | 2012-12-04 17:13:09 -0500 | [diff] [blame] | 337 | hb_set_t *old_before = before; |
| 338 | hb_set_t *old_input = input; |
| 339 | hb_set_t *old_after = after; |
| 340 | before = input = after = hb_set_get_empty (); |
Behdad Esfahbod | 1bcfa06 | 2012-12-04 16:58:09 -0500 | [diff] [blame] | 341 | |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 342 | nesting_level_left--; |
Behdad Esfahbod | 4a350d0 | 2012-12-04 17:13:09 -0500 | [diff] [blame] | 343 | recurse_func (this, lookup_index); |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 344 | nesting_level_left++; |
Behdad Esfahbod | 4a350d0 | 2012-12-04 17:13:09 -0500 | [diff] [blame] | 345 | |
| 346 | before = old_before; |
| 347 | input = old_input; |
| 348 | after = old_after; |
| 349 | |
Behdad Esfahbod | 8b9d9b7 | 2017-10-22 17:48:06 -0400 | [diff] [blame] | 350 | recursed_lookups->add (lookup_index); |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Behdad Esfahbod | e8cfdd7 | 2012-11-16 19:07:06 -0800 | [diff] [blame] | 353 | hb_face_t *face; |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 354 | hb_set_t *before; |
| 355 | hb_set_t *input; |
| 356 | hb_set_t *after; |
| 357 | hb_set_t *output; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 358 | recurse_func_t recurse_func; |
Behdad Esfahbod | 8b9d9b7 | 2017-10-22 17:48:06 -0400 | [diff] [blame] | 359 | hb_set_t *recursed_lookups; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 360 | unsigned int nesting_level_left; |
Behdad Esfahbod | e8cfdd7 | 2012-11-16 19:07:06 -0800 | [diff] [blame] | 361 | |
| 362 | hb_collect_glyphs_context_t (hb_face_t *face_, |
Ebrahim Byagowi | 6310943 | 2018-10-13 14:00:05 +0330 | [diff] [blame] | 363 | hb_set_t *glyphs_before, /* OUT. May be NULL */ |
| 364 | hb_set_t *glyphs_input, /* OUT. May be NULL */ |
| 365 | hb_set_t *glyphs_after, /* OUT. May be NULL */ |
| 366 | hb_set_t *glyphs_output, /* OUT. May be NULL */ |
Behdad Esfahbod | 5ba4504 | 2015-11-02 15:43:08 -0800 | [diff] [blame] | 367 | unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) : |
Behdad Esfahbod | e8cfdd7 | 2012-11-16 19:07:06 -0800 | [diff] [blame] | 368 | face (face_), |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 369 | before (glyphs_before ? glyphs_before : hb_set_get_empty ()), |
| 370 | input (glyphs_input ? glyphs_input : hb_set_get_empty ()), |
| 371 | after (glyphs_after ? glyphs_after : hb_set_get_empty ()), |
| 372 | output (glyphs_output ? glyphs_output : hb_set_get_empty ()), |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 373 | recurse_func (nullptr), |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 374 | recursed_lookups (hb_set_create ()), |
Behdad Esfahbod | 70d6696 | 2020-06-18 17:09:39 -0700 | [diff] [blame] | 375 | nesting_level_left (nesting_level_left_) {} |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 376 | ~hb_collect_glyphs_context_t () { hb_set_destroy (recursed_lookups); } |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 377 | |
| 378 | void set_recurse_func (recurse_func_t func) { recurse_func = func; } |
Behdad Esfahbod | e8cfdd7 | 2012-11-16 19:07:06 -0800 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | |
| 382 | |
Behdad Esfahbod | 8e36ccf | 2015-02-17 19:15:34 +0300 | [diff] [blame] | 383 | template <typename set_t> |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 384 | struct hb_collect_coverage_context_t : |
Behdad Esfahbod | 8d0a90a | 2020-06-18 16:53:54 -0700 | [diff] [blame] | 385 | 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] | 386 | { |
Behdad Esfahbod | dc492d7 | 2020-06-18 17:03:05 -0700 | [diff] [blame] | 387 | 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] | 388 | template <typename T> |
Behdad Esfahbod | c14efb8 | 2019-05-05 09:54:58 -0700 | [diff] [blame] | 389 | return_t dispatch (const T &obj) { return obj.get_coverage (); } |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 390 | static return_t default_return_value () { return Null (Coverage); } |
Behdad Esfahbod | 8e36ccf | 2015-02-17 19:15:34 +0300 | [diff] [blame] | 391 | bool stop_sublookup_iteration (return_t r) const |
| 392 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 393 | r.collect_coverage (set); |
Behdad Esfahbod | 8e36ccf | 2015-02-17 19:15:34 +0300 | [diff] [blame] | 394 | return false; |
| 395 | } |
Behdad Esfahbod | 1d67ef9 | 2012-11-22 16:47:53 -0500 | [diff] [blame] | 396 | |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 397 | hb_collect_coverage_context_t (set_t *set_) : |
Behdad Esfahbod | 70d6696 | 2020-06-18 17:09:39 -0700 | [diff] [blame] | 398 | set (set_) {} |
Behdad Esfahbod | a1733db | 2012-11-23 16:40:04 -0500 | [diff] [blame] | 399 | |
Behdad Esfahbod | 8e36ccf | 2015-02-17 19:15:34 +0300 | [diff] [blame] | 400 | set_t *set; |
Behdad Esfahbod | 2005fa5 | 2012-11-22 14:38:10 -0500 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 404 | struct hb_ot_apply_context_t : |
| 405 | 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] | 406 | { |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 407 | struct matcher_t |
| 408 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 409 | matcher_t () : |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 410 | lookup_props (0), |
Behdad Esfahbod | cfc507c | 2013-02-14 10:40:12 -0500 | [diff] [blame] | 411 | ignore_zwnj (false), |
| 412 | ignore_zwj (false), |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 413 | mask (-1), |
Behdad Esfahbod | e8f3397 | 2022-03-28 12:07:05 -0600 | [diff] [blame^] | 414 | syllable {0}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 415 | match_func (nullptr), |
Ebrahim Byagowi | f7a08cd | 2018-10-30 11:29:09 +0330 | [diff] [blame] | 416 | match_data (nullptr) {} |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 417 | |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 418 | typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 419 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 420 | void set_ignore_zwnj (bool ignore_zwnj_) { ignore_zwnj = ignore_zwnj_; } |
| 421 | void set_ignore_zwj (bool ignore_zwj_) { ignore_zwj = ignore_zwj_; } |
| 422 | void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; } |
| 423 | void set_mask (hb_mask_t mask_) { mask = mask_; } |
| 424 | void set_syllable (uint8_t syllable_) { syllable = syllable_; } |
| 425 | void set_match_func (match_func_t match_func_, |
Ebrahim Byagowi | d0e2add | 2020-07-18 22:14:52 +0430 | [diff] [blame] | 426 | const void *match_data_) |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 427 | { match_func = match_func_; match_data = match_data_; } |
| 428 | |
Behdad Esfahbod | 2b2a6e8 | 2013-02-21 15:07:03 -0500 | [diff] [blame] | 429 | enum may_match_t { |
| 430 | MATCH_NO, |
| 431 | MATCH_YES, |
| 432 | MATCH_MAYBE |
| 433 | }; |
| 434 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 435 | may_match_t may_match (const hb_glyph_info_t &info, |
Behdad Esfahbod | 085793d | 2019-04-24 10:15:59 -0400 | [diff] [blame] | 436 | const HBUINT16 *glyph_data) const |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 437 | { |
Behdad Esfahbod | 2b2a6e8 | 2013-02-21 15:07:03 -0500 | [diff] [blame] | 438 | if (!(info.mask & mask) || |
| 439 | (syllable && syllable != info.syllable ())) |
| 440 | return MATCH_NO; |
| 441 | |
| 442 | if (match_func) |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 443 | return match_func (info.codepoint, *glyph_data, match_data) ? MATCH_YES : MATCH_NO; |
Behdad Esfahbod | 2b2a6e8 | 2013-02-21 15:07:03 -0500 | [diff] [blame] | 444 | |
| 445 | return MATCH_MAYBE; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | enum may_skip_t { |
| 449 | SKIP_NO, |
| 450 | SKIP_YES, |
| 451 | SKIP_MAYBE |
| 452 | }; |
| 453 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 454 | may_skip_t may_skip (const hb_ot_apply_context_t *c, |
| 455 | const hb_glyph_info_t &info) const |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 456 | { |
Behdad Esfahbod | b98c5db | 2014-07-16 13:44:01 -0400 | [diff] [blame] | 457 | if (!c->check_glyph_property (&info, lookup_props)) |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 458 | return SKIP_YES; |
| 459 | |
Khaled Hosny | 06cfe3f | 2017-05-17 21:32:47 +0300 | [diff] [blame] | 460 | if (unlikely (_hb_glyph_info_is_default_ignorable_and_not_hidden (&info) && |
Behdad Esfahbod | 0b45479 | 2013-02-14 10:46:52 -0500 | [diff] [blame] | 461 | (ignore_zwnj || !_hb_glyph_info_is_zwnj (&info)) && |
Behdad Esfahbod | 4ba796b | 2015-07-22 17:41:31 +0100 | [diff] [blame] | 462 | (ignore_zwj || !_hb_glyph_info_is_zwj (&info)))) |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 463 | return SKIP_MAYBE; |
| 464 | |
| 465 | return SKIP_NO; |
| 466 | } |
| 467 | |
| 468 | protected: |
| 469 | unsigned int lookup_props; |
| 470 | bool ignore_zwnj; |
Behdad Esfahbod | 0b45479 | 2013-02-14 10:46:52 -0500 | [diff] [blame] | 471 | bool ignore_zwj; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 472 | hb_mask_t mask; |
| 473 | uint8_t syllable; |
| 474 | match_func_t match_func; |
| 475 | const void *match_data; |
| 476 | }; |
| 477 | |
Behdad Esfahbod | 6962669 | 2015-01-29 13:08:41 +0100 | [diff] [blame] | 478 | struct skipping_iterator_t |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 479 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 480 | void init (hb_ot_apply_context_t *c_, bool context_match = false) |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 481 | { |
Behdad Esfahbod | 514564f | 2015-01-29 13:48:48 +0100 | [diff] [blame] | 482 | c = c_; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 483 | match_glyph_data = nullptr; |
| 484 | matcher.set_match_func (nullptr, nullptr); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 485 | matcher.set_lookup_props (c->lookup_props); |
Behdad Esfahbod | 3583fb0 | 2018-09-23 22:33:38 -0400 | [diff] [blame] | 486 | /* 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] | 487 | 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] | 488 | /* Ignore ZWJ if we are matching context, or asked to. */ |
| 489 | matcher.set_ignore_zwj (context_match || c->auto_zwj); |
Behdad Esfahbod | 514564f | 2015-01-29 13:48:48 +0100 | [diff] [blame] | 490 | matcher.set_mask (context_match ? -1 : c->lookup_mask); |
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_, |
| 497 | const void *match_data_, |
| 498 | const HBUINT16 glyph_data[]) |
Behdad Esfahbod | c074ebc | 2013-02-13 11:22:42 -0500 | [diff] [blame] | 499 | { |
Behdad Esfahbod | 4a6b1ee | 2015-10-21 11:20:55 -0200 | [diff] [blame] | 500 | matcher.set_match_func (match_func_, match_data_); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 501 | match_glyph_data = glyph_data; |
Behdad Esfahbod | c074ebc | 2013-02-13 11:22:42 -0500 | [diff] [blame] | 502 | } |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 503 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 504 | void reset (unsigned int start_index_, |
Ebrahim Byagowi | 08428a1 | 2020-04-24 23:45:17 +0430 | [diff] [blame] | 505 | unsigned int num_items_) |
Behdad Esfahbod | b051be5 | 2015-01-29 13:40:39 +0100 | [diff] [blame] | 506 | { |
| 507 | idx = start_index_; |
| 508 | num_items = num_items_; |
| 509 | end = c->buffer->len; |
| 510 | matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0); |
| 511 | } |
| 512 | |
Ebrahim Byagowi | 64a45be | 2019-11-09 12:25:33 +0330 | [diff] [blame] | 513 | void reject () |
| 514 | { |
| 515 | num_items++; |
| 516 | if (match_glyph_data) match_glyph_data--; |
| 517 | } |
Behdad Esfahbod | 6962669 | 2015-01-29 13:08:41 +0100 | [diff] [blame] | 518 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 519 | matcher_t::may_skip_t |
| 520 | may_skip (const hb_glyph_info_t &info) const |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 521 | { return matcher.may_skip (c, info); } |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 522 | |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 523 | bool next (unsigned *unsafe_to = nullptr) |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 524 | { |
Behdad Esfahbod | 506ffeb | 2012-01-18 16:07:53 -0500 | [diff] [blame] | 525 | assert (num_items > 0); |
Behdad Esfahbod | 37d13ac | 2015-01-29 11:38:01 +0100 | [diff] [blame] | 526 | while (idx + num_items < end) |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 527 | { |
Behdad Esfahbod | a4a48fe | 2012-01-17 18:08:41 -0500 | [diff] [blame] | 528 | idx++; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 529 | const hb_glyph_info_t &info = c->buffer->info[idx]; |
| 530 | |
Behdad Esfahbod | ff93ac8 | 2013-02-21 14:51:40 -0500 | [diff] [blame] | 531 | matcher_t::may_skip_t skip = matcher.may_skip (c, info); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 532 | if (unlikely (skip == matcher_t::SKIP_YES)) |
| 533 | continue; |
| 534 | |
Behdad Esfahbod | 2b2a6e8 | 2013-02-21 15:07:03 -0500 | [diff] [blame] | 535 | matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data); |
Behdad Esfahbod | 722e8b8 | 2013-02-21 15:37:51 -0500 | [diff] [blame] | 536 | if (match == matcher_t::MATCH_YES || |
| 537 | (match == matcher_t::MATCH_MAYBE && |
| 538 | skip == matcher_t::SKIP_NO)) |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 539 | { |
| 540 | num_items--; |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 541 | if (match_glyph_data) match_glyph_data++; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 542 | return true; |
| 543 | } |
| 544 | |
| 545 | if (skip == matcher_t::SKIP_NO) |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 546 | { |
| 547 | if (unsafe_to) |
| 548 | *unsafe_to = idx + 1; |
Behdad Esfahbod | 722e8b8 | 2013-02-21 15:37:51 -0500 | [diff] [blame] | 549 | return false; |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 550 | } |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 551 | } |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 552 | if (unsafe_to) |
| 553 | *unsafe_to = end; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 554 | return false; |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 555 | } |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 556 | bool prev (unsigned *unsafe_from = nullptr) |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 557 | { |
Behdad Esfahbod | 506ffeb | 2012-01-18 16:07:53 -0500 | [diff] [blame] | 558 | assert (num_items > 0); |
Behdad Esfahbod | 18a06f8 | 2018-07-05 14:03:48 +0430 | [diff] [blame] | 559 | while (idx > num_items - 1) |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 560 | { |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 561 | idx--; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 562 | const hb_glyph_info_t &info = c->buffer->out_info[idx]; |
| 563 | |
Behdad Esfahbod | ff93ac8 | 2013-02-21 14:51:40 -0500 | [diff] [blame] | 564 | matcher_t::may_skip_t skip = matcher.may_skip (c, info); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 565 | if (unlikely (skip == matcher_t::SKIP_YES)) |
| 566 | continue; |
| 567 | |
Behdad Esfahbod | 2b2a6e8 | 2013-02-21 15:07:03 -0500 | [diff] [blame] | 568 | matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data); |
Behdad Esfahbod | 722e8b8 | 2013-02-21 15:37:51 -0500 | [diff] [blame] | 569 | if (match == matcher_t::MATCH_YES || |
| 570 | (match == matcher_t::MATCH_MAYBE && |
| 571 | skip == matcher_t::SKIP_NO)) |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 572 | { |
| 573 | num_items--; |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 574 | if (match_glyph_data) match_glyph_data++; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 575 | return true; |
| 576 | } |
| 577 | |
| 578 | if (skip == matcher_t::SKIP_NO) |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 579 | { |
| 580 | if (unsafe_from) |
| 581 | *unsafe_from = hb_max (1u, idx) - 1u; |
Behdad Esfahbod | 722e8b8 | 2013-02-21 15:37:51 -0500 | [diff] [blame] | 582 | return false; |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 583 | } |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 584 | } |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 585 | if (unsafe_from) |
| 586 | *unsafe_from = 0; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 587 | return false; |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 588 | } |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 589 | |
| 590 | unsigned int idx; |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 591 | protected: |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 592 | hb_ot_apply_context_t *c; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 593 | matcher_t matcher; |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 594 | const HBUINT16 *match_glyph_data; |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 595 | |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 596 | unsigned int num_items; |
Behdad Esfahbod | 6962669 | 2015-01-29 13:08:41 +0100 | [diff] [blame] | 597 | unsigned int end; |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 598 | }; |
| 599 | |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 600 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 601 | const char *get_name () { return "APPLY"; } |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 602 | 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] | 603 | template <typename T> |
Behdad Esfahbod | c14efb8 | 2019-05-05 09:54:58 -0700 | [diff] [blame] | 604 | return_t dispatch (const T &obj) { return obj.apply (this); } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 605 | static return_t default_return_value () { return false; } |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 606 | bool stop_sublookup_iteration (return_t r) const { return r; } |
Behdad Esfahbod | 12757b6 | 2018-01-26 18:14:05 -0800 | [diff] [blame] | 607 | return_t recurse (unsigned int sub_lookup_index) |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 608 | { |
Behdad Esfahbod | baf7779 | 2017-11-14 21:53:48 -0800 | [diff] [blame] | 609 | if (unlikely (nesting_level_left == 0 || !recurse_func || buffer->max_ops-- <= 0)) |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 610 | return default_return_value (); |
| 611 | |
| 612 | nesting_level_left--; |
Behdad Esfahbod | 12757b6 | 2018-01-26 18:14:05 -0800 | [diff] [blame] | 613 | bool ret = recurse_func (this, sub_lookup_index); |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 614 | nesting_level_left++; |
| 615 | return ret; |
| 616 | } |
| 617 | |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 618 | skipping_iterator_t iter_input, iter_context; |
| 619 | |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 620 | hb_font_t *font; |
| 621 | hb_face_t *face; |
| 622 | hb_buffer_t *buffer; |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 623 | recurse_func_t recurse_func; |
| 624 | const GDEF &gdef; |
| 625 | const VariationStore &var_store; |
| 626 | |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 627 | hb_direction_t direction; |
| 628 | hb_mask_t lookup_mask; |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 629 | unsigned int table_index; /* GSUB/GPOS */ |
Behdad Esfahbod | 2c8b3b2 | 2015-08-18 14:36:43 +0100 | [diff] [blame] | 630 | unsigned int lookup_index; |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 631 | unsigned int lookup_props; |
| 632 | unsigned int nesting_level_left; |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 633 | |
Behdad Esfahbod | 80de4bc | 2018-09-10 16:24:52 +0200 | [diff] [blame] | 634 | bool has_glyph_classes; |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 635 | bool auto_zwnj; |
| 636 | bool auto_zwj; |
David Corbett | c2a75e0 | 2018-01-25 14:22:03 -0500 | [diff] [blame] | 637 | bool random; |
Behdad Esfahbod | 80de4bc | 2018-09-10 16:24:52 +0200 | [diff] [blame] | 638 | |
Behdad Esfahbod | cfdea88 | 2018-09-11 10:57:48 +0200 | [diff] [blame] | 639 | uint32_t random_state; |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 640 | |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 641 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 642 | hb_ot_apply_context_t (unsigned int table_index_, |
Ebrahim Byagowi | d0e2add | 2020-07-18 22:14:52 +0430 | [diff] [blame] | 643 | hb_font_t *font_, |
| 644 | hb_buffer_t *buffer_) : |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 645 | iter_input (), iter_context (), |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 646 | font (font_), face (font->face), buffer (buffer_), |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 647 | recurse_func (nullptr), |
Behdad Esfahbod | 7dcf8e1 | 2019-06-26 13:44:10 -0700 | [diff] [blame] | 648 | gdef ( |
| 649 | #ifndef HB_NO_OT_LAYOUT |
| 650 | *face->table.GDEF->table |
| 651 | #else |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 652 | Null (GDEF) |
Behdad Esfahbod | 7dcf8e1 | 2019-06-26 13:44:10 -0700 | [diff] [blame] | 653 | #endif |
| 654 | ), |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 655 | var_store (gdef.get_var_store ()), |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 656 | direction (buffer_->props.direction), |
| 657 | lookup_mask (1), |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 658 | table_index (table_index_), |
Behdad Esfahbod | 2c8b3b2 | 2015-08-18 14:36:43 +0100 | [diff] [blame] | 659 | lookup_index ((unsigned int) -1), |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 660 | lookup_props (0), |
| 661 | nesting_level_left (HB_MAX_NESTING_LEVEL), |
Behdad Esfahbod | 80de4bc | 2018-09-10 16:24:52 +0200 | [diff] [blame] | 662 | has_glyph_classes (gdef.has_glyph_classes ()), |
Behdad Esfahbod | cdf1fd0 | 2017-07-14 12:43:34 +0100 | [diff] [blame] | 663 | auto_zwnj (true), |
| 664 | auto_zwj (true), |
David Corbett | c2a75e0 | 2018-01-25 14:22:03 -0500 | [diff] [blame] | 665 | random (false), |
Behdad Esfahbod | d748dc7 | 2018-09-24 18:30:50 -0400 | [diff] [blame] | 666 | random_state (1) { init_iters (); } |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 667 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 668 | void init_iters () |
Behdad Esfahbod | 365576d | 2015-01-29 13:59:42 +0100 | [diff] [blame] | 669 | { |
Behdad Esfahbod | 365576d | 2015-01-29 13:59:42 +0100 | [diff] [blame] | 670 | iter_input.init (this, false); |
| 671 | iter_context.init (this, true); |
| 672 | } |
Behdad Esfahbod | 2cecc38 | 2015-01-29 13:32:05 +0100 | [diff] [blame] | 673 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 674 | void set_lookup_mask (hb_mask_t mask) { lookup_mask = mask; init_iters (); } |
| 675 | void set_auto_zwj (bool auto_zwj_) { auto_zwj = auto_zwj_; init_iters (); } |
| 676 | void set_auto_zwnj (bool auto_zwnj_) { auto_zwnj = auto_zwnj_; init_iters (); } |
| 677 | void set_random (bool random_) { random = random_; } |
| 678 | void set_recurse_func (recurse_func_t func) { recurse_func = func; } |
| 679 | void set_lookup_index (unsigned int lookup_index_) { lookup_index = lookup_index_; } |
| 680 | 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] | 681 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 682 | uint32_t random_number () |
Behdad Esfahbod | 08260c7 | 2018-09-11 10:51:19 +0200 | [diff] [blame] | 683 | { |
Behdad Esfahbod | cfdea88 | 2018-09-11 10:57:48 +0200 | [diff] [blame] | 684 | /* http://www.cplusplus.com/reference/random/minstd_rand/ */ |
| 685 | random_state = random_state * 48271 % 2147483647; |
| 686 | return random_state; |
Behdad Esfahbod | 08260c7 | 2018-09-11 10:51:19 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 689 | bool match_properties_mark (hb_codepoint_t glyph, |
| 690 | unsigned int glyph_props, |
| 691 | unsigned int match_props) const |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 692 | { |
| 693 | /* If using mark filtering sets, the high short of |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 694 | * match_props has the set index. |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 695 | */ |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 696 | if (match_props & LookupFlag::UseMarkFilteringSet) |
| 697 | return gdef.mark_set_covers (match_props >> 16, glyph); |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 698 | |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 699 | /* The second byte of match_props has the meaning |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 700 | * "ignore marks of attachment type different than |
| 701 | * the attachment type specified." |
| 702 | */ |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 703 | if (match_props & LookupFlag::MarkAttachmentType) |
| 704 | return (match_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType); |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 705 | |
| 706 | return true; |
| 707 | } |
| 708 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 709 | bool check_glyph_property (const hb_glyph_info_t *info, |
| 710 | unsigned int match_props) const |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 711 | { |
Behdad Esfahbod | b98c5db | 2014-07-16 13:44:01 -0400 | [diff] [blame] | 712 | hb_codepoint_t glyph = info->codepoint; |
| 713 | unsigned int glyph_props = _hb_glyph_info_get_glyph_props (info); |
| 714 | |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 715 | /* Not covered, if, for example, glyph class is ligature and |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 716 | * match_props includes LookupFlags::IgnoreLigatures |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 717 | */ |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 718 | if (glyph_props & match_props & LookupFlag::IgnoreFlags) |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 719 | return false; |
| 720 | |
Behdad Esfahbod | 5a08ecf | 2012-11-16 13:34:29 -0800 | [diff] [blame] | 721 | if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_PROPS_MARK)) |
Behdad Esfahbod | b931e0b | 2015-04-08 14:39:00 -0700 | [diff] [blame] | 722 | return match_properties_mark (glyph, glyph_props, match_props); |
Behdad Esfahbod | 03f67bc | 2012-07-30 19:47:53 -0400 | [diff] [blame] | 723 | |
| 724 | return true; |
| 725 | } |
| 726 | |
Behdad Esfahbod | fa12f1a | 2022-01-12 15:08:34 -0700 | [diff] [blame] | 727 | void _set_glyph_class (hb_codepoint_t glyph_index, |
Ebrahim Byagowi | 9748ae7 | 2020-08-11 17:52:48 +0430 | [diff] [blame] | 728 | unsigned int class_guess = 0, |
| 729 | bool ligature = false, |
| 730 | bool component = false) const |
Behdad Esfahbod | 60da763 | 2012-07-16 16:13:32 -0400 | [diff] [blame] | 731 | { |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 732 | unsigned int props = _hb_glyph_info_get_glyph_props (&buffer->cur()); |
| 733 | props |= HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED; |
Behdad Esfahbod | 09675a8 | 2013-10-18 01:05:58 +0200 | [diff] [blame] | 734 | if (ligature) |
Behdad Esfahbod | 832a6f9 | 2014-06-04 16:57:42 -0400 | [diff] [blame] | 735 | { |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 736 | props |= HB_OT_LAYOUT_GLYPH_PROPS_LIGATED; |
Behdad Esfahbod | 832a6f9 | 2014-06-04 16:57:42 -0400 | [diff] [blame] | 737 | /* In the only place that the MULTIPLIED bit is used, Uniscribe |
| 738 | * seems to only care about the "last" transformation between |
Bruce Mitchener | 257d0e5 | 2018-10-19 22:49:21 +0700 | [diff] [blame] | 739 | * Ligature and Multiple substitutions. Ie. if you ligate, expand, |
Behdad Esfahbod | 832a6f9 | 2014-06-04 16:57:42 -0400 | [diff] [blame] | 740 | * and ligate again, it forgives the multiplication and acts as |
| 741 | * if only ligation happened. As such, clear MULTIPLIED bit. |
| 742 | */ |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 743 | props &= ~HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED; |
Behdad Esfahbod | 832a6f9 | 2014-06-04 16:57:42 -0400 | [diff] [blame] | 744 | } |
| 745 | if (component) |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 746 | props |= HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED; |
Behdad Esfahbod | 2fca142 | 2012-07-30 18:46:41 -0400 | [diff] [blame] | 747 | if (likely (has_glyph_classes)) |
Behdad Esfahbod | 96c969c | 2022-01-12 15:06:40 -0700 | [diff] [blame] | 748 | { |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 749 | props &= HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE; |
| 750 | _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] | 751 | } |
Behdad Esfahbod | 05bd1b6 | 2012-07-30 19:30:01 -0400 | [diff] [blame] | 752 | else if (class_guess) |
Behdad Esfahbod | 96c969c | 2022-01-12 15:06:40 -0700 | [diff] [blame] | 753 | { |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 754 | props &= HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE; |
| 755 | _hb_glyph_info_set_glyph_props (&buffer->cur(), props | class_guess); |
Behdad Esfahbod | 96c969c | 2022-01-12 15:06:40 -0700 | [diff] [blame] | 756 | } |
| 757 | else |
Behdad Esfahbod | 9408e0d | 2022-01-12 15:07:34 -0700 | [diff] [blame] | 758 | _hb_glyph_info_set_glyph_props (&buffer->cur(), props); |
Behdad Esfahbod | 60da763 | 2012-07-16 16:13:32 -0400 | [diff] [blame] | 759 | } |
Behdad Esfahbod | 4ab9731 | 2012-01-16 22:05:08 -0500 | [diff] [blame] | 760 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 761 | void replace_glyph (hb_codepoint_t glyph_index) const |
Behdad Esfahbod | 3ec77d6 | 2012-06-08 21:44:06 -0400 | [diff] [blame] | 762 | { |
Behdad Esfahbod | fa12f1a | 2022-01-12 15:08:34 -0700 | [diff] [blame] | 763 | _set_glyph_class (glyph_index); |
Behdad Esfahbod | 3f1998a | 2021-03-15 13:33:44 -0600 | [diff] [blame] | 764 | (void) buffer->replace_glyph (glyph_index); |
Behdad Esfahbod | 98370e8 | 2010-10-27 17:39:01 -0400 | [diff] [blame] | 765 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 766 | void replace_glyph_inplace (hb_codepoint_t glyph_index) const |
Behdad Esfahbod | 7fbbf86 | 2012-07-30 18:36:42 -0400 | [diff] [blame] | 767 | { |
Behdad Esfahbod | fa12f1a | 2022-01-12 15:08:34 -0700 | [diff] [blame] | 768 | _set_glyph_class (glyph_index); |
Behdad Esfahbod | 7fbbf86 | 2012-07-30 18:36:42 -0400 | [diff] [blame] | 769 | buffer->cur().codepoint = glyph_index; |
| 770 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 771 | void replace_glyph_with_ligature (hb_codepoint_t glyph_index, |
Behdad Esfahbod | f4cd99f | 2020-04-22 14:45:57 -0700 | [diff] [blame] | 772 | unsigned int class_guess) const |
Behdad Esfahbod | a016174 | 2013-10-18 00:06:30 +0200 | [diff] [blame] | 773 | { |
Behdad Esfahbod | fa12f1a | 2022-01-12 15:08:34 -0700 | [diff] [blame] | 774 | _set_glyph_class (glyph_index, class_guess, true); |
Behdad Esfahbod | 3f1998a | 2021-03-15 13:33:44 -0600 | [diff] [blame] | 775 | (void) buffer->replace_glyph (glyph_index); |
Behdad Esfahbod | a016174 | 2013-10-18 00:06:30 +0200 | [diff] [blame] | 776 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 777 | void output_glyph_for_component (hb_codepoint_t glyph_index, |
Behdad Esfahbod | f4cd99f | 2020-04-22 14:45:57 -0700 | [diff] [blame] | 778 | unsigned int class_guess) const |
Behdad Esfahbod | a016174 | 2013-10-18 00:06:30 +0200 | [diff] [blame] | 779 | { |
Behdad Esfahbod | fa12f1a | 2022-01-12 15:08:34 -0700 | [diff] [blame] | 780 | _set_glyph_class (glyph_index, class_guess, false, true); |
Behdad Esfahbod | 34a1204 | 2021-03-15 14:39:06 -0600 | [diff] [blame] | 781 | (void) buffer->output_glyph (glyph_index); |
Behdad Esfahbod | a016174 | 2013-10-18 00:06:30 +0200 | [diff] [blame] | 782 | } |
Behdad Esfahbod | 1376fb7 | 2010-04-29 02:19:21 -0400 | [diff] [blame] | 783 | }; |
| 784 | |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 785 | |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 786 | struct hb_get_subtables_context_t : |
Behdad Esfahbod | 25aec02 | 2020-06-18 16:58:01 -0700 | [diff] [blame] | 787 | hb_dispatch_context_t<hb_get_subtables_context_t> |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 788 | { |
| 789 | template <typename Type> |
Behdad Esfahbod | a061e47 | 2019-12-10 13:31:50 -0600 | [diff] [blame] | 790 | 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] | 791 | { |
| 792 | const Type *typed_obj = (const Type *) obj; |
| 793 | return typed_obj->apply (c); |
| 794 | } |
| 795 | |
| 796 | typedef bool (*hb_apply_func_t) (const void *obj, OT::hb_ot_apply_context_t *c); |
| 797 | |
| 798 | struct hb_applicable_t |
| 799 | { |
| 800 | template <typename T> |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 801 | void init (const T &obj_, hb_apply_func_t apply_func_) |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 802 | { |
| 803 | obj = &obj_; |
| 804 | apply_func = apply_func_; |
| 805 | digest.init (); |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 806 | obj_.get_coverage ().collect_coverage (&digest); |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 807 | } |
| 808 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 809 | bool apply (OT::hb_ot_apply_context_t *c) const |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 810 | { |
| 811 | return digest.may_have (c->buffer->cur().codepoint) && apply_func (obj, c); |
| 812 | } |
| 813 | |
| 814 | private: |
| 815 | const void *obj; |
| 816 | hb_apply_func_t apply_func; |
| 817 | hb_set_digest_t digest; |
| 818 | }; |
| 819 | |
Behdad Esfahbod | fa333e3 | 2018-12-27 17:56:22 -0500 | [diff] [blame] | 820 | typedef hb_vector_t<hb_applicable_t> array_t; |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 821 | |
| 822 | /* Dispatch interface. */ |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 823 | template <typename T> |
Behdad Esfahbod | c14efb8 | 2019-05-05 09:54:58 -0700 | [diff] [blame] | 824 | return_t dispatch (const T &obj) |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 825 | { |
| 826 | hb_applicable_t *entry = array.push(); |
| 827 | entry->init (obj, apply_to<T>); |
Behdad Esfahbod | 7df3ecf | 2019-05-10 20:43:26 -0700 | [diff] [blame] | 828 | return hb_empty_t (); |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 829 | } |
Behdad Esfahbod | 7df3ecf | 2019-05-10 20:43:26 -0700 | [diff] [blame] | 830 | static return_t default_return_value () { return hb_empty_t (); } |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 831 | |
| 832 | hb_get_subtables_context_t (array_t &array_) : |
Behdad Esfahbod | 1535440 | 2020-06-19 08:30:59 -0700 | [diff] [blame] | 833 | array (array_) {} |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 834 | |
| 835 | array_t &array; |
Behdad Esfahbod | b339099 | 2018-10-10 12:07:49 -0400 | [diff] [blame] | 836 | }; |
| 837 | |
| 838 | |
| 839 | |
Behdad Esfahbod | 94a23aa | 2010-05-05 01:13:09 -0400 | [diff] [blame] | 840 | |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 841 | typedef bool (*intersects_func_t) (const hb_set_t *glyphs, const HBUINT16 &value, const void *data); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 842 | typedef void (*intersected_glyphs_func_t) (const hb_set_t *glyphs, const void *data, unsigned value, hb_set_t *intersected_glyphs); |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 843 | typedef void (*collect_glyphs_func_t) (hb_set_t *glyphs, const HBUINT16 &value, const void *data); |
| 844 | typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 845 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 846 | struct ContextClosureFuncs |
| 847 | { |
| 848 | intersects_func_t intersects; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 849 | intersected_glyphs_func_t intersected_glyphs; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 850 | }; |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 851 | struct ContextCollectGlyphsFuncs |
| 852 | { |
| 853 | collect_glyphs_func_t collect; |
| 854 | }; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 855 | struct ContextApplyFuncs |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 856 | { |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 857 | match_func_t match; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 858 | }; |
| 859 | |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 860 | |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 861 | static inline bool intersects_glyph (const hb_set_t *glyphs, const HBUINT16 &value, const void *data HB_UNUSED) |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 862 | { |
| 863 | return glyphs->has (value); |
| 864 | } |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 865 | static inline bool intersects_class (const hb_set_t *glyphs, const HBUINT16 &value, const void *data) |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 866 | { |
| 867 | const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data); |
| 868 | return class_def.intersects_class (glyphs, value); |
| 869 | } |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 870 | static inline bool intersects_coverage (const hb_set_t *glyphs, const HBUINT16 &value, const void *data) |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 871 | { |
Behdad Esfahbod | 9b4b584 | 2021-03-31 13:27:21 -0600 | [diff] [blame] | 872 | const Offset16To<Coverage> &coverage = (const Offset16To<Coverage>&)value; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 873 | return (data+coverage).intersects (glyphs); |
| 874 | } |
| 875 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 876 | |
| 877 | static inline void intersected_glyph (const hb_set_t *glyphs HB_UNUSED, const void *data, unsigned value, hb_set_t *intersected_glyphs) |
| 878 | { |
| 879 | unsigned g = reinterpret_cast<const HBUINT16 *>(data)[value]; |
| 880 | intersected_glyphs->add (g); |
| 881 | } |
| 882 | static inline void intersected_class_glyphs (const hb_set_t *glyphs, const void *data, unsigned value, hb_set_t *intersected_glyphs) |
| 883 | { |
| 884 | const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data); |
| 885 | class_def.intersected_class_glyphs (glyphs, value, intersected_glyphs); |
| 886 | } |
| 887 | static inline void intersected_coverage_glyphs (const hb_set_t *glyphs, const void *data, unsigned value, hb_set_t *intersected_glyphs) |
| 888 | { |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 889 | Offset16To<Coverage> coverage; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 890 | coverage = value; |
| 891 | (data+coverage).intersected_coverage_glyphs (glyphs, intersected_glyphs); |
| 892 | } |
| 893 | |
| 894 | |
Qunxin Liu | 44d88cf | 2020-05-08 15:33:34 -0700 | [diff] [blame] | 895 | static inline bool array_is_subset_of (const hb_set_t *glyphs, |
| 896 | unsigned int count, |
| 897 | const HBUINT16 values[], |
| 898 | intersects_func_t intersects_func, |
| 899 | const void *intersects_data) |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 900 | { |
Garret Rieger | 95ab110 | 2019-10-21 13:15:46 -0700 | [diff] [blame] | 901 | for (const HBUINT16 &_ : + hb_iter (values, count)) |
Qunxin Liu | 44d88cf | 2020-05-08 15:33:34 -0700 | [diff] [blame] | 902 | if (!intersects_func (glyphs, _, intersects_data)) return false; |
| 903 | return true; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 904 | } |
| 905 | |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 906 | |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 907 | static inline void collect_glyph (hb_set_t *glyphs, const HBUINT16 &value, const void *data HB_UNUSED) |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 908 | { |
| 909 | glyphs->add (value); |
| 910 | } |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 911 | static inline void collect_class (hb_set_t *glyphs, const HBUINT16 &value, const void *data) |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 912 | { |
| 913 | const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data); |
Behdad Esfahbod | 89ad3c6 | 2020-04-23 10:57:30 -0700 | [diff] [blame] | 914 | class_def.collect_class (glyphs, value); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 915 | } |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 916 | static inline void collect_coverage (hb_set_t *glyphs, const HBUINT16 &value, const void *data) |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 917 | { |
Behdad Esfahbod | 9b4b584 | 2021-03-31 13:27:21 -0600 | [diff] [blame] | 918 | const Offset16To<Coverage> &coverage = (const Offset16To<Coverage>&)value; |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 919 | (data+coverage).collect_coverage (glyphs); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 920 | } |
Behdad Esfahbod | 0beb66e | 2012-12-05 18:46:04 -0500 | [diff] [blame] | 921 | 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] | 922 | hb_set_t *glyphs, |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 923 | unsigned int count, |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 924 | const HBUINT16 values[], |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 925 | collect_glyphs_func_t collect_func, |
| 926 | const void *collect_data) |
| 927 | { |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 928 | return |
| 929 | + hb_iter (values, count) |
| 930 | | hb_apply ([&] (const HBUINT16 &_) { collect_func (glyphs, _, collect_data); }) |
| 931 | ; |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 935 | static inline bool match_glyph (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data HB_UNUSED) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 936 | { |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 937 | return glyph_id == value; |
| 938 | } |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 939 | static inline bool match_class (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 940 | { |
Behdad Esfahbod | 2b5a59c | 2009-08-04 11:38:50 -0400 | [diff] [blame] | 941 | const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data); |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 942 | return class_def.get_class (glyph_id) == value; |
| 943 | } |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 944 | static inline bool match_coverage (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 945 | { |
Behdad Esfahbod | 9b4b584 | 2021-03-31 13:27:21 -0600 | [diff] [blame] | 946 | const Offset16To<Coverage> &coverage = (const Offset16To<Coverage>&)value; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 947 | return (data+coverage).get_coverage (glyph_id) != NOT_COVERED; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 948 | } |
| 949 | |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 950 | static inline bool would_match_input (hb_would_apply_context_t *c, |
| 951 | unsigned int count, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 952 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 953 | match_func_t match_func, |
| 954 | const void *match_data) |
| 955 | { |
| 956 | if (count != c->len) |
| 957 | return false; |
| 958 | |
| 959 | for (unsigned int i = 1; i < count; i++) |
Behdad Esfahbod | 472f229 | 2012-08-07 22:25:24 -0400 | [diff] [blame] | 960 | if (likely (!match_func (c->glyphs[i], input[i - 1], match_data))) |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 961 | return false; |
| 962 | |
| 963 | return true; |
| 964 | } |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 965 | static inline bool match_input (hb_ot_apply_context_t *c, |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 966 | unsigned int count, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 967 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 968 | match_func_t match_func, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 969 | const void *match_data, |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 970 | unsigned int *end_position, |
Behdad Esfahbod | 5ba4504 | 2015-11-02 15:43:08 -0800 | [diff] [blame] | 971 | unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 972 | unsigned int *p_total_component_count = nullptr) |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 973 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 974 | TRACE_APPLY (nullptr); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 975 | |
Behdad Esfahbod | 5ba4504 | 2015-11-02 15:43:08 -0800 | [diff] [blame] | 976 | if (unlikely (count > HB_MAX_CONTEXT_LENGTH)) return_trace (false); |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 977 | |
Behdad Esfahbod | 3c3df9c | 2013-10-17 13:58:31 +0200 | [diff] [blame] | 978 | hb_buffer_t *buffer = c->buffer; |
| 979 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 980 | 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] | 981 | skippy_iter.reset (buffer->idx, count - 1); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 982 | skippy_iter.set_match_func (match_func, match_data, input); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 983 | |
Behdad Esfahbod | 191fa88 | 2012-08-28 22:58:55 -0400 | [diff] [blame] | 984 | /* |
| 985 | * This is perhaps the trickiest part of OpenType... Remarks: |
| 986 | * |
| 987 | * - If all components of the ligature were marks, we call this a mark ligature. |
| 988 | * |
| 989 | * - If there is no GDEF, and the ligature is NOT a mark ligature, we categorize |
| 990 | * it as a ligature glyph. |
| 991 | * |
| 992 | * - Ligatures cannot be formed across glyphs attached to different components |
| 993 | * of previous ligatures. Eg. the sequence is LAM,SHADDA,LAM,FATHA,HEH, and |
| 994 | * LAM,LAM,HEH form a ligature, leaving SHADDA,FATHA next to eachother. |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 995 | * However, it would be wrong to ligate that SHADDA,FATHA sequence. |
| 996 | * There are a couple of exceptions to this: |
| 997 | * |
| 998 | * o If a ligature tries ligating with marks that belong to it itself, go ahead, |
| 999 | * assuming that the font designer knows what they are doing (otherwise it can |
| 1000 | * break Indic stuff when a matra wants to ligate with a conjunct, |
| 1001 | * |
| 1002 | * o If two marks want to ligate and they belong to different components of the |
| 1003 | * same ligature glyph, and said ligature glyph is to be ignored according to |
| 1004 | * mark-filtering rules, then allow. |
ebraminio | 7c6937e | 2017-11-20 14:49:22 -0500 | [diff] [blame] | 1005 | * https://github.com/harfbuzz/harfbuzz/issues/545 |
Behdad Esfahbod | 191fa88 | 2012-08-28 22:58:55 -0400 | [diff] [blame] | 1006 | */ |
| 1007 | |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1008 | unsigned int total_component_count = 0; |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1009 | total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->cur()); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1010 | |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1011 | unsigned int first_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur()); |
| 1012 | 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] | 1013 | |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1014 | enum { |
| 1015 | LIGBASE_NOT_CHECKED, |
| 1016 | LIGBASE_MAY_NOT_SKIP, |
| 1017 | LIGBASE_MAY_SKIP |
| 1018 | } ligbase = LIGBASE_NOT_CHECKED; |
| 1019 | |
Behdad Esfahbod | 3c3df9c | 2013-10-17 13:58:31 +0200 | [diff] [blame] | 1020 | match_positions[0] = buffer->idx; |
Behdad Esfahbod | 370f03e | 2012-01-16 17:03:55 -0500 | [diff] [blame] | 1021 | for (unsigned int i = 1; i < count; i++) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1022 | { |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1023 | unsigned unsafe_to; |
| 1024 | if (!skippy_iter.next (&unsafe_to)) |
| 1025 | { |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1026 | *end_position = unsafe_to; |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1027 | return_trace (false); |
| 1028 | } |
Behdad Esfahbod | 6cc136f | 2013-10-17 13:55:48 +0200 | [diff] [blame] | 1029 | |
| 1030 | match_positions[i] = skippy_iter.idx; |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1031 | |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1032 | unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]); |
| 1033 | 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] | 1034 | |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1035 | if (first_lig_id && first_lig_comp) |
| 1036 | { |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1037 | /* If first component was attached to a previous ligature component, |
| 1038 | * all subsequent components should be attached to the same ligature |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1039 | * component, otherwise we shouldn't ligate them... */ |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1040 | 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] | 1041 | { |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 1042 | /* ...unless, we are attached to a base ligature and that base |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1043 | * ligature is ignorable. */ |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 1044 | if (ligbase == LIGBASE_NOT_CHECKED) |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1045 | { |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1046 | bool found = false; |
Behdad Esfahbod | 4d67743 | 2019-05-10 16:35:31 -0700 | [diff] [blame] | 1047 | const auto *out = buffer->out_info; |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1048 | unsigned int j = buffer->out_len; |
| 1049 | 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] | 1050 | { |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1051 | if (_hb_glyph_info_get_lig_comp (&out[j - 1]) == 0) |
| 1052 | { |
| 1053 | j--; |
| 1054 | found = true; |
| 1055 | break; |
| 1056 | } |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1057 | j--; |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1058 | } |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1059 | |
Behdad Esfahbod | 12757b6 | 2018-01-26 18:14:05 -0800 | [diff] [blame] | 1060 | 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] | 1061 | ligbase = LIGBASE_MAY_SKIP; |
| 1062 | else |
| 1063 | ligbase = LIGBASE_MAY_NOT_SKIP; |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1064 | } |
| 1065 | |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 1066 | if (ligbase == LIGBASE_MAY_NOT_SKIP) |
Behdad Esfahbod | 8b2c94c | 2017-10-02 20:02:45 +0200 | [diff] [blame] | 1067 | return_trace (false); |
| 1068 | } |
| 1069 | } |
| 1070 | else |
Behdad Esfahbod | 621c49c | 2017-10-04 15:06:48 +0200 | [diff] [blame] | 1071 | { |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1072 | /* If first component was NOT attached to a previous ligature component, |
| 1073 | * all subsequent components should also NOT be attached to any ligature |
| 1074 | * component, unless they are attached to the first component itself! */ |
| 1075 | 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] | 1076 | return_trace (false); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1077 | } |
| 1078 | |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1079 | 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] | 1080 | } |
| 1081 | |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1082 | *end_position = skippy_iter.idx + 1; |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1083 | |
Behdad Esfahbod | 191fa88 | 2012-08-28 22:58:55 -0400 | [diff] [blame] | 1084 | if (p_total_component_count) |
| 1085 | *p_total_component_count = total_component_count; |
| 1086 | |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1087 | return_trace (true); |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1088 | } |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1089 | static inline bool ligate_input (hb_ot_apply_context_t *c, |
Behdad Esfahbod | e714fe6 | 2013-10-17 13:49:51 +0200 | [diff] [blame] | 1090 | unsigned int count, /* Including the first glyph */ |
Bruce Mitchener | 5a24ea1 | 2018-10-20 08:09:52 +0700 | [diff] [blame] | 1091 | 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] | 1092 | unsigned int match_end, |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1093 | hb_codepoint_t lig_glyph, |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1094 | unsigned int total_component_count) |
| 1095 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1096 | TRACE_APPLY (nullptr); |
Behdad Esfahbod | e714fe6 | 2013-10-17 13:49:51 +0200 | [diff] [blame] | 1097 | |
Behdad Esfahbod | 3c3df9c | 2013-10-17 13:58:31 +0200 | [diff] [blame] | 1098 | hb_buffer_t *buffer = c->buffer; |
| 1099 | |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1100 | buffer->merge_clusters (buffer->idx, match_end); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 1101 | |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1102 | /* - If a base and one or more marks ligate, consider that as a base, NOT |
| 1103 | * ligature, such that all following marks can still attach to it. |
| 1104 | * https://github.com/harfbuzz/harfbuzz/issues/1109 |
| 1105 | * |
| 1106 | * - 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] | 1107 | * 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] | 1108 | * the ligature to keep its old ligature id. This will allow it to attach to |
| 1109 | * 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] | 1110 | * 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] | 1111 | * ligature id and component value of 2. Then if SHADDA,FATHA form a ligature |
| 1112 | * later, we don't want them to lose their ligature id/component, otherwise |
| 1113 | * GPOS will fail to correctly position the mark ligature on top of the |
| 1114 | * LAM,LAM,HEH ligature. See: |
| 1115 | * https://bugzilla.gnome.org/show_bug.cgi?id=676343 |
| 1116 | * |
| 1117 | * - If a ligature is formed of components that some of which are also ligatures |
| 1118 | * themselves, and those ligature components had marks attached to *their* |
| 1119 | * components, we have to attach the marks to the new ligature component |
| 1120 | * positions! Now *that*'s tricky! And these marks may be following the |
| 1121 | * last component of the whole sequence, so we should loop forward looking |
| 1122 | * for them and update them. |
| 1123 | * |
| 1124 | * Eg. the sequence is LAM,LAM,SHADDA,FATHA,HEH, and the font first forms a |
| 1125 | * 'calt' ligature of LAM,HEH, leaving the SHADDA and FATHA with a ligature |
| 1126 | * id and component == 1. Now, during 'liga', the LAM and the LAM-HEH ligature |
| 1127 | * form a LAM-LAM-HEH ligature. We need to reassign the SHADDA and FATHA to |
| 1128 | * the new ligature with a component value of 2. |
| 1129 | * |
| 1130 | * This in fact happened to a font... See: |
| 1131 | * https://bugzilla.gnome.org/show_bug.cgi?id=437633 |
| 1132 | */ |
| 1133 | |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1134 | bool is_base_ligature = _hb_glyph_info_is_base_glyph (&buffer->info[match_positions[0]]); |
| 1135 | bool is_mark_ligature = _hb_glyph_info_is_mark (&buffer->info[match_positions[0]]); |
| 1136 | for (unsigned int i = 1; i < count; i++) |
Behdad Esfahbod | 3cca978 | 2018-10-02 15:02:16 +0200 | [diff] [blame] | 1137 | if (!_hb_glyph_info_is_mark (&buffer->info[match_positions[i]])) |
| 1138 | { |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1139 | is_base_ligature = false; |
Behdad Esfahbod | 3cca978 | 2018-10-02 15:02:16 +0200 | [diff] [blame] | 1140 | is_mark_ligature = false; |
| 1141 | break; |
| 1142 | } |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1143 | bool is_ligature = !is_base_ligature && !is_mark_ligature; |
Behdad Esfahbod | 3cca978 | 2018-10-02 15:02:16 +0200 | [diff] [blame] | 1144 | |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1145 | unsigned int klass = is_ligature ? HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE : 0; |
| 1146 | unsigned int lig_id = is_ligature ? _hb_allocate_lig_id (buffer) : 0; |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1147 | unsigned int last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur()); |
| 1148 | 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] | 1149 | unsigned int components_so_far = last_num_components; |
| 1150 | |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1151 | if (is_ligature) |
Behdad Esfahbod | 7e08f12 | 2013-05-27 14:48:34 -0400 | [diff] [blame] | 1152 | { |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1153 | _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] | 1154 | 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] | 1155 | { |
Behdad Esfahbod | 8259669 | 2015-11-02 17:44:05 -0800 | [diff] [blame] | 1156 | _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] | 1157 | } |
Behdad Esfahbod | 7e08f12 | 2013-05-27 14:48:34 -0400 | [diff] [blame] | 1158 | } |
Behdad Esfahbod | a016174 | 2013-10-18 00:06:30 +0200 | [diff] [blame] | 1159 | c->replace_glyph_with_ligature (lig_glyph, klass); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1160 | |
| 1161 | for (unsigned int i = 1; i < count; i++) |
| 1162 | { |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 1163 | while (buffer->idx < match_positions[i] && buffer->successful) |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1164 | { |
Behdad Esfahbod | 9efddb9 | 2018-10-02 16:05:26 +0200 | [diff] [blame] | 1165 | if (is_ligature) |
| 1166 | { |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 1167 | unsigned int this_comp = _hb_glyph_info_get_lig_comp (&buffer->cur()); |
Behdad Esfahbod | 2f02fc7 | 2015-12-17 15:21:14 +0000 | [diff] [blame] | 1168 | if (this_comp == 0) |
Behdad Esfahbod | 100fbea | 2015-12-17 15:23:09 +0000 | [diff] [blame] | 1169 | this_comp = last_num_components; |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1170 | unsigned int new_lig_comp = components_so_far - last_num_components + |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 1171 | hb_min (this_comp, last_num_components); |
Behdad Esfahbod | 2f02fc7 | 2015-12-17 15:21:14 +0000 | [diff] [blame] | 1172 | _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] | 1173 | } |
Behdad Esfahbod | 8450f43 | 2021-03-15 15:18:06 -0600 | [diff] [blame] | 1174 | (void) buffer->next_glyph (); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1175 | } |
| 1176 | |
Behdad Esfahbod | 3ddf892 | 2013-10-18 00:02:43 +0200 | [diff] [blame] | 1177 | last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur()); |
| 1178 | last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur()); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1179 | components_so_far += last_num_components; |
| 1180 | |
| 1181 | /* Skip the base glyph */ |
Behdad Esfahbod | 3c3df9c | 2013-10-17 13:58:31 +0200 | [diff] [blame] | 1182 | buffer->idx++; |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1183 | } |
| 1184 | |
Ebrahim Byagowi | cc977b6 | 2020-03-26 11:18:02 +0430 | [diff] [blame] | 1185 | if (!is_mark_ligature && last_lig_id) |
| 1186 | { |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1187 | /* Re-adjust components for any marks following. */ |
Ebrahim Byagowi | cc977b6 | 2020-03-26 11:18:02 +0430 | [diff] [blame] | 1188 | for (unsigned i = buffer->idx; i < buffer->len; ++i) |
| 1189 | { |
| 1190 | if (last_lig_id != _hb_glyph_info_get_lig_id (&buffer->info[i])) break; |
| 1191 | |
| 1192 | unsigned this_comp = _hb_glyph_info_get_lig_comp (&buffer->info[i]); |
| 1193 | if (!this_comp) break; |
| 1194 | |
| 1195 | unsigned new_lig_comp = components_so_far - last_num_components + |
| 1196 | hb_min (this_comp, last_num_components); |
| 1197 | _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] | 1198 | } |
| 1199 | } |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1200 | return_trace (true); |
Behdad Esfahbod | a177d02 | 2012-08-28 23:18:22 -0400 | [diff] [blame] | 1201 | } |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1202 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1203 | static inline bool match_backtrack (hb_ot_apply_context_t *c, |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1204 | unsigned int count, |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1205 | const HBUINT16 backtrack[], |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1206 | match_func_t match_func, |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 1207 | const void *match_data, |
| 1208 | unsigned int *match_start) |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1209 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1210 | TRACE_APPLY (nullptr); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1211 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1212 | 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] | 1213 | skippy_iter.reset (c->buffer->backtrack_len (), count); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 1214 | skippy_iter.set_match_func (match_func, match_data, backtrack); |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1215 | |
Behdad Esfahbod | 4d3aeb8 | 2012-01-16 16:43:26 -0500 | [diff] [blame] | 1216 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1217 | { |
| 1218 | unsigned unsafe_from; |
| 1219 | if (!skippy_iter.prev (&unsafe_from)) |
| 1220 | { |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1221 | *match_start = unsafe_from; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1222 | return_trace (false); |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1223 | } |
| 1224 | } |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1225 | |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 1226 | *match_start = skippy_iter.idx; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1227 | return_trace (true); |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1228 | } |
| 1229 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1230 | static inline bool match_lookahead (hb_ot_apply_context_t *c, |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1231 | unsigned int count, |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1232 | const HBUINT16 lookahead[], |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1233 | match_func_t match_func, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 1234 | const void *match_data, |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1235 | unsigned int start_index, |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 1236 | unsigned int *end_index) |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1237 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1238 | TRACE_APPLY (nullptr); |
Behdad Esfahbod | 93814ca | 2012-08-28 22:24:51 -0400 | [diff] [blame] | 1239 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1240 | 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] | 1241 | skippy_iter.reset (start_index - 1, count); |
Behdad Esfahbod | 607feb7 | 2013-02-14 07:43:13 -0500 | [diff] [blame] | 1242 | skippy_iter.set_match_func (match_func, match_data, lookahead); |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1243 | |
Behdad Esfahbod | 370f03e | 2012-01-16 17:03:55 -0500 | [diff] [blame] | 1244 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1245 | { |
| 1246 | unsigned unsafe_to; |
| 1247 | if (!skippy_iter.next (&unsafe_to)) |
| 1248 | { |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1249 | *end_index = unsafe_to; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1250 | return_trace (false); |
Behdad Esfahbod | 3122c2c | 2021-12-04 19:50:33 -0800 | [diff] [blame] | 1251 | } |
| 1252 | } |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 1253 | |
Behdad Esfahbod | 40bd7e9 | 2016-05-02 14:47:45 +0200 | [diff] [blame] | 1254 | *end_index = skippy_iter.idx + 1; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1255 | return_trace (true); |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1256 | } |
| 1257 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 1258 | |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1259 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1260 | struct LookupRecord |
| 1261 | { |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 1262 | bool serialize (hb_serialize_context_t *c, |
| 1263 | const hb_map_t *lookup_map) const |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1264 | { |
| 1265 | TRACE_SERIALIZE (this); |
| 1266 | auto *out = c->embed (*this); |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 1267 | if (unlikely (!out)) return_trace (false); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1268 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 1269 | 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] | 1270 | } |
| 1271 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1272 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 1273 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 1274 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1275 | return_trace (c->check_struct (this)); |
Behdad Esfahbod | cd3827e | 2009-08-04 02:09:34 -0400 | [diff] [blame] | 1276 | } |
| 1277 | |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1278 | HBUINT16 sequenceIndex; /* Index into current glyph |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1279 | * sequence--first glyph = 0 */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1280 | HBUINT16 lookupListIndex; /* Lookup to apply to that |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1281 | * position--zero--based */ |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 1282 | public: |
| 1283 | DEFINE_SIZE_STATIC (4); |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1284 | }; |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1285 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 1286 | static unsigned serialize_lookuprecord_array (hb_serialize_context_t *c, |
| 1287 | const hb_array_t<const LookupRecord> lookupRecords, |
| 1288 | const hb_map_t *lookup_map) |
| 1289 | { |
| 1290 | unsigned count = 0; |
| 1291 | for (const LookupRecord& r : lookupRecords) |
| 1292 | { |
| 1293 | if (!lookup_map->has (r.lookupListIndex)) |
| 1294 | continue; |
| 1295 | |
| 1296 | if (!r.serialize (c, lookup_map)) |
| 1297 | return 0; |
| 1298 | |
| 1299 | count++; |
| 1300 | } |
| 1301 | return count; |
| 1302 | } |
| 1303 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1304 | enum ContextFormat { SimpleContext = 1, ClassBasedContext = 2, CoverageBasedContext = 3 }; |
| 1305 | |
| 1306 | static void context_closure_recurse_lookups (hb_closure_context_t *c, |
| 1307 | unsigned inputCount, const HBUINT16 input[], |
| 1308 | unsigned lookupCount, |
| 1309 | const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */, |
| 1310 | unsigned value, |
| 1311 | ContextFormat context_format, |
| 1312 | const void *data, |
| 1313 | intersected_glyphs_func_t intersected_glyphs_func) |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1314 | { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1315 | hb_set_t *covered_seq_indicies = hb_set_create (); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1316 | for (unsigned int i = 0; i < lookupCount; i++) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1317 | { |
| 1318 | unsigned seqIndex = lookupRecord[i].sequenceIndex; |
Garret Rieger | adca4ce | 2021-03-30 13:20:50 -0700 | [diff] [blame] | 1319 | if (seqIndex >= inputCount) continue; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1320 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1321 | bool has_pos_glyphs = false; |
| 1322 | hb_set_t pos_glyphs; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1323 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1324 | if (hb_set_is_empty (covered_seq_indicies) || !hb_set_has (covered_seq_indicies, seqIndex)) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1325 | { |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1326 | has_pos_glyphs = true; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1327 | if (seqIndex == 0) |
| 1328 | { |
| 1329 | switch (context_format) { |
| 1330 | case ContextFormat::SimpleContext: |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1331 | pos_glyphs.add (value); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1332 | break; |
| 1333 | case ContextFormat::ClassBasedContext: |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 1334 | intersected_glyphs_func (&c->parent_active_glyphs (), data, value, &pos_glyphs); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1335 | break; |
| 1336 | case ContextFormat::CoverageBasedContext: |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 1337 | pos_glyphs.set (c->parent_active_glyphs ()); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | else |
| 1342 | { |
| 1343 | const void *input_data = input; |
| 1344 | unsigned input_value = seqIndex - 1; |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1345 | if (context_format != ContextFormat::SimpleContext) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1346 | { |
| 1347 | input_data = data; |
| 1348 | input_value = input[seqIndex - 1]; |
| 1349 | } |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 1350 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1351 | intersected_glyphs_func (c->glyphs, input_data, input_value, &pos_glyphs); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
Garret Rieger | f3c1f4f | 2022-01-28 11:50:22 -0800 | [diff] [blame] | 1355 | covered_seq_indicies->add (seqIndex); |
| 1356 | if (has_pos_glyphs) { |
| 1357 | c->push_cur_active_glyphs () = pos_glyphs; |
| 1358 | } else { |
| 1359 | c->push_cur_active_glyphs ().set (*c->glyphs); |
| 1360 | } |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 1361 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1362 | unsigned endIndex = inputCount; |
| 1363 | if (context_format == ContextFormat::CoverageBasedContext) |
| 1364 | endIndex += 1; |
| 1365 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1366 | c->recurse (lookupRecord[i].lookupListIndex, covered_seq_indicies, seqIndex, endIndex); |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 1367 | |
Qunxin Liu | 706014f | 2021-12-01 20:20:12 -0800 | [diff] [blame] | 1368 | c->pop_cur_done_glyphs (); |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1369 | } |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 1370 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1371 | hb_set_destroy (covered_seq_indicies); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1372 | } |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 1373 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1374 | template <typename context_t> |
| 1375 | static inline void recurse_lookups (context_t *c, |
| 1376 | unsigned int lookupCount, |
| 1377 | const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */) |
| 1378 | { |
| 1379 | for (unsigned int i = 0; i < lookupCount; i++) |
| 1380 | c->recurse (lookupRecord[i].lookupListIndex); |
| 1381 | } |
| 1382 | |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1383 | static inline void apply_lookup (hb_ot_apply_context_t *c, |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 1384 | unsigned int count, /* Including the first glyph */ |
Behdad Esfahbod | 5ba4504 | 2015-11-02 15:43:08 -0800 | [diff] [blame] | 1385 | unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */ |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 1386 | unsigned int lookupCount, |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1387 | const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */ |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1388 | unsigned int match_end) |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1389 | { |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1390 | hb_buffer_t *buffer = c->buffer; |
jfkthame | 44f7d6e | 2017-02-17 03:03:24 +0000 | [diff] [blame] | 1391 | int end; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1392 | |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1393 | /* All positions are distance from beginning of *output* buffer. |
| 1394 | * Adjust. */ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1395 | { |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1396 | unsigned int bl = buffer->backtrack_len (); |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1397 | end = bl + match_end - buffer->idx; |
Behdad Esfahbod | 8751de5 | 2013-07-18 16:29:50 -0400 | [diff] [blame] | 1398 | |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1399 | int delta = bl - buffer->idx; |
| 1400 | /* Convert positions to new indexing. */ |
| 1401 | for (unsigned int j = 0; j < count; j++) |
| 1402 | match_positions[j] += delta; |
Behdad Esfahbod | 8820bb2 | 2013-02-14 07:41:03 -0500 | [diff] [blame] | 1403 | } |
Behdad Esfahbod | e73a0c2 | 2009-05-18 04:15:25 -0400 | [diff] [blame] | 1404 | |
Behdad Esfahbod | 7185b27 | 2018-05-31 20:03:00 -0700 | [diff] [blame] | 1405 | for (unsigned int i = 0; i < lookupCount && buffer->successful; i++) |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1406 | { |
| 1407 | unsigned int idx = lookupRecord[i].sequenceIndex; |
| 1408 | if (idx >= count) |
| 1409 | continue; |
| 1410 | |
Behdad Esfahbod | 9cc1ed4 | 2015-11-19 12:39:09 -0800 | [diff] [blame] | 1411 | /* Don't recurse to ourself at same position. |
| 1412 | * Note that this test is too naive, it doesn't catch longer loops. */ |
Behdad Esfahbod | 1da75af | 2021-02-10 00:03:30 -0700 | [diff] [blame] | 1413 | if (unlikely (idx == 0 && lookupRecord[i].lookupListIndex == c->lookup_index)) |
Behdad Esfahbod | 9cc1ed4 | 2015-11-19 12:39:09 -0800 | [diff] [blame] | 1414 | continue; |
| 1415 | |
Behdad Esfahbod | ccd9161 | 2022-03-24 13:10:48 -0600 | [diff] [blame] | 1416 | unsigned int orig_len = buffer->backtrack_len () + buffer->lookahead_len (); |
| 1417 | |
| 1418 | /* This can happen if earlier recursed lookups deleted many entries. */ |
| 1419 | if (unlikely (match_positions[idx] >= orig_len)) |
| 1420 | continue; |
| 1421 | |
Behdad Esfahbod | e593072 | 2017-11-14 15:47:55 -0800 | [diff] [blame] | 1422 | if (unlikely (!buffer->move_to (match_positions[idx]))) |
| 1423 | break; |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1424 | |
Behdad Esfahbod | baf7779 | 2017-11-14 21:53:48 -0800 | [diff] [blame] | 1425 | if (unlikely (buffer->max_ops <= 0)) |
| 1426 | break; |
| 1427 | |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1428 | if (!c->recurse (lookupRecord[i].lookupListIndex)) |
| 1429 | continue; |
| 1430 | |
| 1431 | unsigned int new_len = buffer->backtrack_len () + buffer->lookahead_len (); |
| 1432 | int delta = new_len - orig_len; |
| 1433 | |
| 1434 | if (!delta) |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 1435 | continue; |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1436 | |
Behdad Esfahbod | 9ac9af7 | 2017-03-05 13:51:01 -0800 | [diff] [blame] | 1437 | /* Recursed lookup changed buffer len. Adjust. |
| 1438 | * |
| 1439 | * TODO: |
| 1440 | * |
| 1441 | * Right now, if buffer length increased by n, we assume n new glyphs |
| 1442 | * were added right after the current position, and if buffer length |
| 1443 | * was decreased by n, we assume n match positions after the current |
| 1444 | * one where removed. The former (buffer length increased) case is |
| 1445 | * fine, but the decrease case can be improved in at least two ways, |
| 1446 | * both of which are significant: |
| 1447 | * |
| 1448 | * - If recursed-to lookup is MultipleSubst and buffer length |
| 1449 | * decreased, then it's current match position that was deleted, |
| 1450 | * NOT the one after it. |
| 1451 | * |
| 1452 | * - If buffer length was decreased by n, it does not necessarily |
| 1453 | * mean that n match positions where removed, as there might |
| 1454 | * have been marks and default-ignorables in the sequence. We |
| 1455 | * should instead drop match positions between current-position |
Behdad Esfahbod | 1da75af | 2021-02-10 00:03:30 -0700 | [diff] [blame] | 1456 | * and current-position + n instead. Though, am not sure which |
| 1457 | * one is better. Both cases have valid uses. Sigh. |
Behdad Esfahbod | 9ac9af7 | 2017-03-05 13:51:01 -0800 | [diff] [blame] | 1458 | * |
| 1459 | * It should be possible to construct tests for both of these cases. |
| 1460 | */ |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1461 | |
jfkthame | 44f7d6e | 2017-02-17 03:03:24 +0000 | [diff] [blame] | 1462 | end += delta; |
Behdad Esfahbod | ccd9161 | 2022-03-24 13:10:48 -0600 | [diff] [blame] | 1463 | if (end < int (match_positions[idx])) |
Behdad Esfahbod | 359dead | 2016-05-06 16:19:19 +0100 | [diff] [blame] | 1464 | { |
Behdad Esfahbod | 4b4a1b9 | 2016-12-21 23:10:43 -0600 | [diff] [blame] | 1465 | /* 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] | 1466 | * lookup ended up removing many items. |
| 1467 | * Just never rewind end beyond start of current position, since that is |
| 1468 | * not possible in the recursed lookup. Also adjust delta as such. |
| 1469 | * |
| 1470 | * https://bugs.chromium.org/p/chromium/issues/detail?id=659496 |
| 1471 | * https://github.com/harfbuzz/harfbuzz/issues/1611 |
| 1472 | */ |
| 1473 | delta += match_positions[idx] - end; |
Behdad Esfahbod | 4b4a1b9 | 2016-12-21 23:10:43 -0600 | [diff] [blame] | 1474 | end = match_positions[idx]; |
Behdad Esfahbod | 359dead | 2016-05-06 16:19:19 +0100 | [diff] [blame] | 1475 | } |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1476 | |
| 1477 | unsigned int next = idx + 1; /* next now is the position after the recursed lookup. */ |
| 1478 | |
| 1479 | if (delta > 0) |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1480 | { |
Behdad Esfahbod | 5ba4504 | 2015-11-02 15:43:08 -0800 | [diff] [blame] | 1481 | if (unlikely (delta + count > HB_MAX_CONTEXT_LENGTH)) |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1482 | break; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1483 | } |
| 1484 | else |
| 1485 | { |
Behdad Esfahbod | bf2a845 | 2022-03-24 13:09:53 -0600 | [diff] [blame] | 1486 | /* NOTE: delta is non-positive. */ |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 1487 | delta = hb_max (delta, (int) next - (int) count); |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1488 | next -= delta; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1489 | } |
Behdad Esfahbod | 6b65a76 | 2013-10-14 18:51:39 +0200 | [diff] [blame] | 1490 | |
| 1491 | /* Shift! */ |
| 1492 | memmove (match_positions + next + delta, match_positions + next, |
| 1493 | (count - next) * sizeof (match_positions[0])); |
| 1494 | next += delta; |
| 1495 | count += delta; |
| 1496 | |
| 1497 | /* Fill in new entries. */ |
| 1498 | for (unsigned int j = idx + 1; j < next; j++) |
| 1499 | match_positions[j] = match_positions[j - 1] + 1; |
| 1500 | |
| 1501 | /* And fixup the rest. */ |
| 1502 | for (; next < count; next++) |
| 1503 | match_positions[next] += delta; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1504 | } |
| 1505 | |
Behdad Esfahbod | cac6c86 | 2021-03-15 13:46:54 -0600 | [diff] [blame] | 1506 | (void) buffer->move_to (end); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1507 | } |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 1508 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 1509 | |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1510 | |
| 1511 | /* Contextual lookups */ |
| 1512 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1513 | struct ContextClosureLookupContext |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1514 | { |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1515 | ContextClosureFuncs funcs; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1516 | ContextFormat context_format; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1517 | const void *intersects_data; |
| 1518 | }; |
| 1519 | |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1520 | struct ContextCollectGlyphsLookupContext |
| 1521 | { |
| 1522 | ContextCollectGlyphsFuncs funcs; |
| 1523 | const void *collect_data; |
| 1524 | }; |
| 1525 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1526 | struct ContextApplyLookupContext |
| 1527 | { |
| 1528 | ContextApplyFuncs funcs; |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 1529 | const void *match_data; |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1530 | }; |
| 1531 | |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1532 | static inline bool context_intersects (const hb_set_t *glyphs, |
| 1533 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
| 1534 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
| 1535 | ContextClosureLookupContext &lookup_context) |
| 1536 | { |
Qunxin Liu | 44d88cf | 2020-05-08 15:33:34 -0700 | [diff] [blame] | 1537 | return array_is_subset_of (glyphs, |
| 1538 | inputCount ? inputCount - 1 : 0, input, |
| 1539 | lookup_context.funcs.intersects, lookup_context.intersects_data); |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 1542 | static inline void context_closure_lookup (hb_closure_context_t *c, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1543 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1544 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1545 | unsigned int lookupCount, |
| 1546 | const LookupRecord lookupRecord[], |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1547 | 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] | 1548 | ContextClosureLookupContext &lookup_context) |
| 1549 | { |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1550 | if (context_intersects (c->glyphs, |
| 1551 | inputCount, input, |
| 1552 | lookup_context)) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1553 | context_closure_recurse_lookups (c, |
| 1554 | inputCount, input, |
| 1555 | lookupCount, lookupRecord, |
| 1556 | value, |
| 1557 | lookup_context.context_format, |
| 1558 | lookup_context.intersects_data, |
| 1559 | lookup_context.funcs.intersected_glyphs); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1560 | } |
| 1561 | |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1562 | static inline void context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c, |
| 1563 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1564 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1565 | unsigned int lookupCount, |
| 1566 | const LookupRecord lookupRecord[], |
| 1567 | ContextCollectGlyphsLookupContext &lookup_context) |
| 1568 | { |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 1569 | collect_array (c, c->input, |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1570 | inputCount ? inputCount - 1 : 0, input, |
| 1571 | lookup_context.funcs.collect, lookup_context.collect_data); |
| 1572 | recurse_lookups (c, |
| 1573 | lookupCount, lookupRecord); |
| 1574 | } |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1575 | |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1576 | static inline bool context_would_apply_lookup (hb_would_apply_context_t *c, |
| 1577 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1578 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 0beb66e | 2012-12-05 18:46:04 -0500 | [diff] [blame] | 1579 | unsigned int lookupCount HB_UNUSED, |
| 1580 | const LookupRecord lookupRecord[] HB_UNUSED, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1581 | ContextApplyLookupContext &lookup_context) |
| 1582 | { |
| 1583 | return would_match_input (c, |
| 1584 | inputCount, input, |
| 1585 | lookup_context.funcs.match, lookup_context.match_data); |
| 1586 | } |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 1587 | static inline bool context_apply_lookup (hb_ot_apply_context_t *c, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1588 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1589 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1590 | unsigned int lookupCount, |
| 1591 | const LookupRecord lookupRecord[], |
| 1592 | ContextApplyLookupContext &lookup_context) |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1593 | { |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 1594 | unsigned match_end = 0; |
| 1595 | unsigned match_positions[HB_MAX_CONTEXT_LENGTH]; |
| 1596 | if (match_input (c, |
| 1597 | inputCount, input, |
| 1598 | lookup_context.funcs.match, lookup_context.match_data, |
| 1599 | &match_end, match_positions)) |
| 1600 | { |
| 1601 | c->buffer->unsafe_to_break (c->buffer->idx, match_end); |
| 1602 | apply_lookup (c, |
| 1603 | inputCount, match_positions, |
| 1604 | lookupCount, lookupRecord, |
| 1605 | match_end); |
| 1606 | return true; |
| 1607 | } |
| 1608 | else |
| 1609 | { |
| 1610 | c->buffer->unsafe_to_concat (c->buffer->idx, match_end); |
| 1611 | return false; |
| 1612 | } |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 1613 | } |
| 1614 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1615 | struct Rule |
| 1616 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1617 | bool intersects (const hb_set_t *glyphs, ContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1618 | { |
| 1619 | return context_intersects (glyphs, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1620 | inputCount, inputZ.arrayZ, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1621 | lookup_context); |
| 1622 | } |
| 1623 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1624 | 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] | 1625 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 1626 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 014e038 | 2020-03-31 16:29:29 -0700 | [diff] [blame] | 1627 | |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 1628 | const UnsizedArrayOf<LookupRecord> &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 1629 | (inputZ.as_array ((inputCount ? inputCount - 1 : 0))); |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 1630 | context_closure_lookup (c, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1631 | inputCount, inputZ.arrayZ, |
| 1632 | lookupCount, lookupRecord.arrayZ, |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1633 | value, lookup_context); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1634 | } |
| 1635 | |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 1636 | void closure_lookups (hb_closure_lookups_context_t *c, |
| 1637 | ContextClosureLookupContext &lookup_context) const |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1638 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 1639 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 1640 | if (!intersects (c->glyphs, lookup_context)) return; |
Garret Rieger | 014e038 | 2020-03-31 16:29:29 -0700 | [diff] [blame] | 1641 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1642 | const UnsizedArrayOf<LookupRecord> &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
| 1643 | (inputZ.as_array (inputCount ? inputCount - 1 : 0)); |
| 1644 | recurse_lookups (c, lookupCount, lookupRecord.arrayZ); |
| 1645 | } |
| 1646 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1647 | void collect_glyphs (hb_collect_glyphs_context_t *c, |
| 1648 | ContextCollectGlyphsLookupContext &lookup_context) const |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1649 | { |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 1650 | const UnsizedArrayOf<LookupRecord> &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 1651 | (inputZ.as_array (inputCount ? inputCount - 1 : 0)); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1652 | context_collect_glyphs_lookup (c, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1653 | inputCount, inputZ.arrayZ, |
| 1654 | lookupCount, lookupRecord.arrayZ, |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1655 | lookup_context); |
| 1656 | } |
| 1657 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1658 | bool would_apply (hb_would_apply_context_t *c, |
| 1659 | ContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1660 | { |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 1661 | const UnsizedArrayOf<LookupRecord> &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 1662 | (inputZ.as_array (inputCount ? inputCount - 1 : 0)); |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 1663 | return context_would_apply_lookup (c, |
| 1664 | inputCount, inputZ.arrayZ, |
| 1665 | lookupCount, lookupRecord.arrayZ, |
| 1666 | lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1667 | } |
| 1668 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1669 | bool apply (hb_ot_apply_context_t *c, |
| 1670 | ContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1671 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 1672 | TRACE_APPLY (this); |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 1673 | const UnsizedArrayOf<LookupRecord> &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 1674 | (inputZ.as_array (inputCount ? inputCount - 1 : 0)); |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1675 | 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] | 1676 | } |
| 1677 | |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1678 | bool serialize (hb_serialize_context_t *c, |
| 1679 | const hb_map_t *input_mapping, /* old->new glyphid or class mapping */ |
| 1680 | const hb_map_t *lookup_map) const |
| 1681 | { |
| 1682 | TRACE_SERIALIZE (this); |
| 1683 | auto *out = c->start_embed (this); |
| 1684 | if (unlikely (!c->extend_min (out))) return_trace (false); |
| 1685 | |
| 1686 | out->inputCount = inputCount; |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1687 | const hb_array_t<const HBUINT16> input = inputZ.as_array (inputCount - 1); |
| 1688 | for (const auto org : input) |
| 1689 | { |
| 1690 | HBUINT16 d; |
| 1691 | d = input_mapping->get (org); |
| 1692 | c->copy (d); |
| 1693 | } |
| 1694 | |
| 1695 | const UnsizedArrayOf<LookupRecord> &lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> |
Ebrahim Byagowi | 5a7cc7f | 2020-07-29 08:33:32 +0430 | [diff] [blame] | 1696 | (inputZ.as_array ((inputCount ? inputCount - 1 : 0))); |
Garret Rieger | f51b48c | 2021-11-02 16:16:52 -0700 | [diff] [blame] | 1697 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 1698 | unsigned count = serialize_lookuprecord_array (c, lookupRecord.as_array (lookupCount), lookup_map); |
| 1699 | 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] | 1700 | } |
| 1701 | |
| 1702 | bool subset (hb_subset_context_t *c, |
| 1703 | const hb_map_t *lookup_map, |
| 1704 | const hb_map_t *klass_map = nullptr) const |
| 1705 | { |
| 1706 | TRACE_SUBSET (this); |
Qunxin Liu | e88fc41 | 2021-11-18 16:53:36 -0800 | [diff] [blame] | 1707 | if (unlikely (!inputCount)) return_trace (false); |
| 1708 | const hb_array_t<const HBUINT16> input = inputZ.as_array (inputCount - 1); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1709 | |
| 1710 | const hb_map_t *mapping = klass_map == nullptr ? c->plan->glyph_map : klass_map; |
| 1711 | if (!hb_all (input, mapping)) return_trace (false); |
| 1712 | return_trace (serialize (c->serializer, mapping, lookup_map)); |
| 1713 | } |
| 1714 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 1715 | public: |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1716 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 1717 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 1718 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 5aad819 | 2017-11-03 17:16:26 -0400 | [diff] [blame] | 1719 | return_trace (inputCount.sanitize (c) && |
| 1720 | lookupCount.sanitize (c) && |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1721 | c->check_range (inputZ.arrayZ, |
Behdad Esfahbod | 9c6921c | 2018-11-30 15:16:57 -0500 | [diff] [blame] | 1722 | inputZ.item_size * (inputCount ? inputCount - 1 : 0) + |
Behdad Esfahbod | 6d7c6e1 | 2018-02-07 14:09:56 -0600 | [diff] [blame] | 1723 | LookupRecord::static_size * lookupCount)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 1724 | } |
| 1725 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 1726 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1727 | HBUINT16 inputCount; /* Total number of glyphs in input |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1728 | * glyph sequence--includes the first |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1729 | * glyph */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1730 | HBUINT16 lookupCount; /* Number of LookupRecords */ |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1731 | UnsizedArrayOf<HBUINT16> |
Ebrahim Byagowi | ce114d6 | 2019-12-31 15:53:02 +0330 | [diff] [blame] | 1732 | inputZ; /* Array of match inputs--start with |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1733 | * second glyph */ |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 1734 | /*UnsizedArrayOf<LookupRecord> |
| 1735 | lookupRecordX;*/ /* Array of LookupRecords--in |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1736 | * design order */ |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 1737 | public: |
Behdad Esfahbod | 6d7c6e1 | 2018-02-07 14:09:56 -0600 | [diff] [blame] | 1738 | DEFINE_SIZE_ARRAY (4, inputZ); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1739 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1740 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1741 | struct RuleSet |
| 1742 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1743 | bool intersects (const hb_set_t *glyphs, |
| 1744 | ContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1745 | { |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1746 | return |
| 1747 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1748 | | hb_map (hb_add (this)) |
| 1749 | | hb_map ([&] (const Rule &_) { return _.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1750 | | hb_any |
| 1751 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1754 | void closure (hb_closure_context_t *c, unsigned value, |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1755 | ContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1756 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 1757 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 014e038 | 2020-03-31 16:29:29 -0700 | [diff] [blame] | 1758 | |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1759 | return |
| 1760 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1761 | | hb_map (hb_add (this)) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1762 | | hb_apply ([&] (const Rule &_) { _.closure (c, value, lookup_context); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1763 | ; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1764 | } |
| 1765 | |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 1766 | void closure_lookups (hb_closure_lookups_context_t *c, |
| 1767 | ContextClosureLookupContext &lookup_context) const |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1768 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 1769 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1770 | + hb_iter (rule) |
| 1771 | | hb_map (hb_add (this)) |
Garret Rieger | e31c269 | 2020-09-28 16:51:25 -0700 | [diff] [blame] | 1772 | | hb_apply ([&] (const Rule &_) { _.closure_lookups (c, lookup_context); }) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1773 | ; |
| 1774 | } |
| 1775 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1776 | void collect_glyphs (hb_collect_glyphs_context_t *c, |
| 1777 | ContextCollectGlyphsLookupContext &lookup_context) const |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1778 | { |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1779 | return |
| 1780 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1781 | | hb_map (hb_add (this)) |
| 1782 | | hb_apply ([&] (const Rule &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1783 | ; |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1784 | } |
| 1785 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1786 | bool would_apply (hb_would_apply_context_t *c, |
| 1787 | ContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1788 | { |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1789 | return |
| 1790 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1791 | | hb_map (hb_add (this)) |
| 1792 | | hb_map ([&] (const Rule &_) { return _.would_apply (c, lookup_context); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1793 | | hb_any |
| 1794 | ; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1795 | } |
| 1796 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1797 | bool apply (hb_ot_apply_context_t *c, |
| 1798 | ContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1799 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 1800 | TRACE_APPLY (this); |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1801 | return_trace ( |
| 1802 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1803 | | hb_map (hb_add (this)) |
| 1804 | | hb_map ([&] (const Rule &_) { return _.apply (c, lookup_context); }) |
Behdad Esfahbod | 22ec4c3 | 2019-03-29 22:27:46 -0700 | [diff] [blame] | 1805 | | hb_any |
| 1806 | ) |
| 1807 | ; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1808 | } |
| 1809 | |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1810 | bool subset (hb_subset_context_t *c, |
| 1811 | const hb_map_t *lookup_map, |
| 1812 | const hb_map_t *klass_map = nullptr) const |
| 1813 | { |
| 1814 | TRACE_SUBSET (this); |
| 1815 | |
| 1816 | auto snap = c->serializer->snapshot (); |
| 1817 | auto *out = c->serializer->start_embed (*this); |
| 1818 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 1819 | |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 1820 | for (const Offset16To<Rule>& _ : rule) |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1821 | { |
| 1822 | if (!_) continue; |
Garret Rieger | c2cc566 | 2021-09-22 14:15:55 -0700 | [diff] [blame] | 1823 | auto o_snap = c->serializer->snapshot (); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1824 | auto *o = out->rule.serialize_append (c->serializer); |
| 1825 | if (unlikely (!o)) continue; |
| 1826 | |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1827 | if (!o->serialize_subset (c, _, this, lookup_map, klass_map)) |
| 1828 | { |
Ebrahim Byagowi | 5a7cc7f | 2020-07-29 08:33:32 +0430 | [diff] [blame] | 1829 | out->rule.pop (); |
| 1830 | c->serializer->revert (o_snap); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | bool ret = bool (out->rule); |
| 1835 | if (!ret) c->serializer->revert (snap); |
| 1836 | |
| 1837 | return_trace (ret); |
| 1838 | } |
| 1839 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1840 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 1841 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 1842 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1843 | return_trace (rule.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 1844 | } |
| 1845 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 1846 | protected: |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 1847 | Array16OfOffset16To<Rule> |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 1848 | rule; /* Array of Rule tables |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1849 | * ordered by preference */ |
Behdad Esfahbod | ed07422 | 2010-05-10 18:08:46 -0400 | [diff] [blame] | 1850 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 1851 | DEFINE_SIZE_ARRAY (2, rule); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1852 | }; |
| 1853 | |
| 1854 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1855 | struct ContextFormat1 |
| 1856 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1857 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1858 | { |
| 1859 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1860 | {intersects_glyph, intersected_glyph}, |
| 1861 | ContextFormat::SimpleContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1862 | nullptr |
| 1863 | }; |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 1864 | |
| 1865 | return |
| 1866 | + hb_zip (this+coverage, ruleSet) |
| 1867 | | hb_filter (*glyphs, hb_first) |
| 1868 | | hb_map (hb_second) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1869 | | hb_map (hb_add (this)) |
| 1870 | | hb_map ([&] (const RuleSet &_) { return _.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 1871 | | hb_any |
| 1872 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 1875 | bool may_have_non_1to1 () const |
| 1876 | { return true; } |
| 1877 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1878 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 1879 | { |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 1880 | hb_set_t* cur_active_glyphs = &c->push_cur_active_glyphs (); |
| 1881 | get_coverage ().intersected_coverage_glyphs (&c->previous_parent_active_glyphs (), |
| 1882 | cur_active_glyphs); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1883 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1884 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1885 | {intersects_glyph, intersected_glyph}, |
| 1886 | ContextFormat::SimpleContext, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1887 | nullptr |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1888 | }; |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 1889 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1890 | + hb_zip (this+coverage, hb_range ((unsigned) ruleSet.len)) |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 1891 | | hb_filter ([&] (hb_codepoint_t _) { |
| 1892 | return c->previous_parent_active_glyphs ().has (_); |
| 1893 | }, hb_first) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 1894 | | hb_map ([&](const hb_pair_t<hb_codepoint_t, unsigned> _) { return hb_pair_t<unsigned, const RuleSet&> (_.first, this+ruleSet[_.second]); }) |
| 1895 | | 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] | 1896 | ; |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 1897 | |
| 1898 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 1899 | } |
| 1900 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1901 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 1902 | { |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 1903 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 1904 | {intersects_glyph, intersected_glyph}, |
| 1905 | ContextFormat::SimpleContext, |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 1906 | nullptr |
| 1907 | }; |
| 1908 | |
| 1909 | + hb_zip (this+coverage, ruleSet) |
| 1910 | | hb_filter (*c->glyphs, hb_first) |
| 1911 | | hb_map (hb_second) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1912 | | hb_map (hb_add (this)) |
Garret Rieger | e31c269 | 2020-09-28 16:51:25 -0700 | [diff] [blame] | 1913 | | hb_apply ([&] (const RuleSet &_) { _.closure_lookups (c, lookup_context); }) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 1914 | ; |
| 1915 | } |
| 1916 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 1917 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 1918 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1919 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 1920 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 1921 | (this+coverage).collect_coverage (c->input); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1922 | |
| 1923 | struct ContextCollectGlyphsLookupContext lookup_context = { |
| 1924 | {collect_glyph}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1925 | nullptr |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 1926 | }; |
| 1927 | |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 1928 | + hb_iter (ruleSet) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 1929 | | hb_map (hb_add (this)) |
| 1930 | | hb_apply ([&] (const RuleSet &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 1931 | ; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 1932 | } |
| 1933 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1934 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1935 | { |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 1936 | 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] | 1937 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 1938 | {match_glyph}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1939 | nullptr |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1940 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 1941 | return rule_set.would_apply (c, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 1942 | } |
| 1943 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 1944 | const Coverage &get_coverage () const { return this+coverage; } |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 1945 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1946 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 1947 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 1948 | TRACE_APPLY (this); |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 1949 | unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 1950 | if (likely (index == NOT_COVERED)) |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1951 | return_trace (false); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 1952 | |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1953 | const RuleSet &rule_set = this+ruleSet[index]; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 1954 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 1955 | {match_glyph}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1956 | nullptr |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1957 | }; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1958 | return_trace (rule_set.apply (c, lookup_context)); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1959 | } |
| 1960 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1961 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 1962 | { |
| 1963 | TRACE_SUBSET (this); |
Garret Rieger | e583505 | 2020-09-29 11:05:08 -0700 | [diff] [blame] | 1964 | const hb_set_t &glyphset = *c->plan->glyphset_gsub (); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1965 | const hb_map_t &glyph_map = *c->plan->glyph_map; |
| 1966 | |
| 1967 | auto *out = c->serializer->start_embed (*this); |
| 1968 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 1969 | out->format = format; |
| 1970 | |
| 1971 | const hb_map_t *lookup_map = c->table_tag == HB_OT_TAG_GSUB ? c->plan->gsub_lookups : c->plan->gpos_lookups; |
| 1972 | hb_sorted_vector_t<hb_codepoint_t> new_coverage; |
| 1973 | + hb_zip (this+coverage, ruleSet) |
| 1974 | | hb_filter (glyphset, hb_first) |
| 1975 | | hb_filter (subset_offset_array (c, out->ruleSet, this, lookup_map), hb_second) |
| 1976 | | hb_map (hb_first) |
| 1977 | | hb_map (glyph_map) |
| 1978 | | hb_sink (new_coverage) |
| 1979 | ; |
| 1980 | |
Garret Rieger | 085aa65 | 2021-06-14 16:47:45 -0700 | [diff] [blame] | 1981 | out->coverage.serialize_serialize (c->serializer, new_coverage.iter ()); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 1982 | return_trace (bool (new_coverage)); |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 1985 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 1986 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 1987 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 1988 | return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 1989 | } |
| 1990 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 1991 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 1992 | HBUINT16 format; /* Format identifier--format = 1 */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 1993 | Offset16To<Coverage> |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1994 | coverage; /* Offset to Coverage table--from |
| 1995 | * beginning of table */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 1996 | Array16OfOffset16To<RuleSet> |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1997 | ruleSet; /* Array of RuleSet tables |
| 1998 | * ordered by Coverage Index */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 1999 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 2000 | DEFINE_SIZE_ARRAY (6, ruleSet); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2001 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2002 | |
| 2003 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2004 | struct ContextFormat2 |
| 2005 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2006 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2007 | { |
| 2008 | if (!(this+coverage).intersects (glyphs)) |
| 2009 | return false; |
| 2010 | |
| 2011 | const ClassDef &class_def = this+classDef; |
| 2012 | |
| 2013 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2014 | {intersects_class, intersected_class_glyphs}, |
| 2015 | ContextFormat::ClassBasedContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2016 | &class_def |
| 2017 | }; |
| 2018 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2019 | hb_set_t retained_coverage_glyphs; |
| 2020 | (this+coverage).intersected_coverage_glyphs (glyphs, &retained_coverage_glyphs); |
| 2021 | |
| 2022 | hb_set_t coverage_glyph_classes; |
| 2023 | class_def.intersected_classes (&retained_coverage_glyphs, &coverage_glyph_classes); |
| 2024 | |
| 2025 | |
Behdad Esfahbod | 668d2d5 | 2019-03-29 22:48:38 -0700 | [diff] [blame] | 2026 | return |
Behdad Esfahbod | 26111a1 | 2020-06-28 02:59:47 -0700 | [diff] [blame] | 2027 | + hb_iter (ruleSet) |
| 2028 | | hb_map (hb_add (this)) |
| 2029 | | hb_enumerate |
| 2030 | | hb_map ([&] (const hb_pair_t<unsigned, const RuleSet &> p) |
Behdad Esfahbod | e530692 | 2019-03-29 23:31:07 -0700 | [diff] [blame] | 2031 | { return class_def.intersects_class (glyphs, p.first) && |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2032 | coverage_glyph_classes.has (p.first) && |
Behdad Esfahbod | 26111a1 | 2020-06-28 02:59:47 -0700 | [diff] [blame] | 2033 | p.second.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | 668d2d5 | 2019-03-29 22:48:38 -0700 | [diff] [blame] | 2034 | | hb_any |
| 2035 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2036 | } |
| 2037 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 2038 | bool may_have_non_1to1 () const |
| 2039 | { return true; } |
| 2040 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2041 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2042 | { |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2043 | if (!(this+coverage).intersects (c->glyphs)) |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2044 | return; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2045 | |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 2046 | hb_set_t* cur_active_glyphs = &c->push_cur_active_glyphs (); |
| 2047 | get_coverage ().intersected_coverage_glyphs (&c->previous_parent_active_glyphs (), |
| 2048 | cur_active_glyphs); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 2049 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2050 | const ClassDef &class_def = this+classDef; |
| 2051 | |
| 2052 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2053 | {intersects_class, intersected_class_glyphs}, |
| 2054 | ContextFormat::ClassBasedContext, |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 2055 | &class_def |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2056 | }; |
| 2057 | |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 2058 | + hb_enumerate (ruleSet) |
Behdad Esfahbod | 78d35f0 | 2019-05-15 18:15:05 -0700 | [diff] [blame] | 2059 | | hb_filter ([&] (unsigned _) |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 2060 | { return class_def.intersects_class (&c->parent_active_glyphs (), _); }, |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 2061 | hb_first) |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 2062 | | hb_apply ([&] (const hb_pair_t<unsigned, const Offset16To<RuleSet>&> _) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2063 | { |
| 2064 | const RuleSet& rule_set = this+_.second; |
| 2065 | rule_set.closure (c, _.first, lookup_context); |
| 2066 | }) |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 2067 | ; |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 2068 | |
| 2069 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2070 | } |
| 2071 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2072 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 2073 | { |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 2074 | if (!(this+coverage).intersects (c->glyphs)) |
| 2075 | return; |
| 2076 | |
| 2077 | const ClassDef &class_def = this+classDef; |
| 2078 | |
| 2079 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 2080 | {intersects_class, intersected_class_glyphs}, |
| 2081 | ContextFormat::ClassBasedContext, |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 2082 | &class_def |
| 2083 | }; |
| 2084 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2085 | + hb_iter (ruleSet) |
| 2086 | | hb_map (hb_add (this)) |
Garret Rieger | 9fad540 | 2020-09-28 13:24:25 -0700 | [diff] [blame] | 2087 | | hb_enumerate |
| 2088 | | hb_filter ([&] (const hb_pair_t<unsigned, const RuleSet &> p) |
| 2089 | { return class_def.intersects_class (c->glyphs, p.first); }) |
| 2090 | | hb_map (hb_second) |
| 2091 | | hb_apply ([&] (const RuleSet & _) |
| 2092 | { _.closure_lookups (c, lookup_context); }); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2093 | } |
| 2094 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 2095 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 2096 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2097 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2098 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 2099 | (this+coverage).collect_coverage (c->input); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2100 | |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 2101 | const ClassDef &class_def = this+classDef; |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2102 | struct ContextCollectGlyphsLookupContext lookup_context = { |
| 2103 | {collect_class}, |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 2104 | &class_def |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2105 | }; |
| 2106 | |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2107 | + hb_iter (ruleSet) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2108 | | hb_map (hb_add (this)) |
| 2109 | | hb_apply ([&] (const RuleSet &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2110 | ; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2111 | } |
| 2112 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2113 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2114 | { |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2115 | const ClassDef &class_def = this+classDef; |
Behdad Esfahbod | 2dc1141 | 2012-11-24 19:16:34 -0500 | [diff] [blame] | 2116 | unsigned int index = class_def.get_class (c->glyphs[0]); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2117 | const RuleSet &rule_set = this+ruleSet[index]; |
| 2118 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2119 | {match_class}, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2120 | &class_def |
| 2121 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 2122 | return rule_set.would_apply (c, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2123 | } |
| 2124 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 2125 | const Coverage &get_coverage () const { return this+coverage; } |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 2126 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2127 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2128 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2129 | TRACE_APPLY (this); |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 2130 | unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2131 | if (likely (index == NOT_COVERED)) return_trace (false); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 2132 | |
| 2133 | const ClassDef &class_def = this+classDef; |
Behdad Esfahbod | 2dc1141 | 2012-11-24 19:16:34 -0500 | [diff] [blame] | 2134 | index = class_def.get_class (c->buffer->cur().codepoint); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2135 | const RuleSet &rule_set = this+ruleSet[index]; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2136 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2137 | {match_class}, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 2138 | &class_def |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2139 | }; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2140 | return_trace (rule_set.apply (c, lookup_context)); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2141 | } |
| 2142 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2143 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2144 | { |
| 2145 | TRACE_SUBSET (this); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2146 | auto *out = c->serializer->start_embed (*this); |
| 2147 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 2148 | out->format = format; |
| 2149 | if (unlikely (!out->coverage.serialize_subset (c, coverage, this))) |
| 2150 | return_trace (false); |
| 2151 | |
| 2152 | hb_map_t klass_map; |
| 2153 | out->classDef.serialize_subset (c, classDef, this, &klass_map); |
| 2154 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2155 | const hb_set_t* glyphset = c->plan->glyphset_gsub (); |
| 2156 | hb_set_t retained_coverage_glyphs; |
| 2157 | (this+coverage).intersected_coverage_glyphs (glyphset, &retained_coverage_glyphs); |
Garret Rieger | f51b48c | 2021-11-02 16:16:52 -0700 | [diff] [blame] | 2158 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2159 | hb_set_t coverage_glyph_classes; |
| 2160 | (this+classDef).intersected_classes (&retained_coverage_glyphs, &coverage_glyph_classes); |
| 2161 | |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2162 | const hb_map_t *lookup_map = c->table_tag == HB_OT_TAG_GSUB ? c->plan->gsub_lookups : c->plan->gpos_lookups; |
| 2163 | bool ret = true; |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2164 | int non_zero_index = -1, index = 0; |
Behdad Esfahbod | 6d94194 | 2021-02-19 17:08:10 -0700 | [diff] [blame] | 2165 | for (const auto& _ : + hb_enumerate (ruleSet) |
| 2166 | | hb_filter (klass_map, hb_first)) |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2167 | { |
| 2168 | auto *o = out->ruleSet.serialize_append (c->serializer); |
| 2169 | if (unlikely (!o)) |
| 2170 | { |
Ebrahim Byagowi | 5a7cc7f | 2020-07-29 08:33:32 +0430 | [diff] [blame] | 2171 | ret = false; |
| 2172 | break; |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2175 | if (coverage_glyph_classes.has (_.first) && |
| 2176 | o->serialize_subset (c, _.second, this, lookup_map, &klass_map)) |
Ebrahim Byagowi | 5a7cc7f | 2020-07-29 08:33:32 +0430 | [diff] [blame] | 2177 | non_zero_index = index; |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2178 | |
| 2179 | index++; |
| 2180 | } |
| 2181 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 2182 | if (!ret || non_zero_index == -1) return_trace (false); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2183 | |
| 2184 | //prune empty trailing ruleSets |
| 2185 | --index; |
| 2186 | while (index > non_zero_index) |
| 2187 | { |
| 2188 | out->ruleSet.pop (); |
| 2189 | index--; |
| 2190 | } |
| 2191 | |
| 2192 | return_trace (bool (out->ruleSet)); |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2195 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 2196 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2197 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2198 | 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] | 2199 | } |
| 2200 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2201 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2202 | HBUINT16 format; /* Format identifier--format = 2 */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 2203 | Offset16To<Coverage> |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2204 | coverage; /* Offset to Coverage table--from |
| 2205 | * beginning of table */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 2206 | Offset16To<ClassDef> |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2207 | classDef; /* Offset to glyph ClassDef table--from |
| 2208 | * beginning of table */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 2209 | Array16OfOffset16To<RuleSet> |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2210 | ruleSet; /* Array of RuleSet tables |
| 2211 | * ordered by class */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 2212 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 2213 | DEFINE_SIZE_ARRAY (8, ruleSet); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2214 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2215 | |
| 2216 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2217 | struct ContextFormat3 |
| 2218 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2219 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2220 | { |
Behdad Esfahbod | fb05908 | 2018-11-30 20:45:40 -0500 | [diff] [blame] | 2221 | if (!(this+coverageZ[0]).intersects (glyphs)) |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2222 | return false; |
| 2223 | |
| 2224 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2225 | {intersects_coverage, intersected_coverage_glyphs}, |
| 2226 | ContextFormat::CoverageBasedContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2227 | this |
| 2228 | }; |
| 2229 | return context_intersects (glyphs, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2230 | glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2231 | lookup_context); |
| 2232 | } |
| 2233 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 2234 | bool may_have_non_1to1 () const |
| 2235 | { return true; } |
| 2236 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2237 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2238 | { |
Behdad Esfahbod | fb05908 | 2018-11-30 20:45:40 -0500 | [diff] [blame] | 2239 | if (!(this+coverageZ[0]).intersects (c->glyphs)) |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2240 | return; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2241 | |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 2242 | hb_set_t* cur_active_glyphs = &c->push_cur_active_glyphs (); |
| 2243 | get_coverage ().intersected_coverage_glyphs (&c->previous_parent_active_glyphs (), |
| 2244 | cur_active_glyphs); |
| 2245 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 2246 | |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 2247 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2248 | struct ContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2249 | {intersects_coverage, intersected_coverage_glyphs}, |
| 2250 | ContextFormat::CoverageBasedContext, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2251 | this |
| 2252 | }; |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2253 | context_closure_lookup (c, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2254 | glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2255 | lookupCount, lookupRecord, |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2256 | 0, lookup_context); |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 2257 | |
| 2258 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2259 | } |
| 2260 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2261 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 2262 | { |
Garret Rieger | a5c0ec7 | 2020-09-25 14:57:20 -0700 | [diff] [blame] | 2263 | if (!intersects (c->glyphs)) |
| 2264 | return; |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2265 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
| 2266 | recurse_lookups (c, lookupCount, lookupRecord); |
| 2267 | } |
| 2268 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 2269 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 2270 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2271 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2272 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 2273 | (this+coverageZ[0]).collect_coverage (c->input); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2274 | |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 2275 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2276 | struct ContextCollectGlyphsLookupContext lookup_context = { |
| 2277 | {collect_coverage}, |
Behdad Esfahbod | e75943d | 2012-11-30 08:38:24 +0200 | [diff] [blame] | 2278 | this |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2279 | }; |
| 2280 | |
| 2281 | context_collect_glyphs_lookup (c, |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2282 | glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), |
Behdad Esfahbod | d0a5233 | 2012-11-23 18:54:59 -0500 | [diff] [blame] | 2283 | lookupCount, lookupRecord, |
| 2284 | lookup_context); |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2285 | } |
| 2286 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2287 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2288 | { |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 2289 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2290 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2291 | {match_coverage}, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2292 | this |
| 2293 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 2294 | return context_would_apply_lookup (c, |
| 2295 | glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), |
| 2296 | lookupCount, lookupRecord, |
| 2297 | lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2298 | } |
| 2299 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 2300 | const Coverage &get_coverage () const { return this+coverageZ[0]; } |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 2301 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2302 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2303 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2304 | TRACE_APPLY (this); |
Behdad Esfahbod | fb05908 | 2018-11-30 20:45:40 -0500 | [diff] [blame] | 2305 | unsigned int index = (this+coverageZ[0]).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2306 | if (likely (index == NOT_COVERED)) return_trace (false); |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 2307 | |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 2308 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2309 | struct ContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2310 | {match_coverage}, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 2311 | this |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2312 | }; |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2313 | 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] | 2314 | } |
| 2315 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2316 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2317 | { |
| 2318 | TRACE_SUBSET (this); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2319 | auto *out = c->serializer->start_embed (this); |
| 2320 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 2321 | |
| 2322 | out->format = format; |
| 2323 | out->glyphCount = glyphCount; |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2324 | |
Behdad Esfahbod | 26111a1 | 2020-06-28 02:59:47 -0700 | [diff] [blame] | 2325 | auto coverages = coverageZ.as_array (glyphCount); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2326 | |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 2327 | for (const Offset16To<Coverage>& offset : coverages) |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2328 | { |
Behdad Esfahbod | 6d94194 | 2021-02-19 17:08:10 -0700 | [diff] [blame] | 2329 | /* 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] | 2330 | auto *o = c->serializer->allocate_size<Offset16To<Coverage>> (Offset16To<Coverage>::static_size); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2331 | if (unlikely (!o)) return_trace (false); |
| 2332 | if (!o->serialize_subset (c, offset, this)) return_trace (false); |
| 2333 | } |
| 2334 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2335 | const UnsizedArrayOf<LookupRecord>& lookupRecord = StructAfter<UnsizedArrayOf<LookupRecord>> (coverageZ.as_array (glyphCount)); |
Qunxin Liu | 8b5d3eb | 2020-04-17 11:58:31 -0700 | [diff] [blame] | 2336 | 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] | 2337 | |
Ebrahim Byagowi | a79d0e4 | 2020-05-21 07:32:58 +0430 | [diff] [blame] | 2338 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2339 | unsigned count = serialize_lookuprecord_array (c->serializer, lookupRecord.as_array (lookupCount), lookup_map); |
| 2340 | 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] | 2341 | } |
| 2342 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2343 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 2344 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2345 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2346 | if (!c->check_struct (this)) return_trace (false); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2347 | unsigned int count = glyphCount; |
Behdad Esfahbod | fb05908 | 2018-11-30 20:45:40 -0500 | [diff] [blame] | 2348 | if (!count) return_trace (false); /* We want to access coverageZ[0] freely. */ |
Behdad Esfahbod | 9507b05 | 2018-09-10 23:18:07 +0200 | [diff] [blame] | 2349 | if (!c->check_array (coverageZ.arrayZ, count)) return_trace (false); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2350 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2351 | if (!coverageZ[i].sanitize (c, this)) return_trace (false); |
Behdad Esfahbod | 0382b71 | 2018-11-02 12:23:26 -0400 | [diff] [blame] | 2352 | const LookupRecord *lookupRecord = &StructAfter<LookupRecord> (coverageZ.as_array (glyphCount)); |
Behdad Esfahbod | 9507b05 | 2018-09-10 23:18:07 +0200 | [diff] [blame] | 2353 | return_trace (c->check_array (lookupRecord, lookupCount)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2354 | } |
| 2355 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2356 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2357 | HBUINT16 format; /* Format identifier--format = 3 */ |
| 2358 | HBUINT16 glyphCount; /* Number of glyphs in the input glyph |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2359 | * sequence */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2360 | HBUINT16 lookupCount; /* Number of LookupRecords */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 2361 | UnsizedArrayOf<Offset16To<Coverage>> |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2362 | coverageZ; /* Array of offsets to Coverage |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 2363 | * table in glyph sequence order */ |
Behdad Esfahbod | bc485a9 | 2018-09-10 23:02:24 +0200 | [diff] [blame] | 2364 | /*UnsizedArrayOf<LookupRecord> |
| 2365 | lookupRecordX;*/ /* Array of LookupRecords--in |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2366 | * design order */ |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 2367 | public: |
Behdad Esfahbod | 6d7c6e1 | 2018-02-07 14:09:56 -0600 | [diff] [blame] | 2368 | DEFINE_SIZE_ARRAY (6, coverageZ); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2369 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2370 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2371 | struct Context |
| 2372 | { |
Behdad Esfahbod | 36bb24f | 2019-05-05 10:14:17 -0700 | [diff] [blame] | 2373 | template <typename context_t, typename ...Ts> |
Behdad Esfahbod | 83e3eab | 2019-05-07 20:58:43 -0700 | [diff] [blame] | 2374 | typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2375 | { |
Behdad Esfahbod | 00f6a8e | 2014-12-12 20:36:49 -0800 | [diff] [blame] | 2376 | TRACE_DISPATCH (this, u.format); |
Behdad Esfahbod | f396fbb | 2015-10-09 12:25:55 -0400 | [diff] [blame] | 2377 | 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] | 2378 | switch (u.format) { |
Behdad Esfahbod | 6d555ce | 2021-11-02 00:18:22 -0600 | [diff] [blame] | 2379 | case 1: return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...)); |
| 2380 | case 2: return_trace (c->dispatch (u.format2, std::forward<Ts> (ds)...)); |
| 2381 | case 3: return_trace (c->dispatch (u.format3, std::forward<Ts> (ds)...)); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2382 | default:return_trace (c->default_return_value ()); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2383 | } |
| 2384 | } |
| 2385 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2386 | protected: |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2387 | union { |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2388 | HBUINT16 format; /* Format identifier */ |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 2389 | ContextFormat1 format1; |
| 2390 | ContextFormat2 format2; |
| 2391 | ContextFormat3 format3; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2392 | } u; |
| 2393 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 2394 | |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2395 | |
| 2396 | /* Chaining Contextual lookups */ |
| 2397 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2398 | struct ChainContextClosureLookupContext |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2399 | { |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2400 | ContextClosureFuncs funcs; |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2401 | ContextFormat context_format; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2402 | const void *intersects_data[3]; |
| 2403 | }; |
| 2404 | |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2405 | struct ChainContextCollectGlyphsLookupContext |
| 2406 | { |
| 2407 | ContextCollectGlyphsFuncs funcs; |
| 2408 | const void *collect_data[3]; |
| 2409 | }; |
| 2410 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2411 | struct ChainContextApplyLookupContext |
| 2412 | { |
| 2413 | ContextApplyFuncs funcs; |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 2414 | const void *match_data[3]; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 2415 | }; |
| 2416 | |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2417 | static inline bool chain_context_intersects (const hb_set_t *glyphs, |
| 2418 | unsigned int backtrackCount, |
| 2419 | const HBUINT16 backtrack[], |
| 2420 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
| 2421 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
| 2422 | unsigned int lookaheadCount, |
| 2423 | const HBUINT16 lookahead[], |
| 2424 | ChainContextClosureLookupContext &lookup_context) |
| 2425 | { |
Qunxin Liu | 44d88cf | 2020-05-08 15:33:34 -0700 | [diff] [blame] | 2426 | return array_is_subset_of (glyphs, |
| 2427 | backtrackCount, backtrack, |
| 2428 | lookup_context.funcs.intersects, lookup_context.intersects_data[0]) |
| 2429 | && array_is_subset_of (glyphs, |
| 2430 | inputCount ? inputCount - 1 : 0, input, |
| 2431 | lookup_context.funcs.intersects, lookup_context.intersects_data[1]) |
| 2432 | && array_is_subset_of (glyphs, |
| 2433 | lookaheadCount, lookahead, |
| 2434 | lookup_context.funcs.intersects, lookup_context.intersects_data[2]); |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2437 | static inline void chain_context_closure_lookup (hb_closure_context_t *c, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2438 | unsigned int backtrackCount, |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2439 | const HBUINT16 backtrack[], |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2440 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2441 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2442 | unsigned int lookaheadCount, |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2443 | const HBUINT16 lookahead[], |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2444 | unsigned int lookupCount, |
| 2445 | const LookupRecord lookupRecord[], |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2446 | unsigned value, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2447 | ChainContextClosureLookupContext &lookup_context) |
| 2448 | { |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2449 | if (chain_context_intersects (c->glyphs, |
| 2450 | backtrackCount, backtrack, |
| 2451 | inputCount, input, |
| 2452 | lookaheadCount, lookahead, |
| 2453 | lookup_context)) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2454 | context_closure_recurse_lookups (c, |
| 2455 | inputCount, input, |
| 2456 | lookupCount, lookupRecord, |
| 2457 | value, |
| 2458 | lookup_context.context_format, |
| 2459 | lookup_context.intersects_data[1], |
| 2460 | lookup_context.funcs.intersected_glyphs); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2461 | } |
| 2462 | |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2463 | 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] | 2464 | unsigned int backtrackCount, |
| 2465 | const HBUINT16 backtrack[], |
| 2466 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
| 2467 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
| 2468 | unsigned int lookaheadCount, |
| 2469 | const HBUINT16 lookahead[], |
| 2470 | unsigned int lookupCount, |
| 2471 | const LookupRecord lookupRecord[], |
| 2472 | ChainContextCollectGlyphsLookupContext &lookup_context) |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2473 | { |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 2474 | collect_array (c, c->before, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2475 | backtrackCount, backtrack, |
| 2476 | lookup_context.funcs.collect, lookup_context.collect_data[0]); |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 2477 | collect_array (c, c->input, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2478 | inputCount ? inputCount - 1 : 0, input, |
| 2479 | lookup_context.funcs.collect, lookup_context.collect_data[1]); |
Behdad Esfahbod | 8303593 | 2012-12-04 17:08:41 -0500 | [diff] [blame] | 2480 | collect_array (c, c->after, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2481 | lookaheadCount, lookahead, |
| 2482 | lookup_context.funcs.collect, lookup_context.collect_data[2]); |
| 2483 | recurse_lookups (c, |
| 2484 | lookupCount, lookupRecord); |
| 2485 | } |
| 2486 | |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2487 | static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c, |
| 2488 | unsigned int backtrackCount, |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2489 | const HBUINT16 backtrack[] HB_UNUSED, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2490 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2491 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2492 | unsigned int lookaheadCount, |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2493 | const HBUINT16 lookahead[] HB_UNUSED, |
Behdad Esfahbod | 0beb66e | 2012-12-05 18:46:04 -0500 | [diff] [blame] | 2494 | unsigned int lookupCount HB_UNUSED, |
| 2495 | const LookupRecord lookupRecord[] HB_UNUSED, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2496 | ChainContextApplyLookupContext &lookup_context) |
| 2497 | { |
Behdad Esfahbod | d9b204d | 2012-08-23 16:22:28 -0400 | [diff] [blame] | 2498 | return (c->zero_context ? !backtrackCount && !lookaheadCount : true) |
Behdad Esfahbod | 1f2bb17 | 2012-08-23 16:10:37 -0400 | [diff] [blame] | 2499 | && would_match_input (c, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2500 | inputCount, input, |
| 2501 | lookup_context.funcs.match, lookup_context.match_data[1]); |
| 2502 | } |
| 2503 | |
Behdad Esfahbod | fd03449 | 2018-01-17 16:46:51 -0800 | [diff] [blame] | 2504 | 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] | 2505 | unsigned int backtrackCount, |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2506 | const HBUINT16 backtrack[], |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2507 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2508 | const HBUINT16 input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2509 | unsigned int lookaheadCount, |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2510 | const HBUINT16 lookahead[], |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2511 | unsigned int lookupCount, |
| 2512 | const LookupRecord lookupRecord[], |
| 2513 | ChainContextApplyLookupContext &lookup_context) |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 2514 | { |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2515 | unsigned end_index = c->buffer->idx; |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 2516 | unsigned match_end = 0; |
| 2517 | unsigned match_positions[HB_MAX_CONTEXT_LENGTH]; |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2518 | if (!(match_input (c, |
| 2519 | inputCount, input, |
| 2520 | lookup_context.funcs.match, lookup_context.match_data[1], |
| 2521 | &match_end, match_positions) && (end_index = match_end) |
| 2522 | && match_lookahead (c, |
| 2523 | lookaheadCount, lookahead, |
| 2524 | lookup_context.funcs.match, lookup_context.match_data[2], |
| 2525 | match_end, &end_index))) |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 2526 | { |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2527 | c->buffer->unsafe_to_concat (c->buffer->idx, end_index); |
| 2528 | return false; |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 2529 | } |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2530 | |
| 2531 | unsigned start_index = c->buffer->out_len; |
| 2532 | if (!match_backtrack (c, |
| 2533 | backtrackCount, backtrack, |
| 2534 | lookup_context.funcs.match, lookup_context.match_data[0], |
| 2535 | &start_index)) |
Behdad Esfahbod | 78481b3 | 2021-11-21 16:50:34 -0700 | [diff] [blame] | 2536 | { |
| 2537 | c->buffer->unsafe_to_concat_from_outbuffer (start_index, end_index); |
| 2538 | return false; |
| 2539 | } |
Behdad Esfahbod | 60006d3 | 2022-01-20 15:29:28 -0700 | [diff] [blame] | 2540 | |
| 2541 | c->buffer->unsafe_to_break_from_outbuffer (start_index, end_index); |
| 2542 | apply_lookup (c, |
| 2543 | inputCount, match_positions, |
| 2544 | lookupCount, lookupRecord, |
| 2545 | match_end); |
| 2546 | return true; |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 2547 | } |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 2548 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2549 | struct ChainRule |
| 2550 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2551 | bool intersects (const hb_set_t *glyphs, ChainContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2552 | { |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 2553 | const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16>> (backtrack); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2554 | const Array16Of<HBUINT16> &lookahead = StructAfter<Array16Of<HBUINT16>> (input); |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2555 | return chain_context_intersects (glyphs, |
| 2556 | backtrack.len, backtrack.arrayZ, |
Behdad Esfahbod | effc7ce | 2018-09-13 20:21:54 +0200 | [diff] [blame] | 2557 | input.lenP1, input.arrayZ, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2558 | lookahead.len, lookahead.arrayZ, |
| 2559 | lookup_context); |
| 2560 | } |
| 2561 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2562 | void closure (hb_closure_context_t *c, unsigned value, |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2563 | ChainContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2564 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 2565 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 2566 | |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 2567 | const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16>> (backtrack); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2568 | const Array16Of<HBUINT16> &lookahead = StructAfter<Array16Of<HBUINT16>> (input); |
| 2569 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2570 | chain_context_closure_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2571 | backtrack.len, backtrack.arrayZ, |
Behdad Esfahbod | effc7ce | 2018-09-13 20:21:54 +0200 | [diff] [blame] | 2572 | input.lenP1, input.arrayZ, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2573 | lookahead.len, lookahead.arrayZ, |
| 2574 | lookup.len, lookup.arrayZ, |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2575 | value, |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 2576 | lookup_context); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2577 | } |
| 2578 | |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 2579 | void closure_lookups (hb_closure_lookups_context_t *c, |
| 2580 | ChainContextClosureLookupContext &lookup_context) const |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2581 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 2582 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 2583 | if (!intersects (c->glyphs, lookup_context)) return; |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 2584 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2585 | const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16>> (backtrack); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2586 | const Array16Of<HBUINT16> &lookahead = StructAfter<Array16Of<HBUINT16>> (input); |
| 2587 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2588 | recurse_lookups (c, lookup.len, lookup.arrayZ); |
| 2589 | } |
| 2590 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2591 | void collect_glyphs (hb_collect_glyphs_context_t *c, |
| 2592 | ChainContextCollectGlyphsLookupContext &lookup_context) const |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2593 | { |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 2594 | const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16>> (backtrack); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2595 | const Array16Of<HBUINT16> &lookahead = StructAfter<Array16Of<HBUINT16>> (input); |
| 2596 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2597 | chain_context_collect_glyphs_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2598 | backtrack.len, backtrack.arrayZ, |
Behdad Esfahbod | effc7ce | 2018-09-13 20:21:54 +0200 | [diff] [blame] | 2599 | input.lenP1, input.arrayZ, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2600 | lookahead.len, lookahead.arrayZ, |
| 2601 | lookup.len, lookup.arrayZ, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2602 | lookup_context); |
| 2603 | } |
| 2604 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2605 | bool would_apply (hb_would_apply_context_t *c, |
| 2606 | ChainContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2607 | { |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 2608 | const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16>> (backtrack); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2609 | const Array16Of<HBUINT16> &lookahead = StructAfter<Array16Of<HBUINT16>> (input); |
| 2610 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 2611 | return chain_context_would_apply_lookup (c, |
| 2612 | backtrack.len, backtrack.arrayZ, |
| 2613 | input.lenP1, input.arrayZ, |
| 2614 | lookahead.len, lookahead.arrayZ, lookup.len, |
| 2615 | lookup.arrayZ, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2616 | } |
| 2617 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2618 | bool apply (hb_ot_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2619 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2620 | TRACE_APPLY (this); |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 2621 | const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16>> (backtrack); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2622 | const Array16Of<HBUINT16> &lookahead = StructAfter<Array16Of<HBUINT16>> (input); |
| 2623 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2624 | return_trace (chain_context_apply_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2625 | backtrack.len, backtrack.arrayZ, |
Behdad Esfahbod | effc7ce | 2018-09-13 20:21:54 +0200 | [diff] [blame] | 2626 | input.lenP1, input.arrayZ, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 2627 | lookahead.len, lookahead.arrayZ, lookup.len, |
| 2628 | lookup.arrayZ, lookup_context)); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 2629 | } |
| 2630 | |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2631 | template<typename Iterator, |
| 2632 | hb_requires (hb_is_iterator (Iterator))> |
| 2633 | void serialize_array (hb_serialize_context_t *c, |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2634 | HBUINT16 len, |
| 2635 | Iterator it) const |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2636 | { |
| 2637 | c->copy (len); |
| 2638 | for (const auto g : it) |
Behdad Esfahbod | 83b66bf | 2021-02-23 13:04:25 -0700 | [diff] [blame] | 2639 | c->copy ((HBUINT16) g); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2640 | } |
| 2641 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2642 | bool serialize (hb_serialize_context_t *c, |
| 2643 | const hb_map_t *lookup_map, |
| 2644 | const hb_map_t *backtrack_map, |
| 2645 | const hb_map_t *input_map = nullptr, |
| 2646 | const hb_map_t *lookahead_map = nullptr) const |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2647 | { |
| 2648 | TRACE_SERIALIZE (this); |
| 2649 | auto *out = c->start_embed (this); |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2650 | if (unlikely (!out)) return_trace (false); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2651 | |
| 2652 | const hb_map_t *mapping = backtrack_map; |
| 2653 | serialize_array (c, backtrack.len, + backtrack.iter () |
| 2654 | | hb_map (mapping)); |
| 2655 | |
| 2656 | const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16>> (backtrack); |
| 2657 | if (input_map) mapping = input_map; |
| 2658 | serialize_array (c, input.lenP1, + input.iter () |
| 2659 | | hb_map (mapping)); |
| 2660 | |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2661 | const Array16Of<HBUINT16> &lookahead = StructAfter<Array16Of<HBUINT16>> (input); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2662 | if (lookahead_map) mapping = lookahead_map; |
| 2663 | serialize_array (c, lookahead.len, + lookahead.iter () |
| 2664 | | hb_map (mapping)); |
| 2665 | |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2666 | const Array16Of<LookupRecord> &lookupRecord = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 2667 | |
Garret Rieger | 8f47dd5 | 2020-11-04 11:05:22 -0800 | [diff] [blame] | 2668 | HBUINT16* lookupCount = c->embed (&(lookupRecord.len)); |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2669 | if (!lookupCount) return_trace (false); |
Garret Rieger | 8f47dd5 | 2020-11-04 11:05:22 -0800 | [diff] [blame] | 2670 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2671 | unsigned count = serialize_lookuprecord_array (c, lookupRecord.as_array (), lookup_map); |
| 2672 | return_trace (c->check_assign (*lookupCount, count, HB_SERIALIZE_ERROR_INT_OVERFLOW)); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | bool subset (hb_subset_context_t *c, |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 2676 | const hb_map_t *lookup_map, |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2677 | const hb_map_t *backtrack_map = nullptr, |
| 2678 | const hb_map_t *input_map = nullptr, |
| 2679 | const hb_map_t *lookahead_map = nullptr) const |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2680 | { |
| 2681 | TRACE_SUBSET (this); |
| 2682 | |
Qunxin Liu | b2fcca6 | 2019-10-24 15:15:26 -0700 | [diff] [blame] | 2683 | const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16>> (backtrack); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2684 | const Array16Of<HBUINT16> &lookahead = StructAfter<Array16Of<HBUINT16>> (input); |
Qunxin Liu | b2fcca6 | 2019-10-24 15:15:26 -0700 | [diff] [blame] | 2685 | |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2686 | if (!backtrack_map) |
| 2687 | { |
Garret Rieger | e583505 | 2020-09-29 11:05:08 -0700 | [diff] [blame] | 2688 | const hb_set_t &glyphset = *c->plan->glyphset_gsub (); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2689 | if (!hb_all (backtrack, glyphset) || |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2690 | !hb_all (input, glyphset) || |
| 2691 | !hb_all (lookahead, glyphset)) |
| 2692 | return_trace (false); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2693 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2694 | serialize (c->serializer, lookup_map, c->plan->glyph_map); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2695 | } |
| 2696 | else |
| 2697 | { |
| 2698 | if (!hb_all (backtrack, backtrack_map) || |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2699 | !hb_all (input, input_map) || |
| 2700 | !hb_all (lookahead, lookahead_map)) |
| 2701 | return_trace (false); |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 2702 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 2703 | serialize (c->serializer, lookup_map, backtrack_map, input_map, lookahead_map); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | return_trace (true); |
| 2707 | } |
| 2708 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2709 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 2710 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2711 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2712 | if (!backtrack.sanitize (c)) return_trace (false); |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 2713 | const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16>> (backtrack); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2714 | if (!input.sanitize (c)) return_trace (false); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2715 | const Array16Of<HBUINT16> &lookahead = StructAfter<Array16Of<HBUINT16>> (input); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2716 | if (!lookahead.sanitize (c)) return_trace (false); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2717 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2718 | return_trace (lookup.sanitize (c)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2719 | } |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2720 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2721 | protected: |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2722 | Array16Of<HBUINT16> |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 2723 | backtrack; /* Array of backtracking values |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2724 | * (to be matched before the input |
| 2725 | * sequence) */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2726 | HeadlessArrayOf<HBUINT16> |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 2727 | inputX; /* Array of input values (start with |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2728 | * second glyph) */ |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2729 | Array16Of<HBUINT16> |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 2730 | lookaheadX; /* Array of lookahead values's (to be |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 2731 | * matched after the input sequence) */ |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 2732 | Array16Of<LookupRecord> |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 2733 | lookupX; /* Array of LookupRecords--in |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2734 | * design order) */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 2735 | public: |
Behdad Esfahbod | bea34c7 | 2010-05-10 17:28:16 -0400 | [diff] [blame] | 2736 | DEFINE_SIZE_MIN (8); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2737 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2738 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2739 | struct ChainRuleSet |
| 2740 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2741 | bool intersects (const hb_set_t *glyphs, ChainContextClosureLookupContext &lookup_context) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2742 | { |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2743 | return |
| 2744 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2745 | | hb_map (hb_add (this)) |
| 2746 | | hb_map ([&] (const ChainRule &_) { return _.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2747 | | hb_any |
| 2748 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2749 | } |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2750 | 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] | 2751 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 2752 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 2753 | |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2754 | return |
| 2755 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2756 | | hb_map (hb_add (this)) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2757 | | hb_apply ([&] (const ChainRule &_) { _.closure (c, value, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2758 | ; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2759 | } |
| 2760 | |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 2761 | void closure_lookups (hb_closure_lookups_context_t *c, |
| 2762 | ChainContextClosureLookupContext &lookup_context) const |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2763 | { |
Ebrahim Byagowi | 0c65a23 | 2020-04-23 11:23:54 +0430 | [diff] [blame] | 2764 | if (unlikely (c->lookup_limit_exceeded ())) return; |
Garret Rieger | 4ad686b | 2020-03-25 23:32:28 -0700 | [diff] [blame] | 2765 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2766 | + hb_iter (rule) |
| 2767 | | hb_map (hb_add (this)) |
Garret Rieger | e31c269 | 2020-09-28 16:51:25 -0700 | [diff] [blame] | 2768 | | hb_apply ([&] (const ChainRule &_) { _.closure_lookups (c, lookup_context); }) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2769 | ; |
| 2770 | } |
| 2771 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2772 | 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] | 2773 | { |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2774 | return |
| 2775 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2776 | | hb_map (hb_add (this)) |
| 2777 | | hb_apply ([&] (const ChainRule &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2778 | ; |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2779 | } |
| 2780 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2781 | 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] | 2782 | { |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2783 | return |
| 2784 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2785 | | hb_map (hb_add (this)) |
| 2786 | | hb_map ([&] (const ChainRule &_) { return _.would_apply (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2787 | | hb_any |
| 2788 | ; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2789 | } |
| 2790 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2791 | bool apply (hb_ot_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2792 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2793 | TRACE_APPLY (this); |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2794 | return_trace ( |
| 2795 | + hb_iter (rule) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2796 | | hb_map (hb_add (this)) |
| 2797 | | hb_map ([&] (const ChainRule &_) { return _.apply (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2798 | | hb_any |
| 2799 | ) |
| 2800 | ; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 2801 | } |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2802 | |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2803 | bool subset (hb_subset_context_t *c, |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 2804 | const hb_map_t *lookup_map, |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2805 | const hb_map_t *backtrack_klass_map = nullptr, |
| 2806 | const hb_map_t *input_klass_map = nullptr, |
| 2807 | const hb_map_t *lookahead_klass_map = nullptr) const |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2808 | { |
| 2809 | TRACE_SUBSET (this); |
| 2810 | |
| 2811 | auto snap = c->serializer->snapshot (); |
| 2812 | auto *out = c->serializer->start_embed (*this); |
| 2813 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 2814 | |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 2815 | for (const Offset16To<ChainRule>& _ : rule) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2816 | { |
| 2817 | if (!_) continue; |
Garret Rieger | c2cc566 | 2021-09-22 14:15:55 -0700 | [diff] [blame] | 2818 | auto o_snap = c->serializer->snapshot (); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2819 | auto *o = out->rule.serialize_append (c->serializer); |
| 2820 | if (unlikely (!o)) continue; |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 2821 | |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 2822 | if (!o->serialize_subset (c, _, this, |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 2823 | lookup_map, |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2824 | backtrack_klass_map, |
| 2825 | input_klass_map, |
| 2826 | lookahead_klass_map)) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2827 | { |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 2828 | out->rule.pop (); |
| 2829 | c->serializer->revert (o_snap); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | bool ret = bool (out->rule); |
| 2834 | if (!ret) c->serializer->revert (snap); |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 2835 | |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2836 | return_trace (ret); |
| 2837 | } |
| 2838 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2839 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 2840 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2841 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2842 | return_trace (rule.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2843 | } |
| 2844 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2845 | protected: |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 2846 | Array16OfOffset16To<ChainRule> |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 2847 | rule; /* Array of ChainRule tables |
| 2848 | * ordered by preference */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 2849 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 2850 | DEFINE_SIZE_ARRAY (2, rule); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2851 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2852 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2853 | struct ChainContextFormat1 |
| 2854 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2855 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2856 | { |
| 2857 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2858 | {intersects_glyph, intersected_glyph}, |
| 2859 | ContextFormat::SimpleContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2860 | {nullptr, nullptr, nullptr} |
| 2861 | }; |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2862 | |
| 2863 | return |
| 2864 | + hb_zip (this+coverage, ruleSet) |
| 2865 | | hb_filter (*glyphs, hb_first) |
| 2866 | | hb_map (hb_second) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2867 | | hb_map (hb_add (this)) |
| 2868 | | hb_map ([&] (const ChainRuleSet &_) { return _.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2869 | | hb_any |
| 2870 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 2871 | } |
| 2872 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 2873 | bool may_have_non_1to1 () const |
| 2874 | { return true; } |
| 2875 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2876 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2877 | { |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 2878 | hb_set_t* cur_active_glyphs = &c->push_cur_active_glyphs (); |
| 2879 | get_coverage ().intersected_coverage_glyphs (&c->previous_parent_active_glyphs (), |
| 2880 | cur_active_glyphs); |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 2881 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2882 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2883 | {intersects_glyph, intersected_glyph}, |
| 2884 | ContextFormat::SimpleContext, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 2885 | {nullptr, nullptr, nullptr} |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2886 | }; |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2887 | |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2888 | + hb_zip (this+coverage, hb_range ((unsigned) ruleSet.len)) |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 2889 | | hb_filter ([&] (hb_codepoint_t _) { |
| 2890 | return c->previous_parent_active_glyphs ().has (_); |
| 2891 | }, hb_first) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 2892 | | hb_map ([&](const hb_pair_t<hb_codepoint_t, unsigned> _) { return hb_pair_t<unsigned, const ChainRuleSet&> (_.first, this+ruleSet[_.second]); }) |
| 2893 | | 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] | 2894 | ; |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 2895 | |
| 2896 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 2897 | } |
| 2898 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2899 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 2900 | { |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 2901 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 2902 | {intersects_glyph, intersected_glyph}, |
| 2903 | ContextFormat::SimpleContext, |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 2904 | {nullptr, nullptr, nullptr} |
| 2905 | }; |
| 2906 | |
| 2907 | + hb_zip (this+coverage, ruleSet) |
| 2908 | | hb_filter (*c->glyphs, hb_first) |
| 2909 | | hb_map (hb_second) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2910 | | hb_map (hb_add (this)) |
Garret Rieger | e31c269 | 2020-09-28 16:51:25 -0700 | [diff] [blame] | 2911 | | hb_apply ([&] (const ChainRuleSet &_) { _.closure_lookups (c, lookup_context); }) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 2912 | ; |
| 2913 | } |
| 2914 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 2915 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 2916 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2917 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2918 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 2919 | (this+coverage).collect_coverage (c->input); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2920 | |
| 2921 | struct ChainContextCollectGlyphsLookupContext lookup_context = { |
| 2922 | {collect_glyph}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 2923 | {nullptr, nullptr, nullptr} |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 2924 | }; |
| 2925 | |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2926 | + hb_iter (ruleSet) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 2927 | | hb_map (hb_add (this)) |
| 2928 | | hb_apply ([&] (const ChainRuleSet &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 2929 | ; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 2930 | } |
| 2931 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2932 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2933 | { |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 2934 | 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] | 2935 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2936 | {match_glyph}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 2937 | {nullptr, nullptr, nullptr} |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2938 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 2939 | return rule_set.would_apply (c, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 2940 | } |
| 2941 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 2942 | const Coverage &get_coverage () const { return this+coverage; } |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 2943 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2944 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 2945 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2946 | TRACE_APPLY (this); |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 2947 | unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2948 | if (likely (index == NOT_COVERED)) return_trace (false); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 2949 | |
| 2950 | const ChainRuleSet &rule_set = this+ruleSet[index]; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 2951 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 2952 | {match_glyph}, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 2953 | {nullptr, nullptr, nullptr} |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 2954 | }; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2955 | return_trace (rule_set.apply (c, lookup_context)); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2956 | } |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2957 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2958 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2959 | { |
| 2960 | TRACE_SUBSET (this); |
Garret Rieger | e583505 | 2020-09-29 11:05:08 -0700 | [diff] [blame] | 2961 | const hb_set_t &glyphset = *c->plan->glyphset_gsub (); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2962 | const hb_map_t &glyph_map = *c->plan->glyph_map; |
| 2963 | |
| 2964 | auto *out = c->serializer->start_embed (*this); |
| 2965 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 2966 | out->format = format; |
| 2967 | |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 2968 | 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] | 2969 | hb_sorted_vector_t<hb_codepoint_t> new_coverage; |
| 2970 | + hb_zip (this+coverage, ruleSet) |
| 2971 | | hb_filter (glyphset, hb_first) |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 2972 | | 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] | 2973 | | hb_map (hb_first) |
| 2974 | | hb_map (glyph_map) |
| 2975 | | hb_sink (new_coverage) |
| 2976 | ; |
| 2977 | |
Garret Rieger | 085aa65 | 2021-06-14 16:47:45 -0700 | [diff] [blame] | 2978 | out->coverage.serialize_serialize (c->serializer, new_coverage.iter ()); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 2979 | return_trace (bool (new_coverage)); |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 2980 | } |
| 2981 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 2982 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 2983 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 2984 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 2985 | return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 2986 | } |
| 2987 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 2988 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 2989 | HBUINT16 format; /* Format identifier--format = 1 */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 2990 | Offset16To<Coverage> |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 2991 | coverage; /* Offset to Coverage table--from |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 2992 | * beginning of table */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 2993 | Array16OfOffset16To<ChainRuleSet> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 2994 | ruleSet; /* Array of ChainRuleSet tables |
| 2995 | * ordered by Coverage Index */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 2996 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 2997 | DEFINE_SIZE_ARRAY (6, ruleSet); |
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 | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3000 | struct ChainContextFormat2 |
| 3001 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3002 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3003 | { |
| 3004 | if (!(this+coverage).intersects (glyphs)) |
| 3005 | return false; |
| 3006 | |
| 3007 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 3008 | const ClassDef &input_class_def = this+inputClassDef; |
| 3009 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 3010 | |
| 3011 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3012 | {intersects_class, intersected_class_glyphs}, |
| 3013 | ContextFormat::ClassBasedContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3014 | {&backtrack_class_def, |
| 3015 | &input_class_def, |
| 3016 | &lookahead_class_def} |
| 3017 | }; |
| 3018 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3019 | hb_set_t retained_coverage_glyphs; |
| 3020 | (this+coverage).intersected_coverage_glyphs (glyphs, &retained_coverage_glyphs); |
| 3021 | |
| 3022 | hb_set_t coverage_glyph_classes; |
| 3023 | input_class_def.intersected_classes (&retained_coverage_glyphs, &coverage_glyph_classes); |
| 3024 | |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 3025 | return |
Behdad Esfahbod | 26111a1 | 2020-06-28 02:59:47 -0700 | [diff] [blame] | 3026 | + hb_iter (ruleSet) |
| 3027 | | hb_map (hb_add (this)) |
| 3028 | | hb_enumerate |
| 3029 | | hb_map ([&] (const hb_pair_t<unsigned, const ChainRuleSet &> p) |
Behdad Esfahbod | 78d35f0 | 2019-05-15 18:15:05 -0700 | [diff] [blame] | 3030 | { return input_class_def.intersects_class (glyphs, p.first) && |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3031 | coverage_glyph_classes.has (p.first) && |
Behdad Esfahbod | 26111a1 | 2020-06-28 02:59:47 -0700 | [diff] [blame] | 3032 | p.second.intersects (glyphs, lookup_context); }) |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 3033 | | hb_any |
| 3034 | ; |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3035 | } |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 3036 | |
| 3037 | bool may_have_non_1to1 () const |
| 3038 | { return true; } |
| 3039 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3040 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3041 | { |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3042 | if (!(this+coverage).intersects (c->glyphs)) |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 3043 | return; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3044 | |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 3045 | hb_set_t* cur_active_glyphs = &c->push_cur_active_glyphs (); |
| 3046 | get_coverage ().intersected_coverage_glyphs (&c->previous_parent_active_glyphs (), |
| 3047 | cur_active_glyphs); |
| 3048 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 3049 | |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3050 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 3051 | const ClassDef &input_class_def = this+inputClassDef; |
| 3052 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 3053 | |
| 3054 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3055 | {intersects_class, intersected_class_glyphs}, |
| 3056 | ContextFormat::ClassBasedContext, |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3057 | {&backtrack_class_def, |
| 3058 | &input_class_def, |
| 3059 | &lookahead_class_def} |
| 3060 | }; |
| 3061 | |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 3062 | + hb_enumerate (ruleSet) |
Behdad Esfahbod | 78d35f0 | 2019-05-15 18:15:05 -0700 | [diff] [blame] | 3063 | | hb_filter ([&] (unsigned _) |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 3064 | { return input_class_def.intersects_class (&c->parent_active_glyphs (), _); }, |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 3065 | hb_first) |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3066 | | hb_apply ([&] (const hb_pair_t<unsigned, const Offset16To<ChainRuleSet>&> _) |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3067 | { |
| 3068 | const ChainRuleSet& chainrule_set = this+_.second; |
| 3069 | chainrule_set.closure (c, _.first, lookup_context); |
| 3070 | }) |
Behdad Esfahbod | f505b5d | 2019-03-29 22:55:02 -0700 | [diff] [blame] | 3071 | ; |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 3072 | |
| 3073 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3074 | } |
| 3075 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3076 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 3077 | { |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3078 | if (!(this+coverage).intersects (c->glyphs)) |
| 3079 | return; |
| 3080 | |
| 3081 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 3082 | const ClassDef &input_class_def = this+inputClassDef; |
| 3083 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 3084 | |
| 3085 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 3086 | {intersects_class, intersected_class_glyphs}, |
| 3087 | ContextFormat::ClassBasedContext, |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3088 | {&backtrack_class_def, |
| 3089 | &input_class_def, |
| 3090 | &lookahead_class_def} |
| 3091 | }; |
| 3092 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3093 | + hb_iter (ruleSet) |
| 3094 | | hb_map (hb_add (this)) |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3095 | | hb_enumerate |
Behdad Esfahbod | 6e1afac | 2021-02-09 18:48:46 -0700 | [diff] [blame] | 3096 | | hb_filter([&] (unsigned klass) |
| 3097 | { return input_class_def.intersects_class (c->glyphs, klass); }, hb_first) |
Garret Rieger | ad241f9 | 2020-09-28 15:26:13 -0700 | [diff] [blame] | 3098 | | hb_map (hb_second) |
| 3099 | | hb_apply ([&] (const ChainRuleSet &_) |
| 3100 | { _.closure_lookups (c, lookup_context); }) |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3101 | ; |
| 3102 | } |
| 3103 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 3104 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 3105 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3106 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 3107 | { |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 3108 | (this+coverage).collect_coverage (c->input); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3109 | |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 3110 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 3111 | const ClassDef &input_class_def = this+inputClassDef; |
| 3112 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 3113 | |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3114 | struct ChainContextCollectGlyphsLookupContext lookup_context = { |
| 3115 | {collect_class}, |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 3116 | {&backtrack_class_def, |
| 3117 | &input_class_def, |
| 3118 | &lookahead_class_def} |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3119 | }; |
| 3120 | |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3121 | + hb_iter (ruleSet) |
Behdad Esfahbod | 2376867 | 2019-05-15 21:57:26 -0700 | [diff] [blame] | 3122 | | hb_map (hb_add (this)) |
| 3123 | | hb_apply ([&] (const ChainRuleSet &_) { _.collect_glyphs (c, lookup_context); }) |
Behdad Esfahbod | 05f2130 | 2019-03-29 22:40:13 -0700 | [diff] [blame] | 3124 | ; |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 3125 | } |
| 3126 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3127 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3128 | { |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 3129 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3130 | const ClassDef &input_class_def = this+inputClassDef; |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 3131 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3132 | |
Behdad Esfahbod | 2dc1141 | 2012-11-24 19:16:34 -0500 | [diff] [blame] | 3133 | unsigned int index = input_class_def.get_class (c->glyphs[0]); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3134 | const ChainRuleSet &rule_set = this+ruleSet[index]; |
| 3135 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 3136 | {match_class}, |
Behdad Esfahbod | 11fba79 | 2013-01-02 23:36:37 -0600 | [diff] [blame] | 3137 | {&backtrack_class_def, |
| 3138 | &input_class_def, |
| 3139 | &lookahead_class_def} |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3140 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 3141 | return rule_set.would_apply (c, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3142 | } |
| 3143 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3144 | const Coverage &get_coverage () const { return this+coverage; } |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 3145 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3146 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3147 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3148 | TRACE_APPLY (this); |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 3149 | unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3150 | if (likely (index == NOT_COVERED)) return_trace (false); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3151 | |
| 3152 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 3153 | const ClassDef &input_class_def = this+inputClassDef; |
| 3154 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 3155 | |
Behdad Esfahbod | 2dc1141 | 2012-11-24 19:16:34 -0500 | [diff] [blame] | 3156 | index = input_class_def.get_class (c->buffer->cur().codepoint); |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3157 | const ChainRuleSet &rule_set = this+ruleSet[index]; |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3158 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 3159 | {match_class}, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 3160 | {&backtrack_class_def, |
| 3161 | &input_class_def, |
| 3162 | &lookahead_class_def} |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3163 | }; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3164 | return_trace (rule_set.apply (c, lookup_context)); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3165 | } |
| 3166 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3167 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 3168 | { |
| 3169 | TRACE_SUBSET (this); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3170 | auto *out = c->serializer->start_embed (*this); |
| 3171 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 3172 | out->format = format; |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3173 | out->coverage.serialize_subset (c, coverage, this); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3174 | |
| 3175 | hb_map_t backtrack_klass_map; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3176 | hb_map_t input_klass_map; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3177 | hb_map_t lookahead_klass_map; |
Behdad Esfahbod | dfa9d7a | 2021-02-11 11:08:52 -0700 | [diff] [blame] | 3178 | |
| 3179 | out->backtrackClassDef.serialize_subset (c, backtrackClassDef, this, &backtrack_klass_map); |
| 3180 | // TODO: subset inputClassDef based on glyphs survived in Coverage subsetting |
| 3181 | out->inputClassDef.serialize_subset (c, inputClassDef, this, &input_klass_map); |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3182 | out->lookaheadClassDef.serialize_subset (c, lookaheadClassDef, this, &lookahead_klass_map); |
Behdad Esfahbod | dfa9d7a | 2021-02-11 11:08:52 -0700 | [diff] [blame] | 3183 | |
| 3184 | if (unlikely (!c->serializer->propagate_error (backtrack_klass_map, |
| 3185 | input_klass_map, |
| 3186 | lookahead_klass_map))) |
Garret Rieger | 06dbb6a | 2020-07-31 15:56:14 -0700 | [diff] [blame] | 3187 | return_trace (false); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3188 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3189 | const hb_set_t* glyphset = c->plan->glyphset_gsub (); |
| 3190 | hb_set_t retained_coverage_glyphs; |
| 3191 | (this+coverage).intersected_coverage_glyphs (glyphset, &retained_coverage_glyphs); |
| 3192 | |
| 3193 | hb_set_t coverage_glyph_classes; |
| 3194 | (this+inputClassDef).intersected_classes (&retained_coverage_glyphs, &coverage_glyph_classes); |
| 3195 | |
Garret Rieger | dc37555 | 2020-09-25 13:08:46 -0700 | [diff] [blame] | 3196 | int non_zero_index = -1, index = 0; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3197 | bool ret = true; |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 3198 | 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] | 3199 | auto last_non_zero = c->serializer->snapshot (); |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3200 | for (const auto& _ : + hb_enumerate (ruleSet) |
| 3201 | | hb_filter (input_klass_map, hb_first)) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3202 | { |
| 3203 | auto *o = out->ruleSet.serialize_append (c->serializer); |
| 3204 | if (unlikely (!o)) |
| 3205 | { |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 3206 | ret = false; |
| 3207 | break; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3208 | } |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3209 | if (coverage_glyph_classes.has (_.first) && |
| 3210 | o->serialize_subset (c, _.second, this, |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 3211 | lookup_map, |
| 3212 | &backtrack_klass_map, |
| 3213 | &input_klass_map, |
| 3214 | &lookahead_klass_map)) |
Garret Rieger | dc37555 | 2020-09-25 13:08:46 -0700 | [diff] [blame] | 3215 | { |
| 3216 | last_non_zero = c->serializer->snapshot (); |
Ebrahim Byagowi | 5a7cc7f | 2020-07-29 08:33:32 +0430 | [diff] [blame] | 3217 | non_zero_index = index; |
Garret Rieger | dc37555 | 2020-09-25 13:08:46 -0700 | [diff] [blame] | 3218 | } |
Qunxin Liu | 593e58c | 2020-05-20 18:00:25 -0700 | [diff] [blame] | 3219 | |
| 3220 | index++; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3221 | } |
| 3222 | |
Qunxin Liu | 540f19b | 2021-10-29 17:11:53 -0700 | [diff] [blame] | 3223 | if (!ret || non_zero_index == -1) return_trace (false); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3224 | |
Garret Rieger | dc37555 | 2020-09-25 13:08:46 -0700 | [diff] [blame] | 3225 | // prune empty trailing ruleSets |
| 3226 | if (index > non_zero_index) { |
| 3227 | c->serializer->revert (last_non_zero); |
| 3228 | out->ruleSet.len = non_zero_index + 1; |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | return_trace (bool (out->ruleSet)); |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 3232 | } |
| 3233 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3234 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 3235 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3236 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3237 | return_trace (coverage.sanitize (c, this) && |
| 3238 | backtrackClassDef.sanitize (c, this) && |
| 3239 | inputClassDef.sanitize (c, this) && |
| 3240 | lookaheadClassDef.sanitize (c, this) && |
| 3241 | ruleSet.sanitize (c, this)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3242 | } |
| 3243 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3244 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 3245 | HBUINT16 format; /* Format identifier--format = 2 */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3246 | Offset16To<Coverage> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3247 | coverage; /* Offset to Coverage table--from |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3248 | * beginning of table */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3249 | Offset16To<ClassDef> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3250 | backtrackClassDef; /* Offset to glyph ClassDef table |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3251 | * containing backtrack sequence |
| 3252 | * data--from beginning of table */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3253 | Offset16To<ClassDef> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3254 | inputClassDef; /* Offset to glyph ClassDef |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3255 | * table containing input sequence |
| 3256 | * data--from beginning of table */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3257 | Offset16To<ClassDef> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3258 | lookaheadClassDef; /* Offset to glyph ClassDef table |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3259 | * containing lookahead sequence |
| 3260 | * data--from beginning of table */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3261 | Array16OfOffset16To<ChainRuleSet> |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 3262 | ruleSet; /* Array of ChainRuleSet tables |
| 3263 | * ordered by class */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 3264 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 3265 | DEFINE_SIZE_ARRAY (12, ruleSet); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3266 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3267 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3268 | struct ChainContextFormat3 |
| 3269 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3270 | bool intersects (const hb_set_t *glyphs) const |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3271 | { |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3272 | const Array16OfOffset16To<Coverage> &input = StructAfter<Array16OfOffset16To<Coverage>> (backtrack); |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3273 | |
| 3274 | if (!(this+input[0]).intersects (glyphs)) |
| 3275 | return false; |
| 3276 | |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3277 | const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (input); |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3278 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3279 | {intersects_coverage, intersected_coverage_glyphs}, |
| 3280 | ContextFormat::CoverageBasedContext, |
Behdad Esfahbod | 7c9cfa2 | 2018-09-02 19:47:50 -0700 | [diff] [blame] | 3281 | {this, this, this} |
| 3282 | }; |
| 3283 | return chain_context_intersects (glyphs, |
| 3284 | backtrack.len, (const HBUINT16 *) backtrack.arrayZ, |
| 3285 | input.len, (const HBUINT16 *) input.arrayZ + 1, |
| 3286 | lookahead.len, (const HBUINT16 *) lookahead.arrayZ, |
| 3287 | lookup_context); |
| 3288 | } |
| 3289 | |
Qunxin Liu | b4fc593 | 2020-12-09 10:44:18 -0800 | [diff] [blame] | 3290 | bool may_have_non_1to1 () const |
| 3291 | { return true; } |
| 3292 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3293 | void closure (hb_closure_context_t *c) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3294 | { |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3295 | const Array16OfOffset16To<Coverage> &input = StructAfter<Array16OfOffset16To<Coverage>> (backtrack); |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 3296 | |
| 3297 | if (!(this+input[0]).intersects (c->glyphs)) |
| 3298 | return; |
| 3299 | |
Garret Rieger | 4e2f409 | 2022-01-31 12:20:32 -0800 | [diff] [blame] | 3300 | hb_set_t* cur_active_glyphs = &c->push_cur_active_glyphs (); |
| 3301 | get_coverage ().intersected_coverage_glyphs (&c->previous_parent_active_glyphs (), |
| 3302 | cur_active_glyphs); |
| 3303 | |
Qunxin Liu | 0e1c0fa | 2021-01-12 10:17:14 -0800 | [diff] [blame] | 3304 | |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3305 | const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (input); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 3306 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 3307 | struct ChainContextClosureLookupContext lookup_context = { |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3308 | {intersects_coverage, intersected_coverage_glyphs}, |
| 3309 | ContextFormat::CoverageBasedContext, |
Behdad Esfahbod | 5caece6 | 2012-04-23 23:03:12 -0400 | [diff] [blame] | 3310 | {this, this, this} |
| 3311 | }; |
| 3312 | chain_context_closure_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 3313 | backtrack.len, (const HBUINT16 *) backtrack.arrayZ, |
| 3314 | input.len, (const HBUINT16 *) input.arrayZ + 1, |
| 3315 | lookahead.len, (const HBUINT16 *) lookahead.arrayZ, |
| 3316 | lookup.len, lookup.arrayZ, |
Qunxin Liu | b8a58a0 | 2021-01-10 15:50:04 -0800 | [diff] [blame] | 3317 | 0, lookup_context); |
Garret Rieger | bc89965 | 2022-01-28 13:54:10 -0800 | [diff] [blame] | 3318 | |
| 3319 | c->pop_cur_done_glyphs (); |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3320 | } |
| 3321 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3322 | void closure_lookups (hb_closure_lookups_context_t *c) const |
| 3323 | { |
Garret Rieger | a5c0ec7 | 2020-09-25 14:57:20 -0700 | [diff] [blame] | 3324 | if (!intersects (c->glyphs)) |
| 3325 | return; |
| 3326 | |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3327 | const Array16OfOffset16To<Coverage> &input = StructAfter<Array16OfOffset16To<Coverage>> (backtrack); |
| 3328 | const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (input); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 3329 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3330 | recurse_lookups (c, lookup.len, lookup.arrayZ); |
| 3331 | } |
| 3332 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 3333 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const {} |
| 3334 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3335 | void collect_glyphs (hb_collect_glyphs_context_t *c) const |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 3336 | { |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3337 | const Array16OfOffset16To<Coverage> &input = StructAfter<Array16OfOffset16To<Coverage>> (backtrack); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3338 | |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 3339 | (this+input[0]).collect_coverage (c->input); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3340 | |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3341 | const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (input); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 3342 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3343 | struct ChainContextCollectGlyphsLookupContext lookup_context = { |
| 3344 | {collect_coverage}, |
| 3345 | {this, this, this} |
| 3346 | }; |
| 3347 | chain_context_collect_glyphs_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 3348 | backtrack.len, (const HBUINT16 *) backtrack.arrayZ, |
| 3349 | input.len, (const HBUINT16 *) input.arrayZ + 1, |
| 3350 | lookahead.len, (const HBUINT16 *) lookahead.arrayZ, |
| 3351 | lookup.len, lookup.arrayZ, |
Behdad Esfahbod | f1b1278 | 2012-11-24 01:55:34 -0500 | [diff] [blame] | 3352 | lookup_context); |
Behdad Esfahbod | 26514d5 | 2012-11-23 18:13:48 -0500 | [diff] [blame] | 3353 | } |
| 3354 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3355 | bool would_apply (hb_would_apply_context_t *c) const |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3356 | { |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3357 | const Array16OfOffset16To<Coverage> &input = StructAfter<Array16OfOffset16To<Coverage>> (backtrack); |
| 3358 | const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (input); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 3359 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3360 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 3361 | {match_coverage}, |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3362 | {this, this, this} |
| 3363 | }; |
Behdad Esfahbod | 90b60bd | 2019-03-29 22:12:42 -0700 | [diff] [blame] | 3364 | return chain_context_would_apply_lookup (c, |
| 3365 | backtrack.len, (const HBUINT16 *) backtrack.arrayZ, |
| 3366 | input.len, (const HBUINT16 *) input.arrayZ + 1, |
| 3367 | lookahead.len, (const HBUINT16 *) lookahead.arrayZ, |
| 3368 | lookup.len, lookup.arrayZ, lookup_context); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3369 | } |
| 3370 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3371 | const Coverage &get_coverage () const |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 3372 | { |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3373 | const Array16OfOffset16To<Coverage> &input = StructAfter<Array16OfOffset16To<Coverage>> (backtrack); |
Behdad Esfahbod | 44fc237 | 2012-11-21 23:33:13 -0500 | [diff] [blame] | 3374 | return this+input[0]; |
| 3375 | } |
| 3376 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3377 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3378 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3379 | TRACE_APPLY (this); |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3380 | const Array16OfOffset16To<Coverage> &input = StructAfter<Array16OfOffset16To<Coverage>> (backtrack); |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 3381 | |
Behdad Esfahbod | b67881b | 2012-11-24 19:13:55 -0500 | [diff] [blame] | 3382 | unsigned int index = (this+input[0]).get_coverage (c->buffer->cur().codepoint); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3383 | if (likely (index == NOT_COVERED)) return_trace (false); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3384 | |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3385 | const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (input); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 3386 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Behdad Esfahbod | 31081f7 | 2012-04-23 16:54:58 -0400 | [diff] [blame] | 3387 | struct ChainContextApplyLookupContext lookup_context = { |
Behdad Esfahbod | ec35a72 | 2012-11-22 16:05:59 -0500 | [diff] [blame] | 3388 | {match_coverage}, |
Behdad Esfahbod | 40cbefe | 2010-05-10 17:47:22 -0400 | [diff] [blame] | 3389 | {this, this, this} |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 3390 | }; |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3391 | return_trace (chain_context_apply_lookup (c, |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 3392 | backtrack.len, (const HBUINT16 *) backtrack.arrayZ, |
| 3393 | input.len, (const HBUINT16 *) input.arrayZ + 1, |
| 3394 | lookahead.len, (const HBUINT16 *) lookahead.arrayZ, |
| 3395 | lookup.len, lookup.arrayZ, lookup_context)); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3396 | } |
| 3397 | |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3398 | template<typename Iterator, |
| 3399 | hb_requires (hb_is_iterator (Iterator))> |
Ebrahim Byagowi | 07acd1a | 2020-03-08 23:39:24 +0330 | [diff] [blame] | 3400 | 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] | 3401 | { |
| 3402 | TRACE_SERIALIZE (this); |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3403 | auto *out = c->serializer->start_embed<Array16OfOffset16To<Coverage>> (); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3404 | |
Garret Rieger | 90eb1a4 | 2020-09-25 12:36:32 -0700 | [diff] [blame] | 3405 | if (unlikely (!c->serializer->allocate_size<HBUINT16> (HBUINT16::static_size))) |
Garret Rieger | 940e1c6 | 2020-09-28 17:22:01 -0700 | [diff] [blame] | 3406 | return_trace (false); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3407 | |
Garret Rieger | 940e1c6 | 2020-09-28 17:22:01 -0700 | [diff] [blame] | 3408 | for (auto& offset : it) { |
| 3409 | auto *o = out->serialize_append (c->serializer); |
| 3410 | if (unlikely (!o) || !o->serialize_subset (c, offset, base)) |
| 3411 | return_trace (false); |
| 3412 | } |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3413 | |
Garret Rieger | 940e1c6 | 2020-09-28 17:22:01 -0700 | [diff] [blame] | 3414 | return_trace (true); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3415 | } |
| 3416 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3417 | bool subset (hb_subset_context_t *c) const |
Behdad Esfahbod | 339d360 | 2018-09-03 17:33:34 -0700 | [diff] [blame] | 3418 | { |
| 3419 | TRACE_SUBSET (this); |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3420 | |
| 3421 | auto *out = c->serializer->start_embed (this); |
| 3422 | if (unlikely (!out)) return_trace (false); |
| 3423 | if (unlikely (!c->serializer->embed (this->format))) return_trace (false); |
| 3424 | |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3425 | if (!serialize_coverage_offsets (c, backtrack.iter (), this)) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3426 | return_trace (false); |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 3427 | |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3428 | const Array16OfOffset16To<Coverage> &input = StructAfter<Array16OfOffset16To<Coverage>> (backtrack); |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3429 | if (!serialize_coverage_offsets (c, input.iter (), this)) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3430 | return_trace (false); |
| 3431 | |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3432 | const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (input); |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3433 | if (!serialize_coverage_offsets (c, lookahead.iter (), this)) |
Qunxin Liu | b66094a | 2019-09-30 16:19:18 -0700 | [diff] [blame] | 3434 | return_trace (false); |
Ebrahim Byagowi | aca6390 | 2019-10-22 00:06:46 +0330 | [diff] [blame] | 3435 | |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 3436 | const Array16Of<LookupRecord> &lookupRecord = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Qunxin Liu | 37379f8 | 2021-09-02 11:54:37 -0700 | [diff] [blame] | 3437 | 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] | 3438 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 3439 | HBUINT16 *lookupCount = c->serializer->copy<HBUINT16> (lookupRecord.len); |
| 3440 | if (!lookupCount) return_trace (false); |
Qunxin Liu | 37379f8 | 2021-09-02 11:54:37 -0700 | [diff] [blame] | 3441 | |
Qunxin Liu | 36ed56b | 2021-09-23 10:51:21 -0700 | [diff] [blame] | 3442 | unsigned count = serialize_lookuprecord_array (c->serializer, lookupRecord.as_array (), lookup_map); |
| 3443 | 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] | 3444 | } |
| 3445 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3446 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 3447 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3448 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3449 | if (!backtrack.sanitize (c, this)) return_trace (false); |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3450 | const Array16OfOffset16To<Coverage> &input = StructAfter<Array16OfOffset16To<Coverage>> (backtrack); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3451 | if (!input.sanitize (c, this)) return_trace (false); |
| 3452 | if (!input.len) return_trace (false); /* To be consistent with Context. */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3453 | const Array16OfOffset16To<Coverage> &lookahead = StructAfter<Array16OfOffset16To<Coverage>> (input); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3454 | if (!lookahead.sanitize (c, this)) return_trace (false); |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 3455 | const Array16Of<LookupRecord> &lookup = StructAfter<Array16Of<LookupRecord>> (lookahead); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3456 | return_trace (lookup.sanitize (c)); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3457 | } |
| 3458 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3459 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 3460 | HBUINT16 format; /* Format identifier--format = 3 */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3461 | Array16OfOffset16To<Coverage> |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 3462 | backtrack; /* Array of coverage tables |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3463 | * in backtracking sequence, in glyph |
| 3464 | * sequence order */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3465 | Array16OfOffset16To<Coverage> |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 3466 | inputX ; /* Array of coverage |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3467 | * tables in input sequence, in glyph |
| 3468 | * sequence order */ |
Behdad Esfahbod | 6c4e049 | 2021-03-31 15:31:32 -0600 | [diff] [blame] | 3469 | Array16OfOffset16To<Coverage> |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 3470 | lookaheadX; /* Array of coverage tables |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3471 | * in lookahead sequence, in glyph |
| 3472 | * sequence order */ |
Behdad Esfahbod | 5639e25 | 2021-03-31 16:04:43 -0600 | [diff] [blame] | 3473 | Array16Of<LookupRecord> |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 3474 | lookupX; /* Array of LookupRecords--in |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 3475 | * design order) */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 3476 | public: |
Behdad Esfahbod | bea34c7 | 2010-05-10 17:28:16 -0400 | [diff] [blame] | 3477 | DEFINE_SIZE_MIN (10); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3478 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3479 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3480 | struct ChainContext |
| 3481 | { |
Behdad Esfahbod | 36bb24f | 2019-05-05 10:14:17 -0700 | [diff] [blame] | 3482 | template <typename context_t, typename ...Ts> |
Behdad Esfahbod | 83e3eab | 2019-05-07 20:58:43 -0700 | [diff] [blame] | 3483 | typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const |
Behdad Esfahbod | f94b0aa | 2012-04-23 13:04:38 -0400 | [diff] [blame] | 3484 | { |
Behdad Esfahbod | 00f6a8e | 2014-12-12 20:36:49 -0800 | [diff] [blame] | 3485 | TRACE_DISPATCH (this, u.format); |
Behdad Esfahbod | f396fbb | 2015-10-09 12:25:55 -0400 | [diff] [blame] | 3486 | 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] | 3487 | switch (u.format) { |
Behdad Esfahbod | 6d555ce | 2021-11-02 00:18:22 -0600 | [diff] [blame] | 3488 | case 1: return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...)); |
| 3489 | case 2: return_trace (c->dispatch (u.format2, std::forward<Ts> (ds)...)); |
| 3490 | case 3: return_trace (c->dispatch (u.format3, std::forward<Ts> (ds)...)); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3491 | default:return_trace (c->default_return_value ()); |
Behdad Esfahbod | e72b360 | 2012-07-19 14:35:23 -0400 | [diff] [blame] | 3492 | } |
| 3493 | } |
| 3494 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3495 | protected: |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3496 | union { |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 3497 | HBUINT16 format; /* Format identifier */ |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 3498 | ChainContextFormat1 format1; |
| 3499 | ChainContextFormat2 format2; |
| 3500 | ChainContextFormat3 format3; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3501 | } u; |
| 3502 | }; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 3503 | |
| 3504 | |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3505 | template <typename T> |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3506 | struct ExtensionFormat1 |
| 3507 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3508 | unsigned int get_type () const { return extensionLookupType; } |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3509 | |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3510 | template <typename X> |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3511 | const X& get_subtable () const |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3512 | { return this + reinterpret_cast<const Offset32To<typename T::SubTable> &> (extensionOffset); } |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3513 | |
Behdad Esfahbod | 36bb24f | 2019-05-05 10:14:17 -0700 | [diff] [blame] | 3514 | template <typename context_t, typename ...Ts> |
Behdad Esfahbod | 83e3eab | 2019-05-07 20:58:43 -0700 | [diff] [blame] | 3515 | typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3516 | { |
| 3517 | TRACE_DISPATCH (this, format); |
Behdad Esfahbod | f396fbb | 2015-10-09 12:25:55 -0400 | [diff] [blame] | 3518 | 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] | 3519 | 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] | 3520 | } |
| 3521 | |
Qunxin Liu | 8200e48 | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 3522 | void collect_variation_indices (hb_collect_variation_indices_context_t *c) const |
| 3523 | { dispatch (c); } |
| 3524 | |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3525 | /* This is called from may_dispatch() above with hb_sanitize_context_t. */ |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3526 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 3527 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3528 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 949f6af | 2018-01-15 20:44:10 -0500 | [diff] [blame] | 3529 | return_trace (c->check_struct (this) && |
Behdad Esfahbod | 9c3747c | 2018-09-03 16:53:03 -0700 | [diff] [blame] | 3530 | extensionLookupType != T::SubTable::Extension); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3531 | } |
| 3532 | |
Garret Rieger | 08a4997 | 2020-10-06 13:02:12 -0700 | [diff] [blame] | 3533 | bool subset (hb_subset_context_t *c) const |
| 3534 | { |
| 3535 | TRACE_SUBSET (this); |
| 3536 | |
| 3537 | auto *out = c->serializer->start_embed (this); |
| 3538 | if (unlikely (!out || !c->serializer->extend_min (out))) return_trace (false); |
| 3539 | |
| 3540 | out->format = format; |
| 3541 | out->extensionLookupType = extensionLookupType; |
| 3542 | |
| 3543 | const auto& src_offset = |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3544 | reinterpret_cast<const Offset32To<typename T::SubTable> &> (extensionOffset); |
Garret Rieger | 08a4997 | 2020-10-06 13:02:12 -0700 | [diff] [blame] | 3545 | auto& dest_offset = |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3546 | reinterpret_cast<Offset32To<typename T::SubTable> &> (out->extensionOffset); |
Garret Rieger | 08a4997 | 2020-10-06 13:02:12 -0700 | [diff] [blame] | 3547 | |
| 3548 | return_trace (dest_offset.serialize_subset (c, src_offset, this, get_type ())); |
| 3549 | } |
| 3550 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3551 | protected: |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 3552 | HBUINT16 format; /* Format identifier. Set to 1. */ |
| 3553 | HBUINT16 extensionLookupType; /* Lookup type of subtable referenced |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3554 | * by ExtensionOffset (i.e. the |
| 3555 | * extension subtable). */ |
Behdad Esfahbod | cd9bc73 | 2019-05-10 13:17:41 -0700 | [diff] [blame] | 3556 | Offset32 extensionOffset; /* Offset to the extension subtable, |
Behdad Esfahbod | 81f2af4 | 2010-04-22 00:58:49 -0400 | [diff] [blame] | 3557 | * of lookup type subtable. */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 3558 | public: |
| 3559 | DEFINE_SIZE_STATIC (8); |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3560 | }; |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3561 | |
Behdad Esfahbod | 653eeb2 | 2012-11-23 16:57:36 -0500 | [diff] [blame] | 3562 | template <typename T> |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3563 | struct Extension |
| 3564 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3565 | unsigned int get_type () const |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3566 | { |
| 3567 | switch (u.format) { |
Behdad Esfahbod | dacebca | 2010-05-10 19:45:41 -0400 | [diff] [blame] | 3568 | case 1: return u.format1.get_type (); |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3569 | default:return 0; |
| 3570 | } |
| 3571 | } |
Behdad Esfahbod | 7dddd4e | 2012-11-23 17:04:55 -0500 | [diff] [blame] | 3572 | template <typename X> |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3573 | const X& get_subtable () const |
Behdad Esfahbod | 7dddd4e | 2012-11-23 17:04:55 -0500 | [diff] [blame] | 3574 | { |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3575 | switch (u.format) { |
Behdad Esfahbod | 9c3747c | 2018-09-03 16:53:03 -0700 | [diff] [blame] | 3576 | case 1: return u.format1.template get_subtable<typename T::SubTable> (); |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 3577 | default:return Null (typename T::SubTable); |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3578 | } |
Behdad Esfahbod | 7dddd4e | 2012-11-23 17:04:55 -0500 | [diff] [blame] | 3579 | } |
| 3580 | |
Garret Rieger | 08a4997 | 2020-10-06 13:02:12 -0700 | [diff] [blame] | 3581 | // Specialization of dispatch for subset. dispatch() normally just |
| 3582 | // dispatches to the sub table this points too, but for subset |
| 3583 | // we need to run subset on this subtable too. |
| 3584 | template <typename ...Ts> |
| 3585 | typename hb_subset_context_t::return_t dispatch (hb_subset_context_t *c, Ts&&... ds) const |
| 3586 | { |
| 3587 | switch (u.format) { |
| 3588 | case 1: return u.format1.subset (c); |
| 3589 | default: return c->default_return_value (); |
| 3590 | } |
| 3591 | } |
| 3592 | |
Behdad Esfahbod | 36bb24f | 2019-05-05 10:14:17 -0700 | [diff] [blame] | 3593 | template <typename context_t, typename ...Ts> |
Behdad Esfahbod | 83e3eab | 2019-05-07 20:58:43 -0700 | [diff] [blame] | 3594 | typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const |
Behdad Esfahbod | 653eeb2 | 2012-11-23 16:57:36 -0500 | [diff] [blame] | 3595 | { |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3596 | TRACE_DISPATCH (this, u.format); |
Behdad Esfahbod | f396fbb | 2015-10-09 12:25:55 -0400 | [diff] [blame] | 3597 | 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] | 3598 | switch (u.format) { |
Behdad Esfahbod | 6d555ce | 2021-11-02 00:18:22 -0600 | [diff] [blame] | 3599 | case 1: return_trace (u.format1.dispatch (c, std::forward<Ts> (ds)...)); |
Behdad Esfahbod | b471590 | 2015-09-29 14:57:02 +0100 | [diff] [blame] | 3600 | default:return_trace (c->default_return_value ()); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 3601 | } |
| 3602 | } |
| 3603 | |
Behdad Esfahbod | ec8d249 | 2012-07-24 15:40:37 -0400 | [diff] [blame] | 3604 | protected: |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3605 | union { |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 3606 | HBUINT16 format; /* Format identifier */ |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 3607 | ExtensionFormat1<T> format1; |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3608 | } u; |
| 3609 | }; |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 3610 | |
| 3611 | |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 3612 | /* |
| 3613 | * GSUB/GPOS Common |
| 3614 | */ |
| 3615 | |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3616 | struct hb_ot_layout_lookup_accelerator_t |
| 3617 | { |
| 3618 | template <typename TLookup> |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3619 | void init (const TLookup &lookup) |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3620 | { |
| 3621 | digest.init (); |
Behdad Esfahbod | 5cf53c0 | 2020-04-23 10:55:41 -0700 | [diff] [blame] | 3622 | lookup.collect_coverage (&digest); |
Behdad Esfahbod | 78c09bf | 2018-10-10 11:50:46 -0400 | [diff] [blame] | 3623 | |
| 3624 | subtables.init (); |
| 3625 | OT::hb_get_subtables_context_t c_get_subtables (subtables); |
| 3626 | lookup.dispatch (&c_get_subtables); |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3627 | } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3628 | void fini () { subtables.fini (); } |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3629 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3630 | bool may_have (hb_codepoint_t g) const |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3631 | { return digest.may_have (g); } |
| 3632 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3633 | bool apply (hb_ot_apply_context_t *c) const |
Behdad Esfahbod | e78549e | 2018-10-10 11:54:48 -0400 | [diff] [blame] | 3634 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 3635 | for (unsigned int i = 0; i < subtables.length; i++) |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 3636 | if (subtables[i].apply (c)) |
| 3637 | return true; |
| 3638 | return false; |
Behdad Esfahbod | e78549e | 2018-10-10 11:54:48 -0400 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | private: |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3642 | hb_set_digest_t digest; |
Behdad Esfahbod | 78c09bf | 2018-10-10 11:50:46 -0400 | [diff] [blame] | 3643 | hb_get_subtables_context_t::array_t subtables; |
Behdad Esfahbod | 97e5913 | 2018-10-10 11:41:05 -0400 | [diff] [blame] | 3644 | }; |
| 3645 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 3646 | struct GSUBGPOS |
| 3647 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3648 | bool has_data () const { return version.to_int (); } |
| 3649 | unsigned int get_script_count () const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 3650 | { return (this+scriptList).len; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3651 | const Tag& get_script_tag (unsigned int i) const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 3652 | { return (this+scriptList).get_tag (i); } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3653 | unsigned int get_script_tags (unsigned int start_offset, |
| 3654 | unsigned int *script_count /* IN/OUT */, |
| 3655 | hb_tag_t *script_tags /* OUT */) const |
Behdad Esfahbod | e21899b | 2009-11-04 16:36:14 -0500 | [diff] [blame] | 3656 | { return (this+scriptList).get_tags (start_offset, script_count, script_tags); } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3657 | const Script& get_script (unsigned int i) const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 3658 | { return (this+scriptList)[i]; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3659 | bool find_script_index (hb_tag_t tag, unsigned int *index) const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 3660 | { return (this+scriptList).find_index (tag, index); } |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 3661 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3662 | unsigned int get_feature_count () const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 3663 | { return (this+featureList).len; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3664 | hb_tag_t get_feature_tag (unsigned int i) const |
Jonathan Kew | da13293 | 2014-04-27 14:05:24 +0100 | [diff] [blame] | 3665 | { return i == Index::NOT_FOUND_INDEX ? HB_TAG_NONE : (this+featureList).get_tag (i); } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3666 | unsigned int get_feature_tags (unsigned int start_offset, |
| 3667 | unsigned int *feature_count /* IN/OUT */, |
| 3668 | hb_tag_t *feature_tags /* OUT */) const |
Behdad Esfahbod | e21899b | 2009-11-04 16:36:14 -0500 | [diff] [blame] | 3669 | { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3670 | const Feature& get_feature (unsigned int i) const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 3671 | { return (this+featureList)[i]; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3672 | bool find_feature_index (hb_tag_t tag, unsigned int *index) const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 3673 | { return (this+featureList).find_index (tag, index); } |
| 3674 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3675 | unsigned int get_lookup_count () const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 3676 | { return (this+lookupList).len; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3677 | const Lookup& get_lookup (unsigned int i) const |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 3678 | { return (this+lookupList)[i]; } |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 3679 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3680 | bool find_variations_index (const int *coords, unsigned int num_coords, |
| 3681 | unsigned int *index) const |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 3682 | { |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3683 | #ifdef HB_NO_VAR |
Ebrahim Byagowi | 2e1bf61 | 2020-03-26 22:59:26 +0430 | [diff] [blame] | 3684 | *index = FeatureVariations::NOT_FOUND_INDEX; |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 3685 | return false; |
| 3686 | #endif |
Ebrahim Byagowi | 071e2e3 | 2020-03-26 12:01:53 +0430 | [diff] [blame] | 3687 | return (version.to_int () >= 0x00010001u ? this+featureVars : Null (FeatureVariations)) |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 3688 | .find_index (coords, num_coords, index); |
| 3689 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3690 | const Feature& get_feature_variation (unsigned int feature_index, |
Behdad Esfahbod | 3d9a6e6 | 2019-01-22 12:02:06 +0100 | [diff] [blame] | 3691 | unsigned int variations_index) const |
Behdad Esfahbod | ec87ba9 | 2016-09-10 03:53:11 -0700 | [diff] [blame] | 3692 | { |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 3693 | #ifndef HB_NO_VAR |
Behdad Esfahbod | ec87ba9 | 2016-09-10 03:53:11 -0700 | [diff] [blame] | 3694 | if (FeatureVariations::NOT_FOUND_INDEX != variations_index && |
| 3695 | version.to_int () >= 0x00010001u) |
| 3696 | { |
Behdad Esfahbod | 4ebbeb7 | 2016-09-10 04:52:34 -0700 | [diff] [blame] | 3697 | const Feature *feature = (this+featureVars).find_substitute (variations_index, |
| 3698 | feature_index); |
Behdad Esfahbod | ec87ba9 | 2016-09-10 03:53:11 -0700 | [diff] [blame] | 3699 | if (feature) |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 3700 | return *feature; |
Behdad Esfahbod | ec87ba9 | 2016-09-10 03:53:11 -0700 | [diff] [blame] | 3701 | } |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 3702 | #endif |
Behdad Esfahbod | ec87ba9 | 2016-09-10 03:53:11 -0700 | [diff] [blame] | 3703 | return get_feature (feature_index); |
| 3704 | } |
Behdad Esfahbod | 59055b5 | 2016-09-10 01:24:28 -0700 | [diff] [blame] | 3705 | |
Qunxin Liu | 0b39c48 | 2019-10-22 16:00:43 -0700 | [diff] [blame] | 3706 | void feature_variation_collect_lookups (const hb_set_t *feature_indexes, |
| 3707 | hb_set_t *lookup_indexes /* OUT */) const |
| 3708 | { |
| 3709 | #ifndef HB_NO_VAR |
| 3710 | if (version.to_int () >= 0x00010001u) |
| 3711 | (this+featureVars).collect_lookups (feature_indexes, lookup_indexes); |
| 3712 | #endif |
| 3713 | } |
| 3714 | |
Behdad Esfahbod | 9c3747c | 2018-09-03 16:53:03 -0700 | [diff] [blame] | 3715 | template <typename TLookup> |
Qunxin Liu | 973c47f | 2020-06-11 11:27:57 -0700 | [diff] [blame] | 3716 | void closure_lookups (hb_face_t *face, |
| 3717 | const hb_set_t *glyphs, |
Ebrahim Byagowi | 5a7cc7f | 2020-07-29 08:33:32 +0430 | [diff] [blame] | 3718 | hb_set_t *lookup_indexes /* IN/OUT */) const |
Qunxin Liu | 973c47f | 2020-06-11 11:27:57 -0700 | [diff] [blame] | 3719 | { |
| 3720 | hb_set_t visited_lookups, inactive_lookups; |
| 3721 | OT::hb_closure_lookups_context_t c (face, glyphs, &visited_lookups, &inactive_lookups); |
| 3722 | |
| 3723 | for (unsigned lookup_index : + hb_iter (lookup_indexes)) |
| 3724 | reinterpret_cast<const TLookup &> (get_lookup (lookup_index)).closure_lookups (&c, lookup_index); |
| 3725 | |
| 3726 | hb_set_union (lookup_indexes, &visited_lookups); |
| 3727 | hb_set_subtract (lookup_indexes, &inactive_lookups); |
| 3728 | } |
| 3729 | |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3730 | void prune_langsys (const hb_map_t *duplicate_feature_map, |
Behdad Esfahbod | 394f772 | 2021-11-19 11:49:23 -0700 | [diff] [blame] | 3731 | hb_hashmap_t<unsigned, hb_set_t *> *script_langsys_map, |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3732 | hb_set_t *new_feature_indexes /* OUT */) const |
| 3733 | { |
| 3734 | hb_prune_langsys_context_t c (this, script_langsys_map, duplicate_feature_map, new_feature_indexes); |
Garret Rieger | c6adb90 | 2021-03-29 16:08:44 -0700 | [diff] [blame] | 3735 | |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3736 | unsigned count = get_script_count (); |
| 3737 | for (unsigned script_index = 0; script_index < count; script_index++) |
| 3738 | { |
| 3739 | const Script& s = get_script (script_index); |
| 3740 | s.prune_langsys (&c, script_index); |
| 3741 | } |
| 3742 | } |
| 3743 | |
Qunxin Liu | 973c47f | 2020-06-11 11:27:57 -0700 | [diff] [blame] | 3744 | template <typename TLookup> |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3745 | bool subset (hb_subset_layout_context_t *c) const |
Behdad Esfahbod | bfa72a9 | 2018-09-01 18:34:50 -0700 | [diff] [blame] | 3746 | { |
| 3747 | TRACE_SUBSET (this); |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3748 | auto *out = c->subset_context->serializer->embed (*this); |
Behdad Esfahbod | bfa72a9 | 2018-09-01 18:34:50 -0700 | [diff] [blame] | 3749 | if (unlikely (!out)) return_trace (false); |
Behdad Esfahbod | 1b6d0c4 | 2018-12-13 18:10:48 -0500 | [diff] [blame] | 3750 | |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3751 | typedef LookupOffsetList<TLookup> TLookupList; |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3752 | reinterpret_cast<Offset16To<TLookupList> &> (out->lookupList) |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 3753 | .serialize_subset (c->subset_context, |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3754 | reinterpret_cast<const Offset16To<TLookupList> &> (lookupList), |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3755 | this, |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3756 | c); |
| 3757 | |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3758 | reinterpret_cast<Offset16To<RecordListOfFeature> &> (out->featureList) |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 3759 | .serialize_subset (c->subset_context, |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3760 | reinterpret_cast<const Offset16To<RecordListOfFeature> &> (featureList), |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3761 | this, |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3762 | c); |
| 3763 | |
| 3764 | out->scriptList.serialize_subset (c->subset_context, |
| 3765 | scriptList, |
| 3766 | this, |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3767 | c); |
Behdad Esfahbod | 9c3747c | 2018-09-03 16:53:03 -0700 | [diff] [blame] | 3768 | |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 3769 | #ifndef HB_NO_VAR |
Behdad Esfahbod | bfa72a9 | 2018-09-01 18:34:50 -0700 | [diff] [blame] | 3770 | if (version.to_int () >= 0x00010001u) |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3771 | { |
ariza | 188a0a4 | 2020-03-07 11:02:36 -0800 | [diff] [blame] | 3772 | bool ret = out->featureVars.serialize_subset (c->subset_context, featureVars, this, c); |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3773 | if (!ret) |
| 3774 | { |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 3775 | out->version.major = 1; |
| 3776 | out->version.minor = 0; |
Qunxin Liu | e565d1f | 2019-11-01 10:21:36 -0700 | [diff] [blame] | 3777 | } |
| 3778 | } |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 3779 | #endif |
Behdad Esfahbod | 1b6d0c4 | 2018-12-13 18:10:48 -0500 | [diff] [blame] | 3780 | |
Behdad Esfahbod | bfa72a9 | 2018-09-01 18:34:50 -0700 | [diff] [blame] | 3781 | return_trace (true); |
| 3782 | } |
| 3783 | |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3784 | void find_duplicate_features (const hb_map_t *lookup_indices, |
| 3785 | const hb_set_t *feature_indices, |
| 3786 | hb_map_t *duplicate_feature_map /* OUT */) const |
| 3787 | { |
Qunxin Liu | 364b6b3 | 2021-10-31 14:38:20 -0700 | [diff] [blame] | 3788 | if (feature_indices->is_empty ()) return; |
Behdad Esfahbod | 394f772 | 2021-11-19 11:49:23 -0700 | [diff] [blame] | 3789 | hb_hashmap_t<hb_tag_t, hb_set_t *> unique_features; |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3790 | //find out duplicate features after subset |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3791 | for (unsigned i : feature_indices->iter ()) |
| 3792 | { |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3793 | hb_tag_t t = get_feature_tag (i); |
Behdad Esfahbod | 5a91db1 | 2022-01-13 12:44:48 -0700 | [diff] [blame] | 3794 | if (t == HB_MAP_VALUE_INVALID) continue; |
Qunxin Liu | 364b6b3 | 2021-10-31 14:38:20 -0700 | [diff] [blame] | 3795 | if (!unique_features.has (t)) |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3796 | { |
Qunxin Liu | 364b6b3 | 2021-10-31 14:38:20 -0700 | [diff] [blame] | 3797 | hb_set_t* indices = hb_set_create (); |
| 3798 | if (unlikely (indices == hb_set_get_empty () || |
| 3799 | !unique_features.set (t, indices))) |
| 3800 | { |
| 3801 | hb_set_destroy (indices); |
| 3802 | for (auto _ : unique_features.iter ()) |
| 3803 | hb_set_destroy (_.second); |
| 3804 | return; |
| 3805 | } |
| 3806 | if (unique_features.get (t)) |
| 3807 | unique_features.get (t)->add (i); |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3808 | duplicate_feature_map->set (i, i); |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3809 | continue; |
| 3810 | } |
| 3811 | |
Qunxin Liu | 8c5c817 | 2021-09-12 20:16:30 -0700 | [diff] [blame] | 3812 | bool found = false; |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3813 | |
Qunxin Liu | 364b6b3 | 2021-10-31 14:38:20 -0700 | [diff] [blame] | 3814 | hb_set_t* same_tag_features = unique_features.get (t); |
| 3815 | for (unsigned other_f_index : same_tag_features->iter ()) |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3816 | { |
Qunxin Liu | 8c5c817 | 2021-09-12 20:16:30 -0700 | [diff] [blame] | 3817 | const Feature& f = get_feature (i); |
| 3818 | const Feature& other_f = get_feature (other_f_index); |
| 3819 | |
| 3820 | auto f_iter = |
| 3821 | + hb_iter (f.lookupIndex) |
| 3822 | | hb_filter (lookup_indices) |
| 3823 | ; |
Garret Rieger | f51b48c | 2021-11-02 16:16:52 -0700 | [diff] [blame] | 3824 | |
Qunxin Liu | 8c5c817 | 2021-09-12 20:16:30 -0700 | [diff] [blame] | 3825 | auto other_f_iter = |
| 3826 | + hb_iter (other_f.lookupIndex) |
| 3827 | | hb_filter (lookup_indices) |
| 3828 | ; |
Garret Rieger | f51b48c | 2021-11-02 16:16:52 -0700 | [diff] [blame] | 3829 | |
Qunxin Liu | 8c5c817 | 2021-09-12 20:16:30 -0700 | [diff] [blame] | 3830 | bool is_equal = true; |
| 3831 | for (; f_iter && other_f_iter; f_iter++, other_f_iter++) |
| 3832 | { |
| 3833 | unsigned a = *f_iter; |
| 3834 | unsigned b = *other_f_iter; |
| 3835 | if (a != b) { is_equal = false; break; } |
| 3836 | } |
| 3837 | |
| 3838 | if (is_equal == false || f_iter || other_f_iter) continue; |
Garret Rieger | f51b48c | 2021-11-02 16:16:52 -0700 | [diff] [blame] | 3839 | |
Qunxin Liu | 8c5c817 | 2021-09-12 20:16:30 -0700 | [diff] [blame] | 3840 | found = true; |
| 3841 | duplicate_feature_map->set (i, other_f_index); |
| 3842 | break; |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3843 | } |
Garret Rieger | f51b48c | 2021-11-02 16:16:52 -0700 | [diff] [blame] | 3844 | |
Qunxin Liu | 8c5c817 | 2021-09-12 20:16:30 -0700 | [diff] [blame] | 3845 | if (found == false) |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3846 | { |
Qunxin Liu | 364b6b3 | 2021-10-31 14:38:20 -0700 | [diff] [blame] | 3847 | same_tag_features->add (i); |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3848 | duplicate_feature_map->set (i, i); |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3849 | } |
| 3850 | } |
Qunxin Liu | 364b6b3 | 2021-10-31 14:38:20 -0700 | [diff] [blame] | 3851 | |
| 3852 | for (auto _ : unique_features.iter ()) |
| 3853 | hb_set_destroy (_.second); |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3854 | } |
| 3855 | |
Garret Rieger | 718bf5a | 2020-09-29 13:16:01 -0700 | [diff] [blame] | 3856 | void prune_features (const hb_map_t *lookup_indices, /* IN */ |
Qunxin Liu | 56ca435 | 2021-01-28 15:21:26 -0800 | [diff] [blame] | 3857 | hb_set_t *feature_indices /* IN/OUT */) const |
Qunxin Liu | 8ffc9ad | 2019-10-31 15:59:02 -0700 | [diff] [blame] | 3858 | { |
Garret Rieger | 718bf5a | 2020-09-29 13:16:01 -0700 | [diff] [blame] | 3859 | #ifndef HB_NO_VAR |
| 3860 | // This is the set of feature indices which have alternate versions defined |
| 3861 | // if the FeatureVariation's table and the alternate version(s) intersect the |
| 3862 | // set of lookup indices. |
| 3863 | hb_set_t alternate_feature_indices; |
| 3864 | if (version.to_int () >= 0x00010001u) |
| 3865 | (this+featureVars).closure_features (lookup_indices, &alternate_feature_indices); |
Behdad Esfahbod | fad452b | 2021-08-16 20:48:24 -0600 | [diff] [blame] | 3866 | if (unlikely (alternate_feature_indices.in_error())) |
| 3867 | { |
| 3868 | feature_indices->err (); |
Garret Rieger | 718bf5a | 2020-09-29 13:16:01 -0700 | [diff] [blame] | 3869 | return; |
| 3870 | } |
| 3871 | #endif |
| 3872 | |
| 3873 | for (unsigned i : feature_indices->iter()) |
Qunxin Liu | 8ffc9ad | 2019-10-31 15:59:02 -0700 | [diff] [blame] | 3874 | { |
Qunxin Liu | d7c012a | 2020-02-26 13:11:42 -0800 | [diff] [blame] | 3875 | const Feature& f = get_feature (i); |
Garret Rieger | c35d786 | 2021-04-01 14:32:38 -0700 | [diff] [blame] | 3876 | hb_tag_t tag = get_feature_tag (i); |
| 3877 | if (tag == HB_TAG ('p', 'r', 'e', 'f')) |
| 3878 | // Note: Never ever drop feature 'pref', even if it's empty. |
| 3879 | // HarfBuzz chooses shaper for Khmer based on presence of this |
| 3880 | // feature. See thread at: |
| 3881 | // http://lists.freedesktop.org/archives/harfbuzz/2012-November/002660.html |
| 3882 | continue; |
Garret Rieger | 718bf5a | 2020-09-29 13:16:01 -0700 | [diff] [blame] | 3883 | |
Qunxin Liu | ca418ca | 2021-11-17 16:42:08 -0800 | [diff] [blame] | 3884 | |
| 3885 | if (!f.featureParams.is_null () && |
| 3886 | tag == HB_TAG ('s', 'i', 'z', 'e')) |
| 3887 | continue; |
| 3888 | |
| 3889 | if (!f.intersects_lookup_indexes (lookup_indices) |
Behdad Esfahbod | 2902529 | 2021-03-02 15:05:22 -0700 | [diff] [blame] | 3890 | #ifndef HB_NO_VAR |
| 3891 | && !alternate_feature_indices.has (i) |
| 3892 | #endif |
| 3893 | ) |
| 3894 | feature_indices->del (i); |
Qunxin Liu | 8ffc9ad | 2019-10-31 15:59:02 -0700 | [diff] [blame] | 3895 | } |
Qunxin Liu | 8ffc9ad | 2019-10-31 15:59:02 -0700 | [diff] [blame] | 3896 | } |
| 3897 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 3898 | unsigned int get_size () const |
Behdad Esfahbod | bfa72a9 | 2018-09-01 18:34:50 -0700 | [diff] [blame] | 3899 | { |
| 3900 | return min_size + |
| 3901 | (version.to_int () >= 0x00010001u ? featureVars.static_size : 0); |
| 3902 | } |
| 3903 | |
Behdad Esfahbod | 6d61852 | 2018-09-03 16:41:28 -0700 | [diff] [blame] | 3904 | template <typename TLookup> |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 3905 | bool sanitize (hb_sanitize_context_t *c) const |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 3906 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 3907 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 5efe360 | 2021-03-31 15:33:22 -0600 | [diff] [blame] | 3908 | typedef List16OfOffset16To<TLookup> TLookupList; |
Behdad Esfahbod | 8e3cde6 | 2019-06-19 19:58:24 -0700 | [diff] [blame] | 3909 | if (unlikely (!(version.sanitize (c) && |
| 3910 | likely (version.major == 1) && |
| 3911 | scriptList.sanitize (c, this) && |
| 3912 | featureList.sanitize (c, this) && |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3913 | reinterpret_cast<const Offset16To<TLookupList> &> (lookupList).sanitize (c, this)))) |
Behdad Esfahbod | 8e3cde6 | 2019-06-19 19:58:24 -0700 | [diff] [blame] | 3914 | return_trace (false); |
| 3915 | |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 3916 | #ifndef HB_NO_VAR |
Behdad Esfahbod | 8e3cde6 | 2019-06-19 19:58:24 -0700 | [diff] [blame] | 3917 | if (unlikely (!(version.to_int () < 0x00010001u || featureVars.sanitize (c, this)))) |
| 3918 | return_trace (false); |
Behdad Esfahbod | a849873 | 2019-06-19 19:26:22 -0700 | [diff] [blame] | 3919 | #endif |
Behdad Esfahbod | 8e3cde6 | 2019-06-19 19:58:24 -0700 | [diff] [blame] | 3920 | |
| 3921 | return_trace (true); |
Behdad Esfahbod | cd3827e | 2009-08-04 02:09:34 -0400 | [diff] [blame] | 3922 | } |
| 3923 | |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 3924 | template <typename T> |
| 3925 | struct accelerator_t |
| 3926 | { |
Behdad Esfahbod | ac1bb3e | 2022-01-20 11:47:17 -0700 | [diff] [blame] | 3927 | accelerator_t (hb_face_t *face) |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 3928 | { |
Ebrahim Byagowi | ba22df3 | 2020-03-10 10:42:20 +0330 | [diff] [blame] | 3929 | this->table = hb_sanitize_context_t ().reference_table<T> (face); |
Behdad Esfahbod | 5671947 | 2020-06-05 12:57:23 -0700 | [diff] [blame] | 3930 | if (unlikely (this->table->is_blocklisted (this->table.get_blob (), face))) |
Behdad Esfahbod | 574d888 | 2018-11-25 16:51:22 -0500 | [diff] [blame] | 3931 | { |
| 3932 | hb_blob_destroy (this->table.get_blob ()); |
| 3933 | this->table = hb_blob_get_empty (); |
| 3934 | } |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 3935 | |
Behdad Esfahbod | b929100 | 2018-08-26 01:15:47 -0700 | [diff] [blame] | 3936 | this->lookup_count = table->get_lookup_count (); |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 3937 | |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 3938 | 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] | 3939 | if (unlikely (!this->accels)) |
Dominik Röttsches | a5f6f86 | 2020-10-23 14:25:05 +0300 | [diff] [blame] | 3940 | { |
Ebrahim Byagowi | 11aa046 | 2018-11-15 23:10:56 +0330 | [diff] [blame] | 3941 | this->lookup_count = 0; |
Behdad Esfahbod | 53806e5 | 2020-11-25 11:51:37 -0700 | [diff] [blame] | 3942 | this->table.destroy (); |
Dominik Röttsches | a5f6f86 | 2020-10-23 14:25:05 +0300 | [diff] [blame] | 3943 | this->table = hb_blob_get_empty (); |
| 3944 | } |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 3945 | |
| 3946 | for (unsigned int i = 0; i < this->lookup_count; i++) |
Behdad Esfahbod | b929100 | 2018-08-26 01:15:47 -0700 | [diff] [blame] | 3947 | this->accels[i].init (table->get_lookup (i)); |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 3948 | } |
Behdad Esfahbod | ac1bb3e | 2022-01-20 11:47:17 -0700 | [diff] [blame] | 3949 | ~accelerator_t () |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 3950 | { |
Behdad Esfahbod | b929100 | 2018-08-26 01:15:47 -0700 | [diff] [blame] | 3951 | for (unsigned int i = 0; i < this->lookup_count; i++) |
| 3952 | this->accels[i].fini (); |
Behdad Esfahbod | 2337f0d | 2021-07-08 10:58:50 -0600 | [diff] [blame] | 3953 | hb_free (this->accels); |
Behdad Esfahbod | da6aa3b | 2018-11-11 11:40:57 -0500 | [diff] [blame] | 3954 | this->table.destroy (); |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 3955 | } |
| 3956 | |
Behdad Esfahbod | 5d0078a | 2018-11-10 23:52:15 -0500 | [diff] [blame] | 3957 | hb_blob_ptr_t<T> table; |
Behdad Esfahbod | b929100 | 2018-08-26 01:15:47 -0700 | [diff] [blame] | 3958 | unsigned int lookup_count; |
| 3959 | hb_ot_layout_lookup_accelerator_t *accels; |
Behdad Esfahbod | 963413f | 2018-08-26 00:47:55 -0700 | [diff] [blame] | 3960 | }; |
| 3961 | |
Behdad Esfahbod | 212aba6 | 2009-05-24 00:50:27 -0400 | [diff] [blame] | 3962 | protected: |
Behdad Esfahbod | 9a13ed4 | 2016-02-22 11:44:45 +0900 | [diff] [blame] | 3963 | FixedVersion<>version; /* Version of the GSUB/GPOS table--initially set |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 3964 | * to 0x00010000u */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3965 | Offset16To<ScriptList> |
Ebrahim Byagowi | ce114d6 | 2019-12-31 15:53:02 +0330 | [diff] [blame] | 3966 | scriptList; /* ScriptList table */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3967 | Offset16To<FeatureList> |
Ebrahim Byagowi | ce114d6 | 2019-12-31 15:53:02 +0330 | [diff] [blame] | 3968 | featureList; /* FeatureList table */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3969 | Offset16To<LookupList> |
Ebrahim Byagowi | ce114d6 | 2019-12-31 15:53:02 +0330 | [diff] [blame] | 3970 | lookupList; /* LookupList table */ |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 3971 | Offset32To<FeatureVariations> |
Behdad Esfahbod | 59055b5 | 2016-09-10 01:24:28 -0700 | [diff] [blame] | 3972 | featureVars; /* Offset to Feature Variations |
| 3973 | table--from beginning of table |
| 3974 | * (may be NULL). Introduced |
| 3975 | * in version 0x00010001. */ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 3976 | public: |
Behdad Esfahbod | 59055b5 | 2016-09-10 01:24:28 -0700 | [diff] [blame] | 3977 | DEFINE_SIZE_MIN (10); |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 3978 | }; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 3979 | |
Behdad Esfahbod | 6f20f72 | 2009-05-17 20:28:01 -0400 | [diff] [blame] | 3980 | |
Behdad Esfahbod | 7d52e66 | 2012-11-16 18:49:54 -0800 | [diff] [blame] | 3981 | } /* namespace OT */ |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 3982 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 3983 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 3984 | #endif /* HB_OT_LAYOUT_GSUBGPOS_HH */ |