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