driver_string.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package base64Captcha
  2. import (
  3. "image/color"
  4. "strings"
  5. "github.com/golang/freetype/truetype"
  6. )
  7. //DriverChar captcha config for captcha-engine-characters.
  8. type DriverString struct {
  9. // Height png height in pixel.
  10. Height int
  11. // Width Captcha png width in pixel.
  12. Width int
  13. //NoiseCount text noise count.
  14. NoiseCount int
  15. //ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
  16. ShowLineOptions int
  17. //Length random string length.
  18. Length int
  19. //Source is a unicode which is the rand string from.
  20. Source string
  21. //BgColor captcha image background color (optional)
  22. BgColor *color.RGBA
  23. //fontsStorage font storage (optional)
  24. fontsStorage FontsStorage
  25. //Fonts loads by name see fonts.go's comment
  26. Fonts []string
  27. fontsArray []*truetype.Font
  28. }
  29. //NewDriverString creates driver
  30. func NewDriverString(height int, width int, noiseCount int, showLineOptions int, length int, source string, bgColor *color.RGBA, fontsStorage FontsStorage, fonts []string) *DriverString {
  31. if fontsStorage == nil {
  32. fontsStorage = DefaultEmbeddedFonts
  33. }
  34. tfs := []*truetype.Font{}
  35. for _, fff := range fonts {
  36. tf := fontsStorage.LoadFontByName("fonts/" + fff)
  37. tfs = append(tfs, tf)
  38. }
  39. if len(tfs) == 0 {
  40. tfs = fontsAll
  41. }
  42. return &DriverString{Height: height, Width: width, NoiseCount: noiseCount, ShowLineOptions: showLineOptions, Length: length, Source: source, BgColor: bgColor, fontsStorage: fontsStorage, fontsArray: tfs}
  43. }
  44. //ConvertFonts loads fonts by names
  45. func (d *DriverString) ConvertFonts() *DriverString {
  46. if d.fontsStorage == nil {
  47. d.fontsStorage = DefaultEmbeddedFonts
  48. }
  49. tfs := []*truetype.Font{}
  50. for _, fff := range d.Fonts {
  51. tf := d.fontsStorage.LoadFontByName("fonts/" + fff)
  52. tfs = append(tfs, tf)
  53. }
  54. if len(tfs) == 0 {
  55. tfs = fontsAll
  56. }
  57. d.fontsArray = tfs
  58. return d
  59. }
  60. //GenerateIdQuestionAnswer creates id,content and answer
  61. func (d *DriverString) GenerateIdQuestionAnswer() (id, content, answer string) {
  62. id = RandomId()
  63. content = RandText(d.Length, d.Source)
  64. return id, content, content
  65. }
  66. //DrawCaptcha draws captcha item
  67. func (d *DriverString) DrawCaptcha(content string) (item Item, err error) {
  68. var bgc color.RGBA
  69. if d.BgColor != nil {
  70. bgc = *d.BgColor
  71. } else {
  72. bgc = RandLightColor()
  73. }
  74. itemChar := NewItemChar(d.Width, d.Height, bgc)
  75. //draw hollow line
  76. if d.ShowLineOptions&OptionShowHollowLine == OptionShowHollowLine {
  77. itemChar.drawHollowLine()
  78. }
  79. //draw slime line
  80. if d.ShowLineOptions&OptionShowSlimeLine == OptionShowSlimeLine {
  81. itemChar.drawSlimLine(3)
  82. }
  83. //draw sine line
  84. if d.ShowLineOptions&OptionShowSineLine == OptionShowSineLine {
  85. itemChar.drawSineLine()
  86. }
  87. //draw noise
  88. if d.NoiseCount > 0 {
  89. source := TxtNumbers + TxtAlphabet + ",.[]<>"
  90. noise := RandText(d.NoiseCount, strings.Repeat(source, d.NoiseCount))
  91. err = itemChar.drawNoise(noise, d.fontsArray)
  92. if err != nil {
  93. return
  94. }
  95. }
  96. //draw content
  97. err = itemChar.drawText(content, d.fontsArray)
  98. if err != nil {
  99. return
  100. }
  101. return itemChar, nil
  102. }