hb-aat-layout-kerx-table.hh 20.0 KB
Newer Older
1 2
/*
 * Copyright © 2018  Ebrahim Byagowi
3
 * Copyright © 2018  Google, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 *  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
 */

#ifndef HB_AAT_LAYOUT_KERX_TABLE_HH
#define HB_AAT_LAYOUT_KERX_TABLE_HH

31 32
#include "hb-open-type.hh"
#include "hb-aat-layout-common.hh"
33
#include "hb-ot-layout-gpos-table.hh"
B
Behdad Esfahbod 已提交
34
#include "hb-ot-kern-table.hh"
35

36 37 38 39
/*
 * kerx -- Extended Kerning
 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kerx.html
 */
40
#define HB_AAT_TAG_kerx HB_TAG('k','e','r','x')
41 42


43
namespace AAT {
44

45
using namespace OT;
46 47


B
Behdad Esfahbod 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
struct KerxSubTableHeader
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (likely (c->check_struct (this)));
  }

  public:
  HBUINT32	length;
  HBUINT32	coverage;
  HBUINT32	tupleCount;
  public:
  DEFINE_SIZE_STATIC (12);
};

B
Behdad Esfahbod 已提交
64
struct KerxSubTableFormat0
65
{
B
Behdad Esfahbod 已提交
66
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
67
  {
B
Behdad Esfahbod 已提交
68 69
    hb_glyph_pair_t pair = {left, right};
    int i = pairs.bsearch (pair);
B
Behdad Esfahbod 已提交
70
    return i == -1 ? 0 : pairs[i].get_kerning ();
71 72
  }

73 74 75 76
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

77 78 79
    if (!c->plan->requested_kerning)
      return false;

B
Behdad Esfahbod 已提交
80 81 82
    hb_kern_machine_t<KerxSubTableFormat0> machine (*this);

    machine.kern (c->font, c->buffer, c->plan->kern_mask);
83 84 85

    return_trace (true);
  }
86 87 88 89

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
90
    return_trace (likely (pairs.sanitize (c)));
91 92 93
  }

  protected:
B
Behdad Esfahbod 已提交
94
  KerxSubTableHeader	header;
B
Behdad Esfahbod 已提交
95
  BinSearchArrayOf<KernPair, HBUINT32>
B
Behdad Esfahbod 已提交
96
			pairs;	/* Sorted kern records. */
97
  public:
B
Behdad Esfahbod 已提交
98
  DEFINE_SIZE_ARRAY (28, pairs);
99 100 101 102
};

struct KerxSubTableFormat1
{
103 104
  struct EntryData
  {
B
Behdad Esfahbod 已提交
105 106 107
    HBUINT16	kernActionIndex;/* Index into the kerning value array. If
				 * this index is 0xFFFF, then no kerning
				 * is to be performed. */
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    public:
    DEFINE_SIZE_STATIC (2);
  };

  struct driver_context_t
  {
    static const bool in_place = true;
    enum Flags
    {
      Push		= 0x8000,	/* If set, push this glyph on the kerning stack. */
      DontAdvance	= 0x4000,	/* If set, don't advance to the next glyph
					 * before going to the new state. */
      Reset		= 0x2000,	/* If set, reset the kerning data (clear the stack) */
      Reserved		= 0x1FFF,	/* Not used; set to 0. */
    };

B
Behdad Esfahbod 已提交
124 125 126
    inline driver_context_t (const KerxSubTableFormat1 *table,
			     hb_aat_apply_context_t *c_) :
	c (c_),
B
Behdad Esfahbod 已提交
127 128 129
	/* Apparently the offset kernAction is from the beginning of the state-machine,
	 * similar to offsets in morx table, NOT from beginning of this table, like
	 * other subtables in kerx.  Discovered via testing. */
B
Behdad Esfahbod 已提交
130
	kernAction (&table->machine + table->kernAction),
B
Behdad Esfahbod 已提交
131
	depth (0) {}
132 133 134 135

