StyleValue.h 4.9 KB

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