harfbuzz-gsub.c 96.1 KB
Newer Older
1 2 3 4 5 6
/*******************************************************************
 *
 *  Copyright 1996-2000 by
 *  David Turner, Robert Wilhelm, and Werner Lemberg.
 *
 *  Copyright 2006  Behdad Esfahbod
7
 *  Copyright 2007  Red Hat Software
8 9 10 11 12 13 14 15 16 17 18
 *
 *  This is part of HarfBuzz, an OpenType Layout engine library.
 *
 *  See the file name COPYING for licensing information.
 *
 ******************************************************************/
#include "harfbuzz-impl.h"
#include "harfbuzz-gsub-private.h"
#include "harfbuzz-open-private.h"
#include "harfbuzz-gdef-private.h"

19
static HB_Error  GSUB_Do_Glyph_Lookup( HB_GSUBHeader*   gsub,
20 21 22 23 24 25 26 27 28 29 30 31 32
				       FT_UShort         lookup_index,
				       HB_Buffer        buffer,
				       FT_UShort         context_length,
				       int               nesting_level );



/**********************
 * Auxiliary functions
 **********************/



33
HB_Error  HB_Load_GSUB_Table( FT_Face          face,
34 35 36 37 38
			      HB_GSUBHeader** retptr,
			      HB_GDEFHeader*  gdef )
{
  FT_Stream        stream = face->stream;
  FT_Memory        memory = face->memory;
39
  HB_Error         error;
40 41 42 43 44 45 46
  FT_ULong         cur_offset, new_offset, base_offset;

  FT_UShort        i, num_lookups;
  HB_GSUBHeader*  gsub;
  HB_Lookup*      lo;

  if ( !retptr )
47
    return HB_Err_Invalid_Argument;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

  if (( error = _hb_ftglue_face_goto_table( face, TTAG_GSUB, stream ) ))
    return error;

  base_offset = FILE_Pos();

  if ( ALLOC ( gsub, sizeof( *gsub ) ) )
    return error;

  gsub->memory = memory;

  /* skip version */

  if ( FILE_Seek( base_offset + 4L ) ||
       ACCESS_Frame( 2L ) )
    goto Fail4;

  new_offset = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
       ( error = _HB_OPEN_Load_ScriptList( &gsub->ScriptList,
72
				  stream ) ) != HB_Err_Ok )
73 74 75 76 77 78 79 80 81 82 83 84 85
    goto Fail4;
  (void)FILE_Seek( cur_offset );

  if ( ACCESS_Frame( 2L ) )
    goto Fail3;

  new_offset = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
       ( error = _HB_OPEN_Load_FeatureList( &gsub->FeatureList,
86
				   stream ) ) != HB_Err_Ok )
87 88 89 90 91 92 93 94 95 96 97 98 99
    goto Fail3;
  (void)FILE_Seek( cur_offset );

  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  new_offset = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
       ( error = _HB_OPEN_Load_LookupList( &gsub->LookupList,
100
				  stream, HB_Type_GSUB ) ) != HB_Err_Ok )
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
    goto Fail2;

  gsub->gdef = gdef;      /* can be NULL */

  /* We now check the LookupFlags for values larger than 0xFF to find
     out whether we need to load the `MarkAttachClassDef' field of the
     GDEF table -- this hack is necessary for OpenType 1.2 tables since
     the version field of the GDEF table hasn't been incremented.

     For constructed GDEF tables, we only load it if
     `MarkAttachClassDef_offset' is not zero (nevertheless, a build of
     a constructed mark attach table is not supported currently).       */

  if ( gdef &&
       gdef->MarkAttachClassDef_offset && !gdef->MarkAttachClassDef.loaded )
  {
    lo          = gsub->LookupList.Lookup;
    num_lookups = gsub->LookupList.LookupCount;

    for ( i = 0; i < num_lookups; i++ )
    {

      if ( lo[i].LookupFlag & HB_LOOKUP_FLAG_IGNORE_SPECIAL_MARKS )
      {
	if ( FILE_Seek( gdef->MarkAttachClassDef_offset ) ||
	     ( error = _HB_OPEN_Load_ClassDefinition( &gdef->MarkAttachClassDef,
127
					     256, stream ) ) != HB_Err_Ok )
128 129 130 131 132 133 134 135 136
	  goto Fail1;

	break;
      }
    }
  }

  *retptr = gsub;

137
  return HB_Err_Ok;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

Fail1:
  _HB_OPEN_Free_LookupList( &gsub->LookupList, HB_Type_GSUB, memory );

Fail2:
  _HB_OPEN_Free_FeatureList( &gsub->FeatureList, memory );

Fail3:
  _HB_OPEN_Free_ScriptList( &gsub->ScriptList, memory );

Fail4:
  FREE ( gsub );


  return error;
}


156
HB_Error   HB_Done_GSUB_Table( HB_GSUBHeader* gsub )
157 158
{
  FT_Memory memory = gsub->memory;
159

160 161 162 163 164 165
  _HB_OPEN_Free_LookupList( &gsub->LookupList, HB_Type_GSUB, memory );
  _HB_OPEN_Free_FeatureList( &gsub->FeatureList, memory );
  _HB_OPEN_Free_ScriptList( &gsub->ScriptList, memory );

  FREE( gsub );

166
  return HB_Err_Ok;
167 168 169 170 171 172 173 174 175 176 177 178
}

/*****************************
 * SubTable related functions
 *****************************/


/* LookupType 1 */

/* SingleSubstFormat1 */
/* SingleSubstFormat2 */

179
static HB_Error  Load_SingleSubst( HB_GSUB_SubTable* st,
180 181
				   FT_Stream         stream )
{
182
  HB_Error error;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  FT_Memory memory = stream->memory;
  HB_SingleSubst*  ss = &st->single;

  FT_UShort n, count;
  FT_ULong cur_offset, new_offset, base_offset;

  FT_UShort*  s;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 4L ) )
    return error;

  ss->SubstFormat = GET_UShort();
  new_offset      = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
204
       ( error = _HB_OPEN_Load_Coverage( &ss->Coverage, stream ) ) != HB_Err_Ok )
205 206 207 208 209 210 211 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
    return error;
  (void)FILE_Seek( cur_offset );

  switch ( ss->SubstFormat )
  {
  case 1:
    if ( ACCESS_Frame( 2L ) )
      goto Fail2;

    ss->ssf.ssf1.DeltaGlyphID = GET_UShort();

    FORGET_Frame();

    break;

  case 2:
    if ( ACCESS_Frame( 2L ) )
      goto Fail2;

    count = ss->ssf.ssf2.GlyphCount = GET_UShort();

    FORGET_Frame();

    ss->ssf.ssf2.Substitute = NULL;

    if ( ALLOC_ARRAY( ss->ssf.ssf2.Substitute, count, FT_UShort ) )
      goto Fail2;

    s = ss->ssf.ssf2.Substitute;

    if ( ACCESS_Frame( count * 2L ) )
      goto Fail1;

    for ( n = 0; n < count; n++ )
      s[n] = GET_UShort();

    FORGET_Frame();

    break;

  default:
246
    return _hb_err(HB_Err_Invalid_GSUB_SubTable_Format);
247 248
  }

249
  return HB_Err_Ok;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

Fail1:
  FREE( s );

Fail2:
  _HB_OPEN_Free_Coverage( &ss->Coverage, memory );
  return error;
}


static void  Free_SingleSubst( HB_GSUB_SubTable* st,
			       FT_Memory         memory )
{
  HB_SingleSubst*  ss = &st->single;

  switch ( ss->SubstFormat )
  {
  case 1:
    break;

  case 2:
    FREE( ss->ssf.ssf2.Substitute );
    break;
273 274 275

  default:
    break;
276 277 278 279 280 281
  }

  _HB_OPEN_Free_Coverage( &ss->Coverage, memory );
}


282
static HB_Error  Lookup_SingleSubst( HB_GSUBHeader*   gsub,
283 284 285 286 287 288 289
				     HB_GSUB_SubTable* st,
				     HB_Buffer        buffer,
				     FT_UShort         flags,
				     FT_UShort         context_length,
				     int               nesting_level )
{
  FT_UShort index, value, property;
290
  HB_Error  error;
291 292 293
  HB_SingleSubst*  ss = &st->single;
  HB_GDEFHeader*   gdef = gsub->gdef;

294 295
  FT_UNUSED(nesting_level);

296 297 298 299 300 301 302 303 304 305 306 307 308
  if ( context_length != 0xFFFF && context_length < 1 )
    return HB_Err_Not_Covered;

  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
    return error;

  error = _HB_OPEN_Coverage_Index( &ss->Coverage, IN_CURGLYPH(), &index );
  if ( error )
    return error;

  switch ( ss->SubstFormat )
  {
  case 1:
309 310
    value = (IN_CURGLYPH() + ss->ssf.ssf1.DeltaGlyphID ) & 0xFFFF;
    if ( REPLACE_Glyph( buffer, value, nesting_level ) )
311 312 313 314 315
      return error;
    break;

  case 2:
    if ( index >= ss->ssf.ssf2.GlyphCount )
316
      return _hb_err(HB_Err_Invalid_GSUB_SubTable);
317
    value = ss->ssf.ssf2.Substitute[index];
318
    if ( REPLACE_Glyph( buffer, value, nesting_level ) )
319 320 321 322
      return error;
    break;

  default:
323
    return _hb_err(HB_Err_Invalid_GSUB_SubTable);
324 325 326 327 328 329 330 331 332 333 334
  }

  if ( gdef && gdef->NewGlyphClasses )
  {
    /* we inherit the old glyph class to the substituted glyph */

    error = _HB_GDEF_Add_Glyph_Property( gdef, value, property );
    if ( error && error != HB_Err_Not_Covered )
      return error;
  }

335
  return HB_Err_Ok;
336 337 338 339 340 341 342
}


/* LookupType 2 */

/* Sequence */

343
static HB_Error  Load_Sequence( HB_Sequence*  s,
344 345
				FT_Stream      stream )
{
346
  HB_Error error;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  FT_Memory memory = stream->memory;

  FT_UShort n, count;
  FT_UShort*  sub;


  if ( ACCESS_Frame( 2L ) )
    return error;

  count = s->GlyphCount = GET_UShort();

  FORGET_Frame();

  s->Substitute = NULL;

  if ( count )
  {
    if ( ALLOC_ARRAY( s->Substitute, count, FT_UShort ) )
      return error;

    sub = s->Substitute;

    if ( ACCESS_Frame( count * 2L ) )
    {
      FREE( sub );
      return error;
    }

    for ( n = 0; n < count; n++ )
      sub[n] = GET_UShort();

    FORGET_Frame();
  }

381
  return HB_Err_Ok;
382 383 384 385 386 387 388 389 390 391 392 393
}


static void  Free_Sequence( HB_Sequence*  s,
			    FT_Memory      memory )
{
  FREE( s->Substitute );
}


/* MultipleSubstFormat1 */

394
static HB_Error  Load_MultipleSubst( HB_GSUB_SubTable* st,
395 396
				     FT_Stream         stream )
{
397
  HB_Error error;
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  FT_Memory memory = stream->memory;
  HB_MultipleSubst*  ms = &st->multiple;

  FT_UShort      n = 0, m, count;
  FT_ULong       cur_offset, new_offset, base_offset;

  HB_Sequence*  s;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 4L ) )
    return error;

  ms->SubstFormat = GET_UShort();             /* should be 1 */
  new_offset      = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
419
       ( error = _HB_OPEN_Load_Coverage( &ms->Coverage, stream ) ) != HB_Err_Ok )
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    return error;
  (void)FILE_Seek( cur_offset );

  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  count = ms->SequenceCount = GET_UShort();

  FORGET_Frame();

  ms->Sequence = NULL;

  if ( ALLOC_ARRAY( ms->Sequence, count, HB_Sequence ) )
    goto Fail2;

  s = ms->Sequence;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail1;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
448
	 ( error = Load_Sequence( &s[n], stream ) ) != HB_Err_Ok )
449 450 451 452
      goto Fail1;
    (void)FILE_Seek( cur_offset );
  }

453
  return HB_Err_Ok;
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

Fail1:
  for ( m = 0; m < n; m++ )
    Free_Sequence( &s[m], memory );

  FREE( s );

Fail2:
  _HB_OPEN_Free_Coverage( &ms->Coverage, memory );
  return error;
}


static void  Free_MultipleSubst( HB_GSUB_SubTable* st,
				 FT_Memory         memory )
{
  FT_UShort      n, count;
  HB_MultipleSubst*  ms = &st->multiple;

  HB_Sequence*  s;


  if ( ms->Sequence )
  {
    count = ms->SequenceCount;
    s     = ms->Sequence;

    for ( n = 0; n < count; n++ )
      Free_Sequence( &s[n], memory );

    FREE( s );
  }

  _HB_OPEN_Free_Coverage( &ms->Coverage, memory );
}


491
static HB_Error  Lookup_MultipleSubst( HB_GSUBHeader*    gsub,
492 493 494 495 496 497
				       HB_GSUB_SubTable* st,
				       HB_Buffer         buffer,
				       FT_UShort          flags,
				       FT_UShort          context_length,
				       int                nesting_level )
{
498
  HB_Error  error;
499 500 501 502 503
  FT_UShort index, property, n, count;
  FT_UShort*s;
  HB_MultipleSubst*  ms = &st->multiple;
  HB_GDEFHeader*     gdef = gsub->gdef;

504
  FT_UNUSED(nesting_level);
505 506 507 508 509 510 511 512 513 514 515 516

  if ( context_length != 0xFFFF && context_length < 1 )
    return HB_Err_Not_Covered;

  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
    return error;

  error = _HB_OPEN_Coverage_Index( &ms->Coverage, IN_CURGLYPH(), &index );
  if ( error )
    return error;

  if ( index >= ms->SequenceCount )
517
    return _hb_err(HB_Err_Invalid_GSUB_SubTable);
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

  count = ms->Sequence[index].GlyphCount;
  s     = ms->Sequence[index].Substitute;

  if ( ADD_String( buffer, 1, count, s, 0xFFFF, 0xFFFF ) )
    return error;

  if ( gdef && gdef->NewGlyphClasses )
  {
    /* this is a guess only ... */

    if ( property == HB_GDEF_LIGATURE )
      property = HB_GDEF_BASE_GLYPH;

    for ( n = 0; n < count; n++ )
    {
      error = _HB_GDEF_Add_Glyph_Property( gdef, s[n], property );
      if ( error && error != HB_Err_Not_Covered )
	return error;
    }
  }

540
  return HB_Err_Ok;
541 542 543 544 545 546 547
}


/* LookupType 3 */

/* AlternateSet */

548
static HB_Error  Load_AlternateSet( HB_AlternateSet*  as,
549 550
				    FT_Stream          stream )
{
551
  HB_Error error;
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
  FT_Memory memory = stream->memory;

  FT_UShort n, count;
  FT_UShort*  a;


  if ( ACCESS_Frame( 2L ) )
    return error;

  count = as->GlyphCount = GET_UShort();

  FORGET_Frame();

  as->Alternate = NULL;

  if ( ALLOC_ARRAY( as->Alternate, count, FT_UShort ) )
    return error;

  a = as->Alternate;

  if ( ACCESS_Frame( count * 2L ) )
  {
    FREE( a );
    return error;
  }

  for ( n = 0; n < count; n++ )
    a[n] = GET_UShort();

  FORGET_Frame();

583
  return HB_Err_Ok;
584 585 586 587 588 589 590 591 592 593 594 595
}


static void  Free_AlternateSet( HB_AlternateSet*  as,
				FT_Memory          memory )
{
  FREE( as->Alternate );
}


/* AlternateSubstFormat1 */

596
static HB_Error  Load_AlternateSubst( HB_GSUB_SubTable* st,
597 598
				      FT_Stream         stream )
{
599
  HB_Error error;
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
  FT_Memory memory = stream->memory;
  HB_AlternateSubst* as = &st->alternate;

  FT_UShort          n = 0, m, count;
  FT_ULong           cur_offset, new_offset, base_offset;

  HB_AlternateSet*  aset;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 4L ) )
    return error;

  as->SubstFormat = GET_UShort();             /* should be 1 */
  new_offset      = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
621
       ( error = _HB_OPEN_Load_Coverage( &as->Coverage, stream ) ) != HB_Err_Ok )
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    return error;
  (void)FILE_Seek( cur_offset );

  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  count = as->AlternateSetCount = GET_UShort();

  FORGET_Frame();

  as->AlternateSet = NULL;

  if ( ALLOC_ARRAY( as->AlternateSet, count, HB_AlternateSet ) )
    goto Fail2;

  aset = as->AlternateSet;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail1;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
650
	 ( error = Load_AlternateSet( &aset[n], stream ) ) != HB_Err_Ok )
651 652 653 654
      goto Fail1;
    (void)FILE_Seek( cur_offset );
  }

655
  return HB_Err_Ok;
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

Fail1:
  for ( m = 0; m < n; m++ )
    Free_AlternateSet( &aset[m], memory );

  FREE( aset );

Fail2:
  _HB_OPEN_Free_Coverage( &as->Coverage, memory );
  return error;
}


static void  Free_AlternateSubst( HB_GSUB_SubTable* st,
				  FT_Memory         memory )
{
  FT_UShort          n, count;
  HB_AlternateSubst* as = &st->alternate;

  HB_AlternateSet*  aset;


  if ( as->AlternateSet )
  {
    count = as->AlternateSetCount;
    aset  = as->AlternateSet;

    for ( n = 0; n < count; n++ )
      Free_AlternateSet( &aset[n], memory );

    FREE( aset );
  }

  _HB_OPEN_Free_Coverage( &as->Coverage, memory );
}


693
static HB_Error  Lookup_AlternateSubst( HB_GSUBHeader*    gsub,
694 695 696 697 698 699
					HB_GSUB_SubTable* st,
					HB_Buffer         buffer,
					FT_UShort          flags,
					FT_UShort          context_length,
					int                nesting_level )
{
700 701
  HB_Error          error;
  FT_UShort         index, value, alt_index, property;
702 703 704 705
  HB_AlternateSubst* as = &st->alternate;
  HB_GDEFHeader*     gdef = gsub->gdef;
  HB_AlternateSet  aset;

706
  FT_UNUSED(nesting_level);
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

  if ( context_length != 0xFFFF && context_length < 1 )
    return HB_Err_Not_Covered;

  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
    return error;

  error = _HB_OPEN_Coverage_Index( &as->Coverage, IN_CURGLYPH(), &index );
  if ( error )
    return error;

  aset = as->AlternateSet[index];

  /* we use a user-defined callback function to get the alternate index */

  if ( gsub->altfunc )
    alt_index = (gsub->altfunc)( buffer->out_pos, IN_CURGLYPH(),
				 aset.GlyphCount, aset.Alternate,
				 gsub->data );
  else
    alt_index = 0;

729 730
  value = aset.Alternate[alt_index];
  if ( REPLACE_Glyph( buffer, value, nesting_level ) )
731 732 733 734 735 736
    return error;

  if ( gdef && gdef->NewGlyphClasses )
  {
    /* we inherit the old glyph class to the substituted glyph */

737
    error = _HB_GDEF_Add_Glyph_Property( gdef, value, property );
738 739 740 741
    if ( error && error != HB_Err_Not_Covered )
      return error;
  }

742
  return HB_Err_Ok;
743 744 745 746 747 748 749
}


/* LookupType 4 */

/* Ligature */

750
static HB_Error  Load_Ligature( HB_Ligature*  l,
751 752
				FT_Stream      stream )
{
753
  HB_Error error;
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
  FT_Memory memory = stream->memory;

  FT_UShort n, count;
  FT_UShort*  c;


  if ( ACCESS_Frame( 4L ) )
    return error;

  l->LigGlyph       = GET_UShort();
  l->ComponentCount = GET_UShort();

  FORGET_Frame();

  l->Component = NULL;

  count = l->ComponentCount - 1;      /* only ComponentCount - 1 elements */

  if ( ALLOC_ARRAY( l->Component, count, FT_UShort ) )
    return error;

  c = l->Component;

  if ( ACCESS_Frame( count * 2L ) )
  {
    FREE( c );
    return error;
  }

  for ( n = 0; n < count; n++ )
    c[n] = GET_UShort();

  FORGET_Frame();

788
  return HB_Err_Ok;
789 790 791 792 793 794 795 796 797 798 799 800
}


static void  Free_Ligature( HB_Ligature*  l,
			    FT_Memory      memory )
{
  FREE( l->Component );
}


/* LigatureSet */

801
static HB_Error  Load_LigatureSet( HB_LigatureSet*  ls,
802 803
				   FT_Stream         stream )
{
804
  HB_Error error;
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
  FT_Memory memory = stream->memory;

  FT_UShort      n = 0, m, count;
  FT_ULong       cur_offset, new_offset, base_offset;

  HB_Ligature*  l;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 2L ) )
    return error;

  count = ls->LigatureCount = GET_UShort();

  FORGET_Frame();

  ls->Ligature = NULL;

  if ( ALLOC_ARRAY( ls->Ligature, count, HB_Ligature ) )
    return error;

  l = ls->Ligature;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
840
	 ( error = Load_Ligature( &l[n], stream ) ) != HB_Err_Ok )
841 842 843 844
      goto Fail;
    (void)FILE_Seek( cur_offset );
  }

845
  return HB_Err_Ok;
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

Fail:
  for ( m = 0; m < n; m++ )
    Free_Ligature( &l[m], memory );

  FREE( l );
  return error;
}


static void  Free_LigatureSet( HB_LigatureSet*  ls,
			       FT_Memory         memory )
{
  FT_UShort      n, count;

  HB_Ligature*  l;


  if ( ls->Ligature )
  {
    count = ls->LigatureCount;
    l     = ls->Ligature;

    for ( n = 0; n < count; n++ )
      Free_Ligature( &l[n], memory );

    FREE( l );
  }
}


/* LigatureSubstFormat1 */

879
static HB_Error  Load_LigatureSubst( HB_GSUB_SubTable* st,
880 881
				     FT_Stream         stream )
{
882
  HB_Error error;
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
  FT_Memory memory = stream->memory;
  HB_LigatureSubst*  ls = &st->ligature;

  FT_UShort         n = 0, m, count;
  FT_ULong          cur_offset, new_offset, base_offset;

  HB_LigatureSet*  lset;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 4L ) )
    return error;

  ls->SubstFormat = GET_UShort();             /* should be 1 */
  new_offset      = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
904
       ( error = _HB_OPEN_Load_Coverage( &ls->Coverage, stream ) ) != HB_Err_Ok )
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
    return error;
  (void)FILE_Seek( cur_offset );

  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  count = ls->LigatureSetCount = GET_UShort();

  FORGET_Frame();

  ls->LigatureSet = NULL;

  if ( ALLOC_ARRAY( ls->LigatureSet, count, HB_LigatureSet ) )
    goto Fail2;

  lset = ls->LigatureSet;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail1;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
933
	 ( error = Load_LigatureSet( &lset[n], stream ) ) != HB_Err_Ok )
934 935 936 937
      goto Fail1;
    (void)FILE_Seek( cur_offset );
  }

938
  return HB_Err_Ok;
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975

Fail1:
  for ( m = 0; m < n; m++ )
    Free_LigatureSet( &lset[m], memory );

  FREE( lset );

Fail2:
  _HB_OPEN_Free_Coverage( &ls->Coverage, memory );
  return error;
}


static void  Free_LigatureSubst( HB_GSUB_SubTable* st,
				 FT_Memory         memory )
{
  FT_UShort         n, count;
  HB_LigatureSubst*  ls = &st->ligature;

  HB_LigatureSet*  lset;


  if ( ls->LigatureSet )
  {
    count = ls->LigatureSetCount;
    lset  = ls->LigatureSet;

    for ( n = 0; n < count; n++ )
      Free_LigatureSet( &lset[n], memory );

    FREE( lset );
  }

  _HB_OPEN_Free_Coverage( &ls->Coverage, memory );
}


976
static HB_Error  Lookup_LigatureSubst( HB_GSUBHeader*    gsub,
977 978 979 980 981 982 983
				       HB_GSUB_SubTable* st,
				       HB_Buffer         buffer,
				       FT_UShort          flags,
				       FT_UShort          context_length,
				       int                nesting_level )
{
  FT_UShort      index, property;
984
  HB_Error       error;
985 986 987 988 989 990 991
  FT_UShort      numlig, i, j, is_mark, first_is_mark = FALSE;
  FT_UShort*     c;
  HB_LigatureSubst*  ls = &st->ligature;
  HB_GDEFHeader*     gdef = gsub->gdef;

  HB_Ligature*  lig;

992
  FT_UNUSED(nesting_level);
993 994 995 996 997 998 999 1000 1001 1002 1003 1004

  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
    return error;

  if ( property == HB_GDEF_MARK || property & HB_LOOKUP_FLAG_IGNORE_SPECIAL_MARKS )
    first_is_mark = TRUE;

  error = _HB_OPEN_Coverage_Index( &ls->Coverage, IN_CURGLYPH(), &index );
  if ( error )
    return error;

  if ( index >= ls->LigatureSetCount )
1005
     return _hb_err(HB_Err_Invalid_GSUB_SubTable);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

  lig = ls->LigatureSet[index].Ligature;

  for ( numlig = ls->LigatureSet[index].LigatureCount;
	numlig;
	numlig--, lig++ )
  {
    if ( buffer->in_pos + lig->ComponentCount > buffer->in_length )
      goto next_ligature;               /* Not enough glyphs in input */

    c    = lig->Component;

    is_mark = first_is_mark;

    if ( context_length != 0xFFFF && context_length < lig->ComponentCount )
      break;

    for ( i = 1, j = buffer->in_pos + 1; i < lig->ComponentCount; i++, j++ )
    {
      while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
      {
	if ( error && error != HB_Err_Not_Covered )
	  return error;

1030
	if ( j + lig->ComponentCount - i == (FT_Long)buffer->in_length )
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	  goto next_ligature;
	j++;
      }

      if ( !( property == HB_GDEF_MARK || property & HB_LOOKUP_FLAG_IGNORE_SPECIAL_MARKS ) )
	is_mark = FALSE;

      if ( IN_GLYPH( j ) != c[i - 1] )
	goto next_ligature;
    }

    if ( gdef && gdef->NewGlyphClasses )
    {
      /* this is just a guess ... */

      error = _HB_GDEF_Add_Glyph_Property( gdef, lig->LigGlyph,
				  is_mark ? HB_GDEF_MARK : HB_GDEF_LIGATURE );
      if ( error && error != HB_Err_Not_Covered )
	return error;
    }

    if ( j == buffer->in_pos + i ) /* No input glyphs skipped */
    {
      /* We don't use a new ligature ID if there are no skipped
	 glyphs and the ligature already has an ID.             */

      if ( IN_LIGID( buffer->in_pos ) )
      {
	if ( ADD_String( buffer, i, 1, &lig->LigGlyph,
			0xFFFF, 0xFFFF ) )
	  return error;
      }
      else
      {
	FT_UShort ligID = hb_buffer_allocate_ligid( buffer );
	if ( ADD_String( buffer, i, 1, &lig->LigGlyph,
			0xFFFF, ligID ) )
	  return error;
      }
    }
    else
    {
      FT_UShort ligID = hb_buffer_allocate_ligid( buffer );
1074
      if ( ADD_Glyph( buffer, lig->LigGlyph, 0xFFFF, ligID ) )
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
	return error;

      /* Now we must do a second loop to copy the skipped glyphs to
	 `out' and assign component values to it.  We start with the
	 glyph after the first component.  Glyphs between component
	 i and i+1 belong to component i.  Together with the ligID
	 value it is later possible to check whether a specific
	 component value really belongs to a given ligature.         */

      for ( i = 0; i < lig->ComponentCount - 1; i++ )
      {
	while ( CHECK_Property( gdef, IN_CURITEM(),
				flags, &property ) )
1088
	  if ( ADD_Glyph( buffer, IN_CURGLYPH(), i, ligID ) )
1089 1090 1091 1092 1093 1094
	    return error;

	(buffer->in_pos)++;
      }
    }

1095
    return HB_Err_Ok;
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108

  next_ligature:
    ;
  }

  return HB_Err_Not_Covered;
}


/* Do the actual substitution for a context substitution (either format
   5 or 6).  This is only called after we've determined that the input
   matches the subrule.                                                 */

1109
static HB_Error  Do_ContextSubst( HB_GSUBHeader*        gsub,
1110 1111 1112 1113 1114 1115
				  FT_UShort              GlyphCount,
				  FT_UShort              SubstCount,
				  HB_SubstLookupRecord* subst,
				  HB_Buffer             buffer,
				  int                    nesting_level )
{
1116
  HB_Error  error;
1117
  FT_ULong i, old_pos;
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138


  i = 0;

  while ( i < GlyphCount )
  {
    if ( SubstCount && i == subst->SequenceIndex )
    {
      old_pos = buffer->in_pos;

      /* Do a substitution */

      error = GSUB_Do_Glyph_Lookup( gsub, subst->LookupListIndex, buffer,
				    GlyphCount, nesting_level );

      subst++;
      SubstCount--;
      i += buffer->in_pos - old_pos;

      if ( error == HB_Err_Not_Covered )
      {
1139
	if ( COPY_Glyph( buffer ) )
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	  return error;
	i++;
      }
      else if ( error )
	return error;
    }
    else
    {
      /* No substitution for this index */

1150
      if ( COPY_Glyph( buffer ) )
1151 1152 1153 1154 1155
	return error;
      i++;
    }
  }

1156
  return HB_Err_Ok;
1157 1158 1159 1160 1161 1162 1163
}


/* LookupType 5 */

/* SubRule */

1164
static HB_Error  Load_SubRule( HB_SubRule*  sr,
1165 1166
			       FT_Stream     stream )
{
1167
  HB_Error error;
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
  FT_Memory memory = stream->memory;

  FT_UShort               n, count;
  FT_UShort*              i;

  HB_SubstLookupRecord*  slr;


  if ( ACCESS_Frame( 4L ) )
    return error;

  sr->GlyphCount = GET_UShort();
  sr->SubstCount = GET_UShort();

  FORGET_Frame();

  sr->Input = NULL;

  count = sr->GlyphCount - 1;         /* only GlyphCount - 1 elements */

  if ( ALLOC_ARRAY( sr->Input, count, FT_UShort ) )
    return error;

  i = sr->Input;

  if ( ACCESS_Frame( count * 2L ) )
    goto Fail2;

  for ( n = 0; n < count; n++ )
    i[n] = GET_UShort();

  FORGET_Frame();

  sr->SubstLookupRecord = NULL;

  count = sr->SubstCount;

  if ( ALLOC_ARRAY( sr->SubstLookupRecord, count, HB_SubstLookupRecord ) )
    goto Fail2;

  slr = sr->SubstLookupRecord;

  if ( ACCESS_Frame( count * 4L ) )
    goto Fail1;

  for ( n = 0; n < count; n++ )
  {
    slr[n].SequenceIndex   = GET_UShort();
    slr[n].LookupListIndex = GET_UShort();
  }

  FORGET_Frame();

1221
  return HB_Err_Ok;
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241

Fail1:
  FREE( slr );

Fail2:
  FREE( i );
  return error;
}


static void  Free_SubRule( HB_SubRule*  sr,
			   FT_Memory     memory )
{
  FREE( sr->SubstLookupRecord );
  FREE( sr->Input );
}


/* SubRuleSet */

1242
static HB_Error  Load_SubRuleSet( HB_SubRuleSet*  srs,
1243 1244
				  FT_Stream        stream )
{
1245
  HB_Error error;
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
  FT_Memory memory = stream->memory;

  FT_UShort     n = 0, m, count;
  FT_ULong      cur_offset, new_offset, base_offset;

  HB_SubRule*  sr;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 2L ) )
    return error;

  count = srs->SubRuleCount = GET_UShort();

  FORGET_Frame();

  srs->SubRule = NULL;

  if ( ALLOC_ARRAY( srs->SubRule, count, HB_SubRule ) )
    return error;

  sr = srs->SubRule;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
1281
	 ( error = Load_SubRule( &sr[n], stream ) ) != HB_Err_Ok )
1282 1283 1284 1285
      goto Fail;
    (void)FILE_Seek( cur_offset );
  }

1286
  return HB_Err_Ok;
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

Fail:
  for ( m = 0; m < n; m++ )
    Free_SubRule( &sr[m], memory );

  FREE( sr );
  return error;
}


static void  Free_SubRuleSet( HB_SubRuleSet*  srs,
			      FT_Memory        memory )
{
  FT_UShort     n, count;

  HB_SubRule*  sr;


  if ( srs->SubRule )
  {
    count = srs->SubRuleCount;
    sr    = srs->SubRule;

    for ( n = 0; n < count; n++ )
      Free_SubRule( &sr[n], memory );

    FREE( sr );
  }
}


/* ContextSubstFormat1 */

1320
static HB_Error  Load_ContextSubst1( HB_ContextSubstFormat1*  csf1,
1321 1322
				     FT_Stream                 stream )
{
1323
  HB_Error error;
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
  FT_Memory memory = stream->memory;

  FT_UShort        n = 0, m, count;
  FT_ULong         cur_offset, new_offset, base_offset;

  HB_SubRuleSet*  srs;


  base_offset = FILE_Pos() - 2L;

  if ( ACCESS_Frame( 2L ) )
    return error;

  new_offset = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
1343
       ( error = _HB_OPEN_Load_Coverage( &csf1->Coverage, stream ) ) != HB_Err_Ok )
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
    return error;
  (void)FILE_Seek( cur_offset );

  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  count = csf1->SubRuleSetCount = GET_UShort();

  FORGET_Frame();

  csf1->SubRuleSet = NULL;

  if ( ALLOC_ARRAY( csf1->SubRuleSet, count, HB_SubRuleSet ) )
    goto Fail2;

  srs = csf1->SubRuleSet;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail1;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
1372
	 ( error = Load_SubRuleSet( &srs[n], stream ) ) != HB_Err_Ok )
1373 1374 1375 1376
      goto Fail1;
    (void)FILE_Seek( cur_offset );
  }

1377
  return HB_Err_Ok;
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415

Fail1:
  for ( m = 0; m < n; m++ )
    Free_SubRuleSet( &srs[m], memory );

  FREE( srs );

Fail2:
  _HB_OPEN_Free_Coverage( &csf1->Coverage, memory );
  return error;
}


static void  Free_ContextSubst1( HB_ContextSubstFormat1* csf1,
			    FT_Memory                memory )
{
  FT_UShort        n, count;

  HB_SubRuleSet*  srs;


  if ( csf1->SubRuleSet )
  {
    count = csf1->SubRuleSetCount;
    srs   = csf1->SubRuleSet;

    for ( n = 0; n < count; n++ )
      Free_SubRuleSet( &srs[n], memory );

    FREE( srs );
  }

  _HB_OPEN_Free_Coverage( &csf1->Coverage, memory );
}


/* SubClassRule */

1416
static HB_Error  Load_SubClassRule( HB_ContextSubstFormat2*  csf2,
1417 1418 1419
				    HB_SubClassRule*         scr,
				    FT_Stream                 stream )
{
1420
  HB_Error error;
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
  FT_Memory memory = stream->memory;

  FT_UShort               n, count;

  FT_UShort*              c;
  HB_SubstLookupRecord*  slr;
  FT_Bool*                d;


  if ( ACCESS_Frame( 4L ) )
    return error;

  scr->GlyphCount = GET_UShort();
  scr->SubstCount = GET_UShort();

  if ( scr->GlyphCount > csf2->MaxContextLength )
    csf2->MaxContextLength = scr->GlyphCount;

  FORGET_Frame();

  scr->Class = NULL;

  count = scr->GlyphCount - 1;        /* only GlyphCount - 1 elements */

  if ( ALLOC_ARRAY( scr->Class, count, FT_UShort ) )
    return error;

  c = scr->Class;
  d = csf2->ClassDef.Defined;

  if ( ACCESS_Frame( count * 2L ) )
    goto Fail2;

  for ( n = 0; n < count; n++ )
  {
    c[n] = GET_UShort();

    /* We check whether the specific class is used at all.  If not,
       class 0 is used instead.                                     */
    if ( !d[c[n]] )
      c[n] = 0;
  }

  FORGET_Frame();

  scr->SubstLookupRecord = NULL;

  count = scr->SubstCount;

  if ( ALLOC_ARRAY( scr->SubstLookupRecord, count, HB_SubstLookupRecord ) )
    goto Fail2;

  slr = scr->SubstLookupRecord;

  if ( ACCESS_Frame( count * 4L ) )
    goto Fail1;

  for ( n = 0; n < count; n++ )
  {
    slr[n].SequenceIndex   = GET_UShort();
    slr[n].LookupListIndex = GET_UShort();
  }

  FORGET_Frame();

1486
  return HB_Err_Ok;
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506

Fail1:
  FREE( slr );

Fail2:
  FREE( c );
  return error;
}


static void  Free_SubClassRule( HB_SubClassRule*  scr,
				FT_Memory          memory )
{
  FREE( scr->SubstLookupRecord );
  FREE( scr->Class );
}


/* SubClassSet */

1507
static HB_Error  Load_SubClassSet( HB_ContextSubstFormat2*  csf2,
1508 1509 1510
				   HB_SubClassSet*          scs,
				   FT_Stream                 stream )
{
1511
  HB_Error error;
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
  FT_Memory memory = stream->memory;

  FT_UShort          n = 0, m, count;
  FT_ULong           cur_offset, new_offset, base_offset;

  HB_SubClassRule*  scr;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 2L ) )
    return error;

  count = scs->SubClassRuleCount = GET_UShort();

  FORGET_Frame();

  scs->SubClassRule = NULL;

  if ( ALLOC_ARRAY( scs->SubClassRule, count, HB_SubClassRule ) )
    return error;

  scr = scs->SubClassRule;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
	 ( error = Load_SubClassRule( csf2, &scr[n],
1548
				      stream ) ) != HB_Err_Ok )
1549 1550 1551 1552
      goto Fail;
    (void)FILE_Seek( cur_offset );
  }

1553
  return HB_Err_Ok;
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586

Fail:
  for ( m = 0; m < n; m++ )
    Free_SubClassRule( &scr[m], memory );

  FREE( scr );
  return error;
}


static void  Free_SubClassSet( HB_SubClassSet*  scs,
			       FT_Memory         memory )
{
  FT_UShort          n, count;

  HB_SubClassRule*  scr;


  if ( scs->SubClassRule )
  {
    count = scs->SubClassRuleCount;
    scr   = scs->SubClassRule;

    for ( n = 0; n < count; n++ )
      Free_SubClassRule( &scr[n], memory );

    FREE( scr );
  }
}


/* ContextSubstFormat2 */

1587
static HB_Error  Load_ContextSubst2( HB_ContextSubstFormat2*  csf2,
1588 1589
				     FT_Stream                 stream )
{
1590
  HB_Error error;
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
  FT_Memory memory = stream->memory;

  FT_UShort         n = 0, m, count;
  FT_ULong          cur_offset, new_offset, base_offset;

  HB_SubClassSet*  scs;


  base_offset = FILE_Pos() - 2;

  if ( ACCESS_Frame( 2L ) )
    return error;

  new_offset = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
1610
       ( error = _HB_OPEN_Load_Coverage( &csf2->Coverage, stream ) ) != HB_Err_Ok )
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
    return error;
  (void)FILE_Seek( cur_offset );

  if ( ACCESS_Frame( 4L ) )
    goto Fail3;

  new_offset = GET_UShort() + base_offset;

  /* `SubClassSetCount' is the upper limit for class values, thus we
     read it now to make an additional safety check.                 */

  count = csf2->SubClassSetCount = GET_UShort();

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
       ( error = _HB_OPEN_Load_ClassDefinition( &csf2->ClassDef, count,
1629
				       stream ) ) != HB_Err_Ok )
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
    goto Fail3;
  (void)FILE_Seek( cur_offset );

  csf2->SubClassSet      = NULL;
  csf2->MaxContextLength = 0;

  if ( ALLOC_ARRAY( csf2->SubClassSet, count, HB_SubClassSet ) )
    goto Fail2;

  scs = csf2->SubClassSet;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail1;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    if ( new_offset != base_offset )      /* not a NULL offset */
    {
      cur_offset = FILE_Pos();
      if ( FILE_Seek( new_offset ) ||
	   ( error = Load_SubClassSet( csf2, &scs[n],
1655
				       stream ) ) != HB_Err_Ok )
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
	goto Fail1;
      (void)FILE_Seek( cur_offset );
    }
    else
    {
      /* we create a SubClassSet table with no entries */

      csf2->SubClassSet[n].SubClassRuleCount = 0;
      csf2->SubClassSet[n].SubClassRule      = NULL;
    }
  }

1668
  return HB_Err_Ok;
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710

Fail1:
  for ( m = 0; m < n; m++ )
    Free_SubClassSet( &scs[m], memory );

  FREE( scs );

Fail2:
  _HB_OPEN_Free_ClassDefinition( &csf2->ClassDef, memory );

Fail3:
  _HB_OPEN_Free_Coverage( &csf2->Coverage, memory );
  return error;
}


static void  Free_ContextSubst2( HB_ContextSubstFormat2*  csf2,
			    FT_Memory                 memory )
{
  FT_UShort         n, count;

  HB_SubClassSet*  scs;


  if ( csf2->SubClassSet )
  {
    count = csf2->SubClassSetCount;
    scs   = csf2->SubClassSet;

    for ( n = 0; n < count; n++ )
      Free_SubClassSet( &scs[n], memory );

    FREE( scs );
  }

  _HB_OPEN_Free_ClassDefinition( &csf2->ClassDef, memory );
  _HB_OPEN_Free_Coverage( &csf2->Coverage, memory );
}


/* ContextSubstFormat3 */

1711
static HB_Error  Load_ContextSubst3( HB_ContextSubstFormat3*  csf3,
1712 1713
				     FT_Stream                 stream )
{
1714
  HB_Error error;
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
  FT_Memory memory = stream->memory;

  FT_UShort               n = 0, m, count;
  FT_ULong                cur_offset, new_offset, base_offset;

  HB_Coverage*           c;
  HB_SubstLookupRecord*  slr;


  base_offset = FILE_Pos() - 2L;

  if ( ACCESS_Frame( 4L ) )
    return error;

  csf3->GlyphCount = GET_UShort();
  csf3->SubstCount = GET_UShort();

  FORGET_Frame();

  csf3->Coverage = NULL;

  count = csf3->GlyphCount;

  if ( ALLOC_ARRAY( csf3->Coverage, count, HB_Coverage ) )
    return error;

  c = csf3->Coverage;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail2;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
1754
	 ( error = _HB_OPEN_Load_Coverage( &c[n], stream ) ) != HB_Err_Ok )
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
      goto Fail2;
    (void)FILE_Seek( cur_offset );
  }

  csf3->SubstLookupRecord = NULL;

  count = csf3->SubstCount;

  if ( ALLOC_ARRAY( csf3->SubstLookupRecord, count,
		    HB_SubstLookupRecord ) )
    goto Fail2;

  slr = csf3->SubstLookupRecord;

  if ( ACCESS_Frame( count * 4L ) )
    goto Fail1;

  for ( n = 0; n < count; n++ )
  {
    slr[n].SequenceIndex   = GET_UShort();
    slr[n].LookupListIndex = GET_UShort();
  }

  FORGET_Frame();

1780
  return HB_Err_Ok;
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818

Fail1:
  FREE( slr );

Fail2:
  for ( m = 0; m < n; m++ )
    _HB_OPEN_Free_Coverage( &c[m], memory );

  FREE( c );
  return error;
}


static void  Free_ContextSubst3( HB_ContextSubstFormat3*  csf3,
			    FT_Memory                 memory )
{
  FT_UShort      n, count;

  HB_Coverage*  c;


  FREE( csf3->SubstLookupRecord );

  if ( csf3->Coverage )
  {
    count = csf3->GlyphCount;
    c     = csf3->Coverage;

    for ( n = 0; n < count; n++ )
      _HB_OPEN_Free_Coverage( &c[n], memory );

    FREE( c );
  }
}


/* ContextSubst */

1819
static HB_Error  Load_ContextSubst( HB_GSUB_SubTable* st,
1820 1821
				    FT_Stream         stream )
{
1822
  HB_Error error;
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
  HB_ContextSubst*  cs = &st->context;


  if ( ACCESS_Frame( 2L ) )
    return error;

  cs->SubstFormat = GET_UShort();

  FORGET_Frame();

  switch ( cs->SubstFormat )
  {
1835 1836 1837 1838
  case 1:  return Load_ContextSubst1( &cs->csf.csf1, stream );
  case 2:  return Load_ContextSubst2( &cs->csf.csf2, stream );
  case 3:  return Load_ContextSubst3( &cs->csf.csf3, stream );
  default: return _hb_err(HB_Err_Invalid_GSUB_SubTable_Format);
1839 1840
  }

1841
  return HB_Err_Ok;               /* never reached */
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
}


static void  Free_ContextSubst( HB_GSUB_SubTable* st,
			        FT_Memory         memory )
{
  HB_ContextSubst*  cs = &st->context;

  switch ( cs->SubstFormat )
  {
1852 1853 1854 1855
  case 1:  Free_ContextSubst1( &cs->csf.csf1, memory ); break;
  case 2:  Free_ContextSubst2( &cs->csf.csf2, memory ); break;
  case 3:  Free_ContextSubst3( &cs->csf.csf3, memory ); break;
  default:						break;
1856 1857 1858 1859
  }
}


1860
static HB_Error  Lookup_ContextSubst1( HB_GSUBHeader*          gsub,
1861 1862 1863 1864 1865 1866 1867 1868
				       HB_ContextSubstFormat1* csf1,
				       HB_Buffer               buffer,
				       FT_UShort                flags,
				       FT_UShort                context_length,
				       int                      nesting_level )
{
  FT_UShort        index, property;
  FT_UShort        i, j, k, numsr;
1869
  HB_Error         error;
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901

  HB_SubRule*     sr;
  HB_GDEFHeader*  gdef;


  gdef = gsub->gdef;

  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
    return error;

  error = _HB_OPEN_Coverage_Index( &csf1->Coverage, IN_CURGLYPH(), &index );
  if ( error )
    return error;

  sr    = csf1->SubRuleSet[index].SubRule;
  numsr = csf1->SubRuleSet[index].SubRuleCount;

  for ( k = 0; k < numsr; k++ )
  {
    if ( context_length != 0xFFFF && context_length < sr[k].GlyphCount )
      goto next_subrule;

    if ( buffer->in_pos + sr[k].GlyphCount > buffer->in_length )
      goto next_subrule;                        /* context is too long */

    for ( i = 1, j = buffer->in_pos + 1; i < sr[k].GlyphCount; i++, j++ )
    {
      while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
      {
	if ( error && error != HB_Err_Not_Covered )
	  return error;

1902
	if ( j + sr[k].GlyphCount - i == (FT_Long)buffer->in_length )
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
	  goto next_subrule;
	j++;
      }

      if ( IN_GLYPH( j ) != sr[k].Input[i - 1] )
	goto next_subrule;
    }

    return Do_ContextSubst( gsub, sr[k].GlyphCount,
			    sr[k].SubstCount, sr[k].SubstLookupRecord,
			    buffer,
			    nesting_level );
  next_subrule:
    ;
  }

  return HB_Err_Not_Covered;
}


1923
static HB_Error  Lookup_ContextSubst2( HB_GSUBHeader*          gsub,
1924 1925 1926 1927 1928 1929 1930
				       HB_ContextSubstFormat2* csf2,
				       HB_Buffer               buffer,
				       FT_UShort                flags,
				       FT_UShort                context_length,
				       int                      nesting_level )
{
  FT_UShort          index, property;
1931
  HB_Error           error;
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
  FT_Memory          memory = gsub->memory;
  FT_UShort          i, j, k, known_classes;

  FT_UShort*         classes;
  FT_UShort*         cl;

  HB_SubClassSet*   scs;
  HB_SubClassRule*  sr;
  HB_GDEFHeader*    gdef;


  gdef = gsub->gdef;

  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
    return error;

  /* Note: The coverage table in format 2 doesn't give an index into
	   anything.  It just lets us know whether or not we need to
	   do any lookup at all.                                     */

  error = _HB_OPEN_Coverage_Index( &csf2->Coverage, IN_CURGLYPH(), &index );
  if ( error )
    return error;

  if ( ALLOC_ARRAY( classes, csf2->MaxContextLength, FT_UShort ) )
    return error;

  error = _HB_OPEN_Get_Class( &csf2->ClassDef, IN_CURGLYPH(),
		     &classes[0], NULL );
  if ( error && error != HB_Err_Not_Covered )
    goto End;
  known_classes = 0;

  scs = &csf2->SubClassSet[classes[0]];
  if ( !scs )
  {
1968
    error = _hb_err(HB_Err_Invalid_GSUB_SubTable);
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
    goto End;
  }

  for ( k = 0; k < scs->SubClassRuleCount; k++ )
  {
    sr  = &scs->SubClassRule[k];

    if ( context_length != 0xFFFF && context_length < sr->GlyphCount )
      goto next_subclassrule;

    if ( buffer->in_pos + sr->GlyphCount > buffer->in_length )
      goto next_subclassrule;                      /* context is too long */

    cl   = sr->Class;

    /* Start at 1 because [0] is implied */

    for ( i = 1, j = buffer->in_pos + 1; i < sr->GlyphCount; i++, j++ )
    {
      while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
      {
	if ( error && error != HB_Err_Not_Covered )
	  goto End;

1993
	if ( j + sr->GlyphCount - i < (FT_Long)buffer->in_length )
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
	  goto next_subclassrule;
	j++;
      }

      if ( i > known_classes )
      {
	/* Keeps us from having to do this for each rule */

	error = _HB_OPEN_Get_Class( &csf2->ClassDef, IN_GLYPH( j ), &classes[i], NULL );
	if ( error && error != HB_Err_Not_Covered )
	  goto End;
	known_classes = i;
      }

      if ( cl[i - 1] != classes[i] )
	goto next_subclassrule;
    }

    error = Do_ContextSubst( gsub, sr->GlyphCount,
			     sr->SubstCount, sr->SubstLookupRecord,
			     buffer,
			     nesting_level );
    goto End;

  next_subclassrule:
    ;
  }

  error = HB_Err_Not_Covered;

End:
  FREE( classes );
  return error;
}


2030
static HB_Error  Lookup_ContextSubst3( HB_GSUBHeader*          gsub,
2031 2032 2033 2034 2035 2036
				       HB_ContextSubstFormat3* csf3,
				       HB_Buffer               buffer,
				       FT_UShort                flags,
				       FT_UShort                context_length,
				       int                      nesting_level )
{
2037
  HB_Error         error;
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
  FT_UShort        index, i, j, property;

  HB_Coverage*    c;
  HB_GDEFHeader*  gdef;


  gdef = gsub->gdef;

  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
    return error;

  if ( context_length != 0xFFFF && context_length < csf3->GlyphCount )
    return HB_Err_Not_Covered;

  if ( buffer->in_pos + csf3->GlyphCount > buffer->in_length )
    return HB_Err_Not_Covered;         /* context is too long */

  c    = csf3->Coverage;

  for ( i = 1, j = buffer->in_pos + 1; i < csf3->GlyphCount; i++, j++ )
  {
    while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
    {
      if ( error && error != HB_Err_Not_Covered )
	return error;

2064
      if ( j + csf3->GlyphCount - i == (FT_Long)buffer->in_length )
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
	return HB_Err_Not_Covered;
      j++;
    }

    error = _HB_OPEN_Coverage_Index( &c[i], IN_GLYPH( j ), &index );
    if ( error )
      return error;
  }

  return Do_ContextSubst( gsub, csf3->GlyphCount,
			  csf3->SubstCount, csf3->SubstLookupRecord,
			  buffer,
			  nesting_level );
}


2081
static HB_Error  Lookup_ContextSubst( HB_GSUBHeader*    gsub,
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
				      HB_GSUB_SubTable* st,
				      HB_Buffer         buffer,
				      FT_UShort          flags,
				      FT_UShort          context_length,
				      int                nesting_level )
{
  HB_ContextSubst*  cs = &st->context;

  switch ( cs->SubstFormat )
  {
2092 2093 2094 2095
  case 1:  return Lookup_ContextSubst1( gsub, &cs->csf.csf1, buffer, flags, context_length, nesting_level );
  case 2:  return Lookup_ContextSubst2( gsub, &cs->csf.csf2, buffer, flags, context_length, nesting_level );
  case 3:  return Lookup_ContextSubst3( gsub, &cs->csf.csf3, buffer, flags, context_length, nesting_level );
  default: return _hb_err(HB_Err_Invalid_GSUB_SubTable_Format);
2096 2097
  }

2098
  return HB_Err_Ok;               /* never reached */
2099 2100 2101 2102 2103 2104 2105
}


/* LookupType 6 */

/* ChainSubRule */

2106
static HB_Error  Load_ChainSubRule( HB_ChainSubRule*  csr,
2107 2108
				    FT_Stream          stream )
{
2109
  HB_Error error;
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
  FT_Memory memory = stream->memory;

  FT_UShort               n, count;
  FT_UShort*              b;
  FT_UShort*              i;
  FT_UShort*              l;

  HB_SubstLookupRecord*  slr;


  if ( ACCESS_Frame( 2L ) )
    return error;

  csr->BacktrackGlyphCount = GET_UShort();

  FORGET_Frame();

  csr->Backtrack = NULL;

  count = csr->BacktrackGlyphCount;

  if ( ALLOC_ARRAY( csr->Backtrack, count, FT_UShort ) )
    return error;

  b = csr->Backtrack;

  if ( ACCESS_Frame( count * 2L ) )
    goto Fail4;

  for ( n = 0; n < count; n++ )
    b[n] = GET_UShort();

  FORGET_Frame();

  if ( ACCESS_Frame( 2L ) )
    goto Fail4;

  csr->InputGlyphCount = GET_UShort();

  FORGET_Frame();

  csr->Input = NULL;

  count = csr->InputGlyphCount - 1;  /* only InputGlyphCount - 1 elements */

  if ( ALLOC_ARRAY( csr->Input, count, FT_UShort ) )
    goto Fail4;

  i = csr->Input;

  if ( ACCESS_Frame( count * 2L ) )
    goto Fail3;

  for ( n = 0; n < count; n++ )
    i[n] = GET_UShort();

  FORGET_Frame();

  if ( ACCESS_Frame( 2L ) )
    goto Fail3;

  csr->LookaheadGlyphCount = GET_UShort();

  FORGET_Frame();

  csr->Lookahead = NULL;

  count = csr->LookaheadGlyphCount;

  if ( ALLOC_ARRAY( csr->Lookahead, count, FT_UShort ) )
    goto Fail3;

  l = csr->Lookahead;

  if ( ACCESS_Frame( count * 2L ) )
    goto Fail2;

  for ( n = 0; n < count; n++ )
    l[n] = GET_UShort();

  FORGET_Frame();

  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  csr->SubstCount = GET_UShort();

  FORGET_Frame();

  csr->SubstLookupRecord = NULL;

  count = csr->SubstCount;

  if ( ALLOC_ARRAY( csr->SubstLookupRecord, count, HB_SubstLookupRecord ) )
    goto Fail2;

  slr = csr->SubstLookupRecord;

  if ( ACCESS_Frame( count * 4L ) )
    goto Fail1;

  for ( n = 0; n < count; n++ )
  {
    slr[n].SequenceIndex   = GET_UShort();
    slr[n].LookupListIndex = GET_UShort();
  }

  FORGET_Frame();

2219
  return HB_Err_Ok;
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247

Fail1:
  FREE( slr );

Fail2:
  FREE( l );

Fail3:
  FREE( i );

Fail4:
  FREE( b );
  return error;
}


static void  Free_ChainSubRule( HB_ChainSubRule*  csr,
				FT_Memory          memory )
{
  FREE( csr->SubstLookupRecord );
  FREE( csr->Lookahead );
  FREE( csr->Input );
  FREE( csr->Backtrack );
}


/* ChainSubRuleSet */

2248
static HB_Error  Load_ChainSubRuleSet( HB_ChainSubRuleSet*  csrs,
2249 2250
				       FT_Stream             stream )
{
2251
  HB_Error error;
2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
  FT_Memory memory = stream->memory;

  FT_UShort          n = 0, m, count;
  FT_ULong           cur_offset, new_offset, base_offset;

  HB_ChainSubRule*  csr;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 2L ) )
    return error;

  count = csrs->ChainSubRuleCount = GET_UShort();

  FORGET_Frame();

  csrs->ChainSubRule = NULL;

  if ( ALLOC_ARRAY( csrs->ChainSubRule, count, HB_ChainSubRule ) )
    return error;

  csr = csrs->ChainSubRule;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
2287
	 ( error = Load_ChainSubRule( &csr[n], stream ) ) != HB_Err_Ok )
2288 2289 2290 2291
      goto Fail;
    (void)FILE_Seek( cur_offset );
  }

2292
  return HB_Err_Ok;
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325

Fail:
  for ( m = 0; m < n; m++ )
    Free_ChainSubRule( &csr[m], memory );

  FREE( csr );
  return error;
}


static void  Free_ChainSubRuleSet( HB_ChainSubRuleSet*  csrs,
				   FT_Memory             memory )
{
  FT_UShort          n, count;

  HB_ChainSubRule*  csr;


  if ( csrs->ChainSubRule )
  {
    count = csrs->ChainSubRuleCount;
    csr   = csrs->ChainSubRule;

    for ( n = 0; n < count; n++ )
      Free_ChainSubRule( &csr[n], memory );

    FREE( csr );
  }
}


/* ChainContextSubstFormat1 */

2326
static HB_Error  Load_ChainContextSubst1(
2327 2328 2329
		   HB_ChainContextSubstFormat1*  ccsf1,
		   FT_Stream                      stream )
{
2330
  HB_Error error;
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
  FT_Memory memory = stream->memory;

  FT_UShort             n = 0, m, count;
  FT_ULong              cur_offset, new_offset, base_offset;

  HB_ChainSubRuleSet*  csrs;


  base_offset = FILE_Pos() - 2L;

  if ( ACCESS_Frame( 2L ) )
    return error;

  new_offset = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
2350
       ( error = _HB_OPEN_Load_Coverage( &ccsf1->Coverage, stream ) ) != HB_Err_Ok )
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378
    return error;
  (void)FILE_Seek( cur_offset );

  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  count = ccsf1->ChainSubRuleSetCount = GET_UShort();

  FORGET_Frame();

  ccsf1->ChainSubRuleSet = NULL;

  if ( ALLOC_ARRAY( ccsf1->ChainSubRuleSet, count, HB_ChainSubRuleSet ) )
    goto Fail2;

  csrs = ccsf1->ChainSubRuleSet;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail1;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
2379
	 ( error = Load_ChainSubRuleSet( &csrs[n], stream ) ) != HB_Err_Ok )
2380 2381 2382 2383
      goto Fail1;
    (void)FILE_Seek( cur_offset );
  }

2384
  return HB_Err_Ok;
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422

Fail1:
  for ( m = 0; m < n; m++ )
    Free_ChainSubRuleSet( &csrs[m], memory );

  FREE( csrs );

Fail2:
  _HB_OPEN_Free_Coverage( &ccsf1->Coverage, memory );
  return error;
}


static void  Free_ChainContextSubst1( HB_ChainContextSubstFormat1*  ccsf1,
				 FT_Memory                      memory )
{
  FT_UShort             n, count;

  HB_ChainSubRuleSet*  csrs;


  if ( ccsf1->ChainSubRuleSet )
  {
    count = ccsf1->ChainSubRuleSetCount;
    csrs  = ccsf1->ChainSubRuleSet;

    for ( n = 0; n < count; n++ )
      Free_ChainSubRuleSet( &csrs[n], memory );

    FREE( csrs );
  }

  _HB_OPEN_Free_Coverage( &ccsf1->Coverage, memory );
}


/* ChainSubClassRule */

2423
static HB_Error  Load_ChainSubClassRule(
2424 2425 2426 2427
		   HB_ChainContextSubstFormat2*  ccsf2,
		   HB_ChainSubClassRule*         cscr,
		   FT_Stream                      stream )
{
2428
  HB_Error error;
2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
  FT_Memory memory = stream->memory;

  FT_UShort               n, count;

  FT_UShort*              b;
  FT_UShort*              i;
  FT_UShort*              l;
  HB_SubstLookupRecord*  slr;
  FT_Bool*                d;


  if ( ACCESS_Frame( 2L ) )
    return error;

  cscr->BacktrackGlyphCount = GET_UShort();

  FORGET_Frame();

  if ( cscr->BacktrackGlyphCount > ccsf2->MaxBacktrackLength )
    ccsf2->MaxBacktrackLength = cscr->BacktrackGlyphCount;

  cscr->Backtrack = NULL;

  count = cscr->BacktrackGlyphCount;

  if ( ALLOC_ARRAY( cscr->Backtrack, count, FT_UShort ) )
    return error;

  b = cscr->Backtrack;
  d = ccsf2->BacktrackClassDef.Defined;

  if ( ACCESS_Frame( count * 2L ) )
    goto Fail4;

  for ( n = 0; n < count; n++ )
  {
    b[n] = GET_UShort();

    /* We check whether the specific class is used at all.  If not,
       class 0 is used instead.                                     */

    if ( !d[b[n]] )
      b[n] = 0;
  }

  FORGET_Frame();

  if ( ACCESS_Frame( 2L ) )
    goto Fail4;

  cscr->InputGlyphCount = GET_UShort();

  FORGET_Frame();

  if ( cscr->InputGlyphCount > ccsf2->MaxInputLength )
    ccsf2->MaxInputLength = cscr->InputGlyphCount;

  cscr->Input = NULL;

  count = cscr->InputGlyphCount - 1; /* only InputGlyphCount - 1 elements */

  if ( ALLOC_ARRAY( cscr->Input, count, FT_UShort ) )
    goto Fail4;

  i = cscr->Input;
  d = ccsf2->InputClassDef.Defined;

  if ( ACCESS_Frame( count * 2L ) )
    goto Fail3;

  for ( n = 0; n < count; n++ )
  {
    i[n] = GET_UShort();

    if ( !d[i[n]] )
      i[n] = 0;
  }

  FORGET_Frame();

  if ( ACCESS_Frame( 2L ) )
    goto Fail3;

  cscr->LookaheadGlyphCount = GET_UShort();

  FORGET_Frame();

  if ( cscr->LookaheadGlyphCount > ccsf2->MaxLookaheadLength )
    ccsf2->MaxLookaheadLength = cscr->LookaheadGlyphCount;

  cscr->Lookahead = NULL;

  count = cscr->LookaheadGlyphCount;

  if ( ALLOC_ARRAY( cscr->Lookahead, count, FT_UShort ) )
    goto Fail3;

  l = cscr->Lookahead;
  d = ccsf2->LookaheadClassDef.Defined;

  if ( ACCESS_Frame( count * 2L ) )
    goto Fail2;

  for ( n = 0; n < count; n++ )
  {
    l[n] = GET_UShort();

    if ( !d[l[n]] )
      l[n] = 0;
  }

  FORGET_Frame();

  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  cscr->SubstCount = GET_UShort();

  FORGET_Frame();

  cscr->SubstLookupRecord = NULL;

  count = cscr->SubstCount;

  if ( ALLOC_ARRAY( cscr->SubstLookupRecord, count,
		    HB_SubstLookupRecord ) )
    goto Fail2;

  slr = cscr->SubstLookupRecord;

  if ( ACCESS_Frame( count * 4L ) )
    goto Fail1;

  for ( n = 0; n < count; n++ )
  {
    slr[n].SequenceIndex   = GET_UShort();
    slr[n].LookupListIndex = GET_UShort();
  }

  FORGET_Frame();

2570
  return HB_Err_Ok;
2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598

Fail1:
  FREE( slr );

Fail2:
  FREE( l );

Fail3:
  FREE( i );

Fail4:
  FREE( b );
  return error;
}


static void  Free_ChainSubClassRule( HB_ChainSubClassRule*  cscr,
				     FT_Memory               memory )
{
  FREE( cscr->SubstLookupRecord );
  FREE( cscr->Lookahead );
  FREE( cscr->Input );
  FREE( cscr->Backtrack );
}


/* SubClassSet */

2599
static HB_Error  Load_ChainSubClassSet(
2600 2601 2602 2603
		   HB_ChainContextSubstFormat2*  ccsf2,
		   HB_ChainSubClassSet*          cscs,
		   FT_Stream                      stream )
{
2604
  HB_Error error;
2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641
  FT_Memory memory = stream->memory;

  FT_UShort               n = 0, m, count;
  FT_ULong                cur_offset, new_offset, base_offset;

  HB_ChainSubClassRule*  cscr;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 2L ) )
    return error;

  count = cscs->ChainSubClassRuleCount = GET_UShort();

  FORGET_Frame();

  cscs->ChainSubClassRule = NULL;

  if ( ALLOC_ARRAY( cscs->ChainSubClassRule, count,
		    HB_ChainSubClassRule ) )
    return error;

  cscr = cscs->ChainSubClassRule;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
	 ( error = Load_ChainSubClassRule( ccsf2, &cscr[n],
2642
					   stream ) ) != HB_Err_Ok )
2643 2644 2645 2646
      goto Fail;
    (void)FILE_Seek( cur_offset );
  }

2647
  return HB_Err_Ok;
2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677

Fail:
  for ( m = 0; m < n; m++ )
    Free_ChainSubClassRule( &cscr[m], memory );

  FREE( cscr );
  return error;
}


static void  Free_ChainSubClassSet( HB_ChainSubClassSet*  cscs,
				    FT_Memory              memory )
{
  FT_UShort               n, count;

  HB_ChainSubClassRule*  cscr;


  if ( cscs->ChainSubClassRule )
  {
    count = cscs->ChainSubClassRuleCount;
    cscr  = cscs->ChainSubClassRule;

    for ( n = 0; n < count; n++ )
      Free_ChainSubClassRule( &cscr[n], memory );

    FREE( cscr );
  }
}

2678
static HB_Error GSUB_Load_EmptyOrClassDefinition( HB_ClassDefinition*  cd,
2679 2680 2681 2682 2683
					     FT_UShort             limit,
					     FT_ULong              class_offset,
					     FT_ULong              base_offset,
					     FT_Stream             stream )
{
2684
  HB_Error error;
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
  FT_ULong               cur_offset;

  cur_offset = FILE_Pos();

  if ( class_offset )
    {
      if ( !FILE_Seek( class_offset + base_offset ) )
	error = _HB_OPEN_Load_ClassDefinition( cd, limit, stream );
    }
  else
     error = _HB_OPEN_Load_EmptyClassDefinition ( cd, stream );

2697
  if (error == HB_Err_Ok)
2698 2699 2700 2701 2702 2703 2704 2705
    (void)FILE_Seek( cur_offset ); /* Changes error as a side-effect */

  return error;
}


/* ChainContextSubstFormat2 */

2706
static HB_Error  Load_ChainContextSubst2(
2707 2708 2709
		   HB_ChainContextSubstFormat2*  ccsf2,
		   FT_Stream                      stream )
{
2710
  HB_Error error;
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
  FT_Memory memory = stream->memory;

  FT_UShort              n = 0, m, count;
  FT_ULong               cur_offset, new_offset, base_offset;
  FT_ULong               backtrack_offset, input_offset, lookahead_offset;

  HB_ChainSubClassSet*  cscs;


  base_offset = FILE_Pos() - 2;

  if ( ACCESS_Frame( 2L ) )
    return error;

  new_offset = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
2731
       ( error = _HB_OPEN_Load_Coverage( &ccsf2->Coverage, stream ) ) != HB_Err_Ok )
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751
    return error;
  (void)FILE_Seek( cur_offset );

  if ( ACCESS_Frame( 8L ) )
    goto Fail5;

  backtrack_offset = GET_UShort();
  input_offset     = GET_UShort();
  lookahead_offset = GET_UShort();

  /* `ChainSubClassSetCount' is the upper limit for input class values,
     thus we read it now to make an additional safety check. No limit
     is known or needed for the other two class definitions          */

  count = ccsf2->ChainSubClassSetCount = GET_UShort();

  FORGET_Frame();

  if ( ( error = GSUB_Load_EmptyOrClassDefinition( &ccsf2->BacktrackClassDef, 65535,
					      backtrack_offset, base_offset,
2752
					      stream ) ) != HB_Err_Ok )
2753
      goto Fail5;
2754

2755 2756
  if ( ( error = GSUB_Load_EmptyOrClassDefinition( &ccsf2->InputClassDef, count,
					      input_offset, base_offset,
2757
					      stream ) ) != HB_Err_Ok )
2758 2759 2760
      goto Fail4;
  if ( ( error = GSUB_Load_EmptyOrClassDefinition( &ccsf2->LookaheadClassDef, 65535,
					      lookahead_offset, base_offset,
2761
					      stream ) ) != HB_Err_Ok )
2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787
    goto Fail3;

  ccsf2->ChainSubClassSet   = NULL;
  ccsf2->MaxBacktrackLength = 0;
  ccsf2->MaxInputLength     = 0;
  ccsf2->MaxLookaheadLength = 0;

  if ( ALLOC_ARRAY( ccsf2->ChainSubClassSet, count, HB_ChainSubClassSet ) )
    goto Fail2;

  cscs = ccsf2->ChainSubClassSet;

  for ( n = 0; n < count; n++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail1;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    if ( new_offset != base_offset )      /* not a NULL offset */
    {
      cur_offset = FILE_Pos();
      if ( FILE_Seek( new_offset ) ||
	   ( error = Load_ChainSubClassSet( ccsf2, &cscs[n],
2788
					    stream ) ) != HB_Err_Ok )
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
	goto Fail1;
      (void)FILE_Seek( cur_offset );
    }
    else
    {
      /* we create a ChainSubClassSet table with no entries */

      ccsf2->ChainSubClassSet[n].ChainSubClassRuleCount = 0;
      ccsf2->ChainSubClassSet[n].ChainSubClassRule      = NULL;
    }
  }

2801
  return HB_Err_Ok;
2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852

Fail1:
  for ( m = 0; m < n; m++ )
    Free_ChainSubClassSet( &cscs[m], memory );

  FREE( cscs );

Fail2:
  _HB_OPEN_Free_ClassDefinition( &ccsf2->LookaheadClassDef, memory );

Fail3:
  _HB_OPEN_Free_ClassDefinition( &ccsf2->InputClassDef, memory );

Fail4:
  _HB_OPEN_Free_ClassDefinition( &ccsf2->BacktrackClassDef, memory );

Fail5:
  _HB_OPEN_Free_Coverage( &ccsf2->Coverage, memory );
  return error;
}


static void  Free_ChainContextSubst2( HB_ChainContextSubstFormat2*  ccsf2,
				 FT_Memory                      memory )
{
  FT_UShort              n, count;

  HB_ChainSubClassSet*  cscs;


  if ( ccsf2->ChainSubClassSet )
  {
    count = ccsf2->ChainSubClassSetCount;
    cscs  = ccsf2->ChainSubClassSet;

    for ( n = 0; n < count; n++ )
      Free_ChainSubClassSet( &cscs[n], memory );

    FREE( cscs );
  }

  _HB_OPEN_Free_ClassDefinition( &ccsf2->LookaheadClassDef, memory );
  _HB_OPEN_Free_ClassDefinition( &ccsf2->InputClassDef, memory );
  _HB_OPEN_Free_ClassDefinition( &ccsf2->BacktrackClassDef, memory );

  _HB_OPEN_Free_Coverage( &ccsf2->Coverage, memory );
}


/* ChainContextSubstFormat3 */

2853
static HB_Error  Load_ChainContextSubst3(
2854 2855 2856
		   HB_ChainContextSubstFormat3*  ccsf3,
		   FT_Stream                      stream )
{
2857
  HB_Error error;
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
  FT_Memory memory = stream->memory;

  FT_UShort               n, nb = 0, ni =0, nl = 0, m, count;
  FT_UShort               backtrack_count, input_count, lookahead_count;
  FT_ULong                cur_offset, new_offset, base_offset;

  HB_Coverage*           b;
  HB_Coverage*           i;
  HB_Coverage*           l;
  HB_SubstLookupRecord*  slr;


  base_offset = FILE_Pos() - 2L;

  if ( ACCESS_Frame( 2L ) )
    return error;

  ccsf3->BacktrackGlyphCount = GET_UShort();

  FORGET_Frame();

  ccsf3->BacktrackCoverage = NULL;

  backtrack_count = ccsf3->BacktrackGlyphCount;

  if ( ALLOC_ARRAY( ccsf3->BacktrackCoverage, backtrack_count,
		    HB_Coverage ) )
    return error;

  b = ccsf3->BacktrackCoverage;

  for ( nb = 0; nb < backtrack_count; nb++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail4;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
2900
	 ( error = _HB_OPEN_Load_Coverage( &b[nb], stream ) ) != HB_Err_Ok )
2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
      goto Fail4;
    (void)FILE_Seek( cur_offset );
  }

  if ( ACCESS_Frame( 2L ) )
    goto Fail4;

  ccsf3->InputGlyphCount = GET_UShort();

  FORGET_Frame();

  ccsf3->InputCoverage = NULL;

  input_count = ccsf3->InputGlyphCount;

  if ( ALLOC_ARRAY( ccsf3->InputCoverage, input_count, HB_Coverage ) )
    goto Fail4;

  i = ccsf3->InputCoverage;

  for ( ni = 0; ni < input_count; ni++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail3;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
2932
	 ( error = _HB_OPEN_Load_Coverage( &i[ni], stream ) ) != HB_Err_Ok )
2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
      goto Fail3;
    (void)FILE_Seek( cur_offset );
  }

  if ( ACCESS_Frame( 2L ) )
    goto Fail3;

  ccsf3->LookaheadGlyphCount = GET_UShort();

  FORGET_Frame();

  ccsf3->LookaheadCoverage = NULL;

  lookahead_count = ccsf3->LookaheadGlyphCount;

  if ( ALLOC_ARRAY( ccsf3->LookaheadCoverage, lookahead_count,
		    HB_Coverage ) )
    goto Fail3;

  l = ccsf3->LookaheadCoverage;

  for ( nl = 0; nl < lookahead_count; nl++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail2;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
2965
	 ( error = _HB_OPEN_Load_Coverage( &l[nl], stream ) ) != HB_Err_Ok )
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997
      goto Fail2;
    (void)FILE_Seek( cur_offset );
  }

  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  ccsf3->SubstCount = GET_UShort();

  FORGET_Frame();

  ccsf3->SubstLookupRecord = NULL;

  count = ccsf3->SubstCount;

  if ( ALLOC_ARRAY( ccsf3->SubstLookupRecord, count,
		    HB_SubstLookupRecord ) )
    goto Fail2;

  slr = ccsf3->SubstLookupRecord;

  if ( ACCESS_Frame( count * 4L ) )
    goto Fail1;

  for ( n = 0; n < count; n++ )
  {
    slr[n].SequenceIndex   = GET_UShort();
    slr[n].LookupListIndex = GET_UShort();
  }

  FORGET_Frame();

2998
  return HB_Err_Ok;
2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070

Fail1:
  FREE( slr );

Fail2:
  for ( m = 0; m < nl; m++ )
    _HB_OPEN_Free_Coverage( &l[m], memory );

  FREE( l );

Fail3:
  for ( m = 0; m < ni; m++ )
    _HB_OPEN_Free_Coverage( &i[m], memory );

  FREE( i );

Fail4:
  for ( m = 0; m < nb; m++ )
    _HB_OPEN_Free_Coverage( &b[m], memory );

  FREE( b );
  return error;
}


static void  Free_ChainContextSubst3( HB_ChainContextSubstFormat3*  ccsf3,
				 FT_Memory                      memory )
{
  FT_UShort      n, count;

  HB_Coverage*  c;


  FREE( ccsf3->SubstLookupRecord );

  if ( ccsf3->LookaheadCoverage )
  {
    count = ccsf3->LookaheadGlyphCount;
    c     = ccsf3->LookaheadCoverage;

    for ( n = 0; n < count; n++ )
      _HB_OPEN_Free_Coverage( &c[n], memory );

    FREE( c );
  }

  if ( ccsf3->InputCoverage )
  {
    count = ccsf3->InputGlyphCount;
    c     = ccsf3->InputCoverage;

    for ( n = 0; n < count; n++ )
      _HB_OPEN_Free_Coverage( &c[n], memory );

    FREE( c );
  }

  if ( ccsf3->BacktrackCoverage )
  {
    count = ccsf3->BacktrackGlyphCount;
    c     = ccsf3->BacktrackCoverage;

    for ( n = 0; n < count; n++ )
      _HB_OPEN_Free_Coverage( &c[n], memory );

    FREE( c );
  }
}


