FontCache.h 1004 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. bool operator==(const FontSelector& other) const
  17. {
  18. return family == other.family && size == other.size && weight == other.weight;
  19. }
  20. };
  21. namespace AK {
  22. template<>
  23. struct Traits<FontSelector> : public GenericTraits<FontSelector> {
  24. static unsigned hash(const FontSelector& key) { return pair_int_hash(pair_int_hash(key.family.hash(), key.weight), key.size); }
  25. };
  26. }
  27. class FontCache {
  28. public:
  29. static FontCache& the();
  30. RefPtr<Gfx::Font> get(const FontSelector&) const;
  31. void set(const FontSelector&, NonnullRefPtr<Gfx::Font>);
  32. private:
  33. FontCache() { }
  34. mutable HashMap<FontSelector, NonnullRefPtr<Gfx::Font>> m_fonts;
  35. };