Home | History | Annotate | Download | only in truetype
      1 /***************************************************************************/
      2 /*                                                                         */
      3 /*  ttdriver.c                                                             */
      4 /*                                                                         */
      5 /*    TrueType font driver implementation (body).                          */
      6 /*                                                                         */
      7 /*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009    */
      8 /*            2010 by                                                      */
      9 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
     10 /*                                                                         */
     11 /*  This file is part of the FreeType project, and may only be used,       */
     12 /*  modified, and distributed under the terms of the FreeType project      */
     13 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
     14 /*  this file you indicate that you have read the license and              */
     15 /*  understand and accept it fully.                                        */
     16 /*                                                                         */
     17 /***************************************************************************/
     18 
     19 
     20 #include <ft2build.h>
     21 #include FT_INTERNAL_DEBUG_H
     22 #include FT_INTERNAL_STREAM_H
     23 #include FT_INTERNAL_SFNT_H
     24 #include FT_SERVICE_XFREE86_NAME_H
     25 
     26 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
     27 #include FT_MULTIPLE_MASTERS_H
     28 #include FT_SERVICE_MULTIPLE_MASTERS_H
     29 #endif
     30 
     31 #include FT_SERVICE_TRUETYPE_ENGINE_H
     32 #include FT_SERVICE_TRUETYPE_GLYF_H
     33 
     34 #include "ttdriver.h"
     35 #include "ttgload.h"
     36 #include "ttpload.h"
     37 
     38 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
     39 #include "ttgxvar.h"
     40 #endif
     41 
     42 #include "tterrors.h"
     43 
     44 #include "ttpic.h"
     45 
     46   /*************************************************************************/
     47   /*                                                                       */
     48   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
     49   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
     50   /* messages during execution.                                            */
     51   /*                                                                       */
     52 #undef  FT_COMPONENT
     53 #define FT_COMPONENT  trace_ttdriver
     54 
     55 
     56   /*************************************************************************/
     57   /*************************************************************************/
     58   /*************************************************************************/
     59   /****                                                                 ****/
     60   /****                                                                 ****/
     61   /****                          F A C E S                              ****/
     62   /****                                                                 ****/
     63   /****                                                                 ****/
     64   /*************************************************************************/
     65   /*************************************************************************/
     66   /*************************************************************************/
     67 
     68 
     69 #undef  PAIR_TAG
     70 #define PAIR_TAG( left, right )  ( ( (FT_ULong)left << 16 ) | \
     71                                      (FT_ULong)right        )
     72 
     73 
     74   /*************************************************************************/
     75   /*                                                                       */
     76   /* <Function>                                                            */
     77   /*    tt_get_kerning                                                     */
     78   /*                                                                       */
     79   /* <Description>                                                         */
     80   /*    A driver method used to return the kerning vector between two      */
     81   /*    glyphs of the same face.                                           */
     82   /*                                                                       */
     83   /* <Input>                                                               */
     84   /*    face        :: A handle to the source face object.                 */
     85   /*                                                                       */
     86   /*    left_glyph  :: The index of the left glyph in the kern pair.       */
     87   /*                                                                       */
     88   /*    right_glyph :: The index of the right glyph in the kern pair.      */
     89   /*                                                                       */
     90   /* <Output>                                                              */
     91   /*    kerning     :: The kerning vector.  This is in font units for      */
     92   /*                   scalable formats, and in pixels for fixed-sizes     */
     93   /*                   formats.                                            */
     94   /*                                                                       */
     95   /* <Return>                                                              */
     96   /*    FreeType error code.  0 means success.                             */
     97   /*                                                                       */
     98   /* <Note>                                                                */
     99   /*    Only horizontal layouts (left-to-right & right-to-left) are        */
    100   /*    supported by this function.  Other layouts, or more sophisticated  */
    101   /*    kernings, are out of scope of this method (the basic driver        */
    102   /*    interface is meant to be simple).                                  */
    103   /*                                                                       */
    104   /*    They can be implemented by format-specific interfaces.             */
    105   /*                                                                       */
    106   static FT_Error
    107   tt_get_kerning( FT_Face     ttface,          /* TT_Face */
    108                   FT_UInt     left_glyph,
    109                   FT_UInt     right_glyph,
    110                   FT_Vector*  kerning )
    111   {
    112     TT_Face       face = (TT_Face)ttface;
    113     SFNT_Service  sfnt = (SFNT_Service)face->sfnt;
    114 
    115 
    116     kerning->x = 0;
    117     kerning->y = 0;
    118 
    119     if ( sfnt )
    120       kerning->x = sfnt->get_kerning( face, left_glyph, right_glyph );
    121 
    122     return 0;
    123   }
    124 
    125 
    126 #undef PAIR_TAG
    127 
    128 
    129   static FT_Error
    130   tt_get_advances( FT_Face    ttface,
    131                    FT_UInt    start,
    132                    FT_UInt    count,
    133                    FT_Int32   flags,
    134                    FT_Fixed  *advances )
    135   {
    136     FT_UInt  nn;
    137     TT_Face  face  = (TT_Face) ttface;
    138     FT_Bool  check = FT_BOOL(
    139                        !( flags & FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ) );
    140 
    141 
    142     /* XXX: TODO: check for sbits */
    143 
    144     if ( flags & FT_LOAD_VERTICAL_LAYOUT )
    145     {
    146       for ( nn = 0; nn < count; nn++ )
    147       {
    148         FT_Short   tsb;
    149         FT_UShort  ah;
    150 
    151 
    152         TT_Get_VMetrics( face, start + nn, check, &tsb, &ah );
    153         advances[nn] = ah;
    154       }
    155     }
    156     else
    157     {
    158       for ( nn = 0; nn < count; nn++ )
    159       {
    160         FT_Short   lsb;
    161         FT_UShort  aw;
    162 
    163 
    164         TT_Get_HMetrics( face, start + nn, check, &lsb, &aw );
    165         advances[nn] = aw;
    166       }
    167     }
    168 
    169     return TT_Err_Ok;
    170   }
    171 
    172   /*************************************************************************/
    173   /*************************************************************************/
    174   /*************************************************************************/
    175   /****                                                                 ****/
    176   /****                                                                 ****/
    177   /****                           S I Z E S                             ****/
    178   /****                                                                 ****/
    179   /****                                                                 ****/
    180   /*************************************************************************/
    181   /*************************************************************************/
    182   /*************************************************************************/
    183 
    184 
    185 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
    186 
    187   static FT_Error
    188   tt_size_select( FT_Size   size,
    189                   FT_ULong  strike_index )
    190   {
    191     TT_Face   ttface = (TT_Face)size->face;
    192     TT_Size   ttsize = (TT_Size)size;
    193     FT_Error  error  = TT_Err_Ok;
    194 
    195 
    196     ttsize->strike_index = strike_index;
    197 
    198     if ( FT_IS_SCALABLE( size->face ) )
    199     {
    200       /* use the scaled metrics, even when tt_size_reset fails */
    201       FT_Select_Metrics( size->face, strike_index );
    202 
    203       tt_size_reset( ttsize );
    204     }
    205     else
    206     {
    207       SFNT_Service      sfnt    = (SFNT_Service) ttface->sfnt;
    208       FT_Size_Metrics*  metrics = &size->metrics;
    209 
    210 
    211       error = sfnt->load_strike_metrics( ttface, strike_index, metrics );
    212       if ( error )
    213         ttsize->strike_index = 0xFFFFFFFFUL;
    214     }
    215 
    216     return error;
    217   }
    218 
    219 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
    220 
    221 
    222   static FT_Error
    223   tt_size_request( FT_Size          size,
    224                    FT_Size_Request  req )
    225   {
    226     TT_Size   ttsize = (TT_Size)size;
    227     FT_Error  error  = TT_Err_Ok;
    228 
    229 
    230 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
    231 
    232     if ( FT_HAS_FIXED_SIZES( size->face ) )
    233     {
    234       TT_Face       ttface = (TT_Face)size->face;
    235       SFNT_Service  sfnt   = (SFNT_Service) ttface->sfnt;
    236       FT_ULong      strike_index;
    237 
    238 
    239       error = sfnt->set_sbit_strike( ttface, req, &strike_index );
    240 
    241       if ( error )
    242         ttsize->strike_index = 0xFFFFFFFFUL;
    243       else
    244         return tt_size_select( size, strike_index );
    245     }
    246 
    247 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
    248 
    249     FT_Request_Metrics( size->face, req );
    250 
    251     if ( FT_IS_SCALABLE( size->face ) )
    252       error = tt_size_reset( ttsize );
    253 
    254     return error;
    255   }
    256 
    257 
    258   /*************************************************************************/
    259   /*                                                                       */
    260   /* <Function>                                                            */
    261   /*    Load_Glyph                                                         */
    262   /*                                                                       */
    263   /* <Description>                                                         */
    264   /*    A driver method used to load a glyph within a given glyph slot.    */
    265   /*                                                                       */
    266   /* <Input>                                                               */
    267   /*    slot        :: A handle to the target slot object where the glyph  */
    268   /*                   will be loaded.                                     */
    269   /*                                                                       */
    270   /*    size        :: A handle to the source face size at which the glyph */
    271   /*                   must be scaled, loaded, etc.                        */
    272   /*                                                                       */
    273   /*    glyph_index :: The index of the glyph in the font file.            */
    274   /*                                                                       */
    275   /*    load_flags  :: A flag indicating what to load for this glyph.  The */
    276   /*                   FT_LOAD_XXX constants can be used to control the    */
    277   /*                   glyph loading process (e.g., whether the outline    */
    278   /*                   should be scaled, whether to load bitmaps or not,   */
    279   /*                   whether to hint the outline, etc).                  */
    280   /*                                                                       */
    281   /* <Return>                                                              */
    282   /*    FreeType error code.  0 means success.                             */
    283   /*                                                                       */
    284   static FT_Error
    285   Load_Glyph( FT_GlyphSlot  ttslot,         /* TT_GlyphSlot */
    286               FT_Size       ttsize,         /* TT_Size      */
    287               FT_UInt       glyph_index,
    288               FT_Int32      load_flags )
    289   {
    290     TT_GlyphSlot  slot = (TT_GlyphSlot)ttslot;
    291     TT_Size       size = (TT_Size)ttsize;
    292     FT_Face       face = ttslot->face;
    293     FT_Error      error;
    294 
    295 
    296     if ( !slot )
    297       return TT_Err_Invalid_Slot_Handle;
    298 
    299     if ( !size )
    300       return TT_Err_Invalid_Size_Handle;
    301 
    302     if ( !face )
    303       return TT_Err_Invalid_Argument;
    304 
    305 #ifdef FT_CONFIG_OPTION_INCREMENTAL
    306     if ( glyph_index >= (FT_UInt)face->num_glyphs &&
    307          !face->internal->incremental_interface   )
    308 #else
    309     if ( glyph_index >= (FT_UInt)face->num_glyphs )
    310 #endif
    311       return TT_Err_Invalid_Argument;
    312 
    313     if ( load_flags & FT_LOAD_NO_HINTING )
    314     {
    315       /* both FT_LOAD_NO_HINTING and FT_LOAD_NO_AUTOHINT   */
    316       /* are necessary to disable hinting for tricky fonts */
    317 
    318       if ( FT_IS_TRICKY( face ) )
    319         load_flags &= ~FT_LOAD_NO_HINTING;
    320 
    321       if ( load_flags & FT_LOAD_NO_AUTOHINT )
    322         load_flags |= FT_LOAD_NO_HINTING;
    323     }
    324 
    325     if ( load_flags & ( FT_LOAD_NO_RECURSE | FT_LOAD_NO_SCALE ) )
    326     {
    327       load_flags |= FT_LOAD_NO_BITMAP | FT_LOAD_NO_SCALE;
    328 
    329       if ( !FT_IS_TRICKY( face ) )
    330         load_flags |= FT_LOAD_NO_HINTING;
    331     }
    332 
    333     /* now load the glyph outline if necessary */
    334     error = TT_Load_Glyph( size, slot, glyph_index, load_flags );
    335 
    336     /* force drop-out mode to 2 - irrelevant now */
    337     /* slot->outline.dropout_mode = 2; */
    338 
    339     return error;
    340   }
    341 
    342 
    343   /*************************************************************************/
    344   /*************************************************************************/
    345   /*************************************************************************/
    346   /****                                                                 ****/
    347   /****                                                                 ****/
    348   /****                D R I V E R  I N T E R F A C E                   ****/
    349   /****                                                                 ****/
    350   /****                                                                 ****/
    351   /*************************************************************************/
    352   /*************************************************************************/
    353   /*************************************************************************/
    354 
    355 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
    356   FT_DEFINE_SERVICE_MULTIMASTERSREC(tt_service_gx_multi_masters,
    357     (FT_Get_MM_Func)        NULL,
    358     (FT_Set_MM_Design_Func) NULL,
    359     (FT_Set_MM_Blend_Func)  TT_Set_MM_Blend,
    360     (FT_Get_MM_Var_Func)    TT_Get_MM_Var,
    361     (FT_Set_Var_Design_Func)TT_Set_Var_Design
    362   )
    363 #endif
    364 
    365   static const FT_Service_TrueTypeEngineRec  tt_service_truetype_engine =
    366   {
    367 #ifdef TT_USE_BYTECODE_INTERPRETER
    368 
    369 #ifdef TT_CONFIG_OPTION_UNPATENTED_HINTING
    370     FT_TRUETYPE_ENGINE_TYPE_UNPATENTED
    371 #else
    372     FT_TRUETYPE_ENGINE_TYPE_PATENTED
    373 #endif
    374 
    375 #else /* !TT_USE_BYTECODE_INTERPRETER */
    376 
    377     FT_TRUETYPE_ENGINE_TYPE_NONE
    378 
    379 #endif /* TT_USE_BYTECODE_INTERPRETER */
    380   };
    381 
    382   FT_DEFINE_SERVICE_TTGLYFREC(tt_service_truetype_glyf,
    383     (TT_Glyf_GetLocationFunc)tt_face_get_location
    384   )
    385 
    386 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
    387   FT_DEFINE_SERVICEDESCREC4(tt_services,
    388     FT_SERVICE_ID_XF86_NAME,       FT_XF86_FORMAT_TRUETYPE,
    389     FT_SERVICE_ID_MULTI_MASTERS,   &FT_TT_SERVICE_GX_MULTI_MASTERS_GET,
    390     FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
    391     FT_SERVICE_ID_TT_GLYF,         &FT_TT_SERVICE_TRUETYPE_GLYF_GET
    392   )
    393 #else
    394   FT_DEFINE_SERVICEDESCREC3(tt_services,
    395     FT_SERVICE_ID_XF86_NAME,       FT_XF86_FORMAT_TRUETYPE,
    396     FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
    397     FT_SERVICE_ID_TT_GLYF,         &FT_TT_SERVICE_TRUETYPE_GLYF_GET
    398   )
    399 #endif
    400 
    401   FT_CALLBACK_DEF( FT_Module_Interface )
    402   tt_get_interface( FT_Module    driver,    /* TT_Driver */
    403                     const char*  tt_interface )
    404   {
    405     FT_Module_Interface  result;
    406     FT_Module            sfntd;
    407     SFNT_Service         sfnt;
    408 
    409     result = ft_service_list_lookup( FT_TT_SERVICES_GET, tt_interface );
    410     if ( result != NULL )
    411       return result;
    412 
    413     if ( !driver )
    414       return NULL;
    415 
    416     /* only return the default interface from the SFNT module */
    417     sfntd = FT_Get_Module( driver->library, "sfnt" );
    418     if ( sfntd )
    419     {
    420       sfnt = (SFNT_Service)( sfntd->clazz->module_interface );
    421       if ( sfnt )
    422         return sfnt->get_interface( driver, tt_interface );
    423     }
    424 
    425     return 0;
    426   }
    427 
    428 
    429   /* The FT_DriverInterface structure is defined in ftdriver.h. */
    430 
    431 #ifdef TT_USE_BYTECODE_INTERPRETER
    432 #define TT_HINTER_FLAG   FT_MODULE_DRIVER_HAS_HINTER
    433 #else
    434 #define TT_HINTER_FLAG   0
    435 #endif
    436 
    437 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
    438 #define TT_SIZE_SELECT    tt_size_select
    439 #else
    440 #define TT_SIZE_SELECT    0
    441 #endif
    442 
    443   FT_DEFINE_DRIVER(tt_driver_class,
    444 
    445 
    446       FT_MODULE_FONT_DRIVER        |
    447       FT_MODULE_DRIVER_SCALABLE    |
    448       TT_HINTER_FLAG,
    449 
    450       sizeof ( TT_DriverRec ),
    451 
    452       "truetype",      /* driver name                           */
    453       0x10000L,        /* driver version == 1.0                 */
    454       0x20000L,        /* driver requires FreeType 2.0 or above */
    455 
    456       (void*)0,        /* driver specific interface */
    457 
    458       tt_driver_init,
    459       tt_driver_done,
    460       tt_get_interface,
    461 
    462     sizeof ( TT_FaceRec ),
    463     sizeof ( TT_SizeRec ),
    464     sizeof ( FT_GlyphSlotRec ),
    465 
    466     tt_face_init,
    467     tt_face_done,
    468     tt_size_init,
    469     tt_size_done,
    470     tt_slot_init,
    471     0,                      /* FT_Slot_DoneFunc */
    472 
    473     ft_stub_set_char_sizes, /* FT_CONFIG_OPTION_OLD_INTERNALS */
    474     ft_stub_set_pixel_sizes, /* FT_CONFIG_OPTION_OLD_INTERNALS */
    475 
    476     Load_Glyph,
    477 
    478     tt_get_kerning,
    479     0,                      /* FT_Face_AttachFunc      */
    480     tt_get_advances,
    481 
    482     tt_size_request,
    483     TT_SIZE_SELECT
    484   )
    485 
    486 
    487 /* END */
    488