Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007,2008,2009 Red Hat, Inc. |
| 3 | * |
| 4 | * This is part of HarfBuzz, an OpenType Layout engine library. |
| 5 | * |
| 6 | * Permission is hereby granted, without written agreement and without |
| 7 | * license or royalty fees, to use, copy, modify, and distribute this |
| 8 | * software and its documentation for any purpose, provided that the |
| 9 | * above copyright notice and the following two paragraphs appear in |
| 10 | * all copies of this software. |
| 11 | * |
| 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 16 | * DAMAGE. |
| 17 | * |
| 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 23 | * |
| 24 | * Red Hat Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
| 27 | #ifndef HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H |
| 28 | #define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H |
| 29 | |
Behdad Esfahbod | 88a5f5a | 2009-05-25 03:39:11 -0400 | [diff] [blame] | 30 | #include "hb-buffer-private.h" |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 31 | #include "hb-ot-layout-gdef-private.h" |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 32 | |
Behdad Esfahbod | 6f20f72 | 2009-05-17 20:28:01 -0400 | [diff] [blame] | 33 | |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 34 | #define APPLY_ARG_DEF \ |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 35 | hb_ot_layout_t *layout, \ |
| 36 | hb_buffer_t *buffer, \ |
| 37 | unsigned int context_length HB_GNUC_UNUSED, \ |
| 38 | unsigned int nesting_level_left HB_GNUC_UNUSED, \ |
Behdad Esfahbod | ecf17e8 | 2009-05-17 09:34:41 -0400 | [diff] [blame] | 39 | unsigned int lookup_flag, \ |
| 40 | unsigned int property HB_GNUC_UNUSED /* propety of first glyph */ |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 41 | #define APPLY_ARG \ |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 42 | layout, \ |
| 43 | buffer, \ |
| 44 | context_length, \ |
| 45 | nesting_level_left, \ |
Behdad Esfahbod | ecf17e8 | 2009-05-17 09:34:41 -0400 | [diff] [blame] | 46 | lookup_flag, \ |
| 47 | property |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 48 | |
| 49 | |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 50 | typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, char *data); |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 51 | typedef bool (*apply_lookup_func_t) (APPLY_ARG_DEF, unsigned int lookup_index); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 52 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 53 | struct ContextFuncs |
| 54 | { |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 55 | match_func_t match; |
| 56 | apply_lookup_func_t apply; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 57 | }; |
| 58 | |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 59 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 60 | static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, char *data) |
| 61 | { |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 62 | return glyph_id == value; |
| 63 | } |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 64 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 65 | static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, char *data) |
| 66 | { |
Behdad Esfahbod | 4497af0 | 2009-05-25 03:20:18 -0400 | [diff] [blame] | 67 | const ClassDef &class_def = *(const ClassDef *)data; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 68 | return class_def.get_class (glyph_id) == value; |
| 69 | } |
| 70 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 71 | static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, char *data) |
| 72 | { |
Behdad Esfahbod | 6b54c5d | 2009-05-18 18:30:25 -0400 | [diff] [blame] | 73 | const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 74 | return (data+coverage) (glyph_id) != NOT_COVERED; |
| 75 | } |
| 76 | |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 77 | |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 78 | static inline bool match_input (APPLY_ARG_DEF, |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 79 | unsigned int count, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 80 | const USHORT input[], /* Array of input values--start with second glyph */ |
| 81 | match_func_t match_func, |
| 82 | char *match_data, |
| 83 | unsigned int *context_length_out) |
| 84 | { |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 85 | unsigned int i, j; |
Behdad Esfahbod | 7cff75b | 2009-05-18 04:09:05 -0400 | [diff] [blame] | 86 | unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length); |
| 87 | if (HB_UNLIKELY (buffer->in_pos + count > end)) |
| 88 | return false; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 89 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 90 | for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++) |
| 91 | { |
Behdad Esfahbod | 4189b92 | 2009-05-26 17:31:56 -0400 | [diff] [blame^] | 92 | while (_hb_ot_layout_skip_mark (layout, IN_INFO (j), lookup_flag, NULL)) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 93 | { |
Behdad Esfahbod | 7cff75b | 2009-05-18 04:09:05 -0400 | [diff] [blame] | 94 | if (HB_UNLIKELY (j + count - i == end)) |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 95 | return false; |
| 96 | j++; |
| 97 | } |
| 98 | |
Behdad Esfahbod | 4189b92 | 2009-05-26 17:31:56 -0400 | [diff] [blame^] | 99 | if (HB_LIKELY (!match_func (IN_GLYPH (j), input[i - 1], match_data))) |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 103 | *context_length_out = j - buffer->in_pos; |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 108 | static inline bool match_backtrack (APPLY_ARG_DEF, |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 109 | unsigned int count, |
| 110 | const USHORT backtrack[], |
| 111 | match_func_t match_func, |
| 112 | char *match_data) |
| 113 | { |
Behdad Esfahbod | 7cff75b | 2009-05-18 04:09:05 -0400 | [diff] [blame] | 114 | if (HB_UNLIKELY (buffer->out_pos < count)) |
| 115 | return false; |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 116 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 117 | for (unsigned int i = 0, j = buffer->out_pos - 1; i < count; i++, j--) |
| 118 | { |
Behdad Esfahbod | 4189b92 | 2009-05-26 17:31:56 -0400 | [diff] [blame^] | 119 | while (_hb_ot_layout_skip_mark (layout, OUT_INFO (j), lookup_flag, NULL)) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 120 | { |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 121 | if (HB_UNLIKELY (j + 1 == count - i)) |
| 122 | return false; |
| 123 | j--; |
| 124 | } |
| 125 | |
Behdad Esfahbod | 4189b92 | 2009-05-26 17:31:56 -0400 | [diff] [blame^] | 126 | if (HB_LIKELY (!match_func (OUT_GLYPH (j), backtrack[i], match_data))) |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 127 | return false; |
| 128 | } |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 133 | static inline bool match_lookahead (APPLY_ARG_DEF, |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 134 | unsigned int count, |
| 135 | const USHORT lookahead[], |
| 136 | match_func_t match_func, |
| 137 | char *match_data, |
| 138 | unsigned int offset) |
| 139 | { |
| 140 | unsigned int i, j; |
Behdad Esfahbod | 7cff75b | 2009-05-18 04:09:05 -0400 | [diff] [blame] | 141 | unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length); |
| 142 | if (HB_UNLIKELY (buffer->in_pos + offset + count > end)) |
| 143 | return false; |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 144 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 145 | for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++) |
| 146 | { |
Behdad Esfahbod | 4189b92 | 2009-05-26 17:31:56 -0400 | [diff] [blame^] | 147 | while (_hb_ot_layout_skip_mark (layout, OUT_INFO (j), lookup_flag, NULL)) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 148 | { |
Behdad Esfahbod | 7cff75b | 2009-05-18 04:09:05 -0400 | [diff] [blame] | 149 | if (HB_UNLIKELY (j + count - i == end)) |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 150 | return false; |
| 151 | j++; |
| 152 | } |
| 153 | |
Behdad Esfahbod | 4189b92 | 2009-05-26 17:31:56 -0400 | [diff] [blame^] | 154 | if (HB_LIKELY (!match_func (IN_GLYPH (j), lookahead[i], match_data))) |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 155 | return false; |
| 156 | } |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 162 | struct LookupRecord |
| 163 | { |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 164 | USHORT sequenceIndex; /* Index into current glyph |
| 165 | * sequence--first glyph = 0 */ |
| 166 | USHORT lookupListIndex; /* Lookup to apply to that |
| 167 | * position--zero--based */ |
| 168 | }; |
| 169 | ASSERT_SIZE (LookupRecord, 4); |
| 170 | |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 171 | static inline bool apply_lookup (APPLY_ARG_DEF, |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 172 | unsigned int count, /* Including the first glyph */ |
| 173 | unsigned int lookupCount, |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 174 | const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */ |
| 175 | apply_lookup_func_t apply_func) |
| 176 | { |
Behdad Esfahbod | e73a0c2 | 2009-05-18 04:15:25 -0400 | [diff] [blame] | 177 | unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length); |
| 178 | if (HB_UNLIKELY (buffer->in_pos + count > end)) |
| 179 | return false; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 180 | |
Behdad Esfahbod | 7cff75b | 2009-05-18 04:09:05 -0400 | [diff] [blame] | 181 | /* TODO We don't support lookupRecord arrays that are not increasing: |
| 182 | * Should be easy for in_place ones at least. */ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 183 | for (unsigned int i = 0; i < count; i++) |
| 184 | { |
Behdad Esfahbod | 4189b92 | 2009-05-26 17:31:56 -0400 | [diff] [blame^] | 185 | while (_hb_ot_layout_skip_mark (layout, IN_CURINFO (), lookup_flag, NULL)) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 186 | { |
Behdad Esfahbod | e73a0c2 | 2009-05-18 04:15:25 -0400 | [diff] [blame] | 187 | if (HB_UNLIKELY (buffer->in_pos == end)) |
| 188 | return true; |
| 189 | /* No lookup applied for this index */ |
| 190 | _hb_buffer_next_glyph (buffer); |
| 191 | } |
| 192 | |
Behdad Esfahbod | 47958de | 2009-05-18 04:17:47 -0400 | [diff] [blame] | 193 | if (lookupCount && i == lookupRecord->sequenceIndex) |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 194 | { |
| 195 | unsigned int old_pos = buffer->in_pos; |
| 196 | |
| 197 | /* Apply a lookup */ |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 198 | bool done = apply_func (APPLY_ARG, lookupRecord->lookupListIndex); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 199 | |
Behdad Esfahbod | 47958de | 2009-05-18 04:17:47 -0400 | [diff] [blame] | 200 | lookupRecord++; |
| 201 | lookupCount--; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 202 | i += buffer->in_pos - old_pos; |
Behdad Esfahbod | 2e8fb6c | 2009-05-18 04:37:37 -0400 | [diff] [blame] | 203 | if (HB_UNLIKELY (buffer->in_pos == end)) |
| 204 | return true; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 205 | |
| 206 | if (!done) |
| 207 | goto not_applied; |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | not_applied: |
| 212 | /* No lookup applied for this index */ |
| 213 | _hb_buffer_next_glyph (buffer); |
| 214 | i++; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return true; |
| 219 | } |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 220 | |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 221 | |
| 222 | /* Contextual lookups */ |
| 223 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 224 | struct ContextLookupContext |
| 225 | { |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 226 | ContextFuncs funcs; |
| 227 | char *match_data; |
| 228 | }; |
| 229 | |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 230 | static inline bool context_lookup (APPLY_ARG_DEF, |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 231 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 232 | const USHORT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 233 | unsigned int lookupCount, |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 234 | const LookupRecord lookupRecord[], |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 235 | ContextLookupContext &context) |
| 236 | { |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 237 | return match_input (APPLY_ARG, |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 238 | inputCount, input, |
| 239 | context.funcs.match, context.match_data, |
| 240 | &context_length) && |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 241 | apply_lookup (APPLY_ARG, |
Behdad Esfahbod | f14c2b7 | 2009-05-18 02:36:18 -0400 | [diff] [blame] | 242 | inputCount, |
| 243 | lookupCount, lookupRecord, |
| 244 | context.funcs.apply); |
| 245 | } |
| 246 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 247 | struct Rule |
| 248 | { |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 249 | friend struct RuleSet; |
| 250 | |
| 251 | private: |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 252 | inline bool apply (APPLY_ARG_DEF, ContextLookupContext &context) const |
| 253 | { |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 254 | const LookupRecord *lookupRecord = (const LookupRecord *) |
| 255 | ((const char *) input + |
| 256 | sizeof (input[0]) * (inputCount ? inputCount - 1 : 0)); |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 257 | return context_lookup (APPLY_ARG, |
Behdad Esfahbod | 969afd7 | 2009-05-18 05:47:47 -0400 | [diff] [blame] | 258 | inputCount, input, |
| 259 | lookupCount, lookupRecord, |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 260 | context); |
| 261 | } |
| 262 | |
| 263 | private: |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 264 | USHORT inputCount; /* Total number of glyphs in input |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 265 | * glyph sequence--includes the first |
| 266 | * glyph */ |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 267 | USHORT lookupCount; /* Number of LookupRecords */ |
| 268 | USHORT input[]; /* Array of match inputs--start with |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 269 | * second glyph */ |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 270 | LookupRecord lookupRecordX[]; /* Array of LookupRecords--in |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 271 | * design order */ |
| 272 | }; |
| 273 | ASSERT_SIZE (Rule, 4); |
| 274 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 275 | struct RuleSet |
| 276 | { |
| 277 | inline bool apply (APPLY_ARG_DEF, ContextLookupContext &context) const |
| 278 | { |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 279 | unsigned int num_rules = rule.len; |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 280 | for (unsigned int i = 0; i < num_rules; i++) |
| 281 | { |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 282 | if ((this+rule[i]).apply (APPLY_ARG, context)) |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 283 | return true; |
| 284 | } |
| 285 | |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | private: |
| 290 | OffsetArrayOf<Rule> |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 291 | rule; /* Array of Rule tables |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 292 | * ordered by preference */ |
| 293 | }; |
| 294 | |
| 295 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 296 | struct ContextFormat1 |
| 297 | { |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 298 | friend struct Context; |
| 299 | |
| 300 | private: |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 301 | inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const |
| 302 | { |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 303 | unsigned int index = (this+coverage) (IN_CURGLYPH ()); |
Behdad Esfahbod | 4acaffd | 2009-05-18 05:29:29 -0400 | [diff] [blame] | 304 | if (HB_LIKELY (index == NOT_COVERED)) |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 305 | return false; |
| 306 | |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 307 | const RuleSet &rule_set = this+ruleSet[index]; |
| 308 | struct ContextLookupContext context = { |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 309 | {match_glyph, apply_func}, |
| 310 | NULL |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 311 | }; |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 312 | return rule_set.apply (APPLY_ARG, context); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | private: |
| 316 | USHORT format; /* Format identifier--format = 1 */ |
| 317 | OffsetTo<Coverage> |
| 318 | coverage; /* Offset to Coverage table--from |
| 319 | * beginning of table */ |
| 320 | OffsetArrayOf<RuleSet> |
| 321 | ruleSet; /* Array of RuleSet tables |
| 322 | * ordered by Coverage Index */ |
| 323 | }; |
| 324 | ASSERT_SIZE (ContextFormat1, 6); |
| 325 | |
| 326 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 327 | struct ContextFormat2 |
| 328 | { |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 329 | friend struct Context; |
| 330 | |
| 331 | private: |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 332 | inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const |
| 333 | { |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 334 | unsigned int index = (this+coverage) (IN_CURGLYPH ()); |
Behdad Esfahbod | 4acaffd | 2009-05-18 05:29:29 -0400 | [diff] [blame] | 335 | if (HB_LIKELY (index == NOT_COVERED)) |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 336 | return false; |
| 337 | |
| 338 | const ClassDef &class_def = this+classDef; |
| 339 | index = class_def (IN_CURGLYPH ()); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 340 | const RuleSet &rule_set = this+ruleSet[index]; |
| 341 | /* LONGTERMTODO: Old code fetches glyph classes at most once and caches |
| 342 | * them across subrule lookups. Not sure it's worth it. |
| 343 | */ |
| 344 | struct ContextLookupContext context = { |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 345 | {match_class, apply_func}, |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 346 | (char *) &class_def |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 347 | }; |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 348 | return rule_set.apply (APPLY_ARG, context); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | private: |
| 352 | USHORT format; /* Format identifier--format = 2 */ |
| 353 | OffsetTo<Coverage> |
| 354 | coverage; /* Offset to Coverage table--from |
| 355 | * beginning of table */ |
| 356 | OffsetTo<ClassDef> |
| 357 | classDef; /* Offset to glyph ClassDef table--from |
| 358 | * beginning of table */ |
| 359 | OffsetArrayOf<RuleSet> |
| 360 | ruleSet; /* Array of RuleSet tables |
| 361 | * ordered by class */ |
| 362 | }; |
| 363 | ASSERT_SIZE (ContextFormat2, 8); |
| 364 | |
| 365 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 366 | struct ContextFormat3 |
| 367 | { |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 368 | friend struct Context; |
| 369 | |
| 370 | private: |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 371 | inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const |
| 372 | { |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 373 | unsigned int index = (this+coverage[0]) (IN_CURGLYPH ()); |
Behdad Esfahbod | 4acaffd | 2009-05-18 05:29:29 -0400 | [diff] [blame] | 374 | if (HB_LIKELY (index == NOT_COVERED)) |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 375 | return false; |
| 376 | |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 377 | const LookupRecord *lookupRecord = (const LookupRecord *) |
| 378 | ((const char *) coverage + |
| 379 | sizeof (coverage[0]) * glyphCount); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 380 | struct ContextLookupContext context = { |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 381 | {match_coverage, apply_func}, |
| 382 | (char *) this |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 383 | }; |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 384 | return context_lookup (APPLY_ARG, |
Behdad Esfahbod | 969afd7 | 2009-05-18 05:47:47 -0400 | [diff] [blame] | 385 | glyphCount, (const USHORT *) (coverage + 1), |
| 386 | lookupCount, lookupRecord, |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 387 | context); |
| 388 | } |
| 389 | |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 390 | private: |
| 391 | USHORT format; /* Format identifier--format = 3 */ |
| 392 | USHORT glyphCount; /* Number of glyphs in the input glyph |
| 393 | * sequence */ |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 394 | USHORT lookupCount; /* Number of LookupRecords */ |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 395 | OffsetTo<Coverage> |
| 396 | coverage[]; /* Array of offsets to Coverage |
| 397 | * table in glyph sequence order */ |
| 398 | LookupRecord lookupRecordX[]; /* Array of LookupRecords--in |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 399 | * design order */ |
| 400 | }; |
| 401 | ASSERT_SIZE (ContextFormat3, 6); |
| 402 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 403 | struct Context |
| 404 | { |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 405 | protected: |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 406 | bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const |
| 407 | { |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 408 | switch (u.format) { |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 409 | case 1: return u.format1->apply (APPLY_ARG, apply_func); |
| 410 | case 2: return u.format2->apply (APPLY_ARG, apply_func); |
| 411 | case 3: return u.format3->apply (APPLY_ARG, apply_func); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 412 | default:return false; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | private: |
| 417 | union { |
Behdad Esfahbod | f8dc67b | 2009-05-17 19:47:54 -0400 | [diff] [blame] | 418 | USHORT format; /* Format identifier */ |
| 419 | ContextFormat1 format1[]; |
| 420 | ContextFormat2 format2[]; |
| 421 | ContextFormat3 format3[]; |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 422 | } u; |
| 423 | }; |
Behdad Esfahbod | f8dc67b | 2009-05-17 19:47:54 -0400 | [diff] [blame] | 424 | ASSERT_SIZE (Context, 2); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 425 | |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 426 | |
| 427 | /* Chaining Contextual lookups */ |
| 428 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 429 | struct ChainContextLookupContext |
| 430 | { |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 431 | ContextFuncs funcs; |
| 432 | char *match_data[3]; |
| 433 | }; |
| 434 | |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 435 | static inline bool chain_context_lookup (APPLY_ARG_DEF, |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 436 | unsigned int backtrackCount, |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 437 | const USHORT backtrack[], |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 438 | unsigned int inputCount, /* Including the first glyph (not matched) */ |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 439 | const USHORT input[], /* Array of input values--start with second glyph */ |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 440 | unsigned int lookaheadCount, |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 441 | const USHORT lookahead[], |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 442 | unsigned int lookupCount, |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 443 | const LookupRecord lookupRecord[], |
| 444 | ChainContextLookupContext &context) |
| 445 | { |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 446 | /* First guess */ |
| 447 | if (HB_UNLIKELY (buffer->out_pos < backtrackCount || |
Behdad Esfahbod | 122f21f | 2009-05-18 04:21:53 -0400 | [diff] [blame] | 448 | buffer->in_pos + inputCount + lookaheadCount > buffer->in_length || |
| 449 | inputCount + lookaheadCount > context_length)) |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 450 | return false; |
| 451 | |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 452 | unsigned int offset; |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 453 | return match_backtrack (APPLY_ARG, |
Behdad Esfahbod | e072c24 | 2009-05-18 03:47:31 -0400 | [diff] [blame] | 454 | backtrackCount, backtrack, |
| 455 | context.funcs.match, context.match_data[0]) && |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 456 | match_input (APPLY_ARG, |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 457 | inputCount, input, |
| 458 | context.funcs.match, context.match_data[1], |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 459 | &offset) && |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 460 | match_lookahead (APPLY_ARG, |
Behdad Esfahbod | d0ba055 | 2009-05-18 03:56:39 -0400 | [diff] [blame] | 461 | lookaheadCount, lookahead, |
| 462 | context.funcs.match, context.match_data[2], |
| 463 | offset) && |
| 464 | (context_length = offset, true) && |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 465 | apply_lookup (APPLY_ARG, |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 466 | inputCount, |
| 467 | lookupCount, lookupRecord, |
| 468 | context.funcs.apply); |
| 469 | } |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 470 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 471 | struct ChainRule |
| 472 | { |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 473 | friend struct ChainRuleSet; |
| 474 | |
| 475 | private: |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 476 | inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &context) const |
| 477 | { |
Behdad Esfahbod | 515ce4c | 2009-05-26 13:08:00 -0400 | [diff] [blame] | 478 | const HeadlessArrayOf<USHORT> &input = *(const HeadlessArrayOf<USHORT>*) |
| 479 | ((const char *) &backtrack + backtrack.get_size ()); |
| 480 | const ArrayOf<USHORT> &lookahead = *(const ArrayOf<USHORT>*) |
| 481 | ((const char *) &input + input.get_size ()); |
| 482 | const ArrayOf<LookupRecord> &lookup = *(const ArrayOf<LookupRecord>*) |
| 483 | ((const char *) &lookahead + lookahead.get_size ()); |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 484 | return chain_context_lookup (APPLY_ARG, |
Behdad Esfahbod | 969afd7 | 2009-05-18 05:47:47 -0400 | [diff] [blame] | 485 | backtrack.len, backtrack.array, |
| 486 | input.len, input.array + 1, |
| 487 | lookahead.len, lookahead.array, |
| 488 | lookup.len, lookup.array, |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 489 | context); |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 490 | return false; |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 491 | } |
| 492 | |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 493 | |
| 494 | private: |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 495 | ArrayOf<USHORT> |
| 496 | backtrack; /* Array of backtracking values |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 497 | * (to be matched before the input |
| 498 | * sequence) */ |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 499 | HeadlessArrayOf<USHORT> |
| 500 | inputX; /* Array of input values (start with |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 501 | * second glyph) */ |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 502 | ArrayOf<USHORT> |
| 503 | lookaheadX; /* Array of lookahead values's (to be |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 504 | * matched after the input sequence) */ |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 505 | ArrayOf<LookupRecord> |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 506 | lookupX; /* Array of LookupRecords--in |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 507 | * design order) */ |
| 508 | }; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 509 | ASSERT_SIZE (ChainRule, 8); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 510 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 511 | struct ChainRuleSet |
| 512 | { |
| 513 | inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &context) const |
| 514 | { |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 515 | unsigned int num_rules = rule.len; |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 516 | for (unsigned int i = 0; i < num_rules; i++) |
| 517 | { |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 518 | if ((this+rule[i]).apply (APPLY_ARG, context)) |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 519 | return true; |
| 520 | } |
| 521 | |
| 522 | return false; |
| 523 | } |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 524 | |
| 525 | private: |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 526 | OffsetArrayOf<ChainRule> |
| 527 | rule; /* Array of ChainRule tables |
| 528 | * ordered by preference */ |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 529 | }; |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 530 | ASSERT_SIZE (ChainRuleSet, 2); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 531 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 532 | struct ChainContextFormat1 |
| 533 | { |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 534 | friend struct ChainContext; |
| 535 | |
| 536 | private: |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 537 | inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const |
| 538 | { |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 539 | unsigned int index = (this+coverage) (IN_CURGLYPH ()); |
Behdad Esfahbod | 4acaffd | 2009-05-18 05:29:29 -0400 | [diff] [blame] | 540 | if (HB_LIKELY (index == NOT_COVERED)) |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 541 | return false; |
| 542 | |
| 543 | const ChainRuleSet &rule_set = this+ruleSet[index]; |
| 544 | struct ChainContextLookupContext context = { |
| 545 | {match_glyph, apply_func}, |
| 546 | {NULL, NULL, NULL} |
| 547 | }; |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 548 | return rule_set.apply (APPLY_ARG, context); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 549 | } |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 550 | private: |
| 551 | USHORT format; /* Format identifier--format = 1 */ |
Behdad Esfahbod | 48f16ed | 2009-05-17 22:11:30 -0400 | [diff] [blame] | 552 | OffsetTo<Coverage> |
| 553 | coverage; /* Offset to Coverage table--from |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 554 | * beginning of table */ |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 555 | OffsetArrayOf<ChainRuleSet> |
| 556 | ruleSet; /* Array of ChainRuleSet tables |
| 557 | * ordered by Coverage Index */ |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 558 | }; |
| 559 | ASSERT_SIZE (ChainContextFormat1, 6); |
| 560 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 561 | struct ChainContextFormat2 |
| 562 | { |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 563 | friend struct ChainContext; |
| 564 | |
| 565 | private: |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 566 | inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const |
| 567 | { |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 568 | unsigned int index = (this+coverage) (IN_CURGLYPH ()); |
Behdad Esfahbod | 4acaffd | 2009-05-18 05:29:29 -0400 | [diff] [blame] | 569 | if (HB_LIKELY (index == NOT_COVERED)) |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 570 | return false; |
| 571 | |
| 572 | const ClassDef &backtrack_class_def = this+backtrackClassDef; |
| 573 | const ClassDef &input_class_def = this+inputClassDef; |
| 574 | const ClassDef &lookahead_class_def = this+lookaheadClassDef; |
| 575 | |
| 576 | index = input_class_def (IN_CURGLYPH ()); |
| 577 | const ChainRuleSet &rule_set = this+ruleSet[index]; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 578 | /* LONGTERMTODO: Old code fetches glyph classes at most once and caches |
| 579 | * them across subrule lookups. Not sure it's worth it. |
| 580 | */ |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 581 | struct ChainContextLookupContext context = { |
| 582 | {match_class, apply_func}, |
| 583 | {(char *) &backtrack_class_def, |
| 584 | (char *) &input_class_def, |
| 585 | (char *) &lookahead_class_def} |
| 586 | }; |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 587 | return rule_set.apply (APPLY_ARG, context); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | private: |
| 591 | USHORT format; /* Format identifier--format = 2 */ |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 592 | OffsetTo<Coverage> |
| 593 | coverage; /* Offset to Coverage table--from |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 594 | * beginning of table */ |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 595 | OffsetTo<ClassDef> |
| 596 | backtrackClassDef; /* Offset to glyph ClassDef table |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 597 | * containing backtrack sequence |
| 598 | * data--from beginning of table */ |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 599 | OffsetTo<ClassDef> |
| 600 | inputClassDef; /* Offset to glyph ClassDef |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 601 | * table containing input sequence |
| 602 | * data--from beginning of table */ |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 603 | OffsetTo<ClassDef> |
| 604 | lookaheadClassDef; /* Offset to glyph ClassDef table |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 605 | * containing lookahead sequence |
| 606 | * data--from beginning of table */ |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 607 | OffsetArrayOf<ChainRuleSet> |
| 608 | ruleSet; /* Array of ChainRuleSet tables |
| 609 | * ordered by class */ |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 610 | }; |
| 611 | ASSERT_SIZE (ChainContextFormat2, 12); |
| 612 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 613 | struct ChainContextFormat3 |
| 614 | { |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 615 | friend struct ChainContext; |
| 616 | |
| 617 | private: |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 618 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 619 | inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const |
| 620 | { |
Behdad Esfahbod | 515ce4c | 2009-05-26 13:08:00 -0400 | [diff] [blame] | 621 | const OffsetArrayOf<Coverage> &input = *(const OffsetArrayOf<Coverage>*) |
| 622 | ((const char *) &backtrack + backtrack.get_size ()); |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 623 | |
| 624 | unsigned int index = (this+input[0]) (IN_CURGLYPH ()); |
Behdad Esfahbod | 4acaffd | 2009-05-18 05:29:29 -0400 | [diff] [blame] | 625 | if (HB_LIKELY (index == NOT_COVERED)) |
Behdad Esfahbod | aa3d7ad | 2009-05-17 23:17:56 -0400 | [diff] [blame] | 626 | return false; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 627 | |
Behdad Esfahbod | 515ce4c | 2009-05-26 13:08:00 -0400 | [diff] [blame] | 628 | const OffsetArrayOf<Coverage> &lookahead = *(const OffsetArrayOf<Coverage>*) |
| 629 | ((const char *) &input + input.get_size ()); |
| 630 | const ArrayOf<LookupRecord> &lookup = *(const ArrayOf<LookupRecord>*) |
| 631 | ((const char *) &lookahead + lookahead.get_size ()); |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 632 | struct ChainContextLookupContext context = { |
| 633 | {match_coverage, apply_func}, |
| 634 | {(char *) this, (char *) this, (char *) this} |
| 635 | }; |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 636 | return chain_context_lookup (APPLY_ARG, |
Behdad Esfahbod | 969afd7 | 2009-05-18 05:47:47 -0400 | [diff] [blame] | 637 | backtrack.len, (USHORT *) backtrack.array, |
| 638 | input.len, (USHORT *) input.array, |
| 639 | lookahead.len, (USHORT *) lookahead.array, |
| 640 | lookup.len, lookup.array, |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 641 | context); |
| 642 | return false; |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | private: |
| 646 | USHORT format; /* Format identifier--format = 3 */ |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 647 | OffsetArrayOf<Coverage> |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 648 | backtrack; /* Array of coverage tables |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 649 | * in backtracking sequence, in glyph |
| 650 | * sequence order */ |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 651 | OffsetArrayOf<Coverage> |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 652 | inputX ; /* Array of coverage |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 653 | * tables in input sequence, in glyph |
| 654 | * sequence order */ |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 655 | OffsetArrayOf<Coverage> |
Behdad Esfahbod | 13ed440 | 2009-05-18 02:14:37 -0400 | [diff] [blame] | 656 | lookaheadX; /* Array of coverage tables |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 657 | * in lookahead sequence, in glyph |
| 658 | * sequence order */ |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 659 | ArrayOf<LookupRecord> |
Behdad Esfahbod | 02e1e5c | 2009-05-18 02:47:57 -0400 | [diff] [blame] | 660 | lookupX; /* Array of LookupRecords--in |
Behdad Esfahbod | dcb6b60 | 2009-05-18 01:49:57 -0400 | [diff] [blame] | 661 | * design order) */ |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 662 | }; |
| 663 | ASSERT_SIZE (ChainContextFormat3, 10); |
| 664 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 665 | struct ChainContext |
| 666 | { |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 667 | protected: |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 668 | bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const |
| 669 | { |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 670 | switch (u.format) { |
Behdad Esfahbod | eb0dfc8 | 2009-05-18 18:22:44 -0400 | [diff] [blame] | 671 | case 1: return u.format1->apply (APPLY_ARG, apply_func); |
| 672 | case 2: return u.format2->apply (APPLY_ARG, apply_func); |
| 673 | case 3: return u.format3->apply (APPLY_ARG, apply_func); |
Behdad Esfahbod | ca5290f | 2009-05-17 20:48:27 -0400 | [diff] [blame] | 674 | default:return false; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | private: |
| 679 | union { |
| 680 | USHORT format; /* Format identifier */ |
| 681 | ChainContextFormat1 format1[]; |
| 682 | ChainContextFormat2 format2[]; |
| 683 | ChainContextFormat3 format3[]; |
| 684 | } u; |
| 685 | }; |
| 686 | ASSERT_SIZE (ChainContext, 2); |
| 687 | |
| 688 | |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 689 | struct ExtensionFormat1 |
| 690 | { |
| 691 | friend struct Extension; |
| 692 | |
| 693 | private: |
| 694 | inline unsigned int get_type (void) const { return extensionLookupType; } |
| 695 | inline unsigned int get_offset (void) const { return (extensionOffset[0] << 16) + extensionOffset[1]; } |
| 696 | inline const LookupSubTable& get_subtable (void) const |
| 697 | { |
| 698 | unsigned int offset = get_offset (); |
| 699 | if (HB_UNLIKELY (!offset)) return Null(LookupSubTable); |
Behdad Esfahbod | 4497af0 | 2009-05-25 03:20:18 -0400 | [diff] [blame] | 700 | return *(LookupSubTable*)(((char *) this) + offset); |
Behdad Esfahbod | d468f9a | 2009-05-21 22:31:33 -0400 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | private: |
| 704 | USHORT format; /* Format identifier. Set to 1. */ |
| 705 | USHORT extensionLookupType; /* Lookup type of subtable referenced |
| 706 | * by ExtensionOffset (i.e. the |
| 707 | * extension subtable). */ |
| 708 | USHORT extensionOffset[2]; /* Offset to the extension subtable, |
| 709 | * of lookup type subtable. |
| 710 | * Defined as two shorts to avoid |
| 711 | * alignment requirements. */ |
| 712 | }; |
| 713 | ASSERT_SIZE (ExtensionFormat1, 8); |
| 714 | |
| 715 | struct Extension |
| 716 | { |
| 717 | inline unsigned int get_type (void) const |
| 718 | { |
| 719 | switch (u.format) { |
| 720 | case 1: return u.format1->get_type (); |
| 721 | default:return 0; |
| 722 | } |
| 723 | } |
| 724 | inline const LookupSubTable& get_subtable (void) const |
| 725 | { |
| 726 | switch (u.format) { |
| 727 | case 1: return u.format1->get_subtable (); |
| 728 | default:return Null(LookupSubTable); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | private: |
| 733 | union { |
| 734 | USHORT format; /* Format identifier */ |
| 735 | ExtensionFormat1 format1[]; |
| 736 | } u; |
| 737 | }; |
| 738 | ASSERT_SIZE (Extension, 2); |
| 739 | |
| 740 | |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 741 | /* |
| 742 | * GSUB/GPOS Common |
| 743 | */ |
| 744 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 745 | struct GSUBGPOS |
| 746 | { |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 747 | static const hb_tag_t GSUBTag = HB_TAG ('G','S','U','B'); |
| 748 | static const hb_tag_t GPOSTag = HB_TAG ('G','P','O','S'); |
| 749 | |
Behdad Esfahbod | 212aba6 | 2009-05-24 00:50:27 -0400 | [diff] [blame] | 750 | STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (GSUBGPOS, 1); |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 751 | |
| 752 | DEFINE_TAG_LIST_INTERFACE (Script, script ); /* get_script_count (), get_script (i), get_script_tag (i) */ |
| 753 | DEFINE_TAG_LIST_INTERFACE (Feature, feature); /* get_feature_count(), get_feature(i), get_feature_tag(i) */ |
| 754 | DEFINE_LIST_INTERFACE (Lookup, lookup ); /* get_lookup_count (), get_lookup (i) */ |
| 755 | |
| 756 | // LONGTERMTODO bsearch |
| 757 | DEFINE_TAG_FIND_INTERFACE (Script, script ); /* find_script_index (), get_script_by_tag (tag) */ |
| 758 | DEFINE_TAG_FIND_INTERFACE (Feature, feature); /* find_feature_index(), get_feature_by_tag(tag) */ |
| 759 | |
Behdad Esfahbod | 212aba6 | 2009-05-24 00:50:27 -0400 | [diff] [blame] | 760 | protected: |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 761 | FixedVersion version; /* Version of the GSUB/GPOS table--initially set |
Behdad Esfahbod | f45107f | 2009-05-17 20:13:02 -0400 | [diff] [blame] | 762 | * to 0x00010000 */ |
| 763 | OffsetTo<ScriptList> |
| 764 | scriptList; /* ScriptList table */ |
| 765 | OffsetTo<FeatureList> |
| 766 | featureList; /* FeatureList table */ |
| 767 | OffsetTo<LookupList> |
| 768 | lookupList; /* LookupList table */ |
| 769 | }; |
| 770 | ASSERT_SIZE (GSUBGPOS, 10); |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 771 | |
Behdad Esfahbod | 6f20f72 | 2009-05-17 20:28:01 -0400 | [diff] [blame] | 772 | |
Behdad Esfahbod | 66bf7ce | 2009-05-17 08:28:42 -0400 | [diff] [blame] | 773 | #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H */ |