hb-ot-layout-gsubgpos.hh 87.6 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 35
#include "hb.hh"
#include "hb-buffer.hh"
#include "hb-map.hh"
#include "hb-set.hh"
36
#include "hb-ot-map.hh"
37 38
#include "hb-ot-layout-common.hh"
#include "hb-ot-layout-gdef-table.hh"
39

40

41 42
namespace OT {

B
Behdad Esfahbod 已提交
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
struct hb_intersects_context_t :
       hb_dispatch_context_t<hb_intersects_context_t, bool, 0>
{
  inline const char *get_name (void) { return "INTERSECTS"; }
  template <typename T>
  inline return_t dispatch (const T &obj) { return obj.intersects (this->glyphs); }
  static return_t default_return_value (void) { return false; }
  bool stop_sublookup_iteration (return_t r) const { return r; }

  const hb_set_t *glyphs;
  unsigned int debug_depth;

  hb_intersects_context_t (const hb_set_t *glyphs_) :
			     glyphs (glyphs_),
			     debug_depth (0) {}
};

B
Behdad Esfahbod 已提交
61 62
struct hb_closure_context_t :
       hb_dispatch_context_t<hb_closure_context_t, hb_void_t, HB_DEBUG_CLOSURE>
63
{
64
  inline const char *get_name (void) { return "CLOSURE"; }
65 66
  typedef return_t (*recurse_func_t) (hb_closure_context_t *c, unsigned int lookup_index);
  template <typename T>
67
  inline return_t dispatch (const T &obj) { obj.closure (this); return HB_VOID; }
68
  static return_t default_return_value (void) { return HB_VOID; }
69
  bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
70
  void recurse (unsigned int lookup_index)
71
  {
72
    if (unlikely (nesting_level_left == 0 || !recurse_func))
73
      return;
74 75 76 77 78 79

    nesting_level_left--;
    recurse_func (this, lookup_index);
    nesting_level_left++;
  }

80
  bool should_visit_lookup (unsigned int lookup_index)
81 82 83 84 85 86 87 88 89
  {
    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)
  {
90
    /* Have we visited this lookup with the current set of glyphs? */
91 92 93
    return done_lookups->get (lookup_index) == glyphs->get_population ();
  }

94
  hb_face_t *face;
95
  hb_set_t *glyphs;
96
  hb_auto_t<hb_set_t> out[1];
97
  recurse_func_t recurse_func;
98 99 100 101
  unsigned int nesting_level_left;
  unsigned int debug_depth;

  hb_closure_context_t (hb_face_t *face_,
102
			hb_set_t *glyphs_,
103
                        hb_map_t *done_lookups_,
104
		        unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
105 106
			  face (face_),
			  glyphs (glyphs_),
B
Behdad Esfahbod 已提交
107
			  recurse_func (nullptr),
108
			  nesting_level_left (nesting_level_left_),
109 110
			  debug_depth (0),
                          done_lookups (done_lookups_) {}
111

112 113 114 115 116
  ~hb_closure_context_t (void)
  {
    flush ();
  }

117
  void set_recurse_func (recurse_func_t func) { recurse_func = func; }
118

119 120 121 122 123 124
  void flush (void)
  {
    hb_set_union (glyphs, out);
    hb_set_clear (out);
  }

125 126
  private:
  hb_map_t *done_lookups;
127 128 129
};


B
Behdad Esfahbod 已提交
130 131
struct hb_would_apply_context_t :
       hb_dispatch_context_t<hb_would_apply_context_t, bool, HB_DEBUG_WOULD_APPLY>
132
{
133
  inline const char *get_name (void) { return "WOULD_APPLY"; }
B
Behdad Esfahbod 已提交
134
  template <typename T>
135
  inline return_t dispatch (const T &obj) { return obj.would_apply (this); }
B
Behdad Esfahbod 已提交
136
  static return_t default_return_value (void) { return false; }
137
  bool stop_sublookup_iteration (return_t r) const { return r; }
B
Behdad Esfahbod 已提交
138

139
  hb_face_t *face;
140
  const hb_codepoint_t *glyphs;
141
  unsigned int len;
142
  bool zero_context;
143 144 145
  unsigned int debug_depth;

  hb_would_apply_context_t (hb_face_t *face_,
146 147
			    const hb_codepoint_t *glyphs_,
			    unsigned int len_,
B
Behdad Esfahbod 已提交
148
			    bool zero_context_) :
149
			      face (face_),
150 151
			      glyphs (glyphs_),
			      len (len_),
152
			      zero_context (zero_context_),
153
			      debug_depth (0) {}
154 155 156
};


B
Behdad Esfahbod 已提交
157 158
struct hb_collect_glyphs_context_t :
       hb_dispatch_context_t<hb_collect_glyphs_context_t, hb_void_t, HB_DEBUG_COLLECT_GLYPHS>
159
{
160
  inline const char *get_name (void) { return "COLLECT_GLYPHS"; }
B
Behdad Esfahbod 已提交
161
  typedef return_t (*recurse_func_t) (hb_collect_glyphs_context_t *c, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
162
  template <typename T>
163
  inline return_t dispatch (const T &obj) { obj.collect_glyphs (this); return HB_VOID; }
164
  static return_t default_return_value (void) { return HB_VOID; }
165
  bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
166
  void recurse (unsigned int lookup_index)
B
Behdad Esfahbod 已提交
167
  {
B
Behdad Esfahbod 已提交
168
    if (unlikely (nesting_level_left == 0 || !recurse_func))
169
      return;
B
Behdad Esfahbod 已提交
170

B
Behdad Esfahbod 已提交
171
    /* Note that GPOS sets recurse_func to nullptr already, so it doesn't get
172
     * past the previous check.  For GSUB, we only want to collect the output
173 174 175 176 177 178 179
     * 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.
     */
180 181

    if (output == hb_set_get_empty ())
182
      return;
183

184
    /* Return if new lookup was recursed to before. */
B
Behdad Esfahbod 已提交
185
    if (recursed_lookups->has (lookup_index))
186
      return;
187

188 189 190 191
    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 ();
192

B
Behdad Esfahbod 已提交
193
    nesting_level_left--;
194
    recurse_func (this, lookup_index);
B
Behdad Esfahbod 已提交
195
    nesting_level_left++;
196 197 198 199 200

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

B
Behdad Esfahbod 已提交
201
    recursed_lookups->add (lookup_index);
202

203
    return;
B
Behdad Esfahbod 已提交
204 205
  }

206
  hb_face_t *face;
B
Minor  
Behdad Esfahbod 已提交
207 208 209 210
  hb_set_t *before;
  hb_set_t *input;
  hb_set_t *after;
  hb_set_t *output;
B
Behdad Esfahbod 已提交
211
  recurse_func_t recurse_func;
B
Behdad Esfahbod 已提交
212
  hb_set_t *recursed_lookups;
B
Behdad Esfahbod 已提交
213
  unsigned int nesting_level_left;
214 215 216
  unsigned int debug_depth;

  hb_collect_glyphs_context_t (hb_face_t *face_,
B
Behdad Esfahbod 已提交
217 218 219 220
			       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 */
221
			       unsigned int nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
222
			      face (face_),
B
Minor  
Behdad Esfahbod 已提交
223 224 225 226
			      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 已提交
227
			      recurse_func (nullptr),
228
			      recursed_lookups (hb_set_create ()),
B
Behdad Esfahbod 已提交
229
			      nesting_level_left (nesting_level_left_),
230 231
			      debug_depth (0) {}
  ~hb_collect_glyphs_context_t (void) { hb_set_destroy (recursed_lookups); }
B
Behdad Esfahbod 已提交
232 233

  void set_recurse_func (recurse_func_t func) { recurse_func = func; }
234 235 236 237
};



238
template <typename set_t>
B
Behdad Esfahbod 已提交
239 240
struct hb_add_coverage_context_t :
       hb_dispatch_context_t<hb_add_coverage_context_t<set_t>, const Coverage &, HB_DEBUG_GET_COVERAGE>
241
{
242
  inline const char *get_name (void) { return "GET_COVERAGE"; }
243 244
  typedef const Coverage &return_t;
  template <typename T>
245
  inline return_t dispatch (const T &obj) { return obj.get_coverage (); }
246
  static return_t default_return_value (void) { return Null(Coverage); }
247 248 249 250 251
  bool stop_sublookup_iteration (return_t r) const
  {
    r.add_coverage (set);
    return false;
  }
B
Behdad Esfahbod 已提交
252

253 254
  hb_add_coverage_context_t (set_t *set_) :
			    set (set_),
255 256
			    debug_depth (0) {}

257
  set_t *set;
258
  unsigned int debug_depth;
259 260 261
};


262 263
struct hb_ot_apply_context_t :
       hb_dispatch_context_t<hb_ot_apply_context_t, bool, HB_DEBUG_APPLY>
264
{
265
  struct matcher_t
B
Behdad Esfahbod 已提交
266
  {
267 268
    inline matcher_t (void) :
	     lookup_props (0),
269 270
	     ignore_zwnj (false),
	     ignore_zwj (false),
271 272 273 274
	     mask (-1),
#define arg1(arg) (arg) /* Remove the macro to see why it's needed! */
	     syllable arg1(0),
#undef arg1
B
Behdad Esfahbod 已提交
275 276
	     match_func (nullptr),
	     match_data (nullptr) {};
277

B
Behdad Esfahbod 已提交
278
    typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data);
279 280

    inline void set_ignore_zwnj (bool ignore_zwnj_) { ignore_zwnj = ignore_zwnj_; }
281
    inline void set_ignore_zwj (bool ignore_zwj_) { ignore_zwj = ignore_zwj_; }
282 283 284 285 286 287 288
    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_; }

289 290 291 292 293 294 295
    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 已提交
296
				  const HBUINT16          *glyph_data) const
B
Behdad Esfahbod 已提交
297
    {
298 299 300 301 302 303 304 305
      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 已提交
306
    }
307 308 309 310 311 312 313 314

    enum may_skip_t {
      SKIP_NO,
      SKIP_YES,
      SKIP_MAYBE
    };

    inline may_skip_t
315
    may_skip (const hb_ot_apply_context_t *c,
316
	      const hb_glyph_info_t    &info) const
B
Behdad Esfahbod 已提交
317
    {
B
Behdad Esfahbod 已提交
318
      if (!c->check_glyph_property (&info, lookup_props))
319 320
	return SKIP_YES;

321
      if (unlikely (_hb_glyph_info_is_default_ignorable_and_not_hidden (&info) &&
322
		    (ignore_zwnj || !_hb_glyph_info_is_zwnj (&info)) &&
323
		    (ignore_zwj || !_hb_glyph_info_is_zwj (&info))))
324 325 326
	return SKIP_MAYBE;

      return SKIP_NO;
B
Behdad Esfahbod 已提交
327
    }
328 329 330 331

    protected:
    unsigned int lookup_props;
    bool ignore_zwnj;
332
    bool ignore_zwj;
333 334 335 336 337 338
    hb_mask_t mask;
    uint8_t syllable;
    match_func_t match_func;
    const void *match_data;
  };

339
  struct skipping_iterator_t
340
  {
341
    inline void init (hb_ot_apply_context_t *c_, bool context_match = false)
B
Behdad Esfahbod 已提交
342
    {
343
      c = c_;
B
Behdad Esfahbod 已提交
344 345
      match_glyph_data = nullptr;
      matcher.set_match_func (nullptr, nullptr);
346
      matcher.set_lookup_props (c->lookup_props);
347
      /* Ignore ZWNJ if we are matching GSUB context, or matching GPOS. */
348
      matcher.set_ignore_zwnj (c->table_index == 1 || (context_match && c->auto_zwnj));
349
      /* Ignore ZWJ if we are matching GSUB context, or matching GPOS, or if asked to. */
350
      matcher.set_ignore_zwj  (c->table_index == 1 || (context_match || c->auto_zwj));
351 352 353 354 355
      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 已提交
356
    }
B
Behdad Esfahbod 已提交
357 358
    inline void set_match_func (matcher_t::match_func_t match_func_,
				const void *match_data_,
B
Behdad Esfahbod 已提交
359
				const HBUINT16 glyph_data[])
360
    {
B
Behdad Esfahbod 已提交
361
      matcher.set_match_func (match_func_, match_data_);
362
      match_glyph_data = glyph_data;
363
    }
364

365 366 367 368 369 370 371 372 373
    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);
    }

374
    inline void reject (void) { num_items++; match_glyph_data--; }
375

376
    inline matcher_t::may_skip_t
B
Behdad Esfahbod 已提交
377
    may_skip (const hb_glyph_info_t    &info) const
378 379 380 381
    {
      return matcher.may_skip (c, info);
    }

B
Behdad Esfahbod 已提交
382
    inline bool next (void)
B
Behdad Esfahbod 已提交
383
    {
384
      assert (num_items > 0);
385
      while (idx + num_items < end)
B
Behdad Esfahbod 已提交
386
      {
B
Behdad Esfahbod 已提交
387
	idx++;
388 389
	const hb_glyph_info_t &info = c->buffer->info[idx];

B
Minor  
Behdad Esfahbod 已提交
390
	matcher_t::may_skip_t skip = matcher.may_skip (c, info);
391 392 393
	if (unlikely (skip == matcher_t::SKIP_YES))
	  continue;

394
	matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
B
Behdad Esfahbod 已提交
395 396 397
	if (match == matcher_t::MATCH_YES ||
	    (match == matcher_t::MATCH_MAYBE &&
	     skip == matcher_t::SKIP_NO))
398 399 400 401 402 403 404
	{
	  num_items--;
	  match_glyph_data++;
	  return true;
	}

	if (skip == matcher_t::SKIP_NO)
B
Behdad Esfahbod 已提交
405
	  return false;
406 407
      }
      return false;
B
Behdad Esfahbod 已提交
408
    }
B
Behdad Esfahbod 已提交
409
    inline bool prev (void)
B
Behdad Esfahbod 已提交
410
    {
411
      assert (num_items > 0);
B
Behdad Esfahbod 已提交
412
      while (idx > num_items - 1)
B
Behdad Esfahbod 已提交
413 414
      {
	idx--;
415 416
	const hb_glyph_info_t &info = c->buffer->out_info[idx];

B
Minor  
Behdad Esfahbod 已提交
417
	matcher_t::may_skip_t skip = matcher.may_skip (c, info);
418 419 420
	if (unlikely (skip == matcher_t::SKIP_YES))
	  continue;

421
	matcher_t::may_match_t match = matcher.may_match (info, match_glyph_data);
B
Behdad Esfahbod 已提交
422 423 424
	if (match == matcher_t::MATCH_YES ||
	    (match == matcher_t::MATCH_MAYBE &&
	     skip == matcher_t::SKIP_NO))
425 426 427 428 429 430 431
	{
	  num_items--;
	  match_glyph_data++;
	  return true;
	}

	if (skip == matcher_t::SKIP_NO)
B
Behdad Esfahbod 已提交
432
	  return false;
433 434
      }
      return false;
B
Behdad Esfahbod 已提交
435 436 437
    }

