StyleValue.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #pragma once
  2. #include <AK/RefCounted.h>
  3. #include <AK/RefPtr.h>
  4. #include <AK/String.h>
  5. #include <AK/StringView.h>
  6. #include <AK/URL.h>
  7. #include <AK/WeakPtr.h>
  8. #include <LibDraw/Color.h>
  9. #include <LibDraw/GraphicsBitmap.h>
  10. #include <LibHTML/CSS/Length.h>
  11. #include <LibHTML/CSS/PropertyID.h>
  12. class Document;
  13. namespace CSS {
  14. enum class ValueID {
  15. Invalid,
  16. VendorSpecificLink,
  17. Center,
  18. Left,
  19. Right,
  20. };
  21. }
  22. class StyleValue : public RefCounted<StyleValue> {
  23. public:
  24. virtual ~StyleValue();
  25. enum class Type {
  26. Invalid,
  27. Inherit,
  28. Initial,
  29. String,
  30. Length,
  31. Color,
  32. Identifier,
  33. Image,
  34. };
  35. Type type() const { return m_type; }
  36. bool is_inherit() const { return type() == Type::Inherit; }
  37. bool is_initial() const { return type() == Type::Initial; }
  38. bool is_color() const { return type() == Type::Color; }
  39. bool is_identifier() const { return type() == Type::Identifier; }
  40. bool is_image() const { return type() == Type::Image; }
  41. virtual String to_string() const = 0;
  42. virtual Length to_length() const { return {}; }
  43. virtual Color to_color(const Document&) const { return {}; }
  44. virtual bool is_auto() const { return false; }
  45. protected:
  46. explicit StyleValue(Type);
  47. private:
  48. Type m_type { Type::Invalid };
  49. };
  50. class StringStyleValue : public StyleValue {
  51. public:
  52. static NonnullRefPtr<StringStyleValue> create(const String& string)
  53. {
  54. return adopt(*new StringStyleValue(string));
  55. }
  56. virtual ~StringStyleValue() override {}
  57. String to_string() const override { return m_string; }
  58. private:
  59. explicit StringStyleValue(const String& string)
  60. : StyleValue(Type::String)
  61. , m_string(string)
  62. {
  63. }
  64. String m_string;
  65. };
  66. class LengthStyleValue : public StyleValue {
  67. public:
  68. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  69. {
  70. return adopt(*new LengthStyleValue(length));
  71. }
  72. virtual ~LengthStyleValue() override {}
  73. virtual String to_string() const override { return m_length.to_string(); }
  74. virtual Length to_length() const override { return m_length; }
  75. const Length& length() const { return m_length; }
  76. virtual bool is_auto() const override { return m_length.is_auto(); }
  77. private:
  78. explicit LengthStyleValue(const Length& length)
  79. : StyleValue(Type::Length)
  80. , m_length(length)
  81. {
  82. }
  83. Length m_length;
  84. };
  85. class InitialStyleValue final : public StyleValue {
  86. public:
  87. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  88. virtual ~InitialStyleValue() override {}
  89. String to_string() const override { return "initial"; }
  90. private:
  91. InitialStyleValue()
  92. : StyleValue(Type::Initial)
  93. {
  94. }
  95. };
  96. class InheritStyleValue final : public StyleValue {
  97. public:
  98. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  99. virtual ~InheritStyleValue() override {}
  100. String to_string() const override { return "inherit"; }
  101. private:
  102. InheritStyleValue()
  103. : StyleValue(Type::Inherit)
  104. {
  105. }
  106. };
  107. class ColorStyleValue : public StyleValue {
  108. public:
  109. static NonnullRefPtr<ColorStyleValue> create(Color color)
  110. {
  111. return adopt(*new ColorStyleValue(color));
  112. }
  113. virtual ~ColorStyleValue() override {}
  114. Color color() const { return m_color; }
  115. String to_string() const override { return m_color.to_string(); }
  116. Color to_color(const Document&) const override { return m_color; }
  117. private:
  118. explicit ColorStyleValue(Color color)
  119. : StyleValue(Type::Color)
  120. , m_color(color)
  121. {
  122. }
  123. Color m_color;
  124. };
  125. class IdentifierStyleValue final : public StyleValue {
  126. public:
  127. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  128. {
  129. return adopt(*new IdentifierStyleValue(id));
  130. }
  131. virtual ~IdentifierStyleValue() override {}
  132. CSS::ValueID id() const { return m_id; }
  133. virtual String to_string() const override;
  134. virtual Color to_color(const Document&) const override;
  135. private:
  136. explicit IdentifierStyleValue(CSS::ValueID id)
  137. : StyleValue(Type::Identifier)
  138. , m_id(id)
  139. {
  140. }
  141. CSS::ValueID m_id { CSS::ValueID::Invalid };
  142. };
  143. class ImageStyleValue final : public StyleValue {
  144. public:
  145. static NonnullRefPtr<ImageStyleValue> create(const URL& url, Document& document) { return adopt(*new ImageStyleValue(url, document)); }
  146. virtual ~ImageStyleValue() override {}
  147. String to_string() const override { return String::format("Image{%s}", m_url.to_string().characters()); }
  148. const GraphicsBitmap* bitmap() const { return m_bitmap; }
  149. private:
  150. ImageStyleValue(const URL&, Document&);
  151. URL m_url;
  152. WeakPtr<Document> m_document;
  153. RefPtr<GraphicsBitmap> m_bitmap;
  154. };