BorderPainting.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGfx/AntiAliasingPainter.h>
  8. #include <LibGfx/Forward.h>
  9. #include <LibWeb/CSS/ComputedValues.h>
  10. #include <LibWeb/Painting/PaintContext.h>
  11. namespace Web::Painting {
  12. struct BorderRadiusData {
  13. CSSPixels horizontal_radius { 0 };
  14. CSSPixels vertical_radius { 0 };
  15. Gfx::AntiAliasingPainter::CornerRadius as_corner(PaintContext& context) const
  16. {
  17. return Gfx::AntiAliasingPainter::CornerRadius {
  18. context.floored_device_pixels(horizontal_radius).value(),
  19. context.floored_device_pixels(vertical_radius).value()
  20. };
  21. };
  22. inline operator bool() const
  23. {
  24. return horizontal_radius > 0 && vertical_radius > 0;
  25. }
  26. inline void shrink(CSSPixels horizontal, CSSPixels vertical)
  27. {
  28. horizontal_radius = max(CSSPixels(0), horizontal_radius - horizontal);
  29. vertical_radius = max(CSSPixels(0), vertical_radius - vertical);
  30. }
  31. };
  32. struct BorderRadiiData {
  33. BorderRadiusData top_left;
  34. BorderRadiusData top_right;
  35. BorderRadiusData bottom_right;
  36. BorderRadiusData bottom_left;
  37. inline bool has_any_radius() const
  38. {
  39. return top_left || top_right || bottom_right || bottom_left;
  40. }
  41. inline void shrink(CSSPixels top, CSSPixels right, CSSPixels bottom, CSSPixels left)
  42. {
  43. top_left.shrink(left, top);
  44. top_right.shrink(right, top);
  45. bottom_right.shrink(right, bottom);
  46. bottom_left.shrink(left, bottom);
  47. }
  48. };
  49. BorderRadiiData normalized_border_radii_data(Layout::Node const&, CSSPixelRect const&, CSS::BorderRadiusData top_left_radius, CSS::BorderRadiusData top_right_radius, CSS::BorderRadiusData bottom_right_radius, CSS::BorderRadiusData bottom_left_radius);
  50. enum class BorderEdge {
  51. Top,
  52. Right,
  53. Bottom,
  54. Left,
  55. };
  56. struct BordersData {
  57. CSS::BorderData top;
  58. CSS::BorderData right;
  59. CSS::BorderData bottom;
  60. CSS::BorderData left;
  61. };
  62. RefPtr<Gfx::Bitmap> get_cached_corner_bitmap(DevicePixelSize corners_size);
  63. void paint_border(PaintContext& context, BorderEdge edge, DevicePixelRect const& rect, BorderRadiiData const& border_radii_data, BordersData const& borders_data);
  64. void paint_all_borders(PaintContext& context, CSSPixelRect const& bordered_rect, BorderRadiiData const& border_radii_data, BordersData const&);
  65. }