Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在linux系统上获得真实类型字符的字形轮廓大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在寻找一个库来获取 linux系统上真实字体的字形轮廓.我们正在使用 Pango和 Cairo,但不幸的是我没有找到任何东西. 我正在寻找类似于.NET下的 GlyphTypeface.GetGlyphOutline的东西 任何帮助或提示表示赞赏! 提前致谢 解决方案是使用FreeType,它提供了我需要的功能范围: #include <String> #include <iostream>
我正在寻找一个库来获取 linux系统上真实字体的字形轮廓.我们正在使用 PangoCairo,但不幸的是我没有找到任何东西.
我正在寻找类似于.NET下的 GlyphTypeface.GetGlyphOutline的东西

任何帮助或提示表示赞赏!@H_801_22@

提前致谢@H_801_22@

解决方法

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

#include <String>
#include <iostream>
#include  <freetype/ftglyph.h>
#include  <freetype/freetype.h>

//******************* 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=" << pPoint->x << " Y="<<pPoint->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,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,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(),&face );   
  check(error,"error loading font");

  FT_F26Dot6 font_size = GetFontSize(argc,3);
  error = FT_Set_Char_Size( face,font_size,72,72 );
  check(error,"error setTing char size");

  FT_UInt   glyph_index = GetGlyphIndex(argc,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;  
}

大佬总结

以上是大佬教程为你收集整理的如何在linux系统上获得真实类型字符的字形轮廓全部内容,希望文章能够帮你解决如何在linux系统上获得真实类型字符的字形轮廓所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。