    unsigned int idx;
438
    protected:
439
    hb_ot_apply_context_t *c;
440
    matcher_t matcher;
B
Behdad Esfahbod 已提交
441
    const HBUINT16 *match_glyph_data;
442

B
Behdad Esfahbod 已提交
443
    unsigned int num_items;
444
    unsigned int end;
B
Behdad Esfahbod 已提交
445 446
  };

B
Behdad Esfahbod 已提交
447 448

  inline const char *get_name (void) { return "APPLY"; }
449
  typedef return_t (*recurse_func_t) (hb_ot_apply_context_t *c, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
450 451 452 453
  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 已提交
454
  return_t recurse (unsigned int sub_lookup_index)
B
Behdad Esfahbod 已提交
455
  {
456
    if (unlikely (nesting_level_left == 0 || !recurse_func || buffer->max_ops-- <= 0))
B
Behdad Esfahbod 已提交
457 458 459
      return default_return_value ();

    nesting_level_left--;
B
Behdad Esfahbod 已提交
460
    bool ret = recurse_func (this, sub_lookup_index);
B
Behdad Esfahbod 已提交
461 462 463 464
    nesting_level_left++;
    return ret;
  }

465 466
  skipping_iterator_t iter_input, iter_context;

B
Behdad Esfahbod 已提交
467 468 469 470 471
  hb_font_t *font;
  hb_face_t *face;
  hb_buffer_t *buffer;
  recurse_func_t recurse_func;
  const GDEF &gdef;
472
  const VariationStore &var_store;
473 474 475 476

  hb_direction_t direction;
  hb_mask_t lookup_mask;
  unsigned int table_index; /* GSUB/GPOS */
477
  unsigned int lookup_index;
478 479
  unsigned int lookup_props;
  unsigned int nesting_level_left;
B
Behdad Esfahbod 已提交
480 481
  unsigned int debug_depth;

482
  bool has_glyph_classes;
483 484
  bool auto_zwnj;
  bool auto_zwj;
D
David Corbett 已提交
485
  bool random;
486

B
Behdad Esfahbod 已提交
487
  uint32_t random_state;
488

B
Behdad Esfahbod 已提交
489

490
  hb_ot_apply_context_t (unsigned int table_index_,
B
Behdad Esfahbod 已提交
491 492
		      hb_font_t *font_,
		      hb_buffer_t *buffer_) :
493
			iter_input (), iter_context (),
B
Behdad Esfahbod 已提交
494
			font (font_), face (font->face), buffer (buffer_),
B
Behdad Esfahbod 已提交
495
			recurse_func (nullptr),
496
			gdef (_get_gdef (face)),
497
			var_store (gdef.get_var_store ()),
498 499 500
			direction (buffer_->props.direction),
			lookup_mask (1),
			table_index (table_index_),
501
			lookup_index ((unsigned int) -1),
502 503 504
			lookup_props (0),
			nesting_level_left (HB_MAX_NESTING_LEVEL),
			debug_depth (0),
505
			has_glyph_classes (gdef.has_glyph_classes ()),
506 507
			auto_zwnj (true),
			auto_zwj (true),
D
David Corbett 已提交
508
			random (false),
509
			random_state (1) {}
B
Behdad Esfahbod 已提交
510 511 512

  inline void set_lookup_mask (hb_mask_t mask) { lookup_mask = mask; }
  inline void set_auto_zwj (bool auto_zwj_) { auto_zwj = auto_zwj_; }
513
  inline void set_auto_zwnj (bool auto_zwnj_) { auto_zwnj = auto_zwnj_; }
514
  inline void set_random (bool random_) { random = random_; }
B
Behdad Esfahbod 已提交
515
  inline void set_recurse_func (recurse_func_t func) { recurse_func = func; }
516
  inline void set_lookup_index (unsigned int lookup_index_) { lookup_index = lookup_index_; }
517 518 519 520 521 522
  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 已提交
523

B
Behdad Esfahbod 已提交
524 525
  inline uint32_t random_number (void)
  {
B
Behdad Esfahbod 已提交
526 527 528
    /* http://www.cplusplus.com/reference/random/minstd_rand/ */
    random_state = random_state * 48271 % 2147483647;
    return random_state;
B
Behdad Esfahbod 已提交
529 530
  }

531 532 533
  inline bool
  match_properties_mark (hb_codepoint_t  glyph,
			 unsigned int    glyph_props,
B
Behdad Esfahbod 已提交
534
			 unsigned int    match_props) const
535 536
  {
    /* If using mark filtering sets, the high short of
B
Behdad Esfahbod 已提交
537
     * match_props has the set index.
538
     */
B
Behdad Esfahbod 已提交
539 540
    if (match_props & LookupFlag::UseMarkFilteringSet)
      return gdef.mark_set_covers (match_props >> 16, glyph);
541

B
Behdad Esfahbod 已提交
542
    /* The second byte of match_props has the meaning
543 544 545
     * "ignore marks of attachment type different than
     * the attachment type specified."
     */
B
Behdad Esfahbod 已提交
546 547
    if (match_props & LookupFlag::MarkAttachmentType)
      return (match_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
548 549 550 551 552

    return true;
  }

  inline bool
B
Behdad Esfahbod 已提交
553
  check_glyph_property (const hb_glyph_info_t *info,
B
Behdad Esfahbod 已提交
554
			unsigned int  match_props) const
555
  {
B
Behdad Esfahbod 已提交
556 557 558
    hb_codepoint_t glyph = info->codepoint;
    unsigned int glyph_props = _hb_glyph_info_get_glyph_props (info);

559
    /* Not covered, if, for example, glyph class is ligature and
B
Behdad Esfahbod 已提交
560
     * match_props includes LookupFlags::IgnoreLigatures
561
     */
B
Behdad Esfahbod 已提交
562
    if (glyph_props & match_props & LookupFlag::IgnoreFlags)
563 564
      return false;

565
    if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_PROPS_MARK))
B
Behdad Esfahbod 已提交
566
      return match_properties_mark (glyph, glyph_props, match_props);
567 568 569 570

    return true;
  }

571
  inline void _set_glyph_props (hb_codepoint_t glyph_index,
572
			  unsigned int class_guess = 0,
573 574
			  bool ligature = false,
			  bool component = false) const
575
  {
576 577 578 579
    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)
580
    {
581
      add_in |= HB_OT_LAYOUT_GLYPH_PROPS_LIGATED;
582 583 584 585 586 587 588 589 590 591
      /* 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;
592
    if (likely (has_glyph_classes))
593
      _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | gdef.get_glyph_props (glyph_index));
594
    else if (class_guess)
595
      _hb_glyph_info_set_glyph_props (&buffer->cur(), add_in | class_guess);
596
  }
B
Behdad Esfahbod 已提交
597

598
  inline void replace_glyph (hb_codepoint_t glyph_index) const
599
  {
600 601
    _set_glyph_props (glyph_index);
    buffer->replace_glyph (glyph_index);
602
  }
603
  inline void replace_glyph_inplace (hb_codepoint_t glyph_index) const
604
  {
605 606 607 608 609 610
    _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
  {
611
    _set_glyph_props (glyph_index, class_guess, true);
612 613
    buffer->replace_glyph (glyph_index);
  }
614 615
  inline void output_glyph_for_component (hb_codepoint_t glyph_index,
					  unsigned int class_guess) const
B
Behdad Esfahbod 已提交
616
  {
617
    _set_glyph_props (glyph_index, class_guess, false, true);
618
    buffer->output_glyph (glyph_index);
B
Behdad Esfahbod 已提交
619
  }
620 621
};

622

B
Behdad Esfahbod 已提交
623

624
typedef bool (*intersects_func_t) (const hb_set_t *glyphs, const HBUINT16 &value, const void *data);
B
Behdad Esfahbod 已提交
625 626
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);
627

628 629 630 631
struct ContextClosureFuncs
{
  intersects_func_t intersects;
};
632 633 634 635
struct ContextCollectGlyphsFuncs
{
  collect_glyphs_func_t collect;
};
636
struct ContextApplyFuncs
B
Behdad Esfahbod 已提交
637
{
638
  match_func_t match;
639 640
};

641

642
static inline bool intersects_glyph (const hb_set_t *glyphs, const HBUINT16 &value, const void *data HB_UNUSED)
643 644 645
{
  return glyphs->has (value);
}
646
static inline bool intersects_class (const hb_set_t *glyphs, const HBUINT16 &value, const void *data)
647 648 649 650
{
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
  return class_def.intersects_class (glyphs, value);
}
651
static inline bool intersects_coverage (const hb_set_t *glyphs, const HBUINT16 &value, const void *data)
652 653 654 655 656
{
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
  return (data+coverage).intersects (glyphs);
}

657
static inline bool intersects_array (const hb_set_t *glyphs,
658
				     unsigned int count,
B
Behdad Esfahbod 已提交
659
				     const HBUINT16 values[],
660 661 662 663
				     intersects_func_t intersects_func,
				     const void *intersects_data)
{
  for (unsigned int i = 0; i < count; i++)
664
    if (likely (!intersects_func (glyphs, values[i], intersects_data)))
665 666 667 668
      return false;
  return true;
}

669

B
Behdad Esfahbod 已提交
670
static inline void collect_glyph (hb_set_t *glyphs, const HBUINT16 &value, const void *data HB_UNUSED)
671 672 673
{
  glyphs->add (value);
}
B
Behdad Esfahbod 已提交
674
static inline void collect_class (hb_set_t *glyphs, const HBUINT16 &value, const void *data)
675 676
{
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
677
  class_def.add_class (glyphs, value);
678
}
B
Behdad Esfahbod 已提交
679
static inline void collect_coverage (hb_set_t *glyphs, const HBUINT16 &value, const void *data)
680 681 682 683
{
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
  (data+coverage).add_coverage (glyphs);
}
B
Behdad Esfahbod 已提交
684
static inline void collect_array (hb_collect_glyphs_context_t *c HB_UNUSED,
685
				  hb_set_t *glyphs,
686
				  unsigned int count,
B
Behdad Esfahbod 已提交
687
				  const HBUINT16 values[],
688 689 690 691
				  collect_glyphs_func_t collect_func,
				  const void *collect_data)
{
  for (unsigned int i = 0; i < count; i++)
692
    collect_func (glyphs, values[i], collect_data);
693 694 695
}


B
Behdad Esfahbod 已提交
696
static inline bool match_glyph (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data HB_UNUSED)
B
Behdad Esfahbod 已提交
697
{
698 699
  return glyph_id == value;
}
B
Behdad Esfahbod 已提交
700
static inline bool match_class (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data)
B
Behdad Esfahbod 已提交
701
{
B
Behdad Esfahbod 已提交
702
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
703 704
  return class_def.get_class (glyph_id) == value;
}
B
Behdad Esfahbod 已提交
705
static inline bool match_coverage (hb_codepoint_t glyph_id, const HBUINT16 &value, const void *data)
B
Behdad Esfahbod 已提交
706
{
707
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
708
  return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
709 710
}

711 712
static inline bool would_match_input (hb_would_apply_context_t *c,
				      unsigned int count, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
713
				      const HBUINT16 input[], /* Array of input values--start with second glyph */
714 715 716 717 718 719 720
				      match_func_t match_func,
				      const void *match_data)
{
  if (count != c->len)
    return false;

  for (unsigned int i = 1; i < count; i++)
721
    if (likely (!match_func (c->glyphs[i], input[i - 1], match_data)))
722 723 724 725
      return false;

  return true;
}
726
static inline bool match_input (hb_ot_apply_context_t *c,
B
Behdad Esfahbod 已提交
727
				unsigned int count, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
728
				const HBUINT16 input[], /* Array of input values--start with second glyph */
B
Behdad Esfahbod 已提交
729
				match_func_t match_func,
B
Behdad Esfahbod 已提交
730
				const void *match_data,
B
Behdad Esfahbod 已提交
731
				unsigned int *end_offset,
732
				unsigned int match_positions[HB_MAX_CONTEXT_LENGTH],
B
Behdad Esfahbod 已提交
733 734
				bool *p_is_mark_ligature = nullptr,
				unsigned int *p_total_component_count = nullptr)
B
Behdad Esfahbod 已提交
735
{
B
Behdad Esfahbod 已提交
736
  TRACE_APPLY (nullptr);
737

738
  if (unlikely (count > HB_MAX_CONTEXT_LENGTH)) return_trace (false);
739

B
Behdad Esfahbod 已提交
740 741
  hb_buffer_t *buffer = c->buffer;

742
  hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
743
  skippy_iter.reset (buffer->idx, count - 1);
744
  skippy_iter.set_match_func (match_func, match_data, input);
745

746 747 748 749 750 751 752 753 754 755 756
  /*
   * 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.
757 758 759 760 761 762 763 764 765 766
   *   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.
767
   *     https://github.com/harfbuzz/harfbuzz/issues/545
768 769
   */

B
Behdad Esfahbod 已提交
770
  bool is_mark_ligature = _hb_glyph_info_is_mark (&buffer->cur());
771 772

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

B
Behdad Esfahbod 已提交
775 776
  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 已提交
777

778 779 780 781 782 783
  enum {
    LIGBASE_NOT_CHECKED,
    LIGBASE_MAY_NOT_SKIP,
    LIGBASE_MAY_SKIP
  } ligbase = LIGBASE_NOT_CHECKED;

B
Behdad Esfahbod 已提交
784
  match_positions[0] = buffer->idx;
B
Minor  
Behdad Esfahbod 已提交
785
  for (unsigned int i = 1; i < count; i++)
B
Behdad Esfahbod 已提交
786
  {
B
Behdad Esfahbod 已提交
787
    if (!skippy_iter.next ()) return_trace (false);
B
Behdad Esfahbod 已提交
788 789

    match_positions[i] = skippy_iter.idx;
790

B
Behdad Esfahbod 已提交
791 792
    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]);
793

794 795
    if (first_lig_id && first_lig_comp)
    {
796 797
      /* If first component was attached to a previous ligature component,
       * all subsequent components should be attached to the same ligature
798
       * component, otherwise we shouldn't ligate them... */
799
      if (first_lig_id != this_lig_id || first_lig_comp != this_lig_comp)
800 801 802
      {
        /* ...unless, we are attached to a base ligature and that base
	 * ligature is ignorable. */
803
        if (ligbase == LIGBASE_NOT_CHECKED)
804
	{
805 806 807 808
	  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)
809
	  {
810 811 812 813 814 815
	    if (_hb_glyph_info_get_lig_comp (&out[j - 1]) == 0)
	    {
	      j--;
	      found = true;
	      break;
	    }
816 817 818
	    j--;
	  }

B
Behdad Esfahbod 已提交
819
	  if (found && skippy_iter.may_skip (out[j]) == hb_ot_apply_context_t::matcher_t::SKIP_YES)
820 821 822 823
	    ligbase = LIGBASE_MAY_SKIP;
	  else
	    ligbase = LIGBASE_MAY_NOT_SKIP;
	}
824

825
        if (ligbase == LIGBASE_MAY_NOT_SKIP)
826 827 828 829
	  return_trace (false);
      }
    }
    else
830
    {
831 832 833 834
      /* 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 已提交
835
	return_trace (false);
836 837
    }

B
Behdad Esfahbod 已提交
838
    is_mark_ligature = is_mark_ligature && _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx]);
B
Behdad Esfahbod 已提交
839
    total_component_count += _hb_glyph_info_get_lig_num_comps (&buffer->info[skippy_iter.idx]);
B
Behdad Esfahbod 已提交
840 841
  }

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

844 845 846 847 848 849
  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 已提交
850
  return_trace (true);
B
Behdad Esfahbod 已提交
851
}
852
static inline bool ligate_input (hb_ot_apply_context_t *c,
853
				 unsigned int count, /* Including the first glyph */
854
				 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */
855
				 unsigned int match_length,
856 857 858 859
				 hb_codepoint_t lig_glyph,
				 bool is_mark_ligature,
				 unsigned int total_component_count)
{
B
Behdad Esfahbod 已提交
860
  TRACE_APPLY (nullptr);
861

B
Behdad Esfahbod 已提交
862 863 864
  hb_buffer_t *buffer = c->buffer;

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

866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
  /*
   * - 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
   */

894
  unsigned int klass = is_mark_ligature ? 0 : HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
B
Behdad Esfahbod 已提交
895 896 897
  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());
898 899 900
  unsigned int components_so_far = last_num_components;

  if (!is_mark_ligature)
901
  {
B
Behdad Esfahbod 已提交
902
    _hb_glyph_info_set_lig_props_for_ligature (&buffer->cur(), lig_id, total_component_count);
B
Behdad Esfahbod 已提交
903
    if (_hb_glyph_info_get_general_category (&buffer->cur()) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
904
    {
B
Minor  
Behdad Esfahbod 已提交
905
      _hb_glyph_info_set_general_category (&buffer->cur(), HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER);
906
    }
907
  }
908
  c->replace_glyph_with_ligature (lig_glyph, klass);
909 910 911

  for (unsigned int i = 1; i < count; i++)
  {
B
Behdad Esfahbod 已提交
912
    while (buffer->idx < match_positions[i] && buffer->successful)
913 914
    {
      if (!is_mark_ligature) {
915 916
        unsigned int this_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
	if (this_comp == 0)
917
	  this_comp = last_num_components;
918
	unsigned int new_lig_comp = components_so_far - last_num_components +
919 920
				    MIN (this_comp, last_num_components);
	  _hb_glyph_info_set_lig_props_for_mark (&buffer->cur(), lig_id, new_lig_comp);
921
      }
B
Behdad Esfahbod 已提交
922
      buffer->next_glyph ();
923 924
    }

B
Behdad Esfahbod 已提交
925 926
    last_lig_id = _hb_glyph_info_get_lig_id (&buffer->cur());
    last_num_components = _hb_glyph_info_get_lig_num_comps (&buffer->cur());
927 928 929
    components_so_far += last_num_components;

    /* Skip the base glyph */
B
Behdad Esfahbod 已提交
930
    buffer->idx++;
931 932 933 934
  }

  if (!is_mark_ligature && last_lig_id) {
    /* Re-adjust components for any marks following. */
B
Behdad Esfahbod 已提交
935
    for (unsigned int i = buffer->idx; i < buffer->len; i++) {
B
Behdad Esfahbod 已提交
936
      if (last_lig_id == _hb_glyph_info_get_lig_id (&buffer->info[i])) {
937 938 939
        unsigned int this_comp = _hb_glyph_info_get_lig_comp (&buffer->info[i]);
	if (!this_comp)
	  break;
940
	unsigned int new_lig_comp = components_so_far - last_num_components +
941
				    MIN (this_comp, last_num_components);
B
Behdad Esfahbod 已提交
942
	_hb_glyph_info_set_lig_props_for_mark (&buffer->info[i], lig_id, new_lig_comp);
943 944 945 946
      } else
	break;
    }
  }
B
Behdad Esfahbod 已提交
947
  return_trace (true);
948
}
B
Behdad Esfahbod 已提交
949

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

959
  hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_context;
960
  skippy_iter.reset (c->buffer->backtrack_len (), count);
961
  skippy_iter.set_match_func (match_func, match_data, backtrack);
962

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

967 968
  *match_start = skippy_iter.idx;

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

972
static inline bool match_lookahead (hb_ot_apply_context_t *c,
B
Behdad Esfahbod 已提交
973
				    unsigned int count,
B
Behdad Esfahbod 已提交
974
				    const HBUINT16 lookahead[],
B
Behdad Esfahbod 已提交
975
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
976
				    const void *match_data,
977 978
				    unsigned int offset,
				    unsigned int *end_index)
979
{
B
Behdad Esfahbod 已提交
980
  TRACE_APPLY (nullptr);
981

982
  hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_context;
983
  skippy_iter.reset (c->buffer->idx + offset - 1, count);
984
  skippy_iter.set_match_func (match_func, match_data, lookahead);
985

B
Minor  
Behdad Esfahbod 已提交
986
  for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
987
    if (!skippy_iter.next ())
B
Behdad Esfahbod 已提交
988
      return_trace (false);
989

990 991
  *end_index = skippy_iter.idx + 1;

B
Behdad Esfahbod 已提交
992
  return_trace (true);
993 994
}

B
Behdad Esfahbod 已提交
995

996

B
Behdad Esfahbod 已提交
997 998
struct LookupRecord
{
B
Behdad Esfahbod 已提交
999 1000
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1001
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1002
    return_trace (c->check_struct (this));
B
Behdad Esfahbod 已提交
1003 1004
  }

B
Behdad Esfahbod 已提交
1005
  HBUINT16	sequenceIndex;		/* Index into current glyph
1006
					 * sequence--first glyph = 0 */
B
Behdad Esfahbod 已提交
1007
  HBUINT16	lookupListIndex;	/* Lookup to apply to that
1008
					 * position--zero--based */
B
Behdad Esfahbod 已提交
1009 1010
  public:
  DEFINE_SIZE_STATIC (4);
1011 1012
};

1013 1014 1015 1016
template <typename context_t>
static inline void recurse_lookups (context_t *c,
				    unsigned int lookupCount,
				    const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */)
1017 1018
{
  for (unsigned int i = 0; i < lookupCount; i++)
B
Behdad Esfahbod 已提交
1019
    c->recurse (lookupRecord[i].lookupListIndex);
1020
}
B
Behdad Esfahbod 已提交
1021

1022
static inline bool apply_lookup (hb_ot_apply_context_t *c,
1023
				 unsigned int count, /* Including the first glyph */
1024
				 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH], /* Including the first glyph */
1025
				 unsigned int lookupCount,
1026 1027
				 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
				 unsigned int match_length)
1028
{
B
Behdad Esfahbod 已提交
1029
  TRACE_APPLY (nullptr);
1030

1031
  hb_buffer_t *buffer = c->buffer;
1032
  int end;
B
Behdad Esfahbod 已提交
1033

1034 1035 1036 1037 1038
  /* All positions are distance from beginning of *output* buffer.
   * Adjust. */
  {
    unsigned int bl = buffer->backtrack_len ();
    end = bl + match_length;
1039

1040 1041 1042 1043 1044
    int delta = bl - buffer->idx;
    /* Convert positions to new indexing. */
    for (unsigned int j = 0; j < count; j++)
      match_positions[j] += delta;
  }
1045

B
Behdad Esfahbod 已提交
1046
  for (unsigned int i = 0; i < lookupCount && buffer->successful; i++)
B
Behdad Esfahbod 已提交
1047
  {
1048 1049 1050
    unsigned int idx = lookupRecord[i].sequenceIndex;
    if (idx >= count)
      continue;
1051

1052 1053 1054 1055 1056
    /* 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;

1057 1058
    if (unlikely (!buffer->move_to (match_positions[idx])))
      break;
1059

1060 1061 1062
    if (unlikely (buffer->max_ops <= 0))
      break;

1063 1064 1065
    unsigned int orig_len = buffer->backtrack_len () + buffer->lookahead_len ();
    if (!c->recurse (lookupRecord[i].lookupListIndex))
      continue;
1066

1067 1068
    unsigned int new_len = buffer->backtrack_len () + buffer->lookahead_len ();
    int delta = new_len - orig_len;
1069

1070 1071
    if (!delta)
        continue;
1072

B
Behdad Esfahbod 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
    /* 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.
     */
1096

1097
    end += delta;
1098
    if (end <= int (match_positions[idx]))
1099
    {
1100
      /* End might end up being smaller than match_positions[idx] if the recursed
1101
       * lookup ended up removing many items, more than we have had matched.
1102 1103 1104
       * Just never rewind end back and get out of here.
       * https://bugs.chromium.org/p/chromium/issues/detail?id=659496 */
      end = match_positions[idx];
1105
      /* There can't be any further changes. */
1106 1107
      break;
    }
1108

1109 1110 1111 1112
    unsigned int next = idx + 1; /* next now is the position after the recursed lookup. */

    if (delta > 0)
    {
1113
      if (unlikely (delta + count > HB_MAX_CONTEXT_LENGTH))
1114
	break;
1115 1116 1117
    }
    else
    {
1118
      /* NOTE: delta is negative. */
1119 1120
      delta = MAX (delta, (int) next - (int) count);
      next -= delta;
1121
    }
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135

    /* 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;
1136 1137
  }

1138 1139
  buffer->move_to (end);

B
Behdad Esfahbod 已提交
1140
  return_trace (true);
1141
}
1142

B
Behdad Esfahbod 已提交
1143

1144 1145 1146

/* Contextual lookups */

1147 1148 1149 1150 1151 1152
struct ContextClosureLookupContext
{
  ContextClosureFuncs funcs;
  const void *intersects_data;
};

1153 1154 1155 1156 1157 1158
struct ContextCollectGlyphsLookupContext
{
  ContextCollectGlyphsFuncs funcs;
  const void *collect_data;
};

1159
struct ContextApplyLookupContext
B
Behdad Esfahbod 已提交
1160
{
1161
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
1162
  const void *match_data;
1163 1164
};

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
static inline bool context_intersects (const hb_set_t *glyphs,
				       unsigned int inputCount, /* Including the first glyph (not matched) */
				       const HBUINT16 input[], /* Array of input values--start with second glyph */
				       ContextClosureLookupContext &lookup_context)
{
  return intersects_array (glyphs,
			   inputCount ? inputCount - 1 : 0, input,
			   lookup_context.funcs.intersects, lookup_context.intersects_data);
}

B
Behdad Esfahbod 已提交
1175
static inline void context_closure_lookup (hb_closure_context_t *c,
1176
					   unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1177
					   const HBUINT16 input[], /* Array of input values--start with second glyph */
1178 1179 1180 1181
					   unsigned int lookupCount,
					   const LookupRecord lookupRecord[],
					   ContextClosureLookupContext &lookup_context)
{
1182 1183 1184
  if (context_intersects (c->glyphs,
			  inputCount, input,
			  lookup_context))
1185 1186
    recurse_lookups (c,
		     lookupCount, lookupRecord);
1187 1188
}

1189 1190
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 已提交
1191
						  const HBUINT16 input[], /* Array of input values--start with second glyph */
1192 1193 1194 1195
						  unsigned int lookupCount,
						  const LookupRecord lookupRecord[],
						  ContextCollectGlyphsLookupContext &lookup_context)
{
B
Minor  
Behdad Esfahbod 已提交
1196
  collect_array (c, c->input,
1197 1198 1199 1200 1201
		 inputCount ? inputCount - 1 : 0, input,
		 lookup_context.funcs.collect, lookup_context.collect_data);
  recurse_lookups (c,
		   lookupCount, lookupRecord);
}
1202

1203 1204
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 已提交
1205
					       const HBUINT16 input[], /* Array of input values--start with second glyph */
B
Behdad Esfahbod 已提交
1206 1207
					       unsigned int lookupCount HB_UNUSED,
					       const LookupRecord lookupRecord[] HB_UNUSED,
1208 1209 1210 1211 1212 1213
					       ContextApplyLookupContext &lookup_context)
{
  return would_match_input (c,
			    inputCount, input,
			    lookup_context.funcs.match, lookup_context.match_data);
}
1214
static inline bool context_apply_lookup (hb_ot_apply_context_t *c,
1215
					 unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1216
					 const HBUINT16 input[], /* Array of input values--start with second glyph */
1217 1218 1219
					 unsigned int lookupCount,
					 const LookupRecord lookupRecord[],
					 ContextApplyLookupContext &lookup_context)
1220
{
1221
  unsigned int match_length = 0;
1222
  unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
B
Behdad Esfahbod 已提交
1223
  return match_input (c,
B
Behdad Esfahbod 已提交
1224
		      inputCount, input,
1225 1226
		      lookup_context.funcs.match, lookup_context.match_data,
		      &match_length, match_positions)
1227 1228
      && (c->buffer->unsafe_to_break (c->buffer->idx, c->buffer->idx + match_length),
	  apply_lookup (c,
1229 1230
		       inputCount, match_positions,
		       lookupCount, lookupRecord,
1231
		       match_length));
1232 1233
}

B
Behdad Esfahbod 已提交
1234 1235
struct Rule
{
1236 1237 1238
  inline bool intersects (const hb_set_t *glyphs, ContextClosureLookupContext &lookup_context) const
  {
    return context_intersects (glyphs,
1239
			       inputCount, inputZ.arrayZ,
1240 1241 1242
			       lookup_context);
  }

B
Behdad Esfahbod 已提交
1243
  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
1244
  {
B
Behdad Esfahbod 已提交
1245
    TRACE_CLOSURE (this);
1246
    const UnsizedArrayOf<LookupRecord> &lookupRecord = StructAtOffset<UnsizedArrayOf<LookupRecord> > (inputZ.arrayZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
B
Behdad Esfahbod 已提交
1247
    context_closure_lookup (c,
1248 1249
			    inputCount, inputZ.arrayZ,
			    lookupCount, lookupRecord.arrayZ,
B
Behdad Esfahbod 已提交
1250
			    lookup_context);
1251 1252
  }

1253 1254 1255
  inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
  {
    TRACE_COLLECT_GLYPHS (this);
1256
    const UnsizedArrayOf<LookupRecord> &lookupRecord = StructAtOffset<UnsizedArrayOf<LookupRecord> > (inputZ.arrayZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
1257
    context_collect_glyphs_lookup (c,
1258 1259
				   inputCount, inputZ.arrayZ,
				   lookupCount, lookupRecord.arrayZ,
1260 1261 1262
				   lookup_context);
  }

1263 1264
  inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
  {
B
Behdad Esfahbod 已提交
1265
    TRACE_WOULD_APPLY (this);
1266 1267
    const UnsizedArrayOf<LookupRecord> &lookupRecord = StructAtOffset<UnsizedArrayOf<LookupRecord> > (inputZ.arrayZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
    return_trace (context_would_apply_lookup (c, inputCount, inputZ.arrayZ, lookupCount, lookupRecord.arrayZ, lookup_context));
1268 1269
  }

1270
  inline bool apply (hb_ot_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
1271
  {
B
Behdad Esfahbod 已提交
1272
    TRACE_APPLY (this);
1273 1274
    const UnsizedArrayOf<LookupRecord> &lookupRecord = StructAtOffset<UnsizedArrayOf<LookupRecord> > (inputZ.arrayZ, inputZ[0].static_size * (inputCount ? inputCount - 1 : 0));
    return_trace (context_apply_lookup (c, inputCount, inputZ.arrayZ, lookupCount, lookupRecord.arrayZ, lookup_context));
1275 1276
  }

B
Behdad Esfahbod 已提交
1277
  public:
B
Behdad Esfahbod 已提交
1278 1279
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1280
    TRACE_SANITIZE (this);
1281 1282
    return_trace (inputCount.sanitize (c) &&
		  lookupCount.sanitize (c) &&
1283
		  c->check_range (inputZ.arrayZ,
B
Behdad Esfahbod 已提交
1284
				  inputZ[0].static_size * (inputCount ? inputCount - 1 : 0) +
1285
				  LookupRecord::static_size * lookupCount));
B
Behdad Esfahbod 已提交
1286 1287
  }

1288
  protected:
B
Behdad Esfahbod 已提交
1289
  HBUINT16	inputCount;		/* Total number of glyphs in input
1290
					 * glyph sequence--includes the first
1291
					 * glyph */
B
Behdad Esfahbod 已提交
1292
  HBUINT16	lookupCount;		/* Number of LookupRecords */
1293 1294
  UnsizedArrayOf<HBUINT16>
 		inputZ;			/* Array of match inputs--start with
1295
					 * second glyph */
1296 1297
/*UnsizedArrayOf<LookupRecord>
		lookupRecordX;*/	/* Array of LookupRecords--in
1298
					 * design order */
B
Behdad Esfahbod 已提交
1299
  public:
1300
  DEFINE_SIZE_ARRAY (4, inputZ);
1301 1302
};

B
Behdad Esfahbod 已提交
1303 1304
struct RuleSet
{
1305 1306 1307 1308 1309 1310 1311 1312 1313
  inline bool intersects (const hb_set_t *glyphs, ContextClosureLookupContext &lookup_context) const
  {
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
      if ((this+rule[i]).intersects (glyphs, lookup_context))
        return true;
    return false;
  }

B
Behdad Esfahbod 已提交
1314
  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
1315
  {
B
Behdad Esfahbod 已提交
1316
    TRACE_CLOSURE (this);
1317 1318
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
1319
      (this+rule[i]).closure (c, lookup_context);
1320 1321
  }

1322 1323 1324 1325 1326 1327 1328 1329
  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);
  }

1330 1331
  inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
  {
B
Behdad Esfahbod 已提交
1332
    TRACE_WOULD_APPLY (this);
1333 1334 1335 1336
    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 已提交
1337
        return_trace (true);
1338
    }
B
Behdad Esfahbod 已提交
1339
    return_trace (false);
1340 1341
  }

1342
  inline bool apply (hb_ot_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
1343
  {
B
Behdad Esfahbod 已提交
1344
    TRACE_APPLY (this);
1345
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
1346 1347
    for (unsigned int i = 0; i < num_rules; i++)
    {
B
Behdad Esfahbod 已提交
1348
      if ((this+rule[i]).apply (c, lookup_context))
B
Behdad Esfahbod 已提交
1349
        return_trace (true);
1350
    }
B
Behdad Esfahbod 已提交
1351
    return_trace (false);
1352 1353
  }

B
Behdad Esfahbod 已提交
1354 1355
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1356
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1357
    return_trace (rule.sanitize (c, this));
B
Behdad Esfahbod 已提交
1358 1359
  }

1360
  protected:
1361
  OffsetArrayOf<Rule>
1362
		rule;			/* Array of Rule tables
1363
					 * ordered by preference */
B
Behdad Esfahbod 已提交
1364
  public:
1365
  DEFINE_SIZE_ARRAY (2, rule);
1366 1367 1368
};


