c++ - 尝试将 FreeType 与 CImg 一起使用

标签 c++ freetype

我使用一些使用 FreeType2 的代码:

void drawText( FT_Face &face, cimg_library::CImg < unsigned char > &image, const int &heightText, const std::wstring &text, const int &leftTopX, const int &leftTopY, int &width, int &height, unsigned char fontColor[] = NULL, const int separeteGlyphWidth = 1 ) {

    width = 0;
    height = 0;

    FT_Set_Pixel_Sizes( face, 0, heightText );
    FT_GlyphSlot glyphSlot = face->glyph;  

    int shiftX = leftTopX;
    int shiftY = 0;
    for( int numberSymbol = 0; numberSymbol < text.length(); ++numberSymbol ){

        shiftY = leftTopY;

        bool isSpace = false;
        FT_ULong symbol = text[ numberSymbol ]; //FT_ULong symbol = text.at(numberSymbol);
        if (symbol == ' ') {
            symbol = 'a';
            isSpace = true;
        }

        if( FT_Load_Char( face, symbol, FT_LOAD_RENDER ) ) throw "Error, glyph not load!! \n";

        shiftY = heightText - glyphSlot->bitmap.rows;
        if ( shiftY < 0 ) shiftY = 0;

        std::cout << "char: " << (char) text.at( numberSymbol ) << " \tshiftY = " << shiftY << "\t rows = " << glyphSlot->bitmap.rows << "\t extra = " << ( glyphSlot->advance.y >> 6 ) << "\n";

        if ( !isSpace ) drawGlyph( glyphSlot, image, shiftX, shiftY, fontColor );

        shiftX += glyphSlot->bitmap.width + separeteGlyphWidth;

        if( height < shiftY + glyphSlot->bitmap.rows ) height = shiftY + glyphSlot->bitmap.rows;
        width = shiftX;

    }

}


std::string str = "Let me speak from my heart!";
std::wstring wStr( str.begin(), str.end() );

int width, height;
drawText( face, myCImg, 50, wStr, 50, 50, width, height );

但它没有按预期工作。输出图像是这样的: enter image description here

符号有一些奇怪的行为:“y”、“f”、“p”。怎么放低?

看起来我应该用 glyphSlot->metrics.vertAdvance 做点什么,但我不知道如何正确使用它。

最佳答案

快速浏览 online reference如果需要像素坐标,似乎用于 Y 偏移的值应该基于 bitmap_top

所以我建议换行

shiftY = heightText - glyphSlot->bitmap.rows;

shiftY = heightText - glyphSlot->bitmap_top;

这似乎也可以通过查看 basic tutorial 得到证实。它以下列方式使用这些值:

/* now, draw to our target surface */
my_draw_bitmap( &slot->bitmap,
              pen_x + slot->bitmap_left,
              pen_y - slot->bitmap_top );

/* increment pen position */
pen_x += slot->advance.x >> 6;

关于c++ - 尝试将 FreeType 与 CImg 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43346068/

相关文章:

c++ - 与类定义相同类型的静态 constexpr 成员(其他详细信息)

x11 - 如何使用 XRender 扩展对字形进行子像素渲染

ubuntu - ubuntu 16.10 中的 Monospace 10px 大小无效

c++ - 免费类型。如何知道 FT_Face 及其索引中的符号数?

android - 使用 Freetype2 为 ARM 构建 FFmpeg

c++ - 将 QObject 连接指定为唯一的正确方法是什么?

c++ - 如何构建 Notepad++

python - 由于 freetype,在 OS X 10.11 上将 matplotlib 升级到 1.5 失败

c++ - 更改一个对象的引用并且它会更改集合中的所有对象

c++ - QFTP commandFinished 发送得太早