StyleValue.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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/Bitmap.h>
  34. #include <LibGfx/Color.h>
  35. #include <LibWeb/CSS/Length.h>
  36. #include <LibWeb/CSS/PropertyID.h>
  37. #include <LibWeb/Forward.h>
  38. #include <LibWeb/Loader/ImageResource.h>
  39. namespace Web::CSS {
  40. enum class ValueID {
  41. Invalid,
  42. VendorSpecificLink,
  43. VendorSpecificPaletteDesktopBackground,
  44. VendorSpecificPaletteActiveWindowBorder1,
  45. VendorSpecificPaletteActiveWindowBorder2,
  46. VendorSpecificPaletteActiveWindowTitle,
  47. VendorSpecificPaletteInactiveWindowBorder1,
  48. VendorSpecificPaletteInactiveWindowBorder2,
  49. VendorSpecificPaletteInactiveWindowTitle,
  50. VendorSpecificPaletteMovingWindowBorder1,
  51. VendorSpecificPaletteMovingWindowBorder2,
  52. VendorSpecificPaletteMovingWindowTitle,
  53. VendorSpecificPaletteHighlightWindowBorder1,
  54. VendorSpecificPaletteHighlightWindowBorder2,
  55. VendorSpecificPaletteHighlightWindowTitle,
  56. VendorSpecificPaletteMenuStripe,
  57. VendorSpecificPaletteMenuBase,
  58. VendorSpecificPaletteMenuBaseText,
  59. VendorSpecificPaletteMenuSelection,
  60. VendorSpecificPaletteMenuSelectionText,
  61. VendorSpecificPaletteWindow,
  62. VendorSpecificPaletteWindowText,
  63. VendorSpecificPaletteButton,
  64. VendorSpecificPaletteButtonText,
  65. VendorSpecificPaletteBase,
  66. VendorSpecificPaletteBaseText,
  67. VendorSpecificPaletteThreedHighlight,
  68. VendorSpecificPaletteThreedShadow1,
  69. VendorSpecificPaletteThreedShadow2,
  70. VendorSpecificPaletteHoverHighlight,
  71. VendorSpecificPaletteSelection,
  72. VendorSpecificPaletteSelectionText,
  73. VendorSpecificPaletteInactiveSelection,
  74. VendorSpecificPaletteInactiveSelectionText,
  75. VendorSpecificPaletteRubberBandFill,
  76. VendorSpecificPaletteRubberBandBorder,
  77. VendorSpecificPaletteLink,
  78. VendorSpecificPaletteActiveLink,
  79. VendorSpecificPaletteVisitedLink,
  80. VendorSpecificPaletteRuler,
  81. VendorSpecificPaletteRulerBorder,
  82. VendorSpecificPaletteRulerActiveText,
  83. VendorSpecificPaletteRulerInactiveText,
  84. VendorSpecificPaletteTextCursor,
  85. VendorSpecificPaletteFocusOutline,
  86. VendorSpecificPaletteSyntaxComment,
  87. VendorSpecificPaletteSyntaxNumber,
  88. VendorSpecificPaletteSyntaxString,
  89. VendorSpecificPaletteSyntaxType,
  90. VendorSpecificPaletteSyntaxPunctuation,
  91. VendorSpecificPaletteSyntaxOperator,
  92. VendorSpecificPaletteSyntaxKeyword,
  93. VendorSpecificPaletteSyntaxControlKeyword,
  94. VendorSpecificPaletteSyntaxIdentifier,
  95. VendorSpecificPaletteSyntaxPreprocessorStatement,
  96. VendorSpecificPaletteSyntaxPreprocessorValue,
  97. Center,
  98. Left,
  99. Right,
  100. Justify,
  101. VendorSpecificCenter,
  102. };
  103. enum class Position {
  104. Static,
  105. Relative,
  106. Absolute,
  107. Fixed,
  108. Sticky,
  109. };
  110. enum class TextAlign {
  111. Left,
  112. Center,
  113. Right,
  114. Justify,
  115. VendorSpecificCenter,
  116. };
  117. enum class Display {
  118. None,
  119. Block,
  120. Inline,
  121. InlineBlock,
  122. ListItem,
  123. Table,
  124. TableRow,
  125. TableCell,
  126. TableHeaderGroup,
  127. TableRowGroup,
  128. TableFooterGroup,
  129. };
  130. enum class WhiteSpace {
  131. Normal,
  132. Pre,
  133. Nowrap,
  134. PreLine,
  135. PreWrap,
  136. };
  137. enum class Float {
  138. None,
  139. Left,
  140. Right,
  141. };
  142. class StyleValue : public RefCounted<StyleValue> {
  143. public:
  144. virtual ~StyleValue();
  145. enum class Type {
  146. Invalid,
  147. Inherit,
  148. Initial,
  149. String,
  150. Length,
  151. Color,
  152. Identifier,
  153. Image,
  154. Position,
  155. };
  156. Type type() const { return m_type; }
  157. bool is_inherit() const { return type() == Type::Inherit; }
  158. bool is_initial() const { return type() == Type::Initial; }
  159. bool is_color() const { return type() == Type::Color; }
  160. bool is_identifier() const { return type() == Type::Identifier; }
  161. bool is_image() const { return type() == Type::Image; }
  162. bool is_string() const { return type() == Type::String; }
  163. bool is_length() const { return type() == Type::Length; }
  164. bool is_position() const { return type() == Type::Position; }
  165. virtual String to_string() const = 0;
  166. virtual Length to_length() const { return Length::make_auto(); }
  167. virtual Color to_color(const DOM::Document&) const { return {}; }
  168. virtual bool is_auto() const { return false; }
  169. protected:
  170. explicit StyleValue(Type);
  171. private:
  172. Type m_type { Type::Invalid };
  173. };
  174. class StringStyleValue : public StyleValue {
  175. public:
  176. static NonnullRefPtr<StringStyleValue> create(const String& string)
  177. {
  178. return adopt(*new StringStyleValue(string));
  179. }
  180. virtual ~StringStyleValue() override { }
  181. String to_string() const override { return m_string; }
  182. private:
  183. explicit StringStyleValue(const String& string)
  184. : StyleValue(Type::String)
  185. , m_string(string)
  186. {
  187. }
  188. String m_string;
  189. };
  190. class LengthStyleValue : public StyleValue {
  191. public:
  192. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  193. {
  194. return adopt(*new LengthStyleValue(length));
  195. }
  196. virtual ~LengthStyleValue() override { }
  197. virtual String to_string() const override { return m_length.to_string(); }
  198. virtual Length to_length() const override { return m_length; }
  199. const Length& length() const { return m_length; }
  200. virtual bool is_auto() const override { return m_length.is_auto(); }
  201. private:
  202. explicit LengthStyleValue(const Length& length)
  203. : StyleValue(Type::Length)
  204. , m_length(length)
  205. {
  206. }
  207. Length m_length;
  208. };
  209. class InitialStyleValue final : public StyleValue {
  210. public:
  211. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  212. virtual ~InitialStyleValue() override { }
  213. String to_string() const override { return "initial"; }
  214. private:
  215. InitialStyleValue()
  216. : StyleValue(Type::Initial)
  217. {
  218. }
  219. };
  220. class InheritStyleValue final : public StyleValue {
  221. public:
  222. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  223. virtual ~InheritStyleValue() override { }
  224. String to_string() const override { return "inherit"; }
  225. private:
  226. InheritStyleValue()
  227. : StyleValue(Type::Inherit)
  228. {
  229. }
  230. };
  231. class ColorStyleValue : public StyleValue {
  232. public:
  233. static NonnullRefPtr<ColorStyleValue> create(Color color)
  234. {
  235. return adopt(*new ColorStyleValue(color));
  236. }
  237. virtual ~ColorStyleValue() override { }
  238. Color color() const { return m_color; }
  239. String to_string() const override { return m_color.to_string(); }
  240. Color to_color(const DOM::Document&) const override { return m_color; }
  241. private:
  242. explicit ColorStyleValue(Color color)
  243. : StyleValue(Type::Color)
  244. , m_color(color)
  245. {
  246. }
  247. Color m_color;
  248. };
  249. class IdentifierStyleValue final : public StyleValue {
  250. public:
  251. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  252. {
  253. return adopt(*new IdentifierStyleValue(id));
  254. }
  255. virtual ~IdentifierStyleValue() override { }
  256. CSS::ValueID id() const { return m_id; }
  257. virtual String to_string() const override;
  258. virtual Color to_color(const DOM::Document&) const override;
  259. private:
  260. explicit IdentifierStyleValue(CSS::ValueID id)
  261. : StyleValue(Type::Identifier)
  262. , m_id(id)
  263. {
  264. }
  265. CSS::ValueID m_id { CSS::ValueID::Invalid };
  266. };
  267. class ImageStyleValue final
  268. : public StyleValue
  269. , public ImageResourceClient {
  270. public:
  271. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt(*new ImageStyleValue(url, document)); }
  272. virtual ~ImageStyleValue() override { }
  273. String to_string() const override { return String::format("Image{%s}", m_url.to_string().characters()); }
  274. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  275. private:
  276. ImageStyleValue(const URL&, DOM::Document&);
  277. // ^ResourceClient
  278. virtual void resource_did_load() override;
  279. URL m_url;
  280. WeakPtr<DOM::Document> m_document;
  281. RefPtr<Gfx::Bitmap> m_bitmap;
  282. };
  283. }