package provide Freetype::ftglyph 0.1

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

ft::struct define FT_Glyph {
    {library object * FT_Library     1 pointer}
    {clazz   object * FT_Glyph_Class 1 pointer}
    {format  int    - int            1 int}
    {advance object - FT_Vector      1 FT_Vector}
}

ft::struct define FT_BitmapGlyph {
    {root   object - FT_Glyph  1  FT_Glyph}
    {left   int    - FT_Int    1  FT_Int}
    {top    int    - FT_Int    1  FT_Int}
    {bitmap object - FT_Bitmap 1  FT_Bitmap}
}

ft::struct define FT_OutlineGlyph {
    {root    object - FT_Glyph   1 FT_Glyph}
    {outline object - FT_Outline 1 FT_Outline}
}

::ffidl::callout _FT_Get_Glyph {pointer pointer-var} int \
    [::ffidl::symbol $::ft::lib FT_Get_Glyph]
proc FT_Get_Glyph {slot glyphVarName} {
    upvar $glyphVarName glyph
    set p [::ft::_bytearray_from_pointer 0]
    set result [_FT_Get_Glyph [$slot getPointer] p]
    if {$result == 0} {
        set obj [FT_Glyph new [::ft::_bytearray_to_pointer $p]]
        set glyph $obj
    }
    return $result
}

::ffidl::callout _FT_Glyph_Copy {pointer pointer-var} int \
    [::ffidl::symbol $::ft::lib FT_Glyph_Copy]
proc FT_Glyph_Copy {source targetVarName} {
    upvar $targetVarName target
    set p [::ft::_bytearray_from_pointer 0]
    set result [_FT_Glyph_Copy [$source getPointer] p]
    if {$result == 0} {
        set target [FT_Glyph new [::ft::_bytearray_to_pointer $p]]
    }
    return $result
}

::ffidl::callout _FT_Glyph_Transform {pointer pointer pointer} int \
    [::ffidl::symbol $::ft::lib FT_Glyph_Transform]
proc FT_Glyph_Transform {glyph matrix delta} {
    set result [_FT_Glyph_Transform [$glyph getPointer] \
        [$matrix getPointer] [$delta getPointer]]
    return $result
}

set FT_GLYPH_BBOX_UNSCALED  0
set FT_GLYPH_BBOX_SUBPIXELS 0
set FT_GLYPH_BBOX_GRIDFIT   1
set FT_GLYPH_BBOX_TRUNCATE  2
set FT_GLYPH_BBOX_PIXELS    3
set ft_glyph_bbox_unscaled   $FT_GLYPH_BBOX_UNSCALED
set ft_glyph_bbox_subpixels  $FT_GLYPH_BBOX_SUBPIXELS
set ft_glyph_bbox_truncate   $FT_GLYPH_BBOX_TRUNCATE
set ft_glyph_bbox_pixels     $FT_GLYPH_BBOX_PIXELS

::ffidl::callout _FT_Glyph_Get_CBox {pointer FT_UInt pointer} void \
    [::ffidl::symbol $::ft::lib FT_Glyph_Get_CBox]
proc FT_Glyph_Get_CBox {glyph bbox_mode cboxVarName} {
    upvar $cboxVarName cbox
    if {[info exists cbox] == 0} {
        set cbox [FT_BBox new]
    }
    set p [::ft::_bytearray_from_pointer 0]
    _FT_Glyph_Get_CBox [$glyph getPointer] $bbox_mode [$cbox getPointer]
    
    return
}

::ffidl::callout _FT_Glyph_To_Bitmap {pointer-var int pointer FT_Bool} int \
    [::ffidl::symbol $::ft::lib FT_Glyph_To_Bitmap]
proc FT_Glyph_To_Bitmap {the_glyphVarName render_mode origin destroy} {
    upvar $the_glyphVarName the_glyph
    set bin_p_glyph [::ft::_bytearray_from_pointer [$the_glyph getPointer]]
    set origin_pointer 0
    if {[info object isa object $origin]} {
        set origin_pointer [$origin getPointer]
    }
    set result [_FT_Glyph_To_Bitmap bin_p_glyph $render_mode $origin_pointer $destroy]
    set the_glyph [FT_Glyph new [::ft::_bytearray_to_pointer $bin_p_glyph]]
    return $result
}

::ffidl::callout _FT_Done_Glyph {pointer} void [::ffidl::symbol $::ft::lib FT_Done_Glyph]
proc FT_Done_Glyph {glyph} {
    _FT_Done_Glyph [$glyph getPointer]
}

::ffidl::callout _FT_Matrix_Multiply {pointer pointer} void \
    [::ffidl::symbol $::ft::lib FT_Matrix_Multiply]
proc FT_Matrix_Multiply {a b} {
    _FT_Matrix_Multiply [$a getPointer] [$b getPointer]
}

::ffidl::callout _FT_Matrix_Invert {pointer} int \
    [::ffidl::symbol $::ft::lib FT_Matrix_Invert]
proc FT_Matrix_Invert {matrix} {
    set result [_FT_Matrix_Invert [$matrix getPointer]]
    return $result
}


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