PathClipper.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/AntiAliasingPainter.h>
  7. #include <LibGfx/PathClipper.h>
  8. namespace Gfx {
  9. // FIXME: This pretty naive, we should be able to cut down the allocations here
  10. // (especially for the paint style which is a bit sad).
  11. ErrorOr<PathClipper> PathClipper::create(Painter& painter, ClipPath const& clip_path)
  12. {
  13. auto bounding_box = enclosing_int_rect(clip_path.path.bounding_box());
  14. IntRect actual_save_rect {};
  15. auto maybe_bitmap = painter.get_region_bitmap(bounding_box, BitmapFormat::BGRA8888, actual_save_rect);
  16. RefPtr<Bitmap> saved_clip_region;
  17. if (!maybe_bitmap.is_error()) {
  18. saved_clip_region = maybe_bitmap.release_value();
  19. } else if (actual_save_rect.is_empty()) {
  20. // This is okay, no need to report an error.
  21. } else {
  22. return maybe_bitmap.release_error();
  23. }
  24. painter.save();
  25. painter.add_clip_rect(bounding_box);
  26. return PathClipper(move(saved_clip_region), bounding_box, clip_path);
  27. }
  28. ErrorOr<void> PathClipper::apply_clip(Painter& painter)
  29. {
  30. painter.restore();
  31. if (!m_saved_clip_region)
  32. return {};
  33. IntRect actual_save_rect {};
  34. auto clip_area = TRY(painter.get_region_bitmap(m_bounding_box, BitmapFormat::BGRA8888, actual_save_rect));
  35. painter.blit(actual_save_rect.location(), *m_saved_clip_region, m_saved_clip_region->rect(), 1.0f, false);
  36. AntiAliasingPainter aa_painter { painter };
  37. auto fill_offset = m_bounding_box.location() - actual_save_rect.location();
  38. aa_painter.fill_path(m_clip_path.path, TRY(BitmapPaintStyle::create(clip_area, fill_offset)), 1.0f, m_clip_path.winding_rule);
  39. return {};
  40. }
  41. }