FontCache.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/DeprecatedFlyString.h>
  7. #include <LibGfx/Font/Font.h>
  8. #include <LibWeb/FontCache.h>
  9. namespace Web {
  10. RefPtr<Gfx::Font const> FontCache::get(FontSelector const& font_selector) const
  11. {
  12. auto cached_font = m_fonts.get(font_selector);
  13. if (cached_font.has_value())
  14. return cached_font.value();
  15. return nullptr;
  16. }
  17. NonnullRefPtr<Gfx::Font const> FontCache::scaled_font(Gfx::Font const& font, float scale_factor)
  18. {
  19. auto device_font_pt_size = font.point_size() * scale_factor;
  20. FontSelector font_selector = { font.family(), device_font_pt_size, font.weight(), font.width(), font.slope() };
  21. if (auto cached_font = get(font_selector)) {
  22. return *cached_font;
  23. }
  24. if (auto font_with_device_pt_size = font.with_size(device_font_pt_size)) {
  25. set(font_selector, *font_with_device_pt_size);
  26. return font_with_device_pt_size.release_nonnull();
  27. }
  28. return font;
  29. }
  30. void FontCache::set(FontSelector const& font_selector, NonnullRefPtr<Gfx::Font const> font)
  31. {
  32. m_fonts.set(font_selector, move(font));
  33. }
  34. void FontCache::did_load_font(Badge<CSS::StyleComputer>, FlyString const& family_name)
  35. {
  36. m_fonts.remove_all_matching([&family_name](auto& key, auto&) -> bool {
  37. return key.family == family_name;
  38. });
  39. }
  40. }