package provide Freetype::ftmm 0.1

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

ft::struct define FT_MM_Axis {
    {name    string * char    1 pointer}
    {minimum int    - FT_Long 1 FT_Long}
    {maximum int    - FT_Long 1 FT_Long}
}

ft::struct define FT_Multi_Master [list \
    [list num_axis   int    - FT_UInt    1 FT_UInt] \
    [list num_design int    - FT_UInt    1 FT_UInt] \
    [list axis       object - FT_MM_Axis $T1_MAX_MM_AXIS FT_MM_Axis] \
]

ft::struct define FT_Var_Axis {
    {name    string * char      1 pointer}
    {minimum int    - FT_Fixed  1 FT_Fixed}
    {def     int    - FT_Fixed  1 FT_Fixed}
    {maximum int    - FT_Fixed  1 FT_Fixed}
    {tag     int    - FT_ULong  1 FT_ULong}
    {strid   int    - FT_UInt   1 FT_UInt}
}

ft::struct define FT_Var_Named_Style {
    {coords  int  * FT_Fixed 1 pointer}
    {strid   int  - FT_UInt  1 FT_UInt}
}

ft::struct define FT_MM_Var {
    {num_axis        int    - FT_UInt            1 FT_UInt}
    {num_designs     int    - FT_UInt            1 FT_UInt}
    {num_namedstyles int    - FT_UInt            1 FT_UInt}
    {axis            object * FT_Var_Axis        1 pointer}
    {namedstyle      object * FT_Var_Named_Style 1 pointer}
}

::ffidl::callout _FT_Get_Multi_Master {pointer pointer} int \
    [::ffidl::symbol $::ft::lib FT_Get_Multi_Master]
proc  FT_Get_Multi_Master {face masterVarName} {
    upvar $masterVarName master
    if {[info exists master]} {
        set p [$master getPointer]
    } else {
        set _master [FT_Multi_Master new]
        set p [$_master getPointer]
    }
    set result [_FT_Get_Multi_Master [$face getPointer] $p]
    if {$result == 0} {
        set master [FT_Multi_Master new $p]
    } else {
        if {[info exists $_master]} {
            $_master destroy
        }
    }
    return $result
}

::ffidl::callout _FT_Get_MM_Var {pointer pointer-var} int \
    [::ffidl::symbol $::ft::lib FT_Get_MM_Var]
proc  FT_Get_MM_Var {face masterVarName} {
    set p [::ft::_bytearray_from_pointer 0]
    set result [_FT_Get_MM_Var [$face getPointer] p]
    if {$result == 0} {
        set obj [FT_MM_Var new [::ft::_bytearray_to_pointer $p]]
        uplevel [list set $masterVarName $obj]
    }
    return $result
}

::ffidl::callout _FT_Set_MM_Design_Coordinates \
    {pointer FT_UInt pointer} int \
    [::ffidl::symbol $::ft::lib FT_Set_MM_Design_Coordinates]
proc  FT_Set_MM_Design_Coordinates {face num_coords coords} {
    set bin [::ft::_bytearray_from_list $coords FT_Long]
    ::ft::get-bytearray-pointer bin adr len
    set result [_FT_Set_MM_Design_Coordinates \
        [$face getPointer] $num_coords $adr]
    return $result
}

::ffidl::callout _FT_Set_Var_Design_Coordinates \
    {pointer FT_UInt pointer} int \
    [::ffidl::symbol $::ft::lib FT_Set_Var_Design_Coordinates]
proc  FT_Set_Var_Design_Coordinates {face num_coords coords} {
    set bin [::ft::_bytearray_from_list $coords FT_Fixed]
    ::ft::get-bytearray-pointer bin adr len
    set result [_FT_Set_Var_Design_Coordinates \
        [$face getPointer] $num_coords $adr]
    return $result
}

::ffidl::callout _FT_Set_MM_Blend_Coordinates \
    {pointer FT_UInt pointer} int \
    [::ffidl::symbol $::ft::lib FT_Set_MM_Blend_Coordinates]
proc  FT_Set_MM_Blend_Coordinates {face num_coords coords} {
    set bin [::ft::_bytearray_from_list $coords FT_Fixed]
    ::ft::get-bytearray-pointer bin adr len
    set result [_FT_Set_MM_Blend_Coordinates \
        [$face getPointer] $num_coords $adr]
    return $result
}

::ffidl::callout _FT_Set_Var_Blend_Coordinates \
    {pointer FT_UInt pointer} int \
    [::ffidl::symbol $::ft::lib FT_Set_Var_Blend_Coordinates]
proc  FT_Set_Var_Blend_Coordinates {face num_coords coords} {
    set bin [::ft::_bytearray_from_list $coords FT_Fixed]
    ::ft::get-bytearray-pointer bin adr len
    set result [_FT_Set_Var_Blend_Coordinates \
        [$face getPointer] $num_coords $adr]
    return $result
}

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