package provide Freetype::t1tables 0.1

if {![info exists _source([file tail [info script]])]} {
package require Freetype::ft2build
package require $FT_FREETYPE

ft::struct define PS_FontInfo {
    {version             string * FT_String 1 pointer}
    {notice              string * FT_String 1 pointer}
    {full_name           string * FT_String 1 pointer}
    {family_name         string * FT_String 1 pointer}
    {weight              string * FT_String 1 pointer}
    {italic_angle        int    - FT_ULong  1 FT_ULong}
    {is_fixed_pitch      int    - FT_Bool   1 FT_Bool}
    {underline_position  int    - FT_Short  1 FT_Short}
    {underline_thickness int    - FT_UShort 1 FT_UShort}
}

ffidl::typedef T1_FontInfo PS_FontInfo

ft::struct define PS_Private {
    {unique_id              int - FT_Int    1 FT_Int}
    {lenIV                  int - FT_Int    1 FT_Int}
    {num_blue_values        int - FT_Byte   1 FT_Byte}
    {num_other_blues        int - FT_Byte   1 FT_Byte}
    {num_family_blues       int - FT_Byte   1 FT_Byte}
    {num_family_other_blues int - FT_Byte   1 FT_Byte}
    {blue_values            int - FT_Short 14 FT_Short}
    {other_blues            int - FT_Short 10 FT_Short}
    {family_blues           int - FT_Short 14 FT_Short}
    {family_other_blues     int - FT_Short 10 FT_Short}
    {blue_scale             int - FT_Fixed  1 FT_Fixed}
    {blue_shift             int - FT_Int    1 FT_Int}
    {blue_fuzz              int - FT_Int    1 FT_Int}
    {standard_width         int - FT_UShort 1 FT_UShort}
    {standard_height        int - FT_UShort 1 FT_UShort}
    {num_snap_widths        int - FT_Byte   1 FT_Byte}
    {num_snap_heights       int - FT_Byte   1 FT_Byte}
    {force_bold             int - FT_Bool   1 FT_Bool}
    {round_stem_up          int - FT_Bool   1 FT_Bool}
    {snap_widths            int - FT_Short 13 FT_Short}
    {snap_heights           int - FT_Short 13 FT_Short}
    {expansion_factor       int - FT_Fixed  1 FT_Fixed}
    {language_group         int - FT_Long   1 FT_Long}
    {password               int - FT_Long   1 FT_Long}
    {min_feature            int - FT_Short  2 FT_Short}
}

ffidl::typedef T1_Private PS_Private

set T1_BLEND_UNDERLINE_POSITION  0
set T1_BLEND_UNDERLINE_THICKNESS 1
set T1_BLEND_ITALIC_ANGLE        2
set T1_BLEND_BLUE_VALUES         3
set T1_BLEND_OTHER_BLUES         4
set T1_BLEND_STANDARD_WIDTH      5
set T1_BLEND_STANDARD_HEIGHT     6
set T1_BLEND_STEM_SNAP_WIDTHS    7
set T1_BLEND_STEM_SNAP_HEIGHTS   8
set T1_BLEND_BLUE_SCALE          9
set T1_BLEND_BLUE_SHIFT         10
set T1_BLEND_FAMILY_BLUES       11
set T1_BLEND_FAMILY_OTHER_BLUES 12
set T1_BLEND_FORCE_BOLD         13
set T1_BLEND_MAX                14
set t1_blend_underline_position  $T1_BLEND_UNDERLINE_POSITION
set t1_blend_underline_thickness $T1_BLEND_UNDERLINE_THICKNESS
set t1_blend_italic_angle        $T1_BLEND_ITALIC_ANGLE
set t1_blend_blue_values         $T1_BLEND_BLUE_VALUES
set t1_blend_other_blues         $T1_BLEND_OTHER_BLUES
set t1_blend_standard_widths     $T1_BLEND_STANDARD_WIDTH
set t1_blend_standard_height     $T1_BLEND_STANDARD_HEIGHT
set t1_blend_stem_snap_widths    $T1_BLEND_STEM_SNAP_WIDTHS
set t1_blend_stem_snap_heights   $T1_BLEND_STEM_SNAP_HEIGHTS
set t1_blend_blue_scale          $T1_BLEND_BLUE_SCALE
set t1_blend_blue_shift          $T1_BLEND_BLUE_SHIFT
set t1_blend_family_blues        $T1_BLEND_FAMILY_BLUES
set t1_blend_family_other_blues  $T1_BLEND_FAMILY_OTHER_BLUES
set t1_blend_force_bold          $T1_BLEND_FORCE_BOLD
set t1_blend_max                 $T1_BLEND_MAX

set T1_MAX_MM_DESIGNS     16

set T1_MAX_MM_AXIS        4

set T1_MAX_MM_MAP_POINTS  20


ft::struct define PS_DesignMap {
    {num_points    int - FT_Byte  1 FT_Byte}
    {design_points int * FT_Long  1 FT_Long}
    {blend_points  int * FT_Fixed 1 FT_Fixed}
}

ffidl::typedef T1_DesignMap PS_DesignMap

ft::struct define PS_Blend [list \
    [list num_designs           int    - FT_UInt      1                  FT_UInt]\
    [list num_axis              int    - FT_UInt      1                  FT_UInt]\
    [list axis_names            string * FT_String    $T1_MAX_MM_AXIS    FT_String]\
    [list design_pos            int    * FT_Fixed     $T1_MAX_MM_DESIGNS FT_Fixed]\
    [list design_map            object - PS_DesignMap $T1_MAX_MM_AXIS    PS_DesignMap]\
    [list weight_vector         int    * FT_Fixed     1                  pointer]\
    [list default_weight_vector int    * FT_Fixed     1                  pointer]\
    [list font_infos            object * PS_FontInfo  [expr $T1_MAX_MM_DESIGNS + 1] pointer]\
    [list privates              object * PS_Private   [expr $T1_MAX_MM_DESIGNS + 1] pointer]\
    [list blend_bitflags        int    - FT_ULong     1                  FT_ULong]\
    [list bboxes                object * FT_BBox      [expr $T1_MAX_MM_DESIGNS + 1] pointer]\
    [list default_design_vector     int - FT_UInt     $T1_MAX_MM_DESIGNS pointer]\
    [list num_default_design_vector int - FT_UInt     1                  FT_UInt]\
]

ffidl::typedef T1_Blend PS_Blend


ft::struct define CID_FaceDict {
    {private_dict        object - PS_Private 1 PS_Private}
    {len_buildchar       int    - FT_UInt    1 FT_UInt} 
    {forcebold_threshold int    - FT_Fixed   1 FT_Fixed}
    {stroke_width        int    - FT_Pos     1 FT_Pos}
    {expansion_factor    int    - FT_Fixed   1 FT_Fixed}
    {paint_type          int    - FT_Byte    1 FT_Byte}
    {font_type           int    - FT_Byte    1 FT_Byte}
    {font_matrix         object - FT_Matrix  1 FT_Matrix}   
    {font_offset         object - FT_Vector  1 FT_Vector} 
    {num_subrs           int    - FT_UInt    1 FT_UInt}
    {subrmap_offset      int    - FT_ULong   1 FT_ULong}
    {sd_bytes            int    - FT_Int     1 FT_Int}
}

ffidl::typedef CID_FontDict CID_FaceDict

ft::struct define CID_FaceInfo {
    {cid_font_name     string * FT_String    1 pointer}   
    {cid_version       int    - FT_Fixed     1 FT_Fixed}
    {cid_font_type     int    - FT_Int       1 FT_Int}
    {registry          string * FT_String    1 pointer}
    {ordering          string * FT_String    1 pointer}
    {supplement        int    - FT_Int       1 FT_Int}     
    {font_info         object - PS_FontInfo  1 PS_FontInfo}
    {font_bbox         object - FT_BBox      1 FT_BBox}
    {uid_base          int    - FT_ULong     1 FT_ULong}    
    {num_xuid          int    - FT_Int       1 FT_Int}   
    {xuid              int    - FT_ULong    16 FT_ULong}
    {cidmap_offset     int    - FT_ULong     1 FT_ULong}
    {fd_bytes          int    - FT_Int       1 FT_Int}    
    {gd_bytes          int    - FT_Int       1 FT_Int}    
    {cid_count         int    - FT_ULong     1 FT_ULong}
    {num_dicts         int    - FT_Int       1 FT_Int}    
    {font_dicts        object * CID_FaceDict 1 pointer}
    {data_offset       int    - FT_ULong     1 FT_ULong}  
}
ffidl::typedef CID_Info CID_FaceInfo


::ffidl::callout _FT_Has_PS_Glyph_Names {pointer} int \
    [::ffidl::symbol $::ft::lib FT_Has_PS_Glyph_Names]
proc  FT_Has_PS_Glyph_Names {face} {
    set result [_FT_Has_PS_Glyph_Names [$face getPointer]]
    return $result
}

::ffidl::callout _FT_Get_PS_Font_Info {pointer pointer} int \
    [::ffidl::symbol $::ft::lib FT_Get_PS_Font_Info]
proc  FT_Get_PS_Font_Info {face font_infoVarName} {
    upvar $font_infoVarName info

    if {[info exists info] == 0} {
        set info [PS_FontInfo new]
    }
    set result [_FT_Get_PS_Font_Info [$face getPointer] [$info getPointer]]
    if {$result != 0} {
        if {[info exists $info]} {
            $info destroy
            unset $info
        }
    }
    return $result
}

::ffidl::callout _FT_Get_PS_Font_Private {pointer pointer} int \
    [::ffidl::symbol $::ft::lib FT_Get_PS_Font_Private]
proc  FT_Get_PS_Font_Private {face font_privateVarName} {
    upvar $font_privateVarName private

    if {[info exists private] == 0} {
        set private [PS_Private new]
    }
    set result 0

    set result [_FT_Get_PS_Font_Private [$face getPointer] [$private getPointer]]
    if {$result != 0} {
        if {[info exists $info]} {
            $private destroy
            unset private
        }
    }
    return $result
}


set T1_ENCODING_TYPE_NONE      0
set T1_ENCODING_TYPE_ARRAY     1
set T1_ENCODING_TYPE_STANDARD  2
set T1_ENCODING_TYPE_ISOLATIN1 3
set T1_ENCODING_TYPE_EXPERT    4
ffidl::typedef T1_EncodingType int

# conventionally in the font dictionary
set PS_DICT_FONT_TYPE         0 ;# FT_Byte
set PS_DICT_FONT_MATRIX       1 ;# FT_Fixed
set PS_DICT_FONT_BBOX         2 ;# FT_Fixed
set PS_DICT_PAINT_TYPE        3 ;# FT_Byte
set PS_DICT_FONT_NAME         4 ;# FT_String*
set PS_DICT_UNIQUE_ID         5 ;# FT_Int
set PS_DICT_NUM_CHAR_STRINGS  6 ;# FT_Int
set PS_DICT_CHAR_STRING_KEY   7 ;# FT_String*
set PS_DICT_CHAR_STRING       8 ;# FT_String*
set PS_DICT_ENCODING_TYPE     9 ;# T1_EncodingType
set PS_DICT_ENCODING_ENTRY   10 ;# FT_String*
# conventionally in the font Private dictionary
set PS_DICT_NUM_SUBRS              11 ;# FT_Int
set PS_DICT_SUBR                   12 ;# FT_String*
set PS_DICT_STD_HW                 13 ;# FT_UShort
set PS_DICT_STD_VW                 14 ;# FT_UShort
set PS_DICT_NUM_BLUE_VALUES        15 ;# FT_Byte
set PS_DICT_BLUE_VALUE             16 ;# FT_Short
set PS_DICT_BLUE_FUZZ              17 ;# FT_Int
set PS_DICT_NUM_OTHER_BLUES        18 ;# FT_Byte
set PS_DICT_OTHER_BLUE             19 ;# FT_Short
set PS_DICT_NUM_FAMILY_BLUES       20 ;# FT_Byte
set PS_DICT_FAMILY_BLUE            21 ;# FT_Short
set PS_DICT_NUM_FAMILY_OTHER_BLUES 22 ;# FT_Byte
set PS_DICT_FAMILY_OTHER_BLUE      23 ;# FT_Short
set PS_DICT_BLUE_SCALE             24 ;# FT_Fixed
set PS_DICT_BLUE_SHIFT             25 ;# FT_Int
set PS_DICT_NUM_STEM_SNAP_H        26 ;# FT_Byte
set PS_DICT_STEM_SNAP_H            27 ;# FT_Short
set PS_DICT_NUM_STEM_SNAP_V        28 ;# FT_Byte
set PS_DICT_STEM_SNAP_V            29 ;# FT_Short
set PS_DICT_FORCE_BOLD             30 ;# FT_Bool
set PS_DICT_RND_STEM_UP            31 ;# FT_Bool
set PS_DICT_MIN_FEATURE            32 ;# FT_Short
set PS_DICT_LEN_IV                 33 ;# FT_Int
set PS_DICT_PASSWORD               34 ;# FT_Long
set PS_DICT_LANGUAGE_GROUP         35 ;# FT_Long
# conventionally in the font FontInfo dictionary
set PS_DICT_VERSION             36 ;# FT_String*
set PS_DICT_NOTICE              37 ;# FT_String*
set PS_DICT_FULL_NAME           38 ;# FT_String*
set PS_DICT_FAMILY_NAME         39 ;# FT_String*
set PS_DICT_WEIGHT              40 ;# FT_String*
set PS_DICT_IS_FIXED_PITCH      41 ;# FT_Bool
set PS_DICT_UNDERLINE_POSITION  42 ;# FT_Short
set PS_DICT_UNDERLINE_THICKNESS 43 ;# FT_UShort
set PS_DICT_FS_TYPE             44 ;# FT_UShort
set PS_DICT_ITALIC_ANGLE        45 ;# FT_Long
set PS_DICT_MAX $PS_DICT_ITALIC_ANGLE
ffidl::typedef PS_Dict_Keys int


::ffidl::callout _FT_Get_PS_Font_Value {pointer int FT_UInt pointer int} int \
    [::ffidl::symbol $::ft::lib FT_Get_PS_Font_Value]
proc  FT_Get_PS_Font_Value {face key idx valueVarName value_lenVarName} {

    if {$valueVarName eq ""} {
            set adr 0
            set len 0
    } else {
        upvar $valueVarName value
        #upvar $value_lenVarName value_len
        ::ft::get_bytearray_pointer value adr len
    }

    set result [_FT_Get_PS_Font_Value [$face getPointer] $key $idx $adr $len]
    return $result
}

set _source([file tail [info script]]) 1
}
