[韦东山]嵌入式Linux应用开发 lecture6 freetype

freetype安装:先编译安装zlib,再编译安装libpng,最后编译安装freetype;

freetype功能:显示矢量字体 ;

矢量字体:

①确定关键点;

②使用数学曲线连接关键点;

③填充闭合区域内部空间;

显示单个文字步骤:

①确定字符的编码值;

②设置字符大小;

③根据编码值通过charmap找到关键点(glyph);

④把关键点转换成位图点阵;

⑤LCD显示;

int main(int argc, char **argv)
{
	wchar_t *chinese_str = L"繁";   
    //①wchar_t()确定字符的编码值

	FT_Library	  library;
	FT_Face 	  face;
	int error;
    FT_Vector     pen;
	FT_GlyphSlot  slot;
	int font_size = 24;

	error = FT_Init_FreeType( &library );
    //初始化freetype库
	
	error = FT_New_Face( library, argv[1], 0, &face );	
	slot = face->glyph;
    //加载字体文件

	FT_Set_Pixel_Sizes(face, font_size, 0);
	//②设置字体大小

    error = FT_Load_Char( face, chinese_str[0], FT_LOAD_RENDER );
    //③④根据编码值得到位图
	
    draw_bitmap( &slot->bitmap,
                 var.xres/2,
                 var.yres/2);
    //⑤LCD显示

	return 0;	
}

tips:

FT_Get_Char_Index:根据编码值获得glyph_index;

FT_Load_Glyph:根据glyph_index获得glyph;

FT_Render_Glyph:渲染出位图;

显示一行文字步骤:

int display_string(FT_Face     face, wchar_t *wstr, int lcd_x, int lcd_y)
{
    int i;
    int error;
    FT_BBox bbox;
    FT_Vector pen;
    FT_Glyph  glyph;
    FT_GlyphSlot slot = face->glyph;

    /* 把LCD坐标转换为笛卡尔坐标 */
    int x = lcd_x;
    int y = var.yres - lcd_y;

    /* 计算外框 */
    compute_string_bbox(face, wstr, &bbox);

    /* 反推原点 */
    pen.x = (x - bbox.xMin) * 64; /* 单位: 1/64像素 */
    pen.y = (y - bbox.yMax) * 64; /* 单位: 1/64像素 */

    /* 处理每个字符 */
    for (i = 0; i < wcslen(wstr); i++)
    {
        /* 转换:transformation */
        FT_Set_Transform(face, 0, &pen);

        /* 加载位图: load glyph image into the slot (erase previous one) */
        error = FT_Load_Char(face, wstr[i], FT_LOAD_RENDER);
        if (error)
        {
            printf("FT_Load_Char error\n");
            return -1;
        }

        /* 在LCD上绘制: 使用LCD坐标 */
        draw_bitmap( &slot->bitmap,
                        slot->bitmap_left,
                        var.yres - slot->bitmap_top);

        /* 计算下一个字符的原点: increment pen position */
        pen.x += slot->advance.x;
        pen.y += slot->advance.y;
    }

    return 0;
}

tip:LCD显示的坐标系原点在左上角,调用freetype函数的坐标系原点在左下角;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值