PaintableBox.cpp 51 KB

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