hb-aat-layout-kerx-table.hh 25.9 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
#include "hb-kern.hh"
B
Behdad Esfahbod 已提交
32
#include "hb-aat-layout-ankr-table.hh"
33

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


41
namespace AAT {
42

43
using namespace OT;
44 45


46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static inline int
kerxTupleKern (int value,
	       unsigned int tupleCount,
	       const void *base,
	       hb_aat_apply_context_t *c)
{
  if (likely (!tupleCount)) return value;

  unsigned int offset = value;
  const FWORD *pv = &StructAtOffset<FWORD> (base, offset);
  if (unlikely (!pv->sanitize (&c->sanitizer))) return 0;
  return *pv;
}


B
Behdad Esfahbod 已提交
61
struct hb_glyph_pair_t
B
Behdad Esfahbod 已提交
62
{
B
Behdad Esfahbod 已提交
63 64 65
  hb_codepoint_t left;
  hb_codepoint_t right;
};
B
Behdad Esfahbod 已提交
66

B
Behdad Esfahbod 已提交
67 68 69 70 71 72
struct KernPair
{
  inline int get_kerning (void) const
  { return value; }

  inline int cmp (const hb_glyph_pair_t &o) const
73
  {
B
Behdad Esfahbod 已提交
74 75 76 77
    int ret = left.cmp (o.left);
    if (ret) return ret;
    return right.cmp (o.right);
  }
78

B
Behdad Esfahbod 已提交
79 80 81
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
82
    return_trace (c->check_struct (this));
B
Behdad Esfahbod 已提交
83 84
  }

B
Behdad Esfahbod 已提交
85 86 87 88
  protected:
  GlyphID	left;
  GlyphID	right;
  FWORD		value;
B
Behdad Esfahbod 已提交
89
  public:
B
Behdad Esfahbod 已提交
90
  DEFINE_SIZE_STATIC (6);
B
Behdad Esfahbod 已提交
91 92
};

93
template <typename KernSubTableHeader>
B
Behdad Esfahbod 已提交
94
struct KerxSubTableFormat0
95
{
B
Behdad Esfahbod 已提交
96 97 98 99 100 101 102 103
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
  {
    hb_glyph_pair_t pair = {left, right};
    int i = pairs.bsearch (pair);
    if (i == -1) return 0;
    return pairs[i].get_kerning ();
  }

104 105
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
			  hb_aat_apply_context_t *c) const
106
  {
B
Behdad Esfahbod 已提交
107 108
    hb_glyph_pair_t pair = {left, right};
    int i = pairs.bsearch (pair);
109 110
    if (i == -1) return 0;
    int v = pairs[i].get_kerning ();
B
Behdad Esfahbod 已提交
111
    return kerxTupleKern (v, header.tuple_count (), this, c);
112 113
  }

114 115 116 117
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

118 119 120
    if (!c->plan->requested_kerning)
      return false;

121 122 123
    if (header.coverage & header.CrossStream)
      return false;

124 125
    accelerator_t accel (*this, c);
    hb_kern_machine_t<accelerator_t> machine (accel);
B
Behdad Esfahbod 已提交
126
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
127 128 129

    return_trace (true);
  }
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144
  struct accelerator_t
  {
    const KerxSubTableFormat0 &table;
    hb_aat_apply_context_t *c;

    inline accelerator_t (const KerxSubTableFormat0 &table_,
			  hb_aat_apply_context_t *c_) :
			    table (table_), c (c_) {}

    inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
    { return table.get_kerning (left, right, c); }
  };


145 146 147
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
148
    return_trace (likely (pairs.sanitize (c)));
149 150 151
  }

  protected:
152
  KernSubTableHeader	header;
B
Behdad Esfahbod 已提交
153
  BinSearchArrayOf<KernPair, typename KernSubTableHeader::Types::HBUINT>
B
Behdad Esfahbod 已提交
154
			pairs;	/* Sorted kern records. */
155
  public:
156
  DEFINE_SIZE_ARRAY (KernSubTableHeader::static_size + 16, pairs);
