PaintableBox.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, 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/DOM/Document.h>
  10. #include <LibWeb/HTML/HTMLHtmlElement.h>
  11. #include <LibWeb/Layout/BlockContainer.h>
  12. #include <LibWeb/Layout/Viewport.h>
  13. #include <LibWeb/Painting/BackgroundPainting.h>
  14. #include <LibWeb/Painting/FilterPainting.h>
  15. #include <LibWeb/Painting/PaintableBox.h>
  16. #include <LibWeb/Painting/StackingContext.h>
  17. #include <LibWeb/Platform/FontPlugin.h>
  18. namespace Web::Painting {
  19. JS::NonnullGCPtr<PaintableWithLines> PaintableWithLines::create(Layout::BlockContainer const& block_container)
  20. {
  21. return block_container.heap().allocate_without_realm<PaintableWithLines>(block_container);
  22. }
  23. JS::NonnullGCPtr<PaintableBox> PaintableBox::create(Layout::Box const& layout_box)
  24. {
  25. return layout_box.heap().allocate_without_realm<PaintableBox>(layout_box);
  26. }
  27. PaintableBox::PaintableBox(Layout::Box const& layout_box)
  28. : Paintable(layout_box)
  29. {
  30. }
  31. PaintableBox::~PaintableBox()
  32. {
  33. }
  34. void PaintableBox::invalidate_stacking_context()
  35. {
  36. m_stacking_context = nullptr;
  37. }
  38. bool PaintableBox::is_out_of_view(PaintContext& context) const
  39. {
  40. return !context.enclosing_device_rect(absolute_paint_rect())
  41. .to_type<int>()
  42. .translated(context.painter().translation())
  43. .intersects(context.painter().clip_rect());
  44. }
  45. PaintableWithLines::PaintableWithLines(Layout::BlockContainer const& layout_box)
  46. : PaintableBox(layout_box)
  47. {
  48. }
  49. PaintableWithLines::~PaintableWithLines()
  50. {
  51. }
  52. void PaintableBox::set_offset(CSSPixelPoint offset)
  53. {
  54. m_offset = offset;
  55. layout_box().did_set_rect();
  56. }
  57. void PaintableBox::set_content_size(CSSPixelSize size)
  58. {
  59. m_content_size = size;
  60. layout_box().did_set_rect();
  61. }
  62. CSSPixelPoint PaintableBox::effective_offset() const
  63. {
  64. CSSPixelPoint offset;
  65. if (containing_block() && m_containing_line_box_fragment.has_value()) {
  66. auto& paintable_with_lines = *verify_cast<PaintableWithLines>(containing_block()->paint_box());
  67. auto const& fragment = paintable_with_lines.line_boxes()[m_containing_line_box_fragment->line_box_index].fragments()[m_containing_line_box_fragment->fragment_index];
  68. offset = fragment.offset();
  69. } else {
  70. offset = m_offset;
  71. }
  72. if (layout_box().computed_values().position() == CSS::Position::Relative) {
  73. auto const& inset = layout_box().box_model().inset;
  74. offset.translate_by(inset.left, inset.top);
  75. }
  76. return offset;
  77. }
  78. CSSPixelRect PaintableBox::compute_absolute_rect() const
  79. {
  80. CSSPixelRect rect { effective_offset(), content_size() };
  81. for (auto const* block = containing_block(); block && block->paintable(); block = block->paintable()->containing_block())
  82. rect.translate_by(block->paint_box()->effective_offset());
  83. return rect;
  84. }
  85. CSSPixelRect PaintableBox::absolute_rect() const
  86. {
  87. if (!m_absolute_rect.has_value())
  88. m_absolute_rect = compute_absolute_rect();
  89. return *m_absolute_rect;
  90. }
  91. CSSPixelRect PaintableBox::compute_absolute_paint_rect() const
  92. {
  93. // FIXME: This likely incomplete:
  94. auto rect = absolute_border_box_rect();
  95. auto resolved_box_shadow_data = resolve_box_shadow_data();
  96. for (auto const& shadow : resolved_box_shadow_data) {
  97. if (shadow.placement == ShadowPlacement::Inner)
  98. continue;
  99. auto inflate = shadow.spread_distance + shadow.blur_radius;
  100. auto shadow_rect = rect.inflated(inflate, inflate, inflate, inflate).translated(shadow.offset_x, shadow.offset_y);
  101. rect = rect.united(shadow_rect);
  102. }
  103. return rect;
  104. }
  105. CSSPixelRect PaintableBox::absolute_paint_rect() const
  106. {
  107. if (!m_absolute_paint_rect.has_value())
  108. m_absolute_paint_rect = compute_absolute_paint_rect();
  109. return *m_absolute_paint_rect;
  110. }
  111. void PaintableBox::set_containing_line_box_fragment(Optional<Layout::LineBoxFragmentCoordinate> fragment_coordinate)
  112. {
  113. m_containing_line_box_fragment = fragment_coordinate;
  114. }
  115. Painting::StackingContext* PaintableBox::enclosing_stacking_context()
  116. {
  117. for (auto* ancestor = layout_box().parent(); ancestor; ancestor = ancestor->parent()) {
  118. if (!is<Layout::Box>(ancestor))
  119. continue;
  120. auto& ancestor_box = static_cast<Layout::Box&>(const_cast<Layout::NodeWithStyle&>(*ancestor));
  121. if (auto* ancestor_paint_box = ancestor_box.paint_box(); ancestor_paint_box && ancestor_paint_box->stacking_context())
  122. return const_cast<StackingContext*>(ancestor_paint_box->stacking_context());
  123. }
  124. // We should always reach the Layout::Viewport stacking context.
  125. VERIFY_NOT_REACHED();
  126. }
  127. void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
  128. {
  129. if (!is_visible())
  130. return;
  131. auto clip_rect = computed_values().clip();
  132. auto should_clip_rect = clip_rect.is_rect() && layout_box().is_absolutely_positioned();
  133. if (phase == PaintPhase::Background) {
  134. if (should_clip_rect) {
  135. context.painter().save();
  136. auto border_box = absolute_border_box_rect();
  137. context.painter().add_clip_rect(context.rounded_device_rect(clip_rect.to_rect().resolved(Paintable::layout_node(), border_box.to_type<float>()).to_type<CSSPixels>()).to_type<int>());
  138. }
  139. paint_backdrop_filter(context);
  140. paint_background(context);
  141. paint_box_shadow(context);
  142. }
  143. if (phase == PaintPhase::Border) {
  144. paint_border(context);
  145. }
  146. if (phase == PaintPhase::Overlay && should_clip_rect)
  147. context.painter().restore();
  148. if (phase == PaintPhase::Overlay && layout_box().dom_node() && layout_box().document().inspected_node() == layout_box().dom_node()) {
  149. auto content_rect = absolute_rect();
  150. auto margin_box = box_model().margin_box();
  151. CSSPixelRect margin_rect;
  152. margin_rect.set_x(absolute_x() - margin_box.left);
  153. margin_rect.set_width(content_width() + margin_box.left + margin_box.right);
  154. margin_rect.set_y(absolute_y() - margin_box.top);
  155. margin_rect.set_height(content_height() + margin_box.top + margin_box.bottom);
  156. auto border_rect = absolute_border_box_rect();
  157. auto padding_rect = absolute_padding_box_rect();
  158. auto paint_inspector_rect = [&](CSSPixelRect const& rect, Color color) {
  159. auto device_rect = context.enclosing_device_rect(rect).to_type<int>();
  160. context.painter().fill_rect(device_rect, Color(color).with_alpha(100));
  161. context.painter().draw_rect(device_rect, Color(color));
  162. };
  163. paint_inspector_rect(margin_rect, Color::Yellow);
  164. paint_inspector_rect(padding_rect, Color::Cyan);
  165. paint_inspector_rect(border_rect, Color::Green);
  166. paint_inspector_rect(content_rect, Color::Magenta);
  167. auto& font = Platform::FontPlugin::the().default_font();
  168. StringBuilder builder;
  169. if (layout_box().dom_node())
  170. builder.append(layout_box().dom_node()->debug_description());
  171. else
  172. builder.append(layout_box().debug_description());
  173. builder.appendff(" {}x{} @ {},{}", border_rect.width(), border_rect.height(), border_rect.x(), border_rect.y());
  174. auto size_text = builder.to_deprecated_string();
  175. auto size_text_rect = border_rect;
  176. size_text_rect.set_y(border_rect.y() + border_rect.height());
  177. size_text_rect.set_top(size_text_rect.top());
  178. size_text_rect.set_width((float)font.width(size_text) + 4);
  179. size_text_rect.set_height(font.pixel_size() + 4);
  180. auto size_text_device_rect = context.enclosing_device_rect(size_text_rect).to_type<int>();
  181. context.painter().fill_rect(size_text_device_rect, context.palette().color(Gfx::ColorRole::Tooltip));
  182. context.painter().draw_rect(size_text_device_rect, context.palette().threed_shadow1());
  183. context.painter().draw_text(size_text_device_rect, size_text, font, Gfx::TextAlignment::Center, context.palette().color(Gfx::ColorRole::TooltipText));
  184. }
  185. if (phase == PaintPhase::FocusOutline && layout_box().dom_node() && layout_box().dom_node()->is_element() && verify_cast<DOM::Element>(*layout_box().dom_node()).is_focused()) {
  186. // FIXME: Implement this as `outline` using :focus-visible in the default UA stylesheet to make it possible to override/disable.
  187. auto focus_outline_rect = context.enclosing_device_rect(absolute_border_box_rect()).inflated(4, 4);
  188. context.painter().draw_focus_rect(focus_outline_rect.to_type<int>(), context.palette().focus_outline());
  189. }
  190. }
  191. void PaintableBox::paint_border(PaintContext& context) const
  192. {
  193. auto borders_data = BordersData {
  194. .top = box_model().border.top == 0 ? CSS::BorderData() : computed_values().border_top(),
  195. .right = box_model().border.right == 0 ? CSS::BorderData() : computed_values().border_right(),
  196. .bottom = box_model().border.bottom == 0 ? CSS::BorderData() : computed_values().border_bottom(),
  197. .left = box_model().border.left == 0 ? CSS::BorderData() : computed_values().border_left(),
  198. };
  199. paint_all_borders(context, absolute_border_box_rect(), normalized_border_radii_data(), borders_data);
  200. }
  201. void PaintableBox::paint_backdrop_filter(PaintContext& context) const
  202. {
  203. auto& backdrop_filter = computed_values().backdrop_filter();
  204. if (!backdrop_filter.is_none())
  205. Painting::apply_backdrop_filter(context, layout_node(), absolute_border_box_rect(), normalized_border_radii_data(), backdrop_filter);
  206. }
  207. void PaintableBox::paint_background(PaintContext& context) const
  208. {
  209. // If the body's background properties were propagated to the root element, do no re-paint the body's background.
  210. if (layout_box().is_body() && document().html_element()->should_use_body_background_properties())
  211. return;
  212. CSSPixelRect background_rect;
  213. Color background_color = computed_values().background_color();
  214. auto* background_layers = &computed_values().background_layers();
  215. if (layout_box().is_root_element()) {
  216. // CSS 2.1 Appendix E.2: If the element is a root element, paint the background over the entire canvas.
  217. background_rect = context.css_viewport_rect();
  218. // Section 2.11.2: If the computed value of background-image on the root element is none and its background-color is transparent,
  219. // user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY child element.
  220. if (document().html_element()->should_use_body_background_properties()) {
  221. background_layers = document().background_layers();
  222. background_color = document().background_color(context.palette());
  223. }
  224. } else {
  225. background_rect = absolute_padding_box_rect();
  226. }
  227. // HACK: If the Box has a border, use the bordered_rect to paint the background.
  228. // This way if we have a border-radius there will be no gap between the filling and actual border.
  229. if (computed_values().border_top().width || computed_values().border_right().width || computed_values().border_bottom().width || computed_values().border_left().width)
  230. background_rect = absolute_border_box_rect();
  231. Painting::paint_background(context, layout_box(), background_rect, background_color, computed_values().image_rendering(), background_layers, normalized_border_radii_data());
  232. }
  233. Vector<ShadowData> PaintableBox::resolve_box_shadow_data() const
  234. {
  235. auto box_shadow_data = computed_values().box_shadow();
  236. if (box_shadow_data.is_empty())
  237. return {};
  238. Vector<ShadowData> resolved_box_shadow_data;
  239. resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
  240. for (auto const& layer : box_shadow_data) {
  241. resolved_box_shadow_data.empend(
  242. layer.color,
  243. layer.offset_x.to_px(layout_box()),
  244. layer.offset_y.to_px(layout_box()),
  245. layer.blur_radius.to_px(layout_box()),
  246. layer.spread_distance.to_px(layout_box()),
  247. layer.placement == CSS::ShadowPlacement::Outer ? ShadowPlacement::Outer : ShadowPlacement::Inner);
  248. }
  249. return resolved_box_shadow_data;
  250. }
  251. void PaintableBox::paint_box_shadow(PaintContext& context) const
  252. {
  253. auto resolved_box_shadow_data = resolve_box_shadow_data();
  254. if (resolved_box_shadow_data.is_empty())
  255. return;
  256. Painting::paint_box_shadow(context, absolute_border_box_rect(), normalized_border_radii_data(), resolved_box_shadow_data);
  257. }
  258. BorderRadiiData PaintableBox::normalized_border_radii_data(ShrinkRadiiForBorders shrink) const
  259. {
  260. auto border_radius_data = Painting::normalized_border_radii_data(layout_box(),
  261. absolute_border_box_rect(),
  262. computed_values().border_top_left_radius(),
  263. computed_values().border_top_right_radius(),
  264. computed_values().border_bottom_right_radius(),
  265. computed_values().border_bottom_left_radius());
  266. if (shrink == ShrinkRadiiForBorders::Yes)
  267. border_radius_data.shrink(computed_values().border_top().width, computed_values().border_right().width, computed_values().border_bottom().width, computed_values().border_left().width);
  268. return border_radius_data;
  269. }
  270. Optional<CSSPixelRect> PaintableBox::calculate_overflow_clipped_rect() const
  271. {
  272. if (!m_clip_rect.has_value()) {
  273. // NOTE: stacking context should not be crossed while aggregating rectangle to
  274. // clip `overflow: hidden` because intersecting rectangles with different
  275. // transforms doesn't make sense
  276. // TODO: figure out if there are cases when stacking context should be
  277. // crossed to calculate correct clip rect
  278. if (!stacking_context() && containing_block() && containing_block()->paint_box()) {
  279. m_clip_rect = containing_block()->paint_box()->calculate_overflow_clipped_rect();
  280. }
  281. auto overflow_x = computed_values().overflow_x();
  282. auto overflow_y = computed_values().overflow_y();
  283. if (overflow_x == CSS::Overflow::Hidden && overflow_y == CSS::Overflow::Hidden) {
  284. if (m_clip_rect.has_value()) {
  285. m_clip_rect->intersect(absolute_padding_box_rect());
  286. } else {
  287. m_clip_rect = absolute_padding_box_rect();
  288. }
  289. }
  290. }
  291. return m_clip_rect;
  292. }
  293. void PaintableBox::apply_clip_overflow_rect(PaintContext& context, PaintPhase phase) const
  294. {
  295. if (!AK::first_is_one_of(phase, PaintPhase::Background, PaintPhase::Border, PaintPhase::Foreground))
  296. return;
  297. // FIXME: Support more overflow variations.
  298. auto clip_rect = this->calculate_overflow_clipped_rect();
  299. auto overflow_x = computed_values().overflow_x();
  300. auto overflow_y = computed_values().overflow_y();
  301. auto clip_overflow = [&] {
  302. if (!m_clipping_overflow) {
  303. context.painter().save();
  304. context.painter().add_clip_rect(context.rounded_device_rect(*clip_rect).to_type<int>());
  305. m_clipping_overflow = true;
  306. }
  307. };
  308. if (clip_rect.has_value()) {
  309. clip_overflow();
  310. }
  311. if (overflow_y == CSS::Overflow::Hidden && overflow_x == CSS::Overflow::Hidden) {
  312. auto border_radii_data = normalized_border_radii_data(ShrinkRadiiForBorders::Yes);
  313. if (border_radii_data.has_any_radius()) {
  314. auto corner_clipper = BorderRadiusCornerClipper::create(context, context.rounded_device_rect(*clip_rect), border_radii_data, CornerClip::Outside, BorderRadiusCornerClipper::UseCachedBitmap::No);
  315. if (corner_clipper.is_error()) {
  316. dbgln("Failed to create overflow border-radius corner clipper: {}", corner_clipper.error());
  317. return;
  318. }
  319. clip_overflow();
  320. m_overflow_corner_radius_clipper = corner_clipper.release_value();
  321. m_overflow_corner_radius_clipper->sample_under_corners(context.painter());
  322. }
  323. }
  324. }
  325. void PaintableBox::clear_clip_overflow_rect(PaintContext& context, PaintPhase phase) const
  326. {
  327. if (!AK::first_is_one_of(phase, PaintPhase::Background, PaintPhase::Border, PaintPhase::Foreground))
  328. return;
  329. // FIXME: Support more overflow variations.
  330. if (m_clipping_overflow) {
  331. context.painter().restore();
  332. m_clipping_overflow = false;
  333. }
  334. if (m_overflow_corner_radius_clipper.has_value()) {
  335. m_overflow_corner_radius_clipper->blit_corner_clipping(context.painter());
  336. m_overflow_corner_radius_clipper = {};
  337. }
  338. }
  339. static void paint_cursor_if_needed(PaintContext& context, Layout::TextNode const& text_node, Layout::LineBoxFragment const& fragment)
  340. {
  341. auto const& browsing_context = text_node.browsing_context();
  342. if (!browsing_context.is_focused_context())
  343. return;
  344. if (!browsing_context.cursor_blink_state())
  345. return;
  346. if (browsing_context.cursor_position().node() != &text_node.dom_node())
  347. return;
  348. // 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.
  349. if (browsing_context.cursor_position().offset() < (unsigned)fragment.start() || browsing_context.cursor_position().offset() > (unsigned)(fragment.start() + fragment.length()))
  350. return;
  351. if (!fragment.layout_node().dom_node() || !fragment.layout_node().dom_node()->is_editable())
  352. return;
  353. auto fragment_rect = fragment.absolute_rect();
  354. CSSPixelRect cursor_rect {
  355. fragment_rect.x() + text_node.font().width(fragment.text().substring_view(0, text_node.browsing_context().cursor_position().offset() - fragment.start())),
  356. fragment_rect.top(),
  357. 1,
  358. fragment_rect.height()
  359. };
  360. auto cursor_device_rect = context.rounded_device_rect(cursor_rect).to_type<int>();
  361. context.painter().draw_rect(cursor_device_rect, text_node.computed_values().color());
  362. }
  363. static void paint_text_decoration(PaintContext& context, Gfx::Painter& painter, Layout::Node const& text_node, Layout::LineBoxFragment const& fragment)
  364. {
  365. auto& font = fragment.layout_node().font();
  366. auto fragment_box = fragment.absolute_rect();
  367. CSSPixels glyph_height = font.pixel_size();
  368. auto baseline = fragment_box.height() / 2 - (glyph_height + 4) / 2 + glyph_height;
  369. auto line_color = text_node.computed_values().text_decoration_color();
  370. CSSPixels css_line_thickness = [&] {
  371. CSS::Length computed_thickness = text_node.computed_values().text_decoration_thickness().resolved(text_node, CSS::Length(1, CSS::Length::Type::Em));
  372. if (computed_thickness.is_auto())
  373. return max(glyph_height * 0.1f, 1.f);
  374. return computed_thickness.to_px(text_node);
  375. }();
  376. auto device_line_thickness = context.rounded_device_pixels(css_line_thickness);
  377. auto text_decoration_lines = text_node.computed_values().text_decoration_line();
  378. for (auto line : text_decoration_lines) {
  379. DevicePixelPoint line_start_point {};
  380. DevicePixelPoint line_end_point {};
  381. switch (line) {
  382. case CSS::TextDecorationLine::None:
  383. return;
  384. case CSS::TextDecorationLine::Underline:
  385. line_start_point = context.rounded_device_point(fragment_box.top_left().translated(0, baseline + 2));
  386. line_end_point = context.rounded_device_point(fragment_box.top_right().translated(0, baseline + 2));
  387. break;
  388. case CSS::TextDecorationLine::Overline:
  389. line_start_point = context.rounded_device_point(fragment_box.top_left().translated(0, baseline - glyph_height));
  390. line_end_point = context.rounded_device_point(fragment_box.top_right().translated(0, baseline - glyph_height));
  391. break;
  392. case CSS::TextDecorationLine::LineThrough: {
  393. auto x_height = font.x_height();
  394. line_start_point = context.rounded_device_point(fragment_box.top_left().translated(0, baseline - x_height * 0.5f));
  395. line_end_point = context.rounded_device_point(fragment_box.top_right().translated(0, baseline - x_height * 0.5f));
  396. break;
  397. }
  398. case CSS::TextDecorationLine::Blink:
  399. // Conforming user agents may simply not blink the text
  400. return;
  401. }
  402. switch (text_node.computed_values().text_decoration_style()) {
  403. case CSS::TextDecorationStyle::Solid:
  404. 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);
  405. break;
  406. case CSS::TextDecorationStyle::Double:
  407. switch (line) {
  408. case CSS::TextDecorationLine::Underline:
  409. break;
  410. case CSS::TextDecorationLine::Overline:
  411. line_start_point.translate_by(0, -device_line_thickness - context.rounded_device_pixels(1));
  412. line_end_point.translate_by(0, -device_line_thickness - context.rounded_device_pixels(1));
  413. break;
  414. case CSS::TextDecorationLine::LineThrough:
  415. line_start_point.translate_by(0, -device_line_thickness / 2);
  416. line_end_point.translate_by(0, -device_line_thickness / 2);
  417. break;
  418. default:
  419. VERIFY_NOT_REACHED();
  420. }
  421. painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value());
  422. 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());
  423. break;
  424. case CSS::TextDecorationStyle::Dashed:
  425. 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);
  426. break;
  427. case CSS::TextDecorationStyle::Dotted:
  428. 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);
  429. break;
  430. case CSS::TextDecorationStyle::Wavy:
  431. 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());
  432. break;
  433. }
  434. }
  435. }
  436. static void paint_text_fragment(PaintContext& context, Layout::TextNode const& text_node, Layout::LineBoxFragment const& fragment, Painting::PaintPhase phase)
  437. {
  438. auto& painter = context.painter();
  439. if (phase == Painting::PaintPhase::Foreground) {
  440. auto fragment_absolute_rect = fragment.absolute_rect();
  441. auto fragment_absolute_device_rect = context.enclosing_device_rect(fragment_absolute_rect);
  442. if (text_node.document().inspected_node() == &text_node.dom_node())
  443. context.painter().draw_rect(fragment_absolute_device_rect.to_type<int>(), Color::Magenta);
  444. auto text = text_node.text_for_rendering();
  445. DevicePixelPoint baseline_start { fragment_absolute_device_rect.x(), fragment_absolute_device_rect.y() + context.rounded_device_pixels(fragment.baseline()) };
  446. Utf8View view { text.substring_view(fragment.start(), fragment.length()) };
  447. auto& scaled_font = fragment.layout_node().scaled_font(context);
  448. painter.draw_text_run(baseline_start.to_type<int>(), view, scaled_font, text_node.computed_values().color());
  449. auto selection_rect = context.enclosing_device_rect(fragment.selection_rect(text_node.font())).to_type<int>();
  450. if (!selection_rect.is_empty()) {
  451. painter.fill_rect(selection_rect, context.palette().selection());
  452. Gfx::PainterStateSaver saver(painter);
  453. painter.add_clip_rect(selection_rect);
  454. painter.draw_text_run(baseline_start.to_type<int>(), view, scaled_font, context.palette().selection_text());
  455. }
  456. paint_text_decoration(context, painter, text_node, fragment);
  457. paint_cursor_if_needed(context, text_node, fragment);
  458. }
  459. }
  460. void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
  461. {
  462. if (!is_visible())
  463. return;
  464. PaintableBox::paint(context, phase);
  465. if (m_line_boxes.is_empty())
  466. return;
  467. bool should_clip_overflow = computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
  468. Optional<BorderRadiusCornerClipper> corner_clipper;
  469. if (should_clip_overflow) {
  470. context.painter().save();
  471. // FIXME: Handle overflow-x and overflow-y being different values.
  472. auto clip_box = context.rounded_device_rect(absolute_padding_box_rect());
  473. context.painter().add_clip_rect(clip_box.to_type<int>());
  474. auto scroll_offset = context.rounded_device_point(static_cast<Layout::BlockContainer const&>(layout_box()).scroll_offset());
  475. context.painter().translate(-scroll_offset.to_type<int>());
  476. auto border_radii = normalized_border_radii_data(ShrinkRadiiForBorders::Yes);
  477. if (border_radii.has_any_radius()) {
  478. auto clipper = BorderRadiusCornerClipper::create(context, clip_box, border_radii);
  479. if (!clipper.is_error()) {
  480. corner_clipper = clipper.release_value();
  481. corner_clipper->sample_under_corners(context.painter());
  482. }
  483. }
  484. }
  485. // Text shadows
  486. // This is yet another loop, but done here because all shadows should appear under all text.
  487. // So, we paint the shadows before painting any text.
  488. // FIXME: Find a smarter way to do this?
  489. if (phase == PaintPhase::Foreground) {
  490. for (auto& line_box : m_line_boxes) {
  491. for (auto& fragment : line_box.fragments()) {
  492. if (is<Layout::TextNode>(fragment.layout_node())) {
  493. auto& text_shadow = fragment.layout_node().computed_values().text_shadow();
  494. if (!text_shadow.is_empty()) {
  495. Vector<ShadowData> resolved_shadow_data;
  496. resolved_shadow_data.ensure_capacity(text_shadow.size());
  497. for (auto const& layer : text_shadow) {
  498. resolved_shadow_data.empend(
  499. layer.color,
  500. layer.offset_x.to_px(layout_box()),
  501. layer.offset_y.to_px(layout_box()),
  502. layer.blur_radius.to_px(layout_box()),
  503. layer.spread_distance.to_px(layout_box()),
  504. ShadowPlacement::Outer);
  505. }
  506. context.painter().set_font(fragment.layout_node().font());
  507. Painting::paint_text_shadow(context, fragment, resolved_shadow_data);
  508. }
  509. }
  510. }
  511. }
  512. }
  513. for (auto& line_box : m_line_boxes) {
  514. for (auto& fragment : line_box.fragments()) {
  515. if (context.should_show_line_box_borders()) {
  516. auto fragment_absolute_rect = fragment.absolute_rect();
  517. context.painter().draw_rect(context.enclosing_device_rect(fragment_absolute_rect).to_type<int>(), Color::Green);
  518. context.painter().draw_line(
  519. context.rounded_device_point(fragment_absolute_rect.top_left().translated(0, fragment.baseline())).to_type<int>(),
  520. context.rounded_device_point(fragment_absolute_rect.top_right().translated(0, fragment.baseline())).to_type<int>(), Color::Red);
  521. }
  522. if (is<Layout::TextNode>(fragment.layout_node()))
  523. paint_text_fragment(context, static_cast<Layout::TextNode const&>(fragment.layout_node()), fragment, phase);
  524. }
  525. }
  526. if (should_clip_overflow) {
  527. context.painter().restore();
  528. if (corner_clipper.has_value())
  529. corner_clipper->blit_corner_clipping(context.painter());
  530. }
  531. // FIXME: Merge this loop with the above somehow..
  532. if (phase == PaintPhase::FocusOutline) {
  533. for (auto& line_box : m_line_boxes) {
  534. for (auto& fragment : line_box.fragments()) {
  535. auto* node = fragment.layout_node().dom_node();
  536. if (!node)
  537. continue;
  538. auto* parent = node->parent_element();
  539. if (!parent)
  540. continue;
  541. if (parent->is_focused()) {
  542. // FIXME: Implement this as `outline` using :focus-visible in the default UA stylesheet to make it possible to override/disable.
  543. auto focus_outline_rect = context.enclosing_device_rect(fragment.absolute_rect()).to_type<int>().inflated(4, 4);
  544. context.painter().draw_focus_rect(focus_outline_rect, context.palette().focus_outline());
  545. }
  546. }
  547. }
  548. }
  549. }
  550. bool PaintableWithLines::handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
  551. {
  552. if (!layout_box().is_scrollable())
  553. return false;
  554. auto new_offset = layout_box().scroll_offset();
  555. new_offset.translate_by(wheel_delta_x, wheel_delta_y);
  556. layout_box().set_scroll_offset(new_offset);
  557. return true;
  558. }
  559. Layout::BlockContainer const& PaintableWithLines::layout_box() const
  560. {
  561. return static_cast<Layout::BlockContainer const&>(PaintableBox::layout_box());
  562. }
  563. Layout::BlockContainer& PaintableWithLines::layout_box()
  564. {
  565. return static_cast<Layout::BlockContainer&>(PaintableBox::layout_box());
  566. }
  567. void PaintableBox::set_stacking_context(NonnullOwnPtr<StackingContext> stacking_context)
  568. {
  569. m_stacking_context = move(stacking_context);
  570. }
  571. Optional<HitTestResult> PaintableBox::hit_test(CSSPixelPoint position, HitTestType type) const
  572. {
  573. if (!is_visible())
  574. return {};
  575. if (layout_box().is_viewport()) {
  576. const_cast<Layout::Viewport&>(static_cast<Layout::Viewport const&>(layout_box())).build_stacking_context_tree_if_needed();
  577. return stacking_context()->hit_test(position, type);
  578. }
  579. if (!absolute_border_box_rect().contains(position.x(), position.y()))
  580. return {};
  581. for (auto* child = first_child(); child; child = child->next_sibling()) {
  582. auto result = child->hit_test(position, type);
  583. if (!result.has_value())
  584. continue;
  585. if (!result->paintable->visible_for_hit_testing())
  586. continue;
  587. return result;
  588. }
  589. if (!visible_for_hit_testing())
  590. return {};
  591. return HitTestResult { const_cast<PaintableBox&>(*this) };
  592. }
  593. Optional<HitTestResult> PaintableWithLines::hit_test(CSSPixelPoint position, HitTestType type) const
  594. {
  595. if (!layout_box().children_are_inline())
  596. return PaintableBox::hit_test(position, type);
  597. Optional<HitTestResult> last_good_candidate;
  598. for (auto& line_box : m_line_boxes) {
  599. for (auto& fragment : line_box.fragments()) {
  600. if (is<Layout::Box>(fragment.layout_node()) && static_cast<Layout::Box const&>(fragment.layout_node()).paint_box()->stacking_context())
  601. continue;
  602. if (!fragment.layout_node().containing_block()) {
  603. dbgln("FIXME: PaintableWithLines::hit_test(): Missing containing block on {}", fragment.layout_node().debug_description());
  604. continue;
  605. }
  606. auto fragment_absolute_rect = fragment.absolute_rect();
  607. if (fragment_absolute_rect.contains(position)) {
  608. if (is<Layout::BlockContainer>(fragment.layout_node()) && fragment.layout_node().paintable())
  609. return fragment.layout_node().paintable()->hit_test(position, type);
  610. return HitTestResult { const_cast<Paintable&>(const_cast<Paintable&>(*fragment.layout_node().paintable())), fragment.text_index_at(position.x()) };
  611. }
  612. // 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.
  613. // This determines whether the fragment is a good candidate for the position. The last such good fragment is chosen.
  614. // 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.
  615. // We arbitrarily choose to consider the end of the line above and ignore the beginning of the line below.
  616. // If we knew the direction of selection, we could make a better choice.
  617. if (fragment_absolute_rect.bottom() <= position.y()) { // fully below the fragment
  618. last_good_candidate = HitTestResult { const_cast<Paintable&>(*fragment.layout_node().paintable()), fragment.start() + fragment.length() };
  619. } else if (fragment_absolute_rect.top() <= position.y()) { // vertically within the fragment
  620. if (position.x() < fragment_absolute_rect.left()) { // left of the fragment
  621. if (!last_good_candidate.has_value()) { // first fragment of the line
  622. last_good_candidate = HitTestResult { const_cast<Paintable&>(*fragment.layout_node().paintable()), fragment.start() };
  623. }
  624. } else { // right of the fragment
  625. last_good_candidate = HitTestResult { const_cast<Paintable&>(*fragment.layout_node().paintable()), fragment.start() + fragment.length() };
  626. }
  627. }
  628. }
  629. }
  630. if (type == HitTestType::TextCursor && last_good_candidate.has_value())
  631. return last_good_candidate;
  632. if (is_visible() && absolute_border_box_rect().contains(position.x(), position.y()))
  633. return HitTestResult { const_cast<PaintableWithLines&>(*this) };
  634. return {};
  635. }
  636. }