hb-aat-layout-kerx-table.hh 10.7 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 69 70 71
    hb_glyph_pair_t pair = {left, right};
    int i = pairs.bsearch (pair);
    if (i == -1)
      return 0;
    return pairs[i].get_kerning ();
72 73
  }

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

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

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

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

    return_trace (true);
  }
87 88 89 90

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

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

struct KerxSubTableFormat1
{
104 105 106 107
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

108 109 110
    if (!c->plan->requested_kerning)
      return false;

111 112 113 114 115
    /* TODO */

    return_trace (true);
  }

116 117
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
118
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
119 120
    return_trace (likely (c->check_struct (this) &&
			  stateHeader.sanitize (c)));
121 122 123
  }

  protected:
B
Behdad Esfahbod 已提交
124
  KerxSubTableHeader		header;
125 126
  StateTable<HBUINT16>		stateHeader;
  LOffsetTo<ArrayOf<HBUINT16> >	valueTable;
127
  public:
B
Behdad Esfahbod 已提交
128
  DEFINE_SIZE_STATIC (32);
129 130 131 132
};

struct KerxSubTableFormat2
{
B
Behdad Esfahbod 已提交
133 134
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
			  const char *end, unsigned int num_glyphs) const
135
  {
136 137
    unsigned int l = (this+leftClassTable).get_value_or_null (left, num_glyphs);
    unsigned int r = (this+rightClassTable).get_value_or_null (right, num_glyphs);
138
    unsigned int offset = l + r;
B
Behdad Esfahbod 已提交
139 140 141
    const FWORD *v = &StructAtOffset<FWORD> (&(this+array), offset);
    if (unlikely ((const char *) v < (const char *) &array ||
		  (const char *) v > (const char *) end - 2))
142 143 144 145
      return 0;
    return *v;
  }

146 147 148 149
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

150 151 152
    if (!c->plan->requested_kerning)
      return false;

B
Behdad Esfahbod 已提交
153
    accelerator_t accel (*this,
154
			 c->sanitizer.end,
B
Behdad Esfahbod 已提交
155 156 157
			 c->face->get_num_glyphs ());
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
158 159 160 161

    return_trace (true);
  }

162 163 164
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
165 166 167 168 169
    return_trace (likely (c->check_struct (this) &&
			  rowWidth.sanitize (c) &&
			  leftClassTable.sanitize (c, this) &&
			  rightClassTable.sanitize (c, this) &&
			  array.sanitize (c, this)));
170 171
  }

B
Behdad Esfahbod 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  struct accelerator_t
  {
    const KerxSubTableFormat2 &table;
    const char *end;
    unsigned int num_glyphs;

    inline accelerator_t (const KerxSubTableFormat2 &table_,
			  const char *end_, unsigned int num_glyphs_)
			  : table (table_), end (end_), num_glyphs (num_glyphs_) {}

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

188
  protected:
B
Behdad Esfahbod 已提交
189 190
  KerxSubTableHeader	header;
  HBUINT32		rowWidth;	/* The width, in bytes, of a row in the table. */
B
Behdad Esfahbod 已提交
191
  LOffsetTo<Lookup<HBUINT16> >
B
Behdad Esfahbod 已提交
192 193
			leftClassTable;	/* Offset from beginning of this subtable to
					 * left-hand class table. */
B
Behdad Esfahbod 已提交
194
  LOffsetTo<Lookup<HBUINT16> >
B
Behdad Esfahbod 已提交
195 196 197 198
			rightClassTable;/* Offset from beginning of this subtable to
					 * right-hand class table. */
  LOffsetTo<FWORD>	array;		/* Offset from beginning of this subtable to
					 * the start of the kerning array. */
199
  public:
B
Behdad Esfahbod 已提交
200
  DEFINE_SIZE_STATIC (28);
201 202 203 204
};

struct KerxSubTableFormat4
{
205 206 207 208 209 210 211 212 213
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */

    return_trace (true);
  }

214 215 216
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
217 218 219

    /* TODO */
    return_trace (likely (c->check_struct (this)));
220 221 222
  }

  protected:
B
Behdad Esfahbod 已提交
223
  KerxSubTableHeader	header;
224
  public:
B
Behdad Esfahbod 已提交
225
  DEFINE_SIZE_STATIC (12);
226 227 228 229
};

struct KerxSubTableFormat6
{
230 231 232 233
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

234 235 236
    if (!c->plan->requested_kerning)
      return false;

237 238 239 240 241
    /* TODO */

    return_trace (true);
  }

242 243
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
244
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
245 246 247 248 249
    return_trace (likely (c->check_struct (this) &&
			  rowIndexTable.sanitize (c, this) &&
			  columnIndexTable.sanitize (c, this) &&
			  kerningArray.sanitize (c, this) &&
			  kerningVector.sanitize (c, this)));
250 251 252
  }

  protected:
B
Behdad Esfahbod 已提交
253 254 255 256
  KerxSubTableHeader		header;
  HBUINT32			flags;
  HBUINT16			rowCount;
  HBUINT16			columnCount;
257 258 259 260
  LOffsetTo<Lookup<HBUINT16> >	rowIndexTable;
  LOffsetTo<Lookup<HBUINT16> >	columnIndexTable;
  LOffsetTo<Lookup<HBUINT16> >	kerningArray;
  LOffsetTo<Lookup<HBUINT16> >	kerningVector;
261
  public:
B
Behdad Esfahbod 已提交
262
  DEFINE_SIZE_STATIC (36);
263 264
};

265 266
struct KerxTable
{
B
Behdad Esfahbod 已提交
267
  friend struct kerx;
B
Behdad Esfahbod 已提交
268

B
Behdad Esfahbod 已提交
269 270
  inline unsigned int get_size (void) const { return u.header.length; }
  inline unsigned int get_type (void) const { return u.header.coverage & SubtableType; }
271 272

  enum Coverage
273
  {
274 275 276
    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 已提交
277
    Backwards		= 0x10000000,	/* If clear, process the glyphs forwards, that
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
					 * 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 ());
    }
299 300
  }

301
  inline bool sanitize (hb_sanitize_context_t *c) const
302 303
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
304 305
    if (!u.header.sanitize (c) ||
	!c->check_range (this, u.header.length))
306 307
      return_trace (false);

308
    return_trace (dispatch (c));
309 310
  }

311
protected:
312
  union {
B
Behdad Esfahbod 已提交
313
  KerxSubTableHeader	header;
314
  KerxSubTableFormat0	format0;
315
  KerxSubTableFormat1	format1;
316 317 318 319
  KerxSubTableFormat2	format2;
  KerxSubTableFormat4	format4;
  KerxSubTableFormat6	format6;
  } u;
320 321
public:
  DEFINE_SIZE_MIN (12);
322 323
};

324 325 326 327 328

/*
 * The 'kerx' Table
 */

329 330
struct kerx
{
331
  static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
332

333 334 335
  inline bool has_data (void) const { return version != 0; }

  inline void apply (hb_aat_apply_context_t *c) const
336
  {
337 338 339 340 341
    c->set_lookup_index (0);
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
    {
B
Behdad Esfahbod 已提交
342 343
      bool reverse;

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

B
Behdad Esfahbod 已提交
347
      if (HB_DIRECTION_IS_VERTICAL (c->buffer->props.direction) !=
B
Behdad Esfahbod 已提交
348
	  bool (table->u.header.coverage & KerxTable::Vertical))
B
Behdad Esfahbod 已提交
349
	goto skip;
B
Behdad Esfahbod 已提交
350

B
Behdad Esfahbod 已提交
351
      reverse = bool (table->u.header.coverage & KerxTable::Backwards) !=
B
Behdad Esfahbod 已提交
352 353 354
		HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);

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

      if (reverse)
B
Behdad Esfahbod 已提交
358
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
359

360 361
      c->sanitizer.set_object (*table);

B
Behdad Esfahbod 已提交
362 363 364 365
      /* 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? */
366
      table->dispatch (c);
B
Behdad Esfahbod 已提交
367 368

      if (reverse)
B
Behdad Esfahbod 已提交
369
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
370 371 372 373

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

    skip:
374 375
      table = &StructAfter<KerxTable> (*table);
    }
376 377
  }

378
  inline bool sanitize (hb_sanitize_context_t *c) const
379
  {
380
    TRACE_SANITIZE (this);
381 382
    if (!version.sanitize (c) || version < 2 ||
	!tableCount.sanitize (c))
383
      return_trace (false);
384

385 386 387
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
388
    {
389 390
      if (!table->sanitize (c))
	return_trace (false);
391
      table = &StructAfter<KerxTable> (*table);
392 393 394 395 396 397
    }

    return_trace (true);
  }

  protected:
398 399 400 401 402 403 404 405
  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. */

406
  public:
407
  DEFINE_SIZE_MIN (8);
408 409 410 411 412 413
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */