StyleProperties.h 1.6 KB

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