    inline bool is_actionable (StateTableDriver<EntryData> *driver,
			       const Entry<EntryData> *entry)
    {
B
Behdad Esfahbod 已提交
136
      return entry->data.kernActionIndex != 0xFFFF;
137 138 139 140
    }
    inline bool transition (StateTableDriver<EntryData> *driver,
			    const Entry<EntryData> *entry)
    {
B
Behdad Esfahbod 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      hb_buffer_t *buffer = driver->buffer;
      unsigned int flags = entry->flags;

      if (flags & Reset)
      {
        depth = 0;
      }

      if (flags & Push)
      {
        if (likely (depth < ARRAY_LENGTH (stack)))
	  stack[depth++] = buffer->idx;
	else
	  depth = 0; /* Probably not what CoreText does, but better? */
      }

      if (entry->data.kernActionIndex != 0xFFFF)
      {
	const FWORD *actions = &kernAction[entry->data.kernActionIndex];
        if (!c->sanitizer.check_array (actions, depth))
	{
	  depth = 0;
	  return false;
	}

166
	hb_mask_t kern_mask = c->plan->kern_mask;
B
Behdad Esfahbod 已提交
167
        for (unsigned int i = 0; i < depth; i++)
B
Behdad Esfahbod 已提交
168
	{
B
Behdad Esfahbod 已提交
169 170 171 172 173
	  /* Apparently, when spec says "Each pops one glyph from the kerning stack
	   * and applies the kerning value to it.", it doesn't mean it in that order.
	   * The deepest item in the stack corresponds to the first item in the action
	   * list.  Discovered by testing. */
	  unsigned int idx = stack[i];
B
Behdad Esfahbod 已提交
174
	  int v = *actions++;
175 176 177 178 179 180 181 182
	  if (buffer->info[idx].mask & kern_mask)
	  {
	    /* XXX Non-forward direction... */
	    if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
	      buffer->pos[idx].x_advance += c->font->em_scale_x (v);
	    else
	      buffer->pos[idx].y_advance += c->font->em_scale_y (v);
	  }
B
Behdad Esfahbod 已提交
183
	}
B
Behdad Esfahbod 已提交
184
	depth = 0;
B
Behdad Esfahbod 已提交
185
      }
186 187 188 189 190

      return true;
    }

    private:
B
Behdad Esfahbod 已提交
191 192 193 194
    hb_aat_apply_context_t *c;
    const UnsizedArrayOf<FWORD> &kernAction;
    unsigned int stack[8];
    unsigned int depth;
195 196
  };

197 198 199 200
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

201 202 203
    if (!c->plan->requested_kerning)
      return false;

B
Behdad Esfahbod 已提交
204
    driver_context_t dc (this, c);
205 206 207

    StateTableDriver<EntryData> driver (machine, c->buffer, c->font->face);
    driver.drive (&dc);
208 209 210 211

    return_trace (true);
  }

212 213
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
214
    TRACE_SANITIZE (this);
215
    return_trace (likely (machine.sanitize (c)));
216 217 218
  }

  protected:
219 220
  KerxSubTableHeader				header;
  StateTable<EntryData>				machine;
B
Behdad Esfahbod 已提交
221
  LOffsetTo<UnsizedArrayOf<FWORD>, false>	kernAction;
222
  public:
B
Behdad Esfahbod 已提交
223
  DEFINE_SIZE_STATIC (32);
224 225 226 227
};

struct KerxSubTableFormat2
{
B
Behdad Esfahbod 已提交
228
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
229
			  hb_aat_apply_context_t *c) const
230
  {
231
    unsigned int num_glyphs = c->sanitizer.get_num_glyphs ();
232 233
    unsigned int l = (this+leftClassTable).get_value_or_null (left, num_glyphs);
    unsigned int r = (this+rightClassTable).get_value_or_null (right, num_glyphs);
234
    unsigned int offset = l + r;
235 236 237
    const FWORD v = StructAtOffset<FWORD> (&(this+array), offset);
    if (unlikely (!v.sanitize (&c->sanitizer))) return 0;
    return v;
238 239
  }

