我使用freetype渲染了utf8表示形式B字符串(例如"\xef\xbb\x9d\xef\xba\x8e\xef\xaf\xbe\xef\xba\xad\x20"
),并且为了进行对齐计算,我需要获取渲染字符串的字形宽度。
这是我的代码,似乎宽度获得的值无效。那又是什么问题?如何在不使用Harfbuzz或任何其他更高级别的库的情况下如何计算渲染字符串的精确宽度参数?
size_t len = utf8strlen( text);
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
FT_Error error;
int row = 0;
int wdt = 0;
int pch = 0;
error = FT_Init_FreeType( &library ); /* initialize library */
error = FT_New_Face( library, "font.ttf", 0, &face );/* create face object */
error = FT_Set_Pixel_Sizes( face, 52, 24 );
for ( size_t cntr = 0; cntr < len; cntr++ )
{
/* load glyph image into the slot (erase previous one) */
error = FT_Load_Char( face, text[cntr], FT_LOAD_RENDER );
if ( error )
continue; /* ignore errors */
// draw glyph image anti-aliased
FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL );
// get dimensions of bitmap
row += face->glyph->bitmap.rows;
wdt += face->glyph->bitmap.width;
pch += face->glyph->bitmap.pitch;
}