ListItemMarkerBox.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/MarkerPaintable.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. Gfx::Bitmap const* ListItemMarkerBox::list_style_image_bitmap() const
  53. {
  54. return list_style_image() ? list_style_image()->bitmap() : nullptr;
  55. }
  56. RefPtr<Painting::Paintable> ListItemMarkerBox::create_paintable() const
  57. {
  58. return Painting::MarkerPaintable::create(*this);
  59. }
  60. }