/* ChainContextSubst */

3071
static HB_Error  Load_ChainContextSubst( HB_GSUB_SubTable* st,
3072 3073
					 FT_Stream         stream )
{
3074
  HB_Error error;
3075 3076 3077 3078 3079 3080 3081 3082 3083
  HB_ChainContextSubst*  ccs = &st->chain;

  if ( ACCESS_Frame( 2L ) )
    return error;

  ccs->SubstFormat = GET_UShort();

  FORGET_Frame();

3084 3085 3086 3087 3088
  switch ( ccs->SubstFormat ) {
    case 1:  return Load_ChainContextSubst1( &ccs->ccsf.ccsf1, stream );
    case 2:  return Load_ChainContextSubst2( &ccs->ccsf.ccsf2, stream );
    case 3:  return Load_ChainContextSubst3( &ccs->ccsf.ccsf3, stream );
    default: return _hb_err(HB_Err_Invalid_GSUB_SubTable_Format);
3089 3090
  }

3091
  return HB_Err_Ok;               /* never reached */
3092 3093 3094 3095 3096 3097 3098 3099
}


static void  Free_ChainContextSubst( HB_GSUB_SubTable* st,
				     FT_Memory         memory )
{
  HB_ChainContextSubst*  ccs = &st->chain;

3100 3101 3102 3103 3104
  switch ( ccs->SubstFormat ) {
    case 1:  Free_ChainContextSubst1( &ccs->ccsf.ccsf1, memory ); break;
    case 2:  Free_ChainContextSubst2( &ccs->ccsf.ccsf2, memory ); break;
    case 3:  Free_ChainContextSubst3( &ccs->ccsf.ccsf3, memory ); break;
    default:							  break;
3105 3106 3107 3108
  }
}


3109
static HB_Error  Lookup_ChainContextSubst1( HB_GSUBHeader*               gsub,
3110 3111 3112 3113 3114 3115 3116 3117 3118
					    HB_ChainContextSubstFormat1* ccsf1,
					    HB_Buffer                    buffer,
					    FT_UShort                     flags,
					    FT_UShort                     context_length,
					    int                           nesting_level )
{
  FT_UShort          index, property;
  FT_UShort          i, j, k, num_csr;
  FT_UShort          bgc, igc, lgc;
3119
  HB_Error           error;
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193

  HB_ChainSubRule*  csr;
  HB_ChainSubRule   curr_csr;
  HB_GDEFHeader*    gdef;


  gdef = gsub->gdef;

  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
    return error;

  error = _HB_OPEN_Coverage_Index( &ccsf1->Coverage, IN_CURGLYPH(), &index );
  if ( error )
    return error;

  csr     = ccsf1->ChainSubRuleSet[index].ChainSubRule;
  num_csr = ccsf1->ChainSubRuleSet[index].ChainSubRuleCount;

  for ( k = 0; k < num_csr; k++ )
  {
    curr_csr = csr[k];
    bgc      = curr_csr.BacktrackGlyphCount;
    igc      = curr_csr.InputGlyphCount;
    lgc      = curr_csr.LookaheadGlyphCount;

    if ( context_length != 0xFFFF && context_length < igc )
      goto next_chainsubrule;

    /* check whether context is too long; it is a first guess only */

    if ( bgc > buffer->out_pos || buffer->in_pos + igc + lgc > buffer->in_length )
      goto next_chainsubrule;

    if ( bgc )
    {
      /* since we don't know in advance the number of glyphs to inspect,
	 we search backwards for matches in the backtrack glyph array    */

      for ( i = 0, j = buffer->out_pos - 1; i < bgc; i++, j-- )
      {
	while ( CHECK_Property( gdef, OUT_ITEM( j ), flags, &property ) )
	{
	  if ( error && error != HB_Err_Not_Covered )
	    return error;

	  if ( j + 1 == bgc - i )
	    goto next_chainsubrule;
	  j--;
	}

	/* In OpenType 1.3, it is undefined whether the offsets of
	   backtrack glyphs is in logical order or not.  Version 1.4
	   will clarify this:

	     Logical order -      a  b  c  d  e  f  g  h  i  j
					      i
	     Input offsets -                  0  1
	     Backtrack offsets -  3  2  1  0
	     Lookahead offsets -                    0  1  2  3           */

	if ( OUT_GLYPH( j ) != curr_csr.Backtrack[i] )
	  goto next_chainsubrule;
      }
    }

    /* Start at 1 because [0] is implied */

    for ( i = 1, j = buffer->in_pos + 1; i < igc; i++, j++ )
    {
      while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
      {
	if ( error && error != HB_Err_Not_Covered )
	  return error;

3194
	if ( j + igc - i + lgc == (FT_Long)buffer->in_length )
3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212
	  goto next_chainsubrule;
	j++;
      }

      if ( IN_GLYPH( j ) != curr_csr.Input[i - 1] )
	  goto next_chainsubrule;
    }

    /* we are starting to check for lookahead glyphs right after the
       last context glyph                                            */

    for ( i = 0; i < lgc; i++, j++ )
    {
      while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
      {
	if ( error && error != HB_Err_Not_Covered )
	  return error;

3213
	if ( j + lgc - i == (FT_Long)buffer->in_length )
3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
	  goto next_chainsubrule;
	j++;
      }

      if ( IN_GLYPH( j ) != curr_csr.Lookahead[i] )
	goto next_chainsubrule;
    }

    return Do_ContextSubst( gsub, igc,
			    curr_csr.SubstCount,
			    curr_csr.SubstLookupRecord,
			    buffer,
			    nesting_level );

  next_chainsubrule:
    ;
  }

  return HB_Err_Not_Covered;
}


3236
static HB_Error  Lookup_ChainContextSubst2( HB_GSUBHeader*               gsub,
3237 3238 3239 3240 3241 3242 3243 3244
					    HB_ChainContextSubstFormat2* ccsf2,
					    HB_Buffer                    buffer,
					    FT_UShort                     flags,
					    FT_UShort                     context_length,
					    int                           nesting_level )
{
  FT_UShort              index, property;
  FT_Memory              memory;
3245
  HB_Error               error;
3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298
  FT_UShort              i, j, k;
  FT_UShort              bgc, igc, lgc;
  FT_UShort              known_backtrack_classes,
			 known_input_classes,
			 known_lookahead_classes;

  FT_UShort*             backtrack_classes;
  FT_UShort*             input_classes;
  FT_UShort*             lookahead_classes;

  FT_UShort*             bc;
  FT_UShort*             ic;
  FT_UShort*             lc;

  HB_ChainSubClassSet*  cscs;
  HB_ChainSubClassRule  ccsr;
  HB_GDEFHeader*        gdef;


  gdef = gsub->gdef;
  memory = gsub->memory;

  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
    return error;

  /* Note: The coverage table in format 2 doesn't give an index into
	   anything.  It just lets us know whether or not we need to
	   do any lookup at all.                                     */

  error = _HB_OPEN_Coverage_Index( &ccsf2->Coverage, IN_CURGLYPH(), &index );
  if ( error )
    return error;

  if ( ALLOC_ARRAY( backtrack_classes, ccsf2->MaxBacktrackLength, FT_UShort ) )
    return error;
  known_backtrack_classes = 0;

  if ( ALLOC_ARRAY( input_classes, ccsf2->MaxInputLength, FT_UShort ) )
    goto End3;
  known_input_classes = 1;

  if ( ALLOC_ARRAY( lookahead_classes, ccsf2->MaxLookaheadLength, FT_UShort ) )
    goto End2;
  known_lookahead_classes = 0;

  error = _HB_OPEN_Get_Class( &ccsf2->InputClassDef, IN_CURGLYPH(),
		     &input_classes[0], NULL );
  if ( error && error != HB_Err_Not_Covered )
    goto End1;

  cscs = &ccsf2->ChainSubClassSet[input_classes[0]];
  if ( !cscs )
  {
3299
    error = _hb_err(HB_Err_Invalid_GSUB_SubTable);
3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
    goto End1;
  }

  for ( k = 0; k < cscs->ChainSubClassRuleCount; k++ )
  {
    ccsr = cscs->ChainSubClassRule[k];
    bgc  = ccsr.BacktrackGlyphCount;
    igc  = ccsr.InputGlyphCount;
    lgc  = ccsr.LookaheadGlyphCount;

    if ( context_length != 0xFFFF && context_length < igc )
      goto next_chainsubclassrule;

    /* check whether context is too long; it is a first guess only */

    if ( bgc > buffer->out_pos || buffer->in_pos + igc + lgc > buffer->in_length )
      goto next_chainsubclassrule;

    if ( bgc )
    {
      /* Since we don't know in advance the number of glyphs to inspect,
	 we search backwards for matches in the backtrack glyph array.
	 Note that `known_backtrack_classes' starts at index 0.         */

      bc       = ccsr.Backtrack;

      for ( i = 0, j = buffer->out_pos - 1; i < bgc; i++, j-- )
      {
	while ( CHECK_Property( gdef, OUT_ITEM( j ), flags, &property ) )
	{
	  if ( error && error != HB_Err_Not_Covered )
	    goto End1;

	  if ( j + 1 == bgc - i )
	    goto next_chainsubclassrule;
	  j--;
	}

	if ( i >= known_backtrack_classes )
	{
	  /* Keeps us from having to do this for each rule */

	  error = _HB_OPEN_Get_Class( &ccsf2->BacktrackClassDef, OUT_GLYPH( j ),
			     &backtrack_classes[i], NULL );
	  if ( error && error != HB_Err_Not_Covered )
	    goto End1;
	  known_backtrack_classes = i;
	}

	if ( bc[i] != backtrack_classes[i] )
	  goto next_chainsubclassrule;
      }
    }

    ic       = ccsr.Input;

    /* Start at 1 because [0] is implied */

    for ( i = 1, j = buffer->in_pos + 1; i < igc; i++, j++ )
    {
      while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
      {
	if ( error && error != HB_Err_Not_Covered )
	  goto End1;

3365
	if ( j + igc - i + lgc == (FT_Long)buffer->in_length )
3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394
	  goto next_chainsubclassrule;
	j++;
      }

      if ( i >= known_input_classes )
      {
	error = _HB_OPEN_Get_Class( &ccsf2->InputClassDef, IN_GLYPH( j ),
			   &input_classes[i], NULL );
	if ( error && error != HB_Err_Not_Covered )
	  goto End1;
	known_input_classes = i;
      }

      if ( ic[i - 1] != input_classes[i] )
	goto next_chainsubclassrule;
    }

    /* we are starting to check for lookahead glyphs right after the
       last context glyph                                            */

    lc       = ccsr.Lookahead;

    for ( i = 0; i < lgc; i++, j++ )
    {
      while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
      {
	if ( error && error != HB_Err_Not_Covered )
	  goto End1;

3395
	if ( j + lgc - i == (FT_Long)buffer->in_length )
3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437
	  goto next_chainsubclassrule;
	j++;
      }

      if ( i >= known_lookahead_classes )
      {
	error = _HB_OPEN_Get_Class( &ccsf2->LookaheadClassDef, IN_GLYPH( j ),
			   &lookahead_classes[i], NULL );
	if ( error && error != HB_Err_Not_Covered )
	  goto End1;
	known_lookahead_classes = i;
      }

      if ( lc[i] != lookahead_classes[i] )
	goto next_chainsubclassrule;
    }

    error = Do_ContextSubst( gsub, igc,
			     ccsr.SubstCount,
			     ccsr.SubstLookupRecord,
			     buffer,
			     nesting_level );
    goto End1;

  next_chainsubclassrule:
    ;
  }

  error = HB_Err_Not_Covered;

End1:
  FREE( lookahead_classes );

End2:
  FREE( input_classes );

End3:
  FREE( backtrack_classes );
  return error;
}


3438
static HB_Error  Lookup_ChainContextSubst3( HB_GSUBHeader*               gsub,
3439 3440 3441 3442 3443 3444 3445 3446
					    HB_ChainContextSubstFormat3* ccsf3,
					    HB_Buffer                    buffer,
					    FT_UShort                     flags,
					    FT_UShort                     context_length,
					    int                           nesting_level )
{
  FT_UShort        index, i, j, property;
  FT_UShort        bgc, igc, lgc;
3447
  HB_Error         error;
3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505

  HB_Coverage*    bc;
  HB_Coverage*    ic;
  HB_Coverage*    lc;
  HB_GDEFHeader*  gdef;


  gdef = gsub->gdef;

  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
    return error;

  bgc = ccsf3->BacktrackGlyphCount;
  igc = ccsf3->InputGlyphCount;
  lgc = ccsf3->LookaheadGlyphCount;

  if ( context_length != 0xFFFF && context_length < igc )
    return HB_Err_Not_Covered;

  /* check whether context is too long; it is a first guess only */

  if ( bgc > buffer->out_pos || buffer->in_pos + igc + lgc > buffer->in_length )
    return HB_Err_Not_Covered;

  if ( bgc )
  {
    /* Since we don't know in advance the number of glyphs to inspect,
       we search backwards for matches in the backtrack glyph array    */

    bc       = ccsf3->BacktrackCoverage;

    for ( i = 0, j = buffer->out_pos - 1; i < bgc; i++, j-- )
    {
      while ( CHECK_Property( gdef, OUT_ITEM( j ), flags, &property ) )
      {
	if ( error && error != HB_Err_Not_Covered )
	  return error;

	if ( j + 1 == bgc - i )
	  return HB_Err_Not_Covered;
	j--;
      }

      error = _HB_OPEN_Coverage_Index( &bc[i], OUT_GLYPH( j ), &index );
      if ( error )
	return error;
    }
  }

  ic       = ccsf3->InputCoverage;

  for ( i = 0, j = buffer->in_pos; i < igc; i++, j++ )
  {
    /* We already called CHECK_Property for IN_GLYPH( buffer->in_pos ) */
    while ( j > buffer->in_pos && CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
    {
      if ( error && error != HB_Err_Not_Covered )
	return error;
3506 3507

      if ( j + igc - i + lgc == (FT_Long)buffer->in_length )
3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528
	return HB_Err_Not_Covered;
      j++;
    }

    error = _HB_OPEN_Coverage_Index( &ic[i], IN_GLYPH( j ), &index );
    if ( error )
      return error;
  }

  /* we are starting for lookahead glyphs right after the last context
     glyph                                                             */

  lc       = ccsf3->LookaheadCoverage;

  for ( i = 0; i < lgc; i++, j++ )
  {
    while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
    {
      if ( error && error != HB_Err_Not_Covered )
	return error;

3529
      if ( j + lgc - i == (FT_Long)buffer->in_length )
3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546
	return HB_Err_Not_Covered;
      j++;
    }

    error = _HB_OPEN_Coverage_Index( &lc[i], IN_GLYPH( j ), &index );
    if ( error )
      return error;
  }

  return Do_ContextSubst( gsub, igc,
			  ccsf3->SubstCount,
			  ccsf3->SubstLookupRecord,
			  buffer,
			  nesting_level );
}


3547
static HB_Error  Lookup_ChainContextSubst( HB_GSUBHeader*    gsub,
3548 3549 3550 3551 3552 3553 3554 3555
					   HB_GSUB_SubTable* st,
					   HB_Buffer         buffer,
					   FT_UShort          flags,
					   FT_UShort          context_length,
					   int                nesting_level )
{
  HB_ChainContextSubst*  ccs = &st->chain;

3556 3557 3558 3559 3560
  switch ( ccs->SubstFormat ) {
    case 1:  return Lookup_ChainContextSubst1( gsub, &ccs->ccsf.ccsf1, buffer, flags, context_length, nesting_level );
    case 2:  return Lookup_ChainContextSubst2( gsub, &ccs->ccsf.ccsf2, buffer, flags, context_length, nesting_level );
    case 3:  return Lookup_ChainContextSubst3( gsub, &ccs->ccsf.ccsf3, buffer, flags, context_length, nesting_level );
    default: return _hb_err(HB_Err_Invalid_GSUB_SubTable_Format);
3561 3562 3563 3564
  }
}


3565
static HB_Error  Load_ReverseChainContextSubst( HB_GSUB_SubTable* st,
3566 3567
					        FT_Stream         stream )
{
3568
  HB_Error error;
3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585
  FT_Memory memory = stream->memory;
  HB_ReverseChainContextSubst*  rccs = &st->reverse;

  FT_UShort               m, count;

  FT_UShort               nb = 0, nl = 0, n;
  FT_UShort               backtrack_count, lookahead_count;
  FT_ULong                cur_offset, new_offset, base_offset;

  HB_Coverage*           b;
  HB_Coverage*           l;
  FT_UShort*              sub;

  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 2L ) )
    return error;
3586

3587
  rccs->SubstFormat = GET_UShort();
3588

3589
  if ( rccs->SubstFormat != 1 )
3590
    return _hb_err(HB_Err_Invalid_GSUB_SubTable_Format);
3591

3592
  FORGET_Frame();
3593 3594 3595

  if ( ACCESS_Frame( 2L ) )
    return error;
3596

3597 3598 3599 3600 3601 3602
  new_offset = GET_UShort() + base_offset;

  FORGET_Frame();

  cur_offset = FILE_Pos();
  if ( FILE_Seek( new_offset ) ||
3603
       ( error = _HB_OPEN_Load_Coverage( &rccs->Coverage, stream ) ) != HB_Err_Ok )
3604 3605 3606
    return error;
  (void)FILE_Seek( cur_offset );

3607

3608 3609 3610 3611
  if ( ACCESS_Frame( 2L ) )
    goto Fail4;

  rccs->BacktrackGlyphCount = GET_UShort();
3612

3613 3614 3615 3616 3617 3618 3619 3620 3621
  FORGET_Frame();

  rccs->BacktrackCoverage = NULL;

  backtrack_count = rccs->BacktrackGlyphCount;

  if ( ALLOC_ARRAY( rccs->BacktrackCoverage, backtrack_count,
		    HB_Coverage ) )
    goto Fail4;
3622

3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635
  b = rccs->BacktrackCoverage;

  for ( nb = 0; nb < backtrack_count; nb++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail3;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
3636
	 ( error = _HB_OPEN_Load_Coverage( &b[nb], stream ) ) != HB_Err_Ok )
3637 3638 3639 3640 3641 3642 3643 3644 3645
      goto Fail3;
    (void)FILE_Seek( cur_offset );
  }


  if ( ACCESS_Frame( 2L ) )
    goto Fail3;

  rccs->LookaheadGlyphCount = GET_UShort();
3646

3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669
  FORGET_Frame();

  rccs->LookaheadCoverage = NULL;

  lookahead_count = rccs->LookaheadGlyphCount;

  if ( ALLOC_ARRAY( rccs->LookaheadCoverage, lookahead_count,
		    HB_Coverage ) )
    goto Fail3;

  l = rccs->LookaheadCoverage;

  for ( nl = 0; nl < lookahead_count; nl++ )
  {
    if ( ACCESS_Frame( 2L ) )
      goto Fail2;

    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
3670
	 ( error = _HB_OPEN_Load_Coverage( &l[nl], stream ) ) != HB_Err_Ok )
3671 3672 3673
      goto Fail2;
    (void)FILE_Seek( cur_offset );
  }
3674

3675 3676 3677 3678
  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  rccs->GlyphCount = GET_UShort();
3679

3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690
  FORGET_Frame();

  rccs->Substitute = NULL;

  count = rccs->GlyphCount;

  if ( ALLOC_ARRAY( rccs->Substitute, count,
		    FT_UShort ) )
    goto Fail2;

  sub = rccs->Substitute;
3691

3692 3693
  if ( ACCESS_Frame( count * 2L ) )
    goto Fail1;
3694

3695 3696
  for ( n = 0; n < count; n++ )
    sub[n] = GET_UShort();
3697

3698
  FORGET_Frame();
3699

3700
  return HB_Err_Ok;
3701 3702 3703

Fail1:
  FREE( sub );
3704

3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717
Fail2:
  for ( m = 0; m < nl; m++ )
    _HB_OPEN_Free_Coverage( &l[m], memory );

  FREE( l );

Fail3:
  for ( m = 0; m < nb; m++ )
    _HB_OPEN_Free_Coverage( &b[m], memory );

  FREE( b );

Fail4:
3718
  _HB_OPEN_Free_Coverage( &rccs->Coverage, memory );
3719 3720 3721 3722 3723 3724
  return error;
}


static void  Free_ReverseChainContextSubst( HB_GSUB_SubTable* st,
					    FT_Memory         memory )
3725
{
3726 3727 3728 3729 3730 3731
  FT_UShort      n, count;
  HB_ReverseChainContextSubst*  rccs = &st->reverse;

  HB_Coverage*  c;

  _HB_OPEN_Free_Coverage( &rccs->Coverage, memory );
3732

3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758
  if ( rccs->LookaheadCoverage )
  {
    count = rccs->LookaheadGlyphCount;
    c     = rccs->LookaheadCoverage;

    for ( n = 0; n < count; n++ )
      _HB_OPEN_Free_Coverage( &c[n], memory );

    FREE( c );
  }

  if ( rccs->BacktrackCoverage )
  {
    count = rccs->BacktrackGlyphCount;
    c     = rccs->BacktrackCoverage;

    for ( n = 0; n < count; n++ )
      _HB_OPEN_Free_Coverage( &c[n], memory );

    FREE( c );
  }

  FREE ( rccs->Substitute );
}


3759
static HB_Error  Lookup_ReverseChainContextSubst( HB_GSUBHeader*    gsub,
3760 3761
						  HB_GSUB_SubTable* st,
						  HB_Buffer         buffer,
3762 3763 3764
						  FT_UShort         flags,
						  FT_UShort         context_length,
						  int               nesting_level )
3765 3766 3767
{
  FT_UShort        index, input_index, i, j, property;
  FT_UShort        bgc, lgc;
3768
  HB_Error         error;
3769 3770 3771 3772 3773 3774

  HB_ReverseChainContextSubst*  rccs = &st->reverse;
  HB_Coverage*    bc;
  HB_Coverage*    lc;
  HB_GDEFHeader*  gdef;

3775 3776 3777
  if ( nesting_level != 1 || context_length != 0xFFFF )
    return HB_Err_Not_Covered;

3778 3779
  gdef = gsub->gdef;

3780
  if ( CHECK_Property( gdef, IN_CURITEM(), flags, &property ) )
3781 3782 3783 3784 3785 3786
    return error;

  bgc = rccs->BacktrackGlyphCount;
  lgc = rccs->LookaheadGlyphCount;

  /* check whether context is too long; it is a first guess only */
3787

3788
  if ( bgc > buffer->in_pos || buffer->in_pos + 1 + lgc > buffer->in_length )
3789
    return HB_Err_Not_Covered;
3790

3791 3792 3793 3794 3795 3796 3797
  if ( bgc )
  {
    /* Since we don't know in advance the number of glyphs to inspect,
       we search backwards for matches in the backtrack glyph array    */

    bc       = rccs->BacktrackCoverage;

3798
    for ( i = 0, j = buffer->in_pos - 1; i < bgc; i++, j-- )
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815
    {
      while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
      {
	if ( error && error != HB_Err_Not_Covered )
	  return error;

	if ( j + 1 == bgc - i )
	  return HB_Err_Not_Covered;
	j--;
      }

      error = _HB_OPEN_Coverage_Index( &bc[i], IN_GLYPH( j ), &index );
      if ( error )
	return error;
    }
  }

3816
  j = buffer->in_pos;
3817

3818 3819 3820 3821 3822 3823
  error = _HB_OPEN_Coverage_Index( &rccs->Coverage, IN_GLYPH( j ), &input_index );
  if ( error )
      return error;

  lc       = rccs->LookaheadCoverage;

3824
  for ( i = 0, j = buffer->in_pos + 1; i < lgc; i++, j++ )
3825 3826 3827 3828 3829 3830
  {
    while ( CHECK_Property( gdef, IN_ITEM( j ), flags, &property ) )
    {
      if ( error && error != HB_Err_Not_Covered )
	return error;

3831
      if ( j + lgc - i == (FT_Long)buffer->in_length )
3832 3833 3834 3835 3836 3837 3838 3839 3840
	return HB_Err_Not_Covered;
      j++;
    }

    error = _HB_OPEN_Coverage_Index( &lc[i], IN_GLYPH( j ), &index );
    if ( error )
      return error;
  }

3841 3842
  IN_CURGLYPH() = rccs->Substitute[input_index];
  buffer->in_pos--; /* Reverse! */
3843

3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854
  return error;
}



/***********
 * GSUB API
 ***********/



3855
HB_Error  HB_GSUB_Select_Script( HB_GSUBHeader*  gsub,
3856 3857 3858 3859 3860 3861 3862 3863 3864 3865
				 FT_ULong         script_tag,
				 FT_UShort*       script_index )
{
  FT_UShort          n;

  HB_ScriptList*    sl;
  HB_ScriptRecord*  sr;


  if ( !gsub || !script_index )
3866
    return HB_Err_Invalid_Argument;
3867 3868 3869 3870 3871 3872 3873 3874 3875

  sl = &gsub->ScriptList;
  sr = sl->ScriptRecord;

  for ( n = 0; n < sl->ScriptCount; n++ )
    if ( script_tag == sr[n].ScriptTag )
    {
      *script_index = n;

3876
      return HB_Err_Ok;
3877 3878 3879 3880 3881 3882 3883
    }

  return HB_Err_Not_Covered;
}



3884
HB_Error  HB_GSUB_Select_Language( HB_GSUBHeader*  gsub,
3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898
				   FT_ULong         language_tag,
				   FT_UShort        script_index,
				   FT_UShort*       language_index,
				   FT_UShort*       req_feature_index )
{
  FT_UShort           n;

  HB_ScriptList*     sl;
  HB_ScriptRecord*   sr;
  HB_Script*         s;
  HB_LangSysRecord*  lsr;


  if ( !gsub || !language_index || !req_feature_index )
3899
    return HB_Err_Invalid_Argument;
3900 3901 3902 3903 3904

  sl = &gsub->ScriptList;
  sr = sl->ScriptRecord;

  if ( script_index >= sl->ScriptCount )
3905
    return HB_Err_Invalid_Argument;
3906 3907 3908 3909 3910 3911 3912 3913 3914 3915

  s   = &sr[script_index].Script;
  lsr = s->LangSysRecord;

  for ( n = 0; n < s->LangSysCount; n++ )
    if ( language_tag == lsr[n].LangSysTag )
    {
      *language_index = n;
      *req_feature_index = lsr[n].LangSys.ReqFeatureIndex;

3916
      return HB_Err_Ok;
3917 3918 3919 3920 3921 3922 3923 3924 3925 3926
    }

  return HB_Err_Not_Covered;
}


/* selecting 0xFFFF for language_index asks for the values of the
   default language (DefaultLangSys)                              */


3927
HB_Error  HB_GSUB_Select_Feature( HB_GSUBHeader*  gsub,
3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946
				  FT_ULong         feature_tag,
				  FT_UShort        script_index,
				  FT_UShort        language_index,
				  FT_UShort*       feature_index )
{
  FT_UShort           n;

  HB_ScriptList*     sl;
  HB_ScriptRecord*   sr;
  HB_Script*         s;
  HB_LangSysRecord*  lsr;
  HB_LangSys*        ls;
  FT_UShort*          fi;

  HB_FeatureList*    fl;
  HB_FeatureRecord*  fr;


  if ( !gsub || !feature_index )
3947
    return HB_Err_Invalid_Argument;
3948 3949 3950 3951 3952 3953 3954 3955

  sl = &gsub->ScriptList;
  sr = sl->ScriptRecord;

  fl = &gsub->FeatureList;
  fr = fl->FeatureRecord;

  if ( script_index >= sl->ScriptCount )
3956
    return HB_Err_Invalid_Argument;
3957 3958 3959 3960 3961 3962 3963 3964 3965

  s   = &sr[script_index].Script;
  lsr = s->LangSysRecord;

  if ( language_index == 0xFFFF )
    ls = &s->DefaultLangSys;
  else
  {
    if ( language_index >= s->LangSysCount )
3966
      return HB_Err_Invalid_Argument;
3967 3968 3969 3970 3971 3972 3973 3974 3975

    ls = &lsr[language_index].LangSys;
  }

  fi = ls->FeatureIndex;

  for ( n = 0; n < ls->FeatureCount; n++ )
  {
    if ( fi[n] >= fl->FeatureCount )
3976
      return _hb_err(HB_Err_Invalid_GSUB_SubTable_Format);
3977 3978 3979 3980 3981

    if ( feature_tag == fr[fi[n]].FeatureTag )
    {
      *feature_index = fi[n];

3982
      return HB_Err_Ok;
3983 3984 3985 3986 3987 3988 3989 3990 3991 3992
    }
  }

  return HB_Err_Not_Covered;
}


/* The next three functions return a null-terminated list */


3993
HB_Error  HB_GSUB_Query_Scripts( HB_GSUBHeader*  gsub,
3994 3995 3996
				 FT_ULong**       script_tag_list )
{
  FT_UShort          n;
3997
  HB_Error           error;
3998 3999 4000 4001 4002 4003 4004 4005
  FT_Memory          memory;
  FT_ULong*          stl;

  HB_ScriptList*    sl;
  HB_ScriptRecord*  sr;


  if ( !gsub || !script_tag_list )
4006
    return HB_Err_Invalid_Argument;
4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021

  memory = gsub->memory;

  sl = &gsub->ScriptList;
  sr = sl->ScriptRecord;

  if ( ALLOC_ARRAY( stl, sl->ScriptCount + 1, FT_ULong ) )
    return error;

  for ( n = 0; n < sl->ScriptCount; n++ )
    stl[n] = sr[n].ScriptTag;
  stl[n] = 0;

  *script_tag_list = stl;

4022
  return HB_Err_Ok;
4023 4024 4025 4026
}



4027
HB_Error  HB_GSUB_Query_Languages( HB_GSUBHeader*  gsub,
4028 4029 4030 4031
				   FT_UShort        script_index,
				   FT_ULong**       language_tag_list )
{
  FT_UShort           n;
4032
  HB_Error            error;
4033 4034 4035 4036 4037 4038 4039 4040 4041 4042
  FT_Memory           memory;
  FT_ULong*           ltl;

  HB_ScriptList*     sl;
  HB_ScriptRecord*   sr;
  HB_Script*         s;
  HB_LangSysRecord*  lsr;


  if ( !gsub || !language_tag_list )
4043
    return HB_Err_Invalid_Argument;
4044 4045

  memory = gsub->memory;
4046

4047 4048 4049 4050
  sl = &gsub->ScriptList;
  sr = sl->ScriptRecord;

  if ( script_index >= sl->ScriptCount )
4051
    return HB_Err_Invalid_Argument;
4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064

  s   = &sr[script_index].Script;
  lsr = s->LangSysRecord;

  if ( ALLOC_ARRAY( ltl, s->LangSysCount + 1, FT_ULong ) )
    return error;

  for ( n = 0; n < s->LangSysCount; n++ )
    ltl[n] = lsr[n].LangSysTag;
  ltl[n] = 0;

  *language_tag_list = ltl;

4065
  return HB_Err_Ok;
4066 4067 4068 4069 4070 4071 4072
}


/* selecting 0xFFFF for language_index asks for the values of the
   default language (DefaultLangSys)                              */


4073
HB_Error  HB_GSUB_Query_Features( HB_GSUBHeader*  gsub,
4074 4075 4076 4077 4078
				  FT_UShort        script_index,
				  FT_UShort        language_index,
				  FT_ULong**       feature_tag_list )
{
  FT_UShort           n;
4079
  HB_Error            error;
4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094
  FT_Memory           memory;
  FT_ULong*           ftl;

  HB_ScriptList*     sl;
  HB_ScriptRecord*   sr;
  HB_Script*         s;
  HB_LangSysRecord*  lsr;
  HB_LangSys*        ls;
  FT_UShort*          fi;

  HB_FeatureList*    fl;
  HB_FeatureRecord*  fr;


  if ( !gsub || !feature_tag_list )
4095
    return HB_Err_Invalid_Argument;
4096 4097

  memory = gsub->memory;
4098

4099 4100 4101 4102 4103 4104 4105
  sl = &gsub->ScriptList;
  sr = sl->ScriptRecord;

  fl = &gsub->FeatureList;
  fr = fl->FeatureRecord;

  if ( script_index >= sl->ScriptCount )
4106
    return HB_Err_Invalid_Argument;
4107 4108 4109 4110 4111 4112 4113 4114 4115

  s   = &sr[script_index].Script;
  lsr = s->LangSysRecord;

  if ( language_index == 0xFFFF )
    ls = &s->DefaultLangSys;
  else
  {
    if ( language_index >= s->LangSysCount )
4116
      return HB_Err_Invalid_Argument;
4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130

    ls = &lsr[language_index].LangSys;
  }

  fi = ls->FeatureIndex;

  if ( ALLOC_ARRAY( ftl, ls->FeatureCount + 1, FT_ULong ) )
    return error;

  for ( n = 0; n < ls->FeatureCount; n++ )
  {
    if ( fi[n] >= fl->FeatureCount )
    {
      FREE( ftl );
4131
      return _hb_err(HB_Err_Invalid_GSUB_SubTable_Format);
4132 4133 4134 4135 4136 4137 4138
    }
    ftl[n] = fr[fi[n]].FeatureTag;
  }
  ftl[n] = 0;

  *feature_tag_list = ftl;

4139
  return HB_Err_Ok;
4140 4141 4142
}


4143
/* Do an individual subtable lookup.  Returns HB_Err_Ok if substitution
4144
   has been done, or HB_Err_Not_Covered if not.                        */
4145 4146
static HB_Error  GSUB_Do_Glyph_Lookup( HB_GSUBHeader* gsub,
				       FT_UShort      lookup_index,
4147
				       HB_Buffer      buffer,
4148 4149
				       FT_UShort      context_length,
				       int            nesting_level )
4150
{
4151
  HB_Error               error = HB_Err_Not_Covered;
4152 4153 4154 4155 4156 4157 4158
  FT_UShort              i, flags, lookup_count;
  HB_Lookup*             lo;
  int                    lookup_type;

  nesting_level++;

  if ( nesting_level > HB_MAX_NESTING_LEVEL )
4159
    return _hb_err(HB_Err_Too_Many_Nested_Contexts);
4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170

  lookup_count = gsub->LookupList.LookupCount;
  if (lookup_index >= lookup_count)
    return error;

  lo    = &gsub->LookupList.Lookup[lookup_index];
  flags = lo->LookupFlag;
  lookup_type = lo->LookupType;

  for ( i = 0; i < lo->SubTableCount; i++ )
  {
4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192
    HB_GSUB_SubTable *st = &lo->SubTable[i].st.gsub;

    switch (lookup_type) {
      case HB_GSUB_LOOKUP_SINGLE:
	error = Lookup_SingleSubst		( gsub, st, buffer, flags, context_length, nesting_level ); break;
      case HB_GSUB_LOOKUP_MULTIPLE:
	error = Lookup_MultipleSubst		( gsub, st, buffer, flags, context_length, nesting_level ); break;
      case HB_GSUB_LOOKUP_ALTERNATE:
	error = Lookup_AlternateSubst		( gsub, st, buffer, flags, context_length, nesting_level ); break;
      case HB_GSUB_LOOKUP_LIGATURE:
	error = Lookup_LigatureSubst		( gsub, st, buffer, flags, context_length, nesting_level ); break;
      case HB_GSUB_LOOKUP_CONTEXT:
	error = Lookup_ContextSubst		( gsub, st, buffer, flags, context_length, nesting_level ); break;
      case HB_GSUB_LOOKUP_CHAIN:
	error = Lookup_ChainContextSubst	( gsub, st, buffer, flags, context_length, nesting_level ); break;
    /*case HB_GSUB_LOOKUP_EXTENSION:
	error = Lookup_ExtensionSubst		( gsub, st, buffer, flags, context_length, nesting_level ); break;*/
      case HB_GSUB_LOOKUP_REVERSE_CHAIN:
	error = Lookup_ReverseChainContextSubst	( gsub, st, buffer, flags, context_length, nesting_level ); break;
      default:
	error = HB_Err_Not_Covered;
    };
4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203

    /* Check whether we have a successful substitution or an error other
       than HB_Err_Not_Covered                                          */
    if ( error != HB_Err_Not_Covered )
      return error;
  }

  return HB_Err_Not_Covered;
}


4204
HB_Error  _HB_GSUB_Load_SubTable( HB_GSUB_SubTable*  st,
4205 4206 4207
				  FT_Stream     stream,
				  FT_UShort     lookup_type )
{
4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218
  switch (lookup_type) {
    case HB_GSUB_LOOKUP_SINGLE:		return Load_SingleSubst			( st, stream );
    case HB_GSUB_LOOKUP_MULTIPLE:	return Load_MultipleSubst		( st, stream );
    case HB_GSUB_LOOKUP_ALTERNATE:	return Load_AlternateSubst		( st, stream );
    case HB_GSUB_LOOKUP_LIGATURE:	return Load_LigatureSubst		( st, stream );
    case HB_GSUB_LOOKUP_CONTEXT:	return Load_ContextSubst		( st, stream );
    case HB_GSUB_LOOKUP_CHAIN:		return Load_ChainContextSubst		( st, stream );
  /*case HB_GSUB_LOOKUP_EXTENSION:	return Load_ExtensionSubst		( st, stream );*/
    case HB_GSUB_LOOKUP_REVERSE_CHAIN:	return Load_ReverseChainContextSubst	( st, stream );
    default:				return _hb_err(HB_Err_Invalid_GSUB_SubTable_Format);
  };
4219 4220 4221 4222 4223 4224 4225
}


void  _HB_GSUB_Free_SubTable( HB_GSUB_SubTable*  st,
			      FT_Memory     memory,
			      FT_UShort     lookup_type )
{
4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236
  switch ( lookup_type ) {
    case HB_GSUB_LOOKUP_SINGLE:		Free_SingleSubst		( st, memory ); return;
    case HB_GSUB_LOOKUP_MULTIPLE:	Free_MultipleSubst		( st, memory ); return;
    case HB_GSUB_LOOKUP_ALTERNATE:	Free_AlternateSubst		( st, memory ); return;
    case HB_GSUB_LOOKUP_LIGATURE:	Free_LigatureSubst		( st, memory ); return;
    case HB_GSUB_LOOKUP_CONTEXT:	Free_ContextSubst		( st, memory ); return;
    case HB_GSUB_LOOKUP_CHAIN:		Free_ChainContextSubst		( st, memory ); return;
  /*case HB_GSUB_LOOKUP_EXTENSION:	Free_ExtensionSubst		( st, memory ); return;*/
    case HB_GSUB_LOOKUP_REVERSE_CHAIN:	Free_ReverseChainContextSubst	( st, memory ); return;
    default:										return;
  };
4237 4238 4239 4240 4241 4242
}



/* apply one lookup to the input string object */

4243 4244 4245
static HB_Error  GSUB_Do_String_Lookup( HB_GSUBHeader* gsub,
					FT_UShort      lookup_index,
					HB_Buffer      buffer )
4246
{
4247
  HB_Error  error, retError = HB_Err_Not_Covered;
4248

4249 4250
  FT_UInt*  properties  = gsub->LookupList.Properties;
  int       lookup_type = gsub->LookupList.Lookup[lookup_index].LookupType;
4251

4252 4253 4254
  const int       nesting_level = 0;
  /* 0xFFFF indicates that we don't have a context length yet */
  const FT_UShort context_length = 0xFFFF;
4255

4256
  switch (lookup_type) {
4257

4258 4259 4260 4261 4262 4263 4264
    case HB_GSUB_LOOKUP_SINGLE:
    case HB_GSUB_LOOKUP_MULTIPLE:
    case HB_GSUB_LOOKUP_ALTERNATE:
    case HB_GSUB_LOOKUP_LIGATURE:
    case HB_GSUB_LOOKUP_CONTEXT:
    case HB_GSUB_LOOKUP_CHAIN:
      /* in/out forward substitution (implemented lazy) */
4265

4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282
      hb_buffer_clear_output ( buffer );
      buffer->in_pos = 0;
      while ( buffer->in_pos < buffer->in_length )
      {
	if ( ~IN_PROPERTIES( buffer->in_pos ) & properties[lookup_index] )
	{
	  error = GSUB_Do_Glyph_Lookup( gsub, lookup_index, buffer, context_length, nesting_level );
	  if ( error )
	  {
	    if ( error != HB_Err_Not_Covered )
	      return error;
	  }
	  else
	    retError = error;
	}
	else
	  error = HB_Err_Not_Covered;
4283

4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294
	if ( error == HB_Err_Not_Covered )
	  if ( COPY_Glyph ( buffer ) )
	    return error;
      }
      /* we shouldn't swap if error occurred.
       *
       * also don't swap if nothing changed (ie HB_Err_Not_Covered).
       * shouldn't matter in that case though.
       */
      if ( retError == HB_Err_Ok )
	hb_buffer_swap( buffer );
4295

4296
      return retError;
4297

4298 4299
    case HB_GSUB_LOOKUP_REVERSE_CHAIN:
      /* in-place backward substitution */
4300

4301 4302 4303 4304
      buffer->in_pos = buffer->in_length - 1;
      do
      {
	if ( ~IN_PROPERTIES( buffer->in_pos ) & properties[lookup_index] )
4305
	{
4306
	  error = GSUB_Do_Glyph_Lookup( gsub, lookup_index, buffer, context_length, nesting_level );
4307
	  if ( error )
4308 4309 4310 4311
	  {
	    if ( error != HB_Err_Not_Covered )
	      return error;
	  }
4312 4313 4314
	  else
	    retError = error;
	}
4315 4316 4317 4318 4319 4320 4321
	else
	  error = HB_Err_Not_Covered;

	if ( error == HB_Err_Not_Covered )
	  buffer->in_pos--;
      }
      while (buffer->in_pos);
4322

4323 4324 4325 4326 4327 4328
      return retError;

  /*case HB_GSUB_LOOKUP_EXTENSION:*/
    default:
      return retError;
  };
4329 4330 4331
}


4332
HB_Error  HB_GSUB_Add_Feature( HB_GSUBHeader*  gsub,
4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343
			       FT_UShort        feature_index,
			       FT_UInt          property )
{
  FT_UShort    i;

  HB_Feature  feature;
  FT_UInt*     properties;
  FT_UShort*   index;
  FT_UShort    lookup_count;

  /* Each feature can only be added once */
4344

4345 4346 4347
  if ( !gsub ||
       feature_index >= gsub->FeatureList.FeatureCount ||
       gsub->FeatureList.ApplyCount == gsub->FeatureList.FeatureCount )
4348
    return HB_Err_Invalid_Argument;
4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364

  gsub->FeatureList.ApplyOrder[gsub->FeatureList.ApplyCount++] = feature_index;

  properties = gsub->LookupList.Properties;

  feature = gsub->FeatureList.FeatureRecord[feature_index].Feature;
  index   = feature.LookupListIndex;
  lookup_count = gsub->LookupList.LookupCount;

  for ( i = 0; i < feature.LookupListCount; i++ )
  {
    FT_UShort lookup_index = index[i];
    if (lookup_index < lookup_count)
      properties[lookup_index] |= property;
  }

4365
  return HB_Err_Ok;
4366 4367 4368 4369
}



4370
HB_Error  HB_GSUB_Clear_Features( HB_GSUBHeader*  gsub )
4371 4372 4373 4374 4375 4376 4377
{
  FT_UShort i;

  FT_UInt*  properties;


  if ( !gsub )
4378
    return HB_Err_Invalid_Argument;
4379 4380 4381 4382 4383 4384 4385 4386

  gsub->FeatureList.ApplyCount = 0;

  properties = gsub->LookupList.Properties;

  for ( i = 0; i < gsub->LookupList.LookupCount; i++ )
    properties[i] = 0;

4387
  return HB_Err_Ok;
4388 4389 4390 4391
}



4392
HB_Error  HB_GSUB_Register_Alternate_Function( HB_GSUBHeader*  gsub,
4393 4394 4395 4396
					       HB_AltFunction  altfunc,
					       void*            data )
{
  if ( !gsub )
4397
    return HB_Err_Invalid_Argument;
4398 4399 4400 4401

  gsub->altfunc = altfunc;
  gsub->data    = data;

4402
  return HB_Err_Ok;
4403 4404
}

4405 4406 4407 4408
/* returns error if one happened, otherwise returns HB_Err_Not_Covered if no
 * feature were applied, or HB_Err_Ok otherwise.
 */
HB_Error  HB_GSUB_Apply_String( HB_GSUBHeader*   gsub,
4409 4410
				HB_Buffer        buffer )
{
4411
  HB_Error          error, retError = HB_Err_Not_Covered;
4412 4413 4414
  FT_UShort         i, j, lookup_count;

  if ( !gsub ||
4415 4416 4417 4418 4419
       !buffer)
    return HB_Err_Invalid_Argument;

  if ( buffer->in_length == 0 )
    return retError;
4420 4421 4422 4423 4424

  lookup_count = gsub->LookupList.LookupCount;

  for ( i = 0; i < gsub->FeatureList.ApplyCount; i++)
  {
4425 4426
    FT_UShort  feature_index = gsub->FeatureList.ApplyOrder[i];
    HB_Feature feature = gsub->FeatureList.FeatureRecord[feature_index].Feature;
4427 4428 4429

    for ( j = 0; j < feature.LookupListCount; j++ )
    {
4430
      FT_UShort         lookup_index = feature.LookupListIndex[j];
4431 4432 4433 4434 4435

      /* Skip nonexistant lookups */
      if (lookup_index >= lookup_count)
       continue;

4436
      error = GSUB_Do_String_Lookup( gsub, lookup_index, buffer );
4437 4438 4439
      if ( error )
      {
	if ( error != HB_Err_Not_Covered )
4440
	  return error;
4441 4442 4443
      }
      else
	retError = error;
4444
    }
4445
  }
4446

4447 4448 4449 4450 4451 4452 4453
  error = retError;

  return error;
}


/* END */