PaintableBox.cpp 57 KB

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