StyleProperties.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <LibGfx/Font/Font.h>
  10. #include <LibGfx/FontCascadeList.h>
  11. #include <LibGfx/Forward.h>
  12. #include <LibJS/Heap/GCPtr.h>
  13. #include <LibWeb/CSS/ComputedValues.h>
  14. #include <LibWeb/CSS/LengthBox.h>
  15. #include <LibWeb/CSS/PropertyID.h>
  16. #include <LibWeb/CSS/StyleProperty.h>
  17. namespace Web::CSS {
  18. class StyleProperties : public RefCounted<StyleProperties> {
  19. public:
  20. static constexpr size_t number_of_properties = to_underlying(CSS::last_property_id) + 1;
  21. private:
  22. struct Data : public RefCounted<Data> {
  23. friend class StyleComputer;
  24. NonnullRefPtr<Data> clone() const;
  25. // FIXME: These need protection from GC!
  26. JS::GCPtr<CSS::CSSStyleDeclaration const> m_animation_name_source;
  27. JS::GCPtr<CSS::CSSStyleDeclaration const> m_transition_property_source;
  28. Array<RefPtr<CSSStyleValue const>, number_of_properties> m_property_values;
  29. Array<u8, ceil_div(number_of_properties, 8uz)> m_property_important {};
  30. Array<u8, ceil_div(number_of_properties, 8uz)> m_property_inherited {};
  31. HashMap<CSS::PropertyID, NonnullRefPtr<CSSStyleValue const>> m_animated_property_values;
  32. int m_math_depth { InitialValues::math_depth() };
  33. mutable RefPtr<Gfx::FontCascadeList> m_font_list;
  34. Optional<CSSPixels> m_line_height;
  35. };
  36. public:
  37. StyleProperties() = default;
  38. static NonnullRefPtr<StyleProperties> create() { return adopt_ref(*new StyleProperties); }
  39. NonnullRefPtr<StyleProperties> clone() const;
  40. template<typename Callback>
  41. inline void for_each_property(Callback callback) const
  42. {
  43. for (size_t i = 0; i < m_data->m_property_values.size(); ++i) {
  44. if (m_data->m_property_values[i])
  45. callback((CSS::PropertyID)i, *m_data->m_property_values[i]);
  46. }
  47. }
  48. enum class Inherited {
  49. No,
  50. Yes
  51. };
  52. HashMap<CSS::PropertyID, NonnullRefPtr<CSSStyleValue const>> const& animated_property_values() const { return m_data->m_animated_property_values; }
  53. void reset_animated_properties();
  54. bool is_property_important(CSS::PropertyID property_id) const;
  55. bool is_property_inherited(CSS::PropertyID property_id) const;
  56. void set_property_important(CSS::PropertyID, Important);
  57. void set_property_inherited(CSS::PropertyID, Inherited);
  58. void set_property(CSS::PropertyID, NonnullRefPtr<CSSStyleValue const> value, Inherited = Inherited::No, Important = Important::No);
  59. void set_animated_property(CSS::PropertyID, NonnullRefPtr<CSSStyleValue const> value);
  60. enum class WithAnimationsApplied {
  61. No,
  62. Yes,
  63. };
  64. NonnullRefPtr<CSSStyleValue const> property(CSS::PropertyID, WithAnimationsApplied = WithAnimationsApplied::Yes) const;
  65. RefPtr<CSSStyleValue const> maybe_null_property(CSS::PropertyID) const;
  66. void revert_property(CSS::PropertyID, StyleProperties const& style_for_revert);
  67. JS::GCPtr<CSS::CSSStyleDeclaration const> animation_name_source() const { return m_data->m_animation_name_source; }
  68. void set_animation_name_source(JS::GCPtr<CSS::CSSStyleDeclaration const> declaration) { m_data->m_animation_name_source = declaration; }
  69. JS::GCPtr<CSS::CSSStyleDeclaration const> transition_property_source() const { return m_data->m_transition_property_source; }
  70. void set_transition_property_source(JS::GCPtr<CSS::CSSStyleDeclaration const> declaration) { m_data->m_transition_property_source = declaration; }
  71. CSS::Size size_value(CSS::PropertyID) const;
  72. LengthPercentage length_percentage_or_fallback(CSS::PropertyID, LengthPercentage const& fallback) const;
  73. Optional<LengthPercentage> length_percentage(CSS::PropertyID) const;
  74. LengthBox length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id, const CSS::Length& default_value) const;
  75. Color color_or_fallback(CSS::PropertyID, Layout::NodeWithStyle const&, Color fallback) const;
  76. Optional<CSS::TextAnchor> text_anchor() const;
  77. Optional<CSS::TextAlign> text_align() const;
  78. Optional<CSS::TextJustify> text_justify() const;
  79. Optional<CSS::TextOverflow> text_overflow() const;
  80. CSS::Length border_spacing_horizontal(Layout::Node const&) const;
  81. CSS::Length border_spacing_vertical(Layout::Node const&) const;
  82. Optional<CSS::CaptionSide> caption_side() const;
  83. CSS::Clip clip() const;
  84. CSS::Display display() const;
  85. Optional<CSS::Float> float_() const;
  86. Optional<CSS::Clear> clear() const;
  87. Optional<CSS::ColumnSpan> column_span() const;
  88. struct ContentDataAndQuoteNestingLevel {
  89. CSS::ContentData content_data;
  90. u32 final_quote_nesting_level { 0 };
  91. };
  92. ContentDataAndQuoteNestingLevel content(DOM::Element&, u32 initial_quote_nesting_level) const;
  93. Optional<CSS::ContentVisibility> content_visibility() const;
  94. Optional<CSS::Cursor> cursor() const;
  95. Variant<LengthOrCalculated, NumberOrCalculated> tab_size() const;
  96. Optional<CSS::WhiteSpace> white_space() const;
  97. Optional<CSS::LineStyle> line_style(CSS::PropertyID) const;
  98. Optional<CSS::OutlineStyle> outline_style() const;
  99. Vector<CSS::TextDecorationLine> text_decoration_line() const;
  100. Optional<CSS::TextDecorationStyle> text_decoration_style() const;
  101. Optional<CSS::TextTransform> text_transform() const;
  102. Vector<CSS::ShadowData> text_shadow(Layout::Node const&) const;
  103. Optional<CSS::ListStyleType> list_style_type() const;
  104. Optional<CSS::ListStylePosition> list_style_position() const;
  105. Optional<CSS::FlexDirection> flex_direction() const;
  106. Optional<CSS::FlexWrap> flex_wrap() const;
  107. Optional<CSS::FlexBasis> flex_basis() const;
  108. float flex_grow() const;
  109. float flex_shrink() const;
  110. int order() const;
  111. Optional<Color> accent_color(Layout::NodeWithStyle const&) const;
  112. Optional<CSS::AlignContent> align_content() const;
  113. Optional<CSS::AlignItems> align_items() const;
  114. Optional<CSS::AlignSelf> align_self() const;
  115. Optional<CSS::Appearance> appearance() const;
  116. CSS::BackdropFilter backdrop_filter() const;
  117. float opacity() const;
  118. Optional<CSS::Visibility> visibility() const;
  119. Optional<CSS::ImageRendering> image_rendering() const;
  120. Optional<CSS::JustifyContent> justify_content() const;
  121. Optional<CSS::JustifyItems> justify_items() const;
  122. Optional<CSS::JustifySelf> justify_self() const;
  123. Optional<CSS::Overflow> overflow_x() const;
  124. Optional<CSS::Overflow> overflow_y() const;
  125. Vector<CSS::ShadowData> box_shadow(Layout::Node const&) const;
  126. Optional<CSS::BoxSizing> box_sizing() const;
  127. Optional<CSS::PointerEvents> pointer_events() const;
  128. Variant<CSS::VerticalAlign, CSS::LengthPercentage> vertical_align() const;
  129. Optional<CSS::FontVariant> font_variant() const;
  130. Optional<FlyString> font_language_override() const;
  131. Optional<HashMap<FlyString, IntegerOrCalculated>> font_feature_settings() const;
  132. Optional<HashMap<FlyString, NumberOrCalculated>> font_variation_settings() const;
  133. CSS::GridTrackSizeList grid_auto_columns() const;
  134. CSS::GridTrackSizeList grid_auto_rows() const;
  135. CSS::GridTrackSizeList grid_template_columns() const;
  136. CSS::GridTrackSizeList grid_template_rows() const;
  137. [[nodiscard]] CSS::GridAutoFlow grid_auto_flow() const;
  138. CSS::GridTrackPlacement grid_column_end() const;
  139. CSS::GridTrackPlacement grid_column_start() const;
  140. CSS::GridTrackPlacement grid_row_end() const;
  141. CSS::GridTrackPlacement grid_row_start() const;
  142. Optional<CSS::BorderCollapse> border_collapse() const;
  143. Vector<Vector<String>> grid_template_areas() const;
  144. Optional<CSS::ObjectFit> object_fit() const;
  145. CSS::ObjectPosition object_position() const;
  146. Optional<CSS::TableLayout> table_layout() const;
  147. Optional<CSS::Direction> direction() const;
  148. Optional<CSS::UnicodeBidi> unicode_bidi() const;
  149. static Vector<CSS::Transformation> transformations_for_style_value(CSSStyleValue const& value);
  150. Vector<CSS::Transformation> transformations() const;
  151. Optional<CSS::TransformBox> transform_box() const;
  152. CSS::TransformOrigin transform_origin() const;
  153. Optional<CSS::MaskType> mask_type() const;
  154. Color stop_color() const;
  155. float stop_opacity() const;
  156. float fill_opacity() const;
  157. Optional<CSS::StrokeLinecap> stroke_linecap() const;
  158. float stroke_opacity() const;
  159. Optional<CSS::FillRule> fill_rule() const;
  160. Optional<CSS::ClipRule> clip_rule() const;
  161. Gfx::Font const& first_available_computed_font() const { return m_data->m_font_list->first(); }
  162. Gfx::FontCascadeList const& computed_font_list() const
  163. {
  164. VERIFY(m_data->m_font_list);
  165. return *m_data->m_font_list;
  166. }
  167. void set_computed_font_list(NonnullRefPtr<Gfx::FontCascadeList> font_list) const
  168. {
  169. m_data->m_font_list = move(font_list);
  170. }
  171. [[nodiscard]] CSSPixels compute_line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const;
  172. [[nodiscard]] CSSPixels line_height() const { return *m_data->m_line_height; }
  173. void set_line_height(Badge<StyleComputer> const&, CSSPixels line_height) { m_data->m_line_height = line_height; }
  174. bool operator==(StyleProperties const&) const;
  175. Optional<CSS::Positioning> position() const;
  176. Optional<int> z_index() const;
  177. void set_math_depth(int math_depth);
  178. int math_depth() const { return m_data->m_math_depth; }
  179. QuotesData quotes() const;
  180. Vector<CounterData> counter_data(PropertyID) const;
  181. Optional<CSS::ScrollbarWidth> scrollbar_width() const;
  182. static NonnullRefPtr<Gfx::Font const> font_fallback(bool monospace, bool bold);
  183. static float resolve_opacity_value(CSSStyleValue const& value);
  184. private:
  185. friend class StyleComputer;
  186. Optional<CSS::Overflow> overflow(CSS::PropertyID) const;
  187. Vector<CSS::ShadowData> shadow(CSS::PropertyID, Layout::Node const&) const;
  188. AK::CopyOnWrite<StyleProperties::Data> m_data;
  189. };
  190. }