240 241 242 243
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

244 245 246
    if (!c->plan->requested_kerning)
      return false;

247
    accelerator_t accel (*this, c);
B
Behdad Esfahbod 已提交
248 249
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
250 251 252 253

    return_trace (true);
  }

254 255 256
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
257
    return_trace (likely (rowWidth.sanitize (c) &&
E
Ebrahim Byagowi 已提交
258
			  leftClassTable.sanitize (c, this) &&
B
Behdad Esfahbod 已提交
259
			  rightClassTable.sanitize (c, this)));
260 261
  }

B
Behdad Esfahbod 已提交
262 263 264
  struct accelerator_t
  {
    const KerxSubTableFormat2 &table;
265
    hb_aat_apply_context_t *c;
B
Behdad Esfahbod 已提交
266 267

    inline accelerator_t (const KerxSubTableFormat2 &table_,
268 269
			  hb_aat_apply_context_t *c_) :
			    table (table_), c (c_) {}
B
Behdad Esfahbod 已提交
270 271

    inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
272
    { return table.get_kerning (left, right, c); }
B
Behdad Esfahbod 已提交
273 274
  };

275
  protected:
B
Behdad Esfahbod 已提交
276 277
  KerxSubTableHeader	header;
  HBUINT32		rowWidth;	/* The width, in bytes, of a row in the table. */
B
Behdad Esfahbod 已提交
278
  LOffsetTo<Lookup<HBUINT16> >
B
Behdad Esfahbod 已提交
279 280
			leftClassTable;	/* Offset from beginning of this subtable to
					 * left-hand class table. */
B
Behdad Esfahbod 已提交
281
  LOffsetTo<Lookup<HBUINT16> >
B
Behdad Esfahbod 已提交
282 283
			rightClassTable;/* Offset from beginning of this subtable to
					 * right-hand class table. */
B
Behdad Esfahbod 已提交
284 285
  LOffsetTo<UnsizedArrayOf<FWORD>, false>
			 array;		/* Offset from beginning of this subtable to
B
Behdad Esfahbod 已提交
286
					 * the start of the kerning array. */
287
  public:
B
Behdad Esfahbod 已提交
288
  DEFINE_SIZE_STATIC (28);
289 290 291 292
};

struct KerxSubTableFormat4
{
B
Behdad Esfahbod 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  struct EntryData
  {
    HBUINT16	ankrActionIndex;/* Either 0xFFFF (for no action) or the index of
				 * the action to perform. */
    public:
    DEFINE_SIZE_STATIC (2);
  };

  struct driver_context_t
  {
    static const bool in_place = true;
    enum Flags
    {
      Mark		= 0x8000,	/* If set, remember this glyph as the marked glyph. */
      DontAdvance	= 0x4000,	/* If set, don't advance to the next glyph before
					 * going to the new state. */
      Reserved		= 0x3FFF,	/* Not used; set to 0. */
    };

    enum SubTableFlags
    {
      ActionType	= 0xC0000000,	/* A two-bit field containing the action type. */
      Unused		= 0x3F000000,	/* Unused - must be zero. */
      Offset		= 0x00FFFFFF,	/* Masks the offset in bytes from the beginning
					 * of the subtable to the beginning of the control
					 * point table. */
    };

    inline driver_context_t (const KerxSubTableFormat4 *table,
			     hb_aat_apply_context_t *c_) :
	c (c_),
	action_type ((table->flags & ActionType) >> 30),
	ankrData ((HBUINT16 *) ((const char *) &table->machine + (table->flags & Offset))),
	mark_set (false),
	mark (0) {}

    inline bool is_actionable (StateTableDriver<EntryData> *driver,
			       const Entry<EntryData> *entry)
    {
      return entry->data.ankrActionIndex != 0xFFFF;
    }
    inline bool transition (StateTableDriver<EntryData> *driver,
			    const Entry<EntryData> *entry)
    {
      hb_buffer_t *buffer = driver->buffer;
      unsigned int flags = entry->flags;

      if (mark_set && entry->data.ankrActionIndex != 0xFFFF)
      {
342
	hb_glyph_position_t &o = buffer->cur_pos();
B
Behdad Esfahbod 已提交
343 344 345 346 347 348 349 350 351 352
	switch (action_type)
	{
	  case 0: /* Control Point Actions.*/
	  {
	    /* indexed into glyph outline. */
	    const HBUINT16 *data = &ankrData[entry->data.ankrActionIndex];
	    if (!c->sanitizer.check_array (data, 2))
	      return false;
	    HB_UNUSED unsigned int markControlPoint = *data++;
	    HB_UNUSED unsigned int currControlPoint = *data++;
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	    hb_position_t markX = 0;
	    hb_position_t markY = 0;
	    hb_position_t currX = 0;
	    hb_position_t currY = 0;
	    if (!c->font->get_glyph_contour_point_for_origin (c->buffer->info[mark].codepoint,
							      markControlPoint,
							      HB_DIRECTION_LTR /*XXX*/,
							      &markX, &markY) ||
		!c->font->get_glyph_contour_point_for_origin (c->buffer->cur ().codepoint,
							      currControlPoint,
							      HB_DIRECTION_LTR /*XXX*/,
							      &currX, &currY))
	      return true; /* True, such that the machine continues. */

	    o.x_offset = markX - currX;
	    o.y_offset = markY - currY;
B
Behdad Esfahbod 已提交
369 370 371 372 373 374 375 376 377
	  }
	  break;

	  case 1: /* Anchor Point Actions. */
	  {
	   /* Indexed into 'ankr' table. */
	    const HBUINT16 *data = &ankrData[entry->data.ankrActionIndex];
	    if (!c->sanitizer.check_array (data, 2))
	      return false;
378 379 380 381
	    unsigned int markAnchorPoint = *data++;
	    unsigned int currAnchorPoint = *data++;
	    const Anchor markAnchor = c->ankr_table.get_anchor (c->buffer->info[mark].codepoint,
								markAnchorPoint,
382
								c->sanitizer.get_num_glyphs (),
383 384 385
								c->ankr_end);
	    const Anchor currAnchor = c->ankr_table.get_anchor (c->buffer->cur ().codepoint,
								currAnchorPoint,
386
								c->sanitizer.get_num_glyphs (),
387
								c->ankr_end);
388

389 390
	    o.x_offset = c->font->em_scale_x (markAnchor.xCoordinate) - c->font->em_scale_x (currAnchor.xCoordinate);
	    o.y_offset = c->font->em_scale_y (markAnchor.yCoordinate) - c->font->em_scale_y (currAnchor.yCoordinate);
B
Behdad Esfahbod 已提交
391 392 393 394 395 396 397 398
	  }
	  break;

	  case 2: /* Control Point Coordinate Actions. */
	  {
	    const FWORD *data = (const FWORD *) &ankrData[entry->data.ankrActionIndex];
	    if (!c->sanitizer.check_array (data, 4))
	      return false;
399 400 401 402 403 404 405
	    int markX = *data++;
	    int markY = *data++;
	    int currX = *data++;
	    int currY = *data++;

	    o.x_offset = c->font->em_scale_x (markX) - c->font->em_scale_x (currX);
	    o.y_offset = c->font->em_scale_y (markY) - c->font->em_scale_y (currY);
B
Behdad Esfahbod 已提交
406 407 408
	  }
	  break;
	}
409 410 411
	o.attach_type() = ATTACH_TYPE_MARK;
	o.attach_chain() = (int) mark - (int) buffer->idx;
	buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
B
Behdad Esfahbod 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
      }

      if (flags & Mark)
      {
	mark_set = true;
	mark = buffer->idx;
      }

      return true;
    }

    private:
    hb_aat_apply_context_t *c;
    unsigned int action_type;
    const HBUINT16 *ankrData;
    bool mark_set;
    unsigned int mark;
  };

431 432 433 434
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

B
Behdad Esfahbod 已提交
435 436 437 438
    driver_context_t dc (this, c);

    StateTableDriver<EntryData> driver (machine, c->buffer, c->font->face);
    driver.drive (&dc);
439 440 441 442

    return_trace (true);
  }

443 444 445
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
446

B
Behdad Esfahbod 已提交
447 448 449 450
    /* The rest of array sanitizations are done at run-time. */
    return_trace (c->check_struct (this) &&
		  machine.sanitize (c) &&
		  flags.sanitize (c));
451 452 453
  }

  protected:
B
Behdad Esfahbod 已提交
454
  KerxSubTableHeader	header;
B
Behdad Esfahbod 已提交
455 456
  StateTable<EntryData>	machine;
  HBUINT32		flags;
457
  public:
B
Behdad Esfahbod 已提交
458
  DEFINE_SIZE_STATIC (32);
459 460 461 462
};

struct KerxSubTableFormat6
{
463 464 465 466 467 468 469
  enum Flags
  {
    ValuesAreLong	= 0x00000001,
  };

  inline bool is_long (void) const { return flags & ValuesAreLong; }

B
Behdad Esfahbod 已提交
470
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
471
			  hb_aat_apply_context_t *c) const
B
Behdad Esfahbod 已提交
472
  {
473
    unsigned int num_glyphs = c->sanitizer.get_num_glyphs ();
B
Behdad Esfahbod 已提交
474 475 476 477 478 479
    if (is_long ())
    {
      const U::Long &t = u.l;
      unsigned int l = (this+t.rowIndexTable).get_value_or_null (left, num_glyphs);
      unsigned int r = (this+t.columnIndexTable).get_value_or_null (right, num_glyphs);
      unsigned int offset = l + r;
B
Behdad Esfahbod 已提交
480 481
      if (unlikely (offset < l)) return 0; /* Addition overflow. */
      if (unlikely (hb_unsigned_mul_overflows (offset, sizeof (FWORD32)))) return 0;
482 483 484
      const FWORD32 &v = StructAtOffset<FWORD32> (&(this+t.array), offset * sizeof (FWORD32));
      if (unlikely (!v.sanitize (&c->sanitizer))) return 0;
      return v;
B
Behdad Esfahbod 已提交
485 486 487 488 489 490 491
    }
    else
    {
      const U::Short &t = u.s;
      unsigned int l = (this+t.rowIndexTable).get_value_or_null (left, num_glyphs);
      unsigned int r = (this+t.columnIndexTable).get_value_or_null (right, num_glyphs);
      unsigned int offset = l + r;
492 493 494
      const FWORD &v = StructAtOffset<FWORD> (&(this+t.array), offset * sizeof (FWORD));
      if (unlikely (!v.sanitize (&c->sanitizer))) return 0;
      return v;
B
Behdad Esfahbod 已提交
495 496 497
    }
  }

498 499 500 501
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

502 503 504
    if (!c->plan->requested_kerning)
      return false;

505
    accelerator_t accel (*this, c);
B
Behdad Esfahbod 已提交
506 507
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
508 509 510 511

    return_trace (true);
  }

512 513
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
514
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
515
    return_trace (likely (c->check_struct (this) &&
516 517 518
			  is_long () ?
			  (
			    u.l.rowIndexTable.sanitize (c, this) &&
B
Behdad Esfahbod 已提交
519
			    u.l.columnIndexTable.sanitize (c, this)
520 521
			  ) : (
			    u.s.rowIndexTable.sanitize (c, this) &&
B
Behdad Esfahbod 已提交
522
			    u.s.columnIndexTable.sanitize (c, this)
523
			  )));