157 158
};

B
Behdad Esfahbod 已提交
159 160 161 162 163 164

template <bool extended>
struct Format1Entry;

template <>
struct Format1Entry<true>
165
{
B
Behdad Esfahbod 已提交
166 167 168 169 170 171 172 173 174
  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. */
  };

175 176
  struct EntryData
  {
B
Behdad Esfahbod 已提交
177 178 179
    HBUINT16	kernActionIndex;/* Index into the kerning value array. If
				 * this index is 0xFFFF, then no kerning
				 * is to be performed. */
180 181 182
    public:
    DEFINE_SIZE_STATIC (2);
  };
183 184 185 186 187 188

  static inline bool performAction (const Entry<EntryData> *entry)
  { return entry->data.kernActionIndex != 0xFFFF; }

  static inline unsigned int kernActionIndex (const Entry<EntryData> *entry)
  { return entry->data.kernActionIndex; }
B
Behdad Esfahbod 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
};
template <>
struct Format1Entry<false>
{
  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. */
    Offset		= 0x3FFF,	/* Byte offset from beginning of subtable to the
					 * value table for the glyphs on the kerning stack. */

    Reset		= 0x0000,	/* Not supported? */
  };

  typedef void EntryData;
205 206 207 208 209 210

  static inline bool performAction (const Entry<EntryData> *entry)
  { return entry->flags & Offset; }

  static inline unsigned int kernActionIndex (const Entry<EntryData> *entry)
  { return entry->flags & Offset; }
B
Behdad Esfahbod 已提交
211 212 213 214 215 216 217 218 219 220
};

template <typename KernSubTableHeader>
struct KerxSubTableFormat1
{
  typedef typename KernSubTableHeader::Types Types;
  typedef typename Types::HBUINT HBUINT;

  typedef Format1Entry<Types::extended> Format1EntryT;
  typedef typename Format1EntryT::EntryData EntryData;
221 222 223 224

