字库font使用

本文介绍了在Windows和Linux操作系统中字体文件的存放位置,并提供了字体库文件libfont.a的下载链接,以及相关头文件font.h和源代码main.c的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 file
   int              fontstart;         // offset of start of font

   int numGlyphs;                     // number of glyphs, needed for range checking

   int loca,head,glyf,hhea,hmtx,kern; // table locations as offset from start of .ttf
   int index_map;                     // a cmap mapping for our chosen character encoding
   int 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-72
    fontSetSize(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);
    
    // 关闭bitmap
    destroyBitmap(bm);
    // 关闭字体库,不需要频繁关闭
    //fontUnload(f);

    //解除映射
    munmap(lcd,800*480*4);
    return 0;
}
//arm-linux-gcc main.c -o main -L./ -lm -lfont

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值