Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 1 | /* |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 2 | * Copyright © 2018 Ebrahim Byagowi |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 3 | * Copyright © 2018 Google, Inc. |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 4 | * |
| 5 | * This is part of HarfBuzz, a text shaping library. |
| 6 | * |
| 7 | * Permission is hereby granted, without written agreement and without |
| 8 | * license or royalty fees, to use, copy, modify, and distribute this |
| 9 | * software and its documentation for any purpose, provided that the |
| 10 | * above copyright notice and the following two paragraphs appear in |
| 11 | * all copies of this software. |
| 12 | * |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 17 | * DAMAGE. |
| 18 | * |
| 19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 24 | * |
| 25 | * Google Author(s): Behdad Esfahbod |
| 26 | */ |
| 27 | |
| 28 | #ifndef HB_AAT_LAYOUT_KERX_TABLE_HH |
| 29 | #define HB_AAT_LAYOUT_KERX_TABLE_HH |
| 30 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 31 | #include "hb-open-type.hh" |
| 32 | #include "hb-aat-layout-common.hh" |
Behdad Esfahbod | 1622ba5 | 2018-10-11 01:14:18 -0400 | [diff] [blame] | 33 | #include "hb-ot-layout-gpos-table.hh" |
Behdad Esfahbod | ed2a404 | 2018-10-07 22:33:41 -0400 | [diff] [blame] | 34 | #include "hb-ot-kern-table.hh" |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 35 | |
Ebrahim Byagowi | a02c3ee | 2018-04-12 13:38:19 +0430 | [diff] [blame] | 36 | /* |
| 37 | * kerx -- Extended Kerning |
| 38 | * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kerx.html |
| 39 | */ |
Ebrahim Byagowi | 158f281 | 2018-03-26 12:04:30 +0430 | [diff] [blame] | 40 | #define HB_AAT_TAG_kerx HB_TAG('k','e','r','x') |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 41 | |
| 42 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 43 | namespace AAT { |
| 44 | |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 45 | using namespace OT; |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 46 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 47 | |
Behdad Esfahbod | 29d8775 | 2018-10-19 16:06:54 -0700 | [diff] [blame] | 48 | static inline int |
| 49 | kerxTupleKern (int value, |
| 50 | unsigned int tupleCount, |
| 51 | const void *base, |
| 52 | hb_aat_apply_context_t *c) |
| 53 | { |
| 54 | if (likely (!tupleCount)) return value; |
| 55 | |
| 56 | unsigned int offset = value; |
| 57 | const FWORD *pv = &StructAtOffset<FWORD> (base, offset); |
| 58 | if (unlikely (!pv->sanitize (&c->sanitizer))) return 0; |
| 59 | return *pv; |
| 60 | } |
| 61 | |
| 62 | |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 63 | struct KerxSubTableHeader |
| 64 | { |
| 65 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 66 | { |
| 67 | TRACE_SANITIZE (this); |
| 68 | return_trace (likely (c->check_struct (this))); |
| 69 | } |
| 70 | |
| 71 | public: |
| 72 | HBUINT32 length; |
| 73 | HBUINT32 coverage; |
| 74 | HBUINT32 tupleCount; |
| 75 | public: |
| 76 | DEFINE_SIZE_STATIC (12); |
| 77 | }; |
| 78 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 79 | struct KerxSubTableFormat0 |
| 80 | { |
Behdad Esfahbod | b713c13 | 2018-10-20 14:56:28 -0700 | [diff] [blame] | 81 | inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, |
| 82 | hb_aat_apply_context_t *c) const |
Behdad Esfahbod | ed2a404 | 2018-10-07 22:33:41 -0400 | [diff] [blame] | 83 | { |
| 84 | hb_glyph_pair_t pair = {left, right}; |
| 85 | int i = pairs.bsearch (pair); |
Behdad Esfahbod | b713c13 | 2018-10-20 14:56:28 -0700 | [diff] [blame] | 86 | if (i == -1) return 0; |
| 87 | int v = pairs[i].get_kerning (); |
| 88 | return kerxTupleKern (v, header.tupleCount, this, c); |
Behdad Esfahbod | ed2a404 | 2018-10-07 22:33:41 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 91 | inline bool apply (hb_aat_apply_context_t *c) const |
| 92 | { |
| 93 | TRACE_APPLY (this); |
| 94 | |
Behdad Esfahbod | 60f86d3 | 2018-10-10 18:10:05 -0400 | [diff] [blame] | 95 | if (!c->plan->requested_kerning) |
| 96 | return false; |
| 97 | |
Behdad Esfahbod | b713c13 | 2018-10-20 14:56:28 -0700 | [diff] [blame] | 98 | accelerator_t accel (*this, c); |
| 99 | hb_kern_machine_t<accelerator_t> machine (accel); |
Behdad Esfahbod | 53e5594 | 2018-10-09 22:35:22 -0400 | [diff] [blame] | 100 | machine.kern (c->font, c->buffer, c->plan->kern_mask); |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 101 | |
| 102 | return_trace (true); |
| 103 | } |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 104 | |
Behdad Esfahbod | b713c13 | 2018-10-20 14:56:28 -0700 | [diff] [blame] | 105 | struct accelerator_t |
| 106 | { |
| 107 | const KerxSubTableFormat0 &table; |
| 108 | hb_aat_apply_context_t *c; |
| 109 | |
| 110 | inline accelerator_t (const KerxSubTableFormat0 &table_, |
| 111 | hb_aat_apply_context_t *c_) : |
| 112 | table (table_), c (c_) {} |
| 113 | |
| 114 | inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const |
| 115 | { return table.get_kerning (left, right, c); } |
| 116 | }; |
| 117 | |
| 118 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 119 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 120 | { |
| 121 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 68b7050 | 2018-10-20 12:09:41 -0700 | [diff] [blame] | 122 | return_trace (likely (c->check_struct (this) && |
| 123 | pairs.sanitize (c))); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | protected: |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 127 | KerxSubTableHeader header; |
Behdad Esfahbod | ed2a404 | 2018-10-07 22:33:41 -0400 | [diff] [blame] | 128 | BinSearchArrayOf<KernPair, HBUINT32> |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 129 | pairs; /* Sorted kern records. */ |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 130 | public: |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 131 | DEFINE_SIZE_ARRAY (28, pairs); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | struct KerxSubTableFormat1 |
| 135 | { |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 136 | struct EntryData |
| 137 | { |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 138 | HBUINT16 kernActionIndex;/* Index into the kerning value array. If |
| 139 | * this index is 0xFFFF, then no kerning |
| 140 | * is to be performed. */ |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 141 | public: |
| 142 | DEFINE_SIZE_STATIC (2); |
| 143 | }; |
| 144 | |
| 145 | struct driver_context_t |
| 146 | { |
| 147 | static const bool in_place = true; |
| 148 | enum Flags |
| 149 | { |
| 150 | Push = 0x8000, /* If set, push this glyph on the kerning stack. */ |
| 151 | DontAdvance = 0x4000, /* If set, don't advance to the next glyph |
| 152 | * before going to the new state. */ |
| 153 | Reset = 0x2000, /* If set, reset the kerning data (clear the stack) */ |
| 154 | Reserved = 0x1FFF, /* Not used; set to 0. */ |
| 155 | }; |
| 156 | |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 157 | inline driver_context_t (const KerxSubTableFormat1 *table, |
| 158 | hb_aat_apply_context_t *c_) : |
| 159 | c (c_), |
Behdad Esfahbod | 2b72c4b | 2018-10-10 21:53:14 -0400 | [diff] [blame] | 160 | /* Apparently the offset kernAction is from the beginning of the state-machine, |
| 161 | * similar to offsets in morx table, NOT from beginning of this table, like |
| 162 | * other subtables in kerx. Discovered via testing. */ |
Behdad Esfahbod | 9f450f0 | 2018-10-10 21:46:58 -0400 | [diff] [blame] | 163 | kernAction (&table->machine + table->kernAction), |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 164 | depth (0) {} |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 165 | |
Ebrahim Byagowi | b053cab | 2018-10-30 18:41:34 +0330 | [diff] [blame] | 166 | inline bool is_actionable (StateTableDriver<MorxTypes, EntryData> *driver HB_UNUSED, |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 167 | const Entry<EntryData> *entry) |
| 168 | { |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 169 | return entry->data.kernActionIndex != 0xFFFF; |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 170 | } |
Ebrahim Byagowi | b053cab | 2018-10-30 18:41:34 +0330 | [diff] [blame] | 171 | inline bool transition (StateTableDriver<MorxTypes, EntryData> *driver, |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 172 | const Entry<EntryData> *entry) |
| 173 | { |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 174 | hb_buffer_t *buffer = driver->buffer; |
| 175 | unsigned int flags = entry->flags; |
| 176 | |
| 177 | if (flags & Reset) |
| 178 | { |
Behdad Esfahbod | 2352cc3 | 2018-10-19 09:58:45 -0700 | [diff] [blame] | 179 | depth = 0; |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (flags & Push) |
| 183 | { |
Behdad Esfahbod | 2352cc3 | 2018-10-19 09:58:45 -0700 | [diff] [blame] | 184 | if (likely (depth < ARRAY_LENGTH (stack))) |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 185 | stack[depth++] = buffer->idx; |
| 186 | else |
| 187 | depth = 0; /* Probably not what CoreText does, but better? */ |
| 188 | } |
| 189 | |
| 190 | if (entry->data.kernActionIndex != 0xFFFF) |
| 191 | { |
| 192 | const FWORD *actions = &kernAction[entry->data.kernActionIndex]; |
Behdad Esfahbod | 2352cc3 | 2018-10-19 09:58:45 -0700 | [diff] [blame] | 193 | if (!c->sanitizer.check_array (actions, depth)) |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 194 | { |
| 195 | depth = 0; |
| 196 | return false; |
| 197 | } |
| 198 | |
Behdad Esfahbod | f7c45bc | 2018-10-10 22:15:13 -0400 | [diff] [blame] | 199 | hb_mask_t kern_mask = c->plan->kern_mask; |
Behdad Esfahbod | 2352cc3 | 2018-10-19 09:58:45 -0700 | [diff] [blame] | 200 | for (unsigned int i = 0; i < depth; i++) |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 201 | { |
Behdad Esfahbod | 9f450f0 | 2018-10-10 21:46:58 -0400 | [diff] [blame] | 202 | /* Apparently, when spec says "Each pops one glyph from the kerning stack |
| 203 | * and applies the kerning value to it.", it doesn't mean it in that order. |
| 204 | * The deepest item in the stack corresponds to the first item in the action |
| 205 | * list. Discovered by testing. */ |
| 206 | unsigned int idx = stack[i]; |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 207 | int v = *actions++; |
Behdad Esfahbod | bb35725 | 2018-10-15 10:20:39 -0700 | [diff] [blame] | 208 | if (idx < buffer->len && buffer->info[idx].mask & kern_mask) |
Behdad Esfahbod | f7c45bc | 2018-10-10 22:15:13 -0400 | [diff] [blame] | 209 | { |
Behdad Esfahbod | f7c45bc | 2018-10-10 22:15:13 -0400 | [diff] [blame] | 210 | if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) |
Behdad Esfahbod | 8034d1d | 2018-11-02 14:47:42 -0400 | [diff] [blame] | 211 | { |
Behdad Esfahbod | f7c45bc | 2018-10-10 22:15:13 -0400 | [diff] [blame] | 212 | buffer->pos[idx].x_advance += c->font->em_scale_x (v); |
Behdad Esfahbod | 8034d1d | 2018-11-02 14:47:42 -0400 | [diff] [blame] | 213 | if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) |
| 214 | buffer->pos[idx].x_offset += c->font->em_scale_x (v); |
| 215 | } |
Behdad Esfahbod | f7c45bc | 2018-10-10 22:15:13 -0400 | [diff] [blame] | 216 | else |
Behdad Esfahbod | 8034d1d | 2018-11-02 14:47:42 -0400 | [diff] [blame] | 217 | { |
Behdad Esfahbod | f7c45bc | 2018-10-10 22:15:13 -0400 | [diff] [blame] | 218 | buffer->pos[idx].y_advance += c->font->em_scale_y (v); |
Behdad Esfahbod | 8034d1d | 2018-11-02 14:47:42 -0400 | [diff] [blame] | 219 | if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) |
| 220 | buffer->pos[idx].y_offset += c->font->em_scale_y (v); |
| 221 | } |
Behdad Esfahbod | f7c45bc | 2018-10-10 22:15:13 -0400 | [diff] [blame] | 222 | } |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 223 | } |
Behdad Esfahbod | 9f450f0 | 2018-10-10 21:46:58 -0400 | [diff] [blame] | 224 | depth = 0; |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 225 | } |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 230 | private: |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 231 | hb_aat_apply_context_t *c; |
| 232 | const UnsizedArrayOf<FWORD> &kernAction; |
| 233 | unsigned int stack[8]; |
| 234 | unsigned int depth; |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 235 | }; |
| 236 | |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 237 | inline bool apply (hb_aat_apply_context_t *c) const |
| 238 | { |
| 239 | TRACE_APPLY (this); |
| 240 | |
Behdad Esfahbod | 60f86d3 | 2018-10-10 18:10:05 -0400 | [diff] [blame] | 241 | if (!c->plan->requested_kerning) |
| 242 | return false; |
| 243 | |
Behdad Esfahbod | 29d8775 | 2018-10-19 16:06:54 -0700 | [diff] [blame] | 244 | if (header.tupleCount) |
| 245 | return_trace (false); /* TODO kerxTupleKern */ |
| 246 | |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 247 | driver_context_t dc (this, c); |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 248 | |
Ebrahim Byagowi | b053cab | 2018-10-30 18:41:34 +0330 | [diff] [blame] | 249 | StateTableDriver<MorxTypes, EntryData> driver (machine, c->buffer, c->font->face); |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 250 | driver.drive (&dc); |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 251 | |
| 252 | return_trace (true); |
| 253 | } |
| 254 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 255 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 256 | { |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 257 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 40f2b93 | 2018-10-14 14:56:32 -0700 | [diff] [blame] | 258 | /* The rest of array sanitizations are done at run-time. */ |
| 259 | return_trace (likely (c->check_struct (this) && |
| 260 | machine.sanitize (c))); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | protected: |
Behdad Esfahbod | 339036d | 2018-10-10 20:37:22 -0400 | [diff] [blame] | 264 | KerxSubTableHeader header; |
Behdad Esfahbod | 8034d1d | 2018-11-02 14:47:42 -0400 | [diff] [blame] | 265 | StateTable<MorxTypes, EntryData> machine; |
Behdad Esfahbod | 8496753 | 2018-10-10 21:18:37 -0400 | [diff] [blame] | 266 | LOffsetTo<UnsizedArrayOf<FWORD>, false> kernAction; |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 267 | public: |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 268 | DEFINE_SIZE_STATIC (32); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 269 | }; |
| 270 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 271 | struct KerxSubTableFormat2 |
| 272 | { |
Behdad Esfahbod | 1a5a332 | 2018-10-07 23:08:39 -0400 | [diff] [blame] | 273 | inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 274 | hb_aat_apply_context_t *c) const |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 275 | { |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 276 | unsigned int num_glyphs = c->sanitizer.get_num_glyphs (); |
Behdad Esfahbod | 7727e73 | 2018-10-10 13:24:51 -0400 | [diff] [blame] | 277 | unsigned int l = (this+leftClassTable).get_value_or_null (left, num_glyphs); |
| 278 | unsigned int r = (this+rightClassTable).get_value_or_null (right, num_glyphs); |
Behdad Esfahbod | 8aa83d9 | 2018-10-07 22:43:59 -0400 | [diff] [blame] | 279 | unsigned int offset = l + r; |
Behdad Esfahbod | 71f76f2 | 2018-10-13 13:36:27 -0400 | [diff] [blame] | 280 | const FWORD *v = &StructAtOffset<FWORD> (&(this+array), offset); |
| 281 | if (unlikely (!v->sanitize (&c->sanitizer))) return 0; |
Behdad Esfahbod | 29d8775 | 2018-10-19 16:06:54 -0700 | [diff] [blame] | 282 | return kerxTupleKern (*v, header.tupleCount, this, c); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 283 | } |
| 284 | |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 285 | inline bool apply (hb_aat_apply_context_t *c) const |
| 286 | { |
| 287 | TRACE_APPLY (this); |
| 288 | |
Behdad Esfahbod | 60f86d3 | 2018-10-10 18:10:05 -0400 | [diff] [blame] | 289 | if (!c->plan->requested_kerning) |
| 290 | return false; |
| 291 | |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 292 | accelerator_t accel (*this, c); |
Behdad Esfahbod | 53e5594 | 2018-10-09 22:35:22 -0400 | [diff] [blame] | 293 | hb_kern_machine_t<accelerator_t> machine (accel); |
| 294 | machine.kern (c->font, c->buffer, c->plan->kern_mask); |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 295 | |
| 296 | return_trace (true); |
| 297 | } |
| 298 | |
Behdad Esfahbod | 53e5594 | 2018-10-09 22:35:22 -0400 | [diff] [blame] | 299 | struct accelerator_t |
| 300 | { |
| 301 | const KerxSubTableFormat2 &table; |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 302 | hb_aat_apply_context_t *c; |
Behdad Esfahbod | 53e5594 | 2018-10-09 22:35:22 -0400 | [diff] [blame] | 303 | |
| 304 | inline accelerator_t (const KerxSubTableFormat2 &table_, |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 305 | hb_aat_apply_context_t *c_) : |
| 306 | table (table_), c (c_) {} |
Behdad Esfahbod | 53e5594 | 2018-10-09 22:35:22 -0400 | [diff] [blame] | 307 | |
| 308 | inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 309 | { return table.get_kerning (left, right, c); } |
Behdad Esfahbod | 53e5594 | 2018-10-09 22:35:22 -0400 | [diff] [blame] | 310 | }; |
| 311 | |
Behdad Esfahbod | b713c13 | 2018-10-20 14:56:28 -0700 | [diff] [blame] | 312 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 313 | { |
| 314 | TRACE_SANITIZE (this); |
| 315 | return_trace (likely (c->check_struct (this) && |
| 316 | leftClassTable.sanitize (c, this) && |
| 317 | rightClassTable.sanitize (c, this) && |
| 318 | c->check_range (this, array))); |
| 319 | } |
| 320 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 321 | protected: |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 322 | KerxSubTableHeader header; |
| 323 | HBUINT32 rowWidth; /* The width, in bytes, of a row in the table. */ |
Behdad Esfahbod | 87205ef | 2018-10-16 15:40:44 -0700 | [diff] [blame] | 324 | LOffsetTo<Lookup<HBUINT16>, false> |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 325 | leftClassTable; /* Offset from beginning of this subtable to |
| 326 | * left-hand class table. */ |
Behdad Esfahbod | 87205ef | 2018-10-16 15:40:44 -0700 | [diff] [blame] | 327 | LOffsetTo<Lookup<HBUINT16>, false> |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 328 | rightClassTable;/* Offset from beginning of this subtable to |
| 329 | * right-hand class table. */ |
Behdad Esfahbod | c9165f5 | 2018-10-10 20:43:21 -0400 | [diff] [blame] | 330 | LOffsetTo<UnsizedArrayOf<FWORD>, false> |
| 331 | array; /* Offset from beginning of this subtable to |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 332 | * the start of the kerning array. */ |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 333 | public: |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 334 | DEFINE_SIZE_STATIC (28); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | struct KerxSubTableFormat4 |
| 338 | { |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 339 | struct EntryData |
| 340 | { |
| 341 | HBUINT16 ankrActionIndex;/* Either 0xFFFF (for no action) or the index of |
| 342 | * the action to perform. */ |
| 343 | public: |
| 344 | DEFINE_SIZE_STATIC (2); |
| 345 | }; |
| 346 | |
| 347 | struct driver_context_t |
| 348 | { |
| 349 | static const bool in_place = true; |
| 350 | enum Flags |
| 351 | { |
| 352 | Mark = 0x8000, /* If set, remember this glyph as the marked glyph. */ |
| 353 | DontAdvance = 0x4000, /* If set, don't advance to the next glyph before |
| 354 | * going to the new state. */ |
| 355 | Reserved = 0x3FFF, /* Not used; set to 0. */ |
| 356 | }; |
| 357 | |
| 358 | enum SubTableFlags |
| 359 | { |
| 360 | ActionType = 0xC0000000, /* A two-bit field containing the action type. */ |
| 361 | Unused = 0x3F000000, /* Unused - must be zero. */ |
| 362 | Offset = 0x00FFFFFF, /* Masks the offset in bytes from the beginning |
| 363 | * of the subtable to the beginning of the control |
| 364 | * point table. */ |
| 365 | }; |
| 366 | |
| 367 | inline driver_context_t (const KerxSubTableFormat4 *table, |
| 368 | hb_aat_apply_context_t *c_) : |
| 369 | c (c_), |
| 370 | action_type ((table->flags & ActionType) >> 30), |
| 371 | ankrData ((HBUINT16 *) ((const char *) &table->machine + (table->flags & Offset))), |
| 372 | mark_set (false), |
| 373 | mark (0) {} |
| 374 | |
Ebrahim Byagowi | b053cab | 2018-10-30 18:41:34 +0330 | [diff] [blame] | 375 | inline bool is_actionable (StateTableDriver<MorxTypes, EntryData> *driver HB_UNUSED, |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 376 | const Entry<EntryData> *entry) |
| 377 | { |
| 378 | return entry->data.ankrActionIndex != 0xFFFF; |
| 379 | } |
Ebrahim Byagowi | b053cab | 2018-10-30 18:41:34 +0330 | [diff] [blame] | 380 | inline bool transition (StateTableDriver<MorxTypes, EntryData> *driver, |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 381 | const Entry<EntryData> *entry) |
| 382 | { |
| 383 | hb_buffer_t *buffer = driver->buffer; |
| 384 | unsigned int flags = entry->flags; |
| 385 | |
Behdad Esfahbod | bb35725 | 2018-10-15 10:20:39 -0700 | [diff] [blame] | 386 | if (mark_set && entry->data.ankrActionIndex != 0xFFFF && buffer->idx < buffer->len) |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 387 | { |
Behdad Esfahbod | b6bc0d4 | 2018-10-11 01:17:57 -0400 | [diff] [blame] | 388 | hb_glyph_position_t &o = buffer->cur_pos(); |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 389 | switch (action_type) |
| 390 | { |
| 391 | case 0: /* Control Point Actions.*/ |
| 392 | { |
| 393 | /* indexed into glyph outline. */ |
| 394 | const HBUINT16 *data = &ankrData[entry->data.ankrActionIndex]; |
| 395 | if (!c->sanitizer.check_array (data, 2)) |
| 396 | return false; |
| 397 | HB_UNUSED unsigned int markControlPoint = *data++; |
| 398 | HB_UNUSED unsigned int currControlPoint = *data++; |
Behdad Esfahbod | fbbd926 | 2018-10-11 01:22:29 -0400 | [diff] [blame] | 399 | hb_position_t markX = 0; |
| 400 | hb_position_t markY = 0; |
| 401 | hb_position_t currX = 0; |
| 402 | hb_position_t currY = 0; |
| 403 | if (!c->font->get_glyph_contour_point_for_origin (c->buffer->info[mark].codepoint, |
| 404 | markControlPoint, |
| 405 | HB_DIRECTION_LTR /*XXX*/, |
| 406 | &markX, &markY) || |
| 407 | !c->font->get_glyph_contour_point_for_origin (c->buffer->cur ().codepoint, |
| 408 | currControlPoint, |
| 409 | HB_DIRECTION_LTR /*XXX*/, |
| 410 | &currX, &currY)) |
| 411 | return true; /* True, such that the machine continues. */ |
| 412 | |
| 413 | o.x_offset = markX - currX; |
| 414 | o.y_offset = markY - currY; |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 415 | } |
| 416 | break; |
| 417 | |
| 418 | case 1: /* Anchor Point Actions. */ |
| 419 | { |
| 420 | /* Indexed into 'ankr' table. */ |
| 421 | const HBUINT16 *data = &ankrData[entry->data.ankrActionIndex]; |
| 422 | if (!c->sanitizer.check_array (data, 2)) |
| 423 | return false; |
Behdad Esfahbod | 1622ba5 | 2018-10-11 01:14:18 -0400 | [diff] [blame] | 424 | unsigned int markAnchorPoint = *data++; |
| 425 | unsigned int currAnchorPoint = *data++; |
Behdad Esfahbod | b605db2 | 2018-11-04 12:58:02 -0500 | [diff] [blame] | 426 | const Anchor markAnchor = c->ankr_table->get_anchor (c->buffer->info[mark].codepoint, |
| 427 | markAnchorPoint, |
| 428 | c->sanitizer.get_num_glyphs (), |
| 429 | c->ankr_end); |
| 430 | const Anchor currAnchor = c->ankr_table->get_anchor (c->buffer->cur ().codepoint, |
| 431 | currAnchorPoint, |
| 432 | c->sanitizer.get_num_glyphs (), |
| 433 | c->ankr_end); |
Behdad Esfahbod | b6bc0d4 | 2018-10-11 01:17:57 -0400 | [diff] [blame] | 434 | |
Behdad Esfahbod | 1622ba5 | 2018-10-11 01:14:18 -0400 | [diff] [blame] | 435 | o.x_offset = c->font->em_scale_x (markAnchor.xCoordinate) - c->font->em_scale_x (currAnchor.xCoordinate); |
| 436 | o.y_offset = c->font->em_scale_y (markAnchor.yCoordinate) - c->font->em_scale_y (currAnchor.yCoordinate); |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 437 | } |
| 438 | break; |
| 439 | |
| 440 | case 2: /* Control Point Coordinate Actions. */ |
| 441 | { |
| 442 | const FWORD *data = (const FWORD *) &ankrData[entry->data.ankrActionIndex]; |
| 443 | if (!c->sanitizer.check_array (data, 4)) |
| 444 | return false; |
Behdad Esfahbod | b6bc0d4 | 2018-10-11 01:17:57 -0400 | [diff] [blame] | 445 | int markX = *data++; |
| 446 | int markY = *data++; |
| 447 | int currX = *data++; |
| 448 | int currY = *data++; |
| 449 | |
| 450 | o.x_offset = c->font->em_scale_x (markX) - c->font->em_scale_x (currX); |
| 451 | o.y_offset = c->font->em_scale_y (markY) - c->font->em_scale_y (currY); |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 452 | } |
| 453 | break; |
| 454 | } |
Behdad Esfahbod | b6bc0d4 | 2018-10-11 01:17:57 -0400 | [diff] [blame] | 455 | o.attach_type() = ATTACH_TYPE_MARK; |
| 456 | o.attach_chain() = (int) mark - (int) buffer->idx; |
| 457 | buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | if (flags & Mark) |
| 461 | { |
| 462 | mark_set = true; |
| 463 | mark = buffer->idx; |
| 464 | } |
| 465 | |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | private: |
| 470 | hb_aat_apply_context_t *c; |
| 471 | unsigned int action_type; |
| 472 | const HBUINT16 *ankrData; |
| 473 | bool mark_set; |
| 474 | unsigned int mark; |
| 475 | }; |
| 476 | |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 477 | inline bool apply (hb_aat_apply_context_t *c) const |
| 478 | { |
| 479 | TRACE_APPLY (this); |
| 480 | |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 481 | driver_context_t dc (this, c); |
| 482 | |
Ebrahim Byagowi | b053cab | 2018-10-30 18:41:34 +0330 | [diff] [blame] | 483 | StateTableDriver<MorxTypes, EntryData> driver (machine, c->buffer, c->font->face); |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 484 | driver.drive (&dc); |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 485 | |
| 486 | return_trace (true); |
| 487 | } |
| 488 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 489 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 490 | { |
| 491 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 492 | /* The rest of array sanitizations are done at run-time. */ |
Behdad Esfahbod | 40f2b93 | 2018-10-14 14:56:32 -0700 | [diff] [blame] | 493 | return_trace (likely (c->check_struct (this) && |
| 494 | machine.sanitize (c))); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | protected: |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 498 | KerxSubTableHeader header; |
Ebrahim Byagowi | b053cab | 2018-10-30 18:41:34 +0330 | [diff] [blame] | 499 | StateTable<MorxTypes, EntryData> |
| 500 | machine; |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 501 | HBUINT32 flags; |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 502 | public: |
Behdad Esfahbod | 28f0367 | 2018-10-11 00:12:49 -0400 | [diff] [blame] | 503 | DEFINE_SIZE_STATIC (32); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 504 | }; |
| 505 | |
| 506 | struct KerxSubTableFormat6 |
| 507 | { |
Behdad Esfahbod | 22955b2 | 2018-10-10 19:58:20 -0400 | [diff] [blame] | 508 | enum Flags |
| 509 | { |
| 510 | ValuesAreLong = 0x00000001, |
| 511 | }; |
| 512 | |
| 513 | inline bool is_long (void) const { return flags & ValuesAreLong; } |
| 514 | |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 515 | inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 516 | hb_aat_apply_context_t *c) const |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 517 | { |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 518 | unsigned int num_glyphs = c->sanitizer.get_num_glyphs (); |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 519 | if (is_long ()) |
| 520 | { |
| 521 | const U::Long &t = u.l; |
| 522 | unsigned int l = (this+t.rowIndexTable).get_value_or_null (left, num_glyphs); |
| 523 | unsigned int r = (this+t.columnIndexTable).get_value_or_null (right, num_glyphs); |
| 524 | unsigned int offset = l + r; |
Behdad Esfahbod | fc45e69 | 2018-10-13 11:39:12 -0400 | [diff] [blame] | 525 | if (unlikely (offset < l)) return 0; /* Addition overflow. */ |
| 526 | if (unlikely (hb_unsigned_mul_overflows (offset, sizeof (FWORD32)))) return 0; |
Behdad Esfahbod | 71f76f2 | 2018-10-13 13:36:27 -0400 | [diff] [blame] | 527 | const FWORD32 *v = &StructAtOffset<FWORD32> (&(this+t.array), offset * sizeof (FWORD32)); |
| 528 | if (unlikely (!v->sanitize (&c->sanitizer))) return 0; |
Behdad Esfahbod | 29d8775 | 2018-10-19 16:06:54 -0700 | [diff] [blame] | 529 | return kerxTupleKern (*v, header.tupleCount, &(this+vector), c); |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 530 | } |
| 531 | else |
| 532 | { |
| 533 | const U::Short &t = u.s; |
| 534 | unsigned int l = (this+t.rowIndexTable).get_value_or_null (left, num_glyphs); |
| 535 | unsigned int r = (this+t.columnIndexTable).get_value_or_null (right, num_glyphs); |
| 536 | unsigned int offset = l + r; |
Behdad Esfahbod | 71f76f2 | 2018-10-13 13:36:27 -0400 | [diff] [blame] | 537 | const FWORD *v = &StructAtOffset<FWORD> (&(this+t.array), offset * sizeof (FWORD)); |
| 538 | if (unlikely (!v->sanitize (&c->sanitizer))) return 0; |
Behdad Esfahbod | 29d8775 | 2018-10-19 16:06:54 -0700 | [diff] [blame] | 539 | return kerxTupleKern (*v, header.tupleCount, &(this+vector), c); |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 543 | inline bool apply (hb_aat_apply_context_t *c) const |
| 544 | { |
| 545 | TRACE_APPLY (this); |
| 546 | |
Behdad Esfahbod | 60f86d3 | 2018-10-10 18:10:05 -0400 | [diff] [blame] | 547 | if (!c->plan->requested_kerning) |
| 548 | return false; |
| 549 | |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 550 | accelerator_t accel (*this, c); |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 551 | hb_kern_machine_t<accelerator_t> machine (accel); |
| 552 | machine.kern (c->font, c->buffer, c->plan->kern_mask); |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 553 | |
| 554 | return_trace (true); |
| 555 | } |
| 556 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 557 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 558 | { |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 559 | TRACE_SANITIZE (this); |
Ebrahim Byagowi | 2a4cdfa | 2018-04-20 21:12:58 +0430 | [diff] [blame] | 560 | return_trace (likely (c->check_struct (this) && |
Behdad Esfahbod | 11703af | 2018-10-15 21:16:58 -0700 | [diff] [blame] | 561 | (is_long () ? |
| 562 | ( |
| 563 | u.l.rowIndexTable.sanitize (c, this) && |
| 564 | u.l.columnIndexTable.sanitize (c, this) && |
| 565 | c->check_range (this, u.l.array) |
| 566 | ) : ( |
| 567 | u.s.rowIndexTable.sanitize (c, this) && |
| 568 | u.s.columnIndexTable.sanitize (c, this) && |
| 569 | c->check_range (this, u.s.array) |
Behdad Esfahbod | 29d8775 | 2018-10-19 16:06:54 -0700 | [diff] [blame] | 570 | )) && |
| 571 | (header.tupleCount == 0 || |
| 572 | c->check_range (this, vector)))); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 573 | } |
| 574 | |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 575 | struct accelerator_t |
| 576 | { |
| 577 | const KerxSubTableFormat6 &table; |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 578 | hb_aat_apply_context_t *c; |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 579 | |
| 580 | inline accelerator_t (const KerxSubTableFormat6 &table_, |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 581 | hb_aat_apply_context_t *c_) : |
| 582 | table (table_), c (c_) {} |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 583 | |
| 584 | inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const |
Behdad Esfahbod | 5733113 | 2018-10-13 12:16:12 -0400 | [diff] [blame] | 585 | { return table.get_kerning (left, right, c); } |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 586 | }; |
| 587 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 588 | protected: |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 589 | KerxSubTableHeader header; |
| 590 | HBUINT32 flags; |
| 591 | HBUINT16 rowCount; |
| 592 | HBUINT16 columnCount; |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 593 | union U |
Behdad Esfahbod | 22955b2 | 2018-10-10 19:58:20 -0400 | [diff] [blame] | 594 | { |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 595 | struct Long |
Behdad Esfahbod | 22955b2 | 2018-10-10 19:58:20 -0400 | [diff] [blame] | 596 | { |
Behdad Esfahbod | 87205ef | 2018-10-16 15:40:44 -0700 | [diff] [blame] | 597 | LOffsetTo<Lookup<HBUINT32>, false> rowIndexTable; |
| 598 | LOffsetTo<Lookup<HBUINT32>, false> columnIndexTable; |
| 599 | LOffsetTo<UnsizedArrayOf<FWORD32>, false> array; |
Behdad Esfahbod | 22955b2 | 2018-10-10 19:58:20 -0400 | [diff] [blame] | 600 | } l; |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 601 | struct Short |
| 602 | { |
Behdad Esfahbod | 87205ef | 2018-10-16 15:40:44 -0700 | [diff] [blame] | 603 | LOffsetTo<Lookup<HBUINT16>, false> rowIndexTable; |
| 604 | LOffsetTo<Lookup<HBUINT16>, false> columnIndexTable; |
| 605 | LOffsetTo<UnsizedArrayOf<FWORD>, false> array; |
Behdad Esfahbod | ab1f30b | 2018-10-10 20:10:20 -0400 | [diff] [blame] | 606 | } s; |
Behdad Esfahbod | 22955b2 | 2018-10-10 19:58:20 -0400 | [diff] [blame] | 607 | } u; |
Behdad Esfahbod | 29d8775 | 2018-10-19 16:06:54 -0700 | [diff] [blame] | 608 | LOffsetTo<UnsizedArrayOf<FWORD>, false> vector; |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 609 | public: |
Behdad Esfahbod | 29d8775 | 2018-10-19 16:06:54 -0700 | [diff] [blame] | 610 | DEFINE_SIZE_STATIC (36); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 611 | }; |
| 612 | |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 613 | struct KerxTable |
| 614 | { |
Behdad Esfahbod | e640f3a | 2018-10-09 08:28:07 -0400 | [diff] [blame] | 615 | friend struct kerx; |
Behdad Esfahbod | 80e3102 | 2018-10-08 22:41:08 -0400 | [diff] [blame] | 616 | |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 617 | inline unsigned int get_size (void) const { return u.header.length; } |
| 618 | inline unsigned int get_type (void) const { return u.header.coverage & SubtableType; } |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 619 | |
| 620 | enum Coverage |
| 621 | { |
| 622 | Vertical = 0x80000000, /* Set if table has vertical kerning values. */ |
| 623 | CrossStream = 0x40000000, /* Set if table has cross-stream kerning values. */ |
| 624 | Variation = 0x20000000, /* Set if table has variation kerning values. */ |
Behdad Esfahbod | 362d324 | 2018-10-09 23:27:00 -0400 | [diff] [blame] | 625 | Backwards = 0x10000000, /* If clear, process the glyphs forwards, that |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 626 | * is, from first to last in the glyph stream. |
| 627 | * If we, process them from last to first. |
| 628 | * This flag only applies to state-table based |
| 629 | * 'kerx' subtables (types 1 and 4). */ |
| 630 | Reserved = 0x0FFFFF00, /* Reserved, set to zero. */ |
| 631 | SubtableType = 0x000000FF, /* Subtable type. */ |
| 632 | }; |
| 633 | |
| 634 | template <typename context_t> |
| 635 | inline typename context_t::return_t dispatch (context_t *c) const |
| 636 | { |
| 637 | unsigned int subtable_type = get_type (); |
| 638 | TRACE_DISPATCH (this, subtable_type); |
| 639 | switch (subtable_type) { |
Behdad Esfahbod | bfafe20 | 2018-11-06 12:11:45 -0500 | [diff] [blame^] | 640 | case 0: return_trace (c->dispatch (u.format0)); |
| 641 | case 1: return_trace (c->dispatch (u.format1)); |
| 642 | case 2: return_trace (c->dispatch (u.format2)); |
| 643 | case 4: return_trace (c->dispatch (u.format4)); |
| 644 | case 6: return_trace (c->dispatch (u.format6)); |
| 645 | default: return_trace (c->default_return_value ()); |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 646 | } |
| 647 | } |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 648 | |
| 649 | inline bool sanitize (hb_sanitize_context_t *c) const |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 650 | { |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 651 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 652 | if (!u.header.sanitize (c) || |
| 653 | !c->check_range (this, u.header.length)) |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 654 | return_trace (false); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 655 | |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 656 | return_trace (dispatch (c)); |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 657 | } |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 658 | |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 659 | protected: |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 660 | union { |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 661 | KerxSubTableHeader header; |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 662 | KerxSubTableFormat0 format0; |
| 663 | KerxSubTableFormat1 format1; |
| 664 | KerxSubTableFormat2 format2; |
| 665 | KerxSubTableFormat4 format4; |
| 666 | KerxSubTableFormat6 format6; |
| 667 | } u; |
| 668 | public: |
| 669 | DEFINE_SIZE_MIN (12); |
| 670 | }; |
| 671 | |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 672 | |
| 673 | /* |
| 674 | * The 'kerx' Table |
| 675 | */ |
| 676 | |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 677 | struct kerx |
| 678 | { |
Ebrahim Byagowi | 158f281 | 2018-03-26 12:04:30 +0430 | [diff] [blame] | 679 | static const hb_tag_t tableTag = HB_AAT_TAG_kerx; |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 680 | |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 681 | inline bool has_data (void) const { return version != 0; } |
| 682 | |
| 683 | inline void apply (hb_aat_apply_context_t *c) const |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 684 | { |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 685 | c->set_lookup_index (0); |
| 686 | const KerxTable *table = &firstTable; |
| 687 | unsigned int count = tableCount; |
| 688 | for (unsigned int i = 0; i < count; i++) |
| 689 | { |
Behdad Esfahbod | 80e3102 | 2018-10-08 22:41:08 -0400 | [diff] [blame] | 690 | bool reverse; |
| 691 | |
Behdad Esfahbod | 29d8775 | 2018-10-19 16:06:54 -0700 | [diff] [blame] | 692 | if (table->u.header.coverage & (KerxTable::CrossStream)) |
| 693 | goto skip; /* We do NOT handle cross-stream. */ |
Behdad Esfahbod | 44f09af | 2018-10-10 17:32:32 -0400 | [diff] [blame] | 694 | |
Behdad Esfahbod | 80e3102 | 2018-10-08 22:41:08 -0400 | [diff] [blame] | 695 | if (HB_DIRECTION_IS_VERTICAL (c->buffer->props.direction) != |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 696 | bool (table->u.header.coverage & KerxTable::Vertical)) |
Behdad Esfahbod | 44f09af | 2018-10-10 17:32:32 -0400 | [diff] [blame] | 697 | goto skip; |
Behdad Esfahbod | 80e3102 | 2018-10-08 22:41:08 -0400 | [diff] [blame] | 698 | |
Behdad Esfahbod | 5d34164 | 2018-10-10 18:14:41 -0400 | [diff] [blame] | 699 | reverse = bool (table->u.header.coverage & KerxTable::Backwards) != |
Behdad Esfahbod | 80e3102 | 2018-10-08 22:41:08 -0400 | [diff] [blame] | 700 | HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction); |
| 701 | |
| 702 | if (!c->buffer->message (c->font, "start kerx subtable %d", c->lookup_index)) |
Behdad Esfahbod | 44f09af | 2018-10-10 17:32:32 -0400 | [diff] [blame] | 703 | goto skip; |
Behdad Esfahbod | 80e3102 | 2018-10-08 22:41:08 -0400 | [diff] [blame] | 704 | |
| 705 | if (reverse) |
Behdad Esfahbod | 44f09af | 2018-10-10 17:32:32 -0400 | [diff] [blame] | 706 | c->buffer->reverse (); |
Behdad Esfahbod | 80e3102 | 2018-10-08 22:41:08 -0400 | [diff] [blame] | 707 | |
Behdad Esfahbod | 948f59a | 2018-10-09 23:07:47 -0400 | [diff] [blame] | 708 | c->sanitizer.set_object (*table); |
| 709 | |
Behdad Esfahbod | ad76307 | 2018-10-09 22:57:00 -0400 | [diff] [blame] | 710 | /* XXX Reverse-kern is not working yet... |
| 711 | * hb_kern_machine_t would need to know that it's reverse-kerning. |
| 712 | * Or better yet, make it work in reverse as well, so we don't have |
| 713 | * to reverse and reverse back? */ |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 714 | table->dispatch (c); |
Behdad Esfahbod | 80e3102 | 2018-10-08 22:41:08 -0400 | [diff] [blame] | 715 | |
| 716 | if (reverse) |
Behdad Esfahbod | 44f09af | 2018-10-10 17:32:32 -0400 | [diff] [blame] | 717 | c->buffer->reverse (); |
Behdad Esfahbod | 80e3102 | 2018-10-08 22:41:08 -0400 | [diff] [blame] | 718 | |
| 719 | (void) c->buffer->message (c->font, "end kerx subtable %d", c->lookup_index); |
| 720 | |
| 721 | skip: |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 722 | table = &StructAfter<KerxTable> (*table); |
| 723 | } |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 727 | { |
| 728 | TRACE_SANITIZE (this); |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 729 | if (!version.sanitize (c) || version < 2 || |
| 730 | !tableCount.sanitize (c)) |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 731 | return_trace (false); |
| 732 | |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 733 | const KerxTable *table = &firstTable; |
| 734 | unsigned int count = tableCount; |
| 735 | for (unsigned int i = 0; i < count; i++) |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 736 | { |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 737 | if (!table->sanitize (c)) |
| 738 | return_trace (false); |
Ebrahim Byagowi | b73a5a1 | 2018-03-02 00:07:26 +0330 | [diff] [blame] | 739 | table = &StructAfter<KerxTable> (*table); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | return_trace (true); |
| 743 | } |
| 744 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 745 | protected: |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 746 | HBUINT16 version; /* The version number of the extended kerning table |
| 747 | * (currently 2, 3, or 4). */ |
| 748 | HBUINT16 unused; /* Set to 0. */ |
| 749 | HBUINT32 tableCount; /* The number of subtables included in the extended kerning |
| 750 | * table. */ |
| 751 | KerxTable firstTable; /* Subtables. */ |
| 752 | /*subtableGlyphCoverageArray*/ /* Only if version >= 3. We don't use. */ |
| 753 | |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 754 | public: |
Behdad Esfahbod | fdce1e1 | 2018-10-07 14:01:33 -0400 | [diff] [blame] | 755 | DEFINE_SIZE_MIN (8); |
Ebrahim Byagowi | 79756c9 | 2018-02-19 03:17:44 +0330 | [diff] [blame] | 756 | }; |
| 757 | |
| 758 | } /* namespace AAT */ |
| 759 | |
| 760 | |
| 761 | #endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */ |