1 /**************************************************************************** 2 * 3 * pfrload.c 4 * 5 * FreeType PFR loader (body). 6 * 7 * Copyright (C) 2002-2023 by 8 * David Turner, Robert Wilhelm, and Werner Lemberg. 9 * 10 * This file is part of the FreeType project, and may only be used, 11 * modified, and distributed under the terms of the FreeType project 12 * license, LICENSE.TXT. By continuing to use, modify, or distribute 13 * this file you indicate that you have read the license and 14 * understand and accept it fully. 15 * 16 */ 17 18 19 #include "pfrload.h" 20 #include <freetype/internal/ftdebug.h> 21 #include <freetype/internal/ftstream.h> 22 23 #include "pfrerror.h" 24 25 #undef FT_COMPONENT 26 #define FT_COMPONENT pfr 27 28 29 /* 30 * The overall structure of a PFR file is as follows. 31 * 32 * PFR header 33 * 58 bytes (contains nPhysFonts) 34 * 35 * Logical font directory (size at most 2^16 bytes) 36 * 2 bytes (nLogFonts) 37 * + nLogFonts * 5 bytes 38 * 39 * ==> nLogFonts <= 13106 40 * 41 * Logical font section (size at most 2^24 bytes) 42 * nLogFonts * logFontRecord 43 * 44 * logFontRecord (size at most 2^16 bytes) 45 * 12 bytes (fontMatrix) 46 * + 1 byte (flags) 47 * + 0-5 bytes (depending on `flags') 48 * + 0-(1+255*(2+255)) = 0-65536 (depending on `flags') 49 * + 5 bytes (physical font info) 50 * + 0-1 bytes (depending on PFR header) 51 * 52 * ==> minimum size 18 bytes 53 * 54 * Physical font section (size at most 2^24 bytes) 55 * nPhysFonts * (physFontRecord 56 * + nBitmapSizes * nBmapChars * bmapCharRecord) 57 * 58 * physFontRecord (size at most 2^24 bytes) 59 * 14 bytes (font info) 60 * + 1 byte (flags) 61 * + 0-2 (depending on `flags') 62 * + 0-? (structure too complicated to be shown here; depending on 63 * `flags'; contains `nBitmapSizes' and `nBmapChars') 64 * + 3 bytes (nAuxBytes) 65 * + nAuxBytes 66 * + 1 byte (nBlueValues) 67 * + 2 * nBlueValues 68 * + 6 bytes (hinting data) 69 * + 2 bytes (nCharacters) 70 * + nCharacters * (4-10 bytes) (depending on `flags') 71 * 72 * ==> minimum size 27 bytes 73 * 74 * bmapCharRecord 75 * 4-7 bytes 76 * 77 * Glyph program strings (three possible types: simpleGps, compoundGps, 78 * and bitmapGps; size at most 2^24 bytes) 79 * simpleGps (size at most 2^16 bytes) 80 * 1 byte (flags) 81 * 1-2 bytes (n[XY]orus, depending on `flags') 82 * 0-(64+512*2) = 0-1088 bytes (depending on `n[XY]orus') 83 * 0-? (structure too complicated to be shown here; depending on 84 * `flags') 85 * 1-? glyph data (faintly resembling PS Type 1 charstrings) 86 * 87 * ==> minimum size 3 bytes 88 * 89 * compoundGps (size at most 2^16 bytes) 90 * 1 byte (nElements <= 63, flags) 91 * + 0-(1+255*(2+255)) = 0-65536 (depending on `flags') 92 * + nElements * (6-14 bytes) 93 * 94 * bitmapGps (size at most 2^16 bytes) 95 * 1 byte (flags) 96 * 3-13 bytes (position info, depending on `flags') 97 * 0-? bitmap data 98 * 99 * ==> minimum size 4 bytes 100 * 101 * PFR trailer 102 * 8 bytes 103 * 104 * 105 * ==> minimum size of a valid PFR: 106 * 58 (header) 107 * + 2 (nLogFonts) 108 * + 27 (1 physFontRecord) 109 * + 8 (trailer) 110 * ----- 111 * 95 bytes 112 * 113 */ 114 115 116 /*************************************************************************/ 117 /*************************************************************************/ 118 /***** *****/ 119 /***** EXTRA ITEMS *****/ 120 /***** *****/ 121 /*************************************************************************/ 122 /*************************************************************************/ 123 124 125 FT_LOCAL_DEF( FT_Error ) pfr_extra_items_skip(FT_Byte ** pp,FT_Byte * limit)126 pfr_extra_items_skip( FT_Byte* *pp, 127 FT_Byte* limit ) 128 { 129 return pfr_extra_items_parse( pp, limit, NULL, NULL ); 130 } 131 132 133 FT_LOCAL_DEF( FT_Error ) pfr_extra_items_parse(FT_Byte ** pp,FT_Byte * limit,PFR_ExtraItem item_list,FT_Pointer item_data)134 pfr_extra_items_parse( FT_Byte* *pp, 135 FT_Byte* limit, 136 PFR_ExtraItem item_list, 137 FT_Pointer item_data ) 138 { 139 FT_Error error = FT_Err_Ok; 140 FT_Byte* p = *pp; 141 FT_UInt num_items, item_type, item_size; 142 143 144 PFR_CHECK( 1 ); 145 num_items = PFR_NEXT_BYTE( p ); 146 147 for ( ; num_items > 0; num_items-- ) 148 { 149 PFR_CHECK( 2 ); 150 item_size = PFR_NEXT_BYTE( p ); 151 item_type = PFR_NEXT_BYTE( p ); 152 153 PFR_CHECK( item_size ); 154 155 if ( item_list ) 156 { 157 PFR_ExtraItem extra = item_list; 158 159 160 for ( extra = item_list; extra->parser != NULL; extra++ ) 161 { 162 if ( extra->type == item_type ) 163 { 164 error = extra->parser( p, p + item_size, item_data ); 165 if ( error ) 166 goto Exit; 167 168 break; 169 } 170 } 171 } 172 173 p += item_size; 174 } 175 176 Exit: 177 *pp = p; 178 return error; 179 180 Too_Short: 181 FT_ERROR(( "pfr_extra_items_parse: invalid extra items table\n" )); 182 error = FT_THROW( Invalid_Table ); 183 goto Exit; 184 } 185 186 187 /*************************************************************************/ 188 /*************************************************************************/ 189 /***** *****/ 190 /***** PFR HEADER *****/ 191 /***** *****/ 192 /*************************************************************************/ 193 /*************************************************************************/ 194 195 static const FT_Frame_Field pfr_header_fields[] = 196 { 197 #undef FT_STRUCTURE 198 #define FT_STRUCTURE PFR_HeaderRec 199 200 FT_FRAME_START( 58 ), 201 FT_FRAME_ULONG ( signature ), 202 FT_FRAME_USHORT( version ), 203 FT_FRAME_USHORT( signature2 ), 204 FT_FRAME_USHORT( header_size ), 205 206 FT_FRAME_USHORT( log_dir_size ), 207 FT_FRAME_USHORT( log_dir_offset ), 208 209 FT_FRAME_USHORT( log_font_max_size ), 210 FT_FRAME_UOFF3 ( log_font_section_size ), 211 FT_FRAME_UOFF3 ( log_font_section_offset ), 212 213 FT_FRAME_USHORT( phy_font_max_size ), 214 FT_FRAME_UOFF3 ( phy_font_section_size ), 215 FT_FRAME_UOFF3 ( phy_font_section_offset ), 216 217 FT_FRAME_USHORT( gps_max_size ), 218 FT_FRAME_UOFF3 ( gps_section_size ), 219 FT_FRAME_UOFF3 ( gps_section_offset ), 220 221 FT_FRAME_BYTE ( max_blue_values ), 222 FT_FRAME_BYTE ( max_x_orus ), 223 FT_FRAME_BYTE ( max_y_orus ), 224 225 FT_FRAME_BYTE ( phy_font_max_size_high ), 226 FT_FRAME_BYTE ( color_flags ), 227 228 FT_FRAME_UOFF3 ( bct_max_size ), 229 FT_FRAME_UOFF3 ( bct_set_max_size ), 230 FT_FRAME_UOFF3 ( phy_bct_set_max_size ), 231 232 FT_FRAME_USHORT( num_phy_fonts ), 233 FT_FRAME_BYTE ( max_vert_stem_snap ), 234 FT_FRAME_BYTE ( max_horz_stem_snap ), 235 FT_FRAME_USHORT( max_chars ), 236 FT_FRAME_END 237 }; 238 239 240 FT_LOCAL_DEF( FT_Error ) pfr_header_load(PFR_Header header,FT_Stream stream)241 pfr_header_load( PFR_Header header, 242 FT_Stream stream ) 243 { 244 FT_Error error; 245 246 247 /* read header directly */ 248 if ( !FT_STREAM_SEEK( 0 ) && 249 !FT_STREAM_READ_FIELDS( pfr_header_fields, header ) ) 250 { 251 /* make a few adjustments to the header */ 252 header->phy_font_max_size += 253 (FT_UInt32)header->phy_font_max_size_high << 16; 254 } 255 256 return error; 257 } 258 259 260 FT_LOCAL_DEF( FT_Bool ) pfr_header_check(PFR_Header header)261 pfr_header_check( PFR_Header header ) 262 { 263 FT_Bool result = 1; 264 265 266 /* check signature and header size */ 267 if ( header->signature != 0x50465230L || /* "PFR0" */ 268 header->version > 4 || 269 header->header_size < 58 || 270 header->signature2 != 0x0D0A ) /* CR/LF */ 271 result = 0; 272 273 return result; 274 } 275 276 277 /***********************************************************************/ 278 /***********************************************************************/ 279 /***** *****/ 280 /***** PFR LOGICAL FONTS *****/ 281 /***** *****/ 282 /***********************************************************************/ 283 /***********************************************************************/ 284 285 286 FT_LOCAL_DEF( FT_Error ) pfr_log_font_count(FT_Stream stream,FT_UInt32 section_offset,FT_Long * acount)287 pfr_log_font_count( FT_Stream stream, 288 FT_UInt32 section_offset, 289 FT_Long *acount ) 290 { 291 FT_Error error; 292 FT_UInt count; 293 FT_UInt result = 0; 294 295 296 if ( FT_STREAM_SEEK( section_offset ) || 297 FT_READ_USHORT( count ) ) 298 goto Exit; 299 300 /* check maximum value and a rough minimum size: */ 301 /* - no more than 13106 log fonts */ 302 /* - we need 5 bytes for a log header record */ 303 /* - we need at least 18 bytes for a log font record */ 304 /* - the overall size is at least 95 bytes plus the */ 305 /* log header and log font records */ 306 if ( count > ( ( 1 << 16 ) - 2 ) / 5 || 307 2 + count * 5 >= stream->size - section_offset || 308 95 + count * ( 5 + 18 ) >= stream->size ) 309 { 310 FT_ERROR(( "pfr_log_font_count:" 311 " invalid number of logical fonts\n" )); 312 error = FT_THROW( Invalid_Table ); 313 goto Exit; 314 } 315 316 result = count; 317 318 Exit: 319 *acount = (FT_Long)result; 320 return error; 321 } 322 323 324 FT_LOCAL_DEF( FT_Error ) pfr_log_font_load(PFR_LogFont log_font,FT_Stream stream,FT_UInt idx,FT_UInt32 section_offset,FT_Bool size_increment)325 pfr_log_font_load( PFR_LogFont log_font, 326 FT_Stream stream, 327 FT_UInt idx, 328 FT_UInt32 section_offset, 329 FT_Bool size_increment ) 330 { 331 FT_UInt num_log_fonts; 332 FT_UInt flags; 333 FT_UInt32 offset; 334 FT_UInt32 size; 335 FT_Error error; 336 337 338 if ( FT_STREAM_SEEK( section_offset ) || 339 FT_READ_USHORT( num_log_fonts ) ) 340 goto Exit; 341 342 if ( idx >= num_log_fonts ) 343 return FT_THROW( Invalid_Argument ); 344 345 if ( FT_STREAM_SKIP( idx * 5 ) || 346 FT_READ_USHORT( size ) || 347 FT_READ_UOFF3 ( offset ) ) 348 goto Exit; 349 350 /* save logical font size and offset */ 351 log_font->size = size; 352 log_font->offset = offset; 353 354 /* now, check the rest of the table before loading it */ 355 { 356 FT_Byte* p; 357 FT_Byte* limit; 358 FT_UInt local; 359 360 361 if ( FT_STREAM_SEEK( offset ) || 362 FT_FRAME_ENTER( size ) ) 363 goto Exit; 364 365 p = stream->cursor; 366 limit = p + size; 367 368 PFR_CHECK( 13 ); 369 370 log_font->matrix[0] = PFR_NEXT_LONG( p ); 371 log_font->matrix[1] = PFR_NEXT_LONG( p ); 372 log_font->matrix[2] = PFR_NEXT_LONG( p ); 373 log_font->matrix[3] = PFR_NEXT_LONG( p ); 374 375 flags = PFR_NEXT_BYTE( p ); 376 377 local = 0; 378 if ( flags & PFR_LOG_STROKE ) 379 { 380 local++; 381 if ( flags & PFR_LOG_2BYTE_STROKE ) 382 local++; 383 384 if ( ( flags & PFR_LINE_JOIN_MASK ) == PFR_LINE_JOIN_MITER ) 385 local += 3; 386 } 387 if ( flags & PFR_LOG_BOLD ) 388 { 389 local++; 390 if ( flags & PFR_LOG_2BYTE_BOLD ) 391 local++; 392 } 393 394 PFR_CHECK( local ); 395 396 if ( flags & PFR_LOG_STROKE ) 397 { 398 log_font->stroke_thickness = ( flags & PFR_LOG_2BYTE_STROKE ) 399 ? PFR_NEXT_SHORT( p ) 400 : PFR_NEXT_BYTE( p ); 401 402 if ( ( flags & PFR_LINE_JOIN_MASK ) == PFR_LINE_JOIN_MITER ) 403 log_font->miter_limit = PFR_NEXT_LONG( p ); 404 } 405 406 if ( flags & PFR_LOG_BOLD ) 407 log_font->bold_thickness = ( flags & PFR_LOG_2BYTE_BOLD ) 408 ? PFR_NEXT_SHORT( p ) 409 : PFR_NEXT_BYTE( p ); 410 411 if ( flags & PFR_LOG_EXTRA_ITEMS ) 412 { 413 error = pfr_extra_items_skip( &p, limit ); 414 if ( error ) 415 goto Fail; 416 } 417 418 PFR_CHECK( 5 ); 419 log_font->phys_size = PFR_NEXT_USHORT( p ); 420 log_font->phys_offset = PFR_NEXT_ULONG( p ); 421 if ( size_increment ) 422 { 423 PFR_CHECK( 1 ); 424 log_font->phys_size += (FT_UInt32)PFR_NEXT_BYTE( p ) << 16; 425 } 426 } 427 428 Fail: 429 FT_FRAME_EXIT(); 430 431 Exit: 432 return error; 433 434 Too_Short: 435 FT_ERROR(( "pfr_log_font_load: invalid logical font table\n" )); 436 error = FT_THROW( Invalid_Table ); 437 goto Fail; 438 } 439 440 441 /***********************************************************************/ 442 /***********************************************************************/ 443 /***** *****/ 444 /***** PFR PHYSICAL FONTS *****/ 445 /***** *****/ 446 /***********************************************************************/ 447 /***********************************************************************/ 448 449 450 /* load bitmap strikes lists */ 451 FT_CALLBACK_DEF( FT_Error ) pfr_extra_item_load_bitmap_info(FT_Byte * p,FT_Byte * limit,PFR_PhyFont phy_font)452 pfr_extra_item_load_bitmap_info( FT_Byte* p, 453 FT_Byte* limit, 454 PFR_PhyFont phy_font ) 455 { 456 FT_Memory memory = phy_font->memory; 457 PFR_Strike strike; 458 FT_UInt flags0; 459 FT_UInt n, count, size1; 460 FT_Error error = FT_Err_Ok; 461 462 463 PFR_CHECK( 5 ); 464 465 p += 3; /* skip bctSize */ 466 flags0 = PFR_NEXT_BYTE( p ); 467 count = PFR_NEXT_BYTE( p ); 468 469 /* re-allocate when needed */ 470 if ( phy_font->num_strikes + count > phy_font->max_strikes ) 471 { 472 FT_UInt new_max = FT_PAD_CEIL( phy_font->num_strikes + count, 4 ); 473 474 475 if ( FT_RENEW_ARRAY( phy_font->strikes, 476 phy_font->num_strikes, 477 new_max ) ) 478 goto Exit; 479 480 phy_font->max_strikes = new_max; 481 } 482 483 size1 = 1 + 1 + 1 + 2 + 2 + 1; 484 if ( flags0 & PFR_STRIKE_2BYTE_XPPM ) 485 size1++; 486 487 if ( flags0 & PFR_STRIKE_2BYTE_YPPM ) 488 size1++; 489 490 if ( flags0 & PFR_STRIKE_3BYTE_SIZE ) 491 size1++; 492 493 if ( flags0 & PFR_STRIKE_3BYTE_OFFSET ) 494 size1++; 495 496 if ( flags0 & PFR_STRIKE_2BYTE_COUNT ) 497 size1++; 498 499 strike = phy_font->strikes + phy_font->num_strikes; 500 501 PFR_CHECK( count * size1 ); 502 503 for ( n = 0; n < count; n++, strike++ ) 504 { 505 strike->x_ppm = ( flags0 & PFR_STRIKE_2BYTE_XPPM ) 506 ? PFR_NEXT_USHORT( p ) 507 : PFR_NEXT_BYTE( p ); 508 509 strike->y_ppm = ( flags0 & PFR_STRIKE_2BYTE_YPPM ) 510 ? PFR_NEXT_USHORT( p ) 511 : PFR_NEXT_BYTE( p ); 512 513 strike->flags = PFR_NEXT_BYTE( p ); 514 515 strike->bct_size = ( flags0 & PFR_STRIKE_3BYTE_SIZE ) 516 ? PFR_NEXT_ULONG( p ) 517 : PFR_NEXT_USHORT( p ); 518 519 strike->bct_offset = ( flags0 & PFR_STRIKE_3BYTE_OFFSET ) 520 ? PFR_NEXT_ULONG( p ) 521 : PFR_NEXT_USHORT( p ); 522 523 strike->num_bitmaps = ( flags0 & PFR_STRIKE_2BYTE_COUNT ) 524 ? PFR_NEXT_USHORT( p ) 525 : PFR_NEXT_BYTE( p ); 526 } 527 528 phy_font->num_strikes += count; 529 530 Exit: 531 return error; 532 533 Too_Short: 534 error = FT_THROW( Invalid_Table ); 535 FT_ERROR(( "pfr_extra_item_load_bitmap_info:" 536 " invalid bitmap info table\n" )); 537 goto Exit; 538 } 539 540 541 /* Load font ID. This is a so-called `unique' name that is rather 542 * long and descriptive (like `Tiresias ScreenFont v7.51'). 543 * 544 * Note that a PFR font's family name is contained in an *undocumented* 545 * string of the `auxiliary data' portion of a physical font record. This 546 * may also contain the `real' style name! 547 * 548 * If no family name is present, the font ID is used instead for the 549 * family. 550 */ 551 FT_CALLBACK_DEF( FT_Error ) pfr_extra_item_load_font_id(FT_Byte * p,FT_Byte * limit,PFR_PhyFont phy_font)552 pfr_extra_item_load_font_id( FT_Byte* p, 553 FT_Byte* limit, 554 PFR_PhyFont phy_font ) 555 { 556 FT_Error error = FT_Err_Ok; 557 FT_Memory memory = phy_font->memory; 558 FT_UInt len = (FT_UInt)( limit - p ); 559 560 561 if ( phy_font->font_id ) 562 goto Exit; 563 564 if ( FT_QALLOC( phy_font->font_id, len + 1 ) ) 565 goto Exit; 566 567 /* copy font ID name, and terminate it for safety */ 568 FT_MEM_COPY( phy_font->font_id, p, len ); 569 phy_font->font_id[len] = 0; 570 571 Exit: 572 return error; 573 } 574 575 576 /* load stem snap tables */ 577 FT_CALLBACK_DEF( FT_Error ) pfr_extra_item_load_stem_snaps(FT_Byte * p,FT_Byte * limit,PFR_PhyFont phy_font)578 pfr_extra_item_load_stem_snaps( FT_Byte* p, 579 FT_Byte* limit, 580 PFR_PhyFont phy_font ) 581 { 582 FT_UInt count, num_vert, num_horz; 583 FT_Int* snaps = NULL; 584 FT_Error error = FT_Err_Ok; 585 FT_Memory memory = phy_font->memory; 586 587 588 if ( phy_font->vertical.stem_snaps ) 589 goto Exit; 590 591 PFR_CHECK( 1 ); 592 count = PFR_NEXT_BYTE( p ); 593 594 num_vert = count & 15; 595 num_horz = count >> 4; 596 count = num_vert + num_horz; 597 598 PFR_CHECK( count * 2 ); 599 600 if ( FT_QNEW_ARRAY( snaps, count ) ) 601 goto Exit; 602 603 phy_font->vertical.stem_snaps = snaps; 604 phy_font->horizontal.stem_snaps = snaps + num_vert; 605 606 for ( ; count > 0; count--, snaps++ ) 607 *snaps = FT_NEXT_SHORT( p ); 608 609 Exit: 610 return error; 611 612 Too_Short: 613 error = FT_THROW( Invalid_Table ); 614 FT_ERROR(( "pfr_extra_item_load_stem_snaps:" 615 " invalid stem snaps table\n" )); 616 goto Exit; 617 } 618 619 620 /* load kerning pair data */ 621 FT_CALLBACK_DEF( FT_Error ) pfr_extra_item_load_kerning_pairs(FT_Byte * p,FT_Byte * limit,PFR_PhyFont phy_font)622 pfr_extra_item_load_kerning_pairs( FT_Byte* p, 623 FT_Byte* limit, 624 PFR_PhyFont phy_font ) 625 { 626 PFR_KernItem item = NULL; 627 FT_Error error = FT_Err_Ok; 628 FT_Memory memory = phy_font->memory; 629 630 631 if ( FT_NEW( item ) ) 632 goto Exit; 633 634 PFR_CHECK( 4 ); 635 636 item->pair_count = PFR_NEXT_BYTE( p ); 637 item->base_adj = PFR_NEXT_SHORT( p ); 638 item->flags = PFR_NEXT_BYTE( p ); 639 item->offset = phy_font->offset + 640 (FT_Offset)( p - phy_font->cursor ); 641 642 #ifndef PFR_CONFIG_NO_CHECKS 643 item->pair_size = 3; 644 645 if ( item->flags & PFR_KERN_2BYTE_CHAR ) 646 item->pair_size += 2; 647 648 if ( item->flags & PFR_KERN_2BYTE_ADJ ) 649 item->pair_size += 1; 650 651 PFR_CHECK( item->pair_count * item->pair_size ); 652 #endif 653 654 /* load first and last pairs into the item to speed up */ 655 /* lookup later... */ 656 if ( item->pair_count > 0 ) 657 { 658 FT_UInt char1, char2; 659 FT_Byte* q; 660 661 662 if ( item->flags & PFR_KERN_2BYTE_CHAR ) 663 { 664 q = p; 665 char1 = PFR_NEXT_USHORT( q ); 666 char2 = PFR_NEXT_USHORT( q ); 667 668 item->pair1 = PFR_KERN_INDEX( char1, char2 ); 669 670 q = p + item->pair_size * ( item->pair_count - 1 ); 671 char1 = PFR_NEXT_USHORT( q ); 672 char2 = PFR_NEXT_USHORT( q ); 673 674 item->pair2 = PFR_KERN_INDEX( char1, char2 ); 675 } 676 else 677 { 678 q = p; 679 char1 = PFR_NEXT_BYTE( q ); 680 char2 = PFR_NEXT_BYTE( q ); 681 682 item->pair1 = PFR_KERN_INDEX( char1, char2 ); 683 684 q = p + item->pair_size * ( item->pair_count - 1 ); 685 char1 = PFR_NEXT_BYTE( q ); 686 char2 = PFR_NEXT_BYTE( q ); 687 688 item->pair2 = PFR_KERN_INDEX( char1, char2 ); 689 } 690 691 /* add new item to the current list */ 692 item->next = NULL; 693 *phy_font->kern_items_tail = item; 694 phy_font->kern_items_tail = &item->next; 695 phy_font->num_kern_pairs += item->pair_count; 696 } 697 else 698 { 699 /* empty item! */ 700 FT_FREE( item ); 701 } 702 703 Exit: 704 return error; 705 706 Too_Short: 707 FT_FREE( item ); 708 709 error = FT_THROW( Invalid_Table ); 710 FT_ERROR(( "pfr_extra_item_load_kerning_pairs:" 711 " invalid kerning pairs table\n" )); 712 goto Exit; 713 } 714 715 716 static const PFR_ExtraItemRec pfr_phy_font_extra_items[] = 717 { 718 { 1, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_bitmap_info }, 719 { 2, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_font_id }, 720 { 3, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_stem_snaps }, 721 { 4, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_kerning_pairs }, 722 { 0, NULL } 723 }; 724 725 726 /* 727 * Load a name from the auxiliary data. Since this extracts undocumented 728 * strings from the font file, we need to be careful here. 729 */ 730 static FT_Error pfr_aux_name_load(FT_Byte * p,FT_UInt len,FT_Memory memory,FT_String ** astring)731 pfr_aux_name_load( FT_Byte* p, 732 FT_UInt len, 733 FT_Memory memory, 734 FT_String* *astring ) 735 { 736 FT_Error error = FT_Err_Ok; 737 FT_String* result = NULL; 738 FT_UInt n, ok; 739 740 741 if ( *astring ) 742 FT_FREE( *astring ); 743 744 if ( len > 0 && p[len - 1] == 0 ) 745 len--; 746 747 /* check that each character is ASCII */ 748 /* for making sure not to load garbage */ 749 ok = ( len > 0 ); 750 for ( n = 0; n < len; n++ ) 751 if ( p[n] < 32 || p[n] > 127 ) 752 { 753 ok = 0; 754 break; 755 } 756 757 if ( ok ) 758 { 759 if ( FT_QALLOC( result, len + 1 ) ) 760 goto Exit; 761 762 FT_MEM_COPY( result, p, len ); 763 result[len] = 0; 764 } 765 766 Exit: 767 *astring = result; 768 return error; 769 } 770 771 772 FT_LOCAL_DEF( void ) pfr_phy_font_done(PFR_PhyFont phy_font,FT_Memory memory)773 pfr_phy_font_done( PFR_PhyFont phy_font, 774 FT_Memory memory ) 775 { 776 FT_FREE( phy_font->font_id ); 777 FT_FREE( phy_font->family_name ); 778 FT_FREE( phy_font->style_name ); 779 780 FT_FREE( phy_font->vertical.stem_snaps ); 781 phy_font->vertical.num_stem_snaps = 0; 782 783 phy_font->horizontal.stem_snaps = NULL; 784 phy_font->horizontal.num_stem_snaps = 0; 785 786 FT_FREE( phy_font->strikes ); 787 phy_font->num_strikes = 0; 788 phy_font->max_strikes = 0; 789 790 FT_FREE( phy_font->chars ); 791 phy_font->num_chars = 0; 792 phy_font->chars_offset = 0; 793 794 FT_FREE( phy_font->blue_values ); 795 phy_font->num_blue_values = 0; 796 797 { 798 PFR_KernItem item, next; 799 800 801 item = phy_font->kern_items; 802 while ( item ) 803 { 804 next = item->next; 805 FT_FREE( item ); 806 item = next; 807 } 808 phy_font->kern_items = NULL; 809 phy_font->kern_items_tail = NULL; 810 } 811 812 phy_font->num_kern_pairs = 0; 813 } 814 815 816 FT_LOCAL_DEF( FT_Error ) pfr_phy_font_load(PFR_PhyFont phy_font,FT_Stream stream,FT_UInt32 offset,FT_UInt32 size)817 pfr_phy_font_load( PFR_PhyFont phy_font, 818 FT_Stream stream, 819 FT_UInt32 offset, 820 FT_UInt32 size ) 821 { 822 FT_Error error; 823 FT_Memory memory = stream->memory; 824 FT_UInt flags; 825 FT_ULong num_aux; 826 FT_Byte* p; 827 FT_Byte* limit; 828 829 830 phy_font->memory = memory; 831 phy_font->offset = offset; 832 833 phy_font->kern_items = NULL; 834 phy_font->kern_items_tail = &phy_font->kern_items; 835 836 if ( FT_STREAM_SEEK( offset ) || 837 FT_FRAME_ENTER( size ) ) 838 goto Exit; 839 840 phy_font->cursor = stream->cursor; 841 842 p = stream->cursor; 843 limit = p + size; 844 845 PFR_CHECK( 15 ); 846 phy_font->font_ref_number = PFR_NEXT_USHORT( p ); 847 phy_font->outline_resolution = PFR_NEXT_USHORT( p ); 848 phy_font->metrics_resolution = PFR_NEXT_USHORT( p ); 849 phy_font->bbox.xMin = PFR_NEXT_SHORT( p ); 850 phy_font->bbox.yMin = PFR_NEXT_SHORT( p ); 851 phy_font->bbox.xMax = PFR_NEXT_SHORT( p ); 852 phy_font->bbox.yMax = PFR_NEXT_SHORT( p ); 853 phy_font->flags = flags = PFR_NEXT_BYTE( p ); 854 855 if ( !phy_font->outline_resolution || 856 !phy_font->metrics_resolution ) 857 { 858 error = FT_THROW( Invalid_Table ); 859 FT_ERROR(( "pfr_phy_font_load: invalid resolution\n" )); 860 goto Fail; 861 } 862 863 /* get the standard advance for non-proportional fonts */ 864 if ( !( flags & PFR_PHY_PROPORTIONAL ) ) 865 { 866 PFR_CHECK( 2 ); 867 phy_font->standard_advance = PFR_NEXT_SHORT( p ); 868 } 869 870 /* load the extra items when present */ 871 if ( flags & PFR_PHY_EXTRA_ITEMS ) 872 { 873 error = pfr_extra_items_parse( &p, limit, 874 pfr_phy_font_extra_items, phy_font ); 875 if ( error ) 876 goto Fail; 877 } 878 879 /* In certain fonts, the auxiliary bytes contain interesting */ 880 /* information. These are not in the specification but can be */ 881 /* guessed by looking at the content of a few 'PFR0' fonts. */ 882 PFR_CHECK( 3 ); 883 num_aux = PFR_NEXT_ULONG( p ); 884 885 if ( num_aux > 0 ) 886 { 887 FT_Byte* q = p; 888 FT_Byte* q2; 889 890 891 PFR_CHECK_SIZE( num_aux ); 892 p += num_aux; 893 894 while ( num_aux > 0 ) 895 { 896 FT_UInt length, type; 897 898 899 if ( q + 4 > p ) 900 break; 901 902 length = PFR_NEXT_USHORT( q ); 903 if ( length < 4 || length > num_aux ) 904 break; 905 906 q2 = q + length - 2; 907 type = PFR_NEXT_USHORT( q ); 908 909 switch ( type ) 910 { 911 case 1: 912 /* this seems to correspond to the font's family name, padded to */ 913 /* an even number of bytes with a zero byte appended if needed */ 914 error = pfr_aux_name_load( q, length - 4U, memory, 915 &phy_font->family_name ); 916 if ( error ) 917 goto Exit; 918 break; 919 920 case 2: 921 if ( q + 32 > q2 ) 922 break; 923 924 q += 10; 925 phy_font->ascent = PFR_NEXT_SHORT( q ); 926 phy_font->descent = PFR_NEXT_SHORT( q ); 927 phy_font->leading = PFR_NEXT_SHORT( q ); 928 break; 929 930 case 3: 931 /* this seems to correspond to the font's style name, padded to */ 932 /* an even number of bytes with a zero byte appended if needed */ 933 error = pfr_aux_name_load( q, length - 4U, memory, 934 &phy_font->style_name ); 935 if ( error ) 936 goto Exit; 937 break; 938 939 default: 940 ; 941 } 942 943 q = q2; 944 num_aux -= length; 945 } 946 } 947 948 /* read the blue values */ 949 { 950 FT_UInt n, count; 951 952 953 PFR_CHECK( 1 ); 954 phy_font->num_blue_values = count = PFR_NEXT_BYTE( p ); 955 956 PFR_CHECK( count * 2 ); 957 958 if ( FT_QNEW_ARRAY( phy_font->blue_values, count ) ) 959 goto Fail; 960 961 for ( n = 0; n < count; n++ ) 962 phy_font->blue_values[n] = PFR_NEXT_SHORT( p ); 963 } 964 965 PFR_CHECK( 8 ); 966 phy_font->blue_fuzz = PFR_NEXT_BYTE( p ); 967 phy_font->blue_scale = PFR_NEXT_BYTE( p ); 968 969 phy_font->vertical.standard = PFR_NEXT_USHORT( p ); 970 phy_font->horizontal.standard = PFR_NEXT_USHORT( p ); 971 972 /* read the character descriptors */ 973 { 974 FT_UInt n, count, Size; 975 976 977 phy_font->num_chars = count = PFR_NEXT_USHORT( p ); 978 phy_font->chars_offset = offset + (FT_Offset)( p - stream->cursor ); 979 980 if ( !phy_font->num_chars ) 981 { 982 error = FT_THROW( Invalid_Table ); 983 FT_ERROR(( "pfr_phy_font_load: no glyphs\n" )); 984 goto Fail; 985 } 986 987 Size = 1 + 1 + 2; 988 if ( flags & PFR_PHY_2BYTE_CHARCODE ) 989 Size += 1; 990 991 if ( flags & PFR_PHY_PROPORTIONAL ) 992 Size += 2; 993 994 if ( flags & PFR_PHY_ASCII_CODE ) 995 Size += 1; 996 997 if ( flags & PFR_PHY_2BYTE_GPS_SIZE ) 998 Size += 1; 999 1000 if ( flags & PFR_PHY_3BYTE_GPS_OFFSET ) 1001 Size += 1; 1002 1003 PFR_CHECK_SIZE( count * Size ); 1004 1005 if ( FT_QNEW_ARRAY( phy_font->chars, count ) ) 1006 goto Fail; 1007 1008 for ( n = 0; n < count; n++ ) 1009 { 1010 PFR_Char cur = &phy_font->chars[n]; 1011 1012 1013 cur->char_code = ( flags & PFR_PHY_2BYTE_CHARCODE ) 1014 ? PFR_NEXT_USHORT( p ) 1015 : PFR_NEXT_BYTE( p ); 1016 1017 cur->advance = ( flags & PFR_PHY_PROPORTIONAL ) 1018 ? PFR_NEXT_SHORT( p ) 1019 : phy_font->standard_advance; 1020 1021 #if 0 1022 cur->ascii = ( flags & PFR_PHY_ASCII_CODE ) 1023 ? PFR_NEXT_BYTE( p ) 1024 : 0; 1025 #else 1026 if ( flags & PFR_PHY_ASCII_CODE ) 1027 p += 1; 1028 #endif 1029 cur->gps_size = ( flags & PFR_PHY_2BYTE_GPS_SIZE ) 1030 ? PFR_NEXT_USHORT( p ) 1031 : PFR_NEXT_BYTE( p ); 1032 1033 cur->gps_offset = ( flags & PFR_PHY_3BYTE_GPS_OFFSET ) 1034 ? PFR_NEXT_ULONG( p ) 1035 : PFR_NEXT_USHORT( p ); 1036 } 1037 } 1038 1039 /* that's it! */ 1040 1041 Fail: 1042 FT_FRAME_EXIT(); 1043 1044 /* save position of bitmap info */ 1045 phy_font->bct_offset = FT_STREAM_POS(); 1046 phy_font->cursor = NULL; 1047 1048 Exit: 1049 return error; 1050 1051 Too_Short: 1052 error = FT_THROW( Invalid_Table ); 1053 FT_ERROR(( "pfr_phy_font_load: invalid physical font table\n" )); 1054 goto Fail; 1055 } 1056 1057 1058 /* END */ 1059