  struct driver_context_t
  {
    static const bool in_place = true;
B
Behdad Esfahbod 已提交
225
    enum
226
    {
B
Behdad Esfahbod 已提交
227
      DontAdvance	= Format1EntryT::DontAdvance,
228 229
    };

230
    inline driver_context_t (const KerxSubTableFormat1 *table_,
B
Behdad Esfahbod 已提交
231 232
			     hb_aat_apply_context_t *c_) :
	c (c_),
233
	table (table_),
B
Behdad Esfahbod 已提交
234 235 236
	/* 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 已提交
237
	kernAction (&table->machine + table->kernAction),
238
	depth (0),
239 240
	crossStream (table->header.coverage & table->header.CrossStream),
	crossOffset (0) {}
241

242 243 244 245 246 247 248 249 250 251 252

    /* TODO
     * 'kern' table has this pecularity, we don't currently implement.
     *
     * "Because the stateTableOffset in the state table header is (strictly
     * speaking) redundant, some 'kern' tables use it to record an initial
     * state where that should not be StartOfText. To determine if this is
     * done, calculate what the stateTableOffset should be. If it's different
     * from the actual stateTableOffset, use it as the initial state."
     */

253
    inline bool is_actionable (StateTableDriver<Types, EntryData> *driver HB_UNUSED,
254 255
			       const Entry<EntryData> *entry)
    {
256
      return Format1EntryT::performAction (entry);
257
    }
258
    inline bool transition (StateTableDriver<Types, EntryData> *driver,
259 260
			    const Entry<EntryData> *entry)
    {
B
Behdad Esfahbod 已提交
261 262 263
      hb_buffer_t *buffer = driver->buffer;
      unsigned int flags = entry->flags;

B
Behdad Esfahbod 已提交
264
      if (flags & Format1EntryT::Reset)
B
Behdad Esfahbod 已提交
265
	depth = 0;
B
Behdad Esfahbod 已提交
266

B
Behdad Esfahbod 已提交
267
      if (flags & Format1EntryT::Push)
B
Behdad Esfahbod 已提交
268
      {
B
Behdad Esfahbod 已提交
269
	if (likely (depth < ARRAY_LENGTH (stack)))
B
Behdad Esfahbod 已提交
270 271 272 273 274
	  stack[depth++] = buffer->idx;
	else
	  depth = 0; /* Probably not what CoreText does, but better? */
      }

275
      if (Format1EntryT::performAction (entry))
B
Behdad Esfahbod 已提交
276
      {
277 278 279
	unsigned int kern_idx = Format1EntryT::kernActionIndex (entry);
	kern_idx = Types::offsetToIndex (kern_idx, &table->machine, kernAction.arrayZ);
	const FWORD *actions = &kernAction[kern_idx];
B
Behdad Esfahbod 已提交
280
	if (!c->sanitizer.check_array (actions, depth))
B
Behdad Esfahbod 已提交
281 282 283 284 285
	{
	  depth = 0;
	  return false;
	}

286
	hb_mask_t kern_mask = c->plan->kern_mask;
287 288 289 290 291 292 293 294 295 296 297 298

	/* From Apple 'kern' spec:
	 * "Each pops one glyph from the kerning stack and applies the kerning value to it.
	 * The end of the list is marked by an odd value... */
	unsigned int i;
	for (i = 0; i < depth; i++)
	  if (actions[i] & 1)
	  {
	    i++;
	    break;
	  }
	for (; i; i--)
B
Behdad Esfahbod 已提交
299
	{
300 301 302 303 304 305 306 307 308
	  unsigned int idx = stack[depth - i];
	  int v = actions[i - 1];

	  /* "The end of the list is marked by an odd value..."
	   * Ignore it. */
	  v &= ~1;

	  /* The following flag is undocumented in the spec, but described
	   * in the 'kern' table example. */
309
	  if (v == -0x8000)
310 311 312 313
	  {
	    crossOffset = 0;
	    v = 0;
	  }
B
Behdad Esfahbod 已提交
314

315
	  if (idx < buffer->len && buffer->info[idx].mask & kern_mask)
316 317
	  {
	    if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
B
Behdad Esfahbod 已提交
318
	    {
319
	      if (crossStream)
320 321 322 323 324
	      {
		crossOffset += v;
		if (!buffer->pos[idx].y_offset)
		  buffer->pos[idx].y_offset += c->font->em_scale_y (crossOffset);
	      }
325 326
	      else
	      {
327 328 329
		if (!buffer->pos[idx].x_offset)
		{
		  buffer->pos[idx].x_advance += c->font->em_scale_x (v);
330
		  buffer->pos[idx].x_offset += c->font->em_scale_x (v);
331
		}
332
	      }
B
Behdad Esfahbod 已提交
333
	    }
334
	    else
B
Behdad Esfahbod 已提交
335
	    {
336
	      if (crossStream)
337
	      {
338 339 340 341
	        /* CoreText doesn't do crossStream kerning in vertical.  We do. */
		crossOffset += v;
		if (!buffer->pos[idx].x_offset)
		  buffer->pos[idx].x_offset = c->font->em_scale_x (crossOffset);
342
	      }
343 344
	      else
	      {
345 346 347
		if (!buffer->pos[idx].y_offset)
		{
		  buffer->pos[idx].y_advance += c->font->em_scale_y (v);
348
		  buffer->pos[idx].y_offset += c->font->em_scale_y (v);
349
		}
350
	      }
B
Behdad Esfahbod 已提交
351
	    }
352
	  }
B
Behdad Esfahbod 已提交
353
	}
B
Behdad Esfahbod 已提交
354
	depth = 0;
B
Behdad Esfahbod 已提交
355
      }
B
Behdad Esfahbod 已提交
356 357
      else
	buffer->pos[buffer->idx].y_offset += c->font->em_scale_y (crossOffset);
358 359 360 361 362

      return true;
    }

