使用FreeType库从ttf字库中获取点阵字库

运行环境为:Ubuntu 18.04.5 LTS \n \l

自己先下载好FreeType库,我这边使用的版本为:freetype-2.4.10

下面为源码编译就可以使用了;

 ttf字库使用的是windows10里面的,名称为:SIMSUN.TTC

保存的点阵字库可以使用DotFont16X16打开查看;

#include <ft2build.h>
#include <arpa/inet.h>
#include FT_FREETYPE_H

#define FONT_SIZE 16

#define printf(fmt,...) (void)0

int print_font(char* buf, int point_size)
{
    unsigned char mask = 0x80;
    int i = 0;
    while(i < point_size)
    {
        int index = 0;

        mask = 0x80;
        for(index = 0; index < 8; index++)
        {
            if(mask & buf[i])
            {
                printf("O ");
            }
            else
            {
                printf("  ");
            }
            mask >>= 1;
        }
        if((i + 1) % (FONT_SIZE/8) == 0)
        {
            printf("\n");
        }
        i++;
    }
}

static int g_index = 0;
int set_bit_value(int value, char font_buf[32], int font_size)
{
    g_index %= (font_size*font_size);
    int byte_index = g_index / 8;
    int bit_index  = g_index % 8;
    bit_index = 7 - bit_index;
    font_buf[byte_index] &= ~(1 << bit_index);
    font_buf[byte_index] |= (value << bit_index);
    g_index++;
}


static FT_Library library;
static FT_Face face;
void print_face_info(FT_Face face)
{
    int n ;
    FT_CharMap map;
    char* p;
    for(n = 0; n < face->num_charmaps; ++n)
    {
        map = face->charmaps[n];
        int encode = htonl(map->encoding);
        p = (char*)&encode;
        printf("%d  %d %d\n", map->platform_id, map->encoding_id, map->encoding);
        printf("encode : %c %c %c %c\n", p[0], p[1], p[2], p[3]);
    }
}
int freeType_init(const char* ttf_path, int font_size)
{

    int error;
    error = FT_Init_FreeType(&library);
    if (error)
    {
        printf("can not init free type library!\n");
        return 0;
    }

    error = FT_New_Face(library, ttf_path, 0, &face);
    if (error)
    {
        printf("create new face falied!\n");
        return 0;
    }
    
    print_face_info(face);

    printf("faces num : %ld , glyphs num : %ld\n", face->num_faces, face->num_glyphs);

    error = FT_Set_Pixel_Sizes(face, 0, font_size);
    if (error)
    {
        printf("set font size error!\n");
        return 0;
    }

#if 1
    error = FT_Select_Charmap(face, ft_encoding_unicode);
    if (error)
    {
        printf("select charmap error!\n");
        return 0;
    }
#endif
    return 0;
}

int freeType_Uninit()
{

    int error;
    error = FT_Done_FreeType(library);
    if (error)
    {
        printf("can not Uninit free type library!\n");
        return 0;
    }
    return 0;
}

int get_font_by_index(const char* ttf_path, int font_size, int char_index, char font_buf[32])
{
    int error;
    int i, j, k, counter;
    unsigned char temp;
    FT_UInt  glyph_index;

#if 0
    error = FT_Init_FreeType(&library);
    if (error)
    {
        printf("can not init free type library!\n");
        return 0;
    }

    error = FT_New_Face(library, ttf_path, 0, &face);
    if (error)
    {
        printf("create new face falied!\n");
        return 0;
    }
#endif

    //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);
    
    /**********************************************************/
    /************************做索引转换**********************************/
    /**********************************************************/
#if 1
    
    printf("char_index: 0x%x\n", char_index);

    glyph_index = FT_Get_Char_Index( face, char_index  );

    printf("glyph_index : %d\n", glyph_index);

    if(!glyph_index) return 0;
    /**********************************************************/


    error = FT_Load_Glyph(face, glyph_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;
        }
    }
#else
    error = FT_Load_Char( face, char_index, FT_LOAD_RENDER);
    if (error)
    {
        printf("render char failed!\n");
        return 0;
    }
#endif

    // 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);

    FT_Bitmap* bitmap = &face->glyph->bitmap;
    printf("r:%d, w:%d, p: %d, s: %d\n",
     bitmap->rows,
     bitmap->width,
     bitmap->pitch,
     bitmap->num_grays);

    for (j = 0; j < (font_size * 26) / 32 - face->glyph->bitmap_top; j++)
    {
        for (i = 0; i < font_size; i++)
        {
            printf("1");
            set_bit_value(0, font_buf, font_size);
        }
        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("2");
            set_bit_value(0, font_buf, font_size);
        }

        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("*");
                    set_bit_value(1, font_buf, font_size);
                }
                else
                {
                    printf("_");
                    set_bit_value(0, font_buf, font_size);
                }
                temp <<= 1;
                i++;
                if (i > font_size)
                {
                    break;
                }
            }
        }

        for (; i <= font_size; i++)
        {
            // printf("|");

                set_bit_value(0, font_buf, font_size);

        }
        printf("\n");
    }

    for (; j < font_size; j++)
    {
        for (i = 0; i < font_size; i++)
        {
            printf("3");
            set_bit_value(0, font_buf, font_size);
        }
        printf("\n");
    }

    return 1;
}


int main(int argc, char* argv[])
{
    char font_buf[FONT_SIZE * FONT_SIZE / 8] = {'\0'};
    char* unicode_font_name = "unicode_16x16.bin";
    FILE* file = fopen(unicode_font_name, "w+");

    freeType_init(argv[1], FONT_SIZE);

    int i;

    for(i = 0; i < 0xffff; ++i)
    {
        
        if(get_font_by_index(argv[1], FONT_SIZE, i, font_buf))
        {
            
            print_font(font_buf, sizeof(font_buf));

            fwrite(font_buf, 1, sizeof(font_buf), file);
        }
        else
        {
            memset(font_buf, 0, sizeof(font_buf));
            fwrite(font_buf, 1, sizeof(font_buf), file);
        }

        g_index = 0;

    }

    freeType_Uninit();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值