MarkerPaintable.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // FIXME: All this does is round to the nearest whole CSS pixel, but it's goofy.
  32. CSSPixelRect enclosing = absolute_rect().to_type<double>().to_type<float>().to_rounded<float>().to_type<CSSPixels>();
  33. auto device_enclosing = context.enclosing_device_rect(enclosing);
  34. CSSPixels marker_width = enclosing.height() / 2.0;
  35. if (auto const* list_style_image = layout_box().list_style_image()) {
  36. CSSPixelRect image_rect {
  37. 0, 0,
  38. list_style_image->natural_width().value_or(marker_width),
  39. list_style_image->natural_height().value_or(marker_width)
  40. };
  41. image_rect.center_within(enclosing);
  42. auto device_image_rect = context.enclosing_device_rect(image_rect);
  43. list_style_image->resolve_for_size(layout_box(), image_rect.size());
  44. list_style_image->paint(context, device_image_rect, computed_values().image_rendering());
  45. return;
  46. }
  47. CSSPixelRect marker_rect { 0, 0, marker_width, marker_width };
  48. marker_rect.center_within(enclosing);
  49. auto device_marker_rect = context.enclosing_device_rect(marker_rect);
  50. float left = device_marker_rect.x().value();
  51. float right = left + device_marker_rect.width().value();
  52. float top = device_marker_rect.y().value();
  53. float bottom = top + device_marker_rect.height().value();
  54. auto color = computed_values().color();
  55. Gfx::AntiAliasingPainter aa_painter { context.painter() };
  56. switch (layout_box().list_style_type()) {
  57. case CSS::ListStyleType::Square:
  58. context.painter().fill_rect(device_marker_rect.to_type<int>(), color);
  59. break;
  60. case CSS::ListStyleType::Circle:
  61. aa_painter.draw_ellipse(device_marker_rect.to_type<int>(), color, 1);
  62. break;
  63. case CSS::ListStyleType::Disc:
  64. aa_painter.fill_ellipse(device_marker_rect.to_type<int>(), color);
  65. break;
  66. case CSS::ListStyleType::DisclosureClosed: {
  67. // https://drafts.csswg.org/css-counter-styles-3/#disclosure-closed
  68. // 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.
  69. // 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.
  70. // Draw an equilateral triangle pointing right.
  71. auto path = Gfx::Path();
  72. path.move_to({ left, top });
  73. path.line_to({ left + sin_60_deg * (right - left), (top + bottom) / 2 });
  74. path.line_to({ left, bottom });
  75. path.close();
  76. aa_painter.fill_path(path, color);
  77. break;
  78. }
  79. case CSS::ListStyleType::DisclosureOpen: {
  80. // https://drafts.csswg.org/css-counter-styles-3/#disclosure-open
  81. // 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.
  82. // 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.
  83. // Draw an equilateral triangle pointing down.
  84. auto path = Gfx::Path();
  85. path.move_to({ left, top });
  86. path.line_to({ right, top });
  87. path.line_to({ (left + right) / 2, top + sin_60_deg * (bottom - top) });
  88. path.close();
  89. aa_painter.fill_path(path, color);
  90. break;
  91. }
  92. case CSS::ListStyleType::Decimal:
  93. case CSS::ListStyleType::DecimalLeadingZero:
  94. case CSS::ListStyleType::LowerAlpha:
  95. case CSS::ListStyleType::LowerLatin:
  96. case CSS::ListStyleType::LowerRoman:
  97. case CSS::ListStyleType::UpperAlpha:
  98. case CSS::ListStyleType::UpperLatin:
  99. case CSS::ListStyleType::UpperRoman:
  100. if (layout_box().text().is_null())
  101. break;
  102. // FIXME: This should use proper text layout logic!
  103. // This does not line up with the text in the <li> element which looks very sad :(
  104. context.painter().draw_text(device_enclosing.to_type<int>(), layout_box().text(), layout_box().scaled_font(context), Gfx::TextAlignment::Center, color);
  105. break;
  106. case CSS::ListStyleType::None:
  107. return;
  108. default:
  109. VERIFY_NOT_REACHED();
  110. }
  111. }
  112. }