首页 > 其他 > 详细

ttf 字库点阵提取

时间:2020-06-01 21:21:37      阅读:106      评论:0      收藏:0      [点我收藏+]

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;
}

 

ttf 字库点阵提取

原文:https://www.cnblogs.com/hshy/p/13027292.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!