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