Home | History | Annotate | Download | only in autofit
      1 /***************************************************************************/
      2 /*                                                                         */
      3 /*  aftypes.h                                                              */
      4 /*                                                                         */
      5 /*    Auto-fitter types (specification only).                              */
      6 /*                                                                         */
      7 /*  Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 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   /*************************************************************************
     20    *
     21    *  The auto-fitter is a complete rewrite of the old auto-hinter.
     22    *  Its main feature is the ability to differentiate between different
     23    *  scripts in order to apply language-specific rules.
     24    *
     25    *  The code has also been compartmentized into several entities that
     26    *  should make algorithmic experimentation easier than with the old
     27    *  code.
     28    *
     29    *  Finally, we get rid of the Catharon license, since this code is
     30    *  released under the FreeType one.
     31    *
     32    *************************************************************************/
     33 
     34 
     35 #ifndef __AFTYPES_H__
     36 #define __AFTYPES_H__
     37 
     38 #include <ft2build.h>
     39 
     40 #include FT_FREETYPE_H
     41 #include FT_OUTLINE_H
     42 #include FT_INTERNAL_OBJECTS_H
     43 #include FT_INTERNAL_DEBUG_H
     44 
     45 
     46 FT_BEGIN_HEADER
     47 
     48   /*************************************************************************/
     49   /*************************************************************************/
     50   /*****                                                               *****/
     51   /*****                    D E B U G G I N G                          *****/
     52   /*****                                                               *****/
     53   /*************************************************************************/
     54   /*************************************************************************/
     55 
     56 #define xxAF_USE_WARPER  /* only define to use warp hinting */
     57 #define xxAF_DEBUG
     58 
     59 #ifdef AF_DEBUG
     60 
     61 #include FT_CONFIG_STANDARD_LIBRARY_H
     62 
     63 #define AF_LOG( x )  do { if ( _af_debug ) printf x; } while ( 0 )
     64 
     65 extern int    _af_debug;
     66 extern int    _af_debug_disable_horz_hints;
     67 extern int    _af_debug_disable_vert_hints;
     68 extern int    _af_debug_disable_blue_hints;
     69 extern void*  _af_debug_hints;
     70 
     71 #else /* !AF_DEBUG */
     72 
     73 #define AF_LOG( x )  do { } while ( 0 )        /* nothing */
     74 
     75 #endif /* !AF_DEBUG */
     76 
     77 
     78   /*************************************************************************/
     79   /*************************************************************************/
     80   /*****                                                               *****/
     81   /*****                 U T I L I T Y   S T U F F                     *****/
     82   /*****                                                               *****/
     83   /*************************************************************************/
     84   /*************************************************************************/
     85 
     86   typedef struct  AF_WidthRec_
     87   {
     88     FT_Pos  org;  /* original position/width in font units              */
     89     FT_Pos  cur;  /* current/scaled position/width in device sub-pixels */
     90     FT_Pos  fit;  /* current/fitted position/width in device sub-pixels */
     91 
     92   } AF_WidthRec, *AF_Width;
     93 
     94 
     95   FT_LOCAL( void )
     96   af_sort_pos( FT_UInt  count,
     97                FT_Pos*  table );
     98 
     99   FT_LOCAL( void )
    100   af_sort_widths( FT_UInt   count,
    101                   AF_Width  widths );
    102 
    103 
    104   /*************************************************************************/
    105   /*************************************************************************/
    106   /*****                                                               *****/
    107   /*****                   A N G L E   T Y P E S                       *****/
    108   /*****                                                               *****/
    109   /*************************************************************************/
    110   /*************************************************************************/
    111 
    112   /*
    113    *  The auto-fitter doesn't need a very high angular accuracy;
    114    *  this allows us to speed up some computations considerably with a
    115    *  light Cordic algorithm (see afangles.c).
    116    */
    117 
    118   typedef FT_Int  AF_Angle;
    119 
    120 
    121 #define AF_ANGLE_PI   256
    122 #define AF_ANGLE_2PI  ( AF_ANGLE_PI * 2 )
    123 #define AF_ANGLE_PI2  ( AF_ANGLE_PI / 2 )
    124 #define AF_ANGLE_PI4  ( AF_ANGLE_PI / 4 )
    125 
    126 
    127 #if 0
    128   /*
    129    *  compute the angle of a given 2-D vector
    130    */
    131   FT_LOCAL( AF_Angle )
    132   af_angle_atan( FT_Pos  dx,
    133                  FT_Pos  dy );
    134 
    135 
    136   /*
    137    *  compute `angle2 - angle1'; the result is always within
    138    *  the range [-AF_ANGLE_PI .. AF_ANGLE_PI - 1]
    139    */
    140   FT_LOCAL( AF_Angle )
    141   af_angle_diff( AF_Angle  angle1,
    142                  AF_Angle  angle2 );
    143 #endif /* 0 */
    144 
    145 
    146 #define AF_ANGLE_DIFF( result, angle1, angle2 ) \
    147   FT_BEGIN_STMNT                                \
    148     AF_Angle  _delta = (angle2) - (angle1);     \
    149                                                 \
    150                                                 \
    151     _delta %= AF_ANGLE_2PI;                     \
    152     if ( _delta < 0 )                           \
    153       _delta += AF_ANGLE_2PI;                   \
    154                                                 \
    155     if ( _delta > AF_ANGLE_PI )                 \
    156       _delta -= AF_ANGLE_2PI;                   \
    157                                                 \
    158     result = _delta;                            \
    159   FT_END_STMNT
    160 
    161 
    162   /*************************************************************************/
    163   /*************************************************************************/
    164   /*****                                                               *****/
    165   /*****                    O U T L I N E S                            *****/
    166   /*****                                                               *****/
    167   /*************************************************************************/
    168   /*************************************************************************/
    169 
    170   /*  opaque handle to glyph-specific hints -- see `afhints.h' for more
    171    *  details
    172    */
    173   typedef struct AF_GlyphHintsRec_*  AF_GlyphHints;
    174 
    175   /*  This structure is used to model an input glyph outline to
    176    *  the auto-hinter.  The latter will set the `hints' field
    177    *  depending on the glyph's script.
    178    */
    179   typedef struct  AF_OutlineRec_
    180   {
    181     FT_Face        face;
    182     FT_Outline     outline;
    183     FT_UInt        outline_resolution;
    184 
    185     FT_Int         advance;
    186     FT_UInt        metrics_resolution;
    187 
    188     AF_GlyphHints  hints;
    189 
    190   } AF_OutlineRec;
    191 
    192 
    193   /*************************************************************************/
    194   /*************************************************************************/
    195   /*****                                                               *****/
    196   /*****                       S C A L E R S                           *****/
    197   /*****                                                               *****/
    198   /*************************************************************************/
    199   /*************************************************************************/
    200 
    201   /*
    202    *  A scaler models the target pixel device that will receive the
    203    *  auto-hinted glyph image.
    204    */
    205 
    206   typedef enum  AF_ScalerFlags_
    207   {
    208     AF_SCALER_FLAG_NO_HORIZONTAL = 1,  /* disable horizontal hinting */
    209     AF_SCALER_FLAG_NO_VERTICAL   = 2,  /* disable vertical hinting   */
    210     AF_SCALER_FLAG_NO_ADVANCE    = 4   /* disable advance hinting    */
    211 
    212   } AF_ScalerFlags;
    213 
    214 
    215   typedef struct  AF_ScalerRec_
    216   {
    217     FT_Face         face;        /* source font face                        */
    218     FT_Fixed        x_scale;     /* from font units to 1/64th device pixels */
    219     FT_Fixed        y_scale;     /* from font units to 1/64th device pixels */
    220     FT_Pos          x_delta;     /* in 1/64th device pixels                 */
    221     FT_Pos          y_delta;     /* in 1/64th device pixels                 */
    222     FT_Render_Mode  render_mode; /* monochrome, anti-aliased, LCD, etc.     */
    223     FT_UInt32       flags;       /* additional control flags, see above     */
    224 
    225   } AF_ScalerRec, *AF_Scaler;
    226 
    227 
    228 #define AF_SCALER_EQUAL_SCALES( a, b )      \
    229           ( (a)->x_scale == (b)->x_scale && \
    230             (a)->y_scale == (b)->y_scale && \
    231             (a)->x_delta == (b)->x_delta && \
    232             (a)->y_delta == (b)->y_delta )
    233 
    234 
    235   /*************************************************************************/
    236   /*************************************************************************/
    237   /*****                                                               *****/
    238   /*****                       S C R I P T S                           *****/
    239   /*****                                                               *****/
    240   /*************************************************************************/
    241   /*************************************************************************/
    242 
    243   /*
    244    *  The list of know scripts.  Each different script corresponds to the
    245    *  following information:
    246    *
    247    *   - A set of Unicode ranges to test whether the face supports the
    248    *     script.
    249    *
    250    *   - A specific global analyzer that will compute global metrics
    251    *     specific to the script.
    252    *
    253    *   - A specific glyph analyzer that will compute segments and
    254    *     edges for each glyph covered by the script.
    255    *
    256    *   - A specific grid-fitting algorithm that will distort the
    257    *     scaled glyph outline according to the results of the glyph
    258    *     analyzer.
    259    *
    260    *  Note that a given analyzer and/or grid-fitting algorithm can be
    261    *  used by more than one script.
    262    */
    263 
    264   typedef enum  AF_Script_
    265   {
    266     AF_SCRIPT_NONE  = 0,
    267     AF_SCRIPT_LATIN = 1,
    268     AF_SCRIPT_CJK   = 2,
    269     AF_SCRIPT_INDIC = 3,
    270 #ifdef FT_OPTION_AUTOFIT2
    271     AF_SCRIPT_LATIN2,
    272 #endif
    273 
    274     /* add new scripts here.  Don't forget to update the list in */
    275     /* `afglobal.c'.                                             */
    276 
    277     AF_SCRIPT_MAX   /* do not remove */
    278 
    279   } AF_Script;
    280 
    281 
    282   typedef struct AF_ScriptClassRec_ const*  AF_ScriptClass;
    283 
    284   typedef struct  AF_ScriptMetricsRec_
    285   {
    286     AF_ScriptClass  clazz;
    287     AF_ScalerRec    scaler;
    288     FT_Bool         digits_have_same_width;
    289 
    290   } AF_ScriptMetricsRec, *AF_ScriptMetrics;
    291 
    292 
    293   /*  This function parses an FT_Face to compute global metrics for
    294    *  a specific script.
    295    */
    296   typedef FT_Error
    297   (*AF_Script_InitMetricsFunc)( AF_ScriptMetrics  metrics,
    298                                 FT_Face           face );
    299 
    300   typedef void
    301   (*AF_Script_ScaleMetricsFunc)( AF_ScriptMetrics  metrics,
    302                                  AF_Scaler         scaler );
    303 
    304   typedef void
    305   (*AF_Script_DoneMetricsFunc)( AF_ScriptMetrics  metrics );
    306 
    307 
    308   typedef FT_Error
    309   (*AF_Script_InitHintsFunc)( AF_GlyphHints     hints,
    310                               AF_ScriptMetrics  metrics );
    311 
    312   typedef void
    313   (*AF_Script_ApplyHintsFunc)( AF_GlyphHints     hints,
    314                                FT_Outline*       outline,
    315                                AF_ScriptMetrics  metrics );
    316 
    317 
    318   typedef struct  AF_Script_UniRangeRec_
    319   {
    320     FT_UInt32  first;
    321     FT_UInt32  last;
    322 
    323   } AF_Script_UniRangeRec;
    324 
    325 #define AF_UNIRANGE_REC( a, b ) { (FT_UInt32)(a), (FT_UInt32)(b) }
    326 
    327   typedef const AF_Script_UniRangeRec  *AF_Script_UniRange;
    328 
    329 
    330   typedef struct  AF_ScriptClassRec_
    331   {
    332     AF_Script                   script;
    333     AF_Script_UniRange          script_uni_ranges; /* last must be { 0, 0 } */
    334 
    335     FT_Offset                   script_metrics_size;
    336     AF_Script_InitMetricsFunc   script_metrics_init;
    337     AF_Script_ScaleMetricsFunc  script_metrics_scale;
    338     AF_Script_DoneMetricsFunc   script_metrics_done;
    339 
    340     AF_Script_InitHintsFunc     script_hints_init;
    341     AF_Script_ApplyHintsFunc    script_hints_apply;
    342 
    343   } AF_ScriptClassRec;
    344 
    345 /* Declare and define vtables for classes */
    346 #ifndef FT_CONFIG_OPTION_PIC
    347 
    348 #define AF_DECLARE_SCRIPT_CLASS(script_class)                                \
    349   FT_CALLBACK_TABLE const AF_ScriptClassRec                                  \
    350   script_class;
    351 
    352 #define AF_DEFINE_SCRIPT_CLASS(script_class, script_, ranges, m_size,        \
    353                                m_init, m_scale, m_done, h_init, h_apply)     \
    354   FT_CALLBACK_TABLE_DEF const AF_ScriptClassRec                              \
    355   script_class =                                                             \
    356   {                                                                          \
    357     script_,                                                                 \
    358     ranges,                                                                  \
    359                                                                              \
    360     m_size,                                                                  \
    361                                                                              \
    362     m_init,                                                                  \
    363     m_scale,                                                                 \
    364     m_done,                                                                  \
    365                                                                              \
    366     h_init,                                                                  \
    367     h_apply                                                                  \
    368   };
    369 
    370 #else
    371 
    372 #define AF_DECLARE_SCRIPT_CLASS(script_class)                                \
    373   FT_LOCAL(void)                                                             \
    374   FT_Init_Class_##script_class(AF_ScriptClassRec* ac);
    375 
    376 #define AF_DEFINE_SCRIPT_CLASS(script_class, script_, ranges, m_size,        \
    377                                m_init, m_scale, m_done, h_init, h_apply)     \
    378   FT_LOCAL_DEF(void)                                                         \
    379   FT_Init_Class_##script_class(AF_ScriptClassRec* ac)                        \
    380   {                                                                          \
    381     ac->script                = script_;                                     \
    382     ac->script_uni_ranges     = ranges;                                      \
    383                                                                              \
    384     ac->script_metrics_size   = m_size;                                      \
    385                                                                              \
    386     ac->script_metrics_init   = m_init;                                      \
    387     ac->script_metrics_scale  = m_scale;                                     \
    388     ac->script_metrics_done   = m_done;                                      \
    389                                                                              \
    390     ac->script_hints_init     = h_init;                                      \
    391     ac->script_hints_apply    = h_apply;                                     \
    392   }
    393 #endif
    394 
    395 
    396 /* */
    397 
    398 FT_END_HEADER
    399 
    400 #endif /* __AFTYPES_H__ */
    401 
    402 
    403 /* END */
    404