2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2023-02-20 18:56:08 +01:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2023-03-15 22:53:38 +01:00
|
|
|
#include <AK/DeprecatedFlyString.h>
|
2022-04-09 09:28:38 +02:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/FontCache.h>
|
2019-10-18 23:02:10 +02:00
|
|
|
|
|
|
|
|
FontCache& FontCache::the()
|
|
|
|
|
{
|
|
|
|
|
static FontCache cache;
|
|
|
|
|
return cache;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 18:56:08 +01:00
|
|
|
RefPtr<Gfx::Font const> FontCache::get(FontSelector const& font_selector) const
|
2019-10-18 23:02:10 +02:00
|
|
|
{
|
|
|
|
|
auto cached_font = m_fonts.get(font_selector);
|
|
|
|
|
if (cached_font.has_value())
|
|
|
|
|
return cached_font.value();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 22:53:38 +01:00
|
|
|
NonnullRefPtr<Gfx::Font const> FontCache::scaled_font(Gfx::Font const& font, float scale_factor)
|
|
|
|
|
{
|
|
|
|
|
auto device_font_pt_size = font.point_size() * scale_factor;
|
|
|
|
|
FontSelector font_selector = { FlyString::from_deprecated_fly_string(font.family()).release_value_but_fixme_should_propagate_errors(), device_font_pt_size, font.weight(), font.width(), font.slope() };
|
|
|
|
|
if (auto cached_font = FontCache::the().get(font_selector)) {
|
|
|
|
|
return *cached_font;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (auto font_with_device_pt_size = font.with_size(device_font_pt_size)) {
|
|
|
|
|
set(font_selector, *font_with_device_pt_size);
|
|
|
|
|
return font_with_device_pt_size.release_nonnull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return font;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 18:56:08 +01:00
|
|
|
void FontCache::set(FontSelector const& font_selector, NonnullRefPtr<Gfx::Font const> font)
|
2019-10-18 23:02:10 +02:00
|
|
|
{
|
|
|
|
|
m_fonts.set(font_selector, move(font));
|
|
|
|
|
}
|