StyleValue.h 9.1 KB

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