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

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


42
namespace AAT {
43

44
using namespace OT;
45 46


B
Behdad Esfahbod 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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 已提交
63
struct KerxSubTableFormat0
64
{
B
Behdad Esfahbod 已提交
65
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
66
  {
B
Behdad Esfahbod 已提交
67 68
    hb_glyph_pair_t pair = {left, right};
    int i = pairs.bsearch (pair);
B
Behdad Esfahbod 已提交
69
    return i == -1 ? 0 : pairs[i].get_kerning ();
70 71
  }

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

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

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

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

    return_trace (true);
  }
85 86 87 88

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

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

struct KerxSubTableFormat1
{
102 103
  struct EntryData
  {
B
Behdad Esfahbod 已提交
104 105 106
    HBUINT16	kernActionIndex;/* Index into the kerning value array. If
				 * this index is 0xFFFF, then no kerning
				 * is to be performed. */
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    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 已提交
123 124 125
    inline driver_context_t (const KerxSubTableFormat1 *table,
			     hb_aat_apply_context_t *c_) :
	c (c_),
B
Behdad Esfahbod 已提交
126 127 128
	/* 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 已提交
129
	kernAction (&table->machine + table->kernAction),
B
Behdad Esfahbod 已提交
130
	depth (0) {}
131 132 133 134

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

B
Behdad Esfahbod 已提交
165
        for (unsigned int i = 0; i < depth; i++)
B
Behdad Esfahbod 已提交
166
	{
B
Behdad Esfahbod 已提交
167 168 169 170 171
	  /* 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 已提交
172 173 174
	  int v = *actions++;
	  /* XXX Non-forward direction... */
	  if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
B
Behdad Esfahbod 已提交
175
	    buffer->pos[idx].x_advance += c->font->em_scale_x (v);
B
Behdad Esfahbod 已提交
176
	  else
B
Behdad Esfahbod 已提交
177
	    buffer->pos[idx].y_advance += c->font->em_scale_y (v);
B
Behdad Esfahbod 已提交
178
	}
B
Behdad Esfahbod 已提交
179
	depth = 0;
B
Behdad Esfahbod 已提交
180
      }
181 182 183 184 185

      return true;
    }

    private:
B
Behdad Esfahbod 已提交
186 187 188 189
    hb_aat_apply_context_t *c;
    const UnsizedArrayOf<FWORD> &kernAction;
    unsigned int stack[8];
    unsigned int depth;
190 191
  };

192 193 194 195
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

196 197 198
    if (!c->plan->requested_kerning)
      return false;

B
Behdad Esfahbod 已提交
199
    driver_context_t dc (this, c);
200 201 202

    StateTableDriver<EntryData> driver (machine, c->buffer, c->font->face);
    driver.drive (&dc);
203 204 205 206

    return_trace (true);
  }

207 208
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
209
    TRACE_SANITIZE (this);
210
    return_trace (likely (machine.sanitize (c)));
211 212 213
  }

  protected:
214 215
  KerxSubTableHeader				header;
  StateTable<EntryData>				machine;
B
Behdad Esfahbod 已提交
216
  LOffsetTo<UnsizedArrayOf<FWORD>, false>	kernAction;
217
  public:
B
Behdad Esfahbod 已提交
218
  DEFINE_SIZE_STATIC (32);
219 220 221 222
};

struct KerxSubTableFormat2
{
B
Behdad Esfahbod 已提交
223
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
224
			  unsigned int num_glyphs) const
225
  {
226 227
    unsigned int l = (this+leftClassTable).get_value_or_null (left, num_glyphs);
    unsigned int r = (this+rightClassTable).get_value_or_null (right, num_glyphs);
228
    unsigned int offset = l + r;
B
Behdad Esfahbod 已提交
229 230
    const FWORD *v = &StructAtOffset<FWORD> (&(this+array), offset);
    if (unlikely ((const char *) v < (const char *) &array ||
231
		  (const char *) v + v->static_size - (const char *) this > header.length))
232 233 234 235
      return 0;
    return *v;
  }

236 237 238 239
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

240 241 242
    if (!c->plan->requested_kerning)
      return false;

B
Behdad Esfahbod 已提交
243 244 245 246
    accelerator_t accel (*this,
			 c->face->get_num_glyphs ());
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
247 248 249 250

    return_trace (true);
  }

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

B
Behdad Esfahbod 已提交
259 260 261 262 263 264
  struct accelerator_t
  {
    const KerxSubTableFormat2 &table;
    unsigned int num_glyphs;

    inline accelerator_t (const KerxSubTableFormat2 &table_,
265 266
			  unsigned int num_glyphs_)
			  : table (table_), num_glyphs (num_glyphs_) {}
B
Behdad Esfahbod 已提交
267 268 269

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

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

struct KerxSubTableFormat4
{
292 293 294 295 296 297 298 299 300
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */

    return_trace (true);
  }

301 302 303
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
304 305 306

    /* TODO */
    return_trace (likely (c->check_struct (this)));
307 308 309
  }

  protected:
B
Behdad Esfahbod 已提交
310
  KerxSubTableHeader	header;
311
  public:
B
Behdad Esfahbod 已提交
312
  DEFINE_SIZE_STATIC (12);
313 314 315 316
};

struct KerxSubTableFormat6
{
317 318 319 320 321 322 323
  enum Flags
  {
    ValuesAreLong	= 0x00000001,
  };

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

B
Behdad Esfahbod 已提交
324 325 326 327 328 329 330 331 332 333 334
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
			  unsigned int num_glyphs) const
  {
    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;
      const FWORD32 *v = &StructAtOffset<FWORD32> (&(this+t.array), offset * sizeof (FWORD32));
      if (unlikely ((const char *) v < (const char *) &t.array ||
335
		    (const char *) v + v->static_size - (const char *) this > header.length))
B
Behdad Esfahbod 已提交
336 337 338 339 340 341 342 343 344 345 346
	return 0;
      return *v;
    }
    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;
      const FWORD *v = &StructAtOffset<FWORD> (&(this+t.array), offset * sizeof (FWORD));
      if (unlikely ((const char *) v < (const char *) &t.array ||
347
		    (const char *) v + v->static_size - (const char *) this > header.length))
B
Behdad Esfahbod 已提交
348 349 350 351 352
	return 0;
      return *v;
    }
  }

353 354 355 356
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

357 358 359
    if (!c->plan->requested_kerning)
      return false;

B
Behdad Esfahbod 已提交
360 361 362 363
    accelerator_t accel (*this,
			 c->face->get_num_glyphs ());
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
364 365 366 367

    return_trace (true);
  }

368 369
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
370
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
371
    return_trace (likely (c->check_struct (this) &&
372 373 374
			  is_long () ?
			  (
			    u.l.rowIndexTable.sanitize (c, this) &&
B
Behdad Esfahbod 已提交
375
			    u.l.columnIndexTable.sanitize (c, this)
376 377
			  ) : (
			    u.s.rowIndexTable.sanitize (c, this) &&
B
Behdad Esfahbod 已提交
378
			    u.s.columnIndexTable.sanitize (c, this)
379
			  )));
380 381
  }

B
Behdad Esfahbod 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  struct accelerator_t
  {
    const KerxSubTableFormat6 &table;
    unsigned int num_glyphs;

    inline accelerator_t (const KerxSubTableFormat6 &table_,
			  unsigned int num_glyphs_)
			  : table (table_), num_glyphs (num_glyphs_) {}

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

397
  protected:
B
Behdad Esfahbod 已提交
398 399 400 401
  KerxSubTableHeader		header;
  HBUINT32			flags;
  HBUINT16			rowCount;
  HBUINT16			columnCount;
B
Behdad Esfahbod 已提交
402
  union U
403
  {
B
Behdad Esfahbod 已提交
404
    struct Long
405 406 407
    {
      LOffsetTo<Lookup<HBUINT32> >	rowIndexTable;
      LOffsetTo<Lookup<HBUINT32> >	columnIndexTable;
B
Behdad Esfahbod 已提交
408 409
      LOffsetTo<UnsizedArrayOf<FWORD32>, false>
					array;
410
    } l;
B
Behdad Esfahbod 已提交
411 412 413 414
    struct Short
    {
      LOffsetTo<Lookup<HBUINT16> >	rowIndexTable;
      LOffsetTo<Lookup<HBUINT16> >	columnIndexTable;
B
Behdad Esfahbod 已提交
415 416
      LOffsetTo<UnsizedArrayOf<FWORD>, false>
					array;
B
Behdad Esfahbod 已提交
417
    } s;
418
  } u;
419
  public:
420
  DEFINE_SIZE_STATIC (32);
421 422
};

