hb-aat-layout-kerx-table.hh 9.5 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 62 63 64 65
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */

    return_trace (true);
  }
66 67 68 69

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

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

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

    /* TODO */

    return_trace (true);
  }

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

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

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

122 123 124 125 126 127 128 129 130
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */

    return_trace (true);
  }

131 132 133
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
134 135 136 137 138
    return_trace (likely (c->check_struct (this) &&
			  rowWidth.sanitize (c) &&
			  leftClassTable.sanitize (c, this) &&
			  rightClassTable.sanitize (c, this) &&
			  array.sanitize (c, this)));
139 140 141 142
  }

  protected:
  HBUINT32	rowWidth;	/* The width, in bytes, of a row in the table. */
B
Behdad Esfahbod 已提交
143
  LOffsetTo<Lookup<HBUINT16> >
144 145
		leftClassTable;	/* Offset from beginning of this subtable to
				 * left-hand class table. */
B
Behdad Esfahbod 已提交
146
  LOffsetTo<Lookup<HBUINT16> >
147 148 149 150 151 152
		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:
153
  DEFINE_SIZE_STATIC (16);
154 155 156 157
};

struct KerxSubTableFormat4
{
158 159 160 161 162 163 164 165 166
  inline bool apply (hb_aat_apply_context_t *c) const
  {
    TRACE_APPLY (this);

    /* TODO */

    return_trace (true);
  }

167 168 169
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
170 171 172

    /* TODO */
    return_trace (likely (c->check_struct (this)));
173 174 175 176
  }

  protected:
  public:
B
Behdad Esfahbod 已提交
177
  DEFINE_SIZE_STATIC (1);
178 179 180 181
};

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

    /* TODO */

    return_trace (true);
  }

191 192
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
193
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
194 195 196 197 198
    return_trace (likely (c->check_struct (this) &&
			  rowIndexTable.sanitize (c, this) &&
			  columnIndexTable.sanitize (c, this) &&
			  kerningArray.sanitize (c, this) &&
			  kerningVector.sanitize (c, this)));
199 200 201
  }

  protected:
202 203 204
  HBUINT32	flags;
  HBUINT16	rowCount;
  HBUINT16	columnCount;
205 206 207 208
  LOffsetTo<Lookup<HBUINT16> >	rowIndexTable;
  LOffsetTo<Lookup<HBUINT16> >	columnIndexTable;
  LOffsetTo<Lookup<HBUINT16> >	kerningArray;
  LOffsetTo<Lookup<HBUINT16> >	kerningVector;
209
  public:
210
  DEFINE_SIZE_STATIC (24);
211 212
};

213 214
struct KerxTable
{
B
Behdad Esfahbod 已提交
215
  friend struct kerx;
B
Behdad Esfahbod 已提交
216

217 218 219 220
  inline unsigned int get_size (void) const { return length; }
  inline unsigned int get_type (void) const { return coverage & SubtableType; }

  enum Coverage
221
  {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    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 ());
    }
247 248
  }

249
  inline bool sanitize (hb_sanitize_context_t *c) const
250 251
  {
    TRACE_SANITIZE (this);
252 253 254
    if (!length.sanitize (c) ||
	length < min_size ||
	!c->check_range (this, length))
255 256
      return_trace (false);

257
    return_trace (dispatch (c));
258 259
  }

260 261
protected:
  HBUINT32	length;
262 263
  HBUINT32	coverage;
  HBUINT32	tupleCount;
264 265
  union {
  KerxSubTableFormat0	format0;
266
  KerxSubTableFormat1	format1;
267 268 269 270
  KerxSubTableFormat2	format2;
  KerxSubTableFormat4	format4;
  KerxSubTableFormat6	format6;
  } u;
271 272
public:
  DEFINE_SIZE_MIN (12);
273 274
};

275
struct SubtableXXX
276 277 278 279
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
E
Ebrahim Byagowi 已提交
280
    return_trace (likely (c->check_struct (this)));
281
  }
282

283 284 285 286 287 288 289
  protected:
  HBUINT32	length;
  HBUINT32	coverage;
  HBUINT32	tupleCount;
  public:
  DEFINE_SIZE_STATIC (12);
};
290

291 292 293 294 295

/*
 * The 'kerx' Table
 */

296 297
struct kerx
{
298
  static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
299

300 301 302
  inline bool has_data (void) const { return version != 0; }

  inline void apply (hb_aat_apply_context_t *c) const
303
  {
304 305 306 307 308
    c->set_lookup_index (0);
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
    {
B
Behdad Esfahbod 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
      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 已提交
327
      /* XXX Reverse-kern is not working yet... */
328
      table->dispatch (c);
B
Behdad Esfahbod 已提交
329 330 331 332 333 334 335

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

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

    skip:
336 337
      table = &StructAfter<KerxTable> (*table);
    }
338 339
  }

340
  inline bool sanitize (hb_sanitize_context_t *c) const
341
  {
342
    TRACE_SANITIZE (this);
343 344
    if (!version.sanitize (c) || version < 2 ||
	!tableCount.sanitize (c))
345
      return_trace (false);
346

347 348 349
    const KerxTable *table = &firstTable;
    unsigned int count = tableCount;
    for (unsigned int i = 0; i < count; i++)
350
    {
351 352
      if (!table->sanitize (c))
	return_trace (false);
353
      table = &StructAfter<KerxTable> (*table);
354 355 356 357 358 359
    }

    return_trace (true);
  }

  protected:
360 361 362 363 364 365 366 367
  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. */

368
  public:
369
  DEFINE_SIZE_MIN (8);
370 371 372 373 374 375
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */