hb-ot-layout-gsubgpos.hh 77.9 KB
Newer Older
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
B
Behdad Esfahbod 已提交
3
 * Copyright © 2010,2012  Google, Inc.
4
 *
B
Behdad Esfahbod 已提交
5
 *  This is part of HarfBuzz, a text shaping library.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Red Hat Author(s): Behdad Esfahbod
26
 * Google Author(s): Behdad Esfahbod
27 28
 */

29 30
#ifndef HB_OT_LAYOUT_GSUBGPOS_HH
#define HB_OT_LAYOUT_GSUBGPOS_HH
31

32 33 34
#include "hb.hh"
#include "hb-buffer.hh"
#include "hb-map.hh"
35
#include "hb-ot-face.hh"
36
#include "hb-ot-layout-gdef-table.hh"
37
#include "hb-set.hh"
38

39

40 41
namespace OT {

B
Behdad Esfahbod 已提交
42

B
Behdad Esfahbod 已提交
43 44
struct hb_closure_context_t :
       hb_dispatch_context_t<hb_closure_context_t, hb_void_t, HB_DEBUG_CLOSURE>
45
{
46
  inline const char *get_name (void) { return "CLOSURE"; }
47 48
  typedef return_t (*recurse_func_t) (hb_closure_context_t *c, unsigned int lookup_index);
  template <typename T>
49
  inline return_t dispatch (const T &obj) { obj.closure (this); return HB_VOID; }
50
  static return_t default_return_value (void) { return HB_VOID; }
51
  bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
52 53
  return_t recurse (unsigned int lookup_index)
  {
54
    if (unlikely (nesting_level_left == 0 || !recurse_func))
55
      return default_return_value ();
56 57 58 59

    nesting_level_left--;
    recurse_func (this, lookup_index);
    nesting_level_left++;
60
    return HB_VOID;
61 62
  }

63
  bool should_visit_lookup (unsigned int lookup_index)
64 65 66 67 68 69 70 71 72
  {
    if (is_lookup_done (lookup_index))
      return false;
    done_lookups->set (lookup_index, glyphs->get_population ());
    return true;
  }

  bool is_lookup_done (unsigned int lookup_index)
  {
73
    /* Have we visited this lookup with the current set of glyphs? */
74 75 76
    return done_lookups->get (lookup_index) == glyphs->get_population ();
  }

77
  hb_face_t *face;
78
  hb_set_t *glyphs;
79
  hb_auto_t<hb_set_t> out[1];
80
  recurse_func_t recurse_func;
81 82 83 84
  unsigned int nesting_level_left;
  unsigned int debug_depth;

  hb_closure_context_t (hb_face_t *face_,
85
			hb_set_t *glyphs_,
86
                        hb_map_t *done_lookups_,
87
		        unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
88 89
			  face (face_),
			  glyphs (glyphs_),
B
Behdad Esfahbod 已提交
90
			  recurse_func (nullptr),
91
			  nesting_level_left (nesting_level_left_),
92 93
			  debug_depth (0),
                          done_lookups (done_lookups_) {}
94

95 96 97 98 99
  ~hb_closure_context_t (void)
  {
    flush ();
  }

100
  void set_recurse_func (recurse_func_t func) { recurse_func = func; }
101

102 103 104 105 106 107
  void flush (void)
  {
    hb_set_union (glyphs, out);
    hb_set_clear (out);
  }

108 109
  private:
  hb_map_t *done_lookups;
110 111 112
};


B
Behdad Esfahbod 已提交
113 114
struct hb_would_apply_context_t :
       hb_dispatch_context_t<hb_would_apply_context_t, bool, HB_DEBUG_WOULD_APPLY>
115
{
116
  inline const char *get_name (void) { return "WOULD_APPLY"; }
B
Behdad Esfahbod 已提交
117
  template <typename T>
118
  inline return_t dispatch (const T &obj) { return obj.would_apply (this); }
B
Behdad Esfahbod 已提交
119
  static return_t default_return_value (void) { return false; }
120
  bool stop_sublookup_iteration (return_t r) const { return r; }
B
Behdad Esfahbod 已提交
121

122
  hb_face_t *face;
123
  const hb_codepoint_t *glyphs;
124
  unsigned int len;
125
  bool zero_context;
126 127 128
  unsigned int debug_depth;

  hb_would_apply_context_t (hb_face_t *face_,
129 130
			    const hb_codepoint_t *glyphs_,
			    unsigned int len_,
B
Behdad Esfahbod 已提交
131
			    bool zero_context_) :
132
			      face (face_),
133 134
			      glyphs (glyphs_),
			      len (len_),
135
			      zero_context (zero_context_),
136
			      debug_depth (0) {}
137 138 139
};


B
Behdad Esfahbod 已提交
140 141
struct hb_collect_glyphs_context_t :
       hb_dispatch_context_t<hb_collect_glyphs_context_t, hb_void_t, HB_DEBUG_COLLECT_GLYPHS>
142
{
143
  inline const char *get_name (void) { return "COLLECT_GLYPHS"; }
B
Behdad Esfahbod 已提交
144
  typedef return_t (*recurse_func_t) (hb_collect_glyphs_context_t *c, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
145
  template <typename T>
146
  inline return_t dispatch (const T &obj) { obj.collect_glyphs (this); return HB_VOID; }
147
  static return_t default_return_value (void) { return HB_VOID; }
148
  bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
B
Behdad Esfahbod 已提交
149 150
  return_t recurse (unsigned int lookup_index)
  {
B
Behdad Esfahbod 已提交
151 152 153
    if (unlikely (nesting_level_left == 0 || !recurse_func))
      return default_return_value ();

B
Behdad Esfahbod 已提交
154
    /* Note that GPOS sets recurse_func to nullptr already, so it doesn't get
155
     * past the previous check.  For GSUB, we only want to collect the output
156 157 158 159 160 161 162
     * glyphs in the recursion.  If output is not requested, we can go home now.
     *
     * Note further, that the above is not exactly correct.  A recursed lookup
     * is allowed to match input that is not matched in the context, but that's
     * not how most fonts are built.  It's possible to relax that and recurse
     * with all sets here if it proves to be an issue.
     */
163 164

    if (output == hb_set_get_empty ())
165
      return HB_VOID;
166

167
    /* Return if new lookup was recursed to before. */
B
Behdad Esfahbod 已提交
168
    if (recursed_lookups->has (lookup_index))
169 170
      return HB_VOID;

171 172 173 174
    hb_set_t *old_before = before;
    hb_set_t *old_input  = input;
    hb_set_t *old_after  = after;
    before = input = after = hb_set_get_empty ();
175

B
Behdad Esfahbod 已提交
176
    nesting_level_left--;
177
    recurse_func (this, lookup_index);
B
Behdad Esfahbod 已提交
178
    nesting_level_left++;
179 180 181 182 183

    before = old_before;
    input  = old_input;
    after  = old_after;

B
Behdad Esfahbod 已提交
184
    recursed_lookups->add (lookup_index);
185

186
    return HB_VOID;
B
Behdad Esfahbod 已提交
187 188
  }

189
  hb_face_t *face;
B
Minor  
Behdad Esfahbod 已提交
190 191 192 193
  hb_set_t *before;
  hb_set_t *input;
  hb_set_t *after;
  hb_set_t *output;
B
Behdad Esfahbod 已提交
194
  recurse_func_t recurse_func;
B
Behdad Esfahbod 已提交
195
  hb_set_t *recursed_lookups;
B
Behdad Esfahbod 已提交
196
  unsigned int nesting_level_left;
197 198 199
  unsigned int debug_depth;

  hb_collect_glyphs_context_t (hb_face_t *face_,
B
Behdad Esfahbod 已提交
200 201 202 203
			       hb_set_t  *glyphs_before, /* OUT. May be nullptr */
			       hb_set_t  *glyphs_input,  /* OUT. May be nullptr */
			       hb_set_t  *glyphs_after,  /* OUT. May be nullptr */
			       hb_set_t  *glyphs_output, /* OUT. May be nullptr */
204
			       unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
205
			      face (face_),
B
Minor  
Behdad Esfahbod 已提交
206 207 208 209
			      before (glyphs_before ? glyphs_before : hb_set_get_empty ()),
			      input  (glyphs_input  ? glyphs_input  : hb_set_get_empty ()),
			      after  (glyphs_after  ? glyphs_after  : hb_set_get_empty ()),
			      output (glyphs_output ? glyphs_output : hb_set_get_empty ()),
B
Behdad Esfahbod 已提交
210
			      recurse_func (nullptr),
B
Behdad Esfahbod 已提交
211
			      recursed_lookups (nullptr),
B
Behdad Esfahbod 已提交
212
			      nesting_level_left (nesting_level_left_),
213 214
			      debug_depth (0)
  {
B
Behdad Esfahbod 已提交
215
    recursed_lookups = hb_set_create ();
216 217 218
  }
  ~hb_collect_glyphs_context_t (void)
  {
B
Behdad Esfahbod 已提交
219
    hb_set_destroy (recursed_lookups);
220
  }
B
Behdad Esfahbod 已提交
221 222

  void set_recurse_func (recurse_func_t func) { recurse_func = func; }
223 224 225 226
};



B
Behdad Esfahbod 已提交
227 228
/* XXX Can we remove this? */

229
template <typename set_t>
B
Behdad Esfahbod 已提交
230 231
struct hb_add_coverage_context_t :
       hb_dispatch_context_t<hb_add_coverage_context_t<set_t>, const Coverage &, HB_DEBUG_GET_COVERAGE>
232
{
233
  inline const char *get_name (void) { return "GET_COVERAGE"; }
234 235
  typedef const Coverage &return_t;
  template <typename T>
236
  inline return_t dispatch (const T &obj) { return obj.get_coverage (); }
237
  static return_t default_return_value (void) { return Null(Coverage); }
238 239 240 241 242
  bool stop_sublookup_iteration (return_t r) const
  {
    r.add_coverage (set);
    return false;
  }
B
Behdad Esfahbod 已提交
243

244 245
  hb_add_coverage_context_t (set_t *set_) :
			    set (set_),
246 247
			    debug_depth (0) {}

248
  set_t *set;
249
  unsigned int debug_depth;
250 251 252
};


253 254
struct hb_ot_apply_context_t :
       hb_dispatch_context_t<hb_ot_apply_context_t, bool, HB_DEBUG_APPLY>
255
{
256
  struct matcher_t
B
Behdad Esfahbod 已提交
257
  {
258 259
    inline matcher_t (void) :
	     lookup_props (0),
260 261
	     ignore_zwnj (false),
	     ignore_zwj (false),
262 263 264 265
	     mask (-1),
#define arg1(arg) (arg) /* Remove the macro to see why it's needed! */
	     syllable arg1(0),
#undef arg1
B
Behdad Esfahbod 已提交
266 267
	     match_func (nullptr),
	     match_data (nullptr) {};
268

B
Behdad Esfahbod 已提交
269
    typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data);
270 271

    inline void set_ignore_zwnj (bool ignore_zwnj_) { ignore_zwnj = ignore_zwnj_; }
272
    inline void set_ignore_zwj (bool ignore_zwj_) { ignore_zwj = ignore_zwj_; }
273 274 275 276 277 278 279
    inline void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
    inline void set_mask (hb_mask_t mask_) { mask = mask_; }
    inline void set_syllable (uint8_t syllable_)  { syllable = syllable_; }
    inline void set_match_func (match_func_t match_func_,
				const void *match_data_)
    { match_func = match_func_; match_data = match_data_; }

280 281 282 283 284 285 286
    enum may_match_t {
      MATCH_NO,
      MATCH_YES,
      MATCH_MAYBE
    };

    inline may_match_t may_match (const hb_glyph_info_t &info,
B
Behdad Esfahbod 已提交
287
				  const HBUINT16          *glyph_data) const
B
Behdad Esfahbod 已提交
288
    {
289 290 291 292 293 294 295 296
      if (!(info.mask & mask) ||
	  (syllable && syllable != info.syllable ()))
	return MATCH_NO;

      if (match_func)
        return match_func (info.codepoint, *glyph_data, match_data) ? MATCH_YES : MATCH_NO;

      return MATCH_MAYBE;
B
Behdad Esfahbod 已提交
297
    }
298 299 300 301 302 303 304 305

    enum may_skip_t {
      SKIP_NO,
      SKIP_YES,
      SKIP_MAYBE
    };

    inline may_skip_t
306
    may_skip (const hb_ot_apply_context_t *c,
307
	      const hb_glyph_info_t    &info) const
B
Behdad Esfahbod 已提交
308
    {
B
Behdad Esfahbod 已提交
309
      if (!c->check_glyph_property (&info, lookup_props))
310 311
	return SKIP_YES;

312
      if (unlikely (_hb_glyph_info_is_default_ignorable_and_not_hidden (&info) &&
313
		    (ignore_zwnj || !_hb_glyph_info_is_zwnj (&info)) &&
314
		    (ignore_zwj || !_hb_glyph_info_is_zwj (&info))))
315 316 317
	return SKIP_MAYBE;

      return SKIP_NO;
B
Behdad Esfahbod 已提交
318
    }
319 320 321 322

    protected:
    unsigned int lookup_props;
    bool ignore_zwnj;
323
    bool ignore_zwj;
324 325 326 327 328 329
    hb_mask_t mask;
    uint8_t syllable;
    match_func_t match_func;
    const void *match_data;
  };

330
  struct skipping_iterator_t
331
  {
332
    inline void init (hb_ot_apply_context_t *c_, bool context_match = false)
B
Behdad Esfahbod 已提交
333
    {
334
      c = c_;
B
Behdad Esfahbod 已提交
335 336
      match_glyph_data = nullptr;
      matcher.set_match_func (nullptr, nullptr);
337
      matcher.set_lookup_props (c->lookup_props);
338
      /* Ignore ZWNJ if we are matching GSUB context, or matching GPOS. */
339
      matcher.set_ignore_zwnj (c->table_index == 1 || (context_match && c->auto_zwnj));
340
      /* Ignore ZWJ if we are matching GSUB context, or matching GPOS, or if asked to. */
341
      matcher.set_ignore_zwj  (c->table_index == 1 || (context_match || c->auto_zwj));
342 343 344 345 346
      matcher.set_mask (context_match ? -1 : c->lookup_mask);
    }
    inline void set_lookup_props (unsigned int lookup_props)
    {
      matcher.set_lookup_props (lookup_props);
B
Behdad Esfahbod 已提交
347
    }
B
Behdad Esfahbod 已提交
348 349
    inline void set_match_func (matcher_t::match_func_t match_func_,
				const void *match_data_,
B
Behdad Esfahbod 已提交
350
				const HBUINT16 glyph_data[])
351
    {
B
Behdad Esfahbod 已提交
352
      matcher.set_match_func (match_func_, match_data_);
353
      match_glyph_data = glyph_data;
354
    }
355

356 357 358 359 360 361 362 363 364
    inline void reset (unsigned int start_index_,
		       unsigned int num_items_)
    {
      idx = start_index_;
      num_items = num_items_;
      end = c->buffer->len;
      matcher.set_syllable (start_index_ == c->buffer->idx ? c->buffer->cur().syllable () : 0);
    }

365
    inline void reject (void) { num_items++; match_glyph_data--; }
366

367
    inline matcher_t::may_skip_t
B
Behdad Esfahbod 已提交
368
    may_skip (const hb_glyph_info_t    &info) const
369 370 371 372
    {
      return matcher.may_skip (c, info);
    }

B
Behdad Esfahbod 已提交
373
    inline bool next (void)
B
Behdad Esfahbod 已提交
374
    {
375
      assert (num_items > 0);
376
      while (idx + num_items < end)
B
Behdad Esfahbod 已提交
377
      {
B
Behdad Esfahbod 已提交
378
	idx++;
379 380
	const hb_glyph_info_t &info = c->buffer->info[idx];

B
Minor  
Behdad Esfahbod 已提交
381
	matcher_t::may_skip_t skip = matcher.may_skip (c, info);
382 383 384
	if (unlikely (skip == matcher_t::SKIP_YES))
	  continue;

385
	matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
B
Behdad Esfahbod 已提交
386 387 388
	if (match == matcher_t::MATCH_YES ||
	    (match == matcher_t::MATCH_MAYBE &&
	     skip == matcher_t::SKIP_NO))
389 390 391 392 393 394 395
	{
	  num_items--;
	  match_glyph_data++;
	  return true;
	}

	if (skip == matcher_t::SKIP_NO)
B
Behdad Esfahbod 已提交
396
	  return false;
397 398
      }
      return false;
B
Behdad Esfahbod 已提交
399
    }
B
Behdad Esfahbod 已提交
400
    inline bool prev (void)
B
Behdad Esfahbod 已提交
401
    {
402
      assert (num_items > 0);
B
Behdad Esfahbod 已提交
403
      while (idx > num_items - 1)
B
Behdad Esfahbod 已提交
404 405
      {
	idx--;
406 407
	const hb_glyph_info_t &info = c->buffer->out_info[idx];

B
Minor  
Behdad Esfahbod 已提交
408
	matcher_t::may_skip_t skip = matcher.may_skip (c, info);
409 410 411
	if (unlikely (skip == matcher_t::SKIP_YES))
	  continue;

412
	matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
B
Behdad Esfahbod 已提交
413 414 415
	if (match == matcher_t::MATCH_YES ||
	    (match == matcher_t::MATCH_MAYBE &&
	     skip == matcher_t::SKIP_NO))
416 417 418 419 420 421 422
	{
	  num_items--;
	  match_glyph_data++;
	  return true;
	}

	if (skip == matcher_t::SKIP_NO)
B
Behdad Esfahbod 已提交
423
	  return false;
424 425
      }
      return false;
B
Behdad Esfahbod 已提交
426 427 428
    }

    unsigned int idx;
429
    protected:
430
    hb_ot_apply_context_t *c;
431
    matcher_t matcher;
B
Behdad Esfahbod 已提交
432
    const HBUINT16 *match_glyph_data;
433

B
Behdad Esfahbod 已提交
434
    unsigned int num_items;
435
    unsigned int end;
B
Behdad Esfahbod 已提交
436 437
  };

B
Behdad Esfahbod 已提交
438 439

  inline const char *get_name (void) { return "APPLY"; }
440
  typedef return_t (*recurse_func_t) (hb_ot_apply_context_t *c, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
441 442 443 444
  template <typename T>
  inline return_t dispatch (const T &obj) { return obj.apply (this); }
  static return_t default_return_value (void) { return false; }
  bool stop_sublookup_iteration (return_t r) const { return r; }
B
Behdad Esfahbod 已提交
445
  return_t recurse (unsigned int sub_lookup_index)
B
Behdad Esfahbod 已提交
446
  {
447
    if (unlikely (nesting_level_left == 0 || !recurse_func || buffer->max_ops-- <= 0))
B
Behdad Esfahbod 已提交
448 449 450
      return default_return_value ();

    nesting_level_left--;
B
Behdad Esfahbod 已提交
451
    bool ret = recurse_func (this, sub_lookup_index);
B
Behdad Esfahbod 已提交
452 453 454 455
    nesting_level_left++;
    return ret;
  }

456 457
  skipping_iterator_t iter_input, iter_context;

B
Behdad Esfahbod 已提交
458 459 460 461 462
  hb_font_t *font;
  hb_face_t *face;
  hb_buffer_t *buffer;
  recurse_func_t recurse_func;
  const GDEF &gdef;
463
  const VariationStore &var_store;
464 465 466 467

  hb_direction_t direction;
  hb_mask_t lookup_mask;
  unsigned int table_index; /* GSUB/GPOS */
468
  unsigned int lookup_index;
469 470
  unsigned int lookup_props;
  unsigned int nesting_level_left;
B
Behdad Esfahbod 已提交
471 472
  unsigned int debug_depth;

473 474 475 476
  bool auto_zwnj;
  bool auto_zwj;
  bool has_glyph_classes;

B
Behdad Esfahbod 已提交
477

478
  hb_ot_apply_context_t (unsigned int table_index_,
B
Behdad Esfahbod 已提交
479 480
		      hb_font_t *font_,
		      hb_buffer_t *buffer_) :
481
			iter_input (), iter_context (),
B
Behdad Esfahbod 已提交
482
			font (font_), face (font->face), buffer (buffer_),
B
Behdad Esfahbod 已提交
483
			recurse_func (nullptr),
484
			gdef (*hb_ot_face_data (face)->table.GDEF),
485
			var_store (gdef.get_var_store ()),
486 487 488
			direction (buffer_->props.direction),
			lookup_mask (1),
			table_index (table_index_),
489
			lookup_index ((unsigned int) -1),
490 491 492 493 494 495
			lookup_props (0),
			nesting_level_left (HB_MAX_NESTING_LEVEL),
			debug_depth (0),
			auto_zwnj (true),
			auto_zwj (true),
			has_glyph_classes (gdef.has_glyph_classes ()) {}
B
Behdad Esfahbod 已提交
496 497 498

  inline void set_lookup_mask (hb_mask_t mask) { lookup_mask = mask; }
  inline void set_auto_zwj (bool auto_zwj_) { auto_zwj = auto_zwj_; }
499
  inline void set_auto_zwnj (bool auto_zwnj_) { auto_zwnj = auto_zwnj_; }
B
Behdad Esfahbod 已提交
500
  inline void set_recurse_func (recurse_func_t func) { recurse_func = func; }
501
  inline void set_lookup_index (unsigned int lookup_index_) { lookup_index = lookup_index_; }
502 503 504 505 506 507
  inline void set_lookup_props (unsigned int lookup_props_)
  {
    lookup_props = lookup_props_;
    iter_input.init (this, false);
    iter_context.init (this, true);
  }
B
Behdad Esfahbod 已提交
508

509 510 511
  inline bool
  match_properties_mark (hb_codepoint_t  glyph,
			 unsigned int    glyph_props,
B
Behdad Esfahbod 已提交
512
			 unsigned int    match_props) const
513 514
  {
    /* If using mark filtering sets, the high short of
B
Behdad Esfahbod 已提交
515
     * match_props has the set index.
516
     */
B
Behdad Esfahbod 已提交
517 518
    if (match_props & LookupFlag::UseMarkFilteringSet)
      return gdef.mark_set_covers (match_props >> 16, glyph);
519

B
Behdad Esfahbod 已提交
520
    /* The second byte of match_props has the meaning
521 522 523
     * "ignore marks of attachment type different than
     * the attachment type specified."
     */
B
Behdad Esfahbod 已提交
524 525
    if (match_props & LookupFlag::MarkAttachmentType)
      return (match_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
526 527 528 529 530

    return true;
  }

  inline bool
B
Behdad Esfahbod 已提交
531
  check_glyph_property (const hb_glyph_info_t *info,
B
Behdad Esfahbod 已提交
532
			unsigned int  match_props) const
533
  {
B
Behdad Esfahbod 已提交
534 535 536
    hb_codepoint_t glyph = info->codepoint;
    unsigned int glyph_props = _hb_glyph_info_get_glyph_props (info);

537
    /* Not covered, if, for example, glyph class is ligature and
B
Behdad Esfahbod 已提交
538
     * match_props includes LookupFlags::IgnoreLigatures
539
     */
B
Behdad Esfahbod 已提交
540
    if (glyph_props & match_props & LookupFlag::IgnoreFlags)
541 542
      return false;

543
    if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_PROPS_MARK))
B
Behdad Esfahbod 已提交
544
      return match_properties_mark (glyph, glyph_props, match_props);
545 546 547 548

    return true;
  }

549
  inline void _set_glyph_props (hb_codepoint_t glyph_index,
550
			  unsigned int class_guess = 0,
551 552
			  bool ligature = false,
			  bool component = false) const
553
  {
554 555 556 557
    unsigned int add_in = _hb_glyph_info_get_glyph_props (&buffer->cur()) &
			  HB_OT_LAYOUT_GLYPH_PROPS_PRESERVE;
    add_in |= HB_OT_LAYOUT_GLYPH_PROPS_SUBSTITUTED;
    if (ligature)
558
    {
559
      add_in |= HB_OT_LAYOUT_GLYPH_PROPS_LIGATED;
560 561 562 563 564 565 566 567 568 569
      /* In the only place that the MULTIPLIED bit is used, Uniscribe
       * seems to only care about the "last" transformation between
       * Ligature and Multiple substitions.  Ie. if you ligate, expand,
       * and ligate again, it forgives the multiplication and acts as
       * if only ligation happened.  As such, clear MULTIPLIED bit.
       */
      add_in &= ~HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
    }
    if (component)
      add_in |= HB_OT_LAYOUT_GLYPH_PROPS_MULTIPLIED;
570
    if (likely (has_glyph_classes))
571
      _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | gdef.get_glyph_props (glyph_index));
572
    else if (class_guess)
573
      _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | class_guess);
574
  }
B
Behdad Esfahbod 已提交
575

576
  inline void replace_glyph (hb_codepoint_t glyph_index) const
577
  {
578 579
    _set_glyph_props (glyph_index);
    buffer->replace_glyph (glyph_index);
580
  }
581
  inline void replace_glyph_inplace (hb_codepoint_t glyph_index) const
582
  {
583 584 585 586 587 588
    _set_glyph_props (glyph_index);
    buffer->cur().codepoint = glyph_index;
  }
  inline void replace_glyph_with_ligature (hb_codepoint_t glyph_index,
					   unsigned int class_guess) const
  {
589
    _set_glyph_props (glyph_index, class_guess, true);
590 591
    buffer->replace_glyph (glyph_index);
  }
592 593
  inline void output_glyph_for_component (hb_codepoint_t glyph_index,
					  unsigned int class_guess) const
B
Behdad Esfahbod 已提交
594
  {
595
    _set_glyph_props (glyph_index, class_guess, false, true);
596
    buffer->output_glyph (glyph_index);
B
Behdad Esfahbod 已提交
597
  }
598 599
};

600

B
Behdad Esfahbod 已提交
601

B
Behdad Esfahbod 已提交
602 603 604
typedef bool (*intersects_func_t) (hb_set_t *glyphs, const HBUINT16 &value, const void *data);
typedef void (*collect_glyphs_func_t) (hb_set_t *glyphs, const HBUINT16 &value, const void *data);
typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data);
605

606 607 608 609
struct ContextClosureFuncs
{
  intersects_func_t intersects;
};
610 611 612 613
struct ContextCollectGlyphsFuncs
{
  collect_glyphs_func_t collect;
};
614
struct ContextApplyFuncs
B
Behdad Esfahbod 已提交
615
{
616
  match_func_t match;
617 618
};

619

B
Behdad Esfahbod 已提交
620
static inline bool intersects_glyph (hb_set_t *glyphs, const HBUINT16 &value, const void *data HB_UNUSED)
621 622 623
{
  return glyphs->has (value);
}
B
Behdad Esfahbod 已提交
624
static inline bool intersects_class (hb_set_t *glyphs, const HBUINT16 &value, const void *data)
625 626 627 628
{
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
  return class_def.intersects_class (glyphs, value);
}
B
Behdad Esfahbod 已提交
629
static inline bool intersects_coverage (hb_set_t *glyphs, const HBUINT16 &value, const void *data)
630 631 632 633 634 635 636
{
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
  return (data+coverage).intersects (glyphs);
}

static inline bool intersects_array (hb_closure_context_t *c,
				     unsigned int count,
B
Behdad Esfahbod 已提交
637
				     const HBUINT16 values[],
638 639 640 641 642 643 644 645 646
				     intersects_func_t intersects_func,
				     const void *intersects_data)
{
  for (unsigned int i = 0; i < count; i++)
    if (likely (!intersects_func (c->glyphs, values[i], intersects_data)))
      return false;
  return true;
}

