hb-aat-layout-kerx-table.hh 23.3 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


48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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 已提交
63 64
struct KerxSubTableHeader
{
65 66 67 68 69 70 71 72 73 74 75 76 77 78
  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. */
  };

B
Behdad Esfahbod 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92
  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);
};

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

106 107 108 109
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

110 111 112
    if (!c->plan->requested_kerning)
      return false;

113 114 115
    if (header.coverage & header.CrossStream)
      return false;

116 117
    accelerator_t accel (*this, c);
    hb_kern_machine_t<accelerator_t> machine (accel);
B
Behdad Esfahbod 已提交
118
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
119 120 121

    return_trace (true);
  }
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136
  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); }
  };


137 138 139
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
140 141
    return_trace (likely (c->check_struct (this) &&
			  pairs.sanitize (c)));
142 143 144
  }

  protected:
145
  KernSubTableHeader	header;
B
Behdad Esfahbod 已提交
146
  BinSearchArrayOf<KernPair, HBUINT32>
B
Behdad Esfahbod 已提交
147
			pairs;	/* Sorted kern records. */
148
  public:
149
  DEFINE_SIZE_ARRAY (KernSubTableHeader::static_size + 16, pairs);
150 151
};

152
template <typename KernSubTableHeader>
153 154
struct KerxSubTableFormat1
{
155 156
  struct EntryData
  {
B
Behdad Esfahbod 已提交
157 158 159
    HBUINT16	kernActionIndex;/* Index into the kerning value array. If
				 * this index is 0xFFFF, then no kerning
				 * is to be performed. */
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    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 已提交
176 177 178
    inline driver_context_t (const KerxSubTableFormat1 *table,
			     hb_aat_apply_context_t *c_) :
	c (c_),
B
Behdad Esfahbod 已提交
179 180 181
	/* 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 已提交
182
	kernAction (&table->machine + table->kernAction),
183
	depth (0),
184 185
	crossStream (table->header.coverage & table->header.CrossStream),
	crossOffset (0) {}
186

187
    inline bool is_actionable (StateTableDriver<MorxTypes, EntryData> *driver HB_UNUSED,
188 189
			       const Entry<EntryData> *entry)
    {
B
Behdad Esfahbod 已提交
190
      return entry->data.kernActionIndex != 0xFFFF;
191
    }
192
    inline bool transition (StateTableDriver<MorxTypes, EntryData> *driver,
193 194
			    const Entry<EntryData> *entry)
    {
B
Behdad Esfahbod 已提交
195 196 197 198 199
      hb_buffer_t *buffer = driver->buffer;
      unsigned int flags = entry->flags;

      if (flags & Reset)
      {
B
Behdad Esfahbod 已提交
200
	depth = 0;
B
Behdad Esfahbod 已提交
201 202 203 204
      }

      if (flags & Push)
      {
B
Behdad Esfahbod 已提交
205
	if (likely (depth < ARRAY_LENGTH (stack)))
B
Behdad Esfahbod 已提交
206 207 208 209 210 211 212 213
	  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];
B
Behdad Esfahbod 已提交
214
	if (!c->sanitizer.check_array (actions, depth))
B
Behdad Esfahbod 已提交
215 216 217 218 219
	{
	  depth = 0;
	  return false;
	}

220
	hb_mask_t kern_mask = c->plan->kern_mask;
221 222 223 224 225 226 227 228 229 230 231 232

	/* 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 已提交
233
	{
234 235 236 237 238 239 240 241 242
	  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. */
243
	  if (v == -0x8000)
244 245 246 247
	  {
	    crossOffset = 0;
	    v = 0;
	  }
248
	  if (idx < buffer->len && buffer->info[idx].mask & kern_mask)
249 250
	  {
	    if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
B
Behdad Esfahbod 已提交
251
	    {
252
	      if (crossStream)
253 254 255 256 257
	      {
		crossOffset += v;
		if (!buffer->pos[idx].y_offset)
		  buffer->pos[idx].y_offset += c->font->em_scale_y (crossOffset);
	      }
258 259
	      else
	      {
260 261 262
		if (!buffer->pos[idx].x_offset)
		{
		  buffer->pos[idx].x_advance += c->font->em_scale_x (v);
263
		  buffer->pos[idx].x_offset += c->font->em_scale_x (v);
264
		}
265
	      }
B
Behdad Esfahbod 已提交
266
	    }
267
	    else
B
Behdad Esfahbod 已提交
268
	    {
269
	      if (crossStream)
270 271 272 273 274 275
	      {
	        /* CoreText doesn't do crossStream kerning in vertical. */
		//crossOffset += v;
		//if (!buffer->pos[idx].x_offset)
		//  buffer->pos[idx].x_offset = c->font->em_scale_x (crossOffset);
	      }
276 277
	      else
	      {
278 279 280
		if (!buffer->pos[idx].y_offset)
		{
		  buffer->pos[idx].y_advance += c->font->em_scale_y (v);
281
		  buffer->pos[idx].y_offset += c->font->em_scale_y (v);
282
		}
283
	      }
B
Behdad Esfahbod 已提交
284
	    }
285
	  }
B
Behdad Esfahbod 已提交
286
	}
B
Behdad Esfahbod 已提交
287
	depth = 0;
B
Behdad Esfahbod 已提交
288
      }
B
Behdad Esfahbod 已提交
289 290
      else
	buffer->pos[buffer->idx].y_offset += c->font->em_scale_y (crossOffset);
291 292 293 294 295

      return true;
    }

    private:
B
Behdad Esfahbod 已提交
296 297 298 299
    hb_aat_apply_context_t *c;
    const UnsizedArrayOf<FWORD> &kernAction;
    unsigned int stack[8];
    unsigned int depth;
300
    bool crossStream;
301
    int crossOffset;
302 303
  };

304 305 306 307
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

308 309 310
    if (!c->plan->requested_kerning)
      return false;

311 312 313
    if (header.coverage & header.CrossStream)
      return false;

314 315 316
    if (header.tupleCount)
      return_trace (false); /* TODO kerxTupleKern */

B
Behdad Esfahbod 已提交
317
    driver_context_t dc (this, c);
318

319
    StateTableDriver<MorxTypes, EntryData> driver (machine, c->buffer, c->font->face);
320
    driver.drive (&dc);
321 322 323 324

    return_trace (true);
  }

325 326
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
327
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
328 329 330
    /* The rest of array sanitizations are done at run-time. */
    return_trace (likely (c->check_struct (this) &&
			  machine.sanitize (c)));
331 332 333
  }

  protected:
334
  KernSubTableHeader				header;
B
Behdad Esfahbod 已提交
335
  StateTable<MorxTypes, EntryData>		machine;
B
Behdad Esfahbod 已提交
336
  LOffsetTo<UnsizedArrayOf<FWORD>, false>	kernAction;
337
  public:
338
  DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 20);
339 340
};

341
template <typename KernSubTableHeader>
342 343
struct KerxSubTableFormat2
{
B
Behdad Esfahbod 已提交
344
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
345
			  hb_aat_apply_context_t *c) const
346
  {
347
    unsigned int num_glyphs = c->sanitizer.get_num_glyphs ();
348 349
    unsigned int l = (this+leftClassTable).get_value_or_null (left, num_glyphs);
    unsigned int r = (this+rightClassTable).get_value_or_null (right, num_glyphs);
350
    unsigned int offset = l + r;
B
Behdad Esfahbod 已提交
351 352
    const FWORD *v = &StructAtOffset<FWORD> (&(this+array), offset);
    if (unlikely (!v->sanitize (&c->sanitizer))) return 0;
353
    return kerxTupleKern (*v, header.tupleCount, this, c);
354 355
  }

356 357 358 359
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

360 361 362
    if (!c->plan->requested_kerning)
      return false;

363 364 365
    if (header.coverage & header.CrossStream)
      return false;

366
    accelerator_t accel (*this, c);
B
Behdad Esfahbod 已提交
367 368
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
369 370 371 372

    return_trace (true);
  }

B
Behdad Esfahbod 已提交
373 374 375
  struct accelerator_t
  {
    const KerxSubTableFormat2 &table;
376
    hb_aat_apply_context_t *c;
B
Behdad Esfahbod 已提交
377 378

    inline accelerator_t (const KerxSubTableFormat2 &table_,
379 380
			  hb_aat_apply_context_t *c_) :
			    table (table_), c (c_) {}
B
Behdad Esfahbod 已提交
381 382

    inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
383
    { return table.get_kerning (left, right, c); }
B
Behdad Esfahbod 已提交
384 385
  };

386 387 388 389 390 391 392 393 394
  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)));
  }

395
  protected:
396
  KernSubTableHeader	header;
B
Behdad Esfahbod 已提交
397
  HBUINT32		rowWidth;	/* The width, in bytes, of a row in the table. */
398
  LOffsetTo<Lookup<HBUINT16>, false>
B
Behdad Esfahbod 已提交
399 400
			leftClassTable;	/* Offset from beginning of this subtable to
					 * left-hand class table. */
401
  LOffsetTo<Lookup<HBUINT16>, false>
B
Behdad Esfahbod 已提交
402 403
			rightClassTable;/* Offset from beginning of this subtable to
					 * right-hand class table. */
B
Behdad Esfahbod 已提交
404 405
  LOffsetTo<UnsizedArrayOf<FWORD>, false>
			 array;		/* Offset from beginning of this subtable to
B
Behdad Esfahbod 已提交
406
					 * the start of the kerning array. */
407
  public:
408
  DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 16);
409 410
};

411
template <typename KernSubTableHeader>
412 413
struct KerxSubTableFormat4
{
B
Behdad Esfahbod 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
  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) {}

450
    inline bool is_actionable (StateTableDriver<MorxTypes, EntryData> *driver HB_UNUSED,
B
Behdad Esfahbod 已提交
451 452 453 454
			       const Entry<EntryData> *entry)
    {
      return entry->data.ankrActionIndex != 0xFFFF;
    }
455
    inline bool transition (StateTableDriver<MorxTypes, EntryData> *driver,
B
Behdad Esfahbod 已提交
456 457 458 459 460
			    const Entry<EntryData> *entry)
    {
      hb_buffer_t *buffer = driver->buffer;
      unsigned int flags = entry->flags;

461
      if (mark_set && entry->data.ankrActionIndex != 0xFFFF && buffer->idx < buffer->len)
B
Behdad Esfahbod 已提交
462
      {
463
	hb_glyph_position_t &o = buffer->cur_pos();
B
Behdad Esfahbod 已提交
464 465 466 467 468 469 470 471 472 473
	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++;
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	    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 已提交
490 491 492 493 494 495 496 497 498
	  }
	  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;
499 500
	    unsigned int markAnchorPoint = *data++;
	    unsigned int currAnchorPoint = *data++;
501 502 503 504 505 506 507 508
	    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);
509

510 511
	    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 已提交
512 513 514 515 516 517 518 519
	  }
	  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;
520 521 522 523 524 525 526
	    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 已提交
527 528 529
	  }
	  break;
	}
530 531 532
	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 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
      }

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

552 553 554 555
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

B
Behdad Esfahbod 已提交
556 557
    driver_context_t dc (this, c);

558
    StateTableDriver<MorxTypes, EntryData> driver (machine, c->buffer, c->font->face);
B
Behdad Esfahbod 已提交
559
    driver.drive (&dc);
560 561 562 563

    return_trace (true);
  }

564 565 566
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
567
    /* The rest of array sanitizations are done at run-time. */
B
Behdad Esfahbod 已提交
568 569
    return_trace (likely (c->check_struct (this) &&
			  machine.sanitize (c)));
570 571 572
  }

  protected:
573
  KernSubTableHeader	header;
574 575
  StateTable<MorxTypes, EntryData>
			machine;
B
Behdad Esfahbod 已提交
576
  HBUINT32		flags;
577
  public:
578
  DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 20);
579 580
};

581
template <typename KernSubTableHeader>
582 583
struct KerxSubTableFormat6
{
584 585 586 587 588 589 590
  enum Flags
  {
    ValuesAreLong	= 0x00000001,
  };

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

B
Behdad Esfahbod 已提交
591
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
592
			  hb_aat_apply_context_t *c) const
B
Behdad Esfahbod 已提交
593
  {
594
    unsigned int num_glyphs = c->sanitizer.get_num_glyphs ();
B
Behdad Esfahbod 已提交
595 596
    if (is_long ())
    {
597
      const typename U::Long &t = u.l;
B
Behdad Esfahbod 已提交
598 599 600
      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 已提交
601 602
      if (unlikely (offset < l)) return 0; /* Addition overflow. */
      if (unlikely (hb_unsigned_mul_overflows (offset, sizeof (FWORD32)))) return 0;
B
Behdad Esfahbod 已提交
603 604
      const FWORD32 *v = &StructAtOffset<FWORD32> (&(this+t.array), offset * sizeof (FWORD32));
      if (unlikely (!v->sanitize (&c->sanitizer))) return 0;
605
      return kerxTupleKern (*v, header.tupleCount, &(this+vector), c);
B
Behdad Esfahbod 已提交
606 607 608
    }
    else
    {
609
      const typename U::Short &t = u.s;
B
Behdad Esfahbod 已提交
610 611 612
      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 已提交
613 614
      const FWORD *v = &StructAtOffset<FWORD> (&(this+t.array), offset * sizeof (FWORD));
      if (unlikely (!v->sanitize (&c->sanitizer))) return 0;
615
      return kerxTupleKern (*v, header.tupleCount, &(this+vector), c);
B
Behdad Esfahbod 已提交
616 617 618
    }
  }

619 620 621 622
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

623 624 625
    if (!c->plan->requested_kerning)
      return false;

626 627 628
    if (header.coverage & header.CrossStream)
      return false;

629
    accelerator_t accel (*this, c);
B
Behdad Esfahbod 已提交
630 631
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
632 633 634 635

    return_trace (true);
  }

636 637
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
638
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
639
    return_trace (likely (c->check_struct (this) &&
B
Behdad Esfahbod 已提交
640 641 642 643 644 645 646 647 648
			  (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)
649 650 651
			   )) &&
			  (header.tupleCount == 0 ||
			   c->check_range (this, vector))));
652 653
  }

B
Behdad Esfahbod 已提交
654 655 656
  struct accelerator_t
  {
    const KerxSubTableFormat6 &table;
657
    hb_aat_apply_context_t *c;
B
Behdad Esfahbod 已提交
658 659

    inline accelerator_t (const KerxSubTableFormat6 &table_,
660 661
			  hb_aat_apply_context_t *c_) :
			    table (table_), c (c_) {}
B
Behdad Esfahbod 已提交
662 663

    inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
664
    { return table.get_kerning (left, right, c); }
B
Behdad Esfahbod 已提交
665 666
  };

667
  protected:
668
  KernSubTableHeader		header;
B
Behdad Esfahbod 已提交
669 670 671
  HBUINT32			flags;
  HBUINT16			rowCount;
  HBUINT16			columnCount;
B
Behdad Esfahbod 已提交
672
  union U
673
  {
B
Behdad Esfahbod 已提交
674
    struct Long
675
    {
676 677 678
      LOffsetTo<Lookup<HBUINT32>, false>	rowIndexTable;
      LOffsetTo<Lookup<HBUINT32>, false>	columnIndexTable;
      LOffsetTo<UnsizedArrayOf<FWORD32>, false>	array;
679
    } l;
B
Behdad Esfahbod 已提交
680 681
    struct Short
    {
682 683 684
      LOffsetTo<Lookup<HBUINT16>, false>	rowIndexTable;
      LOffsetTo<Lookup<HBUINT16>, false>	columnIndexTable;
      LOffsetTo<UnsizedArrayOf<FWORD>, false>	array;
B
Behdad Esfahbod 已提交
685
    } s;
686
  } u;
687
  LOffsetTo<UnsizedArrayOf<FWORD>, false>	vector;
688
  public:
689
  DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 24);
690 691
};

692 693
struct KerxTable
{
B
Behdad Esfahbod 已提交
694
  friend struct kerx;
B
Behdad Esfahbod 已提交
695

B
Behdad Esfahbod 已提交
696
  inline unsigned int get_size (void) const { return u.header.length; }
697
  inline unsigned int get_type (void) const { return u.header.coverage & u.header.SubtableType; }
698 699 700 701 702 703 704

  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 已提交
705 706 707 708 709 710
    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 ());
711
    }
712 713
  }

714
  inline bool sanitize (hb_sanitize_context_t *c) const
715 716
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
717 718
    if (!u.header.sanitize (c) ||
	!c->check_range (this, u.header.length))
719 720
      return_trace (false);

721
    return_trace (dispatch (c));
722 723
  }

724
protected:
725
  union {
726 727 728 729 730 731
  KerxSubTableHeader				header;
  KerxSubTableFormat0<KerxSubTableHeader>	format0;
  KerxSubTableFormat1<KerxSubTableHeader>	format1;
  KerxSubTableFormat2<KerxSubTableHeader>	format2;
  KerxSubTableFormat4<KerxSubTableHeader>	format4;
  KerxSubTableFormat6<KerxSubTableHeader>	format6;
732
  } u;
733 734
public:
  DEFINE_SIZE_MIN (12);
735 736
};

737 738 739 740 741

/*
 * The 'kerx' Table
 */

742 743
struct kerx
{
744
  static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
745

746 747 748
  inline bool has_data (void) const { return version != 0; }

  inline void apply (hb_aat_apply_context_t *c) const
749
  {
750 751 752 753 754
    c->set_lookup_index (0);
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
    {
B
Behdad Esfahbod 已提交
755 756 757
      bool reverse;

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

761
      reverse = bool (table->u.header.coverage & table->u.header.Backwards) !=
B
Behdad Esfahbod 已提交
762 763 764
		HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);

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

      if (reverse)
B
Behdad Esfahbod 已提交
768
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
769

770 771
      c->sanitizer.set_object (*table);

B
Behdad Esfahbod 已提交
772 773 774 775
      /* 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? */
776
      table->dispatch (c);
B
Behdad Esfahbod 已提交
777 778

      if (reverse)
B
Behdad Esfahbod 已提交
779
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
780 781 782 783

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

    skip:
784
      table = &StructAfter<KerxTable> (*table);
B
Behdad Esfahbod 已提交
785
      c->set_lookup_index (c->lookup_index + 1);
786
    }
787 788
  }

789
  inline bool sanitize (hb_sanitize_context_t *c) const
790
  {
791
    TRACE_SANITIZE (this);
792 793
    if (!version.sanitize (c) || version < 2 ||
	!tableCount.sanitize (c))
794
      return_trace (false);
795

796 797 798
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
799
    {
800 801
      if (!table->sanitize (c))
	return_trace (false);
802
      table = &StructAfter<KerxTable> (*table);
803 804 805 806 807 808
    }

    return_trace (true);
  }

  protected:
809 810 811 812 813 814 815 816
  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. */

817
  public:
818
  DEFINE_SIZE_MIN (8);
819 820 821 822 823 824
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */