freetype_1_在PC上使用freetype显示英文、数字
1、下载freetype,远程登陆服务器,上传freetype,解压
2、配置:./configure
3、安装:sudo make install(安装在/usr/local/lib/)
4、在解压路径下找到freetype-2.9.1\docs\tutorial\里面的例程,这里以example1.c为例
5、在windows(C:\Windows\Fonts)下找到字体文件,上传至linux服务器(为了方便,放到例程所在目录下)
6、编译例程,gcc -o build example1.c -I/usr/include/freetype2/ -lm -lfreetype
6、运行 ./build 字体文件名 字符串
代码分析:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ft2build.h>
#include FT_FREETYPE_H
FT_FREETYPE_H是API的宏
#define WIDTH 640
#define HEIGHT 480
设置分辨率,为了便于观察,可以将分辨率设为80*80
unsigned char image[HEIGHT][WIDTH];
二维数组:存放位图
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 simplicity, we assume that `bitmap->pixel_mode' */
/* is `FT_PIXEL_MODE_GRAY' (i.e., not a bitmap font) */
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];
}
}
}
将位图存入二维数组,如果在arm上使用显示器输出,可以直接修改为显示输出函数
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' );
}
}
PC上的显示函数,显示一副位图
下面开始是主函数部分:
filename = argv[1]; /* first argument */
text = argv[2]; /* second argument */
num_chars = strlen( text );
angle = ( 25.0 / 360 ) * 3.14159 * 2; /* use 25 degrees */
target_height = HEIGHT;
取出字体文件名、文本、字符数,设置旋转角度、笛卡尔坐标系下高度
error = FT_Init_FreeType( &library ); /* initialize library */
初始化
error = FT_New_Face( library, filename, 0, &face );/* create face object */
创建字体平面
error = FT_Set_Char_Size( face, 50 * 64, 0,100, 0 ); /* set character size */
设置字体大小,为了便于观察改为32*64
slot = face->glyph;
定义字体插槽
/* set up matrix */
matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );
设置旋转矩阵参数
pen.x = 300 * 64;
pen.y = ( target_height - 200 ) * 64;
设置原点指针,为匹配上面的修改,将(300,200)设为(10,60)
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;
}
1、设置旋转矩阵
2、从字体文件中加载字符,并转换为位图
3、保存位图
4、原点指针指向下一个字符的原点
show_image();
显示输出
FT_Done_Face ( face );
FT_Done_FreeType( library );
销毁字体平面和库
本文介绍在PC上使用Freetype库显示英文及数字的方法,包括库的下载、配置、安装步骤,以及如何通过GCC编译和运行示例程序。文章详细解析了代码流程,如设置分辨率、位图绘制、显示函数、旋转矩阵和字符加载。
5万+

被折叠的 条评论
为什么被折叠?



