PaintableBox.cpp 56 KB

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