简体   繁体   中英

Issue Rendering FreeType Bitmap Glyphs

I'm trying to load glyphs into bitmaps with freetype and then load that into a d3d9 texture for each glyph, but for some reason the texture isn't being set properly.

I want to load each glyph into its own texture, so that I can draw each character separately.

How I'm creating the glyph textures for a font:

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:\Windows\Fonts\)" ) + 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 );
    }
}

This is how I'm rendering text:

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( );
    }
}

I create a font struct as so:

test_font = new new_font_t( "arial.ttf", 32, 0, 0xFF );

Then I call the render function:

test_font->RenderText( "!" );

As you can see, it should render an exclamation mark, but instead, this is rendered: https://i.imgur.com/Chqnltt.png

Try convert your characters into wchar_t, that fixed it for me.

Here's a piece of code that renders colored text into a 32 bit image buffer, with alpha transparency:

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=='\0') { 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM