#
# Output a font IVS table to html.
# 

#set font_file [file join [file dir [info script]] ../tests/fonts/TakaoExMincho.ttf]
set font_file "D:/Fonts/msmincho510.ttc"
# append a path to freetype tcl binding.
lappend auto_path [file normalize [file join [file dir [info script]] ../../]]

package require Freetype
package require $FT_GLYPH
package require $FT_BITMAP
package require $FT_OUTLINE
package require $FT_BBOX
package require Tk ;# required to GIF output.

set error [FT_Init_FreeType lib]
if {$error != 0} {error $::ft::errors($eoor)}

set error [FT_New_Face $lib $font_file 0 face]
if {$error != 0} {error $::ft::errors($error)}
#----
proc output_img {face size index} {
    set size [::ft::_f26dot6_from_double $size]
    set error [FT_Set_Char_Size $face $size $size 98 98]
    if {$error != 0} {error $::ft::errors($error)}

    set error [FT_Load_Glyph $face $index $::FT_LOAD_DEFAULT]
    if {$error != 0} {error $::ft::errors($error)}

    #set error [FT_Render_Glyph [$face get glyph] $::FT_RENDER_MODE_NORMAL]
    #if {$error != 0} {error $::ft::errors($error)}

    set strength [expr int($size * 0.10)]
   
    # FT_Bitmap”Ĺ
#    set ft_bitmap  [[$face get glyph] get bitmap]
#    FT_Bitmap_Embolden $::lib $ft_bitmap $strength $strength
    
    # FT_Outline”Ĺ
    set ft_outline [[$face get glyph] get outline]
    #FT_Outline_Embolden $ft_outline $strength
    FT_Outline_Get_CBox $ft_outline cbox
    $cbox set xMin [expr [$cbox get xMin] & ~63]
    $cbox set yMin [expr [$cbox get yMin] & ~63]
    $cbox set xMax [expr ([$cbox get xMax] + 63) & ~63]
    $cbox set yMax [expr ([$cbox get yMax] + 63) & ~63]
    #FT_Bitmap_New ft_bitmap
    #$ft_bitmap set width [expr ([$cbox get xMax] - [$cbox get xMin]) >> 6]
    #$ft_bitmap set rows  [expr ([$cbox get yMax] - [$cbox get yMin]) >> 6]
    #$ft_bitmap set pitch [$ft_bitmap get width]
    #$ft_bitmap set num_grays 0xFF
    #$ft_bitmap set pixel_mode $::FT_PIXEL_MODE_GRAY
    #$ft_bitmap set buffer [::ft::__new_buf [expr [$ft_bitmap get pitch] * [$ft_bitmap get rows]]]
    #FT_Outline_Translate $ft_outline [expr -1 * [$cbox get xMin]] [expr -1 * [$cbox get yMin]]
    #FT_Outline_Get_Bitmap $::lib $ft_outline $ft_bitmap

	FT_Render_Glyph [$face get glyph] $::FT_RENDER_MODE_NORMAL
    FT_Get_Glyph [$face get glyph] glyph
    set bitmapglyph [FT_BitmapGlyph new [$glyph getPointer]]
    set ft_bitmap [$bitmapglyph get bitmap]
    set top [$bitmapglyph get top]
    set left [$bitmapglyph get left]
    set width [$ft_bitmap get width]
    set height [$ft_bitmap get rows]
    set buffer [$ft_bitmap get buffer]
    set bin $buffer
    puts top=$top
    puts left=$left
    set rgblist [list]
    foreach {v} [::ft::_bytearray_to_list $bin uint8] {
        set v [expr {0xFF - $v}] ;#invert mono
        lappend rgblist [format "#%02X%02X%02X" $v $v $v]
    }

    set data [list]
    set i 0
    for {set y 0} {$y < $height} {incr y} {
        set row [list]
        for {set x 0} {$x < $width} {incr x} {
            lappend row [lindex $rgblist $i]
            incr i
        }
        lappend data $row
    }

    set img [image create photo -width $width -height $height]
    $img put $data

    set img_file img_[clock seconds].png
    $img write $img_file -background #ffffff -format png

    image delete $img
    #return $index
    return "<img src=$img_file>"
}

set size  480
set index [FT_Get_Char_Index $face 97];# a
set index [FT_Get_Char_Index $face 67];# C

output_img $face $size $index

#----
set error [FT_Done_Face $face]
if {$error != 0} {error $::ft::errors($error)}

set error [FT_Done_FreeType $lib]
if {$error != 0} {error $::ft::errors($error)}

destroy .
exit

