draw.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. size := line.Size()
  39. width := int(scale(size.Width + vectorPad*2))
  40. height := int(scale(size.Height + vectorPad*2))
  41. stroke := scale(line.StrokeWidth)
  42. if stroke < 1 { // software painter doesn't fade lines to compensate
  43. stroke = 1
  44. }
  45. raw := image.NewRGBA(image.Rect(0, 0, width, height))
  46. scanner := rasterx.NewScannerGV(int(size.Width), int(size.Height), raw, raw.Bounds())
  47. dasher := rasterx.NewDasher(width, height, scanner)
  48. dasher.SetColor(col)
  49. dasher.SetStroke(fixed.Int26_6(float64(stroke)*64), 0, nil, nil, nil, 0, nil, 0)
  50. positon := line.Position()
  51. p1x, p1y := scale(line.Position1.X-positon.X+vectorPad), scale(line.Position1.Y-positon.Y+vectorPad)
  52. p2x, p2y := scale(line.Position2.X-positon.X+vectorPad), scale(line.Position2.Y-positon.Y+vectorPad)
  53. if stroke <= 1.5 { // adjust to support 1px
  54. if p1x == p2x {
  55. p1x -= 0.5
  56. p2x -= 0.5
  57. }
  58. if p1y == p2y {
  59. p1y -= 0.5
  60. p2y -= 0.5
  61. }
  62. }
  63. dasher.Start(rasterx.ToFixedP(float64(p1x), float64(p1y)))
  64. dasher.Line(rasterx.ToFixedP(float64(p2x), float64(p2y)))
  65. dasher.Stop(true)
  66. dasher.Draw()
  67. return raw
  68. }
  69. // DrawRectangle rasterizes the given rectangle object with stroke border into an image.
  70. // The bounds of the output image will be increased by vectorPad to allow for stroke overflow at the edges.
  71. // The scale function is used to understand how many pixels are required per unit of size.
  72. func DrawRectangle(rect *canvas.Rectangle, vectorPad float32, scale func(float32) float32) *image.RGBA {
  73. size := rect.Size()
  74. width := int(scale(size.Width + vectorPad*2))
  75. height := int(scale(size.Height + vectorPad*2))
  76. stroke := scale(rect.StrokeWidth)
  77. raw := image.NewRGBA(image.Rect(0, 0, width, height))
  78. scanner := rasterx.NewScannerGV(int(size.Width), int(size.Height), raw, raw.Bounds())
  79. scaledPad := scale(vectorPad)
  80. p1x, p1y := scaledPad, scaledPad
  81. p2x, p2y := scale(size.Width)+scaledPad, scaledPad
  82. p3x, p3y := scale(size.Width)+scaledPad, scale(size.Height)+scaledPad
  83. p4x, p4y := scaledPad, scale(rect.Size().Height)+scaledPad
  84. if rect.FillColor != nil {
  85. filler := rasterx.NewFiller(width, height, scanner)
  86. filler.SetColor(rect.FillColor)
  87. if rect.CornerRadius == 0 {
  88. rasterx.AddRect(float64(p1x), float64(p1y), float64(p3x), float64(p3y), 0, filler)
  89. } else {
  90. r := float64(scale(rect.CornerRadius))
  91. rasterx.AddRoundRect(float64(p1x), float64(p1y), float64(p3x), float64(p3y), r, r, 0, rasterx.RoundGap, filler)
  92. }
  93. filler.Draw()
  94. }
  95. if rect.StrokeColor != nil && rect.StrokeWidth > 0 {
  96. r := scale(rect.CornerRadius)
  97. c := quarterCircleControl * r
  98. dasher := rasterx.NewDasher(width, height, scanner)
  99. dasher.SetColor(rect.StrokeColor)
  100. dasher.SetStroke(fixed.Int26_6(float64(stroke)*64), 0, nil, nil, nil, 0, nil, 0)
  101. if c != 0 {
  102. dasher.Start(rasterx.ToFixedP(float64(p1x), float64(p1y+r)))
  103. dasher.CubeBezier(rasterx.ToFixedP(float64(p1x), float64(p1y+c)), rasterx.ToFixedP(float64(p1x+c), float64(p1y)), rasterx.ToFixedP(float64(p1x+r), float64(p2y)))
  104. } else {
  105. dasher.Start(rasterx.ToFixedP(float64(p1x), float64(p1y)))
  106. }
  107. dasher.Line(rasterx.ToFixedP(float64(p2x-r), float64(p2y)))
  108. if c != 0 {
  109. dasher.CubeBezier(rasterx.ToFixedP(float64(p2x-c), float64(p2y)), rasterx.ToFixedP(float64(p2x), float64(p2y+c)), rasterx.ToFixedP(float64(p2x), float64(p2y+r)))
  110. }
  111. dasher.Line(rasterx.ToFixedP(float64(p3x), float64(p3y-r)))
  112. if c != 0 {
  113. dasher.CubeBezier(rasterx.ToFixedP(float64(p3x), float64(p3y-c)), rasterx.ToFixedP(float64(p3x-c), float64(p3y)), rasterx.ToFixedP(float64(p3x-r), float64(p3y)))
  114. }
  115. dasher.Line(rasterx.ToFixedP(float64(p4x+r), float64(p4y)))
  116. if c != 0 {
  117. dasher.CubeBezier(rasterx.ToFixedP(float64(p4x+c), float64(p4y)), rasterx.ToFixedP(float64(p4x), float64(p4y-c)), rasterx.ToFixedP(float64(p4x), float64(p4y-r)))
  118. }
  119. dasher.Stop(true)
  120. dasher.Draw()
  121. }
  122. return raw
  123. }