2018-09-22 14:23:26 +00:00
|
|
|
package allg5
|
2017-10-03 18:38:09 +00:00
|
|
|
|
|
|
|
// #include <allegro5/allegro.h>
|
|
|
|
// #include <allegro5/allegro_font.h>
|
|
|
|
// #include <allegro5/allegro_ttf.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2017-10-23 09:18:48 +00:00
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
2017-10-03 18:38:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Font struct {
|
2017-10-23 09:18:48 +00:00
|
|
|
font *C.ALLEGRO_FONT
|
2018-08-07 04:59:52 +00:00
|
|
|
hght float32
|
|
|
|
asc float32
|
|
|
|
desc float32
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HorizontalAlignment int
|
|
|
|
|
|
|
|
const (
|
2017-10-23 09:18:48 +00:00
|
|
|
AlignLeft HorizontalAlignment = iota
|
|
|
|
AlignCenter
|
|
|
|
AlignRight
|
2017-10-03 18:38:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func LoadTTFFont(path string, size int) (*Font, error) {
|
2017-10-23 09:18:48 +00:00
|
|
|
p := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(p))
|
|
|
|
|
|
|
|
f := C.al_load_ttf_font(p, C.int(size), 0)
|
|
|
|
if nil == f {
|
2018-02-13 19:59:55 +00:00
|
|
|
return nil, fmt.Errorf("unable to load ttf font '%s'", path)
|
2017-10-23 09:18:48 +00:00
|
|
|
}
|
2018-08-07 04:59:52 +00:00
|
|
|
return &Font{f, 0, 0, 0}, nil
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Font) drawFlags(a HorizontalAlignment) C.int {
|
2017-10-23 09:18:48 +00:00
|
|
|
switch a {
|
|
|
|
case AlignLeft:
|
|
|
|
return C.ALLEGRO_ALIGN_LEFT
|
|
|
|
case AlignCenter:
|
|
|
|
return C.ALLEGRO_ALIGN_CENTRE
|
|
|
|
case AlignRight:
|
|
|
|
return C.ALLEGRO_ALIGN_RIGHT
|
|
|
|
}
|
|
|
|
return C.ALLEGRO_ALIGN_LEFT
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Font) Draw(left, top float32, color Color, align HorizontalAlignment, text string) {
|
2017-10-23 09:18:48 +00:00
|
|
|
t := C.CString(text)
|
|
|
|
defer C.free(unsafe.Pointer(t))
|
|
|
|
|
|
|
|
flags := f.drawFlags(align)
|
|
|
|
C.al_draw_text(f.font, color.color, C.float(left), C.float(top), flags, t)
|
|
|
|
}
|
|
|
|
|
2018-08-07 04:59:52 +00:00
|
|
|
// Ascent returns the ascent of the font
|
|
|
|
func (f *Font) Ascent() float32 {
|
|
|
|
if 0 == f.asc {
|
|
|
|
f.asc = float32(C.al_get_font_ascent(f.font))
|
|
|
|
}
|
|
|
|
return f.asc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Descent returns the descent of the font.
|
|
|
|
func (f *Font) Descent() float32 {
|
|
|
|
if 0 == f.desc {
|
|
|
|
f.desc = float32(C.al_get_font_descent(f.font))
|
|
|
|
}
|
|
|
|
return f.desc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Height returns the height of the font
|
|
|
|
func (f *Font) Height() float32 {
|
|
|
|
if 0 == f.hght {
|
|
|
|
f.hght = f.Ascent() + f.Descent()
|
|
|
|
}
|
|
|
|
return f.hght
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextDimensions returns the bounding box of the rendered text.
|
|
|
|
func (f *Font) TextDimensions(text string) (x, y, w, h float32) {
|
|
|
|
t := C.CString(text)
|
|
|
|
defer C.free(unsafe.Pointer(t))
|
|
|
|
|
|
|
|
var bbx, bby, bbw, bbh C.int
|
|
|
|
C.al_get_text_dimensions(f.font, t, &bbx, &bby, &bbw, &bbh)
|
|
|
|
x = float32(bbx)
|
|
|
|
y = float32(bby)
|
|
|
|
w = float32(bbw)
|
|
|
|
h = float32(bbh)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextWidth returns the width of the rendered text.
|
2017-10-23 09:18:48 +00:00
|
|
|
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))
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Font) Destroy() {
|
2017-10-23 09:18:48 +00:00
|
|
|
C.al_destroy_font(f.font)
|
|
|
|
}
|