您的位置:首页 > 运维架构 > Linux

linux应用--freetype在linux-PC下的一些零碎知识

2014-08-07 18:22 357 查看
1.Freetype简介:

Freetype库是一个完全免费(开源)的、高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件,包括TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF等。

支持单色位图、反走样位图的渲染。FreeType库是高度模块化的程序库,虽然它是使用ANSI C开发,但是采用面向对象的思想,因此,FreeType的用户可以灵活地对它进行裁剪。

2 FreeType的安装:

从http://download.savannah.gnu.org/releases/freetype/下载freetype-2.5.3.tar.gz和freetype-doc-2.5.3.tar.gz两个文件,分别解压。

3. 在PC端采用默认配置安装:

#cd freetype-2.5.3
#./configure
#make
#make install
此时相关的头文件和库都会安装在/usr/local/目录下

在/freetype-2.5.3/docs/tutorial/有个例程文件example1.c,简单编译下,注意编译时应该指定头文件,库的目录:

gcc -o example1 example1.c  -I /usr/local/include/freetype2 -lfreetype -lm


简单分析下example1.c文件

a.初始化库

FT_Library library;
FT_Face face;

FT_Error error = FT_Init_FreeType( &library );


b. 加载相应的字体文件(该字体文件可在c:\window\fonts目录下找)

FT_New_Face( library, filename, 0, &face );


c. 设置字体的大小 (第一个函数字体大小以实际尺寸计算,第二个函数以像素大小计算

error = FT_Set_Char_Size(face,    /* handle to face object */
0,       /* char_width in 1/64th of points  */
16*64,   /* char_height in 1/64th of points */
300,     /* horizontal device resolution    */
300 );   /* vertical device resolution      */

error = FT_Set_Pixel_Sizes(face,   /* handle to face object */
0,      /* pixel_width           */
16 );   /* pixel_height          */
d.加载字符的glyph

glyph_index = FT_Get_Char_Index( face, charcode );

error = FT_Load_Glyph(face,          /* handle to face object */
glyph_index,   /* glyph index           */
load_flags );  /* load flags, see below */

error = FT_Render_Glyph( face->glyph,   /* glyph slot  */
render_mode ); /* render mode */


其中这三个函数的实现等同于:

error = FT_Load_Char( face, charcode, FT_LOAD_RENDER );


e.字体变换(旋转和缩放)

error = FT_Set_Transform( face,       /* target face object    */
&matrix,    /* pointer to 2x2 matrix */
&delta );   /* pointer to 2d vector  */


f. 把字符显示出来

draw_bitmap( &slot->bitmap,  pen_x + slot->bitmap_left,  pen_y - slot->bitmap_top );


运行后就能在终端显示相应的字体

#./example1 ./simkai.ttf world


4. 显示中文字体:

在显示中文时,会发生字节不对齐问题,故将含中文字体的字符串定义为wchat_t 型,在使用该类型时,须包含#include <wchar.h>文件。

eg:

wchar_t *ch_char = L"大海abc";

在编译过程中,需添加字体编码选项:
#gcc -finput-charset=GBK -fexec-charset=UTF-8 -o example1 example1.c -I /usr/local/include/freetype2 -lfreetype -lm

5.获取字体框架坐标:
#include FT_GLYPH_H

......

FT_BBox bbox;
FT_Glyph glyph;

......

error = FT_Get_Glyph( face->glyph, &glyph );//load after FT_Load_Char function
if ( error ) { ......}

FT_Glyph_Get_CBox( glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox );

......

之后 ,从bbox.xMin, bbox.xMax, bbox.yMin, bbox.yMax就能得到字体框架的四个点的坐标。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: