hb-aat-layout-common.hh 20.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright © 2017  Google, Inc.
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * 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.
 *
 * Google Author(s): Behdad Esfahbod
 */

27 28
#ifndef HB_AAT_LAYOUT_COMMON_HH
#define HB_AAT_LAYOUT_COMMON_HH
29

30
#include "hb-aat-layout.hh"
31 32 33 34 35 36 37


namespace AAT {

using namespace OT;


B
Behdad Esfahbod 已提交
38 39 40 41 42 43 44 45 46 47 48 49
/*
 * Lookup Table
 */

template <typename T> struct Lookup;

template <typename T>
struct LookupFormat0
{
  friend struct Lookup<T>;

  private:
50
  inline const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
B
Behdad Esfahbod 已提交
51
  {
52 53
    if (unlikely (glyph_id >= num_glyphs)) return nullptr;
    return &arrayZ[glyph_id];
B
Behdad Esfahbod 已提交
54 55 56 57 58
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
59
    return_trace (arrayZ.sanitize (c, c->get_num_glyphs ()));
B
Behdad Esfahbod 已提交
60 61 62
  }

  protected:
B
Behdad Esfahbod 已提交
63
  HBUINT16	format;		/* Format identifier--format = 0 */
B
Behdad Esfahbod 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  UnsizedArrayOf<T>
		arrayZ;		/* Array of lookup values, indexed by glyph index. */
  public:
  DEFINE_SIZE_ARRAY (2, arrayZ);
};


template <typename T>
struct LookupSegmentSingle
{
  inline int cmp (hb_codepoint_t g) const {
    return g < first ? -1 : g <= last ? 0 : +1 ;
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) && value.sanitize (c));
  }

  GlyphID	last;		/* Last GlyphID in this segment */
  GlyphID	first;		/* First GlyphID in this segment */
  T		value;		/* The lookup value (only one) */
  public:
88
  DEFINE_SIZE_STATIC (4 + T::static_size);
B
Behdad Esfahbod 已提交
89 90 91 92 93 94 95 96
};

template <typename T>
struct LookupFormat2
{
  friend struct Lookup<T>;

  private:
97
  inline const T* get_value (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
98 99
  {
    const LookupSegmentSingle<T> *v = segments.bsearch (glyph_id);
100
    return v ? &v->value : nullptr;
B
Behdad Esfahbod 已提交
101 102 103 104 105 106 107 108 109
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (segments.sanitize (c));
  }

  protected:
B
Behdad Esfahbod 已提交
110
  HBUINT16	format;		/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
111
  VarSizedBinSearchArrayOf<LookupSegmentSingle<T> >
B
Behdad Esfahbod 已提交
112 113 114 115 116 117 118 119 120 121
		segments;	/* The actual segments. These must already be sorted,
				 * according to the first word in each one (the last
				 * glyph in each segment). */
  public:
  DEFINE_SIZE_ARRAY (8, segments);
};

template <typename T>
struct LookupSegmentArray
{
122
  inline const T* get_value (hb_codepoint_t glyph_id, const void *base) const
B
Behdad Esfahbod 已提交
123
  {
124
    return first <= glyph_id && glyph_id <= last ? &(base+valuesZ)[glyph_id - first] : nullptr;
B
Behdad Esfahbod 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  }

  inline int cmp (hb_codepoint_t g) const {
    return g < first ? -1 : g <= last ? 0 : +1 ;
  }

  inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
		  first <= last &&
		  valuesZ.sanitize (c, base, last - first + 1));
  }

  GlyphID	last;		/* Last GlyphID in this segment */
  GlyphID	first;		/* First GlyphID in this segment */
141
  OffsetTo<UnsizedArrayOf<T>, HBUINT16, false>
B
Behdad Esfahbod 已提交
142 143 144 145 146 147 148 149 150 151 152 153
		valuesZ;	/* A 16-bit offset from the start of
				 * the table to the data. */
  public:
  DEFINE_SIZE_STATIC (6);
};

