MarkerPaintable.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <LibWeb/Layout/ListItemMarkerBox.h>
  9. #include <LibWeb/Painting/MarkerPaintable.h>
  10. namespace Web::Painting {
  11. JS::NonnullGCPtr<MarkerPaintable> MarkerPaintable::create(Layout::ListItemMarkerBox const& layout_box)
  12. {
  13. return layout_box.heap().allocate_without_realm<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. void MarkerPaintable::paint(PaintContext& context, PaintPhase phase) const
  24. {
  25. if (phase != PaintPhase::Foreground)
  26. return;
  27. // FIXME: All this does is round to the nearest whole CSS pixel, but it's goofy.
  28. CSSPixelRect enclosing = absolute_rect().to_type<float>().to_rounded<float>().to_type<CSSPixels>();
  29. auto device_enclosing = context.enclosing_device_rect(enclosing);
  30. CSSPixels marker_width = enclosing.height() / 2.0f;
  31. if (auto const* list_style_image = layout_box().list_style_image()) {
  32. CSSPixelRect image_rect {
  33. 0, 0,
  34. list_style_image->natural_width().value_or(marker_width.value()),
  35. list_style_image->natural_height().value_or(marker_width.value())
  36. };
  37. image_rect.center_within(enclosing);
  38. auto device_image_rect = context.enclosing_device_rect(image_rect);
  39. list_style_image->resolve_for_size(layout_box(), image_rect.size());
  40. list_style_image->paint(context, device_image_rect, computed_values().image_rendering());
  41. return;
  42. }
  43. CSSPixelRect marker_rect { 0, 0, marker_width, marker_width };
  44. marker_rect.center_within(enclosing);
  45. auto device_marker_rect = context.enclosing_device_rect(marker_rect);
  46. auto color = computed_values().color();
  47. Gfx::AntiAliasingPainter aa_painter { context.painter() };
  48. switch (layout_box().list_style_type()) {
  49. case CSS::ListStyleType::Square:
  50. context.painter().fill_rect(device_marker_rect.to_type<int>(), color);
  51. break;
  52. case CSS::ListStyleType::Circle:
  53. aa_painter.draw_ellipse(device_marker_rect.to_type<int>(), color, 1);
  54. break;
  55. case CSS::ListStyleType::Disc:
  56. aa_painter.fill_ellipse(device_marker_rect.to_type<int>(), color);
  57. break;
  58. case CSS::ListStyleType::Decimal:
  59. case CSS::ListStyleType::DecimalLeadingZero:
  60. case CSS::ListStyleType::LowerAlpha:
  61. case CSS::ListStyleType::LowerLatin:
  62. case CSS::ListStyleType::LowerRoman:
  63. case CSS::ListStyleType::UpperAlpha:
  64. case CSS::ListStyleType::UpperLatin:
  65. case CSS::ListStyleType::UpperRoman:
  66. if (layout_box().text().is_null())
  67. break;
  68. context.painter().draw_text(device_enclosing.to_type<int>(), layout_box().text(), Gfx::TextAlignment::Center);
  69. break;
  70. case CSS::ListStyleType::None:
  71. return;
  72. default:
  73. VERIFY_NOT_REACHED();
  74. }
  75. }
  76. }