Home | History | Annotate | Download | only in base
      1 /***************************************************************************/
      2 /*                                                                         */
      3 /*  ftobjs.c                                                               */
      4 /*                                                                         */
      5 /*    The FreeType private base classes (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_LIST_H
     21 #include FT_OUTLINE_H
     22 #include FT_INTERNAL_VALIDATE_H
     23 #include FT_INTERNAL_OBJECTS_H
     24 #include FT_INTERNAL_DEBUG_H
     25 #include FT_INTERNAL_RFORK_H
     26 #include FT_INTERNAL_STREAM_H
     27 #include FT_INTERNAL_SFNT_H    /* for SFNT_Load_Table_Func */
     28 #include FT_TRUETYPE_TABLES_H
     29 #include FT_TRUETYPE_TAGS_H
     30 #include FT_TRUETYPE_IDS_H
     31 
     32 #include FT_SERVICE_PROPERTIES_H
     33 #include FT_SERVICE_SFNT_H
     34 #include FT_SERVICE_POSTSCRIPT_NAME_H
     35 #include FT_SERVICE_GLYPH_DICT_H
     36 #include FT_SERVICE_TT_CMAP_H
     37 #include FT_SERVICE_KERNING_H
     38 #include FT_SERVICE_TRUETYPE_ENGINE_H
     39 
     40 #ifdef FT_CONFIG_OPTION_MAC_FONTS
     41 #include "ftbase.h"
     42 #endif
     43 
     44 
     45 #ifdef FT_DEBUG_LEVEL_TRACE
     46 
     47 #include FT_BITMAP_H
     48 
     49 #if defined( _MSC_VER )      /* Visual C++ (and Intel C++)   */
     50   /* We disable the warning `conversion from XXX to YYY,     */
     51   /* possible loss of data' in order to compile cleanly with */
     52   /* the maximum level of warnings: `md5.c' is non-FreeType  */
     53   /* code, and it gets used during development builds only.  */
     54 #pragma warning( push )
     55 #pragma warning( disable : 4244 )
     56 #endif /* _MSC_VER */
     57 
     58   /* It's easiest to include `md5.c' directly.  However, since OpenSSL */
     59   /* also provides the same functions, there might be conflicts if     */
     60   /* both FreeType and OpenSSL are built as static libraries.  For     */
     61   /* this reason, we put the MD5 stuff into the `FT_' namespace.       */
     62 #define MD5_u32plus  FT_MD5_u32plus
     63 #define MD5_CTX      FT_MD5_CTX
     64 #define MD5_Init     FT_MD5_Init
     65 #define MD5_Update   FT_MD5_Update
     66 #define MD5_Final    FT_MD5_Final
     67 
     68 #undef  HAVE_OPENSSL
     69 
     70 #include "md5.c"
     71 
     72 #if defined( _MSC_VER )
     73 #pragma warning( pop )
     74 #endif
     75 
     76 #endif /* FT_DEBUG_LEVEL_TRACE */
     77 
     78 
     79 #define GRID_FIT_METRICS
     80 
     81 
     82   FT_BASE_DEF( FT_Pointer )
     83   ft_service_list_lookup( FT_ServiceDesc  service_descriptors,
     84                           const char*     service_id )
     85   {
     86     FT_Pointer      result = NULL;
     87     FT_ServiceDesc  desc   = service_descriptors;
     88 
     89 
     90     if ( desc && service_id )
     91     {
     92       for ( ; desc->serv_id != NULL; desc++ )
     93       {
     94         if ( ft_strcmp( desc->serv_id, service_id ) == 0 )
     95         {
     96           result = (FT_Pointer)desc->serv_data;
     97           break;
     98         }
     99       }
    100     }
    101 
    102     return result;
    103   }
    104 
    105 
    106   FT_BASE_DEF( void )
    107   ft_validator_init( FT_Validator        valid,
    108                      const FT_Byte*      base,
    109                      const FT_Byte*      limit,
    110                      FT_ValidationLevel  level )
    111   {
    112     valid->base  = base;
    113     valid->limit = limit;
    114     valid->level = level;
    115     valid->error = FT_Err_Ok;
    116   }
    117 
    118 
    119   FT_BASE_DEF( FT_Int )
    120   ft_validator_run( FT_Validator  valid )
    121   {
    122     /* This function doesn't work!  None should call it. */
    123     FT_UNUSED( valid );
    124 
    125     return -1;
    126   }
    127 
    128 
    129   FT_BASE_DEF( void )
    130   ft_validator_error( FT_Validator  valid,
    131                       FT_Error      error )
    132   {
    133     /* since the cast below also disables the compiler's */
    134     /* type check, we introduce a dummy variable, which  */
    135     /* will be optimized away                            */
    136     volatile ft_jmp_buf* jump_buffer = &valid->jump_buffer;
    137 
    138 
    139     valid->error = error;
    140 
    141     /* throw away volatileness; use `jump_buffer' or the  */
    142     /* compiler may warn about an unused local variable   */
    143     ft_longjmp( *(ft_jmp_buf*) jump_buffer, 1 );
    144   }
    145 
    146 
    147   /*************************************************************************/
    148   /*************************************************************************/
    149   /*************************************************************************/
    150   /****                                                                 ****/
    151   /****                                                                 ****/
    152   /****                           S T R E A M                           ****/
    153   /****                                                                 ****/
    154   /****                                                                 ****/
    155   /*************************************************************************/
    156   /*************************************************************************/
    157   /*************************************************************************/
    158 
    159 
    160   /* create a new input stream from an FT_Open_Args structure */
    161   /*                                                          */
    162   FT_BASE_DEF( FT_Error )
    163   FT_Stream_New( FT_Library           library,
    164                  const FT_Open_Args*  args,
    165                  FT_Stream           *astream )
    166   {
    167     FT_Error   error;
    168     FT_Memory  memory;
    169     FT_Stream  stream = NULL;
    170 
    171 
    172     *astream = NULL;
    173 
    174     if ( !library )
    175       return FT_THROW( Invalid_Library_Handle );
    176 
    177     if ( !args )
    178       return FT_THROW( Invalid_Argument );
    179 
    180     memory = library->memory;
    181 
    182     if ( FT_NEW( stream ) )
    183       goto Exit;
    184 
    185     stream->memory = memory;
    186 
    187     if ( args->flags & FT_OPEN_MEMORY )
    188     {
    189       /* create a memory-based stream */
    190       FT_Stream_OpenMemory( stream,
    191                             (const FT_Byte*)args->memory_base,
    192                             (FT_ULong)args->memory_size );
    193     }
    194 
    195 #ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT
    196 
    197     else if ( args->flags & FT_OPEN_PATHNAME )
    198     {
    199       /* create a normal system stream */
    200       error = FT_Stream_Open( stream, args->pathname );
    201       stream->pathname.pointer = args->pathname;
    202     }
    203     else if ( ( args->flags & FT_OPEN_STREAM ) && args->stream )
    204     {
    205       /* use an existing, user-provided stream */
    206 
    207       /* in this case, we do not need to allocate a new stream object */
    208       /* since the caller is responsible for closing it himself       */
    209       FT_FREE( stream );
    210       stream = args->stream;
    211     }
    212 
    213 #endif
    214 
    215     else
    216       error = FT_THROW( Invalid_Argument );
    217 
    218     if ( error )
    219       FT_FREE( stream );
    220     else
    221       stream->memory = memory;  /* just to be certain */
    222 
    223     *astream = stream;
    224 
    225   Exit:
    226     return error;
    227   }
    228 
    229 
    230   FT_BASE_DEF( void )
    231   FT_Stream_Free( FT_Stream  stream,
    232                   FT_Int     external )
    233   {
    234     if ( stream )
    235     {
    236       FT_Memory  memory = stream->memory;
    237 
    238 
    239       FT_Stream_Close( stream );
    240 
    241       if ( !external )
    242         FT_FREE( stream );
    243     }
    244   }
    245 
    246 
    247   /*************************************************************************/
    248   /*                                                                       */
    249   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
    250   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
    251   /* messages during execution.                                            */
    252   /*                                                                       */
    253 #undef  FT_COMPONENT
    254 #define FT_COMPONENT  trace_objs
    255 
    256 
    257   /*************************************************************************/
    258   /*************************************************************************/
    259   /*************************************************************************/
    260   /****                                                                 ****/
    261   /****                                                                 ****/
    262   /****               FACE, SIZE & GLYPH SLOT OBJECTS                   ****/
    263   /****                                                                 ****/
    264   /****                                                                 ****/
    265   /*************************************************************************/
    266   /*************************************************************************/
    267   /*************************************************************************/
    268 
    269 
    270   static FT_Error
    271   ft_glyphslot_init( FT_GlyphSlot  slot )
    272   {
    273     FT_Driver         driver   = slot->face->driver;
    274     FT_Driver_Class   clazz    = driver->clazz;
    275     FT_Memory         memory   = driver->root.memory;
    276     FT_Error          error    = FT_Err_Ok;
    277     FT_Slot_Internal  internal = NULL;
    278 
    279 
    280     slot->library = driver->root.library;
    281 
    282     if ( FT_NEW( internal ) )
    283       goto Exit;
    284 
    285     slot->internal = internal;
    286 
    287     if ( FT_DRIVER_USES_OUTLINES( driver ) )
    288       error = FT_GlyphLoader_New( memory, &internal->loader );
    289 
    290     if ( !error && clazz->init_slot )
    291       error = clazz->init_slot( slot );
    292 
    293   Exit:
    294     return error;
    295   }
    296 
    297 
    298   FT_BASE_DEF( void )
    299   ft_glyphslot_free_bitmap( FT_GlyphSlot  slot )
    300   {
    301     if ( slot->internal && ( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
    302     {
    303       FT_Memory  memory = FT_FACE_MEMORY( slot->face );
    304 
    305 
    306       FT_FREE( slot->bitmap.buffer );
    307       slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
    308     }
    309     else
    310     {
    311       /* assume that the bitmap buffer was stolen or not */
    312       /* allocated from the heap                         */
    313       slot->bitmap.buffer = NULL;
    314     }
    315   }
    316 
    317 
    318   FT_BASE_DEF( void )
    319   ft_glyphslot_set_bitmap( FT_GlyphSlot  slot,
    320                            FT_Byte*      buffer )
    321   {
    322     ft_glyphslot_free_bitmap( slot );
    323 
    324     slot->bitmap.buffer = buffer;
    325 
    326     FT_ASSERT( (slot->internal->flags & FT_GLYPH_OWN_BITMAP) == 0 );
    327   }
    328 
    329 
    330   FT_BASE_DEF( FT_Error )
    331   ft_glyphslot_alloc_bitmap( FT_GlyphSlot  slot,
    332                              FT_ULong      size )
    333   {
    334     FT_Memory  memory = FT_FACE_MEMORY( slot->face );
    335     FT_Error   error;
    336 
    337 
    338     if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
    339       FT_FREE( slot->bitmap.buffer );
    340     else
    341       slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
    342 
    343     (void)FT_ALLOC( slot->bitmap.buffer, size );
    344     return error;
    345   }
    346 
    347 
    348   static void
    349   ft_glyphslot_clear( FT_GlyphSlot  slot )
    350   {
    351     /* free bitmap if needed */
    352     ft_glyphslot_free_bitmap( slot );
    353 
    354     /* clear all public fields in the glyph slot */
    355     FT_ZERO( &slot->metrics );
    356     FT_ZERO( &slot->outline );
    357 
    358     slot->bitmap.width      = 0;
    359     slot->bitmap.rows       = 0;
    360     slot->bitmap.pitch      = 0;
    361     slot->bitmap.pixel_mode = 0;
    362     /* `slot->bitmap.buffer' has been handled by ft_glyphslot_free_bitmap */
    363 
    364     slot->bitmap_left   = 0;
    365     slot->bitmap_top    = 0;
    366     slot->num_subglyphs = 0;
    367     slot->subglyphs     = NULL;
    368     slot->control_data  = NULL;
    369     slot->control_len   = 0;
    370     slot->other         = NULL;
    371     slot->format        = FT_GLYPH_FORMAT_NONE;
    372 
    373     slot->linearHoriAdvance = 0;
    374     slot->linearVertAdvance = 0;
    375     slot->lsb_delta         = 0;
    376     slot->rsb_delta         = 0;
    377   }
    378 
    379 
    380   static void
    381   ft_glyphslot_done( FT_GlyphSlot  slot )
    382   {
    383     FT_Driver        driver = slot->face->driver;
    384     FT_Driver_Class  clazz  = driver->clazz;
    385     FT_Memory        memory = driver->root.memory;
    386 
    387 
    388     if ( clazz->done_slot )
    389       clazz->done_slot( slot );
    390 
    391     /* free bitmap buffer if needed */
    392     ft_glyphslot_free_bitmap( slot );
    393 
    394     /* slot->internal might be NULL in out-of-memory situations */
    395     if ( slot->internal )
    396     {
    397       /* free glyph loader */
    398       if ( FT_DRIVER_USES_OUTLINES( driver ) )
    399       {
    400         FT_GlyphLoader_Done( slot->internal->loader );
    401         slot->internal->loader = NULL;
    402       }
    403 
    404       FT_FREE( slot->internal );
    405     }
    406   }
    407 
    408 
    409   /* documentation is in ftobjs.h */
    410 
    411   FT_BASE_DEF( FT_Error )
    412   FT_New_GlyphSlot( FT_Face        face,
    413                     FT_GlyphSlot  *aslot )
    414   {
    415     FT_Error         error;
    416     FT_Driver        driver;
    417     FT_Driver_Class  clazz;
    418     FT_Memory        memory;
    419     FT_GlyphSlot     slot = NULL;
    420 
    421 
    422     if ( !face )
    423       return FT_THROW( Invalid_Face_Handle );
    424 
    425     if ( !face->driver )
    426       return FT_THROW( Invalid_Argument );
    427 
    428     driver = face->driver;
    429     clazz  = driver->clazz;
    430     memory = driver->root.memory;
    431 
    432     FT_TRACE4(( "FT_New_GlyphSlot: Creating new slot object\n" ));
    433     if ( !FT_ALLOC( slot, clazz->slot_object_size ) )
    434     {
    435       slot->face = face;
    436 
    437       error = ft_glyphslot_init( slot );
    438       if ( error )
    439       {
    440         ft_glyphslot_done( slot );
    441         FT_FREE( slot );
    442         goto Exit;
    443       }
    444 
    445       slot->next  = face->glyph;
    446       face->glyph = slot;
    447 
    448       if ( aslot )
    449         *aslot = slot;
    450     }
    451     else if ( aslot )
    452       *aslot = NULL;
    453 
    454 
    455   Exit:
    456     FT_TRACE4(( "FT_New_GlyphSlot: Return %d\n", error ));
    457     return error;
    458   }
    459 
    460 
    461   /* documentation is in ftobjs.h */
    462 
    463   FT_BASE_DEF( void )
    464   FT_Done_GlyphSlot( FT_GlyphSlot  slot )
    465   {
    466     if ( slot )
    467     {
    468       FT_Driver     driver = slot->face->driver;
    469       FT_Memory     memory = driver->root.memory;
    470       FT_GlyphSlot  prev;
    471       FT_GlyphSlot  cur;
    472 
    473 
    474       /* Remove slot from its parent face's list */
    475       prev = NULL;
    476       cur  = slot->face->glyph;
    477 
    478       while ( cur )
    479       {
    480         if ( cur == slot )
    481         {
    482           if ( !prev )
    483             slot->face->glyph = cur->next;
    484           else
    485             prev->next = cur->next;
    486 
    487           /* finalize client-specific data */
    488           if ( slot->generic.finalizer )
    489             slot->generic.finalizer( slot );
    490 
    491           ft_glyphslot_done( slot );
    492           FT_FREE( slot );
    493           break;
    494         }
    495         prev = cur;
    496         cur  = cur->next;
    497       }
    498     }
    499   }
    500 
    501 
    502   /* documentation is in freetype.h */
    503 
    504   FT_EXPORT_DEF( void )
    505   FT_Set_Transform( FT_Face     face,
    506                     FT_Matrix*  matrix,
    507                     FT_Vector*  delta )
    508   {
    509     FT_Face_Internal  internal;
    510 
    511 
    512     if ( !face )
    513       return;
    514 
    515     internal = face->internal;
    516 
    517     internal->transform_flags = 0;
    518 
    519     if ( !matrix )
    520     {
    521       internal->transform_matrix.xx = 0x10000L;
    522       internal->transform_matrix.xy = 0;
    523       internal->transform_matrix.yx = 0;
    524       internal->transform_matrix.yy = 0x10000L;
    525 
    526       matrix = &internal->transform_matrix;
    527     }
    528     else
    529       internal->transform_matrix = *matrix;
    530 
    531     /* set transform_flags bit flag 0 if `matrix' isn't the identity */
    532     if ( ( matrix->xy | matrix->yx ) ||
    533          matrix->xx != 0x10000L      ||
    534          matrix->yy != 0x10000L      )
    535       internal->transform_flags |= 1;
    536 
    537     if ( !delta )
    538     {
    539       internal->transform_delta.x = 0;
    540       internal->transform_delta.y = 0;
    541 
    542       delta = &internal->transform_delta;
    543     }
    544     else
    545       internal->transform_delta = *delta;
    546 
    547     /* set transform_flags bit flag 1 if `delta' isn't the null vector */
    548     if ( delta->x | delta->y )
    549       internal->transform_flags |= 2;
    550   }
    551 
    552 
    553   static FT_Renderer
    554   ft_lookup_glyph_renderer( FT_GlyphSlot  slot );
    555 
    556 
    557 #ifdef GRID_FIT_METRICS
    558   static void
    559   ft_glyphslot_grid_fit_metrics( FT_GlyphSlot  slot,
    560                                  FT_Bool       vertical )
    561   {
    562     FT_Glyph_Metrics*  metrics = &slot->metrics;
    563     FT_Pos             right, bottom;
    564 
    565 
    566     if ( vertical )
    567     {
    568       metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
    569       metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
    570 
    571       right  = FT_PIX_CEIL( metrics->vertBearingX + metrics->width );
    572       bottom = FT_PIX_CEIL( metrics->vertBearingY + metrics->height );
    573 
    574       metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
    575       metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
    576 
    577       metrics->width  = right - metrics->vertBearingX;
    578       metrics->height = bottom - metrics->vertBearingY;
    579     }
    580     else
    581     {
    582       metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX );
    583       metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY );
    584 
    585       right  = FT_PIX_CEIL ( metrics->horiBearingX + metrics->width );
    586       bottom = FT_PIX_FLOOR( metrics->horiBearingY - metrics->height );
    587 
    588       metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX );
    589       metrics->horiBearingY = FT_PIX_CEIL ( metrics->horiBearingY );
    590 
    591       metrics->width  = right - metrics->horiBearingX;
    592       metrics->height = metrics->horiBearingY - bottom;
    593     }
    594 
    595     metrics->horiAdvance = FT_PIX_ROUND( metrics->horiAdvance );
    596     metrics->vertAdvance = FT_PIX_ROUND( metrics->vertAdvance );
    597   }
    598 #endif /* GRID_FIT_METRICS */
    599 
    600 
    601   /* documentation is in freetype.h */
    602 
    603   FT_EXPORT_DEF( FT_Error )
    604   FT_Load_Glyph( FT_Face   face,
    605                  FT_UInt   glyph_index,
    606                  FT_Int32  load_flags )
    607   {
    608     FT_Error      error;
    609     FT_Driver     driver;
    610     FT_GlyphSlot  slot;
    611     FT_Library    library;
    612     FT_Bool       autohint = FALSE;
    613     FT_Module     hinter;
    614     TT_Face       ttface = (TT_Face)face;
    615 
    616 
    617     if ( !face || !face->size || !face->glyph )
    618       return FT_THROW( Invalid_Face_Handle );
    619 
    620     /* The validity test for `glyph_index' is performed by the */
    621     /* font drivers.                                           */
    622 
    623     slot = face->glyph;
    624     ft_glyphslot_clear( slot );
    625 
    626     driver  = face->driver;
    627     library = driver->root.library;
    628     hinter  = library->auto_hinter;
    629 
    630     /* resolve load flags dependencies */
    631 
    632     if ( load_flags & FT_LOAD_NO_RECURSE )
    633       load_flags |= FT_LOAD_NO_SCALE         |
    634                     FT_LOAD_IGNORE_TRANSFORM;
    635 
    636     if ( load_flags & FT_LOAD_NO_SCALE )
    637     {
    638       load_flags |= FT_LOAD_NO_HINTING |
    639                     FT_LOAD_NO_BITMAP;
    640 
    641       load_flags &= ~FT_LOAD_RENDER;
    642     }
    643 
    644     /*
    645      * Determine whether we need to auto-hint or not.
    646      * The general rules are:
    647      *
    648      * - Do only auto-hinting if we have a hinter module, a scalable font
    649      *   format dealing with outlines, and no transforms except simple
    650      *   slants and/or rotations by integer multiples of 90 degrees.
    651      *
    652      * - Then, auto-hint if FT_LOAD_FORCE_AUTOHINT is set or if we don't
    653      *   have a native font hinter.
    654      *
    655      * - Otherwise, auto-hint for LIGHT hinting mode or if there isn't
    656      *   any hinting bytecode in the TrueType/OpenType font.
    657      *
    658      * - Exception: The font is `tricky' and requires the native hinter to
    659      *   load properly.
    660      */
    661 
    662     if ( hinter                                           &&
    663          !( load_flags & FT_LOAD_NO_HINTING )             &&
    664          !( load_flags & FT_LOAD_NO_AUTOHINT )            &&
    665          FT_DRIVER_IS_SCALABLE( driver )                  &&
    666          FT_DRIVER_USES_OUTLINES( driver )                &&
    667          !FT_IS_TRICKY( face )                            &&
    668          ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM )    ||
    669            ( face->internal->transform_matrix.yx == 0 &&
    670              face->internal->transform_matrix.xx != 0 ) ||
    671            ( face->internal->transform_matrix.xx == 0 &&
    672              face->internal->transform_matrix.yx != 0 ) ) )
    673     {
    674       if ( ( load_flags & FT_LOAD_FORCE_AUTOHINT ) ||
    675            !FT_DRIVER_HAS_HINTER( driver )         )
    676         autohint = TRUE;
    677       else
    678       {
    679         FT_Render_Mode  mode = FT_LOAD_TARGET_MODE( load_flags );
    680 
    681 
    682         /* the check for `num_locations' assures that we actually    */
    683         /* test for instructions in a TTF and not in a CFF-based OTF */
    684         /*                                                           */
    685         /* since `maxSizeOfInstructions' might be unreliable, we     */
    686         /* check the size of the `fpgm' and `prep' tables, too --    */
    687         /* the assumption is that there don't exist real TTFs where  */
    688         /* both `fpgm' and `prep' tables are missing                 */
    689         if ( ( mode == FT_RENDER_MODE_LIGHT                   &&
    690                !FT_DRIVER_HINTS_LIGHTLY( driver ) )             ||
    691              face->internal->ignore_unpatented_hinter           ||
    692              ( FT_IS_SFNT( face )                             &&
    693                ttface->num_locations                          &&
    694                ttface->max_profile.maxSizeOfInstructions == 0 &&
    695                ttface->font_program_size == 0                 &&
    696                ttface->cvt_program_size == 0                  ) )
    697           autohint = TRUE;
    698       }
    699     }
    700 
    701     if ( autohint )
    702     {
    703       FT_AutoHinter_Interface  hinting;
    704 
    705 
    706       /* try to load embedded bitmaps first if available            */
    707       /*                                                            */
    708       /* XXX: This is really a temporary hack that should disappear */
    709       /*      promptly with FreeType 2.1!                           */
    710       /*                                                            */
    711       if ( FT_HAS_FIXED_SIZES( face )             &&
    712           ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
    713       {
    714         error = driver->clazz->load_glyph( slot, face->size,
    715                                            glyph_index,
    716                                            load_flags | FT_LOAD_SBITS_ONLY );
    717 
    718         if ( !error && slot->format == FT_GLYPH_FORMAT_BITMAP )
    719           goto Load_Ok;
    720       }
    721 
    722       {
    723         FT_Face_Internal  internal        = face->internal;
    724         FT_Int            transform_flags = internal->transform_flags;
    725 
    726 
    727         /* since the auto-hinter calls FT_Load_Glyph by itself, */
    728         /* make sure that glyphs aren't transformed             */
    729         internal->transform_flags = 0;
    730 
    731         /* load auto-hinted outline */
    732         hinting = (FT_AutoHinter_Interface)hinter->clazz->module_interface;
    733 
    734         error   = hinting->load_glyph( (FT_AutoHinter)hinter,
    735                                        slot, face->size,
    736                                        glyph_index, load_flags );
    737 
    738         internal->transform_flags = transform_flags;
    739       }
    740     }
    741     else
    742     {
    743       error = driver->clazz->load_glyph( slot,
    744                                          face->size,
    745                                          glyph_index,
    746                                          load_flags );
    747       if ( error )
    748         goto Exit;
    749 
    750       if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
    751       {
    752         /* check that the loaded outline is correct */
    753         error = FT_Outline_Check( &slot->outline );
    754         if ( error )
    755           goto Exit;
    756 
    757 #ifdef GRID_FIT_METRICS
    758         if ( !( load_flags & FT_LOAD_NO_HINTING ) )
    759           ft_glyphslot_grid_fit_metrics( slot,
    760               FT_BOOL( load_flags & FT_LOAD_VERTICAL_LAYOUT ) );
    761 #endif
    762       }
    763     }
    764 
    765   Load_Ok:
    766     /* compute the advance */
    767     if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
    768     {
    769       slot->advance.x = 0;
    770       slot->advance.y = slot->metrics.vertAdvance;
    771     }
    772     else
    773     {
    774       slot->advance.x = slot->metrics.horiAdvance;
    775       slot->advance.y = 0;
    776     }
    777 
    778     /* compute the linear advance in 16.16 pixels */
    779     if ( ( load_flags & FT_LOAD_LINEAR_DESIGN ) == 0 &&
    780          ( FT_IS_SCALABLE( face ) )                  )
    781     {
    782       FT_Size_Metrics*  metrics = &face->size->metrics;
    783 
    784 
    785       /* it's tricky! */
    786       slot->linearHoriAdvance = FT_MulDiv( slot->linearHoriAdvance,
    787                                            metrics->x_scale, 64 );
    788 
    789       slot->linearVertAdvance = FT_MulDiv( slot->linearVertAdvance,
    790                                            metrics->y_scale, 64 );
    791     }
    792 
    793     if ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) == 0 )
    794     {
    795       FT_Face_Internal  internal = face->internal;
    796 
    797 
    798       /* now, transform the glyph image if needed */
    799       if ( internal->transform_flags )
    800       {
    801         /* get renderer */
    802         FT_Renderer  renderer = ft_lookup_glyph_renderer( slot );
    803 
    804 
    805         if ( renderer )
    806           error = renderer->clazz->transform_glyph(
    807                                      renderer, slot,
    808                                      &internal->transform_matrix,
    809                                      &internal->transform_delta );
    810         else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
    811         {
    812           /* apply `standard' transformation if no renderer is available */
    813           if ( internal->transform_flags & 1 )
    814             FT_Outline_Transform( &slot->outline,
    815                                   &internal->transform_matrix );
    816 
    817           if ( internal->transform_flags & 2 )
    818             FT_Outline_Translate( &slot->outline,
    819                                   internal->transform_delta.x,
    820                                   internal->transform_delta.y );
    821         }
    822 
    823         /* transform advance */
    824         FT_Vector_Transform( &slot->advance, &internal->transform_matrix );
    825       }
    826     }
    827 
    828     FT_TRACE5(( "  x advance: %d\n" , slot->advance.x ));
    829     FT_TRACE5(( "  y advance: %d\n" , slot->advance.y ));
    830 
    831     FT_TRACE5(( "  linear x advance: %d\n" , slot->linearHoriAdvance ));
    832     FT_TRACE5(( "  linear y advance: %d\n" , slot->linearVertAdvance ));
    833 
    834     /* do we need to render the image now? */
    835     if ( !error                                    &&
    836          slot->format != FT_GLYPH_FORMAT_BITMAP    &&
    837          slot->format != FT_GLYPH_FORMAT_COMPOSITE &&
    838          load_flags & FT_LOAD_RENDER )
    839     {
    840       FT_Render_Mode  mode = FT_LOAD_TARGET_MODE( load_flags );
    841 
    842 
    843       if ( mode == FT_RENDER_MODE_NORMAL      &&
    844            (load_flags & FT_LOAD_MONOCHROME ) )
    845         mode = FT_RENDER_MODE_MONO;
    846 
    847       error = FT_Render_Glyph( slot, mode );
    848     }
    849 
    850   Exit:
    851     return error;
    852   }
    853 
    854 
    855   /* documentation is in freetype.h */
    856 
    857   FT_EXPORT_DEF( FT_Error )
    858   FT_Load_Char( FT_Face   face,
    859                 FT_ULong  char_code,
    860                 FT_Int32  load_flags )
    861   {
    862     FT_UInt  glyph_index;
    863 
    864 
    865     if ( !face )
    866       return FT_THROW( Invalid_Face_Handle );
    867 
    868     glyph_index = (FT_UInt)char_code;
    869     if ( face->charmap )
    870       glyph_index = FT_Get_Char_Index( face, char_code );
    871 
    872     return FT_Load_Glyph( face, glyph_index, load_flags );
    873   }
    874 
    875 
    876   /* destructor for sizes list */
    877   static void
    878   destroy_size( FT_Memory  memory,
    879                 FT_Size    size,
    880                 FT_Driver  driver )
    881   {
    882     /* finalize client-specific data */
    883     if ( size->generic.finalizer )
    884       size->generic.finalizer( size );
    885 
    886     /* finalize format-specific stuff */
    887     if ( driver->clazz->done_size )
    888       driver->clazz->done_size( size );
    889 
    890     FT_FREE( size->internal );
    891     FT_FREE( size );
    892   }
    893 
    894 
    895   static void
    896   ft_cmap_done_internal( FT_CMap  cmap );
    897 
    898 
    899   static void
    900   destroy_charmaps( FT_Face    face,
    901                     FT_Memory  memory )
    902   {
    903     FT_Int  n;
    904 
    905 
    906     if ( !face )
    907       return;
    908 
    909     for ( n = 0; n < face->num_charmaps; n++ )
    910     {
    911       FT_CMap  cmap = FT_CMAP( face->charmaps[n] );
    912 
    913 
    914       ft_cmap_done_internal( cmap );
    915 
    916       face->charmaps[n] = NULL;
    917     }
    918 
    919     FT_FREE( face->charmaps );
    920     face->num_charmaps = 0;
    921   }
    922 
    923 
    924   /* destructor for faces list */
    925   static void
    926   destroy_face( FT_Memory  memory,
    927                 FT_Face    face,
    928                 FT_Driver  driver )
    929   {
    930     FT_Driver_Class  clazz = driver->clazz;
    931 
    932 
    933     /* discard auto-hinting data */
    934     if ( face->autohint.finalizer )
    935       face->autohint.finalizer( face->autohint.data );
    936 
    937     /* Discard glyph slots for this face.                           */
    938     /* Beware!  FT_Done_GlyphSlot() changes the field `face->glyph' */
    939     while ( face->glyph )
    940       FT_Done_GlyphSlot( face->glyph );
    941 
    942     /* discard all sizes for this face */
    943     FT_List_Finalize( &face->sizes_list,
    944                       (FT_List_Destructor)destroy_size,
    945                       memory,
    946                       driver );
    947     face->size = NULL;
    948 
    949     /* now discard client data */
    950     if ( face->generic.finalizer )
    951       face->generic.finalizer( face );
    952 
    953     /* discard charmaps */
    954     destroy_charmaps( face, memory );
    955 
    956     /* finalize format-specific stuff */
    957     if ( clazz->done_face )
    958       clazz->done_face( face );
    959 
    960     /* close the stream for this face if needed */
    961     FT_Stream_Free(
    962       face->stream,
    963       ( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
    964 
    965     face->stream = NULL;
    966 
    967     /* get rid of it */
    968     if ( face->internal )
    969     {
    970       FT_FREE( face->internal );
    971     }
    972     FT_FREE( face );
    973   }
    974 
    975 
    976   static void
    977   Destroy_Driver( FT_Driver  driver )
    978   {
    979     FT_List_Finalize( &driver->faces_list,
    980                       (FT_List_Destructor)destroy_face,
    981                       driver->root.memory,
    982                       driver );
    983   }
    984 
    985 
    986   /*************************************************************************/
    987   /*                                                                       */
    988   /* <Function>                                                            */
    989   /*    find_unicode_charmap                                               */
    990   /*                                                                       */
    991   /* <Description>                                                         */
    992   /*    This function finds a Unicode charmap, if there is one.            */
    993   /*    And if there is more than one, it tries to favour the more         */
    994   /*    extensive one, i.e., one that supports UCS-4 against those which   */
    995   /*    are limited to the BMP (said UCS-2 encoding.)                      */
    996   /*                                                                       */
    997   /*    This function is called from open_face() (just below), and also    */
    998   /*    from FT_Select_Charmap( ..., FT_ENCODING_UNICODE ).                */
    999   /*                                                                       */
   1000   static FT_Error
   1001   find_unicode_charmap( FT_Face  face )
   1002   {
   1003     FT_CharMap*  first;
   1004     FT_CharMap*  cur;
   1005 
   1006 
   1007     /* caller should have already checked that `face' is valid */
   1008     FT_ASSERT( face );
   1009 
   1010     first = face->charmaps;
   1011 
   1012     if ( !first )
   1013       return FT_THROW( Invalid_CharMap_Handle );
   1014 
   1015     /*
   1016      *  The original TrueType specification(s) only specified charmap
   1017      *  formats that are capable of mapping 8 or 16 bit character codes to
   1018      *  glyph indices.
   1019      *
   1020      *  However, recent updates to the Apple and OpenType specifications
   1021      *  introduced new formats that are capable of mapping 32-bit character
   1022      *  codes as well.  And these are already used on some fonts, mainly to
   1023      *  map non-BMP Asian ideographs as defined in Unicode.
   1024      *
   1025      *  For compatibility purposes, these fonts generally come with
   1026      *  *several* Unicode charmaps:
   1027      *
   1028      *   - One of them in the "old" 16-bit format, that cannot access
   1029      *     all glyphs in the font.
   1030      *
   1031      *   - Another one in the "new" 32-bit format, that can access all
   1032      *     the glyphs.
   1033      *
   1034      *  This function has been written to always favor a 32-bit charmap
   1035      *  when found.  Otherwise, a 16-bit one is returned when found.
   1036      */
   1037 
   1038     /* Since the `interesting' table, with IDs (3,10), is normally the */
   1039     /* last one, we loop backwards.  This loses with type1 fonts with  */
   1040     /* non-BMP characters (<.0001%), this wins with .ttf with non-BMP  */
   1041     /* chars (.01% ?), and this is the same about 99.99% of the time!  */
   1042 
   1043     cur = first + face->num_charmaps;  /* points after the last one */
   1044 
   1045     for ( ; --cur >= first; )
   1046     {
   1047       if ( cur[0]->encoding == FT_ENCODING_UNICODE )
   1048       {
   1049         /* XXX If some new encodings to represent UCS-4 are added, */
   1050         /*     they should be added here.                          */
   1051         if ( ( cur[0]->platform_id == TT_PLATFORM_MICROSOFT &&
   1052                cur[0]->encoding_id == TT_MS_ID_UCS_4        )     ||
   1053              ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
   1054                cur[0]->encoding_id == TT_APPLE_ID_UNICODE_32    ) )
   1055         {
   1056           face->charmap = cur[0];
   1057           return FT_Err_Ok;
   1058         }
   1059       }
   1060     }
   1061 
   1062     /* We do not have any UCS-4 charmap.                */
   1063     /* Do the loop again and search for UCS-2 charmaps. */
   1064     cur = first + face->num_charmaps;
   1065 
   1066     for ( ; --cur >= first; )
   1067     {
   1068       if ( cur[0]->encoding == FT_ENCODING_UNICODE )
   1069       {
   1070         face->charmap = cur[0];
   1071         return FT_Err_Ok;
   1072       }
   1073     }
   1074 
   1075     return FT_THROW( Invalid_CharMap_Handle );
   1076   }
   1077 
   1078 
   1079   /*************************************************************************/
   1080   /*                                                                       */
   1081   /* <Function>                                                            */
   1082   /*    find_variant_selector_charmap                                      */
   1083   /*                                                                       */
   1084   /* <Description>                                                         */
   1085   /*    This function finds the variant selector charmap, if there is one. */
   1086   /*    There can only be one (platform=0, specific=5, format=14).         */
   1087   /*                                                                       */
   1088   static FT_CharMap
   1089   find_variant_selector_charmap( FT_Face  face )
   1090   {
   1091     FT_CharMap*  first;
   1092     FT_CharMap*  end;
   1093     FT_CharMap*  cur;
   1094 
   1095 
   1096     /* caller should have already checked that `face' is valid */
   1097     FT_ASSERT( face );
   1098 
   1099     first = face->charmaps;
   1100 
   1101     if ( !first )
   1102       return NULL;
   1103 
   1104     end = first + face->num_charmaps;  /* points after the last one */
   1105 
   1106     for ( cur = first; cur < end; ++cur )
   1107     {
   1108       if ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE    &&
   1109            cur[0]->encoding_id == TT_APPLE_ID_VARIANT_SELECTOR &&
   1110            FT_Get_CMap_Format( cur[0] ) == 14                  )
   1111         return cur[0];
   1112     }
   1113 
   1114     return NULL;
   1115   }
   1116 
   1117 
   1118   /*************************************************************************/
   1119   /*                                                                       */
   1120   /* <Function>                                                            */
   1121   /*    open_face                                                          */
   1122   /*                                                                       */
   1123   /* <Description>                                                         */
   1124   /*    This function does some work for FT_Open_Face().                   */
   1125   /*                                                                       */
   1126   static FT_Error
   1127   open_face( FT_Driver      driver,
   1128              FT_Stream      *astream,
   1129              FT_Bool        external_stream,
   1130              FT_Long        face_index,
   1131              FT_Int         num_params,
   1132              FT_Parameter*  params,
   1133              FT_Face       *aface )
   1134   {
   1135     FT_Memory         memory;
   1136     FT_Driver_Class   clazz;
   1137     FT_Face           face     = NULL;
   1138     FT_Face_Internal  internal = NULL;
   1139 
   1140     FT_Error          error, error2;
   1141 
   1142 
   1143     clazz  = driver->clazz;
   1144     memory = driver->root.memory;
   1145 
   1146     /* allocate the face object and perform basic initialization */
   1147     if ( FT_ALLOC( face, clazz->face_object_size ) )
   1148       goto Fail;
   1149 
   1150     face->driver = driver;
   1151     face->memory = memory;
   1152     face->stream = *astream;
   1153 
   1154     /* set the FT_FACE_FLAG_EXTERNAL_STREAM bit for FT_Done_Face */
   1155     if ( external_stream )
   1156       face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
   1157 
   1158     if ( FT_NEW( internal ) )
   1159       goto Fail;
   1160 
   1161     face->internal = internal;
   1162 
   1163 #ifdef FT_CONFIG_OPTION_INCREMENTAL
   1164     {
   1165       int  i;
   1166 
   1167 
   1168       face->internal->incremental_interface = NULL;
   1169       for ( i = 0; i < num_params && !face->internal->incremental_interface;
   1170             i++ )
   1171         if ( params[i].tag == FT_PARAM_TAG_INCREMENTAL )
   1172           face->internal->incremental_interface =
   1173             (FT_Incremental_Interface)params[i].data;
   1174     }
   1175 #endif
   1176 
   1177     if ( clazz->init_face )
   1178       error = clazz->init_face( *astream,
   1179                                 face,
   1180                                 (FT_Int)face_index,
   1181                                 num_params,
   1182                                 params );
   1183     *astream = face->stream; /* Stream may have been changed. */
   1184     if ( error )
   1185       goto Fail;
   1186 
   1187     /* select Unicode charmap by default */
   1188     error2 = find_unicode_charmap( face );
   1189 
   1190     /* if no Unicode charmap can be found, FT_Err_Invalid_CharMap_Handle */
   1191     /* is returned.                                                      */
   1192 
   1193     /* no error should happen, but we want to play safe */
   1194     if ( error2 && FT_ERR_NEQ( error2, Invalid_CharMap_Handle ) )
   1195     {
   1196       error = error2;
   1197       goto Fail;
   1198     }
   1199 
   1200     *aface = face;
   1201 
   1202   Fail:
   1203     if ( error )
   1204     {
   1205       destroy_charmaps( face, memory );
   1206       if ( clazz->done_face )
   1207         clazz->done_face( face );
   1208       FT_FREE( internal );
   1209       FT_FREE( face );
   1210       *aface = NULL;
   1211     }
   1212 
   1213     return error;
   1214   }
   1215 
   1216 
   1217   /* there's a Mac-specific extended implementation of FT_New_Face() */
   1218   /* in src/base/ftmac.c                                             */
   1219 
   1220 #ifndef FT_MACINTOSH
   1221 
   1222   /* documentation is in freetype.h */
   1223 
   1224   FT_EXPORT_DEF( FT_Error )
   1225   FT_New_Face( FT_Library   library,
   1226                const char*  pathname,
   1227                FT_Long      face_index,
   1228                FT_Face     *aface )
   1229   {
   1230     FT_Open_Args  args;
   1231 
   1232 
   1233     /* test for valid `library' and `aface' delayed to `FT_Open_Face' */
   1234     if ( !pathname )
   1235       return FT_THROW( Invalid_Argument );
   1236 
   1237     args.flags    = FT_OPEN_PATHNAME;
   1238     args.pathname = (char*)pathname;
   1239     args.stream   = NULL;
   1240 
   1241     return FT_Open_Face( library, &args, face_index, aface );
   1242   }
   1243 
   1244 #endif
   1245 
   1246 
   1247   /* documentation is in freetype.h */
   1248 
   1249   FT_EXPORT_DEF( FT_Error )
   1250   FT_New_Memory_Face( FT_Library      library,
   1251                       const FT_Byte*  file_base,
   1252                       FT_Long         file_size,
   1253                       FT_Long         face_index,
   1254                       FT_Face        *aface )
   1255   {
   1256     FT_Open_Args  args;
   1257 
   1258 
   1259     /* test for valid `library' and `face' delayed to `FT_Open_Face' */
   1260     if ( !file_base )
   1261       return FT_THROW( Invalid_Argument );
   1262 
   1263     args.flags       = FT_OPEN_MEMORY;
   1264     args.memory_base = file_base;
   1265     args.memory_size = file_size;
   1266     args.stream      = NULL;
   1267 
   1268     return FT_Open_Face( library, &args, face_index, aface );
   1269   }
   1270 
   1271 
   1272 #ifdef FT_CONFIG_OPTION_MAC_FONTS
   1273 
   1274   /* The behavior here is very similar to that in base/ftmac.c, but it     */
   1275   /* is designed to work on non-mac systems, so no mac specific calls.     */
   1276   /*                                                                       */
   1277   /* We look at the file and determine if it is a mac dfont file or a mac  */
   1278   /* resource file, or a macbinary file containing a mac resource file.    */
   1279   /*                                                                       */
   1280   /* Unlike ftmac I'm not going to look at a `FOND'.  I don't really see   */
   1281   /* the point, especially since there may be multiple `FOND' resources.   */
   1282   /* Instead I'll just look for `sfnt' and `POST' resources, ordered as    */
   1283   /* they occur in the file.                                               */
   1284   /*                                                                       */
   1285   /* Note that multiple `POST' resources do not mean multiple postscript   */
   1286   /* fonts; they all get jammed together to make what is essentially a     */
   1287   /* pfb file.                                                             */
   1288   /*                                                                       */
   1289   /* We aren't interested in `NFNT' or `FONT' bitmap resources.            */
   1290   /*                                                                       */
   1291   /* As soon as we get an `sfnt' load it into memory and pass it off to    */
   1292   /* FT_Open_Face.                                                         */
   1293   /*                                                                       */
   1294   /* If we have a (set of) `POST' resources, massage them into a (memory)  */
   1295   /* pfb file and pass that to FT_Open_Face.  (As with ftmac.c I'm not     */
   1296   /* going to try to save the kerning info.  After all that lives in the   */
   1297   /* `FOND' which isn't in the file containing the `POST' resources so     */
   1298   /* we don't really have access to it.                                    */
   1299 
   1300 
   1301   /* Finalizer for a memory stream; gets called by FT_Done_Face(). */
   1302   /* It frees the memory it uses.                                  */
   1303   /* From ftmac.c.                                                 */
   1304   static void
   1305   memory_stream_close( FT_Stream  stream )
   1306   {
   1307     FT_Memory  memory = stream->memory;
   1308 
   1309 
   1310     FT_FREE( stream->base );
   1311 
   1312     stream->size  = 0;
   1313     stream->base  = NULL;
   1314     stream->close = NULL;
   1315   }
   1316 
   1317 
   1318   /* Create a new memory stream from a buffer and a size. */
   1319   /* From ftmac.c.                                        */
   1320   static FT_Error
   1321   new_memory_stream( FT_Library           library,
   1322                      FT_Byte*             base,
   1323                      FT_ULong             size,
   1324                      FT_Stream_CloseFunc  close,
   1325                      FT_Stream           *astream )
   1326   {
   1327     FT_Error   error;
   1328     FT_Memory  memory;
   1329     FT_Stream  stream = NULL;
   1330 
   1331 
   1332     if ( !library )
   1333       return FT_THROW( Invalid_Library_Handle );
   1334 
   1335     if ( !base )
   1336       return FT_THROW( Invalid_Argument );
   1337 
   1338     *astream = NULL;
   1339     memory = library->memory;
   1340     if ( FT_NEW( stream ) )
   1341       goto Exit;
   1342 
   1343     FT_Stream_OpenMemory( stream, base, size );
   1344 
   1345     stream->close = close;
   1346 
   1347     *astream = stream;
   1348 
   1349   Exit:
   1350     return error;
   1351   }
   1352 
   1353 
   1354   /* Create a new FT_Face given a buffer and a driver name. */
   1355   /* from ftmac.c */
   1356   FT_LOCAL_DEF( FT_Error )
   1357   open_face_from_buffer( FT_Library   library,
   1358                          FT_Byte*     base,
   1359                          FT_ULong     size,
   1360                          FT_Long      face_index,
   1361                          const char*  driver_name,
   1362                          FT_Face     *aface )
   1363   {
   1364     FT_Open_Args  args;
   1365     FT_Error      error;
   1366     FT_Stream     stream = NULL;
   1367     FT_Memory     memory = library->memory;
   1368 
   1369 
   1370     error = new_memory_stream( library,
   1371                                base,
   1372                                size,
   1373                                memory_stream_close,
   1374                                &stream );
   1375     if ( error )
   1376     {
   1377       FT_FREE( base );
   1378       return error;
   1379     }
   1380 
   1381     args.flags = FT_OPEN_STREAM;
   1382     args.stream = stream;
   1383     if ( driver_name )
   1384     {
   1385       args.flags = args.flags | FT_OPEN_DRIVER;
   1386       args.driver = FT_Get_Module( library, driver_name );
   1387     }
   1388 
   1389 #ifdef FT_MACINTOSH
   1390     /* At this point, the face index has served its purpose;  */
   1391     /* whoever calls this function has already used it to     */
   1392     /* locate the correct font data.  We should not propagate */
   1393     /* this index to FT_Open_Face() (unless it is negative).  */
   1394 
   1395     if ( face_index > 0 )
   1396       face_index &= 0x7FFF0000L; /* retain GX data */
   1397 #endif
   1398 
   1399     error = FT_Open_Face( library, &args, face_index, aface );
   1400 
   1401     if ( error == FT_Err_Ok )
   1402       (*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
   1403     else
   1404 #ifdef FT_MACINTOSH
   1405       FT_Stream_Free( stream, 0 );
   1406 #else
   1407     {
   1408       FT_Stream_Close( stream );
   1409       FT_FREE( stream );
   1410     }
   1411 #endif
   1412 
   1413     return error;
   1414   }
   1415 
   1416 
   1417   /* Look up `TYP1' or `CID ' table from sfnt table directory.       */
   1418   /* `offset' and `length' must exclude the binary header in tables. */
   1419 
   1420   /* Type 1 and CID-keyed font drivers should recognize sfnt-wrapped */
   1421   /* format too.  Here, since we can't expect that the TrueType font */
   1422   /* driver is loaded unconditially, we must parse the font by       */
   1423   /* ourselves.  We are only interested in the name of the table and */
   1424   /* the offset.                                                     */
   1425 
   1426   static FT_Error
   1427   ft_lookup_PS_in_sfnt_stream( FT_Stream  stream,
   1428                                FT_Long    face_index,
   1429                                FT_ULong*  offset,
   1430                                FT_ULong*  length,
   1431                                FT_Bool*   is_sfnt_cid )
   1432   {
   1433     FT_Error   error;
   1434     FT_UShort  numTables;
   1435     FT_Long    pstable_index;
   1436     FT_ULong   tag;
   1437     int        i;
   1438 
   1439 
   1440     *offset = 0;
   1441     *length = 0;
   1442     *is_sfnt_cid = FALSE;
   1443 
   1444     /* TODO: support for sfnt-wrapped PS/CID in TTC format */
   1445 
   1446     /* version check for 'typ1' (should be ignored?) */
   1447     if ( FT_READ_ULONG( tag ) )
   1448       return error;
   1449     if ( tag != TTAG_typ1 )
   1450       return FT_THROW( Unknown_File_Format );
   1451 
   1452     if ( FT_READ_USHORT( numTables ) )
   1453       return error;
   1454     if ( FT_STREAM_SKIP( 2 * 3 ) ) /* skip binary search header */
   1455       return error;
   1456 
   1457     pstable_index = -1;
   1458     *is_sfnt_cid  = FALSE;
   1459 
   1460     for ( i = 0; i < numTables; i++ )
   1461     {
   1462       if ( FT_READ_ULONG( tag )     || FT_STREAM_SKIP( 4 )      ||
   1463            FT_READ_ULONG( *offset ) || FT_READ_ULONG( *length ) )
   1464         return error;
   1465 
   1466       if ( tag == TTAG_CID )
   1467       {
   1468         pstable_index++;
   1469         *offset += 22;
   1470         *length -= 22;
   1471         *is_sfnt_cid = TRUE;
   1472         if ( face_index < 0 )
   1473           return FT_Err_Ok;
   1474       }
   1475       else if ( tag == TTAG_TYP1 )
   1476       {
   1477         pstable_index++;
   1478         *offset += 24;
   1479         *length -= 24;
   1480         *is_sfnt_cid = FALSE;
   1481         if ( face_index < 0 )
   1482           return FT_Err_Ok;
   1483       }
   1484       if ( face_index >= 0 && pstable_index == face_index )
   1485         return FT_Err_Ok;
   1486     }
   1487     return FT_THROW( Table_Missing );
   1488   }
   1489 
   1490 
   1491   FT_LOCAL_DEF( FT_Error )
   1492   open_face_PS_from_sfnt_stream( FT_Library     library,
   1493                                  FT_Stream      stream,
   1494                                  FT_Long        face_index,
   1495                                  FT_Int         num_params,
   1496                                  FT_Parameter  *params,
   1497                                  FT_Face       *aface )
   1498   {
   1499     FT_Error   error;
   1500     FT_Memory  memory = library->memory;
   1501     FT_ULong   offset, length;
   1502     FT_ULong   pos;
   1503     FT_Bool    is_sfnt_cid;
   1504     FT_Byte*   sfnt_ps = NULL;
   1505 
   1506     FT_UNUSED( num_params );
   1507     FT_UNUSED( params );
   1508 
   1509 
   1510     /* ignore GX stuff */
   1511     if ( face_index > 0 )
   1512       face_index &= 0xFFFFL;
   1513 
   1514     pos = FT_STREAM_POS();
   1515 
   1516     error = ft_lookup_PS_in_sfnt_stream( stream,
   1517                                          face_index,
   1518                                          &offset,
   1519                                          &length,
   1520                                          &is_sfnt_cid );
   1521     if ( error )
   1522       goto Exit;
   1523 
   1524     if ( FT_Stream_Seek( stream, pos + offset ) )
   1525       goto Exit;
   1526 
   1527     if ( FT_ALLOC( sfnt_ps, (FT_Long)length ) )
   1528       goto Exit;
   1529 
   1530     error = FT_Stream_Read( stream, (FT_Byte *)sfnt_ps, length );
   1531     if ( error ) {
   1532       FT_FREE( sfnt_ps );
   1533       goto Exit;
   1534     }
   1535 
   1536     error = open_face_from_buffer( library,
   1537                                    sfnt_ps,
   1538                                    length,
   1539                                    FT_MIN( face_index, 0 ),
   1540                                    is_sfnt_cid ? "cid" : "type1",
   1541                                    aface );
   1542   Exit:
   1543     {
   1544       FT_Error  error1;
   1545 
   1546 
   1547       if ( FT_ERR_EQ( error, Unknown_File_Format ) )
   1548       {
   1549         error1 = FT_Stream_Seek( stream, pos );
   1550         if ( error1 )
   1551           return error1;
   1552       }
   1553 
   1554       return error;
   1555     }
   1556   }
   1557 
   1558 
   1559 #ifndef FT_MACINTOSH
   1560 
   1561   /* The resource header says we've got resource_cnt `POST' (type1) */
   1562   /* resources in this file.  They all need to be coalesced into    */
   1563   /* one lump which gets passed on to the type1 driver.             */
   1564   /* Here can be only one PostScript font in a file so face_index   */
   1565   /* must be 0 (or -1).                                             */
   1566   /*                                                                */
   1567   static FT_Error
   1568   Mac_Read_POST_Resource( FT_Library  library,
   1569                           FT_Stream   stream,
   1570                           FT_Long    *offsets,
   1571                           FT_Long     resource_cnt,
   1572                           FT_Long     face_index,
   1573                           FT_Face    *aface )
   1574   {
   1575     FT_Error   error  = FT_ERR( Cannot_Open_Resource );
   1576     FT_Memory  memory = library->memory;
   1577     FT_Byte*   pfb_data = NULL;
   1578     int        i, type, flags;
   1579     FT_ULong   len;
   1580     FT_ULong   pfb_len, pfb_pos, pfb_lenpos;
   1581     FT_ULong   rlen, temp;
   1582 
   1583 
   1584     if ( face_index == -1 )
   1585       face_index = 0;
   1586     if ( face_index != 0 )
   1587       return error;
   1588 
   1589     /* Find the length of all the POST resources, concatenated.  Assume */
   1590     /* worst case (each resource in its own section).                   */
   1591     pfb_len = 0;
   1592     for ( i = 0; i < resource_cnt; ++i )
   1593     {
   1594       error = FT_Stream_Seek( stream, (FT_ULong)offsets[i] );
   1595       if ( error )
   1596         goto Exit;
   1597       if ( FT_READ_ULONG( temp ) )
   1598         goto Exit;
   1599 
   1600       /* FT2 allocator takes signed long buffer length,
   1601        * too large value causing overflow should be checked
   1602        */
   1603       FT_TRACE4(( "                 POST fragment #%d: length=0x%08x"
   1604                   " total pfb_len=0x%08x\n",
   1605                   i, temp, pfb_len + temp + 6));
   1606       if ( FT_MAC_RFORK_MAX_LEN < temp               ||
   1607            FT_MAC_RFORK_MAX_LEN - temp < pfb_len + 6 )
   1608       {
   1609         FT_TRACE2(( "             MacOS resource length cannot exceed"
   1610                     " 0x%08x\n", FT_MAC_RFORK_MAX_LEN ));
   1611         error = FT_THROW( Invalid_Offset );
   1612         goto Exit;
   1613       }
   1614 
   1615       pfb_len += temp + 6;
   1616     }
   1617 
   1618     FT_TRACE2(( "             total buffer size to concatenate %d"
   1619                 " POST fragments: 0x%08x\n",
   1620                  resource_cnt, pfb_len + 2));
   1621     if ( pfb_len + 2 < 6 ) {
   1622       FT_TRACE2(( "             too long fragment length makes"
   1623                   " pfb_len confused: pfb_len=0x%08x\n", pfb_len ));
   1624       error = FT_THROW( Array_Too_Large );
   1625       goto Exit;
   1626     }
   1627     if ( FT_ALLOC( pfb_data, (FT_Long)pfb_len + 2 ) )
   1628       goto Exit;
   1629 
   1630     pfb_data[0] = 0x80;
   1631     pfb_data[1] = 1;            /* Ascii section */
   1632     pfb_data[2] = 0;            /* 4-byte length, fill in later */
   1633     pfb_data[3] = 0;
   1634     pfb_data[4] = 0;
   1635     pfb_data[5] = 0;
   1636     pfb_pos     = 6;
   1637     pfb_lenpos  = 2;
   1638 
   1639     len = 0;
   1640     type = 1;
   1641     for ( i = 0; i < resource_cnt; ++i )
   1642     {
   1643       error = FT_Stream_Seek( stream, (FT_ULong)offsets[i] );
   1644       if ( error )
   1645         goto Exit2;
   1646       if ( FT_READ_ULONG( rlen ) )
   1647         goto Exit2;
   1648 
   1649       /* FT2 allocator takes signed long buffer length,
   1650        * too large fragment length causing overflow should be checked
   1651        */
   1652       if ( 0x7FFFFFFFUL < rlen )
   1653       {
   1654         error = FT_THROW( Invalid_Offset );
   1655         goto Exit2;
   1656       }
   1657 
   1658       if ( FT_READ_USHORT( flags ) )
   1659         goto Exit2;
   1660       FT_TRACE3(( "POST fragment[%d]: offsets=0x%08x, rlen=0x%08x, flags=0x%04x\n",
   1661                    i, offsets[i], rlen, flags ));
   1662 
   1663       error = FT_ERR( Array_Too_Large );
   1664       /* postpone the check of rlen longer than buffer until FT_Stream_Read() */
   1665       if ( ( flags >> 8 ) == 0 )        /* Comment, should not be loaded */
   1666       {
   1667         FT_TRACE3(( "    Skip POST fragment #%d because it is a comment\n", i ));
   1668         continue;
   1669       }
   1670 
   1671       /* the flags are part of the resource, so rlen >= 2.  */
   1672       /* but some fonts declare rlen = 0 for empty fragment */
   1673       if ( rlen > 2 )
   1674         rlen -= 2;
   1675       else
   1676         rlen = 0;
   1677 
   1678       if ( ( flags >> 8 ) == type )
   1679         len += rlen;
   1680       else
   1681       {
   1682         FT_TRACE3(( "    Write POST fragment #%d header (4-byte) to buffer"
   1683                     " %p + 0x%08x\n", i, pfb_data, pfb_lenpos ));
   1684         if ( pfb_lenpos + 3 > pfb_len + 2 )
   1685           goto Exit2;
   1686         pfb_data[pfb_lenpos    ] = (FT_Byte)( len );
   1687         pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
   1688         pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
   1689         pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
   1690 
   1691         if ( ( flags >> 8 ) == 5 )      /* End of font mark */
   1692           break;
   1693 
   1694         FT_TRACE3(( "    Write POST fragment #%d header (6-byte) to buffer"
   1695                     " %p + 0x%08x\n", i, pfb_data, pfb_pos ));
   1696         if ( pfb_pos + 6 > pfb_len + 2 )
   1697           goto Exit2;
   1698         pfb_data[pfb_pos++] = 0x80;
   1699 
   1700         type = flags >> 8;
   1701         len = rlen;
   1702 
   1703         pfb_data[pfb_pos++] = (FT_Byte)type;
   1704         pfb_lenpos          = pfb_pos;
   1705         pfb_data[pfb_pos++] = 0;        /* 4-byte length, fill in later */
   1706         pfb_data[pfb_pos++] = 0;
   1707         pfb_data[pfb_pos++] = 0;
   1708         pfb_data[pfb_pos++] = 0;
   1709       }
   1710 
   1711       if ( pfb_pos > pfb_len || pfb_pos + rlen > pfb_len )
   1712         goto Exit2;
   1713 
   1714       FT_TRACE3(( "    Load POST fragment #%d (%d byte) to buffer"
   1715                   " %p + 0x%08x\n", i, rlen, pfb_data, pfb_pos ));
   1716       error = FT_Stream_Read( stream, (FT_Byte *)pfb_data + pfb_pos, rlen );
   1717       if ( error )
   1718         goto Exit2;
   1719       pfb_pos += rlen;
   1720     }
   1721 
   1722     error = FT_ERR( Array_Too_Large );
   1723     if ( pfb_pos + 2 > pfb_len + 2 )
   1724       goto Exit2;
   1725     pfb_data[pfb_pos++] = 0x80;
   1726     pfb_data[pfb_pos++] = 3;
   1727 
   1728     if ( pfb_lenpos + 3 > pfb_len + 2 )
   1729       goto Exit2;
   1730     pfb_data[pfb_lenpos    ] = (FT_Byte)( len );
   1731     pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 );
   1732     pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 );
   1733     pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 );
   1734 
   1735     return open_face_from_buffer( library,
   1736                                   pfb_data,
   1737                                   pfb_pos,
   1738                                   face_index,
   1739                                   "type1",
   1740                                   aface );
   1741 
   1742   Exit2:
   1743     if ( error == FT_ERR( Array_Too_Large ) )
   1744       FT_TRACE2(( "  Abort due to too-short buffer to store"
   1745                   " all POST fragments\n" ));
   1746     else if ( error == FT_ERR( Invalid_Offset ) )
   1747       FT_TRACE2(( "  Abort due to invalid offset in a POST fragment\n" ));
   1748     if ( error )
   1749       error = FT_ERR( Cannot_Open_Resource );
   1750     FT_FREE( pfb_data );
   1751 
   1752   Exit:
   1753     return error;
   1754   }
   1755 
   1756 
   1757   /* The resource header says we've got resource_cnt `sfnt'      */
   1758   /* (TrueType/OpenType) resources in this file.  Look through   */
   1759   /* them for the one indicated by face_index, load it into mem, */
   1760   /* pass it on to the truetype driver, and return it.           */
   1761   /*                                                             */
   1762   static FT_Error
   1763   Mac_Read_sfnt_Resource( FT_Library  library,
   1764                           FT_Stream   stream,
   1765                           FT_Long    *offsets,
   1766                           FT_Long     resource_cnt,
   1767                           FT_Long     face_index,
   1768                           FT_Face    *aface )
   1769   {
   1770     FT_Memory  memory = library->memory;
   1771     FT_Byte*   sfnt_data = NULL;
   1772     FT_Error   error;
   1773     FT_ULong   flag_offset;
   1774     FT_Long    rlen;
   1775     int        is_cff;
   1776     FT_Long    face_index_in_resource = 0;
   1777 
   1778 
   1779     if ( face_index == -1 )
   1780       face_index = 0;
   1781     if ( face_index >= resource_cnt )
   1782       return FT_THROW( Cannot_Open_Resource );
   1783 
   1784     flag_offset = (FT_ULong)offsets[face_index];
   1785     error = FT_Stream_Seek( stream, flag_offset );
   1786     if ( error )
   1787       goto Exit;
   1788 
   1789     if ( FT_READ_LONG( rlen ) )
   1790       goto Exit;
   1791     if ( rlen == -1 )
   1792       return FT_THROW( Cannot_Open_Resource );
   1793     if ( (FT_ULong)rlen > FT_MAC_RFORK_MAX_LEN )
   1794       return FT_THROW( Invalid_Offset );
   1795 
   1796     error = open_face_PS_from_sfnt_stream( library,
   1797                                            stream,
   1798                                            face_index,
   1799                                            0, NULL,
   1800                                            aface );
   1801     if ( !error )
   1802       goto Exit;
   1803 
   1804     /* rewind sfnt stream before open_face_PS_from_sfnt_stream() */
   1805     if ( FT_Stream_Seek( stream, flag_offset + 4 ) )
   1806       goto Exit;
   1807 
   1808     if ( FT_ALLOC( sfnt_data, rlen ) )
   1809       return error;
   1810     error = FT_Stream_Read( stream, (FT_Byte *)sfnt_data, (FT_ULong)rlen );
   1811     if ( error ) {
   1812       FT_FREE( sfnt_data );
   1813       goto Exit;
   1814     }
   1815 
   1816     is_cff = rlen > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 );
   1817     error = open_face_from_buffer( library,
   1818                                    sfnt_data,
   1819                                    (FT_ULong)rlen,
   1820                                    face_index_in_resource,
   1821                                    is_cff ? "cff" : "truetype",
   1822                                    aface );
   1823 
   1824   Exit:
   1825     return error;
   1826   }
   1827 
   1828 
   1829   /* Check for a valid resource fork header, or a valid dfont    */
   1830   /* header.  In a resource fork the first 16 bytes are repeated */
   1831   /* at the location specified by bytes 4-7.  In a dfont bytes   */
   1832   /* 4-7 point to 16 bytes of zeroes instead.                    */
   1833   /*                                                             */
   1834   static FT_Error
   1835   IsMacResource( FT_Library  library,
   1836                  FT_Stream   stream,
   1837                  FT_Long     resource_offset,
   1838                  FT_Long     face_index,
   1839                  FT_Face    *aface )
   1840   {
   1841     FT_Memory  memory = library->memory;
   1842     FT_Error   error;
   1843     FT_Long    map_offset, rdara_pos;
   1844     FT_Long    *data_offsets;
   1845     FT_Long    count;
   1846 
   1847 
   1848     error = FT_Raccess_Get_HeaderInfo( library, stream, resource_offset,
   1849                                        &map_offset, &rdara_pos );
   1850     if ( error )
   1851       return error;
   1852 
   1853     /* POST resources must be sorted to concatenate properly */
   1854     error = FT_Raccess_Get_DataOffsets( library, stream,
   1855                                         map_offset, rdara_pos,
   1856                                         TTAG_POST, TRUE,
   1857                                         &data_offsets, &count );
   1858     if ( !error )
   1859     {
   1860       error = Mac_Read_POST_Resource( library, stream, data_offsets, count,
   1861                                       face_index, aface );
   1862       FT_FREE( data_offsets );
   1863       /* POST exists in an LWFN providing a single face */
   1864       if ( !error )
   1865         (*aface)->num_faces = 1;
   1866       return error;
   1867     }
   1868 
   1869     /* sfnt resources should not be sorted to preserve the face order by
   1870        QuickDraw API */
   1871     error = FT_Raccess_Get_DataOffsets( library, stream,
   1872                                         map_offset, rdara_pos,
   1873                                         TTAG_sfnt, FALSE,
   1874                                         &data_offsets, &count );
   1875     if ( !error )
   1876     {
   1877       FT_Long  face_index_internal = face_index % count;
   1878 
   1879 
   1880       error = Mac_Read_sfnt_Resource( library, stream, data_offsets, count,
   1881                                       face_index_internal, aface );
   1882       FT_FREE( data_offsets );
   1883       if ( !error )
   1884         (*aface)->num_faces = count;
   1885     }
   1886 
   1887     return error;
   1888   }
   1889 
   1890 
   1891   /* Check for a valid macbinary header, and if we find one   */
   1892   /* check that the (flattened) resource fork in it is valid. */
   1893   /*                                                          */
   1894   static FT_Error
   1895   IsMacBinary( FT_Library  library,
   1896                FT_Stream   stream,
   1897                FT_Long     face_index,
   1898                FT_Face    *aface )
   1899   {
   1900     unsigned char  header[128];
   1901     FT_Error       error;
   1902     FT_Long        dlen, offset;
   1903 
   1904 
   1905     if ( NULL == stream )
   1906       return FT_THROW( Invalid_Stream_Operation );
   1907 
   1908     error = FT_Stream_Seek( stream, 0 );
   1909     if ( error )
   1910       goto Exit;
   1911 
   1912     error = FT_Stream_Read( stream, (FT_Byte*)header, 128 );
   1913     if ( error )
   1914       goto Exit;
   1915 
   1916     if (            header[ 0] !=   0 ||
   1917                     header[74] !=   0 ||
   1918                     header[82] !=   0 ||
   1919                     header[ 1] ==   0 ||
   1920                     header[ 1] >   33 ||
   1921                     header[63] !=   0 ||
   1922          header[2 + header[1]] !=   0 ||
   1923                   header[0x53] > 0x7F )
   1924       return FT_THROW( Unknown_File_Format );
   1925 
   1926     dlen = ( header[0x53] << 24 ) |
   1927            ( header[0x54] << 16 ) |
   1928            ( header[0x55] <<  8 ) |
   1929              header[0x56];
   1930 #if 0
   1931     rlen = ( header[0x57] << 24 ) |
   1932            ( header[0x58] << 16 ) |
   1933            ( header[0x59] <<  8 ) |
   1934              header[0x5A];
   1935 #endif /* 0 */
   1936     offset = 128 + ( ( dlen + 127 ) & ~127 );
   1937 
   1938     return IsMacResource( library, stream, offset, face_index, aface );
   1939 
   1940   Exit:
   1941     return error;
   1942   }
   1943 
   1944 
   1945   static FT_Error
   1946   load_face_in_embedded_rfork( FT_Library           library,
   1947                                FT_Stream            stream,
   1948                                FT_Long              face_index,
   1949                                FT_Face             *aface,
   1950                                const FT_Open_Args  *args )
   1951   {
   1952 
   1953 #undef  FT_COMPONENT
   1954 #define FT_COMPONENT  trace_raccess
   1955 
   1956     FT_Memory  memory = library->memory;
   1957     FT_Error   error  = FT_ERR( Unknown_File_Format );
   1958     FT_UInt    i;
   1959 
   1960     char *     file_names[FT_RACCESS_N_RULES];
   1961     FT_Long    offsets[FT_RACCESS_N_RULES];
   1962     FT_Error   errors[FT_RACCESS_N_RULES];
   1963     FT_Bool    is_darwin_vfs, vfs_rfork_has_no_font = FALSE; /* not tested */
   1964 
   1965     FT_Open_Args  args2;
   1966     FT_Stream     stream2 = NULL;
   1967 
   1968 
   1969     FT_Raccess_Guess( library, stream,
   1970                       args->pathname, file_names, offsets, errors );
   1971 
   1972     for ( i = 0; i < FT_RACCESS_N_RULES; i++ )
   1973     {
   1974       is_darwin_vfs = ft_raccess_rule_by_darwin_vfs( library, i );
   1975       if ( is_darwin_vfs && vfs_rfork_has_no_font )
   1976       {
   1977         FT_TRACE3(( "Skip rule %d: darwin vfs resource fork"
   1978                     " is already checked and"
   1979                     " no font is found\n", i ));
   1980         continue;
   1981       }
   1982 
   1983       if ( errors[i] )
   1984       {
   1985         FT_TRACE3(( "Error[%d] has occurred in rule %d\n", errors[i], i ));
   1986         continue;
   1987       }
   1988 
   1989       args2.flags    = FT_OPEN_PATHNAME;
   1990       args2.pathname = file_names[i] ? file_names[i] : args->pathname;
   1991 
   1992       FT_TRACE3(( "Try rule %d: %s (offset=%d) ...",
   1993                   i, args2.pathname, offsets[i] ));
   1994 
   1995       error = FT_Stream_New( library, &args2, &stream2 );
   1996       if ( is_darwin_vfs && FT_ERR_EQ( error, Cannot_Open_Stream ) )
   1997         vfs_rfork_has_no_font = TRUE;
   1998 
   1999       if ( error )
   2000       {
   2001         FT_TRACE3(( "failed\n" ));
   2002         continue;
   2003       }
   2004 
   2005       error = IsMacResource( library, stream2, offsets[i],
   2006                              face_index, aface );
   2007       FT_Stream_Free( stream2, 0 );
   2008 
   2009       FT_TRACE3(( "%s\n", error ? "failed": "successful" ));
   2010 
   2011       if ( !error )
   2012           break;
   2013       else if ( is_darwin_vfs )
   2014           vfs_rfork_has_no_font = TRUE;
   2015     }
   2016 
   2017     for (i = 0; i < FT_RACCESS_N_RULES; i++)
   2018     {
   2019       if ( file_names[i] )
   2020         FT_FREE( file_names[i] );
   2021     }
   2022 
   2023     /* Caller (load_mac_face) requires FT_Err_Unknown_File_Format. */
   2024     if ( error )
   2025       error = FT_ERR( Unknown_File_Format );
   2026 
   2027     return error;
   2028 
   2029 #undef  FT_COMPONENT
   2030 #define FT_COMPONENT  trace_objs
   2031 
   2032   }
   2033 
   2034 
   2035   /* Check for some macintosh formats without Carbon framework.    */
   2036   /* Is this a macbinary file?  If so look at the resource fork.   */
   2037   /* Is this a mac dfont file?                                     */
   2038   /* Is this an old style resource fork? (in data)                 */
   2039   /* Else call load_face_in_embedded_rfork to try extra rules      */
   2040   /* (defined in `ftrfork.c').                                     */
   2041   /*                                                               */
   2042   static FT_Error
   2043   load_mac_face( FT_Library           library,
   2044                  FT_Stream            stream,
   2045                  FT_Long              face_index,
   2046                  FT_Face             *aface,
   2047                  const FT_Open_Args  *args )
   2048   {
   2049     FT_Error error;
   2050     FT_UNUSED( args );
   2051 
   2052 
   2053     error = IsMacBinary( library, stream, face_index, aface );
   2054     if ( FT_ERR_EQ( error, Unknown_File_Format ) )
   2055     {
   2056 
   2057 #undef  FT_COMPONENT
   2058 #define FT_COMPONENT  trace_raccess
   2059 
   2060 #ifdef FT_DEBUG_LEVEL_TRACE
   2061       FT_TRACE3(( "Try as dfont: " ));
   2062       if ( !( args->flags & FT_OPEN_MEMORY ) )
   2063         FT_TRACE3(( "%s ...", args->pathname ));
   2064 #endif
   2065 
   2066       error = IsMacResource( library, stream, 0, face_index, aface );
   2067 
   2068       FT_TRACE3(( "%s\n", error ? "failed" : "successful" ));
   2069 
   2070 #undef  FT_COMPONENT
   2071 #define FT_COMPONENT  trace_objs
   2072 
   2073     }
   2074 
   2075     if ( ( FT_ERR_EQ( error, Unknown_File_Format )      ||
   2076            FT_ERR_EQ( error, Invalid_Stream_Operation ) ) &&
   2077          ( args->flags & FT_OPEN_PATHNAME )               )
   2078       error = load_face_in_embedded_rfork( library, stream,
   2079                                            face_index, aface, args );
   2080     return error;
   2081   }
   2082 #endif
   2083 
   2084 #endif  /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
   2085 
   2086 
   2087   /* documentation is in freetype.h */
   2088 
   2089   FT_EXPORT_DEF( FT_Error )
   2090   FT_Open_Face( FT_Library           library,
   2091                 const FT_Open_Args*  args,
   2092                 FT_Long              face_index,
   2093                 FT_Face             *aface )
   2094   {
   2095     FT_Error     error;
   2096     FT_Driver    driver = NULL;
   2097     FT_Memory    memory = NULL;
   2098     FT_Stream    stream = NULL;
   2099     FT_Face      face   = NULL;
   2100     FT_ListNode  node   = NULL;
   2101     FT_Bool      external_stream;
   2102     FT_Module*   cur;
   2103     FT_Module*   limit;
   2104 
   2105 
   2106     /* test for valid `library' delayed to `FT_Stream_New' */
   2107 
   2108     if ( ( !aface && face_index >= 0 ) || !args )
   2109       return FT_THROW( Invalid_Argument );
   2110 
   2111     external_stream = FT_BOOL( ( args->flags & FT_OPEN_STREAM ) &&
   2112                                args->stream                     );
   2113 
   2114     /* create input stream */
   2115     error = FT_Stream_New( library, args, &stream );
   2116     if ( error )
   2117       goto Fail3;
   2118 
   2119     memory = library->memory;
   2120 
   2121     /* If the font driver is specified in the `args' structure, use */
   2122     /* it.  Otherwise, we scan the list of registered drivers.      */
   2123     if ( ( args->flags & FT_OPEN_DRIVER ) && args->driver )
   2124     {
   2125       driver = FT_DRIVER( args->driver );
   2126 
   2127       /* not all modules are drivers, so check... */
   2128       if ( FT_MODULE_IS_DRIVER( driver ) )
   2129       {
   2130         FT_Int         num_params = 0;
   2131         FT_Parameter*  params     = NULL;
   2132 
   2133 
   2134         if ( args->flags & FT_OPEN_PARAMS )
   2135         {
   2136           num_params = args->num_params;
   2137           params     = args->params;
   2138         }
   2139 
   2140         error = open_face( driver, &stream, external_stream, face_index,
   2141                            num_params, params, &face );
   2142         if ( !error )
   2143           goto Success;
   2144       }
   2145       else
   2146         error = FT_THROW( Invalid_Handle );
   2147 
   2148       FT_Stream_Free( stream, external_stream );
   2149       goto Fail;
   2150     }
   2151     else
   2152     {
   2153       error = FT_ERR( Missing_Module );
   2154 
   2155       /* check each font driver for an appropriate format */
   2156       cur   = library->modules;
   2157       limit = cur + library->num_modules;
   2158 
   2159       for ( ; cur < limit; cur++ )
   2160       {
   2161         /* not all modules are font drivers, so check... */
   2162         if ( FT_MODULE_IS_DRIVER( cur[0] ) )
   2163         {
   2164           FT_Int         num_params = 0;
   2165           FT_Parameter*  params     = NULL;
   2166 
   2167 
   2168           driver = FT_DRIVER( cur[0] );
   2169 
   2170           if ( args->flags & FT_OPEN_PARAMS )
   2171           {
   2172             num_params = args->num_params;
   2173             params     = args->params;
   2174           }
   2175 
   2176           error = open_face( driver, &stream, external_stream, face_index,
   2177                              num_params, params, &face );
   2178           if ( !error )
   2179             goto Success;
   2180 
   2181 #ifdef FT_CONFIG_OPTION_MAC_FONTS
   2182           if ( ft_strcmp( cur[0]->clazz->module_name, "truetype" ) == 0 &&
   2183                FT_ERR_EQ( error, Table_Missing )                        )
   2184           {
   2185             /* TrueType but essential tables are missing */
   2186             if ( FT_Stream_Seek( stream, 0 ) )
   2187               break;
   2188 
   2189             error = open_face_PS_from_sfnt_stream( library,
   2190                                                    stream,
   2191                                                    face_index,
   2192                                                    num_params,
   2193                                                    params,
   2194                                                    aface );
   2195             if ( !error )
   2196             {
   2197               FT_Stream_Free( stream, external_stream );
   2198               return error;
   2199             }
   2200           }
   2201 #endif
   2202 
   2203           if ( FT_ERR_NEQ( error, Unknown_File_Format ) )
   2204             goto Fail3;
   2205         }
   2206       }
   2207 
   2208     Fail3:
   2209       /* If we are on the mac, and we get an                          */
   2210       /* FT_Err_Invalid_Stream_Operation it may be because we have an */
   2211       /* empty data fork, so we need to check the resource fork.      */
   2212       if ( FT_ERR_NEQ( error, Cannot_Open_Stream )       &&
   2213            FT_ERR_NEQ( error, Unknown_File_Format )      &&
   2214            FT_ERR_NEQ( error, Invalid_Stream_Operation ) )
   2215         goto Fail2;
   2216 
   2217 #if !defined( FT_MACINTOSH ) && defined( FT_CONFIG_OPTION_MAC_FONTS )
   2218       error = load_mac_face( library, stream, face_index, aface, args );
   2219       if ( !error )
   2220       {
   2221         /* We don't want to go to Success here.  We've already done that. */
   2222         /* On the other hand, if we succeeded we still need to close this */
   2223         /* stream (we opened a different stream which extracted the       */
   2224         /* interesting information out of this stream here.  That stream  */
   2225         /* will still be open and the face will point to it).             */
   2226         FT_Stream_Free( stream, external_stream );
   2227         return error;
   2228       }
   2229 
   2230       if ( FT_ERR_NEQ( error, Unknown_File_Format ) )
   2231         goto Fail2;
   2232 #endif  /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */
   2233 
   2234       /* no driver is able to handle this format */
   2235       error = FT_THROW( Unknown_File_Format );
   2236 
   2237   Fail2:
   2238       FT_Stream_Free( stream, external_stream );
   2239       goto Fail;
   2240     }
   2241 
   2242   Success:
   2243     FT_TRACE4(( "FT_Open_Face: New face object, adding to list\n" ));
   2244 
   2245     /* add the face object to its driver's list */
   2246     if ( FT_NEW( node ) )
   2247       goto Fail;
   2248 
   2249     node->data = face;
   2250     /* don't assume driver is the same as face->driver, so use */
   2251     /* face->driver instead.                                   */
   2252     FT_List_Add( &face->driver->faces_list, node );
   2253 
   2254     /* now allocate a glyph slot object for the face */
   2255     FT_TRACE4(( "FT_Open_Face: Creating glyph slot\n" ));
   2256 
   2257     if ( face_index >= 0 )
   2258     {
   2259       error = FT_New_GlyphSlot( face, NULL );
   2260       if ( error )
   2261         goto Fail;
   2262 
   2263       /* finally, allocate a size object for the face */
   2264       {
   2265         FT_Size  size;
   2266 
   2267 
   2268         FT_TRACE4(( "FT_Open_Face: Creating size object\n" ));
   2269 
   2270         error = FT_New_Size( face, &size );
   2271         if ( error )
   2272           goto Fail;
   2273 
   2274         face->size = size;
   2275       }
   2276     }
   2277 
   2278     /* some checks */
   2279 
   2280     if ( FT_IS_SCALABLE( face ) )
   2281     {
   2282       if ( face->height < 0 )
   2283         face->height = (FT_Short)-face->height;
   2284 
   2285       if ( !FT_HAS_VERTICAL( face ) )
   2286         face->max_advance_height = (FT_Short)face->height;
   2287     }
   2288 
   2289     if ( FT_HAS_FIXED_SIZES( face ) )
   2290     {
   2291       FT_Int  i;
   2292 
   2293 
   2294       for ( i = 0; i < face->num_fixed_sizes; i++ )
   2295       {
   2296         FT_Bitmap_Size*  bsize = face->available_sizes + i;
   2297 
   2298 
   2299         if ( bsize->height < 0 )
   2300           bsize->height = (FT_Short)-bsize->height;
   2301         if ( bsize->x_ppem < 0 )
   2302           bsize->x_ppem = (FT_Short)-bsize->x_ppem;
   2303         if ( bsize->y_ppem < 0 )
   2304           bsize->y_ppem = -bsize->y_ppem;
   2305       }
   2306     }
   2307 
   2308     /* initialize internal face data */
   2309     {
   2310       FT_Face_Internal  internal = face->internal;
   2311 
   2312 
   2313       internal->transform_matrix.xx = 0x10000L;
   2314       internal->transform_matrix.xy = 0;
   2315       internal->transform_matrix.yx = 0;
   2316       internal->transform_matrix.yy = 0x10000L;
   2317 
   2318       internal->transform_delta.x = 0;
   2319       internal->transform_delta.y = 0;
   2320 
   2321       internal->refcount = 1;
   2322     }
   2323 
   2324     if ( aface )
   2325       *aface = face;
   2326     else
   2327       FT_Done_Face( face );
   2328 
   2329     goto Exit;
   2330 
   2331   Fail:
   2332     if ( node )
   2333       FT_Done_Face( face );    /* face must be in the driver's list */
   2334     else if ( face )
   2335       destroy_face( memory, face, driver );
   2336 
   2337   Exit:
   2338     FT_TRACE4(( "FT_Open_Face: Return %d\n", error ));
   2339 
   2340     return error;
   2341   }
   2342 
   2343 
   2344   /* documentation is in freetype.h */
   2345 
   2346   FT_EXPORT_DEF( FT_Error )
   2347   FT_Attach_File( FT_Face      face,
   2348                   const char*  filepathname )
   2349   {
   2350     FT_Open_Args  open;
   2351 
   2352 
   2353     /* test for valid `face' delayed to `FT_Attach_Stream' */
   2354 
   2355     if ( !filepathname )
   2356       return FT_THROW( Invalid_Argument );
   2357 
   2358     open.stream   = NULL;
   2359     open.flags    = FT_OPEN_PATHNAME;
   2360     open.pathname = (char*)filepathname;
   2361 
   2362     return FT_Attach_Stream( face, &open );
   2363   }
   2364 
   2365 
   2366   /* documentation is in freetype.h */
   2367 
   2368   FT_EXPORT_DEF( FT_Error )
   2369   FT_Attach_Stream( FT_Face        face,
   2370                     FT_Open_Args*  parameters )
   2371   {
   2372     FT_Stream  stream;
   2373     FT_Error   error;
   2374     FT_Driver  driver;
   2375 
   2376     FT_Driver_Class  clazz;
   2377 
   2378 
   2379     /* test for valid `parameters' delayed to `FT_Stream_New' */
   2380 
   2381     if ( !face )
   2382       return FT_THROW( Invalid_Face_Handle );
   2383 
   2384     driver = face->driver;
   2385     if ( !driver )
   2386       return FT_THROW( Invalid_Driver_Handle );
   2387 
   2388     error = FT_Stream_New( driver->root.library, parameters, &stream );
   2389     if ( error )
   2390       goto Exit;
   2391 
   2392     /* we implement FT_Attach_Stream in each driver through the */
   2393     /* `attach_file' interface                                  */
   2394 
   2395     error = FT_ERR( Unimplemented_Feature );
   2396     clazz = driver->clazz;
   2397     if ( clazz->attach_file )
   2398       error = clazz->attach_file( face, stream );
   2399 
   2400     /* close the attached stream */
   2401     FT_Stream_Free( stream,
   2402                     (FT_Bool)( parameters->stream &&
   2403                                ( parameters->flags & FT_OPEN_STREAM ) ) );
   2404 
   2405   Exit:
   2406     return error;
   2407   }
   2408 
   2409 
   2410   /* documentation is in freetype.h */
   2411 
   2412   FT_EXPORT_DEF( FT_Error )
   2413   FT_Reference_Face( FT_Face  face )
   2414   {
   2415     if ( !face )
   2416       return FT_THROW( Invalid_Face_Handle );
   2417 
   2418     face->internal->refcount++;
   2419 
   2420     return FT_Err_Ok;
   2421   }
   2422 
   2423 
   2424   /* documentation is in freetype.h */
   2425 
   2426   FT_EXPORT_DEF( FT_Error )
   2427   FT_Done_Face( FT_Face  face )
   2428   {
   2429     FT_Error     error;
   2430     FT_Driver    driver;
   2431     FT_Memory    memory;
   2432     FT_ListNode  node;
   2433 
   2434 
   2435     error = FT_ERR( Invalid_Face_Handle );
   2436     if ( face && face->driver )
   2437     {
   2438       face->internal->refcount--;
   2439       if ( face->internal->refcount > 0 )
   2440         error = FT_Err_Ok;
   2441       else
   2442       {
   2443         driver = face->driver;
   2444         memory = driver->root.memory;
   2445 
   2446         /* find face in driver's list */
   2447         node = FT_List_Find( &driver->faces_list, face );
   2448         if ( node )
   2449         {
   2450           /* remove face object from the driver's list */
   2451           FT_List_Remove( &driver->faces_list, node );
   2452           FT_FREE( node );
   2453 
   2454           /* now destroy the object proper */
   2455           destroy_face( memory, face, driver );
   2456           error = FT_Err_Ok;
   2457         }
   2458       }
   2459     }
   2460 
   2461     return error;
   2462   }
   2463 
   2464 
   2465   /* documentation is in ftobjs.h */
   2466 
   2467   FT_EXPORT_DEF( FT_Error )
   2468   FT_New_Size( FT_Face   face,
   2469                FT_Size  *asize )
   2470   {
   2471     FT_Error         error;
   2472     FT_Memory        memory;
   2473     FT_Driver        driver;
   2474     FT_Driver_Class  clazz;
   2475 
   2476     FT_Size          size = NULL;
   2477     FT_ListNode      node = NULL;
   2478 
   2479 
   2480     if ( !face )
   2481       return FT_THROW( Invalid_Face_Handle );
   2482 
   2483     if ( !asize )
   2484       return FT_THROW( Invalid_Argument );
   2485 
   2486     if ( !face->driver )
   2487       return FT_THROW( Invalid_Driver_Handle );
   2488 
   2489     *asize = NULL;
   2490 
   2491     driver = face->driver;
   2492     clazz  = driver->clazz;
   2493     memory = face->memory;
   2494 
   2495     /* Allocate new size object and perform basic initialisation */
   2496     if ( FT_ALLOC( size, clazz->size_object_size ) || FT_NEW( node ) )
   2497       goto Exit;
   2498 
   2499     size->face = face;
   2500 
   2501     /* for now, do not use any internal fields in size objects */
   2502     size->internal = NULL;
   2503 
   2504     if ( clazz->init_size )
   2505       error = clazz->init_size( size );
   2506 
   2507     /* in case of success, add to the face's list */
   2508     if ( !error )
   2509     {
   2510       *asize     = size;
   2511       node->data = size;
   2512       FT_List_Add( &face->sizes_list, node );
   2513     }
   2514 
   2515   Exit:
   2516     if ( error )
   2517     {
   2518       FT_FREE( node );
   2519       FT_FREE( size );
   2520     }
   2521 
   2522     return error;
   2523   }
   2524 
   2525 
   2526   /* documentation is in ftobjs.h */
   2527 
   2528   FT_EXPORT_DEF( FT_Error )
   2529   FT_Done_Size( FT_Size  size )
   2530   {
   2531     FT_Error     error;
   2532     FT_Driver    driver;
   2533     FT_Memory    memory;
   2534     FT_Face      face;
   2535     FT_ListNode  node;
   2536 
   2537 
   2538     if ( !size )
   2539       return FT_THROW( Invalid_Size_Handle );
   2540 
   2541     face = size->face;
   2542     if ( !face )
   2543       return FT_THROW( Invalid_Face_Handle );
   2544 
   2545     driver = face->driver;
   2546     if ( !driver )
   2547       return FT_THROW( Invalid_Driver_Handle );
   2548 
   2549     memory = driver->root.memory;
   2550 
   2551     error = FT_Err_Ok;
   2552     node  = FT_List_Find( &face->sizes_list, size );
   2553     if ( node )
   2554     {
   2555       FT_List_Remove( &face->sizes_list, node );
   2556       FT_FREE( node );
   2557 
   2558       if ( face->size == size )
   2559       {
   2560         face->size = NULL;
   2561         if ( face->sizes_list.head )
   2562           face->size = (FT_Size)(face->sizes_list.head->data);
   2563       }
   2564 
   2565       destroy_size( memory, size, driver );
   2566     }
   2567     else
   2568       error = FT_THROW( Invalid_Size_Handle );
   2569 
   2570     return error;
   2571   }
   2572 
   2573 
   2574   /* documentation is in ftobjs.h */
   2575 
   2576   FT_BASE_DEF( FT_Error )
   2577   FT_Match_Size( FT_Face          face,
   2578                  FT_Size_Request  req,
   2579                  FT_Bool          ignore_width,
   2580                  FT_ULong*        size_index )
   2581   {
   2582     FT_Int   i;
   2583     FT_Long  w, h;
   2584 
   2585 
   2586     if ( !FT_HAS_FIXED_SIZES( face ) )
   2587       return FT_THROW( Invalid_Face_Handle );
   2588 
   2589     /* FT_Bitmap_Size doesn't provide enough info... */
   2590     if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
   2591       return FT_THROW( Unimplemented_Feature );
   2592 
   2593     w = FT_REQUEST_WIDTH ( req );
   2594     h = FT_REQUEST_HEIGHT( req );
   2595 
   2596     if ( req->width && !req->height )
   2597       h = w;
   2598     else if ( !req->width && req->height )
   2599       w = h;
   2600 
   2601     w = FT_PIX_ROUND( w );
   2602     h = FT_PIX_ROUND( h );
   2603 
   2604     for ( i = 0; i < face->num_fixed_sizes; i++ )
   2605     {
   2606       FT_Bitmap_Size*  bsize = face->available_sizes + i;
   2607 
   2608 
   2609       if ( h != FT_PIX_ROUND( bsize->y_ppem ) )
   2610         continue;
   2611 
   2612       if ( w == FT_PIX_ROUND( bsize->x_ppem ) || ignore_width )
   2613       {
   2614         FT_TRACE3(( "FT_Match_Size: bitmap strike %d matches\n", i ));
   2615 
   2616         if ( size_index )
   2617           *size_index = (FT_ULong)i;
   2618 
   2619         return FT_Err_Ok;
   2620       }
   2621     }
   2622 
   2623     return FT_THROW( Invalid_Pixel_Size );
   2624   }
   2625 
   2626 
   2627   /* documentation is in ftobjs.h */
   2628 
   2629   FT_BASE_DEF( void )
   2630   ft_synthesize_vertical_metrics( FT_Glyph_Metrics*  metrics,
   2631                                   FT_Pos             advance )
   2632   {
   2633     FT_Pos  height = metrics->height;
   2634 
   2635 
   2636     /* compensate for glyph with bbox above/below the baseline */
   2637     if ( metrics->horiBearingY < 0 )
   2638     {
   2639       if ( height < metrics->horiBearingY )
   2640         height = metrics->horiBearingY;
   2641     }
   2642     else if ( metrics->horiBearingY > 0 )
   2643       height -= metrics->horiBearingY;
   2644 
   2645     /* the factor 1.2 is a heuristical value */
   2646     if ( !advance )
   2647       advance = height * 12 / 10;
   2648 
   2649     metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2;
   2650     metrics->vertBearingY = ( advance - height ) / 2;
   2651     metrics->vertAdvance  = advance;
   2652   }
   2653 
   2654 
   2655   static void
   2656   ft_recompute_scaled_metrics( FT_Face           face,
   2657                                FT_Size_Metrics*  metrics )
   2658   {
   2659     /* Compute root ascender, descender, test height, and max_advance */
   2660 
   2661 #ifdef GRID_FIT_METRICS
   2662     metrics->ascender    = FT_PIX_CEIL( FT_MulFix( face->ascender,
   2663                                                    metrics->y_scale ) );
   2664 
   2665     metrics->descender   = FT_PIX_FLOOR( FT_MulFix( face->descender,
   2666                                                     metrics->y_scale ) );
   2667 
   2668     metrics->height      = FT_PIX_ROUND( FT_MulFix( face->height,
   2669                                                     metrics->y_scale ) );
   2670 
   2671     metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width,
   2672                                                     metrics->x_scale ) );
   2673 #else /* !GRID_FIT_METRICS */
   2674     metrics->ascender    = FT_MulFix( face->ascender,
   2675                                       metrics->y_scale );
   2676 
   2677     metrics->descender   = FT_MulFix( face->descender,
   2678                                       metrics->y_scale );
   2679 
   2680     metrics->height      = FT_MulFix( face->height,
   2681                                       metrics->y_scale );
   2682 
   2683     metrics->max_advance = FT_MulFix( face->max_advance_width,
   2684                                       metrics->x_scale );
   2685 #endif /* !GRID_FIT_METRICS */
   2686   }
   2687 
   2688 
   2689   FT_BASE_DEF( void )
   2690   FT_Select_Metrics( FT_Face   face,
   2691                      FT_ULong  strike_index )
   2692   {
   2693     FT_Size_Metrics*  metrics;
   2694     FT_Bitmap_Size*   bsize;
   2695 
   2696 
   2697     metrics = &face->size->metrics;
   2698     bsize   = face->available_sizes + strike_index;
   2699 
   2700     metrics->x_ppem = (FT_UShort)( ( bsize->x_ppem + 32 ) >> 6 );
   2701     metrics->y_ppem = (FT_UShort)( ( bsize->y_ppem + 32 ) >> 6 );
   2702 
   2703     if ( FT_IS_SCALABLE( face ) )
   2704     {
   2705       metrics->x_scale = FT_DivFix( bsize->x_ppem,
   2706                                     face->units_per_EM );
   2707       metrics->y_scale = FT_DivFix( bsize->y_ppem,
   2708                                     face->units_per_EM );
   2709 
   2710       ft_recompute_scaled_metrics( face, metrics );
   2711     }
   2712     else
   2713     {
   2714       metrics->x_scale     = 1L << 16;
   2715       metrics->y_scale     = 1L << 16;
   2716       metrics->ascender    = bsize->y_ppem;
   2717       metrics->descender   = 0;
   2718       metrics->height      = bsize->height << 6;
   2719       metrics->max_advance = bsize->x_ppem;
   2720     }
   2721 
   2722     FT_TRACE5(( "FT_Select_Metrics:\n" ));
   2723     FT_TRACE5(( "  x scale: %d (%f)\n",
   2724                 metrics->x_scale, metrics->x_scale / 65536.0 ));
   2725     FT_TRACE5(( "  y scale: %d (%f)\n",
   2726                 metrics->y_scale, metrics->y_scale / 65536.0 ));
   2727     FT_TRACE5(( "  ascender: %f\n",    metrics->ascender / 64.0 ));
   2728     FT_TRACE5(( "  descender: %f\n",   metrics->descender / 64.0 ));
   2729     FT_TRACE5(( "  height: %f\n",      metrics->height / 64.0 ));
   2730     FT_TRACE5(( "  max advance: %f\n", metrics->max_advance / 64.0 ));
   2731     FT_TRACE5(( "  x ppem: %d\n",      metrics->x_ppem ));
   2732     FT_TRACE5(( "  y ppem: %d\n",      metrics->y_ppem ));
   2733   }
   2734 
   2735 
   2736   FT_BASE_DEF( void )
   2737   FT_Request_Metrics( FT_Face          face,
   2738                       FT_Size_Request  req )
   2739   {
   2740     FT_Size_Metrics*  metrics;
   2741 
   2742 
   2743     metrics = &face->size->metrics;
   2744 
   2745     if ( FT_IS_SCALABLE( face ) )
   2746     {
   2747       FT_Long  w = 0, h = 0, scaled_w = 0, scaled_h = 0;
   2748 
   2749 
   2750       switch ( req->type )
   2751       {
   2752       case FT_SIZE_REQUEST_TYPE_NOMINAL:
   2753         w = h = face->units_per_EM;
   2754         break;
   2755 
   2756       case FT_SIZE_REQUEST_TYPE_REAL_DIM:
   2757         w = h = face->ascender - face->descender;
   2758         break;
   2759 
   2760       case FT_SIZE_REQUEST_TYPE_BBOX:
   2761         w = face->bbox.xMax - face->bbox.xMin;
   2762         h = face->bbox.yMax - face->bbox.yMin;
   2763         break;
   2764 
   2765       case FT_SIZE_REQUEST_TYPE_CELL:
   2766         w = face->max_advance_width;
   2767         h = face->ascender - face->descender;
   2768         break;
   2769 
   2770       case FT_SIZE_REQUEST_TYPE_SCALES:
   2771         metrics->x_scale = (FT_Fixed)req->width;
   2772         metrics->y_scale = (FT_Fixed)req->height;
   2773         if ( !metrics->x_scale )
   2774           metrics->x_scale = metrics->y_scale;
   2775         else if ( !metrics->y_scale )
   2776           metrics->y_scale = metrics->x_scale;
   2777         goto Calculate_Ppem;
   2778 
   2779       case FT_SIZE_REQUEST_TYPE_MAX:
   2780         break;
   2781       }
   2782 
   2783       /* to be on the safe side */
   2784       if ( w < 0 )
   2785         w = -w;
   2786 
   2787       if ( h < 0 )
   2788         h = -h;
   2789 
   2790       scaled_w = FT_REQUEST_WIDTH ( req );
   2791       scaled_h = FT_REQUEST_HEIGHT( req );
   2792 
   2793       /* determine scales */
   2794       if ( req->width )
   2795       {
   2796         metrics->x_scale = FT_DivFix( scaled_w, w );
   2797 
   2798         if ( req->height )
   2799         {
   2800           metrics->y_scale = FT_DivFix( scaled_h, h );
   2801 
   2802           if ( req->type == FT_SIZE_REQUEST_TYPE_CELL )
   2803           {
   2804             if ( metrics->y_scale > metrics->x_scale )
   2805               metrics->y_scale = metrics->x_scale;
   2806             else
   2807               metrics->x_scale = metrics->y_scale;
   2808           }
   2809         }
   2810         else
   2811         {
   2812           metrics->y_scale = metrics->x_scale;
   2813           scaled_h = FT_MulDiv( scaled_w, h, w );
   2814         }
   2815       }
   2816       else
   2817       {
   2818         metrics->x_scale = metrics->y_scale = FT_DivFix( scaled_h, h );
   2819         scaled_w = FT_MulDiv( scaled_h, w, h );
   2820       }
   2821 
   2822   Calculate_Ppem:
   2823       /* calculate the ppems */
   2824       if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )
   2825       {
   2826         scaled_w = FT_MulFix( face->units_per_EM, metrics->x_scale );
   2827         scaled_h = FT_MulFix( face->units_per_EM, metrics->y_scale );
   2828       }
   2829 
   2830       metrics->x_ppem = (FT_UShort)( ( scaled_w + 32 ) >> 6 );
   2831       metrics->y_ppem = (FT_UShort)( ( scaled_h + 32 ) >> 6 );
   2832 
   2833       ft_recompute_scaled_metrics( face, metrics );
   2834     }
   2835     else
   2836     {
   2837       FT_ZERO( metrics );
   2838       metrics->x_scale = 1L << 16;
   2839       metrics->y_scale = 1L << 16;
   2840     }
   2841 
   2842     FT_TRACE5(( "FT_Request_Metrics:\n" ));
   2843     FT_TRACE5(( "  x scale: %d (%f)\n",
   2844                 metrics->x_scale, metrics->x_scale / 65536.0 ));
   2845     FT_TRACE5(( "  y scale: %d (%f)\n",
   2846                 metrics->y_scale, metrics->y_scale / 65536.0 ));
   2847     FT_TRACE5(( "  ascender: %f\n",    metrics->ascender / 64.0 ));
   2848     FT_TRACE5(( "  descender: %f\n",   metrics->descender / 64.0 ));
   2849     FT_TRACE5(( "  height: %f\n",      metrics->height / 64.0 ));
   2850     FT_TRACE5(( "  max advance: %f\n", metrics->max_advance / 64.0 ));
   2851     FT_TRACE5(( "  x ppem: %d\n",      metrics->x_ppem ));
   2852     FT_TRACE5(( "  y ppem: %d\n",      metrics->y_ppem ));
   2853   }
   2854 
   2855 
   2856   /* documentation is in freetype.h */
   2857 
   2858   FT_EXPORT_DEF( FT_Error )
   2859   FT_Select_Size( FT_Face  face,
   2860                   FT_Int   strike_index )
   2861   {
   2862     FT_Driver_Class  clazz;
   2863 
   2864 
   2865     if ( !face || !FT_HAS_FIXED_SIZES( face ) )
   2866       return FT_THROW( Invalid_Face_Handle );
   2867 
   2868     if ( strike_index < 0 || strike_index >= face->num_fixed_sizes )
   2869       return FT_THROW( Invalid_Argument );
   2870 
   2871     clazz = face->driver->clazz;
   2872 
   2873     if ( clazz->select_size )
   2874     {
   2875       FT_Error  error;
   2876 
   2877 
   2878       error = clazz->select_size( face->size, (FT_ULong)strike_index );
   2879 
   2880 #ifdef FT_DEBUG_LEVEL_TRACE
   2881       {
   2882         FT_Size_Metrics*  metrics = &face->size->metrics;
   2883 
   2884 
   2885         FT_TRACE5(( "FT_Select_Size (font driver's `select_size'):\n" ));
   2886         FT_TRACE5(( "  x scale: %d (%f)\n",
   2887                     metrics->x_scale, metrics->x_scale / 65536.0 ));
   2888         FT_TRACE5(( "  y scale: %d (%f)\n",
   2889                     metrics->y_scale, metrics->y_scale / 65536.0 ));
   2890         FT_TRACE5(( "  ascender: %f\n",    metrics->ascender / 64.0 ));
   2891         FT_TRACE5(( "  descender: %f\n",   metrics->descender / 64.0 ));
   2892         FT_TRACE5(( "  height: %f\n",      metrics->height / 64.0 ));
   2893         FT_TRACE5(( "  max advance: %f\n", metrics->max_advance / 64.0 ));
   2894         FT_TRACE5(( "  x ppem: %d\n",      metrics->x_ppem ));
   2895         FT_TRACE5(( "  y ppem: %d\n",      metrics->y_ppem ));
   2896       }
   2897 #endif
   2898 
   2899       return error;
   2900     }
   2901 
   2902     FT_Select_Metrics( face, (FT_ULong)strike_index );
   2903 
   2904     return FT_Err_Ok;
   2905   }
   2906 
   2907 
   2908   /* documentation is in freetype.h */
   2909 
   2910   FT_EXPORT_DEF( FT_Error )
   2911   FT_Request_Size( FT_Face          face,
   2912                    FT_Size_Request  req )
   2913   {
   2914     FT_Driver_Class  clazz;
   2915     FT_ULong         strike_index;
   2916 
   2917 
   2918     if ( !face )
   2919       return FT_THROW( Invalid_Face_Handle );
   2920 
   2921     if ( !req || req->width < 0 || req->height < 0 ||
   2922          req->type >= FT_SIZE_REQUEST_TYPE_MAX )
   2923       return FT_THROW( Invalid_Argument );
   2924 
   2925     clazz = face->driver->clazz;
   2926 
   2927     if ( clazz->request_size )
   2928     {
   2929       FT_Error  error;
   2930 
   2931 
   2932       error = clazz->request_size( face->size, req );
   2933 
   2934 #ifdef FT_DEBUG_LEVEL_TRACE
   2935       {
   2936         FT_Size_Metrics*  metrics = &face->size->metrics;
   2937 
   2938 
   2939         FT_TRACE5(( "FT_Request_Size (font driver's `request_size'):\n" ));
   2940         FT_TRACE5(( "  x scale: %d (%f)\n",
   2941                     metrics->x_scale, metrics->x_scale / 65536.0 ));
   2942         FT_TRACE5(( "  y scale: %d (%f)\n",
   2943                     metrics->y_scale, metrics->y_scale / 65536.0 ));
   2944         FT_TRACE5(( "  ascender: %f\n",    metrics->ascender / 64.0 ));
   2945         FT_TRACE5(( "  descender: %f\n",   metrics->descender / 64.0 ));
   2946         FT_TRACE5(( "  height: %f\n",      metrics->height / 64.0 ));
   2947         FT_TRACE5(( "  max advance: %f\n", metrics->max_advance / 64.0 ));
   2948         FT_TRACE5(( "  x ppem: %d\n",      metrics->x_ppem ));
   2949         FT_TRACE5(( "  y ppem: %d\n",      metrics->y_ppem ));
   2950       }
   2951 #endif
   2952 
   2953       return error;
   2954     }
   2955 
   2956     /*
   2957      * The reason that a driver doesn't have `request_size' defined is
   2958      * either that the scaling here suffices or that the supported formats
   2959      * are bitmap-only and size matching is not implemented.
   2960      *
   2961      * In the latter case, a simple size matching is done.
   2962      */
   2963     if ( !FT_IS_SCALABLE( face ) && FT_HAS_FIXED_SIZES( face ) )
   2964     {
   2965       FT_Error  error;
   2966 
   2967 
   2968       error = FT_Match_Size( face, req, 0, &strike_index );
   2969       if ( error )
   2970         return error;
   2971 
   2972       return FT_Select_Size( face, (FT_Int)strike_index );
   2973     }
   2974 
   2975     FT_Request_Metrics( face, req );
   2976 
   2977     return FT_Err_Ok;
   2978   }
   2979 
   2980 
   2981   /* documentation is in freetype.h */
   2982 
   2983   FT_EXPORT_DEF( FT_Error )
   2984   FT_Set_Char_Size( FT_Face     face,
   2985                     FT_F26Dot6  char_width,
   2986                     FT_F26Dot6  char_height,
   2987                     FT_UInt     horz_resolution,
   2988                     FT_UInt     vert_resolution )
   2989   {
   2990     FT_Size_RequestRec  req;
   2991 
   2992 
   2993     /* check of `face' delayed to `FT_Request_Size' */
   2994 
   2995     if ( !char_width )
   2996       char_width = char_height;
   2997     else if ( !char_height )
   2998       char_height = char_width;
   2999 
   3000     if ( !horz_resolution )
   3001       horz_resolution = vert_resolution;
   3002     else if ( !vert_resolution )
   3003       vert_resolution = horz_resolution;
   3004 
   3005     if ( char_width  < 1 * 64 )
   3006       char_width  = 1 * 64;
   3007     if ( char_height < 1 * 64 )
   3008       char_height = 1 * 64;
   3009 
   3010     if ( !horz_resolution )
   3011       horz_resolution = vert_resolution = 72;
   3012 
   3013     req.type           = FT_SIZE_REQUEST_TYPE_NOMINAL;
   3014     req.width          = char_width;
   3015     req.height         = char_height;
   3016     req.horiResolution = horz_resolution;
   3017     req.vertResolution = vert_resolution;
   3018 
   3019     return FT_Request_Size( face, &req );
   3020   }
   3021 
   3022 
   3023   /* documentation is in freetype.h */
   3024 
   3025   FT_EXPORT_DEF( FT_Error )
   3026   FT_Set_Pixel_Sizes( FT_Face  face,
   3027                       FT_UInt  pixel_width,
   3028                       FT_UInt  pixel_height )
   3029   {
   3030     FT_Size_RequestRec  req;
   3031 
   3032 
   3033     /* check of `face' delayed to `FT_Request_Size' */
   3034 
   3035     if ( pixel_width == 0 )
   3036       pixel_width = pixel_height;
   3037     else if ( pixel_height == 0 )
   3038       pixel_height = pixel_width;
   3039 
   3040     if ( pixel_width  < 1 )
   3041       pixel_width  = 1;
   3042     if ( pixel_height < 1 )
   3043       pixel_height = 1;
   3044 
   3045     /* use `>=' to avoid potential compiler warning on 16bit platforms */
   3046     if ( pixel_width >= 0xFFFFU )
   3047       pixel_width = 0xFFFFU;
   3048     if ( pixel_height >= 0xFFFFU )
   3049       pixel_height = 0xFFFFU;
   3050 
   3051     req.type           = FT_SIZE_REQUEST_TYPE_NOMINAL;
   3052     req.width          = (FT_Long)( pixel_width << 6 );
   3053     req.height         = (FT_Long)( pixel_height << 6 );
   3054     req.horiResolution = 0;
   3055     req.vertResolution = 0;
   3056 
   3057     return FT_Request_Size( face, &req );
   3058   }
   3059 
   3060 
   3061   /* documentation is in freetype.h */
   3062 
   3063   FT_EXPORT_DEF( FT_Error )
   3064   FT_Get_Kerning( FT_Face     face,
   3065                   FT_UInt     left_glyph,
   3066                   FT_UInt     right_glyph,
   3067                   FT_UInt     kern_mode,
   3068                   FT_Vector  *akerning )
   3069   {
   3070     FT_Error   error = FT_Err_Ok;
   3071     FT_Driver  driver;
   3072 
   3073 
   3074     if ( !face )
   3075       return FT_THROW( Invalid_Face_Handle );
   3076 
   3077     if ( !akerning )
   3078       return FT_THROW( Invalid_Argument );
   3079 
   3080     driver = face->driver;
   3081 
   3082     akerning->x = 0;
   3083     akerning->y = 0;
   3084 
   3085     if ( driver->clazz->get_kerning )
   3086     {
   3087       error = driver->clazz->get_kerning( face,
   3088                                           left_glyph,
   3089                                           right_glyph,
   3090                                           akerning );
   3091       if ( !error )
   3092       {
   3093         if ( kern_mode != FT_KERNING_UNSCALED )
   3094         {
   3095           akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale );
   3096           akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale );
   3097 
   3098           if ( kern_mode != FT_KERNING_UNFITTED )
   3099           {
   3100             FT_Pos  orig_x = akerning->x;
   3101             FT_Pos  orig_y = akerning->y;
   3102 
   3103 
   3104             /* we scale down kerning values for small ppem values */
   3105             /* to avoid that rounding makes them too big.         */
   3106             /* `25' has been determined heuristically.            */
   3107             if ( face->size->metrics.x_ppem < 25 )
   3108               akerning->x = FT_MulDiv( orig_x,
   3109                                        face->size->metrics.x_ppem, 25 );
   3110             if ( face->size->metrics.y_ppem < 25 )
   3111               akerning->y = FT_MulDiv( orig_y,
   3112                                        face->size->metrics.y_ppem, 25 );
   3113 
   3114             akerning->x = FT_PIX_ROUND( akerning->x );
   3115             akerning->y = FT_PIX_ROUND( akerning->y );
   3116 
   3117 #ifdef FT_DEBUG_LEVEL_TRACE
   3118             {
   3119               FT_Pos  orig_x_rounded = FT_PIX_ROUND( orig_x );
   3120               FT_Pos  orig_y_rounded = FT_PIX_ROUND( orig_y );
   3121 
   3122 
   3123               if ( akerning->x != orig_x_rounded ||
   3124                    akerning->y != orig_y_rounded )
   3125                 FT_TRACE5(( "FT_Get_Kerning: horizontal kerning"
   3126                             " (%d, %d) scaled down to (%d, %d) pixels\n",
   3127                             orig_x_rounded / 64, orig_y_rounded / 64,
   3128                             akerning->x / 64, akerning->y / 64 ));
   3129             }
   3130 #endif
   3131           }
   3132         }
   3133       }
   3134     }
   3135 
   3136     return error;
   3137   }
   3138 
   3139 
   3140   /* documentation is in freetype.h */
   3141 
   3142   FT_EXPORT_DEF( FT_Error )
   3143   FT_Get_Track_Kerning( FT_Face    face,
   3144                         FT_Fixed   point_size,
   3145                         FT_Int     degree,
   3146                         FT_Fixed*  akerning )
   3147   {
   3148     FT_Service_Kerning  service;
   3149     FT_Error            error = FT_Err_Ok;
   3150 
   3151 
   3152     if ( !face )
   3153       return FT_THROW( Invalid_Face_Handle );
   3154 
   3155     if ( !akerning )
   3156       return FT_THROW( Invalid_Argument );
   3157 
   3158     FT_FACE_FIND_SERVICE( face, service, KERNING );
   3159     if ( !service )
   3160       return FT_THROW( Unimplemented_Feature );
   3161 
   3162     error = service->get_track( face,
   3163                                 point_size,
   3164                                 degree,
   3165                                 akerning );
   3166 
   3167     return error;
   3168   }
   3169 
   3170 
   3171   /* documentation is in freetype.h */
   3172 
   3173   FT_EXPORT_DEF( FT_Error )
   3174   FT_Select_Charmap( FT_Face      face,
   3175                      FT_Encoding  encoding )
   3176   {
   3177     FT_CharMap*  cur;
   3178     FT_CharMap*  limit;
   3179 
   3180 
   3181     if ( !face )
   3182       return FT_THROW( Invalid_Face_Handle );
   3183 
   3184     if ( encoding == FT_ENCODING_NONE )
   3185       return FT_THROW( Invalid_Argument );
   3186 
   3187     /* FT_ENCODING_UNICODE is special.  We try to find the `best' Unicode */
   3188     /* charmap available, i.e., one with UCS-4 characters, if possible.   */
   3189     /*                                                                    */
   3190     /* This is done by find_unicode_charmap() above, to share code.       */
   3191     if ( encoding == FT_ENCODING_UNICODE )
   3192       return find_unicode_charmap( face );
   3193 
   3194     cur = face->charmaps;
   3195     if ( !cur )
   3196       return FT_THROW( Invalid_CharMap_Handle );
   3197 
   3198     limit = cur + face->num_charmaps;
   3199 
   3200     for ( ; cur < limit; cur++ )
   3201     {
   3202       if ( cur[0]->encoding == encoding )
   3203       {
   3204         face->charmap = cur[0];
   3205         return 0;
   3206       }
   3207     }
   3208 
   3209     return FT_THROW( Invalid_Argument );
   3210   }
   3211 
   3212 
   3213   /* documentation is in freetype.h */
   3214 
   3215   FT_EXPORT_DEF( FT_Error )
   3216   FT_Set_Charmap( FT_Face     face,
   3217                   FT_CharMap  charmap )
   3218   {
   3219     FT_CharMap*  cur;
   3220     FT_CharMap*  limit;
   3221 
   3222 
   3223     if ( !face )
   3224       return FT_THROW( Invalid_Face_Handle );
   3225 
   3226     cur = face->charmaps;
   3227     if ( !cur || !charmap )
   3228       return FT_THROW( Invalid_CharMap_Handle );
   3229 
   3230     if ( FT_Get_CMap_Format( charmap ) == 14 )
   3231       return FT_THROW( Invalid_Argument );
   3232 
   3233     limit = cur + face->num_charmaps;
   3234 
   3235     for ( ; cur < limit; cur++ )
   3236     {
   3237       if ( cur[0] == charmap )
   3238       {
   3239         face->charmap = cur[0];
   3240         return FT_Err_Ok;
   3241       }
   3242     }
   3243 
   3244     return FT_THROW( Invalid_Argument );
   3245   }
   3246 
   3247 
   3248   /* documentation is in freetype.h */
   3249 
   3250   FT_EXPORT_DEF( FT_Int )
   3251   FT_Get_Charmap_Index( FT_CharMap  charmap )
   3252   {
   3253     FT_Int  i;
   3254 
   3255 
   3256     if ( !charmap || !charmap->face )
   3257       return -1;
   3258 
   3259     for ( i = 0; i < charmap->face->num_charmaps; i++ )
   3260       if ( charmap->face->charmaps[i] == charmap )
   3261         break;
   3262 
   3263     FT_ASSERT( i < charmap->face->num_charmaps );
   3264 
   3265     return i;
   3266   }
   3267 
   3268 
   3269   static void
   3270   ft_cmap_done_internal( FT_CMap  cmap )
   3271   {
   3272     FT_CMap_Class  clazz  = cmap->clazz;
   3273     FT_Face        face   = cmap->charmap.face;
   3274     FT_Memory      memory = FT_FACE_MEMORY( face );
   3275 
   3276 
   3277     if ( clazz->done )
   3278       clazz->done( cmap );
   3279 
   3280     FT_FREE( cmap );
   3281   }
   3282 
   3283 
   3284   FT_BASE_DEF( void )
   3285   FT_CMap_Done( FT_CMap  cmap )
   3286   {
   3287     if ( cmap )
   3288     {
   3289       FT_Face    face   = cmap->charmap.face;
   3290       FT_Memory  memory = FT_FACE_MEMORY( face );
   3291       FT_Error   error;
   3292       FT_Int     i, j;
   3293 
   3294 
   3295       for ( i = 0; i < face->num_charmaps; i++ )
   3296       {
   3297         if ( (FT_CMap)face->charmaps[i] == cmap )
   3298         {
   3299           FT_CharMap  last_charmap = face->charmaps[face->num_charmaps - 1];
   3300 
   3301 
   3302           if ( FT_RENEW_ARRAY( face->charmaps,
   3303                                face->num_charmaps,
   3304                                face->num_charmaps - 1 ) )
   3305             return;
   3306 
   3307           /* remove it from our list of charmaps */
   3308           for ( j = i + 1; j < face->num_charmaps; j++ )
   3309           {
   3310             if ( j == face->num_charmaps - 1 )
   3311               face->charmaps[j - 1] = last_charmap;
   3312             else
   3313               face->charmaps[j - 1] = face->charmaps[j];
   3314           }
   3315 
   3316           face->num_charmaps--;
   3317 
   3318           if ( (FT_CMap)face->charmap == cmap )
   3319             face->charmap = NULL;
   3320 
   3321           ft_cmap_done_internal( cmap );
   3322 
   3323           break;
   3324         }
   3325       }
   3326     }
   3327   }
   3328 
   3329 
   3330   FT_BASE_DEF( FT_Error )
   3331   FT_CMap_New( FT_CMap_Class  clazz,
   3332                FT_Pointer     init_data,
   3333                FT_CharMap     charmap,
   3334                FT_CMap       *acmap )
   3335   {
   3336     FT_Error   error = FT_Err_Ok;
   3337     FT_Face    face;
   3338     FT_Memory  memory;
   3339     FT_CMap    cmap = NULL;
   3340 
   3341 
   3342     if ( clazz == NULL || charmap == NULL || charmap->face == NULL )
   3343       return FT_THROW( Invalid_Argument );
   3344 
   3345     face   = charmap->face;
   3346     memory = FT_FACE_MEMORY( face );
   3347 
   3348     if ( !FT_ALLOC( cmap, clazz->size ) )
   3349     {
   3350       cmap->charmap = *charmap;
   3351       cmap->clazz   = clazz;
   3352 
   3353       if ( clazz->init )
   3354       {
   3355         error = clazz->init( cmap, init_data );
   3356         if ( error )
   3357           goto Fail;
   3358       }
   3359 
   3360       /* add it to our list of charmaps */
   3361       if ( FT_RENEW_ARRAY( face->charmaps,
   3362                            face->num_charmaps,
   3363                            face->num_charmaps + 1 ) )
   3364         goto Fail;
   3365 
   3366       face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap;
   3367     }
   3368 
   3369   Exit:
   3370     if ( acmap )
   3371       *acmap = cmap;
   3372 
   3373     return error;
   3374 
   3375   Fail:
   3376     ft_cmap_done_internal( cmap );
   3377     cmap = NULL;
   3378     goto Exit;
   3379   }
   3380 
   3381 
   3382   /* documentation is in freetype.h */
   3383 
   3384   FT_EXPORT_DEF( FT_UInt )
   3385   FT_Get_Char_Index( FT_Face   face,
   3386                      FT_ULong  charcode )
   3387   {
   3388     FT_UInt  result = 0;
   3389 
   3390 
   3391     if ( face && face->charmap )
   3392     {
   3393       FT_CMap  cmap = FT_CMAP( face->charmap );
   3394 
   3395 
   3396       if ( charcode > 0xFFFFFFFFUL )
   3397       {
   3398         FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
   3399         FT_TRACE1(( " 0x%x is truncated\n", charcode ));
   3400       }
   3401 
   3402       result = cmap->clazz->char_index( cmap, (FT_UInt32)charcode );
   3403       if ( result >= (FT_UInt)face->num_glyphs )
   3404         result = 0;
   3405     }
   3406 
   3407     return result;
   3408   }
   3409 
   3410 
   3411   /* documentation is in freetype.h */
   3412 
   3413   FT_EXPORT_DEF( FT_ULong )
   3414   FT_Get_First_Char( FT_Face   face,
   3415                      FT_UInt  *agindex )
   3416   {
   3417     FT_ULong  result = 0;
   3418     FT_UInt   gindex = 0;
   3419 
   3420 
   3421     /* only do something if we have a charmap, and we have glyphs at all */
   3422     if ( face && face->charmap && face->num_glyphs )
   3423     {
   3424       gindex = FT_Get_Char_Index( face, 0 );
   3425       if ( gindex == 0 )
   3426         result = FT_Get_Next_Char( face, 0, &gindex );
   3427     }
   3428 
   3429     if ( agindex )
   3430       *agindex = gindex;
   3431 
   3432     return result;
   3433   }
   3434 
   3435 
   3436   /* documentation is in freetype.h */
   3437 
   3438   FT_EXPORT_DEF( FT_ULong )
   3439   FT_Get_Next_Char( FT_Face   face,
   3440                     FT_ULong  charcode,
   3441                     FT_UInt  *agindex )
   3442   {
   3443     FT_ULong  result = 0;
   3444     FT_UInt   gindex = 0;
   3445 
   3446 
   3447     if ( face && face->charmap && face->num_glyphs )
   3448     {
   3449       FT_UInt32  code = (FT_UInt32)charcode;
   3450       FT_CMap    cmap = FT_CMAP( face->charmap );
   3451 
   3452 
   3453       do
   3454       {
   3455         gindex = cmap->clazz->char_next( cmap, &code );
   3456 
   3457       } while ( gindex >= (FT_UInt)face->num_glyphs );
   3458 
   3459       result = ( gindex == 0 ) ? 0 : code;
   3460     }
   3461 
   3462     if ( agindex )
   3463       *agindex = gindex;
   3464 
   3465     return result;
   3466   }
   3467 
   3468 
   3469   /* documentation is in freetype.h */
   3470 
   3471   FT_EXPORT_DEF( FT_UInt )
   3472   FT_Face_GetCharVariantIndex( FT_Face   face,
   3473                                FT_ULong  charcode,
   3474                                FT_ULong  variantSelector )
   3475   {
   3476     FT_UInt  result = 0;
   3477 
   3478 
   3479     if ( face                                           &&
   3480          face->charmap                                  &&
   3481          face->charmap->encoding == FT_ENCODING_UNICODE )
   3482     {
   3483       FT_CharMap  charmap = find_variant_selector_charmap( face );
   3484       FT_CMap     ucmap = FT_CMAP( face->charmap );
   3485 
   3486 
   3487       if ( charmap != NULL )
   3488       {
   3489         FT_CMap  vcmap = FT_CMAP( charmap );
   3490 
   3491 
   3492         if ( charcode > 0xFFFFFFFFUL )
   3493         {
   3494           FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
   3495           FT_TRACE1(( " 0x%x is truncated\n", charcode ));
   3496         }
   3497         if ( variantSelector > 0xFFFFFFFFUL )
   3498         {
   3499           FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
   3500           FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
   3501         }
   3502 
   3503         result = vcmap->clazz->char_var_index( vcmap, ucmap,
   3504                                                (FT_UInt32)charcode,
   3505                                                (FT_UInt32)variantSelector );
   3506       }
   3507     }
   3508 
   3509     return result;
   3510   }
   3511 
   3512 
   3513   /* documentation is in freetype.h */
   3514 
   3515   FT_EXPORT_DEF( FT_Int )
   3516   FT_Face_GetCharVariantIsDefault( FT_Face   face,
   3517                                    FT_ULong  charcode,
   3518                                    FT_ULong  variantSelector )
   3519   {
   3520     FT_Int  result = -1;
   3521 
   3522 
   3523     if ( face )
   3524     {
   3525       FT_CharMap  charmap = find_variant_selector_charmap( face );
   3526 
   3527 
   3528       if ( charmap != NULL )
   3529       {
   3530         FT_CMap  vcmap = FT_CMAP( charmap );
   3531 
   3532 
   3533         if ( charcode > 0xFFFFFFFFUL )
   3534         {
   3535           FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
   3536           FT_TRACE1(( " 0x%x is truncated\n", charcode ));
   3537         }
   3538         if ( variantSelector > 0xFFFFFFFFUL )
   3539         {
   3540           FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
   3541           FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
   3542         }
   3543 
   3544         result = vcmap->clazz->char_var_default( vcmap,
   3545                                                  (FT_UInt32)charcode,
   3546                                                  (FT_UInt32)variantSelector );
   3547       }
   3548     }
   3549 
   3550     return result;
   3551   }
   3552 
   3553 
   3554   /* documentation is in freetype.h */
   3555 
   3556   FT_EXPORT_DEF( FT_UInt32* )
   3557   FT_Face_GetVariantSelectors( FT_Face  face )
   3558   {
   3559     FT_UInt32  *result = NULL;
   3560 
   3561 
   3562     if ( face )
   3563     {
   3564       FT_CharMap  charmap = find_variant_selector_charmap( face );
   3565 
   3566 
   3567       if ( charmap != NULL )
   3568       {
   3569         FT_CMap    vcmap  = FT_CMAP( charmap );
   3570         FT_Memory  memory = FT_FACE_MEMORY( face );
   3571 
   3572 
   3573         result = vcmap->clazz->variant_list( vcmap, memory );
   3574       }
   3575     }
   3576 
   3577     return result;
   3578   }
   3579 
   3580 
   3581   /* documentation is in freetype.h */
   3582 
   3583   FT_EXPORT_DEF( FT_UInt32* )
   3584   FT_Face_GetVariantsOfChar( FT_Face   face,
   3585                              FT_ULong  charcode )
   3586   {
   3587     FT_UInt32  *result = NULL;
   3588 
   3589 
   3590     if ( face )
   3591     {
   3592       FT_CharMap  charmap = find_variant_selector_charmap( face );
   3593 
   3594 
   3595       if ( charmap != NULL )
   3596       {
   3597         FT_CMap    vcmap  = FT_CMAP( charmap );
   3598         FT_Memory  memory = FT_FACE_MEMORY( face );
   3599 
   3600 
   3601         if ( charcode > 0xFFFFFFFFUL )
   3602         {
   3603           FT_TRACE1(( "FT_Get_Char_Index: too large charcode" ));
   3604           FT_TRACE1(( " 0x%x is truncated\n", charcode ));
   3605         }
   3606 
   3607         result = vcmap->clazz->charvariant_list( vcmap, memory,
   3608                                                  (FT_UInt32)charcode );
   3609       }
   3610     }
   3611     return result;
   3612   }
   3613 
   3614 
   3615   /* documentation is in freetype.h */
   3616 
   3617   FT_EXPORT_DEF( FT_UInt32* )
   3618   FT_Face_GetCharsOfVariant( FT_Face   face,
   3619                              FT_ULong  variantSelector )
   3620   {
   3621     FT_UInt32  *result = NULL;
   3622 
   3623 
   3624     if ( face )
   3625     {
   3626       FT_CharMap  charmap = find_variant_selector_charmap( face );
   3627 
   3628 
   3629       if ( charmap != NULL )
   3630       {
   3631         FT_CMap    vcmap  = FT_CMAP( charmap );
   3632         FT_Memory  memory = FT_FACE_MEMORY( face );
   3633 
   3634 
   3635         if ( variantSelector > 0xFFFFFFFFUL )
   3636         {
   3637           FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" ));
   3638           FT_TRACE1(( " 0x%x is truncated\n", variantSelector ));
   3639         }
   3640 
   3641         result = vcmap->clazz->variantchar_list( vcmap, memory,
   3642                                                  (FT_UInt32)variantSelector );
   3643       }
   3644     }
   3645 
   3646     return result;
   3647   }
   3648 
   3649 
   3650   /* documentation is in freetype.h */
   3651 
   3652   FT_EXPORT_DEF( FT_UInt )
   3653   FT_Get_Name_Index( FT_Face     face,
   3654                      FT_String*  glyph_name )
   3655   {
   3656     FT_UInt  result = 0;
   3657 
   3658 
   3659     if ( face                       &&
   3660          FT_HAS_GLYPH_NAMES( face ) &&
   3661          glyph_name                 )
   3662     {
   3663       FT_Service_GlyphDict  service;
   3664 
   3665 
   3666       FT_FACE_LOOKUP_SERVICE( face,
   3667                               service,
   3668                               GLYPH_DICT );
   3669 
   3670       if ( service && service->name_index )
   3671         result = service->name_index( face, glyph_name );
   3672     }
   3673 
   3674     return result;
   3675   }
   3676 
   3677 
   3678   /* documentation is in freetype.h */
   3679 
   3680   FT_EXPORT_DEF( FT_Error )
   3681   FT_Get_Glyph_Name( FT_Face     face,
   3682                      FT_UInt     glyph_index,
   3683                      FT_Pointer  buffer,
   3684                      FT_UInt     buffer_max )
   3685   {
   3686     FT_Error              error;
   3687     FT_Service_GlyphDict  service;
   3688 
   3689 
   3690     if ( !face )
   3691       return FT_THROW( Invalid_Face_Handle );
   3692 
   3693     if ( !buffer || buffer_max == 0 )
   3694       return FT_THROW( Invalid_Argument );
   3695 
   3696     /* clean up buffer */
   3697     ((FT_Byte*)buffer)[0] = '\0';
   3698 
   3699     if ( (FT_Long)glyph_index >= face->num_glyphs )
   3700       return FT_THROW( Invalid_Glyph_Index );
   3701 
   3702     if ( !FT_HAS_GLYPH_NAMES( face ) )
   3703       return FT_THROW( Invalid_Argument );
   3704 
   3705     FT_FACE_LOOKUP_SERVICE( face, service, GLYPH_DICT );
   3706     if ( service && service->get_name )
   3707       error = service->get_name( face, glyph_index, buffer, buffer_max );
   3708     else
   3709       error = FT_THROW( Invalid_Argument );
   3710 
   3711     return error;
   3712   }
   3713 
   3714 
   3715   /* documentation is in freetype.h */
   3716 
   3717   FT_EXPORT_DEF( const char* )
   3718   FT_Get_Postscript_Name( FT_Face  face )
   3719   {
   3720     const char*  result = NULL;
   3721 
   3722 
   3723     if ( !face )
   3724       goto Exit;
   3725 
   3726     if ( !result )
   3727     {
   3728       FT_Service_PsFontName  service;
   3729 
   3730 
   3731       FT_FACE_LOOKUP_SERVICE( face,
   3732                               service,
   3733                               POSTSCRIPT_FONT_NAME );
   3734 
   3735       if ( service && service->get_ps_font_name )
   3736         result = service->get_ps_font_name( face );
   3737     }
   3738 
   3739   Exit:
   3740     return result;
   3741   }
   3742 
   3743 
   3744   /* documentation is in tttables.h */
   3745 
   3746   FT_EXPORT_DEF( void* )
   3747   FT_Get_Sfnt_Table( FT_Face      face,
   3748                      FT_Sfnt_Tag  tag )
   3749   {
   3750     void*                  table = NULL;
   3751     FT_Service_SFNT_Table  service;
   3752 
   3753 
   3754     if ( face && FT_IS_SFNT( face ) )
   3755     {
   3756       FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
   3757       if ( service != NULL )
   3758         table = service->get_table( face, tag );
   3759     }
   3760 
   3761     return table;
   3762   }
   3763 
   3764 
   3765   /* documentation is in tttables.h */
   3766 
   3767   FT_EXPORT_DEF( FT_Error )
   3768   FT_Load_Sfnt_Table( FT_Face    face,
   3769                       FT_ULong   tag,
   3770                       FT_Long    offset,
   3771                       FT_Byte*   buffer,
   3772                       FT_ULong*  length )
   3773   {
   3774     FT_Service_SFNT_Table  service;
   3775 
   3776 
   3777     if ( !face || !FT_IS_SFNT( face ) )
   3778       return FT_THROW( Invalid_Face_Handle );
   3779 
   3780     FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
   3781     if ( service == NULL )
   3782       return FT_THROW( Unimplemented_Feature );
   3783 
   3784     return service->load_table( face, tag, offset, buffer, length );
   3785   }
   3786 
   3787 
   3788   /* documentation is in tttables.h */
   3789 
   3790   FT_EXPORT_DEF( FT_Error )
   3791   FT_Sfnt_Table_Info( FT_Face    face,
   3792                       FT_UInt    table_index,
   3793                       FT_ULong  *tag,
   3794                       FT_ULong  *length )
   3795   {
   3796     FT_Service_SFNT_Table  service;
   3797     FT_ULong               offset;
   3798 
   3799 
   3800     /* test for valid `length' delayed to `service->table_info' */
   3801 
   3802     if ( !face || !FT_IS_SFNT( face ) )
   3803       return FT_THROW( Invalid_Face_Handle );
   3804 
   3805     FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE );
   3806     if ( service == NULL )
   3807       return FT_THROW( Unimplemented_Feature );
   3808 
   3809     return service->table_info( face, table_index, tag, &offset, length );
   3810   }
   3811 
   3812 
   3813   /* documentation is in tttables.h */
   3814 
   3815   FT_EXPORT_DEF( FT_ULong )
   3816   FT_Get_CMap_Language_ID( FT_CharMap  charmap )
   3817   {
   3818     FT_Service_TTCMaps  service;
   3819     FT_Face             face;
   3820     TT_CMapInfo         cmap_info;
   3821 
   3822 
   3823     if ( !charmap || !charmap->face )
   3824       return 0;
   3825 
   3826     face = charmap->face;
   3827     FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
   3828     if ( service == NULL )
   3829       return 0;
   3830     if ( service->get_cmap_info( charmap, &cmap_info ))
   3831       return 0;
   3832 
   3833     return cmap_info.language;
   3834   }
   3835 
   3836 
   3837   /* documentation is in tttables.h */
   3838 
   3839   FT_EXPORT_DEF( FT_Long )
   3840   FT_Get_CMap_Format( FT_CharMap  charmap )
   3841   {
   3842     FT_Service_TTCMaps  service;
   3843     FT_Face             face;
   3844     TT_CMapInfo         cmap_info;
   3845 
   3846 
   3847     if ( !charmap || !charmap->face )
   3848       return -1;
   3849 
   3850     face = charmap->face;
   3851     FT_FACE_FIND_SERVICE( face, service, TT_CMAP );
   3852     if ( service == NULL )
   3853       return -1;
   3854     if ( service->get_cmap_info( charmap, &cmap_info ))
   3855       return -1;
   3856 
   3857     return cmap_info.format;
   3858   }
   3859 
   3860 
   3861   /* documentation is in ftsizes.h */
   3862 
   3863   FT_EXPORT_DEF( FT_Error )
   3864   FT_Activate_Size( FT_Size  size )
   3865   {
   3866     FT_Face  face;
   3867 
   3868 
   3869     if ( !size )
   3870       return FT_THROW( Invalid_Size_Handle );
   3871 
   3872     face = size->face;
   3873     if ( !face || !face->driver )
   3874       return FT_THROW( Invalid_Face_Handle );
   3875 
   3876     /* we don't need anything more complex than that; all size objects */
   3877     /* are already listed by the face                                  */
   3878     face->size = size;
   3879 
   3880     return FT_Err_Ok;
   3881   }
   3882 
   3883 
   3884   /*************************************************************************/
   3885   /*************************************************************************/
   3886   /*************************************************************************/
   3887   /****                                                                 ****/
   3888   /****                                                                 ****/
   3889   /****                        R E N D E R E R S                        ****/
   3890   /****                                                                 ****/
   3891   /****                                                                 ****/
   3892   /*************************************************************************/
   3893   /*************************************************************************/
   3894   /*************************************************************************/
   3895 
   3896   /* lookup a renderer by glyph format in the library's list */
   3897   FT_BASE_DEF( FT_Renderer )
   3898   FT_Lookup_Renderer( FT_Library       library,
   3899                       FT_Glyph_Format  format,
   3900                       FT_ListNode*     node )
   3901   {
   3902     FT_ListNode  cur;
   3903     FT_Renderer  result = NULL;
   3904 
   3905 
   3906     if ( !library )
   3907       goto Exit;
   3908 
   3909     cur = library->renderers.head;
   3910 
   3911     if ( node )
   3912     {
   3913       if ( *node )
   3914         cur = (*node)->next;
   3915       *node = NULL;
   3916     }
   3917 
   3918     while ( cur )
   3919     {
   3920       FT_Renderer  renderer = FT_RENDERER( cur->data );
   3921 
   3922 
   3923       if ( renderer->glyph_format == format )
   3924       {
   3925         if ( node )
   3926           *node = cur;
   3927 
   3928         result = renderer;
   3929         break;
   3930       }
   3931       cur = cur->next;
   3932     }
   3933 
   3934   Exit:
   3935     return result;
   3936   }
   3937 
   3938 
   3939   static FT_Renderer
   3940   ft_lookup_glyph_renderer( FT_GlyphSlot  slot )
   3941   {
   3942     FT_Face      face    = slot->face;
   3943     FT_Library   library = FT_FACE_LIBRARY( face );
   3944     FT_Renderer  result  = library->cur_renderer;
   3945 
   3946 
   3947     if ( !result || result->glyph_format != slot->format )
   3948       result = FT_Lookup_Renderer( library, slot->format, 0 );
   3949 
   3950     return result;
   3951   }
   3952 
   3953 
   3954   static void
   3955   ft_set_current_renderer( FT_Library  library )
   3956   {
   3957     FT_Renderer  renderer;
   3958 
   3959 
   3960     renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 );
   3961     library->cur_renderer = renderer;
   3962   }
   3963 
   3964 
   3965   static FT_Error
   3966   ft_add_renderer( FT_Module  module )
   3967   {
   3968     FT_Library   library = module->library;
   3969     FT_Memory    memory  = library->memory;
   3970     FT_Error     error;
   3971     FT_ListNode  node    = NULL;
   3972 
   3973 
   3974     if ( FT_NEW( node ) )
   3975       goto Exit;
   3976 
   3977     {
   3978       FT_Renderer         render = FT_RENDERER( module );
   3979       FT_Renderer_Class*  clazz  = (FT_Renderer_Class*)module->clazz;
   3980 
   3981 
   3982       render->clazz        = clazz;
   3983       render->glyph_format = clazz->glyph_format;
   3984 
   3985       /* allocate raster object if needed */
   3986       if ( clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
   3987            clazz->raster_class->raster_new                )
   3988       {
   3989         error = clazz->raster_class->raster_new( memory, &render->raster );
   3990         if ( error )
   3991           goto Fail;
   3992 
   3993         render->raster_render = clazz->raster_class->raster_render;
   3994         render->render        = clazz->render_glyph;
   3995       }
   3996 
   3997       /* add to list */
   3998       node->data = module;
   3999       FT_List_Add( &library->renderers, node );
   4000 
   4001       ft_set_current_renderer( library );
   4002     }
   4003 
   4004   Fail:
   4005     if ( error )
   4006       FT_FREE( node );
   4007 
   4008   Exit:
   4009     return error;
   4010   }
   4011 
   4012 
   4013   static void
   4014   ft_remove_renderer( FT_Module  module )
   4015   {
   4016     FT_Library   library;
   4017     FT_Memory    memory;
   4018     FT_ListNode  node;
   4019 
   4020 
   4021     library = module->library;
   4022     if ( !library )
   4023       return;
   4024 
   4025     memory = library->memory;
   4026 
   4027     node = FT_List_Find( &library->renderers, module );
   4028     if ( node )
   4029     {
   4030       FT_Renderer  render = FT_RENDERER( module );
   4031 
   4032 
   4033       /* release raster object, if any */
   4034       if ( render->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
   4035            render->raster                                         )
   4036         render->clazz->raster_class->raster_done( render->raster );
   4037 
   4038       /* remove from list */
   4039       FT_List_Remove( &library->renderers, node );
   4040       FT_FREE( node );
   4041 
   4042       ft_set_current_renderer( library );
   4043     }
   4044   }
   4045 
   4046 
   4047   /* documentation is in ftrender.h */
   4048 
   4049   FT_EXPORT_DEF( FT_Renderer )
   4050   FT_Get_Renderer( FT_Library       library,
   4051                    FT_Glyph_Format  format )
   4052   {
   4053     /* test for valid `library' delayed to `FT_Lookup_Renderer' */
   4054 
   4055     return FT_Lookup_Renderer( library, format, 0 );
   4056   }
   4057 
   4058 
   4059   /* documentation is in ftrender.h */
   4060 
   4061   FT_EXPORT_DEF( FT_Error )
   4062   FT_Set_Renderer( FT_Library     library,
   4063                    FT_Renderer    renderer,
   4064                    FT_UInt        num_params,
   4065                    FT_Parameter*  parameters )
   4066   {
   4067     FT_ListNode  node;
   4068     FT_Error     error = FT_Err_Ok;
   4069 
   4070     FT_Renderer_SetModeFunc  set_mode;
   4071 
   4072 
   4073     if ( !library )
   4074     {
   4075       error = FT_THROW( Invalid_Library_Handle );
   4076       goto Exit;
   4077     }
   4078 
   4079     if ( !renderer )
   4080     {
   4081       error = FT_THROW( Invalid_Argument );
   4082       goto Exit;
   4083     }
   4084 
   4085     if ( num_params > 0 && !parameters )
   4086     {
   4087       error = FT_THROW( Invalid_Argument );
   4088       goto Exit;
   4089     }
   4090 
   4091     node = FT_List_Find( &library->renderers, renderer );
   4092     if ( !node )
   4093     {
   4094       error = FT_THROW( Invalid_Argument );
   4095       goto Exit;
   4096     }
   4097 
   4098     FT_List_Up( &library->renderers, node );
   4099 
   4100     if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE )
   4101       library->cur_renderer = renderer;
   4102 
   4103     set_mode = renderer->clazz->set_mode;
   4104 
   4105     for ( ; num_params > 0; num_params-- )
   4106     {
   4107       error = set_mode( renderer, parameters->tag, parameters->data );
   4108       if ( error )
   4109         break;
   4110       parameters++;
   4111     }
   4112 
   4113   Exit:
   4114     return error;
   4115   }
   4116 
   4117 
   4118   FT_BASE_DEF( FT_Error )
   4119   FT_Render_Glyph_Internal( FT_Library      library,
   4120                             FT_GlyphSlot    slot,
   4121                             FT_Render_Mode  render_mode )
   4122   {
   4123     FT_Error     error = FT_Err_Ok;
   4124     FT_Renderer  renderer;
   4125 
   4126 
   4127     /* if it is already a bitmap, no need to do anything */
   4128     switch ( slot->format )
   4129     {
   4130     case FT_GLYPH_FORMAT_BITMAP:   /* already a bitmap, don't do anything */
   4131       break;
   4132 
   4133     default:
   4134       {
   4135         FT_ListNode  node = NULL;
   4136 
   4137 
   4138         /* small shortcut for the very common case */
   4139         if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
   4140         {
   4141           renderer = library->cur_renderer;
   4142           node     = library->renderers.head;
   4143         }
   4144         else
   4145           renderer = FT_Lookup_Renderer( library, slot->format, &node );
   4146 
   4147         error = FT_ERR( Unimplemented_Feature );
   4148         while ( renderer )
   4149         {
   4150           error = renderer->render( renderer, slot, render_mode, NULL );
   4151           if ( !error                                   ||
   4152                FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
   4153             break;
   4154 
   4155           /* FT_Err_Cannot_Render_Glyph is returned if the render mode   */
   4156           /* is unsupported by the current renderer for this glyph image */
   4157           /* format.                                                     */
   4158 
   4159           /* now, look for another renderer that supports the same */
   4160           /* format.                                               */
   4161           renderer = FT_Lookup_Renderer( library, slot->format, &node );
   4162         }
   4163       }
   4164     }
   4165 
   4166 #ifdef FT_DEBUG_LEVEL_TRACE
   4167 
   4168 #undef  FT_COMPONENT
   4169 #define FT_COMPONENT  trace_bitmap
   4170 
   4171     /*
   4172      * Computing the MD5 checksum is expensive, unnecessarily distorting a
   4173      * possible profiling of FreeType if compiled with tracing support.  For
   4174      * this reason, we execute the following code only if explicitly
   4175      * requested.
   4176      */
   4177 
   4178     /* we use FT_TRACE3 in this block */
   4179     if ( ft_trace_levels[trace_bitmap] >= 3 )
   4180     {
   4181       /* we convert to a single bitmap format for computing the checksum */
   4182       if ( !error )
   4183       {
   4184         FT_Bitmap  bitmap;
   4185         FT_Error   err;
   4186 
   4187 
   4188         FT_Bitmap_Init( &bitmap );
   4189 
   4190         /* this also converts the bitmap flow to `down' (i.e., pitch > 0) */
   4191         err = FT_Bitmap_Convert( library, &slot->bitmap, &bitmap, 1 );
   4192         if ( !err )
   4193         {
   4194           MD5_CTX        ctx;
   4195           unsigned char  md5[16];
   4196           int            i;
   4197           unsigned int   rows  = bitmap.rows;
   4198           unsigned int   pitch = (unsigned int)bitmap.pitch;
   4199 
   4200 
   4201           MD5_Init( &ctx );
   4202           MD5_Update( &ctx, bitmap.buffer, rows * pitch );
   4203           MD5_Final( md5, &ctx );
   4204 
   4205           FT_TRACE3(( "MD5 checksum for %dx%d bitmap:\n"
   4206                       "  ",
   4207                       rows, pitch ));
   4208           for ( i = 0; i < 16; i++ )
   4209             FT_TRACE3(( "%02X", md5[i] ));
   4210           FT_TRACE3(( "\n" ));
   4211         }
   4212 
   4213         FT_Bitmap_Done( library, &bitmap );
   4214       }
   4215     }
   4216 
   4217 #undef  FT_COMPONENT
   4218 #define FT_COMPONENT  trace_objs
   4219 
   4220 #endif /* FT_DEBUG_LEVEL_TRACE */
   4221 
   4222     return error;
   4223   }
   4224 
   4225 
   4226   /* documentation is in freetype.h */
   4227 
   4228   FT_EXPORT_DEF( FT_Error )
   4229   FT_Render_Glyph( FT_GlyphSlot    slot,
   4230                    FT_Render_Mode  render_mode )
   4231   {
   4232     FT_Library  library;
   4233 
   4234 
   4235     if ( !slot || !slot->face )
   4236       return FT_THROW( Invalid_Argument );
   4237 
   4238     library = FT_FACE_LIBRARY( slot->face );
   4239 
   4240     return FT_Render_Glyph_Internal( library, slot, render_mode );
   4241   }
   4242 
   4243 
   4244   /*************************************************************************/
   4245   /*************************************************************************/
   4246   /*************************************************************************/
   4247   /****                                                                 ****/
   4248   /****                                                                 ****/
   4249   /****                         M O D U L E S                           ****/
   4250   /****                                                                 ****/
   4251   /****                                                                 ****/
   4252   /*************************************************************************/
   4253   /*************************************************************************/
   4254   /*************************************************************************/
   4255 
   4256 
   4257   /*************************************************************************/
   4258   /*                                                                       */
   4259   /* <Function>                                                            */
   4260   /*    Destroy_Module                                                     */
   4261   /*                                                                       */
   4262   /* <Description>                                                         */
   4263   /*    Destroys a given module object.  For drivers, this also destroys   */
   4264   /*    all child faces.                                                   */
   4265   /*                                                                       */
   4266   /* <InOut>                                                               */
   4267   /*    module :: A handle to the target driver object.                    */
   4268   /*                                                                       */
   4269   /* <Note>                                                                */
   4270   /*    The driver _must_ be LOCKED!                                       */
   4271   /*                                                                       */
   4272   static void
   4273   Destroy_Module( FT_Module  module )
   4274   {
   4275     FT_Memory         memory  = module->memory;
   4276     FT_Module_Class*  clazz   = module->clazz;
   4277     FT_Library        library = module->library;
   4278 
   4279 
   4280     if ( library && library->auto_hinter == module )
   4281       library->auto_hinter = NULL;
   4282 
   4283     /* if the module is a renderer */
   4284     if ( FT_MODULE_IS_RENDERER( module ) )
   4285       ft_remove_renderer( module );
   4286 
   4287     /* if the module is a font driver, add some steps */
   4288     if ( FT_MODULE_IS_DRIVER( module ) )
   4289       Destroy_Driver( FT_DRIVER( module ) );
   4290 
   4291     /* finalize the module object */
   4292     if ( clazz->module_done )
   4293       clazz->module_done( module );
   4294 
   4295     /* discard it */
   4296     FT_FREE( module );
   4297   }
   4298 
   4299 
   4300   /* documentation is in ftmodapi.h */
   4301 
   4302   FT_EXPORT_DEF( FT_Error )
   4303   FT_Add_Module( FT_Library              library,
   4304                  const FT_Module_Class*  clazz )
   4305   {
   4306     FT_Error   error;
   4307     FT_Memory  memory;
   4308     FT_Module  module;
   4309     FT_UInt    nn;
   4310 
   4311 
   4312 #define FREETYPE_VER_FIXED  ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \
   4313                                 FREETYPE_MINOR                  )
   4314 
   4315     if ( !library )
   4316       return FT_THROW( Invalid_Library_Handle );
   4317 
   4318     if ( !clazz )
   4319       return FT_THROW( Invalid_Argument );
   4320 
   4321     /* check freetype version */
   4322     if ( clazz->module_requires > FREETYPE_VER_FIXED )
   4323       return FT_THROW( Invalid_Version );
   4324 
   4325     /* look for a module with the same name in the library's table */
   4326     for ( nn = 0; nn < library->num_modules; nn++ )
   4327     {
   4328       module = library->modules[nn];
   4329       if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 )
   4330       {
   4331         /* this installed module has the same name, compare their versions */
   4332         if ( clazz->module_version <= module->clazz->module_version )
   4333           return FT_THROW( Lower_Module_Version );
   4334 
   4335         /* remove the module from our list, then exit the loop to replace */
   4336         /* it by our new version..                                        */
   4337         FT_Remove_Module( library, module );
   4338         break;
   4339       }
   4340     }
   4341 
   4342     memory = library->memory;
   4343     error  = FT_Err_Ok;
   4344 
   4345     if ( library->num_modules >= FT_MAX_MODULES )
   4346     {
   4347       error = FT_THROW( Too_Many_Drivers );
   4348       goto Exit;
   4349     }
   4350 
   4351     /* allocate module object */
   4352     if ( FT_ALLOC( module, clazz->module_size ) )
   4353       goto Exit;
   4354 
   4355     /* base initialization */
   4356     module->library = library;
   4357     module->memory  = memory;
   4358     module->clazz   = (FT_Module_Class*)clazz;
   4359 
   4360     /* check whether the module is a renderer - this must be performed */
   4361     /* before the normal module initialization                         */
   4362     if ( FT_MODULE_IS_RENDERER( module ) )
   4363     {
   4364       /* add to the renderers list */
   4365       error = ft_add_renderer( module );
   4366       if ( error )
   4367         goto Fail;
   4368     }
   4369 
   4370     /* is the module a auto-hinter? */
   4371     if ( FT_MODULE_IS_HINTER( module ) )
   4372       library->auto_hinter = module;
   4373 
   4374     /* if the module is a font driver */
   4375     if ( FT_MODULE_IS_DRIVER( module ) )
   4376     {
   4377       FT_Driver  driver = FT_DRIVER( module );
   4378 
   4379 
   4380       driver->clazz = (FT_Driver_Class)module->clazz;
   4381     }
   4382 
   4383     if ( clazz->module_init )
   4384     {
   4385       error = clazz->module_init( module );
   4386       if ( error )
   4387         goto Fail;
   4388     }
   4389 
   4390     /* add module to the library's table */
   4391     library->modules[library->num_modules++] = module;
   4392 
   4393   Exit:
   4394     return error;
   4395 
   4396   Fail:
   4397     if ( FT_MODULE_IS_RENDERER( module ) )
   4398     {
   4399       FT_Renderer  renderer = FT_RENDERER( module );
   4400 
   4401 
   4402       if ( renderer->clazz                                          &&
   4403            renderer->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE &&
   4404            renderer->raster                                         )
   4405         renderer->clazz->raster_class->raster_done( renderer->raster );
   4406     }
   4407 
   4408     FT_FREE( module );
   4409     goto Exit;
   4410   }
   4411 
   4412 
   4413   /* documentation is in ftmodapi.h */
   4414 
   4415   FT_EXPORT_DEF( FT_Module )
   4416   FT_Get_Module( FT_Library   library,
   4417                  const char*  module_name )
   4418   {
   4419     FT_Module   result = NULL;
   4420     FT_Module*  cur;
   4421     FT_Module*  limit;
   4422 
   4423 
   4424     if ( !library || !module_name )
   4425       return result;
   4426 
   4427     cur   = library->modules;
   4428     limit = cur + library->num_modules;
   4429 
   4430     for ( ; cur < limit; cur++ )
   4431       if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 )
   4432       {
   4433         result = cur[0];
   4434         break;
   4435       }
   4436 
   4437     return result;
   4438   }
   4439 
   4440 
   4441   /* documentation is in ftobjs.h */
   4442 
   4443   FT_BASE_DEF( const void* )
   4444   FT_Get_Module_Interface( FT_Library   library,
   4445                            const char*  mod_name )
   4446   {
   4447     FT_Module  module;
   4448 
   4449 
   4450     /* test for valid `library' delayed to FT_Get_Module() */
   4451 
   4452     module = FT_Get_Module( library, mod_name );
   4453 
   4454     return module ? module->clazz->module_interface : 0;
   4455   }
   4456 
   4457 
   4458   FT_BASE_DEF( FT_Pointer )
   4459   ft_module_get_service( FT_Module    module,
   4460                          const char*  service_id )
   4461   {
   4462     FT_Pointer  result = NULL;
   4463 
   4464 
   4465     if ( module )
   4466     {
   4467       FT_ASSERT( module->clazz && module->clazz->get_interface );
   4468 
   4469       /* first, look for the service in the module */
   4470       if ( module->clazz->get_interface )
   4471         result = module->clazz->get_interface( module, service_id );
   4472 
   4473       if ( result == NULL )
   4474       {
   4475         /* we didn't find it, look in all other modules then */
   4476         FT_Library  library = module->library;
   4477         FT_Module*  cur     = library->modules;
   4478         FT_Module*  limit   = cur + library->num_modules;
   4479 
   4480 
   4481         for ( ; cur < limit; cur++ )
   4482         {
   4483           if ( cur[0] != module )
   4484           {
   4485             FT_ASSERT( cur[0]->clazz );
   4486 
   4487             if ( cur[0]->clazz->get_interface )
   4488             {
   4489               result = cur[0]->clazz->get_interface( cur[0], service_id );
   4490               if ( result != NULL )
   4491                 break;
   4492             }
   4493           }
   4494         }
   4495       }
   4496     }
   4497 
   4498     return result;
   4499   }
   4500 
   4501 
   4502   /* documentation is in ftmodapi.h */
   4503 
   4504   FT_EXPORT_DEF( FT_Error )
   4505   FT_Remove_Module( FT_Library  library,
   4506                     FT_Module   module )
   4507   {
   4508     /* try to find the module from the table, then remove it from there */
   4509 
   4510     if ( !library )
   4511       return FT_THROW( Invalid_Library_Handle );
   4512 
   4513     if ( module )
   4514     {
   4515       FT_Module*  cur   = library->modules;
   4516       FT_Module*  limit = cur + library->num_modules;
   4517 
   4518 
   4519       for ( ; cur < limit; cur++ )
   4520       {
   4521         if ( cur[0] == module )
   4522         {
   4523           /* remove it from the table */
   4524           library->num_modules--;
   4525           limit--;
   4526           while ( cur < limit )
   4527           {
   4528             cur[0] = cur[1];
   4529             cur++;
   4530           }
   4531           limit[0] = NULL;
   4532 
   4533           /* destroy the module */
   4534           Destroy_Module( module );
   4535 
   4536           return FT_Err_Ok;
   4537         }
   4538       }
   4539     }
   4540     return FT_THROW( Invalid_Driver_Handle );
   4541   }
   4542 
   4543 
   4544   static FT_Error
   4545   ft_property_do( FT_Library        library,
   4546                   const FT_String*  module_name,
   4547                   const FT_String*  property_name,
   4548                   void*             value,
   4549                   FT_Bool           set )
   4550   {
   4551     FT_Module*           cur;
   4552     FT_Module*           limit;
   4553     FT_Module_Interface  interface;
   4554 
   4555     FT_Service_Properties  service;
   4556 
   4557 #ifdef FT_DEBUG_LEVEL_ERROR
   4558     const FT_String*  set_name  = "FT_Property_Set";
   4559     const FT_String*  get_name  = "FT_Property_Get";
   4560     const FT_String*  func_name = set ? set_name : get_name;
   4561 #endif
   4562 
   4563     FT_Bool  missing_func;
   4564 
   4565 
   4566     if ( !library )
   4567       return FT_THROW( Invalid_Library_Handle );
   4568 
   4569     if ( !module_name || !property_name || !value )
   4570       return FT_THROW( Invalid_Argument );
   4571 
   4572     cur   = library->modules;
   4573     limit = cur + library->num_modules;
   4574 
   4575     /* search module */
   4576     for ( ; cur < limit; cur++ )
   4577       if ( !ft_strcmp( cur[0]->clazz->module_name, module_name ) )
   4578         break;
   4579 
   4580     if ( cur == limit )
   4581     {
   4582       FT_ERROR(( "%s: can't find module `%s'\n",
   4583                  func_name, module_name ));
   4584       return FT_THROW( Missing_Module );
   4585     }
   4586 
   4587     /* check whether we have a service interface */
   4588     if ( !cur[0]->clazz->get_interface )
   4589     {
   4590       FT_ERROR(( "%s: module `%s' doesn't support properties\n",
   4591                  func_name, module_name ));
   4592       return FT_THROW( Unimplemented_Feature );
   4593     }
   4594 
   4595     /* search property service */
   4596     interface = cur[0]->clazz->get_interface( cur[0],
   4597                                               FT_SERVICE_ID_PROPERTIES );
   4598     if ( !interface )
   4599     {
   4600       FT_ERROR(( "%s: module `%s' doesn't support properties\n",
   4601                  func_name, module_name ));
   4602       return FT_THROW( Unimplemented_Feature );
   4603     }
   4604 
   4605     service = (FT_Service_Properties)interface;
   4606 
   4607     if ( set )
   4608       missing_func = (FT_Bool)( !service->set_property );
   4609     else
   4610       missing_func = (FT_Bool)( !service->get_property );
   4611 
   4612     if ( missing_func )
   4613     {
   4614       FT_ERROR(( "%s: property service of module `%s' is broken\n",
   4615                  func_name, module_name ));
   4616       return FT_THROW( Unimplemented_Feature );
   4617     }
   4618 
   4619     return set ? service->set_property( cur[0], property_name, value )
   4620                : service->get_property( cur[0], property_name, value );
   4621   }
   4622 
   4623 
   4624   /* documentation is in ftmodapi.h */
   4625 
   4626   FT_EXPORT_DEF( FT_Error )
   4627   FT_Property_Set( FT_Library        library,
   4628                    const FT_String*  module_name,
   4629                    const FT_String*  property_name,
   4630                    const void*       value )
   4631   {
   4632     return ft_property_do( library,
   4633                            module_name,
   4634                            property_name,
   4635                            (void*)value,
   4636                            TRUE );
   4637   }
   4638 
   4639 
   4640   /* documentation is in ftmodapi.h */
   4641 
   4642   FT_EXPORT_DEF( FT_Error )
   4643   FT_Property_Get( FT_Library        library,
   4644                    const FT_String*  module_name,
   4645                    const FT_String*  property_name,
   4646                    void*             value )
   4647   {
   4648     return ft_property_do( library,
   4649                            module_name,
   4650                            property_name,
   4651                            value,
   4652                            FALSE );
   4653   }
   4654 
   4655 
   4656   /*************************************************************************/
   4657   /*************************************************************************/
   4658   /*************************************************************************/
   4659   /****                                                                 ****/
   4660   /****                                                                 ****/
   4661   /****                         L I B R A R Y                           ****/
   4662   /****                                                                 ****/
   4663   /****                                                                 ****/
   4664   /*************************************************************************/
   4665   /*************************************************************************/
   4666   /*************************************************************************/
   4667 
   4668 
   4669   /* documentation is in ftmodapi.h */
   4670 
   4671   FT_EXPORT_DEF( FT_Error )
   4672   FT_Reference_Library( FT_Library  library )
   4673   {
   4674     if ( !library )
   4675       return FT_THROW( Invalid_Library_Handle );
   4676 
   4677     library->refcount++;
   4678 
   4679     return FT_Err_Ok;
   4680   }
   4681 
   4682 
   4683   /* documentation is in ftmodapi.h */
   4684 
   4685   FT_EXPORT_DEF( FT_Error )
   4686   FT_New_Library( FT_Memory    memory,
   4687                   FT_Library  *alibrary )
   4688   {
   4689     FT_Library  library = NULL;
   4690     FT_Error    error;
   4691 
   4692 
   4693     if ( !memory || !alibrary )
   4694       return FT_THROW( Invalid_Argument );
   4695 
   4696 #ifdef FT_DEBUG_LEVEL_ERROR
   4697     /* init debugging support */
   4698     ft_debug_init();
   4699 #endif
   4700 
   4701     /* first of all, allocate the library object */
   4702     if ( FT_NEW( library ) )
   4703       return error;
   4704 
   4705     library->memory = memory;
   4706 
   4707 #ifdef FT_CONFIG_OPTION_PIC
   4708     /* initialize position independent code containers */
   4709     error = ft_pic_container_init( library );
   4710     if ( error )
   4711       goto Fail;
   4712 #endif
   4713 
   4714     /* we don't use raster_pool anymore. */
   4715     library->raster_pool_size = 0;
   4716     library->raster_pool      = NULL;
   4717 
   4718     library->version_major = FREETYPE_MAJOR;
   4719     library->version_minor = FREETYPE_MINOR;
   4720     library->version_patch = FREETYPE_PATCH;
   4721 
   4722     library->refcount = 1;
   4723 
   4724     /* That's ok now */
   4725     *alibrary = library;
   4726 
   4727     return FT_Err_Ok;
   4728 
   4729 #ifdef FT_CONFIG_OPTION_PIC
   4730   Fail:
   4731     ft_pic_container_destroy( library );
   4732 #endif
   4733     FT_FREE( library );
   4734     return error;
   4735   }
   4736 
   4737 
   4738   /* documentation is in freetype.h */
   4739 
   4740   FT_EXPORT_DEF( void )
   4741   FT_Library_Version( FT_Library   library,
   4742                       FT_Int      *amajor,
   4743                       FT_Int      *aminor,
   4744                       FT_Int      *apatch )
   4745   {
   4746     FT_Int  major = 0;
   4747     FT_Int  minor = 0;
   4748     FT_Int  patch = 0;
   4749 
   4750 
   4751     if ( library )
   4752     {
   4753       major = library->version_major;
   4754       minor = library->version_minor;
   4755       patch = library->version_patch;
   4756     }
   4757 
   4758     if ( amajor )
   4759       *amajor = major;
   4760 
   4761     if ( aminor )
   4762       *aminor = minor;
   4763 
   4764     if ( apatch )
   4765       *apatch = patch;
   4766   }
   4767 
   4768 
   4769   /* documentation is in ftmodapi.h */
   4770 
   4771   FT_EXPORT_DEF( FT_Error )
   4772   FT_Done_Library( FT_Library  library )
   4773   {
   4774     FT_Memory  memory;
   4775 
   4776 
   4777     if ( !library )
   4778       return FT_THROW( Invalid_Library_Handle );
   4779 
   4780     library->refcount--;
   4781     if ( library->refcount > 0 )
   4782       goto Exit;
   4783 
   4784     memory = library->memory;
   4785 
   4786     /*
   4787      * Close all faces in the library.  If we don't do this, we can have
   4788      * some subtle memory leaks.
   4789      *
   4790      * Example:
   4791      *
   4792      *  - the cff font driver uses the pshinter module in cff_size_done
   4793      *  - if the pshinter module is destroyed before the cff font driver,
   4794      *    opened FT_Face objects managed by the driver are not properly
   4795      *    destroyed, resulting in a memory leak
   4796      *
   4797      * Some faces are dependent on other faces, like Type42 faces that
   4798      * depend on TrueType faces synthesized internally.
   4799      *
   4800      * The order of drivers should be specified in driver_name[].
   4801      */
   4802     {
   4803       FT_UInt      m, n;
   4804       const char*  driver_name[] = { "type42", NULL };
   4805 
   4806 
   4807       for ( m = 0;
   4808             m < sizeof ( driver_name ) / sizeof ( driver_name[0] );
   4809             m++ )
   4810       {
   4811         for ( n = 0; n < library->num_modules; n++ )
   4812         {
   4813           FT_Module    module      = library->modules[n];
   4814           const char*  module_name = module->clazz->module_name;
   4815           FT_List      faces;
   4816 
   4817 
   4818           if ( driver_name[m]                                &&
   4819                ft_strcmp( module_name, driver_name[m] ) != 0 )
   4820             continue;
   4821 
   4822           if ( ( module->clazz->module_flags & FT_MODULE_FONT_DRIVER ) == 0 )
   4823             continue;
   4824 
   4825           FT_TRACE7(( "FT_Done_Library: close faces for %s\n", module_name ));
   4826 
   4827           faces = &FT_DRIVER( module )->faces_list;
   4828           while ( faces->head )
   4829           {
   4830             FT_Done_Face( FT_FACE( faces->head->data ) );
   4831             if ( faces->head )
   4832               FT_TRACE0(( "FT_Done_Library: failed to free some faces\n" ));
   4833           }
   4834         }
   4835       }
   4836     }
   4837 
   4838     /* Close all other modules in the library */
   4839 #if 1
   4840     /* XXX Modules are removed in the reversed order so that  */
   4841     /* type42 module is removed before truetype module.  This */
   4842     /* avoids double free in some occasions.  It is a hack.   */
   4843     while ( library->num_modules > 0 )
   4844       FT_Remove_Module( library,
   4845                         library->modules[library->num_modules - 1] );
   4846 #else
   4847     {
   4848       FT_UInt  n;
   4849 
   4850 
   4851       for ( n = 0; n < library->num_modules; n++ )
   4852       {
   4853         FT_Module  module = library->modules[n];
   4854 
   4855 
   4856         if ( module )
   4857         {
   4858           Destroy_Module( module );
   4859           library->modules[n] = NULL;
   4860         }
   4861       }
   4862     }
   4863 #endif
   4864 
   4865 #ifdef FT_CONFIG_OPTION_PIC
   4866     /* Destroy pic container contents */
   4867     ft_pic_container_destroy( library );
   4868 #endif
   4869 
   4870     FT_FREE( library );
   4871 
   4872   Exit:
   4873     return FT_Err_Ok;
   4874   }
   4875 
   4876 
   4877   /* documentation is in ftmodapi.h */
   4878 
   4879   FT_EXPORT_DEF( void )
   4880   FT_Set_Debug_Hook( FT_Library         library,
   4881                      FT_UInt            hook_index,
   4882                      FT_DebugHook_Func  debug_hook )
   4883   {
   4884     if ( library && debug_hook &&
   4885          hook_index <
   4886            ( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) )
   4887       library->debug_hooks[hook_index] = debug_hook;
   4888   }
   4889 
   4890 
   4891   /* documentation is in ftmodapi.h */
   4892 
   4893   FT_EXPORT_DEF( FT_TrueTypeEngineType )
   4894   FT_Get_TrueType_Engine_Type( FT_Library  library )
   4895   {
   4896     FT_TrueTypeEngineType  result = FT_TRUETYPE_ENGINE_TYPE_NONE;
   4897 
   4898 
   4899     if ( library )
   4900     {
   4901       FT_Module  module = FT_Get_Module( library, "truetype" );
   4902 
   4903 
   4904       if ( module )
   4905       {
   4906         FT_Service_TrueTypeEngine  service;
   4907 
   4908 
   4909         service = (FT_Service_TrueTypeEngine)
   4910                     ft_module_get_service( module,
   4911                                            FT_SERVICE_ID_TRUETYPE_ENGINE );
   4912         if ( service )
   4913           result = service->engine_type;
   4914       }
   4915     }
   4916 
   4917     return result;
   4918   }
   4919 
   4920 
   4921   /* documentation is in freetype.h */
   4922 
   4923   FT_EXPORT_DEF( FT_Error )
   4924   FT_Get_SubGlyph_Info( FT_GlyphSlot  glyph,
   4925                         FT_UInt       sub_index,
   4926                         FT_Int       *p_index,
   4927                         FT_UInt      *p_flags,
   4928                         FT_Int       *p_arg1,
   4929                         FT_Int       *p_arg2,
   4930                         FT_Matrix    *p_transform )
   4931   {
   4932     FT_Error  error = FT_ERR( Invalid_Argument );
   4933 
   4934 
   4935     if ( glyph                                      &&
   4936          glyph->subglyphs                           &&
   4937          glyph->format == FT_GLYPH_FORMAT_COMPOSITE &&
   4938          sub_index < glyph->num_subglyphs           )
   4939     {
   4940       FT_SubGlyph  subg = glyph->subglyphs + sub_index;
   4941 
   4942 
   4943       *p_index     = subg->index;
   4944       *p_flags     = subg->flags;
   4945       *p_arg1      = subg->arg1;
   4946       *p_arg2      = subg->arg2;
   4947       *p_transform = subg->transform;
   4948 
   4949       error = FT_Err_Ok;
   4950     }
   4951 
   4952     return error;
   4953   }
   4954 
   4955 
   4956 /* END */
   4957