MarkerPaintable.cpp 5.4 KB

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