StyleProperties.h 8.2 KB

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