B
Behdad Esfahbod 已提交
1369 1370
struct ContextFormat1
{
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
  inline bool intersects (const hb_set_t *glyphs) const
  {
    struct ContextClosureLookupContext lookup_context = {
      {intersects_glyph},
      nullptr
    };

    unsigned int count = ruleSet.len;
    for (hb_auto_t<Coverage::Iter> iter (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
      if (glyphs->has (iter.get_glyph ()) &&
	  (this+ruleSet[iter.get_coverage ()]).intersects (glyphs, lookup_context))
        return true;
    }
    return false;
  }

1390
  inline void closure (hb_closure_context_t *c) const
1391
  {
B
Behdad Esfahbod 已提交
1392
    TRACE_CLOSURE (this);
1393 1394

    struct ContextClosureLookupContext lookup_context = {
1395
      {intersects_glyph},
B
Behdad Esfahbod 已提交
1396
      nullptr
1397 1398 1399
    };

    unsigned int count = ruleSet.len;
1400 1401 1402 1403 1404 1405 1406
    for (hb_auto_t<Coverage::Iter> iter (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
      if (c->glyphs->has (iter.get_glyph ()))
	(this+ruleSet[iter.get_coverage ()]).closure (c, lookup_context);
    }
1407 1408
  }

B
Behdad Esfahbod 已提交
1409 1410
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
1411
    TRACE_COLLECT_GLYPHS (this);
B
Minor  
Behdad Esfahbod 已提交
1412
    (this+coverage).add_coverage (c->input);
1413 1414 1415

    struct ContextCollectGlyphsLookupContext lookup_context = {
      {collect_glyph},
B
Behdad Esfahbod 已提交
1416
      nullptr
1417 1418 1419 1420
    };

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

1424 1425
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1426
    TRACE_WOULD_APPLY (this);
1427

1428
    const RuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
1429
    struct ContextApplyLookupContext lookup_context = {
1430
      {match_glyph},
B
Behdad Esfahbod 已提交
1431
      nullptr
1432
    };
B
Behdad Esfahbod 已提交
1433
    return_trace (rule_set.would_apply (c, lookup_context));
1434 1435
  }

1436
  inline const Coverage &get_coverage (void) const
1437
  { return this+coverage; }
1438

1439
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1440
  {
B
Behdad Esfahbod 已提交
1441
    TRACE_APPLY (this);
1442
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
1443
    if (likely (index == NOT_COVERED))
B
Behdad Esfahbod 已提交
1444
      return_trace (false);
1445

1446
    const RuleSet &rule_set = this+ruleSet[index];
1447
    struct ContextApplyLookupContext lookup_context = {
1448
      {match_glyph},
B
Behdad Esfahbod 已提交
1449
      nullptr
1450
    };
B
Behdad Esfahbod 已提交
1451
    return_trace (rule_set.apply (c, lookup_context));
1452 1453
  }

1454 1455 1456 1457 1458 1459 1460
  inline bool subset (hb_subset_context_t *c) const
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

B
Behdad Esfahbod 已提交
1461 1462
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1463
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1464
    return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
1465 1466
  }

1467
  protected:
B
Behdad Esfahbod 已提交
1468
  HBUINT16	format;			/* Format identifier--format = 1 */
1469 1470 1471 1472 1473 1474
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetArrayOf<RuleSet>
		ruleSet;		/* Array of RuleSet tables
					 * ordered by Coverage Index */
1475
  public:
1476
  DEFINE_SIZE_ARRAY (6, ruleSet);
1477 1478 1479
};


B
Behdad Esfahbod 已提交
1480 1481
struct ContextFormat2
{
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
  inline bool intersects (const hb_set_t *glyphs) const
  {
    if (!(this+coverage).intersects (glyphs))
      return false;

    const ClassDef &class_def = this+classDef;

    struct ContextClosureLookupContext lookup_context = {
      {intersects_class},
      &class_def
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      if (class_def.intersects_class (glyphs, i) &&
	  (this+ruleSet[i]).intersects (glyphs, lookup_context))
        return true;

    return false;
  }

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

    const ClassDef &class_def = this+classDef;

    struct ContextClosureLookupContext lookup_context = {
1512
      {intersects_class},
1513
      &class_def
1514 1515 1516 1517 1518 1519
    };

    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 已提交
1520
	rule_set.closure (c, lookup_context);
1521
      }
1522 1523
  }

B
Behdad Esfahbod 已提交
1524 1525
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
1526
    TRACE_COLLECT_GLYPHS (this);
B
Minor  
Behdad Esfahbod 已提交
1527
    (this+coverage).add_coverage (c->input);
1528

1529
    const ClassDef &class_def = this+classDef;
1530 1531
    struct ContextCollectGlyphsLookupContext lookup_context = {
      {collect_class},
1532
      &class_def
1533 1534 1535 1536
    };

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

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

    const ClassDef &class_def = this+classDef;
1545
    unsigned int index = class_def.get_class (c->glyphs[0]);
1546 1547
    const RuleSet &rule_set = this+ruleSet[index];
    struct ContextApplyLookupContext lookup_context = {
1548
      {match_class},
1549 1550
      &class_def
    };
B
Behdad Esfahbod 已提交
1551
    return_trace (rule_set.would_apply (c, lookup_context));
1552 1553
  }

1554
  inline const Coverage &get_coverage (void) const
1555
  { return this+coverage; }
1556

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

    const ClassDef &class_def = this+classDef;
1564
    index = class_def.get_class (c->buffer->cur().codepoint);
1565
    const RuleSet &rule_set = this+ruleSet[index];
1566
    struct ContextApplyLookupContext lookup_context = {
1567
      {match_class},
B
Behdad Esfahbod 已提交
1568
      &class_def
1569
    };
B
Behdad Esfahbod 已提交
1570
    return_trace (rule_set.apply (c, lookup_context));
1571 1572
  }

1573 1574 1575 1576 1577 1578 1579
  inline bool subset (hb_subset_context_t *c) const
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

B
Behdad Esfahbod 已提交
1580 1581
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1582
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1583
    return_trace (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
1584 1585
  }

1586
  protected:
B
Behdad Esfahbod 已提交
1587
  HBUINT16	format;			/* Format identifier--format = 2 */
1588 1589 1590 1591 1592 1593 1594 1595 1596
  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 */
1597
  public:
1598
  DEFINE_SIZE_ARRAY (8, ruleSet);
1599 1600 1601
};


B
Behdad Esfahbod 已提交
1602 1603
struct ContextFormat3
{
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
  inline bool intersects (const hb_set_t *glyphs) const
  {
    if (!(this+coverageZ[0]).intersects (glyphs))
      return false;

    struct ContextClosureLookupContext lookup_context = {
      {intersects_coverage},
      this
    };
    return context_intersects (glyphs,
1614
			       glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1),
1615 1616 1617
			       lookup_context);
  }

1618
  inline void closure (hb_closure_context_t *c) const
1619
  {
B
Behdad Esfahbod 已提交
1620
    TRACE_CLOSURE (this);
1621
    if (!(this+coverageZ[0]).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
1622
      return;
1623

1624
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ.arrayZ, coverageZ[0].static_size * glyphCount);
1625
    struct ContextClosureLookupContext lookup_context = {
1626
      {intersects_coverage},
1627 1628
      this
    };
B
Behdad Esfahbod 已提交
1629
    context_closure_lookup (c,
1630
			    glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1),
B
Behdad Esfahbod 已提交
1631 1632
			    lookupCount, lookupRecord,
			    lookup_context);
1633 1634
  }

B
Behdad Esfahbod 已提交
1635 1636
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
1637
    TRACE_COLLECT_GLYPHS (this);
1638
    (this+coverageZ[0]).add_coverage (c->input);
1639

1640
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ.arrayZ, coverageZ[0].static_size * glyphCount);
1641 1642
    struct ContextCollectGlyphsLookupContext lookup_context = {
      {collect_coverage},
1643
      this
1644 1645 1646
    };

    context_collect_glyphs_lookup (c,
1647
				   glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1),
1648 1649
				   lookupCount, lookupRecord,
				   lookup_context);
B
Behdad Esfahbod 已提交
1650 1651
  }

1652 1653
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1654
    TRACE_WOULD_APPLY (this);
1655

1656
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ.arrayZ, coverageZ[0].static_size * glyphCount);
1657
    struct ContextApplyLookupContext lookup_context = {
1658
      {match_coverage},
1659 1660
      this
    };
1661
    return_trace (context_would_apply_lookup (c, glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), lookupCount, lookupRecord, lookup_context));
1662 1663
  }

1664
  inline const Coverage &get_coverage (void) const
1665
  { return this+coverageZ[0]; }
1666

1667
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1668
  {
B
Behdad Esfahbod 已提交
1669
    TRACE_APPLY (this);
1670
    unsigned int index = (this+coverageZ[0]).get_coverage (c->buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1671
    if (likely (index == NOT_COVERED)) return_trace (false);
1672

1673
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ.arrayZ, coverageZ[0].static_size * glyphCount);
1674
    struct ContextApplyLookupContext lookup_context = {
1675
      {match_coverage},
B
Behdad Esfahbod 已提交
1676
      this
1677
    };
1678
    return_trace (context_apply_lookup (c, glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), lookupCount, lookupRecord, lookup_context));
1679 1680
  }

1681 1682 1683 1684 1685 1686 1687
  inline bool subset (hb_subset_context_t *c) const
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

B
Behdad Esfahbod 已提交
1688 1689
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1690
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1691
    if (!c->check_struct (this)) return_trace (false);
B
Behdad Esfahbod 已提交
1692
    unsigned int count = glyphCount;
B
Behdad Esfahbod 已提交
1693
    if (!count) return_trace (false); /* We want to access coverageZ[0] freely. */
1694
    if (!c->check_array (coverageZ.arrayZ, count)) return_trace (false);
B
Behdad Esfahbod 已提交
1695
    for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
1696
      if (!coverageZ[i].sanitize (c, this)) return_trace (false);
1697
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ.arrayZ, coverageZ[0].static_size * count);
1698
    return_trace (c->check_array (lookupRecord, lookupCount));
B
Behdad Esfahbod 已提交
1699 1700
  }

1701
  protected:
B
Behdad Esfahbod 已提交
1702 1703
  HBUINT16	format;			/* Format identifier--format = 3 */
  HBUINT16	glyphCount;		/* Number of glyphs in the input glyph
1704
					 * sequence */
B
Behdad Esfahbod 已提交
1705
  HBUINT16	lookupCount;		/* Number of LookupRecords */
1706 1707
  UnsizedArrayOf<OffsetTo<Coverage> >
		coverageZ;		/* Array of offsets to Coverage
1708
					 * table in glyph sequence order */
1709 1710
/*UnsizedArrayOf<LookupRecord>
		lookupRecordX;*/	/* Array of LookupRecords--in
1711
					 * design order */
B
Behdad Esfahbod 已提交
1712
  public:
1713
  DEFINE_SIZE_ARRAY (6, coverageZ);
1714 1715
};

B
Behdad Esfahbod 已提交
1716 1717
struct Context
{
1718
  template <typename context_t>
1719
  inline typename context_t::return_t dispatch (context_t *c) const
1720
  {
1721
    TRACE_DISPATCH (this, u.format);
1722
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1723
    switch (u.format) {
B
Behdad Esfahbod 已提交
1724 1725 1726 1727
    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 ());
1728 1729 1730
    }
  }

1731
  protected:
1732
  union {
B
Behdad Esfahbod 已提交
1733
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1734 1735 1736
  ContextFormat1	format1;
  ContextFormat2	format2;
  ContextFormat3	format3;
1737 1738 1739
  } u;
};

1740 1741 1742

/* Chaining Contextual lookups */

1743
struct ChainContextClosureLookupContext
B
Behdad Esfahbod 已提交
1744
{
1745 1746 1747 1748
  ContextClosureFuncs funcs;
  const void *intersects_data[3];
};

1749 1750 1751 1752 1753 1754
struct ChainContextCollectGlyphsLookupContext
{
  ContextCollectGlyphsFuncs funcs;
  const void *collect_data[3];
};

1755 1756 1757
struct ChainContextApplyLookupContext
{
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
1758
  const void *match_data[3];
1759 1760
};

1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
static inline bool chain_context_intersects (const hb_set_t *glyphs,
					     unsigned int backtrackCount,
					     const HBUINT16 backtrack[],
					     unsigned int inputCount, /* Including the first glyph (not matched) */
					     const HBUINT16 input[], /* Array of input values--start with second glyph */
					     unsigned int lookaheadCount,
					     const HBUINT16 lookahead[],
					     ChainContextClosureLookupContext &lookup_context)
{
  return intersects_array (glyphs,
			   backtrackCount, backtrack,
			   lookup_context.funcs.intersects, lookup_context.intersects_data[0])
      && intersects_array (glyphs,
			   inputCount ? inputCount - 1 : 0, input,
			   lookup_context.funcs.intersects, lookup_context.intersects_data[1])
      && intersects_array (glyphs,
			  lookaheadCount, lookahead,
			  lookup_context.funcs.intersects, lookup_context.intersects_data[2]);
}

B
Behdad Esfahbod 已提交
1781
static inline void chain_context_closure_lookup (hb_closure_context_t *c,
1782
						 unsigned int backtrackCount,
B
Behdad Esfahbod 已提交
1783
						 const HBUINT16 backtrack[],
1784
						 unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1785
						 const HBUINT16 input[], /* Array of input values--start with second glyph */
1786
						 unsigned int lookaheadCount,
B
Behdad Esfahbod 已提交
1787
						 const HBUINT16 lookahead[],
1788 1789 1790 1791
						 unsigned int lookupCount,
						 const LookupRecord lookupRecord[],
						 ChainContextClosureLookupContext &lookup_context)
{
1792 1793 1794 1795 1796
  if (chain_context_intersects (c->glyphs,
				backtrackCount, backtrack,
				inputCount, input,
				lookaheadCount, lookahead,
				lookup_context))
1797 1798
    recurse_lookups (c,
		     lookupCount, lookupRecord);
1799 1800
}

1801 1802
static inline void chain_context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
						        unsigned int backtrackCount,
B
Behdad Esfahbod 已提交
1803
						        const HBUINT16 backtrack[],
1804
						        unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1805
						        const HBUINT16 input[], /* Array of input values--start with second glyph */
1806
						        unsigned int lookaheadCount,
B
Behdad Esfahbod 已提交
1807
						        const HBUINT16 lookahead[],
1808 1809 1810 1811
						        unsigned int lookupCount,
						        const LookupRecord lookupRecord[],
						        ChainContextCollectGlyphsLookupContext &lookup_context)
{
B
Minor  
Behdad Esfahbod 已提交
1812
  collect_array (c, c->before,
1813 1814
		 backtrackCount, backtrack,
		 lookup_context.funcs.collect, lookup_context.collect_data[0]);
B
Minor  
Behdad Esfahbod 已提交
1815
  collect_array (c, c->input,
1816 1817
		 inputCount ? inputCount - 1 : 0, input,
		 lookup_context.funcs.collect, lookup_context.collect_data[1]);
B
Minor  
Behdad Esfahbod 已提交
1818
  collect_array (c, c->after,
1819 1820 1821 1822 1823 1824
		 lookaheadCount, lookahead,
		 lookup_context.funcs.collect, lookup_context.collect_data[2]);
  recurse_lookups (c,
		   lookupCount, lookupRecord);
}

1825 1826
static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c,
						     unsigned int backtrackCount,
B
Behdad Esfahbod 已提交
1827
						     const HBUINT16 backtrack[] HB_UNUSED,
1828
						     unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1829
						     const HBUINT16 input[], /* Array of input values--start with second glyph */
1830
						     unsigned int lookaheadCount,
B
Behdad Esfahbod 已提交
1831
						     const HBUINT16 lookahead[] HB_UNUSED,
B
Behdad Esfahbod 已提交
1832 1833
						     unsigned int lookupCount HB_UNUSED,
						     const LookupRecord lookupRecord[] HB_UNUSED,
1834 1835
						     ChainContextApplyLookupContext &lookup_context)
{
1836
  return (c->zero_context ? !backtrackCount && !lookaheadCount : true)
1837
      && would_match_input (c,
1838 1839 1840 1841
			    inputCount, input,
			    lookup_context.funcs.match, lookup_context.match_data[1]);
}

1842
static inline bool chain_context_apply_lookup (hb_ot_apply_context_t *c,
1843
					       unsigned int backtrackCount,
B
Behdad Esfahbod 已提交
1844
					       const HBUINT16 backtrack[],
1845
					       unsigned int inputCount, /* Including the first glyph (not matched) */
B
Behdad Esfahbod 已提交
1846
					       const HBUINT16 input[], /* Array of input values--start with second glyph */
1847
					       unsigned int lookaheadCount,
B
Behdad Esfahbod 已提交
1848
					       const HBUINT16 lookahead[],
1849 1850 1851
					       unsigned int lookupCount,
					       const LookupRecord lookupRecord[],
					       ChainContextApplyLookupContext &lookup_context)
1852
{
1853
  unsigned int start_index = 0, match_length = 0, end_index = 0;
1854
  unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
B
Behdad Esfahbod 已提交
1855
  return match_input (c,
B
Behdad Esfahbod 已提交
1856 1857
		      inputCount, input,
		      lookup_context.funcs.match, lookup_context.match_data[1],
1858
		      &match_length, match_positions)
B
Behdad Esfahbod 已提交
1859 1860
      && match_backtrack (c,
			  backtrackCount, backtrack,
1861 1862
			  lookup_context.funcs.match, lookup_context.match_data[0],
			  &start_index)
B
Behdad Esfahbod 已提交
1863
      && match_lookahead (c,
B
Behdad Esfahbod 已提交
1864 1865
			  lookaheadCount, lookahead,
			  lookup_context.funcs.match, lookup_context.match_data[2],
1866 1867 1868
			  match_length, &end_index)
      && (c->buffer->unsafe_to_break_from_outbuffer (start_index, end_index),
          apply_lookup (c,
1869 1870
		       inputCount, match_positions,
		       lookupCount, lookupRecord,
1871
		       match_length));
1872
}
1873

B
Behdad Esfahbod 已提交
1874 1875
struct ChainRule
{
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
  inline bool intersects (const hb_set_t *glyphs, ChainContextClosureLookupContext &lookup_context) const
  {
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
    return chain_context_intersects (glyphs,
				     backtrack.len, backtrack.arrayZ,
				     input.len, input.arrayZ,
				     lookahead.len, lookahead.arrayZ,
				     lookup_context);
  }

B
Behdad Esfahbod 已提交
1887
  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
1888
  {
B
Behdad Esfahbod 已提交
1889
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
1890 1891
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
1892
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
1893
    chain_context_closure_lookup (c,
1894 1895 1896 1897
				  backtrack.len, backtrack.arrayZ,
				  input.len, input.arrayZ,
				  lookahead.len, lookahead.arrayZ,
				  lookup.len, lookup.arrayZ,
B
Behdad Esfahbod 已提交
1898
				  lookup_context);
1899 1900
  }

1901 1902 1903
  inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
  {
    TRACE_COLLECT_GLYPHS (this);
B
Behdad Esfahbod 已提交
1904 1905
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
1906 1907
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
    chain_context_collect_glyphs_lookup (c,
1908 1909 1910 1911
					 backtrack.len, backtrack.arrayZ,
					 input.len, input.arrayZ,
					 lookahead.len, lookahead.arrayZ,
					 lookup.len, lookup.arrayZ,
1912 1913 1914
					 lookup_context);
  }

1915 1916
  inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
  {
B
Behdad Esfahbod 已提交
1917
    TRACE_WOULD_APPLY (this);
B
Behdad Esfahbod 已提交
1918 1919
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
1920
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
1921
    return_trace (chain_context_would_apply_lookup (c,
1922 1923 1924 1925
						    backtrack.len, backtrack.arrayZ,
						    input.len, input.arrayZ,
						    lookahead.len, lookahead.arrayZ, lookup.len,
						    lookup.arrayZ, lookup_context));
1926 1927
  }

1928
  inline bool apply (hb_ot_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
1929
  {
B
Behdad Esfahbod 已提交
1930
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1931 1932
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
1933
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
1934
    return_trace (chain_context_apply_lookup (c,
1935 1936 1937 1938
					      backtrack.len, backtrack.arrayZ,
					      input.len, input.arrayZ,
					      lookahead.len, lookahead.arrayZ, lookup.len,
					      lookup.arrayZ, lookup_context));
1939 1940
  }

B
Behdad Esfahbod 已提交
1941 1942
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1943
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1944
    if (!backtrack.sanitize (c)) return_trace (false);
B
Behdad Esfahbod 已提交
1945
    const HeadlessArrayOf<HBUINT16> &input = StructAfter<HeadlessArrayOf<HBUINT16> > (backtrack);
B
Behdad Esfahbod 已提交
1946
    if (!input.sanitize (c)) return_trace (false);
B
Behdad Esfahbod 已提交
1947
    const ArrayOf<HBUINT16> &lookahead = StructAfter<ArrayOf<HBUINT16> > (input);
B
Behdad Esfahbod 已提交
1948
    if (!lookahead.sanitize (c)) return_trace (false);
B
Behdad Esfahbod 已提交
1949
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
1950
    return_trace (lookup.sanitize (c));
B
Behdad Esfahbod 已提交
1951
  }
1952

1953
  protected:
B
Behdad Esfahbod 已提交
1954
  ArrayOf<HBUINT16>
B
Behdad Esfahbod 已提交
1955
		backtrack;		/* Array of backtracking values
1956 1957
					 * (to be matched before the input
					 * sequence) */
B
Behdad Esfahbod 已提交
1958
  HeadlessArrayOf<HBUINT16>
B
Behdad Esfahbod 已提交
1959
		inputX;			/* Array of input values (start with
1960
					 * second glyph) */
B
Behdad Esfahbod 已提交
1961
  ArrayOf<HBUINT16>
B
Behdad Esfahbod 已提交
1962
		lookaheadX;		/* Array of lookahead values's (to be
1963
					 * matched after the input sequence) */
B
Behdad Esfahbod 已提交
1964
  ArrayOf<LookupRecord>
1965
		lookupX;		/* Array of LookupRecords--in
1966
					 * design order) */
1967
  public:
B
Behdad Esfahbod 已提交
1968
  DEFINE_SIZE_MIN (8);
1969 1970
};

