#include "FontsManager.h"

#ifdef HAS_FREETYPE
#include "FreeTypeFontEngine.h"
#else
#include "BasicFontEngine.h"
#endif

#include "FontGlyph.h"


FontsManager::FontsManager()
{
#ifdef HAS_FREETYPE
    mFontEngine = std::make_unique<FreeTypeFontEngine>();
#else
    mFontEngine = std::make_unique<BasicFontEngine>();
#endif
    mFontEngine->initialize();
}

std::unique_ptr<FontsManager> FontsManager::Create()
{
    return std::make_unique<FontsManager>();
}

IFontEngine* FontsManager::getFontEngine() const
{
    return mFontEngine.get();
}

FontGlyph* FontsManager::getGlyph(const std::string& fontFace, int size, uint32_t c)
{
    auto path = std::filesystem::path("truetype/liberation/LiberationSans-Regular.ttf");

    mFontEngine->loadFontFace(path, size);

    auto iter = mGlyphs.find(c);
    if(iter != mGlyphs.end())
    {
        return iter->second.get();
    }
    else
    {
        auto glyph = mFontEngine->loadGlyph(c);
        auto glyph_ptr = glyph.get();
        mGlyphs[c] = std::move(glyph);
        return glyph_ptr;
    }
}


