当前位置: 移动技术网 > 移动技术>移动开发>IOS > 运行可用:使用FreeType输出中文汉字点阵图形的源码

运行可用:使用FreeType输出中文汉字点阵图形的源码

2020年07月13日  | 移动技术网移动技术  | 我要评论

  整合了几篇文章,并进行了修改测试。成功之后,放出来供大家参考。

  • 字体下载

https://download.csdn.net/download/quantum7/12600359

  不知道哪个字体能显示汉字,那就下载几个。吾初始都是设置为0,系统自动增加。

  • 代码

  中文能显示,英文当然更能了。

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

#include <ft2build.h>
#include <freetype/freetype.h>
#include FT_FREETYPE_H

#define FONT_FILE "/home/quantum6/code/test-fonts/simsun.ttc"

int main()
{
    FT_Library  m_pFTLib;
    FT_Face     m_pFTFace;
    int i=0;
    int j=0;
    wchar_t chinese_char = L'泰';

    FT_Error result = FT_Init_FreeType(&m_pFTLib);
    if(FT_New_Face(m_pFTLib, FONT_FILE, 0, &m_pFTFace))
     {
        printf("FT_New_Face error!\n");
        return;
     }

    //FT_ENCODING_GB2312, FT_ENCODING_UNICODE
    FT_Select_Charmap(m_pFTFace, FT_ENCODING_UNICODE);
    FT_Set_Char_Size(m_pFTFace, 0, 12<<6, 200, 200);

    result = FT_Load_Glyph(m_pFTFace, FT_Get_Char_Index(m_pFTFace, chinese_char), FT_LOAD_DEFAULT);

     // 第二个参数为渲染模式
    result = FT_Render_Glyph(m_pFTFace->glyph,  FT_RENDER_MODE_NORMAL);  
    printf("result=%d\n", result);

    FT_Bitmap bmp = m_pFTFace->glyph->bitmap;
    int h = bmp.rows;
    int w = bmp.width;

    for (i=0; i<h; i++)
    {
        for (j=0; j<w; j++)
        {
            printf(((bmp.buffer[i * w  + j]) == 0 ) ? " " : "1");
        }
        printf("\n");
    }

}
  • 编译脚本
rm test

gcc ft.c \
    -o test \
    -I/usr/include/freetype2 \
    -L/usr/lib64 -lfreetype

./test
  • 输出结果

本文地址:https://blog.csdn.net/quantum7/article/details/107279477

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网