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