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

posted on   lydstory  阅读(1027)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-06-01 electron 桌面开发

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示