StyleValue.h 9.4 KB

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