PaintableBox.cpp 40 KB

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