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函数的坐标系原点在左下角;