10 минут гугления дали примерно следующий пример. В результате имеем массив пикселов каждой буквы - далее пакуйте в нужный формат и все:
Код | #include <stdio.h> #include <freetype/freetype.h> #include <freetype/ftglyph.h>
inline int next_p2(const int a) { int rval = 1; while (rval < a) { rval <<= 1; }
return rval; }
int main(int, char*[]) { const char* const pString = "Some text"; const char* const pFontFileName = "/usr/share/fonts/TTF/DejaVuSans.ttf"; const int h = 14;
FT_Library freeTypeLibrary;
do { // Init the library if (0 != FT_Init_FreeType(&freeTypeLibrary)) { printf("Unable to initialize FreeType library\n"); break; }
do { // Create a font FT_Face font; if (0 != FT_New_Face(freeTypeLibrary, pFontFileName, 0, &font)) { printf("Unable to load font\n"); break; }
FT_Set_Char_Size(font, h << 6, h << 6, 96, 96);
for (const char* c = pString; 0 != c and '\0' != *c; ++c) { if (0 != FT_Load_Glyph(font, FT_Get_Char_Index(font, *c), FT_LOAD_DEFAULT )) { printf("Unable to load glyph\n"); break; }
FT_Glyph glyph; if (0 != FT_Get_Glyph(font->glyph, &glyph)) { printf("Unable to get glyph\n"); break; }
// Convert the glyph to raster FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1);
const FT_BitmapGlyph bitmapGlyph = (FT_BitmapGlyph)glyph; const FT_Bitmap& bitmap = bitmapGlyph->bitmap;
const int width = next_p2(bitmap.width); const int height = next_p2(bitmap.rows);
const unsigned int buffSize = 2 * width * height; unsigned char* pBuffer = new unsigned char[buffSize]; memset(pBuffer, 0, buffSize);
for (int j = 0; j <height; ++j) { for(int i = 0; i < width; ++i) { pBuffer[2 * (i + j * width)] = pBuffer[2 * (i + j * width) + 1] = (i >= bitmap.width || j >= bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width * j]; } }
// TODO: Pack the image to somewhere
// Release the buffer delete [] pBuffer;
// Destroy glyph FT_Done_Glyph(glyph); }
// Destroy font FT_Done_Face(font); } while (0);
// Release the library if (0 != FT_Done_FreeType(freeTypeLibrary)) { printf("Unable to release FreeType library\n"); break; } } while(0);
return 0; }
|
|