add: LoadFont API

This commit is contained in:
Weilin Shi 2017-09-22 10:01:40 +08:00
parent 29b89c3e2a
commit 21cc8dcbcb
2 changed files with 20 additions and 0 deletions

View File

@ -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. // New creates a new captcha.
// It returns captcha data and any freetype drawing error encountered // It returns captcha data and any freetype drawing error encountered
func New(width int, height int, option ...SetOption) (*Data, error) { func New(width int, height int, option ...SetOption) (*Data, error) {

View File

@ -2,6 +2,7 @@ package captcha
import ( import (
"bytes" "bytes"
"golang.org/x/image/font/gofont/goregular"
"image/color" "image/color"
"testing" "testing"
) )
@ -36,3 +37,15 @@ func TestCovNilFontError(t *testing.T) {
ttfFont = temp 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")
}
}