hb-aat-layout-morx-table.hh 14.3 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_AAT_LAYOUT_MORX_TABLE_HH
#define HB_AAT_LAYOUT_MORX_TABLE_HH

30 31
#include "hb-open-type-private.hh"
#include "hb-aat-layout-common-private.hh"
32 33 34 35 36 37 38 39 40 41 42

#define HB_AAT_TAG_MORX HB_TAG('m','o','r','x')


namespace AAT {

using namespace OT;


struct RearrangementSubtable
{
43 44 45 46 47 48 49 50 51 52 53
  enum Flags {
    MarkFirst	= 0x8000,	/* If set, make the current glyph the first
				 * glyph to be rearranged. */
    DontAdvance	= 0x4000,	/* If set, don't advance to the next glyph
				 * before going to the new state. This means
				 * that the glyph index doesn't change, even
				 * if the glyph at that index has changed. */
    MarkLast	= 0x2000,	/* If set, make the current glyph the last
				 * glyph to be rearranged. */
    Reserved	= 0x1FF0,	/* These bits are reserved and should be set to 0. */
    Verb	= 0x000F,	/* The type of rearrangement specified. */
54 55
  };

56 57 58
  inline bool apply (hb_apply_context_t *c) const
  {
    TRACE_APPLY (this);
59 60 61 62 63

    bool ret = false;
    unsigned int num_glyphs = c->face->get_num_glyphs ();

    unsigned int state = 0;
64 65
    unsigned int last_zero = 0;
    unsigned int last_zero_before_start = 0;
66 67 68 69 70 71
    unsigned int start = 0;
    unsigned int end = 0;

    hb_glyph_info_t *info = c->buffer->info;
    unsigned int count = c->buffer->len;

72
    for (unsigned int i = 0; i <= count; i++)
73
    {
74 75 76
      if (!state)
	last_zero = i;

77 78 79
      unsigned int klass = i < count ?
			   machine.get_class (info[i].codepoint, num_glyphs) :
			   0 /* End of text */;
80
      const Entry<void> *entry = machine.get_entryZ (state, klass);
81 82 83 84 85 86
      if (unlikely (!entry))
        break;

      unsigned int flags = entry->flags;

      if (flags & MarkFirst)
87
      {
88
	start = i;
89 90
	last_zero_before_start = last_zero;
      }
91

92 93
      if (flags & MarkLast)
	end = MIN (i + 1, count);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

      if ((flags & Verb) && start < end)
      {
	/* The following map has two nibbles, for start-side
	 * and end-side. Values of 0,1,2 mean move that many
	 * to the other side. Value of 3 means move 2 and
	 * flip them. */
	static const unsigned char map[16] =
	{
	  0x00,	/* 0	no change */
	  0x10,	/* 1	Ax => xA */
	  0x01,	/* 2	xD => Dx */
	  0x11,	/* 3	AxD => DxA */
	  0x20,	/* 4	ABx => xAB */
	  0x30,	/* 5	ABx => xBA */
	  0x02,	/* 6	xCD => CDx */
	  0x03,	/* 7	xCD => DCx */
	  0x12,	/* 8	AxCD => CDxA */
	  0x13,	/* 9	AxCD => DCxA */
	  0x21,	/* 10	ABxD => DxAB */
	  0x31,	/* 11	ABxD => DxBA */
	  0x22,	/* 12	ABxCD => CDxAB */
	  0x32,	/* 13	ABxCD => CDxBA */
	  0x23,	/* 14	ABxCD => DCxAB */
	  0x33,	/* 15	ABxCD => DCxBA */
	};

	unsigned int m = map[flags & Verb];
	unsigned int l = MIN<unsigned int> (2, m >> 4);
	unsigned int r = MIN<unsigned int> (2, m & 0x0F);
	bool reverse_l = 3 == (m >> 4);
	bool reverse_r = 3 == (m & 0x0F);

	if (end - start >= l + r)
	{
129
	  c->buffer->unsafe_to_break (last_zero_before_start, MIN (i + 1, count));
130 131 132 133 134 135 136
	  c->buffer->merge_clusters (start, end);

	  hb_glyph_info_t buf[4];
	  memcpy (buf, info + start, l * sizeof (buf[0]));
	  memcpy (buf + 2, info + end - r, r * sizeof (buf[0]));

	  if (l != r)
137
	    memmove (info + start + r, info + start + l, (end - start - l - r) * sizeof (buf[0]));
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

	  memcpy (info + start, buf + 2, r * sizeof (buf[0]));
	  memcpy (info + end - l, buf, l * sizeof (buf[0]));
	  if (reverse_l)
	  {
	    buf[0] = info[end - 1];
	    info[end - 1] = info[end - 2];
	    info[end - 2] = buf[0];
	  }
	  if (reverse_r)
	  {
	    buf[0] = info[start];
	    info[start] = info[start + 1];
	    info[start + 1] = buf[0];
	  }
	}
      }

B
Behdad Esfahbod 已提交
156
      if (flags & DontAdvance)
157
        i--; /* TODO Detect infinite loop. */
158 159 160 161 162

      state = entry->newState;
    }

    return_trace (ret);
163 164
  }

165 166 167
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
168
    return_trace (machine.sanitize (c));
169
  }
170 171

  protected:
B
Behdad Esfahbod 已提交
172
  StateTable<void>	machine;
173 174
  public:
  DEFINE_SIZE_MIN (2);
175 176 177 178
};

struct ContextualSubtable
{
179 180 181 182 183 184 185 186 187 188 189 190 191 192
  enum Flags {
    SetMark	= 0x8000,	/* If set, make the current glyph the marked glyph. */
    DontAdvance	= 0x4000,	/* If set, don't advance to the next glyph before
				 * going to the new state. */
    Reserved	= 0x3FFF,	/* These bits are reserved and should be set to 0. */
  };

  /* XXX the following is different in mort: it's directly index to sublookups. */
  struct EntryData
  {
    HBUINT16	markIndex;	/* Index of the substitution table for the
				 * marked glyph (use 0xFFFF for none). */
    HBUINT16	currentIndex;	/* Index of the substitution table for the
				 * current glyph (use 0xFFFF for none). */
B
Behdad Esfahbod 已提交
193
    public:
194
    DEFINE_SIZE_STATIC (4);
195 196
  };

197 198 199
  inline bool apply (hb_apply_context_t *c) const
  {
    TRACE_APPLY (this);
200 201 202 203

    bool ret = false;
    unsigned int num_glyphs = c->face->get_num_glyphs ();

B
Behdad Esfahbod 已提交
204
    const UnsizedOffsetListOf<Lookup<GlyphID>, HBUINT32> &subs = this+substitutionTables;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

    unsigned int state = 0;
    unsigned int last_zero = 0;
    unsigned int last_zero_before_mark = 0;
    unsigned int mark = 0;

    hb_glyph_info_t *info = c->buffer->info;
    unsigned int count = c->buffer->len;

    for (unsigned int i = 0; i <= count; i++)
    {
      if (!state)
	last_zero = i;

      unsigned int klass = i < count ?
			   machine.get_class (info[i].codepoint, num_glyphs) :
			   0 /* End of text */;
222
      const Entry<EntryData> *entry = machine.get_entryZ (state, klass);
223 224 225 226 227 228 229 230 231 232 233 234 235
      if (unlikely (!entry))
        break;

      unsigned int flags = entry->flags;

      if (flags & SetMark)
      {
	mark = i;
	last_zero_before_mark = last_zero;
      }

      if (entry->data.markIndex != 0xFFFF)
      {
236
	const Lookup<GlyphID> &lookup = subs[entry->data.markIndex];
237 238 239 240 241 242 243 244 245 246
	const GlyphID *replacement = lookup.get_value (info[mark].codepoint, num_glyphs);
	if (replacement)
	{
	  c->buffer->unsafe_to_break (last_zero_before_mark, MIN (i + 1, count));
	  info[mark].codepoint = *replacement;
	  ret = true;
	}
      }
      if (entry->data.currentIndex != 0xFFFF)
      {
247
	const Lookup<GlyphID> &lookup = subs[entry->data.currentIndex];
248 249 250 251 252 253 254 255 256
	const GlyphID *replacement = lookup.get_value (info[i].codepoint, num_glyphs);
	if (replacement)
	{
	  c->buffer->unsafe_to_break (last_zero, MIN (i + 1, count));
	  info[i].codepoint = *replacement;
	  ret = true;
	}
      }

B
Behdad Esfahbod 已提交
257
      if (flags & DontAdvance)
258
        i--; /* TODO Detect infinite loop. */
259 260 261 262 263

      state = entry->newState;
    }

    return_trace (ret);
264 265
  }

266 267 268
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

    unsigned int num_entries;
    if (unlikely (!machine.sanitize (c, &num_entries))) return false;

    unsigned int num_lookups = 0;

    const Entry<EntryData> *entries = machine.get_entries ();
    for (unsigned int i = 0; i < num_entries; i++)
    {
      const EntryData &data = entries[i].data;

      num_lookups = MAX<unsigned int> (num_lookups, 1 + data.markIndex);
      num_lookups = MAX<unsigned int> (num_lookups, 1 + data.currentIndex);
    }

