StyleProperties.h 6.9 KB

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