hb-ot-layout-gpos-private.h 27.8 KB
Newer Older
B
Behdad Esfahbod 已提交
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*
 * Copyright (C) 2007,2008,2009  Red Hat, Inc.
 *
 *  This is part of HarfBuzz, an OpenType Layout engine 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.
 *
 * Red Hat Author(s): Behdad Esfahbod
 */

#ifndef HB_OT_LAYOUT_GPOS_PRIVATE_H
#define HB_OT_LAYOUT_GPOS_PRIVATE_H

#include "hb-ot-layout-gsubgpos-private.h"

/* XXX */
#include "harfbuzz-impl.h"


/* Shared Tables: ValueRecord, Anchor Table, and MarkArray */

typedef SHORT Value;
typedef Value ValueRecord[];

#if 0
struct ValueRecord {
  SHORT		xPlacement;		/* Horizontal adjustment for
					 * placement--in design units */
  SHORT		yPlacement;		/* Vertical adjustment for
					 * placement--in design units */
  SHORT		xAdvance;		/* Horizontal adjustment for
					 * advance--in design units (only used
					 * for horizontal writing) */
  SHORT		yAdvance;		/* Vertical adjustment for advance--in
					 * design units (only used for vertical
					 * writing) */
  Offset	xPlaDevice;		/* Offset to Device table for
					 * horizontal placement--measured from
					 * beginning of PosTable (may be NULL) */
  Offset	yPlaDevice;		/* Offset to Device table for vertical
					 * placement--measured from beginning
					 * of PosTable (may be NULL) */
  Offset	xAdvDevice;		/* Offset to Device table for
					 * horizontal advance--measured from
					 * beginning of PosTable (may be NULL) */
  Offset	yAdvDevice;		/* Offset to Device table for vertical
					 * advance--measured from beginning of
					 * PosTable (may be NULL) */
};
66
#endif
B
Behdad Esfahbod 已提交
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
struct ValueFormat : USHORT {

  enum {
    xPlacement	= 0x0001,	/* Includes horizontal adjustment for placement */
    yPlacement	= 0x0002,	/* Includes vertical adjustment for placement */
    xAdvance	= 0x0004,	/* Includes horizontal adjustment for advance */
    yAdvance	= 0x0008,	/* Includes vertical adjustment for advance */
    xPlaDevice	= 0x0010,	/* Includes horizontal Device table for placement */
    yPlaDevice	= 0x0020,	/* Includes vertical Device table for placement */
    xAdvDevice	= 0x0040,	/* Includes horizontal Device table for advance */
    yAdvDevice	= 0x0080,	/* Includes vertical Device table for advance */
    reserved	= 0xF000,	/* For future use */
  };

  inline unsigned int get_len () const {
    return _hb_popcount32 ((unsigned int) *this);
  }
B
Behdad Esfahbod 已提交
85

86 87 88 89 90 91 92 93 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 129 130 131 132 133 134
  const Value* apply_value (hb_ot_layout_t *layout,
			    const char     *base,
			    const Value    *values,
			    HB_Position     glyph_pos) const
  {
    unsigned int x_ppem, y_ppem;
    hb_16dot16_t x_scale, y_scale;
    unsigned int pixel_value;
    unsigned int format = *this;

    if (!format)
      return values;

    x_scale = layout->gpos_info.x_scale;
    y_scale = layout->gpos_info.y_scale;
    /* design units -> fractional pixel */
    if (format & xPlacement)
      glyph_pos->x_pos += x_scale * *(USHORT*)values++ / 0x10000;
    if (format & yPlacement)
      glyph_pos->y_pos += y_scale * *(USHORT*)values++ / 0x10000;
    if (format & xAdvance)
      glyph_pos->x_advance += x_scale * *(USHORT*)values++ / 0x10000;
    if (format & yAdvance)
      glyph_pos->y_advance += y_scale * *(USHORT*)values++ / 0x10000;

    if (HB_LIKELY (!layout->gpos_info.dvi))
    {
      x_ppem = layout->gpos_info.x_ppem;
      y_ppem = layout->gpos_info.y_ppem;
      /* pixel -> fractional pixel */
      if (format & xPlaDevice)
	glyph_pos->x_pos += (base+*(OffsetTo<Device>*)values++).get_delta (x_ppem) << 6;
      if (format & yPlaDevice)
	glyph_pos->y_pos += (base+*(OffsetTo<Device>*)values++).get_delta (y_ppem) << 6;
      if (format & xAdvDevice)
	glyph_pos->x_advance += (base+*(OffsetTo<Device>*)values++).get_delta (x_ppem) << 6;
      if (format & yAdvDevice)
	glyph_pos->y_advance += (base+*(OffsetTo<Device>*)values++).get_delta (y_ppem) << 6;
    }
    else
    {
      if (format & xPlaDevice) values++;
      if (format & yPlaDevice) values++;
      if (format & xAdvDevice) values++;
      if (format & yAdvDevice) values++;
    }

    return values;
  }
B
Behdad Esfahbod 已提交
135
};
136
ASSERT_SIZE (ValueFormat, 2);
B
Behdad Esfahbod 已提交
137 138 139 140 141 142 143 144


struct AnchorFormat1 {

  friend struct Anchor;

  private:
  inline void get_anchor (hb_ot_layout_t *layout, hb_codepoint_t glyph_id,
B
Behdad Esfahbod 已提交
145 146
			  hb_position_t *x, hb_position_t *y) const
  {
B
Behdad Esfahbod 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      *x = layout->gpos_info.x_scale * xCoordinate / 0x10000;
      *y = layout->gpos_info.y_scale * yCoordinate / 0x10000;
  }

  private:
  USHORT	format;			/* Format identifier--format = 1 */
  SHORT		xCoordinate;		/* Horizontal value--in design units */
  SHORT		yCoordinate;		/* Vertical value--in design units */
};
ASSERT_SIZE (AnchorFormat1, 6);

struct AnchorFormat2 {

  friend struct Anchor;

  private:
  inline void get_anchor (hb_ot_layout_t *layout, hb_codepoint_t glyph_id,
B
Behdad Esfahbod 已提交
164 165
			  hb_position_t *x, hb_position_t *y) const
  {
B
Behdad Esfahbod 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
      /* TODO Contour */
      *x = layout->gpos_info.x_scale * xCoordinate / 0x10000;
      *y = layout->gpos_info.y_scale * yCoordinate / 0x10000;
  }

  private:
  USHORT	format;			/* Format identifier--format = 2 */
  SHORT		xCoordinate;		/* Horizontal value--in design units */
  SHORT		yCoordinate;		/* Vertical value--in design units */
  USHORT	anchorPoint;		/* Index to glyph contour point */
};
ASSERT_SIZE (AnchorFormat2, 8);

struct AnchorFormat3 {

  friend struct Anchor;

  private:
  inline void get_anchor (hb_ot_layout_t *layout, hb_codepoint_t glyph_id,
B
Behdad Esfahbod 已提交
185 186
			  hb_position_t *x, hb_position_t *y) const
  {
B
Behdad Esfahbod 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
      /* TODO Device */
      *x += layout->gpos_info.x_scale * xCoordinate / 0x10000;
      *y += layout->gpos_info.y_scale * yCoordinate / 0x10000;
  }

  private:
  USHORT	format;			/* Format identifier--format = 3 */
  SHORT		xCoordinate;		/* Horizontal value--in design units */
  SHORT		yCoordinate;		/* Vertical value--in design units */
  OffsetTo<Device>
		xDeviceTable;		/* Offset to Device table for X
					 * coordinate-- from beginning of
					 * Anchor table (may be NULL) */
  OffsetTo<Device>
		yDeviceTable;		/* Offset to Device table for Y
					 * coordinate-- from beginning of
					 * Anchor table (may be NULL) */
};
ASSERT_SIZE (AnchorFormat3, 10);

struct Anchor {

  inline void get_anchor (hb_ot_layout_t *layout, hb_codepoint_t glyph_id,
B
Behdad Esfahbod 已提交
210 211
			  hb_position_t *x, hb_position_t *y) const
  {
B
Behdad Esfahbod 已提交
212 213 214 215 216 217 218 219 220 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 247 248 249 250 251 252 253 254 255 256 257 258 259
    *x = *y = 0;
    switch (u.format) {
    case 1: u.format1->get_anchor (layout, glyph_id, x, y); return;
    case 2: u.format2->get_anchor (layout, glyph_id, x, y); return;
    case 3: u.format3->get_anchor (layout, glyph_id, x, y); return;
    default:						    return;
    }
  }

  private:
  union {
  USHORT		format;		/* Format identifier */
  AnchorFormat1		format1[];
  AnchorFormat2		format2[];
  AnchorFormat3		format3[];
  } u;
};
ASSERT_SIZE (Anchor, 2);


struct MarkRecord {
  /* TODO */

  private:
  USHORT	klass;			/* Class defined for this mark */
  OffsetTo<Anchor>
		markAnchor;		/* Offset to Anchor table--from
					 * beginning of MarkArray table */
};
ASSERT_SIZE (MarkRecord, 4);

struct MarkArray {
  /* TODO */

  private:
  ArrayOf<MarkRecord>
		markRecord;	/* Array of MarkRecords--in Coverage order */
};
ASSERT_SIZE (MarkArray, 2);


/* Lookups */

struct SinglePosFormat1 {

  friend struct SinglePos;

  private:
B
Behdad Esfahbod 已提交
260 261
  inline bool apply (APPLY_ARG_DEF) const
  {
262 263 264 265 266 267
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
    if (HB_LIKELY (index == NOT_COVERED))
      return false;

    valueFormat.apply_value (layout, (const char *) this, values, CURPOSITION ());
    return true;
B
Behdad Esfahbod 已提交
268 269 270 271 272 273 274
  }

  private:
  USHORT	format;			/* Format identifier--format = 1 */
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
275
  ValueFormat	valueFormat;		/* Defines the types of data in the
B
Behdad Esfahbod 已提交
276 277 278 279 280 281 282 283 284 285 286 287
					 * ValueRecord */
  ValueRecord	values;			/* Defines positioning
					 * value(s)--applied to all glyphs in
					 * the Coverage table */
};
ASSERT_SIZE (SinglePosFormat1, 6);

struct SinglePosFormat2 {

  friend struct SinglePos;

  private:
B
Behdad Esfahbod 已提交
288 289
  inline bool apply (APPLY_ARG_DEF) const
  {
290 291 292 293 294 295 296 297 298 299 300
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
    if (HB_LIKELY (index == NOT_COVERED))
      return false;

    if (HB_LIKELY (index >= valueCount))
      return false;

    valueFormat.apply_value (layout, (const char *) this,
			     values + index * valueFormat.get_len (),
			     CURPOSITION ());
    return true;
B
Behdad Esfahbod 已提交
301 302 303 304 305 306 307
  }

  private:
  USHORT	format;			/* Format identifier--format = 2 */
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
308
  ValueFormat	valueFormat;		/* Defines the types of data in the
B
Behdad Esfahbod 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321
					 * ValueRecord */
  USHORT	valueCount;		/* Number of ValueRecords */
  ValueRecord	values;			/* Array of ValueRecords--positioning
					 * values applied to glyphs */
};
ASSERT_SIZE (SinglePosFormat2, 8);

struct SinglePos {

  friend struct PosLookupSubTable;

  private:

B
Behdad Esfahbod 已提交
322 323
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
324
    switch (u.format) {
325 326
    case 1: return u.format1->apply (APPLY_ARG);
    case 2: return u.format2->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    default:return false;
    }
  }

  private:
  union {
  USHORT		format;		/* Format identifier */
  SinglePosFormat1	format1[];
  SinglePosFormat2	format2[];
  } u;
};
ASSERT_SIZE (SinglePos, 2);


struct PairValueRecord {
B
Behdad Esfahbod 已提交
342 343

  friend struct PairPosFormat1;
B
Behdad Esfahbod 已提交
344 345 346 347 348 349 350 351 352 353

  private:
  GlyphID	secondGlyph;		/* GlyphID of second glyph in the
					 * pair--first glyph is listed in the
					 * Coverage table */
  ValueRecord	values;			/* Positioning data for the first glyph
					 * followed by for second glyph */
};
ASSERT_SIZE (PairValueRecord, 2);

B
Behdad Esfahbod 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
struct PairSet {

  friend struct PairPosFormat1;

  private:
  inline bool apply (APPLY_ARG_DEF,
		     ValueFormat &f1, ValueFormat &f2,
		     unsigned int next_pos) const {


    return true;
  }

  private:
  USHORT	len;			/* Number of PairValueRecords */
  /* XXX */
  PairValueRecord
		array[];		/* Array of PairValueRecords--ordered
					 * by GlyphID of the second glyph */
};
B
Behdad Esfahbod 已提交
374 375 376 377 378 379 380
ASSERT_SIZE (PairSet, 2);

struct PairPosFormat1 {

  friend struct PairPos;

  private:
B
Behdad Esfahbod 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  inline bool apply (APPLY_ARG_DEF) const
  {
    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
    if (HB_UNLIKELY (buffer->in_pos + 2 > end))
      return false;

    unsigned int index = (this+coverage) (IN_CURGLYPH ());
    if (HB_LIKELY (index == NOT_COVERED))
      return false;

    unsigned int j = buffer->in_pos + 1;
    while (!_hb_ot_layout_check_glyph_property (layout, IN_ITEM (j), lookup_flag, &property)) {
      if (HB_UNLIKELY (j == end))
	return false;
      j++;
    }

    const PairSet &pair_set = this+pairSet[index];

B
Behdad Esfahbod 已提交
400 401
    unsigned int len1 = valueFormat1.get_len ();
    unsigned int len2 = valueFormat2.get_len ();
B
Behdad Esfahbod 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
    unsigned int record_len = 1 + len1 + len2;

    unsigned int count = pair_set.len;
    const PairValueRecord *record = pair_set.array;
    for (unsigned int i = 0; i < count; i++) {
      if (IN_GLYPH (j) == record->secondGlyph) {
	valueFormat1.apply_value (layout, (const char *) this, record->values, CURPOSITION ());
	valueFormat2.apply_value (layout, (const char *) this, record->values + len1, POSITION (j));
	if (len2)
	  j++;
	buffer->in_pos = j;
	return true;
      }
      record += record_len;
    }

B
Behdad Esfahbod 已提交
418 419 420 421 422 423 424 425
    return false;
  }

  private:
  USHORT	format;			/* Format identifier--format = 1 */
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
426
  ValueFormat	valueFormat1;		/* Defines the types of data in
B
Behdad Esfahbod 已提交
427 428
					 * ValueRecord1--for the first glyph
					 * in the pair--may be zero (0) */
429
  ValueFormat	valueFormat2;		/* Defines the types of data in
B
Behdad Esfahbod 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442
					 * ValueRecord2--for the second glyph
					 * in the pair--may be zero (0) */
  OffsetArrayOf<PairSet>
		pairSet;		/* Array of PairSet tables
					 * ordered by Coverage Index */
};
ASSERT_SIZE (PairPosFormat1, 10);

struct PairPosFormat2 {

  friend struct PairPos;

  private:
B
Behdad Esfahbod 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
  inline bool apply (APPLY_ARG_DEF) const
  {
    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
    if (HB_UNLIKELY (buffer->in_pos + 2 > end))
      return false;

    unsigned int index = (this+coverage) (IN_CURGLYPH ());
    if (HB_LIKELY (index == NOT_COVERED))
      return false;

    unsigned int j = buffer->in_pos + 1;
    while (!_hb_ot_layout_check_glyph_property (layout, IN_ITEM (j), lookup_flag, &property)) {
      if (HB_UNLIKELY (j == end))
	return false;
      j++;
    }

    unsigned int len1 = valueFormat1.get_len ();
    unsigned int len2 = valueFormat2.get_len ();
    unsigned int record_len = len1 + len2;

    unsigned int klass1 = (this+classDef1) (IN_CURGLYPH ());
    unsigned int klass2 = (this+classDef2) (IN_GLYPH (j));
    if (HB_UNLIKELY (klass1 >= class1Count || klass2 >= class2Count))
      return false;

    const Value *v = values + record_len * (klass1 * class2Count + klass2);
    valueFormat1.apply_value (layout, (const char *) this, v, CURPOSITION ());
    valueFormat2.apply_value (layout, (const char *) this, v + len1, POSITION (j));

    if (len2)
      j++;
    buffer->in_pos = j;

    return true;
B
Behdad Esfahbod 已提交
478 479
  }

B
Behdad Esfahbod 已提交
480

B
Behdad Esfahbod 已提交
481 482 483 484 485
  private:
  USHORT	format;			/* Format identifier--format = 2 */
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
486
  ValueFormat	valueFormat1;		/* ValueRecord definition--for the
B
Behdad Esfahbod 已提交
487 488
					 * first glyph of the pair--may be zero
					 * (0) */
489
  ValueFormat	valueFormat2;		/* ValueRecord definition--for the
B
Behdad Esfahbod 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
					 * second glyph of the pair--may be
					 * zero (0) */
  OffsetTo<ClassDef>
		classDef1;		/* Offset to ClassDef table--from
					 * beginning of PairPos subtable--for
					 * the first glyph of the pair */
  OffsetTo<ClassDef>
		classDef2;		/* Offset to ClassDef table--from
					 * beginning of PairPos subtable--for
					 * the second glyph of the pair */
  USHORT	class1Count;		/* Number of classes in ClassDef1
					 * table--includes Class0 */
  USHORT	class2Count;		/* Number of classes in ClassDef2
					 * table--includes Class0 */
  ValueRecord	values;			/* Matrix of value pairs:
					 * class1-major, class2-minor,
					 * Each entry has value1 and value2 */
};
ASSERT_SIZE (PairPosFormat2, 16);

struct PairPos {

  friend struct PosLookupSubTable;

  private:

516
  inline bool apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
517
    switch (u.format) {
518 519
    case 1: return u.format1->apply (APPLY_ARG);
    case 2: return u.format2->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    default:return false;
    }
  }

  private:
  union {
  USHORT		format;		/* Format identifier */
  PairPosFormat1	format1[];
  PairPosFormat2	format2[];
  } u;
};
ASSERT_SIZE (PairPos, 2);


struct EntryExitRecord {
  /* TODO */

  private:
  OffsetTo<Anchor>
		entryAnchor;		/* Offset to EntryAnchor table--from
					 * beginning of CursivePos
					 * subtable--may be NULL */
  OffsetTo<Anchor>
		exitAnchor;		/* Offset to ExitAnchor table--from
					 * beginning of CursivePos
					 * subtable--may be NULL */
};
ASSERT_SIZE (EntryExitRecord, 4);

struct CursivePosFormat1 {

  friend struct CursivePos;

  private:
554
  inline bool apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    /* TODO */
    return false;
  }

  private:
  USHORT	format;			/* Format identifier--format = 1 */
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
  ArrayOf<EntryExitRecord>
		entryExitRecord;	/* Array of EntryExit records--in
					 * Coverage Index order */
};
ASSERT_SIZE (CursivePosFormat1, 6);

struct CursivePos {

  friend struct PosLookupSubTable;

  private:

576
  inline bool apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
577
    switch (u.format) {
578
    case 1: return u.format1->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    default:return false;
    }
  }

  private:
  union {
  USHORT		format;		/* Format identifier */
  CursivePosFormat1	format1[];
  } u;
};
ASSERT_SIZE (CursivePos, 2);


struct BaseRecord {
  /* TODO */

  private:
  Offset	baseAnchor[];		/* Array of offsets (one per class)
					 * to Anchor tables--from beginning
					 * of BaseArray table--ordered by
					 * class--zero--based */
};
ASSERT_SIZE (BaseRecord, 0);

struct BaseArray {
  /* TODO */

  private:
  USHORT	baseCount;		/* Number of BaseRecords */
  BaseRecord	baseRecord[];		/* Array of BaseRecords--in order of
					 * BaseCoverage Index */
};
ASSERT_SIZE (BaseArray, 2);

struct MarkBasePosFormat1 {

  friend struct MarkBasePos;

  private:
618
  inline bool apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    /* TODO */
    return false;
  }

  private:
  USHORT	format;			/* Format identifier--format = 1 */
  Offset	markCoverage;		/* Offset to MarkCoverage table--from
					 * beginning of MarkBasePos subtable */
  Offset	baseCoverage;		/* Offset to BaseCoverage table--from
					 * beginning of MarkBasePos subtable */
  USHORT	classCount;		/* Number of classes defined for marks */
  Offset	markArray;		/* Offset to MarkArray table--from
					 * beginning of MarkBasePos subtable */
  /* XXXXXXXXXXXXX */
  Offset	baseArray;		/* Offset to BaseArray table--from
					 * beginning of MarkBasePos subtable */
};
ASSERT_SIZE (MarkBasePosFormat1, 12);

struct MarkBasePos {

  friend struct PosLookupSubTable;

  private:

644
  inline bool apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
645
    switch (u.format) {
646
    case 1: return u.format1->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
    default:return false;
    }
  }

  private:
  union {
  USHORT		format;		/* Format identifier */
  MarkBasePosFormat1	format1[];
  } u;
};
ASSERT_SIZE (MarkBasePos, 2);


struct ComponentRecord {
  /* TODO */

  private:
  OffsetTo<Anchor>
		ligatureAnchor[];	/* Array of offsets (one per class)
					 * to Anchor tables--from beginning
					 * of LigatureAttach table--ordered
					 * by class--NULL if a component
					 * does not have an attachment for a
					 * class--zero--based array */
};
ASSERT_SIZE (ComponentRecord, 0);

struct LigatureAttach {
  /* TODO */

  private:
    /* XXXXXXXXXXXXX */
  USHORT	componentCount;		/* Number of ComponentRecords in this
					 * ligature */
  ComponentRecord
		componentRecord[];	/* Array of ComponentRecords--ordered
					 * in writing direction */
};
ASSERT_SIZE (LigatureAttach, 2);

struct LigatureArray {
  /* TODO */

  private:
  OffsetArrayOf<LigatureAttach>
		ligatureAttach;		/* Array of LigatureAttach
					 * tables ordered by
					 * LigatureCoverage Index */
};
ASSERT_SIZE (LigatureArray, 2);

struct MarkLigPosFormat1 {

  friend struct MarkLigPos;

  private:
703
  inline bool apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    /* TODO */
    return false;
  }

  private:
  USHORT	format;			/* Format identifier--format = 1 */
  Offset	markCoverage;		/* Offset to Mark Coverage table--from
					 * beginning of MarkLigPos subtable */
  Offset	ligatureCoverage;	/* Offset to Ligature Coverage
					 * table--from beginning of MarkLigPos
					 * subtable */
  USHORT	classCount;		/* Number of defined mark classes */
  Offset	markArray;		/* Offset to MarkArray table--from
					 * beginning of MarkLigPos subtable */
  Offset	ligatureArray;		/* Offset to LigatureArray table--from
					 * beginning of MarkLigPos subtable */
};
ASSERT_SIZE (MarkLigPosFormat1, 12);

struct MarkLigPos {

  friend struct PosLookupSubTable;

  private:

729
  inline bool apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
730
    switch (u.format) {
731
    case 1: return u.format1->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
    default:return false;
    }
  }

  private:
  union {
  USHORT		format;		/* Format identifier */
  MarkLigPosFormat1	format1[];
  } u;
};
ASSERT_SIZE (MarkLigPos, 2);


struct Mark2Record {
  /* TODO */

  private:
  OffsetTo<Anchor>
		mark2Anchor[];		/* Array of offsets (one per class)
					 * to Anchor tables--from beginning of
					 * Mark2Array table--zero--based array */
};

struct Mark2Array {
  /* TODO */

  private:
  USHORT	mark2Count;		/* Number of Mark2 records */
  Mark2Record	mark2Record[];		/* Array of Mark2Records--in Coverage
					 * order */
};
ASSERT_SIZE (Mark2Array, 2);

struct MarkMarkPosFormat1 {

  friend struct MarkMarkPos;

  private:
770
  inline bool apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    /* TODO */
    return false;
  }

  private:
  USHORT	format;			/* Format identifier--format = 1 */
  Offset	mark1Coverage;		/* Offset to Combining Mark Coverage
					 * table--from beginning of MarkMarkPos
					 * subtable */
  Offset	mark2Coverage;		/* Offset to Base Mark Coverage
					 * table--from beginning of MarkMarkPos
					 * subtable */
  USHORT	offset;			/* Mark1Array */
  Offset	mark2Array;		/* Offset to Mark2Array table for
					 * Mark2--from beginning of MarkMarkPos
					 * subtable */
};
ASSERT_SIZE (MarkMarkPosFormat1, 10);

struct MarkMarkPos {

  friend struct PosLookupSubTable;

  private:

796
  inline bool apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
797
    switch (u.format) {
798
    case 1: return u.format1->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811
    default:return false;
    }
  }

  private:
  union {
  USHORT		format;		/* Format identifier */
  MarkMarkPosFormat1	format1[];
  } u;
};
ASSERT_SIZE (MarkMarkPos, 2);


