Home | History | Annotate | Download | only in truetype
      1 /***************************************************************************/
      2 /*                                                                         */
      3 /*  ttinterp.h                                                             */
      4 /*                                                                         */
      5 /*    TrueType bytecode interpreter (specification).                       */
      6 /*                                                                         */
      7 /*  Copyright 1996-2015 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 #ifndef __TTINTERP_H__
     20 #define __TTINTERP_H__
     21 
     22 #include <ft2build.h>
     23 #include "ttobjs.h"
     24 
     25 
     26 FT_BEGIN_HEADER
     27 
     28 
     29   /*************************************************************************/
     30   /*                                                                       */
     31   /* Rounding mode constants.                                              */
     32   /*                                                                       */
     33 #define TT_Round_Off             5
     34 #define TT_Round_To_Half_Grid    0
     35 #define TT_Round_To_Grid         1
     36 #define TT_Round_To_Double_Grid  2
     37 #define TT_Round_Up_To_Grid      4
     38 #define TT_Round_Down_To_Grid    3
     39 #define TT_Round_Super           6
     40 #define TT_Round_Super_45        7
     41 
     42 
     43   /*************************************************************************/
     44   /*                                                                       */
     45   /* Function types used by the interpreter, depending on various modes    */
     46   /* (e.g. the rounding mode, whether to render a vertical or horizontal   */
     47   /* line etc).                                                            */
     48   /*                                                                       */
     49   /*************************************************************************/
     50 
     51   /* Rounding function */
     52   typedef FT_F26Dot6
     53   (*TT_Round_Func)( TT_ExecContext  exc,
     54                     FT_F26Dot6      distance,
     55                     FT_F26Dot6      compensation );
     56 
     57   /* Point displacement along the freedom vector routine */
     58   typedef void
     59   (*TT_Move_Func)( TT_ExecContext  exc,
     60                    TT_GlyphZone    zone,
     61                    FT_UShort       point,
     62                    FT_F26Dot6      distance );
     63 
     64   /* Distance projection along one of the projection vectors */
     65   typedef FT_F26Dot6
     66   (*TT_Project_Func)( TT_ExecContext  exc,
     67                       FT_Pos          dx,
     68                       FT_Pos          dy );
     69 
     70   /* getting current ppem.  Take care of non-square pixels if necessary */
     71   typedef FT_Long
     72   (*TT_Cur_Ppem_Func)( TT_ExecContext  exc );
     73 
     74   /* reading a cvt value.  Take care of non-square pixels if necessary */
     75   typedef FT_F26Dot6
     76   (*TT_Get_CVT_Func)( TT_ExecContext  exc,
     77                       FT_ULong        idx );
     78 
     79   /* setting or moving a cvt value.  Take care of non-square pixels  */
     80   /* if necessary                                                    */
     81   typedef void
     82   (*TT_Set_CVT_Func)( TT_ExecContext  exc,
     83                       FT_ULong        idx,
     84                       FT_F26Dot6      value );
     85 
     86 
     87   /*************************************************************************/
     88   /*                                                                       */
     89   /* This structure defines a call record, used to manage function calls.  */
     90   /*                                                                       */
     91   typedef struct  TT_CallRec_
     92   {
     93     FT_Int   Caller_Range;
     94     FT_Long  Caller_IP;
     95     FT_Long  Cur_Count;
     96 
     97     TT_DefRecord  *Def; /* either FDEF or IDEF */
     98 
     99   } TT_CallRec, *TT_CallStack;
    100 
    101 
    102 #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
    103 
    104   /*************************************************************************/
    105   /*                                                                       */
    106   /* These structures define rules used to tweak subpixel hinting for      */
    107   /* various fonts.  "", 0, "", NULL value indicates to match any value.   */
    108   /*                                                                       */
    109 
    110 #define SPH_MAX_NAME_SIZE      32
    111 #define SPH_MAX_CLASS_MEMBERS  100
    112 
    113   typedef struct  SPH_TweakRule_
    114   {
    115     const char      family[SPH_MAX_NAME_SIZE];
    116     const FT_UInt   ppem;
    117     const char      style[SPH_MAX_NAME_SIZE];
    118     const FT_ULong  glyph;
    119 
    120   } SPH_TweakRule;
    121 
    122 
    123   typedef struct  SPH_ScaleRule_
    124   {
    125     const char      family[SPH_MAX_NAME_SIZE];
    126     const FT_UInt   ppem;
    127     const char      style[SPH_MAX_NAME_SIZE];
    128     const FT_ULong  glyph;
    129     const FT_ULong  scale;
    130 
    131   } SPH_ScaleRule;
    132 
    133 
    134   typedef struct  SPH_Font_Class_
    135   {
    136     const char  name[SPH_MAX_NAME_SIZE];
    137     const char  member[SPH_MAX_CLASS_MEMBERS][SPH_MAX_NAME_SIZE];
    138 
    139   } SPH_Font_Class;
    140 
    141 #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
    142 
    143 
    144   /*************************************************************************/
    145   /*                                                                       */
    146   /* The main structure for the interpreter which collects all necessary   */
    147   /* variables and states.                                                 */
    148   /*                                                                       */
    149   typedef struct  TT_ExecContextRec_
    150   {
    151     TT_Face            face;
    152     TT_Size            size;
    153     FT_Memory          memory;
    154 
    155     /* instructions state */
    156 
    157     FT_Error           error;      /* last execution error */
    158 
    159     FT_Long            top;        /* top of exec. stack   */
    160 
    161     FT_Long            stackSize;  /* size of exec. stack  */
    162     FT_Long*           stack;      /* current exec. stack  */
    163 
    164     FT_Long            args;
    165     FT_Long            new_top;    /* new top after exec.  */
    166 
    167     TT_GlyphZoneRec    zp0,        /* zone records */
    168                        zp1,
    169                        zp2,
    170                        pts,
    171                        twilight;
    172 
    173     FT_Size_Metrics    metrics;
    174     TT_Size_Metrics    tt_metrics; /* size metrics */
    175 
    176     TT_GraphicsState   GS;         /* current graphics state */
    177 
    178     FT_Int             curRange;  /* current code range number   */
    179     FT_Byte*           code;      /* current code range          */
    180     FT_Long            IP;        /* current instruction pointer */
    181     FT_Long            codeSize;  /* size of current range       */
    182 
    183     FT_Byte            opcode;    /* current opcode              */
    184     FT_Int             length;    /* length of current opcode    */
    185 
    186     FT_Bool            step_ins;  /* true if the interpreter must */
    187                                   /* increment IP after ins. exec */
    188     FT_ULong           cvtSize;
    189     FT_Long*           cvt;
    190 
    191     FT_UInt            glyphSize; /* glyph instructions buffer size */
    192     FT_Byte*           glyphIns;  /* glyph instructions buffer */
    193 
    194     FT_UInt            numFDefs;  /* number of function defs         */
    195     FT_UInt            maxFDefs;  /* maximum number of function defs */
    196     TT_DefArray        FDefs;     /* table of FDefs entries          */
    197 
    198     FT_UInt            numIDefs;  /* number of instruction defs */
    199     FT_UInt            maxIDefs;  /* maximum number of ins defs */
    200     TT_DefArray        IDefs;     /* table of IDefs entries     */
    201 
    202     FT_UInt            maxFunc;   /* maximum function index     */
    203     FT_UInt            maxIns;    /* maximum instruction index  */
    204 
    205     FT_Int             callTop,    /* top of call stack during execution */
    206                        callSize;   /* size of call stack */
    207     TT_CallStack       callStack;  /* call stack */
    208 
    209     FT_UShort          maxPoints;    /* capacity of this context's `pts' */
    210     FT_Short           maxContours;  /* record, expressed in points and  */
    211                                      /* contours.                        */
    212 
    213     TT_CodeRangeTable  codeRangeTable;  /* table of valid code ranges */
    214                                         /* useful for the debugger   */
    215 
    216     FT_UShort          storeSize;  /* size of current storage */
    217     FT_Long*           storage;    /* storage area            */
    218 
    219     FT_F26Dot6         period;     /* values used for the */
    220     FT_F26Dot6         phase;      /* `SuperRounding'     */
    221     FT_F26Dot6         threshold;
    222 
    223     FT_Bool            instruction_trap; /* If `True', the interpreter will */
    224                                          /* exit after each instruction     */
    225 
    226     TT_GraphicsState   default_GS;       /* graphics state resulting from   */
    227                                          /* the prep program                */
    228     FT_Bool            is_composite;     /* true if the glyph is composite  */
    229     FT_Bool            pedantic_hinting; /* true if pedantic interpretation */
    230 
    231     /* latest interpreter additions */
    232 
    233     FT_Long            F_dot_P;    /* dot product of freedom and projection */
    234                                    /* vectors                               */
    235     TT_Round_Func      func_round; /* current rounding function             */
    236 
    237     TT_Project_Func    func_project,   /* current projection function */
    238                        func_dualproj,  /* current dual proj. function */
    239                        func_freeProj;  /* current freedom proj. func  */
    240 
    241     TT_Move_Func       func_move;      /* current point move function */
    242     TT_Move_Func       func_move_orig; /* move original position function */
    243 
    244     TT_Cur_Ppem_Func   func_cur_ppem;  /* get current proj. ppem value  */
    245 
    246     TT_Get_CVT_Func    func_read_cvt;  /* read a cvt entry              */
    247     TT_Set_CVT_Func    func_write_cvt; /* write a cvt entry (in pixels) */
    248     TT_Set_CVT_Func    func_move_cvt;  /* incr a cvt entry (in pixels)  */
    249 
    250     FT_Bool            grayscale;      /* are we hinting for grayscale? */
    251 
    252 #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
    253     TT_Round_Func      func_round_sphn;   /* subpixel rounding function */
    254 
    255     FT_Bool            subpixel_hinting;  /* Using subpixel hinting?       */
    256     FT_Bool            ignore_x_mode;     /* Standard rendering mode for   */
    257                                           /* subpixel hinting.  On if gray */
    258                                           /* or subpixel hinting is on.    */
    259 
    260     /* The following 6 aren't fully implemented but here for MS rasterizer */
    261     /* compatibility.                                                      */
    262     FT_Bool            compatible_widths;     /* compatible widths?        */
    263     FT_Bool            symmetrical_smoothing; /* symmetrical_smoothing?    */
    264     FT_Bool            bgr;                   /* bgr instead of rgb?       */
    265     FT_Bool            vertical_lcd;          /* long side of LCD subpixel */
    266                                               /* rectangles is horizontal  */
    267     FT_Bool            subpixel_positioned;   /* subpixel positioned       */
    268                                               /* (DirectWrite ClearType)?  */
    269     FT_Bool            gray_cleartype;        /* ClearType hinting but     */
    270                                               /* grayscale rendering       */
    271 
    272     FT_Int             rasterizer_version;    /* MS rasterizer version     */
    273 
    274     FT_Bool            iup_called;            /* IUP called for glyph?     */
    275 
    276     FT_ULong           sph_tweak_flags;       /* flags to control          */
    277                                               /* hint tweaks               */
    278 
    279     FT_ULong           sph_in_func_flags;     /* flags to indicate if in   */
    280                                               /* special functions         */
    281 
    282 #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
    283 
    284   } TT_ExecContextRec;
    285 
    286 
    287   extern const TT_GraphicsState  tt_default_graphics_state;
    288 
    289 
    290 #ifdef TT_USE_BYTECODE_INTERPRETER
    291   FT_LOCAL( void )
    292   TT_Goto_CodeRange( TT_ExecContext  exec,
    293                      FT_Int          range,
    294                      FT_Long         IP );
    295 
    296   FT_LOCAL( void )
    297   TT_Set_CodeRange( TT_ExecContext  exec,
    298                     FT_Int          range,
    299                     void*           base,
    300                     FT_Long         length );
    301 
    302   FT_LOCAL( void )
    303   TT_Clear_CodeRange( TT_ExecContext  exec,
    304                       FT_Int          range );
    305 
    306 
    307   FT_LOCAL( FT_Error )
    308   Update_Max( FT_Memory  memory,
    309               FT_ULong*  size,
    310               FT_ULong   multiplier,
    311               void*      _pbuff,
    312               FT_ULong   new_max );
    313 #endif /* TT_USE_BYTECODE_INTERPRETER */
    314 
    315 
    316   /*************************************************************************/
    317   /*                                                                       */
    318   /* <Function>                                                            */
    319   /*    TT_New_Context                                                     */
    320   /*                                                                       */
    321   /* <Description>                                                         */
    322   /*    Queries the face context for a given font.  Note that there is     */
    323   /*    now a _single_ execution context in the TrueType driver which is   */
    324   /*    shared among faces.                                                */
    325   /*                                                                       */
    326   /* <Input>                                                               */
    327   /*    face :: A handle to the source face object.                        */
    328   /*                                                                       */
    329   /* <Return>                                                              */
    330   /*    A handle to the execution context.  Initialized for `face'.        */
    331   /*                                                                       */
    332   /* <Note>                                                                */
    333   /*    Only the glyph loader and debugger should call this function.      */
    334   /*    (And right now only the glyph loader uses it.)                     */
    335   /*                                                                       */
    336   FT_EXPORT( TT_ExecContext )
    337   TT_New_Context( TT_Driver  driver );
    338 
    339 
    340 #ifdef TT_USE_BYTECODE_INTERPRETER
    341   FT_LOCAL( void )
    342   TT_Done_Context( TT_ExecContext  exec );
    343 
    344   FT_LOCAL( FT_Error )
    345   TT_Load_Context( TT_ExecContext  exec,
    346                    TT_Face         face,
    347                    TT_Size         size );
    348 
    349   FT_LOCAL( void )
    350   TT_Save_Context( TT_ExecContext  exec,
    351                    TT_Size         ins );
    352 
    353   FT_LOCAL( FT_Error )
    354   TT_Run_Context( TT_ExecContext  exec );
    355 #endif /* TT_USE_BYTECODE_INTERPRETER */
    356 
    357 
    358   /*************************************************************************/
    359   /*                                                                       */
    360   /* <Function>                                                            */
    361   /*    TT_RunIns                                                          */
    362   /*                                                                       */
    363   /* <Description>                                                         */
    364   /*    Executes one or more instruction in the execution context.  This   */
    365   /*    is the main function of the TrueType opcode interpreter.           */
    366   /*                                                                       */
    367   /* <Input>                                                               */
    368   /*    exec :: A handle to the target execution context.                  */
    369   /*                                                                       */
    370   /* <Return>                                                              */
    371   /*    FreeType error code.  0 means success.                             */
    372   /*                                                                       */
    373   /* <Note>                                                                */
    374   /*    Only the object manager and debugger should call this function.    */
    375   /*                                                                       */
    376   /*    This function is publicly exported because it is directly          */
    377   /*    invoked by the TrueType debugger.                                  */
    378   /*                                                                       */
    379   FT_EXPORT( FT_Error )
    380   TT_RunIns( TT_ExecContext  exec );
    381 
    382 
    383 FT_END_HEADER
    384 
    385 #endif /* __TTINTERP_H__ */
    386 
    387 
    388 /* END */
    389