PaintableBox.cpp 54 KB

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