在嵌入式环境中显示字体,如果采用点阵的方式,要先取得汉字的点阵表示形式,然后根据点阵中每一位是否为1来决定是否对屏幕上相应的像素赋值;如果采用矢量字体的话,例如使用freetype库来显示TrueType类型的字体时,其大致的过程如下:
1.初始化库
1
FT_Library library;
2 FT_Face face;
3 FT_Error error = FT_Init_FreeType( & library );
2 FT_Face face;
3 FT_Error error = FT_Init_FreeType( & library );
2. 加载相应的字体文件
error
=
FT_New_Face( library,
"
/usr/share/fonts/truetype/arial.ttf
"
,
0
,
&
face );
3. 设置字体的大小
1
error
=
FT_Set_Char_Size(face,
/*
handle to face object
*/
2 0 , /* char_width in 1/64th of points */
3 16 * 64 , /* char_height in 1/64th of points */
4 300 , /* horizontal device resolution */
5 300 ); /* vertical device resolution */
6
7 error = FT_Set_Pixel_Sizes(face, /* handle to face object */
8 0 , /* pixel_width */
9 16 ); /* pixel_height */
2 0 , /* char_width in 1/64th of points */
3 16 * 64 , /* char_height in 1/64th of points */
4 300 , /* horizontal device resolution */
5 300 ); /* vertical device resolution */
6
7 error = FT_Set_Pixel_Sizes(face, /* handle to face object */
8 0 , /* pixel_width */
9 16 ); /* pixel_height */
4. 加载字符的glyph
1
glyph_index
=
FT_Get_Char_Index( face, charcode );
2
3 error = FT_Load_Glyph(face, /* handle to face object */
4 glyph_index, /* glyph index */
5 load_flags ); /* load flags, see below */
6
7 error = FT_Render_Glyph( face -> glyph, /* glyph slot */
8 render_mode ); /* render mode */
2
3 error = FT_Load_Glyph(face, /* handle to face object */
4 glyph_index, /* glyph index */
5 load_flags ); /* load flags, see below */
6
7 error = FT_Render_Glyph( face -> glyph, /* glyph slot */
8 render_mode ); /* render mode */
5. 字体变换(旋转和缩放)
1
error
=
FT_Set_Transform( face,
/*
target face object
*/
2 & matrix, /* pointer to 2x2 matrix */
3 & delta ); /* pointer to 2d vector */
2 & matrix, /* pointer to 2x2 matrix */
3 & delta ); /* pointer to 2d vector */
6. 把字符显示出来
1
draw_bitmap(
&
slot
->
bitmap, pen_x
+
slot
->
bitmap_left, pen_y
-
slot
->
bitmap_top );