[FreeType]获取字符点阵的最小示例代码

C/C++第三方开源库的介绍和相关讨论
回复
头像
C艹艹
崭露头角
崭露头角
帖子: 26
注册时间: 2016年09月23日 11:32
联系:

[FreeType]获取字符点阵的最小示例代码

帖子 C艹艹 »

#include <stdio.h>
#include <ft2build.h>
#include FT_FREETYPE_H

int main( int argc, char** argv )
{
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;

char* FontPath;
FontPath = "C:/windows/fonts/consola.ttf";

FT_Init_FreeType( &library ); /* 定义一个FT库实例 */

FT_New_Face( library, FontPath, 0, &face ); /* 从给定的路径加载字体文件并创建FT字形对象 */

slot = face->glyph; /* 获取字形槽对象 */

FT_Set_Pixel_Sizes( face, 50, 25 ); /* 像素宽度,高度 */

FT_Load_Char( face, 'B', FT_LOAD_RENDER ); /* 装载字符 */

printf("rows: %d, cols: %d\n", slot->bitmap.rows, slot->bitmap.width );

for (int y=0; y < slot->bitmap.rows; y++)
{
for (int x=0; x < slot->bitmap.width; x++)
{
printf("%d", !!slot->bitmap.buffer[ y*slot->bitmap.width + x ] );
}
printf("\n");
}

FT_Done_Face ( face );
FT_Done_FreeType( library );

return 0;
}
编译指令
gcc -static -std=c11 char_a.c -o char_a ^ -ID:/lib/freetype-2.7/include ^ -LD:/lib/freetype-2.7/objs/.libs ^ -lfreetype -lpng -lz
路径请根据实际情况修改,其中 -lpng -lz 不是必须的(也是根据实际情况)。

运行输出:
rows: 16, cols: 22 1111111111111111110000 1111111111111111111100 1111110000001111111110 1111110000000001111110 1111110000000001111110 1111110000000011111110 1111111111111111111100 1111111111111111110000 1111111111111111111110 1111110000000111111111 1111110000000000111111 1111110000000000011111 1111110000000000111111 1111110000000111111111 1111111111111111111100 1111111111111111110000
省略了相关的错误处理、判断,仅供参考,不建议在实际中使用这种方式。

请注意获取的点阵尺寸和这里设置的并不相同
FT_Set_Pixel_Sizes( face, 50, 25 ); /* 像素宽度,高度 */
官方的说法是:
You should not rely on the resulting glyphs matching, or being constrained, to this pixel size. Refer to FT_Request_Size to understand how requested sizes relate to actual sizes.

Don't use this function if you are using the FreeType cache API.


环境配置可以参考 http://www.code-by.org/viewtopic.php?f=55&t=134
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 2 访客