PageRenderTime 190ms CodeModel.GetById 114ms app.highlight 65ms RepoModel.GetById 0ms app.codeStats 1ms

/src/compiler/android-ndk/jni/freetype/src/truetype/ttgload.c

http://ftk.googlecode.com/
C | 1996 lines | 1269 code | 439 blank | 288 comment | 202 complexity | 7ee15b458c8604b94b0d46fe4f5cdc39 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/***************************************************************************/
   2/*                                                                         */
   3/*  ttgload.c                                                              */
   4/*                                                                         */
   5/*    TrueType Glyph Loader (body).                                        */
   6/*                                                                         */
   7/*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,   */
   8/*            2010 by                                                      */
   9/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
  10/*                                                                         */
  11/*  This file is part of the FreeType project, and may only be used,       */
  12/*  modified, and distributed under the terms of the FreeType project      */
  13/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
  14/*  this file you indicate that you have read the license and              */
  15/*  understand and accept it fully.                                        */
  16/*                                                                         */
  17/***************************************************************************/
  18
  19
  20#include <ft2build.h>
  21#include FT_INTERNAL_DEBUG_H
  22#include FT_INTERNAL_CALC_H
  23#include FT_INTERNAL_STREAM_H
  24#include FT_INTERNAL_SFNT_H
  25#include FT_TRUETYPE_TAGS_H
  26#include FT_OUTLINE_H
  27
  28#include "ttgload.h"
  29#include "ttpload.h"
  30
  31#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  32#include "ttgxvar.h"
  33#endif
  34
  35#include "tterrors.h"
  36
  37
  38  /*************************************************************************/
  39  /*                                                                       */
  40  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  41  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  42  /* messages during execution.                                            */
  43  /*                                                                       */
  44#undef  FT_COMPONENT
  45#define FT_COMPONENT  trace_ttgload
  46
  47
  48  /*************************************************************************/
  49  /*                                                                       */
  50  /* Composite font flags.                                                 */
  51  /*                                                                       */
  52#define ARGS_ARE_WORDS             0x0001
  53#define ARGS_ARE_XY_VALUES         0x0002
  54#define ROUND_XY_TO_GRID           0x0004
  55#define WE_HAVE_A_SCALE            0x0008
  56/* reserved                        0x0010 */
  57#define MORE_COMPONENTS            0x0020
  58#define WE_HAVE_AN_XY_SCALE        0x0040
  59#define WE_HAVE_A_2X2              0x0080
  60#define WE_HAVE_INSTR              0x0100
  61#define USE_MY_METRICS             0x0200
  62#define OVERLAP_COMPOUND           0x0400
  63#define SCALED_COMPONENT_OFFSET    0x0800
  64#define UNSCALED_COMPONENT_OFFSET  0x1000
  65
  66
  67  /*************************************************************************/
  68  /*                                                                       */
  69  /* Returns the horizontal metrics in font units for a given glyph.  If   */
  70  /* `check' is true, take care of monospaced fonts by returning the       */
  71  /* advance width maximum.                                                */
  72  /*                                                                       */
  73  FT_LOCAL_DEF( void )
  74  TT_Get_HMetrics( TT_Face     face,
  75                   FT_UInt     idx,
  76                   FT_Bool     check,
  77                   FT_Short*   lsb,
  78                   FT_UShort*  aw )
  79  {
  80    ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw );
  81
  82    if ( check && face->postscript.isFixedPitch )
  83      *aw = face->horizontal.advance_Width_Max;
  84
  85    FT_TRACE5(( "  advance width (font units): %d\n", *aw ));
  86    FT_TRACE5(( "  left side bearing (font units): %d\n", *lsb ));
  87  }
  88
  89
  90  /*************************************************************************/
  91  /*                                                                       */
  92  /* Returns the vertical metrics in font units for a given glyph.         */
  93  /* Greg Hitchcock from Microsoft told us that if there were no `vmtx'    */
  94  /* table, typoAscender/Descender from the `OS/2' table would be used     */
  95  /* instead, and if there were no `OS/2' table, use ascender/descender    */
  96  /* from the `hhea' table.  But that is not what Microsoft's rasterizer   */
  97  /* apparently does: It uses the ppem value as the advance height, and    */
  98  /* sets the top side bearing to be zero.                                 */
  99  /*                                                                       */
 100  /* The monospace `check' is probably not meaningful here, but we leave   */
 101  /* it in for a consistent interface.                                     */
 102  /*                                                                       */
 103  FT_LOCAL_DEF( void )
 104  TT_Get_VMetrics( TT_Face     face,
 105                   FT_UInt     idx,
 106                   FT_Bool     check,
 107                   FT_Short*   tsb,
 108                   FT_UShort*  ah )
 109  {
 110    FT_UNUSED( check );
 111
 112    if ( face->vertical_info )
 113      ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah );
 114
 115#if 1             /* Empirically determined, at variance with what MS said */
 116
 117    else
 118    {
 119      *tsb = 0;
 120      *ah  = face->root.units_per_EM;
 121    }
 122
 123#else      /* This is what MS said to do.  It isn't what they do, however. */
 124
 125    else if ( face->os2.version != 0xFFFFU )
 126    {
 127      *tsb = face->os2.sTypoAscender;
 128      *ah  = face->os2.sTypoAscender - face->os2.sTypoDescender;
 129    }
 130    else
 131    {
 132      *tsb = face->horizontal.Ascender;
 133      *ah  = face->horizontal.Ascender - face->horizontal.Descender;
 134    }
 135
 136#endif
 137
 138    FT_TRACE5(( "  advance height (font units): %d\n", *ah ));
 139    FT_TRACE5(( "  top side bearing (font units): %d\n", *tsb ));
 140  }
 141
 142
 143  static void
 144  tt_get_metrics( TT_Loader  loader,
 145                  FT_UInt    glyph_index )
 146  {
 147    TT_Face  face = (TT_Face)loader->face;
 148
 149    FT_Short   left_bearing = 0, top_bearing = 0;
 150    FT_UShort  advance_width = 0, advance_height = 0;
 151
 152
 153    TT_Get_HMetrics( face, glyph_index,
 154                     (FT_Bool)!( loader->load_flags &
 155                                 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ),
 156                     &left_bearing,
 157                     &advance_width );
 158    TT_Get_VMetrics( face, glyph_index,
 159                     (FT_Bool)!( loader->load_flags &
 160                                 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ),
 161                     &top_bearing,
 162                     &advance_height );
 163
 164#ifdef FT_CONFIG_OPTION_INCREMENTAL
 165
 166    /* If this is an incrementally loaded font check whether there are */
 167    /* overriding metrics for this glyph.                              */
 168    if ( face->root.internal->incremental_interface                           &&
 169         face->root.internal->incremental_interface->funcs->get_glyph_metrics )
 170    {
 171      FT_Incremental_MetricsRec  metrics;
 172      FT_Error                   error;
 173
 174
 175      metrics.bearing_x = left_bearing;
 176      metrics.bearing_y = 0;
 177      metrics.advance   = advance_width;
 178      metrics.advance_v = 0;
 179
 180      error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
 181                face->root.internal->incremental_interface->object,
 182                glyph_index, FALSE, &metrics );
 183      if ( error )
 184        goto Exit;
 185
 186      left_bearing  = (FT_Short)metrics.bearing_x;
 187      advance_width = (FT_UShort)metrics.advance;
 188
 189#if 0
 190
 191      /* GWW: Do I do the same for vertical metrics? */
 192      metrics.bearing_x = 0;
 193      metrics.bearing_y = top_bearing;
 194      metrics.advance   = advance_height;
 195
 196      error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
 197                face->root.internal->incremental_interface->object,
 198                glyph_index, TRUE, &metrics );
 199      if ( error )
 200        goto Exit;
 201
 202      top_bearing    = (FT_Short)metrics.bearing_y;
 203      advance_height = (FT_UShort)metrics.advance;
 204
 205#endif /* 0 */
 206
 207    }
 208
 209  Exit:
 210
 211#endif /* FT_CONFIG_OPTION_INCREMENTAL */
 212
 213    loader->left_bearing = left_bearing;
 214    loader->advance      = advance_width;
 215    loader->top_bearing  = top_bearing;
 216    loader->vadvance     = advance_height;
 217
 218    if ( !loader->linear_def )
 219    {
 220      loader->linear_def = 1;
 221      loader->linear     = advance_width;
 222    }
 223  }
 224
 225
 226  /*************************************************************************/
 227  /*                                                                       */
 228  /* Translates an array of coordinates.                                   */
 229  /*                                                                       */
 230  static void
 231  translate_array( FT_UInt     n,
 232                   FT_Vector*  coords,
 233                   FT_Pos      delta_x,
 234                   FT_Pos      delta_y )
 235  {
 236    FT_UInt  k;
 237
 238
 239    if ( delta_x )
 240      for ( k = 0; k < n; k++ )
 241        coords[k].x += delta_x;
 242
 243    if ( delta_y )
 244      for ( k = 0; k < n; k++ )
 245        coords[k].y += delta_y;
 246  }
 247
 248
 249#undef  IS_HINTED
 250#define IS_HINTED( flags )  ( ( flags & FT_LOAD_NO_HINTING ) == 0 )
 251
 252
 253  /*************************************************************************/
 254  /*                                                                       */
 255  /* The following functions are used by default with TrueType fonts.      */
 256  /* However, they can be replaced by alternatives if we need to support   */
 257  /* TrueType-compressed formats (like MicroType) in the future.           */
 258  /*                                                                       */
 259  /*************************************************************************/
 260
 261  FT_CALLBACK_DEF( FT_Error )
 262  TT_Access_Glyph_Frame( TT_Loader  loader,
 263                         FT_UInt    glyph_index,
 264                         FT_ULong   offset,
 265                         FT_UInt    byte_count )
 266  {
 267    FT_Error   error;
 268    FT_Stream  stream = loader->stream;
 269
 270    /* for non-debug mode */
 271    FT_UNUSED( glyph_index );
 272
 273
 274    FT_TRACE5(( "Glyph %ld\n", glyph_index ));
 275
 276    /* the following line sets the `error' variable through macros! */
 277    if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) )
 278      return error;
 279
 280    loader->cursor = stream->cursor;
 281    loader->limit  = stream->limit;
 282
 283    return TT_Err_Ok;
 284  }
 285
 286
 287  FT_CALLBACK_DEF( void )
 288  TT_Forget_Glyph_Frame( TT_Loader  loader )
 289  {
 290    FT_Stream  stream = loader->stream;
 291
 292
 293    FT_FRAME_EXIT();
 294  }
 295
 296
 297  FT_CALLBACK_DEF( FT_Error )
 298  TT_Load_Glyph_Header( TT_Loader  loader )
 299  {
 300    FT_Byte*  p     = loader->cursor;
 301    FT_Byte*  limit = loader->limit;
 302
 303
 304    if ( p + 10 > limit )
 305      return TT_Err_Invalid_Outline;
 306
 307    loader->n_contours = FT_NEXT_SHORT( p );
 308
 309    loader->bbox.xMin = FT_NEXT_SHORT( p );
 310    loader->bbox.yMin = FT_NEXT_SHORT( p );
 311    loader->bbox.xMax = FT_NEXT_SHORT( p );
 312    loader->bbox.yMax = FT_NEXT_SHORT( p );
 313
 314    FT_TRACE5(( "  # of contours: %d\n", loader->n_contours ));
 315    FT_TRACE5(( "  xMin: %4d  xMax: %4d\n", loader->bbox.xMin,
 316                                            loader->bbox.xMax ));
 317    FT_TRACE5(( "  yMin: %4d  yMax: %4d\n", loader->bbox.yMin,
 318                                            loader->bbox.yMax ));
 319    loader->cursor = p;
 320
 321    return TT_Err_Ok;
 322  }
 323
 324
 325  FT_CALLBACK_DEF( FT_Error )
 326  TT_Load_Simple_Glyph( TT_Loader  load )
 327  {
 328    FT_Error        error;
 329    FT_Byte*        p          = load->cursor;
 330    FT_Byte*        limit      = load->limit;
 331    FT_GlyphLoader  gloader    = load->gloader;
 332    FT_Int          n_contours = load->n_contours;
 333    FT_Outline*     outline;
 334    TT_Face         face       = (TT_Face)load->face;
 335    FT_UShort       n_ins;
 336    FT_Int          n_points;
 337
 338    FT_Byte         *flag, *flag_limit;
 339    FT_Byte         c, count;
 340    FT_Vector       *vec, *vec_limit;
 341    FT_Pos          x;
 342    FT_Short        *cont, *cont_limit, prev_cont;
 343    FT_Int          xy_size = 0;
 344
 345
 346    /* check that we can add the contours to the glyph */
 347    error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours );
 348    if ( error )
 349      goto Fail;
 350
 351    /* reading the contours' endpoints & number of points */
 352    cont       = gloader->current.outline.contours;
 353    cont_limit = cont + n_contours;
 354
 355    /* check space for contours array + instructions count */
 356    if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit )
 357      goto Invalid_Outline;
 358
 359    prev_cont = FT_NEXT_USHORT( p );
 360
 361    if ( n_contours > 0 )
 362      cont[0] = prev_cont;
 363
 364    for ( cont++; cont < cont_limit; cont++ )
 365    {
 366      cont[0] = FT_NEXT_USHORT( p );
 367      if ( cont[0] <= prev_cont )
 368      {
 369        /* unordered contours: this is invalid */
 370        error = FT_Err_Invalid_Table;
 371        goto Fail;
 372      }
 373      prev_cont = cont[0];
 374    }
 375
 376    n_points = 0;
 377    if ( n_contours > 0 )
 378    {
 379      n_points = cont[-1] + 1;
 380      if ( n_points < 0 )
 381        goto Invalid_Outline;
 382    }
 383
 384    /* note that we will add four phantom points later */
 385    error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
 386    if ( error )
 387      goto Fail;
 388
 389    /* we'd better check the contours table right now */
 390    outline = &gloader->current.outline;
 391
 392    for ( cont = outline->contours + 1; cont < cont_limit; cont++ )
 393      if ( cont[-1] >= cont[0] )
 394        goto Invalid_Outline;
 395
 396    /* reading the bytecode instructions */
 397    load->glyph->control_len  = 0;
 398    load->glyph->control_data = 0;
 399
 400    if ( p + 2 > limit )
 401      goto Invalid_Outline;
 402
 403    n_ins = FT_NEXT_USHORT( p );
 404
 405    FT_TRACE5(( "  Instructions size: %u\n", n_ins ));
 406
 407    if ( n_ins > face->max_profile.maxSizeOfInstructions )
 408    {
 409      FT_TRACE0(( "TT_Load_Simple_Glyph: too many instructions (%d)\n",
 410                  n_ins ));
 411      error = TT_Err_Too_Many_Hints;
 412      goto Fail;
 413    }
 414
 415    if ( ( limit - p ) < n_ins )
 416    {
 417      FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" ));
 418      error = TT_Err_Too_Many_Hints;
 419      goto Fail;
 420    }
 421
 422#ifdef TT_USE_BYTECODE_INTERPRETER
 423
 424    if ( IS_HINTED( load->load_flags ) )
 425    {
 426      load->glyph->control_len  = n_ins;
 427      load->glyph->control_data = load->exec->glyphIns;
 428
 429      FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins );
 430    }
 431
 432#endif /* TT_USE_BYTECODE_INTERPRETER */
 433
 434    p += n_ins;
 435
 436    /* reading the point tags */
 437    flag       = (FT_Byte*)outline->tags;
 438    flag_limit = flag + n_points;
 439
 440    FT_ASSERT( flag != NULL );
 441
 442    while ( flag < flag_limit )
 443    {
 444      if ( p + 1 > limit )
 445        goto Invalid_Outline;
 446
 447      *flag++ = c = FT_NEXT_BYTE( p );
 448      if ( c & 8 )
 449      {
 450        if ( p + 1 > limit )
 451          goto Invalid_Outline;
 452
 453        count = FT_NEXT_BYTE( p );
 454        if ( flag + (FT_Int)count > flag_limit )
 455          goto Invalid_Outline;
 456
 457        for ( ; count > 0; count-- )
 458          *flag++ = c;
 459      }
 460    }
 461
 462    /* reading the X coordinates */
 463
 464    vec       = outline->points;
 465    vec_limit = vec + n_points;
 466    flag      = (FT_Byte*)outline->tags;
 467    x         = 0;
 468
 469    if ( p + xy_size > limit )
 470      goto Invalid_Outline;
 471
 472    for ( ; vec < vec_limit; vec++, flag++ )
 473    {
 474      FT_Pos   y = 0;
 475      FT_Byte  f = *flag;
 476
 477
 478      if ( f & 2 )
 479      {
 480        if ( p + 1 > limit )
 481          goto Invalid_Outline;
 482
 483        y = (FT_Pos)FT_NEXT_BYTE( p );
 484        if ( ( f & 16 ) == 0 )
 485          y = -y;
 486      }
 487      else if ( ( f & 16 ) == 0 )
 488      {
 489        if ( p + 2 > limit )
 490          goto Invalid_Outline;
 491
 492        y = (FT_Pos)FT_NEXT_SHORT( p );
 493      }
 494
 495      x     += y;
 496      vec->x = x;
 497      /* the cast is for stupid compilers */
 498      *flag  = (FT_Byte)( f & ~( 2 | 16 ) );
 499    }
 500
 501    /* reading the Y coordinates */
 502
 503    vec       = gloader->current.outline.points;
 504    vec_limit = vec + n_points;
 505    flag      = (FT_Byte*)outline->tags;
 506    x         = 0;
 507
 508    for ( ; vec < vec_limit; vec++, flag++ )
 509    {
 510      FT_Pos   y = 0;
 511      FT_Byte  f = *flag;
 512
 513
 514      if ( f & 4 )
 515      {
 516        if ( p + 1 > limit )
 517          goto Invalid_Outline;
 518
 519        y = (FT_Pos)FT_NEXT_BYTE( p );
 520        if ( ( f & 32 ) == 0 )
 521          y = -y;
 522      }
 523      else if ( ( f & 32 ) == 0 )
 524      {
 525        if ( p + 2 > limit )
 526          goto Invalid_Outline;
 527
 528        y = (FT_Pos)FT_NEXT_SHORT( p );
 529      }
 530
 531      x     += y;
 532      vec->y = x;
 533      /* the cast is for stupid compilers */
 534      *flag  = (FT_Byte)( f & FT_CURVE_TAG_ON );
 535    }
 536
 537    outline->n_points   = (FT_UShort)n_points;
 538    outline->n_contours = (FT_Short) n_contours;
 539
 540    load->cursor = p;
 541
 542  Fail:
 543    return error;
 544
 545  Invalid_Outline:
 546    error = TT_Err_Invalid_Outline;
 547    goto Fail;
 548  }
 549
 550
 551  FT_CALLBACK_DEF( FT_Error )
 552  TT_Load_Composite_Glyph( TT_Loader  loader )
 553  {
 554    FT_Error        error;
 555    FT_Byte*        p       = loader->cursor;
 556    FT_Byte*        limit   = loader->limit;
 557    FT_GlyphLoader  gloader = loader->gloader;
 558    FT_SubGlyph     subglyph;
 559    FT_UInt         num_subglyphs;
 560
 561
 562    num_subglyphs = 0;
 563
 564    do
 565    {
 566      FT_Fixed  xx, xy, yy, yx;
 567      FT_UInt   count;
 568
 569
 570      /* check that we can load a new subglyph */
 571      error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 );
 572      if ( error )
 573        goto Fail;
 574
 575      /* check space */
 576      if ( p + 4 > limit )
 577        goto Invalid_Composite;
 578
 579      subglyph = gloader->current.subglyphs + num_subglyphs;
 580
 581      subglyph->arg1 = subglyph->arg2 = 0;
 582
 583      subglyph->flags = FT_NEXT_USHORT( p );
 584      subglyph->index = FT_NEXT_USHORT( p );
 585
 586      /* check space */
 587      count = 2;
 588      if ( subglyph->flags & ARGS_ARE_WORDS )
 589        count += 2;
 590      if ( subglyph->flags & WE_HAVE_A_SCALE )
 591        count += 2;
 592      else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
 593        count += 4;
 594      else if ( subglyph->flags & WE_HAVE_A_2X2 )
 595        count += 8;
 596
 597      if ( p + count > limit )
 598        goto Invalid_Composite;
 599
 600      /* read arguments */
 601      if ( subglyph->flags & ARGS_ARE_WORDS )
 602      {
 603        subglyph->arg1 = FT_NEXT_SHORT( p );
 604        subglyph->arg2 = FT_NEXT_SHORT( p );
 605      }
 606      else
 607      {
 608        subglyph->arg1 = FT_NEXT_CHAR( p );
 609        subglyph->arg2 = FT_NEXT_CHAR( p );
 610      }
 611
 612      /* read transform */
 613      xx = yy = 0x10000L;
 614      xy = yx = 0;
 615
 616      if ( subglyph->flags & WE_HAVE_A_SCALE )
 617      {
 618        xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
 619        yy = xx;
 620      }
 621      else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
 622      {
 623        xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
 624        yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
 625      }
 626      else if ( subglyph->flags & WE_HAVE_A_2X2 )
 627      {
 628        xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
 629        yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
 630        xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
 631        yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
 632      }
 633
 634      subglyph->transform.xx = xx;
 635      subglyph->transform.xy = xy;
 636      subglyph->transform.yx = yx;
 637      subglyph->transform.yy = yy;
 638
 639      num_subglyphs++;
 640
 641    } while ( subglyph->flags & MORE_COMPONENTS );
 642
 643    gloader->current.num_subglyphs = num_subglyphs;
 644
 645#ifdef TT_USE_BYTECODE_INTERPRETER
 646
 647    {
 648      FT_Stream  stream = loader->stream;
 649
 650
 651      /* we must undo the FT_FRAME_ENTER in order to point */
 652      /* to the composite instructions, if we find some.   */
 653      /* We will process them later.                       */
 654      /*                                                   */
 655      loader->ins_pos = (FT_ULong)( FT_STREAM_POS() +
 656                                    p - limit );
 657    }
 658
 659#endif
 660
 661    loader->cursor = p;
 662
 663  Fail:
 664    return error;
 665
 666  Invalid_Composite:
 667    error = TT_Err_Invalid_Composite;
 668    goto Fail;
 669  }
 670
 671
 672  FT_LOCAL_DEF( void )
 673  TT_Init_Glyph_Loading( TT_Face  face )
 674  {
 675    face->access_glyph_frame   = TT_Access_Glyph_Frame;
 676    face->read_glyph_header    = TT_Load_Glyph_Header;
 677    face->read_simple_glyph    = TT_Load_Simple_Glyph;
 678    face->read_composite_glyph = TT_Load_Composite_Glyph;
 679    face->forget_glyph_frame   = TT_Forget_Glyph_Frame;
 680  }
 681
 682
 683  static void
 684  tt_prepare_zone( TT_GlyphZone  zone,
 685                   FT_GlyphLoad  load,
 686                   FT_UInt       start_point,
 687                   FT_UInt       start_contour )
 688  {
 689    zone->n_points    = (FT_UShort)( load->outline.n_points - start_point );
 690    zone->n_contours  = (FT_Short) ( load->outline.n_contours -
 691                                       start_contour );
 692    zone->org         = load->extra_points + start_point;
 693    zone->cur         = load->outline.points + start_point;
 694    zone->orus        = load->extra_points2 + start_point;
 695    zone->tags        = (FT_Byte*)load->outline.tags + start_point;
 696    zone->contours    = (FT_UShort*)load->outline.contours + start_contour;
 697    zone->first_point = (FT_UShort)start_point;
 698  }
 699
 700
 701  /*************************************************************************/
 702  /*                                                                       */
 703  /* <Function>                                                            */
 704  /*    TT_Hint_Glyph                                                      */
 705  /*                                                                       */
 706  /* <Description>                                                         */
 707  /*    Hint the glyph using the zone prepared by the caller.  Note that   */
 708  /*    the zone is supposed to include four phantom points.               */
 709  /*                                                                       */
 710  static FT_Error
 711  TT_Hint_Glyph( TT_Loader  loader,
 712                 FT_Bool    is_composite )
 713  {
 714    TT_GlyphZone  zone = &loader->zone;
 715    FT_Pos        origin;
 716
 717#ifdef TT_USE_BYTECODE_INTERPRETER
 718    FT_UInt       n_ins;
 719#else
 720    FT_UNUSED( is_composite );
 721#endif
 722
 723
 724#ifdef TT_USE_BYTECODE_INTERPRETER
 725    if ( loader->glyph->control_len > 0xFFFFL )
 726    {
 727      FT_TRACE1(( "TT_Hint_Glyph: too long instructions " ));
 728      FT_TRACE1(( "(0x%lx byte) is truncated\n",
 729                 loader->glyph->control_len ));
 730    }
 731    n_ins = (FT_UInt)( loader->glyph->control_len );
 732#endif
 733
 734    origin = zone->cur[zone->n_points - 4].x;
 735    origin = FT_PIX_ROUND( origin ) - origin;
 736    if ( origin )
 737      translate_array( zone->n_points, zone->cur, origin, 0 );
 738
 739#ifdef TT_USE_BYTECODE_INTERPRETER
 740    /* save original point position in org */
 741    if ( n_ins > 0 )
 742      FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points );
 743
 744    /* Reset graphics state. */
 745    loader->exec->GS = ((TT_Size)loader->size)->GS;
 746
 747    /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
 748    /*      completely refer to the (already) hinted subglyphs.     */
 749    if ( is_composite )
 750    {
 751      loader->exec->metrics.x_scale = 1 << 16;
 752      loader->exec->metrics.y_scale = 1 << 16;
 753
 754      FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points );
 755    }
 756    else
 757    {
 758      loader->exec->metrics.x_scale =
 759        ((TT_Size)loader->size)->metrics.x_scale;
 760      loader->exec->metrics.y_scale =
 761        ((TT_Size)loader->size)->metrics.y_scale;
 762    }
 763#endif
 764
 765    /* round pp2 and pp4 */
 766    zone->cur[zone->n_points - 3].x =
 767      FT_PIX_ROUND( zone->cur[zone->n_points - 3].x );
 768    zone->cur[zone->n_points - 1].y =
 769      FT_PIX_ROUND( zone->cur[zone->n_points - 1].y );
 770
 771#ifdef TT_USE_BYTECODE_INTERPRETER
 772
 773    if ( n_ins > 0 )
 774    {
 775      FT_Bool   debug;
 776      FT_Error  error;
 777
 778      FT_GlyphLoader  gloader         = loader->gloader;
 779      FT_Outline      current_outline = gloader->current.outline;
 780
 781
 782      error = TT_Set_CodeRange( loader->exec, tt_coderange_glyph,
 783                                loader->exec->glyphIns, n_ins );
 784      if ( error )
 785        return error;
 786
 787      loader->exec->is_composite = is_composite;
 788      loader->exec->pts          = *zone;
 789
 790      debug = FT_BOOL( !( loader->load_flags & FT_LOAD_NO_SCALE ) &&
 791                       ((TT_Size)loader->size)->debug             );
 792
 793      error = TT_Run_Context( loader->exec, debug );
 794      if ( error && loader->exec->pedantic_hinting )
 795        return error;
 796
 797      /* store drop-out mode in bits 5-7; set bit 2 also as a marker */
 798      current_outline.tags[0] |=
 799        ( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE;
 800    }
 801
 802#endif
 803
 804    /* save glyph phantom points */
 805    if ( !loader->preserve_pps )
 806    {
 807      loader->pp1 = zone->cur[zone->n_points - 4];
 808      loader->pp2 = zone->cur[zone->n_points - 3];
 809      loader->pp3 = zone->cur[zone->n_points - 2];
 810      loader->pp4 = zone->cur[zone->n_points - 1];
 811    }
 812
 813    return TT_Err_Ok;
 814  }
 815
 816
 817  /*************************************************************************/
 818  /*                                                                       */
 819  /* <Function>                                                            */
 820  /*    TT_Process_Simple_Glyph                                            */
 821  /*                                                                       */
 822  /* <Description>                                                         */
 823  /*    Once a simple glyph has been loaded, it needs to be processed.     */
 824  /*    Usually, this means scaling and hinting through bytecode           */
 825  /*    interpretation.                                                    */
 826  /*                                                                       */
 827  static FT_Error
 828  TT_Process_Simple_Glyph( TT_Loader  loader )
 829  {
 830    FT_GlyphLoader  gloader = loader->gloader;
 831    FT_Error        error   = TT_Err_Ok;
 832    FT_Outline*     outline;
 833    FT_Int          n_points;
 834
 835
 836    outline  = &gloader->current.outline;
 837    n_points = outline->n_points;
 838
 839    /* set phantom points */
 840
 841    outline->points[n_points    ] = loader->pp1;
 842    outline->points[n_points + 1] = loader->pp2;
 843    outline->points[n_points + 2] = loader->pp3;
 844    outline->points[n_points + 3] = loader->pp4;
 845
 846    outline->tags[n_points    ] = 0;
 847    outline->tags[n_points + 1] = 0;
 848    outline->tags[n_points + 2] = 0;
 849    outline->tags[n_points + 3] = 0;
 850
 851    n_points += 4;
 852
 853#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
 854
 855    if ( ((TT_Face)loader->face)->doblend )
 856    {
 857      /* Deltas apply to the unscaled data. */
 858      FT_Vector*  deltas;
 859      FT_Memory   memory = loader->face->memory;
 860      FT_Int      i;
 861
 862
 863      error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
 864                                        loader->glyph_index,
 865                                        &deltas,
 866                                        n_points );
 867      if ( error )
 868        return error;
 869
 870      for ( i = 0; i < n_points; ++i )
 871      {
 872        outline->points[i].x += deltas[i].x;
 873        outline->points[i].y += deltas[i].y;
 874      }
 875
 876      FT_FREE( deltas );
 877    }
 878
 879#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
 880
 881    if ( IS_HINTED( loader->load_flags ) )
 882    {
 883      tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 );
 884
 885      FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur,
 886                     loader->zone.n_points + 4 );
 887    }
 888
 889    /* scale the glyph */
 890    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
 891    {
 892      FT_Vector*  vec     = outline->points;
 893      FT_Vector*  limit   = outline->points + n_points;
 894      FT_Fixed    x_scale = ((TT_Size)loader->size)->metrics.x_scale;
 895      FT_Fixed    y_scale = ((TT_Size)loader->size)->metrics.y_scale;
 896
 897
 898      for ( ; vec < limit; vec++ )
 899      {
 900        vec->x = FT_MulFix( vec->x, x_scale );
 901        vec->y = FT_MulFix( vec->y, y_scale );
 902      }
 903
 904      loader->pp1 = outline->points[n_points - 4];
 905      loader->pp2 = outline->points[n_points - 3];
 906      loader->pp3 = outline->points[n_points - 2];
 907      loader->pp4 = outline->points[n_points - 1];
 908    }
 909
 910    if ( IS_HINTED( loader->load_flags ) )
 911    {
 912      loader->zone.n_points += 4;
 913
 914      error = TT_Hint_Glyph( loader, 0 );
 915    }
 916
 917    return error;
 918  }
 919
 920
 921  /*************************************************************************/
 922  /*                                                                       */
 923  /* <Function>                                                            */
 924  /*    TT_Process_Composite_Component                                     */
 925  /*                                                                       */
 926  /* <Description>                                                         */
 927  /*    Once a composite component has been loaded, it needs to be         */
 928  /*    processed.  Usually, this means transforming and translating.      */
 929  /*                                                                       */
 930  static FT_Error
 931  TT_Process_Composite_Component( TT_Loader    loader,
 932                                  FT_SubGlyph  subglyph,
 933                                  FT_UInt      start_point,
 934                                  FT_UInt      num_base_points )
 935  {
 936    FT_GlyphLoader  gloader    = loader->gloader;
 937    FT_Vector*      base_vec   = gloader->base.outline.points;
 938    FT_UInt         num_points = gloader->base.outline.n_points;
 939    FT_Bool         have_scale;
 940    FT_Pos          x, y;
 941
 942
 943    have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE     |
 944                                              WE_HAVE_AN_XY_SCALE |
 945                                              WE_HAVE_A_2X2       ) );
 946
 947    /* perform the transform required for this subglyph */
 948    if ( have_scale )
 949    {
 950      FT_UInt  i;
 951
 952
 953      for ( i = num_base_points; i < num_points; i++ )
 954        FT_Vector_Transform( base_vec + i, &subglyph->transform );
 955    }
 956
 957    /* get offset */
 958    if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
 959    {
 960      FT_UInt     k = subglyph->arg1;
 961      FT_UInt     l = subglyph->arg2;
 962      FT_Vector*  p1;
 963      FT_Vector*  p2;
 964
 965
 966      /* match l-th point of the newly loaded component to the k-th point */
 967      /* of the previously loaded components.                             */
 968
 969      /* change to the point numbers used by our outline */
 970      k += start_point;
 971      l += num_base_points;
 972      if ( k >= num_base_points ||
 973           l >= num_points      )
 974        return TT_Err_Invalid_Composite;
 975
 976      p1 = gloader->base.outline.points + k;
 977      p2 = gloader->base.outline.points + l;
 978
 979      x = p1->x - p2->x;
 980      y = p1->y - p2->y;
 981    }
 982    else
 983    {
 984      x = subglyph->arg1;
 985      y = subglyph->arg2;
 986
 987      if ( !x && !y )
 988        return TT_Err_Ok;
 989
 990  /* Use a default value dependent on                                     */
 991  /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED.  This is useful for old TT */
 992  /* fonts which don't set the xxx_COMPONENT_OFFSET bit.                  */
 993
 994      if ( have_scale &&
 995#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
 996           !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
 997#else
 998            ( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
 999#endif
1000      {
1001
1002#if 0
1003
1004  /*************************************************************************/
1005  /*                                                                       */
1006  /* This algorithm is what Apple documents.  But it doesn't work.         */
1007  /*                                                                       */
1008        int  a = subglyph->transform.xx > 0 ?  subglyph->transform.xx
1009                                            : -subglyph->transform.xx;
1010        int  b = subglyph->transform.yx > 0 ?  subglyph->transform.yx
1011                                            : -subglyph->transform.yx;
1012        int  c = subglyph->transform.xy > 0 ?  subglyph->transform.xy
1013                                            : -subglyph->transform.xy;
1014        int  d = subglyph->transform.yy > 0 ? subglyph->transform.yy
1015                                            : -subglyph->transform.yy;
1016        int  m = a > b ? a : b;
1017        int  n = c > d ? c : d;
1018
1019
1020        if ( a - b <= 33 && a - b >= -33 )
1021          m *= 2;
1022        if ( c - d <= 33 && c - d >= -33 )
1023          n *= 2;
1024        x = FT_MulFix( x, m );
1025        y = FT_MulFix( y, n );
1026
1027#else /* 0 */
1028
1029  /*************************************************************************/
1030  /*                                                                       */
1031  /* This algorithm is a guess and works much better than the above.       */
1032  /*                                                                       */
1033        FT_Fixed  mac_xscale = FT_SqrtFixed(
1034                                 (FT_Int32)FT_MulFix( subglyph->transform.xx,
1035                                                      subglyph->transform.xx ) +
1036                                 (FT_Int32)FT_MulFix( subglyph->transform.xy,
1037                                                      subglyph->transform.xy ) );
1038        FT_Fixed  mac_yscale = FT_SqrtFixed(
1039                                 (FT_Int32)FT_MulFix( subglyph->transform.yy,
1040                                                      subglyph->transform.yy ) +
1041                                 (FT_Int32)FT_MulFix( subglyph->transform.yx,
1042                                                      subglyph->transform.yx ) );
1043
1044
1045        x = FT_MulFix( x, mac_xscale );
1046        y = FT_MulFix( y, mac_yscale );
1047
1048#endif /* 0 */
1049
1050      }
1051
1052      if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
1053      {
1054        FT_Fixed  x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1055        FT_Fixed  y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1056
1057
1058        x = FT_MulFix( x, x_scale );
1059        y = FT_MulFix( y, y_scale );
1060
1061        if ( subglyph->flags & ROUND_XY_TO_GRID )
1062        {
1063          x = FT_PIX_ROUND( x );
1064          y = FT_PIX_ROUND( y );
1065        }
1066      }
1067    }
1068
1069    if ( x || y )
1070      translate_array( num_points - num_base_points,
1071                       base_vec + num_base_points,
1072                       x, y );
1073
1074    return TT_Err_Ok;
1075  }
1076
1077
1078  /*************************************************************************/
1079  /*                                                                       */
1080  /* <Function>                                                            */
1081  /*    TT_Process_Composite_Glyph                                         */
1082  /*                                                                       */
1083  /* <Description>                                                         */
1084  /*    This is slightly different from TT_Process_Simple_Glyph, in that   */
1085  /*    its sole purpose is to hint the glyph.  Thus this function is      */
1086  /*    only available when bytecode interpreter is enabled.               */
1087  /*                                                                       */
1088  static FT_Error
1089  TT_Process_Composite_Glyph( TT_Loader  loader,
1090                              FT_UInt    start_point,
1091                              FT_UInt    start_contour )
1092  {
1093    FT_Error     error;
1094    FT_Outline*  outline;
1095    FT_UInt      i;
1096
1097
1098    outline = &loader->gloader->base.outline;
1099
1100    /* make room for phantom points */
1101    error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader,
1102                                         outline->n_points + 4,
1103                                         0 );
1104    if ( error )
1105      return error;
1106
1107    outline->points[outline->n_points    ] = loader->pp1;
1108    outline->points[outline->n_points + 1] = loader->pp2;
1109    outline->points[outline->n_points + 2] = loader->pp3;
1110    outline->points[outline->n_points + 3] = loader->pp4;
1111
1112    outline->tags[outline->n_points    ] = 0;
1113    outline->tags[outline->n_points + 1] = 0;
1114    outline->tags[outline->n_points + 2] = 0;
1115    outline->tags[outline->n_points + 3] = 0;
1116
1117#ifdef TT_USE_BYTECODE_INTERPRETER
1118
1119    {
1120      FT_Stream  stream = loader->stream;
1121      FT_UShort  n_ins;
1122
1123
1124      /* TT_Load_Composite_Glyph only gives us the offset of instructions */
1125      /* so we read them here                                             */
1126      if ( FT_STREAM_SEEK( loader->ins_pos ) ||
1127           FT_READ_USHORT( n_ins )           )
1128        return error;
1129
1130      FT_TRACE5(( "  Instructions size = %d\n", n_ins ));
1131
1132      /* check it */
1133      if ( n_ins > ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions )
1134      {
1135        FT_TRACE0(( "TT_Process_Composite_Glyph: too many instructions (%d)\n",
1136                    n_ins ));
1137
1138        return TT_Err_Too_Many_Hints;
1139      }
1140      else if ( n_ins == 0 )
1141        return TT_Err_Ok;
1142
1143      if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) )
1144        return error;
1145
1146      loader->glyph->control_data = loader->exec->glyphIns;
1147      loader->glyph->control_len  = n_ins;
1148    }
1149
1150#endif
1151
1152    tt_prepare_zone( &loader->zone, &loader->gloader->base,
1153                     start_point, start_contour );
1154
1155    /* Some points are likely touched during execution of  */
1156    /* instructions on components.  So let's untouch them. */
1157    for ( i = start_point; i < loader->zone.n_points; i++ )
1158      loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
1159
1160    loader->zone.n_points += 4;
1161
1162    return TT_Hint_Glyph( loader, 1 );
1163  }
1164
1165
1166  /* Calculate the four phantom points.                     */
1167  /* The first two stand for horizontal origin and advance. */
1168  /* The last two stand for vertical origin and advance.    */
1169#define TT_LOADER_SET_PP( loader )                                          \
1170          do {                                                              \
1171            (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
1172            (loader)->pp1.y = 0;                                            \
1173            (loader)->pp2.x = (loader)->pp1.x + (loader)->advance;          \
1174            (loader)->pp2.y = 0;                                            \
1175            (loader)->pp3.x = 0;                                            \
1176            (loader)->pp3.y = (loader)->top_bearing + (loader)->bbox.yMax;  \
1177            (loader)->pp4.x = 0;                                            \
1178            (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance;         \
1179          } while ( 0 )
1180
1181
1182  /*************************************************************************/
1183  /*                                                                       */
1184  /* <Function>                                                            */
1185  /*    load_truetype_glyph                                                */
1186  /*                                                                       */
1187  /* <Description>                                                         */
1188  /*    Loads a given truetype glyph.  Handles composites and uses a       */
1189  /*    TT_Loader object.                                                  */
1190  /*                                                                       */
1191  static FT_Error
1192  load_truetype_glyph( TT_Loader  loader,
1193                       FT_UInt    glyph_index,
1194                       FT_UInt    recurse_count,
1195                       FT_Bool    header_only )
1196  {
1197    FT_Error        error        = TT_Err_Ok;
1198    FT_Fixed        x_scale, y_scale;
1199    FT_ULong        offset;
1200    TT_Face         face         = (TT_Face)loader->face;
1201    FT_GlyphLoader  gloader      = loader->gloader;
1202    FT_Bool         opened_frame = 0;
1203
1204#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1205    FT_Vector*      deltas       = NULL;
1206#endif
1207
1208#ifdef FT_CONFIG_OPTION_INCREMENTAL
1209    FT_StreamRec    inc_stream;
1210    FT_Data         glyph_data;
1211    FT_Bool         glyph_data_loaded = 0;
1212#endif
1213
1214
1215    /* some fonts have an incorrect value of `maxComponentDepth', */
1216    /* thus we allow depth 1 to catch the majority of them        */
1217    if ( recurse_count > 1                                   &&
1218         recurse_count > face->max_profile.maxComponentDepth )
1219    {
1220      error = TT_Err_Invalid_Composite;
1221      goto Exit;
1222    }
1223
1224    /* check glyph index */
1225    if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
1226    {
1227      error = TT_Err_Invalid_Glyph_Index;
1228      goto Exit;
1229    }
1230
1231    loader->glyph_index = glyph_index;
1232
1233    if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1234    {
1235      x_scale = ((TT_Size)loader->size)->metrics.x_scale;
1236      y_scale = ((TT_Size)loader->size)->metrics.y_scale;
1237    }
1238    else
1239    {
1240      x_scale = 0x10000L;
1241      y_scale = 0x10000L;
1242    }
1243
1244    tt_get_metrics( loader, glyph_index );
1245
1246    /* Set `offset' to the start of the glyph relative to the start of */
1247    /* the `glyf' table, and `byte_len' to the length of the glyph in  */
1248    /* bytes.                                                          */
1249
1250#ifdef FT_CONFIG_OPTION_INCREMENTAL
1251
1252    /* If we are loading glyph data via the incremental interface, set */
1253    /* the loader stream to a memory stream reading the data returned  */
1254    /* by the interface.                                               */
1255    if ( face->root.internal->incremental_interface )
1256    {
1257      error = face->root.internal->incremental_interface->funcs->get_glyph_data(
1258                face->root.internal->incremental_interface->object,
1259                glyph_index, &glyph_data );
1260      if ( error )
1261        goto Exit;
1262
1263      glyph_data_loaded = 1;
1264      offset            = 0;
1265      loader->byte_len  = glyph_data.length;
1266
1267      FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
1268      FT_Stream_OpenMemory( &inc_stream,
1269                            glyph_data.pointer, glyph_data.length );
1270
1271      loader->stream = &inc_stream;
1272    }
1273    else
1274
1275#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1276
1277      offset = tt_face_get_location( face, glyph_index,
1278                                     (FT_UInt*)&loader->byte_len );
1279
1280    if ( loader->byte_len > 0 )
1281    {
1282#ifdef FT_CONFIG_OPTION_INCREMENTAL
1283      /* for the incremental interface, `glyf_offset' is always zero */
1284      if ( !loader->glyf_offset                        &&
1285           !face->root.internal->incremental_interface )
1286#else
1287      if ( !loader->glyf_offset )
1288#endif /* FT_CONFIG_OPTION_INCREMENTAL */
1289      {
1290        FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
1291        error = TT_Err_Invalid_Table;
1292        goto Exit;
1293      }
1294
1295      error = face->access_glyph_frame( loader, glyph_index,
1296                                        loader->glyf_offset + offset,
1297                                        loader->byte_len );
1298      if ( error )
1299        goto Exit;
1300
1301      opened_frame = 1;
1302
1303      /* read glyph header first */
1304      error = face->read_glyph_header( loader );
1305      if ( error || header_only )
1306        goto Exit;
1307    }
1308
1309    if ( loader->byte_len == 0 || loader->n_contours == 0 )
1310    {
1311      loader->bbox.xMin = 0;
1312      loader->bbox.xMax = 0;
1313      loader->bbox.yMin = 0;
1314      loader->bbox.yMax = 0;
1315
1316      if ( header_only )
1317        goto Exit;
1318
1319      TT_LOADER_SET_PP( loader );
1320
1321#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1322
1323      if ( ((TT_Face)(loader->face))->doblend )
1324      {
1325        /* this must be done before scaling */
1326        FT_Memory  memory = loader->face->memory;
1327
1328
1329        error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
1330                                          glyph_index, &deltas, 4 );
1331        if ( error )
1332          goto Exit;
1333
1334        loader->pp1.x += deltas[0].x; loader->pp1.y += deltas[0].y;
1335        loader->pp2.x += deltas[1].x; loader->pp2.y += deltas[1].y;
1336        loader->pp3.x += deltas[2].x; loader->pp3.y += deltas[2].y;
1337        loader->pp4.x += deltas[3].x; loader->pp4.y += deltas[3].y;
1338
1339        FT_FREE( deltas );
1340      }
1341
1342#endif
1343
1344      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1345      {
1346        loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1347        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1348        loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1349        loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1350      }
1351
1352      error = TT_Err_Ok;
1353      goto Exit;
1354    }
1355
1356    TT_LOADER_SET_PP( loader );
1357
1358    /***********************************************************************/
1359    /***********************************************************************/
1360    /***********************************************************************/
1361
1362    /* if it is a simple glyph, load it */
1363
1364    if ( loader->n_contours > 0 )
1365    {
1366      error = face->read_simple_glyph( loader );
1367      if ( error )
1368        goto Exit;
1369
1370      /* all data have been read */
1371      face->forget_glyph_frame( loader );
1372      opened_frame = 0;
1373
1374      error = TT_Process_Simple_Glyph( loader );
1375      if ( error )
1376        goto Exit;
1377
1378      FT_GlyphLoader_Add( gloader );
1379    }
1380
1381    /***********************************************************************/
1382    /***********************************************************************/
1383    /***********************************************************************/
1384
1385    /* otherwise, load a composite! */
1386    else if ( loader->n_contours == -1 )
1387    {
1388      FT_UInt   start_point;
1389      FT_UInt   start_contour;
1390      FT_ULong  ins_pos;  /* position of composite instructions, if any */
1391
1392
1393      start_point   = gloader->base.outline.n_points;
1394      start_contour = gloader->base.outline.n_contours;
1395
1396      /* for each subglyph, read composite header */
1397      error = face->read_composite_glyph( loader );
1398      if ( error )
1399        goto Exit;
1400
1401      /* store the offset of instructions */
1402      ins_pos = loader->ins_pos;
1403
1404      /* all data we need are read */
1405      face->forget_glyph_frame( loader );
1406      opened_frame = 0;
1407
1408#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1409
1410      if ( face->doblend )
1411      {
1412        FT_Int       i, limit;
1413        FT_SubGlyph  subglyph;
1414        FT_Memory    memory = face->root.memory;
1415
1416
1417        /* this provides additional offsets */
1418        /* for each component's translation */
1419
1420        if ( ( error = TT_Vary_Get_Glyph_Deltas(
1421                         face,
1422                         glyph_index,
1423                         &deltas,
1424                         gloader->current.num_subglyphs + 4 )) != 0 )
1425          goto Exit;
1426
1427        subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
1428        limit    = gloader->current.num_subglyphs;
1429
1430        for ( i = 0; i < limit; ++i, ++subglyph )
1431        {
1432          if ( subglyph->flags & ARGS_ARE_XY_VALUES )
1433          {
1434            /* XXX: overflow check for subglyph->{arg1,arg2}.   */
1435            /* deltas[i].{x,y} must be within signed 16-bit,    */
1436            /* but the restriction of summed delta is not clear */
1437            subglyph->arg1 += (FT_Int16)deltas[i].x;
1438            subglyph->arg2 += (FT_Int16)deltas[i].y;
1439          }
1440        }
1441
1442        loader->pp1.x += deltas[i + 0].x; loader->pp1.y += deltas[i + 0].y;
1443        loader->pp2.x += deltas[i + 1].x; loader->pp2.y += deltas[i + 1].y;
1444        loader->pp3.x += deltas[i + 2].x; loader->pp3.y += deltas[i + 2].y;
1445        loader->pp4.x += deltas[i + 3].x; loader->pp4.y += deltas[i + 3].y;
1446
1447        FT_FREE( deltas );
1448      }
1449
1450#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1451
1452      if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
1453      {
1454        loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
1455        loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
1456        loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
1457        loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
1458      }
1459
1460      /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
1461      /* `as is' in the glyph slot (the client application will be     */
1462      /* responsible for interpreting these data)...                   */
1463      if ( loader->load_flags & FT_LOAD_NO_RECURSE )
1464      {
1465        FT_GlyphLoader_Add( gloader );
1466        loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
1467
1468        goto Exit;
1469      }
1470
1471      /*********************************************************************/
1472      /*********************************************************************/
1473      /*********************************************************************/
1474
1475      {
1476        FT_UInt      n, num_base_points;
1477        FT_SubGlyph  subglyph       = 0;
1478
1479        FT_UInt      num_points     = start_point;
1480        FT_UInt      num_subglyphs  = gloader->current.num_subglyphs;
1481        FT_UInt      num_base_subgs = gloader->base.num_subglyphs;
1482
1483        FT_Stream    old_stream     = loader->stream;
1484
1485
1486        FT_GlyphLoader_Add( gloader );
1487
1488        /* read each subglyph independently */
1489        for ( n = 0; n < num_subglyphs; n++ )
1490        {
1491          FT_Vector  pp[4];
1492
1493
1494          /* Each time we call load_truetype_glyph in this loop, the   */
1495          /* value of `gloader.base.subglyphs' can change due to table */
1496          /* reallocations.  We thus need to recompute the subglyph    */
1497          /* pointer on each iteration.                                */
1498          subglyph = gloader->base.subglyphs + num_base_subgs + n;
1499
1500          pp[0] = loader->pp1;
1501          pp[1] = loader->pp2;
1502          pp[2] = loader->pp3;
1503          pp[3] = loader->pp4;
1504
1505          num_base_points = gloader->base.outline.n_points;
1506
1507          error = load_truetype_glyph( loader, subglyph->index,
1508                                       recurse_count + 1, FALSE );
1509          if ( error )
1510            goto Exit;
1511
1512          /* restore subglyph pointer */
1513          subglyph = gloader->base.subglyphs + num_base_subgs + n;
1514
1515          if ( !( subglyph->flags & USE_MY_METRICS ) )
1516          {
1517            loader->pp1 = pp[0];
1518            loader->pp2 = pp[1];
1519            loader->pp3 = pp[2];
1520            loader->pp4 = pp[3];
1521          }
1522
1523          num_points = gloader->base.outline.n_points;
1524
1525          if ( num_points == num_base_points )
1526            continue;
1527
1528          /* gloader->base.outline consists of three parts:               */
1529          /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
1530          /*                                                              */
1531          /* (1): exists from the beginning                               */
1532          /* (2): components that have been loaded so far                 */
1533          /* (3): the newly loaded component                              */
1534          TT_Process_Composite_Component( loader, subglyph, start_point,
1535                                          num_base_poin

Large files files are truncated, but you can click here to view the full file