StyleValue.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/RefCounted.h>
  28. #include <AK/RefPtr.h>
  29. #include <AK/String.h>
  30. #include <AK/StringView.h>
  31. #include <AK/URL.h>
  32. #include <AK/WeakPtr.h>
  33. #include <LibGfx/Color.h>
  34. #include <LibGfx/Bitmap.h>
  35. #include <LibHTML/CSS/Length.h>
  36. #include <LibHTML/CSS/PropertyID.h>
  37. class Document;
  38. namespace CSS {
  39. enum class ValueID {
  40. Invalid,
  41. VendorSpecificLink,
  42. Center,
  43. Left,
  44. Right,
  45. Justify,
  46. };
  47. }
  48. class StyleValue : public RefCounted<StyleValue> {
  49. public:
  50. virtual ~StyleValue();
  51. enum class Type {
  52. Invalid,
  53. Inherit,
  54. Initial,
  55. String,
  56. Length,
  57. Color,
  58. Identifier,
  59. Image,
  60. };
  61. Type type() const { return m_type; }
  62. bool is_inherit() const { return type() == Type::Inherit; }
  63. bool is_initial() const { return type() == Type::Initial; }
  64. bool is_color() const { return type() == Type::Color; }
  65. bool is_identifier() const { return type() == Type::Identifier; }
  66. bool is_image() const { return type() == Type::Image; }
  67. bool is_string() const { return type() == Type::String; }
  68. bool is_length() const { return type() == Type::Length; }
  69. virtual String to_string() const = 0;
  70. virtual Length to_length() const { return {}; }
  71. virtual Color to_color(const Document&) const { return {}; }
  72. virtual bool is_auto() const { return false; }
  73. protected:
  74. explicit StyleValue(Type);
  75. private:
  76. Type m_type { Type::Invalid };
  77. };
  78. class StringStyleValue : public StyleValue {
  79. public:
  80. static NonnullRefPtr<StringStyleValue> create(const String& string)
  81. {
  82. return adopt(*new StringStyleValue(string));
  83. }
  84. virtual ~StringStyleValue() override {}
  85. String to_string() const override { return m_string; }
  86. private:
  87. explicit StringStyleValue(const String& string)
  88. : StyleValue(Type::String)
  89. , m_string(string)
  90. {
  91. }
  92. String m_string;
  93. };
  94. class LengthStyleValue : public StyleValue {
  95. public:
  96. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  97. {
  98. return adopt(*new LengthStyleValue(length));
  99. }
  100. virtual ~LengthStyleValue() override {}
  101. virtual String to_string() const override { return m_length.to_string(); }
  102. virtual Length to_length() const override { return m_length; }
  103. const Length& length() const { return m_length; }
  104. virtual bool is_auto() const override { return m_length.is_auto(); }
  105. private:
  106. explicit LengthStyleValue(const Length& length)
  107. : StyleValue(Type::Length)
  108. , m_length(length)
  109. {
  110. }
  111. Length m_length;
  112. };
  113. class InitialStyleValue final : public StyleValue {
  114. public:
  115. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  116. virtual ~InitialStyleValue() override {}
  117. String to_string() const override { return "initial"; }
  118. private:
  119. InitialStyleValue()
  120. : StyleValue(Type::Initial)
  121. {
  122. }
  123. };
  124. class InheritStyleValue final : public StyleValue {
  125. public:
  126. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  127. virtual ~InheritStyleValue() override {}
  128. String to_string() const override { return "inherit"; }
  129. private:
  130. InheritStyleValue()
  131. : StyleValue(Type::Inherit)
  132. {
  133. }
  134. };
  135. class ColorStyleValue : public StyleValue {
  136. public:
  137. static NonnullRefPtr<ColorStyleValue> create(Color color)
  138. {
  139. return adopt(*new ColorStyleValue(color));
  140. }
  141. virtual ~ColorStyleValue() override {}
  142. Color color() const { return m_color; }
  143. String to_string() const override { return m_color.to_string(); }
  144. Color to_color(const Document&) const override { return m_color; }
  145. private:
  146. explicit ColorStyleValue(Color color)
  147. : StyleValue(Type::Color)
  148. , m_color(color)
  149. {
  150. }
  151. Color m_color;
  152. };
  153. class IdentifierStyleValue final : public StyleValue {
  154. public:
  155. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  156. {
  157. return adopt(*new IdentifierStyleValue(id));
  158. }
  159. virtual ~IdentifierStyleValue() override {}
  160. CSS::ValueID id() const { return m_id; }
  161. virtual String to_string() const override;
  162. virtual Color to_color(const Document&) const override;
  163. private:
  164. explicit IdentifierStyleValue(CSS::ValueID id)
  165. : StyleValue(Type::Identifier)
  166. , m_id(id)
  167. {
  168. }
  169. CSS::ValueID m_id { CSS::ValueID::Invalid };
  170. };
  171. class ImageStyleValue final : public StyleValue {
  172. public:
  173. static NonnullRefPtr<ImageStyleValue> create(const URL& url, Document& document) { return adopt(*new ImageStyleValue(url, document)); }
  174. virtual ~ImageStyleValue() override {}
  175. String to_string() const override { return String::format("Image{%s}", m_url.to_string().characters()); }
  176. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  177. private:
  178. ImageStyleValue(const URL&, Document&);
  179. URL m_url;
  180. WeakPtr<Document> m_document;
  181. RefPtr<Gfx::Bitmap> m_bitmap;
  182. };