一段使用 FreeType 在控制台打印字符的 C++ 代码
2013-12-28 23:48
681 查看
#include <ft2build.h> #include FT_FREETYPE_H #include FT_GLYPH_H #include FT_BITMAP_H #include <iostream> int main() { FT_Library library; FT_Face face; FT_Init_FreeType(&library); FT_New_Face(library, "XXX.ttf", 0, &face); FT_Select_Charmap(face, FT_ENCODING_UNICODE); FT_Set_Char_Size(face, 0, 12 * 64, 96, 96); FT_ULong charcode = 0x7F8E; FT_UInt index = FT_Get_Char_Index(face, charcode); FT_Load_Glyph(face, index, FT_LOAD_DEFAULT); FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO); FT_Bitmap bitmap; FT_Bitmap_New(&bitmap); FT_Bitmap_Convert(library, &face->glyph->bitmap, &bitmap, 1); for (int y = 0; y < bitmap.rows; y++) { for (int x = 0; x < bitmap.width; x++) { unsigned char c = bitmap.buffer[y * bitmap.width + x]; if (c) { std::cout << '@'; } else { std::cout << ' '; } } std::cout << '\n'; } FT_Bitmap_Done(library, &bitmap); FT_Done_Face(face); FT_Done_FreeType(library); return 0; }
FT_New_Face 函数用来读取某个字体文件
FT_ULong charcode 字符的 UTF-32 的编码,如 0x7F8E 是 U+7F8E 就是美丽的"美"字
FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO); 渲染图片,FT_RENDER_MODE_MONO 是渲染只有黑白两色的位图。
FT_Bitmap_Convert 由于渲染模式的不同,分析起来比较麻烦,使用这个函数可以转换成统一的位深为 8bpp 的位图。
相关文章推荐
- 【c++】类中带默认参数的函数
- “学C语言计划”后续
- 【VC++】007字符输入
- memset函数使用详解
- c++ acm 语句用法备注(保留两位小数&字符转数字)
- C++ 文件操作中迭代器的使用(权哥)
- 【VC++】006路径
- C++中namespace的用法
- c++迭代器总结
- C++基础 - 内置类型和类类类型的初始化
- C++ 容器及选用总结
- 使用C++获取文件路径,已获取当前路径为基础
- C++编译遇到参数错误(cannot convert parameter * from 'const char [**]' to 'LPCWSTR')
- c++ STL之 find和count(权哥)
- VC++中_T与L的区别
- c++ STL之 copy(权哥)
- C++ Primer学习-第15章 面向对象编程
- c语言面试题 指针30个常错题型
- C++中各种类型的成员变量的初始化方法(转)
- c++ STL之 for_each(权哥)