charmaps字符映射表,使用编码值找到对应关键点(glyph字形FT)
支持GB2312,UNICODE,UTF-8
⚫ 给定一个字符可以确定它的编码值(ASCII、UNICODE、GB2312);
⚫ 设置字体大小;
⚫ 根据编码值,从文件头部中通过charmap找到对应的关键点(glyph),它会 根据字体大小调整关键点;
⚫ 把关键点转换为位图点阵;
⚫ 在LCD上显示出来
二、使用Freetype库 #include <ft2build.h>
FT_Library library; // 1.打开引擎
FT_Face face; // 2.加载字体
FT_Vector pen; // 3.笔尖位置
FT_GlyphSlot slot; //4.(字形槽)存放当前图像,把字加载到slot
int font_size = 24; //字号大小
FT_Matrix matrix; //(矩阵)旋转缩放用
double angle; //旋转角度
1.初始化:FT_InitFreetype(&library);
2.加载(打开)字体Face:FT_New_Face(library,argv[1],,&face);
slot = face->glyph;/*<./font_show> <font_file> <angle> [font_size]*/
3.设置字体大小:FT_Set_Char_Sizes 或 FT_Set_Pixel_Sizes(face,font_size,0);
4.选择charmap:FT_Select_Charmap (face,FT_ENCODING_UNICODE);(字符映射表)
5.FT_Load_Char(face, str[0], FT_LOAD_RENDER); //渲染
它就可以得到位图。(charcode字符编码)
- 按你设置的
font_size大小,把字符缩放到目标尺寸 - 根据你设置的
angle和matrix,做旋转 / 变形 - 生成带抗锯齿的灰度位图(0-255,0 = 黑,255 = 白)
- 把位图数据放到
slot->bitmap里,你直接拿buffer、width、rows就能画
小结: L 是 C 语言里的宽字符串前缀,它的作用是:
-1.告诉编译器:“把后面的字符串,当成 wchar_t 类型的宽字符来处理”
- 2.字符串里的每个字符,都会被编码成 wchar_t (Linux 上通常是 4 字节,Windows 上是 2 字节) // 每个字符都是 wchar_t 类型的 Unicode 编码
wchar_t *chinese_str = L"中guo"; ==> wchar_t chinese_str[] = { L'中', L'g', L'u', L'o', 0 };
注意:如果test_wchar.c是以ANSI(GB2312)格式保存,那么需要使用以下命 令来编译: gcc -finput-charset=GB2312 -fexec-charset=UTF-8 -o test_wchar test_wchar.c
#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 <sys/ioctl.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
int fd_fb;
struct fb_var_screeninfo var;
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;
/**********************************************************************
* 函数名称: lcd_put_pixel
* 功能描述: 在LCD指定位置上输出指定颜色(描点)
* 输入参数: x坐标,y坐标,颜色
* 输出参数: 无
* 返 回 值: 会
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2020/05/12 V1.0 zh(angenao) 创建
***********************************************************************/
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;
}
}
}
/**********************************************************************
* 函数名称: draw_bitmap
* 功能描述: 根据bitmap位图,在LCD指定位置显示汉字
* 输入参数: x坐标,y坐标,位图指针
* 输出参数: 无
* 返 回 值: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2020/05/12 V1.0 zh(angenao) 创建
***********************************************************************/
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 ( j = y, q = 0; j < y_max; j++, q++ )
{
for ( i = x, p = 0; i < x_max; i++, p++ )
{
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 *chinese_str = L"中";
FT_Library library;
FT_Face face;
int error;
FT_Vector pen;
FT_GlyphSlot slot; //字槽
int font_size = 24;
FT_Matrix matrix; /* transformation matrix */
double angle;
if (argc < 3)
{
printf("Usage : %s <font_file> <angle> [font_size]\n", argv[0]);
return -1;
}
angle = ( 1.0* strtoul(argv[2], NULL, 0) / 360 ) * 3.14159 * 2; /* use 25 degrees */
if (argc == 4)
font_size = strtoul(argv[3], NULL, 0);
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;
}
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);
/* 一.加载字体库 */
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; //slot = 你当前正在查的那个字
FT_Set_Pixel_Sizes(face, font_size, 0); //字体大小
/* 二、1 确定座标: 笔尖位*/
pen.x = 0;
pen.y = 0;
/* 二、2 矩阵*/
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 );
/* 二、3控制字形 &matrix(旋转、缩放、倾斜) &pen(字体位置)*/
FT_Set_Transform( face, &matrix, &pen);
/* 三.加载并渲染字符,生成位图 */
error = FT_Load_Char(face, chinese_str[0], FT_LOAD_RENDER );
if (error)
{
printf("FT_Load_Char error\n");
return -1;
}
//FT_Load_Char = 翻字典,找到那个字,把它读到 slot 里(slot = face->glyph;)
/* 四.直接画到屏幕*/
draw_bitmap( &slot->bitmap,
var.xres/2,
var.yres/2);
return 0;
}
增:
以后显示文字:只需要 FreeType + 一套代码
不管是什么屏幕:
- OLED
- TFT
- LCD
- HDMI
- 电子墨水屏
- 甚至 I2C、SPI 接口屏
只要是像素屏,FreeType 这套代码 100% 通用!
2113




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



