FontCache.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (c) 2018-2020, 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 <AK/String.h>
  10. #include <LibGfx/Font.h>
  11. #include <LibGfx/Forward.h>
  12. struct FontSelector {
  13. FlyString family;
  14. int size { 0 };
  15. int weight { 0 };
  16. int slope { 0 };
  17. bool operator==(const FontSelector& other) const
  18. {
  19. return family == other.family && size == other.size && weight == other.weight && slope == other.slope;
  20. }
  21. };
  22. namespace AK {
  23. template<>
  24. struct Traits<FontSelector> : public GenericTraits<FontSelector> {
  25. static unsigned hash(const FontSelector& key) { return pair_int_hash(pair_int_hash(key.family.hash(), key.weight), key.size); }
  26. };
  27. }
  28. class FontCache {
  29. public:
  30. static FontCache& the();
  31. RefPtr<Gfx::Font> get(const FontSelector&) const;
  32. void set(const FontSelector&, NonnullRefPtr<Gfx::Font>);
  33. private:
  34. FontCache() = default;
  35. mutable HashMap<FontSelector, NonnullRefPtr<Gfx::Font>> m_fonts;
  36. };