FontCache.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/HashMap.h>
  9. #include <LibGfx/Font/Font.h>
  10. #include <LibGfx/Forward.h>
  11. struct FontSelector {
  12. FlyString family;
  13. float point_size { 0 };
  14. int weight { 0 };
  15. int width { 0 };
  16. int slope { 0 };
  17. bool operator==(FontSelector const& other) const
  18. {
  19. return family == other.family && point_size == other.point_size && weight == other.weight && width == other.width && slope == other.slope;
  20. }
  21. };
  22. namespace AK {
  23. template<>
  24. struct Traits<FontSelector> : public GenericTraits<FontSelector> {
  25. static unsigned hash(FontSelector const& key) { return pair_int_hash(pair_int_hash(key.family.hash(), key.weight), key.point_size); }
  26. };
  27. }
  28. class FontCache {
  29. public:
  30. static FontCache& the();
  31. RefPtr<Gfx::Font const> get(FontSelector const&) const;
  32. void set(FontSelector const&, NonnullRefPtr<Gfx::Font const>);
  33. NonnullRefPtr<Gfx::Font const> scaled_font(Gfx::Font const&, float scale_factor);
  34. private:
  35. FontCache() = default;
  36. mutable HashMap<FontSelector, NonnullRefPtr<Gfx::Font const>> m_fonts;
  37. };