    private:
B
Behdad Esfahbod 已提交
363
    hb_aat_apply_context_t *c;
364
    const KerxSubTableFormat1 *table;
B
Behdad Esfahbod 已提交
365 366 367
    const UnsizedArrayOf<FWORD> &kernAction;
    unsigned int stack[8];
    unsigned int depth;
368
    bool crossStream;
369
    int crossOffset;
370 371
  };

372 373 374 375
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

376 377 378
    if (!c->plan->requested_kerning)
      return false;

B
Behdad Esfahbod 已提交
379
    if (header.tuple_count ())
380 381
      return_trace (false); /* TODO kerxTupleKern */

B
Behdad Esfahbod 已提交
382
    driver_context_t dc (this, c);
383

384
    StateTableDriver<Types, EntryData> driver (machine, c->buffer, c->font->face);
385
    driver.drive (&dc);
386 387 388 389

    return_trace (true);
  }

390 391
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
392
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
393 394 395
    /* The rest of array sanitizations are done at run-time. */
    return_trace (likely (c->check_struct (this) &&
			  machine.sanitize (c)));
396 397 398
  }

  protected:
399
  KernSubTableHeader				header;
B
Behdad Esfahbod 已提交
400 401
  StateTable<Types, EntryData>			machine;
  OffsetTo<UnsizedArrayOf<FWORD>, HBUINT, false>kernAction;
402
  public:
B
Behdad Esfahbod 已提交
403
  DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 5 * sizeof (HBUINT));
404 405
};

406
template <typename KernSubTableHeader>
407 408
struct KerxSubTableFormat2
{
B
Behdad Esfahbod 已提交
409
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
410
			  hb_aat_apply_context_t *c) const
411
  {
412
    unsigned int num_glyphs = c->sanitizer.get_num_glyphs ();
413 414
    unsigned int l = (this+leftClassTable).get_value_or_null (left, num_glyphs);
    unsigned int r = (this+rightClassTable).get_value_or_null (right, num_glyphs);
415
    unsigned int offset = l + r;
B
Behdad Esfahbod 已提交
416 417
    const FWORD *v = &StructAtOffset<FWORD> (&(this+array), offset);
    if (unlikely (!v->sanitize (&c->sanitizer))) return 0;
B
Behdad Esfahbod 已提交
418
    return kerxTupleKern (*v, header.tuple_count (), this, c);
419 420
  }

421 422 423 424
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

425 426 427
    if (!c->plan->requested_kerning)
      return false;

428 429 430
    if (header.coverage & header.CrossStream)
      return false;

431
    accelerator_t accel (*this, c);
B
Behdad Esfahbod 已提交
432 433
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
434 435 436 437

    return_trace (true);
  }

B
Behdad Esfahbod 已提交
438 439 440
  struct accelerator_t
  {
    const KerxSubTableFormat2 &table;
441
    hb_aat_apply_context_t *c;
B
Behdad Esfahbod 已提交
442 443

    inline accelerator_t (const KerxSubTableFormat2 &table_,
444 445
			  hb_aat_apply_context_t *c_) :
			    table (table_), c (c_) {}
B
Behdad Esfahbod 已提交
446 447

    inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
448
    { return table.get_kerning (left, right, c); }
B
Behdad Esfahbod 已提交
449 450
  };

451 452 453 454 455 456 457 458 459
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (likely (c->check_struct (this) &&
			  leftClassTable.sanitize (c, this) &&
			  rightClassTable.sanitize (c, this) &&
			  c->check_range (this, array)));
  }

460
  protected:
461
  KernSubTableHeader	header;
B
Behdad Esfahbod 已提交
462
  HBUINT32		rowWidth;	/* The width, in bytes, of a row in the table. */
463
  LOffsetTo<Lookup<HBUINT16>, false>
B
Behdad Esfahbod 已提交
464 465
			leftClassTable;	/* Offset from beginning of this subtable to
					 * left-hand class table. */
466
  LOffsetTo<Lookup<HBUINT16>, false>
B
Behdad Esfahbod 已提交
467 468
			rightClassTable;/* Offset from beginning of this subtable to
					 * right-hand class table. */
B
Behdad Esfahbod 已提交
469 470
  LOffsetTo<UnsizedArrayOf<FWORD>, false>
			 array;		/* Offset from beginning of this subtable to
B
Behdad Esfahbod 已提交
471
					 * the start of the kerning array. */
472
  public:
473
  DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 16);
474 475
};

476
template <typename KernSubTableHeader>
477 478
struct KerxSubTableFormat4
{
B
Behdad Esfahbod 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
  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) {}

515
    inline bool is_actionable (StateTableDriver<MorxTypes, EntryData> *driver HB_UNUSED,
B
Behdad Esfahbod 已提交
516 517 518 519
			       const Entry<EntryData> *entry)
    {
      return entry->data.ankrActionIndex != 0xFFFF;
    }
520
    inline bool transition (StateTableDriver<MorxTypes, EntryData> *driver,
B
Behdad Esfahbod 已提交
521 522 523 524
			    const Entry<EntryData> *entry)
    {
      hb_buffer_t *buffer = driver->buffer;

525
      if (mark_set && entry->data.ankrActionIndex != 0xFFFF && buffer->idx < buffer->len)
B
Behdad Esfahbod 已提交
526
      {
527
	hb_glyph_position_t &o = buffer->cur_pos();
B
Behdad Esfahbod 已提交
528 529 530 531 532 533 534 535 536 537
	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++;
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
	    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 已提交
554 555 556 557 558 559 560 561 562
	  }
	  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;
563 564
	    unsigned int markAnchorPoint = *data++;
	    unsigned int currAnchorPoint = *data++;
565 566 567 568 569 570 571 572
	    const Anchor markAnchor = c->ankr_table->get_anchor (c->buffer->info[mark].codepoint,
								 markAnchorPoint,
								 c->sanitizer.get_num_glyphs (),
								 c->ankr_end);
	    const Anchor currAnchor = c->ankr_table->get_anchor (c->buffer->cur ().codepoint,
								 currAnchorPoint,
								 c->sanitizer.get_num_glyphs (),
								 c->ankr_end);
573

574 575
	    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 已提交
576 577 578 579 580 581 582 583
	  }
	  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;
584 585 586 587 588 589 590
	    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 已提交
591 592 593
	  }
	  break;
	}
594 595 596
	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 已提交
597 598
      }

B
Behdad Esfahbod 已提交
599
      if (entry->flags & Mark)
B
Behdad Esfahbod 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
      {
	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;
  };

616 617 618 619
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

B
Behdad Esfahbod 已提交
620 621
    driver_context_t dc (this, c);

622
    StateTableDriver<MorxTypes, EntryData> driver (machine, c->buffer, c->font->face);
B
Behdad Esfahbod 已提交
623
    driver.drive (&dc);
624 625 626 627

    return_trace (true);
  }

628 629 630
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
631
    /* The rest of array sanitizations are done at run-time. */
B
Behdad Esfahbod 已提交
632 633
    return_trace (likely (c->check_struct (this) &&
			  machine.sanitize (c)));
634 635 636
  }

  protected:
637
  KernSubTableHeader	header;
638 639
  StateTable<MorxTypes, EntryData>
			machine;
B
Behdad Esfahbod 已提交
640
  HBUINT32		flags;
641
  public:
642
  DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 20);
643 644
};

645
template <typename KernSubTableHeader>
646 647
struct KerxSubTableFormat6
{
648 649 650 651 652 653 654
  enum Flags
  {
    ValuesAreLong	= 0x00000001,
  };

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

B
Behdad Esfahbod 已提交
655
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
656
			  hb_aat_apply_context_t *c) const
