85 lines
1.7 KiB
C
85 lines
1.7 KiB
C
#include <ft2build.h>
|
|
#include FT_FREETYPE_H
|
|
#include "scode.h"
|
|
#include "config.h"
|
|
#include "log.h"
|
|
#include "fontengine.h"
|
|
|
|
static FT_LIbrary library;
|
|
static FT_Face stdfont;
|
|
|
|
static void fonteng_cleanup(void)
|
|
{
|
|
FT_Done_Face(stdfont);
|
|
FT_Done_FreeType(library);
|
|
}
|
|
|
|
HRESULT FontEng_setup(void)
|
|
{
|
|
HRESULT hr = S_OK;
|
|
FT_Error err;
|
|
|
|
err = FT_Init_FreeType(&library);
|
|
if (err != 0)
|
|
{
|
|
Log(LFATAL, "Unable to initialize Freetype (%d)", err);
|
|
return E_FAIL;
|
|
}
|
|
|
|
err = FT_New_Face(library, "/usr/local/share/fonts/opentype/Inconsolata.otf", 0, &stdfont);
|
|
if (err != 0)
|
|
{
|
|
Log(LFATAL, "Unable to load font (%d)", err);
|
|
hr = E_FAIL;
|
|
goto error_0;
|
|
}
|
|
|
|
err = FT_Set_Pixel_Sizes(stdfont, 0, 16);
|
|
if (err != 0)
|
|
{
|
|
Log(LFATAL, "Unable to set font size (%d)", err);
|
|
hr = E_FAIL;
|
|
goto error_1;
|
|
}
|
|
|
|
hr = Config_exitfunc(fonteng_cleanup);
|
|
if (FAILED(hr))
|
|
fonteng_cleanup();
|
|
return hr;
|
|
|
|
error_1:
|
|
FT_Done_Face(stdfont);
|
|
error_0:
|
|
FT_Done_FreeType(library);
|
|
return hr;
|
|
}
|
|
|
|
HRESULT FontEng_do_text_out(INT32 x, INT32 y, PCSTR pstr, TEXTOUTFUNC renderfunc)
|
|
{
|
|
HRESULT hr = S_OK;
|
|
FT_GlyphSlot slot = stdfont->glyph;
|
|
FT_UInt glyph_index;
|
|
FT_Error err;
|
|
|
|
while (*pstr)
|
|
{
|
|
glyph_index = FT_Get_Char_Index(stdfont, *pstr++);
|
|
err = FT_Load_Glyph(stdfont, glyph_index, FT_LOAD_DEFAULT);
|
|
if (err != 0)
|
|
{
|
|
hr = E_FAIL;
|
|
break;
|
|
}
|
|
err = FT_Render_Glyph(stdfont->glyph, FT_RENDER_MODE_NORMAL);
|
|
if (err != 0)
|
|
{
|
|
hr = E_FAIL;
|
|
break;
|
|
}
|
|
(*renderfunc)(x, y, slot->bitmap.width, slot->bitmap.rows, slot->bitmap.buffer);
|
|
x += slot->advance.x >> 6;
|
|
y += slot->advance.y >> 6;
|
|
}
|
|
return hr;
|
|
}
|