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