文件浏览器及数码相框 -2.3.2-freetype_arm-2
显示多行文字
两行文字左边对齐
简单使用两个循环显示两行字体
根据上一行字体的宽度来进行下一行左边的计算
#include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <linux/fb.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <math.h> #include <wchar.h> #include <ft2build.h> #include <stdlib.h> #include FT_FREETYPE_H #include FT_GLYPH_H #include <linux/font.h> int fd_fb; struct fb_var_screeninfo var; /* Current var */ struct fb_fix_screeninfo fix; /* Current fix */ int screen_size; //空间大小 unsigned char *fb_mem; //framebaffer空间地址 unsigned int line_width; unsigned int pixel_width; void lcd_put_pixel(int x, int y, unsigned int color) { unsigned char *pen_8 = fb_mem + y * line_width + x * pixel_width; //当前像素对应内存位置 unsigned short *pen_16; unsigned int *pen_32; unsigned int red, blue, green; pen_16 = (unsigned short *)pen_8; pen_32 = (unsigned int *)pen_8; switch(var.bits_per_pixel) { case 8: { *pen_8 = color; //对应调色板颜色 break; } case 16: { /* 5*6*5 */ red = (color >> 16) & 0xff; green = (color >> 8) & 0xff; blue = (color >> 0) & 0xff; color = ((red >> 3 ) << 11) | ((green >> 2) << 5) | ( blue >> 3); /* 颜色数据为高位 */ *pen_16 = color; break; } case 32: { *pen_32 = color; break; } } } 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; printf("x = %d, y = %d\n", x, y); 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 >= var.xres || j >= var.yres ) continue; // image[j][i] |= bitmap->buffer[q * bitmap->width + p]; lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]); } } } int main(int argc, char **argv) { wchar_t * str1 = L"陈志朋gif"; wchar_t * str2 = L"hello the world"; FT_BBox bbox; FT_Glyph glyph; FT_Library library; FT_Face face; FT_Vector pen; /* untransformed origin */ FT_GlyphSlot slot; FT_Matrix matrix; /* transformation matrix */ int error; double angle; int i; int line_box_ymin = 10000; int line_box_ymax = 0; if(argc != 2) { printf("Usage : %s <font_file> <angle>\n",argv[0]); return -1; } fd_fb = open("/dev/fb0",O_RDWR); if(fd_fb < 0) { printf("can't open /dev/fb0 \n"); return -1; } if(ioctl(fd_fb, FBIOGET_VSCREENINFO, &var)) { printf("can't get var \n"); return -1; } if(ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix)) { printf("can't get fix \n"); return -1; } screen_size = var.xres * var.yres * var.bits_per_pixel / 8; //单位字节 line_width = var.xres * var.bits_per_pixel / 8; pixel_width = var.bits_per_pixel / 8; fb_mem = (unsigned char *)mmap(NULL, screen_size, \ PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0); if(fb_mem == (unsigned char *) -1) { printf("can't mmap \n"); return -1; } memset(fb_mem, 0, screen_size); /* 显示矢量文字 */ error = FT_Init_FreeType( &library ); /* initialize library */ /* error handling omitted */ error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */ /* error handling omitted */ slot = face->glyph; FT_Set_Pixel_Sizes(face, 24, 0); /*确定坐标 *lcd_x = 0 *lcd_y = 24 *笛卡尔坐标 *x = lcd_x = 0 *y = var.yres - 24 */ pen.x = ( 0 ) * 64; pen.y = ( var.yres - 24 ) * 64; for(i = 0; i < wcslen(str1); i++) { /* set transformation */ FT_Set_Transform( face,0, &pen ); /* load glyph image into the slot (erase previous one) */ error = FT_Load_Char( face, str1[i], FT_LOAD_RENDER ); if ( error ) { printf("FT_Load_Char error\n"); return -1; } error = FT_Get_Glyph( face->glyph, &glyph ); if (error) { printf("FT_Get_Glyph error!\n"); return -1; } FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox ); if(line_box_ymin > bbox.yMin) line_box_ymin = bbox.yMin; if(line_box_ymax < bbox.yMax) line_box_ymax = bbox.yMax; /* now, draw to our target surface (convert position) */ draw_bitmap( &slot->bitmap, slot->bitmap_left, var.yres - slot->bitmap_top ); pen.x += slot->advance.x; } /*确定坐标 *lcd_x = 0 *lcd_y = 24 *笛卡尔坐标 *x = lcd_x = 0 *y = var.yres - 24 */ pen.x = ( 0 ) * 64; pen.y = ( var.yres - (line_box_ymax - line_box_ymin + 24) ) * 64; for(i = 0; i < wcslen(str2); i++) { /* set transformation */ FT_Set_Transform( face,0, &pen ); /* load glyph image into the slot (erase previous one) */ error = FT_Load_Char( face, str2[i], FT_LOAD_RENDER ); if ( error ) { printf("FT_Load_Char error\n"); return -1; } error = FT_Get_Glyph( face->glyph, &glyph ); if (error) { printf("FT_Get_Glyph error!\n"); return -1; } FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox ); if(line_box_ymin > bbox.yMin) line_box_ymin = bbox.yMin; if(line_box_ymax < bbox.yMax) line_box_ymax = bbox.yMax; /* now, draw to our target surface (convert position) */ draw_bitmap( &slot->bitmap, slot->bitmap_left, var.yres - slot->bitmap_top ); pen.x += slot->advance.x; } }
使用freetpye中的函数实现一行文字居中
4.6 高级文本渲染:变换 + 居中 + 字距调整
现在我们将修改我们的代码,以便可以容易地变换已渲染的字符串,例如旋转它。我们将以实行少许小
改进开始:
4.6.1 打包然后平移字形
我们先把与一个字形图像相关的信息打包到一个结构体,而不是并行的数组。因此我们定义下面的结构
体类型:
typedef struct TGlyph_
{
FT_UInt index; /* 字形索引 */
FT_Vector pos; /* 基线上面的字形原点 */
FT_Glyph image; /* 字形图像 */
} TGlyph, *PGlyph;
我们在装载每一个字形图像过程中,在把它装载它在基线所在位置后便直接平移它。我们将看到,这有若干
好处。我们的字形序列装载其因而变成:
FT_GlyphSlot slot = face->glyph; /* 一个小捷径 */
FT_UInt glyph_index;
FT_Bool use_kerning;
FT_UInt previous;
int pen_x, pen_y, n;
TGlyph glyphs[MAX_GLYPHS]; /* 字形表 */
PGlyph glyph; /* 表中的当前字形*/
FT_UInt num_glyphs;
... 初始化库 ...
... 创建 face 对象 ...
... 设置字符尺寸 ...
pen_x = 0; /* 以 (0,0) 开始 */
pen_y = 0;
num_glyphs = 0;
use_kerning = FT_HAS_KERNING( face );
previous = 0;
glyph = glyphs;
for ( n = 0; n < num_chars; n++ )
{
glyph->index = FT_Get_Char_Index( face, text[n] );
if ( use_kerning && previous && glyph->index )
{
FT_Vector delta;
FT_Get_Kerning( face, previous, glyph->index,
FT_KERNING_MODE_DEFAULT, &delta );
pen_x += delta.x >> 6;
}
/* 保存当前笔位置 */
glyph->pos.x = pen_x;
glyph->pos.y = pen_y;
error = FT_Load_Glyph(face,glyph_index,FT_LOAD_DEFAULT);
if ( error ) continue;
error = FT_Get_Glyph( face->glyph, &glyph->image );
if ( error ) continue;
/* 现在平移字形图像 */
FT_Glyph_Transform( glyph->image, 0, &glyph->pos );
pen_x += slot->advance.x >> 6;
previous = glyph->index;
/* 增加字形的数量 */
glyph++;
}
/* 计算已装载的字形的数量 */
num_glyphs = glyph - glyphs;
注意,这个时候平移字形有若干好处。第一是当我们计算字符串的边界框时不需要平移字形 bbox。代码将会
变成这样:
void compute_string_bbox( FT_BBox *abbox )
{
FT_BBox bbox;
bbox.xMin = bbox.yMin = 32000;
bbox.xMax = bbox.yMax = -32000;
for ( n = 0; n < num_glyphs; n++ )
{
FT_BBox glyph_bbox;
FT_Glyph_Get_CBox( glyphs[n], &glyph_bbox );
if (glyph_bbox.xMin < bbox.xMin)
bbox.xMin = glyph_bbox.xMin;
if (glyph_bbox.yMin < bbox.yMin)
bbox.yMin = glyph_bbox.yMin;
if (glyph_bbox.xMax > bbox.xMax)
bbox.xMax = glyph_bbox.xMax;
if (glyph_bbox.yMax > bbox.yMax)
bbox.yMax = glyph_bbox.yMax;
}
if ( bbox.xMin > bbox.xMax )
{
bbox.xMin = 0;
bbox.yMin = 0;
bbox.xMax = 0;
bbox.yMax = 0;
}
*abbox = bbox;
}
更详细描述: compute_string_bbox 函数现在可以计算一个已转换的字形字符串的边界框。例如,我们可以做
如下的事情:
FT_BBox bbox;
FT_Matrix matrix;
FT_Vector delta;
... 装载字形序列 ...
... 设置 "matrix" 和 "delta" ...
/* 变换字形 */
for ( n = 0; n < num_glyphs; n++ )
FT_Glyph_Transform( glyphs[n].image, &matrix, &delta );
/* 计算已变换字形的边界框 */
compute_string_bbox( &bbox );
#include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <linux/fb.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <math.h> #include <wchar.h> #include <ft2build.h> #include <stdlib.h> #include FT_FREETYPE_H #include FT_GLYPH_H #include <linux/font.h> #define MAX_GLYPHS 100 typedef struct TGlyph_ { FT_UInt index; /* ???? */ FT_Vector pos; /* ????????? */ FT_Glyph image; /* ???? */ } TGlyph, *PGlyph; int fd_fb; struct fb_var_screeninfo var; /* Current var */ struct fb_fix_screeninfo fix; /* Current fix */ int screen_size; //空间大小 unsigned char *fb_mem; //framebaffer空间地址 unsigned int line_width; unsigned int pixel_width; void lcd_put_pixel(int x, int y, unsigned int color) { unsigned char *pen_8 = fb_mem + y * line_width + x * pixel_width; //当前像素对应内存位置 unsigned short *pen_16; unsigned int *pen_32; unsigned int red, blue, green; pen_16 = (unsigned short *)pen_8; pen_32 = (unsigned int *)pen_8; switch(var.bits_per_pixel) { case 8: { *pen_8 = color; //对应调色板颜色 break; } case 16: { /* 5*6*5 */ red = (color >> 16) & 0xff; green = (color >> 8) & 0xff; blue = (color >> 0) & 0xff; color = ((red >> 3 ) << 11) | ((green >> 2) << 5) | ( blue >> 3); /* 颜色数据为高位 */ *pen_16 = color; break; } case 32: { *pen_32 = color; break; } } } 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; printf("x = %d, y = %d\n", x, y); 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 >= var.xres || j >= var.yres ) continue; // image[j][i] |= bitmap->buffer[q * bitmap->width + p]; lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]); } } } int Get_Glphs_Frm_Wstr(FT_Face face, wchar_t * wstr, TGlyph glyphs[]) { PGlyph glyph = glyphs; FT_GlyphSlot slot = face->glyph; int n; int pen_x =0; int pen_y = 0; int error; for(n = 0; n < wcslen(wstr); n++ ) { glyph->index = FT_Get_Char_Index( face, wstr[n] ); /* 保存当前笔位置 */ glyph->pos.x = pen_x; glyph->pos.y = pen_y; /* load 是把glyph 加载到face->glyph */ error = FT_Load_Glyph(face, glyph->index, FT_LOAD_DEFAULT); if ( error ) continue; error = FT_Get_Glyph( face->glyph, &glyph->image ); if ( error ) continue; /* 现在平移字形图像 */ /* 使得glyph->image里面有位置信息 */ FT_Glyph_Transform( glyph->image, 0, &glyph->pos ); pen_x += slot->advance.x; /* 1 / 64 piont */ /* 增加字形的数量 */ glyph++; } /* 计算已装载的字形的数量 */ return (glyph - glyphs); } void compute_string_bbox( TGlyph glyphs[], FT_UInt num_glyphs, FT_BBox *abbox ) { FT_BBox bbox; int n; bbox.xMin = bbox.yMin = 32000; bbox.xMax = bbox.yMax = -32000; for ( n = 0; n < num_glyphs; n++ ) { FT_BBox glyph_bbox; FT_Glyph_Get_CBox( glyphs[n].image, FT_GLYPH_BBOX_TRUNCATE, &glyph_bbox ); if (glyph_bbox.xMin < bbox.xMin) bbox.xMin = glyph_bbox.xMin; if (glyph_bbox.yMin < bbox.yMin) bbox.yMin = glyph_bbox.yMin; if (glyph_bbox.xMax > bbox.xMax) bbox.xMax = glyph_bbox.xMax; if (glyph_bbox.yMax > bbox.yMax) bbox.yMax = glyph_bbox.yMax; } *abbox = bbox; } void Draw_Glyphs(TGlyph glyphs[], FT_UInt num_glyphs, FT_Vector pen) { int n; int error; for ( n = 0; n < num_glyphs; n++ ) { FT_Glyph_Transform( glyphs[n].image, 0, &pen ); error = FT_Glyph_To_Bitmap(&glyphs[n].image, FT_RENDER_MODE_NORMAL, 0, 1 ); /* 没有附加的平移*//* 销毁 "image" 指向的副本 */ if ( !error ) { FT_BitmapGlyph bit = (FT_BitmapGlyph)glyphs[n].image; draw_bitmap( &bit->bitmap, bit->left, var.yres - bit->top); FT_Done_Glyph( glyphs[n].image ); } } } int main(int argc, char **argv) { wchar_t * str1 = L"陈志朋gif"; wchar_t * str2 = L"hello the world"; FT_BBox bbox; FT_Glyph glyph; FT_Library library; FT_Face face; FT_Vector pen; /* untransformed origin */ FT_GlyphSlot slot; FT_Matrix matrix; /* transformation matrix */ TGlyph glyphs[MAX_GLYPHS]; /* ??? */ FT_UInt num_glyphs; int error; double angle; int i; int line_box_ymin = 10000; int line_box_ymax = 0; int line_box_width; int line_box_hight; if(argc != 2) { printf("Usage : %s <font_file> <angle>\n",argv[0]); return -1; } fd_fb = open("/dev/fb0",O_RDWR); if(fd_fb < 0) { printf("can't open /dev/fb0 \n"); return -1; } if(ioctl(fd_fb, FBIOGET_VSCREENINFO, &var)) { printf("can't get var \n"); return -1; } if(ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix)) { printf("can't get fix \n"); return -1; } screen_size = var.xres * var.yres * var.bits_per_pixel / 8; //单位字节 line_width = var.xres * var.bits_per_pixel / 8; pixel_width = var.bits_per_pixel / 8; fb_mem = (unsigned char *)mmap(NULL, screen_size, \ PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0); if(fb_mem == (unsigned char *) -1) { printf("can't mmap \n"); return -1; } memset(fb_mem, 0, screen_size); /* 显示矢量文字 */ error = FT_Init_FreeType( &library ); /* initialize library */ /* error handling omitted */ error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */ /* error handling omitted */ slot = face->glyph; FT_Set_Pixel_Sizes(face, 24, 0); num_glyphs = Get_Glphs_Frm_Wstr(face, str1, glyphs); compute_string_bbox(glyphs, num_glyphs, &bbox); line_box_width = bbox.xMax - bbox.xMin; line_box_hight = bbox.yMax - bbox.yMin; pen.x = (var.xres - line_box_width) / 2 * 64; pen.y = (var.yres - line_box_hight) / 2 * 64; Draw_Glyphs(glyphs, num_glyphs, pen); num_glyphs = Get_Glphs_Frm_Wstr(face, str2, glyphs); compute_string_bbox(glyphs, num_glyphs, &bbox); line_box_width = bbox.xMax - bbox.xMin; line_box_hight = bbox.yMax - bbox.yMin; pen.x = (var.xres - line_box_width) / 2 * 64; pen.y = pen.y - 24 * 64; Draw_Glyphs(glyphs, num_glyphs, pen); }
中心双行输出
4.6.2 渲染一个已变换的字形序列
无论如何,如果我们想重用字形来以不同的角度或变换方式绘制字符串,直接变换序列中的字形都不是
一个好主意。更好的方法是在字形被渲染前执行放射变换,如下面的代码所示:
FT_Vector start;
FT_Matrix transform;
/* 获取原始字形序列的 bbox */
compute_string_bbox( &string_bbox );
/* 计算整数象素表示的字符串尺度 */
string_width = (string_bbox.xMax - string_bbox.xMin) / 64;
string_height = (string_bbox.yMax - string_bbox.yMin) / 64;
/* 设置 26.6 笛卡儿空间表示的笔起始位置 */
start.x = ( ( my_target_width - string_width ) / 2 ) * 64;
start.y = ( ( my_target_height - string_height ) / 2 ) * 64;
/* 设置变换(旋转) */
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 );
for ( n = 0; n < num_glyphs; n++ )
{
FT_Glyph image;
FT_Vector pen;
FT_BBox bbox;
/* 创建原始字形的副本 */
error = FT_Glyph_Copy( glyphs[n].image, &image );
if ( error ) continue;
/* 变换副本(这将平移它到正确的位置) */
FT_Glyph_Transform( image, &matrix, &start );
/* 检查边界框;如果已变换的字形图像不在*/
/* 我们的目标表面中,我们可以避免渲染它 */
FT_Glyph_Get_CBox( image, ft_glyph_bbox_pixels, &bbox );
if ( bbox.xMax <= 0 || bbox.xMin >= my_target_width ||
bbox.yMax <= 0 || bbox.yMin >= my_target_height )
continue;
/* 把字形图像转换为位图(销毁字形的副本!) */
error = FT_Glyph_To_Bitmap(
&image,
FT_RENDER_MODE_NORMAL,
0, /* 没有附加的平移*/
1 ); /* 销毁 "image" 指向的副本 */
if ( !error )
{
FT_BitmapGlyph bit = (FT_BitmapGlyph)image;
my_draw_bitmap( bitmap->bitmap,
bitmap->left,
my_target_height - bitmap->top );
FT_Done_Glyph( image );
}
}
这份代码相对于原始版本有少许改变:
z 我们没改变原始的字形图像,而是变换该字形图像的拷贝。
z 我们执行“剪取”操作以处理渲染和绘制的字形不在我们的目标表面(surface)的情况。
z 当调用 FT_Glyhp_To_Bitmap 时,我们总是销毁字形图像的拷贝,这是为了销毁已变换的图像。注意,即
使当这个函数返回错误码,该图像依然会被销毁(这就是为什么 FT_Done_Glyph 只在复合语句中被调用
的原因)。
z 平移字形序列到起始笔位置集成到 FT_Glyph_Transform 函数,而不是 FT_Glyph_To_Bitmap 函数。
可以多次调用这个函数以渲染字符串到不同角度的,或者甚至改变计算 start 的方法以移动它到另外的地
方。无论如何,要注意通常的实现会使用一个字形缓冲以减少内存消耗。据个例子,让我们假定我们的字符