StyleProperties.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #pragma once
  2. #include <AK/HashMap.h>
  3. #include <AK/NonnullRefPtr.h>
  4. #include <LibDraw/Font.h>
  5. #include <LibHTML/CSS/StyleValue.h>
  6. class Color;
  7. class StyleProperties : public RefCounted<StyleProperties> {
  8. public:
  9. static NonnullRefPtr<StyleProperties> create() { return adopt(*new StyleProperties); }
  10. template<typename Callback>
  11. inline void for_each_property(Callback callback) const
  12. {
  13. for (auto& it : m_property_values)
  14. callback((CSS::PropertyID)it.key, *it.value);
  15. }
  16. void set_property(CSS::PropertyID, NonnullRefPtr<StyleValue> value);
  17. Optional<NonnullRefPtr<StyleValue>> property(CSS::PropertyID) const;
  18. Length length_or_fallback(CSS::PropertyID, const Length& fallback) const;
  19. String string_or_fallback(CSS::PropertyID, const StringView& fallback) const;
  20. Color color_or_fallback(CSS::PropertyID, const Document&, Color fallback) const;
  21. const Font& font() const
  22. {
  23. if (!m_font)
  24. load_font();
  25. return *m_font;
  26. }
  27. int line_height() const;
  28. bool operator==(const StyleProperties&) const;
  29. bool operator!=(const StyleProperties& other) const { return !(*this == other); }
  30. private:
  31. HashMap<unsigned, NonnullRefPtr<StyleValue>> m_property_values;
  32. void load_font() const;
  33. mutable RefPtr<Font> m_font;
  34. };