19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/***************************************************************************/ 29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* */ 39a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* ttgload.c */ 49a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* */ 59a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* TrueType Glyph Loader (body). */ 69a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* */ 79a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* Copyright 1996-2014 */ 89a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* David Turner, Robert Wilhelm, and Werner Lemberg. */ 99a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* */ 109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* This file is part of the FreeType project, and may only be used, */ 119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* modified, and distributed under the terms of the FreeType project */ 129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 13e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org/* this file you indicate that you have read the license and */ 149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* understand and accept it fully. */ 159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* */ 169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/***************************************************************************/ 179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <ft2build.h> 209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include FT_INTERNAL_DEBUG_H 219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include FT_INTERNAL_CALC_H 229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include FT_INTERNAL_STREAM_H 239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include FT_INTERNAL_SFNT_H 249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include FT_TRUETYPE_TAGS_H 259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include FT_OUTLINE_H 269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include FT_TRUETYPE_DRIVER_H 27045e45efeca8776975254550137ec65268aadb54turaj@webrtc.org 289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "ttgload.h" 299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "ttpload.h" 309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "ttgxvar.h" 339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#endif 349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "tterrors.h" 360e2cb2989ce31ddd9069045a86ef4a9a781dddd3henrik.lundin@webrtc.org#include "ttsubpix.h" 370e2cb2989ce31ddd9069045a86ef4a9a781dddd3henrik.lundin@webrtc.org 389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org 399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org /*************************************************************************/ 409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org /* */ 410e2cb2989ce31ddd9069045a86ef4a9a781dddd3henrik.lundin@webrtc.org /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 420e2cb2989ce31ddd9069045a86ef4a9a781dddd3henrik.lundin@webrtc.org /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 430e2cb2989ce31ddd9069045a86ef4a9a781dddd3henrik.lundin@webrtc.org /* messages during execution. */ 440e2cb2989ce31ddd9069045a86ef4a9a781dddd3henrik.lundin@webrtc.org /* */ 450e2cb2989ce31ddd9069045a86ef4a9a781dddd3henrik.lundin@webrtc.org#undef FT_COMPONENT 460e2cb2989ce31ddd9069045a86ef4a9a781dddd3henrik.lundin@webrtc.org#define FT_COMPONENT trace_ttgload 47d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org 48d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org 49d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org /*************************************************************************/ 50d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org /* */ 51d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org /* Composite glyph flags. */ 52d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org /* */ 53d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org#define ARGS_ARE_WORDS 0x0001 54d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org#define ARGS_ARE_XY_VALUES 0x0002 55d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org#define ROUND_XY_TO_GRID 0x0004 56d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org#define WE_HAVE_A_SCALE 0x0008 57d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org/* reserved 0x0010 */ 58d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org#define MORE_COMPONENTS 0x0020 59d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org#define WE_HAVE_AN_XY_SCALE 0x0040 60d60fc6a759089cdb8edf09d76244d03a52bcbb59henrik.lundin@webrtc.org#define WE_HAVE_A_2X2 0x0080 619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#define WE_HAVE_INSTR 0x0100 629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#define USE_MY_METRICS 0x0200 63#define OVERLAP_COMPOUND 0x0400 64#define SCALED_COMPONENT_OFFSET 0x0800 65#define UNSCALED_COMPONENT_OFFSET 0x1000 66 67 68 /*************************************************************************/ 69 /* */ 70 /* Return the horizontal metrics in font units for a given glyph. */ 71 /* */ 72 FT_LOCAL_DEF( void ) 73 TT_Get_HMetrics( TT_Face face, 74 FT_UInt idx, 75 FT_Short* lsb, 76 FT_UShort* aw ) 77 { 78 ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw ); 79 80 FT_TRACE5(( " advance width (font units): %d\n", *aw )); 81 FT_TRACE5(( " left side bearing (font units): %d\n", *lsb )); 82 } 83 84 85 /*************************************************************************/ 86 /* */ 87 /* Return the vertical metrics in font units for a given glyph. */ 88 /* See macro `TT_LOADER_SET_PP' below for explanations. */ 89 /* */ 90 FT_LOCAL_DEF( void ) 91 TT_Get_VMetrics( TT_Face face, 92 FT_UInt idx, 93 FT_Pos yMax, 94 FT_Short* tsb, 95 FT_UShort* ah ) 96 { 97 if ( face->vertical_info ) 98 ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah ); 99 100 else if ( face->os2.version != 0xFFFFU ) 101 { 102 *tsb = (FT_Short)( face->os2.sTypoAscender - yMax ); 103 *ah = face->os2.sTypoAscender - face->os2.sTypoDescender; 104 } 105 106 else 107 { 108 *tsb = (FT_Short)( face->horizontal.Ascender - yMax ); 109 *ah = face->horizontal.Ascender - face->horizontal.Descender; 110 } 111 112 FT_TRACE5(( " advance height (font units): %d\n", *ah )); 113 FT_TRACE5(( " top side bearing (font units): %d\n", *tsb )); 114 } 115 116 117 static FT_Error 118 tt_get_metrics( TT_Loader loader, 119 FT_UInt glyph_index ) 120 { 121 TT_Face face = (TT_Face)loader->face; 122#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 123 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 124#endif 125 126 FT_Error error; 127 FT_Stream stream = loader->stream; 128 129 FT_Short left_bearing = 0, top_bearing = 0; 130 FT_UShort advance_width = 0, advance_height = 0; 131 132 /* we must preserve the stream position */ 133 /* (which gets altered by the metrics functions) */ 134 FT_ULong pos = FT_STREAM_POS(); 135 136 137 TT_Get_HMetrics( face, glyph_index, 138 &left_bearing, 139 &advance_width ); 140 TT_Get_VMetrics( face, glyph_index, 141 loader->bbox.yMax, 142 &top_bearing, 143 &advance_height ); 144 145 if ( FT_STREAM_SEEK( pos ) ) 146 return error; 147 148 loader->left_bearing = left_bearing; 149 loader->advance = advance_width; 150 loader->top_bearing = top_bearing; 151 loader->vadvance = advance_height; 152 153#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 154 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 155 { 156 if ( loader->exec ) 157 loader->exec->sph_tweak_flags = 0; 158 159 /* this may not be the right place for this, but it works */ 160 if ( loader->exec && loader->exec->ignore_x_mode ) 161 sph_set_tweaks( loader, glyph_index ); 162 } 163#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 164 165 if ( !loader->linear_def ) 166 { 167 loader->linear_def = 1; 168 loader->linear = advance_width; 169 } 170 171 return FT_Err_Ok; 172 } 173 174 175#ifdef FT_CONFIG_OPTION_INCREMENTAL 176 177 static void 178 tt_get_metrics_incr_overrides( TT_Loader loader, 179 FT_UInt glyph_index ) 180 { 181 TT_Face face = (TT_Face)loader->face; 182 183 FT_Short left_bearing = 0, top_bearing = 0; 184 FT_UShort advance_width = 0, advance_height = 0; 185 186 187 /* If this is an incrementally loaded font check whether there are */ 188 /* overriding metrics for this glyph. */ 189 if ( face->root.internal->incremental_interface && 190 face->root.internal->incremental_interface->funcs->get_glyph_metrics ) 191 { 192 FT_Incremental_MetricsRec metrics; 193 FT_Error error; 194 195 196 metrics.bearing_x = loader->left_bearing; 197 metrics.bearing_y = 0; 198 metrics.advance = loader->advance; 199 metrics.advance_v = 0; 200 201 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics( 202 face->root.internal->incremental_interface->object, 203 glyph_index, FALSE, &metrics ); 204 if ( error ) 205 goto Exit; 206 207 left_bearing = (FT_Short)metrics.bearing_x; 208 advance_width = (FT_UShort)metrics.advance; 209 210#if 0 211 212 /* GWW: Do I do the same for vertical metrics? */ 213 metrics.bearing_x = 0; 214 metrics.bearing_y = loader->top_bearing; 215 metrics.advance = loader->vadvance; 216 217 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics( 218 face->root.internal->incremental_interface->object, 219 glyph_index, TRUE, &metrics ); 220 if ( error ) 221 goto Exit; 222 223 top_bearing = (FT_Short)metrics.bearing_y; 224 advance_height = (FT_UShort)metrics.advance; 225 226#endif /* 0 */ 227 228 loader->left_bearing = left_bearing; 229 loader->advance = advance_width; 230 loader->top_bearing = top_bearing; 231 loader->vadvance = advance_height; 232 233 if ( !loader->linear_def ) 234 { 235 loader->linear_def = 1; 236 loader->linear = advance_width; 237 } 238 } 239 240 Exit: 241 return; 242 } 243 244#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 245 246 247 /*************************************************************************/ 248 /* */ 249 /* Translates an array of coordinates. */ 250 /* */ 251 static void 252 translate_array( FT_UInt n, 253 FT_Vector* coords, 254 FT_Pos delta_x, 255 FT_Pos delta_y ) 256 { 257 FT_UInt k; 258 259 260 if ( delta_x ) 261 for ( k = 0; k < n; k++ ) 262 coords[k].x += delta_x; 263 264 if ( delta_y ) 265 for ( k = 0; k < n; k++ ) 266 coords[k].y += delta_y; 267 } 268 269 270 /*************************************************************************/ 271 /* */ 272 /* The following functions are used by default with TrueType fonts. */ 273 /* However, they can be replaced by alternatives if we need to support */ 274 /* TrueType-compressed formats (like MicroType) in the future. */ 275 /* */ 276 /*************************************************************************/ 277 278 FT_CALLBACK_DEF( FT_Error ) 279 TT_Access_Glyph_Frame( TT_Loader loader, 280 FT_UInt glyph_index, 281 FT_ULong offset, 282 FT_UInt byte_count ) 283 { 284 FT_Error error; 285 FT_Stream stream = loader->stream; 286 287 /* for non-debug mode */ 288 FT_UNUSED( glyph_index ); 289 290 291 FT_TRACE4(( "Glyph %ld\n", glyph_index )); 292 293 /* the following line sets the `error' variable through macros! */ 294 if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) ) 295 return error; 296 297 loader->cursor = stream->cursor; 298 loader->limit = stream->limit; 299 300 return FT_Err_Ok; 301 } 302 303 304 FT_CALLBACK_DEF( void ) 305 TT_Forget_Glyph_Frame( TT_Loader loader ) 306 { 307 FT_Stream stream = loader->stream; 308 309 310 FT_FRAME_EXIT(); 311 } 312 313 314 FT_CALLBACK_DEF( FT_Error ) 315 TT_Load_Glyph_Header( TT_Loader loader ) 316 { 317 FT_Byte* p = loader->cursor; 318 FT_Byte* limit = loader->limit; 319 320 321 if ( p + 10 > limit ) 322 return FT_THROW( Invalid_Outline ); 323 324 loader->n_contours = FT_NEXT_SHORT( p ); 325 326 loader->bbox.xMin = FT_NEXT_SHORT( p ); 327 loader->bbox.yMin = FT_NEXT_SHORT( p ); 328 loader->bbox.xMax = FT_NEXT_SHORT( p ); 329 loader->bbox.yMax = FT_NEXT_SHORT( p ); 330 331 FT_TRACE5(( " # of contours: %d\n", loader->n_contours )); 332 FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader->bbox.xMin, 333 loader->bbox.xMax )); 334 FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader->bbox.yMin, 335 loader->bbox.yMax )); 336 loader->cursor = p; 337 338 return FT_Err_Ok; 339 } 340 341 342 FT_CALLBACK_DEF( FT_Error ) 343 TT_Load_Simple_Glyph( TT_Loader load ) 344 { 345 FT_Error error; 346 FT_Byte* p = load->cursor; 347 FT_Byte* limit = load->limit; 348 FT_GlyphLoader gloader = load->gloader; 349 FT_Int n_contours = load->n_contours; 350 FT_Outline* outline; 351 FT_UShort n_ins; 352 FT_Int n_points; 353 FT_ULong tmp; 354 355 FT_Byte *flag, *flag_limit; 356 FT_Byte c, count; 357 FT_Vector *vec, *vec_limit; 358 FT_Pos x; 359 FT_Short *cont, *cont_limit, prev_cont; 360 FT_Int xy_size = 0; 361 362 363 /* check that we can add the contours to the glyph */ 364 error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours ); 365 if ( error ) 366 goto Fail; 367 368 /* reading the contours' endpoints & number of points */ 369 cont = gloader->current.outline.contours; 370 cont_limit = cont + n_contours; 371 372 /* check space for contours array + instructions count */ 373 if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit ) 374 goto Invalid_Outline; 375 376 prev_cont = FT_NEXT_SHORT( p ); 377 378 if ( n_contours > 0 ) 379 cont[0] = prev_cont; 380 381 if ( prev_cont < 0 ) 382 goto Invalid_Outline; 383 384 for ( cont++; cont < cont_limit; cont++ ) 385 { 386 cont[0] = FT_NEXT_SHORT( p ); 387 if ( cont[0] <= prev_cont ) 388 { 389 /* unordered contours: this is invalid */ 390 goto Invalid_Outline; 391 } 392 prev_cont = cont[0]; 393 } 394 395 n_points = 0; 396 if ( n_contours > 0 ) 397 { 398 n_points = cont[-1] + 1; 399 if ( n_points < 0 ) 400 goto Invalid_Outline; 401 } 402 403 /* note that we will add four phantom points later */ 404 error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 ); 405 if ( error ) 406 goto Fail; 407 408 /* reading the bytecode instructions */ 409 load->glyph->control_len = 0; 410 load->glyph->control_data = 0; 411 412 if ( p + 2 > limit ) 413 goto Invalid_Outline; 414 415 n_ins = FT_NEXT_USHORT( p ); 416 417 FT_TRACE5(( " Instructions size: %u\n", n_ins )); 418 419 /* check it */ 420 if ( ( limit - p ) < n_ins ) 421 { 422 FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" )); 423 error = FT_THROW( Too_Many_Hints ); 424 goto Fail; 425 } 426 427#ifdef TT_USE_BYTECODE_INTERPRETER 428 429 if ( IS_HINTED( load->load_flags ) ) 430 { 431 /* we don't trust `maxSizeOfInstructions' in the `maxp' table */ 432 /* and thus update the bytecode array size by ourselves */ 433 434 tmp = load->exec->glyphSize; 435 error = Update_Max( load->exec->memory, 436 &tmp, 437 sizeof ( FT_Byte ), 438 (void*)&load->exec->glyphIns, 439 n_ins ); 440 441 load->exec->glyphSize = (FT_UShort)tmp; 442 if ( error ) 443 return error; 444 445 load->glyph->control_len = n_ins; 446 load->glyph->control_data = load->exec->glyphIns; 447 448 FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins ); 449 } 450 451#endif /* TT_USE_BYTECODE_INTERPRETER */ 452 453 p += n_ins; 454 455 outline = &gloader->current.outline; 456 457 /* reading the point tags */ 458 flag = (FT_Byte*)outline->tags; 459 flag_limit = flag + n_points; 460 461 FT_ASSERT( flag != NULL ); 462 463 while ( flag < flag_limit ) 464 { 465 if ( p + 1 > limit ) 466 goto Invalid_Outline; 467 468 *flag++ = c = FT_NEXT_BYTE( p ); 469 if ( c & 8 ) 470 { 471 if ( p + 1 > limit ) 472 goto Invalid_Outline; 473 474 count = FT_NEXT_BYTE( p ); 475 if ( flag + (FT_Int)count > flag_limit ) 476 goto Invalid_Outline; 477 478 for ( ; count > 0; count-- ) 479 *flag++ = c; 480 } 481 } 482 483 /* reading the X coordinates */ 484 485 vec = outline->points; 486 vec_limit = vec + n_points; 487 flag = (FT_Byte*)outline->tags; 488 x = 0; 489 490 if ( p + xy_size > limit ) 491 goto Invalid_Outline; 492 493 for ( ; vec < vec_limit; vec++, flag++ ) 494 { 495 FT_Pos y = 0; 496 FT_Byte f = *flag; 497 498 499 if ( f & 2 ) 500 { 501 if ( p + 1 > limit ) 502 goto Invalid_Outline; 503 504 y = (FT_Pos)FT_NEXT_BYTE( p ); 505 if ( ( f & 16 ) == 0 ) 506 y = -y; 507 } 508 else if ( ( f & 16 ) == 0 ) 509 { 510 if ( p + 2 > limit ) 511 goto Invalid_Outline; 512 513 y = (FT_Pos)FT_NEXT_SHORT( p ); 514 } 515 516 x += y; 517 vec->x = x; 518 /* the cast is for stupid compilers */ 519 *flag = (FT_Byte)( f & ~( 2 | 16 ) ); 520 } 521 522 /* reading the Y coordinates */ 523 524 vec = gloader->current.outline.points; 525 vec_limit = vec + n_points; 526 flag = (FT_Byte*)outline->tags; 527 x = 0; 528 529 for ( ; vec < vec_limit; vec++, flag++ ) 530 { 531 FT_Pos y = 0; 532 FT_Byte f = *flag; 533 534 535 if ( f & 4 ) 536 { 537 if ( p + 1 > limit ) 538 goto Invalid_Outline; 539 540 y = (FT_Pos)FT_NEXT_BYTE( p ); 541 if ( ( f & 32 ) == 0 ) 542 y = -y; 543 } 544 else if ( ( f & 32 ) == 0 ) 545 { 546 if ( p + 2 > limit ) 547 goto Invalid_Outline; 548 549 y = (FT_Pos)FT_NEXT_SHORT( p ); 550 } 551 552 x += y; 553 vec->y = x; 554 /* the cast is for stupid compilers */ 555 *flag = (FT_Byte)( f & FT_CURVE_TAG_ON ); 556 } 557 558 outline->n_points = (FT_UShort)n_points; 559 outline->n_contours = (FT_Short) n_contours; 560 561 load->cursor = p; 562 563 Fail: 564 return error; 565 566 Invalid_Outline: 567 error = FT_THROW( Invalid_Outline ); 568 goto Fail; 569 } 570 571 572 FT_CALLBACK_DEF( FT_Error ) 573 TT_Load_Composite_Glyph( TT_Loader loader ) 574 { 575 FT_Error error; 576 FT_Byte* p = loader->cursor; 577 FT_Byte* limit = loader->limit; 578 FT_GlyphLoader gloader = loader->gloader; 579 FT_SubGlyph subglyph; 580 FT_UInt num_subglyphs; 581 582 583 num_subglyphs = 0; 584 585 do 586 { 587 FT_Fixed xx, xy, yy, yx; 588 FT_UInt count; 589 590 591 /* check that we can load a new subglyph */ 592 error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 ); 593 if ( error ) 594 goto Fail; 595 596 /* check space */ 597 if ( p + 4 > limit ) 598 goto Invalid_Composite; 599 600 subglyph = gloader->current.subglyphs + num_subglyphs; 601 602 subglyph->arg1 = subglyph->arg2 = 0; 603 604 subglyph->flags = FT_NEXT_USHORT( p ); 605 subglyph->index = FT_NEXT_USHORT( p ); 606 607 /* check space */ 608 count = 2; 609 if ( subglyph->flags & ARGS_ARE_WORDS ) 610 count += 2; 611 if ( subglyph->flags & WE_HAVE_A_SCALE ) 612 count += 2; 613 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE ) 614 count += 4; 615 else if ( subglyph->flags & WE_HAVE_A_2X2 ) 616 count += 8; 617 618 if ( p + count > limit ) 619 goto Invalid_Composite; 620 621 /* read arguments */ 622 if ( subglyph->flags & ARGS_ARE_WORDS ) 623 { 624 subglyph->arg1 = FT_NEXT_SHORT( p ); 625 subglyph->arg2 = FT_NEXT_SHORT( p ); 626 } 627 else 628 { 629 subglyph->arg1 = FT_NEXT_CHAR( p ); 630 subglyph->arg2 = FT_NEXT_CHAR( p ); 631 } 632 633 /* read transform */ 634 xx = yy = 0x10000L; 635 xy = yx = 0; 636 637 if ( subglyph->flags & WE_HAVE_A_SCALE ) 638 { 639 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 640 yy = xx; 641 } 642 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE ) 643 { 644 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 645 yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 646 } 647 else if ( subglyph->flags & WE_HAVE_A_2X2 ) 648 { 649 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 650 yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 651 xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 652 yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 653 } 654 655 subglyph->transform.xx = xx; 656 subglyph->transform.xy = xy; 657 subglyph->transform.yx = yx; 658 subglyph->transform.yy = yy; 659 660 num_subglyphs++; 661 662 } while ( subglyph->flags & MORE_COMPONENTS ); 663 664 gloader->current.num_subglyphs = num_subglyphs; 665 666#ifdef TT_USE_BYTECODE_INTERPRETER 667 668 { 669 FT_Stream stream = loader->stream; 670 671 672 /* we must undo the FT_FRAME_ENTER in order to point */ 673 /* to the composite instructions, if we find some. */ 674 /* We will process them later. */ 675 /* */ 676 loader->ins_pos = (FT_ULong)( FT_STREAM_POS() + 677 p - limit ); 678 } 679 680#endif 681 682 loader->cursor = p; 683 684 Fail: 685 return error; 686 687 Invalid_Composite: 688 error = FT_THROW( Invalid_Composite ); 689 goto Fail; 690 } 691 692 693 FT_LOCAL_DEF( void ) 694 TT_Init_Glyph_Loading( TT_Face face ) 695 { 696 face->access_glyph_frame = TT_Access_Glyph_Frame; 697 face->read_glyph_header = TT_Load_Glyph_Header; 698 face->read_simple_glyph = TT_Load_Simple_Glyph; 699 face->read_composite_glyph = TT_Load_Composite_Glyph; 700 face->forget_glyph_frame = TT_Forget_Glyph_Frame; 701 } 702 703 704 static void 705 tt_prepare_zone( TT_GlyphZone zone, 706 FT_GlyphLoad load, 707 FT_UInt start_point, 708 FT_UInt start_contour ) 709 { 710 zone->n_points = (FT_UShort)( load->outline.n_points - start_point ); 711 zone->n_contours = (FT_Short) ( load->outline.n_contours - 712 start_contour ); 713 zone->org = load->extra_points + start_point; 714 zone->cur = load->outline.points + start_point; 715 zone->orus = load->extra_points2 + start_point; 716 zone->tags = (FT_Byte*)load->outline.tags + start_point; 717 zone->contours = (FT_UShort*)load->outline.contours + start_contour; 718 zone->first_point = (FT_UShort)start_point; 719 } 720 721 722 /*************************************************************************/ 723 /* */ 724 /* <Function> */ 725 /* TT_Hint_Glyph */ 726 /* */ 727 /* <Description> */ 728 /* Hint the glyph using the zone prepared by the caller. Note that */ 729 /* the zone is supposed to include four phantom points. */ 730 /* */ 731 static FT_Error 732 TT_Hint_Glyph( TT_Loader loader, 733 FT_Bool is_composite ) 734 { 735#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 736 TT_Face face = (TT_Face)loader->face; 737 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 738#endif 739 740 TT_GlyphZone zone = &loader->zone; 741 742#ifdef TT_USE_BYTECODE_INTERPRETER 743 FT_UInt n_ins; 744#else 745 FT_UNUSED( is_composite ); 746#endif 747 748 749#ifdef TT_USE_BYTECODE_INTERPRETER 750 if ( loader->glyph->control_len > 0xFFFFL ) 751 { 752 FT_TRACE1(( "TT_Hint_Glyph: too long instructions" )); 753 FT_TRACE1(( " (0x%lx byte) is truncated\n", 754 loader->glyph->control_len )); 755 } 756 n_ins = (FT_UInt)( loader->glyph->control_len ); 757 758 /* save original point position in org */ 759 if ( n_ins > 0 ) 760 FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points ); 761 762 /* Reset graphics state. */ 763 loader->exec->GS = ((TT_Size)loader->size)->GS; 764 765 /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */ 766 /* completely refer to the (already) hinted subglyphs. */ 767 if ( is_composite ) 768 { 769 loader->exec->metrics.x_scale = 1 << 16; 770 loader->exec->metrics.y_scale = 1 << 16; 771 772 FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points ); 773 } 774 else 775 { 776 loader->exec->metrics.x_scale = 777 ((TT_Size)loader->size)->metrics.x_scale; 778 loader->exec->metrics.y_scale = 779 ((TT_Size)loader->size)->metrics.y_scale; 780 } 781#endif 782 783 /* round phantom points */ 784 zone->cur[zone->n_points - 4].x = 785 FT_PIX_ROUND( zone->cur[zone->n_points - 4].x ); 786 zone->cur[zone->n_points - 3].x = 787 FT_PIX_ROUND( zone->cur[zone->n_points - 3].x ); 788 zone->cur[zone->n_points - 2].y = 789 FT_PIX_ROUND( zone->cur[zone->n_points - 2].y ); 790 zone->cur[zone->n_points - 1].y = 791 FT_PIX_ROUND( zone->cur[zone->n_points - 1].y ); 792 793#ifdef TT_USE_BYTECODE_INTERPRETER 794 795 if ( n_ins > 0 ) 796 { 797 FT_Bool debug; 798 FT_Error error; 799 800 FT_GlyphLoader gloader = loader->gloader; 801 FT_Outline current_outline = gloader->current.outline; 802 803 804 error = TT_Set_CodeRange( loader->exec, tt_coderange_glyph, 805 loader->exec->glyphIns, n_ins ); 806 if ( error ) 807 return error; 808 809 loader->exec->is_composite = is_composite; 810 loader->exec->pts = *zone; 811 812 debug = FT_BOOL( !( loader->load_flags & FT_LOAD_NO_SCALE ) && 813 ((TT_Size)loader->size)->debug ); 814 815 error = TT_Run_Context( loader->exec, debug ); 816 if ( error && loader->exec->pedantic_hinting ) 817 return error; 818 819 /* store drop-out mode in bits 5-7; set bit 2 also as a marker */ 820 current_outline.tags[0] |= 821 ( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE; 822 } 823 824#endif 825 826 /* save glyph phantom points */ 827 loader->pp1 = zone->cur[zone->n_points - 4]; 828 loader->pp2 = zone->cur[zone->n_points - 3]; 829 loader->pp3 = zone->cur[zone->n_points - 2]; 830 loader->pp4 = zone->cur[zone->n_points - 1]; 831 832#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 833 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 834 { 835 if ( loader->exec->sph_tweak_flags & SPH_TWEAK_DEEMBOLDEN ) 836 FT_Outline_EmboldenXY( &loader->gloader->current.outline, -24, 0 ); 837 838 else if ( loader->exec->sph_tweak_flags & SPH_TWEAK_EMBOLDEN ) 839 FT_Outline_EmboldenXY( &loader->gloader->current.outline, 24, 0 ); 840 } 841#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 842 843 return FT_Err_Ok; 844 } 845 846 847 /*************************************************************************/ 848 /* */ 849 /* <Function> */ 850 /* TT_Process_Simple_Glyph */ 851 /* */ 852 /* <Description> */ 853 /* Once a simple glyph has been loaded, it needs to be processed. */ 854 /* Usually, this means scaling and hinting through bytecode */ 855 /* interpretation. */ 856 /* */ 857 static FT_Error 858 TT_Process_Simple_Glyph( TT_Loader loader ) 859 { 860 FT_GlyphLoader gloader = loader->gloader; 861 FT_Error error = FT_Err_Ok; 862 FT_Outline* outline; 863 FT_Int n_points; 864 865 866 outline = &gloader->current.outline; 867 n_points = outline->n_points; 868 869 /* set phantom points */ 870 871 outline->points[n_points ] = loader->pp1; 872 outline->points[n_points + 1] = loader->pp2; 873 outline->points[n_points + 2] = loader->pp3; 874 outline->points[n_points + 3] = loader->pp4; 875 876 outline->tags[n_points ] = 0; 877 outline->tags[n_points + 1] = 0; 878 outline->tags[n_points + 2] = 0; 879 outline->tags[n_points + 3] = 0; 880 881 n_points += 4; 882 883#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 884 885 if ( ((TT_Face)loader->face)->doblend ) 886 { 887 /* Deltas apply to the unscaled data. */ 888 FT_Vector* deltas; 889 FT_Memory memory = loader->face->memory; 890 FT_Int i; 891 892 893 error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face), 894 loader->glyph_index, 895 &deltas, 896 n_points ); 897 if ( error ) 898 return error; 899 900 for ( i = 0; i < n_points; ++i ) 901 { 902 outline->points[i].x += deltas[i].x; 903 outline->points[i].y += deltas[i].y; 904 } 905 906 FT_FREE( deltas ); 907 } 908 909#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 910 911 if ( IS_HINTED( loader->load_flags ) ) 912 { 913 tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 ); 914 915 FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur, 916 loader->zone.n_points + 4 ); 917 } 918 919 { 920#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 921 TT_Face face = (TT_Face)loader->face; 922 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 923 924 FT_String* family = face->root.family_name; 925 FT_Int ppem = loader->size->metrics.x_ppem; 926 FT_String* style = face->root.style_name; 927 FT_Int x_scale_factor = 1000; 928#endif 929 930 FT_Vector* vec = outline->points; 931 FT_Vector* limit = outline->points + n_points; 932 933 FT_Fixed x_scale = 0; /* pacify compiler */ 934 FT_Fixed y_scale = 0; 935 936 FT_Bool do_scale = FALSE; 937 938 939#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 940 941 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 942 { 943 /* scale, but only if enabled and only if TT hinting is being used */ 944 if ( IS_HINTED( loader->load_flags ) ) 945 x_scale_factor = sph_test_tweak_x_scaling( face, 946 family, 947 ppem, 948 style, 949 loader->glyph_index ); 950 /* scale the glyph */ 951 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 || 952 x_scale_factor != 1000 ) 953 { 954 x_scale = FT_MulDiv( ((TT_Size)loader->size)->metrics.x_scale, 955 x_scale_factor, 1000 ); 956 y_scale = ((TT_Size)loader->size)->metrics.y_scale; 957 958 /* compensate for any scaling by de/emboldening; */ 959 /* the amount was determined via experimentation */ 960 if ( x_scale_factor != 1000 && ppem > 11 ) 961 FT_Outline_EmboldenXY( outline, 962 FT_MulFix( 1280 * ppem, 963 1000 - x_scale_factor ), 964 0 ); 965 do_scale = TRUE; 966 } 967 } 968 else 969 970#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 971 972 { 973 /* scale the glyph */ 974 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 975 { 976 x_scale = ((TT_Size)loader->size)->metrics.x_scale; 977 y_scale = ((TT_Size)loader->size)->metrics.y_scale; 978 979 do_scale = TRUE; 980 } 981 } 982 983 if ( do_scale ) 984 { 985 for ( ; vec < limit; vec++ ) 986 { 987 vec->x = FT_MulFix( vec->x, x_scale ); 988 vec->y = FT_MulFix( vec->y, y_scale ); 989 } 990 991 loader->pp1 = outline->points[n_points - 4]; 992 loader->pp2 = outline->points[n_points - 3]; 993 loader->pp3 = outline->points[n_points - 2]; 994 loader->pp4 = outline->points[n_points - 1]; 995 } 996 } 997 998 if ( IS_HINTED( loader->load_flags ) ) 999 { 1000 loader->zone.n_points += 4; 1001 1002 error = TT_Hint_Glyph( loader, 0 ); 1003 } 1004 1005 return error; 1006 } 1007 1008 1009 /*************************************************************************/ 1010 /* */ 1011 /* <Function> */ 1012 /* TT_Process_Composite_Component */ 1013 /* */ 1014 /* <Description> */ 1015 /* Once a composite component has been loaded, it needs to be */ 1016 /* processed. Usually, this means transforming and translating. */ 1017 /* */ 1018 static FT_Error 1019 TT_Process_Composite_Component( TT_Loader loader, 1020 FT_SubGlyph subglyph, 1021 FT_UInt start_point, 1022 FT_UInt num_base_points ) 1023 { 1024 FT_GlyphLoader gloader = loader->gloader; 1025 FT_Vector* base_vec = gloader->base.outline.points; 1026 FT_UInt num_points = gloader->base.outline.n_points; 1027 FT_Bool have_scale; 1028 FT_Pos x, y; 1029 1030 1031 have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE | 1032 WE_HAVE_AN_XY_SCALE | 1033 WE_HAVE_A_2X2 ) ); 1034 1035 /* perform the transform required for this subglyph */ 1036 if ( have_scale ) 1037 { 1038 FT_UInt i; 1039 1040 1041 for ( i = num_base_points; i < num_points; i++ ) 1042 FT_Vector_Transform( base_vec + i, &subglyph->transform ); 1043 } 1044 1045 /* get offset */ 1046 if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) ) 1047 { 1048 FT_UInt k = subglyph->arg1; 1049 FT_UInt l = subglyph->arg2; 1050 FT_Vector* p1; 1051 FT_Vector* p2; 1052 1053 1054 /* match l-th point of the newly loaded component to the k-th point */ 1055 /* of the previously loaded components. */ 1056 1057 /* change to the point numbers used by our outline */ 1058 k += start_point; 1059 l += num_base_points; 1060 if ( k >= num_base_points || 1061 l >= num_points ) 1062 return FT_THROW( Invalid_Composite ); 1063 1064 p1 = gloader->base.outline.points + k; 1065 p2 = gloader->base.outline.points + l; 1066 1067 x = p1->x - p2->x; 1068 y = p1->y - p2->y; 1069 } 1070 else 1071 { 1072 x = subglyph->arg1; 1073 y = subglyph->arg2; 1074 1075 if ( !x && !y ) 1076 return FT_Err_Ok; 1077 1078 /* Use a default value dependent on */ 1079 /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old */ 1080 /* TT fonts which don't set the xxx_COMPONENT_OFFSET bit. */ 1081 1082 if ( have_scale && 1083#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED 1084 !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) ) 1085#else 1086 ( subglyph->flags & SCALED_COMPONENT_OFFSET ) ) 1087#endif 1088 { 1089 1090#if 0 1091 1092 /*******************************************************************/ 1093 /* */ 1094 /* This algorithm is what Apple documents. But it doesn't work. */ 1095 /* */ 1096 int a = subglyph->transform.xx > 0 ? subglyph->transform.xx 1097 : -subglyph->transform.xx; 1098 int b = subglyph->transform.yx > 0 ? subglyph->transform.yx 1099 : -subglyph->transform.yx; 1100 int c = subglyph->transform.xy > 0 ? subglyph->transform.xy 1101 : -subglyph->transform.xy; 1102 int d = subglyph->transform.yy > 0 ? subglyph->transform.yy 1103 : -subglyph->transform.yy; 1104 int m = a > b ? a : b; 1105 int n = c > d ? c : d; 1106 1107 1108 if ( a - b <= 33 && a - b >= -33 ) 1109 m *= 2; 1110 if ( c - d <= 33 && c - d >= -33 ) 1111 n *= 2; 1112 x = FT_MulFix( x, m ); 1113 y = FT_MulFix( y, n ); 1114 1115#else /* 1 */ 1116 1117 /*******************************************************************/ 1118 /* */ 1119 /* This algorithm is a guess and works much better than the above. */ 1120 /* */ 1121 FT_Fixed mac_xscale = FT_Hypot( subglyph->transform.xx, 1122 subglyph->transform.xy ); 1123 FT_Fixed mac_yscale = FT_Hypot( subglyph->transform.yy, 1124 subglyph->transform.yx ); 1125 1126 1127 x = FT_MulFix( x, mac_xscale ); 1128 y = FT_MulFix( y, mac_yscale ); 1129 1130#endif /* 1 */ 1131 1132 } 1133 1134 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 1135 { 1136 FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale; 1137 FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale; 1138 1139 1140 x = FT_MulFix( x, x_scale ); 1141 y = FT_MulFix( y, y_scale ); 1142 1143 if ( subglyph->flags & ROUND_XY_TO_GRID ) 1144 { 1145 x = FT_PIX_ROUND( x ); 1146 y = FT_PIX_ROUND( y ); 1147 } 1148 } 1149 } 1150 1151 if ( x || y ) 1152 translate_array( num_points - num_base_points, 1153 base_vec + num_base_points, 1154 x, y ); 1155 1156 return FT_Err_Ok; 1157 } 1158 1159 1160 /*************************************************************************/ 1161 /* */ 1162 /* <Function> */ 1163 /* TT_Process_Composite_Glyph */ 1164 /* */ 1165 /* <Description> */ 1166 /* This is slightly different from TT_Process_Simple_Glyph, in that */ 1167 /* its sole purpose is to hint the glyph. Thus this function is */ 1168 /* only available when bytecode interpreter is enabled. */ 1169 /* */ 1170 static FT_Error 1171 TT_Process_Composite_Glyph( TT_Loader loader, 1172 FT_UInt start_point, 1173 FT_UInt start_contour ) 1174 { 1175 FT_Error error; 1176 FT_Outline* outline; 1177 FT_UInt i; 1178 1179 1180 outline = &loader->gloader->base.outline; 1181 1182 /* make room for phantom points */ 1183 error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader, 1184 outline->n_points + 4, 1185 0 ); 1186 if ( error ) 1187 return error; 1188 1189 outline->points[outline->n_points ] = loader->pp1; 1190 outline->points[outline->n_points + 1] = loader->pp2; 1191 outline->points[outline->n_points + 2] = loader->pp3; 1192 outline->points[outline->n_points + 3] = loader->pp4; 1193 1194 outline->tags[outline->n_points ] = 0; 1195 outline->tags[outline->n_points + 1] = 0; 1196 outline->tags[outline->n_points + 2] = 0; 1197 outline->tags[outline->n_points + 3] = 0; 1198 1199#ifdef TT_USE_BYTECODE_INTERPRETER 1200 1201 { 1202 FT_Stream stream = loader->stream; 1203 FT_UShort n_ins, max_ins; 1204 FT_ULong tmp; 1205 1206 1207 /* TT_Load_Composite_Glyph only gives us the offset of instructions */ 1208 /* so we read them here */ 1209 if ( FT_STREAM_SEEK( loader->ins_pos ) || 1210 FT_READ_USHORT( n_ins ) ) 1211 return error; 1212 1213 FT_TRACE5(( " Instructions size = %d\n", n_ins )); 1214 1215 /* check it */ 1216 max_ins = ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions; 1217 if ( n_ins > max_ins ) 1218 { 1219 /* don't trust `maxSizeOfInstructions'; */ 1220 /* only do a rough safety check */ 1221 if ( (FT_Int)n_ins > loader->byte_len ) 1222 { 1223 FT_TRACE1(( "TT_Process_Composite_Glyph:" 1224 " too many instructions (%d) for glyph with length %d\n", 1225 n_ins, loader->byte_len )); 1226 return FT_THROW( Too_Many_Hints ); 1227 } 1228 1229 tmp = loader->exec->glyphSize; 1230 error = Update_Max( loader->exec->memory, 1231 &tmp, 1232 sizeof ( FT_Byte ), 1233 (void*)&loader->exec->glyphIns, 1234 n_ins ); 1235 1236 loader->exec->glyphSize = (FT_UShort)tmp; 1237 if ( error ) 1238 return error; 1239 } 1240 else if ( n_ins == 0 ) 1241 return FT_Err_Ok; 1242 1243 if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) ) 1244 return error; 1245 1246 loader->glyph->control_data = loader->exec->glyphIns; 1247 loader->glyph->control_len = n_ins; 1248 } 1249 1250#endif 1251 1252 tt_prepare_zone( &loader->zone, &loader->gloader->base, 1253 start_point, start_contour ); 1254 1255 /* Some points are likely touched during execution of */ 1256 /* instructions on components. So let's untouch them. */ 1257 for ( i = 0; i < loader->zone.n_points; i++ ) 1258 loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH; 1259 1260 loader->zone.n_points += 4; 1261 1262 return TT_Hint_Glyph( loader, 1 ); 1263 } 1264 1265 1266 /* 1267 * Calculate the phantom points 1268 * 1269 * Defining the right side bearing (rsb) as 1270 * 1271 * rsb = aw - (lsb + xmax - xmin) 1272 * 1273 * (with `aw' the advance width, `lsb' the left side bearing, and `xmin' 1274 * and `xmax' the glyph's minimum and maximum x value), the OpenType 1275 * specification defines the initial position of horizontal phantom points 1276 * as 1277 * 1278 * pp1 = (round(xmin - lsb), 0) , 1279 * pp2 = (round(pp1 + aw), 0) . 1280 * 1281 * Note that the rounding to the grid (in the device space) is not 1282 * documented currently in the specification. 1283 * 1284 * However, the specification lacks the precise definition of vertical 1285 * phantom points. Greg Hitchcock provided the following explanation. 1286 * 1287 * - a `vmtx' table is present 1288 * 1289 * For any glyph, the minimum and maximum y values (`ymin' and `ymax') 1290 * are given in the `glyf' table, the top side bearing (tsb) and advance 1291 * height (ah) are given in the `vmtx' table. The bottom side bearing 1292 * (bsb) is then calculated as 1293 * 1294 * bsb = ah - (tsb + ymax - ymin) , 1295 * 1296 * and the initial position of vertical phantom points is 1297 * 1298 * pp3 = (x, round(ymax + tsb)) , 1299 * pp4 = (x, round(pp3 - ah)) . 1300 * 1301 * See below for value `x'. 1302 * 1303 * - no `vmtx' table in the font 1304 * 1305 * If there is an `OS/2' table, we set 1306 * 1307 * DefaultAscender = sTypoAscender , 1308 * DefaultDescender = sTypoDescender , 1309 * 1310 * otherwise we use data from the `hhea' table: 1311 * 1312 * DefaultAscender = Ascender , 1313 * DefaultDescender = Descender . 1314 * 1315 * With these two variables we can now set 1316 * 1317 * ah = DefaultAscender - sDefaultDescender , 1318 * tsb = DefaultAscender - yMax , 1319 * 1320 * and proceed as if a `vmtx' table was present. 1321 * 1322 * Usually we have 1323 * 1324 * x = aw / 2 , (1) 1325 * 1326 * but there is one compatibility case where it can be set to 1327 * 1328 * x = -DefaultDescender - 1329 * ((DefaultAscender - DefaultDescender - aw) / 2) . (2) 1330 * 1331 * and another one with 1332 * 1333 * x = 0 . (3) 1334 * 1335 * In Windows, the history of those values is quite complicated, 1336 * depending on the hinting engine (that is, the graphics framework). 1337 * 1338 * framework from to formula 1339 * ---------------------------------------------------------- 1340 * GDI Windows 98 current (1) 1341 * (Windows 2000 for NT) 1342 * GDI+ Windows XP Windows 7 (2) 1343 * GDI+ Windows 8 current (3) 1344 * DWrite Windows 7 current (3) 1345 * 1346 * For simplicity, FreeType uses (1) for grayscale subpixel hinting and 1347 * (3) for everything else. 1348 * 1349 */ 1350#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 1351 1352#define TT_LOADER_SET_PP( loader ) \ 1353 do \ 1354 { \ 1355 FT_Bool subpixel_ = loader->exec ? loader->exec->subpixel \ 1356 : 0; \ 1357 FT_Bool grayscale_ = loader->exec ? loader->exec->grayscale \ 1358 : 0; \ 1359 FT_Bool use_aw_2_ = (FT_Bool)( subpixel_ && grayscale_ ); \ 1360 \ 1361 \ 1362 (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \ 1363 (loader)->pp1.y = 0; \ 1364 (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \ 1365 (loader)->pp2.y = 0; \ 1366 \ 1367 (loader)->pp3.x = use_aw_2_ ? (loader)->advance / 2 : 0; \ 1368 (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing; \ 1369 (loader)->pp4.x = use_aw_2_ ? (loader)->advance / 2 : 0; \ 1370 (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \ 1371 } while ( 0 ) 1372 1373#else /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 1374 1375#define TT_LOADER_SET_PP( loader ) \ 1376 do \ 1377 { \ 1378 (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \ 1379 (loader)->pp1.y = 0; \ 1380 (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \ 1381 (loader)->pp2.y = 0; \ 1382 \ 1383 (loader)->pp3.x = 0; \ 1384 (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing; \ 1385 (loader)->pp4.x = 0; \ 1386 (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \ 1387 } while ( 0 ) 1388 1389#endif /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 1390 1391 1392 /*************************************************************************/ 1393 /* */ 1394 /* <Function> */ 1395 /* load_truetype_glyph */ 1396 /* */ 1397 /* <Description> */ 1398 /* Loads a given truetype glyph. Handles composites and uses a */ 1399 /* TT_Loader object. */ 1400 /* */ 1401 static FT_Error 1402 load_truetype_glyph( TT_Loader loader, 1403 FT_UInt glyph_index, 1404 FT_UInt recurse_count, 1405 FT_Bool header_only ) 1406 { 1407 FT_Error error = FT_Err_Ok; 1408 FT_Fixed x_scale, y_scale; 1409 FT_ULong offset; 1410 TT_Face face = (TT_Face)loader->face; 1411 FT_GlyphLoader gloader = loader->gloader; 1412 FT_Bool opened_frame = 0; 1413 1414#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1415 FT_Vector* deltas = NULL; 1416#endif 1417 1418#ifdef FT_CONFIG_OPTION_INCREMENTAL 1419 FT_StreamRec inc_stream; 1420 FT_Data glyph_data; 1421 FT_Bool glyph_data_loaded = 0; 1422#endif 1423 1424 1425 /* some fonts have an incorrect value of `maxComponentDepth', */ 1426 /* thus we allow depth 1 to catch the majority of them */ 1427 if ( recurse_count > 1 && 1428 recurse_count > face->max_profile.maxComponentDepth ) 1429 { 1430 error = FT_THROW( Invalid_Composite ); 1431 goto Exit; 1432 } 1433 1434 /* check glyph index */ 1435 if ( glyph_index >= (FT_UInt)face->root.num_glyphs ) 1436 { 1437 error = FT_THROW( Invalid_Glyph_Index ); 1438 goto Exit; 1439 } 1440 1441 loader->glyph_index = glyph_index; 1442 1443 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1444 { 1445 x_scale = ((TT_Size)loader->size)->metrics.x_scale; 1446 y_scale = ((TT_Size)loader->size)->metrics.y_scale; 1447 } 1448 else 1449 { 1450 x_scale = 0x10000L; 1451 y_scale = 0x10000L; 1452 } 1453 1454 /* Set `offset' to the start of the glyph relative to the start of */ 1455 /* the `glyf' table, and `byte_len' to the length of the glyph in */ 1456 /* bytes. */ 1457 1458#ifdef FT_CONFIG_OPTION_INCREMENTAL 1459 1460 /* If we are loading glyph data via the incremental interface, set */ 1461 /* the loader stream to a memory stream reading the data returned */ 1462 /* by the interface. */ 1463 if ( face->root.internal->incremental_interface ) 1464 { 1465 error = face->root.internal->incremental_interface->funcs->get_glyph_data( 1466 face->root.internal->incremental_interface->object, 1467 glyph_index, &glyph_data ); 1468 if ( error ) 1469 goto Exit; 1470 1471 glyph_data_loaded = 1; 1472 offset = 0; 1473 loader->byte_len = glyph_data.length; 1474 1475 FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) ); 1476 FT_Stream_OpenMemory( &inc_stream, 1477 glyph_data.pointer, glyph_data.length ); 1478 1479 loader->stream = &inc_stream; 1480 } 1481 else 1482 1483#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1484 1485 offset = tt_face_get_location( face, glyph_index, 1486 (FT_UInt*)&loader->byte_len ); 1487 1488 if ( loader->byte_len > 0 ) 1489 { 1490#ifdef FT_CONFIG_OPTION_INCREMENTAL 1491 /* for the incremental interface, `glyf_offset' is always zero */ 1492 if ( !loader->glyf_offset && 1493 !face->root.internal->incremental_interface ) 1494#else 1495 if ( !loader->glyf_offset ) 1496#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1497 { 1498 FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" )); 1499 error = FT_THROW( Invalid_Table ); 1500 goto Exit; 1501 } 1502 1503 error = face->access_glyph_frame( loader, glyph_index, 1504 loader->glyf_offset + offset, 1505 loader->byte_len ); 1506 if ( error ) 1507 goto Exit; 1508 1509 opened_frame = 1; 1510 1511 /* read glyph header first */ 1512 error = face->read_glyph_header( loader ); 1513 if ( error ) 1514 goto Exit; 1515 1516 /* the metrics must be computed after loading the glyph header */ 1517 /* since we need the glyph's `yMax' value in case the vertical */ 1518 /* metrics must be emulated */ 1519 error = tt_get_metrics( loader, glyph_index ); 1520 if ( error ) 1521 goto Exit; 1522 1523 if ( header_only ) 1524 goto Exit; 1525 } 1526 1527 if ( loader->byte_len == 0 || loader->n_contours == 0 ) 1528 { 1529 loader->bbox.xMin = 0; 1530 loader->bbox.xMax = 0; 1531 loader->bbox.yMin = 0; 1532 loader->bbox.yMax = 0; 1533 1534 error = tt_get_metrics( loader, glyph_index ); 1535 if ( error ) 1536 goto Exit; 1537 1538 if ( header_only ) 1539 goto Exit; 1540 1541 /* must initialize points before (possibly) overriding */ 1542 /* glyph metrics from the incremental interface */ 1543 TT_LOADER_SET_PP( loader ); 1544 1545#ifdef FT_CONFIG_OPTION_INCREMENTAL 1546 tt_get_metrics_incr_overrides( loader, glyph_index ); 1547#endif 1548 1549#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1550 1551 if ( ((TT_Face)(loader->face))->doblend ) 1552 { 1553 /* this must be done before scaling */ 1554 FT_Memory memory = loader->face->memory; 1555 1556 1557 error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face), 1558 glyph_index, &deltas, 4 ); 1559 if ( error ) 1560 goto Exit; 1561 1562 loader->pp1.x += deltas[0].x; 1563 loader->pp1.y += deltas[0].y; 1564 loader->pp2.x += deltas[1].x; 1565 loader->pp2.y += deltas[1].y; 1566 1567 loader->pp3.x += deltas[2].x; 1568 loader->pp3.y += deltas[2].y; 1569 loader->pp4.x += deltas[3].x; 1570 loader->pp4.y += deltas[3].y; 1571 1572 FT_FREE( deltas ); 1573 } 1574 1575#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 1576 1577 /* scale phantom points, if necessary; */ 1578 /* they get rounded in `TT_Hint_Glyph' */ 1579 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1580 { 1581 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); 1582 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 1583 /* pp1.y and pp2.y are always zero */ 1584 1585 loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale ); 1586 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale ); 1587 loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale ); 1588 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale ); 1589 } 1590 1591 error = FT_Err_Ok; 1592 goto Exit; 1593 } 1594 1595 /* must initialize phantom points before (possibly) overriding */ 1596 /* glyph metrics from the incremental interface */ 1597 TT_LOADER_SET_PP( loader ); 1598 1599#ifdef FT_CONFIG_OPTION_INCREMENTAL 1600 tt_get_metrics_incr_overrides( loader, glyph_index ); 1601#endif 1602 1603 /***********************************************************************/ 1604 /***********************************************************************/ 1605 /***********************************************************************/ 1606 1607 /* if it is a simple glyph, load it */ 1608 1609 if ( loader->n_contours > 0 ) 1610 { 1611 error = face->read_simple_glyph( loader ); 1612 if ( error ) 1613 goto Exit; 1614 1615 /* all data have been read */ 1616 face->forget_glyph_frame( loader ); 1617 opened_frame = 0; 1618 1619 error = TT_Process_Simple_Glyph( loader ); 1620 if ( error ) 1621 goto Exit; 1622 1623 FT_GlyphLoader_Add( gloader ); 1624 } 1625 1626 /***********************************************************************/ 1627 /***********************************************************************/ 1628 /***********************************************************************/ 1629 1630 /* otherwise, load a composite! */ 1631 else if ( loader->n_contours == -1 ) 1632 { 1633 FT_UInt start_point; 1634 FT_UInt start_contour; 1635 FT_ULong ins_pos; /* position of composite instructions, if any */ 1636 1637 1638 start_point = gloader->base.outline.n_points; 1639 start_contour = gloader->base.outline.n_contours; 1640 1641 /* for each subglyph, read composite header */ 1642 error = face->read_composite_glyph( loader ); 1643 if ( error ) 1644 goto Exit; 1645 1646 /* store the offset of instructions */ 1647 ins_pos = loader->ins_pos; 1648 1649 /* all data we need are read */ 1650 face->forget_glyph_frame( loader ); 1651 opened_frame = 0; 1652 1653#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1654 1655 if ( face->doblend ) 1656 { 1657 FT_Int i, limit; 1658 FT_SubGlyph subglyph; 1659 FT_Memory memory = face->root.memory; 1660 1661 1662 /* this provides additional offsets */ 1663 /* for each component's translation */ 1664 1665 if ( ( error = TT_Vary_Get_Glyph_Deltas( 1666 face, 1667 glyph_index, 1668 &deltas, 1669 gloader->current.num_subglyphs + 4 ) ) != 0 ) 1670 goto Exit; 1671 1672 subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs; 1673 limit = gloader->current.num_subglyphs; 1674 1675 for ( i = 0; i < limit; ++i, ++subglyph ) 1676 { 1677 if ( subglyph->flags & ARGS_ARE_XY_VALUES ) 1678 { 1679 /* XXX: overflow check for subglyph->{arg1,arg2}. */ 1680 /* deltas[i].{x,y} must be within signed 16-bit, */ 1681 /* but the restriction of summed delta is not clear */ 1682 subglyph->arg1 += (FT_Int16)deltas[i].x; 1683 subglyph->arg2 += (FT_Int16)deltas[i].y; 1684 } 1685 } 1686 1687 loader->pp1.x += deltas[i + 0].x; 1688 loader->pp1.y += deltas[i + 0].y; 1689 loader->pp2.x += deltas[i + 1].x; 1690 loader->pp2.y += deltas[i + 1].y; 1691 1692 loader->pp3.x += deltas[i + 2].x; 1693 loader->pp3.y += deltas[i + 2].y; 1694 loader->pp4.x += deltas[i + 3].x; 1695 loader->pp4.y += deltas[i + 3].y; 1696 1697 FT_FREE( deltas ); 1698 } 1699 1700#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 1701 1702 /* scale phantom points, if necessary; */ 1703 /* they get rounded in `TT_Hint_Glyph' */ 1704 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1705 { 1706 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); 1707 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 1708 /* pp1.y and pp2.y are always zero */ 1709 1710 loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale ); 1711 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale ); 1712 loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale ); 1713 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale ); 1714 } 1715 1716 /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */ 1717 /* `as is' in the glyph slot (the client application will be */ 1718 /* responsible for interpreting these data)... */ 1719 if ( loader->load_flags & FT_LOAD_NO_RECURSE ) 1720 { 1721 FT_GlyphLoader_Add( gloader ); 1722 loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE; 1723 1724 goto Exit; 1725 } 1726 1727 /*********************************************************************/ 1728 /*********************************************************************/ 1729 /*********************************************************************/ 1730 1731 { 1732 FT_UInt n, num_base_points; 1733 FT_SubGlyph subglyph = 0; 1734 1735 FT_UInt num_points = start_point; 1736 FT_UInt num_subglyphs = gloader->current.num_subglyphs; 1737 FT_UInt num_base_subgs = gloader->base.num_subglyphs; 1738 1739 FT_Stream old_stream = loader->stream; 1740 FT_Int old_byte_len = loader->byte_len; 1741 1742 1743 FT_GlyphLoader_Add( gloader ); 1744 1745 /* read each subglyph independently */ 1746 for ( n = 0; n < num_subglyphs; n++ ) 1747 { 1748 FT_Vector pp[4]; 1749 1750 1751 /* Each time we call load_truetype_glyph in this loop, the */ 1752 /* value of `gloader.base.subglyphs' can change due to table */ 1753 /* reallocations. We thus need to recompute the subglyph */ 1754 /* pointer on each iteration. */ 1755 subglyph = gloader->base.subglyphs + num_base_subgs + n; 1756 1757 pp[0] = loader->pp1; 1758 pp[1] = loader->pp2; 1759 pp[2] = loader->pp3; 1760 pp[3] = loader->pp4; 1761 1762 num_base_points = gloader->base.outline.n_points; 1763 1764 error = load_truetype_glyph( loader, subglyph->index, 1765 recurse_count + 1, FALSE ); 1766 if ( error ) 1767 goto Exit; 1768 1769 /* restore subglyph pointer */ 1770 subglyph = gloader->base.subglyphs + num_base_subgs + n; 1771 1772 /* restore phantom points if necessary */ 1773 if ( !( subglyph->flags & USE_MY_METRICS ) ) 1774 { 1775 loader->pp1 = pp[0]; 1776 loader->pp2 = pp[1]; 1777 loader->pp3 = pp[2]; 1778 loader->pp4 = pp[3]; 1779 } 1780 1781 num_points = gloader->base.outline.n_points; 1782 1783 if ( num_points == num_base_points ) 1784 continue; 1785 1786 /* gloader->base.outline consists of three parts: */ 1787 /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */ 1788 /* */ 1789 /* (1): exists from the beginning */ 1790 /* (2): components that have been loaded so far */ 1791 /* (3): the newly loaded component */ 1792 TT_Process_Composite_Component( loader, subglyph, start_point, 1793 num_base_points ); 1794 } 1795 1796 loader->stream = old_stream; 1797 loader->byte_len = old_byte_len; 1798 1799 /* process the glyph */ 1800 loader->ins_pos = ins_pos; 1801 if ( IS_HINTED( loader->load_flags ) && 1802 1803#ifdef TT_USE_BYTECODE_INTERPRETER 1804 1805 subglyph->flags & WE_HAVE_INSTR && 1806 1807#endif 1808 1809 num_points > start_point ) 1810 TT_Process_Composite_Glyph( loader, start_point, start_contour ); 1811 1812 } 1813 } 1814 else 1815 { 1816 /* invalid composite count (negative but not -1) */ 1817 error = FT_THROW( Invalid_Outline ); 1818 goto Exit; 1819 } 1820 1821 /***********************************************************************/ 1822 /***********************************************************************/ 1823 /***********************************************************************/ 1824 1825 Exit: 1826 1827 if ( opened_frame ) 1828 face->forget_glyph_frame( loader ); 1829 1830#ifdef FT_CONFIG_OPTION_INCREMENTAL 1831 1832 if ( glyph_data_loaded ) 1833 face->root.internal->incremental_interface->funcs->free_glyph_data( 1834 face->root.internal->incremental_interface->object, 1835 &glyph_data ); 1836 1837#endif 1838 1839 return error; 1840 } 1841 1842 1843 static FT_Error 1844 compute_glyph_metrics( TT_Loader loader, 1845 FT_UInt glyph_index ) 1846 { 1847 TT_Face face = (TT_Face)loader->face; 1848#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 1849 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 1850#endif 1851 1852 FT_BBox bbox; 1853 FT_Fixed y_scale; 1854 TT_GlyphSlot glyph = loader->glyph; 1855 TT_Size size = (TT_Size)loader->size; 1856 1857 1858 y_scale = 0x10000L; 1859 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1860 y_scale = size->root.metrics.y_scale; 1861 1862 if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE ) 1863 FT_Outline_Get_CBox( &glyph->outline, &bbox ); 1864 else 1865 bbox = loader->bbox; 1866 1867 /* get the device-independent horizontal advance; it is scaled later */ 1868 /* by the base layer. */ 1869 glyph->linearHoriAdvance = loader->linear; 1870 1871 glyph->metrics.horiBearingX = bbox.xMin; 1872 glyph->metrics.horiBearingY = bbox.yMax; 1873 glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; 1874 1875 /* adjust advance width to the value contained in the hdmx table */ 1876 if ( !face->postscript.isFixedPitch && 1877 IS_HINTED( loader->load_flags ) ) 1878 { 1879 FT_Byte* widthp; 1880 1881 1882 widthp = tt_face_get_device_metrics( face, 1883 size->root.metrics.x_ppem, 1884 glyph_index ); 1885 1886#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 1887 1888 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 1889 { 1890 FT_Bool ignore_x_mode; 1891 1892 1893 ignore_x_mode = FT_BOOL( FT_LOAD_TARGET_MODE( loader->load_flags ) != 1894 FT_RENDER_MODE_MONO ); 1895 1896 if ( widthp && 1897 ( ( ignore_x_mode && loader->exec->compatible_widths ) || 1898 !ignore_x_mode || 1899 SPH_OPTION_BITMAP_WIDTHS ) ) 1900 glyph->metrics.horiAdvance = *widthp << 6; 1901 } 1902 else 1903 1904#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 1905 1906 { 1907 if ( widthp ) 1908 glyph->metrics.horiAdvance = *widthp << 6; 1909 } 1910 } 1911 1912 /* set glyph dimensions */ 1913 glyph->metrics.width = bbox.xMax - bbox.xMin; 1914 glyph->metrics.height = bbox.yMax - bbox.yMin; 1915 1916 /* Now take care of vertical metrics. In the case where there is */ 1917 /* no vertical information within the font (relatively common), */ 1918 /* create some metrics manually */ 1919 { 1920 FT_Pos top; /* scaled vertical top side bearing */ 1921 FT_Pos advance; /* scaled vertical advance height */ 1922 1923 1924 /* Get the unscaled top bearing and advance height. */ 1925 if ( face->vertical_info && 1926 face->vertical.number_Of_VMetrics > 0 ) 1927 { 1928 top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax, 1929 y_scale ); 1930 1931 if ( loader->pp3.y <= loader->pp4.y ) 1932 advance = 0; 1933 else 1934 advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y, 1935 y_scale ); 1936 } 1937 else 1938 { 1939 FT_Pos height; 1940 1941 1942 /* XXX Compute top side bearing and advance height in */ 1943 /* Get_VMetrics instead of here. */ 1944 1945 /* NOTE: The OS/2 values are the only `portable' ones, */ 1946 /* which is why we use them, if there is an OS/2 */ 1947 /* table in the font. Otherwise, we use the */ 1948 /* values defined in the horizontal header. */ 1949 1950 height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin, 1951 y_scale ); 1952 if ( face->os2.version != 0xFFFFU ) 1953 advance = (FT_Pos)( face->os2.sTypoAscender - 1954 face->os2.sTypoDescender ); 1955 else 1956 advance = (FT_Pos)( face->horizontal.Ascender - 1957 face->horizontal.Descender ); 1958 1959 top = ( advance - height ) / 2; 1960 } 1961 1962#ifdef FT_CONFIG_OPTION_INCREMENTAL 1963 { 1964 FT_Incremental_InterfaceRec* incr; 1965 FT_Incremental_MetricsRec metrics; 1966 FT_Error error; 1967 1968 1969 incr = face->root.internal->incremental_interface; 1970 1971 /* If this is an incrementally loaded font see if there are */ 1972 /* overriding metrics for this glyph. */ 1973 if ( incr && incr->funcs->get_glyph_metrics ) 1974 { 1975 metrics.bearing_x = 0; 1976 metrics.bearing_y = top; 1977 metrics.advance = advance; 1978 1979 error = incr->funcs->get_glyph_metrics( incr->object, 1980 glyph_index, 1981 TRUE, 1982 &metrics ); 1983 if ( error ) 1984 return error; 1985 1986 top = metrics.bearing_y; 1987 advance = metrics.advance; 1988 } 1989 } 1990 1991 /* GWW: Do vertical metrics get loaded incrementally too? */ 1992 1993#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1994 1995 glyph->linearVertAdvance = advance; 1996 1997 /* scale the metrics */ 1998 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 1999 { 2000 top = FT_MulFix( top, y_scale ); 2001 advance = FT_MulFix( advance, y_scale ); 2002 } 2003 2004 /* XXX: for now, we have no better algorithm for the lsb, but it */ 2005 /* should work fine. */ 2006 /* */ 2007 glyph->metrics.vertBearingX = glyph->metrics.horiBearingX - 2008 glyph->metrics.horiAdvance / 2; 2009 glyph->metrics.vertBearingY = top; 2010 glyph->metrics.vertAdvance = advance; 2011 } 2012 2013 return 0; 2014 } 2015 2016 2017#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 2018 2019 static FT_Error 2020 load_sbit_image( TT_Size size, 2021 TT_GlyphSlot glyph, 2022 FT_UInt glyph_index, 2023 FT_Int32 load_flags ) 2024 { 2025 TT_Face face; 2026 SFNT_Service sfnt; 2027 FT_Stream stream; 2028 FT_Error error; 2029 TT_SBit_MetricsRec metrics; 2030 2031 2032 face = (TT_Face)glyph->face; 2033 sfnt = (SFNT_Service)face->sfnt; 2034 stream = face->root.stream; 2035 2036 error = sfnt->load_sbit_image( face, 2037 size->strike_index, 2038 glyph_index, 2039 (FT_Int)load_flags, 2040 stream, 2041 &glyph->bitmap, 2042 &metrics ); 2043 if ( !error ) 2044 { 2045 glyph->outline.n_points = 0; 2046 glyph->outline.n_contours = 0; 2047 2048 glyph->metrics.width = (FT_Pos)metrics.width << 6; 2049 glyph->metrics.height = (FT_Pos)metrics.height << 6; 2050 2051 glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6; 2052 glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6; 2053 glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6; 2054 2055 glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6; 2056 glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6; 2057 glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6; 2058 2059 glyph->format = FT_GLYPH_FORMAT_BITMAP; 2060 2061 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 2062 { 2063 glyph->bitmap_left = metrics.vertBearingX; 2064 glyph->bitmap_top = metrics.vertBearingY; 2065 } 2066 else 2067 { 2068 glyph->bitmap_left = metrics.horiBearingX; 2069 glyph->bitmap_top = metrics.horiBearingY; 2070 } 2071 } 2072 2073 return error; 2074 } 2075 2076#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 2077 2078 2079 static FT_Error 2080 tt_loader_init( TT_Loader loader, 2081 TT_Size size, 2082 TT_GlyphSlot glyph, 2083 FT_Int32 load_flags, 2084 FT_Bool glyf_table_only ) 2085 { 2086 TT_Face face; 2087 FT_Stream stream; 2088#ifdef TT_USE_BYTECODE_INTERPRETER 2089 FT_Bool pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC ); 2090#endif 2091 2092 2093 face = (TT_Face)glyph->face; 2094 stream = face->root.stream; 2095 2096 FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) ); 2097 2098#ifdef TT_USE_BYTECODE_INTERPRETER 2099 2100 /* load execution context */ 2101 if ( IS_HINTED( load_flags ) && !glyf_table_only ) 2102 { 2103 TT_ExecContext exec; 2104 FT_Bool grayscale; 2105 2106#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2107 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 2108 2109 FT_Bool subpixel = FALSE; 2110 2111#if 0 2112 /* not used yet */ 2113 FT_Bool compatible_widths; 2114 FT_Bool symmetrical_smoothing; 2115 FT_Bool bgr; 2116 FT_Bool subpixel_positioned; 2117#endif 2118#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 2119 2120 FT_Bool reexecute = FALSE; 2121 2122 2123 if ( size->bytecode_ready < 0 || size->cvt_ready < 0 ) 2124 { 2125 FT_Error error = tt_size_ready_bytecode( size, pedantic ); 2126 2127 2128 if ( error ) 2129 return error; 2130 } 2131 else if ( size->bytecode_ready ) 2132 return size->bytecode_ready; 2133 else if ( size->cvt_ready ) 2134 return size->cvt_ready; 2135 2136 /* query new execution context */ 2137 exec = size->debug ? size->context 2138 : ( (TT_Driver)FT_FACE_DRIVER( face ) )->context; 2139 if ( !exec ) 2140 return FT_THROW( Could_Not_Find_Context ); 2141 2142#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2143 2144 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 2145 { 2146 subpixel = FT_BOOL( ( FT_LOAD_TARGET_MODE( load_flags ) != 2147 FT_RENDER_MODE_MONO ) && 2148 SPH_OPTION_SET_SUBPIXEL ); 2149 2150 if ( subpixel ) 2151 grayscale = FALSE; 2152 else if ( SPH_OPTION_SET_GRAYSCALE ) 2153 { 2154 grayscale = TRUE; 2155 subpixel = FALSE; 2156 } 2157 else 2158 grayscale = FALSE; 2159 2160 if ( FT_IS_TRICKY( glyph->face ) ) 2161 subpixel = FALSE; 2162 2163 exec->ignore_x_mode = subpixel || grayscale; 2164 exec->rasterizer_version = SPH_OPTION_SET_RASTERIZER_VERSION; 2165 if ( exec->sph_tweak_flags & SPH_TWEAK_RASTERIZER_35 ) 2166 exec->rasterizer_version = TT_INTERPRETER_VERSION_35; 2167 2168#if 1 2169 exec->compatible_widths = SPH_OPTION_SET_COMPATIBLE_WIDTHS; 2170 exec->symmetrical_smoothing = FALSE; 2171 exec->bgr = FALSE; 2172 exec->subpixel_positioned = TRUE; 2173#else /* 0 */ 2174 exec->compatible_widths = 2175 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2176 TT_LOAD_COMPATIBLE_WIDTHS ); 2177 exec->symmetrical_smoothing = 2178 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2179 TT_LOAD_SYMMETRICAL_SMOOTHING ); 2180 exec->bgr = 2181 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2182 TT_LOAD_BGR ); 2183 exec->subpixel_positioned = 2184 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2185 TT_LOAD_SUBPIXEL_POSITIONED ); 2186#endif /* 0 */ 2187 2188 } 2189 else 2190 2191#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 2192 2193 { 2194 grayscale = FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2195 FT_RENDER_MODE_MONO ); 2196 } 2197 2198 TT_Load_Context( exec, face, size ); 2199 2200#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2201 2202 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 2203 { 2204 /* a change from mono to subpixel rendering (and vice versa) */ 2205 /* requires a re-execution of the CVT program */ 2206 if ( subpixel != exec->subpixel ) 2207 { 2208 FT_TRACE4(( "tt_loader_init: subpixel hinting change," 2209 " re-executing `prep' table\n" )); 2210 2211 exec->subpixel = subpixel; 2212 reexecute = TRUE; 2213 } 2214 2215 /* a change from mono to grayscale rendering (and vice versa) */ 2216 /* requires a re-execution of the CVT program */ 2217 if ( grayscale != exec->grayscale ) 2218 { 2219 FT_TRACE4(( "tt_loader_init: grayscale hinting change," 2220 " re-executing `prep' table\n" )); 2221 2222 exec->grayscale = grayscale; 2223 reexecute = TRUE; 2224 } 2225 } 2226 else 2227 2228#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 2229 2230 { 2231 /* a change from mono to grayscale rendering (and vice versa) */ 2232 /* requires a re-execution of the CVT program */ 2233 if ( grayscale != exec->grayscale ) 2234 { 2235 FT_TRACE4(( "tt_loader_init: grayscale change," 2236 " re-executing `prep' table\n" )); 2237 2238 exec->grayscale = grayscale; 2239 reexecute = TRUE; 2240 } 2241 } 2242 2243 if ( reexecute ) 2244 { 2245 FT_UInt i; 2246 FT_Error error; 2247 2248 2249 for ( i = 0; i < size->cvt_size; i++ ) 2250 size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale ); 2251 error = tt_size_run_prep( size, pedantic ); 2252 if ( error ) 2253 return error; 2254 } 2255 2256 /* see whether the cvt program has disabled hinting */ 2257 if ( exec->GS.instruct_control & 1 ) 2258 load_flags |= FT_LOAD_NO_HINTING; 2259 2260 /* load default graphics state -- if needed */ 2261 if ( exec->GS.instruct_control & 2 ) 2262 exec->GS = tt_default_graphics_state; 2263 2264 exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC ); 2265 loader->exec = exec; 2266 loader->instructions = exec->glyphIns; 2267 } 2268 2269#endif /* TT_USE_BYTECODE_INTERPRETER */ 2270 2271 /* seek to the beginning of the glyph table -- for Type 42 fonts */ 2272 /* the table might be accessed from a Postscript stream or something */ 2273 /* else... */ 2274 2275#ifdef FT_CONFIG_OPTION_INCREMENTAL 2276 2277 if ( face->root.internal->incremental_interface ) 2278 loader->glyf_offset = 0; 2279 else 2280 2281#endif 2282 2283 { 2284 FT_Error error = face->goto_table( face, TTAG_glyf, stream, 0 ); 2285 2286 2287 if ( FT_ERR_EQ( error, Table_Missing ) ) 2288 loader->glyf_offset = 0; 2289 else if ( error ) 2290 { 2291 FT_ERROR(( "tt_loader_init: could not access glyph table\n" )); 2292 return error; 2293 } 2294 else 2295 loader->glyf_offset = FT_STREAM_POS(); 2296 } 2297 2298 /* get face's glyph loader */ 2299 if ( !glyf_table_only ) 2300 { 2301 FT_GlyphLoader gloader = glyph->internal->loader; 2302 2303 2304 FT_GlyphLoader_Rewind( gloader ); 2305 loader->gloader = gloader; 2306 } 2307 2308 loader->load_flags = load_flags; 2309 2310 loader->face = (FT_Face)face; 2311 loader->size = (FT_Size)size; 2312 loader->glyph = (FT_GlyphSlot)glyph; 2313 loader->stream = stream; 2314 2315 return FT_Err_Ok; 2316 } 2317 2318 2319 /*************************************************************************/ 2320 /* */ 2321 /* <Function> */ 2322 /* TT_Load_Glyph */ 2323 /* */ 2324 /* <Description> */ 2325 /* A function used to load a single glyph within a given glyph slot, */ 2326 /* for a given size. */ 2327 /* */ 2328 /* <Input> */ 2329 /* glyph :: A handle to a target slot object where the glyph */ 2330 /* will be loaded. */ 2331 /* */ 2332 /* size :: A handle to the source face size at which the glyph */ 2333 /* must be scaled/loaded. */ 2334 /* */ 2335 /* glyph_index :: The index of the glyph in the font file. */ 2336 /* */ 2337 /* load_flags :: A flag indicating what to load for this glyph. The */ 2338 /* FT_LOAD_XXX constants can be used to control the */ 2339 /* glyph loading process (e.g., whether the outline */ 2340 /* should be scaled, whether to load bitmaps or not, */ 2341 /* whether to hint the outline, etc). */ 2342 /* */ 2343 /* <Return> */ 2344 /* FreeType error code. 0 means success. */ 2345 /* */ 2346 FT_LOCAL_DEF( FT_Error ) 2347 TT_Load_Glyph( TT_Size size, 2348 TT_GlyphSlot glyph, 2349 FT_UInt glyph_index, 2350 FT_Int32 load_flags ) 2351 { 2352 FT_Error error; 2353 TT_LoaderRec loader; 2354 2355 2356 FT_TRACE1(( "TT_Load_Glyph: glyph index %d\n", glyph_index )); 2357 2358#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 2359 2360 /* try to load embedded bitmap if any */ 2361 /* */ 2362 /* XXX: The convention should be emphasized in */ 2363 /* the documents because it can be confusing. */ 2364 if ( size->strike_index != 0xFFFFFFFFUL && 2365 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 ) 2366 { 2367 error = load_sbit_image( size, glyph, glyph_index, load_flags ); 2368 if ( !error ) 2369 { 2370 if ( FT_IS_SCALABLE( glyph->face ) ) 2371 { 2372 /* for the bbox we need the header only */ 2373 (void)tt_loader_init( &loader, size, glyph, load_flags, TRUE ); 2374 (void)load_truetype_glyph( &loader, glyph_index, 0, TRUE ); 2375 glyph->linearHoriAdvance = loader.linear; 2376 glyph->linearVertAdvance = loader.top_bearing + loader.bbox.yMax - 2377 loader.vadvance; 2378 2379 /* sanity check: if `horiAdvance' in the sbit metric */ 2380 /* structure isn't set, use `linearHoriAdvance' */ 2381 if ( !glyph->metrics.horiAdvance && glyph->linearHoriAdvance ) 2382 glyph->metrics.horiAdvance = 2383 FT_MulFix( glyph->linearHoriAdvance, 2384 size->root.metrics.x_scale ); 2385 } 2386 2387 return FT_Err_Ok; 2388 } 2389 } 2390 2391#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 2392 2393 /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */ 2394 if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid ) 2395 return FT_THROW( Invalid_Size_Handle ); 2396 2397 if ( load_flags & FT_LOAD_SBITS_ONLY ) 2398 return FT_THROW( Invalid_Argument ); 2399 2400 error = tt_loader_init( &loader, size, glyph, load_flags, FALSE ); 2401 if ( error ) 2402 return error; 2403 2404 glyph->format = FT_GLYPH_FORMAT_OUTLINE; 2405 glyph->num_subglyphs = 0; 2406 glyph->outline.flags = 0; 2407 2408 /* main loading loop */ 2409 error = load_truetype_glyph( &loader, glyph_index, 0, FALSE ); 2410 if ( !error ) 2411 { 2412 if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE ) 2413 { 2414 glyph->num_subglyphs = loader.gloader->base.num_subglyphs; 2415 glyph->subglyphs = loader.gloader->base.subglyphs; 2416 } 2417 else 2418 { 2419 glyph->outline = loader.gloader->base.outline; 2420 glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS; 2421 2422 /* Translate array so that (0,0) is the glyph's origin. Note */ 2423 /* that this behaviour is independent on the value of bit 1 of */ 2424 /* the `flags' field in the `head' table -- at least major */ 2425 /* applications like Acroread indicate that. */ 2426 if ( loader.pp1.x ) 2427 FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 ); 2428 } 2429 2430#ifdef TT_USE_BYTECODE_INTERPRETER 2431 2432 if ( IS_HINTED( load_flags ) ) 2433 { 2434 if ( loader.exec->GS.scan_control ) 2435 { 2436 /* convert scan conversion mode to FT_OUTLINE_XXX flags */ 2437 switch ( loader.exec->GS.scan_type ) 2438 { 2439 case 0: /* simple drop-outs including stubs */ 2440 glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS; 2441 break; 2442 case 1: /* simple drop-outs excluding stubs */ 2443 /* nothing; it's the default rendering mode */ 2444 break; 2445 case 4: /* smart drop-outs including stubs */ 2446 glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS | 2447 FT_OUTLINE_INCLUDE_STUBS; 2448 break; 2449 case 5: /* smart drop-outs excluding stubs */ 2450 glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS; 2451 break; 2452 2453 default: /* no drop-out control */ 2454 glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS; 2455 break; 2456 } 2457 } 2458 else 2459 glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS; 2460 } 2461 2462#endif /* TT_USE_BYTECODE_INTERPRETER */ 2463 2464 compute_glyph_metrics( &loader, glyph_index ); 2465 } 2466 2467 /* Set the `high precision' bit flag. */ 2468 /* This is _critical_ to get correct output for monochrome */ 2469 /* TrueType glyphs at all sizes using the bytecode interpreter. */ 2470 /* */ 2471 if ( !( load_flags & FT_LOAD_NO_SCALE ) && 2472 size->root.metrics.y_ppem < 24 ) 2473 glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION; 2474 2475 return error; 2476 } 2477 2478 2479/* END */ 2480