524 525
  }

B
Behdad Esfahbod 已提交
526 527 528
  struct accelerator_t
  {
    const KerxSubTableFormat6 &table;
529
    hb_aat_apply_context_t *c;
B
Behdad Esfahbod 已提交
530 531

    inline accelerator_t (const KerxSubTableFormat6 &table_,
532 533
			  hb_aat_apply_context_t *c_) :
			    table (table_), c (c_) {}
B
Behdad Esfahbod 已提交
534 535

    inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
536
    { return table.get_kerning (left, right, c); }
B
Behdad Esfahbod 已提交
537 538
  };

539
  protected:
B
Behdad Esfahbod 已提交
540 541 542 543
  KerxSubTableHeader		header;
  HBUINT32			flags;
  HBUINT16			rowCount;
  HBUINT16			columnCount;
B
Behdad Esfahbod 已提交
544
  union U
545
  {
B
Behdad Esfahbod 已提交
546
    struct Long
547 548 549
    {
      LOffsetTo<Lookup<HBUINT32> >	rowIndexTable;
      LOffsetTo<Lookup<HBUINT32> >	columnIndexTable;
B
Behdad Esfahbod 已提交
550 551
      LOffsetTo<UnsizedArrayOf<FWORD32>, false>
					array;
552
    } l;
B
Behdad Esfahbod 已提交
553 554 555 556
    struct Short
    {
      LOffsetTo<Lookup<HBUINT16> >	rowIndexTable;
      LOffsetTo<Lookup<HBUINT16> >	columnIndexTable;
B
Behdad Esfahbod 已提交
557 558
      LOffsetTo<UnsizedArrayOf<FWORD>, false>
					array;
B
Behdad Esfahbod 已提交
559
    } s;
560
  } u;
561
  public:
562
  DEFINE_SIZE_STATIC (32);
563 564
};

565 566
struct KerxTable
{
B
Behdad Esfahbod 已提交
567
  friend struct kerx;
B
Behdad Esfahbod 已提交
568

B
Behdad Esfahbod 已提交
569 570
  inline unsigned int get_size (void) const { return u.header.length; }
  inline unsigned int get_type (void) const { return u.header.coverage & SubtableType; }
571 572

  enum Coverage
573
  {
574 575 576
    Vertical		= 0x80000000,	/* Set if table has vertical kerning values. */
    CrossStream		= 0x40000000,	/* Set if table has cross-stream kerning values. */
    Variation		= 0x20000000,	/* Set if table has variation kerning values. */
B
Behdad Esfahbod 已提交
577
    Backwards		= 0x10000000,	/* If clear, process the glyphs forwards, that
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
					 * is, from first to last in the glyph stream.
					 * If we, process them from last to first.
					 * This flag only applies to state-table based
					 * 'kerx' subtables (types 1 and 4). */
    Reserved		= 0x0FFFFF00,	/* Reserved, set to zero. */
    SubtableType	= 0x000000FF,	/* Subtable type. */
  };

  template <typename context_t>
  inline typename context_t::return_t dispatch (context_t *c) const
  {
    unsigned int subtable_type = get_type ();
    TRACE_DISPATCH (this, subtable_type);
    switch (subtable_type) {
    case 0	:		return_trace (c->dispatch (u.format0));
    case 1	:		return_trace (c->dispatch (u.format1));
    case 2	:		return_trace (c->dispatch (u.format2));
    case 4	:		return_trace (c->dispatch (u.format4));
    case 6	:		return_trace (c->dispatch (u.format6));
    default:			return_trace (c->default_return_value ());
    }
599 600
  }

601
  inline bool sanitize (hb_sanitize_context_t *c) const
602 603
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
604 605
    if (!u.header.sanitize (c) ||
	!c->check_range (this, u.header.length))
606 607
      return_trace (false);

608
    return_trace (dispatch (c));
609 610
  }

