add: LoadFont API
This commit is contained in:
parent
29b89c3e2a
commit
21cc8dcbcb
|
@ -78,6 +78,13 @@ func init() {
|
|||
}
|
||||
}
|
||||
|
||||
// LoadFont let you load an external font
|
||||
func LoadFont(fontData []byte) error {
|
||||
var err error
|
||||
ttfFont, err = freetype.ParseFont(fontData)
|
||||
return err
|
||||
}
|
||||
|
||||
// New creates a new captcha.
|
||||
// It returns captcha data and any freetype drawing error encountered
|
||||
func New(width int, height int, option ...SetOption) (*Data, error) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package captcha
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
"image/color"
|
||||
"testing"
|
||||
)
|
||||
|
@ -36,3 +37,15 @@ func TestCovNilFontError(t *testing.T) {
|
|||
|
||||
ttfFont = temp
|
||||
}
|
||||
|
||||
func TestLoadFont(t *testing.T) {
|
||||
err := LoadFont(goregular.TTF)
|
||||
if err != nil {
|
||||
t.Fatal("Fail to load go font")
|
||||
}
|
||||
|
||||
err = LoadFont([]byte("invalid"))
|
||||
if err == nil {
|
||||
t.Fatal("LoadFont incorrecly parse an invalid font")
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue