MarkerPaintable.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGC/Heap.h>
  7. #include <LibWeb/Layout/ListItemMarkerBox.h>
  8. #include <LibWeb/Painting/MarkerPaintable.h>
  9. namespace Web::Painting {
  10. GC_DEFINE_ALLOCATOR(MarkerPaintable);
  11. GC::Ref<MarkerPaintable> MarkerPaintable::create(Layout::ListItemMarkerBox const& layout_box)
  12. {
  13. return layout_box.heap().allocate<MarkerPaintable>(layout_box);
  14. }
  15. MarkerPaintable::MarkerPaintable(Layout::ListItemMarkerBox const& layout_box)
  16. : PaintableBox(layout_box)
  17. {
  18. }
  19. Layout::ListItemMarkerBox const& MarkerPaintable::layout_box() const
  20. {
  21. return static_cast<Layout::ListItemMarkerBox const&>(layout_node());
  22. }
  23. constexpr float sin_60_deg = 0.866025403f;
  24. void MarkerPaintable::paint(PaintContext& context, PaintPhase phase) const
  25. {
  26. if (phase == PaintPhase::Overlay)
  27. PaintableBox::paint(context, phase);
  28. if (phase != PaintPhase::Foreground)
  29. return;
  30. CSSPixelRect enclosing = absolute_rect().to_rounded<CSSPixels>();
  31. auto device_enclosing = context.enclosing_device_rect(enclosing);
  32. CSSPixels marker_width = enclosing.height() / 2;
  33. if (auto const* list_style_image = layout_box().list_style_image()) {
  34. CSSPixelRect image_rect {
  35. 0, 0,
  36. list_style_image->natural_width().value_or(marker_width),
  37. list_style_image->natural_height().value_or(marker_width)
  38. };
  39. image_rect.center_within(enclosing);
  40. auto device_image_rect = context.enclosing_device_rect(image_rect);
  41. list_style_image->resolve_for_size(layout_box(), image_rect.size());
  42. list_style_image->paint(context, device_image_rect, computed_values().image_rendering());
  43. return;
  44. }
  45. CSSPixelRect marker_rect { 0, 0, marker_width, marker_width };
  46. marker_rect.center_within(enclosing);
  47. auto device_marker_rect = context.enclosing_device_rect(marker_rect);
  48. float left = device_marker_rect.x().value();
  49. float right = left + device_marker_rect.width().value();
  50. float top = device_marker_rect.y().value();
  51. float bottom = top + device_marker_rect.height().value();
  52. auto color = computed_values().color();
  53. switch (layout_box().list_style_type()) {
  54. case CSS::ListStyleType::Square:
  55. context.display_list_recorder().fill_rect(device_marker_rect.to_type<int>(), color);
  56. break;
  57. case CSS::ListStyleType::Circle:
  58. context.display_list_recorder().draw_ellipse(device_marker_rect.to_type<int>(), color, 1);
  59. break;
  60. case CSS::ListStyleType::Disc:
  61. context.display_list_recorder().fill_ellipse(device_marker_rect.to_type<int>(), color);
  62. break;
  63. case CSS::ListStyleType::DisclosureClosed: {
  64. // https://drafts.csswg.org/css-counter-styles-3/#disclosure-closed
  65. // For the disclosure-open and disclosure-closed counter styles, the marker must be an image or character suitable for indicating the open and closed states of a disclosure widget, such as HTML’s details element.
  66. // FIXME: If the image is directional, it must respond to the writing mode of the element, similar to the bidi-sensitive images feature of the Images 4 module.
  67. // Draw an equilateral triangle pointing right.
  68. auto path = Gfx::Path();
  69. path.move_to({ left, top });
  70. path.line_to({ left + sin_60_deg * (right - left), (top + bottom) / 2 });
  71. path.line_to({ left, bottom });
  72. path.close();
  73. context.display_list_recorder().fill_path({ .path = path, .color = color, .winding_rule = Gfx::WindingRule::EvenOdd });
  74. break;
  75. }
  76. case CSS::ListStyleType::DisclosureOpen: {
  77. // https://drafts.csswg.org/css-counter-styles-3/#disclosure-open
  78. // For the disclosure-open and disclosure-closed counter styles, the marker must be an image or character suitable for indicating the open and closed states of a disclosure widget, such as HTML’s details element.
  79. // FIXME: If the image is directional, it must respond to the writing mode of the element, similar to the bidi-sensitive images feature of the Images 4 module.
  80. // Draw an equilateral triangle pointing down.
  81. auto path = Gfx::Path();
  82. path.move_to({ left, top });
  83. path.line_to({ right, top });
  84. path.line_to({ (left + right) / 2, top + sin_60_deg * (bottom - top) });
  85. path.close();
  86. context.display_list_recorder().fill_path({ .path = path, .color = color, .winding_rule = Gfx::WindingRule::EvenOdd });
  87. break;
  88. }
  89. case CSS::ListStyleType::Decimal:
  90. case CSS::ListStyleType::DecimalLeadingZero:
  91. case CSS::ListStyleType::LowerAlpha:
  92. case CSS::ListStyleType::LowerLatin:
  93. case CSS::ListStyleType::LowerRoman:
  94. case CSS::ListStyleType::UpperAlpha:
  95. case CSS::ListStyleType::UpperLatin:
  96. case CSS::ListStyleType::UpperRoman: {
  97. auto text = layout_box().text();
  98. if (!text.has_value())
  99. break;
  100. // FIXME: This should use proper text layout logic!
  101. // This does not line up with the text in the <li> element which looks very sad :(
  102. context.display_list_recorder().draw_text(device_enclosing.to_type<int>(), MUST(String::from_byte_string(*text)), layout_box().scaled_font(context), Gfx::TextAlignment::Center, color);
  103. break;
  104. }
  105. case CSS::ListStyleType::None:
  106. return;
  107. default:
  108. VERIFY_NOT_REACHED();
  109. }
  110. }
  111. }