StyleProperties.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/LengthBox.h>
  12. #include <LibWeb/CSS/StyleValue.h>
  13. namespace Web::CSS {
  14. class StyleProperties : public RefCounted<StyleProperties> {
  15. public:
  16. StyleProperties();
  17. explicit StyleProperties(const StyleProperties&);
  18. static NonnullRefPtr<StyleProperties> create() { return adopt(*new StyleProperties); }
  19. NonnullRefPtr<StyleProperties> clone() const;
  20. template<typename Callback>
  21. inline void for_each_property(Callback callback) const
  22. {
  23. for (auto& it : m_property_values)
  24. callback((CSS::PropertyID)it.key, *it.value);
  25. }
  26. void set_property(CSS::PropertyID, NonnullRefPtr<StyleValue> value);
  27. void set_property(CSS::PropertyID, const StringView&);
  28. Optional<NonnullRefPtr<StyleValue>> property(CSS::PropertyID) const;
  29. Length length_or_fallback(CSS::PropertyID, const Length& fallback) const;
  30. 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;
  31. String string_or_fallback(CSS::PropertyID, const StringView& fallback) const;
  32. Color color_or_fallback(CSS::PropertyID, const DOM::Document&, Color fallback) const;
  33. Optional<CSS::TextAlign> text_align() const;
  34. CSS::Display display() const;
  35. Optional<CSS::Float> float_() const;
  36. Optional<CSS::Clear> clear() const;
  37. Optional<CSS::Cursor> cursor() const;
  38. Optional<CSS::WhiteSpace> white_space() const;
  39. Optional<CSS::LineStyle> line_style(CSS::PropertyID) const;
  40. Optional<CSS::TextDecorationLine> text_decoration_line() const;
  41. Optional<CSS::TextTransform> text_transform() const;
  42. Optional<CSS::ListStyleType> list_style_type() const;
  43. Optional<CSS::FlexDirection> flex_direction() const;
  44. Optional<CSS::Overflow> overflow_x() const;
  45. Optional<CSS::Overflow> overflow_y() const;
  46. Optional<CSS::Repeat> background_repeat_x() const;
  47. Optional<CSS::Repeat> background_repeat_y() const;
  48. const Gfx::Font& font() const
  49. {
  50. if (!m_font)
  51. load_font();
  52. return *m_font;
  53. }
  54. float line_height(const Layout::Node&) const;
  55. bool operator==(const StyleProperties&) const;
  56. bool operator!=(const StyleProperties& other) const { return !(*this == other); }
  57. Optional<CSS::Position> position() const;
  58. Optional<int> z_index() const;
  59. private:
  60. HashMap<unsigned, NonnullRefPtr<StyleValue>> m_property_values;
  61. Optional<CSS::Overflow> overflow(CSS::PropertyID) const;
  62. void load_font() const;
  63. RefPtr<Gfx::Font> font_fallback(bool monospace, bool bold) const;
  64. mutable RefPtr<Gfx::Font> m_font;
  65. };
  66. }