GlyphAtlas.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/Noncopyable.h>
  9. #include <LibAccelGfx/GL.h>
  10. #include <LibGfx/Font/Font.h>
  11. namespace AccelGfx {
  12. class GlyphAtlas {
  13. AK_MAKE_NONCOPYABLE(GlyphAtlas);
  14. public:
  15. GlyphAtlas()
  16. : m_texture(GL::create_texture())
  17. {
  18. }
  19. ~GlyphAtlas()
  20. {
  21. GL::delete_texture(m_texture);
  22. }
  23. static GlyphAtlas& the();
  24. struct GlyphsTextureKey {
  25. Gfx::Font const* font;
  26. u32 code_point;
  27. bool operator==(GlyphsTextureKey const& other) const
  28. {
  29. return font == other.font && code_point == other.code_point;
  30. }
  31. };
  32. void update(HashMap<Gfx::Font const*, HashTable<u32>> const& unique_glyphs);
  33. Optional<Gfx::IntRect> get_glyph_rect(Gfx::Font const*, u32 code_point) const;
  34. GL::Texture const& texture() const { return m_texture; }
  35. private:
  36. GL::Texture m_texture;
  37. HashMap<GlyphsTextureKey, Gfx::IntRect> m_glyphs_texture_map;
  38. };
  39. }
  40. namespace AK {
  41. template<>
  42. struct Traits<AccelGfx::GlyphAtlas::GlyphsTextureKey> : public DefaultTraits<AccelGfx::GlyphAtlas::GlyphsTextureKey> {
  43. static unsigned hash(AccelGfx::GlyphAtlas::GlyphsTextureKey const& key)
  44. {
  45. return pair_int_hash(ptr_hash(key.font), key.code_point);
  46. }
  47. };
  48. }