From 3e4b61d8c944f1d44749e2c67bb902fe09b1aa56 Mon Sep 17 00:00:00 2001 From: Weilin Shi <934587911@qq.com> Date: Sun, 8 Oct 2017 10:25:22 +0800 Subject: [PATCH] add: FontScale option --- captcha.go | 14 +++++++++----- example/load-font/main.go | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/captcha.go b/captcha.go index 475d0cd..73ced5f 100644 --- a/captcha.go +++ b/captcha.go @@ -36,9 +36,12 @@ type Options struct { // CurveNumber is the number of curves to draw on captcha image. // It defaults to 2. CurveNumber int - // FontDPI controls scale of font. - // The default is 92.0. + // FontDPI controls DPI (dots per inch) of font. + // The default is 72.0. FontDPI float64 + // FontScale controls the scale of font. + // The default is 1.0. + FontScale float64 width int height int @@ -50,7 +53,8 @@ func newDefaultOption(width, height int) *Options { CharPreset: charPreset, TextLength: 4, CurveNumber: 2, - FontDPI: 92.0, + FontDPI: 72.0, + FontScale: 1.0, width: width, height: height, } @@ -198,8 +202,8 @@ func drawText(text string, img *image.NRGBA, opts *Options) error { fontOffset := rng.Intn(fontSpacing / 2) for idx, char := range text { - fontScale := 1 + rng.Float64()*0.5 - fontSize := float64(opts.height) / fontScale + fontScale := 0.8 + rng.Float64()*0.4 + fontSize := float64(opts.height) / fontScale * opts.FontScale ctx.SetFontSize(fontSize) ctx.SetSrc(image.NewUniform(randomInvertColor(opts.BackgroundColor))) x := fontSpacing*idx + fontOffset diff --git a/example/load-font/main.go b/example/load-font/main.go index 40c1d8e..c56af48 100644 --- a/example/load-font/main.go +++ b/example/load-font/main.go @@ -34,7 +34,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.FontDPI = 72.0 + options.FontScale = 0.8 }) if err != nil { fmt.Fprint(w, nil) @@ -42,4 +42,4 @@ func captchaHandle(w http.ResponseWriter, _ *http.Request) { return } img.WriteTo(w) -} \ No newline at end of file +}