Added text width function for font
This commit is contained in:
parent
a24dc7ba30
commit
0aff140884
@ -6,53 +6,60 @@ package allegro5
|
|||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Font struct {
|
type Font struct {
|
||||||
font *C.ALLEGRO_FONT
|
font *C.ALLEGRO_FONT
|
||||||
}
|
}
|
||||||
|
|
||||||
type HorizontalAlignment int
|
type HorizontalAlignment int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AlignLeft HorizontalAlignment = iota
|
AlignLeft HorizontalAlignment = iota
|
||||||
AlignCenter
|
AlignCenter
|
||||||
AlignRight
|
AlignRight
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadTTFFont(path string, size int) (*Font, error) {
|
func LoadTTFFont(path string, size int) (*Font, error) {
|
||||||
p := C.CString(path)
|
p := C.CString(path)
|
||||||
defer C.free(unsafe.Pointer(p))
|
defer C.free(unsafe.Pointer(p))
|
||||||
|
|
||||||
f := C.al_load_ttf_font(p, C.int(size), 0)
|
f := C.al_load_ttf_font(p, C.int(size), 0)
|
||||||
if nil == f {
|
if nil == f {
|
||||||
return nil, fmt.Errorf("Unable to load TTF font '%s'", path)
|
return nil, fmt.Errorf("Unable to load TTF font '%s'", path)
|
||||||
}
|
}
|
||||||
return &Font{f}, nil
|
return &Font{f}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Font) drawFlags(a HorizontalAlignment) C.int {
|
func (f *Font) drawFlags(a HorizontalAlignment) C.int {
|
||||||
switch a {
|
switch a {
|
||||||
case AlignLeft:
|
case AlignLeft:
|
||||||
return C.ALLEGRO_ALIGN_LEFT
|
return C.ALLEGRO_ALIGN_LEFT
|
||||||
case AlignCenter:
|
case AlignCenter:
|
||||||
return C.ALLEGRO_ALIGN_CENTRE
|
return C.ALLEGRO_ALIGN_CENTRE
|
||||||
case AlignRight:
|
case AlignRight:
|
||||||
return C.ALLEGRO_ALIGN_RIGHT
|
return C.ALLEGRO_ALIGN_RIGHT
|
||||||
}
|
}
|
||||||
return C.ALLEGRO_ALIGN_LEFT
|
return C.ALLEGRO_ALIGN_LEFT
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Font) Draw(left, top float32, color Color, align HorizontalAlignment, text string) {
|
func (f *Font) Draw(left, top float32, color Color, align HorizontalAlignment, text string) {
|
||||||
t := C.CString(text)
|
t := C.CString(text)
|
||||||
defer C.free(unsafe.Pointer(t))
|
defer C.free(unsafe.Pointer(t))
|
||||||
|
|
||||||
flags := f.drawFlags(align)
|
flags := f.drawFlags(align)
|
||||||
C.al_draw_text(f.font, color.color, C.float(left), C.float(top), flags, t)
|
C.al_draw_text(f.font, color.color, C.float(left), C.float(top), flags, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Font) TextWidth(text string) float32 {
|
||||||
|
t := C.CString(text)
|
||||||
|
defer C.free(unsafe.Pointer(t))
|
||||||
|
|
||||||
|
return float32(C.al_get_text_width(f.font, t))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Font) Destroy() {
|
func (f *Font) Destroy() {
|
||||||
C.al_destroy_font(f.font)
|
C.al_destroy_font(f.font)
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user