StyleValue.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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(Background, background) \
  72. __ENUMERATE_STYLE_VALUE_TYPE(BackgroundRepeat, background_repeat) \
  73. __ENUMERATE_STYLE_VALUE_TYPE(BackgroundSize, background_size) \
  74. __ENUMERATE_STYLE_VALUE_TYPE(Border, border) \
  75. __ENUMERATE_STYLE_VALUE_TYPE(BorderRadius, border_radius) \
  76. __ENUMERATE_STYLE_VALUE_TYPE(BorderRadiusShorthand, border_radius_shorthand) \
  77. __ENUMERATE_STYLE_VALUE_TYPE(Calculated, calculated) \
  78. __ENUMERATE_STYLE_VALUE_TYPE(Color, color) \
  79. __ENUMERATE_STYLE_VALUE_TYPE(ConicGradient, conic_gradient) \
  80. __ENUMERATE_STYLE_VALUE_TYPE(Content, content) \
  81. __ENUMERATE_STYLE_VALUE_TYPE(CustomIdent, custom_ident) \
  82. __ENUMERATE_STYLE_VALUE_TYPE(Display, display) \
  83. __ENUMERATE_STYLE_VALUE_TYPE(Easing, easing) \
  84. __ENUMERATE_STYLE_VALUE_TYPE(Edge, edge) \
  85. __ENUMERATE_STYLE_VALUE_TYPE(FilterValueList, filter_value_list) \
  86. __ENUMERATE_STYLE_VALUE_TYPE(FlexFlow, flex_flow) \
  87. __ENUMERATE_STYLE_VALUE_TYPE(Font, font) \
  88. __ENUMERATE_STYLE_VALUE_TYPE(Frequency, frequency) \
  89. __ENUMERATE_STYLE_VALUE_TYPE(GridAreaShorthand, grid_area_shorthand) \
  90. __ENUMERATE_STYLE_VALUE_TYPE(GridAutoFlow, grid_auto_flow) \
  91. __ENUMERATE_STYLE_VALUE_TYPE(GridTemplateArea, grid_template_area) \
  92. __ENUMERATE_STYLE_VALUE_TYPE(GridTrackPlacement, grid_track_placement) \
  93. __ENUMERATE_STYLE_VALUE_TYPE(GridTrackPlacementShorthand, grid_track_placement_shorthand) \
  94. __ENUMERATE_STYLE_VALUE_TYPE(GridTrackSizeList, grid_track_size_list) \
  95. __ENUMERATE_STYLE_VALUE_TYPE(GridTrackSizeListShorthand, grid_track_size_list_shorthand) \
  96. __ENUMERATE_STYLE_VALUE_TYPE(Identifier, identifier) \
  97. __ENUMERATE_STYLE_VALUE_TYPE(Image, image) \
  98. __ENUMERATE_STYLE_VALUE_TYPE(Inherit, inherit) \
  99. __ENUMERATE_STYLE_VALUE_TYPE(Initial, initial) \
  100. __ENUMERATE_STYLE_VALUE_TYPE(Integer, integer) \
  101. __ENUMERATE_STYLE_VALUE_TYPE(Length, length) \
  102. __ENUMERATE_STYLE_VALUE_TYPE(LinearGradient, linear_gradient) \
  103. __ENUMERATE_STYLE_VALUE_TYPE(ListStyle, list_style) \
  104. __ENUMERATE_STYLE_VALUE_TYPE(MathDepth, math_depth) \
  105. __ENUMERATE_STYLE_VALUE_TYPE(Number, number) \
  106. __ENUMERATE_STYLE_VALUE_TYPE(Overflow, overflow) \
  107. __ENUMERATE_STYLE_VALUE_TYPE(Percentage, percentage) \
  108. __ENUMERATE_STYLE_VALUE_TYPE(PlaceContent, place_content) \
  109. __ENUMERATE_STYLE_VALUE_TYPE(PlaceItems, place_items) \
  110. __ENUMERATE_STYLE_VALUE_TYPE(PlaceSelf, place_self) \
  111. __ENUMERATE_STYLE_VALUE_TYPE(Position, position) \
  112. __ENUMERATE_STYLE_VALUE_TYPE(RadialGradient, radial_gradient) \
  113. __ENUMERATE_STYLE_VALUE_TYPE(Ratio, ratio) \
  114. __ENUMERATE_STYLE_VALUE_TYPE(Rect, rect) \
  115. __ENUMERATE_STYLE_VALUE_TYPE(Resolution, resolution) \
  116. __ENUMERATE_STYLE_VALUE_TYPE(Revert, revert) \
  117. __ENUMERATE_STYLE_VALUE_TYPE(Shadow, shadow) \
  118. __ENUMERATE_STYLE_VALUE_TYPE(Shorthand, shorthand) \
  119. __ENUMERATE_STYLE_VALUE_TYPE(String, string) \
  120. __ENUMERATE_STYLE_VALUE_TYPE(TextDecoration, text_decoration) \
  121. __ENUMERATE_STYLE_VALUE_TYPE(Time, time) \
  122. __ENUMERATE_STYLE_VALUE_TYPE(Transformation, transformation) \
  123. __ENUMERATE_STYLE_VALUE_TYPE(Unresolved, unresolved) \
  124. __ENUMERATE_STYLE_VALUE_TYPE(Unset, unset) \
  125. __ENUMERATE_STYLE_VALUE_TYPE(URL, url) \
  126. __ENUMERATE_STYLE_VALUE_TYPE(ValueList, value_list)
  127. // NOTE:
  128. using ValueListStyleValue = StyleValueList;
  129. class StyleValue : public RefCounted<StyleValue> {
  130. public:
  131. virtual ~StyleValue() = default;
  132. enum class Type {
  133. #define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \
  134. TitleCaseName,
  135. ENUMERATE_STYLE_VALUE_TYPES
  136. #undef __ENUMERATE_STYLE_VALUE_TYPE
  137. };
  138. Type type() const { return m_type; }
  139. #define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \
  140. bool is_##SnakeCaseName() const { return type() == Type::TitleCaseName; } \
  141. TitleCaseName##StyleValue const& as_##SnakeCaseName() const; \
  142. TitleCaseName##StyleValue& as_##SnakeCaseName() { return const_cast<TitleCaseName##StyleValue&>(const_cast<StyleValue const&>(*this).as_##SnakeCaseName()); }
  143. ENUMERATE_STYLE_VALUE_TYPES
  144. #undef __ENUMERATE_STYLE_VALUE_TYPE
  145. bool is_abstract_image() const
  146. {
  147. return AK::first_is_one_of(type(), Type::Image, Type::LinearGradient, Type::ConicGradient, Type::RadialGradient);
  148. }
  149. AbstractImageStyleValue const& as_abstract_image() const;
  150. AbstractImageStyleValue& as_abstract_image() { return const_cast<AbstractImageStyleValue&>(const_cast<StyleValue const&>(*this).as_abstract_image()); }
  151. bool is_builtin() const { return is_inherit() || is_initial() || is_unset(); }
  152. bool has_auto() const;
  153. virtual bool has_color() const { return false; }
  154. virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const;
  155. virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const { return {}; }
  156. ValueID to_identifier() const;
  157. virtual String to_string() const = 0;
  158. [[nodiscard]] int to_font_weight() const;
  159. [[nodiscard]] int to_font_slope() const;
  160. [[nodiscard]] int to_font_stretch_width() const;
  161. virtual bool equals(StyleValue const& other) const = 0;
  162. bool operator==(StyleValue const& other) const
  163. {
  164. return this->equals(other);
  165. }
  166. protected:
  167. explicit StyleValue(Type);
  168. private:
  169. Type m_type;
  170. };
  171. template<typename T>
  172. struct StyleValueWithDefaultOperators : public StyleValue {
  173. using StyleValue::StyleValue;
  174. virtual bool equals(StyleValue const& other) const override
  175. {
  176. if (type() != other.type())
  177. return false;
  178. auto const& typed_other = static_cast<T const&>(other);
  179. return static_cast<T const&>(*this).properties_equal(typed_other);
  180. }
  181. };
  182. }
  183. template<>
  184. struct AK::Formatter<Web::CSS::StyleValue> : Formatter<StringView> {
  185. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::StyleValue const& style_value)
  186. {
  187. return Formatter<StringView>::format(builder, style_value.to_string());
  188. }
  189. };