mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-05-03 21:12:08 +02:00
This commit is a three-parter that is hard to separate without breaking
marker rendering:
1. Any marker style that results in a string, except for a literal
string (e.g. `list-style-type: "@"`), should get the string ". "
appended. We forgot to do this for the alpha and roman types.
2. Instead of using the "pixel size rounded up" from a font and adding
an arbitrary 1 to that, we now use the exact pixel size for as long
as possible to improve our vertical positioning of markers.
3. Instead of always adding a "default marker width" to the marker
content width, we now only do this if we did not have text metrics
available (i.e. the marker style is not a text type). This greatly
improves horizontal positioning of text markers.
83 lines
3.0 KiB
C++
83 lines
3.0 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);
|
|
}
|
|
|
|
void ListItemMarkerBox::visit_edges(Cell::Visitor& visitor)
|
|
{
|
|
Base::visit_edges(visitor);
|
|
visitor.visit(m_list_item_element);
|
|
}
|
|
|
|
}
|