35 lines
1.1 KiB
C++
35 lines
1.1 KiB
C++
#include "Display/UI/Text/GlyphCache.h"
|
|
#include <stdexcept>
|
|
#include <iostream>
|
|
#include "Display/UI/Text/FontFace.h"
|
|
#include <ft2build.h>
|
|
#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<FT_Face>(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
|