1 /***************************************************************************/ 2 /* */ 3 /* ttdriver.c */ 4 /* */ 5 /* TrueType font driver implementation (body). */ 6 /* */ 7 /* Copyright 1996-2017 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_STREAM_H 22 #include FT_INTERNAL_SFNT_H 23 #include FT_SERVICE_FONT_FORMAT_H 24 25 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 26 #include FT_MULTIPLE_MASTERS_H 27 #include FT_SERVICE_MULTIPLE_MASTERS_H 28 #include FT_SERVICE_METRICS_VARIATIONS_H 29 #endif 30 31 #include FT_SERVICE_TRUETYPE_ENGINE_H 32 #include FT_SERVICE_TRUETYPE_GLYF_H 33 #include FT_SERVICE_PROPERTIES_H 34 #include FT_TRUETYPE_DRIVER_H 35 36 #include "ttdriver.h" 37 #include "ttgload.h" 38 #include "ttpload.h" 39 40 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 41 #include "ttgxvar.h" 42 #endif 43 44 #include "tterrors.h" 45 46 #include "ttpic.h" 47 48 /*************************************************************************/ 49 /* */ 50 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 51 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 52 /* messages during execution. */ 53 /* */ 54 #undef FT_COMPONENT 55 #define FT_COMPONENT trace_ttdriver 56 57 58 /* 59 * PROPERTY SERVICE 60 * 61 */ 62 static FT_Error tt_property_set(FT_Module module,const char * property_name,const void * value,FT_Bool value_is_string)63 tt_property_set( FT_Module module, /* TT_Driver */ 64 const char* property_name, 65 const void* value, 66 FT_Bool value_is_string ) 67 { 68 FT_Error error = FT_Err_Ok; 69 TT_Driver driver = (TT_Driver)module; 70 71 #ifndef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES 72 FT_UNUSED( value_is_string ); 73 #endif 74 75 76 if ( !ft_strcmp( property_name, "interpreter-version" ) ) 77 { 78 FT_UInt interpreter_version; 79 80 81 #ifdef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES 82 if ( value_is_string ) 83 { 84 const char* s = (const char*)value; 85 86 87 interpreter_version = (FT_UInt)ft_strtol( s, NULL, 10 ); 88 } 89 else 90 #endif 91 { 92 FT_UInt* iv = (FT_UInt*)value; 93 94 95 interpreter_version = *iv; 96 } 97 98 if ( interpreter_version == TT_INTERPRETER_VERSION_35 99 #ifdef TT_SUPPORT_SUBPIXEL_HINTING_INFINALITY 100 || interpreter_version == TT_INTERPRETER_VERSION_38 101 #endif 102 #ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL 103 || interpreter_version == TT_INTERPRETER_VERSION_40 104 #endif 105 ) 106 driver->interpreter_version = interpreter_version; 107 else 108 error = FT_ERR( Unimplemented_Feature ); 109 110 return error; 111 } 112 113 FT_TRACE0(( "tt_property_set: missing property `%s'\n", 114 property_name )); 115 return FT_THROW( Missing_Property ); 116 } 117 118 119 static FT_Error tt_property_get(FT_Module module,const char * property_name,const void * value)120 tt_property_get( FT_Module module, /* TT_Driver */ 121 const char* property_name, 122 const void* value ) 123 { 124 FT_Error error = FT_Err_Ok; 125 TT_Driver driver = (TT_Driver)module; 126 127 FT_UInt interpreter_version = driver->interpreter_version; 128 129 130 if ( !ft_strcmp( property_name, "interpreter-version" ) ) 131 { 132 FT_UInt* val = (FT_UInt*)value; 133 134 135 *val = interpreter_version; 136 137 return error; 138 } 139 140 FT_TRACE0(( "tt_property_get: missing property `%s'\n", 141 property_name )); 142 return FT_THROW( Missing_Property ); 143 } 144 145 146 FT_DEFINE_SERVICE_PROPERTIESREC( 147 tt_service_properties, 148 149 (FT_Properties_SetFunc)tt_property_set, /* set_property */ 150 (FT_Properties_GetFunc)tt_property_get /* get_property */ 151 ) 152 153 154 /*************************************************************************/ 155 /*************************************************************************/ 156 /*************************************************************************/ 157 /**** ****/ 158 /**** ****/ 159 /**** F A C E S ****/ 160 /**** ****/ 161 /**** ****/ 162 /*************************************************************************/ 163 /*************************************************************************/ 164 /*************************************************************************/ 165 166 167 /*************************************************************************/ 168 /* */ 169 /* <Function> */ 170 /* tt_get_kerning */ 171 /* */ 172 /* <Description> */ 173 /* A driver method used to return the kerning vector between two */ 174 /* glyphs of the same face. */ 175 /* */ 176 /* <Input> */ 177 /* face :: A handle to the source face object. */ 178 /* */ 179 /* left_glyph :: The index of the left glyph in the kern pair. */ 180 /* */ 181 /* right_glyph :: The index of the right glyph in the kern pair. */ 182 /* */ 183 /* <Output> */ 184 /* kerning :: The kerning vector. This is in font units for */ 185 /* scalable formats, and in pixels for fixed-sizes */ 186 /* formats. */ 187 /* */ 188 /* <Return> */ 189 /* FreeType error code. 0 means success. */ 190 /* */ 191 /* <Note> */ 192 /* Only horizontal layouts (left-to-right & right-to-left) are */ 193 /* supported by this function. Other layouts, or more sophisticated */ 194 /* kernings, are out of scope of this method (the basic driver */ 195 /* interface is meant to be simple). */ 196 /* */ 197 /* They can be implemented by format-specific interfaces. */ 198 /* */ 199 static FT_Error tt_get_kerning(FT_Face ttface,FT_UInt left_glyph,FT_UInt right_glyph,FT_Vector * kerning)200 tt_get_kerning( FT_Face ttface, /* TT_Face */ 201 FT_UInt left_glyph, 202 FT_UInt right_glyph, 203 FT_Vector* kerning ) 204 { 205 TT_Face face = (TT_Face)ttface; 206 SFNT_Service sfnt = (SFNT_Service)face->sfnt; 207 208 209 kerning->x = 0; 210 kerning->y = 0; 211 212 if ( sfnt ) 213 kerning->x = sfnt->get_kerning( face, left_glyph, right_glyph ); 214 215 return 0; 216 } 217 218 219 static FT_Error tt_get_advances(FT_Face ttface,FT_UInt start,FT_UInt count,FT_Int32 flags,FT_Fixed * advances)220 tt_get_advances( FT_Face ttface, 221 FT_UInt start, 222 FT_UInt count, 223 FT_Int32 flags, 224 FT_Fixed *advances ) 225 { 226 FT_UInt nn; 227 TT_Face face = (TT_Face)ttface; 228 229 230 /* XXX: TODO: check for sbits */ 231 232 if ( flags & FT_LOAD_VERTICAL_LAYOUT ) 233 { 234 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 235 /* no fast retrieval for blended MM fonts without VVAR table */ 236 if ( !face->is_default_instance && 237 !( face->variation_support & TT_FACE_FLAG_VAR_VADVANCE ) ) 238 return FT_THROW( Unimplemented_Feature ); 239 #endif 240 241 for ( nn = 0; nn < count; nn++ ) 242 { 243 FT_Short tsb; 244 FT_UShort ah; 245 246 247 /* since we don't need `tsb', we use zero for `yMax' parameter */ 248 TT_Get_VMetrics( face, start + nn, 0, &tsb, &ah ); 249 advances[nn] = ah; 250 } 251 } 252 else 253 { 254 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 255 /* no fast retrieval for blended MM fonts without HVAR table */ 256 if ( !face->is_default_instance && 257 !( face->variation_support & TT_FACE_FLAG_VAR_HADVANCE ) ) 258 return FT_THROW( Unimplemented_Feature ); 259 #endif 260 261 for ( nn = 0; nn < count; nn++ ) 262 { 263 FT_Short lsb; 264 FT_UShort aw; 265 266 267 TT_Get_HMetrics( face, start + nn, &lsb, &aw ); 268 advances[nn] = aw; 269 } 270 } 271 272 return FT_Err_Ok; 273 } 274 275 276 /*************************************************************************/ 277 /*************************************************************************/ 278 /*************************************************************************/ 279 /**** ****/ 280 /**** ****/ 281 /**** S I Z E S ****/ 282 /**** ****/ 283 /**** ****/ 284 /*************************************************************************/ 285 /*************************************************************************/ 286 /*************************************************************************/ 287 288 289 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 290 291 static FT_Error tt_size_select(FT_Size size,FT_ULong strike_index)292 tt_size_select( FT_Size size, 293 FT_ULong strike_index ) 294 { 295 TT_Face ttface = (TT_Face)size->face; 296 TT_Size ttsize = (TT_Size)size; 297 FT_Error error = FT_Err_Ok; 298 299 300 ttsize->strike_index = strike_index; 301 302 if ( FT_IS_SCALABLE( size->face ) ) 303 { 304 /* use the scaled metrics, even when tt_size_reset fails */ 305 FT_Select_Metrics( size->face, strike_index ); 306 307 tt_size_reset( ttsize, 0 ); /* ignore return value */ 308 } 309 else 310 { 311 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt; 312 FT_Size_Metrics* metrics = &size->metrics; 313 314 315 error = sfnt->load_strike_metrics( ttface, strike_index, metrics ); 316 if ( error ) 317 ttsize->strike_index = 0xFFFFFFFFUL; 318 } 319 320 return error; 321 } 322 323 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 324 325 326 static FT_Error tt_size_request(FT_Size size,FT_Size_Request req)327 tt_size_request( FT_Size size, 328 FT_Size_Request req ) 329 { 330 TT_Size ttsize = (TT_Size)size; 331 FT_Error error = FT_Err_Ok; 332 333 334 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 335 336 if ( FT_HAS_FIXED_SIZES( size->face ) ) 337 { 338 TT_Face ttface = (TT_Face)size->face; 339 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt; 340 FT_ULong strike_index; 341 342 343 error = sfnt->set_sbit_strike( ttface, req, &strike_index ); 344 345 if ( error ) 346 ttsize->strike_index = 0xFFFFFFFFUL; 347 else 348 return tt_size_select( size, strike_index ); 349 } 350 351 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 352 353 FT_Request_Metrics( size->face, req ); 354 355 if ( FT_IS_SCALABLE( size->face ) ) 356 { 357 error = tt_size_reset( ttsize, 0 ); 358 ttsize->root.metrics = ttsize->metrics; 359 360 #ifdef TT_USE_BYTECODE_INTERPRETER 361 /* for the `MPS' bytecode instruction we need the point size */ 362 { 363 FT_UInt resolution = ttsize->metrics.x_ppem > ttsize->metrics.y_ppem 364 ? req->horiResolution 365 : req->vertResolution; 366 367 368 /* if we don't have a resolution value, assume 72dpi */ 369 if ( req->type == FT_SIZE_REQUEST_TYPE_SCALES || 370 !resolution ) 371 resolution = 72; 372 373 ttsize->point_size = FT_MulDiv( ttsize->ttmetrics.ppem, 374 64 * 72, 375 resolution ); 376 } 377 #endif 378 } 379 380 return error; 381 } 382 383 384 /*************************************************************************/ 385 /* */ 386 /* <Function> */ 387 /* tt_glyph_load */ 388 /* */ 389 /* <Description> */ 390 /* A driver method used to load a glyph within a given glyph slot. */ 391 /* */ 392 /* <Input> */ 393 /* slot :: A handle to the target slot object where the glyph */ 394 /* will be loaded. */ 395 /* */ 396 /* size :: A handle to the source face size at which the glyph */ 397 /* must be scaled, loaded, etc. */ 398 /* */ 399 /* glyph_index :: The index of the glyph in the font file. */ 400 /* */ 401 /* load_flags :: A flag indicating what to load for this glyph. The */ 402 /* FT_LOAD_XXX constants can be used to control the */ 403 /* glyph loading process (e.g., whether the outline */ 404 /* should be scaled, whether to load bitmaps or not, */ 405 /* whether to hint the outline, etc). */ 406 /* */ 407 /* <Return> */ 408 /* FreeType error code. 0 means success. */ 409 /* */ 410 static FT_Error tt_glyph_load(FT_GlyphSlot ttslot,FT_Size ttsize,FT_UInt glyph_index,FT_Int32 load_flags)411 tt_glyph_load( FT_GlyphSlot ttslot, /* TT_GlyphSlot */ 412 FT_Size ttsize, /* TT_Size */ 413 FT_UInt glyph_index, 414 FT_Int32 load_flags ) 415 { 416 TT_GlyphSlot slot = (TT_GlyphSlot)ttslot; 417 TT_Size size = (TT_Size)ttsize; 418 FT_Face face = ttslot->face; 419 FT_Error error; 420 421 422 if ( !slot ) 423 return FT_THROW( Invalid_Slot_Handle ); 424 425 if ( !size ) 426 return FT_THROW( Invalid_Size_Handle ); 427 428 if ( !face ) 429 return FT_THROW( Invalid_Face_Handle ); 430 431 #ifdef FT_CONFIG_OPTION_INCREMENTAL 432 if ( glyph_index >= (FT_UInt)face->num_glyphs && 433 !face->internal->incremental_interface ) 434 #else 435 if ( glyph_index >= (FT_UInt)face->num_glyphs ) 436 #endif 437 return FT_THROW( Invalid_Argument ); 438 439 if ( load_flags & FT_LOAD_NO_HINTING ) 440 { 441 /* both FT_LOAD_NO_HINTING and FT_LOAD_NO_AUTOHINT */ 442 /* are necessary to disable hinting for tricky fonts */ 443 444 if ( FT_IS_TRICKY( face ) ) 445 load_flags &= ~FT_LOAD_NO_HINTING; 446 447 if ( load_flags & FT_LOAD_NO_AUTOHINT ) 448 load_flags |= FT_LOAD_NO_HINTING; 449 } 450 451 if ( load_flags & ( FT_LOAD_NO_RECURSE | FT_LOAD_NO_SCALE ) ) 452 { 453 load_flags |= FT_LOAD_NO_BITMAP | FT_LOAD_NO_SCALE; 454 455 if ( !FT_IS_TRICKY( face ) ) 456 load_flags |= FT_LOAD_NO_HINTING; 457 } 458 459 /* now load the glyph outline if necessary */ 460 error = TT_Load_Glyph( size, slot, glyph_index, load_flags ); 461 462 /* force drop-out mode to 2 - irrelevant now */ 463 /* slot->outline.dropout_mode = 2; */ 464 465 return error; 466 } 467 468 469 /*************************************************************************/ 470 /*************************************************************************/ 471 /*************************************************************************/ 472 /**** ****/ 473 /**** ****/ 474 /**** D R I V E R I N T E R F A C E ****/ 475 /**** ****/ 476 /**** ****/ 477 /*************************************************************************/ 478 /*************************************************************************/ 479 /*************************************************************************/ 480 481 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 482 483 FT_DEFINE_SERVICE_MULTIMASTERSREC( 484 tt_service_gx_multi_masters, 485 486 (FT_Get_MM_Func) NULL, /* get_mm */ 487 (FT_Set_MM_Design_Func) NULL, /* set_mm_design */ 488 (FT_Set_MM_Blend_Func) TT_Set_MM_Blend, /* set_mm_blend */ 489 (FT_Get_MM_Blend_Func) TT_Get_MM_Blend, /* get_mm_blend */ 490 (FT_Get_MM_Var_Func) TT_Get_MM_Var, /* get_mm_var */ 491 (FT_Set_Var_Design_Func)TT_Set_Var_Design, /* set_var_design */ 492 (FT_Get_Var_Design_Func)TT_Get_Var_Design, /* get_var_design */ 493 494 (FT_Get_Var_Blend_Func) tt_get_var_blend, /* get_var_blend */ 495 (FT_Done_Blend_Func) tt_done_blend /* done_blend */ 496 ) 497 498 FT_DEFINE_SERVICE_METRICSVARIATIONSREC( 499 tt_service_metrics_variations, 500 501 (FT_HAdvance_Adjust_Func)tt_hadvance_adjust, /* hadvance_adjust */ 502 (FT_LSB_Adjust_Func) NULL, /* lsb_adjust */ 503 (FT_RSB_Adjust_Func) NULL, /* rsb_adjust */ 504 505 (FT_VAdvance_Adjust_Func)tt_vadvance_adjust, /* vadvance_adjust */ 506 (FT_TSB_Adjust_Func) NULL, /* tsb_adjust */ 507 (FT_BSB_Adjust_Func) NULL, /* bsb_adjust */ 508 (FT_VOrg_Adjust_Func) NULL, /* vorg_adjust */ 509 510 (FT_Metrics_Adjust_Func) tt_apply_mvar /* metrics_adjust */ 511 ) 512 513 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 514 515 516 static const FT_Service_TrueTypeEngineRec tt_service_truetype_engine = 517 { 518 #ifdef TT_USE_BYTECODE_INTERPRETER 519 520 FT_TRUETYPE_ENGINE_TYPE_PATENTED 521 522 #else /* !TT_USE_BYTECODE_INTERPRETER */ 523 524 FT_TRUETYPE_ENGINE_TYPE_NONE 525 526 #endif /* TT_USE_BYTECODE_INTERPRETER */ 527 }; 528 529 530 FT_DEFINE_SERVICE_TTGLYFREC( 531 tt_service_truetype_glyf, 532 533 (TT_Glyf_GetLocationFunc)tt_face_get_location /* get_location */ 534 ) 535 536 537 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 538 FT_DEFINE_SERVICEDESCREC6( 539 tt_services, 540 541 FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_TRUETYPE, 542 FT_SERVICE_ID_MULTI_MASTERS, &TT_SERVICE_GX_MULTI_MASTERS_GET, 543 FT_SERVICE_ID_METRICS_VARIATIONS, &TT_SERVICE_METRICS_VARIATIONS_GET, 544 FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine, 545 FT_SERVICE_ID_TT_GLYF, &TT_SERVICE_TRUETYPE_GLYF_GET, 546 FT_SERVICE_ID_PROPERTIES, &TT_SERVICE_PROPERTIES_GET ) 547 #else 548 FT_DEFINE_SERVICEDESCREC4( 549 tt_services, 550 551 FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_TRUETYPE, 552 FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine, 553 FT_SERVICE_ID_TT_GLYF, &TT_SERVICE_TRUETYPE_GLYF_GET, 554 FT_SERVICE_ID_PROPERTIES, &TT_SERVICE_PROPERTIES_GET ) 555 #endif 556 557 FT_CALLBACK_DEF(FT_Module_Interface)558 FT_CALLBACK_DEF( FT_Module_Interface ) 559 tt_get_interface( FT_Module driver, /* TT_Driver */ 560 const char* tt_interface ) 561 { 562 FT_Library library; 563 FT_Module_Interface result; 564 FT_Module sfntd; 565 SFNT_Service sfnt; 566 567 568 /* TT_SERVICES_GET dereferences `library' in PIC mode */ 569 #ifdef FT_CONFIG_OPTION_PIC 570 if ( !driver ) 571 return NULL; 572 library = driver->library; 573 if ( !library ) 574 return NULL; 575 #endif 576 577 result = ft_service_list_lookup( TT_SERVICES_GET, tt_interface ); 578 if ( result ) 579 return result; 580 581 #ifndef FT_CONFIG_OPTION_PIC 582 if ( !driver ) 583 return NULL; 584 library = driver->library; 585 if ( !library ) 586 return NULL; 587 #endif 588 589 /* only return the default interface from the SFNT module */ 590 sfntd = FT_Get_Module( library, "sfnt" ); 591 if ( sfntd ) 592 { 593 sfnt = (SFNT_Service)( sfntd->clazz->module_interface ); 594 if ( sfnt ) 595 return sfnt->get_interface( driver, tt_interface ); 596 } 597 598 return 0; 599 } 600 601 602 /* The FT_DriverInterface structure is defined in ftdriver.h. */ 603 604 #ifdef TT_USE_BYTECODE_INTERPRETER 605 #define TT_HINTER_FLAG FT_MODULE_DRIVER_HAS_HINTER 606 #else 607 #define TT_HINTER_FLAG 0 608 #endif 609 610 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 611 #define TT_SIZE_SELECT tt_size_select 612 #else 613 #define TT_SIZE_SELECT 0 614 #endif 615 616 FT_DEFINE_DRIVER( 617 tt_driver_class, 618 619 FT_MODULE_FONT_DRIVER | 620 FT_MODULE_DRIVER_SCALABLE | 621 TT_HINTER_FLAG, 622 623 sizeof ( TT_DriverRec ), 624 625 "truetype", /* driver name */ 626 0x10000L, /* driver version == 1.0 */ 627 0x20000L, /* driver requires FreeType 2.0 or above */ 628 629 NULL, /* module-specific interface */ 630 631 tt_driver_init, /* FT_Module_Constructor module_init */ 632 tt_driver_done, /* FT_Module_Destructor module_done */ 633 tt_get_interface, /* FT_Module_Requester get_interface */ 634 635 sizeof ( TT_FaceRec ), 636 sizeof ( TT_SizeRec ), 637 sizeof ( FT_GlyphSlotRec ), 638 639 tt_face_init, /* FT_Face_InitFunc init_face */ 640 tt_face_done, /* FT_Face_DoneFunc done_face */ 641 tt_size_init, /* FT_Size_InitFunc init_size */ 642 tt_size_done, /* FT_Size_DoneFunc done_size */ 643 tt_slot_init, /* FT_Slot_InitFunc init_slot */ 644 NULL, /* FT_Slot_DoneFunc done_slot */ 645 646 tt_glyph_load, /* FT_Slot_LoadFunc load_glyph */ 647 648 tt_get_kerning, /* FT_Face_GetKerningFunc get_kerning */ 649 NULL, /* FT_Face_AttachFunc attach_file */ 650 tt_get_advances, /* FT_Face_GetAdvancesFunc get_advances */ 651 652 tt_size_request, /* FT_Size_RequestFunc request_size */ 653 TT_SIZE_SELECT /* FT_Size_SelectFunc select_size */ 654 ) 655 656 657 /* END */ 658