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