2017-04-11 134 views
0

我与使用对FreeType2一些代码工作:尝试使用的FreeType与CIMG

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但我不知道如何正确使用它。

+1

只是看了一下文档,我只是尝试使用'bitmap_top'就像'shiftY = height文字 - glyphSlot-> bitmap_top;' – PeterT

+0

@PeterT,哈,谢谢!真的行! :)你可以添加这个信息作为答案 - 我会接受它。 – JavaRunner

回答

1

快速查看online reference如果想要使用像素坐标,则Y偏移的值应该基于bitmap_top

因此,我建议改变路线

shiftY = heightText - glyphSlot->bitmap.rows; 

shiftY = heightText - glyphSlot->bitmap_top; 

这似乎也通过查看其以下列方式使用这些值的basic tutorial confimed:

/* 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; 
+0

再次感谢!这真的帮助我! – JavaRunner