PaintableBox.cpp 54 KB

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