1 /***************************************************************************/ 
   5 /*    TrueType font driver implementation (body).                          */ 
   7 /*  Copyright 1996-2000 by                                                 */ 
   8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */ 
  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.                                        */ 
  16 /***************************************************************************/ 
  19 #include <freetype/internal/ftdebug.h> 
  20 #include <freetype/internal/ftstream.h> 
  21 #include <freetype/internal/sfnt.h> 
  22 #include <freetype/ttnameid.h> 
  25 #ifdef FT_FLAT_COMPILE 
  32 #include <truetype/ttdriver.h> 
  33 #include <truetype/ttgload.h> 
  38   /*************************************************************************/ 
  40   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */ 
  41   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */ 
  42   /* messages during execution.                                            */ 
  45 #define FT_COMPONENT  trace_ttdriver 
  48   /*************************************************************************/ 
  49   /*************************************************************************/ 
  50   /*************************************************************************/ 
  56   /*************************************************************************/ 
  57   /*************************************************************************/ 
  58   /*************************************************************************/ 
  62 #define PAIR_TAG( left, right )  ( ( (FT_ULong)left << 16 ) | \ 
  66   /*************************************************************************/ 
  72   /*    A driver method used to return the kerning vector between two      */ 
  73   /*    glyphs of the same face.                                           */ 
  76   /*    face        :: A handle to the source face object.                 */ 
  78   /*    left_glyph  :: The index of the left glyph in the kern pair.       */ 
  80   /*    right_glyph :: The index of the right glyph in the kern pair.      */ 
  83   /*    kerning     :: The kerning vector.  This is in font units for      */ 
  84   /*                   scalable formats, and in pixels for fixed-sizes     */ 
  88   /*    FreeType error code.  0 means success.                             */ 
  91   /*    Only horizontal layouts (left-to-right & right-to-left) are        */ 
  92   /*    supported by this function.  Other layouts, or more sophisticated  */ 
  93   /*    kernings, are out of scope of this method (the basic driver        */ 
  94   /*    interface is meant to be simple).                                  */ 
  96   /*    They can be implemented by format-specific interfaces.             */ 
  99   FT_Error  
Get_Kerning( TT_Face     face
, 
 104     TT_Kern_0_Pair
*  pair
; 
 108       return TT_Err_Invalid_Face_Handle
; 
 113     if ( face
->kern_pairs 
) 
 115       /* there are some kerning pairs in this font file! */ 
 116       FT_ULong  search_tag 
= PAIR_TAG( left_glyph
, right_glyph 
); 
 121       right 
= face
->num_kern_pairs 
- 1; 
 123       while ( left 
<= right 
) 
 125         FT_Int    middle 
= left 
+ ( ( right 
- left 
) >> 1 ); 
 129         pair     
= face
->kern_pairs 
+ middle
; 
 130         cur_pair 
= PAIR_TAG( pair
->left
, pair
->right 
); 
 132         if ( cur_pair 
== search_tag 
) 
 135         if ( cur_pair 
< search_tag 
) 
 146     kerning
->x 
= pair
->value
; 
 154   /*************************************************************************/ 
 155   /*************************************************************************/ 
 156   /*************************************************************************/ 
 159   /****                           S I Z E S                             ****/ 
 162   /*************************************************************************/ 
 163   /*************************************************************************/ 
 164   /*************************************************************************/ 
 167   /*************************************************************************/ 
 173   /*    A driver method used to reset a size's character sizes (horizontal */ 
 174   /*    and vertical) expressed in fractional points.                      */ 
 177   /*    char_width      :: The character width expressed in 26.6           */ 
 178   /*                       fractional points.                              */ 
 180   /*    char_height     :: The character height expressed in 26.6          */ 
 181   /*                       fractional points.                              */ 
 183   /*    horz_resolution :: The horizontal resolution of the output device. */ 
 185   /*    vert_resolution :: The vertical resolution of the output device.   */ 
 188   /*    size            :: A handle to the target size object.             */ 
 191   /*    FreeType error code.  0 means success.                             */ 
 194   FT_Error  
Set_Char_Sizes( TT_Size     size
, 
 195                             FT_F26Dot6  char_width
, 
 196                             FT_F26Dot6  char_height
, 
 197                             FT_UInt     horz_resolution
, 
 198                             FT_UInt     vert_resolution 
) 
 200     FT_Size_Metrics
*  metrics 
= &size
->root
.metrics
; 
 201     TT_Face           face    
= (TT_Face
)size
->root
.face
; 
 202     FT_Long           dim_x
, dim_y
; 
 205     /* This bit flag, when set, indicates that the pixel size must be */ 
 206     /* truncated to an integer.  Nearly all TrueType fonts have this  */ 
 207     /* bit set, as hinting won't work really well otherwise.          */ 
 209     /* However, for those rare fonts who do not set it, we override   */ 
 210     /* the default computations performed by the base layer.  I       */ 
 211     /* really don't know whether this is useful, but hey, that's the  */ 
 214     if ( ( face
->header
.Flags 
& 8 ) == 0 ) 
 216       /* Compute pixel sizes in 26.6 units */ 
 217       dim_x 
= ( char_width  
* horz_resolution 
) / 72; 
 218       dim_y 
= ( char_height 
* vert_resolution 
) / 72; 
 220       metrics
->x_scale 
= FT_DivFix( dim_x
, face
->root
.units_per_EM 
); 
 221       metrics
->y_scale 
= FT_DivFix( dim_y
, face
->root
.units_per_EM 
); 
 223       metrics
->x_ppem  
= (FT_UShort
)( dim_x 
>> 6 ); 
 224       metrics
->y_ppem  
= (FT_UShort
)( dim_y 
>> 6 ); 
 227     size
->ttmetrics
.valid 
= FALSE
; 
 229     return TT_Reset_Size( size 
); 
 233   /*************************************************************************/ 
 236   /*    Set_Pixel_Sizes                                                    */ 
 239   /*    A driver method used to reset a size's character sizes (horizontal */ 
 240   /*    and vertical) expressed in integer pixels.                         */ 
 243   /*    pixel_width  :: The character width expressed in integer pixels.   */ 
 245   /*    pixel_height :: The character height expressed in integer pixels.  */ 
 248   /*    size         :: A handle to the target size object.                */ 
 251   /*    FreeType error code.  0 means success.                             */ 
 254   FT_Error  
Set_Pixel_Sizes( TT_Size  size
, 
 256                              FT_UInt  pixel_height 
) 
 258     FT_UNUSED( pixel_width 
); 
 259     FT_UNUSED( pixel_height 
); 
 261     /* many things have been pre-computed by the base layer */ 
 263     size
->ttmetrics
.valid 
= FALSE
; 
 265     return TT_Reset_Size( size 
); 
 269   /*************************************************************************/ 
 275   /*    A driver method used to load a glyph within a given glyph slot.    */ 
 278   /*    slot        :: A handle to the target slot object where the glyph  */ 
 279   /*                   will be loaded.                                     */ 
 281   /*    size        :: A handle to the source face size at which the glyph */ 
 282   /*                   must be scaled, loaded, etc.                        */ 
 284   /*    glyph_index :: The index of the glyph in the font file.            */ 
 286   /*    load_flags  :: A flag indicating what to load for this glyph.  The */ 
 287   /*                   FTLOAD_??? constants can be used to control the     */ 
 288   /*                   glyph loading process (e.g., whether the outline    */ 
 289   /*                   should be scaled, whether to load bitmaps or not,   */ 
 290   /*                   whether to hint the outline, etc).                  */ 
 293   /*    FreeType error code.  0 means success.                             */ 
 296   FT_Error  
Load_Glyph( TT_GlyphSlot  slot
, 
 298                         FT_UShort     glyph_index
, 
 305       return TT_Err_Invalid_Glyph_Handle
; 
 307     /* check whether we want a scaled outline or bitmap */ 
 309       load_flags 
|= FT_LOAD_NO_SCALE 
| FT_LOAD_NO_HINTING
; 
 311     if ( load_flags 
& FT_LOAD_NO_SCALE 
) 
 314     /* reset the size object if necessary */ 
 317       /* these two object must have the same parent */ 
 318       if ( size
->root
.face 
!= slot
->face 
) 
 319         return TT_Err_Invalid_Face_Handle
; 
 321       if ( !size
->ttmetrics
.valid 
) 
 323         if ( FT_SET_ERROR( TT_Reset_Size( size 
) ) ) 
 328     /* now load the glyph outline if necessary */ 
 329     error 
= TT_Load_Glyph( size
, slot
, glyph_index
, load_flags 
); 
 331     /* force drop-out mode to 2 - irrelevant now */ 
 332     /* slot->outline.dropout_mode = 2; */ 
 338   /*************************************************************************/ 
 339   /*************************************************************************/ 
 340   /*************************************************************************/ 
 343   /****             C H A R A C T E R   M A P P I N G S                 ****/ 
 346   /*************************************************************************/ 
 347   /*************************************************************************/ 
 348   /*************************************************************************/ 
 350   /*************************************************************************/ 
 356   /*    Uses a charmap to return a given character code's glyph index.     */ 
 359   /*    charmap  :: A handle to the source charmap object.                 */ 
 360   /*    charcode :: The character code.                                    */ 
 363   /*    Glyph index.  0 means `undefined character code'.                  */ 
 366   FT_UInt  
Get_Char_Index( TT_CharMap  charmap
, 
 374     cmap 
= &charmap
->cmap
; 
 375     face 
= (TT_Face
)charmap
->root
.face
; 
 377     /* Load table if needed */ 
 380       SFNT_Interface
*  sfnt 
= (SFNT_Interface
*)face
->sfnt
; 
 383       error 
= sfnt
->load_charmap( face
, cmap
, face
->root
.stream 
); 
 390     if ( cmap
->get_index 
) 
 391       return cmap
->get_index( cmap
, charcode 
); 
 397   /*************************************************************************/ 
 398   /*************************************************************************/ 
 399   /*************************************************************************/ 
 402   /****                D R I V E R  I N T E R F A C E                   ****/ 
 405   /*************************************************************************/ 
 406   /*************************************************************************/ 
 407   /*************************************************************************/ 
 411   FT_Module_Interface  
tt_get_interface( TT_Driver    driver
, 
 412                                          const char*  interface 
) 
 414     FT_Module        sfntd 
= FT_Get_Module( driver
->root
.root
.library
, 
 416     SFNT_Interface
*  sfnt
; 
 419     /* only return the default interface from the SFNT module */ 
 422       sfnt 
= (SFNT_Interface
*)( sfntd
->clazz
->module_interface 
); 
 424         return sfnt
->get_interface( FT_MODULE( driver 
), interface 
); 
 431   /* The FT_DriverInterface structure is defined in ftdriver.h. */ 
 433   const FT_Driver_Class  tt_driver_class 
= 
 436       ft_module_font_driver     
| 
 437       ft_module_driver_scalable 
| 
 438 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 
 439       ft_module_driver_has_hinter
, 
 444       sizeof ( TT_DriverRec 
), 
 446       "truetype",      /* driver name                           */ 
 447       0x10000L
,        /* driver version == 1.0                 */ 
 448       0x20000L
,        /* driver requires FreeType 2.0 or above */ 
 450       (void*)0,        /* driver specific interface */ 
 452       (FT_Module_Constructor
)TT_Init_Driver
, 
 453       (FT_Module_Destructor
) TT_Done_Driver
, 
 454       (FT_Module_Requester
)  tt_get_interface
, 
 457     sizeof ( TT_FaceRec 
), 
 458     sizeof ( TT_SizeRec 
), 
 459     sizeof ( FT_GlyphSlotRec 
), 
 462     (FTDriver_initFace
)     TT_Init_Face
, 
 463     (FTDriver_doneFace
)     TT_Done_Face
, 
 464     (FTDriver_initSize
)     TT_Init_Size
, 
 465     (FTDriver_doneSize
)     TT_Done_Size
, 
 466     (FTDriver_initGlyphSlot
)0, 
 467     (FTDriver_doneGlyphSlot
)0, 
 469     (FTDriver_setCharSizes
) Set_Char_Sizes
, 
 470     (FTDriver_setPixelSizes
)Set_Pixel_Sizes
, 
 471     (FTDriver_loadGlyph
)    Load_Glyph
, 
 472     (FTDriver_getCharIndex
) Get_Char_Index
, 
 474     (FTDriver_getKerning
)   Get_Kerning
, 
 475     (FTDriver_attachFile
)   0, 
 476     (FTDriver_getAdvances
)  0 
 480 #ifdef FT_CONFIG_OPTION_DYNAMIC_DRIVERS 
 483   /*************************************************************************/ 
 486   /*    getDriverInterface                                                 */ 
 489   /*    This function is used when compiling the TrueType driver as a      */ 
 490   /*    shared library (`.DLL' or `.so').  It will be used by the          */ 
 491   /*    high-level library of FreeType to retrieve the address of the      */ 
 492   /*    driver's generic interface.                                        */ 
 494   /*    It shouldn't be implemented in a static build, as each driver must */ 
 495   /*    have the same function as an exported entry point.                 */ 
 498   /*    The address of the TrueType's driver generic interface.  The       */ 
 499   /*    format-specific interface can then be retrieved through the method */ 
 500   /*    interface->get_format_interface.                                   */ 
 502   EXPORT_FUNC( const FT_Driver_Class
* )  getDriverClass( void ) 
 504     return &tt_driver_class
; 
 508 #endif /* CONFIG_OPTION_DYNAMIC_DRIVERS */