BorderRadiiData.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibGfx/AntiAliasingPainter.h>
  9. #include <LibGfx/Forward.h>
  10. #include <LibWeb/CSS/ComputedValues.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 const& context) const;
  16. inline operator bool() const
  17. {
  18. return horizontal_radius > 0 || vertical_radius > 0;
  19. }
  20. inline void shrink(CSSPixels horizontal, CSSPixels vertical)
  21. {
  22. if (horizontal_radius != 0)
  23. horizontal_radius = max(CSSPixels(0), horizontal_radius - horizontal);
  24. if (vertical_radius != 0)
  25. vertical_radius = max(CSSPixels(0), vertical_radius - vertical);
  26. }
  27. inline void union_max_radii(BorderRadiusData const& other)
  28. {
  29. horizontal_radius = max(horizontal_radius, other.horizontal_radius);
  30. vertical_radius = max(vertical_radius, other.vertical_radius);
  31. }
  32. };
  33. using CornerRadius = Gfx::AntiAliasingPainter::CornerRadius;
  34. struct CornerRadii {
  35. CornerRadius top_left;
  36. CornerRadius top_right;
  37. CornerRadius bottom_right;
  38. CornerRadius bottom_left;
  39. inline bool has_any_radius() const
  40. {
  41. return top_left || top_right || bottom_right || bottom_left;
  42. }
  43. };
  44. struct BorderRadiiData {
  45. BorderRadiusData top_left;
  46. BorderRadiusData top_right;
  47. BorderRadiusData bottom_right;
  48. BorderRadiusData bottom_left;
  49. inline bool has_any_radius() const
  50. {
  51. return top_left || top_right || bottom_right || bottom_left;
  52. }
  53. inline void union_max_radii(BorderRadiiData const& other)
  54. {
  55. top_left.union_max_radii(other.top_left);
  56. top_right.union_max_radii(other.top_right);
  57. bottom_right.union_max_radii(other.bottom_right);
  58. bottom_left.union_max_radii(other.bottom_left);
  59. }
  60. inline void shrink(CSSPixels top, CSSPixels right, CSSPixels bottom, CSSPixels left)
  61. {
  62. top_left.shrink(left, top);
  63. top_right.shrink(right, top);
  64. bottom_right.shrink(right, bottom);
  65. bottom_left.shrink(left, bottom);
  66. }
  67. inline void inflate(CSSPixels top, CSSPixels right, CSSPixels bottom, CSSPixels left)
  68. {
  69. shrink(-top, -right, -bottom, -left);
  70. }
  71. inline CornerRadii as_corners(PaintContext const& context) const
  72. {
  73. return CornerRadii {
  74. top_left.as_corner(context),
  75. top_right.as_corner(context),
  76. bottom_right.as_corner(context),
  77. bottom_left.as_corner(context)
  78. };
  79. }
  80. };
  81. }