Ai
1 Star 5 Fork 1

小康6650/StudyProject

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TestFreeType.cpp 2.77 KB
一键复制 编辑 原始数据 按行查看 历史
小康6650 提交于 2019-06-13 14:10 +08:00 . 1
#include <iostream>
#include <iomanip>
using namespace std;
#include <ft2build.h>
#include FT_FREETYPE_H
#define WIDTH 120
#define HEIGHT 120
unsigned char image[HEIGHT][WIDTH]{ 0 };
int x = 0, y = 0, drawHeight= 0;
void draw_bitmap(FT_Bitmap* bitmap, FT_Int width, FT_Int height, bool isEndLine = false)
{
if (x + width > WIDTH)
{
x = 0;
y += height;
}
FT_Int x_max = x + bitmap->width;
FT_Int y_max = y + bitmap->rows;
for (int i = x, p = 0; i < x_max; i++, p++)
{
for (int j = y, q = 0; j < y_max; j++, q++)
{
if (i < 0 || j < 0 ||
i >= WIDTH || j >= HEIGHT)
continue;
image[j][i] |= bitmap->buffer[q * bitmap->width + p];
}
}
x += width;
drawHeight = y + height;
if (isEndLine)
{
x = 0;
y += height;
}
}
void show_image(void)
{
for (int i = 0; i < drawHeight; i++)
{
for (int j = 0; j < WIDTH; j++)
{
putchar(image[i][j] == 0 ? ' '
: image[i][j] < 128 ? '+'
: '*');
}
putchar('\n');
}
}
int main()
{
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
FT_Matrix matrix;
FT_Error error;
error = FT_Init_FreeType(&library);
error = FT_New_Face(library, "../../font/FangSong.ttf", 0, &face);
error = FT_Set_Char_Size(face, 60 * 64, 0, 60, 0);
slot = face->glyph;
int height = (int)((face->size->metrics.height >> 6) * 0.61 -5);
matrix.xx = (FT_Fixed)(1 * 0x10000L);
matrix.xy = (FT_Fixed)(0 * 0x10000L);
matrix.yx = (FT_Fixed)(0 * 0x10000L);
matrix.yy = (FT_Fixed)(0.61 * 0x10000L);//控制台每行之间有间距,纵向压缩抵消间距,防止字体太高
FT_Set_Transform(face, &matrix, NULL);
error = FT_Load_Char(face, L'A', FT_LOAD_RENDER);
if (!error)
{
draw_bitmap(&slot->bitmap, slot->advance.x>>6, height);
}
matrix.xx = (FT_Fixed)(1 * 0x10000L);
matrix.xy = (FT_Fixed)(0.4 * 0x10000L); //斜体
matrix.yx = (FT_Fixed)(0 * 0x10000L);
matrix.yy = (FT_Fixed)(0.61 * 0x10000L);
FT_Set_Transform(face, &matrix, NULL);
error = FT_Load_Char(face, L'A', FT_LOAD_RENDER);
if (!error)
{
draw_bitmap(&slot->bitmap, slot->advance.x>>6, height, true);
}
matrix.xx = (FT_Fixed)(1 * 0x10000L);
matrix.xy = (FT_Fixed)(0 * 0x10000L);
matrix.yx = (FT_Fixed)(0 * 0x10000L);
matrix.yy = (FT_Fixed)(0.61 * 0x10000L);
FT_Set_Transform(face, &matrix, NULL);
error = FT_Load_Char(face, L'测', FT_LOAD_RENDER);
if (!error)
{
draw_bitmap(&slot->bitmap, slot->advance.x >> 6, height, true);
}
//if (FT_Select_Charmap(face, FT_ENCODING_PRC))
//{
// return 3;
//}
//char ch[] = "测";
//FT_ULong charCode = 0;
//char *cp = (char*)&charCode;
//cp[1] = ch[0];
//cp[0] = ch[1];
//error = FT_Load_Char(face, charCode, FT_LOAD_RENDER);
//if (!error)
//{
// draw_bitmap(&slot->bitmap, slot->advance.x >> 6, height);
//}
show_image();
FT_Done_Face(face);
FT_Done_FreeType(library);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Kyle12/StudyProject.git
git@gitee.com:Kyle12/StudyProject.git
Kyle12
StudyProject
StudyProject
master

搜索帮助