From c6bde002a6160523a509f42592aeecb7d6ff9469 Mon Sep 17 00:00:00 2001 From: s3rj1k Date: Fri, 10 May 2019 15:05:17 +0300 Subject: [PATCH] hide ttfFont from globalvars, remove Init func Signed-off-by: s3rj1k --- captcha.go | 34 +++++++++++++++++++--------------- captcha_test.go | 27 +++++++-------------------- example/load-font/main.go | 8 +++++++- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/captcha.go b/captcha.go index 23f6892..2f31149 100644 --- a/captcha.go +++ b/captcha.go @@ -22,10 +22,6 @@ import ( const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" -var ( - ttfFont *truetype.Font -) - // Options manage captcha generation details. type Options struct { // BackgroundColor is captcha image's background color. @@ -53,6 +49,8 @@ type Options struct { Noise float64 // Palette is the set of colors to chose from Palette color.Palette + // TTFFont defines font to use for captcha text. + TTFFont *truetype.Font width int height int @@ -79,6 +77,8 @@ func newDefaultOption(width, height int) *Options { time.Now().UnixNano(), ), ), + + TTFFont: MustLoadFont(ttf), } } @@ -113,23 +113,27 @@ func (data *Data) WriteGIF(w io.Writer, o *gif.Options) error { return gif.Encode(w, data.img, o) } -// nolint: gochecknoinits -func init() { - ttfFont, _ = freetype.ParseFont(ttf) +// LoadFont let you load an external font. +func LoadFont(fontData []byte) (*truetype.Font, error) { + return freetype.ParseFont(fontData) } -// LoadFont let you load an external font. -func LoadFont(fontData []byte) error { - var err error - ttfFont, err = freetype.ParseFont(fontData) - return err +// MustLoadFont let you load an external font. +// Function assumes that font is valid. +func MustLoadFont(fontData []byte) *truetype.Font { + ttfFont, err := LoadFont(fontData) + if err != nil { + return nil + } + + return ttfFont } // LoadFontFromReader load an external font from an io.Reader interface. -func LoadFontFromReader(reader io.Reader) error { +func LoadFontFromReader(reader io.Reader) (*truetype.Font, error) { var buf bytes.Buffer if _, err := io.Copy(&buf, reader); err != nil { - return err + return nil, err } return LoadFont(buf.Bytes()) @@ -240,7 +244,7 @@ func drawText(text string, img *image.NRGBA, opts *Options) error { // nolint: i ctx.SetClip(img.Bounds()) ctx.SetDst(img) ctx.SetHinting(font.HintingFull) - ctx.SetFont(ttfFont) + ctx.SetFont(opts.TTFFont) fontSpacing := opts.width / len(text) fontOffset := opts.rng.Intn(fontSpacing / 2) diff --git a/captcha_test.go b/captcha_test.go index 74fc4cb..3fb2a9f 100644 --- a/captcha_test.go +++ b/captcha_test.go @@ -87,21 +87,10 @@ func TestNewMathExpr(t *testing.T) { } } -func TestCovNilFontError(t *testing.T) { - temp := ttfFont - ttfFont = nil - - _, err := New(150, 50) - if err == nil { - t.Fatal("Expect to get nil font error") +func TestCovInternalFontErr(t *testing.T) { + if ttfFont := MustLoadFont(ttf); ttfFont == nil { + t.Fatal("Fail to load internal font") } - - _, err = NewMathExpr(150, 50) - if err == nil { - t.Fatal("Expect to get nil font error") - } - - ttfFont = temp } type errReader struct{} @@ -111,20 +100,18 @@ func (errReader) Read(_ []byte) (int, error) { } func TestCovReaderErr(t *testing.T) { - err := LoadFontFromReader(errReader{}) + _, err := LoadFontFromReader(errReader{}) if err == nil { t.Fatal("Expect to get io.Reader error") } } func TestLoadFont(t *testing.T) { - err := LoadFont(goregular.TTF) - if err != nil { + if _, err := LoadFont(goregular.TTF); err != nil { t.Fatal("Fail to load go font") } - err = LoadFont([]byte("invalid")) - if err == nil { + if _, err := LoadFont([]byte("invalid")); err == nil { t.Fatal("LoadFont incorrectly parse an invalid font") } } @@ -135,7 +122,7 @@ func TestLoadFontFromReader(t *testing.T) { t.Fatal("Fail to load test file") } - if err = LoadFontFromReader(file); err != nil { + if _, err := LoadFontFromReader(file); err != nil { t.Fatal("Fail to load font from io.Reader") } } diff --git a/example/load-font/main.go b/example/load-font/main.go index cf4113c..3e97d6a 100644 --- a/example/load-font/main.go +++ b/example/load-font/main.go @@ -7,10 +7,15 @@ import ( "github.com/steambap/captcha" "golang.org/x/image/font/gofont/goregular" + "github.com/golang/freetype/truetype" ) +var ttf *truetype.Font + func main() { - err := captcha.LoadFont(goregular.TTF) + var err error + + ttf, err = captcha.LoadFont(goregular.TTF) if err != nil { panic(err) } @@ -44,6 +49,7 @@ func indexHandle(w http.ResponseWriter, _ *http.Request) { func captchaHandle(w http.ResponseWriter, _ *http.Request) { img, err := captcha.New(150, 50, func(options *captcha.Options) { options.FontScale = 0.8 + options.TTFFont = ttf }) if err != nil { fmt.Fprint(w, nil)