1 /***************************************************************************/ 2 /* */ 3 /* ttpload.c */ 4 /* */ 5 /* TrueType-specific tables loader (body). */ 6 /* */ 7 /* Copyright 1996-2015 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 <ft2build.h> 20 #include FT_INTERNAL_DEBUG_H 21 #include FT_INTERNAL_OBJECTS_H 22 #include FT_INTERNAL_STREAM_H 23 #include FT_TRUETYPE_TAGS_H 24 25 #include "ttpload.h" 26 27 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 28 #include "ttgxvar.h" 29 #endif 30 31 #include "tterrors.h" 32 33 34 /*************************************************************************/ 35 /* */ 36 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 37 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 38 /* messages during execution. */ 39 /* */ 40 #undef FT_COMPONENT 41 #define FT_COMPONENT trace_ttpload 42 43 44 /*************************************************************************/ 45 /* */ 46 /* <Function> */ 47 /* tt_face_load_loca */ 48 /* */ 49 /* <Description> */ 50 /* Load the locations table. */ 51 /* */ 52 /* <InOut> */ 53 /* face :: A handle to the target face object. */ 54 /* */ 55 /* <Input> */ 56 /* stream :: The input stream. */ 57 /* */ 58 /* <Return> */ 59 /* FreeType error code. 0 means success. */ 60 /* */ 61 FT_LOCAL_DEF( FT_Error ) 62 tt_face_load_loca( TT_Face face, 63 FT_Stream stream ) 64 { 65 FT_Error error; 66 FT_ULong table_len; 67 FT_Int shift; 68 69 70 /* we need the size of the `glyf' table for malformed `loca' tables */ 71 error = face->goto_table( face, TTAG_glyf, stream, &face->glyf_len ); 72 73 /* it is possible that a font doesn't have a glyf table at all */ 74 /* or its size is zero */ 75 if ( FT_ERR_EQ( error, Table_Missing ) ) 76 face->glyf_len = 0; 77 else if ( error ) 78 goto Exit; 79 80 FT_TRACE2(( "Locations " )); 81 error = face->goto_table( face, TTAG_loca, stream, &table_len ); 82 if ( error ) 83 { 84 error = FT_THROW( Locations_Missing ); 85 goto Exit; 86 } 87 88 if ( face->header.Index_To_Loc_Format != 0 ) 89 { 90 shift = 2; 91 92 if ( table_len >= 0x40000L ) 93 { 94 FT_TRACE2(( "table too large\n" )); 95 error = FT_THROW( Invalid_Table ); 96 goto Exit; 97 } 98 face->num_locations = table_len >> shift; 99 } 100 else 101 { 102 shift = 1; 103 104 if ( table_len >= 0x20000L ) 105 { 106 FT_TRACE2(( "table too large\n" )); 107 error = FT_THROW( Invalid_Table ); 108 goto Exit; 109 } 110 face->num_locations = table_len >> shift; 111 } 112 113 if ( face->num_locations != (FT_ULong)face->root.num_glyphs + 1 ) 114 { 115 FT_TRACE2(( "glyph count mismatch! loca: %d, maxp: %d\n", 116 face->num_locations - 1, face->root.num_glyphs )); 117 118 /* we only handle the case where `maxp' gives a larger value */ 119 if ( face->num_locations <= (FT_ULong)face->root.num_glyphs ) 120 { 121 FT_ULong new_loca_len = 122 ( (FT_ULong)face->root.num_glyphs + 1 ) << shift; 123 124 TT_Table entry = face->dir_tables; 125 TT_Table limit = entry + face->num_tables; 126 127 FT_Long pos = (FT_Long)FT_STREAM_POS(); 128 FT_Long dist = 0x7FFFFFFFL; 129 130 131 /* compute the distance to next table in font file */ 132 for ( ; entry < limit; entry++ ) 133 { 134 FT_Long diff = (FT_Long)entry->Offset - pos; 135 136 137 if ( diff > 0 && diff < dist ) 138 dist = diff; 139 } 140 141 if ( entry == limit ) 142 { 143 /* `loca' is the last table */ 144 dist = (FT_Long)stream->size - pos; 145 } 146 147 if ( new_loca_len <= (FT_ULong)dist ) 148 { 149 face->num_locations = (FT_ULong)face->root.num_glyphs + 1; 150 table_len = new_loca_len; 151 152 FT_TRACE2(( "adjusting num_locations to %d\n", 153 face->num_locations )); 154 } 155 else 156 { 157 face->root.num_glyphs = face->num_locations 158 ? (FT_Long)face->num_locations - 1 : 0; 159 160 FT_TRACE2(( "adjusting num_glyphs to %d\n", 161 face->root.num_glyphs )); 162 } 163 } 164 } 165 166 /* 167 * Extract the frame. We don't need to decompress it since 168 * we are able to parse it directly. 169 */ 170 if ( FT_FRAME_EXTRACT( table_len, face->glyph_locations ) ) 171 goto Exit; 172 173 FT_TRACE2(( "loaded\n" )); 174 175 Exit: 176 return error; 177 } 178 179 180 FT_LOCAL_DEF( FT_ULong ) 181 tt_face_get_location( TT_Face face, 182 FT_UInt gindex, 183 FT_UInt *asize ) 184 { 185 FT_ULong pos1, pos2; 186 FT_Byte* p; 187 FT_Byte* p_limit; 188 189 190 pos1 = pos2 = 0; 191 192 if ( gindex < face->num_locations ) 193 { 194 if ( face->header.Index_To_Loc_Format != 0 ) 195 { 196 p = face->glyph_locations + gindex * 4; 197 p_limit = face->glyph_locations + face->num_locations * 4; 198 199 pos1 = FT_NEXT_ULONG( p ); 200 pos2 = pos1; 201 202 if ( p + 4 <= p_limit ) 203 pos2 = FT_NEXT_ULONG( p ); 204 } 205 else 206 { 207 p = face->glyph_locations + gindex * 2; 208 p_limit = face->glyph_locations + face->num_locations * 2; 209 210 pos1 = FT_NEXT_USHORT( p ); 211 pos2 = pos1; 212 213 if ( p + 2 <= p_limit ) 214 pos2 = FT_NEXT_USHORT( p ); 215 216 pos1 <<= 1; 217 pos2 <<= 1; 218 } 219 } 220 221 /* Check broken location data */ 222 if ( pos1 > face->glyf_len ) 223 { 224 FT_TRACE1(( "tt_face_get_location:" 225 " too large offset=0x%08lx found for gid=0x%04lx," 226 " exceeding the end of glyf table (0x%08lx)\n", 227 pos1, gindex, face->glyf_len )); 228 *asize = 0; 229 return 0; 230 } 231 232 if ( pos2 > face->glyf_len ) 233 { 234 FT_TRACE1(( "tt_face_get_location:" 235 " too large offset=0x%08lx found for gid=0x%04lx," 236 " truncate at the end of glyf table (0x%08lx)\n", 237 pos2, gindex + 1, face->glyf_len )); 238 pos2 = face->glyf_len; 239 } 240 241 /* The `loca' table must be ordered; it refers to the length of */ 242 /* an entry as the difference between the current and the next */ 243 /* position. However, there do exist (malformed) fonts which */ 244 /* don't obey this rule, so we are only able to provide an */ 245 /* upper bound for the size. */ 246 /* */ 247 /* We get (intentionally) a wrong, non-zero result in case the */ 248 /* `glyf' table is missing. */ 249 if ( pos2 >= pos1 ) 250 *asize = (FT_UInt)( pos2 - pos1 ); 251 else 252 *asize = (FT_UInt)( face->glyf_len - pos1 ); 253 254 return pos1; 255 } 256 257 258 FT_LOCAL_DEF( void ) 259 tt_face_done_loca( TT_Face face ) 260 { 261 FT_Stream stream = face->root.stream; 262 263 264 FT_FRAME_RELEASE( face->glyph_locations ); 265 face->num_locations = 0; 266 } 267 268 269 270 /*************************************************************************/ 271 /* */ 272 /* <Function> */ 273 /* tt_face_load_cvt */ 274 /* */ 275 /* <Description> */ 276 /* Load the control value table into a face object. */ 277 /* */ 278 /* <InOut> */ 279 /* face :: A handle to the target face object. */ 280 /* */ 281 /* <Input> */ 282 /* stream :: A handle to the input stream. */ 283 /* */ 284 /* <Return> */ 285 /* FreeType error code. 0 means success. */ 286 /* */ 287 FT_LOCAL_DEF( FT_Error ) 288 tt_face_load_cvt( TT_Face face, 289 FT_Stream stream ) 290 { 291 #ifdef TT_USE_BYTECODE_INTERPRETER 292 293 FT_Error error; 294 FT_Memory memory = stream->memory; 295 FT_ULong table_len; 296 297 298 FT_TRACE2(( "CVT " )); 299 300 error = face->goto_table( face, TTAG_cvt, stream, &table_len ); 301 if ( error ) 302 { 303 FT_TRACE2(( "is missing\n" )); 304 305 face->cvt_size = 0; 306 face->cvt = NULL; 307 error = FT_Err_Ok; 308 309 goto Exit; 310 } 311 312 face->cvt_size = table_len / 2; 313 314 if ( FT_NEW_ARRAY( face->cvt, face->cvt_size ) ) 315 goto Exit; 316 317 if ( FT_FRAME_ENTER( face->cvt_size * 2L ) ) 318 goto Exit; 319 320 { 321 FT_Short* cur = face->cvt; 322 FT_Short* limit = cur + face->cvt_size; 323 324 325 for ( ; cur < limit; cur++ ) 326 *cur = FT_GET_SHORT(); 327 } 328 329 FT_FRAME_EXIT(); 330 FT_TRACE2(( "loaded\n" )); 331 332 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 333 if ( face->doblend ) 334 error = tt_face_vary_cvt( face, stream ); 335 #endif 336 337 Exit: 338 return error; 339 340 #else /* !TT_USE_BYTECODE_INTERPRETER */ 341 342 FT_UNUSED( face ); 343 FT_UNUSED( stream ); 344 345 return FT_Err_Ok; 346 347 #endif 348 } 349 350 351 /*************************************************************************/ 352 /* */ 353 /* <Function> */ 354 /* tt_face_load_fpgm */ 355 /* */ 356 /* <Description> */ 357 /* Load the font program. */ 358 /* */ 359 /* <InOut> */ 360 /* face :: A handle to the target face object. */ 361 /* */ 362 /* <Input> */ 363 /* stream :: A handle to the input stream. */ 364 /* */ 365 /* <Return> */ 366 /* FreeType error code. 0 means success. */ 367 /* */ 368 FT_LOCAL_DEF( FT_Error ) 369 tt_face_load_fpgm( TT_Face face, 370 FT_Stream stream ) 371 { 372 #ifdef TT_USE_BYTECODE_INTERPRETER 373 374 FT_Error error; 375 FT_ULong table_len; 376 377 378 FT_TRACE2(( "Font program " )); 379 380 /* The font program is optional */ 381 error = face->goto_table( face, TTAG_fpgm, stream, &table_len ); 382 if ( error ) 383 { 384 face->font_program = NULL; 385 face->font_program_size = 0; 386 error = FT_Err_Ok; 387 388 FT_TRACE2(( "is missing\n" )); 389 } 390 else 391 { 392 face->font_program_size = table_len; 393 if ( FT_FRAME_EXTRACT( table_len, face->font_program ) ) 394 goto Exit; 395 396 FT_TRACE2(( "loaded, %12d bytes\n", face->font_program_size )); 397 } 398 399 Exit: 400 return error; 401 402 #else /* !TT_USE_BYTECODE_INTERPRETER */ 403 404 FT_UNUSED( face ); 405 FT_UNUSED( stream ); 406 407 return FT_Err_Ok; 408 409 #endif 410 } 411 412 413 /*************************************************************************/ 414 /* */ 415 /* <Function> */ 416 /* tt_face_load_prep */ 417 /* */ 418 /* <Description> */ 419 /* Load the cvt program. */ 420 /* */ 421 /* <InOut> */ 422 /* face :: A handle to the target face object. */ 423 /* */ 424 /* <Input> */ 425 /* stream :: A handle to the input stream. */ 426 /* */ 427 /* <Return> */ 428 /* FreeType error code. 0 means success. */ 429 /* */ 430 FT_LOCAL_DEF( FT_Error ) 431 tt_face_load_prep( TT_Face face, 432 FT_Stream stream ) 433 { 434 #ifdef TT_USE_BYTECODE_INTERPRETER 435 436 FT_Error error; 437 FT_ULong table_len; 438 439 440 FT_TRACE2(( "Prep program " )); 441 442 error = face->goto_table( face, TTAG_prep, stream, &table_len ); 443 if ( error ) 444 { 445 face->cvt_program = NULL; 446 face->cvt_program_size = 0; 447 error = FT_Err_Ok; 448 449 FT_TRACE2(( "is missing\n" )); 450 } 451 else 452 { 453 face->cvt_program_size = table_len; 454 if ( FT_FRAME_EXTRACT( table_len, face->cvt_program ) ) 455 goto Exit; 456 457 FT_TRACE2(( "loaded, %12d bytes\n", face->cvt_program_size )); 458 } 459 460 Exit: 461 return error; 462 463 #else /* !TT_USE_BYTECODE_INTERPRETER */ 464 465 FT_UNUSED( face ); 466 FT_UNUSED( stream ); 467 468 return FT_Err_Ok; 469 470 #endif 471 } 472 473 474 /*************************************************************************/ 475 /* */ 476 /* <Function> */ 477 /* tt_face_load_hdmx */ 478 /* */ 479 /* <Description> */ 480 /* Load the `hdmx' table into the face object. */ 481 /* */ 482 /* <Input> */ 483 /* face :: A handle to the target face object. */ 484 /* */ 485 /* stream :: A handle to the input stream. */ 486 /* */ 487 /* <Return> */ 488 /* FreeType error code. 0 means success. */ 489 /* */ 490 491 FT_LOCAL_DEF( FT_Error ) 492 tt_face_load_hdmx( TT_Face face, 493 FT_Stream stream ) 494 { 495 FT_Error error; 496 FT_Memory memory = stream->memory; 497 FT_UInt version, nn, num_records; 498 FT_ULong table_size, record_size; 499 FT_Byte* p; 500 FT_Byte* limit; 501 502 503 /* this table is optional */ 504 error = face->goto_table( face, TTAG_hdmx, stream, &table_size ); 505 if ( error || table_size < 8 ) 506 return FT_Err_Ok; 507 508 if ( FT_FRAME_EXTRACT( table_size, face->hdmx_table ) ) 509 goto Exit; 510 511 p = face->hdmx_table; 512 limit = p + table_size; 513 514 version = FT_NEXT_USHORT( p ); 515 num_records = FT_NEXT_USHORT( p ); 516 record_size = FT_NEXT_ULONG( p ); 517 518 /* The maximum number of bytes in an hdmx device record is the */ 519 /* maximum number of glyphs + 2; this is 0xFFFF + 2, thus */ 520 /* explaining why `record_size' is a long (which we read as */ 521 /* unsigned long for convenience). In practice, two bytes are */ 522 /* sufficient to hold the size value. */ 523 /* */ 524 /* There are at least two fonts, HANNOM-A and HANNOM-B version */ 525 /* 2.0 (2005), which get this wrong: The upper two bytes of */ 526 /* the size value are set to 0xFF instead of 0x00. We catch */ 527 /* and fix this. */ 528 529 if ( record_size >= 0xFFFF0000UL ) 530 record_size &= 0xFFFFU; 531 532 /* The limit for `num_records' is a heuristic value. */ 533 if ( version != 0 || 534 num_records > 255 || 535 record_size > 0x10001L || 536 record_size < 4 ) 537 { 538 error = FT_THROW( Invalid_File_Format ); 539 goto Fail; 540 } 541 542 if ( FT_NEW_ARRAY( face->hdmx_record_sizes, num_records ) ) 543 goto Fail; 544 545 for ( nn = 0; nn < num_records; nn++ ) 546 { 547 if ( p + record_size > limit ) 548 break; 549 550 face->hdmx_record_sizes[nn] = p[0]; 551 p += record_size; 552 } 553 554 face->hdmx_record_count = nn; 555 face->hdmx_table_size = table_size; 556 face->hdmx_record_size = record_size; 557 558 Exit: 559 return error; 560 561 Fail: 562 FT_FRAME_RELEASE( face->hdmx_table ); 563 face->hdmx_table_size = 0; 564 goto Exit; 565 } 566 567 568 FT_LOCAL_DEF( void ) 569 tt_face_free_hdmx( TT_Face face ) 570 { 571 FT_Stream stream = face->root.stream; 572 FT_Memory memory = stream->memory; 573 574 575 FT_FREE( face->hdmx_record_sizes ); 576 FT_FRAME_RELEASE( face->hdmx_table ); 577 } 578 579 580 /*************************************************************************/ 581 /* */ 582 /* Return the advance width table for a given pixel size if it is found */ 583 /* in the font's `hdmx' table (if any). */ 584 /* */ 585 FT_LOCAL_DEF( FT_Byte* ) 586 tt_face_get_device_metrics( TT_Face face, 587 FT_UInt ppem, 588 FT_UInt gindex ) 589 { 590 FT_UInt nn; 591 FT_Byte* result = NULL; 592 FT_ULong record_size = face->hdmx_record_size; 593 FT_Byte* record = face->hdmx_table + 8; 594 595 596 for ( nn = 0; nn < face->hdmx_record_count; nn++ ) 597 if ( face->hdmx_record_sizes[nn] == ppem ) 598 { 599 gindex += 2; 600 if ( gindex < record_size ) 601 result = record + nn * record_size + gindex; 602 break; 603 } 604 605 return result; 606 } 607 608 609 /* END */ 610