StyleValue.h 4.8 KB

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