ListItemMarkerBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringBuilder.h>
  8. #include <LibGfx/Painter.h>
  9. #include <LibWeb/Layout/ListItemMarkerBox.h>
  10. #include <LibWeb/Painting/Paintable.h>
  11. namespace Web::Layout {
  12. ListItemMarkerBox::ListItemMarkerBox(DOM::Document& document, CSS::ListStyleType style_type, size_t index, NonnullRefPtr<CSS::StyleProperties> style)
  13. : Box(document, nullptr, move(style))
  14. , m_list_style_type(style_type)
  15. , m_index(index)
  16. {
  17. switch (m_list_style_type) {
  18. case CSS::ListStyleType::Square:
  19. case CSS::ListStyleType::Circle:
  20. case CSS::ListStyleType::Disc:
  21. break;
  22. case CSS::ListStyleType::Decimal:
  23. m_text = String::formatted("{}.", m_index);
  24. break;
  25. case CSS::ListStyleType::DecimalLeadingZero:
  26. // This is weird, but in accordance to spec.
  27. m_text = m_index < 10 ? String::formatted("0{}.", m_index) : String::formatted("{}.", m_index);
  28. break;
  29. case CSS::ListStyleType::LowerAlpha:
  30. case CSS::ListStyleType::LowerLatin:
  31. m_text = String::bijective_base_from(m_index - 1).to_lowercase();
  32. break;
  33. case CSS::ListStyleType::UpperAlpha:
  34. case CSS::ListStyleType::UpperLatin:
  35. m_text = String::bijective_base_from(m_index - 1);
  36. break;
  37. case CSS::ListStyleType::LowerRoman:
  38. m_text = String::roman_number_from(m_index).to_lowercase();
  39. break;
  40. case CSS::ListStyleType::UpperRoman:
  41. m_text = String::roman_number_from(m_index);
  42. break;
  43. case CSS::ListStyleType::None:
  44. break;
  45. default:
  46. VERIFY_NOT_REACHED();
  47. }
  48. }
  49. ListItemMarkerBox::~ListItemMarkerBox()
  50. {
  51. }
  52. void ListItemMarkerBox::paint(PaintContext& context, Painting::PaintPhase phase)
  53. {
  54. if (phase != Painting::PaintPhase::Foreground)
  55. return;
  56. auto enclosing = enclosing_int_rect(m_paint_box->absolute_rect());
  57. if (auto const* list_style_image = list_style_image_bitmap()) {
  58. context.painter().blit(enclosing.location(), *list_style_image, list_style_image->rect());
  59. return;
  60. }
  61. auto color = computed_values().color();
  62. int marker_width = (int)enclosing.height() / 2;
  63. Gfx::IntRect marker_rect { 0, 0, marker_width, marker_width };
  64. marker_rect.center_within(enclosing);
  65. switch (m_list_style_type) {
  66. case CSS::ListStyleType::Square:
  67. context.painter().fill_rect(marker_rect, color);
  68. break;
  69. case CSS::ListStyleType::Circle:
  70. // For some reason for draw_ellipse() the ellipse is outside of the rect while for fill_ellipse() the ellipse is inside.
  71. // Scale the marker_rect with sqrt(2) to get an ellipse arc (circle) that appears as if it was inside of the marker_rect.
  72. marker_rect.set_height(marker_rect.height() / 1.41);
  73. marker_rect.set_width(marker_rect.width() / 1.41);
  74. marker_rect.center_within(enclosing);
  75. context.painter().draw_ellipse_intersecting(marker_rect, color);
  76. break;
  77. case CSS::ListStyleType::Disc:
  78. context.painter().fill_ellipse(marker_rect, color);
  79. break;
  80. case CSS::ListStyleType::Decimal:
  81. case CSS::ListStyleType::DecimalLeadingZero:
  82. case CSS::ListStyleType::LowerAlpha:
  83. case CSS::ListStyleType::LowerLatin:
  84. case CSS::ListStyleType::LowerRoman:
  85. case CSS::ListStyleType::UpperAlpha:
  86. case CSS::ListStyleType::UpperLatin:
  87. case CSS::ListStyleType::UpperRoman:
  88. if (m_text.is_null())
  89. break;
  90. context.painter().draw_text(enclosing, m_text, Gfx::TextAlignment::Center);
  91. break;
  92. case CSS::ListStyleType::None:
  93. return;
  94. default:
  95. VERIFY_NOT_REACHED();
  96. }
  97. }
  98. Gfx::Bitmap const* ListItemMarkerBox::list_style_image_bitmap() const
  99. {
  100. return list_style_image() ? list_style_image()->bitmap() : nullptr;
  101. }
  102. }