B
Behdad Esfahbod 已提交
657
  {
658
    unsigned int num_glyphs = c->sanitizer.get_num_glyphs ();
B
Behdad Esfahbod 已提交
659 660
    if (is_long ())
    {
661
      const typename U::Long &t = u.l;
B
Behdad Esfahbod 已提交
662 663 664
      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 已提交
665 666
      if (unlikely (offset < l)) return 0; /* Addition overflow. */
      if (unlikely (hb_unsigned_mul_overflows (offset, sizeof (FWORD32)))) return 0;
B
Behdad Esfahbod 已提交
667 668
      const FWORD32 *v = &StructAtOffset<FWORD32> (&(this+t.array), offset * sizeof (FWORD32));
      if (unlikely (!v->sanitize (&c->sanitizer))) return 0;
B
Behdad Esfahbod 已提交
669
      return kerxTupleKern (*v, header.tuple_count (), &(this+vector), c);
B
Behdad Esfahbod 已提交
670 671 672
    }
    else
    {
673
      const typename U::Short &t = u.s;
B
Behdad Esfahbod 已提交
674 675 676
      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 已提交
677 678
      const FWORD *v = &StructAtOffset<FWORD> (&(this+t.array), offset * sizeof (FWORD));
      if (unlikely (!v->sanitize (&c->sanitizer))) return 0;
B
Behdad Esfahbod 已提交
679
      return kerxTupleKern (*v, header.tuple_count (), &(this+vector), c);
B
Behdad Esfahbod 已提交
680 681 682
    }
  }

683 684 685 686
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

687 688 689
    if (!c->plan->requested_kerning)
      return false;

690 691 692
    if (header.coverage & header.CrossStream)
      return false;

693
    accelerator_t accel (*this, c);
B
Behdad Esfahbod 已提交
694 695
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
696 697 698 699

    return_trace (true);
  }

700 701
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
702
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
703
    return_trace (likely (c->check_struct (this) &&
B
Behdad Esfahbod 已提交
704 705 706 707 708 709 710 711 712
			  (is_long () ?
			   (
			     u.l.rowIndexTable.sanitize (c, this) &&
			     u.l.columnIndexTable.sanitize (c, this) &&
			     c->check_range (this, u.l.array)
			   ) : (
			     u.s.rowIndexTable.sanitize (c, this) &&
			     u.s.columnIndexTable.sanitize (c, this) &&
			     c->check_range (this, u.s.array)
713
			   )) &&
B
Behdad Esfahbod 已提交
714
			  (header.tuple_count () == 0 ||
715
			   c->check_range (this, vector))));
716 717
  }

B
Behdad Esfahbod 已提交
718 719 720
  struct accelerator_t
  {
    const KerxSubTableFormat6 &table;
721
    hb_aat_apply_context_t *c;
B
Behdad Esfahbod 已提交
722 723

    inline accelerator_t (const KerxSubTableFormat6 &table_,
724 725
			  hb_aat_apply_context_t *c_) :
			    table (table_), c (c_) {}
B
Behdad Esfahbod 已提交
726 727

    inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
728
    { return table.get_kerning (left, right, c); }
B
Behdad Esfahbod 已提交
729 730
  };

731
  protected:
732
  KernSubTableHeader		header;
B
Behdad Esfahbod 已提交
733 734 735
  HBUINT32			flags;
  HBUINT16			rowCount;
  HBUINT16			columnCount;
B
Behdad Esfahbod 已提交
736
  union U
737
  {
B
Behdad Esfahbod 已提交
738
    struct Long
739
    {
740 741 742
      LOffsetTo<Lookup<HBUINT32>, false>	rowIndexTable;
      LOffsetTo<Lookup<HBUINT32>, false>	columnIndexTable;
      LOffsetTo<UnsizedArrayOf<FWORD32>, false>	array;
743
    } l;
B
Behdad Esfahbod 已提交
744 745
    struct Short
    {
746 747 748
      LOffsetTo<Lookup<HBUINT16>, false>	rowIndexTable;
      LOffsetTo<Lookup<HBUINT16>, false>	columnIndexTable;
      LOffsetTo<UnsizedArrayOf<FWORD>, false>	array;
B
Behdad Esfahbod 已提交
749
    } s;
750
  } u;
751
  LOffsetTo<UnsizedArrayOf<FWORD>, false>	vector;
752
  public:
753
  DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 24);
754 755
};

B
Behdad Esfahbod 已提交
756 757 758 759 760

struct KerxSubTableHeader
{
  typedef MorxTypes Types;

B
Behdad Esfahbod 已提交
761 762
  unsigned int tuple_count (void) const { return tupleCount; }

B
Behdad Esfahbod 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
  enum Coverage
  {
    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. */
    Backwards		= 0x10000000,	/* If clear, process the glyphs forwards, that
					 * 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. */
  };

  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);
};

791 792
struct KerxTable
{
B
Behdad Esfahbod 已提交
793
  friend struct kerx;
B
Behdad Esfahbod 已提交
794

B
Behdad Esfahbod 已提交
795
  inline unsigned int get_size (void) const { return u.header.length; }
796
  inline unsigned int get_type (void) const { return u.header.coverage & u.header.SubtableType; }
797 798 799 800 801 802 803

  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) {
B
Behdad Esfahbod 已提交
804 805 806 807 808 809
    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 ());
810
    }
811 812
  }

813
  inline bool sanitize (hb_sanitize_context_t *c) const
814 815
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
816 817
    if (!u.header.sanitize (c) ||
	!c->check_range (this, u.header.length))
818 819
      return_trace (false);

820
    return_trace (dispatch (c));
821 822
  }

823
protected:
824
  union {
825 826 827 828 829 830
  KerxSubTableHeader				header;
  KerxSubTableFormat0<KerxSubTableHeader>	format0;
  KerxSubTableFormat1<KerxSubTableHeader>	format1;
  KerxSubTableFormat2<KerxSubTableHeader>	format2;
  KerxSubTableFormat4<KerxSubTableHeader>	format4;
  KerxSubTableFormat6<KerxSubTableHeader>	format6;
831
  } u;
832 833
public:
  DEFINE_SIZE_MIN (12);
834 835
};

836 837 838 839 840

/*
 * The 'kerx' Table
 */

841 842
struct kerx
{
843
  static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
844

845 846 847
  inline bool has_data (void) const { return version != 0; }

  inline void apply (hb_aat_apply_context_t *c) const
848
  {
849 850 851 852 853
    c->set_lookup_index (0);
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
    {
B
Behdad Esfahbod 已提交
854 855 856
      bool reverse;

      if (HB_DIRECTION_IS_VERTICAL (c->buffer->props.direction) !=
857
	  bool (table->u.header.coverage & table->u.header.Vertical))
B
Behdad Esfahbod 已提交
858
	goto skip;
B
Behdad Esfahbod 已提交
859

860
      reverse = bool (table->u.header.coverage & table->u.header.Backwards) !=
B
Behdad Esfahbod 已提交
861 862 863
		HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);

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

      if (reverse)
B
Behdad Esfahbod 已提交
867
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
868

869 870
      c->sanitizer.set_object (*table);

B
Behdad Esfahbod 已提交
871 872 873 874
      /* 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? */
875
      table->dispatch (c);
B
Behdad Esfahbod 已提交
876 877

      if (reverse)
B
Behdad Esfahbod 已提交
878
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
879 880 881 882

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

    skip:
883
      table = &StructAfter<KerxTable> (*table);
B
Behdad Esfahbod 已提交
884
      c->set_lookup_index (c->lookup_index + 1);
885
    }
886 887
  }

888
  inline bool sanitize (hb_sanitize_context_t *c) const
889
  {
890
    TRACE_SANITIZE (this);
891 892
    if (!version.sanitize (c) || version < 2 ||
	!tableCount.sanitize (c))
893
      return_trace (false);
894

895 896 897
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
898
    {
899 900
      if (!table->sanitize (c))
	return_trace (false);
901
      table = &StructAfter<KerxTable> (*table);
902 903 904 905 906 907
    }

    return_trace (true);
  }

  protected:
908 909 910 911 912 913 914 915
  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. */

916
  public:
917
  DEFINE_SIZE_MIN (8);
918 919
};

B
Behdad Esfahbod 已提交
920

921 922 923 924
} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */