ladybird/Userland/Libraries/LibGfx/FontCascadeList.cpp
Aliaksandr Kalenik 2cb0039a13 LibGfx+LibWeb: Produce font cascade list in CSS font matching algorithm
According to the CSS font matching algorithm specification, it is
supposed to be executed for each glyph instead of each text run, as is
currently done. This change partially implements this by having the
font matching algorithm produce a list of fonts against which each
glyph will be tested to find its suitable font.

Now, it becomes possible to have per-glyph fallback fonts: if the
needed glyph is not present in a font, we can check the subsequent
fonts in the list.
2023-12-10 17:32:04 +01:00

55 lines
1.3 KiB
C++

/*
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/FontCascadeList.h>
namespace Gfx {
void FontCascadeList::add(NonnullRefPtr<Font> font)
{
m_fonts.append({ move(font), {} });
}
void FontCascadeList::add(NonnullRefPtr<Font> font, Vector<UnicodeRange> unicode_ranges)
{
m_fonts.append({ move(font), move(unicode_ranges) });
}
void FontCascadeList::extend(FontCascadeList const& other)
{
for (auto const& font : other.m_fonts) {
m_fonts.append({ font.font->clone(), font.unicode_ranges });
}
}
Gfx::Font const& FontCascadeList::font_for_code_point(u32 code_point) const
{
for (auto const& entry : m_fonts) {
if (!entry.unicode_ranges.has_value())
return entry.font;
if (!entry.font->contains_glyph(code_point))
continue;
for (auto const& range : *entry.unicode_ranges) {
if (range.contains(code_point))
return entry.font;
}
}
VERIFY_NOT_REACHED();
}
bool FontCascadeList::equals(FontCascadeList const& other) const
{
if (m_fonts.size() != other.m_fonts.size())
return false;
for (size_t i = 0; i < m_fonts.size(); ++i) {
if (m_fonts[i].font != other.m_fonts[i].font)
return false;
}
return true;
}
}