647

B
Behdad Esfahbod 已提交
648
static inline void collect_glyph (hb_set_t *glyphs, const HBUINT16 &value, const void *data HB_UNUSED)
649 650 651
{
  glyphs->add (value);
}
B
Behdad Esfahbod 已提交
652
static inline void collect_class (hb_set_t *glyphs, const HBUINT16 &value, const void *data)
653 654
{
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
655
  class_def.add_class (glyphs, value);
656
}
B
Behdad Esfahbod 已提交
657
static inline void collect_coverage (hb_set_t *glyphs, const HBUINT16 &value, const void *data)
658 659 660 661
{
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
  (data+coverage).add_coverage (glyphs);
}
B
Behdad Esfahbod 已提交
662
static inline void collect_array (hb_collect_glyphs_context_t *c HB_UNUSED,
663
				  hb_set_t *glyphs,
664
				  unsigned int count,
B
Behdad Esfahbod 已提交
665
				  const HBUINT16 values[],
666 667 668 669
				  collect_glyphs_func_t collect_func,
				  const void *collect_data)
{
  for (unsigned int i = 0; i < count; i++)
670
    collect_func (glyphs, values[i], collect_data);
671 672 673
}


B
Behdad Esfahbod 已提交
674
static inline bool match_glyph (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data HB_UNUSED)
B
Behdad Esfahbod 已提交
675
{
676 677
  return glyph_id == value;
}
B
Behdad Esfahbod 已提交
678
static inline bool match_class (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data)
B
Behdad Esfahbod 已提交
679
{
B
Behdad Esfahbod 已提交
680
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
681 682
  return class_def.get_class (glyph_id) == value;
}
B
Behdad Esfahbod 已提交
683
static inline bool match_coverage (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data)
B
Behdad Esfahbod 已提交
684
{
685
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
686
  return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
687 688
}

689 690
static inline bool would_match_input (hb_would_apply_context_t *c,
				      unsigned int count, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
691
				      const HBUINT16 input[], /* Array of input values--start with second glyph */
692 693 694 695 696 697 698
				      match_func_t match_func,
				      const void *match_data)
{
  if (count != c->len)
    return false;

  for (unsigned int i = 1; i < count; i++)
699
    if (likely (!match_func (c->glyphs[i], input[i - 1], match_data)))
700 701 702 703
      return false;

  return true;
}
704
static inline bool match_input (hb_ot_apply_context_t *c,
B
Behdad Esfahbod 已提交
705
				unsigned int count, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
706
				const HBUINT16 input[], /* Array of input values--start with second glyph */
B
Behdad Esfahbod 已提交
707
				match_func_t match_func,
B
Behdad Esfahbod 已提交
708
				const void *match_data,
B
Behdad Esfahbod 已提交
709
				unsigned int *end_offset,
710
				unsigned int match_positions[HB_MAX_CONTEXT_LENGTH],
B
Behdad Esfahbod 已提交
711 712
				bool *p_is_mark_ligature = nullptr,
				unsigned int *p_total_component_count = nullptr)
B
Behdad Esfahbod 已提交
713
{
B
Behdad Esfahbod 已提交
714
  TRACE_APPLY (nullptr);
715

716
  if (unlikely (count > HB_MAX_CONTEXT_LENGTH)) return_trace (false);
717

B
Behdad Esfahbod 已提交
718 719
  hb_buffer_t *buffer = c->buffer;

720
  hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
721
  skippy_iter.reset (buffer->idx, count - 1);
722
  skippy_iter.set_match_func (match_func, match_data, input);
723

724 725 726 727 728 729 730 731 732 733 734
  /*
   * This is perhaps the trickiest part of OpenType...  Remarks:
   *
   * - If all components of the ligature were marks, we call this a mark ligature.
   *
   * - If there is no GDEF, and the ligature is NOT a mark ligature, we categorize
   *   it as a ligature glyph.
   *
   * - Ligatures cannot be formed across glyphs attached to different components
   *   of previous ligatures.  Eg. the sequence is LAM,SHADDA,LAM,FATHA,HEH, and
   *   LAM,LAM,HEH form a ligature, leaving SHADDA,FATHA next to eachother.
735 736 737 738 739 740 741 742 743 744
   *   However, it would be wrong to ligate that SHADDA,FATHA sequence.
   *   There are a couple of exceptions to this:
   *
   *   o If a ligature tries ligating with marks that belong to it itself, go ahead,
   *     assuming that the font designer knows what they are doing (otherwise it can
   *     break Indic stuff when a matra wants to ligate with a conjunct,
   *
   *   o If two marks want to ligate and they belong to different components of the
   *     same ligature glyph, and said ligature glyph is to be ignored according to
   *     mark-filtering rules, then allow.
745
   *     https://github.com/harfbuzz/harfbuzz/issues/545
746 747
   */

B
Behdad Esfahbod 已提交
748
  bool is_mark_ligature = _hb_glyph_info_is_mark (&buffer->cur());
749 750

  unsigned int total_component_count = 0;
B
Behdad Esfahbod 已提交
751
  total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->cur());
752

B
Behdad Esfahbod 已提交
753 754
  unsigned int first_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
  unsigned int first_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
B
Behdad Esfahbod 已提交
755

756 757 758 759 760 761
  enum {
    LIGBASE_NOT_CHECKED,
    LIGBASE_MAY_NOT_SKIP,
    LIGBASE_MAY_SKIP
  } ligbase = LIGBASE_NOT_CHECKED;

B
Behdad Esfahbod 已提交
762
  match_positions[0] = buffer->idx;
B
Minor  
Behdad Esfahbod 已提交
763
  for (unsigned int i = 1; i < count; i++)
B
Behdad Esfahbod 已提交
764
  {
B
Behdad Esfahbod 已提交
765
    if (!skippy_iter.next ()) return_trace (false);
B
Behdad Esfahbod 已提交
766 767

    match_positions[i] = skippy_iter.idx;
768

B
Behdad Esfahbod 已提交
769 770
    unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]);
    unsigned int this_lig_comp = _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]);
771

772 773
    if (first_lig_id && first_lig_comp)
    {
774 775
      /* If first component was attached to a previous ligature component,
       * all subsequent components should be attached to the same ligature
776
       * component, otherwise we shouldn't ligate them... */
777
      if (first_lig_id != this_lig_id || first_lig_comp != this_lig_comp)
778 779 780
      {
        /* ...unless, we are attached to a base ligature and that base
	 * ligature is ignorable. */
781
        if (ligbase == LIGBASE_NOT_CHECKED)
782
	{
783 784 785 786
	  bool found = false;
	  const hb_glyph_info_t *out = buffer->out_info;
	  unsigned int j = buffer->out_len;
	  while (j && _hb_glyph_info_get_lig_id (&out[j - 1]) == first_lig_id)
787
	  {
788 789 790 791 792 793
	    if (_hb_glyph_info_get_lig_comp (&out[j - 1]) == 0)
	    {
	      j--;
	      found = true;
	      break;
	    }
794 795 796
	    j--;
	  }

B
Behdad Esfahbod 已提交
797
	  if (found && skippy_iter.may_skip (out[j]) == hb_ot_apply_context_t::matcher_t::SKIP_YES)
798 799 800 801
	    ligbase = LIGBASE_MAY_SKIP;
	  else
	    ligbase = LIGBASE_MAY_NOT_SKIP;
	}
802

803
        if (ligbase == LIGBASE_MAY_NOT_SKIP)
804 805 806 807
	  return_trace (false);
      }
    }
    else
808
    {
809 810 811 812
      /* If first component was NOT attached to a previous ligature component,
       * all subsequent components should also NOT be attached to any ligature
       * component, unless they are attached to the first component itself! */
      if (this_lig_id && this_lig_comp && (this_lig_id != first_lig_id))
B
Behdad Esfahbod 已提交
813
	return_trace (false);
814 815
    }

B
Behdad Esfahbod 已提交
816
    is_mark_ligature = is_mark_ligature && _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx]);
B
Behdad Esfahbod 已提交
817
    total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->info[skippy_iter.idx]);
B
Behdad Esfahbod 已提交
818 819
  }

B
Behdad Esfahbod 已提交
820
  *end_offset = skippy_iter.idx - buffer->idx + 1;
B
Behdad Esfahbod 已提交
821

822 823 824 825 826 827
  if (p_is_mark_ligature)
    *p_is_mark_ligature = is_mark_ligature;

  if (p_total_component_count)
    *p_total_component_count = total_component_count;

B
Behdad Esfahbod 已提交
828
  return_trace (true);
B
Behdad Esfahbod 已提交
829
}
830
static inline bool ligate_input (hb_ot_apply_context_t *c,
831
				 unsigned int count, /* Including the first glyph */
832
				 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */
833
				 unsigned int match_length,
834 835 836 837
				 hb_codepoint_t lig_glyph,
				 bool is_mark_ligature,
				 unsigned int total_component_count)
{
B
Behdad Esfahbod 已提交
838
  TRACE_APPLY (nullptr);
839

B
Behdad Esfahbod 已提交
840 841 842
  hb_buffer_t *buffer = c->buffer;

  buffer->merge_clusters (buffer->idx, buffer->idx + match_length);
843

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
  /*
   * - If it *is* a mark ligature, we don't allocate a new ligature id, and leave
   *   the ligature to keep its old ligature id.  This will allow it to attach to
   *   a base ligature in GPOS.  Eg. if the sequence is: LAM,LAM,SHADDA,FATHA,HEH,
   *   and LAM,LAM,HEH for a ligature, they will leave SHADDA and FATHA wit a
   *   ligature id and component value of 2.  Then if SHADDA,FATHA form a ligature
   *   later, we don't want them to lose their ligature id/component, otherwise
   *   GPOS will fail to correctly position the mark ligature on top of the
   *   LAM,LAM,HEH ligature.  See:
   *     https://bugzilla.gnome.org/show_bug.cgi?id=676343
   *
   * - If a ligature is formed of components that some of which are also ligatures
   *   themselves, and those ligature components had marks attached to *their*
   *   components, we have to attach the marks to the new ligature component
   *   positions!  Now *that*'s tricky!  And these marks may be following the
   *   last component of the whole sequence, so we should loop forward looking
   *   for them and update them.
   *
   *   Eg. the sequence is LAM,LAM,SHADDA,FATHA,HEH, and the font first forms a
   *   'calt' ligature of LAM,HEH, leaving the SHADDA and FATHA with a ligature
   *   id and component == 1.  Now, during 'liga', the LAM and the LAM-HEH ligature
   *   form a LAM-LAM-HEH ligature.  We need to reassign the SHADDA and FATHA to
   *   the new ligature with a component value of 2.
   *
   *   This in fact happened to a font...  See:
   *   https://bugzilla.gnome.org/show_bug.cgi?id=437633
   */

872
  unsigned int klass = is_mark_ligature ? 0 : HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
B
Behdad Esfahbod 已提交
873 874 875
  unsigned int lig_id = is_mark_ligature ? 0 : _hb_allocate_lig_id (buffer);
  unsigned int last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
  unsigned int last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
876 877 878
  unsigned int components_so_far = last_num_components;

  if (!is_mark_ligature)
879
  {
B
Behdad Esfahbod 已提交
880
    _hb_glyph_info_set_lig_props_for_ligature (&buffer->cur(), lig_id, total_component_count);
B
Behdad Esfahbod 已提交
881
    if (_hb_glyph_info_get_general_category (&buffer->cur()) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
882
    {
B
Minor  
Behdad Esfahbod 已提交
883
      _hb_glyph_info_set_general_category (&buffer->cur(), HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER);
884
    }
885
  }
886
  c->replace_glyph_with_ligature (lig_glyph, klass);
887 888 889

  for (unsigned int i = 1; i < count; i++)
  {
B
Behdad Esfahbod 已提交
890
    while (buffer->idx < match_positions[i] && buffer->successful)
891 892
    {
      if (!is_mark_ligature) {
893 894
        unsigned int this_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
	if (this_comp == 0)
895
	  this_comp = last_num_components;
896
	unsigned int new_lig_comp = components_so_far - last_num_components +
897 898
				    MIN (this_comp, last_num_components);
	  _hb_glyph_info_set_lig_props_for_mark (&buffer->cur(), lig_id, new_lig_comp);
899
      }
B
Behdad Esfahbod 已提交
900
      buffer->next_glyph ();
901 902
    }

B
Behdad Esfahbod 已提交
903 904
    last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
    last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
905 906 907
    components_so_far += last_num_components;

    /* Skip the base glyph */
B
Behdad Esfahbod 已提交
908
    buffer->idx++;
909 910 911 912
  }

  if (!is_mark_ligature && last_lig_id) {
    /* Re-adjust components for any marks following. */
B
Behdad Esfahbod 已提交
913
    for (unsigned int i = buffer->idx; i < buffer->len; i++) {
B
Behdad Esfahbod 已提交
914
      if (last_lig_id == _hb_glyph_info_get_lig_id (&buffer->info[i])) {
915 916 917
        unsigned int this_comp = _hb_glyph_info_get_lig_comp (&buffer->info[i]);
	if (!this_comp)
	  break;
918
	unsigned int new_lig_comp = components_so_far - last_num_components +
919
				    MIN (this_comp, last_num_components);
B
Behdad Esfahbod 已提交
920
	_hb_glyph_info_set_lig_props_for_mark (&buffer->info[i], lig_id, new_lig_comp);
921 922 923 924
      } else
	break;
    }
  }
B
Behdad Esfahbod 已提交
925
  return_trace (true);
926
}
B
Behdad Esfahbod 已提交
927

928
static inline bool match_backtrack (hb_ot_apply_context_t *c,
929
				    unsigned int count,
B
Behdad Esfahbod 已提交
930
				    const HBUINT16 backtrack[],
931
				    match_func_t match_func,
932 933
				    const void *match_data,
				    unsigned int *match_start)
934
{
B
Behdad Esfahbod 已提交
935
  TRACE_APPLY (nullptr);
936

937
  hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_context;
938
  skippy_iter.reset (c->buffer->backtrack_len (), count);
939
  skippy_iter.set_match_func (match_func, match_data, backtrack);
940

941
  for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
942
    if (!skippy_iter.prev ())
B
Behdad Esfahbod 已提交
943
      return_trace (false);
944

945 946
  *match_start = skippy_iter.idx;

B
Behdad Esfahbod 已提交
947
  return_trace (true);
948 949
}

950
static inline bool match_lookahead (hb_ot_apply_context_t *c,
B
Behdad Esfahbod 已提交
951
				    unsigned int count,
B
Behdad Esfahbod 已提交
952
				    const HBUINT16 lookahead[],
B
Behdad Esfahbod 已提交
953
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
954
				    const void *match_data,
955 956
				    unsigned int offset,
				    unsigned int *end_index)
957
{
B
Behdad Esfahbod 已提交
958
  TRACE_APPLY (nullptr);
959

960
  hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_context;
961
  skippy_iter.reset (c->buffer->idx + offset - 1, count);
962
  skippy_iter.set_match_func (match_func, match_data, lookahead);
963

B
Minor  
Behdad Esfahbod 已提交
964
  for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
965
    if (!skippy_iter.next ())
B
Behdad Esfahbod 已提交
966
      return_trace (false);
967

968 969
  *end_index = skippy_iter.idx + 1;

B
Behdad Esfahbod 已提交
970
  return_trace (true);
971 972
}

B
Behdad Esfahbod 已提交
973

974

B
Behdad Esfahbod 已提交
975 976
struct LookupRecord
{
B
Behdad Esfahbod 已提交
977 978
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
979
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
980
    return_trace (c->check_struct (this));
B
Behdad Esfahbod 已提交
981 982
  }

B
Behdad Esfahbod 已提交
983
  HBUINT16	sequenceIndex;		/* Index into current glyph
984
					 * sequence--first glyph = 0 */
B
Behdad Esfahbod 已提交
985
  HBUINT16	lookupListIndex;	/* Lookup to apply to that
986
					 * position--zero--based */
B
Behdad Esfahbod 已提交
987 988
  public:
  DEFINE_SIZE_STATIC (4);
989 990
};

B
Behdad Esfahbod 已提交
991

992 993 994 995
template <typename context_t>
static inline void recurse_lookups (context_t *c,
				    unsigned int lookupCount,
				    const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */)
996 997
{
  for (unsigned int i = 0; i < lookupCount; i++)
B
Behdad Esfahbod 已提交
998
    c->recurse (lookupRecord[i].lookupListIndex);
999
}
B
Behdad Esfahbod 已提交
1000

1001
static inline bool apply_lookup (hb_ot_apply_context_t *c,
1002
				 unsigned int count, /* Including the first glyph */
1003
				 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */
1004
				 unsigned int lookupCount,
1005 1006
				 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
				 unsigned int match_length)
1007
{
B
Behdad Esfahbod 已提交
1008
  TRACE_APPLY (nullptr);
1009

1010
  hb_buffer_t *buffer = c->buffer;
1011
  int end;
B
Behdad Esfahbod 已提交
1012

1013 1014 1015 1016 1017
  /* All positions are distance from beginning of *output* buffer.
   * Adjust. */
  {
    unsigned int bl = buffer->backtrack_len ();
    end = bl + match_length;
1018

1019 1020 1021 1022 1023
    int delta = bl - buffer->idx;
    /* Convert positions to new indexing. */
    for (unsigned int j = 0; j < count; j++)
      match_positions[j] += delta;
  }
1024

B
Behdad Esfahbod 已提交
1025
  for (unsigned int i = 0; i < lookupCount && buffer->successful; i++)
B
Behdad Esfahbod 已提交
1026
  {
1027 1028 1029
    unsigned int idx = lookupRecord[i].sequenceIndex;
    if (idx >= count)
      continue;
1030

1031 1032 1033 1034 1035
    /* Don't recurse to ourself at same position.
     * Note that this test is too naive, it doesn't catch longer loops. */
    if (idx == 0 && lookupRecord[i].lookupListIndex == c->lookup_index)
      continue;

1036 1037
    if (unlikely (!buffer->move_to (match_positions[idx])))
      break;
1038

1039 1040 1041
    if (unlikely (buffer->max_ops <= 0))
      break;

1042 1043 1044
    unsigned int orig_len = buffer->backtrack_len () + buffer->lookahead_len ();
    if (!c->recurse (lookupRecord[i].lookupListIndex))
      continue;
1045

1046 1047
    unsigned int new_len = buffer->backtrack_len () + buffer->lookahead_len ();
    int delta = new_len - orig_len;
1048

1049 1050
    if (!delta)
        continue;
1051

B
Behdad Esfahbod 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
    /* Recursed lookup changed buffer len.  Adjust.
     *
     * TODO:
     *
     * Right now, if buffer length increased by n, we assume n new glyphs
     * were added right after the current position, and if buffer length
     * was decreased by n, we assume n match positions after the current
     * one where removed.  The former (buffer length increased) case is
     * fine, but the decrease case can be improved in at least two ways,
     * both of which are significant:
     *
     *   - If recursed-to lookup is MultipleSubst and buffer length
     *     decreased, then it's current match position that was deleted,
     *     NOT the one after it.
     *
     *   - If buffer length was decreased by n, it does not necessarily
     *     mean that n match positions where removed, as there might
     *     have been marks and default-ignorables in the sequence.  We
     *     should instead drop match positions between current-position
     *     and current-position + n instead.
     *
     * It should be possible to construct tests for both of these cases.
     */
1075

1076
    end += delta;
1077
    if (end <= int (match_positions[idx]))
1078
    {
1079
      /* End might end up being smaller than match_positions[idx] if the recursed
1080
       * lookup ended up removing many items, more than we have had matched.
1081 1082 1083
       * Just never rewind end back and get out of here.
       * https://bugs.chromium.org/p/chromium/issues/detail?id=659496 */
      end = match_positions[idx];
1084
      /* There can't be any further changes. */
1085 1086
      break;
    }
1087

1088 1089 1090 1091
    unsigned int next = idx + 1; /* next now is the position after the recursed lookup. */

    if (delta > 0)
    {
1092
      if (unlikely (delta + count > HB_MAX_CONTEXT_LENGTH))
1093
	break;
1094 1095 1096
    }
    else
    {
1097
      /* NOTE: delta is negative. */
1098 1099
      delta = MAX (delta, (int) next - (int) count);
      next -= delta;
1100
    }
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114

    /* Shift! */
    memmove (match_positions + next + delta, match_positions + next,
	     (count - next) * sizeof (match_positions[0]));
    next += delta;
    count += delta;

    /* Fill in new entries. */
    for (unsigned int j = idx + 1; j < next; j++)
      match_positions[j] = match_positions[j - 1] + 1;

    /* And fixup the rest. */
    for (; next < count; next++)
      match_positions[next] += delta;
1115 1116
  }

1117 1118
  buffer->move_to (end);

B
Behdad Esfahbod 已提交
1119
  return_trace (true);
1120
}
1121

B
Behdad Esfahbod 已提交
1122

1123 1124 1125

/* Contextual lookups */

1126 1127 1128 1129 1130 1131
struct ContextClosureLookupContext
{
  ContextClosureFuncs funcs;
  const void *intersects_data;
};

1132 1133 1134 1135 1136 1137
struct ContextCollectGlyphsLookupContext
{
  ContextCollectGlyphsFuncs funcs;
  const void *collect_data;
};

1138
struct ContextApplyLookupContext
B
Behdad Esfahbod 已提交
1139
{
1140
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
1141
  const void *match_data;
1142 1143
};

B
Behdad Esfahbod 已提交
1144
static inline void context_closure_lookup (hb_closure_context_t *c,
1145
					   unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1146
					   const HBUINT16 input[], /* Array of input values--start with second glyph */
1147 1148 1149 1150
					   unsigned int lookupCount,
					   const LookupRecord lookupRecord[],
					   ContextClosureLookupContext &lookup_context)
{
B
Behdad Esfahbod 已提交
1151 1152 1153
  if (intersects_array (c,
			inputCount ? inputCount - 1 : 0, input,
			lookup_context.funcs.intersects, lookup_context.intersects_data))
1154 1155
    recurse_lookups (c,
		     lookupCount, lookupRecord);
1156 1157
}

1158 1159
static inline void context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
						  unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1160
						  const HBUINT16 input[], /* Array of input values--start with second glyph */
1161 1162 1163 1164
						  unsigned int lookupCount,
						  const LookupRecord lookupRecord[],
						  ContextCollectGlyphsLookupContext &lookup_context)
{
B
Minor  
Behdad Esfahbod 已提交
1165
  collect_array (c, c->input,
1166 1167 1168 1169 1170
		 inputCount ? inputCount - 1 : 0, input,
		 lookup_context.funcs.collect, lookup_context.collect_data);
  recurse_lookups (c,
		   lookupCount, lookupRecord);
}
1171

1172 1173
static inline bool context_would_apply_lookup (hb_would_apply_context_t *c,
					       unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1174
					       const HBUINT16 input[], /* Array of input values--start with second glyph */
B
Behdad Esfahbod 已提交
1175 1176
					       unsigned int lookupCount HB_UNUSED,
					       const LookupRecord lookupRecord[] HB_UNUSED,
1177 1178 1179 1180 1181 1182
					       ContextApplyLookupContext &lookup_context)
{
  return would_match_input (c,
			    inputCount, input,
			    lookup_context.funcs.match, lookup_context.match_data);
}
1183
static inline bool context_apply_lookup (hb_ot_apply_context_t *c,
1184
					 unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1185
					 const HBUINT16 input[], /* Array of input values--start with second glyph */
1186 1187 1188
					 unsigned int lookupCount,
					 const LookupRecord lookupRecord[],
					 ContextApplyLookupContext &lookup_context)
1189
{
1190
  unsigned int match_length = 0;
1191
  unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
B
Behdad Esfahbod 已提交
1192
  return match_input (c,
B
Behdad Esfahbod 已提交
1193
		      inputCount, input,
1194 1195
		      lookup_context.funcs.match, lookup_context.match_data,
		      &match_length, match_positions)
1196 1197
      && (c->buffer->unsafe_to_break (c->buffer->idx, c->buffer->idx + match_length),
	  apply_lookup (c,
1198 1199
		       inputCount, match_positions,
		       lookupCount, lookupRecord,
1200
		       match_length));
1201 1202
}

B
Behdad Esfahbod 已提交
1203 1204
struct Rule
{
B
Behdad Esfahbod 已提交
1205
  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
1206
  {
B
Behdad Esfahbod 已提交
1207
    TRACE_CLOSURE (this);
1208
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
B
Behdad Esfahbod 已提交
1209
    context_closure_lookup (c,
1210
			    inputCount, inputZ,
B
Behdad Esfahbod 已提交
1211 1212
			    lookupCount, lookupRecord,
			    lookup_context);
1213 1214
  }

1215 1216 1217
  inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
  {
    TRACE_COLLECT_GLYPHS (this);
1218
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
1219
    context_collect_glyphs_lookup (c,
1220
				   inputCount, inputZ,
1221 1222 1223 1224
				   lookupCount, lookupRecord,
				   lookup_context);
  }

1225 1226
  inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
  {
B
Behdad Esfahbod 已提交
1227
    TRACE_WOULD_APPLY (this);
1228
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
B
Behdad Esfahbod 已提交
1229
    return_trace (context_would_apply_lookup (c, inputCount, inputZ, lookupCount, lookupRecord, lookup_context));
1230 1231
  }

1232
  inline bool apply (hb_ot_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
1233
  {
B
Behdad Esfahbod 已提交
1234
    TRACE_APPLY (this);
1235
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (inputZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
B
Behdad Esfahbod 已提交
1236
    return_trace (context_apply_lookup (c, inputCount, inputZ, lookupCount, lookupRecord, lookup_context));
1237 1238
  }

B
Behdad Esfahbod 已提交
1239
  public:
B
Behdad Esfahbod 已提交
1240 1241
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1242
    TRACE_SANITIZE (this);
1243 1244 1245 1246
    return_trace (inputCount.sanitize (c) &&
		  lookupCount.sanitize (c) &&
		  c->check_range (inputZ,
				  inputZ[0].static_size * inputCount +
1247
				  LookupRecord::static_size * lookupCount));
B
Behdad Esfahbod 已提交
1248 1249
  }

1250
  protected:
B
Behdad Esfahbod 已提交
1251
  HBUINT16	inputCount;		/* Total number of glyphs in input
1252
					 * glyph sequence--includes the first
1253
					 * glyph */
B
Behdad Esfahbod 已提交
1254 1255
  HBUINT16	lookupCount;		/* Number of LookupRecords */
  HBUINT16	inputZ[VAR];		/* Array of match inputs--start with
1256
					 * second glyph */
1257
/*LookupRecord	lookupRecordX[VAR];*/	/* Array of LookupRecords--in
1258
					 * design order */
B
Behdad Esfahbod 已提交
1259
  public:
1260
  DEFINE_SIZE_ARRAY (4, inputZ);
1261 1262
};

B
Behdad Esfahbod 已提交
1263 1264
struct RuleSet
{
B
Behdad Esfahbod 已提交
1265
  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
1266
  {
B
Behdad Esfahbod 已提交
1267
    TRACE_CLOSURE (this);
1268 1269
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
1270
      (this+rule[i]).closure (c, lookup_context);
1271 1272
  }

1273 1274 1275 1276 1277 1278 1279 1280
  inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
  {
    TRACE_COLLECT_GLYPHS (this);
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
      (this+rule[i]).collect_glyphs (c, lookup_context);
  }

1281 1282
  inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
  {
B
Behdad Esfahbod 已提交
1283
    TRACE_WOULD_APPLY (this);
1284 1285 1286 1287
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
    {
      if ((this+rule[i]).would_apply (c, lookup_context))
B
Behdad Esfahbod 已提交
1288
        return_trace (true);
1289
    }
B
Behdad Esfahbod 已提交
1290
    return_trace (false);
1291 1292
  }

1293
  inline bool apply (hb_ot_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
1294
  {
B
Behdad Esfahbod 已提交
1295
    TRACE_APPLY (this);
1296
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
1297 1298
    for (unsigned int i = 0; i < num_rules; i++)
    {
B
Behdad Esfahbod 已提交
1299
      if ((this+rule[i]).apply (c, lookup_context))
B
Behdad Esfahbod 已提交
1300
        return_trace (true);
1301
    }
B
Behdad Esfahbod 已提交
1302
    return_trace (false);
1303 1304
  }

B
Behdad Esfahbod 已提交
1305 1306
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1307
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1308
    return_trace (rule.sanitize (c, this));
B
Behdad Esfahbod 已提交
1309 1310
  }

1311
  protected:
1312
  OffsetArrayOf<Rule>
1313
		rule;			/* Array of Rule tables
1314
					 * ordered by preference */
B
Behdad Esfahbod 已提交
1315
  public:
1316
  DEFINE_SIZE_ARRAY (2, rule);
1317 1318 1319
};


B
Behdad Esfahbod 已提交
1320 1321
struct ContextFormat1
{
1322
  inline void closure (hb_closure_context_t *c) const
1323
  {
B
Behdad Esfahbod 已提交
1324
    TRACE_CLOSURE (this);
1325 1326 1327 1328

    const Coverage &cov = (this+coverage);

    struct ContextClosureLookupContext lookup_context = {
1329
      {intersects_glyph},
B
Behdad Esfahbod 已提交
1330
      nullptr
1331 1332 1333 1334 1335 1336
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      if (cov.intersects_coverage (c->glyphs, i)) {
	const RuleSet &rule_set = this+ruleSet[i];
B
Behdad Esfahbod 已提交
1337
	rule_set.closure (c, lookup_context);
1338
      }
1339 1340
  }

B
Behdad Esfahbod 已提交
1341 1342
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
1343
    TRACE_COLLECT_GLYPHS (this);
B
Minor  
Behdad Esfahbod 已提交
1344
    (this+coverage).add_coverage (c->input);
1345 1346 1347

    struct ContextCollectGlyphsLookupContext lookup_context = {
      {collect_glyph},
B
Behdad Esfahbod 已提交
1348
      nullptr
1349 1350 1351 1352
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
1353
      (this+ruleSet[i]).collect_glyphs (c, lookup_context);
B
Behdad Esfahbod 已提交
1354 1355
  }

1356 1357
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1358
    TRACE_WOULD_APPLY (this);
1359

1360
    const RuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
1361
    struct ContextApplyLookupContext lookup_context = {
1362
      {match_glyph},
B
Behdad Esfahbod 已提交
1363
      nullptr
1364
    };
B
Behdad Esfahbod 已提交
1365
    return_trace (rule_set.would_apply (c, lookup_context));
1366 1367
  }

1368 1369 1370 1371 1372
  inline const Coverage &get_coverage (void) const
  {
    return this+coverage;
  }

1373
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1374
  {
B
Behdad Esfahbod 已提交
1375
    TRACE_APPLY (this);
1376
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
1377
    if (likely (index == NOT_COVERED))
B
Behdad Esfahbod 已提交
1378
      return_trace (false);
1379

1380
    const RuleSet &rule_set = this+ruleSet[index];
1381
    struct ContextApplyLookupContext lookup_context = {
1382
      {match_glyph},
B
Behdad Esfahbod 已提交
1383
      nullptr
1384
    };
B
Behdad Esfahbod 已提交
1385
    return_trace (rule_set.apply (c, lookup_context));
1386 1387
  }

B
Behdad Esfahbod 已提交
1388 1389
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1390
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1391
    return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
1392 1393
  }

1394
  protected:
B
Behdad Esfahbod 已提交
1395
  HBUINT16	format;			/* Format identifier--format = 1 */
1396 1397 1398 1399 1400 1401
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetArrayOf<RuleSet>
		ruleSet;		/* Array of RuleSet tables
					 * ordered by Coverage Index */
1402
  public:
1403
  DEFINE_SIZE_ARRAY (6, ruleSet);
1404 1405 1406
};


B
Behdad Esfahbod 已提交
1407 1408
struct ContextFormat2
{
1409
  inline void closure (hb_closure_context_t *c) const
1410
  {
B
Behdad Esfahbod 已提交
1411
    TRACE_CLOSURE (this);
1412
    if (!(this+coverage).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
1413
      return;
1414 1415 1416 1417

    const ClassDef &class_def = this+classDef;

    struct ContextClosureLookupContext lookup_context = {
1418
      {intersects_class},
1419
      &class_def
1420 1421 1422 1423 1424 1425
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      if (class_def.intersects_class (c->glyphs, i)) {
	const RuleSet &rule_set = this+ruleSet[i];
B
Behdad Esfahbod 已提交
1426
	rule_set.closure (c, lookup_context);
1427
      }
1428 1429
  }

B
Behdad Esfahbod 已提交
1430 1431
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
1432
    TRACE_COLLECT_GLYPHS (this);
B
Minor  
Behdad Esfahbod 已提交
1433
    (this+coverage).add_coverage (c->input);
1434

1435
    const ClassDef &class_def = this+classDef;
1436 1437
    struct ContextCollectGlyphsLookupContext lookup_context = {
      {collect_class},
1438
      &class_def
1439 1440 1441 1442
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
1443
      (this+ruleSet[i]).collect_glyphs (c, lookup_context);
B
Behdad Esfahbod 已提交
1444 1445
  }

1446 1447
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1448
    TRACE_WOULD_APPLY (this);
1449 1450

    const ClassDef &class_def = this+classDef;
1451
    unsigned int index = class_def.get_class (c->glyphs[0]);
1452 1453
    const RuleSet &rule_set = this+ruleSet[index];
    struct ContextApplyLookupContext lookup_context = {
1454
      {match_class},
1455 1456
      &class_def
    };
B
Behdad Esfahbod 已提交
1457
    return_trace (rule_set.would_apply (c, lookup_context));
1458 1459
  }

1460 1461 1462 1463 1464
  inline const Coverage &get_coverage (void) const
  {
    return this+coverage;
  }

1465
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1466
  {
B
Behdad Esfahbod 已提交
1467
    TRACE_APPLY (this);
1468
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1469
    if (likely (index == NOT_COVERED)) return_trace (false);
1470 1471

    const ClassDef &class_def = this+classDef;
1472
    index = class_def.get_class (c->buffer->cur().codepoint);
1473
    const RuleSet &rule_set = this+ruleSet[index];
1474
    struct ContextApplyLookupContext lookup_context = {
1475
      {match_class},
B
Behdad Esfahbod 已提交
1476
      &class_def
1477
    };
B
Behdad Esfahbod 已提交
1478
    return_trace (rule_set.apply (c, lookup_context));
1479 1480
  }

B
Behdad Esfahbod 已提交
1481 1482
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1483
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1484
    return_trace (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
1485 1486
  }

1487
  protected:
B
Behdad Esfahbod 已提交
1488
  HBUINT16	format;			/* Format identifier--format = 2 */
1489 1490 1491 1492 1493 1494 1495 1496 1497
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetTo<ClassDef>
		classDef;		/* Offset to glyph ClassDef table--from
					 * beginning of table */
  OffsetArrayOf<RuleSet>
		ruleSet;		/* Array of RuleSet tables
					 * ordered by class */
1498
  public:
1499
  DEFINE_SIZE_ARRAY (8, ruleSet);
1500 1501 1502
};


B
Behdad Esfahbod 已提交
1503 1504
struct ContextFormat3
{
1505
  inline void closure (hb_closure_context_t *c) const
1506
  {
B
Behdad Esfahbod 已提交
1507
    TRACE_CLOSURE (this);
1508
    if (!(this+coverageZ[0]).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
1509
      return;
1510

1511
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
1512
    struct ContextClosureLookupContext lookup_context = {
1513
      {intersects_coverage},
1514 1515
      this
    };
B
Behdad Esfahbod 已提交
1516
    context_closure_lookup (c,
B
Behdad Esfahbod 已提交
1517
			    glyphCount, (const HBUINT16 *) (coverageZ + 1),
B
Behdad Esfahbod 已提交
1518 1519
			    lookupCount, lookupRecord,
			    lookup_context);
1520 1521
  }

B
Behdad Esfahbod 已提交
1522 1523
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
1524
    TRACE_COLLECT_GLYPHS (this);
1525
    (this+coverageZ[0]).add_coverage (c->input);
1526

1527
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
1528 1529
    struct ContextCollectGlyphsLookupContext lookup_context = {
      {collect_coverage},
1530
      this
1531 1532 1533
    };

    context_collect_glyphs_lookup (c,
B
Behdad Esfahbod 已提交
1534
				   glyphCount, (const HBUINT16 *) (coverageZ + 1),
1535 1536
				   lookupCount, lookupRecord,
				   lookup_context);
B
Behdad Esfahbod 已提交
1537 1538
  }

1539 1540
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1541
    TRACE_WOULD_APPLY (this);
1542

1543
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
1544
    struct ContextApplyLookupContext lookup_context = {
1545
      {match_coverage},
1546 1547
      this
    };
B
Behdad Esfahbod 已提交
1548
    return_trace (context_would_apply_lookup (c, glyphCount, (const HBUINT16 *) (coverageZ + 1), lookupCount, lookupRecord, lookup_context));
1549 1550
  }

1551 1552
  inline const Coverage &get_coverage (void) const
  {
1553
    return this+coverageZ[0];
1554 1555
  }

1556
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1557
  {
B
Behdad Esfahbod 已提交
1558
    TRACE_APPLY (this);
1559
    unsigned int index = (this+coverageZ[0]).get_coverage (c->buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1560
    if (likely (index == NOT_COVERED)) return_trace (false);
1561

1562
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * glyphCount);
1563
    struct ContextApplyLookupContext lookup_context = {
1564
      {match_coverage},
B
Behdad Esfahbod 已提交
1565
      this
1566
    };
B
Behdad Esfahbod 已提交
1567
    return_trace (context_apply_lookup (c, glyphCount, (const HBUINT16 *) (coverageZ + 1), lookupCount, lookupRecord, lookup_context));
1568 1569
  }

B
Behdad Esfahbod 已提交
1570 1571
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1572
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1573
    if (!c->check_struct (this)) return_trace (false);
B
Behdad Esfahbod 已提交
1574
    unsigned int count = glyphCount;
B
Behdad Esfahbod 已提交
1575 1576
    if (!count) return_trace (false); /* We want to access coverageZ[0] freely. */
    if (!c->check_array (coverageZ, coverageZ[0].static_size, count)) return_trace (false);
B
Behdad Esfahbod 已提交
1577
    for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
1578
      if (!coverageZ[i].sanitize (c, this)) return_trace (false);
B
Behdad Esfahbod 已提交
1579
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ, coverageZ[0].static_size * count);
B
Behdad Esfahbod 已提交
1580
    return_trace (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
B
Behdad Esfahbod 已提交
1581 1582
  }

1583
  protected:
B
Behdad Esfahbod 已提交
1584 1585
  HBUINT16	format;			/* Format identifier--format = 3 */
  HBUINT16	glyphCount;		/* Number of glyphs in the input glyph
1586
					 * sequence */
B
Behdad Esfahbod 已提交
1587
  HBUINT16	lookupCount;		/* Number of LookupRecords */
1588
  OffsetTo<Coverage>
1589
		coverageZ[VAR];		/* Array of offsets to Coverage
1590
					 * table in glyph sequence order */
1591
/*LookupRecord	lookupRecordX[VAR];*/	/* Array of LookupRecords--in
1592
					 * design order */
B
Behdad Esfahbod 已提交
1593
  public:
1594
  DEFINE_SIZE_ARRAY (6, coverageZ);
1595 1596
};

B
Behdad Esfahbod 已提交
1597 1598
struct Context
{
1599
  template <typename context_t>
1600
  inline typename context_t::return_t dispatch (context_t *c) const
1601
  {
1602
    TRACE_DISPATCH (this, u.format);
1603
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1604
    switch (u.format) {
B
Behdad Esfahbod 已提交
1605 1606 1607 1608
    case 1: return_trace (c->dispatch (u.format1));
    case 2: return_trace (c->dispatch (u.format2));
    case 3: return_trace (c->dispatch (u.format3));
    default:return_trace (c->default_return_value ());
1609 1610 1611
    }
  }

1612
  protected:
1613
  union {
B
Behdad Esfahbod 已提交
1614
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1615 1616 1617
  ContextFormat1	format1;
  ContextFormat2	format2;
  ContextFormat3	format3;
1618 1619 1620
  } u;
};

1621 1622 1623

/* Chaining Contextual lookups */

1624
struct ChainContextClosureLookupContext
B
Behdad Esfahbod 已提交
1625
{
1626 1627 1628 1629
  ContextClosureFuncs funcs;
  const void *intersects_data[3];
};

1630 1631 1632 1633 1634 1635
struct ChainContextCollectGlyphsLookupContext
{
  ContextCollectGlyphsFuncs funcs;
  const void *collect_data[3];
};

1636 1637 1638
struct ChainContextApplyLookupContext
{
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
1639
  const void *match_data[3];
1640 1641
};

B
Behdad Esfahbod 已提交
1642
static inline void chain_context_closure_lookup (hb_closure_context_t *c,
1643
						 unsigned int backtrackCount,
B
Behdad Esfahbod 已提交
1644
						 const HBUINT16 backtrack[],
1645
						 unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1646
						 const HBUINT16 input[], /* Array of input values--start with second glyph */
1647
						 unsigned int lookaheadCount,
B
Behdad Esfahbod 已提交
1648
						 const HBUINT16 lookahead[],
1649 1650 1651 1652
						 unsigned int lookupCount,
						 const LookupRecord lookupRecord[],
						 ChainContextClosureLookupContext &lookup_context)
{
B
Behdad Esfahbod 已提交
1653 1654 1655 1656 1657 1658
  if (intersects_array (c,
			backtrackCount, backtrack,
			lookup_context.funcs.intersects, lookup_context.intersects_data[0])
   && intersects_array (c,
			inputCount ? inputCount - 1 : 0, input,
			lookup_context.funcs.intersects, lookup_context.intersects_data[1])
B
Minor  
Behdad Esfahbod 已提交
1659
   && intersects_array (c,
B
Behdad Esfahbod 已提交
1660 1661
		       lookaheadCount, lookahead,
		       lookup_context.funcs.intersects, lookup_context.intersects_data[2]))
1662 1663
    recurse_lookups (c,
		     lookupCount, lookupRecord);
1664 1665
}

1666 1667
static inline void chain_context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
						        unsigned int backtrackCount,
B
Behdad Esfahbod 已提交
1668
						        const HBUINT16 backtrack[],
1669
						        unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1670
						        const HBUINT16 input[], /* Array of input values--start with second glyph */
1671
						        unsigned int lookaheadCount,
B
Behdad Esfahbod 已提交
1672
						        const HBUINT16 lookahead[],
1673 1674 1675 1676
						        unsigned int lookupCount,
						        const LookupRecord lookupRecord[],
						        ChainContextCollectGlyphsLookupContext &lookup_context)
{
B
Minor  
Behdad Esfahbod 已提交
1677
  collect_array (c, c->before,
1678 1679
		 backtrackCount, backtrack,
		 lookup_context.funcs.collect, lookup_context.collect_data[0]);
B
Minor  
Behdad Esfahbod 已提交
1680
  collect_array (c, c->input,
1681 1682
		 inputCount ? inputCount - 1 : 0, input,
		 lookup_context.funcs.collect, lookup_context.collect_data[1]);
B
Minor  
Behdad Esfahbod 已提交
1683
  collect_array (c, c->after,
1684 1685 1686 1687 1688 1689
		 lookaheadCount, lookahead,
		 lookup_context.funcs.collect, lookup_context.collect_data[2]);
  recurse_lookups (c,
		   lookupCount, lookupRecord);
}

1690 1691
static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c,
						     unsigned int backtrackCount,
B
Behdad Esfahbod 已提交
1692
						     const HBUINT16 backtrack[] HB_UNUSED,
1693
						     unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1694
						     const HBUINT16 input[], /* Array of input values--start with second glyph */
1695
						     unsigned int lookaheadCount,
B
Behdad Esfahbod 已提交
1696
						     const HBUINT16 lookahead[] HB_UNUSED,
B
Behdad Esfahbod 已提交
1697 1698
						     unsigned int lookupCount HB_UNUSED,
						     const LookupRecord lookupRecord[] HB_UNUSED,
1699 1700
						     ChainContextApplyLookupContext &lookup_context)
{
1701
  return (c->zero_context ? !backtrackCount && !lookaheadCount : true)
1702
      && would_match_input (c,
1703 1704 1705 1706
			    inputCount, input,
			    lookup_context.funcs.match, lookup_context.match_data[1]);
}

1707
static inline bool chain_context_apply_lookup (hb_ot_apply_context_t *c,
1708
					       unsigned int backtrackCount,
B
Behdad Esfahbod 已提交
1709
					       const HBUINT16 backtrack[],
1710
					       unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1711
					       const HBUINT16 input[], /* Array of input values--start with second glyph */
1712
					       unsigned int lookaheadCount,
B
Behdad Esfahbod 已提交
1713
					       const HBUINT16 lookahead[],
1714 1715 1716
					       unsigned int lookupCount,
					       const LookupRecord lookupRecord[],
					       ChainContextApplyLookupContext &lookup_context)
1717
{
1718
  unsigned int start_index = 0, match_length = 0, end_index = 0;
1719
  unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
B
Behdad Esfahbod 已提交
1720
  return match_input (c,
B
Behdad Esfahbod 已提交
1721 1722
		      inputCount, input,
		      lookup_context.funcs.match, lookup_context.match_data[1],
1723
		      &match_length, match_positions)
B
Behdad Esfahbod 已提交
1724 1725
      && match_backtrack (c,
			  backtrackCount, backtrack,
1726 1727
			  lookup_context.funcs.match, lookup_context.match_data[0],
			  &start_index)
B
Behdad Esfahbod 已提交
1728
      && match_lookahead (c,
B
Behdad Esfahbod 已提交
1729 1730
			  lookaheadCount, lookahead,
			  lookup_context.funcs.match, lookup_context.match_data[2],
1731 1732 1733
			  match_length, &end_index)
      && (c->buffer->unsafe_to_break_from_outbuffer (start_index, end_index),
          apply_lookup (c,
1734 1735
		       inputCount, match_positions,
		       lookupCount, lookupRecord,
1736
		       match_length));
1737
}
1738

B
Behdad Esfahbod 已提交
1739 1740
struct ChainRule
{
B
Behdad Esfahbod 已提交
1741
  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
1742
  {
B
Behdad Esfahbod 已提交
1743
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
1744 1745
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
1746
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
1747
    chain_context_closure_lookup (c,
1748 1749 1750 1751
				  backtrack.len, backtrack.arrayZ,
				  input.len, input.arrayZ,
				  lookahead.len, lookahead.arrayZ,
				  lookup.len, lookup.arrayZ,
B
Behdad Esfahbod 已提交
1752
				  lookup_context);
1753 1754
  }

1755 1756 1757
  inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
  {
    TRACE_COLLECT_GLYPHS (this);
B
Behdad Esfahbod 已提交
1758 1759
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
1760 1761
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
    chain_context_collect_glyphs_lookup (c,
1762 1763 1764 1765
					 backtrack.len, backtrack.arrayZ,
					 input.len, input.arrayZ,
					 lookahead.len, lookahead.arrayZ,
					 lookup.len, lookup.arrayZ,
1766 1767 1768
					 lookup_context);
  }

1769 1770
  inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
  {
B
Behdad Esfahbod 已提交
1771
    TRACE_WOULD_APPLY (this);
B
Behdad Esfahbod 已提交
1772 1773
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
1774
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
1775
    return_trace (chain_context_would_apply_lookup (c,
1776 1777 1778 1779
						    backtrack.len, backtrack.arrayZ,
						    input.len, input.arrayZ,
						    lookahead.len, lookahead.arrayZ, lookup.len,
						    lookup.arrayZ, lookup_context));
1780 1781
  }

1782
  inline bool apply (hb_ot_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
1783
  {
B
Behdad Esfahbod 已提交
1784
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1785 1786
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
1787
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
1788
    return_trace (chain_context_apply_lookup (c,
1789 1790 1791 1792
					      backtrack.len, backtrack.arrayZ,
					      input.len, input.arrayZ,
					      lookahead.len, lookahead.arrayZ, lookup.len,
					      lookup.arrayZ, lookup_context));
1793 1794
  }

B
Behdad Esfahbod 已提交
1795 1796
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1797
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1798
    if (!backtrack.sanitize (c)) return_trace (false);
B
Behdad Esfahbod 已提交
1799
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
B
Behdad Esfahbod 已提交
1800
    if (!input.sanitize (c)) return_trace (false);
B
Behdad Esfahbod 已提交
1801
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
B
Behdad Esfahbod 已提交
1802
    if (!lookahead.sanitize (c)) return_trace (false);
B
Behdad Esfahbod 已提交
1803
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
1804
    return_trace (lookup.sanitize (c));
B
Behdad Esfahbod 已提交
1805
  }
1806

1807
  protected:
B
Behdad Esfahbod 已提交
1808
  ArrayOf<HBUINT16>
B
Behdad Esfahbod 已提交
1809
		backtrack;		/* Array of backtracking values
1810 1811
					 * (to be matched before the input
					 * sequence) */
B
Behdad Esfahbod 已提交
1812
  HeadlessArrayOf<HBUINT16>
B
Behdad Esfahbod 已提交
1813
		inputX;			/* Array of input values (start with
1814
					 * second glyph) */
B
Behdad Esfahbod 已提交
1815
  ArrayOf<HBUINT16>
B
Behdad Esfahbod 已提交
1816
		lookaheadX;		/* Array of lookahead values's (to be
1817
					 * matched after the input sequence) */
B
Behdad Esfahbod 已提交
1818
  ArrayOf<LookupRecord>
1819
		lookupX;		/* Array of LookupRecords--in
1820
					 * design order) */
1821
  public:
B
Behdad Esfahbod 已提交
1822
  DEFINE_SIZE_MIN (8);
1823 1824
};

B
Behdad Esfahbod 已提交
1825 1826
struct ChainRuleSet
{
B
Behdad Esfahbod 已提交
1827
  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
1828
  {
B
Behdad Esfahbod 已提交
1829
    TRACE_CLOSURE (this);
1830 1831
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
1832
      (this+rule[i]).closure (c, lookup_context);
1833 1834
  }

1835 1836 1837 1838 1839 1840 1841 1842
  inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
  {
    TRACE_COLLECT_GLYPHS (this);
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
      (this+rule[i]).collect_glyphs (c, lookup_context);
  }

1843 1844
  inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
  {
B
Behdad Esfahbod 已提交
1845
    TRACE_WOULD_APPLY (this);
1846 1847 1848
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
      if ((this+rule[i]).would_apply (c, lookup_context))
B
Behdad Esfahbod 已提交
1849
        return_trace (true);
1850

B
Behdad Esfahbod 已提交
1851
    return_trace (false);
1852 1853
  }

1854
  inline bool apply (hb_ot_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
1855
  {
B
Behdad Esfahbod 已提交
1856
    TRACE_APPLY (this);
1857
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
1858
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
1859
      if ((this+rule[i]).apply (c, lookup_context))
B
Behdad Esfahbod 已提交
1860
        return_trace (true);
1861

B
Behdad Esfahbod 已提交
1862
    return_trace (false);
1863
  }
1864

B
Behdad Esfahbod 已提交
1865 1866
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1867
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1868
    return_trace (rule.sanitize (c, this));
B
Behdad Esfahbod 已提交
1869 1870
  }

1871
  protected:
1872 1873 1874
  OffsetArrayOf<ChainRule>
		rule;			/* Array of ChainRule tables
					 * ordered by preference */
1875
  public:
1876
  DEFINE_SIZE_ARRAY (2, rule);
1877 1878
};

B
Behdad Esfahbod 已提交
1879 1880
struct ChainContextFormat1
{
1881
  inline void closure (hb_closure_context_t *c) const
1882
  {
B
Behdad Esfahbod 已提交
1883
    TRACE_CLOSURE (this);
1884 1885 1886
    const Coverage &cov = (this+coverage);

    struct ChainContextClosureLookupContext lookup_context = {
1887
      {intersects_glyph},
B
Behdad Esfahbod 已提交
1888
      {nullptr, nullptr, nullptr}
1889 1890 1891 1892 1893 1894
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      if (cov.intersects_coverage (c->glyphs, i)) {
	const ChainRuleSet &rule_set = this+ruleSet[i];
B
Behdad Esfahbod 已提交
1895
	rule_set.closure (c, lookup_context);
1896
      }
1897 1898
  }

B
Behdad Esfahbod 已提交
1899 1900
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
1901
    TRACE_COLLECT_GLYPHS (this);
B
Minor  
Behdad Esfahbod 已提交
1902
    (this+coverage).add_coverage (c->input);
1903 1904 1905

    struct ChainContextCollectGlyphsLookupContext lookup_context = {
      {collect_glyph},
B
Behdad Esfahbod 已提交
1906
      {nullptr, nullptr, nullptr}
1907 1908 1909 1910 1911
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      (this+ruleSet[i]).collect_glyphs (c, lookup_context);
B
Behdad Esfahbod 已提交
1912 1913
  }

1914 1915
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1916
    TRACE_WOULD_APPLY (this);
1917

1918
    const ChainRuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
1919
    struct ChainContextApplyLookupContext lookup_context = {
1920
      {match_glyph},
B
Behdad Esfahbod 已提交
1921
      {nullptr, nullptr, nullptr}
1922
    };
B
Behdad Esfahbod 已提交
1923
    return_trace (rule_set.would_apply (c, lookup_context));
1924 1925
  }

1926 1927 1928 1929 1930
  inline const Coverage &get_coverage (void) const
  {
    return this+coverage;
  }

1931
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1932
  {
B
Behdad Esfahbod 已提交
1933
    TRACE_APPLY (this);
1934
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1935
    if (likely (index == NOT_COVERED)) return_trace (false);
1936

1937
    const ChainRuleSet &rule_set = this+ruleSet[index];
1938
    struct ChainContextApplyLookupContext lookup_context = {
1939
      {match_glyph},
B
Behdad Esfahbod 已提交
1940
      {nullptr, nullptr, nullptr}
1941
    };
B
Behdad Esfahbod 已提交
1942
    return_trace (rule_set.apply (c, lookup_context));
1943
  }
B
Behdad Esfahbod 已提交
1944

B
Behdad Esfahbod 已提交
1945 1946
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1947
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1948
    return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
1949 1950
  }

1951
  protected:
B
Behdad Esfahbod 已提交
1952
  HBUINT16	format;			/* Format identifier--format = 1 */
1953 1954
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
1955
					 * beginning of table */
1956 1957 1958
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by Coverage Index */
1959
  public:
1960
  DEFINE_SIZE_ARRAY (6, ruleSet);
1961 1962
};

B
Behdad Esfahbod 已提交
1963 1964
struct ChainContextFormat2
{
1965
  inline void closure (hb_closure_context_t *c) const
1966
  {
B
Behdad Esfahbod 已提交
1967
    TRACE_CLOSURE (this);
1968
    if (!(this+coverage).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
1969
      return;
1970 1971 1972 1973 1974 1975

    const ClassDef &backtrack_class_def = this+backtrackClassDef;
    const ClassDef &input_class_def = this+inputClassDef;
    const ClassDef &lookahead_class_def = this+lookaheadClassDef;

    struct ChainContextClosureLookupContext lookup_context = {
1976
      {intersects_class},
1977 1978 1979 1980 1981 1982 1983 1984 1985
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      if (input_class_def.intersects_class (c->glyphs, i)) {
	const ChainRuleSet &rule_set = this+ruleSet[i];
B
Behdad Esfahbod 已提交
1986
	rule_set.closure (c, lookup_context);
1987
      }
1988 1989
  }

B
Behdad Esfahbod 已提交
1990 1991
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
1992
    TRACE_COLLECT_GLYPHS (this);
B
Minor  
Behdad Esfahbod 已提交
1993
    (this+coverage).add_coverage (c->input);
1994

1995 1996 1997 1998
    const ClassDef &backtrack_class_def = this+backtrackClassDef;
    const ClassDef &input_class_def = this+inputClassDef;
    const ClassDef &lookahead_class_def = this+lookaheadClassDef;

1999 2000
    struct ChainContextCollectGlyphsLookupContext lookup_context = {
      {collect_class},
2001 2002 2003
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
2004 2005 2006 2007 2008
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      (this+ruleSet[i]).collect_glyphs (c, lookup_context);
B
Behdad Esfahbod 已提交
2009 2010
  }

2011 2012
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2013
    TRACE_WOULD_APPLY (this);
2014

2015
    const ClassDef &backtrack_class_def = this+backtrackClassDef;
2016
    const ClassDef &input_class_def = this+inputClassDef;
2017
    const ClassDef &lookahead_class_def = this+lookaheadClassDef;
2018

2019
    unsigned int index = input_class_def.get_class (c->glyphs[0]);
2020 2021
    const ChainRuleSet &rule_set = this+ruleSet[index];
    struct ChainContextApplyLookupContext lookup_context = {
2022
      {match_class},
2023 2024 2025
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
2026
    };
B
Behdad Esfahbod 已提交
2027
    return_trace (rule_set.would_apply (c, lookup_context));
2028 2029
  }

2030 2031 2032 2033 2034
  inline const Coverage &get_coverage (void) const
  {
    return this+coverage;
  }

2035
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
2036
  {
B
Behdad Esfahbod 已提交
2037
    TRACE_APPLY (this);
2038
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
2039
    if (likely (index == NOT_COVERED)) return_trace (false);
2040 2041 2042 2043 2044

    const ClassDef &backtrack_class_def = this+backtrackClassDef;
    const ClassDef &input_class_def = this+inputClassDef;
    const ClassDef &lookahead_class_def = this+lookaheadClassDef;

2045
    index = input_class_def.get_class (c->buffer->cur().codepoint);
2046
    const ChainRuleSet &rule_set = this+ruleSet[index];
2047
    struct ChainContextApplyLookupContext lookup_context = {
2048
      {match_class},
B
Behdad Esfahbod 已提交
2049 2050 2051
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
2052
    };
B
Behdad Esfahbod 已提交
2053
    return_trace (rule_set.apply (c, lookup_context));
2054 2055
  }

B
Behdad Esfahbod 已提交
2056 2057
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2058
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
2059 2060 2061 2062 2063
    return_trace (coverage.sanitize (c, this) &&
		  backtrackClassDef.sanitize (c, this) &&
		  inputClassDef.sanitize (c, this) &&
		  lookaheadClassDef.sanitize (c, this) &&
		  ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
2064 2065
  }

2066
  protected:
B
Behdad Esfahbod 已提交
2067
  HBUINT16	format;			/* Format identifier--format = 2 */
2068 2069
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
2070
					 * beginning of table */
2071 2072
  OffsetTo<ClassDef>
		backtrackClassDef;	/* Offset to glyph ClassDef table
2073 2074
					 * containing backtrack sequence
					 * data--from beginning of table */
2075 2076
  OffsetTo<ClassDef>
		inputClassDef;		/* Offset to glyph ClassDef
2077 2078
					 * table containing input sequence
					 * data--from beginning of table */
2079 2080
  OffsetTo<ClassDef>
		lookaheadClassDef;	/* Offset to glyph ClassDef table
2081 2082
					 * containing lookahead sequence
					 * data--from beginning of table */
2083 2084 2085
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by class */
2086
  public:
2087
  DEFINE_SIZE_ARRAY (12, ruleSet);
2088 2089
};

B
Behdad Esfahbod 已提交
2090 2091
struct ChainContextFormat3
{
2092
  inline void closure (hb_closure_context_t *c) const
2093
  {
B
Behdad Esfahbod 已提交
2094
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
2095 2096 2097 2098 2099 2100 2101 2102
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);

    if (!(this+input[0]).intersects (c->glyphs))
      return;

    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
    struct ChainContextClosureLookupContext lookup_context = {
2103
      {intersects_coverage},
B
Behdad Esfahbod 已提交
2104 2105 2106
      {this, this, this}
    };
    chain_context_closure_lookup (c,
2107 2108 2109 2110
				  backtrack.len, (const HBUINT16 *) backtrack.arrayZ,
				  input.len, (const HBUINT16 *) input.arrayZ + 1,
				  lookahead.len, (const HBUINT16 *) lookahead.arrayZ,
				  lookup.len, lookup.arrayZ,
B
Behdad Esfahbod 已提交
2111
				  lookup_context);
2112 2113
  }

B
Behdad Esfahbod 已提交
2114 2115
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
2116 2117 2118
    TRACE_COLLECT_GLYPHS (this);
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);

B
Minor  
Behdad Esfahbod 已提交
2119
    (this+input[0]).add_coverage (c->input);
2120 2121 2122 2123 2124 2125 2126 2127

    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
    struct ChainContextCollectGlyphsLookupContext lookup_context = {
      {collect_coverage},
      {this, this, this}
    };
    chain_context_collect_glyphs_lookup (c,
2128 2129 2130 2131
					 backtrack.len, (const HBUINT16 *) backtrack.arrayZ,
					 input.len, (const HBUINT16 *) input.arrayZ + 1,
					 lookahead.len, (const HBUINT16 *) lookahead.arrayZ,
					 lookup.len, lookup.arrayZ,
2132
					 lookup_context);
B
Behdad Esfahbod 已提交
2133 2134
  }

2135 2136
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2137
    TRACE_WOULD_APPLY (this);
2138

B
Behdad Esfahbod 已提交
2139
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2140 2141 2142
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
    struct ChainContextApplyLookupContext lookup_context = {
2143
      {match_coverage},
2144 2145
      {this, this, this}
    };
B
Behdad Esfahbod 已提交
2146
    return_trace (chain_context_would_apply_lookup (c,
2147 2148 2149 2150
						    backtrack.len, (const HBUINT16 *) backtrack.arrayZ,
						    input.len, (const HBUINT16 *) input.arrayZ + 1,
						    lookahead.len, (const HBUINT16 *) lookahead.arrayZ,
						    lookup.len, lookup.arrayZ, lookup_context));
2151 2152
  }

2153 2154 2155 2156 2157 2158
  inline const Coverage &get_coverage (void) const
  {
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
    return this+input[0];
  }

2159
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
2160
  {
B
Behdad Esfahbod 已提交
2161
    TRACE_APPLY (this);
2162
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2163

2164
    unsigned int index = (this+input[0]).get_coverage (c->buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
2165
    if (likely (index == NOT_COVERED)) return_trace (false);
2166

2167 2168
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2169
    struct ChainContextApplyLookupContext lookup_context = {
2170
      {match_coverage},
B
Behdad Esfahbod 已提交
2171
      {this, this, this}
2172
    };
B
Behdad Esfahbod 已提交
2173
    return_trace (chain_context_apply_lookup (c,
2174 2175 2176 2177
					      backtrack.len, (const HBUINT16 *) backtrack.arrayZ,
					      input.len, (const HBUINT16 *) input.arrayZ + 1,
					      lookahead.len, (const HBUINT16 *) lookahead.arrayZ,
					      lookup.len, lookup.arrayZ, lookup_context));
2178 2179
  }

B
Behdad Esfahbod 已提交
2180 2181
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2182
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
2183
    if (!backtrack.sanitize (c, this)) return_trace (false);
B
Behdad Esfahbod 已提交
2184
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
B
Behdad Esfahbod 已提交
2185 2186
    if (!input.sanitize (c, this)) return_trace (false);
    if (!input.len) return_trace (false); /* To be consistent with Context. */
B
Behdad Esfahbod 已提交
2187
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
B
Behdad Esfahbod 已提交
2188
    if (!lookahead.sanitize (c, this)) return_trace (false);
B
Behdad Esfahbod 已提交
2189
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
2190
    return_trace (lookup.sanitize (c));
B
Behdad Esfahbod 已提交
2191 2192
  }

2193
  protected:
B
Behdad Esfahbod 已提交
2194
  HBUINT16	format;			/* Format identifier--format = 3 */
B
Behdad Esfahbod 已提交
2195
  OffsetArrayOf<Coverage>
2196
		backtrack;		/* Array of coverage tables
2197 2198
					 * in backtracking sequence, in  glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
2199
  OffsetArrayOf<Coverage>
2200
		inputX		;	/* Array of coverage
2201 2202
					 * tables in input sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
2203
  OffsetArrayOf<Coverage>
2204
		lookaheadX;		/* Array of coverage tables
2205 2206
					 * in lookahead sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
2207
  ArrayOf<LookupRecord>
2208
		lookupX;		/* Array of LookupRecords--in
B
Behdad Esfahbod 已提交
2209
					 * design order) */
2210
  public:
B
Behdad Esfahbod 已提交
2211
  DEFINE_SIZE_MIN (10);
2212 2213
};

B
Behdad Esfahbod 已提交
2214 2215
struct ChainContext
{
2216
  template <typename context_t>
2217
  inline typename context_t::return_t dispatch (context_t *c) const
2218
  {
2219
    TRACE_DISPATCH (this, u.format);
2220
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
2221
    switch (u.format) {
B
Behdad Esfahbod 已提交
2222 2223 2224 2225
    case 1: return_trace (c->dispatch (u.format1));
    case 2: return_trace (c->dispatch (u.format2));
    case 3: return_trace (c->dispatch (u.format3));
    default:return_trace (c->default_return_value ());
2226 2227 2228
    }
  }

2229
  protected:
2230
  union {
B
Behdad Esfahbod 已提交
2231
  HBUINT16		format;	/* Format identifier */
B
Behdad Esfahbod 已提交
2232 2233 2234
  ChainContextFormat1	format1;
  ChainContextFormat2	format2;
  ChainContextFormat3	format3;
2235 2236 2237 2238
  } u;
};


2239
template <typename T>
2240 2241 2242 2243
struct ExtensionFormat1
{
  inline unsigned int get_type (void) const { return extensionLookupType; }

2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
  template <typename X>
  inline const X& get_subtable (void) const
  {
    unsigned int offset = extensionOffset;
    if (unlikely (!offset)) return Null(typename T::LookupSubTable);
    return StructAtOffset<typename T::LookupSubTable> (this, offset);
  }

  template <typename context_t>
  inline typename context_t::return_t dispatch (context_t *c) const
  {
    TRACE_DISPATCH (this, format);
2256
    if (unlikely (!c->may_dispatch (this, this))) return_trace (c->no_dispatch_return_value ());
B
Behdad Esfahbod 已提交
2257
    return_trace (get_subtable<typename T::LookupSubTable> ().dispatch (c, get_type ()));
2258 2259 2260
  }

  /* This is called from may_dispatch() above with hb_sanitize_context_t. */
B
Behdad Esfahbod 已提交
2261 2262
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2263
    TRACE_SANITIZE (this);
2264 2265 2266
    return_trace (c->check_struct (this) &&
		  extensionOffset != 0 &&
		  extensionLookupType != T::LookupSubTable::Extension);
B
Behdad Esfahbod 已提交
2267 2268
  }

2269
  protected:
B
Behdad Esfahbod 已提交
2270 2271
  HBUINT16	format;			/* Format identifier. Set to 1. */
  HBUINT16	extensionLookupType;	/* Lookup type of subtable referenced
2272 2273
					 * by ExtensionOffset (i.e. the
					 * extension subtable). */
B
Behdad Esfahbod 已提交
2274
  HBUINT32	extensionOffset;	/* Offset to the extension subtable,
2275
					 * of lookup type subtable. */
2276 2277
  public:
  DEFINE_SIZE_STATIC (8);
2278 2279
};

B
Behdad Esfahbod 已提交
2280
template <typename T>
2281 2282 2283 2284 2285
struct Extension
{
  inline unsigned int get_type (void) const
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
2286
    case 1: return u.format1.get_type ();
2287 2288 2289
    default:return 0;
    }
  }
2290 2291 2292
  template <typename X>
  inline const X& get_subtable (void) const
  {
2293 2294 2295 2296
    switch (u.format) {
    case 1: return u.format1.template get_subtable<typename T::LookupSubTable> ();
    default:return Null(typename T::LookupSubTable);
    }
2297 2298
  }

B
Behdad Esfahbod 已提交
2299
  template <typename context_t>
2300
  inline typename context_t::return_t dispatch (context_t *c) const
B
Behdad Esfahbod 已提交
2301
  {
2302
    TRACE_DISPATCH (this, u.format);
2303
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
B
Behdad Esfahbod 已提交
2304
    switch (u.format) {
B
Behdad Esfahbod 已提交
2305 2306
    case 1: return_trace (u.format1.dispatch (c));
    default:return_trace (c->default_return_value ());
B
Behdad Esfahbod 已提交
2307 2308 2309
    }
  }

2310
  protected:
2311
  union {
B
Behdad Esfahbod 已提交
2312
  HBUINT16		format;		/* Format identifier */
2313
  ExtensionFormat1<T>	format1;
2314 2315 2316 2317
  } u;
};


B
Behdad Esfahbod 已提交
2318 2319 2320 2321
/*
 * GSUB/GPOS Common
 */

B
Behdad Esfahbod 已提交
2322 2323
struct GSUBGPOS
{
2324
  inline bool has_data (void) const { return version.to_int () != 0; }
2325 2326 2327 2328
  inline unsigned int get_script_count (void) const
  { return (this+scriptList).len; }
  inline const Tag& get_script_tag (unsigned int i) const
  { return (this+scriptList).get_tag (i); }
B
Behdad Esfahbod 已提交
2329 2330 2331 2332
  inline unsigned int get_script_tags (unsigned int start_offset,
				       unsigned int *script_count /* IN/OUT */,
				       hb_tag_t     *script_tags /* OUT */) const
  { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
2333 2334 2335 2336 2337 2338 2339
  inline const Script& get_script (unsigned int i) const
  { return (this+scriptList)[i]; }
  inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
  { return (this+scriptList).find_index (tag, index); }

  inline unsigned int get_feature_count (void) const
  { return (this+featureList).len; }
2340 2341
  inline hb_tag_t get_feature_tag (unsigned int i) const
  { return i == Index::NOT_FOUND_INDEX ? HB_TAG_NONE : (this+featureList).get_tag (i); }
B
Behdad Esfahbod 已提交
2342 2343 2344 2345
  inline unsigned int get_feature_tags (unsigned int start_offset,
					unsigned int *feature_count /* IN/OUT */,
					hb_tag_t     *feature_tags /* OUT */) const
  { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
2346 2347 2348 2349 2350 2351 2352 2353 2354
  inline const Feature& get_feature (unsigned int i) const
  { return (this+featureList)[i]; }
  inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
  { return (this+featureList).find_index (tag, index); }

  inline unsigned int get_lookup_count (void) const
  { return (this+lookupList).len; }
  inline const Lookup& get_lookup (unsigned int i) const
  { return (this+lookupList)[i]; }
B
Behdad Esfahbod 已提交
2355

2356 2357
  inline bool find_variations_index (const int *coords, unsigned int num_coords,
				     unsigned int *index) const
2358
  { return (version.to_int () >= 0x00010001u ? this+featureVars : Null(FeatureVariations))
2359
	   .find_index (coords, num_coords, index); }
2360 2361 2362 2363 2364 2365
  inline const Feature& get_feature_variation (unsigned int feature_index,
					       unsigned int variations_index) const
  {
    if (FeatureVariations::NOT_FOUND_INDEX != variations_index &&
	version.to_int () >= 0x00010001u)
    {
2366 2367
      const Feature *feature = (this+featureVars).find_substitute (variations_index,
								   feature_index);
2368 2369 2370 2371 2372
      if (feature)
        return *feature;
    }
    return get_feature (feature_index);
  }
2373

B
Behdad Esfahbod 已提交
2374 2375
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2376
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
2377 2378 2379 2380
    return_trace (version.sanitize (c) &&
		  likely (version.major == 1) &&
		  scriptList.sanitize (c, this) &&
		  featureList.sanitize (c, this) &&
2381 2382
		  lookupList.sanitize (c, this) &&
		  (version.to_int () < 0x00010001u || featureVars.sanitize (c, this)));
B
Behdad Esfahbod 已提交
2383 2384
  }

2385
  protected:
B
Behdad Esfahbod 已提交
2386
  FixedVersion<>version;	/* Version of the GSUB/GPOS table--initially set
2387
				 * to 0x00010000u */
B
Behdad Esfahbod 已提交
2388 2389 2390 2391 2392 2393
  OffsetTo<ScriptList>
		scriptList;  	/* ScriptList table */
  OffsetTo<FeatureList>
		featureList; 	/* FeatureList table */
  OffsetTo<LookupList>
		lookupList; 	/* LookupList table */
B
Behdad Esfahbod 已提交
2394
  LOffsetTo<FeatureVariations>
2395 2396 2397 2398
		featureVars;	/* Offset to Feature Variations
				   table--from beginning of table
				 * (may be NULL).  Introduced
				 * in version 0x00010001. */
2399
  public:
2400
  DEFINE_SIZE_MIN (10);
B
Behdad Esfahbod 已提交
2401
};
2402

2403

B
Behdad Esfahbod 已提交
2404
} /* namespace OT */
2405

B
Behdad Esfahbod 已提交
2406

2407
#endif /* HB_OT_LAYOUT_GSUBGPOS_HH */