MarkerPaintable.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. NonnullRefPtr<MarkerPaintable> MarkerPaintable::create(Layout::ListItemMarkerBox const& layout_box)
  12. {
  13. return adopt_ref(*new 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. auto enclosing = enclosing_int_rect(absolute_rect());
  28. if (auto const* list_style_image = layout_box().list_style_image_bitmap()) {
  29. context.painter().blit(enclosing.location(), *list_style_image, list_style_image->rect());
  30. return;
  31. }
  32. auto color = computed_values().color();
  33. int marker_width = (int)enclosing.height() / 2;
  34. Gfx::IntRect marker_rect { 0, 0, marker_width, marker_width };
  35. marker_rect.center_within(enclosing);
  36. Gfx::AntiAliasingPainter aa_painter { context.painter() };
  37. switch (layout_box().list_style_type()) {
  38. case CSS::ListStyleType::Square:
  39. context.painter().fill_rect(marker_rect, color);
  40. break;
  41. case CSS::ListStyleType::Circle:
  42. aa_painter.draw_ellipse(marker_rect, color, 1);
  43. break;
  44. case CSS::ListStyleType::Disc:
  45. aa_painter.fill_ellipse(marker_rect, color);
  46. break;
  47. case CSS::ListStyleType::Decimal:
  48. case CSS::ListStyleType::DecimalLeadingZero:
  49. case CSS::ListStyleType::LowerAlpha:
  50. case CSS::ListStyleType::LowerLatin:
  51. case CSS::ListStyleType::LowerRoman:
  52. case CSS::ListStyleType::UpperAlpha:
  53. case CSS::ListStyleType::UpperLatin:
  54. case CSS::ListStyleType::UpperRoman:
  55. if (layout_box().text().is_null())
  56. break;
  57. context.painter().draw_text(enclosing, layout_box().text(), Gfx::TextAlignment::Center);
  58. break;
  59. case CSS::ListStyleType::None:
  60. return;
  61. default:
  62. VERIFY_NOT_REACHED();
  63. }
  64. }
  65. }