StyleValue.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <AK/Concepts.h>
  11. #include <AK/GenericShorthands.h>
  12. #include <AK/NonnullOwnPtr.h>
  13. #include <AK/RefCounted.h>
  14. #include <AK/RefPtr.h>
  15. #include <AK/String.h>
  16. #include <AK/StringView.h>
  17. #include <AK/URL.h>
  18. #include <AK/Variant.h>
  19. #include <AK/Vector.h>
  20. #include <AK/WeakPtr.h>
  21. #include <LibGfx/Color.h>
  22. #include <LibWeb/CSS/Enums.h>
  23. #include <LibWeb/CSS/Length.h>
  24. #include <LibWeb/CSS/ValueID.h>
  25. #include <LibWeb/Forward.h>
  26. namespace Web::CSS {
  27. template<typename T>
  28. struct ValueComparingNonnullRefPtr : public NonnullRefPtr<T> {
  29. using NonnullRefPtr<T>::NonnullRefPtr;
  30. ValueComparingNonnullRefPtr(NonnullRefPtr<T> const& other)
  31. : NonnullRefPtr<T>(other)
  32. {
  33. }
  34. ValueComparingNonnullRefPtr(NonnullRefPtr<T>&& other)
  35. : NonnullRefPtr<T>(move(other))
  36. {
  37. }
  38. bool operator==(ValueComparingNonnullRefPtr const& other) const
  39. {
  40. return this->ptr() == other.ptr() || this->ptr()->equals(*other);
  41. }
  42. private:
  43. using NonnullRefPtr<T>::operator==;
  44. };
  45. template<typename T>
  46. struct ValueComparingRefPtr : public RefPtr<T> {
  47. using RefPtr<T>::RefPtr;
  48. ValueComparingRefPtr(RefPtr<T> const& other)
  49. : RefPtr<T>(other)
  50. {
  51. }
  52. ValueComparingRefPtr(RefPtr<T>&& other)
  53. : RefPtr<T>(move(other))
  54. {
  55. }
  56. template<typename U>
  57. bool operator==(ValueComparingNonnullRefPtr<U> const& other) const
  58. {
  59. return this->ptr() == other.ptr() || (this->ptr() && this->ptr()->equals(*other));
  60. }
  61. bool operator==(ValueComparingRefPtr const& other) const
  62. {
  63. return this->ptr() == other.ptr() || (this->ptr() && other.ptr() && this->ptr()->equals(*other));
  64. }
  65. private:
  66. using RefPtr<T>::operator==;
  67. };
  68. using StyleValueVector = Vector<ValueComparingNonnullRefPtr<StyleValue const>>;
  69. #define ENUMERATE_STYLE_VALUE_TYPES \
  70. __ENUMERATE_STYLE_VALUE_TYPE(Angle, angle) \
  71. __ENUMERATE_STYLE_VALUE_TYPE(BackgroundRepeat, background_repeat) \
  72. __ENUMERATE_STYLE_VALUE_TYPE(BackgroundSize, background_size) \
  73. __ENUMERATE_STYLE_VALUE_TYPE(Border, border) \
  74. __ENUMERATE_STYLE_VALUE_TYPE(BorderRadius, border_radius) \
  75. __ENUMERATE_STYLE_VALUE_TYPE(Calculated, calculated) \
  76. __ENUMERATE_STYLE_VALUE_TYPE(Color, color) \
  77. __ENUMERATE_STYLE_VALUE_TYPE(ConicGradient, conic_gradient) \
  78. __ENUMERATE_STYLE_VALUE_TYPE(Content, content) \
  79. __ENUMERATE_STYLE_VALUE_TYPE(CustomIdent, custom_ident) \
  80. __ENUMERATE_STYLE_VALUE_TYPE(Display, display) \
  81. __ENUMERATE_STYLE_VALUE_TYPE(Easing, easing) \
  82. __ENUMERATE_STYLE_VALUE_TYPE(Edge, edge) \
  83. __ENUMERATE_STYLE_VALUE_TYPE(FilterValueList, filter_value_list) \
  84. __ENUMERATE_STYLE_VALUE_TYPE(Frequency, frequency) \
  85. __ENUMERATE_STYLE_VALUE_TYPE(GridAreaShorthand, grid_area_shorthand) \
  86. __ENUMERATE_STYLE_VALUE_TYPE(GridAutoFlow, grid_auto_flow) \
  87. __ENUMERATE_STYLE_VALUE_TYPE(GridTemplateArea, grid_template_area) \
  88. __ENUMERATE_STYLE_VALUE_TYPE(GridTrackPlacement, grid_track_placement) \
  89. __ENUMERATE_STYLE_VALUE_TYPE(GridTrackPlacementShorthand, grid_track_placement_shorthand) \
  90. __ENUMERATE_STYLE_VALUE_TYPE(GridTrackSizeList, grid_track_size_list) \
  91. __ENUMERATE_STYLE_VALUE_TYPE(GridTrackSizeListShorthand, grid_track_size_list_shorthand) \
  92. __ENUMERATE_STYLE_VALUE_TYPE(Identifier, identifier) \
  93. __ENUMERATE_STYLE_VALUE_TYPE(Image, image) \
  94. __ENUMERATE_STYLE_VALUE_TYPE(Inherit, inherit) \
  95. __ENUMERATE_STYLE_VALUE_TYPE(Initial, initial) \
  96. __ENUMERATE_STYLE_VALUE_TYPE(Integer, integer) \
  97. __ENUMERATE_STYLE_VALUE_TYPE(Length, length) \
  98. __ENUMERATE_STYLE_VALUE_TYPE(LinearGradient, linear_gradient) \
  99. __ENUMERATE_STYLE_VALUE_TYPE(ListStyle, list_style) \
  100. __ENUMERATE_STYLE_VALUE_TYPE(MathDepth, math_depth) \
  101. __ENUMERATE_STYLE_VALUE_TYPE(Number, number) \
  102. __ENUMERATE_STYLE_VALUE_TYPE(Overflow, overflow) \
  103. __ENUMERATE_STYLE_VALUE_TYPE(Percentage, percentage) \
  104. __ENUMERATE_STYLE_VALUE_TYPE(PlaceContent, place_content) \
  105. __ENUMERATE_STYLE_VALUE_TYPE(PlaceItems, place_items) \
  106. __ENUMERATE_STYLE_VALUE_TYPE(PlaceSelf, place_self) \
  107. __ENUMERATE_STYLE_VALUE_TYPE(Position, position) \
  108. __ENUMERATE_STYLE_VALUE_TYPE(RadialGradient, radial_gradient) \
  109. __ENUMERATE_STYLE_VALUE_TYPE(Ratio, ratio) \
  110. __ENUMERATE_STYLE_VALUE_TYPE(Rect, rect) \
  111. __ENUMERATE_STYLE_VALUE_TYPE(Resolution, resolution) \
  112. __ENUMERATE_STYLE_VALUE_TYPE(Revert, revert) \
  113. __ENUMERATE_STYLE_VALUE_TYPE(Shadow, shadow) \
  114. __ENUMERATE_STYLE_VALUE_TYPE(Shorthand, shorthand) \
  115. __ENUMERATE_STYLE_VALUE_TYPE(String, string) \
  116. __ENUMERATE_STYLE_VALUE_TYPE(TextDecoration, text_decoration) \
  117. __ENUMERATE_STYLE_VALUE_TYPE(Time, time) \
  118. __ENUMERATE_STYLE_VALUE_TYPE(Transformation, transformation) \
  119. __ENUMERATE_STYLE_VALUE_TYPE(Unresolved, unresolved) \
  120. __ENUMERATE_STYLE_VALUE_TYPE(Unset, unset) \
  121. __ENUMERATE_STYLE_VALUE_TYPE(URL, url) \
  122. __ENUMERATE_STYLE_VALUE_TYPE(ValueList, value_list)
  123. // NOTE:
  124. using ValueListStyleValue = StyleValueList;
  125. class StyleValue : public RefCounted<StyleValue> {
  126. public:
  127. virtual ~StyleValue() = default;
  128. enum class Type {
  129. #define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \
  130. TitleCaseName,
  131. ENUMERATE_STYLE_VALUE_TYPES
  132. #undef __ENUMERATE_STYLE_VALUE_TYPE
  133. };
  134. Type type() const { return m_type; }
  135. #define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \
  136. bool is_##SnakeCaseName() const { return type() == Type::TitleCaseName; } \
  137. TitleCaseName##StyleValue const& as_##SnakeCaseName() const; \
  138. TitleCaseName##StyleValue& as_##SnakeCaseName() { return const_cast<TitleCaseName##StyleValue&>(const_cast<StyleValue const&>(*this).as_##SnakeCaseName()); }
  139. ENUMERATE_STYLE_VALUE_TYPES
  140. #undef __ENUMERATE_STYLE_VALUE_TYPE
  141. bool is_abstract_image() const
  142. {
  143. return AK::first_is_one_of(type(), Type::Image, Type::LinearGradient, Type::ConicGradient, Type::RadialGradient);
  144. }
  145. AbstractImageStyleValue const& as_abstract_image() const;
  146. AbstractImageStyleValue& as_abstract_image() { return const_cast<AbstractImageStyleValue&>(const_cast<StyleValue const&>(*this).as_abstract_image()); }
  147. bool is_builtin() const { return is_inherit() || is_initial() || is_unset(); }
  148. bool has_auto() const;
  149. virtual bool has_color() const { return false; }
  150. virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const;
  151. virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const { return {}; }
  152. ValueID to_identifier() const;
  153. virtual String to_string() const = 0;
  154. [[nodiscard]] int to_font_weight() const;
  155. [[nodiscard]] int to_font_slope() const;
  156. [[nodiscard]] int to_font_stretch_width() const;
  157. virtual bool equals(StyleValue const& other) const = 0;
  158. bool operator==(StyleValue const& other) const
  159. {
  160. return this->equals(other);
  161. }
  162. protected:
  163. explicit StyleValue(Type);
  164. private:
  165. Type m_type;
  166. };
  167. template<typename T>
  168. struct StyleValueWithDefaultOperators : public StyleValue {
  169. using StyleValue::StyleValue;
  170. virtual bool equals(StyleValue const& other) const override
  171. {
  172. if (type() != other.type())
  173. return false;
  174. auto const& typed_other = static_cast<T const&>(other);
  175. return static_cast<T const&>(*this).properties_equal(typed_other);
  176. }
  177. };
  178. }
  179. template<>
  180. struct AK::Formatter<Web::CSS::StyleValue> : Formatter<StringView> {
  181. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::StyleValue const& style_value)
  182. {
  183. return Formatter<StringView>::format(builder, style_value.to_string());
  184. }
  185. };