611
protected:
612
  union {
B
Behdad Esfahbod 已提交
613
  KerxSubTableHeader	header;
614
  KerxSubTableFormat0	format0;
615
  KerxSubTableFormat1	format1;
616 617 618 619
  KerxSubTableFormat2	format2;
  KerxSubTableFormat4	format4;
  KerxSubTableFormat6	format6;
  } u;
620 621
public:
  DEFINE_SIZE_MIN (12);
622 623
};

624 625 626 627 628

/*
 * The 'kerx' Table
 */

629 630
struct kerx
{
631
  static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
632

633 634 635
  inline bool has_data (void) const { return version != 0; }

  inline void apply (hb_aat_apply_context_t *c) const
636
  {
637 638 639 640 641
    c->set_lookup_index (0);
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
    {
B
Behdad Esfahbod 已提交
642 643
      bool reverse;

644 645
      if (table->u.header.coverage & (KerxTable::CrossStream | KerxTable::Variation) ||
	  table->u.header.tupleCount)
B
Behdad Esfahbod 已提交
646 647
	goto skip; /* We do NOT handle cross-stream or variation kerning. */

B
Behdad Esfahbod 已提交
648
      if (HB_DIRECTION_IS_VERTICAL (c->buffer->props.direction) !=
B
Behdad Esfahbod 已提交
649
	  bool (table->u.header.coverage & KerxTable::Vertical))
B
Behdad Esfahbod 已提交
650
	goto skip;
B
Behdad Esfahbod 已提交
651

B
Behdad Esfahbod 已提交
652
      reverse = bool (table->u.header.coverage & KerxTable::Backwards) !=
B
Behdad Esfahbod 已提交
653 654 655
		HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);

      if (!c->buffer->message (c->font, "start kerx subtable %d", c->lookup_index))
B
Behdad Esfahbod 已提交
656
	goto skip;
B
Behdad Esfahbod 已提交
657 658

      if (reverse)
B
Behdad Esfahbod 已提交
659
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
660

661 662
      c->sanitizer.set_object (*table);

B
Behdad Esfahbod 已提交
663 664 665 666
      /* XXX Reverse-kern is not working yet...
       * hb_kern_machine_t would need to know that it's reverse-kerning.
       * Or better yet, make it work in reverse as well, so we don't have
       * to reverse and reverse back? */
667
      table->dispatch (c);
B
Behdad Esfahbod 已提交
668 669

      if (reverse)
B
Behdad Esfahbod 已提交
670
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
671 672 673 674

      (void) c->buffer->message (c->font, "end kerx subtable %d", c->lookup_index);

    skip:
675 676
      table = &StructAfter<KerxTable> (*table);
    }
677 678
  }

679
  inline bool sanitize (hb_sanitize_context_t *c) const
680
  {
681
    TRACE_SANITIZE (this);
682 683
    if (!version.sanitize (c) || version < 2 ||
	!tableCount.sanitize (c))
684
      return_trace (false);
685

686 687 688
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
689
    {
690 691
      if (!table->sanitize (c))
	return_trace (false);
692
      table = &StructAfter<KerxTable> (*table);
693 694 695 696 697 698
    }

    return_trace (true);
  }

  protected:
699 700 701 702 703 704 705 706
  HBUINT16	version;	/* The version number of the extended kerning table
				 * (currently 2, 3, or 4). */
  HBUINT16	unused;		/* Set to 0. */
  HBUINT32	tableCount;	/* The number of subtables included in the extended kerning
				 * table. */
  KerxTable	firstTable;	/* Subtables. */
/*subtableGlyphCoverageArray*/	/* Only if version >= 3. We don't use. */

707
  public:
708
  DEFINE_SIZE_MIN (8);
709 710 711 712 713 714
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */