StyleProperties.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2018-2020, 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.h>
  10. #include <LibGfx/Forward.h>
  11. #include <LibWeb/CSS/ComputedValues.h>
  12. #include <LibWeb/CSS/LengthBox.h>
  13. #include <LibWeb/CSS/StyleValue.h>
  14. namespace Web::CSS {
  15. class StyleProperties : public RefCounted<StyleProperties> {
  16. public:
  17. StyleProperties() = default;
  18. explicit StyleProperties(const StyleProperties&);
  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])
  26. callback((CSS::PropertyID)i, *m_property_values[i]);
  27. }
  28. }
  29. auto& properties() { return m_property_values; }
  30. auto const& properties() const { return m_property_values; }
  31. void set_property(CSS::PropertyID, NonnullRefPtr<StyleValue> value);
  32. Optional<NonnullRefPtr<StyleValue>> property(CSS::PropertyID) const;
  33. Length length_or_fallback(CSS::PropertyID, Length const& fallback) const;
  34. LengthPercentage length_percentage_or_fallback(CSS::PropertyID, LengthPercentage const& fallback) const;
  35. Optional<LengthPercentage> length_percentage(CSS::PropertyID) const;
  36. 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;
  37. Color color_or_fallback(CSS::PropertyID, Layout::NodeWithStyle const&, Color fallback) const;
  38. Optional<CSS::TextAlign> text_align() const;
  39. Optional<CSS::TextJustify> text_justify() const;
  40. CSS::Display display() const;
  41. Optional<CSS::Float> float_() const;
  42. Optional<CSS::Clear> clear() const;
  43. CSS::ContentData content() const;
  44. Optional<CSS::Cursor> cursor() const;
  45. Optional<CSS::WhiteSpace> white_space() const;
  46. Optional<CSS::LineStyle> line_style(CSS::PropertyID) const;
  47. Optional<CSS::TextDecorationLine> text_decoration_line() const;
  48. Optional<CSS::TextDecorationStyle> text_decoration_style() const;
  49. Optional<CSS::TextTransform> text_transform() const;
  50. Optional<CSS::ListStyleType> list_style_type() const;
  51. Optional<CSS::FlexDirection> flex_direction() const;
  52. Optional<CSS::FlexWrap> flex_wrap() const;
  53. Optional<CSS::FlexBasisData> flex_basis() const;
  54. float flex_grow() const;
  55. float flex_shrink() const;
  56. Optional<CSS::AlignItems> align_items() const;
  57. float opacity() const;
  58. Optional<CSS::Visibility> visibility() const;
  59. Optional<CSS::ImageRendering> image_rendering() const;
  60. Optional<CSS::JustifyContent> justify_content() const;
  61. Optional<CSS::Overflow> overflow_x() const;
  62. Optional<CSS::Overflow> overflow_y() const;
  63. Vector<CSS::BoxShadowData> box_shadow() const;
  64. CSS::BoxSizing box_sizing() const;
  65. Optional<CSS::PointerEvents> pointer_events() const;
  66. Variant<CSS::VerticalAlign, CSS::LengthPercentage> vertical_align() const;
  67. Vector<CSS::Transformation> transformations() const;
  68. CSS::TransformOrigin transform_origin() const;
  69. Gfx::Font const& computed_font() const
  70. {
  71. VERIFY(m_font);
  72. return *m_font;
  73. }
  74. void set_computed_font(NonnullRefPtr<Gfx::Font> font)
  75. {
  76. m_font = move(font);
  77. }
  78. float line_height(const Layout::Node&) const;
  79. bool operator==(const StyleProperties&) const;
  80. bool operator!=(const StyleProperties& other) const { return !(*this == other); }
  81. Optional<CSS::Position> position() const;
  82. Optional<int> z_index() const;
  83. static NonnullRefPtr<Gfx::Font> font_fallback(bool monospace, bool bold);
  84. private:
  85. friend class StyleComputer;
  86. Array<RefPtr<StyleValue>, to_underlying(CSS::last_property_id) + 1> m_property_values;
  87. Optional<CSS::Overflow> overflow(CSS::PropertyID) const;
  88. mutable RefPtr<Gfx::Font> m_font;
  89. };
  90. }