draw.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package painter
  2. import (
  3. "image"
  4. "fyne.io/fyne/v2"
  5. "fyne.io/fyne/v2/canvas"
  6. "github.com/srwiley/rasterx"
  7. "golang.org/x/image/math/fixed"
  8. )
  9. const quarterCircleControl = 1 - 0.55228
  10. // DrawCircle rasterizes the given circle object into an image.
  11. // The bounds of the output image will be increased by vectorPad to allow for stroke overflow at the edges.
  12. // The scale function is used to understand how many pixels are required per unit of size.
  13. func DrawCircle(circle *canvas.Circle, vectorPad float32, scale func(float32) float32) *image.RGBA {
  14. radius := fyne.Min(circle.Size().Width, circle.Size().Height) / 2
  15. width := int(scale(circle.Size().Width + vectorPad*2))
  16. height := int(scale(circle.Size().Height + vectorPad*2))
  17. stroke := scale(circle.StrokeWidth)
  18. raw := image.NewRGBA(image.Rect(0, 0, width, height))
  19. scanner := rasterx.NewScannerGV(int(circle.Size().Width), int(circle.Size().Height), raw, raw.Bounds())
  20. if circle.FillColor != nil {
  21. filler := rasterx.NewFiller(width, height, scanner)
  22. filler.SetColor(circle.FillColor)
  23. rasterx.AddCircle(float64(width/2), float64(height/2), float64(scale(radius)), filler)
  24. filler.Draw()
  25. }
  26. dasher := rasterx.NewDasher(width, height, scanner)
  27. dasher.SetColor(circle.StrokeColor)
  28. dasher.SetStroke(fixed.Int26_6(float64(stroke)*64), 0, nil, nil, nil, 0, nil, 0)
  29. rasterx.AddCircle(float64(width/2), float64(height/2), float64(scale(radius)), dasher)
  30. dasher.Draw()
  31. return raw
  32. }
  33. // DrawLine rasterizes the given line object into an image.
  34. // The bounds of the output image will be increased by vectorPad to allow for stroke overflow at the edges.
  35. // The scale function is used to understand how many pixels are required per unit of size.
  36. func DrawLine(line *canvas.Line, vectorPad float32, scale func(float32) float32) *image.RGBA {
  37. col := line.StrokeColor
  38. width := int(scale(line.Size().Width + vectorPad*2))
  39. height := int(scale(line.Size().Height + vectorPad*2))
  40. stroke := scale(line.StrokeWidth)
  41. if stroke < 1 { // software painter doesn't fade lines to compensate
  42. stroke = 1
  43. }
  44. raw := image.NewRGBA(image.Rect(0, 0, width, height))
  45. scanner := rasterx.NewScannerGV(int(line.Size().Width), int(line.Size().Height), raw, raw.Bounds())
  46. dasher := rasterx.NewDasher(width, height, scanner)
  47. dasher.SetColor(col)
  48. dasher.SetStroke(fixed.Int26_6(float64(stroke)*64), 0, nil, nil, nil, 0, nil, 0)
  49. p1x, p1y := scale(line.Position1.X-line.Position().X+vectorPad), scale(line.Position1.Y-line.Position().Y+vectorPad)
  50. p2x, p2y := scale(line.Position2.X-line.Position().X+vectorPad), scale(line.Position2.Y-line.Position().Y+vectorPad)
  51. if stroke <= 1.5 { // adjust to support 1px
  52. if p1x == p2x {
  53. p1x -= 0.5
  54. p2x -= 0.5
  55. }
  56. if p1y == p2y {
  57. p1y -= 0.5
  58. p2y -= 0.5
  59. }
  60. }
  61. dasher.Start(rasterx.ToFixedP(float64(p1x), float64(p1y)))
  62. dasher.Line(rasterx.ToFixedP(float64(p2x), float64(p2y)))
  63. dasher.Stop(true)
  64. dasher.Draw()
  65. return raw
  66. }
  67. // DrawRectangle rasterizes the given rectangle object with stroke border into an image.
  68. // The bounds of the output image will be increased by vectorPad to allow for stroke overflow at the edges.
  69. // The scale function is used to understand how many pixels are required per unit of size.
  70. func DrawRectangle(rect *canvas.Rectangle, vectorPad float32, scale func(float32) float32) *image.RGBA {
  71. width := int(scale(rect.Size().Width + vectorPad*2))
  72. height := int(scale(rect.Size().Height + vectorPad*2))
  73. stroke := scale(rect.StrokeWidth)
  74. raw := image.NewRGBA(image.Rect(0, 0, width, height))
  75. scanner := rasterx.NewScannerGV(int(rect.Size().Width), int(rect.Size().Height), raw, raw.Bounds())
  76. scaledPad := scale(vectorPad)
  77. p1x, p1y := scaledPad, scaledPad
  78. p2x, p2y := scale(rect.Size().Width)+scaledPad, scaledPad
  79. p3x, p3y := scale(rect.Size().Width)+scaledPad, scale(rect.Size().Height)+scaledPad
  80. p4x, p4y := scaledPad, scale(rect.Size().Height)+scaledPad
  81. if rect.FillColor != nil {
  82. filler := rasterx.NewFiller(width, height, scanner)
  83. filler.SetColor(rect.FillColor)
  84. if rect.CornerRadius == 0 {
  85. rasterx.AddRect(float64(p1x), float64(p1y), float64(p3x), float64(p3y), 0, filler)
  86. } else {
  87. r := float64(scale(rect.CornerRadius))
  88. rasterx.AddRoundRect(float64(p1x), float64(p1y), float64(p3x), float64(p3y), r, r, 0, rasterx.RoundGap, filler)
  89. }
  90. filler.Draw()
  91. }
  92. if rect.StrokeColor != nil && rect.StrokeWidth > 0 {
  93. r := scale(rect.CornerRadius)
  94. c := quarterCircleControl * r
  95. dasher := rasterx.NewDasher(width, height, scanner)
  96. dasher.SetColor(rect.StrokeColor)
  97. dasher.SetStroke(fixed.Int26_6(float64(stroke)*64), 0, nil, nil, nil, 0, nil, 0)
  98. if c != 0 {
  99. dasher.Start(rasterx.ToFixedP(float64(p1x), float64(p1y+r)))
  100. dasher.CubeBezier(rasterx.ToFixedP(float64(p1x), float64(p1y+c)), rasterx.ToFixedP(float64(p1x+c), float64(p1y)), rasterx.ToFixedP(float64(p1x+r), float64(p2y)))
  101. } else {
  102. dasher.Start(rasterx.ToFixedP(float64(p1x), float64(p1y)))
  103. }
  104. dasher.Line(rasterx.ToFixedP(float64(p2x-r), float64(p2y)))
  105. if c != 0 {
  106. dasher.CubeBezier(rasterx.ToFixedP(float64(p2x-c), float64(p2y)), rasterx.ToFixedP(float64(p2x), float64(p2y+c)), rasterx.ToFixedP(float64(p2x), float64(p2y+r)))
  107. }
  108. dasher.Line(rasterx.ToFixedP(float64(p3x), float64(p3y-r)))
  109. if c != 0 {
  110. dasher.CubeBezier(rasterx.ToFixedP(float64(p3x), float64(p3y-c)), rasterx.ToFixedP(float64(p3x-c), float64(p3y)), rasterx.ToFixedP(float64(p3x-r), float64(p3y)))
  111. }
  112. dasher.Line(rasterx.ToFixedP(float64(p4x+r), float64(p4y)))
  113. if c != 0 {
  114. dasher.CubeBezier(rasterx.ToFixedP(float64(p4x+c), float64(p4y)), rasterx.ToFixedP(float64(p4x), float64(p4y-c)), rasterx.ToFixedP(float64(p4x), float64(p4y-r)))
  115. }
  116. dasher.Stop(true)
  117. dasher.Draw()
  118. }
  119. return raw
  120. }