hb-ot-kern-table.hh 13.1 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
/*
 * 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

30
#include "hb-open-type.hh"
31 32 33 34 35 36 37
#include "hb-ot-shape.hh"
#include "hb-ot-layout-gsubgpos.hh"


template <typename Driver>
struct hb_kern_machine_t
{
38
  hb_kern_machine_t (const Driver &driver_) : driver (driver_) {}
39

40 41 42
  inline void kern (hb_font_t *font,
		    hb_buffer_t  *buffer,
		    hb_mask_t kern_mask) const
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  {
    OT::hb_ot_apply_context_t c (1, font, buffer);
    c.set_lookup_mask (kern_mask);
    c.set_lookup_props (OT::LookupFlag::IgnoreMarks);
    OT::hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c.iter_input;
    skippy_iter.init (&c);

    bool horizontal = HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction);
    unsigned int count = buffer->len;
    hb_glyph_info_t *info = buffer->info;
    hb_glyph_position_t *pos = buffer->pos;
    for (unsigned int idx = 0; idx < count;)
    {
      if (!(info[idx].mask & kern_mask))
      {
	idx++;
	continue;
      }

      skippy_iter.reset (idx, 1);
      if (!skippy_iter.next ())
      {
	idx++;
	continue;
      }

      unsigned int i = idx;
      unsigned int j = skippy_iter.idx;
B
Behdad Esfahbod 已提交
71 72
      hb_position_t kern1, kern2;

73 74 75
      hb_position_t kern = driver.get_kerning (info[i].codepoint,
					       info[j].codepoint);

B
Behdad Esfahbod 已提交
76 77 78 79 80 81

      if (likely (!kern))
        goto skip;

      kern1 = kern >> 1;
      kern2 = kern - kern1;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

      if (horizontal)
      {
	pos[i].x_advance += kern1;
	pos[j].x_advance += kern2;
	pos[j].x_offset += kern2;
      }
      else
      {
	pos[i].y_advance += kern1;
	pos[j].y_advance += kern2;
	pos[j].y_offset += kern2;
      }

      buffer->unsafe_to_break (i, j + 1);

B
Behdad Esfahbod 已提交
98
    skip:
99 100 101 102
      idx = skippy_iter.idx;
    }
  }

103
  const Driver &driver;
104 105
};

106 107 108

/*
 * kern -- Kerning
109 110
 * https://docs.microsoft.com/en-us/typography/opentype/spec/kern
 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kern.html
111 112 113
 */
#define HB_OT_TAG_kern HB_TAG('k','e','r','n')

114 115 116 117

namespace OT {


B
Behdad Esfahbod 已提交
118 119 120 121 122
struct hb_glyph_pair_t
{
  hb_codepoint_t left;
  hb_codepoint_t right;
};
123

B
Behdad Esfahbod 已提交
124
struct KernPair
B
Behdad Esfahbod 已提交
125
{
B
Behdad Esfahbod 已提交
126 127 128 129
  inline int get_kerning (void) const
  { return value; }

  inline int cmp (const hb_glyph_pair_t &o) const
B
Behdad Esfahbod 已提交
130
  {
B
Behdad Esfahbod 已提交
131 132 133
    int ret = left.cmp (o.left);
    if (ret) return ret;
    return right.cmp (o.right);
B
Behdad Esfahbod 已提交
134 135 136 137 138
  }

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

B
Behdad Esfahbod 已提交
142 143 144 145 146 147 148
  protected:
  GlyphID	left;
  GlyphID	right;
  FWORD		value;
  public:
  DEFINE_SIZE_STATIC (6);
};
B
Behdad Esfahbod 已提交
149

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

  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 已提交
171 172
};

173
struct KernClassTable
B
Behdad Esfahbod 已提交
174
{
175 176 177
  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 已提交
178
  {
179 180
    TRACE_SANITIZE (this);
    return_trace (firstGlyph.sanitize (c) && classes.sanitize (c));
B
Behdad Esfahbod 已提交
181 182
  }

183
  protected:
B
Behdad Esfahbod 已提交
184 185
  HBUINT16		firstGlyph;	/* First glyph in class range. */
  ArrayOf<HBUINT16>	classes;	/* Glyph classes. */
186 187 188 189 190 191
  public:
  DEFINE_SIZE_ARRAY (4, classes);
};

struct KernSubTableFormat2
{
192
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
B
Behdad Esfahbod 已提交
193
  {
B
Behdad Esfahbod 已提交
194 195 196 197 198 199
    /* This subtable is disabled.  It's not cleaer to me *exactly* where the offests are
     * based from.  I *think* they should be based from beginning of kern subtable wrapper,
     * *NOT* "this".  Since we know of no fonts that use this subtable, we are disabling
     * it.  Someday fix it and re-enable.  Better yet, find fonts that use it... Meh,
     * Windows doesn't implement it.  Maybe just remove... */
    return 0;
200
    unsigned int l = (this+leftClassTable).get_class (left);
201
    unsigned int r = (this+rightClassTable).get_class (right);
202
    unsigned int offset = l + r;
B
Behdad Esfahbod 已提交
203 204 205
    const FWORD *v = &StructAtOffset<FWORD> (&(this+array), offset);
    if (unlikely ((const char *) v < (const char *) &array ||
		  (const char *) v > (const char *) end - 2))
206 207
      return 0;
    return *v;
B
Behdad Esfahbod 已提交
208 209 210 211 212
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
213
    return_trace (true); /* Disabled.  See above. */
214 215 216 217
    return_trace (rowWidth.sanitize (c) &&
		  leftClassTable.sanitize (c, this) &&
		  rightClassTable.sanitize (c, this) &&
		  array.sanitize (c, this));
B
Behdad Esfahbod 已提交
218
  }
219 220

  protected:
B
Behdad Esfahbod 已提交
221
  HBUINT16	rowWidth;	/* The width, in bytes, of a row in the table. */
222 223 224 225 226 227 228 229 230 231 232
  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 已提交
233 234 235 236
};

struct KernSubTable
{
237
  inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end, unsigned int format) const
B
Behdad Esfahbod 已提交
238 239 240
  {
    switch (format) {
    case 0: return u.format0.get_kerning (left, right);
241
    case 2: return u.format2.get_kerning (left, right, end);
B
Behdad Esfahbod 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    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);
};

265 266 267 268 269 270 271

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 已提交
272
  inline bool is_horizontal (void) const
B
Behdad Esfahbod 已提交
273
  { return (thiz()->coverage & T::CheckFlags) == T::CheckHorizontal; }
B
Behdad Esfahbod 已提交
274 275

  inline bool is_override (void) const
B
Behdad Esfahbod 已提交
276
  { return bool (thiz()->coverage & T::Override); }
B
Behdad Esfahbod 已提交
277

278 279
  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 已提交
280

281 282
  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 已提交
283

284 285 286 287 288
  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 已提交
289
    return_trace (c->check_struct (thiz()) &&
290
		  thiz()->length >= T::min_size &&
B
Behdad Esfahbod 已提交
291
		  c->check_range (thiz(), thiz()->length) &&
292
		  thiz()->subtable.sanitize (c, thiz()->format));
293 294 295 296 297 298 299 300 301
  }
};

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

302
  inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right) const
B
Behdad Esfahbod 已提交
303 304
  {
    int v = 0;
305
    const typename T::SubTableWrapper *st = CastP<typename T::SubTableWrapper> (&thiz()->dataZ);
B
Behdad Esfahbod 已提交
306 307 308 309 310
    unsigned int count = thiz()->nTables;
    for (unsigned int i = 0; i < count; i++)
    {
      if (st->is_override ())
        v = 0;
311
      v += st->get_h_kerning (left, right, st->length + (const char *) st);
B
Behdad Esfahbod 已提交
312 313 314 315 316
      st = &StructAfter<typename T::SubTableWrapper> (*st);
    }
    return v;
  }

317 318 319 320 321 322 323
  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);

324
    const typename T::SubTableWrapper *st = CastP<typename T::SubTableWrapper> (&thiz()->dataZ);
B
Behdad Esfahbod 已提交
325
    unsigned int count = thiz()->nTables;
326 327 328 329 330 331 332 333 334 335 336 337 338
    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>
{
339
  friend struct KernTable<KernOT>;
340 341 342 343 344

  static const uint16_t VERSION = 0x0000u;

  struct SubTableWrapper : KernSubTableWrapper<SubTableWrapper>
  {
345
    friend struct KernTable<KernOT>;
346
    friend struct KernSubTableWrapper<SubTableWrapper>;
347

B
Behdad Esfahbod 已提交
348 349 350 351 352 353
    enum Coverage
    {
      Direction		= 0x01u,
      Minimum		= 0x02u,
      CrossStream	= 0x04u,
      Override		= 0x08u,
B
Behdad Esfahbod 已提交
354

B
Behdad Esfahbod 已提交
355
      Variation		= 0x00u, /* Not supported. */
B
Behdad Esfahbod 已提交
356

B
Behdad Esfahbod 已提交
357 358
      CheckFlags	= 0x07u,
      CheckHorizontal	= 0x01u
B
Behdad Esfahbod 已提交
359 360
    };

361
    protected:
B
Behdad Esfahbod 已提交
362 363 364 365
    HBUINT16	versionZ;	/* Unused. */
    HBUINT16	length;		/* Length of the subtable (including this header). */
    HBUINT8	format;		/* Subtable format. */
    HBUINT8	coverage;	/* Coverage bits. */
B
Behdad Esfahbod 已提交
366
    KernSubTable subtable;	/* Subtable data. */
367
    public:
B
Behdad Esfahbod 已提交
368
    DEFINE_SIZE_MIN (6);
369 370 371
  };

  protected:
372 373 374
  HBUINT16			version;	/* Version--0x0000u */
  HBUINT16			nTables;	/* Number of subtables in the kerning table. */
  UnsizedArrayOf<HBUINT8>	dataZ;
375
  public:
376
  DEFINE_SIZE_ARRAY (4, dataZ);
377 378 379 380
};

struct KernAAT : KernTable<KernAAT>
{
381
  friend struct KernTable<KernAAT>;
382 383 384 385 386

  static const uint32_t VERSION = 0x00010000u;

  struct SubTableWrapper : KernSubTableWrapper<SubTableWrapper>
  {
387
    friend struct KernTable<KernAAT>;
388
    friend struct KernSubTableWrapper<SubTableWrapper>;
389

B
Behdad Esfahbod 已提交
390 391 392 393 394
    enum Coverage
    {
      Direction		= 0x80u,
      CrossStream	= 0x40u,
      Variation		= 0x20u,
B
Behdad Esfahbod 已提交
395

B
Behdad Esfahbod 已提交
396
      Override		= 0x00u, /* Not supported. */
B
Behdad Esfahbod 已提交
397

B
Behdad Esfahbod 已提交
398 399
      CheckFlags	= 0xE0u,
      CheckHorizontal	= 0x00u
B
Behdad Esfahbod 已提交
400 401
    };

402
    protected:
B
Behdad Esfahbod 已提交
403 404 405 406
    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).
407
				 * This value specifies which tuple this subtable covers. */
B
Behdad Esfahbod 已提交
408
    KernSubTable subtable;	/* Subtable data. */
409
    public:
B
Behdad Esfahbod 已提交
410
    DEFINE_SIZE_MIN (8);
411 412 413
  };

  protected:
414 415 416
  HBUINT32			version;	/* Version--0x00010000u */
  HBUINT32			nTables;	/* Number of subtables in the kerning table. */
  UnsizedArrayOf<HBUINT8>	dataZ;
417
  public:
418
  DEFINE_SIZE_ARRAY (8, dataZ);
419 420 421 422 423 424
};

struct kern
{
  static const hb_tag_t tableTag = HB_OT_TAG_kern;

425 426 427
  inline bool has_data (void) const
  { return u.version32 != 0; }

428
  inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right) const
B
Behdad Esfahbod 已提交
429 430
  {
    switch (u.major) {
431 432
    case 0: return u.ot.get_h_kerning (left, right);
    case 1: return u.aat.get_h_kerning (left, right);
B
Behdad Esfahbod 已提交
433 434 435 436
    default:return 0;
    }
  }

437 438 439
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
440
    if (!u.version32.sanitize (c)) return_trace (false);
441 442 443 444 445 446 447
    switch (u.major) {
    case 0: return_trace (u.ot.sanitize (c));
    case 1: return_trace (u.aat.sanitize (c));
    default:return_trace (true);
    }
  }

448 449
  struct accelerator_t
  {
450
    inline void init (hb_face_t *face)
451
    {
B
Behdad Esfahbod 已提交
452
      blob = hb_sanitize_context_t().reference_table<kern> (face);
453
      table = blob->as<kern> ();
454 455 456 457
    }
    inline void fini (void)
    {
      hb_blob_destroy (blob);
458 459
    }

460 461 462
    inline bool has_data (void) const
    { return table->has_data (); }

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

466 467 468
    inline int get_kerning (hb_codepoint_t first, hb_codepoint_t second) const
    { return get_h_kerning (first, second); }

469 470 471
    inline void apply (hb_font_t *font,
		       hb_buffer_t  *buffer,
		       hb_mask_t kern_mask) const
472
    {
B
Behdad Esfahbod 已提交
473
      /* We only apply horizontal kerning in this table. */
474 475 476 477 478
      if (!HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
        return;

      hb_kern_machine_t<accelerator_t> machine (*this);

479
      machine.kern (font, buffer, kern_mask);
480 481
    }

482
    private:
483
    hb_blob_t *blob;
484 485 486
    const kern *table;
  };

487 488
  protected:
  union {
489
  HBUINT32		version32;
B
Behdad Esfahbod 已提交
490
  HBUINT16		major;
491 492 493 494
  KernOT		ot;
  KernAAT		aat;
  } u;
  public:
B
Behdad Esfahbod 已提交
495
  DEFINE_SIZE_UNION (4, version32);
496 497
};

498 499
struct kern_accelerator_t : kern::accelerator_t {};

500 501 502 503
} /* namespace OT */


#endif /* HB_OT_KERN_TABLE_HH */