ladybird/Libraries/LibWeb/Layout/ListItemMarkerBox.cpp
Jelle Raaijmakers 115e5f42af LibWeb: Improve graphical list item marker positioning
While 788d5368a7 took care of better text
marker positioning, this improves graphical marker positioning instead.

By looking at how Firefox and Chrome render markers, it's clear that
there are three parts to positioning a graphical marker:

  * The containing space that the marker resides in;
  * The marker dimensions;
  * The distance between the marker and the start of the list item.

The space that the marker can be contained in, is the area to the left
of the list item with a height of the marker's line-height. The marker
dimensions are relative to the marker's font's pixel size: most of them
are a square at 35% of the font size, but the disclosure markers are
sized at 50% instead. Finally, the marker distance is always gauged at
50% of the font size.

So for example, a list item with `list-style-type: disc` and `font-size:
20px`, has 10px between its start and the right side of the marker, and
the marker's dimensions are 7x7.

The percentages I've chosen closely resemble how Firefox lays out its
list item markers.
2025-07-17 09:35:09 +01:00

99 lines
3.6 KiB
C++

/*
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Layout/ListItemMarkerBox.h>
#include <LibWeb/Painting/MarkerPaintable.h>
namespace Web::Layout {
GC_DEFINE_ALLOCATOR(ListItemMarkerBox);
ListItemMarkerBox::ListItemMarkerBox(DOM::Document& document, CSS::ListStyleType style_type, CSS::ListStylePosition style_position, GC::Ref<DOM::Element> list_item_element, GC::Ref<CSS::ComputedProperties> style)
: Box(document, nullptr, move(style))
, m_list_style_type(style_type)
, m_list_style_position(style_position)
, m_list_item_element(list_item_element)
{
}
ListItemMarkerBox::~ListItemMarkerBox() = default;
Optional<String> ListItemMarkerBox::text() const
{
auto index = m_list_item_element->ordinal_value();
return m_list_style_type.visit(
[index](CSS::CounterStyleNameKeyword keyword) -> Optional<String> {
String text;
switch (keyword) {
case CSS::CounterStyleNameKeyword::Square:
case CSS::CounterStyleNameKeyword::Circle:
case CSS::CounterStyleNameKeyword::Disc:
case CSS::CounterStyleNameKeyword::DisclosureClosed:
case CSS::CounterStyleNameKeyword::DisclosureOpen:
case CSS::CounterStyleNameKeyword::None:
return {};
case CSS::CounterStyleNameKeyword::Decimal:
text = String::number(index);
break;
case CSS::CounterStyleNameKeyword::DecimalLeadingZero:
// This is weird, but in accordance to spec.
text = index < 10 ? MUST(String::formatted("0{}", index)) : String::number(index);
break;
case CSS::CounterStyleNameKeyword::LowerAlpha:
case CSS::CounterStyleNameKeyword::LowerLatin:
text = String::bijective_base_from(index - 1, String::Case::Lower);
break;
case CSS::CounterStyleNameKeyword::UpperAlpha:
case CSS::CounterStyleNameKeyword::UpperLatin:
text = String::bijective_base_from(index - 1, String::Case::Upper);
break;
case CSS::CounterStyleNameKeyword::LowerRoman:
text = String::roman_number_from(index, String::Case::Lower);
break;
case CSS::CounterStyleNameKeyword::UpperRoman:
text = String::roman_number_from(index, String::Case::Upper);
break;
default:
VERIFY_NOT_REACHED();
}
return MUST(String::formatted("{}. ", text));
},
[](String const& string) -> Optional<String> {
return string;
});
}
GC::Ptr<Painting::Paintable> ListItemMarkerBox::create_paintable() const
{
return Painting::MarkerPaintable::create(*this);
}
CSSPixels ListItemMarkerBox::relative_size() const
{
auto font_size = first_available_font().pixel_size();
auto marker_text = text();
if (marker_text.has_value())
return CSSPixels::nearest_value_for(font_size);
// Scale the marker box relative to the used font's pixel size.
switch (m_list_style_type.get<CSS::CounterStyleNameKeyword>()) {
case CSS::CounterStyleNameKeyword::DisclosureClosed:
case CSS::CounterStyleNameKeyword::DisclosureOpen:
return CSSPixels::nearest_value_for(ceilf(font_size * .5f));
default:
return CSSPixels::nearest_value_for(ceilf(font_size * .35f));
}
}
void ListItemMarkerBox::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_list_item_element);
}
}