当前位置:网站首页>电子产品量产工具(3)- 文字系统实现
电子产品量产工具(3)- 文字系统实现
2022-08-05 05:16:00 【乛 乛 .】
电子产品量产工具)
3 文字系统设计
3 .1 输入系统头文件front_manager.h
在头文件中抽象出一个结构体来描述一个字符的大小、位置和位图信息。并抽象出一个结构体描述字库的操作比如freetype的操作
3 .1.1 字符信息结构体FontBitMap
typedef struct Region {
int iLeftUpX;
int iLeftUpY;
int iWidth;
int iHeigh;
}Region, *PRegion;
typedef struct FontBitMap {
Region tRegion;
int iCurOriginX; /*当前字体原点x坐标*/
int iCurOriginY; /*当前字体原点y坐标*/
int iNextOriginX; /*下一个字体原点y坐标*/
int iNextOriginY; /*下一个字体原点y坐标*/
unsigned char *pucBuffer; /*存有字符的位图信息*/
}FontBitMap, *PFontBitMap;
3.1.2 字库操作结构体FontBitMap
typedef struct FontOpr {
char *name; /*字体模块名字*/
int (*FontInit)(char *aFineName); /*字体模块初始化函数*/
int (*SetFontSize)(int iFontSize); /*设置字体尺寸*/
int (*GetFontBitMap)(unsigned int dwCode, PFontBitMap ptFontBitMap);/*根据编码值获得字符位图,并存入ptFontBitMap*/
struct FontOpr *ptNext;/*若想支持多种字库文件,使用链表管理*/
}FontOpr, *PFontOpr;
3.1.3 front_manager.h完整代码
#ifndef _FONT_MANAGER_H
#define _FONT_MANAGER_H
#include <common.h>
typedef struct FontBitMap {
Region tRegion;
int iCurOriginX;
int iCurOriginY;
int iNextOriginX;
int iNextOriginY;
unsigned char *pucBuffer;
}FontBitMap, *PFontBitMap;
typedef struct FontOpr {
char *name;
int (*FontInit)(char *aFineName);
int (*SetFontSize)(int iFontSize);
int (*GetFontBitMap)(unsigned int dwCode, PFontBitMap ptFontBitMap);
struct FontOpr *ptNext;
}FontOpr, *PFontOpr;
void RegisterFont(PFontOpr ptFontOpr);
void FontsRegister(void);
int SelectAndInitFont(char *aFontOprName, char *aFontFileName);
int SetFontSize(int iFontSize);
int GetFontBitMap(unsigned int dwCode, PFontBitMap ptFontBitMap);
#endif
3.2 文字系统文件freetype.c
3.2 字库操作结构体
实现freetype字库对应的操作结构体
static FontOpr g_tFreetypeOpr = {
.name = "freetype", /*模块名*/
.FontInit = FreeTypeFontInit, /*模块初始化*/
.SetFontSize = FreeTypeSetFontSize, /*字体大小设置*/
.GetFontBitMap = FreeTypeGetFontBitMap, /*字体位图*/
};
3.2.1 字库初始化函数FreeTypeFontInit
static FT_Face g_tFace;
static int g_iDefaultFontSize = 12;
....
static int FreeTypeFontInit(char *aFineName)
{
FT_Library library;
int error;
error = FT_Init_FreeType( &library ); /* initialize library */
if (error)
{
printf("FT_Init_FreeType err\n");
return -1;
}
error = FT_New_Face(library, aFineName, 0, &g_tFace ); /* create face object */
if (error)
{
printf("FT_New_Face err\n");
return -1;
}
FT_Set_Pixel_Sizes(g_tFace, g_iDefaultFontSize, 0);
return 0;
}
3.2.2 字体大小设置函数FreeTypeSetFontSize
static int FreeTypeSetFontSize(int iFontSize)
{
FT_Set_Pixel_Sizes(g_tFace, iFontSize, 0);
return 0;
}
3.2.3 位图获取函数FreeTypeGetFontBitMap
static int FreeTypeGetFontBitMap(unsigned int dwCode, PFontBitMap ptFontBitMap)
{
int error;
FT_Vector pen;
FT_GlyphSlot slot = g_tFace->glyph;
pen.x = ptFontBitMap->iCurOriginX * 64; /* 单位: 1/64像素 */
pen.y = ptFontBitMap->iCurOriginY * 64; /* 单位: 1/64像素 */
/* 转换:transformation */
FT_Set_Transform(g_tFace, 0, &pen);
/* 加载位图: load glyph image into the slot (erase previous one) */
error = FT_Load_Char(g_tFace, dwCode, FT_LOAD_RENDER);
if (error)
{
printf("FT_Load_Char error\n");
return -1;
}
ptFontBitMap->pucBuffer = slot->bitmap.buffer;
ptFontBitMap->tRegion.iLeftUpX = slot->bitmap_left;
ptFontBitMap->tRegion.iLeftUpY = ptFontBitMap->iCurOriginY*2 - slot->bitmap_top;
ptFontBitMap->tRegion.iWidth = slot->bitmap.width;
ptFontBitMap->tRegion.iHeigh = slot->bitmap.rows;
/*下个字符原点*/
ptFontBitMap->iNextOriginX = ptFontBitMap->iCurOriginX + slot->advance.x / 64;
ptFontBitMap->iNextOriginY = ptFontBitMap->iCurOriginY;
return 0;
}
3.2.4 注册函数
将字库操作结构体注册进上层链表
void FreetypeRegister(void)
{
RegisterFont(&g_tFreetypeOpr);
}
3.2.5 freetype.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 <font_manager.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
static FT_Face g_tFace;
static int FreeTypeFontInit(char *aFineName)
{
FT_Library library;
int error;
error = FT_Init_FreeType( &library ); /* initialize library */
if (error)
{
printf("FT_Init_FreeType err\n");
return -1;
}
error = FT_New_Face(library, aFineName, 0, &g_tFace ); /* create face object */
if (error)
{
printf("FT_New_Face err\n");
return -1;
}
FT_Set_Pixel_Sizes(g_tFace, g_iDefaultFontSize, 0);
return 0;
}
static int FreeTypeSetFontSize(int iFontSize)
{
FT_Set_Pixel_Sizes(g_tFace, iFontSize, 0);
return 0;
}
static int FreeTypeGetFontBitMap(unsigned int dwCode, PFontBitMap ptFontBitMap)
{
int error;
FT_Vector pen;
FT_GlyphSlot slot = g_tFace->glyph;
pen.x = ptFontBitMap->iCurOriginX * 64; /* 单位: 1/64像素 */
pen.y = ptFontBitMap->iCurOriginY * 64; /* 单位: 1/64像素 */
/* 转换:transformation */
FT_Set_Transform(g_tFace, 0, &pen);
/* 加载位图: load glyph image into the slot (erase previous one) */
error = FT_Load_Char(g_tFace, dwCode, FT_LOAD_RENDER);
if (error)
{
printf("FT_Load_Char error\n");
return -1;
}
ptFontBitMap->pucBuffer = slot->bitmap.buffer;
ptFontBitMap->tRegion.iLeftUpX = slot->bitmap_left;
ptFontBitMap->tRegion.iLeftUpY = ptFontBitMap->iCurOriginY*2 - slot->bitmap_top;
ptFontBitMap->tRegion.iWidth = slot->bitmap.width;
ptFontBitMap->tRegion.iHeigh = slot->bitmap.rows;
ptFontBitMap->iNextOriginX = ptFontBitMap->iCurOriginX + slot->advance.x / 64;
ptFontBitMap->iNextOriginY = ptFontBitMap->iCurOriginY;
return 0;
}
static FontOpr g_tFreetypeOpr = {
.name = "freetype",
.FontInit = FreeTypeFontInit,
.SetFontSize = FreeTypeSetFontSize,
.GetFontBitMap = FreeTypeGetFontBitMap,
};
void FreetypeRegister(void)
{
RegisterFont(&g_tFreetypeOpr);
}
3.3 字库管理front_manager.c
3.3.1 注册函数FontsRegister
将下层结构体注册进链表
static PFontOpr g_ptFonts = NULL;
....
void RegisterFont(PFontOpr ptFontOpr)
{
ptFontOpr->ptNext = g_ptFonts;
g_ptFonts = ptFontOpr;
}
void FontsRegister(void)
{
extern void FreetypeRegister(void);
FreetypeRegister();
}
3.3.2 选择初始化函数SelectAndInitFont
static PFontOpr g_ptDefaulFontOpr = NULL;
....
int SelectAndInitFont(char *aFontOprName, char *aFontFileName)
{
PFontOpr ptTmp = g_ptFonts;
while (ptTmp) /*选择字体*/
{
if (strcmp(ptTmp->name, aFontOprName) == 0)
break;
ptTmp = ptTmp->ptNext;
}
if (!ptTmp)
return -1;
g_ptDefaulFontOpr = ptTmp; /*保存字体*/
return ptTmp->FontInit(aFontFileName);
}
3.3.3 字体大小设置函数SetFontSize
int SetFontSize(int iFontSize)
{
return g_ptDefaulFontOpr->SetFontSize(iFontSize);
}
3.2.5 front_manager.c完整代码
#include <font_manager.h>
#include <string.h>
static PFontOpr g_ptFonts = NULL;
static PFontOpr g_ptDefaulFontOpr = NULL;
void RegisterFont(PFontOpr ptFontOpr)
{
ptFontOpr->ptNext = g_ptFonts;
g_ptFonts = ptFontOpr;
}
void FontsRegister(void)
{
extern void FreetypeRegister(void);
FreetypeRegister();
}
int SelectAndInitFont(char *aFontOprName, char *aFontFileName)
{
PFontOpr ptTmp = g_ptFonts;
while (ptTmp)
{
if (strcmp(ptTmp->name, aFontOprName) == 0)
break;
ptTmp = ptTmp->ptNext;
}
if (!ptTmp)
return -1;
g_ptDefaulFontOpr = ptTmp;
return ptTmp->FontInit(aFontFileName);
}
int SetFontSize(int iFontSize)
{
return g_ptDefaulFontOpr->SetFontSize(iFontSize);
}
int GetFontBitMap(unsigned int dwCode, PFontBitMap ptFontBitMap)
{
return g_ptDefaulFontOpr->GetFontBitMap(dwCode, ptFontBitMap);
}
3.3.3 位图获取函数GetFontBitMap
int GetFontBitMap(unsigned int dwCode, PFontBitMap ptFontBitMap)
{
return g_ptDefaulFontOpr->GetFontBitMap(dwCode, ptFontBitMap);
}
3.4 字库测试test.c
void lcd_put_ascii(int x, int y, unsigned char c)
{
unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16];
int i, b;
unsigned char byte;
for (i = 0; i < 16; i++)
{
byte = dots[i];
for (b = 7; b >= 0; b--)
{
if (byte & (1<<b))
{
/* show */
PutPixel(x+7-b, y+i, 0xffffff); /* 白 */
}
else
{
/* hide */
PutPixel(x+7-b, y+i, 0); /* 黑 */
}
}
}
}
int main(int argc, char **argv)
{
PDispBuff ptBuffer;
int error;
FontBitMap tFontBitMap;
char *str= "www.100ask.net";
int i = 0;
int lcd_x;
int lcd_y;
int font_size;
if (argc != 5)
{
printf("Usage: %s <font_file> <lcd_x> <lcd_y> <font_size>\n", argv[0]); /*打印用法*/
return -1;
}
lcd_x = strtol(argv[2], NULL, 0); /*首个字符x坐标*/
lcd_y = strtol(argv[3], NULL, 0); /*首个字符y坐标*/
font_size = strtol(argv[4], NULL, 0); /*字符大小*/
DisplayInit(); /*显示系统初始化*/
SelectDefaultDisplay("fb"); /*选择lcd显示设备*/
InitDefaultDisplay(); /*lcd显示设备初始化*/
ptBuffer = GetDisplayBuffer(); /*获取内存空间*/
FontsRegister(); /*注册字体*/
error = SelectAndInitFont("freetype", argv[1]); /*选择字体*/
if (error)
{
printf("SelectAndInitFont err\n");
return -1;
}
SetFontSize(font_size); /*字符大小*/
while (str[i]) /*获取每个字符的位图并显示在lcd上*/
{
/* get bitmap */
tFontBitMap.iCurOriginX = lcd_x; /*首个字符x坐标*/
tFontBitMap.iCurOriginY = lcd_y; /*首个字符y坐标*/
error = GetFontBitMap(str[i], &tFontBitMap); /*获取字符位图*/
if (error)
{
printf("SelectAndInitFont err\n");
return -1;
}
DrawFontBitMap(&tFontBitMap, 0xff0000); /*绘制位图*/
FlushDisplayRegion(&tFontBitMap.tRegion, ptBuffer); /*刷新至lcd*/
lcd_x = tFontBitMap.iNextOriginX; /*下个字符x坐标*/
lcd_y = tFontBitMap.iNextOriginY; /*下个字符y坐标*/
i++;
}
return 0;
}
边栏推荐
- MSRA提出学习实例和分布式视觉表示的极端掩蔽模型ExtreMA
- 网管日记:故障网络交换机快速替换方法
- 网络信息安全运营方法论 (上)
- 1008 数组元素循环右移问题 (20 分)
- [Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers
- 伪RTOS-ProroThread在CH573芯片上的移植
- LeetCode刷题之第61题
- 吞吐?带宽?傻傻分不清楚
- ECCV2022 | RU & Google propose zero-shot object detection with CLIP!
- 网工必用神器:网络排查工具MTR
猜你喜欢
A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.
PoE视频监控解决方案
全尺度表示的上下文非局部对齐
记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!
LeetCode刷题之第701题
基于STM32F407的一个温度传感器报警系统(用的是DS18B20温度传感器,4针0.96寸OLED显示屏,并且附带日期显示)
Thread handler句柄 IntentServvice handlerThread
【Kaggle项目实战记录】一个图片分类项目的步骤和思路分享——以树叶分类为例(用Pytorch)
Tensorflow steps on the pit notes and records various errors and solutions
哥廷根大学提出CLIPSeg,能同时作三个分割任务的模型
随机推荐
Jupyter notebook选择不同的Anaconda环境作为内核运行
【Pytorch学习笔记】11.取Dataset的子集、给Dataset打乱顺序的方法(使用Subset、random_split)
沁恒MCU从EVT中提取文件建立MounRiver独立工程
基于STM32F407的一个温度传感器报警系统(用的是DS18B20温度传感器,4针0.96寸OLED显示屏,并且附带日期显示)
Machine Learning (1) - Machine Learning Fundamentals
基于STM32F407的WIFI通信(使用的是ESP8266模块)
LeetCode刷题之第530题
LeetCode刷题之第701题
1004 成绩排名 (20 分)
六、请求处理—获取请求参数系列注解是怎样工作的?
Polygon计算每一个角的角度
SharedPreferences and SQlite database
物联网-广域网技术之NB-IoT
网络信息安全运营方法论 (上)
ECCV2022 | RU & Google propose zero-shot object detection with CLIP!
Kubernetes常备技能
Detailed explanation of BroadCast Receiver (broadcast)
OSPF故障排除办法
全尺度表示的上下文非局部对齐
深度学习系列(一)简介、线性回归与成本函数