B
Behdad Esfahbod 已提交
1971 1972
struct ChainRuleSet
{
1973 1974 1975 1976 1977 1978 1979 1980
  inline bool intersects (const hb_set_t *glyphs, ChainContextClosureLookupContext &lookup_context) const
  {
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
      if ((this+rule[i]).intersects (glyphs, lookup_context))
        return true;
    return false;
  }
B
Behdad Esfahbod 已提交
1981
  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
1982
  {
B
Behdad Esfahbod 已提交
1983
    TRACE_CLOSURE (this);
1984 1985
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
1986
      (this+rule[i]).closure (c, lookup_context);
1987 1988
  }

1989 1990 1991 1992 1993 1994 1995 1996
  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);
  }

1997 1998
  inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
  {
B
Behdad Esfahbod 已提交
1999
    TRACE_WOULD_APPLY (this);
2000 2001 2002
    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 已提交
2003
        return_trace (true);
2004

B
Behdad Esfahbod 已提交
2005
    return_trace (false);
2006 2007
  }

2008
  inline bool apply (hb_ot_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
2009
  {
B
Behdad Esfahbod 已提交
2010
    TRACE_APPLY (this);
2011
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
2012
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
2013
      if ((this+rule[i]).apply (c, lookup_context))
B
Behdad Esfahbod 已提交
2014
        return_trace (true);
2015

B
Behdad Esfahbod 已提交
2016
    return_trace (false);
2017
  }
2018

B
Behdad Esfahbod 已提交
2019 2020
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2021
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
2022
    return_trace (rule.sanitize (c, this));
B
Behdad Esfahbod 已提交
2023 2024
  }

2025
  protected:
2026 2027 2028
  OffsetArrayOf<ChainRule>
		rule;			/* Array of ChainRule tables
					 * ordered by preference */
2029
  public:
2030
  DEFINE_SIZE_ARRAY (2, rule);
2031 2032
};

B
Behdad Esfahbod 已提交
2033 2034
struct ChainContextFormat1
{
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
  inline bool intersects (const hb_set_t *glyphs) const
  {
    struct ChainContextClosureLookupContext lookup_context = {
      {intersects_glyph},
      {nullptr, nullptr, nullptr}
    };

    unsigned int count = ruleSet.len;
    for (hb_auto_t<Coverage::Iter> iter (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
      if (glyphs->has (iter.get_glyph ()) &&
	  (this+ruleSet[iter.get_coverage ()]).intersects (glyphs, lookup_context))
        return true;
    }
    return false;
  }

2054
  inline void closure (hb_closure_context_t *c) const
2055
  {
B
Behdad Esfahbod 已提交
2056
    TRACE_CLOSURE (this);
2057 2058

    struct ChainContextClosureLookupContext lookup_context = {
2059
      {intersects_glyph},
B
Behdad Esfahbod 已提交
2060
      {nullptr, nullptr, nullptr}
2061 2062 2063
    };

    unsigned int count = ruleSet.len;
2064 2065 2066 2067 2068 2069 2070
    for (hb_auto_t<Coverage::Iter> iter (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
      if (c->glyphs->has (iter.get_glyph ()))
	(this+ruleSet[iter.get_coverage ()]).closure (c, lookup_context);
    }
2071 2072
  }

B
Behdad Esfahbod 已提交
2073 2074
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
2075
    TRACE_COLLECT_GLYPHS (this);
B
Minor  
Behdad Esfahbod 已提交
2076
    (this+coverage).add_coverage (c->input);
2077 2078 2079

    struct ChainContextCollectGlyphsLookupContext lookup_context = {
      {collect_glyph},
B
Behdad Esfahbod 已提交
2080
      {nullptr, nullptr, nullptr}
2081 2082 2083 2084 2085
    };

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

2088 2089
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2090
    TRACE_WOULD_APPLY (this);
2091

2092
    const ChainRuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
2093
    struct ChainContextApplyLookupContext lookup_context = {
2094
      {match_glyph},
B
Behdad Esfahbod 已提交
2095
      {nullptr, nullptr, nullptr}
2096
    };
B
Behdad Esfahbod 已提交
2097
    return_trace (rule_set.would_apply (c, lookup_context));
2098 2099
  }

2100
  inline const Coverage &get_coverage (void) const
2101
  { return this+coverage; }
2102

2103
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
2104
  {
B
Behdad Esfahbod 已提交
2105
    TRACE_APPLY (this);
2106
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
2107
    if (likely (index == NOT_COVERED)) return_trace (false);
2108

2109
    const ChainRuleSet &rule_set = this+ruleSet[index];
2110
    struct ChainContextApplyLookupContext lookup_context = {
2111
      {match_glyph},
B
Behdad Esfahbod 已提交
2112
      {nullptr, nullptr, nullptr}
2113
    };
B
Behdad Esfahbod 已提交
2114
    return_trace (rule_set.apply (c, lookup_context));
2115
  }
B
Behdad Esfahbod 已提交
2116

2117 2118 2119 2120 2121 2122 2123
  inline bool subset (hb_subset_context_t *c) const
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

B
Behdad Esfahbod 已提交
2124 2125
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2126
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
2127
    return_trace (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
2128 2129
  }

2130
  protected:
B
Behdad Esfahbod 已提交
2131
  HBUINT16	format;			/* Format identifier--format = 1 */
2132 2133
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
2134
					 * beginning of table */
2135 2136 2137
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by Coverage Index */
2138
  public:
2139
  DEFINE_SIZE_ARRAY (6, ruleSet);
2140 2141
};

B
Behdad Esfahbod 已提交
2142 2143
struct ChainContextFormat2
{
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
  inline bool intersects (const hb_set_t *glyphs) const
  {
    if (!(this+coverage).intersects (glyphs))
      return false;

    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 = {
      {intersects_class},
      {&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 (glyphs, i) &&
	  (this+ruleSet[i]).intersects (glyphs, lookup_context))
        return true;

    return false;
  }
2168
  inline void closure (hb_closure_context_t *c) const
2169
  {
B
Behdad Esfahbod 已提交
2170
    TRACE_CLOSURE (this);
2171
    if (!(this+coverage).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
2172
      return;
2173 2174 2175 2176 2177 2178

    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 = {
2179
      {intersects_class},
2180 2181 2182 2183 2184 2185 2186 2187 2188
      {&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 已提交
2189
	rule_set.closure (c, lookup_context);
2190
      }
2191 2192
  }

B
Behdad Esfahbod 已提交
2193 2194
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
2195
    TRACE_COLLECT_GLYPHS (this);
B
Minor  
Behdad Esfahbod 已提交
2196
    (this+coverage).add_coverage (c->input);
2197

2198 2199 2200 2201
    const ClassDef &backtrack_class_def = this+backtrackClassDef;
    const ClassDef &input_class_def = this+inputClassDef;
    const ClassDef &lookahead_class_def = this+lookaheadClassDef;

2202 2203
    struct ChainContextCollectGlyphsLookupContext lookup_context = {
      {collect_class},
2204 2205 2206
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
2207 2208 2209 2210 2211
    };

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

2214 2215
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2216
    TRACE_WOULD_APPLY (this);
2217

2218
    const ClassDef &backtrack_class_def = this+backtrackClassDef;
2219
    const ClassDef &input_class_def = this+inputClassDef;
2220
    const ClassDef &lookahead_class_def = this+lookaheadClassDef;
2221

2222
    unsigned int index = input_class_def.get_class (c->glyphs[0]);
2223 2224
    const ChainRuleSet &rule_set = this+ruleSet[index];
    struct ChainContextApplyLookupContext lookup_context = {
2225
      {match_class},
2226 2227 2228
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
2229
    };
B
Behdad Esfahbod 已提交
2230
    return_trace (rule_set.would_apply (c, lookup_context));
2231 2232
  }

2233
  inline const Coverage &get_coverage (void) const
2234
  { return this+coverage; }
2235

2236
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
2237
  {
B
Behdad Esfahbod 已提交
2238
    TRACE_APPLY (this);
2239
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
2240
    if (likely (index == NOT_COVERED)) return_trace (false);
2241 2242 2243 2244 2245

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

2246
    index = input_class_def.get_class (c->buffer->cur().codepoint);
2247
    const ChainRuleSet &rule_set = this+ruleSet[index];
2248
    struct ChainContextApplyLookupContext lookup_context = {
2249
      {match_class},
B
Behdad Esfahbod 已提交
2250 2251 2252
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
2253
    };
B
Behdad Esfahbod 已提交
2254
    return_trace (rule_set.apply (c, lookup_context));
2255 2256
  }

2257 2258 2259 2260 2261 2262 2263
  inline bool subset (hb_subset_context_t *c) const
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

B
Behdad Esfahbod 已提交
2264 2265
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2266
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
2267 2268 2269 2270 2271
    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 已提交
2272 2273
  }

2274
  protected:
B
Behdad Esfahbod 已提交
2275
  HBUINT16	format;			/* Format identifier--format = 2 */
2276 2277
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
2278
					 * beginning of table */
2279 2280
  OffsetTo<ClassDef>
		backtrackClassDef;	/* Offset to glyph ClassDef table
2281 2282
					 * containing backtrack sequence
					 * data--from beginning of table */
2283 2284
  OffsetTo<ClassDef>
		inputClassDef;		/* Offset to glyph ClassDef
2285 2286
					 * table containing input sequence
					 * data--from beginning of table */
2287 2288
  OffsetTo<ClassDef>
		lookaheadClassDef;	/* Offset to glyph ClassDef table
2289 2290
					 * containing lookahead sequence
					 * data--from beginning of table */
2291 2292 2293
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by class */
2294
  public:
2295
  DEFINE_SIZE_ARRAY (12, ruleSet);
2296 2297
};

B
Behdad Esfahbod 已提交
2298 2299
struct ChainContextFormat3
{
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
  inline bool intersects (const hb_set_t *glyphs) const
  {
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);

    if (!(this+input[0]).intersects (glyphs))
      return false;

    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    struct ChainContextClosureLookupContext lookup_context = {
      {intersects_coverage},
      {this, this, this}
    };
    return chain_context_intersects (glyphs,
				     backtrack.len, (const HBUINT16 *) backtrack.arrayZ,
				     input.len, (const HBUINT16 *) input.arrayZ + 1,
				     lookahead.len, (const HBUINT16 *) lookahead.arrayZ,
				     lookup_context);
  }

2319
  inline void closure (hb_closure_context_t *c) const
2320
  {
B
Behdad Esfahbod 已提交
2321
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
2322 2323 2324 2325 2326 2327 2328 2329
    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 = {
2330
      {intersects_coverage},
B
Behdad Esfahbod 已提交
2331 2332 2333
      {this, this, this}
    };
    chain_context_closure_lookup (c,
2334 2335 2336 2337
				  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 已提交
2338
				  lookup_context);
2339 2340
  }

B
Behdad Esfahbod 已提交
2341 2342
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
2343 2344 2345
    TRACE_COLLECT_GLYPHS (this);
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);

B
Minor  
Behdad Esfahbod 已提交
2346
    (this+input[0]).add_coverage (c->input);
2347 2348 2349 2350 2351 2352 2353 2354

    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,
2355 2356 2357 2358
					 backtrack.len, (const HBUINT16 *) backtrack.arrayZ,
					 input.len, (const HBUINT16 *) input.arrayZ + 1,
					 lookahead.len, (const HBUINT16 *) lookahead.arrayZ,
					 lookup.len, lookup.arrayZ,
2359
					 lookup_context);
B
Behdad Esfahbod 已提交
2360 2361
  }

2362 2363
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2364
    TRACE_WOULD_APPLY (this);
2365

B
Behdad Esfahbod 已提交
2366
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2367 2368 2369
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
    struct ChainContextApplyLookupContext lookup_context = {
2370
      {match_coverage},
2371 2372
      {this, this, this}
    };
B
Behdad Esfahbod 已提交
2373
    return_trace (chain_context_would_apply_lookup (c,
2374 2375 2376 2377
						    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));
2378 2379
  }

2380 2381 2382 2383 2384 2385
  inline const Coverage &get_coverage (void) const
  {
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
    return this+input[0];
  }

2386
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
2387
  {
B
Behdad Esfahbod 已提交
2388
    TRACE_APPLY (this);
2389
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
2390

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

2394 2395
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
2396
    struct ChainContextApplyLookupContext lookup_context = {
2397
      {match_coverage},
B
Behdad Esfahbod 已提交
2398
      {this, this, this}
2399
    };
B
Behdad Esfahbod 已提交
2400
    return_trace (chain_context_apply_lookup (c,
2401 2402 2403 2404
					      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));
2405 2406
  }

2407 2408 2409 2410 2411 2412 2413
  inline bool subset (hb_subset_context_t *c) const
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

B
Behdad Esfahbod 已提交
2414 2415
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2416
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
2417
    if (!backtrack.sanitize (c, this)) return_trace (false);
B
Behdad Esfahbod 已提交
2418
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
B
Behdad Esfahbod 已提交
2419 2420
    if (!input.sanitize (c, this)) return_trace (false);
    if (!input.len) return_trace (false); /* To be consistent with Context. */
B
Behdad Esfahbod 已提交
2421
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
B
Behdad Esfahbod 已提交
2422
    if (!lookahead.sanitize (c, this)) return_trace (false);
B
Behdad Esfahbod 已提交
2423
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
2424
    return_trace (lookup.sanitize (c));
B
Behdad Esfahbod 已提交
2425 2426
  }

2427
  protected:
B
Behdad Esfahbod 已提交
2428
  HBUINT16	format;			/* Format identifier--format = 3 */
B
Behdad Esfahbod 已提交
2429
  OffsetArrayOf<Coverage>
2430
		backtrack;		/* Array of coverage tables
2431 2432
					 * in backtracking sequence, in  glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
2433
  OffsetArrayOf<Coverage>
2434
		inputX		;	/* Array of coverage
2435 2436
					 * tables in input sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
2437
  OffsetArrayOf<Coverage>
2438
		lookaheadX;		/* Array of coverage tables
2439 2440
					 * in lookahead sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
2441
  ArrayOf<LookupRecord>
2442
		lookupX;		/* Array of LookupRecords--in
B
Behdad Esfahbod 已提交
2443
					 * design order) */
2444
  public:
B
Behdad Esfahbod 已提交
2445
  DEFINE_SIZE_MIN (10);
2446 2447
};

B
Behdad Esfahbod 已提交
2448 2449
struct ChainContext
{
2450
  template <typename context_t>
2451
  inline typename context_t::return_t dispatch (context_t *c) const
2452
  {
2453
    TRACE_DISPATCH (this, u.format);
2454
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
2455
    switch (u.format) {
B
Behdad Esfahbod 已提交
2456 2457 2458 2459
    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 ());
2460 2461 2462
    }
  }

2463
  protected:
2464
  union {
B
Behdad Esfahbod 已提交
2465
  HBUINT16		format;	/* Format identifier */
B
Behdad Esfahbod 已提交
2466 2467 2468
  ChainContextFormat1	format1;
  ChainContextFormat2	format2;
  ChainContextFormat3	format3;
2469 2470 2471 2472
  } u;
};


2473
template <typename T>
2474 2475 2476 2477
struct ExtensionFormat1
{
  inline unsigned int get_type (void) const { return extensionLookupType; }

2478 2479 2480 2481
  template <typename X>
  inline const X& get_subtable (void) const
  {
    unsigned int offset = extensionOffset;
B
Behdad Esfahbod 已提交
2482 2483
    if (unlikely (!offset)) return Null(typename T::SubTable);
    return StructAtOffset<typename T::SubTable> (this, offset);
2484 2485 2486 2487 2488 2489
  }

  template <typename context_t>
  inline typename context_t::return_t dispatch (context_t *c) const
  {
    TRACE_DISPATCH (this, format);
2490
    if (unlikely (!c->may_dispatch (this, this))) return_trace (c->no_dispatch_return_value ());
B
Behdad Esfahbod 已提交
2491
    return_trace (get_subtable<typename T::SubTable> ().dispatch (c, get_type ()));
2492 2493 2494
  }

  /* This is called from may_dispatch() above with hb_sanitize_context_t. */
B
Behdad Esfahbod 已提交
2495 2496
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2497
    TRACE_SANITIZE (this);
2498 2499
    return_trace (c->check_struct (this) &&
		  extensionOffset != 0 &&
B
Behdad Esfahbod 已提交
2500
		  extensionLookupType != T::SubTable::Extension);
B
Behdad Esfahbod 已提交
2501 2502
  }

2503
  protected:
B
Behdad Esfahbod 已提交
2504 2505
  HBUINT16	format;			/* Format identifier. Set to 1. */
  HBUINT16	extensionLookupType;	/* Lookup type of subtable referenced
2506 2507
					 * by ExtensionOffset (i.e. the
					 * extension subtable). */
B
Behdad Esfahbod 已提交
2508
  HBUINT32	extensionOffset;	/* Offset to the extension subtable,
2509
					 * of lookup type subtable. */
2510 2511
  public:
  DEFINE_SIZE_STATIC (8);
2512 2513
};

B
Behdad Esfahbod 已提交
2514
template <typename T>
2515 2516 2517 2518 2519
struct Extension
{
  inline unsigned int get_type (void) const
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
2520
    case 1: return u.format1.get_type ();
2521 2522 2523
    default:return 0;
    }
  }
2524 2525 2526
  template <typename X>
  inline const X& get_subtable (void) const
  {
2527
    switch (u.format) {
B
Behdad Esfahbod 已提交
2528 2529
    case 1: return u.format1.template get_subtable<typename T::SubTable> ();
    default:return Null(typename T::SubTable);
2530
    }
2531 2532
  }

B
Behdad Esfahbod 已提交
2533
  template <typename context_t>
2534
  inline typename context_t::return_t dispatch (context_t *c) const
B
Behdad Esfahbod 已提交
2535
  {
2536
    TRACE_DISPATCH (this, u.format);
2537
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
B
Behdad Esfahbod 已提交
2538
    switch (u.format) {
B
Behdad Esfahbod 已提交
2539 2540
    case 1: return_trace (u.format1.dispatch (c));
    default:return_trace (c->default_return_value ());
B
Behdad Esfahbod 已提交
2541 2542 2543
    }
  }

2544
  protected:
2545
  union {
B
Behdad Esfahbod 已提交
2546
  HBUINT16		format;		/* Format identifier */
2547
  ExtensionFormat1<T>	format1;
2548 2549 2550 2551
  } u;
};


B
Behdad Esfahbod 已提交
2552 2553 2554 2555
/*
 * GSUB/GPOS Common
 */

B
Behdad Esfahbod 已提交
2556 2557
struct GSUBGPOS
{
2558
  inline bool has_data (void) const { return version.to_int () != 0; }
2559 2560 2561 2562
  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 已提交
2563 2564 2565 2566
  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); }
2567 2568 2569 2570 2571 2572 2573
  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; }
2574 2575
  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 已提交
2576 2577 2578 2579
  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); }
2580 2581 2582 2583 2584 2585 2586 2587 2588
  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 已提交
2589

2590 2591
  inline bool find_variations_index (const int *coords, unsigned int num_coords,
				     unsigned int *index) const
2592
  { return (version.to_int () >= 0x00010001u ? this+featureVars : Null(FeatureVariations))
2593
	   .find_index (coords, num_coords, index); }
2594 2595 2596 2597 2598 2599
  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)
    {
2600 2601
      const Feature *feature = (this+featureVars).find_substitute (variations_index,
								   feature_index);
2602 2603 2604 2605 2606
      if (feature)
        return *feature;
    }
    return get_feature (feature_index);
  }
2607

B
Behdad Esfahbod 已提交
2608
  template <typename TLookup>
2609 2610 2611 2612 2613
  inline bool subset (hb_subset_context_t *c) const
  {
    TRACE_SUBSET (this);
    struct GSUBGPOS *out = c->serializer->embed (*this);
    if (unlikely (!out)) return_trace (false);
2614 2615
    out->scriptList.serialize_subset (c, this+scriptList, out);
    out->featureList.serialize_subset (c, this+featureList, out);
B
Behdad Esfahbod 已提交
2616 2617 2618 2619 2620 2621 2622 2623

    typedef OffsetListOf<TLookup> TLookupList;
    /* TODO Use intersects() to count how many subtables survive? */
    CastR<OffsetTo<TLookupList> > (out->lookupList)
      .serialize_subset (c,
			 this+CastR<const OffsetTo<TLookupList> > (lookupList),
			 out);

2624
    if (version.to_int () >= 0x00010001u)
2625
     out->featureVars.serialize_subset (c, this+featureVars, out);
2626 2627 2628 2629 2630 2631 2632 2633 2634
    return_trace (true);
  }

  inline unsigned int get_size (void) const
  {
    return min_size +
	   (version.to_int () >= 0x00010001u ? featureVars.static_size : 0);
  }

2635
  template <typename TLookup>
B
Behdad Esfahbod 已提交
2636 2637
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
2638
    TRACE_SANITIZE (this);
2639
    typedef OffsetListOf<TLookup> TLookupList;
B
Behdad Esfahbod 已提交
2640 2641 2642 2643
    return_trace (version.sanitize (c) &&
		  likely (version.major == 1) &&
		  scriptList.sanitize (c, this) &&
		  featureList.sanitize (c, this) &&
2644
		  CastR<OffsetTo<TLookupList> > (lookupList).sanitize (c, this) &&
2645
		  (version.to_int () < 0x00010001u || featureVars.sanitize (c, this)));
B
Behdad Esfahbod 已提交
2646 2647
  }

B
WIP  
Behdad Esfahbod 已提交
2648 2649 2650 2651 2652 2653
  template <typename T>
  struct accelerator_t
  {
    inline void init (hb_face_t *face)
    {
      this->blob = hb_sanitize_context_t().reference_table<T> (face);
B
Behdad Esfahbod 已提交
2654
      table = this->blob->template as<T> ();
B
WIP  
Behdad Esfahbod 已提交
2655

2656
      this->lookup_count = table->get_lookup_count ();
B
WIP  
Behdad Esfahbod 已提交
2657 2658 2659 2660 2661 2662

      this->accels = (hb_ot_layout_lookup_accelerator_t *) calloc (this->lookup_count, sizeof (hb_ot_layout_lookup_accelerator_t));
      if (unlikely (!this->accels))
        this->lookup_count = 0;

      for (unsigned int i = 0; i < this->lookup_count; i++)
2663
	this->accels[i].init (table->get_lookup (i));
B
WIP  
Behdad Esfahbod 已提交
2664 2665 2666 2667
    }

    inline void fini (void)
    {
2668 2669 2670
      for (unsigned int i = 0; i < this->lookup_count; i++)
	this->accels[i].fini ();
      free (this->accels);
B
WIP  
Behdad Esfahbod 已提交
2671 2672 2673
      hb_blob_destroy (this->blob);
    }

2674 2675 2676 2677
    hb_blob_t *blob;
    const T *table;
    unsigned int lookup_count;
    hb_ot_layout_lookup_accelerator_t *accels;
B
WIP  
Behdad Esfahbod 已提交
2678 2679
  };

2680
  protected:
B
Behdad Esfahbod 已提交
2681
  FixedVersion<>version;	/* Version of the GSUB/GPOS table--initially set
2682
				 * to 0x00010000u */
B
Behdad Esfahbod 已提交
2683 2684 2685 2686 2687 2688
  OffsetTo<ScriptList>
		scriptList;  	/* ScriptList table */
  OffsetTo<FeatureList>
		featureList; 	/* FeatureList table */
  OffsetTo<LookupList>
		lookupList; 	/* LookupList table */
B
Behdad Esfahbod 已提交
2689
  LOffsetTo<FeatureVariations>
2690 2691 2692 2693
		featureVars;	/* Offset to Feature Variations
				   table--from beginning of table
				 * (may be NULL).  Introduced
				 * in version 0x00010001. */
2694
  public:
2695
  DEFINE_SIZE_MIN (10);
B
Behdad Esfahbod 已提交
2696
};
2697

2698

B
Behdad Esfahbod 已提交
2699
} /* namespace OT */
2700

B
Behdad Esfahbod 已提交
2701

2702
#endif /* HB_OT_LAYOUT_GSUBGPOS_HH */