FontCache.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. 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 = { FlyString::from_deprecated_fly_string(font.family()).release_value_but_fixme_should_propagate_errors(), 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. }