StyleValue.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. };
  103. enum class Position {
  104. Static,
  105. Relative,
  106. Absolute,
  107. Fixed,
  108. Sticky,
  109. };
  110. }
  111. class StyleValue : public RefCounted<StyleValue> {
  112. public:
  113. virtual ~StyleValue();
  114. enum class Type {
  115. Invalid,
  116. Inherit,
  117. Initial,
  118. String,
  119. Length,
  120. Percentage,
  121. Color,
  122. Identifier,
  123. Image,
  124. Position,
  125. };
  126. Type type() const { return m_type; }
  127. bool is_inherit() const { return type() == Type::Inherit; }
  128. bool is_initial() const { return type() == Type::Initial; }
  129. bool is_color() const { return type() == Type::Color; }
  130. bool is_identifier() const { return type() == Type::Identifier; }
  131. bool is_image() const { return type() == Type::Image; }
  132. bool is_string() const { return type() == Type::String; }
  133. bool is_length() const { return type() == Type::Length; }
  134. bool is_percentage() const { return type() == Type::Percentage; }
  135. bool is_position() const { return type() == Type::Position; }
  136. virtual String to_string() const = 0;
  137. virtual Length to_length() const { return {}; }
  138. virtual Color to_color(const Document&) const { return {}; }
  139. virtual bool is_auto() const { return false; }
  140. protected:
  141. explicit StyleValue(Type);
  142. private:
  143. Type m_type { Type::Invalid };
  144. };
  145. class StringStyleValue : public StyleValue {
  146. public:
  147. static NonnullRefPtr<StringStyleValue> create(const String& string)
  148. {
  149. return adopt(*new StringStyleValue(string));
  150. }
  151. virtual ~StringStyleValue() override { }
  152. String to_string() const override { return m_string; }
  153. private:
  154. explicit StringStyleValue(const String& string)
  155. : StyleValue(Type::String)
  156. , m_string(string)
  157. {
  158. }
  159. String m_string;
  160. };
  161. class LengthStyleValue : public StyleValue {
  162. public:
  163. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  164. {
  165. return adopt(*new LengthStyleValue(length));
  166. }
  167. virtual ~LengthStyleValue() override { }
  168. virtual String to_string() const override { return m_length.to_string(); }
  169. virtual Length to_length() const override { return m_length; }
  170. const Length& length() const { return m_length; }
  171. virtual bool is_auto() const override { return m_length.is_auto(); }
  172. private:
  173. explicit LengthStyleValue(const Length& length)
  174. : StyleValue(Type::Length)
  175. , m_length(length)
  176. {
  177. }
  178. Length m_length;
  179. };
  180. class PercentageStyleValue : public StyleValue {
  181. public:
  182. static NonnullRefPtr<PercentageStyleValue> create(float percentage)
  183. {
  184. return adopt(*new PercentageStyleValue(percentage));
  185. }
  186. virtual ~PercentageStyleValue() override { }
  187. virtual String to_string() const override { return String::format("%g%%", m_percentage); }
  188. Length to_length(float reference) const { return Length((m_percentage / 100.0f) * reference, Length::Type::Px); }
  189. private:
  190. virtual Length to_length() const override { return {}; }
  191. virtual bool is_auto() const override { return false; }
  192. explicit PercentageStyleValue(float percentage)
  193. : StyleValue(Type::Percentage)
  194. , m_percentage(percentage)
  195. {
  196. }
  197. float m_percentage { 0 };
  198. };
  199. class InitialStyleValue final : public StyleValue {
  200. public:
  201. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  202. virtual ~InitialStyleValue() override { }
  203. String to_string() const override { return "initial"; }
  204. private:
  205. InitialStyleValue()
  206. : StyleValue(Type::Initial)
  207. {
  208. }
  209. };
  210. class InheritStyleValue final : public StyleValue {
  211. public:
  212. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  213. virtual ~InheritStyleValue() override { }
  214. String to_string() const override { return "inherit"; }
  215. private:
  216. InheritStyleValue()
  217. : StyleValue(Type::Inherit)
  218. {
  219. }
  220. };
  221. class ColorStyleValue : public StyleValue {
  222. public:
  223. static NonnullRefPtr<ColorStyleValue> create(Color color)
  224. {
  225. return adopt(*new ColorStyleValue(color));
  226. }
  227. virtual ~ColorStyleValue() override { }
  228. Color color() const { return m_color; }
  229. String to_string() const override { return m_color.to_string(); }
  230. Color to_color(const Document&) const override { return m_color; }
  231. private:
  232. explicit ColorStyleValue(Color color)
  233. : StyleValue(Type::Color)
  234. , m_color(color)
  235. {
  236. }
  237. Color m_color;
  238. };
  239. class IdentifierStyleValue final : public StyleValue {
  240. public:
  241. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  242. {
  243. return adopt(*new IdentifierStyleValue(id));
  244. }
  245. virtual ~IdentifierStyleValue() override { }
  246. CSS::ValueID id() const { return m_id; }
  247. virtual String to_string() const override;
  248. virtual Color to_color(const Document&) const override;
  249. private:
  250. explicit IdentifierStyleValue(CSS::ValueID id)
  251. : StyleValue(Type::Identifier)
  252. , m_id(id)
  253. {
  254. }
  255. CSS::ValueID m_id { CSS::ValueID::Invalid };
  256. };
  257. class ImageStyleValue final
  258. : public StyleValue
  259. , public ImageResourceClient {
  260. public:
  261. static NonnullRefPtr<ImageStyleValue> create(const URL& url, Document& document) { return adopt(*new ImageStyleValue(url, document)); }
  262. virtual ~ImageStyleValue() override { }
  263. String to_string() const override { return String::format("Image{%s}", m_url.to_string().characters()); }
  264. const Gfx::Bitmap* bitmap() const { return m_bitmap; }
  265. private:
  266. ImageStyleValue(const URL&, Document&);
  267. // ^ResourceClient
  268. virtual void resource_did_load() override;
  269. URL m_url;
  270. WeakPtr<Document> m_document;
  271. RefPtr<Gfx::Bitmap> m_bitmap;
  272. };
  273. }