hb-aat-layout-kerx-table.hh 10.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"
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
struct KerxSubTableFormat0
48
{
B
Behdad Esfahbod 已提交
49
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
50
  {
B
Behdad Esfahbod 已提交
51 52 53 54 55
    hb_glyph_pair_t pair = {left, right};
    int i = pairs.bsearch (pair);
    if (i == -1)
      return 0;
    return pairs[i].get_kerning ();
56 57
  }

58 59 60 61
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

62 63 64
    if (!c->plan->requested_kerning)
      return false;

B
Behdad Esfahbod 已提交
65 66 67
    hb_kern_machine_t<KerxSubTableFormat0> machine (*this);

    machine.kern (c->font, c->buffer, c->plan->kern_mask);
68 69 70

    return_trace (true);
  }
71 72 73 74

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
75
    return_trace (likely (pairs.sanitize (c)));
76 77 78
  }

  protected:
B
Behdad Esfahbod 已提交
79 80
  BinSearchArrayOf<KernPair, HBUINT32>
		pairs;	/* Sorted kern records. */
81
  public:
B
Behdad Esfahbod 已提交
82
  DEFINE_SIZE_ARRAY (16, pairs);
83 84 85 86
};

struct KerxSubTableFormat1
{
87 88 89 90
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

91 92 93
    if (!c->plan->requested_kerning)
      return false;

94 95 96 97 98
    /* TODO */

    return_trace (true);
  }

99 100
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
101
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
102 103
    return_trace (likely (c->check_struct (this) &&
			  stateHeader.sanitize (c)));
104 105 106
  }

  protected:
107 108
  StateTable<HBUINT16>		stateHeader;
  LOffsetTo<ArrayOf<HBUINT16> >	valueTable;
109
  public:
110
  DEFINE_SIZE_STATIC (20);
111 112 113 114
};

struct KerxSubTableFormat2
{
B
Behdad Esfahbod 已提交
115 116
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
			  const char *end, unsigned int num_glyphs) const
117
  {
118 119
    unsigned int l = (this+leftClassTable).get_value_or_null (left, num_glyphs);
    unsigned int r = (this+rightClassTable).get_value_or_null (right, num_glyphs);
120
    unsigned int offset = l + r;
B
Behdad Esfahbod 已提交
121 122 123
    const FWORD *v = &StructAtOffset<FWORD> (&(this+array), offset);
    if (unlikely ((const char *) v < (const char *) &array ||
		  (const char *) v > (const char *) end - 2))
124 125 126 127
      return 0;
    return *v;
  }

128 129 130 131
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

132 133 134
    if (!c->plan->requested_kerning)
      return false;

B
Behdad Esfahbod 已提交
135
    accelerator_t accel (*this,
136
			 c->sanitizer.end,
B
Behdad Esfahbod 已提交
137 138 139
			 c->face->get_num_glyphs ());
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
140 141 142 143

    return_trace (true);
  }

144 145 146
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
147 148 149 150 151
    return_trace (likely (c->check_struct (this) &&
			  rowWidth.sanitize (c) &&
			  leftClassTable.sanitize (c, this) &&
			  rightClassTable.sanitize (c, this) &&
			  array.sanitize (c, this)));
152 153
  }

B
Behdad Esfahbod 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  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);
    }
  };

170 171
  protected:
  HBUINT32	rowWidth;	/* The width, in bytes, of a row in the table. */
B
Behdad Esfahbod 已提交
172
  LOffsetTo<Lookup<HBUINT16> >
173 174
		leftClassTable;	/* Offset from beginning of this subtable to
				 * left-hand class table. */
B
Behdad Esfahbod 已提交
175
  LOffsetTo<Lookup<HBUINT16> >
176 177 178 179 180 181
		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. */
  public:
182
  DEFINE_SIZE_STATIC (16);
183 184 185 186
};

struct KerxSubTableFormat4
{
187 188 189 190 191 192 193 194 195
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */

    return_trace (true);
  }

196 197 198
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
199 200 201

    /* TODO */
    return_trace (likely (c->check_struct (this)));
202 203 204 205
  }

  protected:
  public:
B
Behdad Esfahbod 已提交
206
  DEFINE_SIZE_STATIC (1);
207 208 209 210
};

struct KerxSubTableFormat6
{
211 212 213 214
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

215 216 217
    if (!c->plan->requested_kerning)
      return false;

218 219 220 221 222
    /* TODO */

    return_trace (true);
  }

223 224
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
225
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
226 227 228 229 230
    return_trace (likely (c->check_struct (this) &&
			  rowIndexTable.sanitize (c, this) &&
			  columnIndexTable.sanitize (c, this) &&
			  kerningArray.sanitize (c, this) &&
			  kerningVector.sanitize (c, this)));
231 232 233
  }

  protected:
234 235 236
  HBUINT32	flags;
  HBUINT16	rowCount;
  HBUINT16	columnCount;
237 238 239 240
  LOffsetTo<Lookup<HBUINT16> >	rowIndexTable;
  LOffsetTo<Lookup<HBUINT16> >	columnIndexTable;
  LOffsetTo<Lookup<HBUINT16> >	kerningArray;
  LOffsetTo<Lookup<HBUINT16> >	kerningVector;
241
  public:
242
  DEFINE_SIZE_STATIC (24);
243 244
};

245 246
struct KerxTable
{
B
Behdad Esfahbod 已提交
247
  friend struct kerx;
B
Behdad Esfahbod 已提交
248

249 250 251 252
  inline unsigned int get_size (void) const { return length; }
  inline unsigned int get_type (void) const { return coverage & SubtableType; }

  enum Coverage
253
  {
254 255 256
    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 已提交
257
    Backwards		= 0x10000000,	/* If clear, process the glyphs forwards, that
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
					 * 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 ());
    }
279 280
  }

281
  inline bool sanitize (hb_sanitize_context_t *c) const
282 283
  {
    TRACE_SANITIZE (this);
284 285 286
    if (!length.sanitize (c) ||
	length < min_size ||
	!c->check_range (this, length))
287 288
      return_trace (false);

289
    return_trace (dispatch (c));
290 291
  }

292 293
protected:
  HBUINT32	length;
294 295
  HBUINT32	coverage;
  HBUINT32	tupleCount;
296 297
  union {
  KerxSubTableFormat0	format0;
298
  KerxSubTableFormat1	format1;
299 300 301 302
  KerxSubTableFormat2	format2;
  KerxSubTableFormat4	format4;
  KerxSubTableFormat6	format6;
  } u;
303 304
public:
  DEFINE_SIZE_MIN (12);
305 306
};

307 308 309 310 311

/*
 * The 'kerx' Table
 */

312 313
struct kerx
{
314
  static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
315

316 317 318
  inline bool has_data (void) const { return version != 0; }

  inline void apply (hb_aat_apply_context_t *c) const
319
  {
320 321 322 323 324
    c->set_lookup_index (0);
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
    {
B
Behdad Esfahbod 已提交
325 326
      bool reverse;

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

B
Behdad Esfahbod 已提交
330 331
      if (HB_DIRECTION_IS_VERTICAL (c->buffer->props.direction) !=
	  bool (table->coverage & KerxTable::Vertical))
B
Behdad Esfahbod 已提交
332
	goto skip;
B
Behdad Esfahbod 已提交
333

B
Behdad Esfahbod 已提交
334
      reverse = bool (table->coverage & KerxTable::Backwards) !=
B
Behdad Esfahbod 已提交
335 336 337
		HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);

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

      if (reverse)
B
Behdad Esfahbod 已提交
341
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
342

343 344
      c->sanitizer.set_object (*table);

B
Behdad Esfahbod 已提交
345 346 347 348
      /* 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? */
349
      table->dispatch (c);
B
Behdad Esfahbod 已提交
350 351

      if (reverse)
B
Behdad Esfahbod 已提交
352
	c->buffer->reverse ();
B
Behdad Esfahbod 已提交
353 354 355 356

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

    skip:
357 358
      table = &StructAfter<KerxTable> (*table);
    }
359 360
  }

361
  inline bool sanitize (hb_sanitize_context_t *c) const
362
  {
363
    TRACE_SANITIZE (this);
364 365
    if (!version.sanitize (c) || version < 2 ||
	!tableCount.sanitize (c))
366
      return_trace (false);
367

368 369 370
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
371
    {
372 373
      if (!table->sanitize (c))
	return_trace (false);
374
      table = &StructAfter<KerxTable> (*table);
375 376 377 378 379 380
    }

    return_trace (true);
  }

  protected:
381 382 383 384 385 386 387 388
  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. */

389
  public:
390
  DEFINE_SIZE_MIN (8);
391 392 393 394 395 396
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */