StyleProperties.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.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 <LibWeb/CSS/ComputedValues.h>
  13. #include <LibWeb/CSS/LengthBox.h>
  14. #include <LibWeb/CSS/PropertyID.h>
  15. namespace Web::CSS {
  16. class StyleProperties : public RefCounted<StyleProperties> {
  17. public:
  18. StyleProperties() = default;
  19. static NonnullRefPtr<StyleProperties> create() { return adopt_ref(*new StyleProperties); }
  20. NonnullRefPtr<StyleProperties> clone() const;
  21. template<typename Callback>
  22. inline void for_each_property(Callback callback) const
  23. {
  24. for (size_t i = 0; i < m_property_values.size(); ++i) {
  25. if (m_property_values[i].has_value())
  26. callback((CSS::PropertyID)i, *m_property_values[i]->style);
  27. }
  28. }
  29. enum class Important {
  30. No,
  31. Yes
  32. };
  33. enum class Inherited {
  34. No,
  35. Yes
  36. };
  37. struct StyleAndSourceDeclaration {
  38. NonnullRefPtr<StyleValue const> style;
  39. CSS::CSSStyleDeclaration const* declaration = nullptr;
  40. Important important { Important::No };
  41. Inherited inherited { Inherited::No };
  42. };
  43. using PropertyValues = Array<Optional<StyleAndSourceDeclaration>, to_underlying(CSS::last_property_id) + 1>;
  44. auto& properties() { return m_property_values; }
  45. auto const& properties() const { return m_property_values; }
  46. void reset_animated_properties();
  47. bool is_property_important(CSS::PropertyID property_id) const;
  48. bool is_property_inherited(CSS::PropertyID property_id) const;
  49. void set_property(CSS::PropertyID, NonnullRefPtr<StyleValue const> value, CSS::CSSStyleDeclaration const* source_declaration = nullptr, Inherited = Inherited::No, Important = Important::No);
  50. void set_animated_property(CSS::PropertyID, NonnullRefPtr<StyleValue const> value);
  51. NonnullRefPtr<StyleValue const> property(CSS::PropertyID) const;
  52. RefPtr<StyleValue const> maybe_null_property(CSS::PropertyID) const;
  53. CSS::CSSStyleDeclaration const* property_source_declaration(CSS::PropertyID) const;
  54. CSS::Size size_value(CSS::PropertyID) const;
  55. LengthPercentage length_percentage_or_fallback(CSS::PropertyID, LengthPercentage const& fallback) const;
  56. Optional<LengthPercentage> length_percentage(CSS::PropertyID) const;
  57. 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;
  58. Color color_or_fallback(CSS::PropertyID, Layout::NodeWithStyle const&, Color fallback) const;
  59. Optional<CSS::TextAnchor> text_anchor() const;
  60. Optional<CSS::TextAlign> text_align() const;
  61. Optional<CSS::TextJustify> text_justify() const;
  62. CSS::Length border_spacing_horizontal() const;
  63. CSS::Length border_spacing_vertical() const;
  64. Optional<CSS::CaptionSide> caption_side() const;
  65. CSS::Clip clip() const;
  66. CSS::Display display() const;
  67. Optional<CSS::Float> float_() const;
  68. Optional<CSS::Clear> clear() const;
  69. struct ContentDataAndQuoteNestingLevel {
  70. CSS::ContentData content_data;
  71. u32 final_quote_nesting_level { 0 };
  72. };
  73. ContentDataAndQuoteNestingLevel content(u32 initial_quote_nesting_level) const;
  74. Optional<CSS::Cursor> cursor() const;
  75. Optional<CSS::WhiteSpace> white_space() const;
  76. Optional<CSS::LineStyle> line_style(CSS::PropertyID) const;
  77. Optional<CSS::OutlineStyle> outline_style() const;
  78. Vector<CSS::TextDecorationLine> text_decoration_line() const;
  79. Optional<CSS::TextDecorationStyle> text_decoration_style() const;
  80. Optional<CSS::TextTransform> text_transform() const;
  81. Vector<CSS::ShadowData> text_shadow(Layout::Node const&) const;
  82. Optional<CSS::ListStyleType> list_style_type() const;
  83. Optional<CSS::ListStylePosition> list_style_position() const;
  84. Optional<CSS::FlexDirection> flex_direction() const;
  85. Optional<CSS::FlexWrap> flex_wrap() const;
  86. Optional<CSS::FlexBasis> flex_basis() const;
  87. float flex_grow() const;
  88. float flex_shrink() const;
  89. int order() const;
  90. Optional<Color> accent_color(Layout::NodeWithStyle const&) const;
  91. Optional<CSS::AlignContent> align_content() const;
  92. Optional<CSS::AlignItems> align_items() const;
  93. Optional<CSS::AlignSelf> align_self() const;
  94. Optional<CSS::Appearance> appearance() const;
  95. CSS::BackdropFilter backdrop_filter() const;
  96. float opacity() const;
  97. Optional<CSS::Visibility> visibility() const;
  98. Optional<CSS::ImageRendering> image_rendering() const;
  99. Optional<CSS::JustifyContent> justify_content() const;
  100. Optional<CSS::JustifyItems> justify_items() const;
  101. Optional<CSS::JustifySelf> justify_self() const;
  102. Optional<CSS::Overflow> overflow_x() const;
  103. Optional<CSS::Overflow> overflow_y() const;
  104. Vector<CSS::ShadowData> box_shadow(Layout::Node const&) const;
  105. Optional<CSS::BoxSizing> box_sizing() const;
  106. Optional<CSS::PointerEvents> pointer_events() const;
  107. Variant<CSS::VerticalAlign, CSS::LengthPercentage> vertical_align() const;
  108. Optional<CSS::FontVariant> font_variant() const;
  109. CSS::GridTrackSizeList grid_auto_columns() const;
  110. CSS::GridTrackSizeList grid_auto_rows() const;
  111. CSS::GridTrackSizeList grid_template_columns() const;
  112. CSS::GridTrackSizeList grid_template_rows() const;
  113. [[nodiscard]] CSS::GridAutoFlow grid_auto_flow() const;
  114. CSS::GridTrackPlacement grid_column_end() const;
  115. CSS::GridTrackPlacement grid_column_start() const;
  116. CSS::GridTrackPlacement grid_row_end() const;
  117. CSS::GridTrackPlacement grid_row_start() const;
  118. Optional<CSS::BorderCollapse> border_collapse() const;
  119. Vector<Vector<String>> grid_template_areas() const;
  120. String grid_area() const;
  121. Optional<CSS::ObjectFit> object_fit() const;
  122. CSS::ObjectPosition object_position() const;
  123. Optional<CSS::TableLayout> table_layout() const;
  124. static Vector<CSS::Transformation> transformations_for_style_value(StyleValue const& value);
  125. Vector<CSS::Transformation> transformations() const;
  126. Optional<CSS::TransformBox> transform_box() const;
  127. CSS::TransformOrigin transform_origin() const;
  128. Optional<CSS::MaskType> mask_type() const;
  129. Color stop_color() const;
  130. float stop_opacity() const;
  131. float fill_opacity() const;
  132. float stroke_opacity() const;
  133. Optional<CSS::FillRule> fill_rule() const;
  134. Gfx::Font const& first_available_computed_font() const { return m_font_list->first(); }
  135. Gfx::FontCascadeList const& computed_font_list() const
  136. {
  137. VERIFY(m_font_list);
  138. return *m_font_list;
  139. }
  140. void set_computed_font_list(NonnullRefPtr<Gfx::FontCascadeList> font_list) const
  141. {
  142. m_font_list = move(font_list);
  143. }
  144. [[nodiscard]] CSSPixels compute_line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const;
  145. [[nodiscard]] CSSPixels compute_line_height(Layout::Node const&) const;
  146. [[nodiscard]] CSSPixels line_height() const { return *m_line_height; }
  147. void set_line_height(Badge<StyleComputer> const&, CSSPixels line_height) { m_line_height = line_height; }
  148. bool operator==(StyleProperties const&) const;
  149. Optional<CSS::Positioning> position() const;
  150. Optional<int> z_index() const;
  151. void set_math_depth(int math_depth);
  152. int math_depth() const { return m_math_depth; }
  153. QuotesData quotes() const;
  154. Optional<CSS::ScrollbarWidth> scrollbar_width() const;
  155. static NonnullRefPtr<Gfx::Font const> font_fallback(bool monospace, bool bold);
  156. private:
  157. friend class StyleComputer;
  158. PropertyValues m_property_values;
  159. Array<Optional<NonnullRefPtr<StyleValue const>>, to_underlying(CSS::last_property_id) + 1> m_animated_property_values;
  160. Optional<CSS::Overflow> overflow(CSS::PropertyID) const;
  161. Vector<CSS::ShadowData> shadow(CSS::PropertyID, Layout::Node const&) const;
  162. int m_math_depth { InitialValues::math_depth() };
  163. mutable RefPtr<Gfx::FontCascadeList> m_font_list;
  164. Optional<CSSPixels> m_line_height;
  165. };
  166. }