PaintableBox.cpp 47 KB

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