代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。