ttf 字库点阵提取
http://blog.chinaunix.net/uid-30091091-id-5749246.html
#include <ft2build.h> #include FT_FREETYPE_H int main(int argc, char **argv) { FT_Library library; FT_Face face; int error; int i, j, k, counter; unsigned char temp; int char_index; int font_size; if (argc != 4) { printf("Usage: test filename font_size index\n"); return 0; } char_index = atoi(argv[3]); font_size = atoi(argv[2]); error = FT_Init_FreeType(&library); if (error) { printf("can not init free type library!\n"); return 0; } error = FT_New_Face(library, argv[1], 0, &face); if (error) { printf("create new face falied!\n"); return 0; } error = FT_Set_Pixel_Sizes(face, 0, font_size); if (error) { printf("set font size error!\n"); return 0; } //printf("file family name %s\n", face->family_name); //printf("file style name %s\n", face->style_name); //printf("number of char %d\n", face->num_glyphs); //printf("number of fixed bitmap %d\n", face->num_fixed_sizes); //printf("Char size %d\n", face->size); error = FT_Load_Glyph(face, char_index, FT_LOAD_DEFAULT); if (error) { printf("Load char error!\n"); return 0; } if (face->glyph->format != FT_GLYPH_FORMAT_BITMAP) { error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO); if (error) { printf("render char failed!\n"); return 0; } } // printf("rows %d, width %d\n", face->glyph->bitmap.rows, face->glyph->bitmap.width); //printf("pitch %d\n", face->glyph->bitmap.pitch); // printf("bit_map_left %d bit_map_top %d\n", face->glyph->bitmap_left, // face->glyph->bitmap_top); // printf("width %d height %d\n", face->glyph->metrics.width, face->glyph->metrics.height); // printf("%d %d %d\n", face->glyph->metrics.horiBearingX, face->glyph->metrics.horiBearingY, // face->glyph->metrics.horiAdvance); for (j = 0; j < (font_size * 26) / 32 - face->glyph->bitmap_top; j++) { for (i = 0; i < font_size; i++) { printf("_"); } printf("\n"); } for (; j < face->glyph->bitmap.rows + (font_size * 26) / 32 - face->glyph->bitmap_top; j++) { for (i = 1; i <= face->glyph->bitmap_left; i++) { printf("_"); } for (k = 0; k < face->glyph->bitmap.pitch; k++) { temp = face->glyph->bitmap.buffer[face->glyph->bitmap.pitch*(j + face->glyph->bitmap_top - (font_size * 26) / 32) + k]; for (counter = 0; counter < 8; counter++) { if (temp & 0x80) { printf("*"); } else { printf("_"); } temp <<= 1; i++; if (i > font_size) { break; } } } for (; i <= font_size; i++) { // printf("|"); } printf("\n"); } for (; j < font_size; j++) { for (i = 0; i < font_size; i++) { printf("_"); } printf("\n"); } return 0; }
gcc -o 2 -g 2.c -I /usr/local/include/freetype2/ -lfreetype -lm
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-06-01 electron 桌面开发