423 424
struct KerxTable
{
B
Behdad Esfahbod 已提交
425
  friend struct kerx;
B
Behdad Esfahbod 已提交
426

B
Behdad Esfahbod 已提交
427 428
  inline unsigned int get_size (void) const { return u.header.length; }
  inline unsigned int get_type (void) const { return u.header.coverage & SubtableType; }
429 430

  enum Coverage
431
  {
432 433 434
    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 已提交
435
    Backwards		= 0x10000000,	/* If clear, process the glyphs forwards, that
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
					 * 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 ());
    }
457 458
  }

459
  inline bool sanitize (hb_sanitize_context_t *c) const
460 461
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
462 463
    if (!u.header.sanitize (c) ||
	!c->check_range (this, u.header.length))
464 465
      return_trace (false);

466
    return_trace (dispatch (c));
467 468
  }

469
protected:
470
  union {
B
Behdad Esfahbod 已提交
471
  KerxSubTableHeader	header;
472
  KerxSubTableFormat0	format0;
473
  KerxSubTableFormat1	format1;
474 475 476 477
  KerxSubTableFormat2	format2;
  KerxSubTableFormat4	format4;
  KerxSubTableFormat6	format6;
  } u;
478 479
public:
  DEFINE_SIZE_MIN (12);
480 481
};

482 483 484 485 486

/*
 * The 'kerx' Table
 */

487 488
struct kerx
{
489
  static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
490

491 492 493
  inline bool has_data (void) const { return version != 0; }

  inline void apply (hb_aat_apply_context_t *c) const
494
  {
495 496 497 498 499
    c->set_lookup_index (0);
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
    {
B
Behdad Esfahbod 已提交
500 501
      bool reverse;

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

B
Behdad Esfahbod 已提交
506
      if (HB_DIRECTION_IS_VERTICAL (c->buffer->props.direction) !=
B
Behdad Esfahbod 已提交
507
	  bool (table->u.header.coverage & KerxTable::Vertical))
B
Behdad Esfahbod 已提交
508
	goto skip;
B
Behdad Esfahbod 已提交
509

B
Behdad Esfahbod 已提交
510
      reverse = bool (table->u.header.coverage & KerxTable::Backwards) !=
B
Behdad Esfahbod 已提交
511 512 513
		HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);

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

      if (reverse)
B
Behdad Esfahbod 已提交
517
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
518

519 520
      c->sanitizer.set_object (*table);

B
Behdad Esfahbod 已提交
521 522 523 524
      /* 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? */
525
      table->dispatch (c);
B
Behdad Esfahbod 已提交
526 527

      if (reverse)
B
Behdad Esfahbod 已提交
528
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
529 530 531 532

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

    skip:
533 534
      table = &StructAfter<KerxTable> (*table);
    }
535 536
  }

537
  inline bool sanitize (hb_sanitize_context_t *c) const
538
  {
539
    TRACE_SANITIZE (this);
540 541
    if (!version.sanitize (c) || version < 2 ||
	!tableCount.sanitize (c))
542
      return_trace (false);
543

544 545 546
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
547
    {
548 549
      if (!table->sanitize (c))
	return_trace (false);
550
      table = &StructAfter<KerxTable> (*table);
551 552 553 554 555 556
    }

    return_trace (true);
  }

  protected:
557 558 559 560 561 562 563 564
  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. */

565
  public:
566
  DEFINE_SIZE_MIN (8);
567 568 569 570 571 572
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */