BorderRadiiData.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <andreas@ladybird.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 <LibWeb/CSS/ComputedValues.h>
  9. namespace Web::Painting {
  10. struct CornerRadius {
  11. int horizontal_radius { 0 };
  12. int vertical_radius { 0 };
  13. inline operator bool() const
  14. {
  15. return horizontal_radius > 0 && vertical_radius > 0;
  16. }
  17. };
  18. struct BorderRadiusData {
  19. CSSPixels horizontal_radius { 0 };
  20. CSSPixels vertical_radius { 0 };
  21. CornerRadius as_corner(PaintContext const& context) const;
  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. if (horizontal_radius != 0)
  29. horizontal_radius = max(CSSPixels(0), horizontal_radius - horizontal);
  30. if (vertical_radius != 0)
  31. vertical_radius = max(CSSPixels(0), vertical_radius - vertical);
  32. }
  33. inline void union_max_radii(BorderRadiusData const& other)
  34. {
  35. horizontal_radius = max(horizontal_radius, other.horizontal_radius);
  36. vertical_radius = max(vertical_radius, other.vertical_radius);
  37. }
  38. };
  39. struct CornerRadii {
  40. CornerRadius top_left;
  41. CornerRadius top_right;
  42. CornerRadius bottom_right;
  43. CornerRadius bottom_left;
  44. inline bool has_any_radius() const
  45. {
  46. return top_left || top_right || bottom_right || bottom_left;
  47. }
  48. };
  49. struct BorderRadiiData {
  50. BorderRadiusData top_left;
  51. BorderRadiusData top_right;
  52. BorderRadiusData bottom_right;
  53. BorderRadiusData bottom_left;
  54. inline bool has_any_radius() const
  55. {
  56. return top_left || top_right || bottom_right || bottom_left;
  57. }
  58. inline void union_max_radii(BorderRadiiData const& other)
  59. {
  60. top_left.union_max_radii(other.top_left);
  61. top_right.union_max_radii(other.top_right);
  62. bottom_right.union_max_radii(other.bottom_right);
  63. bottom_left.union_max_radii(other.bottom_left);
  64. }
  65. inline void shrink(CSSPixels top, CSSPixels right, CSSPixels bottom, CSSPixels left)
  66. {
  67. top_left.shrink(left, top);
  68. top_right.shrink(right, top);
  69. bottom_right.shrink(right, bottom);
  70. bottom_left.shrink(left, bottom);
  71. }
  72. inline void inflate(CSSPixels top, CSSPixels right, CSSPixels bottom, CSSPixels left)
  73. {
  74. shrink(-top, -right, -bottom, -left);
  75. }
  76. inline CornerRadii as_corners(PaintContext const& context) const
  77. {
  78. return CornerRadii {
  79. top_left.as_corner(context),
  80. top_right.as_corner(context),
  81. bottom_right.as_corner(context),
  82. bottom_left.as_corner(context)
  83. };
  84. }
  85. };
  86. }