StyleValue.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. }
  143. namespace Web {
  144. class StyleValue : public RefCounted<StyleValue> {
  145. public:
  146. virtual ~StyleValue();
  147. enum class Type {
  148. Invalid,
  149. Inherit,
  150. Initial,
  151. String,
  152. Length,
  153. Color,
  154. Identifier,
  155. Image,
  156. Position,
  157. };
  158. Type type() const { return m_type; }
  159. bool is_inherit() const { return type() == Type::Inherit; }
  160. bool is_initial() const { return type() == Type::Initial; }
  161. bool is_color() const { return type() == Type::Color; }
  162. bool is_identifier() const { return type() == Type::Identifier; }
  163. bool is_image() const { return type() == Type::Image; }
  164. bool is_string() const { return type() == Type::String; }
  165. bool is_length() const { return type() == Type::Length; }
  166. bool is_position() const { return type() == Type::Position; }
  167. virtual String to_string() const = 0;
  168. virtual Length to_length() const { return Length::make_auto(); }
  169. virtual Color to_color(const DOM::Document&) const { return {}; }
  170. virtual bool is_auto() const { return false; }
  171. protected:
  172. explicit StyleValue(Type);
  173. private:
  174. Type m_type { Type::Invalid };
  175. };
  176. class StringStyleValue : public StyleValue {
  177. public:
  178. static NonnullRefPtr<StringStyleValue> create(const String& string)
  179. {
  180. return adopt(*new StringStyleValue(string));
  181. }
  182. virtual ~StringStyleValue() override { }
  183. String to_string() const override { return m_string; }
  184. private:
  185. explicit StringStyleValue(const String& string)
  186. : StyleValue(Type::String)
  187. , m_string(string)
  188. {
  189. }
  190. String m_string;
  191. };
  192. class LengthStyleValue : public StyleValue {
  193. public:
  194. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  195. {
  196. return adopt(*new LengthStyleValue(length));
  197. }
  198. virtual ~LengthStyleValue() override { }
  199. virtual String to_string() const override { return m_length.to_string(); }
  200. virtual Length to_length() const override { return m_length; }
  201. const Length& length() const { return m_length; }
  202. virtual bool is_auto() const override { return m_length.is_auto(); }
  203. private:
  204. explicit LengthStyleValue(const Length& length)
  205. : StyleValue(Type::Length)
  206. , m_length(length)
  207. {
  208. }
  209. Length m_length;
  210. };
  211. class InitialStyleValue final : public StyleValue {
  212. public:
  213. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  214. virtual ~InitialStyleValue() override { }
  215. String to_string() const override { return "initial"; }
  216. private:
  217. InitialStyleValue()
  218. : StyleValue(Type::Initial)
  219. {
  220. }
  221. };
  222. class InheritStyleValue final : public StyleValue {
  223. public:
  224. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  225. virtual ~InheritStyleValue() override { }
  226. String to_string() const override { return "inherit"; }
  227. private:
  228. InheritStyleValue()
  229. : StyleValue(Type::Inherit)
  230. {
  231. }
  232. };
  233. class ColorStyleValue : public StyleValue {
  234. public:
  235. static NonnullRefPtr<ColorStyleValue> create(Color color)
  236. {
  237. return adopt(*new ColorStyleValue(color));
  238. }
  239. virtual ~ColorStyleValue() override { }
  240. Color color() const { return m_color; }
  241. String to_string() const override { return m_color.to_string(); }
  242. Color to_color(const DOM::Document&) const override { return m_color; }
  243. private:
  244. explicit ColorStyleValue(Color color)
  245. : StyleValue(Type::Color)
  246. , m_color(color)
  247. {
  248. }
  249. Color m_color;
  250. };
  251. class IdentifierStyleValue final : public StyleValue {
  252. public:
  253. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  254. {
  255. return adopt(*new IdentifierStyleValue(id));
  256. }
  257. virtual ~IdentifierStyleValue() override { }
  258. CSS::ValueID id() const { return m_id; }
  259. virtual String to_string() const override;
  260. virtual Color to_color(const DOM::Document&) const override;
  261. private:
  262. explicit IdentifierStyleValue(CSS::ValueID id)
  263. : StyleValue(Type::Identifier)
  264. , m_id(id)
  265. {
  266. }
  267. CSS::ValueID m_id { CSS::ValueID::Invalid };
  268. };
  269. class ImageStyleValue final
  270. : public StyleValue
  271. , public ImageResourceClient {
  272. public:
  273. static NonnullRefPtr<ImageStyleValue> create(const URL& url, DOM::Document& document) { return adopt(*new ImageStyleValue(url, document)); }
  274. virtual ~ImageStyleValue() override { }
  275. String to_string() const override { return String::format("Image{%s}", m_url.to_string().characters()); }
  276. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  277. private:
  278. ImageStyleValue(const URL&, DOM::Document&);
  279. // ^ResourceClient
  280. virtual void resource_did_load() override;
  281. URL m_url;
  282. WeakPtr<DOM::Document> m_document;
  283. RefPtr<Gfx::Bitmap> m_bitmap;
  284. };
  285. }