812
static inline bool position_lookup (APPLY_ARG_DEF, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
813 814 815

struct ContextPos : Context {

816 817
  inline bool apply (APPLY_ARG_DEF) const {
    return Context::apply (APPLY_ARG, position_lookup);
B
Behdad Esfahbod 已提交
818 819 820 821 822 823
  }
};
ASSERT_SIZE (ContextPos, 2);

struct ChainContextPos : ChainContext {

824 825
  inline bool apply (APPLY_ARG_DEF) const {
    return ChainContext::apply (APPLY_ARG, position_lookup);
B
Behdad Esfahbod 已提交
826 827 828 829 830 831 832 833 834 835 836 837
  }
};
ASSERT_SIZE (ChainContextPos, 2);


struct ExtensionPosFormat1 {

  friend struct ExtensionPos;

  private:
  inline unsigned int get_type (void) const { return extensionLookupType; }
  inline unsigned int get_offset (void) const { return (extensionOffset[0] << 16) + extensionOffset[1]; }
838
  inline bool apply (APPLY_ARG_DEF) const;
B
Behdad Esfahbod 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865

  private:
  USHORT	format;			/* Format identifier. Set to 1. */
  USHORT	extensionLookupType;	/* Lookup type of subtable referenced
					 * by ExtensionOffset (i.e. the
					 * extension subtable). */
  USHORT	extensionOffset[2];	/* Offset to the extension subtable,
					 * of lookup type subtable.
					 * Defined as two shorts to avoid
					 * alignment requirements. */
};
ASSERT_SIZE (ExtensionPosFormat1, 8);

struct ExtensionPos {

  friend struct PosLookup;
  friend struct PosLookupSubTable;

  private:

  inline unsigned int get_type (void) const {
    switch (u.format) {
    case 1: return u.format1->get_type ();
    default:return 0;
    }
  }

866
  inline bool apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
867
    switch (u.format) {
868
    case 1: return u.format1->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
    default:return false;
    }
  }

  private:
  union {
  USHORT		format;		/* Format identifier */
  ExtensionPosFormat1	format1[];
  } u;
};
ASSERT_SIZE (ExtensionPos, 2);


/*
 * PosLookup
 */

enum {
  GPOS_Single			= 1,
  GPOS_Pair			= 2,
  GPOS_Cursive			= 3,
  GPOS_MarkBase			= 4,
  GPOS_MarkLig			= 5,
  GPOS_MarkMark			= 6,
  GPOS_Context			= 7,
  GPOS_ChainContext		= 8,
  GPOS_Extension		= 9,
};


struct PosLookupSubTable {

  friend struct PosLookup;

903
  inline bool apply (APPLY_ARG_DEF, unsigned int lookup_type) const {
B
Behdad Esfahbod 已提交
904 905

    switch (lookup_type) {
906 907 908 909 910 911 912 913 914
    case GPOS_Single:			return u.single->apply (APPLY_ARG);
    case GPOS_Pair:			return u.pair->apply (APPLY_ARG);
    case GPOS_Cursive:			return u.cursive->apply (APPLY_ARG);
    case GPOS_MarkBase:			return u.markBase->apply (APPLY_ARG);
    case GPOS_MarkLig:			return u.markLig->apply (APPLY_ARG);
    case GPOS_MarkMark:			return u.markMark->apply (APPLY_ARG);
    case GPOS_Context:			return u.context->apply (APPLY_ARG);
    case GPOS_ChainContext:		return u.chainContext->apply (APPLY_ARG);
    case GPOS_Extension:		return u.extension->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
    default:return false;
    }
  }

  private:
  union {
  USHORT			format;
  SinglePos			single[];
  PairPos			pair[];
  CursivePos			cursive[];
  MarkBasePos			markBase[];
  MarkLigPos			markLig[];
  MarkMarkPos			markMark[];
  ContextPos			context[];
  ChainContextPos		chainContext[];
  ExtensionPos			extension[];
  } u;
};
ASSERT_SIZE (PosLookupSubTable, 2);


struct PosLookup : Lookup {

  inline const PosLookupSubTable& get_subtable (unsigned int i) const {
B
Behdad Esfahbod 已提交
939
    return (const PosLookupSubTable&) Lookup::get_subtable (i);
B
Behdad Esfahbod 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
  }

  /* Like get_type(), but looks through extension lookups.
   * Never returns Extension */
  inline unsigned int get_effective_type (void) const {
    unsigned int type = get_type ();

    if (HB_UNLIKELY (type == GPOS_Extension)) {
      unsigned int count = get_subtable_count ();
      type = get_subtable(0).u.extension->get_type ();
      /* The spec says all subtables should have the same type.
       * This is specially important if one has a reverse type! */
      for (unsigned int i = 1; i < count; i++)
        if (get_subtable(i).u.extension->get_type () != type)
	  return 0;
    }

    return type;
  }

960 961 962 963 964
  inline bool apply_subtables (hb_ot_layout_t *layout,
			       hb_buffer_t    *buffer,
			       unsigned int    context_length,
			       unsigned int    nesting_level_left,
			       unsigned int    property) const {
B
Behdad Esfahbod 已提交
965 966 967 968
    unsigned int lookup_type = get_type ();
    unsigned int lookup_flag = get_flag ();

    for (unsigned int i = 0; i < get_subtable_count (); i++)
969
      if (get_subtable (i).apply (APPLY_ARG, lookup_type))
B
Behdad Esfahbod 已提交
970 971 972 973 974
	return true;

    return false;
  }

975
  inline bool apply_once (hb_ot_layout_t *layout, hb_buffer_t *buffer) const {
B
Behdad Esfahbod 已提交
976 977 978 979 980 981 982

    unsigned int lookup_flag = get_flag ();

    unsigned int property;
    if (!_hb_ot_layout_check_glyph_property (layout, IN_CURITEM (), lookup_flag, &property))
      return false;

983
    return apply_subtables (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL, property);
B
Behdad Esfahbod 已提交
984 985
  }

986 987 988
  bool apply_string (hb_ot_layout_t *layout,
		     hb_buffer_t    *buffer,
		     hb_ot_layout_feature_mask_t mask) const {
B
Behdad Esfahbod 已提交
989 990 991 992 993 994

    bool ret = false;

    if (HB_UNLIKELY (!buffer->in_length))
      return false;

995 996
    layout->gpos_info.last = 0xFFFF; /* no last valid glyph for cursive pos. */

B
Behdad Esfahbod 已提交
997 998 999
    buffer->in_pos = 0;
    while (buffer->in_pos < buffer->in_length) {

1000 1001
      bool done;
      if (~IN_PROPERTIES (buffer->in_pos) & mask) {
1002
	  done = apply_once (layout, buffer);
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	  ret |= done;
      } else {
          done = false;
	  /* Contrary to properties defined in GDEF, user-defined properties
	     will always stop a possible cursive positioning.                */
	  layout->gpos_info.last = 0xFFFF;
      }

      if (!done)
	buffer->in_pos++;
B
Behdad Esfahbod 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    }

    return ret;
  }
};
ASSERT_SIZE (PosLookup, 6);


/*
 * GPOS
 */

struct GPOS : GSUBGPOS {
  static const hb_tag_t Tag		= HB_TAG ('G','P','O','S');

  STATIC_DEFINE_GET_FOR_DATA (GPOS);
  /* XXX check version here? */

  inline const PosLookup& get_lookup (unsigned int i) const {
B
Behdad Esfahbod 已提交
1032
    return (PosLookup&)(((GSUBGPOS *)this)->get_lookup (i));
B
Behdad Esfahbod 已提交
1033 1034 1035 1036 1037 1038
  }

  inline bool position_lookup (hb_ot_layout_t *layout,
			       hb_buffer_t    *buffer,
			       unsigned int    lookup_index,
			       hb_ot_layout_feature_mask_t  mask) const {
1039
    return get_lookup (lookup_index).apply_string (layout, buffer, mask);
B
Behdad Esfahbod 已提交
1040 1041 1042 1043 1044 1045 1046 1047
  }

};
ASSERT_SIZE (GPOS, 10);


/* Out-of-class implementation for methods recursing */

1048
inline bool ExtensionPosFormat1::apply (APPLY_ARG_DEF) const {
B
Behdad Esfahbod 已提交
1049 1050 1051 1052 1053
  unsigned int lookup_type = get_type ();

  if (HB_UNLIKELY (lookup_type ==  GPOS_Extension))
    return false;

1054
  return ((PosLookupSubTable&)*(((char *) this) + get_offset ())).apply (APPLY_ARG, lookup_type);
B
Behdad Esfahbod 已提交
1055 1056
}

1057
static inline bool position_lookup (APPLY_ARG_DEF, unsigned int lookup_index) {
B
Behdad Esfahbod 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
  const GPOS &gpos = *(layout->gpos);
  const PosLookup &l = gpos.get_lookup (lookup_index);

  if (HB_UNLIKELY (nesting_level_left == 0))
    return false;
  nesting_level_left--;

  if (HB_UNLIKELY (context_length < 1))
    return false;

1068
  return l.apply_subtables (layout, buffer, context_length, nesting_level_left, property);
B
Behdad Esfahbod 已提交
1069 1070 1071 1072
}


#endif /* HB_OT_LAYOUT_GPOS_PRIVATE_H */