FontCache.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #include <LibWeb/Forward.h>
  12. namespace Web {
  13. struct FontSelector {
  14. FlyString family;
  15. float point_size { 0 };
  16. int weight { 0 };
  17. int width { 0 };
  18. int slope { 0 };
  19. bool operator==(FontSelector const& other) const
  20. {
  21. return family == other.family && point_size == other.point_size && weight == other.weight && width == other.width && slope == other.slope;
  22. }
  23. };
  24. class FontCache {
  25. public:
  26. FontCache() = default;
  27. RefPtr<Gfx::Font const> get(FontSelector const&) const;
  28. void set(FontSelector const&, NonnullRefPtr<Gfx::Font const>);
  29. NonnullRefPtr<Gfx::Font const> scaled_font(Gfx::Font const&, float scale_factor);
  30. void did_load_font(Badge<CSS::StyleComputer>, FlyString const& family_name);
  31. private:
  32. mutable HashMap<FontSelector, NonnullRefPtr<Gfx::Font const>> m_fonts;
  33. };
  34. }
  35. namespace AK {
  36. template<>
  37. struct Traits<Web::FontSelector> : public GenericTraits<Web::FontSelector> {
  38. static unsigned hash(Web::FontSelector const& key) { return pair_int_hash(pair_int_hash(key.family.hash(), key.weight), key.point_size); }
  39. };
  40. }