利用freetype显示unicode字符

本文介绍如何使用Freetype库加载TTF字体文件,并通过Unicode加载特定字符的点阵图像。文中详细展示了初始化Freetype库、设置字体大小、获取字符索引及加载并显示字符的过程。

使用freetype加载矢量字库,ttf字体文件,通过字符的unicode,load一个字符的点阵!汉字的unicode使用32bit既可表达,比如‘’我‘’的unicode是0x6211


定义全局变量

static FT_Library g_lib;
static FT_Face g_face;
初始化freetype

<pre name="code" class="cpp">pShowScreen<span style="font-family: Arial, Helvetica, sans-serif;">是frambuffer的映射内存地址。</span>
int Show_Char(int Unicode,int FontSizePixel,MID_FONT_COLOR fontColor,int StartX,int StartY){
         int charUnicode = Unicode;
	int index,x,y;
	FT_Glyph glyph;
	unsigned short int *pShowLine;
	int error;
	index =0;x=0;y =0;

	if(pShowScreen == NULL) {printf("<Use OSD_Init not yet!!!> \n");return -1;}

    	pShowLine = (unsigned short int*) (pShowScreen );
	
	error =  FT_Set_Pixel_Sizes(g_face,FontSizePixel,FontSizePixel);
	if(error != 0) {
		return HI_FAILURE;
	}
	index =  FT_Get_Char_Index(g_face,charUnicode);

        error = FT_Load_Glyph(g_face,index, FT_LOAD_DEFAULT);
   	
	error = FT_Get_Glyph(g_face->glyph, &glyph);

	if(!error){
        
        error = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1);
		
		
        FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph) glyph;
        FT_Bitmap *bitmap = &bitmap_glyph -> bitmap;
		
		
        for (y = StartY; y < bitmap->rows + StartY; ++y) {
            for (x = StartX; x < bitmap->width + StartX; ++x) {

		if(x < 0 || y < 0) continue;

	       if(bitmap->buffer[(y-StartY)* bitmap->width + (x-StartX)]){
                      *(pShowLine + y * var.xres + x) = fontColor;
		}else{
                  
		}
            }
        }
        FT_Done_Glyph(glyph);
        glyph = NULL;	
	}else return  -1;
	return 0;
}

MSG_PRINT("init error:%d,errno:%d \n",error,errno);}error = FT_New_Face(g_lib,fontFile, 0, &g_face);if(error != 0) { MSG_PRINT("init FreeType error, please check font file \n "); return -1;}error = FT_Select_Charmap(g_face, ft_encoding_unicode);if(error != 0) {return -1;}

load unicode



释放freetype

int FreeType_Realse(){
	FT_Done_Face(g_face);
	FT_Done_FreeType(g_lib);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

john_liqinghan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值