painter.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package software
  2. import (
  3. "image"
  4. "fyne.io/fyne/v2"
  5. "fyne.io/fyne/v2/canvas"
  6. "fyne.io/fyne/v2/internal/driver"
  7. "fyne.io/fyne/v2/internal/scale"
  8. )
  9. // Painter is a simple software painter that can paint a canvas in memory.
  10. type Painter struct {
  11. }
  12. // NewPainter creates a new Painter.
  13. func NewPainter() *Painter {
  14. return &Painter{}
  15. }
  16. // Paint is the main entry point for a simple software painter.
  17. // The canvas to be drawn is passed in as a parameter and the return is an
  18. // image containing the result of rendering.
  19. func (*Painter) Paint(c fyne.Canvas) image.Image {
  20. bounds := image.Rect(0, 0, scale.ToScreenCoordinate(c, c.Size().Width), scale.ToScreenCoordinate(c, c.Size().Height))
  21. base := image.NewNRGBA(bounds)
  22. paint := func(obj fyne.CanvasObject, pos, clipPos fyne.Position, clipSize fyne.Size) bool {
  23. w := fyne.Min(clipPos.X+clipSize.Width, c.Size().Width)
  24. h := fyne.Min(clipPos.Y+clipSize.Height, c.Size().Height)
  25. clip := image.Rect(
  26. scale.ToScreenCoordinate(c, clipPos.X),
  27. scale.ToScreenCoordinate(c, clipPos.Y),
  28. scale.ToScreenCoordinate(c, w),
  29. scale.ToScreenCoordinate(c, h),
  30. )
  31. switch o := obj.(type) {
  32. case *canvas.Image:
  33. drawImage(c, o, pos, base, clip)
  34. case *canvas.Text:
  35. drawText(c, o, pos, base, clip)
  36. case gradient:
  37. drawGradient(c, o, pos, base, clip)
  38. case *canvas.Circle:
  39. drawCircle(c, o, pos, base, clip)
  40. case *canvas.Line:
  41. drawLine(c, o, pos, base, clip)
  42. case *canvas.Raster:
  43. drawRaster(c, o, pos, base, clip)
  44. case *canvas.Rectangle:
  45. drawRectangle(c, o, pos, base, clip)
  46. }
  47. return false
  48. }
  49. driver.WalkVisibleObjectTree(c.Content(), paint, nil)
  50. for _, o := range c.Overlays().List() {
  51. driver.WalkVisibleObjectTree(o, paint, nil)
  52. }
  53. return base
  54. }