#include "Display/UI/Text/GlyphCache.h" #include #include #include "Display/UI/Text/FontFace.h" #include #include FT_FREETYPE_H namespace Display::UI::Text { GlyphCache::GlyphCache(const FontFace &fontFace) : fontFace(fontFace) {} const Glyph &GlyphCache::getGlyph(char c) { FT_Face face = static_cast(fontFace.getFace()); auto it = cache.find(c); if (it != cache.end()) return it->second; if (!face) throw std::runtime_error("FontFace not initialized!"); if (FT_Load_Char(face, c, FT_LOAD_RENDER)) throw std::runtime_error("FT_Load_Char failed"); FT_GlyphSlot g = face->glyph; Glyph glyph; glyph.width = g->bitmap.width; glyph.height = g->bitmap.rows; glyph.bearingX = g->bitmap_left; glyph.bearingY = g->bitmap_top; glyph.advance = g->advance.x >> 6; glyph.buffer.assign(g->bitmap.buffer, g->bitmap.buffer + g->bitmap.width * g->bitmap.rows); return cache.emplace(c, std::move(glyph)).first->second; } } // namespace Display::UI::Text