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

B
Behdad Esfahbod 已提交
62 63 64
    hb_kern_machine_t<KerxSubTableFormat0> machine (*this);

    machine.kern (c->font, c->buffer, c->plan->kern_mask);
65 66 67

    return_trace (true);
  }
68 69 70 71

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

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

struct KerxSubTableFormat1
{
84 85 86 87 88 89 90 91 92
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */

    return_trace (true);
  }

93 94
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
95
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
96 97
    return_trace (likely (c->check_struct (this) &&
			  stateHeader.sanitize (c)));
98 99 100
  }

  protected:
101 102
  StateTable<HBUINT16>		stateHeader;
  LOffsetTo<ArrayOf<HBUINT16> >	valueTable;
103
  public:
104
  DEFINE_SIZE_STATIC (20);
105 106 107 108
};

struct KerxSubTableFormat2
{
B
Behdad Esfahbod 已提交
109 110
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
			  const char *end, unsigned int num_glyphs) const
111
  {
B
Behdad Esfahbod 已提交
112 113
    unsigned int l = *(this+leftClassTable).get_value (left, num_glyphs);
    unsigned int r = *(this+rightClassTable).get_value (right, num_glyphs);
114
    unsigned int offset = l + r;
115
    const FWORD *arr = &(this+array);
B
Behdad Esfahbod 已提交
116 117
    if (unlikely ((const void *) arr < (const void *) this || (const void *) arr >= (const void *) end))
      return 0;
118 119 120 121 122 123
    const FWORD *v = &StructAtOffset<FWORD> (arr, offset);
    if (unlikely ((const void *) v < (const void *) arr || (const void *) (v + 1) > (const void *) end))
      return 0;
    return *v;
  }

124 125 126 127
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

B
Behdad Esfahbod 已提交
128
    accelerator_t accel (*this,
B
Behdad Esfahbod 已提交
129
			 c->sanitizer.end, /* XXX Use SubTable length? */
B
Behdad Esfahbod 已提交
130 131 132
			 c->face->get_num_glyphs ());
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
133 134 135 136

    return_trace (true);
  }

137 138 139
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
140 141 142 143 144
    return_trace (likely (c->check_struct (this) &&
			  rowWidth.sanitize (c) &&
			  leftClassTable.sanitize (c, this) &&
			  rightClassTable.sanitize (c, this) &&
			  array.sanitize (c, this)));
145 146
  }

B
Behdad Esfahbod 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  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);
    }
  };

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

struct KerxSubTableFormat4
{
180 181 182 183 184 185 186 187 188
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */

    return_trace (true);
  }

189 190 191
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
192 193 194

    /* TODO */
    return_trace (likely (c->check_struct (this)));
195 196 197 198
  }

  protected:
  public:
B
Behdad Esfahbod 已提交
199
  DEFINE_SIZE_STATIC (1);
200 201 202 203
};

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

    /* TODO */

    return_trace (true);
  }

213 214
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
215
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
216 217 218 219 220
    return_trace (likely (c->check_struct (this) &&
			  rowIndexTable.sanitize (c, this) &&
			  columnIndexTable.sanitize (c, this) &&
			  kerningArray.sanitize (c, this) &&
			  kerningVector.sanitize (c, this)));
221 222 223
  }

  protected:
224 225 226
  HBUINT32	flags;
  HBUINT16	rowCount;
  HBUINT16	columnCount;
227 228 229 230
  LOffsetTo<Lookup<HBUINT16> >	rowIndexTable;
  LOffsetTo<Lookup<HBUINT16> >	columnIndexTable;
  LOffsetTo<Lookup<HBUINT16> >	kerningArray;
  LOffsetTo<Lookup<HBUINT16> >	kerningVector;
231
  public:
232
  DEFINE_SIZE_STATIC (24);
233 234
};

235 236
struct KerxTable
{
B
Behdad Esfahbod 已提交
237
  friend struct kerx;
B
Behdad Esfahbod 已提交
238

239 240 241 242
  inline unsigned int get_size (void) const { return length; }
  inline unsigned int get_type (void) const { return coverage & SubtableType; }

  enum Coverage
243
  {
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    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. */
    ProcessDirection	= 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. */
  };

  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 ());
    }
269 270
  }

271
  inline bool sanitize (hb_sanitize_context_t *c) const
272 273
  {
    TRACE_SANITIZE (this);
274 275 276
    if (!length.sanitize (c) ||
	length < min_size ||
	!c->check_range (this, length))
277 278
      return_trace (false);

279
    return_trace (dispatch (c));
280 281
  }

282 283
protected:
  HBUINT32	length;
284 285
  HBUINT32	coverage;
  HBUINT32	tupleCount;
286 287
  union {
  KerxSubTableFormat0	format0;
288
  KerxSubTableFormat1	format1;
289 290 291 292
  KerxSubTableFormat2	format2;
  KerxSubTableFormat4	format4;
  KerxSubTableFormat6	format6;
  } u;
293 294
public:
  DEFINE_SIZE_MIN (12);
295 296
};

297
struct SubtableXXX
298 299 300 301
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
302
    return_trace (likely (c->check_struct (this)));
303
  }
304

305 306 307 308 309 310 311
  protected:
  HBUINT32	length;
  HBUINT32	coverage;
  HBUINT32	tupleCount;
  public:
  DEFINE_SIZE_STATIC (12);
};
312

313 314 315 316 317

/*
 * The 'kerx' Table
 */

318 319
struct kerx
{
320
  static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
321

322 323 324
  inline bool has_data (void) const { return version != 0; }

  inline void apply (hb_aat_apply_context_t *c) const
325
  {
326 327 328 329 330
    c->set_lookup_index (0);
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
    {
B
Behdad Esfahbod 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
      bool reverse;

      if (HB_DIRECTION_IS_VERTICAL (c->buffer->props.direction) !=
	  bool (table->coverage & KerxTable::Vertical))
        goto skip;

      if (table->coverage & KerxTable::CrossStream)
        goto skip; /* We do NOT handle cross-stream kerning. */

      reverse = bool (table->coverage & KerxTable::ProcessDirection) !=
		HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);

      if (!c->buffer->message (c->font, "start kerx subtable %d", c->lookup_index))
        goto skip;

      if (reverse)
        c->buffer->reverse ();

B
Behdad Esfahbod 已提交
349
      /* XXX Reverse-kern is not working yet... */
350
      table->dispatch (c);
B
Behdad Esfahbod 已提交
351 352 353 354 355 356 357

      if (reverse)
        c->buffer->reverse ();

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

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

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

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

    return_trace (true);
  }

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

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

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */