ttdriver.c
上传用户:yisoukefu
上传日期:2020-08-09
资源大小:39506k
文件大小:16k
源码类别:

其他游戏

开发平台:

Visual C++

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /*  ttdriver.c                                                             */
  4. /*                                                                         */
  5. /*    TrueType font driver implementation (body).                          */
  6. /*                                                                         */
  7. /*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007 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. #include <ft2build.h>
  18. #include FT_INTERNAL_DEBUG_H
  19. #include FT_INTERNAL_STREAM_H
  20. #include FT_INTERNAL_SFNT_H
  21. #include FT_TRUETYPE_IDS_H
  22. #include FT_SERVICE_XFREE86_NAME_H
  23. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  24. #include FT_MULTIPLE_MASTERS_H
  25. #include FT_SERVICE_MULTIPLE_MASTERS_H
  26. #endif
  27. #include FT_SERVICE_TRUETYPE_ENGINE_H
  28. #include FT_SERVICE_TRUETYPE_GLYF_H
  29. #include "ttdriver.h"
  30. #include "ttgload.h"
  31. #include "ttpload.h"
  32. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  33. #include "ttgxvar.h"
  34. #endif
  35. #include "tterrors.h"
  36.   /*************************************************************************/
  37.   /*                                                                       */
  38.   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  39.   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  40.   /* messages during execution.                                            */
  41.   /*                                                                       */
  42. #undef  FT_COMPONENT
  43. #define FT_COMPONENT  trace_ttdriver
  44.   /*************************************************************************/
  45.   /*************************************************************************/
  46.   /*************************************************************************/
  47.   /****                                                                 ****/
  48.   /****                                                                 ****/
  49.   /****                          F A C E S                              ****/
  50.   /****                                                                 ****/
  51.   /****                                                                 ****/
  52.   /*************************************************************************/
  53.   /*************************************************************************/
  54.   /*************************************************************************/
  55. #undef  PAIR_TAG
  56. #define PAIR_TAG( left, right )  ( ( (FT_ULong)left << 16 ) | 
  57.                                      (FT_ULong)right        )
  58.   /*************************************************************************/
  59.   /*                                                                       */
  60.   /* <Function>                                                            */
  61.   /*    tt_get_kerning                                                     */
  62.   /*                                                                       */
  63.   /* <Description>                                                         */
  64.   /*    A driver method used to return the kerning vector between two      */
  65.   /*    glyphs of the same face.                                           */
  66.   /*                                                                       */
  67.   /* <Input>                                                               */
  68.   /*    face        :: A handle to the source face object.                 */
  69.   /*                                                                       */
  70.   /*    left_glyph  :: The index of the left glyph in the kern pair.       */
  71.   /*                                                                       */
  72.   /*    right_glyph :: The index of the right glyph in the kern pair.      */
  73.   /*                                                                       */
  74.   /* <Output>                                                              */
  75.   /*    kerning     :: The kerning vector.  This is in font units for      */
  76.   /*                   scalable formats, and in pixels for fixed-sizes     */
  77.   /*                   formats.                                            */
  78.   /*                                                                       */
  79.   /* <Return>                                                              */
  80.   /*    FreeType error code.  0 means success.                             */
  81.   /*                                                                       */
  82.   /* <Note>                                                                */
  83.   /*    Only horizontal layouts (left-to-right & right-to-left) are        */
  84.   /*    supported by this function.  Other layouts, or more sophisticated  */
  85.   /*    kernings, are out of scope of this method (the basic driver        */
  86.   /*    interface is meant to be simple).                                  */
  87.   /*                                                                       */
  88.   /*    They can be implemented by format-specific interfaces.             */
  89.   /*                                                                       */
  90.   static FT_Error
  91.   tt_get_kerning( FT_Face     ttface,          /* TT_Face */
  92.                   FT_UInt     left_glyph,
  93.                   FT_UInt     right_glyph,
  94.                   FT_Vector*  kerning )
  95.   {
  96.     TT_Face       face = (TT_Face)ttface;
  97.     SFNT_Service  sfnt = (SFNT_Service)face->sfnt;
  98.     kerning->x = 0;
  99.     kerning->y = 0;
  100.     if ( sfnt )
  101.       kerning->x = sfnt->get_kerning( face, left_glyph, right_glyph );
  102.     return 0;
  103.   }
  104. #undef PAIR_TAG
  105.   /*************************************************************************/
  106.   /*************************************************************************/
  107.   /*************************************************************************/
  108.   /****                                                                 ****/
  109.   /****                                                                 ****/
  110.   /****                           S I Z E S                             ****/
  111.   /****                                                                 ****/
  112.   /****                                                                 ****/
  113.   /*************************************************************************/
  114.   /*************************************************************************/
  115.   /*************************************************************************/
  116. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  117.   static FT_Error
  118.   tt_size_select( FT_Size   size,
  119.                   FT_ULong  strike_index )
  120.   {
  121.     TT_Face   ttface = (TT_Face)size->face;
  122.     TT_Size   ttsize = (TT_Size)size;
  123.     FT_Error  error  = TT_Err_Ok;
  124.     ttsize->strike_index = strike_index;
  125.     if ( FT_IS_SCALABLE( size->face ) )
  126.     {
  127.       /* use the scaled metrics, even when tt_size_reset fails */
  128.       FT_Select_Metrics( size->face, strike_index );
  129.       tt_size_reset( ttsize );
  130.     }
  131.     else
  132.     {
  133.       SFNT_Service      sfnt    = (SFNT_Service) ttface->sfnt;
  134.       FT_Size_Metrics*  metrics = &size->metrics;
  135.       error = sfnt->load_strike_metrics( ttface, strike_index, metrics );
  136.       if ( error )
  137.         ttsize->strike_index = 0xFFFFFFFFUL;
  138.     }
  139.     return error;
  140.   }
  141. #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
  142.   static FT_Error
  143.   tt_size_request( FT_Size          size,
  144.                    FT_Size_Request  req )
  145.   {
  146.     TT_Size   ttsize = (TT_Size)size;
  147.     FT_Error  error  = TT_Err_Ok;
  148. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  149.     if ( FT_HAS_FIXED_SIZES( size->face ) )
  150.     {
  151.       TT_Face       ttface = (TT_Face)size->face;
  152.       SFNT_Service  sfnt   = (SFNT_Service) ttface->sfnt;
  153.       FT_ULong      strike_index;
  154.       error = sfnt->set_sbit_strike( ttface, req, &strike_index );
  155.       if ( error )
  156.         ttsize->strike_index = 0xFFFFFFFFUL;
  157.       else
  158.         return tt_size_select( size, strike_index );
  159.     }
  160. #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
  161.     FT_Request_Metrics( size->face, req );
  162.     if ( FT_IS_SCALABLE( size->face ) )
  163.       error = tt_size_reset( ttsize );
  164.     return error;
  165.   }
  166.   /*************************************************************************/
  167.   /*                                                                       */
  168.   /* <Function>                                                            */
  169.   /*    Load_Glyph                                                         */
  170.   /*                                                                       */
  171.   /* <Description>                                                         */
  172.   /*    A driver method used to load a glyph within a given glyph slot.    */
  173.   /*                                                                       */
  174.   /* <Input>                                                               */
  175.   /*    slot        :: A handle to the target slot object where the glyph  */
  176.   /*                   will be loaded.                                     */
  177.   /*                                                                       */
  178.   /*    size        :: A handle to the source face size at which the glyph */
  179.   /*                   must be scaled, loaded, etc.                        */
  180.   /*                                                                       */
  181.   /*    glyph_index :: The index of the glyph in the font file.            */
  182.   /*                                                                       */
  183.   /*    load_flags  :: A flag indicating what to load for this glyph.  The */
  184.   /*                   FTLOAD_??? constants can be used to control the     */
  185.   /*                   glyph loading process (e.g., whether the outline    */
  186.   /*                   should be scaled, whether to load bitmaps or not,   */
  187.   /*                   whether to hint the outline, etc).                  */
  188.   /*                                                                       */
  189.   /* <Return>                                                              */
  190.   /*    FreeType error code.  0 means success.                             */
  191.   /*                                                                       */
  192.   static FT_Error
  193.   Load_Glyph( FT_GlyphSlot  ttslot,         /* TT_GlyphSlot */
  194.               FT_Size       ttsize,         /* TT_Size      */
  195.               FT_UInt       glyph_index,
  196.               FT_Int32      load_flags )
  197.   {
  198.     TT_GlyphSlot  slot = (TT_GlyphSlot)ttslot;
  199.     TT_Size       size = (TT_Size)ttsize;
  200.     FT_Face       face = ttslot->face;
  201.     FT_Error      error;
  202.     if ( !slot )
  203.       return TT_Err_Invalid_Slot_Handle;
  204.     if ( !size )
  205.       return TT_Err_Invalid_Size_Handle;
  206.     if ( !face || glyph_index >= (FT_UInt)face->num_glyphs )
  207.       return TT_Err_Invalid_Argument;
  208.     if ( load_flags & ( FT_LOAD_NO_RECURSE | FT_LOAD_NO_SCALE ) )
  209.     {
  210.       load_flags |= FT_LOAD_NO_HINTING |
  211.                     FT_LOAD_NO_BITMAP  |
  212.                     FT_LOAD_NO_SCALE;
  213.     }
  214.     /* now load the glyph outline if necessary */
  215.     error = TT_Load_Glyph( size, slot, glyph_index, load_flags );
  216.     /* force drop-out mode to 2 - irrelevant now */
  217.     /* slot->outline.dropout_mode = 2; */
  218.     return error;
  219.   }
  220.   /*************************************************************************/
  221.   /*************************************************************************/
  222.   /*************************************************************************/
  223.   /****                                                                 ****/
  224.   /****                                                                 ****/
  225.   /****                D R I V E R  I N T E R F A C E                   ****/
  226.   /****                                                                 ****/
  227.   /****                                                                 ****/
  228.   /*************************************************************************/
  229.   /*************************************************************************/
  230.   /*************************************************************************/
  231. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  232.   static const FT_Service_MultiMastersRec  tt_service_gx_multi_masters =
  233.   {
  234.     (FT_Get_MM_Func)        NULL,
  235.     (FT_Set_MM_Design_Func) NULL,
  236.     (FT_Set_MM_Blend_Func)  TT_Set_MM_Blend,
  237.     (FT_Get_MM_Var_Func)    TT_Get_MM_Var,
  238.     (FT_Set_Var_Design_Func)TT_Set_Var_Design
  239.   };
  240. #endif
  241.   static const FT_Service_TrueTypeEngineRec  tt_service_truetype_engine =
  242.   {
  243. #ifdef TT_USE_BYTECODE_INTERPRETER
  244. #ifdef TT_CONFIG_OPTION_UNPATENTED_HINTING
  245.     FT_TRUETYPE_ENGINE_TYPE_UNPATENTED
  246. #else
  247.     FT_TRUETYPE_ENGINE_TYPE_PATENTED
  248. #endif
  249. #else /* !TT_USE_BYTECODE_INTERPRETER */
  250.     FT_TRUETYPE_ENGINE_TYPE_NONE
  251. #endif /* TT_USE_BYTECODE_INTERPRETER */
  252.   };
  253.   static const FT_Service_TTGlyfRec  tt_service_truetype_glyf =
  254.   {
  255.     (TT_Glyf_GetLocationFunc)tt_face_get_location
  256.   };
  257.   static const FT_ServiceDescRec  tt_services[] =
  258.   {
  259.     { FT_SERVICE_ID_XF86_NAME,       FT_XF86_FORMAT_TRUETYPE },
  260. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  261.     { FT_SERVICE_ID_MULTI_MASTERS,   &tt_service_gx_multi_masters },
  262. #endif
  263.     { FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine },
  264.     { FT_SERVICE_ID_TT_GLYF,         &tt_service_truetype_glyf },
  265.     { NULL, NULL }
  266.   };
  267.   FT_CALLBACK_DEF( FT_Module_Interface )
  268.   tt_get_interface( FT_Module    driver,    /* TT_Driver */
  269.                     const char*  tt_interface )
  270.   {
  271.     FT_Module_Interface  result;
  272.     FT_Module            sfntd;
  273.     SFNT_Service         sfnt;
  274.     result = ft_service_list_lookup( tt_services, tt_interface );
  275.     if ( result != NULL )
  276.       return result;
  277.     /* only return the default interface from the SFNT module */
  278.     sfntd = FT_Get_Module( driver->library, "sfnt" );
  279.     if ( sfntd )
  280.     {
  281.       sfnt = (SFNT_Service)( sfntd->clazz->module_interface );
  282.       if ( sfnt )
  283.         return sfnt->get_interface( driver, tt_interface );
  284.     }
  285.     return 0;
  286.   }
  287.   /* The FT_DriverInterface structure is defined in ftdriver.h. */
  288.   FT_CALLBACK_TABLE_DEF
  289.   const FT_Driver_ClassRec  tt_driver_class =
  290.   {
  291.     {
  292.       FT_MODULE_FONT_DRIVER        |
  293.       FT_MODULE_DRIVER_SCALABLE    |
  294. #ifdef TT_USE_BYTECODE_INTERPRETER
  295.       FT_MODULE_DRIVER_HAS_HINTER,
  296. #else
  297.       0,
  298. #endif
  299.       sizeof ( TT_DriverRec ),
  300.       "truetype",      /* driver name                           */
  301.       0x10000L,        /* driver version == 1.0                 */
  302.       0x20000L,        /* driver requires FreeType 2.0 or above */
  303.       (void*)0,        /* driver specific interface */
  304.       tt_driver_init,
  305.       tt_driver_done,
  306.       tt_get_interface,
  307.     },
  308.     sizeof ( TT_FaceRec ),
  309.     sizeof ( TT_SizeRec ),
  310.     sizeof ( FT_GlyphSlotRec ),
  311.     tt_face_init,
  312.     tt_face_done,
  313.     tt_size_init,
  314.     tt_size_done,
  315.     tt_slot_init,
  316.     0,                      /* FT_Slot_DoneFunc */
  317. #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
  318.     ft_stub_set_char_sizes,
  319.     ft_stub_set_pixel_sizes,
  320. #endif
  321.     Load_Glyph,
  322.     tt_get_kerning,
  323.     0,                      /* FT_Face_AttachFunc      */
  324.     0,                      /* FT_Face_GetAdvancesFunc */
  325.     tt_size_request,
  326. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  327.     tt_size_select
  328. #else
  329.     0                       /* FT_Size_SelectFunc      */
  330. #endif
  331.   };
  332. /* END */