add: FontScale option

This commit is contained in:
Weilin Shi 2017-10-08 10:25:22 +08:00
parent b25acadd0a
commit 3e4b61d8c9
2 changed files with 11 additions and 7 deletions

View File

@ -36,9 +36,12 @@ type Options struct {
// CurveNumber is the number of curves to draw on captcha image. // CurveNumber is the number of curves to draw on captcha image.
// It defaults to 2. // It defaults to 2.
CurveNumber int CurveNumber int
// FontDPI controls scale of font. // FontDPI controls DPI (dots per inch) of font.
// The default is 92.0. // The default is 72.0.
FontDPI float64 FontDPI float64
// FontScale controls the scale of font.
// The default is 1.0.
FontScale float64
width int width int
height int height int
@ -50,7 +53,8 @@ func newDefaultOption(width, height int) *Options {
CharPreset: charPreset, CharPreset: charPreset,
TextLength: 4, TextLength: 4,
CurveNumber: 2, CurveNumber: 2,
FontDPI: 92.0, FontDPI: 72.0,
FontScale: 1.0,
width: width, width: width,
height: height, height: height,
} }
@ -198,8 +202,8 @@ func drawText(text string, img *image.NRGBA, opts *Options) error {
fontOffset := rng.Intn(fontSpacing / 2) fontOffset := rng.Intn(fontSpacing / 2)
for idx, char := range text { for idx, char := range text {
fontScale := 1 + rng.Float64()*0.5 fontScale := 0.8 + rng.Float64()*0.4
fontSize := float64(opts.height) / fontScale fontSize := float64(opts.height) / fontScale * opts.FontScale
ctx.SetFontSize(fontSize) ctx.SetFontSize(fontSize)
ctx.SetSrc(image.NewUniform(randomInvertColor(opts.BackgroundColor))) ctx.SetSrc(image.NewUniform(randomInvertColor(opts.BackgroundColor)))
x := fontSpacing*idx + fontOffset x := fontSpacing*idx + fontOffset

View File

@ -34,7 +34,7 @@ func indexHandle(w http.ResponseWriter, _ *http.Request) {
func captchaHandle(w http.ResponseWriter, _ *http.Request) { func captchaHandle(w http.ResponseWriter, _ *http.Request) {
img, err := captcha.New(150, 50, func(options *captcha.Options) { img, err := captcha.New(150, 50, func(options *captcha.Options) {
options.FontDPI = 72.0 options.FontScale = 0.8
}) })
if err != nil { if err != nil {
fmt.Fprint(w, nil) fmt.Fprint(w, nil)
@ -42,4 +42,4 @@ func captchaHandle(w http.ResponseWriter, _ *http.Request) {
return return
} }
img.WriteTo(w) img.WriteTo(w)
} }