Ai
1 Star 5 Fork 1

小康6650/StudyProject

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SimpleGlyph.cpp 3.30 KB
一键复制 编辑 原始数据 按行查看 历史
小康6650 提交于 2019-06-13 13:54 +08:00 . 1
/* example1.c */
/* */
/* This small program shows how to print a rotated string with the */
/* FreeType 2 library. */
#include <stdio.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#define WIDTH 120
#define HEIGHT 90
/* origin is the upper left corner */
unsigned char image[HEIGHT][WIDTH];
/* Replace this function with something useful. */
void
draw_bitmap(FT_Bitmap* bitmap,
FT_Int x,
FT_Int y)
{
FT_Int i, j, p, q;
FT_Int x_max = x + bitmap->width;
FT_Int y_max = y + bitmap->rows;
for (i = x, p = 0; i < x_max; i++, p++)
{
for (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];
}
}
}
void
show_image(void)
{
int i, j;
for (i = 0; i < HEIGHT; i++)
{
for (j = 0; j < WIDTH; j++)
{
putchar(image[i][j] == 0 ? ' '
: image[i][j] < 128 ? '+'
: '*');
}
putchar('\n');
}
}
int
main(int argc,
char** argv)
{
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
FT_Matrix matrix; /* transformation matrix */
FT_Vector pen; /* untransformed origin */
FT_Error error;
const char* filename;
const wchar_t* text;
double angle;
int target_height;
int n, num_chars;
//if (argc != 3)
//{
// fprintf(stderr, "usage: %s font sample-text\n", argv[0]);
// exit(1);
//}
filename = "../../font/myfont.TTF"; /* first argument */
text = L"Ba中测"; /* second argument */
num_chars = wcslen(text);
angle = -0.1; /* use 25 degrees */
target_height = HEIGHT;
error = FT_Init_FreeType(&library); /* initialize library */
/* error handling omitted */
error = FT_New_Face(library, filename, 0, &face);/* create face object */
/* error handling omitted */
/* use 60pt at 60dpi */
error = FT_Set_Char_Size(face, 60 * 64, 0, 60, 0); /* set character size */
slot = face->glyph;
/* set up matrix */
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); //控制台每行之间有间距,纵向压缩抵消间距,防止字体太高
/* the pen position in 26.6 cartesian space coordinates; */
/* start at (300,200) relative to the upper left corner */
pen.x = 4 * 64;
pen.y = (5 ) * 64;
FT_UInt glyph_index = -1;
/* retrieve glyph index from character code */
glyph_index = FT_Get_Char_Index(face, text[1]);
for (n = 0; n < num_chars; n++)
{
/* set transformation */
FT_Set_Transform(face, &matrix, &pen);
/* load glyph image into the slot (erase previous one) */
error = FT_Load_Char(face, text[n], FT_LOAD_RENDER);
if (error)
continue; /* ignore errors */
/* now, draw to our target surface (convert position) */
draw_bitmap(&slot->bitmap,
slot->bitmap_left,
target_height - slot->bitmap_top);
/* increment pen position */
pen.x += slot->advance.x;
pen.y += slot->advance.y;
}
show_image();
FT_Done_Face(face);
FT_Done_FreeType(library);
return 0;
}
/* EOF */
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Kyle12/StudyProject.git
git@gitee.com:Kyle12/StudyProject.git
Kyle12
StudyProject
StudyProject
master

搜索帮助