hb-ot-kern-table.hh 10.8 KB
Newer Older
1 2 3 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 31 32 33
/*
 * Copyright © 2017  Google, Inc.
 *
 *  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_OT_KERN_TABLE_HH
#define HB_OT_KERN_TABLE_HH

#include "hb-open-type-private.hh"

/*
 * kern -- Kerning
34 35
 * https://docs.microsoft.com/en-us/typography/opentype/spec/kern
 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kern.html
36 37 38
 */
#define HB_OT_TAG_kern HB_TAG('k','e','r','n')

39 40 41 42

namespace OT {


B
Behdad Esfahbod 已提交
43 44 45 46 47
struct hb_glyph_pair_t
{
  hb_codepoint_t left;
  hb_codepoint_t right;
};
48

B
Behdad Esfahbod 已提交
49
struct KernPair
B
Behdad Esfahbod 已提交
50
{
B
Behdad Esfahbod 已提交
51 52 53 54
  inline int get_kerning (void) const
  { return value; }

  inline int cmp (const hb_glyph_pair_t &o) const
B
Behdad Esfahbod 已提交
55
  {
B
Behdad Esfahbod 已提交
56 57 58
    int ret = left.cmp (o.left);
    if (ret) return ret;
    return right.cmp (o.right);
B
Behdad Esfahbod 已提交
59 60 61 62 63
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
64 65
    return_trace (c->check_struct (this));
  }
B
Behdad Esfahbod 已提交
66

B
Behdad Esfahbod 已提交
67 68 69 70 71 72 73
  protected:
  GlyphID	left;
  GlyphID	right;
  FWORD		value;
  public:
  DEFINE_SIZE_STATIC (6);
};
B
Behdad Esfahbod 已提交
74

B
Behdad Esfahbod 已提交
75 76 77 78 79 80 81 82 83
struct KernSubTableFormat0
{
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
  {
    hb_glyph_pair_t pair = {left, right};
    int i = pairs.bsearch (pair);
    if (i == -1)
      return 0;
    return pairs[i].get_kerning ();
B
Behdad Esfahbod 已提交
84
  }
B
Behdad Esfahbod 已提交
85 86 87 88 89 90 91 92 93 94 95

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (pairs.sanitize (c));
  }

  protected:
  BinSearchArrayOf<KernPair> pairs;	/* Array of kerning pairs. */
  public:
  DEFINE_SIZE_ARRAY (8, pairs);
B
Behdad Esfahbod 已提交
96 97
};

98
struct KernClassTable
B
Behdad Esfahbod 已提交
99
{
100 101 102
  inline unsigned int get_class (hb_codepoint_t g) const { return classes[g - firstGlyph]; }

  inline bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
103
  {
104 105
    TRACE_SANITIZE (this);
    return_trace (firstGlyph.sanitize (c) && classes.sanitize (c));
B
Behdad Esfahbod 已提交
106 107
  }

108
  protected:
B
Behdad Esfahbod 已提交
109 110
  HBUINT16		firstGlyph;	/* First glyph in class range. */
  ArrayOf<HBUINT16>	classes;	/* Glyph classes. */
111 112 113 114 115 116
  public:
  DEFINE_SIZE_ARRAY (4, classes);
};

struct KernSubTableFormat2
{
117
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
B
Behdad Esfahbod 已提交
118
  {
119
    unsigned int l = (this+leftClassTable).get_class (left);
120
    unsigned int r = (this+rightClassTable).get_class (right);
121
    unsigned int offset = l * rowWidth + r * sizeof (FWORD);
122 123 124 125 126
    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))
127 128
      return 0;
    return *v;
B
Behdad Esfahbod 已提交
129 130 131 132 133
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
134 135 136 137
    return_trace (rowWidth.sanitize (c) &&
		  leftClassTable.sanitize (c, this) &&
		  rightClassTable.sanitize (c, this) &&
		  array.sanitize (c, this));
B
Behdad Esfahbod 已提交
138
  }
139 140

  protected:
B
Behdad Esfahbod 已提交
141
  HBUINT16	rowWidth;	/* The width, in bytes, of a row in the table. */
142 143 144 145 146 147 148 149 150 151 152
  OffsetTo<KernClassTable>
		leftClassTable;	/* Offset from beginning of this subtable to
				 * left-hand class table. */
  OffsetTo<KernClassTable>
		rightClassTable;/* Offset from beginning of this subtable to
				 * right-hand class table. */
  OffsetTo<FWORD>
		array;		/* Offset from beginning of this subtable to
				 * the start of the kerning array. */
  public:
  DEFINE_SIZE_MIN (8);
B
Behdad Esfahbod 已提交
153 154 155 156
};

struct KernSubTable
{
157
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end, unsigned int format) const
B
Behdad Esfahbod 已提交
158 159 160
  {
    switch (format) {
    case 0: return u.format0.get_kerning (left, right);
161
    case 2: return u.format2.get_kerning (left, right, end);
B
Behdad Esfahbod 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    default:return 0;
    }
  }

  inline bool sanitize (hb_sanitize_context_t *c, unsigned int format) const
  {
    TRACE_SANITIZE (this);
    switch (format) {
    case 0: return_trace (u.format0.sanitize (c));
    case 2: return_trace (u.format2.sanitize (c));
    default:return_trace (true);
    }
  }

  protected:
  union {
  KernSubTableFormat0	format0;
  KernSubTableFormat2	format2;
  } u;
  public:
  DEFINE_SIZE_MIN (0);
};

185 186 187 188 189 190 191

template <typename T>
struct KernSubTableWrapper
{
  /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
  inline const T* thiz (void) const { return static_cast<const T *> (this); }

B
Behdad Esfahbod 已提交
192 193 194 195 196 197
  inline bool is_horizontal (void) const
  { return (thiz()->coverage & T::COVERAGE_CHECK_FLAGS) == T::COVERAGE_CHECK_HORIZONTAL; }

  inline bool is_override (void) const
  { return bool (thiz()->coverage & T::COVERAGE_OVERRIDE_FLAG); }

198 199
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
  { return thiz()->subtable.get_kerning (left, right, end, thiz()->format); }
B
Behdad Esfahbod 已提交
200

201 202
  inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
  { return is_horizontal () ? get_kerning (left, right, end) : 0; }
B
Behdad Esfahbod 已提交
203

204 205 206 207 208
  inline unsigned int get_size (void) const { return thiz()->length; }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
209
    return_trace (c->check_struct (thiz()) &&
210
		  thiz()->length >= T::min_size &&
B
Behdad Esfahbod 已提交
211
		  c->check_array (thiz(), 1, thiz()->length) &&
212
		  thiz()->subtable.sanitize (c, thiz()->format));
213 214 215 216 217 218 219 220 221
  }
};

template <typename T>
struct KernTable
{
  /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
  inline const T* thiz (void) const { return static_cast<const T *> (this); }

222
  inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right, unsigned int table_length) const
B
Behdad Esfahbod 已提交
223 224 225 226 227 228 229 230
  {
    int v = 0;
    const typename T::SubTableWrapper *st = CastP<typename T::SubTableWrapper> (thiz()->data);
    unsigned int count = thiz()->nTables;
    for (unsigned int i = 0; i < count; i++)
    {
      if (st->is_override ())
        v = 0;
231
      v += st->get_h_kerning (left, right, table_length + (const char *) this);
B
Behdad Esfahbod 已提交
232 233 234 235 236
      st = &StructAfter<typename T::SubTableWrapper> (*st);
    }
    return v;
  }

237 238 239 240 241 242 243
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    if (unlikely (!c->check_struct (thiz()) ||
		  thiz()->version != T::VERSION))
      return_trace (false);

B
Behdad Esfahbod 已提交
244 245
    const typename T::SubTableWrapper *st = CastP<typename T::SubTableWrapper> (thiz()->data);
    unsigned int count = thiz()->nTables;
246 247 248 249 250 251 252 253 254 255 256 257 258
    for (unsigned int i = 0; i < count; i++)
    {
      if (unlikely (!st->sanitize (c)))
	return_trace (false);
      st = &StructAfter<typename T::SubTableWrapper> (*st);
    }

    return_trace (true);
  }
};

struct KernOT : KernTable<KernOT>
{
259
  friend struct KernTable<KernOT>;
260 261 262 263 264

  static const uint16_t VERSION = 0x0000u;

  struct SubTableWrapper : KernSubTableWrapper<SubTableWrapper>
  {
265
    friend struct KernSubTableWrapper<SubTableWrapper>;
266

B
Behdad Esfahbod 已提交
267 268 269 270 271 272 273 274 275 276 277 278
    enum coverage_flags_t {
      COVERAGE_DIRECTION_FLAG	= 0x01u,
      COVERAGE_MINIMUM_FLAG	= 0x02u,
      COVERAGE_CROSSSTREAM_FLAG	= 0x04u,
      COVERAGE_OVERRIDE_FLAG	= 0x08u,

      COVERAGE_VARIATION_FLAG	= 0x00u, /* Not supported. */

      COVERAGE_CHECK_FLAGS	= 0x07u,
      COVERAGE_CHECK_HORIZONTAL	= 0x01u
    };

279
    protected:
B
Behdad Esfahbod 已提交
280 281 282 283
    HBUINT16	versionZ;	/* Unused. */
    HBUINT16	length;		/* Length of the subtable (including this header). */
    HBUINT8	format;		/* Subtable format. */
    HBUINT8	coverage;	/* Coverage bits. */
B
Behdad Esfahbod 已提交
284
    KernSubTable subtable;	/* Subtable data. */
285
    public:
B
Behdad Esfahbod 已提交
286
    DEFINE_SIZE_MIN (6);
287 288 289
  };

  protected:
B
Behdad Esfahbod 已提交
290 291 292
  HBUINT16	version;	/* Version--0x0000u */
  HBUINT16	nTables;	/* Number of subtables in the kerning table. */
  HBUINT8		data[VAR];
293 294 295 296 297 298
  public:
  DEFINE_SIZE_ARRAY (4, data);
};

struct KernAAT : KernTable<KernAAT>
{
299
  friend struct KernTable<KernAAT>;
300 301 302 303 304

  static const uint32_t VERSION = 0x00010000u;

  struct SubTableWrapper : KernSubTableWrapper<SubTableWrapper>
  {
305
    friend struct KernSubTableWrapper<SubTableWrapper>;
306

B
Behdad Esfahbod 已提交
307 308 309 310 311 312 313 314 315 316 317
    enum coverage_flags_t {
      COVERAGE_DIRECTION_FLAG	= 0x80u,
      COVERAGE_CROSSSTREAM_FLAG	= 0x40u,
      COVERAGE_VARIATION_FLAG	= 0x20u,

      COVERAGE_OVERRIDE_FLAG	= 0x00u, /* Not supported. */

      COVERAGE_CHECK_FLAGS	= 0xE0u,
      COVERAGE_CHECK_HORIZONTAL	= 0x00u
    };

318
    protected:
B
Behdad Esfahbod 已提交
319 320 321 322
    HBUINT32	length;		/* Length of the subtable (including this header). */
    HBUINT8	coverage;	/* Coverage bits. */
    HBUINT8	format;		/* Subtable format. */
    HBUINT16	tupleIndex;	/* The tuple index (used for variations fonts).
323
				 * This value specifies which tuple this subtable covers. */
B
Behdad Esfahbod 已提交
324
    KernSubTable subtable;	/* Subtable data. */
325
    public:
B
Behdad Esfahbod 已提交
326
    DEFINE_SIZE_MIN (8);
327 328 329
  };

  protected:
B
Behdad Esfahbod 已提交
330 331 332
  HBUINT32		version;	/* Version--0x00010000u */
  HBUINT32		nTables;	/* Number of subtables in the kerning table. */
  HBUINT8		data[VAR];
333 334 335 336 337 338 339 340
  public:
  DEFINE_SIZE_ARRAY (8, data);
};

struct kern
{
  static const hb_tag_t tableTag = HB_OT_TAG_kern;

341
  inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right, unsigned int table_length) const
B
Behdad Esfahbod 已提交
342 343
  {
    switch (u.major) {
344 345
    case 0: return u.ot.get_h_kerning (left, right, table_length);
    case 1: return u.aat.get_h_kerning (left, right, table_length);
B
Behdad Esfahbod 已提交
346 347 348 349
    default:return 0;
    }
  }

350 351 352 353 354 355 356 357 358 359 360
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    if (!u.major.sanitize (c)) return_trace (false);
    switch (u.major) {
    case 0: return_trace (u.ot.sanitize (c));
    case 1: return_trace (u.aat.sanitize (c));
    default:return_trace (true);
    }
  }

361 362
  struct accelerator_t
  {
363
    inline void init (hb_face_t *face)
364
    {
B
Behdad Esfahbod 已提交
365
      blob = hb_sanitize_context_t().reference_table<kern> (face);
366
      table = blob->as<kern> ();
B
Minor  
Behdad Esfahbod 已提交
367
      table_length = blob->length;
368 369 370 371
    }
    inline void fini (void)
    {
      hb_blob_destroy (blob);
372 373 374 375 376 377
    }

    inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right) const
    { return table->get_h_kerning (left, right, table_length); }

    private:
378
    hb_blob_t *blob;
379 380 381 382
    const kern *table;
    unsigned int table_length;
  };

383 384
  protected:
  union {
B
Behdad Esfahbod 已提交
385
  HBUINT16		major;
386 387 388 389 390 391 392 393 394 395 396
  KernOT		ot;
  KernAAT		aat;
  } u;
  public:
  DEFINE_SIZE_UNION (2, major);
};

} /* namespace OT */


#endif /* HB_OT_KERN_TABLE_HH */