StyleValue.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <LibWeb/CSS/Length.h>
  36. #include <LibWeb/CSS/PropertyID.h>
  37. namespace Web {
  38. class Document;
  39. namespace CSS {
  40. enum class ValueID {
  41. Invalid,
  42. VendorSpecificLink,
  43. Center,
  44. Left,
  45. Right,
  46. Justify,
  47. };
  48. enum class Position {
  49. Static,
  50. Relative,
  51. Absolute,
  52. Fixed,
  53. Sticky,
  54. };
  55. }
  56. class StyleValue : public RefCounted<StyleValue> {
  57. public:
  58. virtual ~StyleValue();
  59. enum class Type {
  60. Invalid,
  61. Inherit,
  62. Initial,
  63. String,
  64. Length,
  65. Color,
  66. Identifier,
  67. Image,
  68. Position,
  69. };
  70. Type type() const { return m_type; }
  71. bool is_inherit() const { return type() == Type::Inherit; }
  72. bool is_initial() const { return type() == Type::Initial; }
  73. bool is_color() const { return type() == Type::Color; }
  74. bool is_identifier() const { return type() == Type::Identifier; }
  75. bool is_image() const { return type() == Type::Image; }
  76. bool is_string() const { return type() == Type::String; }
  77. bool is_length() const { return type() == Type::Length; }
  78. bool is_position() const { return type() == Type::Position; }
  79. virtual String to_string() const = 0;
  80. virtual Length to_length() const { return {}; }
  81. virtual Color to_color(const Document&) const { return {}; }
  82. virtual bool is_auto() const { return false; }
  83. protected:
  84. explicit StyleValue(Type);
  85. private:
  86. Type m_type { Type::Invalid };
  87. };
  88. class StringStyleValue : public StyleValue {
  89. public:
  90. static NonnullRefPtr<StringStyleValue> create(const String& string)
  91. {
  92. return adopt(*new StringStyleValue(string));
  93. }
  94. virtual ~StringStyleValue() override {}
  95. String to_string() const override { return m_string; }
  96. private:
  97. explicit StringStyleValue(const String& string)
  98. : StyleValue(Type::String)
  99. , m_string(string)
  100. {
  101. }
  102. String m_string;
  103. };
  104. class LengthStyleValue : public StyleValue {
  105. public:
  106. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  107. {
  108. return adopt(*new LengthStyleValue(length));
  109. }
  110. virtual ~LengthStyleValue() override {}
  111. virtual String to_string() const override { return m_length.to_string(); }
  112. virtual Length to_length() const override { return m_length; }
  113. const Length& length() const { return m_length; }
  114. virtual bool is_auto() const override { return m_length.is_auto(); }
  115. private:
  116. explicit LengthStyleValue(const Length& length)
  117. : StyleValue(Type::Length)
  118. , m_length(length)
  119. {
  120. }
  121. Length m_length;
  122. };
  123. class InitialStyleValue final : public StyleValue {
  124. public:
  125. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  126. virtual ~InitialStyleValue() override {}
  127. String to_string() const override { return "initial"; }
  128. private:
  129. InitialStyleValue()
  130. : StyleValue(Type::Initial)
  131. {
  132. }
  133. };
  134. class InheritStyleValue final : public StyleValue {
  135. public:
  136. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  137. virtual ~InheritStyleValue() override {}
  138. String to_string() const override { return "inherit"; }
  139. private:
  140. InheritStyleValue()
  141. : StyleValue(Type::Inherit)
  142. {
  143. }
  144. };
  145. class ColorStyleValue : public StyleValue {
  146. public:
  147. static NonnullRefPtr<ColorStyleValue> create(Color color)
  148. {
  149. return adopt(*new ColorStyleValue(color));
  150. }
  151. virtual ~ColorStyleValue() override {}
  152. Color color() const { return m_color; }
  153. String to_string() const override { return m_color.to_string(); }
  154. Color to_color(const Document&) const override { return m_color; }
  155. private:
  156. explicit ColorStyleValue(Color color)
  157. : StyleValue(Type::Color)
  158. , m_color(color)
  159. {
  160. }
  161. Color m_color;
  162. };
  163. class IdentifierStyleValue final : public StyleValue {
  164. public:
  165. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  166. {
  167. return adopt(*new IdentifierStyleValue(id));
  168. }
  169. virtual ~IdentifierStyleValue() override {}
  170. CSS::ValueID id() const { return m_id; }
  171. virtual String to_string() const override;
  172. virtual Color to_color(const Document&) const override;
  173. private:
  174. explicit IdentifierStyleValue(CSS::ValueID id)
  175. : StyleValue(Type::Identifier)
  176. , m_id(id)
  177. {
  178. }
  179. CSS::ValueID m_id { CSS::ValueID::Invalid };
  180. };
  181. class ImageStyleValue final : public StyleValue {
  182. public:
  183. static NonnullRefPtr<ImageStyleValue> create(const URL& url, Document& document) { return adopt(*new ImageStyleValue(url, document)); }
  184. virtual ~ImageStyleValue() override {}
  185. String to_string() const override { return String::format("Image{%s}", m_url.to_string().characters()); }
  186. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  187. private:
  188. ImageStyleValue(const URL&, Document&);
  189. URL m_url;
  190. WeakPtr<Document> m_document;
  191. RefPtr<Gfx::Bitmap> m_bitmap;
  192. };
  193. }