渲染freetype位图字形的问题
Issue Rendering FreeType Bitmap Glyphs
我正在尝试将字形加载到带有freetype的位图中,然后将其加载到每个字形的d3d9纹理中,但由于某种原因,纹理尚未正确设置。
我想将每个字形加载到自己的纹理中,以便我可以分别绘制每个字符。
我如何为字体创建字形纹理:
new_font_t::new_font_t( const char* szFontFilename, int iHeight, std::size_t _sRangeBegin, std::size_t _sRangeEnd ): sRangeBegin( _sRangeBegin ), sRangeEnd( _sRangeEnd )
{
assert( !FT_New_Face( _RenderContext.ftLibrary, ( std::string( R"(C:WindowsFonts)" ) + szFontFilename ).c_str( ), 0, &fFont ) );
FT_Set_Char_Size( fFont, 0, iHeight * 64, 96, 96 );
pGlyphs = new IDirect3DTexture9*[ sRangeEnd - sRangeBegin ];
for( auto u = sRangeBegin; u <= sRangeEnd; u++ )
{
const auto iIndex = FT_Get_Char_Index( fFont, u );
D3DLOCKED_RECT recGlyph;
FT_Load_Glyph( fFont, iIndex, FT_LOAD_DEFAULT );
FT_Render_Glyph( fFont->glyph, FT_RENDER_MODE_NORMAL );
const auto uWidth = fFont->glyph->bitmap.width;
D3DXCreateTexture( _RenderContext.pDevice, uWidth, iHeight, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pGlyphs[ u ] );
pGlyphs[ u ]->LockRect( 0, &recGlyph, nullptr, 0 );
for ( auto i = 0; i < int( uWidth ); i++ )
{
for ( auto j = 0; j < iHeight; j++ )
if( fFont->glyph->bitmap.buffer[ i + j * uWidth ] > 0 )
reinterpret_cast< DWORD* >( recGlyph.pBits )[ i + j * uWidth ] = 0xFFFFFFFF;
}
pGlyphs[ u ]->UnlockRect( 0 );
}
}
这是我呈现文本的方式:
void new_font_t::RenderText( const char* szText )
{
if( _RenderContext.pSprite->Begin( D3DXSPRITE_ALPHABLEND ) == D3D_OK )
{
auto loc = D3DXVECTOR3( 50, 100, 0 );
for ( auto u = 0u; u < strlen( szText ); u++ )
_RenderContext.pSprite->Draw( pGlyphs[ szText[ u ] ], nullptr, nullptr, &loc, 0xFFFFFFFF );
_RenderContext.pSprite->End( );
}
}
我这样创建一个字体结构:
test_font = new new_font_t( "arial.ttf", 32, 0, 0xFF );
然后我调用渲染函数:
test_font->RenderText( "!" );
如您所见,它应该呈现一个感叹号,但相反,这是:https://i.stack.imgur.com/6jiuj.png
尝试将您的字符转换为WCHAR_T,这是为我修复的。
这是一块代码,将彩色文本呈现为32位图像缓冲区,并具有alpha透明度:
void DrawTxt(char *txt, int x, int y, int w, int h, uint32_t col, uint8_t *buf) {
int origx = x;
wchar_t wstr[512];
int len = MultiByteToWideChar(CP_ACP, MB_USEGLYPHCHARS|MB_PRECOMPOSED, txt, -1, wstr, 512);
for(int n=0; n<len; n++) {
wchar_t wc = wstr[n];
if(wc==' ') { continue; }
if(wc=='r') { x = origx; continue; }
if(wc=='n') { y += _ph; x = origx; continue; } // line spacing
if(wc==' ' && x>w) { y += _ph; x = origx; } // word break
FT_Load_Char(_face, wc, FT_LOAD_RENDER);
FT_GlyphSlot g = _face->glyph;
x += g->bitmap_left; // horizontal adjust
uint8_t *src = g->bitmap.buffer;
uint8_t *end = buf + w*h*4 - 1;
uint32_t *dst = (uint32_t*)buf+x + y*w;
dst += w * (_ph - g->bitmap_top); // adjust to baseline
for(int gy=0; gy<g->bitmap.rows; gy++) {
for(int gx=0; gx<g->bitmap.width; gx++) {
if(x+gx>=w) break; // out right edge in pixmap
uint8_t *c = (uint8_t *)&col;
uint8_t *d = (uint8_t *)&dst[gx];
if(d<buf || d>end) continue; // avoid under/overflow
uint32_t alpha = *src; // premultiply alpha
d[0] |= unsigned char((alpha * c[2]) / 255);
d[1] |= unsigned char((alpha * c[1]) / 255);
d[2] |= unsigned char((alpha * c[0]) / 255);
d[3] |= unsigned char(alpha);
src++;
}
dst += w;
}
x -= g->bitmap_left;
x += g->advance.x >> 6;
}
}
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 渲染freetype位图字形的问题