windows字体位置 C:\Windows\Fonts
linux字体位置 /usr/share/fonts/DroidSansFallback.ttf
库文件libfont.a
链接:https://pan.baidu.com/s/1BYamH8s_v54IlSrvQe21hg?pwd=mft7
提取码:mft7
font.h
#ifndef __font_h__
#define __font_h__#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include<unistd.h>#define color u32
#define getColor(a, b, c, d) (a|b<<8|c<<16|d<<24)typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;typedef char s8;
typedef short s16;
typedef int s32;
typedef long long s64;typedef struct stbtt_fontinfo
{void * userdata;unsigned char * data; // pointer to .ttf fileint fontstart; // offset of start of fontint numGlyphs; // number of glyphs, needed for range checkingint loca,head,glyf,hhea,hmtx,kern; // table locations as offset from start of .ttfint index_map; // a cmap mapping for our chosen character encodingint indexToLocFormat; // format needed to map from glyph index to glyph
} stbtt_fontinfo;typedef struct{u32 height;u32 width;u32 byteperpixel;u8 *map;
}bitmap;typedef struct{stbtt_fontinfo *info;u8 *buffer;float scale;
}font;//lcd设备结构体
struct LcdDevice
{int fd;unsigned int *mp; //保存映射首地址};//1.初始化字库
font *fontLoad(char *fontPath);//2.设置字体的大小
void fontSetSize(font *f, s32 pixels);//3.设置字体输出框的大小//byteperpixel 4
bitmap *createBitmap(u32 width, u32 height, u32 byteperpixel);//可以指定输出框的颜色
bitmap *createBitmapWithInit(u32 width, u32 height, u32 byteperpixel, color c);//4.把字体输出到输出框中 //maxWidth 0 任意长,宽度就是画布宽度减去起始位置
void fontPrint(font *f, bitmap *screen, s32 x, s32 y, char *text, color c, s32 maxWidth);//5.把输出框的所有信息显示到LCD屏幕中
void show_font_to_lcd(unsigned int *p,int px,int py,bitmap *bm);// 关闭字体库
void fontUnload(font *f);// 关闭bitmap
void destroyBitmap(bitmap *bm);#endif
main.c
#include "font.h"int (*lcd)[800];
//映射
void init_lcd()
{int fd = open("/dev/fb0",O_RDWR);if(fd<0){puts("open lcd fail");return;}lcd = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);if(lcd!=MAP_FAILED)puts("映射成功");close(fd);
}
//清屏
void clear(int (*lcd)[800],int color)
{for(int y=0;y<480;y++){for(int x=0;x<800;x++){lcd[y][x] = color;}}
}
int main()
{ //初始化LCD init_lcd();clear(lcd,0);//1.加载字库font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");if(f == NULL)puts("加载失败");//2.设置字体的大小 8-72fontSetSize(f,72);//3.设置字体输出框的大小bitmap *bm = createBitmap(200,100, 4);//4.把字体输出到输出框中fontPrint(f,bm, 10,10, "hello",0xff000000, 0);//颜色RGBA//不定长超过画布则不显示//5.把输出框的所有信息显示到LCD屏幕中 show_font_to_lcd((unsigned int *)lcd,0,0,bm);// 关闭bitmapdestroyBitmap(bm);// 关闭字体库,不需要频繁关闭//fontUnload(f);//解除映射munmap(lcd,800*480*4);return 0;
}
//arm-linux-gcc main.c -o main -L./ -lm -lfont