StyleValue.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include <AK/ByteBuffer.h>
  27. #include <LibGfx/PNGLoader.h>
  28. #include <LibGfx/Palette.h>
  29. #include <LibWeb/CSS/StyleValue.h>
  30. #include <LibWeb/DOM/Document.h>
  31. #include <LibWeb/InProcessWebView.h>
  32. #include <LibWeb/Loader/LoadRequest.h>
  33. #include <LibWeb/Loader/ResourceLoader.h>
  34. #include <LibWeb/Page/Frame.h>
  35. namespace Web::CSS {
  36. StyleValue::StyleValue(Type type)
  37. : m_type(type)
  38. {
  39. }
  40. StyleValue::~StyleValue()
  41. {
  42. }
  43. String IdentifierStyleValue::to_string() const
  44. {
  45. return CSS::string_from_value_id(m_id);
  46. }
  47. Color IdentifierStyleValue::to_color(const DOM::Document& document) const
  48. {
  49. if (id() == CSS::ValueID::LibwebLink)
  50. return document.link_color();
  51. ASSERT(document.page());
  52. auto palette = document.page()->palette();
  53. switch (id()) {
  54. case CSS::ValueID::LibwebPaletteDesktopBackground:
  55. return palette.color(ColorRole::DesktopBackground);
  56. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  57. return palette.color(ColorRole::ActiveWindowBorder1);
  58. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  59. return palette.color(ColorRole::ActiveWindowBorder2);
  60. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  61. return palette.color(ColorRole::ActiveWindowTitle);
  62. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  63. return palette.color(ColorRole::InactiveWindowBorder1);
  64. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  65. return palette.color(ColorRole::InactiveWindowBorder2);
  66. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  67. return palette.color(ColorRole::InactiveWindowTitle);
  68. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  69. return palette.color(ColorRole::MovingWindowBorder1);
  70. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  71. return palette.color(ColorRole::MovingWindowBorder2);
  72. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  73. return palette.color(ColorRole::MovingWindowTitle);
  74. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  75. return palette.color(ColorRole::HighlightWindowBorder1);
  76. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  77. return palette.color(ColorRole::HighlightWindowBorder2);
  78. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  79. return palette.color(ColorRole::HighlightWindowTitle);
  80. case CSS::ValueID::LibwebPaletteMenuStripe:
  81. return palette.color(ColorRole::MenuStripe);
  82. case CSS::ValueID::LibwebPaletteMenuBase:
  83. return palette.color(ColorRole::MenuBase);
  84. case CSS::ValueID::LibwebPaletteMenuBaseText:
  85. return palette.color(ColorRole::MenuBaseText);
  86. case CSS::ValueID::LibwebPaletteMenuSelection:
  87. return palette.color(ColorRole::MenuSelection);
  88. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  89. return palette.color(ColorRole::MenuSelectionText);
  90. case CSS::ValueID::LibwebPaletteWindow:
  91. return palette.color(ColorRole::Window);
  92. case CSS::ValueID::LibwebPaletteWindowText:
  93. return palette.color(ColorRole::WindowText);
  94. case CSS::ValueID::LibwebPaletteButton:
  95. return palette.color(ColorRole::Button);
  96. case CSS::ValueID::LibwebPaletteButtonText:
  97. return palette.color(ColorRole::ButtonText);
  98. case CSS::ValueID::LibwebPaletteBase:
  99. return palette.color(ColorRole::Base);
  100. case CSS::ValueID::LibwebPaletteBaseText:
  101. return palette.color(ColorRole::BaseText);
  102. case CSS::ValueID::LibwebPaletteThreedHighlight:
  103. return palette.color(ColorRole::ThreedHighlight);
  104. case CSS::ValueID::LibwebPaletteThreedShadow1:
  105. return palette.color(ColorRole::ThreedShadow1);
  106. case CSS::ValueID::LibwebPaletteThreedShadow2:
  107. return palette.color(ColorRole::ThreedShadow2);
  108. case CSS::ValueID::LibwebPaletteHoverHighlight:
  109. return palette.color(ColorRole::HoverHighlight);
  110. case CSS::ValueID::LibwebPaletteSelection:
  111. return palette.color(ColorRole::Selection);
  112. case CSS::ValueID::LibwebPaletteSelectionText:
  113. return palette.color(ColorRole::SelectionText);
  114. case CSS::ValueID::LibwebPaletteInactiveSelection:
  115. return palette.color(ColorRole::InactiveSelection);
  116. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  117. return palette.color(ColorRole::InactiveSelectionText);
  118. case CSS::ValueID::LibwebPaletteRubberBandFill:
  119. return palette.color(ColorRole::RubberBandFill);
  120. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  121. return palette.color(ColorRole::RubberBandBorder);
  122. case CSS::ValueID::LibwebPaletteLink:
  123. return palette.color(ColorRole::Link);
  124. case CSS::ValueID::LibwebPaletteActiveLink:
  125. return palette.color(ColorRole::ActiveLink);
  126. case CSS::ValueID::LibwebPaletteVisitedLink:
  127. return palette.color(ColorRole::VisitedLink);
  128. case CSS::ValueID::LibwebPaletteRuler:
  129. return palette.color(ColorRole::Ruler);
  130. case CSS::ValueID::LibwebPaletteRulerBorder:
  131. return palette.color(ColorRole::RulerBorder);
  132. case CSS::ValueID::LibwebPaletteRulerActiveText:
  133. return palette.color(ColorRole::RulerActiveText);
  134. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  135. return palette.color(ColorRole::RulerInactiveText);
  136. case CSS::ValueID::LibwebPaletteTextCursor:
  137. return palette.color(ColorRole::TextCursor);
  138. case CSS::ValueID::LibwebPaletteFocusOutline:
  139. return palette.color(ColorRole::FocusOutline);
  140. case CSS::ValueID::LibwebPaletteSyntaxComment:
  141. return palette.color(ColorRole::SyntaxComment);
  142. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  143. return palette.color(ColorRole::SyntaxNumber);
  144. case CSS::ValueID::LibwebPaletteSyntaxString:
  145. return palette.color(ColorRole::SyntaxString);
  146. case CSS::ValueID::LibwebPaletteSyntaxType:
  147. return palette.color(ColorRole::SyntaxType);
  148. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  149. return palette.color(ColorRole::SyntaxPunctuation);
  150. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  151. return palette.color(ColorRole::SyntaxOperator);
  152. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  153. return palette.color(ColorRole::SyntaxKeyword);
  154. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  155. return palette.color(ColorRole::SyntaxControlKeyword);
  156. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  157. return palette.color(ColorRole::SyntaxIdentifier);
  158. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  159. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  160. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  161. return palette.color(ColorRole::SyntaxPreprocessorValue);
  162. default:
  163. return {};
  164. }
  165. }
  166. ImageStyleValue::ImageStyleValue(const URL& url, DOM::Document& document)
  167. : StyleValue(Type::Image)
  168. , m_url(url)
  169. , m_document(document)
  170. {
  171. LoadRequest request;
  172. request.set_url(url);
  173. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  174. }
  175. void ImageStyleValue::resource_did_load()
  176. {
  177. if (!m_document)
  178. return;
  179. m_bitmap = resource()->bitmap();
  180. // FIXME: Do less than a full repaint if possible?
  181. if (m_document->frame())
  182. m_document->frame()->set_needs_display({});
  183. }
  184. }