#include "FontManager.h"
#include "jes/Runtime.h"
#include <iostream>

namespace jes
{
    bool FontManager::load()
    {
        if (FT_Init_FreeType(&m_ft) != 0)
        {
            std::cerr << "Error initializing FreeType" << std::endl;
            return false;
        }

        /* Load failsafe font */
        m_mono_font = load_font("FreeMono", 20);

        if (m_mono_font == NULL)
        {
            std::cerr << "Error loading FreeMono font" << std::endl;
            return false;
        }

        return true;
    }

    FontRef FontManager::load_font(const char * fname, size_t size)
    {
        PathRef font_path = Runtime::locate(Runtime::FONT, fname);
        if (font_path == NULL)
            return NULL;

        FontRef font = new Font();
        if (!font->load(m_ft, font_path->to_s().c_str(), size))
            return NULL;

        return font;
    }
}