    return_trace (substitutionTables.sanitize (c, this, num_lookups));
285
  }
286 287

  protected:
B
Behdad Esfahbod 已提交
288 289 290
  StateTable<EntryData>	machine;
  OffsetTo<UnsizedOffsetListOf<Lookup<GlyphID>, HBUINT32>, HBUINT32>
			substitutionTables;
291 292
  public:
  DEFINE_SIZE_MIN (2);
293 294 295 296
};

struct LigatureSubtable
{
297 298 299 300 301 302 303
  inline bool apply (hb_apply_context_t *c) const
  {
    TRACE_APPLY (this);
    /* TODO */
    return_trace (false);
  }

304 305 306 307
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    /* TODO */
308
    return_trace (true);
309 310 311 312 313
  }
};

struct NoncontextualSubtable
{
314 315 316
  inline bool apply (hb_apply_context_t *c) const
  {
    TRACE_APPLY (this);
317

318
    bool ret = false;
319 320 321 322
    unsigned int num_glyphs = c->face->get_num_glyphs ();

    hb_glyph_info_t *info = c->buffer->info;
    unsigned int count = c->buffer->len;
323 324 325 326 327 328 329 330 331
    for (unsigned int i = 0; i < count; i++)
    {
      const GlyphID *replacement = substitute.get_value (info[i].codepoint, num_glyphs);
      if (replacement)
      {
	info[i].codepoint = *replacement;
	ret = true;
      }
    }
332

333 334 335
    return_trace (ret);
  }

336 337 338
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
339
    return_trace (substitute.sanitize (c));
340
  }
B
Behdad Esfahbod 已提交
341 342 343 344 345

  protected:
  Lookup<GlyphID>	substitute;
  public:
  DEFINE_SIZE_MIN (2);
346 347 348 349
};

struct InsertionSubtable
{
350 351 352 353 354 355 356
  inline bool apply (hb_apply_context_t *c) const
  {
    TRACE_APPLY (this);
    /* TODO */
    return_trace (false);
  }

357 358 359 360
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    /* TODO */
361
    return_trace (true);
362 363 364 365 366 367 368 369 370 371 372 373 374
  }
};


struct Feature
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this));
  }

  public:
B
Behdad Esfahbod 已提交
375 376 377
  HBUINT16	featureType;	/* The type of feature. */
  HBUINT16	featureSetting;	/* The feature's setting (aka selector). */
  HBUINT32	enableFlags;	/* Flags for the settings that this feature
378
				 * and setting enables. */
B
Behdad Esfahbod 已提交
379
  HBUINT32	disableFlags;	/* Complement of flags for the settings that this
380 381 382 383 384 385 386 387 388
				 * feature and setting disable. */

  public:
  DEFINE_SIZE_STATIC (12);
};


struct ChainSubtable
{
B
Behdad Esfahbod 已提交
389
  friend struct Chain;
390 391 392 393 394 395 396 397 398 399 400 401

  inline unsigned int get_size (void) const { return length; }
  inline unsigned int get_type (void) const { return coverage & 0xFF; }

  enum Type {
    Rearrangement	= 0,
    Contextual		= 1,
    Ligature		= 2,
    Noncontextual	= 4,
    Insertion		= 5
  };

402 403 404 405 406
  inline void apply (hb_apply_context_t *c) const
  {
    dispatch (c);
  }

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
  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 Rearrangement:		return_trace (c->dispatch (u.rearrangement));
    case Contextual:		return_trace (c->dispatch (u.contextual));
    case Ligature:		return_trace (c->dispatch (u.ligature));
    case Noncontextual:		return_trace (c->dispatch (u.noncontextual));
    case Insertion:		return_trace (c->dispatch (u.insertion));
    default:			return_trace (c->default_return_value ());
    }
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    if (!length.sanitize (c) ||
	length < min_size ||
	!c->check_range (this, length))
      return_trace (false);

    return_trace (dispatch (c));
  }

  protected:
B
Behdad Esfahbod 已提交
434 435 436
  HBUINT32	length;		/* Total subtable length, including this header. */
  HBUINT32	coverage;	/* Coverage flags and subtable type. */
  HBUINT32	subFeatureFlags;/* The 32-bit mask identifying which subtable this is. */
437
  union {
B
Behdad Esfahbod 已提交
438 439 440 441 442
  RearrangementSubtable		rearrangement;
  ContextualSubtable		contextual;
  LigatureSubtable		ligature;
  NoncontextualSubtable		noncontextual;
  InsertionSubtable		insertion;
443 444
  } u;
  public:
B
Behdad Esfahbod 已提交
445
  DEFINE_SIZE_MIN (2 * sizeof (HBUINT32) + 4);
446 447 448 449
};

struct Chain
{
450 451
  inline void apply (hb_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
452
    const ChainSubtable *subtable = &StructAtOffset<ChainSubtable> (featureZ, featureZ[0].static_size * featureCount);
453 454 455 456
    unsigned int count = subtableCount;
    for (unsigned int i = 0; i < count; i++)
    {
      subtable->apply (c);
B
Behdad Esfahbod 已提交
457
      subtable = &StructAfter<ChainSubtable> (*subtable);
458 459
    }
  }
460 461 462 463 464 465 466 467 468 469 470 471 472 473

  inline unsigned int get_size (void) const { return length; }

  inline bool sanitize (hb_sanitize_context_t *c, unsigned int major) const
  {
    TRACE_SANITIZE (this);
    if (!length.sanitize (c) ||
	length < min_size ||
	!c->check_range (this, length))
      return_trace (false);

    if (!c->check_array (featureZ, featureZ[0].static_size, featureCount))
      return_trace (false);

B
Behdad Esfahbod 已提交
474
    const ChainSubtable *subtable = &StructAtOffset<ChainSubtable> (featureZ, featureZ[0].static_size * featureCount);
475 476 477 478 479
    unsigned int count = subtableCount;
    for (unsigned int i = 0; i < count; i++)
    {
      if (!subtable->sanitize (c))
	return_trace (false);
B
Behdad Esfahbod 已提交
480
      subtable = &StructAfter<ChainSubtable> (*subtable);
481 482 483 484 485 486
    }

    return_trace (true);
  }

  protected:
B
Behdad Esfahbod 已提交
487 488
  HBUINT32	defaultFlags;	/* The default specification for subtables. */
  HBUINT32	length;		/* Total byte count, including this header. */
B
Behdad Esfahbod 已提交
489 490
  HBUINT32	featureCount;	/* Number of feature subtable entries. */
  HBUINT32	subtableCount;	/* The number of subtables in the chain. */
491

B
Behdad Esfahbod 已提交
492 493
  Feature	featureZ[VAR];	/* Features. */
  ChainSubtable	subtableX[VAR];	/* Subtables. */
494 495 496
  // subtableGlyphCoverageArray if major == 3

  public:
B
Behdad Esfahbod 已提交
497
  DEFINE_SIZE_MIN (8 + 2 * sizeof (HBUINT32));
498 499 500 501 502 503 504
};


/*
 * The 'mort'/'morx' Tables
 */

B
Behdad Esfahbod 已提交
505
struct morx
506
{
B
Behdad Esfahbod 已提交
507
  static const hb_tag_t tableTag = HB_AAT_TAG_MORX;
508

509 510
  inline void apply (hb_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
511
    const Chain *chain = chains;
512 513 514 515
    unsigned int count = chainCount;
    for (unsigned int i = 0; i < count; i++)
    {
      chain->apply (c);
B
Behdad Esfahbod 已提交
516
      chain = &StructAfter<Chain> (*chain);
517 518 519
    }
  }

520 521 522 523
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    if (!version.sanitize (c) ||
B
Behdad Esfahbod 已提交
524
	(version.major >> (sizeof (HBUINT32) == 4 ? 1 : 0)) != 1 ||
525 526 527
	!chainCount.sanitize (c))
      return_trace (false);

B
Behdad Esfahbod 已提交
528
    const Chain *chain = chains;
529 530 531 532 533
    unsigned int count = chainCount;
    for (unsigned int i = 0; i < count; i++)
    {
      if (!chain->sanitize (c, version.major))
	return_trace (false);
B
Behdad Esfahbod 已提交
534
      chain = &StructAfter<Chain> (*chain);
535 536 537 538 539 540 541 542
    }

    return_trace (true);
  }

  protected:
  FixedVersion<>version;	/* Version number of the glyph metamorphosis table.
				 * 1 for mort, 2 or 3 for morx. */
B
Behdad Esfahbod 已提交
543
  HBUINT32	chainCount;	/* Number of metamorphosis chains contained in this
544
				 * table. */
B
Behdad Esfahbod 已提交
545
  Chain		chains[VAR];	/* Chains. */
546 547 548 549 550 551 552 553 554

  public:
  DEFINE_SIZE_MIN (8);
};

} /* namespace AAT */


#endif /* HB_AAT_LAYOUT_MORX_TABLE_HH */