StyleProperties.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. StyleProperties();
  10. explicit StyleProperties(const StyleProperties&);
  11. static NonnullRefPtr<StyleProperties> create() { return adopt(*new StyleProperties); }
  12. NonnullRefPtr<StyleProperties> clone() const;
  13. template<typename Callback>
  14. inline void for_each_property(Callback callback) const
  15. {
  16. for (auto& it : m_property_values)
  17. callback((CSS::PropertyID)it.key, *it.value);
  18. }
  19. void set_property(CSS::PropertyID, NonnullRefPtr<StyleValue> value);
  20. Optional<NonnullRefPtr<StyleValue>> property(CSS::PropertyID) const;
  21. Length length_or_fallback(CSS::PropertyID, const Length& fallback) const;
  22. String string_or_fallback(CSS::PropertyID, const StringView& fallback) const;
  23. Color color_or_fallback(CSS::PropertyID, const Document&, Color fallback) const;
  24. const Font& font() const
  25. {
  26. if (!m_font)
  27. load_font();
  28. return *m_font;
  29. }
  30. float line_height() const;
  31. bool operator==(const StyleProperties&) const;
  32. bool operator!=(const StyleProperties& other) const { return !(*this == other); }
  33. private:
  34. HashMap<unsigned, NonnullRefPtr<StyleValue>> m_property_values;
  35. void load_font() const;
  36. mutable RefPtr<Font> m_font;
  37. };