数码相框 在LCD上显示多行文字(6)
目的:
1、从左边起显示几行文字
2、居中显示几行文字
在LCD上显示下列两行文字:
我是程序员gif
Hello World
分析:
1、从左边起显示几行文字
(1)先描画字体
(2)然后算出边框
定义两个标志变量:line_box_ymax和line_box_ymin
通过FT_Glyph_Get_CBox()测量字形图象,获取一行文字的yMax,Min最大值,最小值。
显示第一行时:
pen.x = 0*64; //单位是64分之一像素
显示第2~n行时:
pen.y = (var.yres - 24) * 64; //fb_var.yres:LCD总高度,原点为(0, 24)
2、居中显示几行文字
(1)先算出边框(知道起点,长、宽)
(2)再确定坐标并描画
代码中的一些变量解释:
bbox:是每个字体文件在x和y方向最大值和最小值的结构体
face:字体文件
pen:原点和位置
从左显示多行24X24文字
内容如下:
定义:某一行的BBox的y最小值line_box_ymin和最大值line_box_ymax
通过函数FT_Glyph_Get_CBox()测量字体图象,从glyph得到BBox,BBox含有每个文字的xMin,xMax,yMin,yMax,获取一行文字的yMax,Min最大值和最小值。
从左起显示几行文字代码如下:
#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 FT_FREETYPE_H
#include FT_GLYPH_H
int fd_fb;
struct fb_var_screeninfo var; /* Current var */
struct fb_fix_screeninfo fix; /* Current fix */
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;
/* color : 0x00RRGGBB */
void lcd_put_pixel(int x, int y, unsigned int color)
{
unsigned char *pen_8 = fbmem+y*line_width+x*pixel_width;
unsigned short *pen_16;
unsigned int *pen_32;
unsigned int red, green, blue;
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:
{
/* 565 */
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;
}
default:
{
printf("can't surport %dbpp\n", var.bits_per_pixel);
break;
}
}
}
/* 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;
//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];
//输出函数,因为buffer是一个字节,而lcd_put_pixel()是0x00RRGGBB,所以打印出来的是蓝色字体
lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]);//参数:x坐标,y坐标,颜色值
}
}
}
int main(int argc, char **argv)
{
wchar_t *wstr1 = L"我是程序员gif";
wchar_t *wstr2 = L"Hello World";
FT_Library library;
FT_Face face;
int error;
FT_Vector pen; /* untransformed origin */
FT_GlyphSlot slot;
int i;
//方框
FT_BBox bbox;
FT_Glyph glyph;
//某一行的BBox的y最小值和最大值
int line_box_ymin = 10000;
int line_box_ymax = 0;
if (argc != 2)//打印出用法
{
printf("Usage : %s <font_file>\n", argv[0]);
return -1;
}
fd_fb = open("/dev/fb0", O_RDWR);//打开LCD
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;
}
//记录LCD的参数
line_width = var.xres * var.bits_per_pixel / 8;
pixel_width = var.bits_per_pixel / 8;
screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
fbmem = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);//内存映射
if (fbmem == (unsigned char *)-1)
{
printf("can't mmap\n");
return -1;
}
/* 清屏: 全部设为黑色 */
memset(fbmem, 0, screen_size);
/* 显示矢量字体,把show_font和example2两个合在一起 */
error = FT_Init_FreeType( &library ); /* 初始化freetype库 */
/* error handling omitted */
//装载字体文件
error = FT_New_Face( library, argv[1], 0, &face ); /* 打开一个字体文件 */
/* error handling omitted */
//每得到一个face,都会有一个slot插槽,插槽就是装载一个字体里面的数据
//每加载一个文字,插槽slot就会变换一下,里面的数据时最新的
slot = face->glyph;
//设置字体的大小24X24
FT_Set_Pixel_Sizes(face, 24, 0);
/* 确定坐标: A中\矢量字体\
* lcd_x = 0
* lcd_y = 24
* 转换为笛卡尔坐标系:
* x = lcd_x = 0
* y = var.yres - lcd_y = var.yres/2 - 24
*/
//pen就是矢量字体的位置
//第一个字符的原点“我”
pen.x = 0 * 64;//单位是64分之一像素
pen.y = (var.yres - 24) * 64;
for (i = 0; i < wcslen(wstr1); i++)
{
/* 设置转换方法 */
FT_Set_Transform( face, 0, &pen );//设置转换矩阵matrix,转换参数,0表示不旋转
/* load glyph image into the slot (erase previous one) */
//FT_LOAD_RENDER参数会把glyph矢量文件转换为位图
//根据chinese_str(字符unicode码),在字体文件face得到glyph,加载到slot(face->glyph)里面
error = FT_Load_Char( face, wstr1[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;
}
//从glyph得到BBox,BBox含有每个文字的xMin,xMax,yMin,yMax
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) */
//描绘出来,slot就是face里面的glyph
draw_bitmap( &slot->bitmap,
slot->bitmap_left,//bitmap_left这是笛卡尔坐标系
var.yres - slot->bitmap_top );//slot->bitmap_top是笛卡尔坐标系
/* increment pen position */
//增加笔的位置
pen.x += slot->advance.x;
//pen.y += slot->advance.y;
}
/* 确定坐标: A中\矢量字体\
* lcd_x = 0
* lcd_y = line_box_ymax - line_box_ymin + 24
* 转换为笛卡尔坐标系:
* x = lcd_x = 0
* y = var.yres - lcd_y = var.yres - (line_box_ymax - line_box_ymin + 24)
*/
//pen就是矢量字体的位置
//第一个字符的原点“我”
pen.x = 0 * 64;//单位是64分之一像素
pen.y = (var.yres - (line_box_ymax - line_box_ymin + 24)) * 64;
for (i = 0; i < wcslen(wstr2); i++)
{
/* 设置转换方法 */
FT_Set_Transform( face, 0, &pen );//设置转换矩阵matrix,转换参数,0表示不旋转
/* load glyph image into the slot (erase previous one) */
//FT_LOAD_RENDER参数会把glyph矢量文件转换为位图
//根据chinese_str(字符unicode码),在字体文件face得到glyph,加载到slot(face->glyph)里面
error = FT_Load_Char( face, wstr2[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;
}
//从glyph得到BBox,BBox含有每个文字的xMin,xMax,yMin,yMax
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) */
//描绘出来,slot就是face里面的glyph
draw_bitmap( &slot->bitmap,
slot->bitmap_left,//bitmap_left这是笛卡尔坐标系
var.yres - slot->bitmap_top );//slot->bitmap_top是笛卡尔坐标系
/* increment pen position */
//增加笔的位置
pen.x += slot->advance.x;
//pen.y += slot->advance.y;
}
return 0;
}
居中显示
内容如下:
参考第11页:https://wenku.baidu.com/view/060a0b44f12d2af90342e63a.html?from=search
(1)首先定义一个用来存储一行文字的字形图象数组
(2)首先以坐标(0,0)为基值,获取每个文字的字形图像和坐标值,存到glyphs[]里
(3)通过glyphs[]存的一行字形图象,计算出边界框
(4)通过边界框,找到居中显示的坐标信息
(5)通过坐标信息,将glyphs[]存的一行字形图像显示出来
居中显示文字代码如下:
#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 FT_FREETYPE_H
#include FT_GLYPH_H
typedef struct TGlyph_
{
FT_UInt index; /* glyph index */
FT_Vector pos; /* glyph origin on the baseline */
FT_Glyph image; /* glyph image *///真正的glyph
} TGlyph, *PGlyph;
#define MAX_GLYPHS 100
int fd_fb;
struct fb_var_screeninfo var; /* Current var */
struct fb_fix_screeninfo fix; /* Current fix */
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;
/* color : 0x00RRGGBB */
void lcd_put_pixel(int x, int y, unsigned int color)
{
unsigned char *pen_8 = fbmem+y*line_width+x*pixel_width;
unsigned short *pen_16;
unsigned int *pen_32;
unsigned int red, green, blue;
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:
{
/* 565 */
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;
}
default:
{
printf("can't surport %dbpp\n", var.bits_per_pixel);
break;
}
}
}
/* 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;
//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];
//输出函数,因为buffer是一个字节,而lcd_put_pixel()是0x00RRGGBB,所以打印出来的是蓝色字体
lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]);//参数:x坐标,y坐标,颜色值
}
}
}
//从宽字符获得glyphs
int Get_Glyphs_Frm_Wstr(FT_Face face, wchar_t *wstr, TGlyph glyphs[])
{
int n;
PGlyph glyph = glyphs;
int pen_x = 0;
int pen_y = 0;
int error;
FT_GlyphSlot slot = face->glyph;//插槽
//把wstr每个字符取出来
for (n = 0; n < wcslen(wstr); n++)
{
//根据字符串wstr的unicode码wstr[n]获得glyph的索引
glyph->index = FT_Get_Char_Index( face, wstr[n] );
/* store current pen position */
glyph->pos.x = pen_x;
glyph->pos.y = pen_y;
//从字体文件face的glyph加载出来
//load时是把glyph放入face->glyph插槽
error = FT_Load_Glyph( face, glyph->index, FT_LOAD_DEFAULT );
if ( error )
continue;
//把glyph拷贝出来,防止覆盖
error = FT_Get_Glyph( face->glyph, &glyph->image );
if ( error )
continue;
/* translate the glyph image now */
//这使得glyph->image里含有位置信息
FT_Glyph_Transform( glyph->image, 0, &glyph->pos );
//下一个原点的位置
pen_x += slot->advance.x;//单位是1/64point
/* increment number of glyphs */
//指向下一个保存的位置
glyph++;
}
/* count number of glyphs loaded */
//返回glyph的个数
return (glyph - glyphs);
}
//计算长度和宽度,计算glyphs
void compute_string_bbox(TGlyph glyphs[], FT_UInt num_glyphs, FT_BBox *abbox)
{
//含有x和y方向最小值,最大值
FT_BBox bbox;
int n;
bbox.xMin = bbox.yMin = 32000;
bbox.xMax = bbox.yMax = -32000;
//取出每一个字的BBox,一一比较,确定值
for ( n = 0; n < num_glyphs; n++ )
{
FT_BBox glyph_bbox;
//FT_CLYPH_BBOX_TRUNCATE以像素为单位取出来
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++)
{
//pen是新旧位置的偏移值,因为原本是在笛卡尔坐标系的原点,现在要移动到中间
FT_Glyph_Transform(glyphs[n].image, 0, &pen);
/* convert glyph image to bitmap (destroy the glyph copy!) */
//转换为位图
error = FT_Glyph_To_Bitmap(&glyphs[n].image, FT_RENDER_MODE_NORMAL,
0, /* no additional translation */
1 ); /* destroy copy in "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 *wstr1 = L"我是程序员gif";
wchar_t *wstr2 = L"Hello World";
FT_Library library;
FT_Face face;//字体
int error;
FT_Vector pen; /* untransformed origin */
FT_GlyphSlot slot;
int i;
//方框
FT_BBox bbox;
//某一行的BBox的y最小值和最大值
int line_box_ymin = 10000;
int line_box_ymax = 0;
//中间显示时的宽度和高度
int line_box_width;
int line_box_height;
TGlyph glyphs[MAX_GLYPHS]; /* glyphs table */
FT_UInt num_glyphs; //glyphs数组存有多少文字
if (argc != 2)//打印出用法
{
printf("Usage : %s <font_file>\n", argv[0]);
return -1;
}
fd_fb = open("/dev/fb0", O_RDWR);//打开LCD
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;
}
//记录LCD的参数
line_width = var.xres * var.bits_per_pixel / 8;
pixel_width = var.bits_per_pixel / 8;
screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
fbmem = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);//内存映射
if (fbmem == (unsigned char *)-1)
{
printf("can't mmap\n");
return -1;
}
/* 清屏: 全部设为黑色 */
memset(fbmem, 0, screen_size);
/* 显示矢量字体,把show_font和example2两个合在一起 */
error = FT_Init_FreeType( &library ); /* 初始化freetype库 */
/* error handling omitted */
//装载字体文件
error = FT_New_Face( library, argv[1], 0, &face ); /* 打开一个字体文件 */
/* error handling omitted */
//每得到一个face,都会有一个slot插槽,插槽就是装载一个字体里面的数据
//每加载一个文字,插槽slot就会变换一下,里面的数据时最新的
slot = face->glyph;
//设置字体的大小24X24
FT_Set_Pixel_Sizes(face, 24, 0);
/* 第一行字符wstr1 */
//从Wstr里面获得glyphs,face是字体文件,
num_glyphs = Get_Glyphs_Frm_Wstr(face, wstr1, glyphs);
compute_string_bbox(glyphs, num_glyphs, &bbox);
line_box_width = bbox.xMax - bbox.xMin;
line_box_height = bbox.yMax - bbox.yMin;
//确定坐标系
pen.x = (var.xres - line_box_width)/2 * 64;
pen.y = (var.yres - line_box_height)/2 * 64;
//描绘
Draw_Glyphs(glyphs, num_glyphs, pen);
/* 第二行字wstr2 */
//把wstr2从字体文件face 加载出来到glyphs
num_glyphs = Get_Glyphs_Frm_Wstr(face, wstr2, glyphs);
//计算大小
compute_string_bbox(glyphs, num_glyphs, &bbox);
line_box_width = bbox.xMax - bbox.xMin;
line_box_height = bbox.yMax - bbox.yMin;
//确定坐标系
pen.x = (var.xres - line_box_width)/2 * 64;
pen.y = pen.y - 24 * 64;//减24相当于往下移动24
//描绘
Draw_Glyphs(glyphs, num_glyphs, pen);
return 0;
}
效果图: