Skip to content

Commit

Permalink
Merge pull request #9 from reu98/feat/document
Browse files Browse the repository at this point in the history
feat: add document
  • Loading branch information
reu98 authored Dec 21, 2023
2 parents 3dfd738 + 5951ec8 commit 146b60c
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 74 deletions.
40 changes: 18 additions & 22 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: go-svg-captcha-ci
name: svg-captcha

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set up Golang
uses: actions/setup-go@v3
with:
go-version: '^1.21.4'
- name: Set up Golang
uses: actions/setup-go@v3
with:
go-version: '^1.21.4'

- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Install dependencies
run: go mod tidy
- name: Install dependencies
run: go mod tidy

- name: Build
run: go build -v ./...
- name: Build
run: go build -v ./...

- name: Test
run: go test -race -coverprofile=coverage.txt -covermode=atomic
- name: Test
run: go test -race -coverprofile=coverage.txt -covermode=atomic

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@
## [1.0.4] - 2023-12-21

- **Release package**

## [1.0.5] - 2023-12-22

### Fix:

- Remove the custom text option
- Remove size option in math

### Add:

- Document package
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ see [contributing.md](CONTRIBUTING.md) for more detail.

## Roadmap

- [ ] Finish [Documentation](https://pkg.go.dev/github.com/reu98/go-svg-captcha)
- [x] Finish [Documentation](https://pkg.go.dev/github.com/reu98/go-svg-captcha)
- [ ] Generate multiple captchas
- [ ] Write image
- [ ] Upload the image to the cloud
Expand Down
19 changes: 8 additions & 11 deletions captcha.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
// Package captcha provides an easy to use
package captcha

import "fmt"

type Captcha interface {
CreateByText(*OptionText) Result
CreateByMath(*OptionMath) Result
}

type Result struct {
// A random string or the result of an operation.
Text string

// The HTML code snippet for SVG.
Data string
}

// CreateByText: generate a new captcha
func CreateByText(option OptionText) (*Result, error) {
var text string
opt := getOptionByText(option)
if opt.Text != nil {
text = *opt.Text
} else {
text = opt.randomText()
}
text := opt.randomText()

data, err := createCaptcha(text, opt)
if err != nil {
Expand All @@ -32,6 +27,8 @@ func CreateByText(option OptionText) (*Result, error) {
}, err
}

// CreateByMath: Generate a new captcha.
// It will return a captcha with an operation like 1 + 1.
func CreateByMath(option OptionMath) (*Result, error) {
opt := getOptionByMath(option)
min := mathMinDefault
Expand Down
15 changes: 0 additions & 15 deletions captcha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ func TestCreateByText(t *testing.T) {
require.NotEmpty(t, (*data).Data)
}

func TestCreateByTextByOption(t *testing.T) {
var dataType Result
text := "ABCD"
option := OptionText{
Text: &text,
}
data, err := CreateByText(option)

require.NoError(t, err)
require.NotNil(t, data)
require.IsType(t, dataType, *data)
require.Len(t, (*data).Text, int(sizeDefault))
require.NotEmpty(t, (*data).Data)
}

func TestCreateByMath(t *testing.T) {
var dataType Result
maxMath := 18
Expand Down
3 changes: 2 additions & 1 deletion constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const (
heightDefault uint16 = 50
noiseDefault uint8 = 1
sizeDefault uint8 = 4
fontSizeDefault uint8 = 60
fontSizeDefault uint8 = 12
ratioFontSize uint8 = 4
noiseGreyColorMinDefault uint8 = 1
noiseGreyColorMaxDefault uint8 = 9
noiseGreyColorMinInverse uint8 = 7
Expand Down
2 changes: 1 addition & 1 deletion draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (opt *option) drawChar(char rune, x float32, y float32) (string, error) {
return "", err
}

fontSize := fontSizeDefault
fontSize := fontSizeDefault * ratioFontSize
if opt.FontSize != nil && *opt.FontSize > 0 {
fontSize = *opt.FontSize
}
Expand Down
99 changes: 77 additions & 22 deletions option.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,89 @@
package captcha

type OptionText struct {
Size *uint8
Width *uint16
Height *uint16
FontSize *uint8
IsColor *bool
IsInverse *bool
Noise *uint8
BackgroundColor *string
// The length of the random string
// Default: 4
Size *uint8

// The width of the SVG captcha
// Default: 150
Width *uint16

// The height of the SVG captcha
// Default: 50
Height *uint16

// The font size in the captcha
// Default: 12
FontSize *uint8

// The color of the characters in the captcha.
// True: the characters will have individual colors.
// False: The characters will be gray
// Default: false
IsColor *bool

// Invert the colors.
// Default: false
IsInverse *bool

// The number of lines in the captcha
// Default: 1
Noise *uint8

// Background color of the SVG captcha
BackgroundColor *string

// Remove unacceptable characters from the captcha.
IgnoreCharacters *string
Text *string

// The characters that can be displayed in the captcha.
// Default: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
CharactersPreset *string
}

type OptionMath struct {
Size *uint8
Width *uint16
Height *uint16
FontSize *uint8
IsColor *bool
IsInverse *bool
Noise *uint8
// The width of the SVG captcha
// Default: 150
Width *uint16

// The height of the SVG captcha
// Default: 50
Height *uint16

// The font size in the captcha
// Default: 12
FontSize *uint8

// The color of the characters in the captcha.
// True: the characters will have individual colors.
// False: The characters will be gray
// Default: false
IsColor *bool

// Invert the colors.
// Default: false
IsInverse *bool

// The number of lines in the captcha
// Default: 1
Noise *uint8

// Background color of the SVG captcha
BackgroundColor *string
MathOperator *matchOperator
MathMin *uint8
MathMax *uint16

// The operation for the mathematical calculation
// Supports addition (+) and subtraction (-) operations
// If there's no specified operation, it will automatically choose one.
MathOperator *matchOperator

// The minimum value for a number in the operation
// Default: 1
MathMin *uint8

// The maximum value for a number in the operation.
// Default: 9
MathMax *uint16
}

type option struct {
Expand All @@ -41,7 +99,6 @@ type option struct {
MathMin *uint8
MathMax *uint16
IgnoreCharacters *string
Text *string
CharactersPreset *string
}

Expand All @@ -55,15 +112,13 @@ func getOptionByText(opt OptionText) *option {
IsInverse: opt.IsInverse,
Noise: opt.Noise,
BackgroundColor: opt.BackgroundColor,
Text: opt.Text,
IgnoreCharacters: opt.IgnoreCharacters,
CharactersPreset: opt.CharactersPreset,
}
}

func getOptionByMath(opt OptionMath) *option {
return &option{
Size: opt.Size,
Width: opt.Width,
Height: opt.Height,
FontSize: opt.FontSize,
Expand Down
1 change: 0 additions & 1 deletion option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func TestGetOptionByText(t *testing.T) {
require.Nil(t, data.IsInverse)
require.Nil(t, data.Noise)
require.Nil(t, data.BackgroundColor)
require.Nil(t, data.Text)
require.Nil(t, data.IgnoreCharacters)
require.Nil(t, data.CharactersPreset)
}
Expand Down

0 comments on commit 146b60c

Please sign in to comment.