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 128
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */
B
Behdad Esfahbod 已提交
129 130 131 132 133 134 135
#if 0
    accelerator_t accel (*this,
			 c->blob->data + c->blob->len,
			 c->face->get_num_glyphs ());
    hb_kern_machine_t<accelerator_t> machine (accel);
    machine.kern (c->font, c->buffer, c->plan->kern_mask);
#endif
136 137 138 139

    return_trace (true);
  }

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

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

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

struct KerxSubTableFormat4
{
183 184 185 186 187 188 189 190 191
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */

    return_trace (true);
  }

192 193 194
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
195 196 197

    /* TODO */
    return_trace (likely (c->check_struct (this)));
198 199 200 201
  }

  protected:
  public:
B
Behdad Esfahbod 已提交
202
  DEFINE_SIZE_STATIC (1);
203 204 205 206
};

struct KerxSubTableFormat6
{
207 208 209 210 211 212 213 214 215
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */

    return_trace (true);
  }

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

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

238 239
struct KerxTable
{
B
Behdad Esfahbod 已提交
240
  friend struct kerx;
B
Behdad Esfahbod 已提交
241

242 243 244 245
  inline unsigned int get_size (void) const { return length; }
  inline unsigned int get_type (void) const { return coverage & SubtableType; }

  enum Coverage
246
  {
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    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 ());
    }
272 273
  }

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

282
    return_trace (dispatch (c));
283 284
  }

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

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

308 309 310 311 312 313 314
  protected:
  HBUINT32	length;
  HBUINT32	coverage;
  HBUINT32	tupleCount;
  public:
  DEFINE_SIZE_STATIC (12);
};
315

316 317 318 319 320

/*
 * The 'kerx' Table
 */

321 322
struct kerx
{
323
  static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
324

325 326 327
  inline bool has_data (void) const { return version != 0; }

  inline void apply (hb_aat_apply_context_t *c) const
328
  {
329 330 331 332 333
    c->set_lookup_index (0);
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
    {
B
Behdad Esfahbod 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
      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 已提交
352
      /* XXX Reverse-kern is not working yet... */
353
      table->dispatch (c);
B
Behdad Esfahbod 已提交
354 355 356 357 358 359 360

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

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

    skip:
361 362
      table = &StructAfter<KerxTable> (*table);
    }
363 364
  }

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

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

    return_trace (true);
  }

  protected:
385 386 387 388 389 390 391 392
  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. */

393
  public:
394
  DEFINE_SIZE_MIN (8);
395 396 397 398 399 400
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */