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


/***************************
 * Script related functions
 ***************************/


/* LangSys */

24
static HB_Error  Load_LangSys( HB_LangSys*  ls,
25 26
			       FT_Stream     stream )
{
27
  HB_Error   error;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  FT_UShort  n, count;
  FT_UShort* fi;


  if ( ACCESS_Frame( 6L ) )
    return error;

  ls->LookupOrderOffset    = GET_UShort();    /* should be 0 */
  ls->ReqFeatureIndex      = GET_UShort();
  count = ls->FeatureCount = GET_UShort();

  FORGET_Frame();

  ls->FeatureIndex = NULL;

  if ( ALLOC_ARRAY( ls->FeatureIndex, count, FT_UShort ) )
    return error;

  if ( ACCESS_Frame( count * 2L ) )
  {
    FREE( ls->FeatureIndex );
    return error;
  }

  fi = ls->FeatureIndex;

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

  FORGET_Frame();

59
  return HB_Err_Ok;
60 61 62
}


63
static void  Free_LangSys( HB_LangSys*  ls )
64 65 66 67 68 69 70
{
  FREE( ls->FeatureIndex );
}


/* Script */

71
static HB_Error  Load_Script( HB_Script*  s,
72 73
			      FT_Stream    stream )
{
74
  HB_Error   error;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  FT_UShort  n, m, count;
  FT_ULong   cur_offset, new_offset, base_offset;

  HB_LangSysRecord*  lsr;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 2L ) )
    return error;

  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_LangSys( &s->DefaultLangSys,
95
				 stream ) ) != HB_Err_Ok )
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
      return error;
    (void)FILE_Seek( cur_offset );
  }
  else
  {
    /* we create a DefaultLangSys table with no entries */

    s->DefaultLangSys.LookupOrderOffset = 0;
    s->DefaultLangSys.ReqFeatureIndex   = 0xFFFF;
    s->DefaultLangSys.FeatureCount      = 0;
    s->DefaultLangSys.FeatureIndex      = NULL;
  }

  if ( ACCESS_Frame( 2L ) )
    goto Fail2;

  count = s->LangSysCount = GET_UShort();

  /* safety check; otherwise the official handling of TrueType Open
     fonts won't work */

  if ( s->LangSysCount == 0 && s->DefaultLangSys.FeatureCount == 0 )
  {
    error = HB_Err_Empty_Script;
    goto Fail2;
  }

  FORGET_Frame();

  s->LangSysRecord = NULL;

  if ( ALLOC_ARRAY( s->LangSysRecord, count, HB_LangSysRecord ) )
    goto Fail2;

  lsr = s->LangSysRecord;

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

    lsr[n].LangSysTag = GET_ULong();
    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
144
	 ( error = Load_LangSys( &lsr[n].LangSys, stream ) ) != HB_Err_Ok )
145 146 147 148
      goto Fail1;
    (void)FILE_Seek( cur_offset );
  }

149
  return HB_Err_Ok;
150 151 152

Fail1:
  for ( m = 0; m < n; m++ )
153
    Free_LangSys( &lsr[m].LangSys );
154 155 156 157

  FREE( s->LangSysRecord );

Fail2:
158
  Free_LangSys( &s->DefaultLangSys );
159 160 161 162
  return error;
}


163
static void  Free_Script( HB_Script*  s )
164 165 166 167 168 169
{
  FT_UShort           n, count;

  HB_LangSysRecord*  lsr;


170
  Free_LangSys( &s->DefaultLangSys );
171 172 173 174 175 176 177

  if ( s->LangSysRecord )
  {
    count = s->LangSysCount;
    lsr   = s->LangSysRecord;

    for ( n = 0; n < count; n++ )
178
      Free_LangSys( &lsr[n].LangSys );
179 180 181 182 183 184 185 186

    FREE( lsr );
  }
}


/* ScriptList */

187
HB_Error  _HB_OPEN_Load_ScriptList( HB_ScriptList*  sl,
188 189
			   FT_Stream        stream )
{
190
  HB_Error   error;
191 192 193 194 195 196 197 198 199 200 201 202 203 204 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

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

  HB_ScriptRecord*  sr;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 2L ) )
    return error;

  script_count = GET_UShort();

  FORGET_Frame();

  sl->ScriptRecord = NULL;

  if ( ALLOC_ARRAY( sl->ScriptRecord, script_count, HB_ScriptRecord ) )
    return error;

  sr = sl->ScriptRecord;

  sl->ScriptCount= 0;
  for ( n = 0; n < script_count; n++ )
  {
    if ( ACCESS_Frame( 6L ) )
      goto Fail;

    sr[sl->ScriptCount].ScriptTag = GET_ULong();
    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();

    if ( FILE_Seek( new_offset ) )
      goto Fail;

    error = Load_Script( &sr[sl->ScriptCount].Script, stream );
231
    if ( error == HB_Err_Ok )
232 233 234 235 236 237 238
      sl->ScriptCount += 1;
    else if ( error != HB_Err_Empty_Script )
      goto Fail;

    (void)FILE_Seek( cur_offset );
  }

239 240 241 242
  /* Empty tables are harmless and generated by fontforge.
   * See http://bugzilla.gnome.org/show_bug.cgi?id=347073
   */
#if 0
243 244
  if ( sl->ScriptCount == 0 )
  {
245
    error = _hb_err(HB_Err_Invalid_SubTable);
246 247
    goto Fail;
  }
248
#endif
249
  
250
  return HB_Err_Ok;
251 252 253

Fail:
  for ( n = 0; n < sl->ScriptCount; n++ )
254
    Free_Script( &sr[n].Script );
255 256 257 258 259 260

  FREE( sl->ScriptRecord );
  return error;
}


261
void  _HB_OPEN_Free_ScriptList( HB_ScriptList*  sl )
262 263 264 265 266 267 268 269 270 271 272 273
{
  FT_UShort          n, count;

  HB_ScriptRecord*  sr;


  if ( sl->ScriptRecord )
  {
    count = sl->ScriptCount;
    sr    = sl->ScriptRecord;

    for ( n = 0; n < count; n++ )
274
      Free_Script( &sr[n].Script );
275 276 277 278 279 280 281 282 283 284 285 286 287 288

    FREE( sr );
  }
}



/*********************************
 * Feature List related functions
 *********************************/


/* Feature */

289
static HB_Error  Load_Feature( HB_Feature*  f,
290 291
			       FT_Stream     stream )
{
292
  HB_Error   error;
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

  FT_UShort   n, count;

  FT_UShort*  lli;


  if ( ACCESS_Frame( 4L ) )
    return error;

  f->FeatureParams           = GET_UShort();    /* should be 0 */
  count = f->LookupListCount = GET_UShort();

  FORGET_Frame();

  f->LookupListIndex = NULL;

  if ( ALLOC_ARRAY( f->LookupListIndex, count, FT_UShort ) )
    return error;

  lli = f->LookupListIndex;

  if ( ACCESS_Frame( count * 2L ) )
  {
    FREE( f->LookupListIndex );
    return error;
  }

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

  FORGET_Frame();

325
  return HB_Err_Ok;
326 327 328
}


329
static void  Free_Feature( HB_Feature*  f )
330 331 332 333 334 335 336
{
  FREE( f->LookupListIndex );
}


/* FeatureList */

337
HB_Error  _HB_OPEN_Load_FeatureList( HB_FeatureList*  fl,
338 339
			    FT_Stream         stream )
{
340
  HB_Error   error;
341 342 343 344 345 346 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

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

  HB_FeatureRecord*  fr;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 2L ) )
    return error;

  count = fl->FeatureCount = GET_UShort();

  FORGET_Frame();

  fl->FeatureRecord = NULL;

  if ( ALLOC_ARRAY( fl->FeatureRecord, count, HB_FeatureRecord ) )
    return error;
  if ( ALLOC_ARRAY( fl->ApplyOrder, count, FT_UShort ) )
    goto Fail2;
  
  fl->ApplyCount = 0;

  fr = fl->FeatureRecord;

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

    fr[n].FeatureTag = GET_ULong();
    new_offset = GET_UShort() + base_offset;

    FORGET_Frame();

    cur_offset = FILE_Pos();
    if ( FILE_Seek( new_offset ) ||
380
	 ( error = Load_Feature( &fr[n].Feature, stream ) ) != HB_Err_Ok )
381 382 383 384
      goto Fail1;
    (void)FILE_Seek( cur_offset );
  }

385
  return HB_Err_Ok;
386 387 388

Fail1:
  for ( m = 0; m < n; m++ )
389
    Free_Feature( &fr[m].Feature );
390 391 392 393 394 395 396 397 398 399

  FREE( fl->ApplyOrder );

Fail2:
  FREE( fl->FeatureRecord );

  return error;
}


400
void  _HB_OPEN_Free_FeatureList( HB_FeatureList*  fl )
401 402 403 404 405 406 407 408 409 410 411 412
{
  FT_UShort           n, count;

  HB_FeatureRecord*  fr;


  if ( fl->FeatureRecord )
  {
    count = fl->FeatureCount;
    fr    = fl->FeatureRecord;

    for ( n = 0; n < count; n++ )
413
      Free_Feature( &fr[n].Feature );
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

    FREE( fr );
  }
  
  FREE( fl->ApplyOrder );
}



/********************************
 * Lookup List related functions
 ********************************/

/* the subroutines of the following two functions are defined in
   ftxgsub.c and ftxgpos.c respectively                          */


/* SubTable */

433
static HB_Error  Load_SubTable( HB_SubTable*  st,
434 435 436 437 438 439 440 441 442 443 444 445 446
				FT_Stream     stream,
				HB_Type       table_type,
				FT_UShort     lookup_type )
{
  if ( table_type == HB_Type_GSUB )
    return _HB_GSUB_Load_SubTable ( &st->st.gsub, stream, lookup_type );
  else
    return _HB_GPOS_Load_SubTable ( &st->st.gpos, stream, lookup_type );
}


static void  Free_SubTable( HB_SubTable*  st,
			    HB_Type       table_type,
447
			    FT_UShort     lookup_type )
448 449
{
  if ( table_type == HB_Type_GSUB )
450
    _HB_GSUB_Free_SubTable ( &st->st.gsub, lookup_type );
451
  else
452
    _HB_GPOS_Free_SubTable ( &st->st.gpos, lookup_type );
453 454 455 456 457
}


/* Lookup */

458
static HB_Error  Load_Lookup( HB_Lookup*   l,
459 460 461
			      FT_Stream     stream,
			      HB_Type      type )
{
462
  HB_Error   error;
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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

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

  HB_SubTable*  st;

  FT_Bool        is_extension = FALSE;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 6L ) )
    return error;

  l->LookupType            = GET_UShort();
  l->LookupFlag            = GET_UShort();
  count = l->SubTableCount = GET_UShort();

  FORGET_Frame();

  l->SubTable = NULL;

  if ( ALLOC_ARRAY( l->SubTable, count, HB_SubTable ) )
    return error;

  st = l->SubTable;

  if ( ( type == HB_Type_GSUB && l->LookupType == HB_GSUB_LOOKUP_EXTENSION ) ||
       ( type == HB_Type_GPOS && l->LookupType == HB_GPOS_LOOKUP_EXTENSION ) )
    is_extension = TRUE;

  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 ( is_extension )
    {
      if ( FILE_Seek( new_offset ) || ACCESS_Frame( 8L ) )
	goto Fail;

      if (GET_UShort() != 1) /* format should be 1 */
	goto Fail;

      l->LookupType = GET_UShort();
      new_offset += GET_ULong();

      FORGET_Frame();
    }

    if ( FILE_Seek( new_offset ) ||
	 ( error = Load_SubTable( &st[n], stream,
521
				  type, l->LookupType ) ) != HB_Err_Ok )
522 523 524 525
      goto Fail;
    (void)FILE_Seek( cur_offset );
  }

526
  return HB_Err_Ok;
527 528 529

Fail:
  for ( m = 0; m < n; m++ )
530
    Free_SubTable( &st[m], type, l->LookupType );
531 532 533 534 535 536 537

  FREE( l->SubTable );
  return error;
}


static void  Free_Lookup( HB_Lookup*   l,
538
			  HB_Type      type )
539 540 541 542 543 544 545 546 547 548 549 550
{
  FT_UShort      n, count;

  HB_SubTable*  st;


  if ( l->SubTable )
  {
    count = l->SubTableCount;
    st    = l->SubTable;

    for ( n = 0; n < count; n++ )
551
      Free_SubTable( &st[n], type, l->LookupType );
552 553 554 555 556 557 558 559

    FREE( st );
  }
}


/* LookupList */

560
HB_Error  _HB_OPEN_Load_LookupList( HB_LookupList*  ll,
561 562 563
			   FT_Stream        stream,
			   HB_Type         type )
{
564
  HB_Error   error;
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

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

  HB_Lookup*  l;


  base_offset = FILE_Pos();

  if ( ACCESS_Frame( 2L ) )
    return error;

  count = ll->LookupCount = GET_UShort();

  FORGET_Frame();

  ll->Lookup = NULL;

  if ( ALLOC_ARRAY( ll->Lookup, count, HB_Lookup ) )
    return error;
  if ( ALLOC_ARRAY( ll->Properties, count, FT_UInt ) )
    goto Fail2;

  l = ll->Lookup;

  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 ) ||
601
	 ( error = Load_Lookup( &l[n], stream, type ) ) != HB_Err_Ok )
602 603 604 605
      goto Fail1;
    (void)FILE_Seek( cur_offset );
  }

606
  return HB_Err_Ok;
607 608 609 610 611

Fail1:
  FREE( ll->Properties );

  for ( m = 0; m < n; m++ )
612
    Free_Lookup( &l[m], type );
613 614 615 616 617 618 619 620

Fail2:
  FREE( ll->Lookup );
  return error;
}


void  _HB_OPEN_Free_LookupList( HB_LookupList*  ll,
621
				HB_Type         type )
622 623 624 625 626 627 628 629 630 631 632 633 634 635
{
  FT_UShort    n, count;

  HB_Lookup*  l;


  FREE( ll->Properties );

  if ( ll->Lookup )
  {
    count = ll->LookupCount;
    l     = ll->Lookup;

    for ( n = 0; n < count; n++ )
636
      Free_Lookup( &l[n], type );
637 638 639 640 641 642 643 644 645 646 647 648 649 650

    FREE( l );
  }
}



/*****************************
 * Coverage related functions
 *****************************/


/* CoverageFormat1 */

651
static HB_Error  Load_Coverage1( HB_CoverageFormat1*  cf1,
652 653
				 FT_Stream             stream )
{
654
  HB_Error   error;
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685

  FT_UShort  n, count;

  FT_UShort* ga;


  if ( ACCESS_Frame( 2L ) )
    return error;

  count = cf1->GlyphCount = GET_UShort();

  FORGET_Frame();

  cf1->GlyphArray = NULL;

  if ( ALLOC_ARRAY( cf1->GlyphArray, count, FT_UShort ) )
    return error;

  ga = cf1->GlyphArray;

  if ( ACCESS_Frame( count * 2L ) )
  {
    FREE( cf1->GlyphArray );
    return error;
  }

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

  FORGET_Frame();

686
  return HB_Err_Ok;
687 688 689
}


690
static void  Free_Coverage1( HB_CoverageFormat1*  cf1 )
691 692 693 694 695 696 697
{
  FREE( cf1->GlyphArray );
}


/* CoverageFormat2 */

698
static HB_Error  Load_Coverage2( HB_CoverageFormat2*  cf2,
699 700
				 FT_Stream             stream )
{
701
  HB_Error   error;
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735

  FT_UShort         n, count;

  HB_RangeRecord*  rr;


  if ( ACCESS_Frame( 2L ) )
    return error;

  count = cf2->RangeCount = GET_UShort();

  FORGET_Frame();

  cf2->RangeRecord = NULL;

  if ( ALLOC_ARRAY( cf2->RangeRecord, count, HB_RangeRecord ) )
    return error;

  rr = cf2->RangeRecord;

  if ( ACCESS_Frame( count * 6L ) )
    goto Fail;

  for ( n = 0; n < count; n++ )
  {
    rr[n].Start              = GET_UShort();
    rr[n].End                = GET_UShort();
    rr[n].StartCoverageIndex = GET_UShort();

    /* sanity check; we are limited to 16bit integers */
    if ( rr[n].Start > rr[n].End ||
	 ( rr[n].End - rr[n].Start + (long)rr[n].StartCoverageIndex ) >=
	   0x10000L )
    {
736
      error = _hb_err(HB_Err_Invalid_SubTable);
737 738 739 740 741 742
      goto Fail;
    }
  }

  FORGET_Frame();

743
  return HB_Err_Ok;
744 745 746 747 748 749 750

Fail:
  FREE( cf2->RangeRecord );
  return error;
}


751
static void  Free_Coverage2( HB_CoverageFormat2*  cf2 )
752 753 754 755 756
{
  FREE( cf2->RangeRecord );
}


757
HB_Error  _HB_OPEN_Load_Coverage( HB_Coverage*  c,
758 759
			 FT_Stream      stream )
{
760
  HB_Error   error;
761 762 763 764 765 766 767 768 769 770

  if ( ACCESS_Frame( 2L ) )
    return error;

  c->CoverageFormat = GET_UShort();

  FORGET_Frame();

  switch ( c->CoverageFormat )
  {
771 772 773
  case 1:  return Load_Coverage1( &c->cf.cf1, stream );
  case 2:  return Load_Coverage2( &c->cf.cf2, stream );
  default: return _hb_err(HB_Err_Invalid_SubTable_Format);
774 775
  }

776
  return HB_Err_Ok;               /* never reached */
777 778 779
}


780
void  _HB_OPEN_Free_Coverage( HB_Coverage*  c )
781 782 783
{
  switch ( c->CoverageFormat )
  {
784 785
  case 1:  Free_Coverage1( &c->cf.cf1 ); break;
  case 2:  Free_Coverage2( &c->cf.cf2 ); break;
786
  default:					 break;
787 788 789 790
  }
}


791
static HB_Error  Coverage_Index1( HB_CoverageFormat1*  cf1,
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
				  FT_UShort             glyphID,
				  FT_UShort*            index )
{
  FT_UShort min, max, new_min, new_max, middle;

  FT_UShort*  array = cf1->GlyphArray;


  /* binary search */

  if ( cf1->GlyphCount == 0 )
    return HB_Err_Not_Covered;

  new_min = 0;
  new_max = cf1->GlyphCount - 1;

  do
  {
    min = new_min;
    max = new_max;

    /* we use (min + max) / 2 = max - (max - min) / 2  to avoid
       overflow and rounding errors                             */

    middle = max - ( ( max - min ) >> 1 );

    if ( glyphID == array[middle] )
    {
      *index = middle;
821
      return HB_Err_Ok;
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
    }
    else if ( glyphID < array[middle] )
    {
      if ( middle == min )
	break;
      new_max = middle - 1;
    }
    else
    {
      if ( middle == max )
	break;
      new_min = middle + 1;
    }
  } while ( min < max );

  return HB_Err_Not_Covered;
}


841
static HB_Error  Coverage_Index2( HB_CoverageFormat2*  cf2,
842 843 844 845 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
				  FT_UShort             glyphID,
				  FT_UShort*            index )
{
  FT_UShort         min, max, new_min, new_max, middle;

  HB_RangeRecord*  rr = cf2->RangeRecord;


  /* binary search */

  if ( cf2->RangeCount == 0 )
    return HB_Err_Not_Covered;

  new_min = 0;
  new_max = cf2->RangeCount - 1;

  do
  {
    min = new_min;
    max = new_max;

    /* we use (min + max) / 2 = max - (max - min) / 2  to avoid
       overflow and rounding errors                             */

    middle = max - ( ( max - min ) >> 1 );

    if ( glyphID >= rr[middle].Start && glyphID <= rr[middle].End )
    {
      *index = rr[middle].StartCoverageIndex + glyphID - rr[middle].Start;
871
      return HB_Err_Ok;
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
    }
    else if ( glyphID < rr[middle].Start )
    {
      if ( middle == min )
	break;
      new_max = middle - 1;
    }
    else
    {
      if ( middle == max )
	break;
      new_min = middle + 1;
    }
  } while ( min < max );

  return HB_Err_Not_Covered;
}


891
HB_Error  _HB_OPEN_Coverage_Index( HB_Coverage*  c,
892 893 894 895 896
			  FT_UShort      glyphID,
			  FT_UShort*     index )
{
  switch ( c->CoverageFormat )
  {
897 898 899
  case 1:  return Coverage_Index1( &c->cf.cf1, glyphID, index );
  case 2:  return Coverage_Index2( &c->cf.cf2, glyphID, index );
  default: return _hb_err(HB_Err_Invalid_SubTable_Format);
900 901
  }

902
  return HB_Err_Ok;               /* never reached */
903 904 905 906 907 908 909 910 911 912 913
}



/*************************************
 * Class Definition related functions
 *************************************/


/* ClassDefFormat1 */

914
static HB_Error  Load_ClassDef1( HB_ClassDefinition*  cd,
915 916 917
				 FT_UShort             limit,
				 FT_Stream             stream )
{
918
  HB_Error   error;
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940

  FT_UShort             n, count;

  FT_UShort*            cva;
  FT_Bool*              d;

  HB_ClassDefFormat1*  cdf1;


  cdf1 = &cd->cd.cd1;

  if ( ACCESS_Frame( 4L ) )
    return error;

  cdf1->StartGlyph         = GET_UShort();
  count = cdf1->GlyphCount = GET_UShort();

  FORGET_Frame();

  /* sanity check; we are limited to 16bit integers */

  if ( cdf1->StartGlyph + (long)count >= 0x10000L )
941
    return _hb_err(HB_Err_Invalid_SubTable);
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

  cdf1->ClassValueArray = NULL;

  if ( ALLOC_ARRAY( cdf1->ClassValueArray, count, FT_UShort ) )
    return error;

  d   = cd->Defined;
  cva = cdf1->ClassValueArray;

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

  for ( n = 0; n < count; n++ )
  {
    cva[n] = GET_UShort();
    if ( cva[n] >= limit )
    {
959
      error = _hb_err(HB_Err_Invalid_SubTable);
960 961 962 963 964 965 966
      goto Fail;
    }
    d[cva[n]] = TRUE;
  }

  FORGET_Frame();

967
  return HB_Err_Ok;
968 969 970 971 972 973 974 975

Fail:
  FREE( cva );

  return error;
}


976
static void  Free_ClassDef1( HB_ClassDefFormat1*  cdf1 )
977 978 979 980 981 982 983
{
  FREE( cdf1->ClassValueArray );
}


/* ClassDefFormat2 */

984
static HB_Error  Load_ClassDef2( HB_ClassDefinition*  cd,
985 986 987
				 FT_UShort             limit,
				 FT_Stream             stream )
{
988
  HB_Error   error;
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002

  FT_UShort              n, count;

  HB_ClassRangeRecord*  crr;
  FT_Bool*               d;

  HB_ClassDefFormat2*   cdf2;


  cdf2 = &cd->cd.cd2;

  if ( ACCESS_Frame( 2L ) )
    return error;

1003 1004
  count = GET_UShort();
  cdf2->ClassRangeCount = 0; /* zero for now.  we fill with the number of good entries later */
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

  FORGET_Frame();

  cdf2->ClassRangeRecord = NULL;

  if ( ALLOC_ARRAY( cdf2->ClassRangeRecord, count, HB_ClassRangeRecord ) )
    return error;

  d   = cd->Defined;
  crr = cdf2->ClassRangeRecord;

  if ( ACCESS_Frame( count * 6L ) )
    goto Fail;

  for ( n = 0; n < count; n++ )
  {
    crr[n].Start = GET_UShort();
    crr[n].End   = GET_UShort();
    crr[n].Class = GET_UShort();

    /* sanity check */

    if ( crr[n].Start > crr[n].End ||
	 crr[n].Class >= limit )
    {
1030 1031 1032 1033 1034 1035
      /* XXX
       * Corrupt entry.  Skip it.
       * This is hit by Nafees Nastaliq font for example
       */
       n--;
       count--;
1036
    }
1037 1038
    else
      d[crr[n].Class] = TRUE;
1039 1040 1041 1042
  }

  FORGET_Frame();

1043 1044 1045
  cdf2->ClassRangeCount = count;

  return HB_Err_Ok;
1046 1047 1048 1049 1050 1051 1052 1053

Fail:
  FREE( crr );

  return error;
}


1054
static void  Free_ClassDef2( HB_ClassDefFormat2*  cdf2 )
1055 1056 1057 1058 1059 1060 1061
{
  FREE( cdf2->ClassRangeRecord );
}


/* ClassDefinition */

1062
HB_Error  _HB_OPEN_Load_ClassDefinition( HB_ClassDefinition*  cd,
1063 1064 1065
				FT_UShort             limit,
				FT_Stream             stream )
{
1066
  HB_Error   error;
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079

  if ( ALLOC_ARRAY( cd->Defined, limit, FT_Bool ) )
    return error;

  if ( ACCESS_Frame( 2L ) )
    goto Fail;

  cd->ClassFormat = GET_UShort();

  FORGET_Frame();

  switch ( cd->ClassFormat )
  {
1080 1081 1082
  case 1:  error = Load_ClassDef1( cd, limit, stream ); break;
  case 2:  error = Load_ClassDef2( cd, limit, stream ); break;
  default: error = _hb_err(HB_Err_Invalid_SubTable_Format);	break;
1083 1084 1085 1086 1087 1088 1089
  }

  if ( error )
    goto Fail;

  cd->loaded = TRUE;

1090
  return HB_Err_Ok;
1091 1092 1093 1094 1095 1096 1097

Fail:
  FREE( cd->Defined );
  return error;
}


1098
static HB_Error  _HB_OPEN_Load_EmptyClassDefinition( HB_ClassDefinition*  cd )
1099
{
1100
  HB_Error   error;
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

  if ( ALLOC_ARRAY( cd->Defined, 1, FT_Bool ) )
    return error;

  cd->ClassFormat = 1; /* Meaningless */
  cd->Defined[0] = FALSE;

  if ( ALLOC_ARRAY( cd->cd.cd1.ClassValueArray, 1, FT_UShort ) )
    goto Fail;

1111
  return HB_Err_Ok;
1112 1113 1114 1115 1116 1117

Fail:
  FREE( cd->Defined );
  return error;
}

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
HB_Error _HB_OPEN_Load_EmptyOrClassDefinition( HB_ClassDefinition*  cd,
					       FT_UShort            limit,
					       FT_ULong             class_offset,
					       FT_ULong             base_offset,
					       FT_Stream            stream )
{
  HB_Error error;
  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 );

  if (error == HB_Err_Ok)
    (void)FILE_Seek( cur_offset ); /* Changes error as a side-effect */

  return error;
}

void  _HB_OPEN_Free_ClassDefinition( HB_ClassDefinition*  cd )
1144 1145 1146 1147 1148 1149 1150 1151
{
  if ( !cd->loaded )
    return;

  FREE( cd->Defined );

  switch ( cd->ClassFormat )
  {
1152 1153 1154
  case 1:  Free_ClassDef1( &cd->cd.cd1 ); break;
  case 2:  Free_ClassDef2( &cd->cd.cd2 ); break;
  default:				  break;
1155 1156 1157 1158
  }
}


1159
static HB_Error  Get_Class1( HB_ClassDefFormat1*  cdf1,
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
			     FT_UShort             glyphID,
			     FT_UShort*            class,
			     FT_UShort*            index )
{
  FT_UShort*  cva = cdf1->ClassValueArray;


  if ( index )
    *index = 0;

  if ( glyphID >= cdf1->StartGlyph &&
1171
       glyphID < cdf1->StartGlyph + cdf1->GlyphCount )
1172 1173
  {
    *class = cva[glyphID - cdf1->StartGlyph];
1174
    return HB_Err_Ok;
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
  }
  else
  {
    *class = 0;
    return HB_Err_Not_Covered;
  }
}


/* we need the index value of the last searched class range record
   in case of failure for constructed GDEF tables                  */

1187
static HB_Error  Get_Class2( HB_ClassDefFormat2*  cdf2,
1188 1189 1190 1191
			     FT_UShort             glyphID,
			     FT_UShort*            class,
			     FT_UShort*            index )
{
1192
  HB_Error               error = HB_Err_Ok;
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 1221 1222 1223 1224
  FT_UShort              min, max, new_min, new_max, middle;

  HB_ClassRangeRecord*  crr = cdf2->ClassRangeRecord;


  /* binary search */

  if ( cdf2->ClassRangeCount == 0 )
    {
      *class = 0;
      if ( index )
	*index = 0;
      
      return HB_Err_Not_Covered;
    }

  new_min = 0;
  new_max = cdf2->ClassRangeCount - 1;

  do
  {
    min = new_min;
    max = new_max;

    /* we use (min + max) / 2 = max - (max - min) / 2  to avoid
       overflow and rounding errors                             */

    middle = max - ( ( max - min ) >> 1 );

    if ( glyphID >= crr[middle].Start && glyphID <= crr[middle].End )
    {
      *class = crr[middle].Class;
1225
      error  = HB_Err_Ok;
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
      break;
    }
    else if ( glyphID < crr[middle].Start )
    {
      if ( middle == min )
      {
	*class = 0;
	error  = HB_Err_Not_Covered;
	break;
      }
      new_max = middle - 1;
    }
    else
    {
      if ( middle == max )
      {
	*class = 0;
	error  = HB_Err_Not_Covered;
	break;
      }
      new_min = middle + 1;
    }
  } while ( min < max );

  if ( index )
    *index = middle;

  return error;
}


1257
HB_Error  _HB_OPEN_Get_Class( HB_ClassDefinition*  cd,
1258 1259 1260 1261 1262 1263
		     FT_UShort             glyphID,
		     FT_UShort*            class,
		     FT_UShort*            index )
{
  switch ( cd->ClassFormat )
  {
1264 1265 1266
  case 1:  return Get_Class1( &cd->cd.cd1, glyphID, class, index );
  case 2:  return Get_Class2( &cd->cd.cd2, glyphID, class, index );
  default: return _hb_err(HB_Err_Invalid_SubTable_Format);
1267 1268
  }

1269
  return HB_Err_Ok;               /* never reached */
1270 1271 1272 1273 1274 1275 1276 1277 1278
}



/***************************
 * Device related functions
 ***************************/


1279
HB_Error  _HB_OPEN_Load_Device( HB_Device*  d,
1280 1281
		       FT_Stream    stream )
{
1282
  HB_Error   error;
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297

  FT_UShort   n, count;

  FT_UShort*  dv;


  if ( ACCESS_Frame( 6L ) )
    return error;

  d->StartSize   = GET_UShort();
  d->EndSize     = GET_UShort();
  d->DeltaFormat = GET_UShort();

  FORGET_Frame();

1298 1299
  d->DeltaValue = NULL;

1300 1301
  if ( d->StartSize > d->EndSize ||
       d->DeltaFormat == 0 || d->DeltaFormat > 3 )
1302 1303 1304 1305 1306 1307 1308 1309
    {
      /* XXX
       * I've seen fontforge generate DeltaFormat == 0.
       * Just return Ok and let the NULL DeltaValue disable
       * this table.
       */
      return HB_Err_Ok;
    }
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

  count = ( ( d->EndSize - d->StartSize + 1 ) >>
	      ( 4 - d->DeltaFormat ) ) + 1;

  if ( ALLOC_ARRAY( d->DeltaValue, count, FT_UShort ) )
    return error;

  if ( ACCESS_Frame( count * 2L ) )
  {
    FREE( d->DeltaValue );
    return error;
  }

  dv = d->DeltaValue;

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

  FORGET_Frame();

1330
  return HB_Err_Ok;
1331 1332 1333
}


1334
void  _HB_OPEN_Free_Device( HB_Device*  d )
1335 1336 1337 1338 1339 1340 1341 1342 1343 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 1372 1373 1374
{
  FREE( d->DeltaValue );
}


/* Since we have the delta values stored in compressed form, we must
   uncompress it now.  To simplify the interface, the function always
   returns a meaningful value in `value'; the error is just for
   information.
			       |                |
   format = 1: 0011223344556677|8899101112131415|...
			       |                |
		    byte 1           byte 2

     00: (byte >> 14) & mask
     11: (byte >> 12) & mask
     ...

     mask = 0x0003
			       |                |
   format = 2: 0000111122223333|4444555566667777|...
			       |                |
		    byte 1           byte 2

     0000: (byte >> 12) & mask
     1111: (byte >>  8) & mask
     ...

     mask = 0x000F
			       |                |
   format = 3: 0000000011111111|2222222233333333|...
			       |                |
		    byte 1           byte 2

     00000000: (byte >> 8) & mask
     11111111: (byte >> 0) & mask
     ....

     mask = 0x00FF                                    */

1375
HB_Error  _HB_OPEN_Get_Device( HB_Device*  d,
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
		      FT_UShort    size,
		      FT_Short*    value )
{
  FT_UShort  byte, bits, mask, f, s;


  f = d->DeltaFormat;

  if ( d->DeltaValue && size >= d->StartSize && size <= d->EndSize )
  {
    s    = size - d->StartSize;
    byte = d->DeltaValue[s >> ( 4 - f )];
    bits = byte >> ( 16 - ( ( s % ( 1 << ( 4 - f ) ) + 1 ) << f ) );
    mask = 0xFFFF >> ( 16 - ( 1 << f ) );

    *value = (FT_Short)( bits & mask );

    /* conversion to a signed value */

    if ( *value >= ( ( mask + 1 ) >> 1 ) )
      *value -= mask + 1;

1398
    return HB_Err_Ok;
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
  }
  else
  {
    *value = 0;
    return HB_Err_Not_Covered;
  }
}


/* END */