package provide Freetype::ftsnames 0.1

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

ft::struct define FT_SfntName {
    {platform_id int    - FT_UShort 1 FT_UShort}
    {encoding_id int    - FT_UShort 1 FT_UShort}
    {language_id int    - FT_UShort 1 FT_UShort}
    {name_id     int    - FT_UShort 1 FT_UShort}
    {string      string * char      1 pointer}
    {string_len  int    - FT_UInt   1 FT_UInt}
}

oo::define FT_SfntName method get {varName} {
    if {$varName eq "string"} {
        set len [next string_len]
        set adr [::ft::get_pointer_pointer [my getPointer string]]
        set bin [::ffidl::peek $adr $len]
        set bin [string reverse $bin]
        set str [encoding convertfrom unicode $bin]
        return  [string reverse $str]
    }
    next $varName
}

oo::define FT_SfntName set {varName varValue} {
    error "$varName is read only"
}

::ffidl::callout _FT_Get_Sfnt_Name_Count {pointer} FT_UInt \
    [::ffidl::symbol $::ft::lib FT_Get_Sfnt_Name_Count]
proc  FT_Get_Sfnt_Name_Count {face} {
    set result [_FT_Get_Sfnt_Name_Count [$face getPointer]]
    return $result
}

::ffidl::callout _FT_Get_Sfnt_Name {pointer int pointer} int \
    [::ffidl::symbol $::ft::lib FT_Get_Sfnt_Name]
proc  FT_Get_Sfnt_Name {face idx nameVarName} {
    upvar $nameVarName name
    if {[info exists name]} {
        set p [$name getPointer]
    } else {
        set temp [FT_SfntName new]
        set p [$temp getPointer]
    }
    set result [_FT_Get_Sfnt_Name [$face getPointer] $idx $p]
    if {$result == 0} {
        if {[info exists temp]} {
            set name $temp
        }
    } else {
        if {[info exists temp]} {
            $temp destroy
        }
    }
    return $result
}

set FT_PARAM_TAG_IGNORE_PREFERRED_FAMILY    [FT_MAKE_TAG "i" "g" "p" "f"]
set FT_PARAM_TAG_IGNORE_PREFERRED_SUBFAMILY [FT_MAKE_TAG "i" "g" "p" "s"]

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