linux提取ttf字体轮廓,如何在linux系统上获得真实类型字符的字形轮廓

解决方案是使用FreeType,它提供了我需要的功能范围:

#include

#include

#include

#include

//******************* check error code ********************

void Check(FT_Error ErrCode, const char* OKMsg, const char* ErrMsg)

{

if(ErrCode != 0)

{

std::cout << ErrMsg << ": " << ErrCode << "\n";

std::cout << "program halted\n";

exit(1);

}

else

std::cout << OKMsg << "\n";

}

//******************** get outline ************************

int GetOutLine(FT_Glyph glyph, FT_OutlineGlyph* Outg)

{

int Err = 0;

switch ( glyph->format )

{

case FT_GLYPH_FORMAT_BITMAP:

Err = 1;

break;

case FT_GLYPH_FORMAT_OUTLINE:

*Outg = (FT_OutlineGlyph)glyph;

break;

default:

;

}

return Err;

}

//*********** print outline to console ***************

int PrintOutLine(FT_OutlineGlyph Outg)

{

int Err = 0;

FT_Outline* Ol = &Outg->outline;

int Start = 0; //start index of contour

int End = 0; //end index of contour

short* pContEnd = Ol->contours; //pointer to contour end

FT_Vector* pPoint = Ol->points; //pointer to outline point

char* pFlag = Ol->tags; //pointer to flag

for(int c = 0; c < Ol->n_contours; c++)

{

std::cout << "\nContour " << c << ":\n";

End = *pContEnd++;

for(int p = Start; p <= End; p++)

{

char Ch = *pFlag++ + '0';

std::cout << "Point " << p <x << " Y="<y << " Flag=" << Ch << "\n";

pPoint++;

}

Start = End + 1;

}

return Err;

}

//*********** get glyph index from command line *************

FT_UInt GetGlyphIndex(int argc, char* argv[], int Nr)

{

if(argc > Nr)

{

return atoi(argv[Nr]);

}

else

{

return 36;

}

}

//*********** get font name from command line *************

void GetFontName(int argc, char* argv[], int Nr, std::string& FontName)

{

if(argc > Nr)

{

FontName += argv[Nr];

}

else

{

FontName += "FreeMono.ttf";

}

}

//*********** get font size from command line *************

int GetFontSize(int argc, char* argv[], int Nr)

{

short FontSize = 50 * 64;

if(argc > Nr)

FontSize += atoi(argv[Nr]);

return FontSize;

}

//******************** M A I N ************************

// par1: FontName, par2:Glyph-Nr, par3: FontSize as FT_F26Dot6

int main(int argc, char* argv[])

{

FT_Face face;

FT_Library library;

FT_Error error;

error = FT_Init_FreeType( &library );

Check(error, "", "error initializing FT lib");

std::string FontName;

FontName = "/usr/share/fonts/truetype/freefont/";

GetFontName(argc, argv, 1, FontName);

error = FT_New_Face( library, FontName.c_str(), 0, &face );

Check(error, "", "error loading font");

FT_F26Dot6 font_size = GetFontSize(argc, argv, 3);

error = FT_Set_Char_Size( face, font_size, font_size, 72, 72 );

Check(error, "", "error setting char size");

FT_UInt glyph_index = GetGlyphIndex(argc, argv, 2);

FT_Int32 load_flags = FT_LOAD_DEFAULT;

error = FT_Load_Glyph( face, glyph_index, load_flags );

Check(error, "", "error loading glyph");

FT_Glyph glyph;

error = FT_Get_Glyph( face->glyph, &glyph );

Check(error, "", "error getting glyph");

FT_OutlineGlyph Outg;

error = GetOutLine(glyph, &Outg);

Check(error,"", "error getting outline");

std::cout << "=======================================================\n";

std::cout << "Font: " << FontName << "\n";

std::cout << "Glyph Index: " << glyph_index << "\n";

std::cout << "=======================================================\n";

error = PrintOutLine(Outg);

Check(error,"", "error printing outline");

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值