1 /***************************************************************************/ 2 /* */ 3 /* t1gload.c */ 4 /* */ 5 /* Type 1 Glyph Loader (body). */ 6 /* */ 7 /* Copyright 1996-2018 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 "t1gload.h" 21 #include FT_INTERNAL_CALC_H 22 #include FT_INTERNAL_DEBUG_H 23 #include FT_INTERNAL_STREAM_H 24 #include FT_OUTLINE_H 25 #include FT_INTERNAL_POSTSCRIPT_AUX_H 26 #include FT_INTERNAL_CFF_TYPES_H 27 #include FT_DRIVER_H 28 29 #include "t1errors.h" 30 31 32 /*************************************************************************/ 33 /* */ 34 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 35 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 36 /* messages during execution. */ 37 /* */ 38 #undef FT_COMPONENT 39 #define FT_COMPONENT trace_t1gload 40 41 42 static FT_Error 43 T1_Parse_Glyph_And_Get_Char_String( T1_Decoder decoder, 44 FT_UInt glyph_index, 45 FT_Data* char_string, 46 FT_Bool* force_scaling ) 47 { 48 T1_Face face = (T1_Face)decoder->builder.face; 49 T1_Font type1 = &face->type1; 50 FT_Error error = FT_Err_Ok; 51 52 PSAux_Service psaux = (PSAux_Service)face->psaux; 53 const T1_Decoder_Funcs decoder_funcs = psaux->t1_decoder_funcs; 54 PS_Decoder psdecoder; 55 56 #ifdef FT_CONFIG_OPTION_INCREMENTAL 57 FT_Incremental_InterfaceRec *inc = 58 face->root.internal->incremental_interface; 59 #endif 60 61 #ifdef T1_CONFIG_OPTION_OLD_ENGINE 62 PS_Driver driver = (PS_Driver)FT_FACE_DRIVER( face ); 63 #endif 64 65 decoder->font_matrix = type1->font_matrix; 66 decoder->font_offset = type1->font_offset; 67 68 #ifdef FT_CONFIG_OPTION_INCREMENTAL 69 70 /* For incremental fonts get the character data using the */ 71 /* callback function. */ 72 if ( inc ) 73 error = inc->funcs->get_glyph_data( inc->object, 74 glyph_index, char_string ); 75 else 76 77 #endif /* FT_CONFIG_OPTION_INCREMENTAL */ 78 79 /* For ordinary fonts get the character data stored in the face record. */ 80 { 81 char_string->pointer = type1->charstrings[glyph_index]; 82 char_string->length = (FT_Int)type1->charstrings_len[glyph_index]; 83 } 84 85 if ( !error ) 86 { 87 /* choose which renderer to use */ 88 #ifdef T1_CONFIG_OPTION_OLD_ENGINE 89 if ( driver->hinting_engine == FT_HINTING_FREETYPE || 90 decoder->builder.metrics_only ) 91 error = decoder_funcs->parse_charstrings_old( 92 decoder, 93 (FT_Byte*)char_string->pointer, 94 (FT_UInt)char_string->length ); 95 #else 96 if ( decoder->builder.metrics_only ) 97 error = decoder_funcs->parse_metrics( 98 decoder, 99 (FT_Byte*)char_string->pointer, 100 (FT_UInt)char_string->length ); 101 #endif 102 else 103 { 104 CFF_SubFontRec subfont; 105 106 107 psaux->ps_decoder_init( &psdecoder, decoder, TRUE ); 108 109 psaux->t1_make_subfont( FT_FACE( face ), 110 &face->type1.private_dict, &subfont ); 111 psdecoder.current_subfont = &subfont; 112 113 error = decoder_funcs->parse_charstrings( 114 &psdecoder, 115 (FT_Byte*)char_string->pointer, 116 (FT_ULong)char_string->length ); 117 118 /* Adobe's engine uses 16.16 numbers everywhere; */ 119 /* as a consequence, glyphs larger than 2000ppem get rejected */ 120 if ( FT_ERR_EQ( error, Glyph_Too_Big ) ) 121 { 122 /* this time, we retry unhinted and scale up the glyph later on */ 123 /* (the engine uses and sets the hardcoded value 0x10000 / 64 = */ 124 /* 0x400 for both `x_scale' and `y_scale' in this case) */ 125 ((T1_GlyphSlot)decoder->builder.glyph)->hint = FALSE; 126 127 *force_scaling = TRUE; 128 129 error = decoder_funcs->parse_charstrings( 130 &psdecoder, 131 (FT_Byte*)char_string->pointer, 132 (FT_ULong)char_string->length ); 133 } 134 } 135 } 136 137 #ifdef FT_CONFIG_OPTION_INCREMENTAL 138 139 /* Incremental fonts can optionally override the metrics. */ 140 if ( !error && inc && inc->funcs->get_glyph_metrics ) 141 { 142 FT_Incremental_MetricsRec metrics; 143 144 145 metrics.bearing_x = FIXED_TO_INT( decoder->builder.left_bearing.x ); 146 metrics.bearing_y = 0; 147 metrics.advance = FIXED_TO_INT( decoder->builder.advance.x ); 148 metrics.advance_v = FIXED_TO_INT( decoder->builder.advance.y ); 149 150 error = inc->funcs->get_glyph_metrics( inc->object, 151 glyph_index, FALSE, &metrics ); 152 153 decoder->builder.left_bearing.x = INT_TO_FIXED( metrics.bearing_x ); 154 decoder->builder.advance.x = INT_TO_FIXED( metrics.advance ); 155 decoder->builder.advance.y = INT_TO_FIXED( metrics.advance_v ); 156 } 157 158 #endif /* FT_CONFIG_OPTION_INCREMENTAL */ 159 160 return error; 161 } 162 163 164 FT_CALLBACK_DEF( FT_Error ) 165 T1_Parse_Glyph( T1_Decoder decoder, 166 FT_UInt glyph_index ) 167 { 168 FT_Data glyph_data; 169 FT_Bool force_scaling = FALSE; 170 FT_Error error = T1_Parse_Glyph_And_Get_Char_String( 171 decoder, glyph_index, &glyph_data, 172 &force_scaling ); 173 174 175 #ifdef FT_CONFIG_OPTION_INCREMENTAL 176 177 if ( !error ) 178 { 179 T1_Face face = (T1_Face)decoder->builder.face; 180 181 182 if ( face->root.internal->incremental_interface ) 183 face->root.internal->incremental_interface->funcs->free_glyph_data( 184 face->root.internal->incremental_interface->object, 185 &glyph_data ); 186 } 187 188 #endif /* FT_CONFIG_OPTION_INCREMENTAL */ 189 190 return error; 191 } 192 193 194 /*************************************************************************/ 195 /*************************************************************************/ 196 /*************************************************************************/ 197 /********** *********/ 198 /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/ 199 /********** *********/ 200 /********** The following code is in charge of computing *********/ 201 /********** the maximum advance width of the font. It *********/ 202 /********** quickly processes each glyph charstring to *********/ 203 /********** extract the value from either a `sbw' or `seac' *********/ 204 /********** operator. *********/ 205 /********** *********/ 206 /*************************************************************************/ 207 /*************************************************************************/ 208 /*************************************************************************/ 209 210 211 FT_LOCAL_DEF( FT_Error ) 212 T1_Compute_Max_Advance( T1_Face face, 213 FT_Pos* max_advance ) 214 { 215 FT_Error error; 216 T1_DecoderRec decoder; 217 FT_Int glyph_index; 218 T1_Font type1 = &face->type1; 219 PSAux_Service psaux = (PSAux_Service)face->psaux; 220 221 222 FT_ASSERT( ( face->len_buildchar == 0 ) == ( face->buildchar == NULL ) ); 223 224 *max_advance = 0; 225 226 /* initialize load decoder */ 227 error = psaux->t1_decoder_funcs->init( &decoder, 228 (FT_Face)face, 229 0, /* size */ 230 0, /* glyph slot */ 231 (FT_Byte**)type1->glyph_names, 232 face->blend, 233 0, 234 FT_RENDER_MODE_NORMAL, 235 T1_Parse_Glyph ); 236 if ( error ) 237 return error; 238 239 decoder.builder.metrics_only = 1; 240 decoder.builder.load_points = 0; 241 242 decoder.num_subrs = type1->num_subrs; 243 decoder.subrs = type1->subrs; 244 decoder.subrs_len = type1->subrs_len; 245 decoder.subrs_hash = type1->subrs_hash; 246 247 decoder.buildchar = face->buildchar; 248 decoder.len_buildchar = face->len_buildchar; 249 250 *max_advance = 0; 251 252 /* for each glyph, parse the glyph charstring and extract */ 253 /* the advance width */ 254 for ( glyph_index = 0; glyph_index < type1->num_glyphs; glyph_index++ ) 255 { 256 /* now get load the unscaled outline */ 257 (void)T1_Parse_Glyph( &decoder, (FT_UInt)glyph_index ); 258 if ( glyph_index == 0 || decoder.builder.advance.x > *max_advance ) 259 *max_advance = decoder.builder.advance.x; 260 261 /* ignore the error if one occurred - skip to next glyph */ 262 } 263 264 psaux->t1_decoder_funcs->done( &decoder ); 265 266 return FT_Err_Ok; 267 } 268 269 270 FT_LOCAL_DEF( FT_Error ) 271 T1_Get_Advances( FT_Face t1face, /* T1_Face */ 272 FT_UInt first, 273 FT_UInt count, 274 FT_Int32 load_flags, 275 FT_Fixed* advances ) 276 { 277 T1_Face face = (T1_Face)t1face; 278 T1_DecoderRec decoder; 279 T1_Font type1 = &face->type1; 280 PSAux_Service psaux = (PSAux_Service)face->psaux; 281 FT_UInt nn; 282 FT_Error error; 283 284 285 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 286 { 287 for ( nn = 0; nn < count; nn++ ) 288 advances[nn] = 0; 289 290 return FT_Err_Ok; 291 } 292 293 error = psaux->t1_decoder_funcs->init( &decoder, 294 (FT_Face)face, 295 0, /* size */ 296 0, /* glyph slot */ 297 (FT_Byte**)type1->glyph_names, 298 face->blend, 299 0, 300 FT_RENDER_MODE_NORMAL, 301 T1_Parse_Glyph ); 302 if ( error ) 303 return error; 304 305 decoder.builder.metrics_only = 1; 306 decoder.builder.load_points = 0; 307 308 decoder.num_subrs = type1->num_subrs; 309 decoder.subrs = type1->subrs; 310 decoder.subrs_len = type1->subrs_len; 311 decoder.subrs_hash = type1->subrs_hash; 312 313 decoder.buildchar = face->buildchar; 314 decoder.len_buildchar = face->len_buildchar; 315 316 for ( nn = 0; nn < count; nn++ ) 317 { 318 error = T1_Parse_Glyph( &decoder, first + nn ); 319 if ( !error ) 320 advances[nn] = FIXED_TO_INT( decoder.builder.advance.x ); 321 else 322 advances[nn] = 0; 323 } 324 325 return FT_Err_Ok; 326 } 327 328 329 FT_LOCAL_DEF( FT_Error ) 330 T1_Load_Glyph( FT_GlyphSlot t1glyph, /* T1_GlyphSlot */ 331 FT_Size t1size, /* T1_Size */ 332 FT_UInt glyph_index, 333 FT_Int32 load_flags ) 334 { 335 T1_GlyphSlot glyph = (T1_GlyphSlot)t1glyph; 336 FT_Error error; 337 T1_DecoderRec decoder; 338 T1_Face face = (T1_Face)t1glyph->face; 339 FT_Bool hinting; 340 FT_Bool scaled; 341 FT_Bool force_scaling = FALSE; 342 T1_Font type1 = &face->type1; 343 PSAux_Service psaux = (PSAux_Service)face->psaux; 344 const T1_Decoder_Funcs decoder_funcs = psaux->t1_decoder_funcs; 345 346 FT_Matrix font_matrix; 347 FT_Vector font_offset; 348 FT_Data glyph_data; 349 FT_Bool must_finish_decoder = FALSE; 350 #ifdef FT_CONFIG_OPTION_INCREMENTAL 351 FT_Bool glyph_data_loaded = 0; 352 #endif 353 354 355 #ifdef FT_CONFIG_OPTION_INCREMENTAL 356 if ( glyph_index >= (FT_UInt)face->root.num_glyphs && 357 !face->root.internal->incremental_interface ) 358 #else 359 if ( glyph_index >= (FT_UInt)face->root.num_glyphs ) 360 #endif /* FT_CONFIG_OPTION_INCREMENTAL */ 361 { 362 error = FT_THROW( Invalid_Argument ); 363 goto Exit; 364 } 365 366 FT_TRACE1(( "T1_Load_Glyph: glyph index %d\n", glyph_index )); 367 368 FT_ASSERT( ( face->len_buildchar == 0 ) == ( face->buildchar == NULL ) ); 369 370 if ( load_flags & FT_LOAD_NO_RECURSE ) 371 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING; 372 373 if ( t1size ) 374 { 375 glyph->x_scale = t1size->metrics.x_scale; 376 glyph->y_scale = t1size->metrics.y_scale; 377 } 378 else 379 { 380 glyph->x_scale = 0x10000L; 381 glyph->y_scale = 0x10000L; 382 } 383 384 t1glyph->outline.n_points = 0; 385 t1glyph->outline.n_contours = 0; 386 387 hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 && 388 ( load_flags & FT_LOAD_NO_HINTING ) == 0 ); 389 scaled = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 ); 390 391 glyph->hint = hinting; 392 glyph->scaled = scaled; 393 t1glyph->format = FT_GLYPH_FORMAT_OUTLINE; 394 395 error = decoder_funcs->init( &decoder, 396 t1glyph->face, 397 t1size, 398 t1glyph, 399 (FT_Byte**)type1->glyph_names, 400 face->blend, 401 FT_BOOL( hinting ), 402 FT_LOAD_TARGET_MODE( load_flags ), 403 T1_Parse_Glyph ); 404 if ( error ) 405 goto Exit; 406 407 must_finish_decoder = TRUE; 408 409 decoder.builder.no_recurse = FT_BOOL( 410 ( load_flags & FT_LOAD_NO_RECURSE ) != 0 ); 411 412 decoder.num_subrs = type1->num_subrs; 413 decoder.subrs = type1->subrs; 414 decoder.subrs_len = type1->subrs_len; 415 decoder.subrs_hash = type1->subrs_hash; 416 417 decoder.buildchar = face->buildchar; 418 decoder.len_buildchar = face->len_buildchar; 419 420 /* now load the unscaled outline */ 421 error = T1_Parse_Glyph_And_Get_Char_String( &decoder, glyph_index, 422 &glyph_data, 423 &force_scaling ); 424 if ( error ) 425 goto Exit; 426 #ifdef FT_CONFIG_OPTION_INCREMENTAL 427 glyph_data_loaded = 1; 428 #endif 429 430 hinting = glyph->hint; 431 font_matrix = decoder.font_matrix; 432 font_offset = decoder.font_offset; 433 434 /* save new glyph tables */ 435 decoder_funcs->done( &decoder ); 436 437 must_finish_decoder = FALSE; 438 439 /* now, set the metrics -- this is rather simple, as */ 440 /* the left side bearing is the xMin, and the top side */ 441 /* bearing the yMax */ 442 if ( !error ) 443 { 444 t1glyph->outline.flags &= FT_OUTLINE_OWNER; 445 t1glyph->outline.flags |= FT_OUTLINE_REVERSE_FILL; 446 447 /* for composite glyphs, return only left side bearing and */ 448 /* advance width */ 449 if ( load_flags & FT_LOAD_NO_RECURSE ) 450 { 451 FT_Slot_Internal internal = t1glyph->internal; 452 453 454 t1glyph->metrics.horiBearingX = 455 FIXED_TO_INT( decoder.builder.left_bearing.x ); 456 t1glyph->metrics.horiAdvance = 457 FIXED_TO_INT( decoder.builder.advance.x ); 458 459 internal->glyph_matrix = font_matrix; 460 internal->glyph_delta = font_offset; 461 internal->glyph_transformed = 1; 462 } 463 else 464 { 465 FT_BBox cbox; 466 FT_Glyph_Metrics* metrics = &t1glyph->metrics; 467 468 469 /* copy the _unscaled_ advance width */ 470 metrics->horiAdvance = 471 FIXED_TO_INT( decoder.builder.advance.x ); 472 t1glyph->linearHoriAdvance = 473 FIXED_TO_INT( decoder.builder.advance.x ); 474 t1glyph->internal->glyph_transformed = 0; 475 476 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 477 { 478 /* make up vertical ones */ 479 metrics->vertAdvance = ( face->type1.font_bbox.yMax - 480 face->type1.font_bbox.yMin ) >> 16; 481 t1glyph->linearVertAdvance = metrics->vertAdvance; 482 } 483 else 484 { 485 metrics->vertAdvance = 486 FIXED_TO_INT( decoder.builder.advance.y ); 487 t1glyph->linearVertAdvance = 488 FIXED_TO_INT( decoder.builder.advance.y ); 489 } 490 491 t1glyph->format = FT_GLYPH_FORMAT_OUTLINE; 492 493 if ( t1size && t1size->metrics.y_ppem < 24 ) 494 t1glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION; 495 496 #if 1 497 /* apply the font matrix, if any */ 498 if ( font_matrix.xx != 0x10000L || font_matrix.yy != 0x10000L || 499 font_matrix.xy != 0 || font_matrix.yx != 0 ) 500 { 501 FT_Outline_Transform( &t1glyph->outline, &font_matrix ); 502 503 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, 504 font_matrix.xx ); 505 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, 506 font_matrix.yy ); 507 } 508 509 if ( font_offset.x || font_offset.y ) 510 { 511 FT_Outline_Translate( &t1glyph->outline, 512 font_offset.x, 513 font_offset.y ); 514 515 metrics->horiAdvance += font_offset.x; 516 metrics->vertAdvance += font_offset.y; 517 } 518 #endif 519 520 if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 || force_scaling ) 521 { 522 /* scale the outline and the metrics */ 523 FT_Int n; 524 FT_Outline* cur = decoder.builder.base; 525 FT_Vector* vec = cur->points; 526 FT_Fixed x_scale = glyph->x_scale; 527 FT_Fixed y_scale = glyph->y_scale; 528 529 530 /* First of all, scale the points, if we are not hinting */ 531 if ( !hinting || ! decoder.builder.hints_funcs ) 532 for ( n = cur->n_points; n > 0; n--, vec++ ) 533 { 534 vec->x = FT_MulFix( vec->x, x_scale ); 535 vec->y = FT_MulFix( vec->y, y_scale ); 536 } 537 538 /* Then scale the metrics */ 539 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale ); 540 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale ); 541 } 542 543 /* compute the other metrics */ 544 FT_Outline_Get_CBox( &t1glyph->outline, &cbox ); 545 546 metrics->width = cbox.xMax - cbox.xMin; 547 metrics->height = cbox.yMax - cbox.yMin; 548 549 metrics->horiBearingX = cbox.xMin; 550 metrics->horiBearingY = cbox.yMax; 551 552 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 553 { 554 /* make up vertical ones */ 555 ft_synthesize_vertical_metrics( metrics, 556 metrics->vertAdvance ); 557 } 558 } 559 560 /* Set control data to the glyph charstrings. Note that this is */ 561 /* _not_ zero-terminated. */ 562 t1glyph->control_data = (FT_Byte*)glyph_data.pointer; 563 t1glyph->control_len = glyph_data.length; 564 } 565 566 567 Exit: 568 569 #ifdef FT_CONFIG_OPTION_INCREMENTAL 570 if ( glyph_data_loaded && face->root.internal->incremental_interface ) 571 { 572 face->root.internal->incremental_interface->funcs->free_glyph_data( 573 face->root.internal->incremental_interface->object, 574 &glyph_data ); 575 576 /* Set the control data to null - it is no longer available if */ 577 /* loaded incrementally. */ 578 t1glyph->control_data = NULL; 579 t1glyph->control_len = 0; 580 } 581 #endif 582 583 if ( must_finish_decoder ) 584 decoder_funcs->done( &decoder ); 585 586 return error; 587 } 588 589 590 /* END */ 591