Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29930578
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: LINUX

2011-01-12 14:59:18



使用freetype2从ttf字库中提取任意大点阵字体
最近玩minigui,由于minigui的gpl版本提供的字库只有12x12和16x16两种字号,做出来的效果很是难看,于是想添加一些其他字号的字库。从网上苦苦搜索,也下载到一些其他童鞋提供的字库,但是不知道是我的使用有问题还是什么,它不能显示正确的汉字,实在米有办法了,就自己动手来做字库吧!

minigui使用的是点阵字库,而我们常用的是ttf等矢量字库,因此打算从ttf字库当中提取点阵字体出来,做成minigui能使用的点阵字库。下面奉上我的源代码,这个代码在终端中打印字符来模拟点阵:

#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 (= 0; j < (font_size * 26) / 32 - face->glyph->bitmap_top; j++)
    {
        for (= 0; i < font_size; i++)
        {
            printf("_");
        }
        printf("\n");
    }

    for (; j < face->glyph->bitmap.rows + (font_size * 26) / 32 - face->glyph->bitmap_top; j++)
    {
        for (= 1; i <= face->glyph->bitmap_left; i++)
        {
            printf("_");
        }

        for (= 0; k < face->glyph->bitmap.pitch; k++)
        {
            temp = face->glyph->bitmap.buffer[face->glyph->bitmap.pitch*(+ 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 (> font_size)
                {
                    break;
                }
            }
        }

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

        }
        printf("\n");
    }

    for (; j < font_size; j++)
    {
        for (= 0; i < font_size; i++)
        {
            printf("_");
        }
        printf("\n");
    }

    return 0;
}

代码写得很难看,大家将就看一下,呵呵!

编译完成后在linux的命令行运行该程序,该程序需要三个参数:第一个参数是要使用的ttf字库的路径,第二个参数为要显示的字号,第三个参数为要显示的字在字库当中的index。

下面是运行效果:


这里是显示一个字,一个字能显示出来了,你别告诉我不知道怎么把整个字库提取出来哈!

这种方式可以提取任意ttf字库的字出来,且可以生成任意大小的点阵字体。经过实验发现,生成的字号越大,字体越好看。

目前我已经做了24号、32号、40号、48号点阵字库,在minigui上测试效果良好!需要的童鞋可以联系我!
阅读(11767) | 评论(4) | 转发(4) |
给主人留下些什么吧!~~

waming21022016-07-14 09:48:54

你好,我用的是STM32F407的单片机,如何实现矢量字的显示呢,FT_New_Face函数该如何使用,ttf字库存在STM32外挂的SD卡中,FT_New_Face函数的参数文件路径该如何写呢

strong460669992013-09-27 16:44:37

博主,626488630@qq.com,谢谢。

chinaunix网友2011-03-08 13:12:43

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com

chinaunix网友2011-03-08 13:12:26

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com