template <typename T>
struct LookupFormat4
{
  friend struct Lookup<T>;

  private:
154
  inline const T* get_value (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
155 156
  {
    const LookupSegmentArray<T> *v = segments.bsearch (glyph_id);
157
    return v ? v->get_value (glyph_id, this) : nullptr;
B
Behdad Esfahbod 已提交
158 159 160 161 162 163 164 165 166
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (segments.sanitize (c, this));
  }

  protected:
167
  HBUINT16	format;		/* Format identifier--format = 4 */
B
Behdad Esfahbod 已提交
168
  VarSizedBinSearchArrayOf<LookupSegmentArray<T> >
B
Behdad Esfahbod 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		segments;	/* The actual segments. These must already be sorted,
				 * according to the first word in each one (the last
				 * glyph in each segment). */
  public:
  DEFINE_SIZE_ARRAY (8, segments);
};

template <typename T>
struct LookupSingle
{
  inline int cmp (hb_codepoint_t g) const { return glyph.cmp (g); }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) && value.sanitize (c));
  }

  GlyphID	glyph;		/* Last GlyphID */
  T		value;		/* The lookup value (only one) */
  public:
190
  DEFINE_SIZE_STATIC (2 + T::static_size);
B
Behdad Esfahbod 已提交
191 192 193 194 195 196 197 198
};

template <typename T>
struct LookupFormat6
{
  friend struct Lookup<T>;

  private:
199
  inline const T* get_value (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
200 201
  {
    const LookupSingle<T> *v = entries.bsearch (glyph_id);
202
    return v ? &v->value : nullptr;
B
Behdad Esfahbod 已提交
203 204 205 206 207 208 209 210 211
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (entries.sanitize (c));
  }

  protected:
B
Behdad Esfahbod 已提交
212
  HBUINT16	format;		/* Format identifier--format = 6 */
B
Behdad Esfahbod 已提交
213
  VarSizedBinSearchArrayOf<LookupSingle<T> >
B
Behdad Esfahbod 已提交
214 215 216 217 218 219 220 221 222 223 224
		entries;	/* The actual entries, sorted by glyph index. */
  public:
  DEFINE_SIZE_ARRAY (8, entries);
};

template <typename T>
struct LookupFormat8
{
  friend struct Lookup<T>;

  private:
225
  inline const T* get_value (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
226
  {
B
Behdad Esfahbod 已提交
227 228
    return firstGlyph <= glyph_id && glyph_id - firstGlyph < glyphCount ?
	   &valueArrayZ[glyph_id - firstGlyph] : nullptr;
B
Behdad Esfahbod 已提交
229 230 231 232 233 234 235 236 237
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) && valueArrayZ.sanitize (c, glyphCount));
  }

  protected:
B
Behdad Esfahbod 已提交
238
  HBUINT16	format;		/* Format identifier--format = 8 */
B
Behdad Esfahbod 已提交
239
  GlyphID	firstGlyph;	/* First glyph index included in the trimmed array. */
B
Behdad Esfahbod 已提交
240
  HBUINT16	glyphCount;	/* Total number of glyphs (equivalent to the last
B
Behdad Esfahbod 已提交
241 242 243 244 245 246 247 248
				 * glyph minus the value of firstGlyph plus 1). */
  UnsizedArrayOf<T>
		valueArrayZ;	/* The lookup values (indexed by the glyph index
				 * minus the value of firstGlyph). */
  public:
  DEFINE_SIZE_ARRAY (6, valueArrayZ);
};

B
Behdad Esfahbod 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
template <typename T>
struct LookupFormat10
{
  friend struct Lookup<T>;

