ladybird/Libraries/LibGfx/FontCascadeList.h
Andreas Kling 0fece0650e
Some checks are pending
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
LibGfx: Let FontCascadeList quickly reject out-of-range code points
By keeping track of the enclosing range around all Unicode ranges of a
FontCascadeList entry, we can quickly reject any code point that's
outside all ranges.

This knocks font_for_code_point() from 7% to 3% in the profile when
scrolling on https://screenshotone.com/
2025-07-14 19:05:25 +02:00

59 lines
1.6 KiB
C++

/*
* Copyright (c) 2023-2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/UnicodeRange.h>
namespace Gfx {
class FontCascadeList : public RefCounted<FontCascadeList> {
public:
static NonnullRefPtr<FontCascadeList> create()
{
return adopt_ref(*new FontCascadeList());
}
size_t size() const { return m_fonts.size(); }
bool is_empty() const { return m_fonts.is_empty() && !m_last_resort_font; }
Font const& first() const { return !m_fonts.is_empty() ? *m_fonts.first().font : *m_last_resort_font; }
template<typename Callback>
void for_each_font_entry(Callback callback) const
{
for (auto const& font : m_fonts)
callback(font);
}
void add(NonnullRefPtr<Font const> font);
void add(NonnullRefPtr<Font const> font, Vector<UnicodeRange> unicode_ranges);
void extend(FontCascadeList const& other);
Gfx::Font const& font_for_code_point(u32 code_point) const;
bool equals(FontCascadeList const& other) const;
struct Entry {
NonnullRefPtr<Font const> font;
struct RangeData {
// The enclosing range is the union of all Unicode ranges. Used for fast skipping.
UnicodeRange enclosing_range;
Vector<UnicodeRange> unicode_ranges;
};
Optional<RangeData> range_data;
};
void set_last_resort_font(NonnullRefPtr<Font> font) { m_last_resort_font = move(font); }
private:
RefPtr<Font const> m_last_resort_font;
Vector<Entry> m_fonts;
};
}