package provide Freetype::ftcache 0.1

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

::ffidl::typedef FTC_FaceID FT_Pointer

# objects, unknown size
ft::struct define FTC_Manager {
}

ft::struct define FTC_Node {
}

::ffidl::callout _FTC_Manager_New {pointer FT_UInt FT_UInt FT_ULong
    pointer FT_Pointer pointer-var} int \
    [::ffidl::symbol $::ft::lib FTC_Manager_New]
proc FTC_Manager_New {library max_faces max_sizes max_bytes requester
        req_data managerVarName} {
    set p [::ft::_bytearray_from_pointer 0]
    set result [_FTC_Manager_New [$library getPointer] $max_faces \
        $max_sizes $max_bytes $requester $req_data p]
    set obj [FTC_Manager new [::ft::_bytearray_to_pointer $p]]
    uplevel [list set $managerVarName $obj]
    return $result
}

::ffidl::callout _FTC_Manager_Reset {pointer} void \
    [::ffidl::symbol $::ft::lib FTC_Manager_Reset]
proc FTC_Manager_Reset {manager} {
    _FTC_Manager_Reset [$manager getPointer]
}

::ffidl::callout _FTC_Manager_Done {pointer} void \
    [::ffidl::symbol $::ft::lib FTC_Manager_Done]
proc FTC_Manager_Done {manager} {
    _FTC_Manager_Done [$manager getPointer]
}

::ffidl::callout _FTC_Manager_LookupFace {pointer FTC_FaceID pointer-var} int \
    [::ffidl::symbol $::ft::lib FTC_Manager_LookupFace]
proc FTC_Manager_LookupFace {manager face_id faceVarName} {
    upvar $faceVarName face
    set p [::ft::_bytearray_from_pointer 0]
    set result [_FTC_Manager_LookupFace [$manager getPointer] $face_id p]
    set face [FT_Face new [::ft::_bytearray_to_pointer $p]]
    return $result
}

ft::struct define FTC_Scaler {
    {face_id int - FTC_FaceID 1 FTC_FaceID}
    {width   int - FT_UInt    1 FT_UInt}
    {height  int - FT_UInt    1 FT_UInt}
    {pixel   int - FT_Int     1 FT_Int}
    {x_res   int - FT_UInt    1 FT_UInt}
    {y_res   int - FT_UInt    1 FT_UInt}
}

::ffidl::callout _FTC_Manager_LookupSize {pointer pointer pointer-var} int \
    [::ffidl::symbol $::ft::lib FTC_Manager_LookupSize]
proc FTC_Manager_LookupSize {manager scaler sizeVarName} {
    upvar $sizeVarName size
    set p [::ft::_bytearray_from_pointer 0]
    set result [_FTC_Manager_LookupSize [$manager getPointer] [$scaler getPointer] p]
    set size [FT_Size new [::ft::_bytearray_to_pointer $p]]
    return $result
}

::ffidl::callout _FTC_Node_Unref {pointer pointer} void \
    [::ffidl::symbol $::ft::lib FTC_Node_Unref]
proc FTC_Node_Unref {node manager} {
    _FTC_Node_Unref [$node getPointer] [$manager getPointer]
}

::ffidl::callout _FTC_Manager_RemoveFaceID {pointer FTC_FaceID} void \
    [::ffidl::symbol $::ft::lib FTC_Manager_RemoveFaceID]
proc FTC_Manager_RemoveFaceID {manager face_id} {
    _FTC_Manager_RemoveFaceID [$manager getPointer] $face_id
}

#TODO internal objects, unknown size of structure
ft::struct define FTC_CMapCache {
}

::ffidl::callout _FTC_CMapCache_New {pointer pointer-var} int \
    [::ffidl::symbol $::ft::lib FTC_CMapCache_New]
proc FTC_CMapCache_New {manager cacheVarName} {
    upvar $cacheVarName cache
    set p [::ft::_bytearray_from_pointer 0]
    set result [_FTC_CMapCache_New [$manager getPointer] p]
    set cache [FTC_CMapCache new [::ft::_bytearray_to_pointer $p]]
    return $result
}

::ffidl::callout _FTC_CMapCache_Lookup {pointer FTC_FaceID FT_Int
    FT_UInt32} FT_UInt [::ffidl::symbol $::ft::lib FTC_CMapCache_Lookup]
proc FTC_CMapCache_Lookup {cache face_id cmap_index char_code} {
    set result [_FTC_CMapCache_Lookup [$cache getPointer] \
        $face_id $cmap_index $char_code]
    return $result
}

ft::struct define FTC_ImageType {
    {face_id int - FTC_FaceID 1 FTC_FaceID}
    {width   int - FT_Int     1 FT_Int}
    {height  int - FT_Int     1 FT_Int}
    {flags   int - FT_Int32   1 FT_Int32}
}

proc FTC_IMAGE_TYPE_COMPARE {d1 d2} {
    return [$d1 get face_id] == [$d2 get face_id] && \
           [$d1 get width]   == [$d2 get width] && \
           [$d1 get flags]   == [$d2 get flags]
}

#TODO internal objects, unknown size of structure
ft::struct define FTC_ImageCache {
}

::ffidl::callout _FTC_ImageCache_New {pointer pointer-var} int \
    [::ffidl::symbol $::ft::lib FTC_ImageCache_New]
proc FTC_ImageCache_New {manager cacheVarName} {
    upvar $cacheVarName cache
    set p [::ft::_bytearray_from_pointer 0]
    set result [_FTC_ImageCache_New [$manager getPointer] p
    set cache [FTC_ImageCache new [::ft::_bytearray_to_pointer $p]]
    return $result
}

::ffidl::callout _FTC_ImageCache_Lookup {pointer pointer FT_UInt
    pointer-var pointer-var} int [::ffidl::symbol $::ft::lib FTC_ImageCache_Lookup]
proc FTC_ImageCache_Lookup {cache type gindex glyphVarName nodeVarName} {
    upvar $glyphVarName glyph
    upvar $nodeVarName node
    set p1 [::ft::_bytearray_from_pointer 0]
    set p2 [::ft::_bytearray_from_pointer 0]
    set result [_FTC_ImageCache_Lookup [$cache getPointer] [$type getPointer] $gindex p1 p2]
    set glyph [FT_Glyph new [::ft::_bytearray_to_pointer $p1]]
    set node [FTC_Node new [::ft::_bytearray_to_pointer $p2]]
    return $result
}

::ffidl::callout _FTC_ImageCache_LookupScaler {pointer pointer FT_ULong
    FT_UInt pointer-var pointer-var} int \
    [::ffidl::symbol $::ft::lib FTC_ImageCache_LookupScaler]
proc FTC_ImageCache_LookupScaler {cache scaler load_flags gint
    glyphVarName nodeVarName} {
    upvar $glyphVarName glyph
    upvar $nodeVarName node
    set p1 [::ft::_bytearray_from_pointer 0]
    set p2 [::ft::_bytearray_from_pointer 0]
    set result [_FTC_ImageCache_LookupScaler [$cache getPointer] \
        [$scaler getPointer] $load_flags $gndex p1 p2]
    set glyph [FT_Glyph new [::ft::_bytearray_to_pointer $p1]]
    set node [FTC_Node new [::ft::_bytearray_to_pointer $p2]]
    return $result
}

ft::struct define FTC_SBit {
    {width     int - FT_Byte  1 FT_Byte}
    {height    int - FT_Byte  1 FT_Byte}
    {left      int - FT_Char  1 FT_Char}
    {top       int - FT_Char  1 FT_Char}
    {format    int - FT_Byte  1 FT_Byte}
    {max_grays int - FT_Byte  1 FT_Byte}
    {pitch     int - FT_Short 1 FT_Short}
    {xadvance  int - FT_Char  1 FT_Char}
    {yadvance  int - FT_Char  1 FT_Char}
    {buffer    int * FT_Byte  1 pointer}
}

#TODO internal objects, unknown size of structure
ft::struct define FTC_SBitCache {
}

::ffidl::callout _FTC_SBitCache_New {pointer pointer-var} int \
    [::ffidl::symbol $::ft::lib FTC_SBitCache_New]
proc FTC_SBitCache_New {manager cacheVarName} {
    upvar $cacheVarName cache
    set p [::ft::_bytearray_from_pointer 0]
    set result [_FTC_SBitCache_New [$manager getPointer] p]
    set cache [FTC_SBitCache new [::ft::_bytearray_to_pointer $p]
    return $result
}

::ffidl::callout _FTC_SBitCache_Lookup {pointer pointer FT_UInt
    pointer-var pointer-var} int \
    [::ffidl::symbol $::ft::lib FTC_SBitCache_Lookup]
proc FTC_SBitCache_Lookup {cache type gindex sbitVarName nodeVarName} {
    upvar $sbitVarName sbit
    upvar $nodeVarName node
    set p1 [::ft::_bytearray_from_pointer 0]
    set p2 [::ft::_bytearray_from_pointer 0]
    set result [_FTC_SBitCache_Lookup [$cache getPointer] [$type getPointer] \
        $gindex p1 p2]
    set sbit [FTC_SBit new [::ft::_bytearray_to_pointer $p1]]
    set node [FTC_Node new [::ft::_bytearray_to_pointer $p2]]
    return $result
}

::ffidl::callout _FTC_SBitCache_LookupScaler {pointer pointer FT_ULong
    FT_UInt pointer-var pointer-var} int \
    [::ffidl::symbol $::ft::lib FTC_SBitCache_LookupScaler]
proc FTC_SBitCache_LookupScaler {cache scaler load_Flags gindex
    sbitVarName nodeVarName} {
    upvar $sbitVarName sbit
    upvar $nodeVarName node
    set p1 [::ft::_bytearray_from_pointer 0]
    set p2 [::ft::_bytearray_from_pointer 0]
    set result [_FTC_SBitCache_LookupScaler [$cache getPointer] \
        [$type getPointer] $load_flags $gindex p1 p2]
    set sbit [FTC_SBit new [::ft::_bytearray_to_pointer $p1]]
    set node [FTC_Node new [::ft::_bytearray_to_pointer $p2]]
    return $result
}

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