  private:
  inline const typename T::type get_value_or_null (hb_codepoint_t glyph_id) const
  {
    if (!(firstGlyph <= glyph_id && glyph_id - firstGlyph < glyphCount))
      return Null(T);

    const HBUINT8 *p = &valueArrayZ[(glyph_id - firstGlyph) * valueSize];

    unsigned int v = 0;
    unsigned int count = valueSize;
    for (unsigned int i = 0; i < count; i++)
      v = (v << 8) | *p++;

    return v;
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
		  valueSize <= 4 &&
		  valueArrayZ.sanitize (c, glyphCount * valueSize));
  }

  protected:
  HBUINT16	format;		/* Format identifier--format = 8 */
  HBUINT16	valueSize;	/* Byte size of each value. */
  GlyphID	firstGlyph;	/* First glyph index included in the trimmed array. */
  HBUINT16	glyphCount;	/* Total number of glyphs (equivalent to the last
				 * glyph minus the value of firstGlyph plus 1). */
  UnsizedArrayOf<HBUINT8>
		valueArrayZ;	/* The lookup values (indexed by the glyph index
				 * minus the value of firstGlyph). */
  public:
288
  DEFINE_SIZE_ARRAY (8, valueArrayZ);
B
Behdad Esfahbod 已提交
289 290
};

B
Behdad Esfahbod 已提交
291 292 293
template <typename T>
struct Lookup
{
294
  inline const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
B
Behdad Esfahbod 已提交
295 296 297 298 299 300 301
  {
    switch (u.format) {
    case 0: return u.format0.get_value (glyph_id, num_glyphs);
    case 2: return u.format2.get_value (glyph_id);
    case 4: return u.format4.get_value (glyph_id);
    case 6: return u.format6.get_value (glyph_id);
    case 8: return u.format8.get_value (glyph_id);
302
    default:return nullptr;
B
Behdad Esfahbod 已提交
303 304 305
    }
  }

B
Behdad Esfahbod 已提交
306
  inline const typename T::type get_value_or_null (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
307
  {
B
Behdad Esfahbod 已提交
308 309 310 311 312 313 314
    switch (u.format) {
      /* Format 10 cannot return a pointer. */
      case 10: return u.format10.get_value_or_null (glyph_id);
      default:
      const T *v = get_value (glyph_id, num_glyphs);
      return v ? *v : Null(T);
    }
315 316
  }

B
Behdad Esfahbod 已提交
317 318 319 320 321 322 323 324 325 326
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    if (!u.format.sanitize (c)) return_trace (false);
    switch (u.format) {
    case 0: return_trace (u.format0.sanitize (c));
    case 2: return_trace (u.format2.sanitize (c));
    case 4: return_trace (u.format4.sanitize (c));
    case 6: return_trace (u.format6.sanitize (c));
    case 8: return_trace (u.format8.sanitize (c));
B
Behdad Esfahbod 已提交
327
    case 10: return_trace (u.format10.sanitize (c));
B
Behdad Esfahbod 已提交
328 329 330 331 332 333
    default:return_trace (true);
    }
  }

  protected:
  union {
B
Behdad Esfahbod 已提交
334
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
335 336 337 338 339
  LookupFormat0<T>	format0;
  LookupFormat2<T>	format2;
  LookupFormat4<T>	format4;
  LookupFormat6<T>	format6;
  LookupFormat8<T>	format8;
B
Behdad Esfahbod 已提交
340
  LookupFormat10<T>	format10;
B
Behdad Esfahbod 已提交
341 342
  } u;
  public:
343
  DEFINE_SIZE_UNION (2, format);
B
Behdad Esfahbod 已提交
344
};
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
/* Lookup 0 has unbounded size (dependant on num_glyphs).  So we need to defined
 * special NULL objects for Lookup<> objects, but since it's template our macros
 * don't work.  So we have to hand-code them here.  UGLY. */
} /* Close namespace. */
/* Ugly hand-coded null objects for template Lookup<> :(. */
extern HB_INTERNAL const unsigned char _hb_Null_AAT_Lookup[2];
template <>
/*static*/ inline const AAT::Lookup<OT::HBUINT16>& Null<AAT::Lookup<OT::HBUINT16> > (void) {
  return *reinterpret_cast<const AAT::Lookup<OT::HBUINT16> *> (_hb_Null_AAT_Lookup);
}
template <>
/*static*/ inline const AAT::Lookup<OT::HBUINT32>& Null<AAT::Lookup<OT::HBUINT32> > (void) {
  return *reinterpret_cast<const AAT::Lookup<OT::HBUINT32> *> (_hb_Null_AAT_Lookup);
}
template <>
B
Behdad Esfahbod 已提交
360 361
/*static*/ inline const AAT::Lookup<OT::Offset<OT::HBUINT16, false> >& Null<AAT::Lookup<OT::Offset<OT::HBUINT16, false> > > (void) {
  return *reinterpret_cast<const AAT::Lookup<OT::Offset<OT::HBUINT16, false> > *> (_hb_Null_AAT_Lookup);
362 363
}
namespace AAT {
B
Behdad Esfahbod 已提交
364

365
enum { DELETED_GLYPH = 0xFFFF };
B
Behdad Esfahbod 已提交
366

367
/*
B
Minor  
Behdad Esfahbod 已提交
368
 * (Extended) State Table
369 370 371 372 373 374 375 376
 */

template <typename T>
struct Entry
{
  inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
  {
    TRACE_SANITIZE (this);
377 378 379
    /* Note, we don't recurse-sanitize data because we don't access it.
     * That said, in our DEFINE_SIZE_STATIC we access T::static_size,
     * which ensures that data has a simple sanitize(). To be determined
380 381 382 383
     * if I need to remove that as well.
     *
     * XXX Because we are a template, our DEFINE_SIZE_STATIC assertion
     * wouldn't be checked. */
384
    return_trace (c->check_struct (this));
385 386 387
  }

  public:
388 389 390
  HBUINT16	newState;	/* Byte offset from beginning of state table
				 * to the new state. Really?!?! Or just state
				 * number?  The latter in morx for sure. */
391 392 393 394 395 396 397 398 399
  HBUINT16	flags;		/* Table specific. */
  T		data;		/* Optional offsets to per-glyph tables. */
  public:
  DEFINE_SIZE_STATIC (4 + T::static_size);
};

template <>
struct Entry<void>
{
400
  inline bool sanitize (hb_sanitize_context_t *c, unsigned int count /*XXX Unused?*/) const
401 402 403 404 405 406 407 408 409 410 411 412
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this));
  }

  public:
  HBUINT16	newState;	/* Byte offset from beginning of state table to the new state. */
  HBUINT16	flags;		/* Table specific. */
  public:
  DEFINE_SIZE_STATIC (4);
};

413
template <typename Types, typename Extra>
414 415
struct StateTable
{
416 417 418 419
  typedef typename Types::HBUINT HBUINT;
  typedef typename Types::HBUSHORT HBUSHORT;
  typedef typename Types::ClassType ClassType;

420 421 422 423 424 425 426 427 428 429 430 431 432
  enum State
  {
    STATE_START_OF_TEXT = 0,
    STATE_START_OF_LINE = 1,
  };
  enum Class
  {
    CLASS_END_OF_TEXT = 0,
    CLASS_OUT_OF_BOUNDS = 1,
    CLASS_DELETED_GLYPH = 2,
    CLASS_END_OF_LINE = 3,
  };

B
Behdad Esfahbod 已提交
433
  inline unsigned int new_state (unsigned int newState) const
B
Behdad Esfahbod 已提交
434
  { return Types::extended ? newState : (newState - stateArrayTable) / nClasses; }
B
Behdad Esfahbod 已提交
435

436
  inline unsigned int get_class (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
B
Behdad Esfahbod 已提交
437
  {
438
    if (unlikely (glyph_id == DELETED_GLYPH)) return CLASS_DELETED_GLYPH;
439
    return (this+classTable).get_class (glyph_id, num_glyphs);
B
Behdad Esfahbod 已提交
440
  }
441

442 443 444 445 446 447
  inline const Entry<Extra> *get_entries () const
  {
    return (this+entryTable).arrayZ;
  }

  inline const Entry<Extra> *get_entryZ (unsigned int state, unsigned int klass) const
448
  {
B
Behdad Esfahbod 已提交
449 450
    if (unlikely (klass >= nClasses)) return nullptr;

451
    const HBUSHORT *states = (this+stateArrayTable).arrayZ;
452 453
    const Entry<Extra> *entries = (this+entryTable).arrayZ;

B
Behdad Esfahbod 已提交
454
    unsigned int entry = states[state * nClasses + klass];
455

B
Behdad Esfahbod 已提交
456
    return &entries[entry];
457 458
  }

459 460
  inline bool sanitize (hb_sanitize_context_t *c,
			unsigned int *num_entries_out = nullptr) const
461 462
  {
    TRACE_SANITIZE (this);
463 464
    if (unlikely (!(c->check_struct (this) &&
		    classTable.sanitize (c, this)))) return_trace (false);
B
Behdad Esfahbod 已提交
465

466
    const HBUSHORT *states = (this+stateArrayTable).arrayZ;
B
Behdad Esfahbod 已提交
467 468
    const Entry<Extra> *entries = (this+entryTable).arrayZ;

B
Behdad Esfahbod 已提交
469 470
    unsigned int num_classes = nClasses;

B
Behdad Esfahbod 已提交
471 472 473 474 475 476 477
    unsigned int num_states = 1;
    unsigned int num_entries = 0;

    unsigned int state = 0;
    unsigned int entry = 0;
    while (state < num_states)
    {
B
Behdad Esfahbod 已提交
478 479 480
      if (unlikely (hb_unsigned_mul_overflows (num_classes, states[0].static_size)))
	return_trace (false);

B
Behdad Esfahbod 已提交
481
      if (unlikely (!c->check_array (states,
482
				     num_states,
B
Behdad Esfahbod 已提交
483
				     num_classes * states[0].static_size)))
B
Behdad Esfahbod 已提交
484
	return_trace (false);
B
Behdad Esfahbod 已提交
485 486
      if ((c->max_ops -= num_states - state) < 0)
	return_trace (false);
B
Behdad Esfahbod 已提交
487
      { /* Sweep new states. */
488 489
	const HBUSHORT *stop = &states[num_states * num_classes];
	for (const HBUSHORT *p = &states[state * num_classes]; p < stop; p++)
B
Behdad Esfahbod 已提交
490 491 492 493
	  num_entries = MAX<unsigned int> (num_entries, *p + 1);
	state = num_states;
      }

494
      if (unlikely (!c->check_array (entries, num_entries)))
B
Behdad Esfahbod 已提交
495
	return_trace (false);
B
Behdad Esfahbod 已提交
496 497
      if ((c->max_ops -= num_entries - entry) < 0)
	return_trace (false);
B
Behdad Esfahbod 已提交
498 499 500
      { /* Sweep new entries. */
	const Entry<Extra> *stop = &entries[num_entries];
	for (const Entry<Extra> *p = &entries[entry]; p < stop; p++)
B
Behdad Esfahbod 已提交
501 502 503 504
	{
	  unsigned int newState = new_state (p->newState);
	  num_states = MAX<unsigned int> (num_states, newState + 1);
	}
B
Behdad Esfahbod 已提交
505 506 507 508
	entry = num_entries;
      }
    }

509 510 511
    if (num_entries_out)
      *num_entries_out = num_entries;

B
Behdad Esfahbod 已提交
512
    return_trace (true);
513 514 515
  }

  protected:
516
  HBUINT	nClasses;	/* Number of classes, which is the number of indices
517
				 * in a single line in the state array. */
518
  OffsetTo<ClassType, HBUINT, false>
519
		classTable;	/* Offset to the class table. */
520
  OffsetTo<UnsizedArrayOf<HBUSHORT>, HBUINT, false>
521
		stateArrayTable;/* Offset to the state array. */
522
  OffsetTo<UnsizedArrayOf<Entry<Extra> >, HBUINT, false>
523 524 525
		entryTable;	/* Offset to the entry array. */

  public:
B
Behdad Esfahbod 已提交
526
  DEFINE_SIZE_STATIC (4 * sizeof (HBUINT));
527 528
};

529 530
struct ClassTable
{
B
Behdad Esfahbod 已提交
531
  inline unsigned int get_class (hb_codepoint_t glyph_id, unsigned int outOfRange=0) const
532
  {
B
Behdad Esfahbod 已提交
533 534
    unsigned int i = glyph_id - firstGlyph;
    return i >= classArray.len ? outOfRange : classArray.arrayZ[i];
535 536 537 538
  }
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
539
    return_trace (c->check_struct (this) && classArray.sanitize (c));
540 541
  }
  protected:
B
Behdad Esfahbod 已提交
542 543 544
  GlyphID		firstGlyph;	/* First glyph index included in the trimmed array. */
  ArrayOf<HBUINT8>	classArray;	/* The class codes (indexed by glyph index minus
					 * firstGlyph). */
545
  public:
B
Behdad Esfahbod 已提交
546
  DEFINE_SIZE_ARRAY (4, classArray);
547 548
};

B
Behdad Esfahbod 已提交
549
struct ObsoleteTypes
550 551 552 553 554 555 556 557
{
  static const bool extended = false;
  typedef HBUINT16 HBUINT;
  typedef HBUINT8 HBUSHORT;
  struct ClassType : ClassTable
  {
    inline unsigned int get_class (hb_codepoint_t glyph_id, unsigned int num_glyphs HB_UNUSED) const
    {
B
Behdad Esfahbod 已提交
558
      return ClassTable::get_class (glyph_id, 1);
559 560
    }
  };
B
Behdad Esfahbod 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574
  template <typename T>
  static inline unsigned int offsetToIndex (unsigned int offset,
					    const void *base,
					    const T *array)
  {
    return (offset - ((const char *) array - (const char *) base)) / sizeof (T);
  }
  template <typename T>
  static inline unsigned int wordOffsetToIndex (unsigned int offset,
						const void *base,
						const T *array)
  {
    return offsetToIndex (2 * offset, base, array);
  }
575
};
B
Behdad Esfahbod 已提交
576
struct ExtendedTypes
577 578 579 580 581 582 583 584 585 586 587 588
{
  static const bool extended = true;
  typedef HBUINT32 HBUINT;
  typedef HBUINT16 HBUSHORT;
  struct ClassType : Lookup<HBUINT16>
  {
    inline unsigned int get_class (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
    {
      const HBUINT16 *v = get_value (glyph_id, num_glyphs);
      return v ? *v : 1;
    }
  };
B
Behdad Esfahbod 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602
  template <typename T>
  static inline unsigned int offsetToIndex (unsigned int offset,
					    const void *base,
					    const T *array)
  {
    return offset;
  }
  template <typename T>
  static inline unsigned int wordOffsetToIndex (unsigned int offset,
						const void *base,
						const T *array)
  {
    return offset;
  }
603 604 605
};

template <typename Types, typename EntryData>
606 607
struct StateTableDriver
{
608
  inline StateTableDriver (const StateTable<Types, EntryData> &machine_,
609 610 611 612
			   hb_buffer_t *buffer_,
			   hb_face_t *face_) :
	      machine (machine_),
	      buffer (buffer_),
B
Behdad Esfahbod 已提交
613
	      num_glyphs (face_->get_num_glyphs ()) {}
614 615 616 617

  template <typename context_t>
  inline void drive (context_t *c)
  {
618 619 620
    if (!c->in_place)
      buffer->clear_output ();

621
    unsigned int state = StateTable<Types, EntryData>::STATE_START_OF_TEXT;
622
    bool last_was_dont_advance = false;
B
Behdad Esfahbod 已提交
623
    for (buffer->idx = 0; buffer->successful;)
624
    {
625
      unsigned int klass = buffer->idx < buffer->len ?
B
Behdad Esfahbod 已提交
626
			   machine.get_class (buffer->info[buffer->idx].codepoint, num_glyphs) :
627
			   (unsigned) StateTable<Types, EntryData>::CLASS_END_OF_TEXT;
628 629 630 631
      const Entry<EntryData> *entry = machine.get_entryZ (state, klass);
      if (unlikely (!entry))
	break;

B
Behdad Esfahbod 已提交
632
      /* Unsafe-to-break before this if not in state 0, as things might
633 634 635 636
       * go differently if we start from state 0 here.
       *
       * Ugh.  The indexing here is ugly... */
      if (state && buffer->backtrack_len () && buffer->idx < buffer->len)
B
Behdad Esfahbod 已提交
637
      {
638 639 640
	/* If there's no action and we're just epsilon-transitioning to state 0,
	 * safe to break. */
	if (c->is_actionable (this, entry) ||
641
	    !(entry->newState == StateTable<Types, EntryData>::STATE_START_OF_TEXT &&
B
Behdad Esfahbod 已提交
642
	      entry->flags == context_t::DontAdvance))
643
	  buffer->unsafe_to_break_from_outbuffer (buffer->backtrack_len () - 1, buffer->idx + 1);
B
Behdad Esfahbod 已提交
644 645 646 647 648 649 650 651 652 653
      }

      /* Unsafe-to-break if end-of-text would kick in here. */
      if (buffer->idx + 2 <= buffer->len)
      {
	const Entry<EntryData> *end_entry = machine.get_entryZ (state, 0);
	if (c->is_actionable (this, end_entry))
	  buffer->unsafe_to_break (buffer->idx, buffer->idx + 2);
      }

654
      if (unlikely (!c->transition (this, entry)))
B
Behdad Esfahbod 已提交
655
	break;
656

B
Behdad Esfahbod 已提交
657
      last_was_dont_advance = (entry->flags & context_t::DontAdvance) && buffer->max_ops-- > 0;
658

B
Behdad Esfahbod 已提交
659
      state = machine.new_state (entry->newState);
660 661

      if (buffer->idx == buffer->len)
B
Behdad Esfahbod 已提交
662
	break;
663

664
      if (!last_was_dont_advance)
B
Behdad Esfahbod 已提交
665
	buffer->next_glyph ();
666 667
    }

668 669
    if (!c->in_place)
    {
670 671
      for (; buffer->successful && buffer->idx < buffer->len;)
	buffer->next_glyph ();
672
      buffer->swap_buffers ();
673
    }
674 675 676
  }

  public:
677
  const StateTable<Types, EntryData> &machine;
678 679 680 681
  hb_buffer_t *buffer;
  unsigned int num_glyphs;
};

682

683
struct ankr;
684 685 686 687 688 689 690 691 692 693

struct hb_aat_apply_context_t :
       hb_dispatch_context_t<hb_aat_apply_context_t, bool, HB_DEBUG_APPLY>
{
  inline const char *get_name (void) { return "APPLY"; }
  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 已提交
694
  hb_ot_shape_plan_t *plan;
695 696 697
  hb_font_t *font;
  hb_face_t *face;
  hb_buffer_t *buffer;
698
  hb_sanitize_context_t sanitizer;
699
  const ankr *ankr_table;
700
  const char *ankr_end;
701

B
Behdad Esfahbod 已提交
702 703 704 705
  /* Unused. For debug tracing only. */
  unsigned int lookup_index;
  unsigned int debug_depth;

706 707 708 709
  HB_INTERNAL hb_aat_apply_context_t (hb_ot_shape_plan_t *plan_,
				      hb_font_t *font_,
				      hb_buffer_t *buffer_,
				      hb_blob_t *blob = const_cast<hb_blob_t *> (&Null(hb_blob_t)));
710

711
  HB_INTERNAL ~hb_aat_apply_context_t (void);
B
Behdad Esfahbod 已提交
712

713 714 715
  HB_INTERNAL void set_ankr_table (const AAT::ankr *ankr_table_, const char *ankr_end_);

  inline void set_lookup_index (unsigned int i) { lookup_index = i; }
716 717 718
};


719 720 721
} /* namespace AAT */


722
#endif /* HB_AAT_LAYOUT_COMMON_HH */