FontCache.cpp 1.3 KB

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