PathClipper.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGfx/Painter.h>
  8. #include <LibGfx/Path.h>
  9. namespace Gfx {
  10. struct ClipPath {
  11. Path path;
  12. Painter::WindingRule winding_rule;
  13. };
  14. class PathClipper {
  15. public:
  16. static ErrorOr<PathClipper> create(Painter&, ClipPath const& clip_path);
  17. ErrorOr<void> apply_clip(Painter& painter);
  18. private:
  19. PathClipper(RefPtr<Bitmap const> saved_clip_region, IntRect bounding_box, ClipPath const& clip_path)
  20. : m_saved_clip_region(saved_clip_region)
  21. , m_bounding_box(bounding_box)
  22. , m_clip_path(clip_path)
  23. {
  24. }
  25. RefPtr<Bitmap const> m_saved_clip_region;
  26. IntRect m_bounding_box;
  27. ClipPath const& m_clip_path;
  28. };
  29. class ScopedPathClip {
  30. AK_MAKE_NONMOVABLE(ScopedPathClip);
  31. AK_MAKE_NONCOPYABLE(ScopedPathClip);
  32. public:
  33. ScopedPathClip(Painter& painter, Optional<ClipPath> const& clip_path)
  34. : m_painter(painter)
  35. {
  36. if (clip_path.has_value()) {
  37. auto clipper = PathClipper::create(painter, *clip_path);
  38. if (!clipper.is_error())
  39. m_path_clipper = clipper.release_value();
  40. else
  41. dbgln("Error: Failed to apply clip path: {}", clipper.error());
  42. }
  43. }
  44. ~ScopedPathClip()
  45. {
  46. if (m_path_clipper.has_value())
  47. m_path_clipper->apply_clip(m_painter).release_value_but_fixme_should_propagate_errors();
  48. }
  49. private:
  50. Painter& m_painter;
  51. Optional<PathClipper> m_path_clipper;
  52. };
  53. }