ListItemMarkerBox.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 <LibWeb/Layout/ListItemMarkerBox.h>
  8. #include <LibWeb/Painting/MarkerPaintable.h>
  9. namespace Web::Layout {
  10. ListItemMarkerBox::ListItemMarkerBox(DOM::Document& document, CSS::ListStyleType style_type, size_t index, NonnullRefPtr<CSS::StyleProperties> style)
  11. : Box(document, nullptr, move(style))
  12. , m_list_style_type(style_type)
  13. , m_index(index)
  14. {
  15. switch (m_list_style_type) {
  16. case CSS::ListStyleType::Square:
  17. case CSS::ListStyleType::Circle:
  18. case CSS::ListStyleType::Disc:
  19. break;
  20. case CSS::ListStyleType::Decimal:
  21. m_text = String::formatted("{}.", m_index);
  22. break;
  23. case CSS::ListStyleType::DecimalLeadingZero:
  24. // This is weird, but in accordance to spec.
  25. m_text = m_index < 10 ? String::formatted("0{}.", m_index) : String::formatted("{}.", m_index);
  26. break;
  27. case CSS::ListStyleType::LowerAlpha:
  28. case CSS::ListStyleType::LowerLatin:
  29. m_text = String::bijective_base_from(m_index - 1).to_lowercase();
  30. break;
  31. case CSS::ListStyleType::UpperAlpha:
  32. case CSS::ListStyleType::UpperLatin:
  33. m_text = String::bijective_base_from(m_index - 1);
  34. break;
  35. case CSS::ListStyleType::LowerRoman:
  36. m_text = String::roman_number_from(m_index).to_lowercase();
  37. break;
  38. case CSS::ListStyleType::UpperRoman:
  39. m_text = String::roman_number_from(m_index);
  40. break;
  41. case CSS::ListStyleType::None:
  42. break;
  43. default:
  44. VERIFY_NOT_REACHED();
  45. }
  46. }
  47. ListItemMarkerBox::~ListItemMarkerBox() = default;
  48. Gfx::Bitmap const* ListItemMarkerBox::list_style_image_bitmap() const
  49. {
  50. return list_style_image() ? list_style_image()->bitmap() : nullptr;
  51. }
  52. RefPtr<Painting::Paintable> ListItemMarkerBox::create_paintable() const
  53. {
  54. return Painting::MarkerPaintable::create(*this);
  55. }
  56. }