PaintableBox.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /*
  2. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/GenericShorthands.h>
  8. #include <LibGfx/DeprecatedPainter.h>
  9. #include <LibGfx/Font/ScaledFont.h>
  10. #include <LibUnicode/CharacterTypes.h>
  11. #include <LibWeb/CSS/SystemColor.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/HTML/HTMLHtmlElement.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/Layout/BlockContainer.h>
  16. #include <LibWeb/Layout/Viewport.h>
  17. #include <LibWeb/Painting/BackgroundPainting.h>
  18. #include <LibWeb/Painting/PaintableBox.h>
  19. #include <LibWeb/Painting/SVGPaintable.h>
  20. #include <LibWeb/Painting/SVGSVGPaintable.h>
  21. #include <LibWeb/Painting/StackingContext.h>
  22. #include <LibWeb/Painting/TableBordersPainting.h>
  23. #include <LibWeb/Painting/TextPaintable.h>
  24. #include <LibWeb/Painting/ViewportPaintable.h>
  25. #include <LibWeb/Platform/FontPlugin.h>
  26. namespace Web::Painting {
  27. JS::NonnullGCPtr<PaintableWithLines> PaintableWithLines::create(Layout::BlockContainer const& block_container)
  28. {
  29. return block_container.heap().allocate_without_realm<PaintableWithLines>(block_container);
  30. }
  31. JS::NonnullGCPtr<PaintableBox> PaintableBox::create(Layout::Box const& layout_box)
  32. {
  33. return layout_box.heap().allocate_without_realm<PaintableBox>(layout_box);
  34. }
  35. PaintableBox::PaintableBox(Layout::Box const& layout_box)
  36. : Paintable(layout_box)
  37. {
  38. }
  39. PaintableBox::~PaintableBox()
  40. {
  41. }
  42. PaintableWithLines::PaintableWithLines(Layout::BlockContainer const& layout_box)
  43. : PaintableBox(layout_box)
  44. {
  45. }
  46. PaintableWithLines::~PaintableWithLines()
  47. {
  48. }
  49. CSSPixelPoint PaintableBox::scroll_offset() const
  50. {
  51. if (is_viewport()) {
  52. auto navigable = document().navigable();
  53. VERIFY(navigable);
  54. return navigable->viewport_scroll_offset();
  55. }
  56. auto const& node = layout_node();
  57. if (node.is_generated_for_before_pseudo_element())
  58. return node.pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoBefore);
  59. if (node.is_generated_for_after_pseudo_element())
  60. return node.pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoAfter);
  61. if (!(dom_node() && is<DOM::Element>(*dom_node())))
  62. return {};
  63. return static_cast<DOM::Element const*>(dom_node())->scroll_offset(DOM::Element::ScrollOffsetFor::Self);
  64. }
  65. void PaintableBox::set_scroll_offset(CSSPixelPoint offset)
  66. {
  67. auto scrollable_overflow_rect = this->scrollable_overflow_rect();
  68. if (!scrollable_overflow_rect.has_value())
  69. return;
  70. document().set_needs_to_refresh_scroll_state(true);
  71. auto padding_rect = absolute_padding_box_rect();
  72. auto max_x_offset = max(scrollable_overflow_rect->width() - padding_rect.width(), 0);
  73. auto max_y_offset = max(scrollable_overflow_rect->height() - padding_rect.height(), 0);
  74. offset.set_x(clamp(offset.x(), 0, max_x_offset));
  75. offset.set_y(clamp(offset.y(), 0, max_y_offset));
  76. // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
  77. if (offset.y() < 0 || scroll_offset() == offset)
  78. return;
  79. auto& node = layout_node();
  80. if (node.is_generated_for_before_pseudo_element()) {
  81. node.pseudo_element_generator()->set_scroll_offset(DOM::Element::ScrollOffsetFor::PseudoBefore, offset);
  82. } else if (node.is_generated_for_after_pseudo_element()) {
  83. node.pseudo_element_generator()->set_scroll_offset(DOM::Element::ScrollOffsetFor::PseudoAfter, offset);
  84. } else if (is<DOM::Element>(*dom_node())) {
  85. static_cast<DOM::Element*>(dom_node())->set_scroll_offset(DOM::Element::ScrollOffsetFor::Self, offset);
  86. } else {
  87. return;
  88. }
  89. // https://drafts.csswg.org/cssom-view-1/#scrolling-events
  90. // Whenever an element gets scrolled (whether in response to user interaction or by an API),
  91. // the user agent must run these steps:
  92. // 1. Let doc be the element’s node document.
  93. auto& document = layout_box().document();
  94. // FIXME: 2. If the element is a snap container, run the steps to update snapchanging targets for the element with
  95. // the element’s eventual snap target in the block axis as newBlockTarget and the element’s eventual snap
  96. // target in the inline axis as newInlineTarget.
  97. JS::NonnullGCPtr<DOM::EventTarget> const event_target = *dom_node();
  98. // 3. If the element is already in doc’s pending scroll event targets, abort these steps.
  99. if (document.pending_scroll_event_targets().contains_slow(event_target))
  100. return;
  101. // 4. Append the element to doc’s pending scroll event targets.
  102. document.pending_scroll_event_targets().append(*layout_box().dom_node());
  103. set_needs_display();
  104. }
  105. void PaintableBox::scroll_by(int delta_x, int delta_y)
  106. {
  107. set_scroll_offset(scroll_offset().translated(delta_x, delta_y));
  108. }
  109. void PaintableBox::set_offset(CSSPixelPoint offset)
  110. {
  111. m_offset = offset;
  112. }
  113. void PaintableBox::set_content_size(CSSPixelSize size)
  114. {
  115. m_content_size = size;
  116. layout_box().did_set_content_size();
  117. }
  118. CSSPixelPoint PaintableBox::offset() const
  119. {
  120. return m_offset;
  121. }
  122. CSSPixelRect PaintableBox::compute_absolute_rect() const
  123. {
  124. CSSPixelRect rect { offset(), content_size() };
  125. for (auto const* block = containing_block(); block; block = block->containing_block())
  126. rect.translate_by(block->offset());
  127. return rect;
  128. }
  129. CSSPixelRect PaintableBox::absolute_rect() const
  130. {
  131. if (!m_absolute_rect.has_value())
  132. m_absolute_rect = compute_absolute_rect();
  133. return *m_absolute_rect;
  134. }
  135. CSSPixelRect PaintableBox::compute_absolute_paint_rect() const
  136. {
  137. // FIXME: This likely incomplete:
  138. auto rect = absolute_border_box_rect();
  139. if (has_scrollable_overflow()) {
  140. auto scrollable_overflow_rect = this->scrollable_overflow_rect().value();
  141. if (computed_values().overflow_x() == CSS::Overflow::Visible)
  142. rect.unite_horizontally(scrollable_overflow_rect);
  143. if (computed_values().overflow_y() == CSS::Overflow::Visible)
  144. rect.unite_vertically(scrollable_overflow_rect);
  145. }
  146. for (auto const& shadow : box_shadow_data()) {
  147. if (shadow.placement == ShadowPlacement::Inner)
  148. continue;
  149. auto inflate = shadow.spread_distance + shadow.blur_radius;
  150. auto shadow_rect = rect.inflated(inflate, inflate, inflate, inflate).translated(shadow.offset_x, shadow.offset_y);
  151. rect = rect.united(shadow_rect);
  152. }
  153. return rect;
  154. }
  155. CSSPixelRect PaintableBox::absolute_paint_rect() const
  156. {
  157. if (!m_absolute_paint_rect.has_value())
  158. m_absolute_paint_rect = compute_absolute_paint_rect();
  159. return *m_absolute_paint_rect;
  160. }
  161. Optional<CSSPixelRect> PaintableBox::get_clip_rect() const
  162. {
  163. auto clip = computed_values().clip();
  164. if (clip.is_rect() && layout_box().is_absolutely_positioned()) {
  165. auto border_box = absolute_border_box_rect();
  166. return clip.to_rect().resolved(layout_node(), border_box);
  167. }
  168. return {};
  169. }
  170. bool PaintableBox::wants_mouse_events() const
  171. {
  172. if (scroll_thumb_rect(ScrollDirection::Vertical).has_value())
  173. return true;
  174. if (scroll_thumb_rect(ScrollDirection::Horizontal).has_value())
  175. return true;
  176. return false;
  177. }
  178. void PaintableBox::before_paint(PaintContext& context, [[maybe_unused]] PaintPhase phase) const
  179. {
  180. if (!is_visible())
  181. return;
  182. if (!has_css_transform()) {
  183. apply_clip_overflow_rect(context, phase);
  184. }
  185. apply_scroll_offset(context, phase);
  186. }
  187. void PaintableBox::after_paint(PaintContext& context, [[maybe_unused]] PaintPhase phase) const
  188. {
  189. if (!is_visible())
  190. return;
  191. reset_scroll_offset(context, phase);
  192. if (!has_css_transform()) {
  193. clear_clip_overflow_rect(context, phase);
  194. }
  195. }
  196. bool PaintableBox::is_scrollable(ScrollDirection direction) const
  197. {
  198. auto overflow = direction == ScrollDirection::Horizontal ? computed_values().overflow_x() : computed_values().overflow_y();
  199. auto scrollable_overflow_size = direction == ScrollDirection::Horizontal ? scrollable_overflow_rect()->width() : scrollable_overflow_rect()->height();
  200. auto scrollport_size = direction == ScrollDirection::Horizontal ? absolute_padding_box_rect().width() : absolute_padding_box_rect().height();
  201. if (is_viewport() || overflow == CSS::Overflow::Auto)
  202. return scrollable_overflow_size > scrollport_size;
  203. return overflow == CSS::Overflow::Scroll;
  204. }
  205. static constexpr CSSPixels scrollbar_thumb_thickness = 8;
  206. Optional<CSSPixelRect> PaintableBox::scroll_thumb_rect(ScrollDirection direction) const
  207. {
  208. auto maybe_scrollbar_data = compute_scrollbar_data(direction);
  209. if (!maybe_scrollbar_data.has_value())
  210. return {};
  211. auto scroll_offset = direction == ScrollDirection::Horizontal ? -own_scroll_frame_offset().x() : -own_scroll_frame_offset().y();
  212. auto thumb_offset = scroll_offset * maybe_scrollbar_data->scroll_length;
  213. CSSPixelRect thumb_rect = maybe_scrollbar_data->thumb_rect;
  214. if (direction == ScrollDirection::Horizontal) {
  215. thumb_rect.translate_by(thumb_offset, 0);
  216. } else {
  217. thumb_rect.translate_by(0, thumb_offset);
  218. }
  219. return thumb_rect;
  220. }
  221. Optional<PaintableBox::ScrollbarData> PaintableBox::compute_scrollbar_data(ScrollDirection direction) const
  222. {
  223. if (!is_scrollable(direction)) {
  224. return {};
  225. }
  226. if (!own_scroll_frame_id().has_value()) {
  227. return {};
  228. }
  229. auto padding_rect = absolute_padding_box_rect();
  230. auto scrollable_overflow_rect = this->scrollable_overflow_rect().value();
  231. auto scroll_overflow_size = direction == ScrollDirection::Horizontal ? scrollable_overflow_rect.width() : scrollable_overflow_rect.height();
  232. auto scrollport_size = direction == ScrollDirection::Horizontal ? padding_rect.width() : padding_rect.height();
  233. if (scroll_overflow_size == 0)
  234. return {};
  235. auto thumb_length = scrollport_size * (scrollport_size / scroll_overflow_size);
  236. CSSPixelFraction scroll_size = 0;
  237. if (scroll_overflow_size > scrollport_size)
  238. scroll_size = (scrollport_size - thumb_length) / (scroll_overflow_size - scrollport_size);
  239. CSSPixelRect rect;
  240. if (direction == ScrollDirection::Vertical) {
  241. rect = { padding_rect.right() - scrollbar_thumb_thickness, padding_rect.top(), scrollbar_thumb_thickness, thumb_length };
  242. } else {
  243. rect = { padding_rect.left() - scrollbar_thumb_thickness, padding_rect.bottom() - scrollbar_thumb_thickness, thumb_length, scrollbar_thumb_thickness };
  244. }
  245. return PaintableBox::ScrollbarData { rect, scroll_size };
  246. }
  247. void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
  248. {
  249. if (!is_visible())
  250. return;
  251. if (phase == PaintPhase::Background) {
  252. paint_backdrop_filter(context);
  253. paint_background(context);
  254. paint_box_shadow(context);
  255. }
  256. auto const is_table_with_collapsed_borders = display().is_table_inside() && computed_values().border_collapse() == CSS::BorderCollapse::Collapse;
  257. if (!display().is_table_cell() && !is_table_with_collapsed_borders && phase == PaintPhase::Border) {
  258. paint_border(context);
  259. }
  260. if ((display().is_table_inside() || computed_values().border_collapse() == CSS::BorderCollapse::Collapse) && phase == PaintPhase::TableCollapsedBorder) {
  261. paint_table_borders(context, *this);
  262. }
  263. if (phase == PaintPhase::Outline) {
  264. auto const& outline_data = this->outline_data();
  265. if (outline_data.has_value()) {
  266. auto outline_offset = this->outline_offset();
  267. auto border_radius_data = normalized_border_radii_data(ShrinkRadiiForBorders::No);
  268. auto borders_rect = absolute_border_box_rect();
  269. auto outline_offset_x = outline_offset;
  270. auto outline_offset_y = outline_offset;
  271. // "Both the height and the width of the outside of the shape drawn by the outline should not
  272. // become smaller than twice the computed value of the outline-width property to make sure
  273. // that an outline can be rendered even with large negative values."
  274. // https://www.w3.org/TR/css-ui-4/#outline-offset
  275. // So, if the horizontal outline offset is > half the borders_rect's width then we set it to that.
  276. // (And the same for y)
  277. if ((borders_rect.width() / 2) + outline_offset_x < 0)
  278. outline_offset_x = -borders_rect.width() / 2;
  279. if ((borders_rect.height() / 2) + outline_offset_y < 0)
  280. outline_offset_y = -borders_rect.height() / 2;
  281. border_radius_data.inflate(outline_data->top.width + outline_offset_y, outline_data->right.width + outline_offset_x, outline_data->bottom.width + outline_offset_y, outline_data->left.width + outline_offset_x);
  282. borders_rect.inflate(outline_data->top.width + outline_offset_y, outline_data->right.width + outline_offset_x, outline_data->bottom.width + outline_offset_y, outline_data->left.width + outline_offset_x);
  283. paint_all_borders(context.display_list_recorder(), context.rounded_device_rect(borders_rect), border_radius_data.as_corners(context), outline_data->to_device_pixels(context));
  284. }
  285. }
  286. auto scrollbar_width = computed_values().scrollbar_width();
  287. if (phase == PaintPhase::Overlay && scrollbar_width != CSS::ScrollbarWidth::None) {
  288. if (auto scrollbar_data = compute_scrollbar_data(ScrollDirection::Vertical); scrollbar_data.has_value()) {
  289. context.display_list_recorder().paint_scrollbar(own_scroll_frame_id().value(), context.rounded_device_rect(scrollbar_data->thumb_rect).to_type<int>(), scrollbar_data->scroll_length, true);
  290. }
  291. if (auto scrollbar_data = compute_scrollbar_data(ScrollDirection::Horizontal); scrollbar_data.has_value()) {
  292. context.display_list_recorder().paint_scrollbar(own_scroll_frame_id().value(), context.rounded_device_rect(scrollbar_data->thumb_rect).to_type<int>(), scrollbar_data->scroll_length, false);
  293. }
  294. }
  295. if (phase == PaintPhase::Overlay && layout_box().document().inspected_layout_node() == &layout_box()) {
  296. auto content_rect = absolute_rect();
  297. auto margin_box = box_model().margin_box();
  298. CSSPixelRect margin_rect;
  299. margin_rect.set_x(absolute_x() - margin_box.left);
  300. margin_rect.set_width(content_width() + margin_box.left + margin_box.right);
  301. margin_rect.set_y(absolute_y() - margin_box.top);
  302. margin_rect.set_height(content_height() + margin_box.top + margin_box.bottom);
  303. auto border_rect = absolute_border_box_rect();
  304. auto padding_rect = absolute_padding_box_rect();
  305. auto paint_inspector_rect = [&](CSSPixelRect const& rect, Color color) {
  306. auto device_rect = context.enclosing_device_rect(rect).to_type<int>();
  307. context.display_list_recorder().fill_rect(device_rect, Color(color).with_alpha(100));
  308. context.display_list_recorder().draw_rect(device_rect, Color(color));
  309. };
  310. paint_inspector_rect(margin_rect, Color::Yellow);
  311. paint_inspector_rect(padding_rect, Color::Cyan);
  312. paint_inspector_rect(border_rect, Color::Green);
  313. paint_inspector_rect(content_rect, Color::Magenta);
  314. auto& font = Platform::FontPlugin::the().default_font();
  315. StringBuilder builder;
  316. if (layout_box().dom_node())
  317. builder.append(layout_box().dom_node()->debug_description());
  318. else
  319. builder.append(layout_box().debug_description());
  320. builder.appendff(" {}x{} @ {},{}", border_rect.width(), border_rect.height(), border_rect.x(), border_rect.y());
  321. auto size_text = MUST(builder.to_string());
  322. auto size_text_rect = border_rect;
  323. size_text_rect.set_y(border_rect.y() + border_rect.height());
  324. size_text_rect.set_top(size_text_rect.top());
  325. size_text_rect.set_width(CSSPixels::nearest_value_for(font.width(size_text)) + 4);
  326. size_text_rect.set_height(CSSPixels::nearest_value_for(font.pixel_size()) + 4);
  327. auto size_text_device_rect = context.enclosing_device_rect(size_text_rect).to_type<int>();
  328. context.display_list_recorder().fill_rect(size_text_device_rect, context.palette().color(Gfx::ColorRole::Tooltip));
  329. context.display_list_recorder().draw_rect(size_text_device_rect, context.palette().threed_shadow1());
  330. context.display_list_recorder().draw_text(size_text_device_rect, size_text, font, Gfx::TextAlignment::Center, context.palette().color(Gfx::ColorRole::TooltipText));
  331. }
  332. }
  333. BordersData PaintableBox::remove_element_kind_from_borders_data(PaintableBox::BordersDataWithElementKind borders_data)
  334. {
  335. return {
  336. .top = borders_data.top.border_data,
  337. .right = borders_data.right.border_data,
  338. .bottom = borders_data.bottom.border_data,
  339. .left = borders_data.left.border_data,
  340. };
  341. }
  342. void PaintableBox::paint_border(PaintContext& context) const
  343. {
  344. auto borders_data = m_override_borders_data.has_value() ? remove_element_kind_from_borders_data(m_override_borders_data.value()) : BordersData {
  345. .top = box_model().border.top == 0 ? CSS::BorderData() : computed_values().border_top(),
  346. .right = box_model().border.right == 0 ? CSS::BorderData() : computed_values().border_right(),
  347. .bottom = box_model().border.bottom == 0 ? CSS::BorderData() : computed_values().border_bottom(),
  348. .left = box_model().border.left == 0 ? CSS::BorderData() : computed_values().border_left(),
  349. };
  350. paint_all_borders(context.display_list_recorder(), context.rounded_device_rect(absolute_border_box_rect()), normalized_border_radii_data().as_corners(context), borders_data.to_device_pixels(context));
  351. }
  352. void PaintableBox::paint_backdrop_filter(PaintContext& context) const
  353. {
  354. auto const& backdrop_filter = computed_values().backdrop_filter();
  355. if (backdrop_filter.is_none()) {
  356. return;
  357. }
  358. auto backdrop_region = context.rounded_device_rect(absolute_border_box_rect());
  359. auto border_radii_data = normalized_border_radii_data();
  360. ScopedCornerRadiusClip corner_clipper { context, backdrop_region, border_radii_data };
  361. context.display_list_recorder().apply_backdrop_filter(backdrop_region.to_type<int>(), border_radii_data, backdrop_filter);
  362. }
  363. void PaintableBox::paint_background(PaintContext& context) const
  364. {
  365. // If the body's background properties were propagated to the root element, do no re-paint the body's background.
  366. if (layout_box().is_body() && document().html_element()->should_use_body_background_properties())
  367. return;
  368. Painting::paint_background(context, layout_box(), computed_values().image_rendering(), m_resolved_background, normalized_border_radii_data());
  369. }
  370. void PaintableBox::paint_box_shadow(PaintContext& context) const
  371. {
  372. auto const& resolved_box_shadow_data = box_shadow_data();
  373. if (resolved_box_shadow_data.is_empty())
  374. return;
  375. auto borders_data = BordersData {
  376. .top = computed_values().border_top(),
  377. .right = computed_values().border_right(),
  378. .bottom = computed_values().border_bottom(),
  379. .left = computed_values().border_left(),
  380. };
  381. Painting::paint_box_shadow(context, absolute_border_box_rect(), absolute_padding_box_rect(),
  382. borders_data, normalized_border_radii_data(), resolved_box_shadow_data);
  383. }
  384. BorderRadiiData PaintableBox::normalized_border_radii_data(ShrinkRadiiForBorders shrink) const
  385. {
  386. auto border_radii_data = this->border_radii_data();
  387. if (shrink == ShrinkRadiiForBorders::Yes)
  388. border_radii_data.shrink(computed_values().border_top().width, computed_values().border_right().width, computed_values().border_bottom().width, computed_values().border_left().width);
  389. return border_radii_data;
  390. }
  391. void PaintableBox::apply_scroll_offset(PaintContext& context, PaintPhase) const
  392. {
  393. if (scroll_frame_id().has_value()) {
  394. context.display_list_recorder().save();
  395. context.display_list_recorder().set_scroll_frame_id(scroll_frame_id().value());
  396. }
  397. }
  398. void PaintableBox::reset_scroll_offset(PaintContext& context, PaintPhase) const
  399. {
  400. if (scroll_frame_id().has_value())
  401. context.display_list_recorder().restore();
  402. }
  403. void PaintableBox::apply_clip_overflow_rect(PaintContext& context, PaintPhase phase) const
  404. {
  405. if (!AK::first_is_one_of(phase, PaintPhase::Background, PaintPhase::Border, PaintPhase::TableCollapsedBorder, PaintPhase::Foreground, PaintPhase::Outline))
  406. return;
  407. apply_clip(context);
  408. }
  409. void PaintableBox::clear_clip_overflow_rect(PaintContext& context, PaintPhase phase) const
  410. {
  411. if (!AK::first_is_one_of(phase, PaintPhase::Background, PaintPhase::Border, PaintPhase::TableCollapsedBorder, PaintPhase::Foreground, PaintPhase::Outline))
  412. return;
  413. restore_clip(context);
  414. }
  415. void paint_cursor_if_needed(PaintContext& context, TextPaintable const& paintable, PaintableFragment const& fragment)
  416. {
  417. auto const& navigable = *paintable.navigable();
  418. auto const& document = paintable.document();
  419. if (!navigable.is_focused())
  420. return;
  421. if (!document.cursor_blink_state())
  422. return;
  423. if (document.cursor_position()->node() != paintable.dom_node())
  424. return;
  425. // NOTE: This checks if the cursor is before the start or after the end of the fragment. If it is at the end, after all text, it should still be painted.
  426. if (document.cursor_position()->offset() < (unsigned)fragment.start() || document.cursor_position()->offset() > (unsigned)(fragment.start() + fragment.length()))
  427. return;
  428. if (!fragment.layout_node().dom_node() || !fragment.layout_node().dom_node()->is_editable())
  429. return;
  430. auto fragment_rect = fragment.absolute_rect();
  431. auto text = fragment.string_view();
  432. CSSPixelRect cursor_rect {
  433. fragment_rect.x() + CSSPixels::nearest_value_for(paintable.layout_node().first_available_font().width(text.substring_view(0, document.cursor_position()->offset() - fragment.start()))),
  434. fragment_rect.top(),
  435. 1,
  436. fragment_rect.height()
  437. };
  438. auto cursor_device_rect = context.rounded_device_rect(cursor_rect).to_type<int>();
  439. context.display_list_recorder().draw_rect(cursor_device_rect, paintable.computed_values().color());
  440. }
  441. void paint_text_decoration(PaintContext& context, TextPaintable const& paintable, PaintableFragment const& fragment)
  442. {
  443. auto& painter = context.display_list_recorder();
  444. auto& font = fragment.layout_node().first_available_font();
  445. auto fragment_box = fragment.absolute_rect();
  446. CSSPixels glyph_height = CSSPixels::nearest_value_for(font.pixel_size());
  447. auto baseline = fragment.baseline();
  448. auto line_color = paintable.computed_values().text_decoration_color();
  449. auto const& text_paintable = static_cast<TextPaintable const&>(fragment.paintable());
  450. auto device_line_thickness = context.rounded_device_pixels(text_paintable.text_decoration_thickness());
  451. auto text_decoration_lines = paintable.computed_values().text_decoration_line();
  452. for (auto line : text_decoration_lines) {
  453. DevicePixelPoint line_start_point {};
  454. DevicePixelPoint line_end_point {};
  455. switch (line) {
  456. case CSS::TextDecorationLine::None:
  457. return;
  458. case CSS::TextDecorationLine::Underline:
  459. line_start_point = context.rounded_device_point(fragment_box.top_left().translated(0, baseline + 2));
  460. line_end_point = context.rounded_device_point(fragment_box.top_right().translated(-1, baseline + 2));
  461. break;
  462. case CSS::TextDecorationLine::Overline:
  463. line_start_point = context.rounded_device_point(fragment_box.top_left().translated(0, baseline - glyph_height));
  464. line_end_point = context.rounded_device_point(fragment_box.top_right().translated(-1, baseline - glyph_height));
  465. break;
  466. case CSS::TextDecorationLine::LineThrough: {
  467. auto x_height = font.x_height();
  468. line_start_point = context.rounded_device_point(fragment_box.top_left().translated(0, baseline - x_height * CSSPixels(0.5f)));
  469. line_end_point = context.rounded_device_point(fragment_box.top_right().translated(-1, baseline - x_height * CSSPixels(0.5f)));
  470. break;
  471. }
  472. case CSS::TextDecorationLine::Blink:
  473. // Conforming user agents may simply not blink the text
  474. return;
  475. }
  476. switch (paintable.computed_values().text_decoration_style()) {
  477. case CSS::TextDecorationStyle::Solid:
  478. painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::LineStyle::Solid);
  479. break;
  480. case CSS::TextDecorationStyle::Double:
  481. switch (line) {
  482. case CSS::TextDecorationLine::Underline:
  483. break;
  484. case CSS::TextDecorationLine::Overline:
  485. line_start_point.translate_by(0, -device_line_thickness - context.rounded_device_pixels(1));
  486. line_end_point.translate_by(0, -device_line_thickness - context.rounded_device_pixels(1));
  487. break;
  488. case CSS::TextDecorationLine::LineThrough:
  489. line_start_point.translate_by(0, -device_line_thickness / 2);
  490. line_end_point.translate_by(0, -device_line_thickness / 2);
  491. break;
  492. default:
  493. VERIFY_NOT_REACHED();
  494. }
  495. painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value());
  496. painter.draw_line(line_start_point.translated(0, device_line_thickness + 1).to_type<int>(), line_end_point.translated(0, device_line_thickness + 1).to_type<int>(), line_color, device_line_thickness.value());
  497. break;
  498. case CSS::TextDecorationStyle::Dashed:
  499. painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::LineStyle::Dashed);
  500. break;
  501. case CSS::TextDecorationStyle::Dotted:
  502. painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::LineStyle::Dotted);
  503. break;
  504. case CSS::TextDecorationStyle::Wavy:
  505. painter.draw_triangle_wave(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value() + 1, device_line_thickness.value());
  506. break;
  507. }
  508. }
  509. }
  510. void paint_text_fragment(PaintContext& context, TextPaintable const& paintable, PaintableFragment const& fragment, PaintPhase phase)
  511. {
  512. if (!paintable.is_visible())
  513. return;
  514. auto& painter = context.display_list_recorder();
  515. if (phase == PaintPhase::Foreground) {
  516. auto fragment_absolute_rect = fragment.absolute_rect();
  517. auto fragment_absolute_device_rect = context.enclosing_device_rect(fragment_absolute_rect);
  518. if (paintable.document().inspected_layout_node() == &paintable.layout_node())
  519. context.display_list_recorder().draw_rect(fragment_absolute_device_rect.to_type<int>(), Color::Magenta);
  520. auto text = paintable.text_for_rendering();
  521. auto glyph_run = fragment.glyph_run();
  522. if (!glyph_run)
  523. return;
  524. DevicePixelPoint baseline_start { fragment_absolute_device_rect.x(), fragment_absolute_device_rect.y() + context.rounded_device_pixels(fragment.baseline()) };
  525. auto scale = context.device_pixels_per_css_pixel();
  526. painter.draw_text_run(baseline_start.to_type<int>(), *glyph_run, paintable.computed_values().webkit_text_fill_color(), fragment_absolute_device_rect.to_type<int>(), scale);
  527. auto selection_rect = context.enclosing_device_rect(fragment.selection_rect(paintable.layout_node().first_available_font())).to_type<int>();
  528. if (!selection_rect.is_empty()) {
  529. painter.fill_rect(selection_rect, CSS::SystemColor::highlight());
  530. DisplayListRecorderStateSaver saver(painter);
  531. painter.add_clip_rect(selection_rect);
  532. painter.draw_text_run(baseline_start.to_type<int>(), *glyph_run, CSS::SystemColor::highlight_text(), fragment_absolute_device_rect.to_type<int>(), scale);
  533. }
  534. paint_text_decoration(context, paintable, fragment);
  535. paint_cursor_if_needed(context, paintable, fragment);
  536. }
  537. }
  538. void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
  539. {
  540. if (!is_visible())
  541. return;
  542. PaintableBox::paint(context, phase);
  543. if (fragments().is_empty())
  544. return;
  545. bool should_clip_overflow = computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
  546. Optional<u32> corner_clip_id;
  547. auto clip_box = absolute_padding_box_rect();
  548. if (get_clip_rect().has_value()) {
  549. clip_box.intersect(get_clip_rect().value());
  550. should_clip_overflow = true;
  551. }
  552. if (should_clip_overflow) {
  553. context.display_list_recorder().save();
  554. // FIXME: Handle overflow-x and overflow-y being different values.
  555. context.display_list_recorder().add_clip_rect(context.rounded_device_rect(clip_box).to_type<int>());
  556. auto border_radii = normalized_border_radii_data(ShrinkRadiiForBorders::Yes);
  557. CornerRadii corner_radii {
  558. .top_left = border_radii.top_left.as_corner(context),
  559. .top_right = border_radii.top_right.as_corner(context),
  560. .bottom_right = border_radii.bottom_right.as_corner(context),
  561. .bottom_left = border_radii.bottom_left.as_corner(context)
  562. };
  563. if (corner_radii.has_any_radius()) {
  564. context.display_list_recorder().add_rounded_rect_clip(corner_radii, context.rounded_device_rect(clip_box).to_type<int>(), CornerClip::Outside);
  565. }
  566. if (own_scroll_frame_id().has_value()) {
  567. context.display_list_recorder().set_scroll_frame_id(own_scroll_frame_id().value());
  568. }
  569. }
  570. // Text shadows
  571. // This is yet another loop, but done here because all shadows should appear under all text.
  572. // So, we paint the shadows before painting any text.
  573. // FIXME: Find a smarter way to do this?
  574. if (phase == PaintPhase::Foreground) {
  575. for (auto& fragment : fragments()) {
  576. paint_text_shadow(context, fragment, fragment.shadows());
  577. }
  578. }
  579. for (auto const& fragment : m_fragments) {
  580. auto fragment_absolute_rect = fragment.absolute_rect();
  581. auto fragment_absolute_device_rect = context.enclosing_device_rect(fragment_absolute_rect);
  582. if (context.should_show_line_box_borders()) {
  583. context.display_list_recorder().draw_rect(fragment_absolute_device_rect.to_type<int>(), Color::Green);
  584. context.display_list_recorder().draw_line(
  585. context.rounded_device_point(fragment_absolute_rect.top_left().translated(0, fragment.baseline())).to_type<int>(),
  586. context.rounded_device_point(fragment_absolute_rect.top_right().translated(-1, fragment.baseline())).to_type<int>(), Color::Red);
  587. }
  588. if (is<TextPaintable>(fragment.paintable()))
  589. paint_text_fragment(context, static_cast<TextPaintable const&>(fragment.paintable()), fragment, phase);
  590. }
  591. if (should_clip_overflow) {
  592. context.display_list_recorder().restore();
  593. }
  594. }
  595. Paintable::DispatchEventOfSameName PaintableBox::handle_mousedown(Badge<EventHandler>, CSSPixelPoint position, unsigned, unsigned)
  596. {
  597. auto vertical_scroll_thumb_rect = scroll_thumb_rect(ScrollDirection::Vertical);
  598. auto horizontal_scroll_thumb_rect = scroll_thumb_rect(ScrollDirection::Horizontal);
  599. if (vertical_scroll_thumb_rect.has_value() && vertical_scroll_thumb_rect.value().contains(position)) {
  600. m_last_mouse_tracking_position = position;
  601. m_scroll_thumb_dragging_direction = ScrollDirection::Vertical;
  602. const_cast<HTML::Navigable&>(*navigable()).event_handler().set_mouse_event_tracking_paintable(this);
  603. } else if (horizontal_scroll_thumb_rect.has_value() && horizontal_scroll_thumb_rect.value().contains(position)) {
  604. m_last_mouse_tracking_position = position;
  605. m_scroll_thumb_dragging_direction = ScrollDirection::Horizontal;
  606. const_cast<HTML::Navigable&>(*navigable()).event_handler().set_mouse_event_tracking_paintable(this);
  607. }
  608. return Paintable::DispatchEventOfSameName::Yes;
  609. }
  610. Paintable::DispatchEventOfSameName PaintableBox::handle_mouseup(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned)
  611. {
  612. if (m_last_mouse_tracking_position.has_value()) {
  613. m_last_mouse_tracking_position.clear();
  614. m_scroll_thumb_dragging_direction.clear();
  615. const_cast<HTML::Navigable&>(*navigable()).event_handler().set_mouse_event_tracking_paintable(nullptr);
  616. }
  617. return Paintable::DispatchEventOfSameName::Yes;
  618. }
  619. Paintable::DispatchEventOfSameName PaintableBox::handle_mousemove(Badge<EventHandler>, CSSPixelPoint position, unsigned, unsigned)
  620. {
  621. if (m_last_mouse_tracking_position.has_value()) {
  622. Gfx::Point<double> scroll_delta;
  623. if (m_scroll_thumb_dragging_direction == ScrollDirection::Horizontal)
  624. scroll_delta.set_x((position.x() - m_last_mouse_tracking_position->x()).to_double());
  625. else
  626. scroll_delta.set_y((position.y() - m_last_mouse_tracking_position->y()).to_double());
  627. auto padding_rect = absolute_padding_box_rect();
  628. auto scrollable_overflow_rect = this->scrollable_overflow_rect().value();
  629. auto scroll_overflow_size = m_scroll_thumb_dragging_direction == ScrollDirection::Horizontal ? scrollable_overflow_rect.width() : scrollable_overflow_rect.height();
  630. auto scrollport_size = m_scroll_thumb_dragging_direction == ScrollDirection::Horizontal ? padding_rect.width() : padding_rect.height();
  631. auto scroll_px_per_mouse_position_delta_px = scroll_overflow_size.to_double() / scrollport_size.to_double();
  632. scroll_delta *= scroll_px_per_mouse_position_delta_px;
  633. if (is_viewport()) {
  634. document().window()->scroll_by(scroll_delta.x(), scroll_delta.y());
  635. } else {
  636. scroll_by(scroll_delta.x(), scroll_delta.y());
  637. }
  638. m_last_mouse_tracking_position = position;
  639. return Paintable::DispatchEventOfSameName::No;
  640. }
  641. return Paintable::DispatchEventOfSameName::Yes;
  642. }
  643. bool PaintableBox::handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
  644. {
  645. if (!layout_box().is_user_scrollable())
  646. return false;
  647. // TODO: Vertical and horizontal scroll overflow should be handled seperately.
  648. if (!has_scrollable_overflow())
  649. return false;
  650. scroll_by(wheel_delta_x, wheel_delta_y);
  651. return true;
  652. }
  653. Layout::BlockContainer const& PaintableWithLines::layout_box() const
  654. {
  655. return static_cast<Layout::BlockContainer const&>(PaintableBox::layout_box());
  656. }
  657. Layout::BlockContainer& PaintableWithLines::layout_box()
  658. {
  659. return static_cast<Layout::BlockContainer&>(PaintableBox::layout_box());
  660. }
  661. TraversalDecision PaintableBox::hit_test_scrollbars(CSSPixelPoint position, Function<TraversalDecision(HitTestResult)> const& callback) const
  662. {
  663. auto vertical_scroll_thumb_rect = scroll_thumb_rect(ScrollDirection::Vertical);
  664. if (vertical_scroll_thumb_rect.has_value() && vertical_scroll_thumb_rect.value().contains(position))
  665. return callback(HitTestResult { const_cast<PaintableBox&>(*this) });
  666. auto horizontal_scroll_thumb_rect = scroll_thumb_rect(ScrollDirection::Horizontal);
  667. if (horizontal_scroll_thumb_rect.has_value() && horizontal_scroll_thumb_rect.value().contains(position))
  668. return callback(HitTestResult { const_cast<PaintableBox&>(*this) });
  669. return TraversalDecision::Continue;
  670. }
  671. TraversalDecision PaintableBox::hit_test(CSSPixelPoint position, HitTestType type, Function<TraversalDecision(HitTestResult)> const& callback) const
  672. {
  673. if (clip_rect_for_hit_testing().has_value() && !clip_rect_for_hit_testing()->contains(position))
  674. return TraversalDecision::Continue;
  675. auto position_adjusted_by_scroll_offset = position;
  676. position_adjusted_by_scroll_offset.translate_by(-cumulative_offset_of_enclosing_scroll_frame());
  677. if (!is_visible())
  678. return TraversalDecision::Continue;
  679. if (hit_test_scrollbars(position_adjusted_by_scroll_offset, callback) == TraversalDecision::Break)
  680. return TraversalDecision::Break;
  681. if (layout_box().is_viewport()) {
  682. auto& viewport_paintable = const_cast<ViewportPaintable&>(static_cast<ViewportPaintable const&>(*this));
  683. viewport_paintable.build_stacking_context_tree_if_needed();
  684. viewport_paintable.document().update_paint_and_hit_testing_properties_if_needed();
  685. viewport_paintable.refresh_scroll_state();
  686. return stacking_context()->hit_test(position, type, callback);
  687. }
  688. for (auto const* child = last_child(); child; child = child->previous_sibling()) {
  689. auto z_index = child->computed_values().z_index();
  690. if (child->layout_node().is_positioned() && z_index.value_or(0) == 0)
  691. continue;
  692. if (child->hit_test(position, type, callback) == TraversalDecision::Break)
  693. return TraversalDecision::Break;
  694. }
  695. if (!absolute_border_box_rect().contains(position_adjusted_by_scroll_offset.x(), position_adjusted_by_scroll_offset.y()))
  696. return TraversalDecision::Continue;
  697. if (!visible_for_hit_testing())
  698. return TraversalDecision::Continue;
  699. return callback(HitTestResult { const_cast<PaintableBox&>(*this) });
  700. }
  701. Optional<HitTestResult> PaintableBox::hit_test(CSSPixelPoint position, HitTestType type) const
  702. {
  703. Optional<HitTestResult> result;
  704. (void)PaintableBox::hit_test(position, type, [&](HitTestResult candidate) {
  705. VERIFY(!result.has_value());
  706. if (!candidate.paintable->visible_for_hit_testing())
  707. return TraversalDecision::Continue;
  708. result = move(candidate);
  709. return TraversalDecision::Break;
  710. });
  711. return result;
  712. }
  713. TraversalDecision PaintableWithLines::hit_test(CSSPixelPoint position, HitTestType type, Function<TraversalDecision(HitTestResult)> const& callback) const
  714. {
  715. if (clip_rect_for_hit_testing().has_value() && !clip_rect_for_hit_testing()->contains(position))
  716. return TraversalDecision::Continue;
  717. auto position_adjusted_by_scroll_offset = position;
  718. position_adjusted_by_scroll_offset.translate_by(-cumulative_offset_of_enclosing_scroll_frame());
  719. if (!layout_box().children_are_inline() || m_fragments.is_empty()) {
  720. return PaintableBox::hit_test(position, type, callback);
  721. }
  722. if (hit_test_scrollbars(position_adjusted_by_scroll_offset, callback) == TraversalDecision::Break)
  723. return TraversalDecision::Break;
  724. for (auto const* child = last_child(); child; child = child->previous_sibling()) {
  725. if (child->hit_test(position, type, callback) == TraversalDecision::Break)
  726. return TraversalDecision::Break;
  727. }
  728. Optional<HitTestResult> last_good_candidate;
  729. for (auto const& fragment : fragments()) {
  730. if (fragment.paintable().stacking_context())
  731. continue;
  732. auto fragment_absolute_rect = fragment.absolute_rect();
  733. if (fragment_absolute_rect.contains(position_adjusted_by_scroll_offset)) {
  734. if (fragment.paintable().hit_test(position, type, callback) == TraversalDecision::Break)
  735. return TraversalDecision::Break;
  736. HitTestResult hit_test_result { const_cast<Paintable&>(fragment.paintable()), fragment.text_index_at(position_adjusted_by_scroll_offset.x()) };
  737. if (callback(hit_test_result) == TraversalDecision::Break)
  738. return TraversalDecision::Break;
  739. }
  740. // If we reached this point, the position is not within the fragment. However, the fragment start or end might be the place to place the cursor.
  741. // This determines whether the fragment is a good candidate for the position. The last such good fragment is chosen.
  742. // The best candidate is either the end of the line above, the beginning of the line below, or the beginning or end of the current line.
  743. // We arbitrarily choose to consider the end of the line above and ignore the beginning of the line below.
  744. // If we knew the direction of selection, we could make a better choice.
  745. if (fragment_absolute_rect.bottom() - 1 <= position_adjusted_by_scroll_offset.y()) { // fully below the fragment
  746. last_good_candidate = HitTestResult { const_cast<Paintable&>(fragment.paintable()), fragment.start() + fragment.length() };
  747. } else if (fragment_absolute_rect.top() <= position_adjusted_by_scroll_offset.y()) { // vertically within the fragment
  748. if (position_adjusted_by_scroll_offset.x() < fragment_absolute_rect.left()) { // left of the fragment
  749. if (!last_good_candidate.has_value()) { // first fragment of the line
  750. last_good_candidate = HitTestResult { const_cast<Paintable&>(fragment.paintable()), fragment.start() };
  751. }
  752. } else { // right of the fragment
  753. last_good_candidate = HitTestResult { const_cast<Paintable&>(fragment.paintable()), fragment.start() + fragment.length() };
  754. }
  755. }
  756. }
  757. if (type == HitTestType::TextCursor && last_good_candidate.has_value()) {
  758. if (callback(last_good_candidate.value()) == TraversalDecision::Break)
  759. return TraversalDecision::Break;
  760. }
  761. if (!stacking_context() && is_visible() && absolute_border_box_rect().contains(position_adjusted_by_scroll_offset.x(), position_adjusted_by_scroll_offset.y())) {
  762. if (callback(HitTestResult { const_cast<PaintableWithLines&>(*this) }) == TraversalDecision::Break)
  763. return TraversalDecision::Break;
  764. }
  765. return TraversalDecision::Continue;
  766. }
  767. void PaintableBox::set_needs_display()
  768. {
  769. document().set_needs_display(absolute_rect());
  770. }
  771. Optional<CSSPixelRect> PaintableBox::get_masking_area() const
  772. {
  773. // FIXME: Support clip-paths with transforms.
  774. if (!combined_css_transform().is_identity_or_translation())
  775. return {};
  776. auto clip_path = computed_values().clip_path();
  777. // FIXME: Support other clip sources.
  778. if (!clip_path.has_value() || !clip_path->is_basic_shape())
  779. return {};
  780. // FIXME: Support other geometry boxes. See: https://drafts.fxtf.org/css-masking/#typedef-geometry-box
  781. return absolute_border_box_rect();
  782. }
  783. void PaintableBox::resolve_paint_properties()
  784. {
  785. auto const& computed_values = this->computed_values();
  786. auto const& layout_node = this->layout_node();
  787. // Border radii
  788. CSSPixelRect const border_rect { 0, 0, border_box_width(), border_box_height() };
  789. auto const& border_top_left_radius = computed_values.border_top_left_radius();
  790. auto const& border_top_right_radius = computed_values.border_top_right_radius();
  791. auto const& border_bottom_right_radius = computed_values.border_bottom_right_radius();
  792. auto const& border_bottom_left_radius = computed_values.border_bottom_left_radius();
  793. auto radii_data = normalize_border_radii_data(layout_node, border_rect, border_top_left_radius,
  794. border_top_right_radius, border_bottom_right_radius,
  795. border_bottom_left_radius);
  796. set_border_radii_data(radii_data);
  797. // Box shadows
  798. auto const& box_shadow_data = computed_values.box_shadow();
  799. Vector<Painting::ShadowData> resolved_box_shadow_data;
  800. resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
  801. for (auto const& layer : box_shadow_data) {
  802. resolved_box_shadow_data.empend(
  803. layer.color,
  804. layer.offset_x.to_px(layout_node),
  805. layer.offset_y.to_px(layout_node),
  806. layer.blur_radius.to_px(layout_node),
  807. layer.spread_distance.to_px(layout_node),
  808. layer.placement == CSS::ShadowPlacement::Outer ? Painting::ShadowPlacement::Outer
  809. : Painting::ShadowPlacement::Inner);
  810. }
  811. set_box_shadow_data(move(resolved_box_shadow_data));
  812. auto const& transformations = computed_values.transformations();
  813. if (!transformations.is_empty()) {
  814. auto matrix = Gfx::FloatMatrix4x4::identity();
  815. for (auto const& transform : transformations)
  816. matrix = matrix * transform.to_matrix(*this).release_value();
  817. set_transform(matrix);
  818. }
  819. auto const& transform_origin = computed_values.transform_origin();
  820. // https://www.w3.org/TR/css-transforms-1/#transform-box
  821. auto transform_box = computed_values.transform_box();
  822. // For SVG elements without associated CSS layout box, the used value for content-box is fill-box and for
  823. // border-box is stroke-box.
  824. // FIXME: This currently detects any SVG element except the <svg> one. Is that correct?
  825. // And is it correct to use `else` below?
  826. if (is<Painting::SVGPaintable>(*this)) {
  827. switch (transform_box) {
  828. case CSS::TransformBox::ContentBox:
  829. transform_box = CSS::TransformBox::FillBox;
  830. break;
  831. case CSS::TransformBox::BorderBox:
  832. transform_box = CSS::TransformBox::StrokeBox;
  833. break;
  834. default:
  835. break;
  836. }
  837. }
  838. // For elements with associated CSS layout box, the used value for fill-box is content-box and for
  839. // stroke-box and view-box is border-box.
  840. else {
  841. switch (transform_box) {
  842. case CSS::TransformBox::FillBox:
  843. transform_box = CSS::TransformBox::ContentBox;
  844. break;
  845. case CSS::TransformBox::StrokeBox:
  846. case CSS::TransformBox::ViewBox:
  847. transform_box = CSS::TransformBox::BorderBox;
  848. break;
  849. default:
  850. break;
  851. }
  852. }
  853. CSSPixelRect reference_box = [&]() {
  854. switch (transform_box) {
  855. case CSS::TransformBox::ContentBox:
  856. // Uses the content box as reference box.
  857. // FIXME: The reference box of a table is the border box of its table wrapper box, not its table box.
  858. return absolute_rect();
  859. case CSS::TransformBox::BorderBox:
  860. // Uses the border box as reference box.
  861. // FIXME: The reference box of a table is the border box of its table wrapper box, not its table box.
  862. return absolute_border_box_rect();
  863. case CSS::TransformBox::FillBox:
  864. // Uses the object bounding box as reference box.
  865. // FIXME: For now we're using the content rect as an approximation.
  866. return absolute_rect();
  867. case CSS::TransformBox::StrokeBox:
  868. // Uses the stroke bounding box as reference box.
  869. // FIXME: For now we're using the border rect as an approximation.
  870. return absolute_border_box_rect();
  871. case CSS::TransformBox::ViewBox:
  872. // Uses the nearest SVG viewport as reference box.
  873. // FIXME: If a viewBox attribute is specified for the SVG viewport creating element:
  874. // - The reference box is positioned at the origin of the coordinate system established by the viewBox attribute.
  875. // - The dimension of the reference box is set to the width and height values of the viewBox attribute.
  876. auto* svg_paintable = first_ancestor_of_type<Painting::SVGSVGPaintable>();
  877. if (!svg_paintable)
  878. return absolute_border_box_rect();
  879. return svg_paintable->absolute_rect();
  880. }
  881. VERIFY_NOT_REACHED();
  882. }();
  883. auto x = reference_box.left() + transform_origin.x.to_px(layout_node, reference_box.width());
  884. auto y = reference_box.top() + transform_origin.y.to_px(layout_node, reference_box.height());
  885. set_transform_origin({ x, y });
  886. set_transform_origin({ x, y });
  887. // Outlines
  888. auto outline_width = computed_values.outline_width().to_px(layout_node);
  889. auto outline_data = borders_data_for_outline(layout_node, computed_values.outline_color(), computed_values.outline_style(), outline_width);
  890. auto outline_offset = computed_values.outline_offset().to_px(layout_node);
  891. set_outline_data(outline_data);
  892. set_outline_offset(outline_offset);
  893. auto combined_transform = compute_combined_css_transform();
  894. set_combined_css_transform(combined_transform);
  895. CSSPixelRect background_rect;
  896. Color background_color = computed_values.background_color();
  897. auto const* background_layers = &computed_values.background_layers();
  898. if (layout_box().is_root_element()) {
  899. background_rect = navigable()->viewport_rect();
  900. // Section 2.11.2: If the computed value of background-image on the root element is none and its background-color is transparent,
  901. // user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY child element.
  902. if (document().html_element()->should_use_body_background_properties()) {
  903. background_layers = document().background_layers();
  904. background_color = document().background_color();
  905. }
  906. } else {
  907. background_rect = absolute_padding_box_rect();
  908. }
  909. // HACK: If the Box has a border, use the bordered_rect to paint the background.
  910. // This way if we have a border-radius there will be no gap between the filling and actual border.
  911. if (computed_values.border_top().width != 0 || computed_values.border_right().width != 0 || computed_values.border_bottom().width != 0 || computed_values.border_left().width != 0)
  912. background_rect = absolute_border_box_rect();
  913. m_resolved_background.layers.clear();
  914. if (background_layers) {
  915. m_resolved_background = resolve_background_layers(*background_layers, layout_box(), background_color, background_rect, normalized_border_radii_data());
  916. };
  917. }
  918. void PaintableWithLines::resolve_paint_properties()
  919. {
  920. PaintableBox::resolve_paint_properties();
  921. auto const& layout_node = this->layout_node();
  922. for (auto const& fragment : fragments()) {
  923. auto const& text_shadow = fragment.m_layout_node->computed_values().text_shadow();
  924. if (!text_shadow.is_empty()) {
  925. Vector<Painting::ShadowData> resolved_shadow_data;
  926. resolved_shadow_data.ensure_capacity(text_shadow.size());
  927. for (auto const& layer : text_shadow) {
  928. resolved_shadow_data.empend(
  929. layer.color,
  930. layer.offset_x.to_px(layout_node),
  931. layer.offset_y.to_px(layout_node),
  932. layer.blur_radius.to_px(layout_node),
  933. layer.spread_distance.to_px(layout_node),
  934. Painting::ShadowPlacement::Outer);
  935. }
  936. const_cast<Painting::PaintableFragment&>(fragment).set_shadows(move(resolved_shadow_data));
  937. }
  938. }
  939. }
  940. }