1 /**************************************************************************** 2 * 3 * cffobjs.c 4 * 5 * OpenType objects manager (body). 6 * 7 * Copyright (C) 1996-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 20 #include <freetype/internal/ftdebug.h> 21 #include <freetype/internal/ftcalc.h> 22 #include <freetype/internal/ftstream.h> 23 #include <freetype/fterrors.h> 24 #include <freetype/ttnameid.h> 25 #include <freetype/tttags.h> 26 #include <freetype/internal/sfnt.h> 27 #include <freetype/ftdriver.h> 28 29 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 30 #include <freetype/ftmm.h> 31 #include <freetype/internal/services/svmm.h> 32 #include <freetype/internal/services/svmetric.h> 33 #endif 34 35 #include <freetype/internal/cffotypes.h> 36 #include "cffobjs.h" 37 #include "cffload.h" 38 #include "cffcmap.h" 39 40 #include "cfferrs.h" 41 42 #include <freetype/internal/psaux.h> 43 #include <freetype/internal/services/svcfftl.h> 44 45 46 /************************************************************************** 47 * 48 * The macro FT_COMPONENT is used in trace mode. It is an implicit 49 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log 50 * messages during execution. 51 */ 52 #undef FT_COMPONENT 53 #define FT_COMPONENT cffobjs 54 55 56 /************************************************************************** 57 * 58 * SIZE FUNCTIONS 59 * 60 */ 61 62 63 static PSH_Globals_Funcs cff_size_get_globals_funcs(CFF_Size size)64 cff_size_get_globals_funcs( CFF_Size size ) 65 { 66 CFF_Face face = (CFF_Face)size->root.face; 67 CFF_Font font = (CFF_Font)face->extra.data; 68 PSHinter_Service pshinter = font->pshinter; 69 FT_Module module; 70 71 72 module = FT_Get_Module( size->root.face->driver->root.library, 73 "pshinter" ); 74 return ( module && pshinter && pshinter->get_globals_funcs ) 75 ? pshinter->get_globals_funcs( module ) 76 : 0; 77 } 78 79 80 FT_LOCAL_DEF( void ) cff_size_done(FT_Size cffsize)81 cff_size_done( FT_Size cffsize ) /* CFF_Size */ 82 { 83 FT_Memory memory = cffsize->face->memory; 84 CFF_Size size = (CFF_Size)cffsize; 85 CFF_Face face = (CFF_Face)size->root.face; 86 CFF_Font font = (CFF_Font)face->extra.data; 87 CFF_Internal internal = (CFF_Internal)cffsize->internal->module_data; 88 89 90 if ( internal ) 91 { 92 PSH_Globals_Funcs funcs; 93 94 95 funcs = cff_size_get_globals_funcs( size ); 96 if ( funcs ) 97 { 98 FT_UInt i; 99 100 101 funcs->destroy( internal->topfont ); 102 103 for ( i = font->num_subfonts; i > 0; i-- ) 104 funcs->destroy( internal->subfonts[i - 1] ); 105 } 106 107 FT_FREE( internal ); 108 } 109 } 110 111 112 /* CFF and Type 1 private dictionaries have slightly different */ 113 /* structures; we need to synthesize a Type 1 dictionary on the fly */ 114 115 static void cff_make_private_dict(CFF_SubFont subfont,PS_Private priv)116 cff_make_private_dict( CFF_SubFont subfont, 117 PS_Private priv ) 118 { 119 CFF_Private cpriv = &subfont->private_dict; 120 FT_UInt n, count; 121 122 123 FT_ZERO( priv ); 124 125 count = priv->num_blue_values = cpriv->num_blue_values; 126 for ( n = 0; n < count; n++ ) 127 priv->blue_values[n] = (FT_Short)cpriv->blue_values[n]; 128 129 count = priv->num_other_blues = cpriv->num_other_blues; 130 for ( n = 0; n < count; n++ ) 131 priv->other_blues[n] = (FT_Short)cpriv->other_blues[n]; 132 133 count = priv->num_family_blues = cpriv->num_family_blues; 134 for ( n = 0; n < count; n++ ) 135 priv->family_blues[n] = (FT_Short)cpriv->family_blues[n]; 136 137 count = priv->num_family_other_blues = cpriv->num_family_other_blues; 138 for ( n = 0; n < count; n++ ) 139 priv->family_other_blues[n] = (FT_Short)cpriv->family_other_blues[n]; 140 141 priv->blue_scale = cpriv->blue_scale; 142 priv->blue_shift = (FT_Int)cpriv->blue_shift; 143 priv->blue_fuzz = (FT_Int)cpriv->blue_fuzz; 144 145 priv->standard_width[0] = (FT_UShort)cpriv->standard_width; 146 priv->standard_height[0] = (FT_UShort)cpriv->standard_height; 147 148 count = priv->num_snap_widths = cpriv->num_snap_widths; 149 for ( n = 0; n < count; n++ ) 150 priv->snap_widths[n] = (FT_Short)cpriv->snap_widths[n]; 151 152 count = priv->num_snap_heights = cpriv->num_snap_heights; 153 for ( n = 0; n < count; n++ ) 154 priv->snap_heights[n] = (FT_Short)cpriv->snap_heights[n]; 155 156 priv->force_bold = cpriv->force_bold; 157 priv->language_group = cpriv->language_group; 158 priv->lenIV = cpriv->lenIV; 159 } 160 161 162 FT_LOCAL_DEF( FT_Error ) cff_size_init(FT_Size cffsize)163 cff_size_init( FT_Size cffsize ) /* CFF_Size */ 164 { 165 CFF_Size size = (CFF_Size)cffsize; 166 FT_Error error = FT_Err_Ok; 167 PSH_Globals_Funcs funcs = cff_size_get_globals_funcs( size ); 168 169 FT_Memory memory = cffsize->face->memory; 170 CFF_Internal internal = NULL; 171 CFF_Face face = (CFF_Face)cffsize->face; 172 CFF_Font font = (CFF_Font)face->extra.data; 173 174 PS_PrivateRec priv; 175 176 FT_UInt i; 177 178 if ( !funcs ) 179 goto Exit; 180 181 if ( FT_NEW( internal ) ) 182 goto Exit; 183 184 cff_make_private_dict( &font->top_font, &priv ); 185 error = funcs->create( cffsize->face->memory, &priv, 186 &internal->topfont ); 187 if ( error ) 188 goto Exit; 189 190 for ( i = font->num_subfonts; i > 0; i-- ) 191 { 192 CFF_SubFont sub = font->subfonts[i - 1]; 193 194 195 cff_make_private_dict( sub, &priv ); 196 error = funcs->create( cffsize->face->memory, &priv, 197 &internal->subfonts[i - 1] ); 198 if ( error ) 199 goto Exit; 200 } 201 202 cffsize->internal->module_data = internal; 203 204 size->strike_index = 0xFFFFFFFFUL; 205 206 Exit: 207 if ( error ) 208 { 209 if ( internal ) 210 { 211 for ( i = font->num_subfonts; i > 0; i-- ) 212 FT_FREE( internal->subfonts[i - 1] ); 213 FT_FREE( internal->topfont ); 214 } 215 216 FT_FREE( internal ); 217 } 218 219 return error; 220 } 221 222 223 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 224 225 FT_LOCAL_DEF( FT_Error ) cff_size_select(FT_Size size,FT_ULong strike_index)226 cff_size_select( FT_Size size, 227 FT_ULong strike_index ) 228 { 229 CFF_Size cffsize = (CFF_Size)size; 230 PSH_Globals_Funcs funcs; 231 232 233 cffsize->strike_index = strike_index; 234 235 FT_Select_Metrics( size->face, strike_index ); 236 237 funcs = cff_size_get_globals_funcs( cffsize ); 238 239 if ( funcs ) 240 { 241 CFF_Face face = (CFF_Face)size->face; 242 CFF_Font font = (CFF_Font)face->extra.data; 243 CFF_Internal internal = (CFF_Internal)size->internal->module_data; 244 245 FT_Long top_upm = (FT_Long)font->top_font.font_dict.units_per_em; 246 FT_UInt i; 247 248 249 funcs->set_scale( internal->topfont, 250 size->metrics.x_scale, size->metrics.y_scale, 251 0, 0 ); 252 253 for ( i = font->num_subfonts; i > 0; i-- ) 254 { 255 CFF_SubFont sub = font->subfonts[i - 1]; 256 FT_Long sub_upm = (FT_Long)sub->font_dict.units_per_em; 257 FT_Pos x_scale, y_scale; 258 259 260 if ( top_upm != sub_upm ) 261 { 262 x_scale = FT_MulDiv( size->metrics.x_scale, top_upm, sub_upm ); 263 y_scale = FT_MulDiv( size->metrics.y_scale, top_upm, sub_upm ); 264 } 265 else 266 { 267 x_scale = size->metrics.x_scale; 268 y_scale = size->metrics.y_scale; 269 } 270 271 funcs->set_scale( internal->subfonts[i - 1], 272 x_scale, y_scale, 0, 0 ); 273 } 274 } 275 276 return FT_Err_Ok; 277 } 278 279 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 280 281 282 FT_LOCAL_DEF( FT_Error ) cff_size_request(FT_Size size,FT_Size_Request req)283 cff_size_request( FT_Size size, 284 FT_Size_Request req ) 285 { 286 FT_Error error; 287 288 CFF_Size cffsize = (CFF_Size)size; 289 PSH_Globals_Funcs funcs; 290 291 292 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 293 294 if ( FT_HAS_FIXED_SIZES( size->face ) ) 295 { 296 CFF_Face cffface = (CFF_Face)size->face; 297 SFNT_Service sfnt = (SFNT_Service)cffface->sfnt; 298 FT_ULong strike_index; 299 300 301 if ( sfnt->set_sbit_strike( cffface, req, &strike_index ) ) 302 cffsize->strike_index = 0xFFFFFFFFUL; 303 else 304 return cff_size_select( size, strike_index ); 305 } 306 307 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 308 309 error = FT_Request_Metrics( size->face, req ); 310 if ( error ) 311 goto Exit; 312 313 funcs = cff_size_get_globals_funcs( cffsize ); 314 315 if ( funcs ) 316 { 317 CFF_Face cffface = (CFF_Face)size->face; 318 CFF_Font font = (CFF_Font)cffface->extra.data; 319 CFF_Internal internal = (CFF_Internal)size->internal->module_data; 320 321 FT_Long top_upm = (FT_Long)font->top_font.font_dict.units_per_em; 322 FT_UInt i; 323 324 325 funcs->set_scale( internal->topfont, 326 size->metrics.x_scale, size->metrics.y_scale, 327 0, 0 ); 328 329 for ( i = font->num_subfonts; i > 0; i-- ) 330 { 331 CFF_SubFont sub = font->subfonts[i - 1]; 332 FT_Long sub_upm = (FT_Long)sub->font_dict.units_per_em; 333 FT_Pos x_scale, y_scale; 334 335 336 if ( top_upm != sub_upm ) 337 { 338 x_scale = FT_MulDiv( size->metrics.x_scale, top_upm, sub_upm ); 339 y_scale = FT_MulDiv( size->metrics.y_scale, top_upm, sub_upm ); 340 } 341 else 342 { 343 x_scale = size->metrics.x_scale; 344 y_scale = size->metrics.y_scale; 345 } 346 347 funcs->set_scale( internal->subfonts[i - 1], 348 x_scale, y_scale, 0, 0 ); 349 } 350 } 351 352 Exit: 353 return error; 354 } 355 356 357 /************************************************************************** 358 * 359 * SLOT FUNCTIONS 360 * 361 */ 362 363 FT_LOCAL_DEF( void ) cff_slot_done(FT_GlyphSlot slot)364 cff_slot_done( FT_GlyphSlot slot ) 365 { 366 if ( slot->internal ) 367 slot->internal->glyph_hints = NULL; 368 } 369 370 371 FT_LOCAL_DEF( FT_Error ) cff_slot_init(FT_GlyphSlot slot)372 cff_slot_init( FT_GlyphSlot slot ) 373 { 374 CFF_Face face = (CFF_Face)slot->face; 375 CFF_Font font = (CFF_Font)face->extra.data; 376 PSHinter_Service pshinter = font->pshinter; 377 378 379 if ( pshinter ) 380 { 381 FT_Module module; 382 383 384 module = FT_Get_Module( slot->face->driver->root.library, 385 "pshinter" ); 386 if ( module ) 387 { 388 T2_Hints_Funcs funcs; 389 390 391 funcs = pshinter->get_t2_funcs( module ); 392 slot->internal->glyph_hints = (void*)funcs; 393 } 394 } 395 396 return FT_Err_Ok; 397 } 398 399 400 /************************************************************************** 401 * 402 * FACE FUNCTIONS 403 * 404 */ 405 406 static FT_String* cff_strcpy(FT_Memory memory,const FT_String * source)407 cff_strcpy( FT_Memory memory, 408 const FT_String* source ) 409 { 410 FT_Error error; 411 FT_String* result; 412 413 414 FT_MEM_STRDUP( result, source ); 415 416 return result; 417 } 418 419 420 /* Strip all subset prefixes of the form `ABCDEF+'. Usually, there */ 421 /* is only one, but font names like `APCOOG+JFABTD+FuturaBQ-Bold' */ 422 /* have been seen in the wild. */ 423 424 static void remove_subset_prefix(FT_String * name)425 remove_subset_prefix( FT_String* name ) 426 { 427 FT_Int32 idx = 0; 428 FT_Int32 length = (FT_Int32)ft_strlen( name ) + 1; 429 FT_Bool continue_search = 1; 430 431 432 while ( continue_search ) 433 { 434 if ( length >= 7 && name[6] == '+' ) 435 { 436 for ( idx = 0; idx < 6; idx++ ) 437 { 438 /* ASCII uppercase letters */ 439 if ( !( 'A' <= name[idx] && name[idx] <= 'Z' ) ) 440 continue_search = 0; 441 } 442 443 if ( continue_search ) 444 { 445 for ( idx = 7; idx < length; idx++ ) 446 name[idx - 7] = name[idx]; 447 length -= 7; 448 } 449 } 450 else 451 continue_search = 0; 452 } 453 } 454 455 456 /* Remove the style part from the family name (if present). */ 457 458 static void remove_style(FT_String * family_name,const FT_String * style_name)459 remove_style( FT_String* family_name, 460 const FT_String* style_name ) 461 { 462 FT_Int32 family_name_length, style_name_length; 463 464 465 family_name_length = (FT_Int32)ft_strlen( family_name ); 466 style_name_length = (FT_Int32)ft_strlen( style_name ); 467 468 if ( family_name_length > style_name_length ) 469 { 470 FT_Int idx; 471 472 473 for ( idx = 1; idx <= style_name_length; idx++ ) 474 { 475 if ( family_name[family_name_length - idx] != 476 style_name[style_name_length - idx] ) 477 break; 478 } 479 480 if ( idx > style_name_length ) 481 { 482 /* family_name ends with style_name; remove it */ 483 idx = family_name_length - style_name_length - 1; 484 485 /* also remove special characters */ 486 /* between real family name and style */ 487 while ( idx > 0 && 488 ( family_name[idx] == '-' || 489 family_name[idx] == ' ' || 490 family_name[idx] == '_' || 491 family_name[idx] == '+' ) ) 492 idx--; 493 494 if ( idx > 0 ) 495 family_name[idx + 1] = '\0'; 496 } 497 } 498 } 499 500 501 FT_LOCAL_DEF( FT_Error ) cff_face_init(FT_Stream stream,FT_Face cffface,FT_Int face_index,FT_Int num_params,FT_Parameter * params)502 cff_face_init( FT_Stream stream, 503 FT_Face cffface, /* CFF_Face */ 504 FT_Int face_index, 505 FT_Int num_params, 506 FT_Parameter* params ) 507 { 508 CFF_Face face = (CFF_Face)cffface; 509 FT_Error error; 510 SFNT_Service sfnt; 511 FT_Service_PsCMaps psnames; 512 PSHinter_Service pshinter; 513 PSAux_Service psaux; 514 FT_Service_CFFLoad cffload; 515 FT_Bool pure_cff = 1; 516 FT_Bool cff2 = 0; 517 FT_Bool sfnt_format = 0; 518 FT_Library library = cffface->driver->root.library; 519 520 521 sfnt = (SFNT_Service)FT_Get_Module_Interface( library, 522 "sfnt" ); 523 if ( !sfnt ) 524 { 525 FT_ERROR(( "cff_face_init: cannot access `sfnt' module\n" )); 526 error = FT_THROW( Missing_Module ); 527 goto Exit; 528 } 529 530 FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS ); 531 532 pshinter = (PSHinter_Service)FT_Get_Module_Interface( library, 533 "pshinter" ); 534 535 psaux = (PSAux_Service)FT_Get_Module_Interface( library, 536 "psaux" ); 537 if ( !psaux ) 538 { 539 FT_ERROR(( "cff_face_init: cannot access `psaux' module\n" )); 540 error = FT_THROW( Missing_Module ); 541 goto Exit; 542 } 543 face->psaux = psaux; 544 545 FT_FACE_FIND_GLOBAL_SERVICE( face, cffload, CFF_LOAD ); 546 547 FT_TRACE2(( "CFF driver\n" )); 548 549 /* create input stream from resource */ 550 if ( FT_STREAM_SEEK( 0 ) ) 551 goto Exit; 552 553 /* check whether we have a valid OpenType file */ 554 FT_TRACE2(( " " )); 555 error = sfnt->init_face( stream, face, face_index, num_params, params ); 556 if ( !error ) 557 { 558 if ( face->format_tag != TTAG_OTTO ) /* `OTTO'; OpenType/CFF font */ 559 { 560 FT_TRACE2(( " not an OpenType/CFF font\n" )); 561 error = FT_THROW( Unknown_File_Format ); 562 goto Exit; 563 } 564 565 /* if we are performing a simple font format check, exit immediately */ 566 if ( face_index < 0 ) 567 return FT_Err_Ok; 568 569 sfnt_format = 1; 570 571 /* now, the font can be either an OpenType/CFF font, or an SVG CEF */ 572 /* font; in the latter case it doesn't have a `head' table */ 573 error = face->goto_table( face, TTAG_head, stream, 0 ); 574 if ( !error ) 575 { 576 pure_cff = 0; 577 578 /* load font directory */ 579 error = sfnt->load_face( stream, face, face_index, 580 num_params, params ); 581 if ( error ) 582 goto Exit; 583 } 584 else 585 { 586 /* load the `cmap' table explicitly */ 587 error = sfnt->load_cmap( face, stream ); 588 if ( error ) 589 goto Exit; 590 } 591 592 /* now load the CFF part of the file; */ 593 /* give priority to CFF2 */ 594 error = face->goto_table( face, TTAG_CFF2, stream, 0 ); 595 if ( !error ) 596 { 597 cff2 = 1; 598 face->is_cff2 = cff2; 599 } 600 601 if ( FT_ERR_EQ( error, Table_Missing ) ) 602 error = face->goto_table( face, TTAG_CFF, stream, 0 ); 603 604 if ( error ) 605 goto Exit; 606 } 607 else 608 { 609 /* rewind to start of file; we are going to load a pure-CFF font */ 610 if ( FT_STREAM_SEEK( 0 ) ) 611 goto Exit; 612 error = FT_Err_Ok; 613 } 614 615 /* now load and parse the CFF table in the file */ 616 { 617 CFF_Font cff = NULL; 618 CFF_FontRecDict dict; 619 FT_Memory memory = cffface->memory; 620 FT_Int32 flags; 621 FT_UInt i; 622 623 624 if ( FT_NEW( cff ) ) 625 goto Exit; 626 627 face->extra.data = cff; 628 error = cff_font_load( library, 629 stream, 630 face_index, 631 cff, 632 face, 633 pure_cff, 634 cff2 ); 635 if ( error ) 636 goto Exit; 637 638 /* if we are performing a simple font format check, exit immediately */ 639 /* (this is here for pure CFF) */ 640 if ( face_index < 0 ) 641 { 642 cffface->num_faces = (FT_Long)cff->num_faces; 643 return FT_Err_Ok; 644 } 645 646 cff->pshinter = pshinter; 647 cff->psnames = psnames; 648 cff->cffload = cffload; 649 650 cffface->face_index = face_index & 0xFFFF; 651 652 /* Complement the root flags with some interesting information. */ 653 /* Note that this is only necessary for pure CFF and CEF fonts; */ 654 /* SFNT based fonts use the `name' table instead. */ 655 656 cffface->num_glyphs = (FT_Long)cff->num_glyphs; 657 658 dict = &cff->top_font.font_dict; 659 660 /* we need the `psnames' module for CFF and CEF formats */ 661 /* which aren't CID-keyed */ 662 if ( dict->cid_registry == 0xFFFFU && !psnames ) 663 { 664 FT_ERROR(( "cff_face_init:" 665 " cannot open CFF & CEF fonts\n" )); 666 FT_ERROR(( " " 667 " without the `psnames' module\n" )); 668 error = FT_THROW( Missing_Module ); 669 goto Exit; 670 } 671 672 #ifdef FT_DEBUG_LEVEL_TRACE 673 { 674 FT_UInt idx; 675 FT_String* s; 676 677 678 FT_TRACE4(( "SIDs\n" )); 679 680 /* dump string index, including default strings for convenience */ 681 for ( idx = 0; idx <= 390; idx++ ) 682 { 683 s = cff_index_get_sid_string( cff, idx ); 684 if ( s ) 685 FT_TRACE4(( " %5d %s\n", idx, s )); 686 } 687 688 /* In Multiple Master CFFs, two SIDs hold the Normalize Design */ 689 /* Vector (NDV) and Convert Design Vector (CDV) charstrings, */ 690 /* which may contain null bytes in the middle of the data, too. */ 691 /* We thus access `cff->strings' directly. */ 692 for ( idx = 1; idx < cff->num_strings; idx++ ) 693 { 694 FT_Byte* s1 = cff->strings[idx - 1]; 695 FT_Byte* s2 = cff->strings[idx]; 696 FT_PtrDist s1len = s2 - s1 - 1; /* without the final null byte */ 697 FT_PtrDist l; 698 699 700 FT_TRACE4(( " %5d ", idx + 390 )); 701 for ( l = 0; l < s1len; l++ ) 702 FT_TRACE4(( "%c", s1[l] )); 703 FT_TRACE4(( "\n" )); 704 } 705 706 /* print last element */ 707 if ( cff->num_strings ) 708 { 709 FT_Byte* s1 = cff->strings[cff->num_strings - 1]; 710 FT_Byte* s2 = cff->string_pool + cff->string_pool_size; 711 FT_PtrDist s1len = s2 - s1 - 1; 712 FT_PtrDist l; 713 714 715 FT_TRACE4(( " %5d ", cff->num_strings + 390 )); 716 for ( l = 0; l < s1len; l++ ) 717 FT_TRACE4(( "%c", s1[l] )); 718 FT_TRACE4(( "\n" )); 719 } 720 } 721 #endif /* FT_DEBUG_LEVEL_TRACE */ 722 723 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 724 { 725 FT_Service_MultiMasters 726 mm = (FT_Service_MultiMasters)face->mm; 727 FT_Service_MetricsVariations 728 var = (FT_Service_MetricsVariations)face->face_var; 729 730 FT_UInt instance_index = (FT_UInt)face_index >> 16; 731 732 733 if ( FT_HAS_MULTIPLE_MASTERS( cffface ) && 734 mm && 735 instance_index > 0 ) 736 { 737 error = mm->set_instance( cffface, instance_index ); 738 if ( error ) 739 goto Exit; 740 741 if ( var ) 742 var->metrics_adjust( cffface ); 743 } 744 } 745 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 746 747 if ( !dict->has_font_matrix ) 748 dict->units_per_em = pure_cff ? 1000 : face->root.units_per_EM; 749 750 /* Normalize the font matrix so that `matrix->yy' is 1; if */ 751 /* it is zero, we use `matrix->yx' instead. The scaling is */ 752 /* done with `units_per_em' then (at this point, it already */ 753 /* contains the scaling factor, but without normalization */ 754 /* of the matrix). */ 755 /* */ 756 /* Note that the offsets must be expressed in integer font */ 757 /* units. */ 758 759 { 760 FT_Matrix* matrix = &dict->font_matrix; 761 FT_Vector* offset = &dict->font_offset; 762 FT_ULong* upm = &dict->units_per_em; 763 FT_Fixed temp; 764 765 766 temp = matrix->yy ? FT_ABS( matrix->yy ) 767 : FT_ABS( matrix->yx ); 768 769 if ( temp != 0x10000L ) 770 { 771 *upm = (FT_ULong)FT_DivFix( (FT_Long)*upm, temp ); 772 773 matrix->xx = FT_DivFix( matrix->xx, temp ); 774 matrix->yx = FT_DivFix( matrix->yx, temp ); 775 matrix->xy = FT_DivFix( matrix->xy, temp ); 776 matrix->yy = FT_DivFix( matrix->yy, temp ); 777 offset->x = FT_DivFix( offset->x, temp ); 778 offset->y = FT_DivFix( offset->y, temp ); 779 } 780 781 offset->x >>= 16; 782 offset->y >>= 16; 783 } 784 785 for ( i = cff->num_subfonts; i > 0; i-- ) 786 { 787 CFF_FontRecDict sub = &cff->subfonts[i - 1]->font_dict; 788 CFF_FontRecDict top = &cff->top_font.font_dict; 789 790 FT_Matrix* matrix; 791 FT_Vector* offset; 792 FT_ULong* upm; 793 FT_Fixed temp; 794 795 796 if ( sub->has_font_matrix ) 797 { 798 FT_Long scaling; 799 800 801 /* if we have a top-level matrix, */ 802 /* concatenate the subfont matrix */ 803 804 if ( top->has_font_matrix ) 805 { 806 if ( top->units_per_em > 1 && sub->units_per_em > 1 ) 807 scaling = (FT_Long)FT_MIN( top->units_per_em, 808 sub->units_per_em ); 809 else 810 scaling = 1; 811 812 FT_Matrix_Multiply_Scaled( &top->font_matrix, 813 &sub->font_matrix, 814 scaling ); 815 FT_Vector_Transform_Scaled( &sub->font_offset, 816 &top->font_matrix, 817 scaling ); 818 819 sub->units_per_em = (FT_ULong) 820 FT_MulDiv( (FT_Long)sub->units_per_em, 821 (FT_Long)top->units_per_em, 822 scaling ); 823 } 824 } 825 else 826 { 827 sub->font_matrix = top->font_matrix; 828 sub->font_offset = top->font_offset; 829 830 sub->units_per_em = top->units_per_em; 831 } 832 833 matrix = &sub->font_matrix; 834 offset = &sub->font_offset; 835 upm = &sub->units_per_em; 836 837 temp = matrix->yy ? FT_ABS( matrix->yy ) 838 : FT_ABS( matrix->yx ); 839 840 841 if ( temp != 0x10000L ) 842 { 843 *upm = (FT_ULong)FT_DivFix( (FT_Long)*upm, temp ); 844 845 matrix->xx = FT_DivFix( matrix->xx, temp ); 846 matrix->yx = FT_DivFix( matrix->yx, temp ); 847 matrix->xy = FT_DivFix( matrix->xy, temp ); 848 matrix->yy = FT_DivFix( matrix->yy, temp ); 849 offset->x = FT_DivFix( offset->x, temp ); 850 offset->y = FT_DivFix( offset->y, temp ); 851 } 852 853 offset->x >>= 16; 854 offset->y >>= 16; 855 } 856 857 if ( pure_cff ) 858 { 859 char* style_name = NULL; 860 861 862 /* set up num_faces */ 863 cffface->num_faces = (FT_Long)cff->num_faces; 864 865 /* compute number of glyphs */ 866 if ( dict->cid_registry != 0xFFFFU ) 867 cffface->num_glyphs = (FT_Long)( cff->charset.max_cid + 1 ); 868 else 869 cffface->num_glyphs = (FT_Long)cff->charstrings_index.count; 870 871 /* set global bbox, as well as EM size */ 872 cffface->bbox.xMin = dict->font_bbox.xMin >> 16; 873 cffface->bbox.yMin = dict->font_bbox.yMin >> 16; 874 /* no `U' suffix here to 0xFFFF! */ 875 cffface->bbox.xMax = ( dict->font_bbox.xMax + 0xFFFF ) >> 16; 876 cffface->bbox.yMax = ( dict->font_bbox.yMax + 0xFFFF ) >> 16; 877 878 cffface->units_per_EM = (FT_UShort)( dict->units_per_em ); 879 880 cffface->ascender = (FT_Short)( cffface->bbox.yMax ); 881 cffface->descender = (FT_Short)( cffface->bbox.yMin ); 882 883 cffface->height = (FT_Short)( ( cffface->units_per_EM * 12 ) / 10 ); 884 if ( cffface->height < cffface->ascender - cffface->descender ) 885 cffface->height = (FT_Short)( cffface->ascender - 886 cffface->descender ); 887 888 cffface->underline_position = 889 (FT_Short)( dict->underline_position >> 16 ); 890 cffface->underline_thickness = 891 (FT_Short)( dict->underline_thickness >> 16 ); 892 893 /* retrieve font family & style name */ 894 if ( dict->family_name ) 895 { 896 char* family_name; 897 898 899 family_name = cff_index_get_sid_string( cff, dict->family_name ); 900 if ( family_name ) 901 cffface->family_name = cff_strcpy( memory, family_name ); 902 } 903 904 if ( !cffface->family_name ) 905 { 906 cffface->family_name = cff_index_get_name( 907 cff, 908 (FT_UInt)( face_index & 0xFFFF ) ); 909 if ( cffface->family_name ) 910 remove_subset_prefix( cffface->family_name ); 911 } 912 913 if ( cffface->family_name ) 914 { 915 char* full = cff_index_get_sid_string( cff, 916 dict->full_name ); 917 char* fullp = full; 918 char* family = cffface->family_name; 919 920 921 /* We try to extract the style name from the full name. */ 922 /* We need to ignore spaces and dashes during the search. */ 923 if ( full && family ) 924 { 925 while ( *fullp ) 926 { 927 /* skip common characters at the start of both strings */ 928 if ( *fullp == *family ) 929 { 930 family++; 931 fullp++; 932 continue; 933 } 934 935 /* ignore spaces and dashes in full name during comparison */ 936 if ( *fullp == ' ' || *fullp == '-' ) 937 { 938 fullp++; 939 continue; 940 } 941 942 /* ignore spaces and dashes in family name during comparison */ 943 if ( *family == ' ' || *family == '-' ) 944 { 945 family++; 946 continue; 947 } 948 949 if ( !*family && *fullp ) 950 { 951 /* The full name begins with the same characters as the */ 952 /* family name, with spaces and dashes removed. In this */ 953 /* case, the remaining string in `fullp' will be used as */ 954 /* the style name. */ 955 style_name = cff_strcpy( memory, fullp ); 956 957 /* remove the style part from the family name (if present) */ 958 if ( style_name ) 959 remove_style( cffface->family_name, style_name ); 960 } 961 break; 962 } 963 } 964 } 965 else 966 { 967 char *cid_font_name = 968 cff_index_get_sid_string( cff, 969 dict->cid_font_name ); 970 971 972 /* do we have a `/FontName' for a CID-keyed font? */ 973 if ( cid_font_name ) 974 cffface->family_name = cff_strcpy( memory, cid_font_name ); 975 } 976 977 if ( style_name ) 978 cffface->style_name = style_name; 979 else 980 /* assume "Regular" style if we don't know better */ 981 cffface->style_name = cff_strcpy( memory, "Regular" ); 982 983 /******************************************************************** 984 * 985 * Compute face flags. 986 */ 987 flags = FT_FACE_FLAG_SCALABLE | /* scalable outlines */ 988 FT_FACE_FLAG_HORIZONTAL | /* horizontal data */ 989 FT_FACE_FLAG_HINTER; /* has native hinter */ 990 991 if ( sfnt_format ) 992 flags |= FT_FACE_FLAG_SFNT; 993 994 /* fixed width font? */ 995 if ( dict->is_fixed_pitch ) 996 flags |= FT_FACE_FLAG_FIXED_WIDTH; 997 998 /* XXX: WE DO NOT SUPPORT KERNING METRICS IN THE GPOS TABLE FOR NOW */ 999 #if 0 1000 /* kerning available? */ 1001 if ( face->kern_pairs ) 1002 flags |= FT_FACE_FLAG_KERNING; 1003 #endif 1004 1005 cffface->face_flags |= flags; 1006 1007 /******************************************************************** 1008 * 1009 * Compute style flags. 1010 */ 1011 flags = 0; 1012 1013 if ( dict->italic_angle ) 1014 flags |= FT_STYLE_FLAG_ITALIC; 1015 1016 { 1017 char *weight = cff_index_get_sid_string( cff, 1018 dict->weight ); 1019 1020 1021 if ( weight ) 1022 if ( !ft_strcmp( weight, "Bold" ) || 1023 !ft_strcmp( weight, "Black" ) ) 1024 flags |= FT_STYLE_FLAG_BOLD; 1025 } 1026 1027 /* double check */ 1028 if ( !(flags & FT_STYLE_FLAG_BOLD) && cffface->style_name ) 1029 if ( !ft_strncmp( cffface->style_name, "Bold", 4 ) || 1030 !ft_strncmp( cffface->style_name, "Black", 5 ) ) 1031 flags |= FT_STYLE_FLAG_BOLD; 1032 1033 cffface->style_flags = flags; 1034 } 1035 1036 /* CID-keyed CFF or CFF2 fonts don't have glyph names -- the SFNT */ 1037 /* loader has unset this flag because of the 3.0 `post' table. */ 1038 if ( dict->cid_registry == 0xFFFFU && !cff2 ) 1039 cffface->face_flags |= FT_FACE_FLAG_GLYPH_NAMES; 1040 1041 if ( dict->cid_registry != 0xFFFFU && pure_cff ) 1042 cffface->face_flags |= FT_FACE_FLAG_CID_KEYED; 1043 1044 /******************************************************************** 1045 * 1046 * Compute char maps. 1047 */ 1048 1049 /* Try to synthesize a Unicode charmap if there is none available */ 1050 /* already. If an OpenType font contains a Unicode "cmap", we */ 1051 /* will use it, whatever be in the CFF part of the file. */ 1052 { 1053 FT_CharMapRec cmaprec; 1054 FT_CharMap cmap; 1055 FT_Int nn; 1056 CFF_Encoding encoding = &cff->encoding; 1057 1058 1059 for ( nn = 0; nn < cffface->num_charmaps; nn++ ) 1060 { 1061 cmap = cffface->charmaps[nn]; 1062 1063 /* Windows Unicode? */ 1064 if ( cmap->platform_id == TT_PLATFORM_MICROSOFT && 1065 cmap->encoding_id == TT_MS_ID_UNICODE_CS ) 1066 goto Skip_Unicode; 1067 1068 /* Apple Unicode platform id? */ 1069 if ( cmap->platform_id == TT_PLATFORM_APPLE_UNICODE ) 1070 goto Skip_Unicode; /* Apple Unicode */ 1071 } 1072 1073 /* since CID-keyed fonts don't contain glyph names, we can't */ 1074 /* construct a cmap */ 1075 if ( pure_cff && cff->top_font.font_dict.cid_registry != 0xFFFFU ) 1076 goto Exit; 1077 1078 /* we didn't find a Unicode charmap -- synthesize one */ 1079 cmaprec.face = cffface; 1080 cmaprec.platform_id = TT_PLATFORM_MICROSOFT; 1081 cmaprec.encoding_id = TT_MS_ID_UNICODE_CS; 1082 cmaprec.encoding = FT_ENCODING_UNICODE; 1083 1084 nn = cffface->num_charmaps; 1085 1086 error = FT_CMap_New( &cff_cmap_unicode_class_rec, NULL, 1087 &cmaprec, NULL ); 1088 if ( error && 1089 FT_ERR_NEQ( error, No_Unicode_Glyph_Name ) && 1090 FT_ERR_NEQ( error, Unimplemented_Feature ) ) 1091 goto Exit; 1092 error = FT_Err_Ok; 1093 1094 /* if no Unicode charmap was previously selected, select this one */ 1095 if ( !cffface->charmap && nn != cffface->num_charmaps ) 1096 cffface->charmap = cffface->charmaps[nn]; 1097 1098 Skip_Unicode: 1099 if ( encoding->count > 0 ) 1100 { 1101 FT_CMap_Class clazz; 1102 1103 1104 cmaprec.face = cffface; 1105 cmaprec.platform_id = TT_PLATFORM_ADOBE; /* Adobe platform id */ 1106 1107 if ( encoding->offset == 0 ) 1108 { 1109 cmaprec.encoding_id = TT_ADOBE_ID_STANDARD; 1110 cmaprec.encoding = FT_ENCODING_ADOBE_STANDARD; 1111 clazz = &cff_cmap_encoding_class_rec; 1112 } 1113 else if ( encoding->offset == 1 ) 1114 { 1115 cmaprec.encoding_id = TT_ADOBE_ID_EXPERT; 1116 cmaprec.encoding = FT_ENCODING_ADOBE_EXPERT; 1117 clazz = &cff_cmap_encoding_class_rec; 1118 } 1119 else 1120 { 1121 cmaprec.encoding_id = TT_ADOBE_ID_CUSTOM; 1122 cmaprec.encoding = FT_ENCODING_ADOBE_CUSTOM; 1123 clazz = &cff_cmap_encoding_class_rec; 1124 } 1125 1126 error = FT_CMap_New( clazz, NULL, &cmaprec, NULL ); 1127 } 1128 } 1129 } 1130 1131 Exit: 1132 return error; 1133 } 1134 1135 1136 FT_LOCAL_DEF( void ) cff_face_done(FT_Face cffface)1137 cff_face_done( FT_Face cffface ) /* CFF_Face */ 1138 { 1139 CFF_Face face = (CFF_Face)cffface; 1140 FT_Memory memory; 1141 SFNT_Service sfnt; 1142 1143 1144 if ( !face ) 1145 return; 1146 1147 memory = cffface->memory; 1148 sfnt = (SFNT_Service)face->sfnt; 1149 1150 if ( sfnt ) 1151 sfnt->done_face( face ); 1152 1153 { 1154 CFF_Font cff = (CFF_Font)face->extra.data; 1155 1156 1157 if ( cff ) 1158 { 1159 cff_font_done( cff ); 1160 FT_FREE( face->extra.data ); 1161 } 1162 } 1163 1164 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1165 cff_done_blend( face ); 1166 face->blend = NULL; 1167 #endif 1168 } 1169 1170 1171 FT_LOCAL_DEF( FT_Error ) cff_driver_init(FT_Module module)1172 cff_driver_init( FT_Module module ) /* CFF_Driver */ 1173 { 1174 PS_Driver driver = (PS_Driver)module; 1175 1176 FT_UInt32 seed; 1177 1178 1179 /* set default property values, cf. `ftcffdrv.h' */ 1180 driver->hinting_engine = FT_HINTING_ADOBE; 1181 1182 driver->no_stem_darkening = TRUE; 1183 1184 driver->darken_params[0] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X1; 1185 driver->darken_params[1] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y1; 1186 driver->darken_params[2] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2; 1187 driver->darken_params[3] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y2; 1188 driver->darken_params[4] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3; 1189 driver->darken_params[5] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y3; 1190 driver->darken_params[6] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4; 1191 driver->darken_params[7] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4; 1192 1193 /* compute random seed from some memory addresses */ 1194 seed = (FT_UInt32)( (FT_Offset)(char*)&seed ^ 1195 (FT_Offset)(char*)&module ^ 1196 (FT_Offset)(char*)module->memory ); 1197 seed = seed ^ ( seed >> 10 ) ^ ( seed >> 20 ); 1198 1199 driver->random_seed = (FT_Int32)seed; 1200 if ( driver->random_seed < 0 ) 1201 driver->random_seed = -driver->random_seed; 1202 else if ( driver->random_seed == 0 ) 1203 driver->random_seed = 123456789; 1204 1205 return FT_Err_Ok; 1206 } 1207 1208 1209 FT_LOCAL_DEF( void ) cff_driver_done(FT_Module module)1210 cff_driver_done( FT_Module module ) /* CFF_Driver */ 1211 { 1212 FT_UNUSED( module ); 1213 } 1214 1215 1216 /* END */ 1217