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