add: godoc comments
This commit is contained in:
parent
abf1a76526
commit
8cc4044e70
41
captcha.go
41
captcha.go
|
@ -1,3 +1,4 @@
|
||||||
|
// Package captcha provides a simple API for captcha generation
|
||||||
package captcha
|
package captcha
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -20,32 +21,52 @@ const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
|
||||||
var rng = rand.New(rand.NewSource(time.Now().UnixNano()))
|
var rng = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
var ttfFont *truetype.Font
|
var ttfFont *truetype.Font
|
||||||
|
|
||||||
|
// Options manage captcha generation details.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
|
// BackgroundColor is captcha image's background color.
|
||||||
|
// It defaults to color.Transparent.
|
||||||
BackgroundColor color.Color
|
BackgroundColor color.Color
|
||||||
CharPreset string
|
// CharPreset decides what text will be on captcha image.
|
||||||
TxtLength int
|
// It defaults to digit 0-9 and all English alphabet.
|
||||||
width int
|
// ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
|
||||||
height int
|
CharPreset string
|
||||||
|
// TextLength is the length of captcha text.
|
||||||
|
// It defaults to 4.
|
||||||
|
TextLength int
|
||||||
|
// CurveNumber is the number of curves to draw on captcha image.
|
||||||
|
// It defaults to 3.
|
||||||
|
CurveNumber int
|
||||||
|
|
||||||
|
width int
|
||||||
|
height int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDefaultOption(width, height int) *Options {
|
func newDefaultOption(width, height int) *Options {
|
||||||
return &Options{
|
return &Options{
|
||||||
BackgroundColor: color.Transparent,
|
BackgroundColor: color.Transparent,
|
||||||
CharPreset: charPreset,
|
CharPreset: charPreset,
|
||||||
TxtLength: 4,
|
TextLength: 4,
|
||||||
|
CurveNumber: 3,
|
||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetOption is a function that can be used to modify default options.
|
||||||
type SetOption func(*Options)
|
type SetOption func(*Options)
|
||||||
|
|
||||||
|
// Data is the result of captcha generation.
|
||||||
|
// It has a `Text` field and a private `img` field that will
|
||||||
|
// be used in `WriteTo` receiver
|
||||||
type Data struct {
|
type Data struct {
|
||||||
|
// Text is captcha solution
|
||||||
Text string
|
Text string
|
||||||
|
|
||||||
img *image.NRGBA
|
img *image.NRGBA
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteTo encodes image data and writes to an io.Writer.
|
||||||
|
// It returns possible error from PNG encoding
|
||||||
func (data *Data) WriteTo(w io.Writer) error {
|
func (data *Data) WriteTo(w io.Writer) error {
|
||||||
return png.Encode(w, data.img)
|
return png.Encode(w, data.img)
|
||||||
}
|
}
|
||||||
|
@ -58,6 +79,8 @@ func init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) {
|
func New(width int, height int, option ...SetOption) (*Data, error) {
|
||||||
options := newDefaultOption(width, height)
|
options := newDefaultOption(width, height)
|
||||||
for _, setOption := range option {
|
for _, setOption := range option {
|
||||||
|
@ -68,7 +91,7 @@ func New(width int, height int, option ...SetOption) (*Data, error) {
|
||||||
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||||
draw.Draw(img, img.Bounds(), &image.Uniform{options.BackgroundColor}, image.ZP, draw.Src)
|
draw.Draw(img, img.Bounds(), &image.Uniform{options.BackgroundColor}, image.ZP, draw.Src)
|
||||||
drawNoise(img, options)
|
drawNoise(img, options)
|
||||||
drawLine(img, options)
|
drawCurves(img, options)
|
||||||
err := drawText(text, img, options)
|
err := drawText(text, img, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -79,7 +102,7 @@ func New(width int, height int, option ...SetOption) (*Data, error) {
|
||||||
|
|
||||||
func randomText(opts *Options) (text string) {
|
func randomText(opts *Options) (text string) {
|
||||||
n := len(opts.CharPreset)
|
n := len(opts.CharPreset)
|
||||||
for i := 0; i < opts.TxtLength; i++ {
|
for i := 0; i < opts.TextLength; i++ {
|
||||||
text += string(opts.CharPreset[rng.Intn(n)])
|
text += string(opts.CharPreset[rng.Intn(n)])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,8 +126,8 @@ func randomColor() color.RGBA {
|
||||||
return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
|
return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawLine(img *image.NRGBA, opts *Options) {
|
func drawCurves(img *image.NRGBA, opts *Options) {
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < opts.CurveNumber; i++ {
|
||||||
drawSineCurve(img, opts)
|
drawSineCurve(img, opts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue