Branch
Hash :
b04db387
Author :
Date :
2025-07-04T19:52:53
[sfnt] Rewrite GPOS kerning support. (2/2) The previous code had a fundamental flaw: it didn't validate the necessary parts of the 'GPOS' table before accessing it, causing crashes with malformed data (since `TT_CONFIG_OPTION_GPOS_KERNING` is off by default, standard fuzzers don't catch these problems). Additionally, it did a lot of parsing while accessing kerning data, making it rather slow. The new implementation fixes this. After validation, offsets to the 'GPOS' lookup subtables used in the 'kern' feature that correspond to 'simple' kerning (i.e., similar to 'kern' table kerning) are stored in `TT_Face`; this greatly simplifies and accelerates access to the kerning data. Testing with font `SF-Pro.ttf` version '1.00', the validation time for the 'GPOS' table increases the start-up time of `FT_New_Face` by less than 1%, while calls to `FT_Get_Kerning` become about 3.5 times faster. * include/freetype/internal (gpos_kerning_available): Replace with... (gpos_lookups_kerning, num_gpos_lookups_kerning): ... these new fields. Update callers. * src/ttgpos.c [TT_CONFIG_OPTION_GPOS_KERNING]: A new implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 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 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 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 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 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 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 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 933 934 935 936 937 938 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 976 977 978 979 980 981 982
/****************************************************************************
*
* ttgpos.c
*
* Routines to parse and access the 'GPOS' table for simple kerning (body).
*
* Copyright (C) 2025 by
* David Turner, Robert Wilhelm, and Werner Lemberg.
*
* This file is part of the FreeType project, and may only be used,
* modified, and distributed under the terms of the FreeType project
* license, LICENSE.TXT. By continuing to use, modify, or distribute
* this file you indicate that you have read the license and
* understand and accept it fully.
*
*/
#include <freetype/freetype.h>
#include <freetype/tttables.h>
#include <freetype/tttags.h>
#include <freetype/internal/ftdebug.h>
#include <freetype/internal/ftstream.h>
#include "ttgpos.h"
#ifdef TT_CONFIG_OPTION_GPOS_KERNING
/**************************************************************************
*
* The macro FT_COMPONENT is used in trace mode. It is an implicit
* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
* messages during execution.
*/
#undef FT_COMPONENT
#define FT_COMPONENT ttgpos
/*********************************/
/******** ********/
/******** GPOS validation ********/
/******** ********/
/*********************************/
static FT_Bool
tt_face_validate_coverage( FT_Byte* table,
FT_Byte* table_limit,
FT_UInt max_num_coverage_indices )
{
FT_UInt format;
FT_Byte* p = table;
FT_Byte* limit;
FT_Long last_id = -1;
if ( table_limit < p + 4 )
return FALSE;
format = FT_NEXT_USHORT( p );
if ( format == 1 )
{
FT_UInt glyphCount = FT_NEXT_USHORT( p );
if ( glyphCount > max_num_coverage_indices )
return FALSE;
limit = p + glyphCount * 2;
if ( table_limit < limit )
return FALSE;
while ( p < limit )
{
FT_UInt id = FT_NEXT_USHORT( p );
if ( last_id >= id )
return FALSE;
last_id = id;
}
}
else if ( format == 2 )
{
FT_UInt rangeCount = FT_NEXT_USHORT( p );
limit = p + rangeCount * 6;
if ( table_limit < limit )
return FALSE;
while ( p < limit )
{
FT_UInt startGlyphID = FT_NEXT_USHORT( p );
FT_UInt endGlyphID = FT_NEXT_USHORT( p );
FT_UInt startCoverageIndex = FT_NEXT_USHORT( p );
if ( startGlyphID > endGlyphID )
return FALSE;
if ( last_id >= startGlyphID )
return FALSE;
last_id = endGlyphID;
/* XXX: Is this modulo 65536 arithmetic? */
if ( startCoverageIndex + endGlyphID - startGlyphID >=
max_num_coverage_indices )
return FALSE;
}
}
else
return FALSE;
return TRUE;
}
static FT_Bool
tt_face_validate_class_def( FT_Byte* table,
FT_Byte* table_limit,
FT_UInt num_classes )
{
FT_UInt format;
FT_Byte* p = table;
FT_Byte* limit;
FT_UInt max_class_value = 0;
if ( table_limit < p + 2 )
return FALSE;
format = FT_NEXT_USHORT( p );
if ( format == 1 )
{
FT_UInt glyphCount;
if ( table_limit < p + 4 )
return FALSE;
p += 2; /* Skip `startGlyphID`. */
glyphCount = FT_NEXT_USHORT( p );
limit = p + glyphCount * 2;
if ( table_limit < limit )
return FALSE;
while ( p < limit )
{
FT_UInt class_value = FT_NEXT_USHORT( p );
if ( class_value > max_class_value )
max_class_value = class_value;
}
}
else if ( format == 2 )
{
FT_UInt classRangeCount;
FT_Long last_id = -1;
if ( table_limit < p + 2 )
return FALSE;
classRangeCount = FT_NEXT_USHORT( p );
limit = p + classRangeCount * 6;
if ( table_limit < limit )
return FALSE;
while ( p < limit )
{
FT_UInt startGlyphID = FT_NEXT_USHORT( p );
FT_UInt endGlyphID = FT_NEXT_USHORT( p );
FT_UInt class_value = FT_NEXT_USHORT( p );
if ( startGlyphID > endGlyphID )
return FALSE;
if ( last_id >= startGlyphID )
return FALSE;
last_id = endGlyphID;
if ( class_value > max_class_value )
max_class_value = class_value;
}
}
else
return FALSE;
if ( max_class_value + 1 != num_classes )
return FALSE;
return TRUE;
}
static FT_Bool
tt_face_validate_feature( FT_Byte* table,
FT_Byte* table_limit,
FT_UInt use_lookup_table_size,
FT_Byte* use_lookup_table )
{
FT_UInt lookupIndexCount;
FT_Byte* p = table;
FT_Byte* limit;
if ( table_limit < p + 4 )
return FALSE;
p += 2; /* Skip `featureParamsOffset`. */
lookupIndexCount = FT_NEXT_USHORT( p );
limit = p + lookupIndexCount * 2;
if ( table_limit < limit )
return FALSE;
while ( p < limit )
{
FT_UInt lookup_index = FT_NEXT_USHORT( p );
if ( lookup_index >= use_lookup_table_size )
return FALSE;
use_lookup_table[lookup_index] = TRUE;
}
return TRUE;
}
static FT_Bool
tt_face_validate_feature_table( FT_Byte* table,
FT_Byte* table_limit,
FT_UInt use_lookup_table_size,
FT_Byte* use_lookup_table )
{
FT_UInt featureCount;
FT_Byte* p = table;
FT_Byte* limit;
if ( table_limit < p + 2 )
return FALSE;
featureCount = FT_NEXT_USHORT( p );
limit = p + featureCount * 6;
if ( table_limit < limit )
return FALSE;
/* We completely ignore GPOS script information */
/* and collect lookup tables of all 'kern' features. */
while ( p < limit )
{
FT_ULong featureTag = FT_NEXT_ULONG( p );
FT_UInt featureOffset = FT_NEXT_USHORT( p );
if ( featureTag == TTAG_kern )
{
if ( !tt_face_validate_feature( table + featureOffset,
table_limit,
use_lookup_table_size,
use_lookup_table ) )
return FALSE;
}
}
return TRUE;
}
static FT_Bool
tt_face_validate_pair_set( FT_Byte* table,
FT_Byte* table_limit )
{
FT_UInt pairValueCount;
FT_Byte* p = table;
FT_Byte* limit;
FT_Long last_id = -1;
if ( table_limit < p + 2 )
return FALSE;
/* For our purposes, the first value record only contains X advances */
/* while the second one is empty; a `PairValue` record has thus a */
/* size of four bytes. */
pairValueCount = FT_NEXT_USHORT( p );
limit = p + pairValueCount * 4;
if ( table_limit < limit )
return FALSE;
/* We validate the order of `secondGlyph` so that binary search works. */
while ( p < limit )
{
FT_UInt id = FT_NEXT_USHORT( p );
if ( last_id >= id )
return FALSE;
last_id = id;
p += 2; /* Skip `valueRecord1`. */
}
return TRUE;
}
static FT_Bool
tt_face_validate_pair_pos1( FT_Byte* table,
FT_Byte* table_limit,
FT_Bool* is_fitting )
{
FT_Byte* coverage;
FT_UInt valueFormat1;
FT_UInt valueFormat2;
/* Subtable format is already checked. */
FT_Byte* p = table + 2;
FT_Byte* limit;
/* The six bytes for the coverage table offset */
/* and the value formats are already checked. */
coverage = table + FT_NEXT_USHORT( p );
/* For the limited purpose of accessing the simplest type of kerning */
/* (similar to what FreeType's 'kern' table handling provides) we */
/* only consider tables that contains X advance values for the first */
/* glyph and no data for the second glyph. */
valueFormat1 = FT_NEXT_USHORT( p );
valueFormat2 = FT_NEXT_USHORT( p );
if ( valueFormat1 == 0x4 && valueFormat2 == 0 )
{
FT_UInt pairSetCount;
if ( table_limit < p + 2 )
return FALSE;
pairSetCount = FT_NEXT_USHORT( p );
limit = p + pairSetCount * 2;
if ( table_limit < limit )
return FALSE;
if ( !tt_face_validate_coverage( coverage,
table_limit,
pairSetCount ) )
return FALSE;
while ( p < limit )
{
FT_Byte* pair_set = table + FT_NEXT_USHORT( p );
if ( !tt_face_validate_pair_set( pair_set, table_limit ) )
return FALSE;
}
*is_fitting = TRUE;
}
return TRUE;
}
static FT_Bool
tt_face_validate_pair_pos2( FT_Byte* table,
FT_Byte* table_limit,
FT_Bool* is_fitting )
{
FT_Byte* coverage;
FT_UInt valueFormat1;
FT_UInt valueFormat2;
/* Subtable format is already checked. */
FT_Byte* p = table + 2;
FT_Byte* limit;
/* The six bytes for the coverage table offset */
/* and the value formats are already checked. */
coverage = table + FT_NEXT_USHORT( p );
valueFormat1 = FT_NEXT_USHORT( p );
valueFormat2 = FT_NEXT_USHORT( p );
if ( valueFormat1 == 0x4 && valueFormat2 == 0 )
{
FT_Byte* class_def1;
FT_Byte* class_def2;
FT_UInt class1Count;
FT_UInt class2Count;
/* The number of coverage indices is not relevant here. */
if ( !tt_face_validate_coverage( coverage, table_limit, FT_UINT_MAX ) )
return FALSE;
if ( table_limit < p + 8 )
return FALSE;
class_def1 = table + FT_NEXT_USHORT( p );
class_def2 = table + FT_NEXT_USHORT( p );
class1Count = FT_NEXT_USHORT( p );
class2Count = FT_NEXT_USHORT( p );
if ( !tt_face_validate_class_def( class_def1,
table_limit,
class1Count ) )
return FALSE;
if ( !tt_face_validate_class_def( class_def2,
table_limit,
class2Count ) )
return FALSE;
/* For our purposes, the first value record only contains */
/* X advances while the second one is empty. */
limit = p + class1Count * class2Count * 2;
if ( table_limit < limit )
return FALSE;
*is_fitting = TRUE;
}
return TRUE;
}
/* The return value is the number of fitting subtables. */
static FT_UInt
tt_face_validate_lookup_table( FT_Byte* table,
FT_Byte* table_limit )
{
FT_UInt lookupType;
FT_UInt real_lookupType = 0;
FT_UInt subtableCount;
FT_Byte* p = table;
FT_Byte* limit;
FT_UInt num_fitting_subtables = 0;
if ( table_limit < p + 6 )
return 0;
lookupType = FT_NEXT_USHORT( p );
p += 2; /* Skip `lookupFlag`. */
subtableCount = FT_NEXT_USHORT( p );
limit = p + subtableCount * 2;
if ( table_limit < limit )
return 0;
while ( p < limit )
{
FT_Byte* subtable = table + FT_NEXT_USHORT( p );
FT_UInt format;
FT_Bool is_fitting = FALSE;
if ( lookupType == 9 )
{
/* Positioning extension. */
FT_Byte* q = subtable;
if ( table_limit < q + 8 )
return 0;
if ( FT_NEXT_USHORT( q ) != 1 ) /* format */
return 0;
if ( real_lookupType == 0 )
real_lookupType = FT_NEXT_USHORT( q );
else if ( real_lookupType != FT_NEXT_USHORT( q ) )
return 0;
subtable += FT_PEEK_ULONG( q );
}
else
real_lookupType = lookupType;
/* Ensure the first eight bytes of the subtable formats. */
if ( table_limit < subtable + 8 )
return 0;
format = FT_PEEK_USHORT( subtable );
if ( real_lookupType == 2 )
{
if ( format == 1 )
{
if ( !tt_face_validate_pair_pos1( subtable,
table_limit,
&is_fitting ) )
return 0;
}
else if ( format == 2 )
{
if ( !tt_face_validate_pair_pos2( subtable,
table_limit,
&is_fitting ) )
return 0;
}
else
return 0;
}
else
return 0;
if ( is_fitting )
num_fitting_subtables++;
}
return num_fitting_subtables;
}
static void
tt_face_get_subtable_offsets( FT_Byte* table,
FT_Byte* gpos,
FT_UInt32* gpos_lookups_kerning,
FT_UInt* idx )
{
FT_UInt lookupType;
FT_UInt subtableCount;
FT_Byte* p = table;
FT_Byte* limit;
lookupType = FT_NEXT_USHORT( p );
p += 2;
subtableCount = FT_NEXT_USHORT( p );
limit = p + subtableCount * 2;
while ( p < limit )
{
FT_Byte* subtable = table + FT_NEXT_USHORT( p );
FT_UInt valueFormat1;
FT_UInt valueFormat2;
if ( lookupType == 9 )
subtable += FT_PEEK_ULONG( subtable + 4 );
/* Table offsets for `valueFormat[12]` values */
/* are identical for both subtable formats. */
valueFormat1 = FT_PEEK_USHORT( subtable + 4 );
valueFormat2 = FT_PEEK_USHORT( subtable + 6 );
if ( valueFormat1 == 0x4 && valueFormat2 == 0 )
{
/* We store offsets relative to the start of the GPOS table. */
gpos_lookups_kerning[(*idx)++] = (FT_UInt32)( subtable - gpos );
}
}
}
FT_LOCAL_DEF( FT_Error )
tt_face_load_gpos( TT_Face face,
FT_Stream stream )
{
FT_Error error;
FT_Memory memory = face->root.memory;
FT_ULong gpos_length;
FT_Byte* gpos;
FT_Byte* gpos_limit;
FT_UInt32* gpos_lookups_kerning;
FT_UInt featureListOffset;
FT_UInt lookupListOffset;
FT_Byte* lookup_list;
FT_UInt lookupCount;
FT_UInt i;
FT_Byte* use_lookup_table = NULL;
FT_UInt num_fitting_subtables;
FT_Byte* p;
FT_Byte* limit;
face->gpos_table = NULL;
face->gpos_lookups_kerning = NULL;
face->num_gpos_lookups_kerning = 0;
gpos = NULL;
gpos_lookups_kerning = NULL;
error = face->goto_table( face, TTAG_GPOS, stream, &gpos_length );
if ( error )
goto Fail;
if ( FT_FRAME_EXTRACT( gpos_length, gpos ) )
goto Fail;
if ( gpos_length < 10 )
goto Fail;
gpos_limit = gpos + gpos_length;
/* We first need the number of GPOS lookups. */
lookupListOffset = FT_PEEK_USHORT( gpos + 8 );
lookup_list = gpos + lookupListOffset;
p = lookup_list;
if ( gpos_limit < p + 2 )
goto Fail;
lookupCount = FT_NEXT_USHORT( p );
limit = p + lookupCount * 2;
if ( gpos_limit < limit )
goto Fail;
/* Allocate an auxiliary array for Boolean values that */
/* gets filled while walking over all 'kern' features. */
if ( FT_NEW_ARRAY( use_lookup_table, lookupCount ) )
goto Fail;
featureListOffset = FT_PEEK_USHORT( gpos + 6 );
if ( !tt_face_validate_feature_table( gpos + featureListOffset,
gpos_limit,
lookupCount,
use_lookup_table ) )
goto Fail;
/* Now walk over all lookup tables and get the */
/* number of fitting subtables. */
num_fitting_subtables = 0;
for ( i = 0; i < lookupCount; i++ )
{
FT_UInt lookupOffset;
if ( !use_lookup_table[i] )
continue;
lookupOffset = FT_PEEK_USHORT( p + i * 2 );
num_fitting_subtables +=
tt_face_validate_lookup_table( lookup_list + lookupOffset,
gpos_limit );
}
/* Loop again over all lookup tables and */
/* collect offsets to those subtables. */
if ( num_fitting_subtables )
{
FT_UInt idx;
if ( FT_QNEW_ARRAY( gpos_lookups_kerning, num_fitting_subtables ) )
goto Fail;
idx = 0;
for ( i = 0; i < lookupCount; i++ )
{
FT_UInt lookupOffset;
if ( !use_lookup_table[i] )
continue;
lookupOffset = FT_PEEK_USHORT( p + i * 2 );
tt_face_get_subtable_offsets( lookup_list + lookupOffset,
gpos,
gpos_lookups_kerning,
&idx );
}
}
FT_FREE( use_lookup_table );
use_lookup_table = NULL;
face->gpos_table = gpos;
face->gpos_lookups_kerning = gpos_lookups_kerning;
face->num_gpos_lookups_kerning = num_fitting_subtables;
Exit:
return error;
Fail:
FT_FREE( gpos );
FT_FREE( gpos_lookups_kerning );
FT_FREE( use_lookup_table );
/* If we don't have an explicit error code, set it to a generic value. */
if ( !error )
error = FT_THROW( Invalid_Table );
goto Exit;
}
FT_LOCAL_DEF( void )
tt_face_done_gpos( TT_Face face )
{
FT_Stream stream = face->root.stream;
FT_Memory memory = face->root.memory;
FT_FRAME_RELEASE( face->gpos_table );
FT_FREE( face->gpos_lookups_kerning );
}
/*********************************/
/******** ********/
/******** GPOS access ********/
/******** ********/
/*********************************/
static FT_Long
tt_face_get_coverage_index( FT_Byte* table,
FT_UInt glyph_index )
{
FT_Byte* p = table;
FT_UInt format = FT_NEXT_USHORT( p );
FT_UInt count = FT_NEXT_USHORT( p );
FT_UInt min, max;
min = 0;
max = count;
if ( format == 1 )
{
while ( min < max )
{
FT_UInt mid = min + ( max - min ) / 2;
FT_UInt mid_index = FT_PEEK_USHORT( p + mid * 2 );
if ( glyph_index > mid_index )
min = mid + 1;
else if ( glyph_index < mid_index )
max = mid;
else
return mid;
}
}
else
{
while ( min < max )
{
FT_UInt mid = min + ( max - min ) / 2;
FT_UInt startGlyphID = FT_PEEK_USHORT( p + mid * 6 );
FT_UInt endGlyphID = FT_PEEK_USHORT( p + mid * 6 + 2 );
if ( glyph_index > endGlyphID )
min = mid + 1;
else if ( glyph_index < startGlyphID )
max = mid;
else
{
FT_UInt startCoverageIndex = FT_PEEK_USHORT( p + mid * 6 + 4 );
return startCoverageIndex + glyph_index - startGlyphID;
}
}
}
return -1;
}
static FT_UInt
tt_face_get_class( FT_Byte* table,
FT_UInt glyph_index )
{
FT_Byte* p = table;
FT_UInt format = FT_NEXT_USHORT( p );
if ( format == 1 )
{
FT_UInt startGlyphID = FT_NEXT_USHORT( p );
FT_UInt glyphCount = FT_NEXT_USHORT( p );
/* XXX: Is this modulo 65536 arithmetic? */
if ( startGlyphID <= glyph_index &&
startGlyphID + glyphCount >= glyph_index )
return FT_PEEK_USHORT( p + ( glyph_index - startGlyphID ) * 2 );
}
else
{
FT_UInt count = FT_NEXT_USHORT( p );
FT_UInt min, max;
min = 0;
max = count;
while ( min < max )
{
FT_UInt mid = min + ( max - min ) / 2;
FT_UInt startGlyphID = FT_PEEK_USHORT( p + mid * 6 );
FT_UInt endGlyphID = FT_PEEK_USHORT( p + mid * 6 + 2 );
if ( glyph_index > endGlyphID )
min = mid + 1;
else if ( glyph_index < startGlyphID )
max = mid;
else
return FT_PEEK_USHORT( p + mid * 6 + 4 );
}
}
return 0;
}
static FT_Bool
tt_face_get_pair_pos1_kerning( FT_Byte* table,
FT_UInt first_glyph,
FT_UInt second_glyph,
FT_Int* kerning )
{
FT_Byte* coverage = table + FT_PEEK_USHORT( table + 2 );
FT_Long coverage_index = tt_face_get_coverage_index( coverage,
first_glyph );
FT_UInt pair_set_offset;
FT_Byte* p;
FT_UInt count;
FT_UInt min, max;
if ( coverage_index < 0 )
return FALSE;
pair_set_offset = FT_PEEK_USHORT( table + 10 + coverage_index * 2 );
p = table + pair_set_offset;
count = FT_NEXT_USHORT( p );
min = 0;
max = count;
while ( min < max )
{
FT_UInt mid = min + ( max - min ) / 2;
FT_UInt mid_index = FT_PEEK_USHORT( p + mid * 4 );
if ( second_glyph > mid_index )
min = max + 1;
else if ( second_glyph < mid_index )
max = mid;
else
{
*kerning = FT_PEEK_SHORT( p + mid * 4 + 2 );
return TRUE;
}
}
return FALSE;
}
static FT_Bool
tt_face_get_pair_pos2_kerning( FT_Byte* table,
FT_UInt first_glyph,
FT_UInt second_glyph,
FT_Int* kerning )
{
FT_Byte* coverage = table + FT_PEEK_USHORT( table + 2 );
FT_Long coverage_index = tt_face_get_coverage_index( coverage,
first_glyph );
FT_Byte* class_def1;
FT_Byte* class_def2;
FT_UInt first_class;
FT_UInt second_class;
FT_UInt class2Count;
if ( coverage_index < 0 )
return FALSE;
class_def1 = table + FT_PEEK_USHORT( table + 8 );
class_def2 = table + FT_PEEK_USHORT( table + 10 );
class2Count = FT_PEEK_USHORT( table + 14 );
first_class = tt_face_get_class( class_def1, first_glyph );
second_class = tt_face_get_class( class_def2, second_glyph );
*kerning =
FT_PEEK_SHORT( table + 16 +
( first_class * class2Count + second_class ) * 2 );
return TRUE;
}
FT_LOCAL_DEF( FT_Int )
tt_face_get_gpos_kerning( TT_Face face,
FT_UInt first_glyph,
FT_UInt second_glyph )
{
FT_Int kerning = 0;
FT_UInt i;
/* We only have `PairPos` subtables. */
for ( i = 0; i < face->num_gpos_lookups_kerning; i++ )
{
FT_Byte* subtable = face->gpos_table + face->gpos_lookups_kerning[i];
FT_Byte* p = subtable;
FT_UInt format = FT_NEXT_USHORT( p );
if ( format == 1 )
{
if ( tt_face_get_pair_pos1_kerning( subtable,
first_glyph,
second_glyph,
&kerning ) )
break;
}
else
{
if ( tt_face_get_pair_pos2_kerning( subtable,
first_glyph,
second_glyph,
&kerning ) )
break;
}
}
return kerning;
}
#else /* !TT_CONFIG_OPTION_GPOS_KERNING */
/* ANSI C doesn't like empty source files */
typedef int tt_gpos_dummy_;
#endif /* !TT_CONFIG_OPTION_GPOS_KERNING */
/* END */