PaintableBox.cpp 38 KB

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